sysfs.c 20 KB

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