sysfs.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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 *)&F2FS_OPTION(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_encrypt(sb))
  84. len += snprintf(buf, PAGE_SIZE - len, "%s",
  85. "encryption");
  86. if (f2fs_sb_has_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. if (f2fs_sb_has_quota_ino(sb))
  102. len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
  103. len ? ", " : "", "quota_ino");
  104. if (f2fs_sb_has_inode_crtime(sb))
  105. len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
  106. len ? ", " : "", "inode_crtime");
  107. if (f2fs_sb_has_lost_found(sb))
  108. len += snprintf(buf + len, PAGE_SIZE - len, "%s%s",
  109. len ? ", " : "", "lost_found");
  110. len += snprintf(buf + len, PAGE_SIZE - len, "\n");
  111. return len;
  112. }
  113. static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
  114. struct f2fs_sb_info *sbi, char *buf)
  115. {
  116. return snprintf(buf, PAGE_SIZE, "%u\n", sbi->current_reserved_blocks);
  117. }
  118. static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  119. struct f2fs_sb_info *sbi, char *buf)
  120. {
  121. unsigned char *ptr = NULL;
  122. unsigned int *ui;
  123. ptr = __struct_ptr(sbi, a->struct_type);
  124. if (!ptr)
  125. return -EINVAL;
  126. if (!strcmp(a->attr.name, "extension_list")) {
  127. __u8 (*extlist)[F2FS_EXTENSION_LEN] =
  128. sbi->raw_super->extension_list;
  129. int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
  130. int hot_count = sbi->raw_super->hot_ext_count;
  131. int len = 0, i;
  132. len += snprintf(buf + len, PAGE_SIZE - len,
  133. "cold file extension:\n");
  134. for (i = 0; i < cold_count; i++)
  135. len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
  136. extlist[i]);
  137. len += snprintf(buf + len, PAGE_SIZE - len,
  138. "hot file extension:\n");
  139. for (i = cold_count; i < cold_count + hot_count; i++)
  140. len += snprintf(buf + len, PAGE_SIZE - len, "%s\n",
  141. extlist[i]);
  142. return len;
  143. }
  144. ui = (unsigned int *)(ptr + a->offset);
  145. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  146. }
  147. static ssize_t __sbi_store(struct f2fs_attr *a,
  148. struct f2fs_sb_info *sbi,
  149. const char *buf, size_t count)
  150. {
  151. unsigned char *ptr;
  152. unsigned long t;
  153. unsigned int *ui;
  154. ssize_t ret;
  155. ptr = __struct_ptr(sbi, a->struct_type);
  156. if (!ptr)
  157. return -EINVAL;
  158. if (!strcmp(a->attr.name, "extension_list")) {
  159. const char *name = strim((char *)buf);
  160. bool set = true, hot;
  161. if (!strncmp(name, "[h]", 3))
  162. hot = true;
  163. else if (!strncmp(name, "[c]", 3))
  164. hot = false;
  165. else
  166. return -EINVAL;
  167. name += 3;
  168. if (*name == '!') {
  169. name++;
  170. set = false;
  171. }
  172. if (strlen(name) >= F2FS_EXTENSION_LEN)
  173. return -EINVAL;
  174. down_write(&sbi->sb_lock);
  175. ret = f2fs_update_extension_list(sbi, name, hot, set);
  176. if (ret)
  177. goto out;
  178. ret = f2fs_commit_super(sbi, false);
  179. if (ret)
  180. f2fs_update_extension_list(sbi, name, hot, !set);
  181. out:
  182. up_write(&sbi->sb_lock);
  183. return ret ? ret : count;
  184. }
  185. ui = (unsigned int *)(ptr + a->offset);
  186. ret = kstrtoul(skip_spaces(buf), 0, &t);
  187. if (ret < 0)
  188. return ret;
  189. #ifdef CONFIG_F2FS_FAULT_INJECTION
  190. if (a->struct_type == FAULT_INFO_TYPE && t >= (1 << FAULT_MAX))
  191. return -EINVAL;
  192. #endif
  193. if (a->struct_type == RESERVED_BLOCKS) {
  194. spin_lock(&sbi->stat_lock);
  195. if (t > (unsigned long)(sbi->user_block_count -
  196. F2FS_OPTION(sbi).root_reserved_blocks)) {
  197. spin_unlock(&sbi->stat_lock);
  198. return -EINVAL;
  199. }
  200. *ui = t;
  201. sbi->current_reserved_blocks = min(sbi->reserved_blocks,
  202. sbi->user_block_count - valid_user_blocks(sbi));
  203. spin_unlock(&sbi->stat_lock);
  204. return count;
  205. }
  206. if (!strcmp(a->attr.name, "discard_granularity")) {
  207. if (t == 0 || t > MAX_PLIST_NUM)
  208. return -EINVAL;
  209. if (t == *ui)
  210. return count;
  211. *ui = t;
  212. return count;
  213. }
  214. if (!strcmp(a->attr.name, "trim_sections"))
  215. return -EINVAL;
  216. if (!strcmp(a->attr.name, "gc_urgent")) {
  217. if (t >= 1) {
  218. sbi->gc_mode = GC_URGENT;
  219. if (sbi->gc_thread) {
  220. wake_up_interruptible_all(
  221. &sbi->gc_thread->gc_wait_queue_head);
  222. wake_up_discard_thread(sbi, true);
  223. }
  224. } else {
  225. sbi->gc_mode = GC_NORMAL;
  226. }
  227. return count;
  228. }
  229. if (!strcmp(a->attr.name, "gc_idle")) {
  230. if (t == GC_IDLE_CB)
  231. sbi->gc_mode = GC_IDLE_CB;
  232. else if (t == GC_IDLE_GREEDY)
  233. sbi->gc_mode = GC_IDLE_GREEDY;
  234. else
  235. sbi->gc_mode = GC_NORMAL;
  236. return count;
  237. }
  238. *ui = t;
  239. if (!strcmp(a->attr.name, "iostat_enable") && *ui == 0)
  240. f2fs_reset_iostat(sbi);
  241. return count;
  242. }
  243. static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
  244. struct f2fs_sb_info *sbi,
  245. const char *buf, size_t count)
  246. {
  247. ssize_t ret;
  248. bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
  249. a->struct_type == GC_THREAD);
  250. if (gc_entry)
  251. down_read(&sbi->sb->s_umount);
  252. ret = __sbi_store(a, sbi, buf, count);
  253. if (gc_entry)
  254. up_read(&sbi->sb->s_umount);
  255. return ret;
  256. }
  257. static ssize_t f2fs_attr_show(struct kobject *kobj,
  258. struct attribute *attr, char *buf)
  259. {
  260. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  261. s_kobj);
  262. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  263. return a->show ? a->show(a, sbi, buf) : 0;
  264. }
  265. static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
  266. const char *buf, size_t len)
  267. {
  268. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  269. s_kobj);
  270. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  271. return a->store ? a->store(a, sbi, buf, len) : 0;
  272. }
  273. static void f2fs_sb_release(struct kobject *kobj)
  274. {
  275. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  276. s_kobj);
  277. complete(&sbi->s_kobj_unregister);
  278. }
  279. enum feat_id {
  280. FEAT_CRYPTO = 0,
  281. FEAT_BLKZONED,
  282. FEAT_ATOMIC_WRITE,
  283. FEAT_EXTRA_ATTR,
  284. FEAT_PROJECT_QUOTA,
  285. FEAT_INODE_CHECKSUM,
  286. FEAT_FLEXIBLE_INLINE_XATTR,
  287. FEAT_QUOTA_INO,
  288. FEAT_INODE_CRTIME,
  289. FEAT_LOST_FOUND,
  290. };
  291. static ssize_t f2fs_feature_show(struct f2fs_attr *a,
  292. struct f2fs_sb_info *sbi, char *buf)
  293. {
  294. switch (a->id) {
  295. case FEAT_CRYPTO:
  296. case FEAT_BLKZONED:
  297. case FEAT_ATOMIC_WRITE:
  298. case FEAT_EXTRA_ATTR:
  299. case FEAT_PROJECT_QUOTA:
  300. case FEAT_INODE_CHECKSUM:
  301. case FEAT_FLEXIBLE_INLINE_XATTR:
  302. case FEAT_QUOTA_INO:
  303. case FEAT_INODE_CRTIME:
  304. case FEAT_LOST_FOUND:
  305. return snprintf(buf, PAGE_SIZE, "supported\n");
  306. }
  307. return 0;
  308. }
  309. #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
  310. static struct f2fs_attr f2fs_attr_##_name = { \
  311. .attr = {.name = __stringify(_name), .mode = _mode }, \
  312. .show = _show, \
  313. .store = _store, \
  314. .struct_type = _struct_type, \
  315. .offset = _offset \
  316. }
  317. #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
  318. F2FS_ATTR_OFFSET(struct_type, name, 0644, \
  319. f2fs_sbi_show, f2fs_sbi_store, \
  320. offsetof(struct struct_name, elname))
  321. #define F2FS_GENERAL_RO_ATTR(name) \
  322. static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
  323. #define F2FS_FEATURE_RO_ATTR(_name, _id) \
  324. static struct f2fs_attr f2fs_attr_##_name = { \
  325. .attr = {.name = __stringify(_name), .mode = 0444 }, \
  326. .show = f2fs_feature_show, \
  327. .id = _id, \
  328. }
  329. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_urgent_sleep_time,
  330. urgent_sleep_time);
  331. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_min_sleep_time, min_sleep_time);
  332. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_max_sleep_time, max_sleep_time);
  333. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, gc_no_gc_sleep_time, no_gc_sleep_time);
  334. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_idle, gc_mode);
  335. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_urgent, gc_mode);
  336. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, reclaim_segments, rec_prefree_segments);
  337. F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, max_small_discards, max_discards);
  338. F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, discard_granularity, discard_granularity);
  339. F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, reserved_blocks, reserved_blocks);
  340. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, batched_trim_sections, trim_sections);
  341. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, ipu_policy, ipu_policy);
  342. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ipu_util, min_ipu_util);
  343. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_fsync_blocks, min_fsync_blocks);
  344. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_hot_blocks, min_hot_blocks);
  345. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, min_ssr_sections, min_ssr_sections);
  346. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ram_thresh, ram_thresh);
  347. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, ra_nid_pages, ra_nid_pages);
  348. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, dirty_nats_ratio, dirty_nats_ratio);
  349. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, max_victim_search, max_victim_search);
  350. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, dir_level, dir_level);
  351. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, cp_interval, interval_time[CP_TIME]);
  352. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, idle_interval, interval_time[REQ_TIME]);
  353. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, iostat_enable, iostat_enable);
  354. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, readdir_ra, readdir_ra);
  355. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, gc_pin_file_thresh, gc_pin_file_threshold);
  356. F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
  357. #ifdef CONFIG_F2FS_FAULT_INJECTION
  358. F2FS_RW_ATTR(FAULT_INFO_RATE, f2fs_fault_info, inject_rate, inject_rate);
  359. F2FS_RW_ATTR(FAULT_INFO_TYPE, f2fs_fault_info, inject_type, inject_type);
  360. #endif
  361. F2FS_GENERAL_RO_ATTR(dirty_segments);
  362. F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
  363. F2FS_GENERAL_RO_ATTR(features);
  364. F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
  365. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  366. F2FS_FEATURE_RO_ATTR(encryption, FEAT_CRYPTO);
  367. #endif
  368. #ifdef CONFIG_BLK_DEV_ZONED
  369. F2FS_FEATURE_RO_ATTR(block_zoned, FEAT_BLKZONED);
  370. #endif
  371. F2FS_FEATURE_RO_ATTR(atomic_write, FEAT_ATOMIC_WRITE);
  372. F2FS_FEATURE_RO_ATTR(extra_attr, FEAT_EXTRA_ATTR);
  373. F2FS_FEATURE_RO_ATTR(project_quota, FEAT_PROJECT_QUOTA);
  374. F2FS_FEATURE_RO_ATTR(inode_checksum, FEAT_INODE_CHECKSUM);
  375. F2FS_FEATURE_RO_ATTR(flexible_inline_xattr, FEAT_FLEXIBLE_INLINE_XATTR);
  376. F2FS_FEATURE_RO_ATTR(quota_ino, FEAT_QUOTA_INO);
  377. F2FS_FEATURE_RO_ATTR(inode_crtime, FEAT_INODE_CRTIME);
  378. F2FS_FEATURE_RO_ATTR(lost_found, FEAT_LOST_FOUND);
  379. #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
  380. static struct attribute *f2fs_attrs[] = {
  381. ATTR_LIST(gc_urgent_sleep_time),
  382. ATTR_LIST(gc_min_sleep_time),
  383. ATTR_LIST(gc_max_sleep_time),
  384. ATTR_LIST(gc_no_gc_sleep_time),
  385. ATTR_LIST(gc_idle),
  386. ATTR_LIST(gc_urgent),
  387. ATTR_LIST(reclaim_segments),
  388. ATTR_LIST(max_small_discards),
  389. ATTR_LIST(discard_granularity),
  390. ATTR_LIST(batched_trim_sections),
  391. ATTR_LIST(ipu_policy),
  392. ATTR_LIST(min_ipu_util),
  393. ATTR_LIST(min_fsync_blocks),
  394. ATTR_LIST(min_hot_blocks),
  395. ATTR_LIST(min_ssr_sections),
  396. ATTR_LIST(max_victim_search),
  397. ATTR_LIST(dir_level),
  398. ATTR_LIST(ram_thresh),
  399. ATTR_LIST(ra_nid_pages),
  400. ATTR_LIST(dirty_nats_ratio),
  401. ATTR_LIST(cp_interval),
  402. ATTR_LIST(idle_interval),
  403. ATTR_LIST(iostat_enable),
  404. ATTR_LIST(readdir_ra),
  405. ATTR_LIST(gc_pin_file_thresh),
  406. ATTR_LIST(extension_list),
  407. #ifdef CONFIG_F2FS_FAULT_INJECTION
  408. ATTR_LIST(inject_rate),
  409. ATTR_LIST(inject_type),
  410. #endif
  411. ATTR_LIST(dirty_segments),
  412. ATTR_LIST(lifetime_write_kbytes),
  413. ATTR_LIST(features),
  414. ATTR_LIST(reserved_blocks),
  415. ATTR_LIST(current_reserved_blocks),
  416. NULL,
  417. };
  418. static struct attribute *f2fs_feat_attrs[] = {
  419. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  420. ATTR_LIST(encryption),
  421. #endif
  422. #ifdef CONFIG_BLK_DEV_ZONED
  423. ATTR_LIST(block_zoned),
  424. #endif
  425. ATTR_LIST(atomic_write),
  426. ATTR_LIST(extra_attr),
  427. ATTR_LIST(project_quota),
  428. ATTR_LIST(inode_checksum),
  429. ATTR_LIST(flexible_inline_xattr),
  430. ATTR_LIST(quota_ino),
  431. ATTR_LIST(inode_crtime),
  432. ATTR_LIST(lost_found),
  433. NULL,
  434. };
  435. static const struct sysfs_ops f2fs_attr_ops = {
  436. .show = f2fs_attr_show,
  437. .store = f2fs_attr_store,
  438. };
  439. static struct kobj_type f2fs_sb_ktype = {
  440. .default_attrs = f2fs_attrs,
  441. .sysfs_ops = &f2fs_attr_ops,
  442. .release = f2fs_sb_release,
  443. };
  444. static struct kobj_type f2fs_ktype = {
  445. .sysfs_ops = &f2fs_attr_ops,
  446. };
  447. static struct kset f2fs_kset = {
  448. .kobj = {.ktype = &f2fs_ktype},
  449. };
  450. static struct kobj_type f2fs_feat_ktype = {
  451. .default_attrs = f2fs_feat_attrs,
  452. .sysfs_ops = &f2fs_attr_ops,
  453. };
  454. static struct kobject f2fs_feat = {
  455. .kset = &f2fs_kset,
  456. };
  457. static int segment_info_seq_show(struct seq_file *seq, void *offset)
  458. {
  459. struct super_block *sb = seq->private;
  460. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  461. unsigned int total_segs =
  462. le32_to_cpu(sbi->raw_super->segment_count_main);
  463. int i;
  464. seq_puts(seq, "format: segment_type|valid_blocks\n"
  465. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  466. for (i = 0; i < total_segs; i++) {
  467. struct seg_entry *se = get_seg_entry(sbi, i);
  468. if ((i % 10) == 0)
  469. seq_printf(seq, "%-10d", i);
  470. seq_printf(seq, "%d|%-3u", se->type,
  471. get_valid_blocks(sbi, i, false));
  472. if ((i % 10) == 9 || i == (total_segs - 1))
  473. seq_putc(seq, '\n');
  474. else
  475. seq_putc(seq, ' ');
  476. }
  477. return 0;
  478. }
  479. static int segment_bits_seq_show(struct seq_file *seq, void *offset)
  480. {
  481. struct super_block *sb = seq->private;
  482. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  483. unsigned int total_segs =
  484. le32_to_cpu(sbi->raw_super->segment_count_main);
  485. int i, j;
  486. seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
  487. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  488. for (i = 0; i < total_segs; i++) {
  489. struct seg_entry *se = get_seg_entry(sbi, i);
  490. seq_printf(seq, "%-10d", i);
  491. seq_printf(seq, "%d|%-3u|", se->type,
  492. get_valid_blocks(sbi, i, false));
  493. for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
  494. seq_printf(seq, " %.2x", se->cur_valid_map[j]);
  495. seq_putc(seq, '\n');
  496. }
  497. return 0;
  498. }
  499. static int iostat_info_seq_show(struct seq_file *seq, void *offset)
  500. {
  501. struct super_block *sb = seq->private;
  502. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  503. time64_t now = ktime_get_real_seconds();
  504. if (!sbi->iostat_enable)
  505. return 0;
  506. seq_printf(seq, "time: %-16llu\n", now);
  507. /* print app IOs */
  508. seq_printf(seq, "app buffered: %-16llu\n",
  509. sbi->write_iostat[APP_BUFFERED_IO]);
  510. seq_printf(seq, "app direct: %-16llu\n",
  511. sbi->write_iostat[APP_DIRECT_IO]);
  512. seq_printf(seq, "app mapped: %-16llu\n",
  513. sbi->write_iostat[APP_MAPPED_IO]);
  514. /* print fs IOs */
  515. seq_printf(seq, "fs data: %-16llu\n",
  516. sbi->write_iostat[FS_DATA_IO]);
  517. seq_printf(seq, "fs node: %-16llu\n",
  518. sbi->write_iostat[FS_NODE_IO]);
  519. seq_printf(seq, "fs meta: %-16llu\n",
  520. sbi->write_iostat[FS_META_IO]);
  521. seq_printf(seq, "fs gc data: %-16llu\n",
  522. sbi->write_iostat[FS_GC_DATA_IO]);
  523. seq_printf(seq, "fs gc node: %-16llu\n",
  524. sbi->write_iostat[FS_GC_NODE_IO]);
  525. seq_printf(seq, "fs cp data: %-16llu\n",
  526. sbi->write_iostat[FS_CP_DATA_IO]);
  527. seq_printf(seq, "fs cp node: %-16llu\n",
  528. sbi->write_iostat[FS_CP_NODE_IO]);
  529. seq_printf(seq, "fs cp meta: %-16llu\n",
  530. sbi->write_iostat[FS_CP_META_IO]);
  531. seq_printf(seq, "fs discard: %-16llu\n",
  532. sbi->write_iostat[FS_DISCARD]);
  533. return 0;
  534. }
  535. int __init f2fs_init_sysfs(void)
  536. {
  537. int ret;
  538. kobject_set_name(&f2fs_kset.kobj, "f2fs");
  539. f2fs_kset.kobj.parent = fs_kobj;
  540. ret = kset_register(&f2fs_kset);
  541. if (ret)
  542. return ret;
  543. ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
  544. NULL, "features");
  545. if (ret)
  546. kset_unregister(&f2fs_kset);
  547. else
  548. f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
  549. return ret;
  550. }
  551. void f2fs_exit_sysfs(void)
  552. {
  553. kobject_put(&f2fs_feat);
  554. kset_unregister(&f2fs_kset);
  555. remove_proc_entry("fs/f2fs", NULL);
  556. f2fs_proc_root = NULL;
  557. }
  558. int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
  559. {
  560. struct super_block *sb = sbi->sb;
  561. int err;
  562. sbi->s_kobj.kset = &f2fs_kset;
  563. init_completion(&sbi->s_kobj_unregister);
  564. err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
  565. "%s", sb->s_id);
  566. if (err)
  567. return err;
  568. if (f2fs_proc_root)
  569. sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
  570. if (sbi->s_proc) {
  571. proc_create_single_data("segment_info", S_IRUGO, sbi->s_proc,
  572. segment_info_seq_show, sb);
  573. proc_create_single_data("segment_bits", S_IRUGO, sbi->s_proc,
  574. segment_bits_seq_show, sb);
  575. proc_create_single_data("iostat_info", S_IRUGO, sbi->s_proc,
  576. iostat_info_seq_show, sb);
  577. }
  578. return 0;
  579. }
  580. void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
  581. {
  582. if (sbi->s_proc) {
  583. remove_proc_entry("iostat_info", sbi->s_proc);
  584. remove_proc_entry("segment_info", sbi->s_proc);
  585. remove_proc_entry("segment_bits", sbi->s_proc);
  586. remove_proc_entry(sbi->sb->s_id, f2fs_proc_root);
  587. }
  588. kobject_del(&sbi->s_kobj);
  589. }