sysfs.c 42 KB

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