sysfs.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * f2fs sysfs interface
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. * Copyright (c) 2017 Chao Yu <chao@kernel.org>
  8. */
  9. #include <linux/compiler.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/f2fs_fs.h>
  12. #include <linux/seq_file.h>
  13. #include <linux/unicode.h>
  14. #include <linux/ioprio.h>
  15. #include <linux/sysfs.h>
  16. #include "f2fs.h"
  17. #include "segment.h"
  18. #include "gc.h"
  19. #include "iostat.h"
  20. #include <trace/events/f2fs.h>
  21. static struct proc_dir_entry *f2fs_proc_root;
  22. /* Sysfs support for f2fs */
  23. enum {
  24. GC_THREAD, /* struct f2fs_gc_thread */
  25. SM_INFO, /* struct f2fs_sm_info */
  26. DCC_INFO, /* struct discard_cmd_control */
  27. NM_INFO, /* struct f2fs_nm_info */
  28. F2FS_SBI, /* struct f2fs_sb_info */
  29. #ifdef CONFIG_F2FS_STAT_FS
  30. STAT_INFO, /* struct f2fs_stat_info */
  31. #endif
  32. #ifdef CONFIG_F2FS_FAULT_INJECTION
  33. FAULT_INFO_RATE, /* struct f2fs_fault_info */
  34. FAULT_INFO_TYPE, /* struct f2fs_fault_info */
  35. #endif
  36. RESERVED_BLOCKS, /* struct f2fs_sb_info */
  37. CPRC_INFO, /* struct ckpt_req_control */
  38. ATGC_INFO, /* struct atgc_management */
  39. };
  40. static const char *gc_mode_names[MAX_GC_MODE] = {
  41. "GC_NORMAL",
  42. "GC_IDLE_CB",
  43. "GC_IDLE_GREEDY",
  44. "GC_IDLE_AT",
  45. "GC_URGENT_HIGH",
  46. "GC_URGENT_LOW",
  47. "GC_URGENT_MID"
  48. };
  49. struct f2fs_attr {
  50. struct attribute attr;
  51. ssize_t (*show)(struct f2fs_attr *a, struct f2fs_sb_info *sbi, char *buf);
  52. ssize_t (*store)(struct f2fs_attr *a, struct f2fs_sb_info *sbi,
  53. const char *buf, size_t len);
  54. int struct_type;
  55. int offset;
  56. int id;
  57. };
  58. struct f2fs_base_attr {
  59. struct attribute attr;
  60. ssize_t (*show)(struct f2fs_base_attr *a, char *buf);
  61. ssize_t (*store)(struct f2fs_base_attr *a, const char *buf, size_t len);
  62. };
  63. static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  64. struct f2fs_sb_info *sbi, char *buf);
  65. static unsigned char *__struct_ptr(struct f2fs_sb_info *sbi, int struct_type)
  66. {
  67. if (struct_type == GC_THREAD)
  68. return (unsigned char *)sbi->gc_thread;
  69. else if (struct_type == SM_INFO)
  70. return (unsigned char *)SM_I(sbi);
  71. else if (struct_type == DCC_INFO)
  72. return (unsigned char *)SM_I(sbi)->dcc_info;
  73. else if (struct_type == NM_INFO)
  74. return (unsigned char *)NM_I(sbi);
  75. else if (struct_type == F2FS_SBI || struct_type == RESERVED_BLOCKS)
  76. return (unsigned char *)sbi;
  77. #ifdef CONFIG_F2FS_FAULT_INJECTION
  78. else if (struct_type == FAULT_INFO_RATE ||
  79. struct_type == FAULT_INFO_TYPE)
  80. return (unsigned char *)&F2FS_OPTION(sbi).fault_info;
  81. #endif
  82. #ifdef CONFIG_F2FS_STAT_FS
  83. else if (struct_type == STAT_INFO)
  84. return (unsigned char *)F2FS_STAT(sbi);
  85. #endif
  86. else if (struct_type == CPRC_INFO)
  87. return (unsigned char *)&sbi->cprc_info;
  88. else if (struct_type == ATGC_INFO)
  89. return (unsigned char *)&sbi->am;
  90. return NULL;
  91. }
  92. static ssize_t dirty_segments_show(struct f2fs_attr *a,
  93. struct f2fs_sb_info *sbi, char *buf)
  94. {
  95. return sysfs_emit(buf, "%llu\n",
  96. (unsigned long long)(dirty_segments(sbi)));
  97. }
  98. static ssize_t free_segments_show(struct f2fs_attr *a,
  99. struct f2fs_sb_info *sbi, char *buf)
  100. {
  101. return sysfs_emit(buf, "%llu\n",
  102. (unsigned long long)(free_segments(sbi)));
  103. }
  104. static ssize_t ovp_segments_show(struct f2fs_attr *a,
  105. struct f2fs_sb_info *sbi, char *buf)
  106. {
  107. return sysfs_emit(buf, "%llu\n",
  108. (unsigned long long)(overprovision_segments(sbi)));
  109. }
  110. static ssize_t lifetime_write_kbytes_show(struct f2fs_attr *a,
  111. struct f2fs_sb_info *sbi, char *buf)
  112. {
  113. return sysfs_emit(buf, "%llu\n",
  114. (unsigned long long)(sbi->kbytes_written +
  115. ((f2fs_get_sectors_written(sbi) -
  116. sbi->sectors_written_start) >> 1)));
  117. }
  118. static ssize_t sb_status_show(struct f2fs_attr *a,
  119. struct f2fs_sb_info *sbi, char *buf)
  120. {
  121. return sysfs_emit(buf, "%lx\n", sbi->s_flag);
  122. }
  123. static ssize_t cp_status_show(struct f2fs_attr *a,
  124. struct f2fs_sb_info *sbi, char *buf)
  125. {
  126. return sysfs_emit(buf, "%x\n", le32_to_cpu(F2FS_CKPT(sbi)->ckpt_flags));
  127. }
  128. static ssize_t pending_discard_show(struct f2fs_attr *a,
  129. struct f2fs_sb_info *sbi, char *buf)
  130. {
  131. if (!SM_I(sbi)->dcc_info)
  132. return -EINVAL;
  133. return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
  134. &SM_I(sbi)->dcc_info->discard_cmd_cnt));
  135. }
  136. static ssize_t issued_discard_show(struct f2fs_attr *a,
  137. struct f2fs_sb_info *sbi, char *buf)
  138. {
  139. if (!SM_I(sbi)->dcc_info)
  140. return -EINVAL;
  141. return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
  142. &SM_I(sbi)->dcc_info->issued_discard));
  143. }
  144. static ssize_t queued_discard_show(struct f2fs_attr *a,
  145. struct f2fs_sb_info *sbi, char *buf)
  146. {
  147. if (!SM_I(sbi)->dcc_info)
  148. return -EINVAL;
  149. return sysfs_emit(buf, "%llu\n", (unsigned long long)atomic_read(
  150. &SM_I(sbi)->dcc_info->queued_discard));
  151. }
  152. static ssize_t undiscard_blks_show(struct f2fs_attr *a,
  153. struct f2fs_sb_info *sbi, char *buf)
  154. {
  155. if (!SM_I(sbi)->dcc_info)
  156. return -EINVAL;
  157. return sysfs_emit(buf, "%u\n",
  158. SM_I(sbi)->dcc_info->undiscard_blks);
  159. }
  160. static ssize_t atgc_enabled_show(struct f2fs_attr *a,
  161. struct f2fs_sb_info *sbi, char *buf)
  162. {
  163. return sysfs_emit(buf, "%d\n", sbi->am.atgc_enabled ? 1 : 0);
  164. }
  165. static ssize_t gc_mode_show(struct f2fs_attr *a,
  166. struct f2fs_sb_info *sbi, char *buf)
  167. {
  168. return sysfs_emit(buf, "%s\n", gc_mode_names[sbi->gc_mode]);
  169. }
  170. static ssize_t features_show(struct f2fs_attr *a,
  171. struct f2fs_sb_info *sbi, char *buf)
  172. {
  173. int len = 0;
  174. if (f2fs_sb_has_encrypt(sbi))
  175. len += sysfs_emit_at(buf, len, "%s",
  176. "encryption");
  177. if (f2fs_sb_has_blkzoned(sbi))
  178. len += sysfs_emit_at(buf, len, "%s%s",
  179. len ? ", " : "", "blkzoned");
  180. if (f2fs_sb_has_extra_attr(sbi))
  181. len += sysfs_emit_at(buf, len, "%s%s",
  182. len ? ", " : "", "extra_attr");
  183. if (f2fs_sb_has_project_quota(sbi))
  184. len += sysfs_emit_at(buf, len, "%s%s",
  185. len ? ", " : "", "projquota");
  186. if (f2fs_sb_has_inode_chksum(sbi))
  187. len += sysfs_emit_at(buf, len, "%s%s",
  188. len ? ", " : "", "inode_checksum");
  189. if (f2fs_sb_has_flexible_inline_xattr(sbi))
  190. len += sysfs_emit_at(buf, len, "%s%s",
  191. len ? ", " : "", "flexible_inline_xattr");
  192. if (f2fs_sb_has_quota_ino(sbi))
  193. len += sysfs_emit_at(buf, len, "%s%s",
  194. len ? ", " : "", "quota_ino");
  195. if (f2fs_sb_has_inode_crtime(sbi))
  196. len += sysfs_emit_at(buf, len, "%s%s",
  197. len ? ", " : "", "inode_crtime");
  198. if (f2fs_sb_has_lost_found(sbi))
  199. len += sysfs_emit_at(buf, len, "%s%s",
  200. len ? ", " : "", "lost_found");
  201. if (f2fs_sb_has_verity(sbi))
  202. len += sysfs_emit_at(buf, len, "%s%s",
  203. len ? ", " : "", "verity");
  204. if (f2fs_sb_has_sb_chksum(sbi))
  205. len += sysfs_emit_at(buf, len, "%s%s",
  206. len ? ", " : "", "sb_checksum");
  207. if (f2fs_sb_has_casefold(sbi))
  208. len += sysfs_emit_at(buf, len, "%s%s",
  209. len ? ", " : "", "casefold");
  210. if (f2fs_sb_has_readonly(sbi))
  211. len += sysfs_emit_at(buf, len, "%s%s",
  212. len ? ", " : "", "readonly");
  213. if (f2fs_sb_has_compression(sbi))
  214. len += sysfs_emit_at(buf, len, "%s%s",
  215. len ? ", " : "", "compression");
  216. len += sysfs_emit_at(buf, len, "%s%s",
  217. len ? ", " : "", "pin_file");
  218. len += sysfs_emit_at(buf, len, "\n");
  219. return len;
  220. }
  221. static ssize_t current_reserved_blocks_show(struct f2fs_attr *a,
  222. struct f2fs_sb_info *sbi, char *buf)
  223. {
  224. return sysfs_emit(buf, "%u\n", sbi->current_reserved_blocks);
  225. }
  226. static ssize_t unusable_show(struct f2fs_attr *a,
  227. struct f2fs_sb_info *sbi, char *buf)
  228. {
  229. block_t unusable;
  230. if (test_opt(sbi, DISABLE_CHECKPOINT))
  231. unusable = sbi->unusable_block_count;
  232. else
  233. unusable = f2fs_get_unusable_blocks(sbi);
  234. return sysfs_emit(buf, "%llu\n", (unsigned long long)unusable);
  235. }
  236. static ssize_t encoding_show(struct f2fs_attr *a,
  237. struct f2fs_sb_info *sbi, char *buf)
  238. {
  239. #if IS_ENABLED(CONFIG_UNICODE)
  240. struct super_block *sb = sbi->sb;
  241. if (f2fs_sb_has_casefold(sbi))
  242. return sysfs_emit(buf, "UTF-8 (%d.%d.%d)\n",
  243. (sb->s_encoding->version >> 16) & 0xff,
  244. (sb->s_encoding->version >> 8) & 0xff,
  245. sb->s_encoding->version & 0xff);
  246. #endif
  247. return sysfs_emit(buf, "(none)\n");
  248. }
  249. static ssize_t mounted_time_sec_show(struct f2fs_attr *a,
  250. struct f2fs_sb_info *sbi, char *buf)
  251. {
  252. return sysfs_emit(buf, "%llu\n", SIT_I(sbi)->mounted_time);
  253. }
  254. #ifdef CONFIG_F2FS_STAT_FS
  255. static ssize_t moved_blocks_foreground_show(struct f2fs_attr *a,
  256. struct f2fs_sb_info *sbi, char *buf)
  257. {
  258. struct f2fs_stat_info *si = F2FS_STAT(sbi);
  259. return sysfs_emit(buf, "%llu\n",
  260. (unsigned long long)(si->tot_blks -
  261. (si->bg_data_blks + si->bg_node_blks)));
  262. }
  263. static ssize_t moved_blocks_background_show(struct f2fs_attr *a,
  264. struct f2fs_sb_info *sbi, char *buf)
  265. {
  266. struct f2fs_stat_info *si = F2FS_STAT(sbi);
  267. return sysfs_emit(buf, "%llu\n",
  268. (unsigned long long)(si->bg_data_blks + si->bg_node_blks));
  269. }
  270. static ssize_t avg_vblocks_show(struct f2fs_attr *a,
  271. struct f2fs_sb_info *sbi, char *buf)
  272. {
  273. struct f2fs_stat_info *si = F2FS_STAT(sbi);
  274. si->dirty_count = dirty_segments(sbi);
  275. f2fs_update_sit_info(sbi);
  276. return sysfs_emit(buf, "%llu\n", (unsigned long long)(si->avg_vblocks));
  277. }
  278. #endif
  279. static ssize_t main_blkaddr_show(struct f2fs_attr *a,
  280. struct f2fs_sb_info *sbi, char *buf)
  281. {
  282. return sysfs_emit(buf, "%llu\n",
  283. (unsigned long long)MAIN_BLKADDR(sbi));
  284. }
  285. static ssize_t f2fs_sbi_show(struct f2fs_attr *a,
  286. struct f2fs_sb_info *sbi, char *buf)
  287. {
  288. unsigned char *ptr = NULL;
  289. unsigned int *ui;
  290. ptr = __struct_ptr(sbi, a->struct_type);
  291. if (!ptr)
  292. return -EINVAL;
  293. if (!strcmp(a->attr.name, "extension_list")) {
  294. __u8 (*extlist)[F2FS_EXTENSION_LEN] =
  295. sbi->raw_super->extension_list;
  296. int cold_count = le32_to_cpu(sbi->raw_super->extension_count);
  297. int hot_count = sbi->raw_super->hot_ext_count;
  298. int len = 0, i;
  299. len += sysfs_emit_at(buf, len, "cold file extension:\n");
  300. for (i = 0; i < cold_count; i++)
  301. len += sysfs_emit_at(buf, len, "%s\n", extlist[i]);
  302. len += sysfs_emit_at(buf, len, "hot file extension:\n");
  303. for (i = cold_count; i < cold_count + hot_count; i++)
  304. len += sysfs_emit_at(buf, len, "%s\n", extlist[i]);
  305. return len;
  306. }
  307. if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
  308. struct ckpt_req_control *cprc = &sbi->cprc_info;
  309. int class = IOPRIO_PRIO_CLASS(cprc->ckpt_thread_ioprio);
  310. int level = IOPRIO_PRIO_LEVEL(cprc->ckpt_thread_ioprio);
  311. if (class != IOPRIO_CLASS_RT && class != IOPRIO_CLASS_BE)
  312. return -EINVAL;
  313. return sysfs_emit(buf, "%s,%d\n",
  314. class == IOPRIO_CLASS_RT ? "rt" : "be", level);
  315. }
  316. #ifdef CONFIG_F2FS_FS_COMPRESSION
  317. if (!strcmp(a->attr.name, "compr_written_block"))
  318. return sysfs_emit(buf, "%llu\n", sbi->compr_written_block);
  319. if (!strcmp(a->attr.name, "compr_saved_block"))
  320. return sysfs_emit(buf, "%llu\n", sbi->compr_saved_block);
  321. if (!strcmp(a->attr.name, "compr_new_inode"))
  322. return sysfs_emit(buf, "%u\n", sbi->compr_new_inode);
  323. #endif
  324. if (!strcmp(a->attr.name, "gc_segment_mode"))
  325. return sysfs_emit(buf, "%u\n", sbi->gc_segment_mode);
  326. if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
  327. return sysfs_emit(buf, "%u\n",
  328. sbi->gc_reclaimed_segs[sbi->gc_segment_mode]);
  329. }
  330. if (!strcmp(a->attr.name, "current_atomic_write")) {
  331. s64 current_write = atomic64_read(&sbi->current_atomic_write);
  332. return sysfs_emit(buf, "%lld\n", current_write);
  333. }
  334. if (!strcmp(a->attr.name, "peak_atomic_write"))
  335. return sysfs_emit(buf, "%lld\n", sbi->peak_atomic_write);
  336. if (!strcmp(a->attr.name, "committed_atomic_block"))
  337. return sysfs_emit(buf, "%llu\n", sbi->committed_atomic_block);
  338. if (!strcmp(a->attr.name, "revoked_atomic_block"))
  339. return sysfs_emit(buf, "%llu\n", sbi->revoked_atomic_block);
  340. #ifdef CONFIG_F2FS_STAT_FS
  341. if (!strcmp(a->attr.name, "cp_foreground_calls"))
  342. return sysfs_emit(buf, "%d\n",
  343. atomic_read(&sbi->cp_call_count[TOTAL_CALL]) -
  344. atomic_read(&sbi->cp_call_count[BACKGROUND]));
  345. if (!strcmp(a->attr.name, "cp_background_calls"))
  346. return sysfs_emit(buf, "%d\n",
  347. atomic_read(&sbi->cp_call_count[BACKGROUND]));
  348. #endif
  349. ui = (unsigned int *)(ptr + a->offset);
  350. return sysfs_emit(buf, "%u\n", *ui);
  351. }
  352. static ssize_t __sbi_store(struct f2fs_attr *a,
  353. struct f2fs_sb_info *sbi,
  354. const char *buf, size_t count)
  355. {
  356. unsigned char *ptr;
  357. unsigned long t;
  358. unsigned int *ui;
  359. ssize_t ret;
  360. ptr = __struct_ptr(sbi, a->struct_type);
  361. if (!ptr)
  362. return -EINVAL;
  363. if (!strcmp(a->attr.name, "extension_list")) {
  364. const char *name = strim((char *)buf);
  365. bool set = true, hot;
  366. if (!strncmp(name, "[h]", 3))
  367. hot = true;
  368. else if (!strncmp(name, "[c]", 3))
  369. hot = false;
  370. else
  371. return -EINVAL;
  372. name += 3;
  373. if (*name == '!') {
  374. name++;
  375. set = false;
  376. }
  377. if (!strlen(name) || strlen(name) >= F2FS_EXTENSION_LEN)
  378. return -EINVAL;
  379. f2fs_down_write(&sbi->sb_lock);
  380. ret = f2fs_update_extension_list(sbi, name, hot, set);
  381. if (ret)
  382. goto out;
  383. ret = f2fs_commit_super(sbi, false);
  384. if (ret)
  385. f2fs_update_extension_list(sbi, name, hot, !set);
  386. out:
  387. f2fs_up_write(&sbi->sb_lock);
  388. return ret ? ret : count;
  389. }
  390. if (!strcmp(a->attr.name, "ckpt_thread_ioprio")) {
  391. const char *name = strim((char *)buf);
  392. struct ckpt_req_control *cprc = &sbi->cprc_info;
  393. int class;
  394. long level;
  395. int ret;
  396. if (!strncmp(name, "rt,", 3))
  397. class = IOPRIO_CLASS_RT;
  398. else if (!strncmp(name, "be,", 3))
  399. class = IOPRIO_CLASS_BE;
  400. else
  401. return -EINVAL;
  402. name += 3;
  403. ret = kstrtol(name, 10, &level);
  404. if (ret)
  405. return ret;
  406. if (level >= IOPRIO_NR_LEVELS || level < 0)
  407. return -EINVAL;
  408. cprc->ckpt_thread_ioprio = IOPRIO_PRIO_VALUE(class, level);
  409. if (test_opt(sbi, MERGE_CHECKPOINT)) {
  410. ret = set_task_ioprio(cprc->f2fs_issue_ckpt,
  411. cprc->ckpt_thread_ioprio);
  412. if (ret)
  413. return ret;
  414. }
  415. return count;
  416. }
  417. ui = (unsigned int *)(ptr + a->offset);
  418. ret = kstrtoul(skip_spaces(buf), 0, &t);
  419. if (ret < 0)
  420. return ret;
  421. #ifdef CONFIG_F2FS_FAULT_INJECTION
  422. if (a->struct_type == FAULT_INFO_TYPE) {
  423. if (f2fs_build_fault_attr(sbi, 0, t))
  424. return -EINVAL;
  425. return count;
  426. }
  427. if (a->struct_type == FAULT_INFO_RATE) {
  428. if (f2fs_build_fault_attr(sbi, t, 0))
  429. return -EINVAL;
  430. return count;
  431. }
  432. #endif
  433. if (a->struct_type == RESERVED_BLOCKS) {
  434. spin_lock(&sbi->stat_lock);
  435. if (t > (unsigned long)(sbi->user_block_count -
  436. F2FS_OPTION(sbi).root_reserved_blocks -
  437. SEGS_TO_BLKS(sbi,
  438. SM_I(sbi)->additional_reserved_segments))) {
  439. spin_unlock(&sbi->stat_lock);
  440. return -EINVAL;
  441. }
  442. *ui = t;
  443. sbi->current_reserved_blocks = min(sbi->reserved_blocks,
  444. sbi->user_block_count - valid_user_blocks(sbi));
  445. spin_unlock(&sbi->stat_lock);
  446. return count;
  447. }
  448. if (!strcmp(a->attr.name, "discard_io_aware_gran")) {
  449. if (t > MAX_PLIST_NUM)
  450. return -EINVAL;
  451. if (!f2fs_block_unit_discard(sbi))
  452. return -EINVAL;
  453. if (t == *ui)
  454. return count;
  455. *ui = t;
  456. return count;
  457. }
  458. if (!strcmp(a->attr.name, "discard_granularity")) {
  459. if (t == 0 || t > MAX_PLIST_NUM)
  460. return -EINVAL;
  461. if (!f2fs_block_unit_discard(sbi))
  462. return -EINVAL;
  463. if (t == *ui)
  464. return count;
  465. *ui = t;
  466. return count;
  467. }
  468. if (!strcmp(a->attr.name, "max_ordered_discard")) {
  469. if (t == 0 || t > MAX_PLIST_NUM)
  470. return -EINVAL;
  471. if (!f2fs_block_unit_discard(sbi))
  472. return -EINVAL;
  473. *ui = t;
  474. return count;
  475. }
  476. if (!strcmp(a->attr.name, "discard_urgent_util")) {
  477. if (t > 100)
  478. return -EINVAL;
  479. *ui = t;
  480. return count;
  481. }
  482. if (!strcmp(a->attr.name, "discard_io_aware")) {
  483. if (t >= DPOLICY_IO_AWARE_MAX)
  484. return -EINVAL;
  485. *ui = t;
  486. return count;
  487. }
  488. if (!strcmp(a->attr.name, "migration_granularity")) {
  489. if (t == 0 || t > SEGS_PER_SEC(sbi))
  490. return -EINVAL;
  491. }
  492. if (!strcmp(a->attr.name, "migration_window_granularity")) {
  493. if (t == 0 || t > SEGS_PER_SEC(sbi))
  494. return -EINVAL;
  495. }
  496. if (!strcmp(a->attr.name, "gc_urgent")) {
  497. if (t == 0) {
  498. sbi->gc_mode = GC_NORMAL;
  499. } else if (t == 1) {
  500. sbi->gc_mode = GC_URGENT_HIGH;
  501. if (sbi->gc_thread) {
  502. sbi->gc_thread->gc_wake = true;
  503. wake_up_interruptible_all(
  504. &sbi->gc_thread->gc_wait_queue_head);
  505. wake_up_discard_thread(sbi, true);
  506. }
  507. } else if (t == 2) {
  508. sbi->gc_mode = GC_URGENT_LOW;
  509. } else if (t == 3) {
  510. sbi->gc_mode = GC_URGENT_MID;
  511. if (sbi->gc_thread) {
  512. sbi->gc_thread->gc_wake = true;
  513. wake_up_interruptible_all(
  514. &sbi->gc_thread->gc_wait_queue_head);
  515. }
  516. } else {
  517. return -EINVAL;
  518. }
  519. return count;
  520. }
  521. if (!strcmp(a->attr.name, "gc_idle")) {
  522. if (t == GC_IDLE_CB) {
  523. sbi->gc_mode = GC_IDLE_CB;
  524. } else if (t == GC_IDLE_GREEDY) {
  525. sbi->gc_mode = GC_IDLE_GREEDY;
  526. } else if (t == GC_IDLE_AT) {
  527. if (!sbi->am.atgc_enabled)
  528. return -EINVAL;
  529. sbi->gc_mode = GC_IDLE_AT;
  530. } else {
  531. sbi->gc_mode = GC_NORMAL;
  532. }
  533. return count;
  534. }
  535. if (!strcmp(a->attr.name, "gc_remaining_trials")) {
  536. spin_lock(&sbi->gc_remaining_trials_lock);
  537. sbi->gc_remaining_trials = t;
  538. spin_unlock(&sbi->gc_remaining_trials_lock);
  539. return count;
  540. }
  541. if (!strcmp(a->attr.name, "gc_no_zoned_gc_percent")) {
  542. if (t > 100)
  543. return -EINVAL;
  544. *ui = (unsigned int)t;
  545. return count;
  546. }
  547. if (!strcmp(a->attr.name, "gc_boost_zoned_gc_percent")) {
  548. if (t > 100)
  549. return -EINVAL;
  550. *ui = (unsigned int)t;
  551. return count;
  552. }
  553. if (!strcmp(a->attr.name, "gc_valid_thresh_ratio")) {
  554. if (t > 100)
  555. return -EINVAL;
  556. *ui = (unsigned int)t;
  557. return count;
  558. }
  559. #ifdef CONFIG_F2FS_IOSTAT
  560. if (!strcmp(a->attr.name, "iostat_enable")) {
  561. sbi->iostat_enable = !!t;
  562. if (!sbi->iostat_enable)
  563. f2fs_reset_iostat(sbi);
  564. return count;
  565. }
  566. if (!strcmp(a->attr.name, "iostat_period_ms")) {
  567. if (t < MIN_IOSTAT_PERIOD_MS || t > MAX_IOSTAT_PERIOD_MS)
  568. return -EINVAL;
  569. spin_lock_irq(&sbi->iostat_lock);
  570. sbi->iostat_period_ms = (unsigned int)t;
  571. spin_unlock_irq(&sbi->iostat_lock);
  572. return count;
  573. }
  574. #endif
  575. #ifdef CONFIG_BLK_DEV_ZONED
  576. if (!strcmp(a->attr.name, "blkzone_alloc_policy")) {
  577. if (t < BLKZONE_ALLOC_PRIOR_SEQ || t > BLKZONE_ALLOC_PRIOR_CONV)
  578. return -EINVAL;
  579. sbi->blkzone_alloc_policy = t;
  580. return count;
  581. }
  582. #endif
  583. #ifdef CONFIG_F2FS_FS_COMPRESSION
  584. if (!strcmp(a->attr.name, "compr_written_block") ||
  585. !strcmp(a->attr.name, "compr_saved_block")) {
  586. if (t != 0)
  587. return -EINVAL;
  588. sbi->compr_written_block = 0;
  589. sbi->compr_saved_block = 0;
  590. return count;
  591. }
  592. if (!strcmp(a->attr.name, "compr_new_inode")) {
  593. if (t != 0)
  594. return -EINVAL;
  595. sbi->compr_new_inode = 0;
  596. return count;
  597. }
  598. if (!strcmp(a->attr.name, "compress_percent")) {
  599. if (t == 0 || t > 100)
  600. return -EINVAL;
  601. *ui = t;
  602. return count;
  603. }
  604. if (!strcmp(a->attr.name, "compress_watermark")) {
  605. if (t == 0 || t > 100)
  606. return -EINVAL;
  607. *ui = t;
  608. return count;
  609. }
  610. #endif
  611. if (!strcmp(a->attr.name, "atgc_candidate_ratio")) {
  612. if (t > 100)
  613. return -EINVAL;
  614. sbi->am.candidate_ratio = t;
  615. return count;
  616. }
  617. if (!strcmp(a->attr.name, "atgc_age_weight")) {
  618. if (t > 100)
  619. return -EINVAL;
  620. sbi->am.age_weight = t;
  621. return count;
  622. }
  623. if (!strcmp(a->attr.name, "gc_segment_mode")) {
  624. if (t < MAX_GC_MODE)
  625. sbi->gc_segment_mode = t;
  626. else
  627. return -EINVAL;
  628. return count;
  629. }
  630. if (!strcmp(a->attr.name, "gc_pin_file_threshold")) {
  631. if (t > MAX_GC_FAILED_PINNED_FILES)
  632. return -EINVAL;
  633. sbi->gc_pin_file_threshold = t;
  634. return count;
  635. }
  636. if (!strcmp(a->attr.name, "gc_reclaimed_segments")) {
  637. if (t != 0)
  638. return -EINVAL;
  639. sbi->gc_reclaimed_segs[sbi->gc_segment_mode] = 0;
  640. return count;
  641. }
  642. if (!strcmp(a->attr.name, "seq_file_ra_mul")) {
  643. if (t >= MIN_RA_MUL && t <= MAX_RA_MUL)
  644. sbi->seq_file_ra_mul = t;
  645. else
  646. return -EINVAL;
  647. return count;
  648. }
  649. if (!strcmp(a->attr.name, "max_fragment_chunk")) {
  650. if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
  651. sbi->max_fragment_chunk = t;
  652. else
  653. return -EINVAL;
  654. return count;
  655. }
  656. if (!strcmp(a->attr.name, "max_fragment_hole")) {
  657. if (t >= MIN_FRAGMENT_SIZE && t <= MAX_FRAGMENT_SIZE)
  658. sbi->max_fragment_hole = t;
  659. else
  660. return -EINVAL;
  661. return count;
  662. }
  663. if (!strcmp(a->attr.name, "peak_atomic_write")) {
  664. if (t != 0)
  665. return -EINVAL;
  666. sbi->peak_atomic_write = 0;
  667. return count;
  668. }
  669. if (!strcmp(a->attr.name, "committed_atomic_block")) {
  670. if (t != 0)
  671. return -EINVAL;
  672. sbi->committed_atomic_block = 0;
  673. return count;
  674. }
  675. if (!strcmp(a->attr.name, "revoked_atomic_block")) {
  676. if (t != 0)
  677. return -EINVAL;
  678. sbi->revoked_atomic_block = 0;
  679. return count;
  680. }
  681. if (!strcmp(a->attr.name, "readdir_ra")) {
  682. sbi->readdir_ra = !!t;
  683. return count;
  684. }
  685. if (!strcmp(a->attr.name, "hot_data_age_threshold")) {
  686. if (t == 0 || t >= sbi->warm_data_age_threshold)
  687. return -EINVAL;
  688. if (t == *ui)
  689. return count;
  690. *ui = (unsigned int)t;
  691. return count;
  692. }
  693. if (!strcmp(a->attr.name, "warm_data_age_threshold")) {
  694. if (t <= sbi->hot_data_age_threshold)
  695. return -EINVAL;
  696. if (t == *ui)
  697. return count;
  698. *ui = (unsigned int)t;
  699. return count;
  700. }
  701. if (!strcmp(a->attr.name, "last_age_weight")) {
  702. if (t > 100)
  703. return -EINVAL;
  704. if (t == *ui)
  705. return count;
  706. *ui = (unsigned int)t;
  707. return count;
  708. }
  709. if (!strcmp(a->attr.name, "max_read_extent_count")) {
  710. if (t > UINT_MAX)
  711. return -EINVAL;
  712. *ui = (unsigned int)t;
  713. return count;
  714. }
  715. if (!strcmp(a->attr.name, "ipu_policy")) {
  716. if (t >= BIT(F2FS_IPU_MAX))
  717. return -EINVAL;
  718. /* allow F2FS_IPU_NOCACHE only for IPU in the pinned file */
  719. if (f2fs_lfs_mode(sbi) && (t & ~BIT(F2FS_IPU_NOCACHE)))
  720. return -EINVAL;
  721. SM_I(sbi)->ipu_policy = (unsigned int)t;
  722. return count;
  723. }
  724. if (!strcmp(a->attr.name, "dir_level")) {
  725. if (t > MAX_DIR_HASH_DEPTH)
  726. return -EINVAL;
  727. sbi->dir_level = t;
  728. return count;
  729. }
  730. *ui = (unsigned int)t;
  731. return count;
  732. }
  733. static ssize_t f2fs_sbi_store(struct f2fs_attr *a,
  734. struct f2fs_sb_info *sbi,
  735. const char *buf, size_t count)
  736. {
  737. ssize_t ret;
  738. bool gc_entry = (!strcmp(a->attr.name, "gc_urgent") ||
  739. a->struct_type == GC_THREAD);
  740. if (gc_entry) {
  741. if (!down_read_trylock(&sbi->sb->s_umount))
  742. return -EAGAIN;
  743. }
  744. ret = __sbi_store(a, sbi, buf, count);
  745. if (gc_entry)
  746. up_read(&sbi->sb->s_umount);
  747. return ret;
  748. }
  749. static ssize_t f2fs_attr_show(struct kobject *kobj,
  750. struct attribute *attr, char *buf)
  751. {
  752. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  753. s_kobj);
  754. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  755. return a->show ? a->show(a, sbi, buf) : 0;
  756. }
  757. static ssize_t f2fs_attr_store(struct kobject *kobj, struct attribute *attr,
  758. const char *buf, size_t len)
  759. {
  760. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  761. s_kobj);
  762. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  763. return a->store ? a->store(a, sbi, buf, len) : 0;
  764. }
  765. static void f2fs_sb_release(struct kobject *kobj)
  766. {
  767. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  768. s_kobj);
  769. complete(&sbi->s_kobj_unregister);
  770. }
  771. static ssize_t f2fs_base_attr_show(struct kobject *kobj,
  772. struct attribute *attr, char *buf)
  773. {
  774. struct f2fs_base_attr *a = container_of(attr,
  775. struct f2fs_base_attr, attr);
  776. return a->show ? a->show(a, buf) : 0;
  777. }
  778. static ssize_t f2fs_base_attr_store(struct kobject *kobj,
  779. struct attribute *attr,
  780. const char *buf, size_t len)
  781. {
  782. struct f2fs_base_attr *a = container_of(attr,
  783. struct f2fs_base_attr, attr);
  784. return a->store ? a->store(a, buf, len) : 0;
  785. }
  786. /*
  787. * Note that there are three feature list entries:
  788. * 1) /sys/fs/f2fs/features
  789. * : shows runtime features supported by in-kernel f2fs along with Kconfig.
  790. * - ref. F2FS_FEATURE_RO_ATTR()
  791. *
  792. * 2) /sys/fs/f2fs/$s_id/features <deprecated>
  793. * : shows on-disk features enabled by mkfs.f2fs, used for old kernels. This
  794. * won't add new feature anymore, and thus, users should check entries in 3)
  795. * instead of this 2).
  796. *
  797. * 3) /sys/fs/f2fs/$s_id/feature_list
  798. * : shows on-disk features enabled by mkfs.f2fs per instance, which follows
  799. * sysfs entry rule where each entry should expose single value.
  800. * This list covers old feature list provided by 2) and beyond. Therefore,
  801. * please add new on-disk feature in this list only.
  802. * - ref. F2FS_SB_FEATURE_RO_ATTR()
  803. */
  804. static ssize_t f2fs_feature_show(struct f2fs_base_attr *a, char *buf)
  805. {
  806. return sysfs_emit(buf, "supported\n");
  807. }
  808. #define F2FS_FEATURE_RO_ATTR(_name) \
  809. static struct f2fs_base_attr f2fs_base_attr_##_name = { \
  810. .attr = {.name = __stringify(_name), .mode = 0444 }, \
  811. .show = f2fs_feature_show, \
  812. }
  813. static ssize_t f2fs_sb_feature_show(struct f2fs_attr *a,
  814. struct f2fs_sb_info *sbi, char *buf)
  815. {
  816. if (F2FS_HAS_FEATURE(sbi, a->id))
  817. return sysfs_emit(buf, "supported\n");
  818. return sysfs_emit(buf, "unsupported\n");
  819. }
  820. #define F2FS_SB_FEATURE_RO_ATTR(_name, _feat) \
  821. static struct f2fs_attr f2fs_attr_sb_##_name = { \
  822. .attr = {.name = __stringify(_name), .mode = 0444 }, \
  823. .show = f2fs_sb_feature_show, \
  824. .id = F2FS_FEATURE_##_feat, \
  825. }
  826. #define F2FS_ATTR_OFFSET(_struct_type, _name, _mode, _show, _store, _offset) \
  827. static struct f2fs_attr f2fs_attr_##_name = { \
  828. .attr = {.name = __stringify(_name), .mode = _mode }, \
  829. .show = _show, \
  830. .store = _store, \
  831. .struct_type = _struct_type, \
  832. .offset = _offset \
  833. }
  834. #define F2FS_RO_ATTR(struct_type, struct_name, name, elname) \
  835. F2FS_ATTR_OFFSET(struct_type, name, 0444, \
  836. f2fs_sbi_show, NULL, \
  837. offsetof(struct struct_name, elname))
  838. #define F2FS_RW_ATTR(struct_type, struct_name, name, elname) \
  839. F2FS_ATTR_OFFSET(struct_type, name, 0644, \
  840. f2fs_sbi_show, f2fs_sbi_store, \
  841. offsetof(struct struct_name, elname))
  842. #define F2FS_GENERAL_RO_ATTR(name) \
  843. static struct f2fs_attr f2fs_attr_##name = __ATTR(name, 0444, name##_show, NULL)
  844. #ifdef CONFIG_F2FS_STAT_FS
  845. #define STAT_INFO_RO_ATTR(name, elname) \
  846. F2FS_RO_ATTR(STAT_INFO, f2fs_stat_info, name, elname)
  847. #endif
  848. #define GC_THREAD_RW_ATTR(name, elname) \
  849. F2FS_RW_ATTR(GC_THREAD, f2fs_gc_kthread, name, elname)
  850. #define SM_INFO_RW_ATTR(name, elname) \
  851. F2FS_RW_ATTR(SM_INFO, f2fs_sm_info, name, elname)
  852. #define SM_INFO_GENERAL_RW_ATTR(elname) \
  853. SM_INFO_RW_ATTR(elname, elname)
  854. #define DCC_INFO_RW_ATTR(name, elname) \
  855. F2FS_RW_ATTR(DCC_INFO, discard_cmd_control, name, elname)
  856. #define DCC_INFO_GENERAL_RW_ATTR(elname) \
  857. DCC_INFO_RW_ATTR(elname, elname)
  858. #define NM_INFO_RW_ATTR(name, elname) \
  859. F2FS_RW_ATTR(NM_INFO, f2fs_nm_info, name, elname)
  860. #define NM_INFO_GENERAL_RW_ATTR(elname) \
  861. NM_INFO_RW_ATTR(elname, elname)
  862. #define F2FS_SBI_RW_ATTR(name, elname) \
  863. F2FS_RW_ATTR(F2FS_SBI, f2fs_sb_info, name, elname)
  864. #define F2FS_SBI_GENERAL_RW_ATTR(elname) \
  865. F2FS_SBI_RW_ATTR(elname, elname)
  866. #define F2FS_SBI_GENERAL_RO_ATTR(elname) \
  867. F2FS_RO_ATTR(F2FS_SBI, f2fs_sb_info, elname, elname)
  868. #ifdef CONFIG_F2FS_FAULT_INJECTION
  869. #define FAULT_INFO_GENERAL_RW_ATTR(type, elname) \
  870. F2FS_RW_ATTR(type, f2fs_fault_info, elname, elname)
  871. #endif
  872. #define RESERVED_BLOCKS_GENERAL_RW_ATTR(elname) \
  873. F2FS_RW_ATTR(RESERVED_BLOCKS, f2fs_sb_info, elname, elname)
  874. #define CPRC_INFO_GENERAL_RW_ATTR(elname) \
  875. F2FS_RW_ATTR(CPRC_INFO, ckpt_req_control, elname, elname)
  876. #define ATGC_INFO_RW_ATTR(name, elname) \
  877. F2FS_RW_ATTR(ATGC_INFO, atgc_management, name, elname)
  878. /* GC_THREAD ATTR */
  879. GC_THREAD_RW_ATTR(gc_urgent_sleep_time, urgent_sleep_time);
  880. GC_THREAD_RW_ATTR(gc_min_sleep_time, min_sleep_time);
  881. GC_THREAD_RW_ATTR(gc_max_sleep_time, max_sleep_time);
  882. GC_THREAD_RW_ATTR(gc_no_gc_sleep_time, no_gc_sleep_time);
  883. GC_THREAD_RW_ATTR(gc_no_zoned_gc_percent, no_zoned_gc_percent);
  884. GC_THREAD_RW_ATTR(gc_boost_zoned_gc_percent, boost_zoned_gc_percent);
  885. GC_THREAD_RW_ATTR(gc_valid_thresh_ratio, valid_thresh_ratio);
  886. /* SM_INFO ATTR */
  887. SM_INFO_RW_ATTR(reclaim_segments, rec_prefree_segments);
  888. SM_INFO_GENERAL_RW_ATTR(ipu_policy);
  889. SM_INFO_GENERAL_RW_ATTR(min_ipu_util);
  890. SM_INFO_GENERAL_RW_ATTR(min_fsync_blocks);
  891. SM_INFO_GENERAL_RW_ATTR(min_seq_blocks);
  892. SM_INFO_GENERAL_RW_ATTR(min_hot_blocks);
  893. SM_INFO_GENERAL_RW_ATTR(min_ssr_sections);
  894. SM_INFO_GENERAL_RW_ATTR(reserved_segments);
  895. /* DCC_INFO ATTR */
  896. DCC_INFO_RW_ATTR(max_small_discards, max_discards);
  897. DCC_INFO_GENERAL_RW_ATTR(max_discard_request);
  898. DCC_INFO_GENERAL_RW_ATTR(min_discard_issue_time);
  899. DCC_INFO_GENERAL_RW_ATTR(mid_discard_issue_time);
  900. DCC_INFO_GENERAL_RW_ATTR(max_discard_issue_time);
  901. DCC_INFO_GENERAL_RW_ATTR(discard_io_aware_gran);
  902. DCC_INFO_GENERAL_RW_ATTR(discard_urgent_util);
  903. DCC_INFO_GENERAL_RW_ATTR(discard_granularity);
  904. DCC_INFO_GENERAL_RW_ATTR(max_ordered_discard);
  905. DCC_INFO_GENERAL_RW_ATTR(discard_io_aware);
  906. /* NM_INFO ATTR */
  907. NM_INFO_RW_ATTR(max_roll_forward_node_blocks, max_rf_node_blocks);
  908. NM_INFO_GENERAL_RW_ATTR(ram_thresh);
  909. NM_INFO_GENERAL_RW_ATTR(ra_nid_pages);
  910. NM_INFO_GENERAL_RW_ATTR(dirty_nats_ratio);
  911. /* F2FS_SBI ATTR */
  912. F2FS_RW_ATTR(F2FS_SBI, f2fs_super_block, extension_list, extension_list);
  913. F2FS_SBI_RW_ATTR(gc_idle, gc_mode);
  914. F2FS_SBI_RW_ATTR(gc_urgent, gc_mode);
  915. F2FS_SBI_RW_ATTR(cp_interval, interval_time[CP_TIME]);
  916. F2FS_SBI_RW_ATTR(idle_interval, interval_time[REQ_TIME]);
  917. F2FS_SBI_RW_ATTR(discard_idle_interval, interval_time[DISCARD_TIME]);
  918. F2FS_SBI_RW_ATTR(gc_idle_interval, interval_time[GC_TIME]);
  919. F2FS_SBI_RW_ATTR(umount_discard_timeout, interval_time[UMOUNT_DISCARD_TIMEOUT]);
  920. F2FS_SBI_RW_ATTR(gc_pin_file_thresh, gc_pin_file_threshold);
  921. F2FS_SBI_RW_ATTR(gc_reclaimed_segments, gc_reclaimed_segs);
  922. F2FS_SBI_GENERAL_RW_ATTR(max_victim_search);
  923. F2FS_SBI_GENERAL_RW_ATTR(migration_granularity);
  924. F2FS_SBI_GENERAL_RW_ATTR(migration_window_granularity);
  925. F2FS_SBI_GENERAL_RW_ATTR(dir_level);
  926. #ifdef CONFIG_F2FS_IOSTAT
  927. F2FS_SBI_GENERAL_RW_ATTR(iostat_enable);
  928. F2FS_SBI_GENERAL_RW_ATTR(iostat_period_ms);
  929. #endif
  930. F2FS_SBI_GENERAL_RW_ATTR(readdir_ra);
  931. F2FS_SBI_GENERAL_RW_ATTR(max_io_bytes);
  932. F2FS_SBI_GENERAL_RW_ATTR(data_io_flag);
  933. F2FS_SBI_GENERAL_RW_ATTR(node_io_flag);
  934. F2FS_SBI_GENERAL_RW_ATTR(gc_remaining_trials);
  935. F2FS_SBI_GENERAL_RW_ATTR(seq_file_ra_mul);
  936. F2FS_SBI_GENERAL_RW_ATTR(gc_segment_mode);
  937. F2FS_SBI_GENERAL_RW_ATTR(max_fragment_chunk);
  938. F2FS_SBI_GENERAL_RW_ATTR(max_fragment_hole);
  939. #ifdef CONFIG_F2FS_FS_COMPRESSION
  940. F2FS_SBI_GENERAL_RW_ATTR(compr_written_block);
  941. F2FS_SBI_GENERAL_RW_ATTR(compr_saved_block);
  942. F2FS_SBI_GENERAL_RW_ATTR(compr_new_inode);
  943. F2FS_SBI_GENERAL_RW_ATTR(compress_percent);
  944. F2FS_SBI_GENERAL_RW_ATTR(compress_watermark);
  945. #endif
  946. /* atomic write */
  947. F2FS_SBI_GENERAL_RO_ATTR(current_atomic_write);
  948. F2FS_SBI_GENERAL_RW_ATTR(peak_atomic_write);
  949. F2FS_SBI_GENERAL_RW_ATTR(committed_atomic_block);
  950. F2FS_SBI_GENERAL_RW_ATTR(revoked_atomic_block);
  951. /* block age extent cache */
  952. F2FS_SBI_GENERAL_RW_ATTR(hot_data_age_threshold);
  953. F2FS_SBI_GENERAL_RW_ATTR(warm_data_age_threshold);
  954. F2FS_SBI_GENERAL_RW_ATTR(last_age_weight);
  955. /* read extent cache */
  956. F2FS_SBI_GENERAL_RW_ATTR(max_read_extent_count);
  957. #ifdef CONFIG_BLK_DEV_ZONED
  958. F2FS_SBI_GENERAL_RO_ATTR(unusable_blocks_per_sec);
  959. F2FS_SBI_GENERAL_RW_ATTR(blkzone_alloc_policy);
  960. #endif
  961. /* STAT_INFO ATTR */
  962. #ifdef CONFIG_F2FS_STAT_FS
  963. STAT_INFO_RO_ATTR(cp_foreground_calls, cp_call_count[FOREGROUND]);
  964. STAT_INFO_RO_ATTR(cp_background_calls, cp_call_count[BACKGROUND]);
  965. STAT_INFO_RO_ATTR(gc_foreground_calls, gc_call_count[FOREGROUND]);
  966. STAT_INFO_RO_ATTR(gc_background_calls, gc_call_count[BACKGROUND]);
  967. #endif
  968. /* FAULT_INFO ATTR */
  969. #ifdef CONFIG_F2FS_FAULT_INJECTION
  970. FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_RATE, inject_rate);
  971. FAULT_INFO_GENERAL_RW_ATTR(FAULT_INFO_TYPE, inject_type);
  972. #endif
  973. /* RESERVED_BLOCKS ATTR */
  974. RESERVED_BLOCKS_GENERAL_RW_ATTR(reserved_blocks);
  975. /* CPRC_INFO ATTR */
  976. CPRC_INFO_GENERAL_RW_ATTR(ckpt_thread_ioprio);
  977. /* ATGC_INFO ATTR */
  978. ATGC_INFO_RW_ATTR(atgc_candidate_ratio, candidate_ratio);
  979. ATGC_INFO_RW_ATTR(atgc_candidate_count, max_candidate_count);
  980. ATGC_INFO_RW_ATTR(atgc_age_weight, age_weight);
  981. ATGC_INFO_RW_ATTR(atgc_age_threshold, age_threshold);
  982. F2FS_GENERAL_RO_ATTR(dirty_segments);
  983. F2FS_GENERAL_RO_ATTR(free_segments);
  984. F2FS_GENERAL_RO_ATTR(ovp_segments);
  985. F2FS_GENERAL_RO_ATTR(lifetime_write_kbytes);
  986. F2FS_GENERAL_RO_ATTR(features);
  987. F2FS_GENERAL_RO_ATTR(current_reserved_blocks);
  988. F2FS_GENERAL_RO_ATTR(unusable);
  989. F2FS_GENERAL_RO_ATTR(encoding);
  990. F2FS_GENERAL_RO_ATTR(mounted_time_sec);
  991. F2FS_GENERAL_RO_ATTR(main_blkaddr);
  992. F2FS_GENERAL_RO_ATTR(pending_discard);
  993. F2FS_GENERAL_RO_ATTR(atgc_enabled);
  994. F2FS_GENERAL_RO_ATTR(gc_mode);
  995. #ifdef CONFIG_F2FS_STAT_FS
  996. F2FS_GENERAL_RO_ATTR(moved_blocks_background);
  997. F2FS_GENERAL_RO_ATTR(moved_blocks_foreground);
  998. F2FS_GENERAL_RO_ATTR(avg_vblocks);
  999. #endif
  1000. #ifdef CONFIG_FS_ENCRYPTION
  1001. F2FS_FEATURE_RO_ATTR(encryption);
  1002. F2FS_FEATURE_RO_ATTR(test_dummy_encryption_v2);
  1003. #if IS_ENABLED(CONFIG_UNICODE)
  1004. F2FS_FEATURE_RO_ATTR(encrypted_casefold);
  1005. #endif
  1006. #endif /* CONFIG_FS_ENCRYPTION */
  1007. #ifdef CONFIG_BLK_DEV_ZONED
  1008. F2FS_FEATURE_RO_ATTR(block_zoned);
  1009. #endif
  1010. F2FS_FEATURE_RO_ATTR(atomic_write);
  1011. F2FS_FEATURE_RO_ATTR(extra_attr);
  1012. F2FS_FEATURE_RO_ATTR(project_quota);
  1013. F2FS_FEATURE_RO_ATTR(inode_checksum);
  1014. F2FS_FEATURE_RO_ATTR(flexible_inline_xattr);
  1015. F2FS_FEATURE_RO_ATTR(quota_ino);
  1016. F2FS_FEATURE_RO_ATTR(inode_crtime);
  1017. F2FS_FEATURE_RO_ATTR(lost_found);
  1018. #ifdef CONFIG_FS_VERITY
  1019. F2FS_FEATURE_RO_ATTR(verity);
  1020. #endif
  1021. F2FS_FEATURE_RO_ATTR(sb_checksum);
  1022. #if IS_ENABLED(CONFIG_UNICODE)
  1023. F2FS_FEATURE_RO_ATTR(casefold);
  1024. #endif
  1025. F2FS_FEATURE_RO_ATTR(readonly);
  1026. #ifdef CONFIG_F2FS_FS_COMPRESSION
  1027. F2FS_FEATURE_RO_ATTR(compression);
  1028. #endif
  1029. F2FS_FEATURE_RO_ATTR(pin_file);
  1030. #define ATTR_LIST(name) (&f2fs_attr_##name.attr)
  1031. static struct attribute *f2fs_attrs[] = {
  1032. ATTR_LIST(gc_urgent_sleep_time),
  1033. ATTR_LIST(gc_min_sleep_time),
  1034. ATTR_LIST(gc_max_sleep_time),
  1035. ATTR_LIST(gc_no_gc_sleep_time),
  1036. ATTR_LIST(gc_no_zoned_gc_percent),
  1037. ATTR_LIST(gc_boost_zoned_gc_percent),
  1038. ATTR_LIST(gc_valid_thresh_ratio),
  1039. ATTR_LIST(gc_idle),
  1040. ATTR_LIST(gc_urgent),
  1041. ATTR_LIST(reclaim_segments),
  1042. ATTR_LIST(main_blkaddr),
  1043. ATTR_LIST(max_small_discards),
  1044. ATTR_LIST(max_discard_request),
  1045. ATTR_LIST(min_discard_issue_time),
  1046. ATTR_LIST(mid_discard_issue_time),
  1047. ATTR_LIST(max_discard_issue_time),
  1048. ATTR_LIST(discard_io_aware_gran),
  1049. ATTR_LIST(discard_urgent_util),
  1050. ATTR_LIST(discard_granularity),
  1051. ATTR_LIST(max_ordered_discard),
  1052. ATTR_LIST(discard_io_aware),
  1053. ATTR_LIST(pending_discard),
  1054. ATTR_LIST(gc_mode),
  1055. ATTR_LIST(ipu_policy),
  1056. ATTR_LIST(min_ipu_util),
  1057. ATTR_LIST(min_fsync_blocks),
  1058. ATTR_LIST(min_seq_blocks),
  1059. ATTR_LIST(min_hot_blocks),
  1060. ATTR_LIST(min_ssr_sections),
  1061. ATTR_LIST(reserved_segments),
  1062. ATTR_LIST(max_victim_search),
  1063. ATTR_LIST(migration_granularity),
  1064. ATTR_LIST(migration_window_granularity),
  1065. ATTR_LIST(dir_level),
  1066. ATTR_LIST(ram_thresh),
  1067. ATTR_LIST(ra_nid_pages),
  1068. ATTR_LIST(dirty_nats_ratio),
  1069. ATTR_LIST(max_roll_forward_node_blocks),
  1070. ATTR_LIST(cp_interval),
  1071. ATTR_LIST(idle_interval),
  1072. ATTR_LIST(discard_idle_interval),
  1073. ATTR_LIST(gc_idle_interval),
  1074. ATTR_LIST(umount_discard_timeout),
  1075. #ifdef CONFIG_F2FS_IOSTAT
  1076. ATTR_LIST(iostat_enable),
  1077. ATTR_LIST(iostat_period_ms),
  1078. #endif
  1079. ATTR_LIST(readdir_ra),
  1080. ATTR_LIST(max_io_bytes),
  1081. ATTR_LIST(gc_pin_file_thresh),
  1082. ATTR_LIST(extension_list),
  1083. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1084. ATTR_LIST(inject_rate),
  1085. ATTR_LIST(inject_type),
  1086. #endif
  1087. ATTR_LIST(data_io_flag),
  1088. ATTR_LIST(node_io_flag),
  1089. ATTR_LIST(gc_remaining_trials),
  1090. ATTR_LIST(ckpt_thread_ioprio),
  1091. ATTR_LIST(dirty_segments),
  1092. ATTR_LIST(free_segments),
  1093. ATTR_LIST(ovp_segments),
  1094. ATTR_LIST(unusable),
  1095. ATTR_LIST(lifetime_write_kbytes),
  1096. ATTR_LIST(features),
  1097. ATTR_LIST(reserved_blocks),
  1098. ATTR_LIST(current_reserved_blocks),
  1099. ATTR_LIST(encoding),
  1100. ATTR_LIST(mounted_time_sec),
  1101. #ifdef CONFIG_F2FS_STAT_FS
  1102. ATTR_LIST(cp_foreground_calls),
  1103. ATTR_LIST(cp_background_calls),
  1104. ATTR_LIST(gc_foreground_calls),
  1105. ATTR_LIST(gc_background_calls),
  1106. ATTR_LIST(moved_blocks_foreground),
  1107. ATTR_LIST(moved_blocks_background),
  1108. ATTR_LIST(avg_vblocks),
  1109. #endif
  1110. #ifdef CONFIG_BLK_DEV_ZONED
  1111. ATTR_LIST(unusable_blocks_per_sec),
  1112. ATTR_LIST(blkzone_alloc_policy),
  1113. #endif
  1114. #ifdef CONFIG_F2FS_FS_COMPRESSION
  1115. ATTR_LIST(compr_written_block),
  1116. ATTR_LIST(compr_saved_block),
  1117. ATTR_LIST(compr_new_inode),
  1118. ATTR_LIST(compress_percent),
  1119. ATTR_LIST(compress_watermark),
  1120. #endif
  1121. /* For ATGC */
  1122. ATTR_LIST(atgc_candidate_ratio),
  1123. ATTR_LIST(atgc_candidate_count),
  1124. ATTR_LIST(atgc_age_weight),
  1125. ATTR_LIST(atgc_age_threshold),
  1126. ATTR_LIST(atgc_enabled),
  1127. ATTR_LIST(seq_file_ra_mul),
  1128. ATTR_LIST(gc_segment_mode),
  1129. ATTR_LIST(gc_reclaimed_segments),
  1130. ATTR_LIST(max_fragment_chunk),
  1131. ATTR_LIST(max_fragment_hole),
  1132. ATTR_LIST(current_atomic_write),
  1133. ATTR_LIST(peak_atomic_write),
  1134. ATTR_LIST(committed_atomic_block),
  1135. ATTR_LIST(revoked_atomic_block),
  1136. ATTR_LIST(hot_data_age_threshold),
  1137. ATTR_LIST(warm_data_age_threshold),
  1138. ATTR_LIST(last_age_weight),
  1139. ATTR_LIST(max_read_extent_count),
  1140. NULL,
  1141. };
  1142. ATTRIBUTE_GROUPS(f2fs);
  1143. #define BASE_ATTR_LIST(name) (&f2fs_base_attr_##name.attr)
  1144. static struct attribute *f2fs_feat_attrs[] = {
  1145. #ifdef CONFIG_FS_ENCRYPTION
  1146. BASE_ATTR_LIST(encryption),
  1147. BASE_ATTR_LIST(test_dummy_encryption_v2),
  1148. #if IS_ENABLED(CONFIG_UNICODE)
  1149. BASE_ATTR_LIST(encrypted_casefold),
  1150. #endif
  1151. #endif /* CONFIG_FS_ENCRYPTION */
  1152. #ifdef CONFIG_BLK_DEV_ZONED
  1153. BASE_ATTR_LIST(block_zoned),
  1154. #endif
  1155. BASE_ATTR_LIST(atomic_write),
  1156. BASE_ATTR_LIST(extra_attr),
  1157. BASE_ATTR_LIST(project_quota),
  1158. BASE_ATTR_LIST(inode_checksum),
  1159. BASE_ATTR_LIST(flexible_inline_xattr),
  1160. BASE_ATTR_LIST(quota_ino),
  1161. BASE_ATTR_LIST(inode_crtime),
  1162. BASE_ATTR_LIST(lost_found),
  1163. #ifdef CONFIG_FS_VERITY
  1164. BASE_ATTR_LIST(verity),
  1165. #endif
  1166. BASE_ATTR_LIST(sb_checksum),
  1167. #if IS_ENABLED(CONFIG_UNICODE)
  1168. BASE_ATTR_LIST(casefold),
  1169. #endif
  1170. BASE_ATTR_LIST(readonly),
  1171. #ifdef CONFIG_F2FS_FS_COMPRESSION
  1172. BASE_ATTR_LIST(compression),
  1173. #endif
  1174. BASE_ATTR_LIST(pin_file),
  1175. NULL,
  1176. };
  1177. ATTRIBUTE_GROUPS(f2fs_feat);
  1178. F2FS_GENERAL_RO_ATTR(sb_status);
  1179. F2FS_GENERAL_RO_ATTR(cp_status);
  1180. F2FS_GENERAL_RO_ATTR(issued_discard);
  1181. F2FS_GENERAL_RO_ATTR(queued_discard);
  1182. F2FS_GENERAL_RO_ATTR(undiscard_blks);
  1183. static struct attribute *f2fs_stat_attrs[] = {
  1184. ATTR_LIST(sb_status),
  1185. ATTR_LIST(cp_status),
  1186. ATTR_LIST(issued_discard),
  1187. ATTR_LIST(queued_discard),
  1188. ATTR_LIST(undiscard_blks),
  1189. NULL,
  1190. };
  1191. ATTRIBUTE_GROUPS(f2fs_stat);
  1192. F2FS_SB_FEATURE_RO_ATTR(encryption, ENCRYPT);
  1193. F2FS_SB_FEATURE_RO_ATTR(block_zoned, BLKZONED);
  1194. F2FS_SB_FEATURE_RO_ATTR(extra_attr, EXTRA_ATTR);
  1195. F2FS_SB_FEATURE_RO_ATTR(project_quota, PRJQUOTA);
  1196. F2FS_SB_FEATURE_RO_ATTR(inode_checksum, INODE_CHKSUM);
  1197. F2FS_SB_FEATURE_RO_ATTR(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
  1198. F2FS_SB_FEATURE_RO_ATTR(quota_ino, QUOTA_INO);
  1199. F2FS_SB_FEATURE_RO_ATTR(inode_crtime, INODE_CRTIME);
  1200. F2FS_SB_FEATURE_RO_ATTR(lost_found, LOST_FOUND);
  1201. F2FS_SB_FEATURE_RO_ATTR(verity, VERITY);
  1202. F2FS_SB_FEATURE_RO_ATTR(sb_checksum, SB_CHKSUM);
  1203. F2FS_SB_FEATURE_RO_ATTR(casefold, CASEFOLD);
  1204. F2FS_SB_FEATURE_RO_ATTR(compression, COMPRESSION);
  1205. F2FS_SB_FEATURE_RO_ATTR(readonly, RO);
  1206. static struct attribute *f2fs_sb_feat_attrs[] = {
  1207. ATTR_LIST(sb_encryption),
  1208. ATTR_LIST(sb_block_zoned),
  1209. ATTR_LIST(sb_extra_attr),
  1210. ATTR_LIST(sb_project_quota),
  1211. ATTR_LIST(sb_inode_checksum),
  1212. ATTR_LIST(sb_flexible_inline_xattr),
  1213. ATTR_LIST(sb_quota_ino),
  1214. ATTR_LIST(sb_inode_crtime),
  1215. ATTR_LIST(sb_lost_found),
  1216. ATTR_LIST(sb_verity),
  1217. ATTR_LIST(sb_sb_checksum),
  1218. ATTR_LIST(sb_casefold),
  1219. ATTR_LIST(sb_compression),
  1220. ATTR_LIST(sb_readonly),
  1221. NULL,
  1222. };
  1223. ATTRIBUTE_GROUPS(f2fs_sb_feat);
  1224. static const struct sysfs_ops f2fs_attr_ops = {
  1225. .show = f2fs_attr_show,
  1226. .store = f2fs_attr_store,
  1227. };
  1228. static const struct kobj_type f2fs_sb_ktype = {
  1229. .default_groups = f2fs_groups,
  1230. .sysfs_ops = &f2fs_attr_ops,
  1231. .release = f2fs_sb_release,
  1232. };
  1233. static const struct kobj_type f2fs_ktype = {
  1234. .sysfs_ops = &f2fs_attr_ops,
  1235. };
  1236. static struct kset f2fs_kset = {
  1237. .kobj = {.ktype = &f2fs_ktype},
  1238. };
  1239. static const struct sysfs_ops f2fs_feat_attr_ops = {
  1240. .show = f2fs_base_attr_show,
  1241. .store = f2fs_base_attr_store,
  1242. };
  1243. static const struct kobj_type f2fs_feat_ktype = {
  1244. .default_groups = f2fs_feat_groups,
  1245. .sysfs_ops = &f2fs_feat_attr_ops,
  1246. };
  1247. static struct kobject f2fs_feat = {
  1248. .kset = &f2fs_kset,
  1249. };
  1250. static ssize_t f2fs_stat_attr_show(struct kobject *kobj,
  1251. struct attribute *attr, char *buf)
  1252. {
  1253. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  1254. s_stat_kobj);
  1255. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  1256. return a->show ? a->show(a, sbi, buf) : 0;
  1257. }
  1258. static ssize_t f2fs_stat_attr_store(struct kobject *kobj, struct attribute *attr,
  1259. const char *buf, size_t len)
  1260. {
  1261. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  1262. s_stat_kobj);
  1263. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  1264. return a->store ? a->store(a, sbi, buf, len) : 0;
  1265. }
  1266. static void f2fs_stat_kobj_release(struct kobject *kobj)
  1267. {
  1268. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  1269. s_stat_kobj);
  1270. complete(&sbi->s_stat_kobj_unregister);
  1271. }
  1272. static const struct sysfs_ops f2fs_stat_attr_ops = {
  1273. .show = f2fs_stat_attr_show,
  1274. .store = f2fs_stat_attr_store,
  1275. };
  1276. static const struct kobj_type f2fs_stat_ktype = {
  1277. .default_groups = f2fs_stat_groups,
  1278. .sysfs_ops = &f2fs_stat_attr_ops,
  1279. .release = f2fs_stat_kobj_release,
  1280. };
  1281. static ssize_t f2fs_sb_feat_attr_show(struct kobject *kobj,
  1282. struct attribute *attr, char *buf)
  1283. {
  1284. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  1285. s_feature_list_kobj);
  1286. struct f2fs_attr *a = container_of(attr, struct f2fs_attr, attr);
  1287. return a->show ? a->show(a, sbi, buf) : 0;
  1288. }
  1289. static void f2fs_feature_list_kobj_release(struct kobject *kobj)
  1290. {
  1291. struct f2fs_sb_info *sbi = container_of(kobj, struct f2fs_sb_info,
  1292. s_feature_list_kobj);
  1293. complete(&sbi->s_feature_list_kobj_unregister);
  1294. }
  1295. static const struct sysfs_ops f2fs_feature_list_attr_ops = {
  1296. .show = f2fs_sb_feat_attr_show,
  1297. };
  1298. static const struct kobj_type f2fs_feature_list_ktype = {
  1299. .default_groups = f2fs_sb_feat_groups,
  1300. .sysfs_ops = &f2fs_feature_list_attr_ops,
  1301. .release = f2fs_feature_list_kobj_release,
  1302. };
  1303. static int __maybe_unused segment_info_seq_show(struct seq_file *seq,
  1304. void *offset)
  1305. {
  1306. struct super_block *sb = seq->private;
  1307. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1308. unsigned int total_segs =
  1309. le32_to_cpu(sbi->raw_super->segment_count_main);
  1310. int i;
  1311. seq_puts(seq, "format: segment_type|valid_blocks\n"
  1312. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  1313. for (i = 0; i < total_segs; i++) {
  1314. struct seg_entry *se = get_seg_entry(sbi, i);
  1315. if ((i % 10) == 0)
  1316. seq_printf(seq, "%-10d", i);
  1317. seq_printf(seq, "%d|%-3u", se->type, se->valid_blocks);
  1318. if ((i % 10) == 9 || i == (total_segs - 1))
  1319. seq_putc(seq, '\n');
  1320. else
  1321. seq_putc(seq, ' ');
  1322. }
  1323. return 0;
  1324. }
  1325. static int __maybe_unused segment_bits_seq_show(struct seq_file *seq,
  1326. void *offset)
  1327. {
  1328. struct super_block *sb = seq->private;
  1329. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1330. unsigned int total_segs =
  1331. le32_to_cpu(sbi->raw_super->segment_count_main);
  1332. int i, j;
  1333. seq_puts(seq, "format: segment_type|valid_blocks|bitmaps\n"
  1334. "segment_type(0:HD, 1:WD, 2:CD, 3:HN, 4:WN, 5:CN)\n");
  1335. for (i = 0; i < total_segs; i++) {
  1336. struct seg_entry *se = get_seg_entry(sbi, i);
  1337. seq_printf(seq, "%-10d", i);
  1338. seq_printf(seq, "%d|%-3u|", se->type, se->valid_blocks);
  1339. for (j = 0; j < SIT_VBLOCK_MAP_SIZE; j++)
  1340. seq_printf(seq, " %.2x", se->cur_valid_map[j]);
  1341. seq_putc(seq, '\n');
  1342. }
  1343. return 0;
  1344. }
  1345. static int __maybe_unused victim_bits_seq_show(struct seq_file *seq,
  1346. void *offset)
  1347. {
  1348. struct super_block *sb = seq->private;
  1349. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1350. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1351. int i;
  1352. seq_puts(seq, "format: victim_secmap bitmaps\n");
  1353. for (i = 0; i < MAIN_SECS(sbi); i++) {
  1354. if ((i % 10) == 0)
  1355. seq_printf(seq, "%-10d", i);
  1356. seq_printf(seq, "%d", test_bit(i, dirty_i->victim_secmap) ? 1 : 0);
  1357. if ((i % 10) == 9 || i == (MAIN_SECS(sbi) - 1))
  1358. seq_putc(seq, '\n');
  1359. else
  1360. seq_putc(seq, ' ');
  1361. }
  1362. return 0;
  1363. }
  1364. static int __maybe_unused discard_plist_seq_show(struct seq_file *seq,
  1365. void *offset)
  1366. {
  1367. struct super_block *sb = seq->private;
  1368. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1369. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1370. int i, count;
  1371. seq_puts(seq, "Discard pend list(Show diacrd_cmd count on each entry, .:not exist):\n");
  1372. if (!f2fs_realtime_discard_enable(sbi))
  1373. return 0;
  1374. if (dcc) {
  1375. mutex_lock(&dcc->cmd_lock);
  1376. for (i = 0; i < MAX_PLIST_NUM; i++) {
  1377. struct list_head *pend_list;
  1378. struct discard_cmd *dc, *tmp;
  1379. if (i % 8 == 0)
  1380. seq_printf(seq, " %-3d", i);
  1381. count = 0;
  1382. pend_list = &dcc->pend_list[i];
  1383. list_for_each_entry_safe(dc, tmp, pend_list, list)
  1384. count++;
  1385. if (count)
  1386. seq_printf(seq, " %7d", count);
  1387. else
  1388. seq_puts(seq, " .");
  1389. if (i % 8 == 7)
  1390. seq_putc(seq, '\n');
  1391. }
  1392. seq_putc(seq, '\n');
  1393. mutex_unlock(&dcc->cmd_lock);
  1394. }
  1395. return 0;
  1396. }
  1397. static int __maybe_unused disk_map_seq_show(struct seq_file *seq,
  1398. void *offset)
  1399. {
  1400. struct super_block *sb = seq->private;
  1401. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1402. int i;
  1403. seq_printf(seq, "Address Layout : %5luB Block address (# of Segments)\n",
  1404. F2FS_BLKSIZE);
  1405. seq_printf(seq, " SB : %12s\n", "0/1024B");
  1406. seq_printf(seq, " seg0_blkaddr : 0x%010x\n", SEG0_BLKADDR(sbi));
  1407. seq_printf(seq, " Checkpoint : 0x%010x (%10d)\n",
  1408. le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr), 2);
  1409. seq_printf(seq, " SIT : 0x%010x (%10d)\n",
  1410. SIT_I(sbi)->sit_base_addr,
  1411. le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_sit));
  1412. seq_printf(seq, " NAT : 0x%010x (%10d)\n",
  1413. NM_I(sbi)->nat_blkaddr,
  1414. le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_nat));
  1415. seq_printf(seq, " SSA : 0x%010x (%10d)\n",
  1416. SM_I(sbi)->ssa_blkaddr,
  1417. le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_ssa));
  1418. seq_printf(seq, " Main : 0x%010x (%10d)\n",
  1419. SM_I(sbi)->main_blkaddr,
  1420. le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main));
  1421. seq_printf(seq, " # of Sections : %12d\n",
  1422. le32_to_cpu(F2FS_RAW_SUPER(sbi)->section_count));
  1423. seq_printf(seq, " Segs/Sections : %12d\n",
  1424. SEGS_PER_SEC(sbi));
  1425. seq_printf(seq, " Section size : %12d MB\n",
  1426. SEGS_PER_SEC(sbi) << 1);
  1427. if (!f2fs_is_multi_device(sbi))
  1428. return 0;
  1429. seq_puts(seq, "\nDisk Map for multi devices:\n");
  1430. for (i = 0; i < sbi->s_ndevs; i++)
  1431. seq_printf(seq, "Disk:%2d (zoned=%d): 0x%010x - 0x%010x on %s\n",
  1432. i, bdev_is_zoned(FDEV(i).bdev),
  1433. FDEV(i).start_blk, FDEV(i).end_blk,
  1434. FDEV(i).path);
  1435. return 0;
  1436. }
  1437. int __init f2fs_init_sysfs(void)
  1438. {
  1439. int ret;
  1440. kobject_set_name(&f2fs_kset.kobj, "f2fs");
  1441. f2fs_kset.kobj.parent = fs_kobj;
  1442. ret = kset_register(&f2fs_kset);
  1443. if (ret)
  1444. return ret;
  1445. ret = kobject_init_and_add(&f2fs_feat, &f2fs_feat_ktype,
  1446. NULL, "features");
  1447. if (ret)
  1448. goto put_kobject;
  1449. f2fs_proc_root = proc_mkdir("fs/f2fs", NULL);
  1450. if (!f2fs_proc_root) {
  1451. ret = -ENOMEM;
  1452. goto put_kobject;
  1453. }
  1454. return 0;
  1455. put_kobject:
  1456. kobject_put(&f2fs_feat);
  1457. kset_unregister(&f2fs_kset);
  1458. return ret;
  1459. }
  1460. void f2fs_exit_sysfs(void)
  1461. {
  1462. kobject_put(&f2fs_feat);
  1463. kset_unregister(&f2fs_kset);
  1464. remove_proc_entry("fs/f2fs", NULL);
  1465. f2fs_proc_root = NULL;
  1466. }
  1467. int f2fs_register_sysfs(struct f2fs_sb_info *sbi)
  1468. {
  1469. struct super_block *sb = sbi->sb;
  1470. int err;
  1471. sbi->s_kobj.kset = &f2fs_kset;
  1472. init_completion(&sbi->s_kobj_unregister);
  1473. err = kobject_init_and_add(&sbi->s_kobj, &f2fs_sb_ktype, NULL,
  1474. "%s", sb->s_id);
  1475. if (err)
  1476. goto put_sb_kobj;
  1477. sbi->s_stat_kobj.kset = &f2fs_kset;
  1478. init_completion(&sbi->s_stat_kobj_unregister);
  1479. err = kobject_init_and_add(&sbi->s_stat_kobj, &f2fs_stat_ktype,
  1480. &sbi->s_kobj, "stat");
  1481. if (err)
  1482. goto put_stat_kobj;
  1483. sbi->s_feature_list_kobj.kset = &f2fs_kset;
  1484. init_completion(&sbi->s_feature_list_kobj_unregister);
  1485. err = kobject_init_and_add(&sbi->s_feature_list_kobj,
  1486. &f2fs_feature_list_ktype,
  1487. &sbi->s_kobj, "feature_list");
  1488. if (err)
  1489. goto put_feature_list_kobj;
  1490. sbi->s_proc = proc_mkdir(sb->s_id, f2fs_proc_root);
  1491. if (!sbi->s_proc) {
  1492. err = -ENOMEM;
  1493. goto put_feature_list_kobj;
  1494. }
  1495. proc_create_single_data("segment_info", 0444, sbi->s_proc,
  1496. segment_info_seq_show, sb);
  1497. proc_create_single_data("segment_bits", 0444, sbi->s_proc,
  1498. segment_bits_seq_show, sb);
  1499. #ifdef CONFIG_F2FS_IOSTAT
  1500. proc_create_single_data("iostat_info", 0444, sbi->s_proc,
  1501. iostat_info_seq_show, sb);
  1502. #endif
  1503. proc_create_single_data("victim_bits", 0444, sbi->s_proc,
  1504. victim_bits_seq_show, sb);
  1505. proc_create_single_data("discard_plist_info", 0444, sbi->s_proc,
  1506. discard_plist_seq_show, sb);
  1507. proc_create_single_data("disk_map", 0444, sbi->s_proc,
  1508. disk_map_seq_show, sb);
  1509. return 0;
  1510. put_feature_list_kobj:
  1511. kobject_put(&sbi->s_feature_list_kobj);
  1512. wait_for_completion(&sbi->s_feature_list_kobj_unregister);
  1513. put_stat_kobj:
  1514. kobject_put(&sbi->s_stat_kobj);
  1515. wait_for_completion(&sbi->s_stat_kobj_unregister);
  1516. put_sb_kobj:
  1517. kobject_put(&sbi->s_kobj);
  1518. wait_for_completion(&sbi->s_kobj_unregister);
  1519. return err;
  1520. }
  1521. void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi)
  1522. {
  1523. remove_proc_subtree(sbi->sb->s_id, f2fs_proc_root);
  1524. kobject_put(&sbi->s_stat_kobj);
  1525. wait_for_completion(&sbi->s_stat_kobj_unregister);
  1526. kobject_put(&sbi->s_feature_list_kobj);
  1527. wait_for_completion(&sbi->s_feature_list_kobj_unregister);
  1528. kobject_put(&sbi->s_kobj);
  1529. wait_for_completion(&sbi->s_kobj_unregister);
  1530. }