segment.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858
  1. /*
  2. * fs/f2fs/segment.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/f2fs_fs.h>
  13. #include <linux/bio.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/prefetch.h>
  16. #include <linux/kthread.h>
  17. #include <linux/swap.h>
  18. #include <linux/timer.h>
  19. #include "f2fs.h"
  20. #include "segment.h"
  21. #include "node.h"
  22. #include "trace.h"
  23. #include <trace/events/f2fs.h>
  24. #define __reverse_ffz(x) __reverse_ffs(~(x))
  25. static struct kmem_cache *discard_entry_slab;
  26. static struct kmem_cache *bio_entry_slab;
  27. static struct kmem_cache *sit_entry_set_slab;
  28. static struct kmem_cache *inmem_entry_slab;
  29. static unsigned long __reverse_ulong(unsigned char *str)
  30. {
  31. unsigned long tmp = 0;
  32. int shift = 24, idx = 0;
  33. #if BITS_PER_LONG == 64
  34. shift = 56;
  35. #endif
  36. while (shift >= 0) {
  37. tmp |= (unsigned long)str[idx++] << shift;
  38. shift -= BITS_PER_BYTE;
  39. }
  40. return tmp;
  41. }
  42. /*
  43. * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
  44. * MSB and LSB are reversed in a byte by f2fs_set_bit.
  45. */
  46. static inline unsigned long __reverse_ffs(unsigned long word)
  47. {
  48. int num = 0;
  49. #if BITS_PER_LONG == 64
  50. if ((word & 0xffffffff00000000UL) == 0)
  51. num += 32;
  52. else
  53. word >>= 32;
  54. #endif
  55. if ((word & 0xffff0000) == 0)
  56. num += 16;
  57. else
  58. word >>= 16;
  59. if ((word & 0xff00) == 0)
  60. num += 8;
  61. else
  62. word >>= 8;
  63. if ((word & 0xf0) == 0)
  64. num += 4;
  65. else
  66. word >>= 4;
  67. if ((word & 0xc) == 0)
  68. num += 2;
  69. else
  70. word >>= 2;
  71. if ((word & 0x2) == 0)
  72. num += 1;
  73. return num;
  74. }
  75. /*
  76. * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
  77. * f2fs_set_bit makes MSB and LSB reversed in a byte.
  78. * @size must be integral times of unsigned long.
  79. * Example:
  80. * MSB <--> LSB
  81. * f2fs_set_bit(0, bitmap) => 1000 0000
  82. * f2fs_set_bit(7, bitmap) => 0000 0001
  83. */
  84. static unsigned long __find_rev_next_bit(const unsigned long *addr,
  85. unsigned long size, unsigned long offset)
  86. {
  87. const unsigned long *p = addr + BIT_WORD(offset);
  88. unsigned long result = size;
  89. unsigned long tmp;
  90. if (offset >= size)
  91. return size;
  92. size -= (offset & ~(BITS_PER_LONG - 1));
  93. offset %= BITS_PER_LONG;
  94. while (1) {
  95. if (*p == 0)
  96. goto pass;
  97. tmp = __reverse_ulong((unsigned char *)p);
  98. tmp &= ~0UL >> offset;
  99. if (size < BITS_PER_LONG)
  100. tmp &= (~0UL << (BITS_PER_LONG - size));
  101. if (tmp)
  102. goto found;
  103. pass:
  104. if (size <= BITS_PER_LONG)
  105. break;
  106. size -= BITS_PER_LONG;
  107. offset = 0;
  108. p++;
  109. }
  110. return result;
  111. found:
  112. return result - size + __reverse_ffs(tmp);
  113. }
  114. static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
  115. unsigned long size, unsigned long offset)
  116. {
  117. const unsigned long *p = addr + BIT_WORD(offset);
  118. unsigned long result = size;
  119. unsigned long tmp;
  120. if (offset >= size)
  121. return size;
  122. size -= (offset & ~(BITS_PER_LONG - 1));
  123. offset %= BITS_PER_LONG;
  124. while (1) {
  125. if (*p == ~0UL)
  126. goto pass;
  127. tmp = __reverse_ulong((unsigned char *)p);
  128. if (offset)
  129. tmp |= ~0UL << (BITS_PER_LONG - offset);
  130. if (size < BITS_PER_LONG)
  131. tmp |= ~0UL >> size;
  132. if (tmp != ~0UL)
  133. goto found;
  134. pass:
  135. if (size <= BITS_PER_LONG)
  136. break;
  137. size -= BITS_PER_LONG;
  138. offset = 0;
  139. p++;
  140. }
  141. return result;
  142. found:
  143. return result - size + __reverse_ffz(tmp);
  144. }
  145. void register_inmem_page(struct inode *inode, struct page *page)
  146. {
  147. struct f2fs_inode_info *fi = F2FS_I(inode);
  148. struct inmem_pages *new;
  149. f2fs_trace_pid(page);
  150. set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
  151. SetPagePrivate(page);
  152. new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
  153. /* add atomic page indices to the list */
  154. new->page = page;
  155. INIT_LIST_HEAD(&new->list);
  156. /* increase reference count with clean state */
  157. mutex_lock(&fi->inmem_lock);
  158. get_page(page);
  159. list_add_tail(&new->list, &fi->inmem_pages);
  160. inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  161. mutex_unlock(&fi->inmem_lock);
  162. trace_f2fs_register_inmem_page(page, INMEM);
  163. }
  164. static int __revoke_inmem_pages(struct inode *inode,
  165. struct list_head *head, bool drop, bool recover)
  166. {
  167. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  168. struct inmem_pages *cur, *tmp;
  169. int err = 0;
  170. list_for_each_entry_safe(cur, tmp, head, list) {
  171. struct page *page = cur->page;
  172. if (drop)
  173. trace_f2fs_commit_inmem_page(page, INMEM_DROP);
  174. lock_page(page);
  175. if (recover) {
  176. struct dnode_of_data dn;
  177. struct node_info ni;
  178. trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
  179. set_new_dnode(&dn, inode, NULL, NULL, 0);
  180. if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) {
  181. err = -EAGAIN;
  182. goto next;
  183. }
  184. get_node_info(sbi, dn.nid, &ni);
  185. f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
  186. cur->old_addr, ni.version, true, true);
  187. f2fs_put_dnode(&dn);
  188. }
  189. next:
  190. /* we don't need to invalidate this in the sccessful status */
  191. if (drop || recover)
  192. ClearPageUptodate(page);
  193. set_page_private(page, 0);
  194. ClearPagePrivate(page);
  195. f2fs_put_page(page, 1);
  196. list_del(&cur->list);
  197. kmem_cache_free(inmem_entry_slab, cur);
  198. dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  199. }
  200. return err;
  201. }
  202. void drop_inmem_pages(struct inode *inode)
  203. {
  204. struct f2fs_inode_info *fi = F2FS_I(inode);
  205. mutex_lock(&fi->inmem_lock);
  206. __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
  207. mutex_unlock(&fi->inmem_lock);
  208. clear_inode_flag(inode, FI_ATOMIC_FILE);
  209. stat_dec_atomic_write(inode);
  210. }
  211. static int __commit_inmem_pages(struct inode *inode,
  212. struct list_head *revoke_list)
  213. {
  214. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  215. struct f2fs_inode_info *fi = F2FS_I(inode);
  216. struct inmem_pages *cur, *tmp;
  217. struct f2fs_io_info fio = {
  218. .sbi = sbi,
  219. .type = DATA,
  220. .op = REQ_OP_WRITE,
  221. .op_flags = REQ_SYNC | REQ_PRIO,
  222. .encrypted_page = NULL,
  223. };
  224. bool submit_bio = false;
  225. int err = 0;
  226. list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
  227. struct page *page = cur->page;
  228. lock_page(page);
  229. if (page->mapping == inode->i_mapping) {
  230. trace_f2fs_commit_inmem_page(page, INMEM);
  231. set_page_dirty(page);
  232. f2fs_wait_on_page_writeback(page, DATA, true);
  233. if (clear_page_dirty_for_io(page)) {
  234. inode_dec_dirty_pages(inode);
  235. remove_dirty_inode(inode);
  236. }
  237. fio.page = page;
  238. err = do_write_data_page(&fio);
  239. if (err) {
  240. unlock_page(page);
  241. break;
  242. }
  243. /* record old blkaddr for revoking */
  244. cur->old_addr = fio.old_blkaddr;
  245. submit_bio = true;
  246. }
  247. unlock_page(page);
  248. list_move_tail(&cur->list, revoke_list);
  249. }
  250. if (submit_bio)
  251. f2fs_submit_merged_bio_cond(sbi, inode, NULL, 0, DATA, WRITE);
  252. if (!err)
  253. __revoke_inmem_pages(inode, revoke_list, false, false);
  254. return err;
  255. }
  256. int commit_inmem_pages(struct inode *inode)
  257. {
  258. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  259. struct f2fs_inode_info *fi = F2FS_I(inode);
  260. struct list_head revoke_list;
  261. int err;
  262. INIT_LIST_HEAD(&revoke_list);
  263. f2fs_balance_fs(sbi, true);
  264. f2fs_lock_op(sbi);
  265. set_inode_flag(inode, FI_ATOMIC_COMMIT);
  266. mutex_lock(&fi->inmem_lock);
  267. err = __commit_inmem_pages(inode, &revoke_list);
  268. if (err) {
  269. int ret;
  270. /*
  271. * try to revoke all committed pages, but still we could fail
  272. * due to no memory or other reason, if that happened, EAGAIN
  273. * will be returned, which means in such case, transaction is
  274. * already not integrity, caller should use journal to do the
  275. * recovery or rewrite & commit last transaction. For other
  276. * error number, revoking was done by filesystem itself.
  277. */
  278. ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
  279. if (ret)
  280. err = ret;
  281. /* drop all uncommitted pages */
  282. __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
  283. }
  284. mutex_unlock(&fi->inmem_lock);
  285. clear_inode_flag(inode, FI_ATOMIC_COMMIT);
  286. f2fs_unlock_op(sbi);
  287. return err;
  288. }
  289. /*
  290. * This function balances dirty node and dentry pages.
  291. * In addition, it controls garbage collection.
  292. */
  293. void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
  294. {
  295. #ifdef CONFIG_F2FS_FAULT_INJECTION
  296. if (time_to_inject(sbi, FAULT_CHECKPOINT))
  297. f2fs_stop_checkpoint(sbi, false);
  298. #endif
  299. if (!need)
  300. return;
  301. /* balance_fs_bg is able to be pending */
  302. if (excess_cached_nats(sbi))
  303. f2fs_balance_fs_bg(sbi);
  304. /*
  305. * We should do GC or end up with checkpoint, if there are so many dirty
  306. * dir/node pages without enough free segments.
  307. */
  308. if (has_not_enough_free_secs(sbi, 0, 0)) {
  309. mutex_lock(&sbi->gc_mutex);
  310. f2fs_gc(sbi, false, false);
  311. }
  312. }
  313. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
  314. {
  315. /* try to shrink extent cache when there is no enough memory */
  316. if (!available_free_memory(sbi, EXTENT_CACHE))
  317. f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
  318. /* check the # of cached NAT entries */
  319. if (!available_free_memory(sbi, NAT_ENTRIES))
  320. try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
  321. if (!available_free_memory(sbi, FREE_NIDS))
  322. try_to_free_nids(sbi, MAX_FREE_NIDS);
  323. else
  324. build_free_nids(sbi, false);
  325. if (!is_idle(sbi))
  326. return;
  327. /* checkpoint is the only way to shrink partial cached entries */
  328. if (!available_free_memory(sbi, NAT_ENTRIES) ||
  329. !available_free_memory(sbi, INO_ENTRIES) ||
  330. excess_prefree_segs(sbi) ||
  331. excess_dirty_nats(sbi) ||
  332. f2fs_time_over(sbi, CP_TIME)) {
  333. if (test_opt(sbi, DATA_FLUSH)) {
  334. struct blk_plug plug;
  335. blk_start_plug(&plug);
  336. sync_dirty_inodes(sbi, FILE_INODE);
  337. blk_finish_plug(&plug);
  338. }
  339. f2fs_sync_fs(sbi->sb, true);
  340. stat_inc_bg_cp_count(sbi->stat_info);
  341. }
  342. }
  343. static int __submit_flush_wait(struct block_device *bdev)
  344. {
  345. struct bio *bio = f2fs_bio_alloc(0);
  346. int ret;
  347. bio->bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
  348. bio->bi_bdev = bdev;
  349. ret = submit_bio_wait(bio);
  350. bio_put(bio);
  351. return ret;
  352. }
  353. static int submit_flush_wait(struct f2fs_sb_info *sbi)
  354. {
  355. int ret = __submit_flush_wait(sbi->sb->s_bdev);
  356. int i;
  357. if (sbi->s_ndevs && !ret) {
  358. for (i = 1; i < sbi->s_ndevs; i++) {
  359. ret = __submit_flush_wait(FDEV(i).bdev);
  360. if (ret)
  361. break;
  362. }
  363. }
  364. return ret;
  365. }
  366. static int issue_flush_thread(void *data)
  367. {
  368. struct f2fs_sb_info *sbi = data;
  369. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  370. wait_queue_head_t *q = &fcc->flush_wait_queue;
  371. repeat:
  372. if (kthread_should_stop())
  373. return 0;
  374. if (!llist_empty(&fcc->issue_list)) {
  375. struct flush_cmd *cmd, *next;
  376. int ret;
  377. fcc->dispatch_list = llist_del_all(&fcc->issue_list);
  378. fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
  379. ret = submit_flush_wait(sbi);
  380. llist_for_each_entry_safe(cmd, next,
  381. fcc->dispatch_list, llnode) {
  382. cmd->ret = ret;
  383. complete(&cmd->wait);
  384. }
  385. fcc->dispatch_list = NULL;
  386. }
  387. wait_event_interruptible(*q,
  388. kthread_should_stop() || !llist_empty(&fcc->issue_list));
  389. goto repeat;
  390. }
  391. int f2fs_issue_flush(struct f2fs_sb_info *sbi)
  392. {
  393. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  394. struct flush_cmd cmd;
  395. trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
  396. test_opt(sbi, FLUSH_MERGE));
  397. if (test_opt(sbi, NOBARRIER))
  398. return 0;
  399. if (!test_opt(sbi, FLUSH_MERGE) || !atomic_read(&fcc->submit_flush)) {
  400. int ret;
  401. atomic_inc(&fcc->submit_flush);
  402. ret = submit_flush_wait(sbi);
  403. atomic_dec(&fcc->submit_flush);
  404. return ret;
  405. }
  406. init_completion(&cmd.wait);
  407. atomic_inc(&fcc->submit_flush);
  408. llist_add(&cmd.llnode, &fcc->issue_list);
  409. if (!fcc->dispatch_list)
  410. wake_up(&fcc->flush_wait_queue);
  411. if (fcc->f2fs_issue_flush) {
  412. wait_for_completion(&cmd.wait);
  413. atomic_dec(&fcc->submit_flush);
  414. } else {
  415. llist_del_all(&fcc->issue_list);
  416. atomic_set(&fcc->submit_flush, 0);
  417. }
  418. return cmd.ret;
  419. }
  420. int create_flush_cmd_control(struct f2fs_sb_info *sbi)
  421. {
  422. dev_t dev = sbi->sb->s_bdev->bd_dev;
  423. struct flush_cmd_control *fcc;
  424. int err = 0;
  425. if (SM_I(sbi)->cmd_control_info) {
  426. fcc = SM_I(sbi)->cmd_control_info;
  427. goto init_thread;
  428. }
  429. fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
  430. if (!fcc)
  431. return -ENOMEM;
  432. atomic_set(&fcc->submit_flush, 0);
  433. init_waitqueue_head(&fcc->flush_wait_queue);
  434. init_llist_head(&fcc->issue_list);
  435. SM_I(sbi)->cmd_control_info = fcc;
  436. init_thread:
  437. fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
  438. "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
  439. if (IS_ERR(fcc->f2fs_issue_flush)) {
  440. err = PTR_ERR(fcc->f2fs_issue_flush);
  441. kfree(fcc);
  442. SM_I(sbi)->cmd_control_info = NULL;
  443. return err;
  444. }
  445. return err;
  446. }
  447. void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
  448. {
  449. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  450. if (fcc && fcc->f2fs_issue_flush) {
  451. struct task_struct *flush_thread = fcc->f2fs_issue_flush;
  452. fcc->f2fs_issue_flush = NULL;
  453. kthread_stop(flush_thread);
  454. }
  455. if (free) {
  456. kfree(fcc);
  457. SM_I(sbi)->cmd_control_info = NULL;
  458. }
  459. }
  460. static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  461. enum dirty_type dirty_type)
  462. {
  463. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  464. /* need not be added */
  465. if (IS_CURSEG(sbi, segno))
  466. return;
  467. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  468. dirty_i->nr_dirty[dirty_type]++;
  469. if (dirty_type == DIRTY) {
  470. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  471. enum dirty_type t = sentry->type;
  472. if (unlikely(t >= DIRTY)) {
  473. f2fs_bug_on(sbi, 1);
  474. return;
  475. }
  476. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
  477. dirty_i->nr_dirty[t]++;
  478. }
  479. }
  480. static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  481. enum dirty_type dirty_type)
  482. {
  483. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  484. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  485. dirty_i->nr_dirty[dirty_type]--;
  486. if (dirty_type == DIRTY) {
  487. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  488. enum dirty_type t = sentry->type;
  489. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
  490. dirty_i->nr_dirty[t]--;
  491. if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
  492. clear_bit(GET_SECNO(sbi, segno),
  493. dirty_i->victim_secmap);
  494. }
  495. }
  496. /*
  497. * Should not occur error such as -ENOMEM.
  498. * Adding dirty entry into seglist is not critical operation.
  499. * If a given segment is one of current working segments, it won't be added.
  500. */
  501. static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
  502. {
  503. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  504. unsigned short valid_blocks;
  505. if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
  506. return;
  507. mutex_lock(&dirty_i->seglist_lock);
  508. valid_blocks = get_valid_blocks(sbi, segno, 0);
  509. if (valid_blocks == 0) {
  510. __locate_dirty_segment(sbi, segno, PRE);
  511. __remove_dirty_segment(sbi, segno, DIRTY);
  512. } else if (valid_blocks < sbi->blocks_per_seg) {
  513. __locate_dirty_segment(sbi, segno, DIRTY);
  514. } else {
  515. /* Recovery routine with SSR needs this */
  516. __remove_dirty_segment(sbi, segno, DIRTY);
  517. }
  518. mutex_unlock(&dirty_i->seglist_lock);
  519. }
  520. static struct bio_entry *__add_bio_entry(struct f2fs_sb_info *sbi,
  521. struct bio *bio, block_t lstart, block_t len)
  522. {
  523. struct list_head *wait_list = &(SM_I(sbi)->wait_list);
  524. struct bio_entry *be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
  525. INIT_LIST_HEAD(&be->list);
  526. be->bio = bio;
  527. be->lstart = lstart;
  528. be->len = len;
  529. init_completion(&be->event);
  530. list_add_tail(&be->list, wait_list);
  531. return be;
  532. }
  533. /* This should be covered by global mutex, &sit_i->sentry_lock */
  534. void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
  535. {
  536. struct list_head *wait_list = &(SM_I(sbi)->wait_list);
  537. struct bio_entry *be, *tmp;
  538. list_for_each_entry_safe(be, tmp, wait_list, list) {
  539. struct bio *bio = be->bio;
  540. int err;
  541. if (!completion_done(&be->event)) {
  542. if ((be->lstart <= blkaddr &&
  543. blkaddr < be->lstart + be->len) ||
  544. blkaddr == NULL_ADDR)
  545. wait_for_completion_io(&be->event);
  546. else
  547. continue;
  548. }
  549. err = be->error;
  550. if (err == -EOPNOTSUPP)
  551. err = 0;
  552. if (err)
  553. f2fs_msg(sbi->sb, KERN_INFO,
  554. "Issue discard failed, ret: %d", err);
  555. bio_put(bio);
  556. list_del(&be->list);
  557. kmem_cache_free(bio_entry_slab, be);
  558. }
  559. }
  560. static void f2fs_submit_bio_wait_endio(struct bio *bio)
  561. {
  562. struct bio_entry *be = (struct bio_entry *)bio->bi_private;
  563. be->error = bio->bi_error;
  564. complete(&be->event);
  565. }
  566. /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
  567. static int __f2fs_issue_discard_async(struct f2fs_sb_info *sbi,
  568. struct block_device *bdev, block_t blkstart, block_t blklen)
  569. {
  570. struct bio *bio = NULL;
  571. block_t lblkstart = blkstart;
  572. int err;
  573. trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
  574. if (sbi->s_ndevs) {
  575. int devi = f2fs_target_device_index(sbi, blkstart);
  576. blkstart -= FDEV(devi).start_blk;
  577. }
  578. err = __blkdev_issue_discard(bdev,
  579. SECTOR_FROM_BLOCK(blkstart),
  580. SECTOR_FROM_BLOCK(blklen),
  581. GFP_NOFS, 0, &bio);
  582. if (!err && bio) {
  583. struct bio_entry *be = __add_bio_entry(sbi, bio,
  584. lblkstart, blklen);
  585. bio->bi_private = be;
  586. bio->bi_end_io = f2fs_submit_bio_wait_endio;
  587. bio->bi_opf |= REQ_SYNC;
  588. submit_bio(bio);
  589. }
  590. return err;
  591. }
  592. #ifdef CONFIG_BLK_DEV_ZONED
  593. static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
  594. struct block_device *bdev, block_t blkstart, block_t blklen)
  595. {
  596. sector_t nr_sects = SECTOR_FROM_BLOCK(blklen);
  597. sector_t sector;
  598. int devi = 0;
  599. if (sbi->s_ndevs) {
  600. devi = f2fs_target_device_index(sbi, blkstart);
  601. blkstart -= FDEV(devi).start_blk;
  602. }
  603. sector = SECTOR_FROM_BLOCK(blkstart);
  604. if (sector & (bdev_zone_sectors(bdev) - 1) ||
  605. nr_sects != bdev_zone_sectors(bdev)) {
  606. f2fs_msg(sbi->sb, KERN_INFO,
  607. "(%d) %s: Unaligned discard attempted (block %x + %x)",
  608. devi, sbi->s_ndevs ? FDEV(devi).path: "",
  609. blkstart, blklen);
  610. return -EIO;
  611. }
  612. /*
  613. * We need to know the type of the zone: for conventional zones,
  614. * use regular discard if the drive supports it. For sequential
  615. * zones, reset the zone write pointer.
  616. */
  617. switch (get_blkz_type(sbi, bdev, blkstart)) {
  618. case BLK_ZONE_TYPE_CONVENTIONAL:
  619. if (!blk_queue_discard(bdev_get_queue(bdev)))
  620. return 0;
  621. return __f2fs_issue_discard_async(sbi, bdev, blkstart, blklen);
  622. case BLK_ZONE_TYPE_SEQWRITE_REQ:
  623. case BLK_ZONE_TYPE_SEQWRITE_PREF:
  624. trace_f2fs_issue_reset_zone(sbi->sb, blkstart);
  625. return blkdev_reset_zones(bdev, sector,
  626. nr_sects, GFP_NOFS);
  627. default:
  628. /* Unknown zone type: broken device ? */
  629. return -EIO;
  630. }
  631. }
  632. #endif
  633. static int __issue_discard_async(struct f2fs_sb_info *sbi,
  634. struct block_device *bdev, block_t blkstart, block_t blklen)
  635. {
  636. #ifdef CONFIG_BLK_DEV_ZONED
  637. if (f2fs_sb_mounted_blkzoned(sbi->sb) &&
  638. bdev_zoned_model(bdev) != BLK_ZONED_NONE)
  639. return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
  640. #endif
  641. return __f2fs_issue_discard_async(sbi, bdev, blkstart, blklen);
  642. }
  643. static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
  644. block_t blkstart, block_t blklen)
  645. {
  646. sector_t start = blkstart, len = 0;
  647. struct block_device *bdev;
  648. struct seg_entry *se;
  649. unsigned int offset;
  650. block_t i;
  651. int err = 0;
  652. bdev = f2fs_target_device(sbi, blkstart, NULL);
  653. for (i = blkstart; i < blkstart + blklen; i++, len++) {
  654. if (i != start) {
  655. struct block_device *bdev2 =
  656. f2fs_target_device(sbi, i, NULL);
  657. if (bdev2 != bdev) {
  658. err = __issue_discard_async(sbi, bdev,
  659. start, len);
  660. if (err)
  661. return err;
  662. bdev = bdev2;
  663. start = i;
  664. len = 0;
  665. }
  666. }
  667. se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
  668. offset = GET_BLKOFF_FROM_SEG0(sbi, i);
  669. if (!f2fs_test_and_set_bit(offset, se->discard_map))
  670. sbi->discard_blks--;
  671. }
  672. if (len)
  673. err = __issue_discard_async(sbi, bdev, start, len);
  674. return err;
  675. }
  676. static void __add_discard_entry(struct f2fs_sb_info *sbi,
  677. struct cp_control *cpc, struct seg_entry *se,
  678. unsigned int start, unsigned int end)
  679. {
  680. struct list_head *head = &SM_I(sbi)->discard_list;
  681. struct discard_entry *new, *last;
  682. if (!list_empty(head)) {
  683. last = list_last_entry(head, struct discard_entry, list);
  684. if (START_BLOCK(sbi, cpc->trim_start) + start ==
  685. last->blkaddr + last->len) {
  686. last->len += end - start;
  687. goto done;
  688. }
  689. }
  690. new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
  691. INIT_LIST_HEAD(&new->list);
  692. new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
  693. new->len = end - start;
  694. list_add_tail(&new->list, head);
  695. done:
  696. SM_I(sbi)->nr_discards += end - start;
  697. }
  698. static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
  699. bool check_only)
  700. {
  701. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  702. int max_blocks = sbi->blocks_per_seg;
  703. struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
  704. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  705. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  706. unsigned long *discard_map = (unsigned long *)se->discard_map;
  707. unsigned long *dmap = SIT_I(sbi)->tmp_map;
  708. unsigned int start = 0, end = -1;
  709. bool force = (cpc->reason == CP_DISCARD);
  710. int i;
  711. if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
  712. return false;
  713. if (!force) {
  714. if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
  715. SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
  716. return false;
  717. }
  718. /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
  719. for (i = 0; i < entries; i++)
  720. dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
  721. (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
  722. while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
  723. start = __find_rev_next_bit(dmap, max_blocks, end + 1);
  724. if (start >= max_blocks)
  725. break;
  726. end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
  727. if (force && start && end != max_blocks
  728. && (end - start) < cpc->trim_minlen)
  729. continue;
  730. if (check_only)
  731. return true;
  732. __add_discard_entry(sbi, cpc, se, start, end);
  733. }
  734. return false;
  735. }
  736. void release_discard_addrs(struct f2fs_sb_info *sbi)
  737. {
  738. struct list_head *head = &(SM_I(sbi)->discard_list);
  739. struct discard_entry *entry, *this;
  740. /* drop caches */
  741. list_for_each_entry_safe(entry, this, head, list) {
  742. list_del(&entry->list);
  743. kmem_cache_free(discard_entry_slab, entry);
  744. }
  745. }
  746. /*
  747. * Should call clear_prefree_segments after checkpoint is done.
  748. */
  749. static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
  750. {
  751. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  752. unsigned int segno;
  753. mutex_lock(&dirty_i->seglist_lock);
  754. for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
  755. __set_test_and_free(sbi, segno);
  756. mutex_unlock(&dirty_i->seglist_lock);
  757. }
  758. void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  759. {
  760. struct list_head *head = &(SM_I(sbi)->discard_list);
  761. struct discard_entry *entry, *this;
  762. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  763. struct blk_plug plug;
  764. unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
  765. unsigned int start = 0, end = -1;
  766. unsigned int secno, start_segno;
  767. bool force = (cpc->reason == CP_DISCARD);
  768. blk_start_plug(&plug);
  769. mutex_lock(&dirty_i->seglist_lock);
  770. while (1) {
  771. int i;
  772. start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
  773. if (start >= MAIN_SEGS(sbi))
  774. break;
  775. end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
  776. start + 1);
  777. for (i = start; i < end; i++)
  778. clear_bit(i, prefree_map);
  779. dirty_i->nr_dirty[PRE] -= end - start;
  780. if (!test_opt(sbi, DISCARD))
  781. continue;
  782. if (force && start >= cpc->trim_start &&
  783. (end - 1) <= cpc->trim_end)
  784. continue;
  785. if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
  786. f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
  787. (end - start) << sbi->log_blocks_per_seg);
  788. continue;
  789. }
  790. next:
  791. secno = GET_SECNO(sbi, start);
  792. start_segno = secno * sbi->segs_per_sec;
  793. if (!IS_CURSEC(sbi, secno) &&
  794. !get_valid_blocks(sbi, start, sbi->segs_per_sec))
  795. f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
  796. sbi->segs_per_sec << sbi->log_blocks_per_seg);
  797. start = start_segno + sbi->segs_per_sec;
  798. if (start < end)
  799. goto next;
  800. }
  801. mutex_unlock(&dirty_i->seglist_lock);
  802. /* send small discards */
  803. list_for_each_entry_safe(entry, this, head, list) {
  804. if (force && entry->len < cpc->trim_minlen)
  805. goto skip;
  806. f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
  807. cpc->trimmed += entry->len;
  808. skip:
  809. list_del(&entry->list);
  810. SM_I(sbi)->nr_discards -= entry->len;
  811. kmem_cache_free(discard_entry_slab, entry);
  812. }
  813. blk_finish_plug(&plug);
  814. }
  815. static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
  816. {
  817. struct sit_info *sit_i = SIT_I(sbi);
  818. if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
  819. sit_i->dirty_sentries++;
  820. return false;
  821. }
  822. return true;
  823. }
  824. static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
  825. unsigned int segno, int modified)
  826. {
  827. struct seg_entry *se = get_seg_entry(sbi, segno);
  828. se->type = type;
  829. if (modified)
  830. __mark_sit_entry_dirty(sbi, segno);
  831. }
  832. static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
  833. {
  834. struct seg_entry *se;
  835. unsigned int segno, offset;
  836. long int new_vblocks;
  837. segno = GET_SEGNO(sbi, blkaddr);
  838. se = get_seg_entry(sbi, segno);
  839. new_vblocks = se->valid_blocks + del;
  840. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  841. f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
  842. (new_vblocks > sbi->blocks_per_seg)));
  843. se->valid_blocks = new_vblocks;
  844. se->mtime = get_mtime(sbi);
  845. SIT_I(sbi)->max_mtime = se->mtime;
  846. /* Update valid block bitmap */
  847. if (del > 0) {
  848. if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
  849. f2fs_bug_on(sbi, 1);
  850. if (f2fs_discard_en(sbi) &&
  851. !f2fs_test_and_set_bit(offset, se->discard_map))
  852. sbi->discard_blks--;
  853. } else {
  854. if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
  855. f2fs_bug_on(sbi, 1);
  856. if (f2fs_discard_en(sbi) &&
  857. f2fs_test_and_clear_bit(offset, se->discard_map))
  858. sbi->discard_blks++;
  859. }
  860. if (!f2fs_test_bit(offset, se->ckpt_valid_map))
  861. se->ckpt_valid_blocks += del;
  862. __mark_sit_entry_dirty(sbi, segno);
  863. /* update total number of valid blocks to be written in ckpt area */
  864. SIT_I(sbi)->written_valid_blocks += del;
  865. if (sbi->segs_per_sec > 1)
  866. get_sec_entry(sbi, segno)->valid_blocks += del;
  867. }
  868. void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
  869. {
  870. update_sit_entry(sbi, new, 1);
  871. if (GET_SEGNO(sbi, old) != NULL_SEGNO)
  872. update_sit_entry(sbi, old, -1);
  873. locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
  874. locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
  875. }
  876. void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
  877. {
  878. unsigned int segno = GET_SEGNO(sbi, addr);
  879. struct sit_info *sit_i = SIT_I(sbi);
  880. f2fs_bug_on(sbi, addr == NULL_ADDR);
  881. if (addr == NEW_ADDR)
  882. return;
  883. /* add it into sit main buffer */
  884. mutex_lock(&sit_i->sentry_lock);
  885. update_sit_entry(sbi, addr, -1);
  886. /* add it into dirty seglist */
  887. locate_dirty_segment(sbi, segno);
  888. mutex_unlock(&sit_i->sentry_lock);
  889. }
  890. bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
  891. {
  892. struct sit_info *sit_i = SIT_I(sbi);
  893. unsigned int segno, offset;
  894. struct seg_entry *se;
  895. bool is_cp = false;
  896. if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
  897. return true;
  898. mutex_lock(&sit_i->sentry_lock);
  899. segno = GET_SEGNO(sbi, blkaddr);
  900. se = get_seg_entry(sbi, segno);
  901. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  902. if (f2fs_test_bit(offset, se->ckpt_valid_map))
  903. is_cp = true;
  904. mutex_unlock(&sit_i->sentry_lock);
  905. return is_cp;
  906. }
  907. /*
  908. * This function should be resided under the curseg_mutex lock
  909. */
  910. static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
  911. struct f2fs_summary *sum)
  912. {
  913. struct curseg_info *curseg = CURSEG_I(sbi, type);
  914. void *addr = curseg->sum_blk;
  915. addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
  916. memcpy(addr, sum, sizeof(struct f2fs_summary));
  917. }
  918. /*
  919. * Calculate the number of current summary pages for writing
  920. */
  921. int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
  922. {
  923. int valid_sum_count = 0;
  924. int i, sum_in_page;
  925. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  926. if (sbi->ckpt->alloc_type[i] == SSR)
  927. valid_sum_count += sbi->blocks_per_seg;
  928. else {
  929. if (for_ra)
  930. valid_sum_count += le16_to_cpu(
  931. F2FS_CKPT(sbi)->cur_data_blkoff[i]);
  932. else
  933. valid_sum_count += curseg_blkoff(sbi, i);
  934. }
  935. }
  936. sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
  937. SUM_FOOTER_SIZE) / SUMMARY_SIZE;
  938. if (valid_sum_count <= sum_in_page)
  939. return 1;
  940. else if ((valid_sum_count - sum_in_page) <=
  941. (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
  942. return 2;
  943. return 3;
  944. }
  945. /*
  946. * Caller should put this summary page
  947. */
  948. struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
  949. {
  950. return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
  951. }
  952. void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
  953. {
  954. struct page *page = grab_meta_page(sbi, blk_addr);
  955. void *dst = page_address(page);
  956. if (src)
  957. memcpy(dst, src, PAGE_SIZE);
  958. else
  959. memset(dst, 0, PAGE_SIZE);
  960. set_page_dirty(page);
  961. f2fs_put_page(page, 1);
  962. }
  963. static void write_sum_page(struct f2fs_sb_info *sbi,
  964. struct f2fs_summary_block *sum_blk, block_t blk_addr)
  965. {
  966. update_meta_page(sbi, (void *)sum_blk, blk_addr);
  967. }
  968. static void write_current_sum_page(struct f2fs_sb_info *sbi,
  969. int type, block_t blk_addr)
  970. {
  971. struct curseg_info *curseg = CURSEG_I(sbi, type);
  972. struct page *page = grab_meta_page(sbi, blk_addr);
  973. struct f2fs_summary_block *src = curseg->sum_blk;
  974. struct f2fs_summary_block *dst;
  975. dst = (struct f2fs_summary_block *)page_address(page);
  976. mutex_lock(&curseg->curseg_mutex);
  977. down_read(&curseg->journal_rwsem);
  978. memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
  979. up_read(&curseg->journal_rwsem);
  980. memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
  981. memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
  982. mutex_unlock(&curseg->curseg_mutex);
  983. set_page_dirty(page);
  984. f2fs_put_page(page, 1);
  985. }
  986. static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
  987. {
  988. struct curseg_info *curseg = CURSEG_I(sbi, type);
  989. unsigned int segno = curseg->segno + 1;
  990. struct free_segmap_info *free_i = FREE_I(sbi);
  991. if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
  992. return !test_bit(segno, free_i->free_segmap);
  993. return 0;
  994. }
  995. /*
  996. * Find a new segment from the free segments bitmap to right order
  997. * This function should be returned with success, otherwise BUG
  998. */
  999. static void get_new_segment(struct f2fs_sb_info *sbi,
  1000. unsigned int *newseg, bool new_sec, int dir)
  1001. {
  1002. struct free_segmap_info *free_i = FREE_I(sbi);
  1003. unsigned int segno, secno, zoneno;
  1004. unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
  1005. unsigned int hint = *newseg / sbi->segs_per_sec;
  1006. unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
  1007. unsigned int left_start = hint;
  1008. bool init = true;
  1009. int go_left = 0;
  1010. int i;
  1011. spin_lock(&free_i->segmap_lock);
  1012. if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
  1013. segno = find_next_zero_bit(free_i->free_segmap,
  1014. (hint + 1) * sbi->segs_per_sec, *newseg + 1);
  1015. if (segno < (hint + 1) * sbi->segs_per_sec)
  1016. goto got_it;
  1017. }
  1018. find_other_zone:
  1019. secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
  1020. if (secno >= MAIN_SECS(sbi)) {
  1021. if (dir == ALLOC_RIGHT) {
  1022. secno = find_next_zero_bit(free_i->free_secmap,
  1023. MAIN_SECS(sbi), 0);
  1024. f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
  1025. } else {
  1026. go_left = 1;
  1027. left_start = hint - 1;
  1028. }
  1029. }
  1030. if (go_left == 0)
  1031. goto skip_left;
  1032. while (test_bit(left_start, free_i->free_secmap)) {
  1033. if (left_start > 0) {
  1034. left_start--;
  1035. continue;
  1036. }
  1037. left_start = find_next_zero_bit(free_i->free_secmap,
  1038. MAIN_SECS(sbi), 0);
  1039. f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
  1040. break;
  1041. }
  1042. secno = left_start;
  1043. skip_left:
  1044. hint = secno;
  1045. segno = secno * sbi->segs_per_sec;
  1046. zoneno = secno / sbi->secs_per_zone;
  1047. /* give up on finding another zone */
  1048. if (!init)
  1049. goto got_it;
  1050. if (sbi->secs_per_zone == 1)
  1051. goto got_it;
  1052. if (zoneno == old_zoneno)
  1053. goto got_it;
  1054. if (dir == ALLOC_LEFT) {
  1055. if (!go_left && zoneno + 1 >= total_zones)
  1056. goto got_it;
  1057. if (go_left && zoneno == 0)
  1058. goto got_it;
  1059. }
  1060. for (i = 0; i < NR_CURSEG_TYPE; i++)
  1061. if (CURSEG_I(sbi, i)->zone == zoneno)
  1062. break;
  1063. if (i < NR_CURSEG_TYPE) {
  1064. /* zone is in user, try another */
  1065. if (go_left)
  1066. hint = zoneno * sbi->secs_per_zone - 1;
  1067. else if (zoneno + 1 >= total_zones)
  1068. hint = 0;
  1069. else
  1070. hint = (zoneno + 1) * sbi->secs_per_zone;
  1071. init = false;
  1072. goto find_other_zone;
  1073. }
  1074. got_it:
  1075. /* set it as dirty segment in free segmap */
  1076. f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
  1077. __set_inuse(sbi, segno);
  1078. *newseg = segno;
  1079. spin_unlock(&free_i->segmap_lock);
  1080. }
  1081. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  1082. {
  1083. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1084. struct summary_footer *sum_footer;
  1085. curseg->segno = curseg->next_segno;
  1086. curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
  1087. curseg->next_blkoff = 0;
  1088. curseg->next_segno = NULL_SEGNO;
  1089. sum_footer = &(curseg->sum_blk->footer);
  1090. memset(sum_footer, 0, sizeof(struct summary_footer));
  1091. if (IS_DATASEG(type))
  1092. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  1093. if (IS_NODESEG(type))
  1094. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  1095. __set_sit_entry_type(sbi, type, curseg->segno, modified);
  1096. }
  1097. /*
  1098. * Allocate a current working segment.
  1099. * This function always allocates a free segment in LFS manner.
  1100. */
  1101. static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  1102. {
  1103. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1104. unsigned int segno = curseg->segno;
  1105. int dir = ALLOC_LEFT;
  1106. write_sum_page(sbi, curseg->sum_blk,
  1107. GET_SUM_BLOCK(sbi, segno));
  1108. if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
  1109. dir = ALLOC_RIGHT;
  1110. if (test_opt(sbi, NOHEAP))
  1111. dir = ALLOC_RIGHT;
  1112. get_new_segment(sbi, &segno, new_sec, dir);
  1113. curseg->next_segno = segno;
  1114. reset_curseg(sbi, type, 1);
  1115. curseg->alloc_type = LFS;
  1116. }
  1117. static void __next_free_blkoff(struct f2fs_sb_info *sbi,
  1118. struct curseg_info *seg, block_t start)
  1119. {
  1120. struct seg_entry *se = get_seg_entry(sbi, seg->segno);
  1121. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  1122. unsigned long *target_map = SIT_I(sbi)->tmp_map;
  1123. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  1124. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  1125. int i, pos;
  1126. for (i = 0; i < entries; i++)
  1127. target_map[i] = ckpt_map[i] | cur_map[i];
  1128. pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
  1129. seg->next_blkoff = pos;
  1130. }
  1131. /*
  1132. * If a segment is written by LFS manner, next block offset is just obtained
  1133. * by increasing the current block offset. However, if a segment is written by
  1134. * SSR manner, next block offset obtained by calling __next_free_blkoff
  1135. */
  1136. static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
  1137. struct curseg_info *seg)
  1138. {
  1139. if (seg->alloc_type == SSR)
  1140. __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
  1141. else
  1142. seg->next_blkoff++;
  1143. }
  1144. /*
  1145. * This function always allocates a used segment(from dirty seglist) by SSR
  1146. * manner, so it should recover the existing segment information of valid blocks
  1147. */
  1148. static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
  1149. {
  1150. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1151. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1152. unsigned int new_segno = curseg->next_segno;
  1153. struct f2fs_summary_block *sum_node;
  1154. struct page *sum_page;
  1155. write_sum_page(sbi, curseg->sum_blk,
  1156. GET_SUM_BLOCK(sbi, curseg->segno));
  1157. __set_test_and_inuse(sbi, new_segno);
  1158. mutex_lock(&dirty_i->seglist_lock);
  1159. __remove_dirty_segment(sbi, new_segno, PRE);
  1160. __remove_dirty_segment(sbi, new_segno, DIRTY);
  1161. mutex_unlock(&dirty_i->seglist_lock);
  1162. reset_curseg(sbi, type, 1);
  1163. curseg->alloc_type = SSR;
  1164. __next_free_blkoff(sbi, curseg, 0);
  1165. if (reuse) {
  1166. sum_page = get_sum_page(sbi, new_segno);
  1167. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  1168. memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
  1169. f2fs_put_page(sum_page, 1);
  1170. }
  1171. }
  1172. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
  1173. {
  1174. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1175. const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
  1176. if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0, 0))
  1177. return v_ops->get_victim(sbi,
  1178. &(curseg)->next_segno, BG_GC, type, SSR);
  1179. /* For data segments, let's do SSR more intensively */
  1180. for (; type >= CURSEG_HOT_DATA; type--)
  1181. if (v_ops->get_victim(sbi, &(curseg)->next_segno,
  1182. BG_GC, type, SSR))
  1183. return 1;
  1184. return 0;
  1185. }
  1186. /*
  1187. * flush out current segment and replace it with new segment
  1188. * This function should be returned with success, otherwise BUG
  1189. */
  1190. static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
  1191. int type, bool force)
  1192. {
  1193. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1194. if (force)
  1195. new_curseg(sbi, type, true);
  1196. else if (type == CURSEG_WARM_NODE)
  1197. new_curseg(sbi, type, false);
  1198. else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
  1199. new_curseg(sbi, type, false);
  1200. else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
  1201. change_curseg(sbi, type, true);
  1202. else
  1203. new_curseg(sbi, type, false);
  1204. stat_inc_seg_type(sbi, curseg);
  1205. }
  1206. void allocate_new_segments(struct f2fs_sb_info *sbi)
  1207. {
  1208. struct curseg_info *curseg;
  1209. unsigned int old_segno;
  1210. int i;
  1211. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1212. curseg = CURSEG_I(sbi, i);
  1213. old_segno = curseg->segno;
  1214. SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
  1215. locate_dirty_segment(sbi, old_segno);
  1216. }
  1217. }
  1218. static const struct segment_allocation default_salloc_ops = {
  1219. .allocate_segment = allocate_segment_by_default,
  1220. };
  1221. bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  1222. {
  1223. __u64 trim_start = cpc->trim_start;
  1224. bool has_candidate = false;
  1225. mutex_lock(&SIT_I(sbi)->sentry_lock);
  1226. for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
  1227. if (add_discard_addrs(sbi, cpc, true)) {
  1228. has_candidate = true;
  1229. break;
  1230. }
  1231. }
  1232. mutex_unlock(&SIT_I(sbi)->sentry_lock);
  1233. cpc->trim_start = trim_start;
  1234. return has_candidate;
  1235. }
  1236. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
  1237. {
  1238. __u64 start = F2FS_BYTES_TO_BLK(range->start);
  1239. __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
  1240. unsigned int start_segno, end_segno;
  1241. struct cp_control cpc;
  1242. int err = 0;
  1243. if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
  1244. return -EINVAL;
  1245. cpc.trimmed = 0;
  1246. if (end <= MAIN_BLKADDR(sbi))
  1247. goto out;
  1248. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
  1249. f2fs_msg(sbi->sb, KERN_WARNING,
  1250. "Found FS corruption, run fsck to fix.");
  1251. goto out;
  1252. }
  1253. /* start/end segment number in main_area */
  1254. start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
  1255. end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
  1256. GET_SEGNO(sbi, end);
  1257. cpc.reason = CP_DISCARD;
  1258. cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
  1259. /* do checkpoint to issue discard commands safely */
  1260. for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
  1261. cpc.trim_start = start_segno;
  1262. if (sbi->discard_blks == 0)
  1263. break;
  1264. else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
  1265. cpc.trim_end = end_segno;
  1266. else
  1267. cpc.trim_end = min_t(unsigned int,
  1268. rounddown(start_segno +
  1269. BATCHED_TRIM_SEGMENTS(sbi),
  1270. sbi->segs_per_sec) - 1, end_segno);
  1271. mutex_lock(&sbi->gc_mutex);
  1272. err = write_checkpoint(sbi, &cpc);
  1273. mutex_unlock(&sbi->gc_mutex);
  1274. if (err)
  1275. break;
  1276. schedule();
  1277. }
  1278. out:
  1279. range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
  1280. return err;
  1281. }
  1282. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  1283. {
  1284. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1285. if (curseg->next_blkoff < sbi->blocks_per_seg)
  1286. return true;
  1287. return false;
  1288. }
  1289. static int __get_segment_type_2(struct page *page, enum page_type p_type)
  1290. {
  1291. if (p_type == DATA)
  1292. return CURSEG_HOT_DATA;
  1293. else
  1294. return CURSEG_HOT_NODE;
  1295. }
  1296. static int __get_segment_type_4(struct page *page, enum page_type p_type)
  1297. {
  1298. if (p_type == DATA) {
  1299. struct inode *inode = page->mapping->host;
  1300. if (S_ISDIR(inode->i_mode))
  1301. return CURSEG_HOT_DATA;
  1302. else
  1303. return CURSEG_COLD_DATA;
  1304. } else {
  1305. if (IS_DNODE(page) && is_cold_node(page))
  1306. return CURSEG_WARM_NODE;
  1307. else
  1308. return CURSEG_COLD_NODE;
  1309. }
  1310. }
  1311. static int __get_segment_type_6(struct page *page, enum page_type p_type)
  1312. {
  1313. if (p_type == DATA) {
  1314. struct inode *inode = page->mapping->host;
  1315. if (S_ISDIR(inode->i_mode))
  1316. return CURSEG_HOT_DATA;
  1317. else if (is_cold_data(page) || file_is_cold(inode))
  1318. return CURSEG_COLD_DATA;
  1319. else
  1320. return CURSEG_WARM_DATA;
  1321. } else {
  1322. if (IS_DNODE(page))
  1323. return is_cold_node(page) ? CURSEG_WARM_NODE :
  1324. CURSEG_HOT_NODE;
  1325. else
  1326. return CURSEG_COLD_NODE;
  1327. }
  1328. }
  1329. static int __get_segment_type(struct page *page, enum page_type p_type)
  1330. {
  1331. switch (F2FS_P_SB(page)->active_logs) {
  1332. case 2:
  1333. return __get_segment_type_2(page, p_type);
  1334. case 4:
  1335. return __get_segment_type_4(page, p_type);
  1336. }
  1337. /* NR_CURSEG_TYPE(6) logs by default */
  1338. f2fs_bug_on(F2FS_P_SB(page),
  1339. F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
  1340. return __get_segment_type_6(page, p_type);
  1341. }
  1342. void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  1343. block_t old_blkaddr, block_t *new_blkaddr,
  1344. struct f2fs_summary *sum, int type)
  1345. {
  1346. struct sit_info *sit_i = SIT_I(sbi);
  1347. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1348. mutex_lock(&curseg->curseg_mutex);
  1349. mutex_lock(&sit_i->sentry_lock);
  1350. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  1351. f2fs_wait_discard_bio(sbi, *new_blkaddr);
  1352. /*
  1353. * __add_sum_entry should be resided under the curseg_mutex
  1354. * because, this function updates a summary entry in the
  1355. * current summary block.
  1356. */
  1357. __add_sum_entry(sbi, type, sum);
  1358. __refresh_next_blkoff(sbi, curseg);
  1359. stat_inc_block_count(sbi, curseg);
  1360. if (!__has_curseg_space(sbi, type))
  1361. sit_i->s_ops->allocate_segment(sbi, type, false);
  1362. /*
  1363. * SIT information should be updated before segment allocation,
  1364. * since SSR needs latest valid block information.
  1365. */
  1366. refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
  1367. mutex_unlock(&sit_i->sentry_lock);
  1368. if (page && IS_NODESEG(type))
  1369. fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
  1370. mutex_unlock(&curseg->curseg_mutex);
  1371. }
  1372. static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
  1373. {
  1374. int type = __get_segment_type(fio->page, fio->type);
  1375. int err;
  1376. if (fio->type == NODE || fio->type == DATA)
  1377. mutex_lock(&fio->sbi->wio_mutex[fio->type]);
  1378. reallocate:
  1379. allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
  1380. &fio->new_blkaddr, sum, type);
  1381. /* writeout dirty page into bdev */
  1382. err = f2fs_submit_page_mbio(fio);
  1383. if (err == -EAGAIN) {
  1384. fio->old_blkaddr = fio->new_blkaddr;
  1385. goto reallocate;
  1386. }
  1387. if (fio->type == NODE || fio->type == DATA)
  1388. mutex_unlock(&fio->sbi->wio_mutex[fio->type]);
  1389. }
  1390. void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
  1391. {
  1392. struct f2fs_io_info fio = {
  1393. .sbi = sbi,
  1394. .type = META,
  1395. .op = REQ_OP_WRITE,
  1396. .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
  1397. .old_blkaddr = page->index,
  1398. .new_blkaddr = page->index,
  1399. .page = page,
  1400. .encrypted_page = NULL,
  1401. };
  1402. if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
  1403. fio.op_flags &= ~REQ_META;
  1404. set_page_writeback(page);
  1405. f2fs_submit_page_mbio(&fio);
  1406. }
  1407. void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
  1408. {
  1409. struct f2fs_summary sum;
  1410. set_summary(&sum, nid, 0, 0);
  1411. do_write_page(&sum, fio);
  1412. }
  1413. void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
  1414. {
  1415. struct f2fs_sb_info *sbi = fio->sbi;
  1416. struct f2fs_summary sum;
  1417. struct node_info ni;
  1418. f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
  1419. get_node_info(sbi, dn->nid, &ni);
  1420. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  1421. do_write_page(&sum, fio);
  1422. f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
  1423. }
  1424. void rewrite_data_page(struct f2fs_io_info *fio)
  1425. {
  1426. fio->new_blkaddr = fio->old_blkaddr;
  1427. stat_inc_inplace_blocks(fio->sbi);
  1428. f2fs_submit_page_mbio(fio);
  1429. }
  1430. void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  1431. block_t old_blkaddr, block_t new_blkaddr,
  1432. bool recover_curseg, bool recover_newaddr)
  1433. {
  1434. struct sit_info *sit_i = SIT_I(sbi);
  1435. struct curseg_info *curseg;
  1436. unsigned int segno, old_cursegno;
  1437. struct seg_entry *se;
  1438. int type;
  1439. unsigned short old_blkoff;
  1440. segno = GET_SEGNO(sbi, new_blkaddr);
  1441. se = get_seg_entry(sbi, segno);
  1442. type = se->type;
  1443. if (!recover_curseg) {
  1444. /* for recovery flow */
  1445. if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
  1446. if (old_blkaddr == NULL_ADDR)
  1447. type = CURSEG_COLD_DATA;
  1448. else
  1449. type = CURSEG_WARM_DATA;
  1450. }
  1451. } else {
  1452. if (!IS_CURSEG(sbi, segno))
  1453. type = CURSEG_WARM_DATA;
  1454. }
  1455. curseg = CURSEG_I(sbi, type);
  1456. mutex_lock(&curseg->curseg_mutex);
  1457. mutex_lock(&sit_i->sentry_lock);
  1458. old_cursegno = curseg->segno;
  1459. old_blkoff = curseg->next_blkoff;
  1460. /* change the current segment */
  1461. if (segno != curseg->segno) {
  1462. curseg->next_segno = segno;
  1463. change_curseg(sbi, type, true);
  1464. }
  1465. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  1466. __add_sum_entry(sbi, type, sum);
  1467. if (!recover_curseg || recover_newaddr)
  1468. update_sit_entry(sbi, new_blkaddr, 1);
  1469. if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
  1470. update_sit_entry(sbi, old_blkaddr, -1);
  1471. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  1472. locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
  1473. locate_dirty_segment(sbi, old_cursegno);
  1474. if (recover_curseg) {
  1475. if (old_cursegno != curseg->segno) {
  1476. curseg->next_segno = old_cursegno;
  1477. change_curseg(sbi, type, true);
  1478. }
  1479. curseg->next_blkoff = old_blkoff;
  1480. }
  1481. mutex_unlock(&sit_i->sentry_lock);
  1482. mutex_unlock(&curseg->curseg_mutex);
  1483. }
  1484. void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
  1485. block_t old_addr, block_t new_addr,
  1486. unsigned char version, bool recover_curseg,
  1487. bool recover_newaddr)
  1488. {
  1489. struct f2fs_summary sum;
  1490. set_summary(&sum, dn->nid, dn->ofs_in_node, version);
  1491. __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
  1492. recover_curseg, recover_newaddr);
  1493. f2fs_update_data_blkaddr(dn, new_addr);
  1494. }
  1495. void f2fs_wait_on_page_writeback(struct page *page,
  1496. enum page_type type, bool ordered)
  1497. {
  1498. if (PageWriteback(page)) {
  1499. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1500. f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, type, WRITE);
  1501. if (ordered)
  1502. wait_on_page_writeback(page);
  1503. else
  1504. wait_for_stable_page(page);
  1505. }
  1506. }
  1507. void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
  1508. block_t blkaddr)
  1509. {
  1510. struct page *cpage;
  1511. if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
  1512. return;
  1513. cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
  1514. if (cpage) {
  1515. f2fs_wait_on_page_writeback(cpage, DATA, true);
  1516. f2fs_put_page(cpage, 1);
  1517. }
  1518. }
  1519. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  1520. {
  1521. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1522. struct curseg_info *seg_i;
  1523. unsigned char *kaddr;
  1524. struct page *page;
  1525. block_t start;
  1526. int i, j, offset;
  1527. start = start_sum_block(sbi);
  1528. page = get_meta_page(sbi, start++);
  1529. kaddr = (unsigned char *)page_address(page);
  1530. /* Step 1: restore nat cache */
  1531. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1532. memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
  1533. /* Step 2: restore sit cache */
  1534. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1535. memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
  1536. offset = 2 * SUM_JOURNAL_SIZE;
  1537. /* Step 3: restore summary entries */
  1538. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1539. unsigned short blk_off;
  1540. unsigned int segno;
  1541. seg_i = CURSEG_I(sbi, i);
  1542. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  1543. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  1544. seg_i->next_segno = segno;
  1545. reset_curseg(sbi, i, 0);
  1546. seg_i->alloc_type = ckpt->alloc_type[i];
  1547. seg_i->next_blkoff = blk_off;
  1548. if (seg_i->alloc_type == SSR)
  1549. blk_off = sbi->blocks_per_seg;
  1550. for (j = 0; j < blk_off; j++) {
  1551. struct f2fs_summary *s;
  1552. s = (struct f2fs_summary *)(kaddr + offset);
  1553. seg_i->sum_blk->entries[j] = *s;
  1554. offset += SUMMARY_SIZE;
  1555. if (offset + SUMMARY_SIZE <= PAGE_SIZE -
  1556. SUM_FOOTER_SIZE)
  1557. continue;
  1558. f2fs_put_page(page, 1);
  1559. page = NULL;
  1560. page = get_meta_page(sbi, start++);
  1561. kaddr = (unsigned char *)page_address(page);
  1562. offset = 0;
  1563. }
  1564. }
  1565. f2fs_put_page(page, 1);
  1566. return 0;
  1567. }
  1568. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  1569. {
  1570. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1571. struct f2fs_summary_block *sum;
  1572. struct curseg_info *curseg;
  1573. struct page *new;
  1574. unsigned short blk_off;
  1575. unsigned int segno = 0;
  1576. block_t blk_addr = 0;
  1577. /* get segment number and block addr */
  1578. if (IS_DATASEG(type)) {
  1579. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  1580. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  1581. CURSEG_HOT_DATA]);
  1582. if (__exist_node_summaries(sbi))
  1583. blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
  1584. else
  1585. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  1586. } else {
  1587. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  1588. CURSEG_HOT_NODE]);
  1589. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  1590. CURSEG_HOT_NODE]);
  1591. if (__exist_node_summaries(sbi))
  1592. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  1593. type - CURSEG_HOT_NODE);
  1594. else
  1595. blk_addr = GET_SUM_BLOCK(sbi, segno);
  1596. }
  1597. new = get_meta_page(sbi, blk_addr);
  1598. sum = (struct f2fs_summary_block *)page_address(new);
  1599. if (IS_NODESEG(type)) {
  1600. if (__exist_node_summaries(sbi)) {
  1601. struct f2fs_summary *ns = &sum->entries[0];
  1602. int i;
  1603. for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
  1604. ns->version = 0;
  1605. ns->ofs_in_node = 0;
  1606. }
  1607. } else {
  1608. int err;
  1609. err = restore_node_summary(sbi, segno, sum);
  1610. if (err) {
  1611. f2fs_put_page(new, 1);
  1612. return err;
  1613. }
  1614. }
  1615. }
  1616. /* set uncompleted segment to curseg */
  1617. curseg = CURSEG_I(sbi, type);
  1618. mutex_lock(&curseg->curseg_mutex);
  1619. /* update journal info */
  1620. down_write(&curseg->journal_rwsem);
  1621. memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
  1622. up_write(&curseg->journal_rwsem);
  1623. memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
  1624. memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
  1625. curseg->next_segno = segno;
  1626. reset_curseg(sbi, type, 0);
  1627. curseg->alloc_type = ckpt->alloc_type[type];
  1628. curseg->next_blkoff = blk_off;
  1629. mutex_unlock(&curseg->curseg_mutex);
  1630. f2fs_put_page(new, 1);
  1631. return 0;
  1632. }
  1633. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  1634. {
  1635. int type = CURSEG_HOT_DATA;
  1636. int err;
  1637. if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
  1638. int npages = npages_for_summary_flush(sbi, true);
  1639. if (npages >= 2)
  1640. ra_meta_pages(sbi, start_sum_block(sbi), npages,
  1641. META_CP, true);
  1642. /* restore for compacted data summary */
  1643. if (read_compacted_summaries(sbi))
  1644. return -EINVAL;
  1645. type = CURSEG_HOT_NODE;
  1646. }
  1647. if (__exist_node_summaries(sbi))
  1648. ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
  1649. NR_CURSEG_TYPE - type, META_CP, true);
  1650. for (; type <= CURSEG_COLD_NODE; type++) {
  1651. err = read_normal_summaries(sbi, type);
  1652. if (err)
  1653. return err;
  1654. }
  1655. return 0;
  1656. }
  1657. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  1658. {
  1659. struct page *page;
  1660. unsigned char *kaddr;
  1661. struct f2fs_summary *summary;
  1662. struct curseg_info *seg_i;
  1663. int written_size = 0;
  1664. int i, j;
  1665. page = grab_meta_page(sbi, blkaddr++);
  1666. kaddr = (unsigned char *)page_address(page);
  1667. /* Step 1: write nat cache */
  1668. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1669. memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
  1670. written_size += SUM_JOURNAL_SIZE;
  1671. /* Step 2: write sit cache */
  1672. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1673. memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
  1674. written_size += SUM_JOURNAL_SIZE;
  1675. /* Step 3: write summary entries */
  1676. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1677. unsigned short blkoff;
  1678. seg_i = CURSEG_I(sbi, i);
  1679. if (sbi->ckpt->alloc_type[i] == SSR)
  1680. blkoff = sbi->blocks_per_seg;
  1681. else
  1682. blkoff = curseg_blkoff(sbi, i);
  1683. for (j = 0; j < blkoff; j++) {
  1684. if (!page) {
  1685. page = grab_meta_page(sbi, blkaddr++);
  1686. kaddr = (unsigned char *)page_address(page);
  1687. written_size = 0;
  1688. }
  1689. summary = (struct f2fs_summary *)(kaddr + written_size);
  1690. *summary = seg_i->sum_blk->entries[j];
  1691. written_size += SUMMARY_SIZE;
  1692. if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
  1693. SUM_FOOTER_SIZE)
  1694. continue;
  1695. set_page_dirty(page);
  1696. f2fs_put_page(page, 1);
  1697. page = NULL;
  1698. }
  1699. }
  1700. if (page) {
  1701. set_page_dirty(page);
  1702. f2fs_put_page(page, 1);
  1703. }
  1704. }
  1705. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  1706. block_t blkaddr, int type)
  1707. {
  1708. int i, end;
  1709. if (IS_DATASEG(type))
  1710. end = type + NR_CURSEG_DATA_TYPE;
  1711. else
  1712. end = type + NR_CURSEG_NODE_TYPE;
  1713. for (i = type; i < end; i++)
  1714. write_current_sum_page(sbi, i, blkaddr + (i - type));
  1715. }
  1716. void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1717. {
  1718. if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
  1719. write_compacted_summaries(sbi, start_blk);
  1720. else
  1721. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  1722. }
  1723. void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1724. {
  1725. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  1726. }
  1727. int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
  1728. unsigned int val, int alloc)
  1729. {
  1730. int i;
  1731. if (type == NAT_JOURNAL) {
  1732. for (i = 0; i < nats_in_cursum(journal); i++) {
  1733. if (le32_to_cpu(nid_in_journal(journal, i)) == val)
  1734. return i;
  1735. }
  1736. if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
  1737. return update_nats_in_cursum(journal, 1);
  1738. } else if (type == SIT_JOURNAL) {
  1739. for (i = 0; i < sits_in_cursum(journal); i++)
  1740. if (le32_to_cpu(segno_in_journal(journal, i)) == val)
  1741. return i;
  1742. if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
  1743. return update_sits_in_cursum(journal, 1);
  1744. }
  1745. return -1;
  1746. }
  1747. static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
  1748. unsigned int segno)
  1749. {
  1750. return get_meta_page(sbi, current_sit_addr(sbi, segno));
  1751. }
  1752. static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
  1753. unsigned int start)
  1754. {
  1755. struct sit_info *sit_i = SIT_I(sbi);
  1756. struct page *src_page, *dst_page;
  1757. pgoff_t src_off, dst_off;
  1758. void *src_addr, *dst_addr;
  1759. src_off = current_sit_addr(sbi, start);
  1760. dst_off = next_sit_addr(sbi, src_off);
  1761. /* get current sit block page without lock */
  1762. src_page = get_meta_page(sbi, src_off);
  1763. dst_page = grab_meta_page(sbi, dst_off);
  1764. f2fs_bug_on(sbi, PageDirty(src_page));
  1765. src_addr = page_address(src_page);
  1766. dst_addr = page_address(dst_page);
  1767. memcpy(dst_addr, src_addr, PAGE_SIZE);
  1768. set_page_dirty(dst_page);
  1769. f2fs_put_page(src_page, 1);
  1770. set_to_next_sit(sit_i, start);
  1771. return dst_page;
  1772. }
  1773. static struct sit_entry_set *grab_sit_entry_set(void)
  1774. {
  1775. struct sit_entry_set *ses =
  1776. f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
  1777. ses->entry_cnt = 0;
  1778. INIT_LIST_HEAD(&ses->set_list);
  1779. return ses;
  1780. }
  1781. static void release_sit_entry_set(struct sit_entry_set *ses)
  1782. {
  1783. list_del(&ses->set_list);
  1784. kmem_cache_free(sit_entry_set_slab, ses);
  1785. }
  1786. static void adjust_sit_entry_set(struct sit_entry_set *ses,
  1787. struct list_head *head)
  1788. {
  1789. struct sit_entry_set *next = ses;
  1790. if (list_is_last(&ses->set_list, head))
  1791. return;
  1792. list_for_each_entry_continue(next, head, set_list)
  1793. if (ses->entry_cnt <= next->entry_cnt)
  1794. break;
  1795. list_move_tail(&ses->set_list, &next->set_list);
  1796. }
  1797. static void add_sit_entry(unsigned int segno, struct list_head *head)
  1798. {
  1799. struct sit_entry_set *ses;
  1800. unsigned int start_segno = START_SEGNO(segno);
  1801. list_for_each_entry(ses, head, set_list) {
  1802. if (ses->start_segno == start_segno) {
  1803. ses->entry_cnt++;
  1804. adjust_sit_entry_set(ses, head);
  1805. return;
  1806. }
  1807. }
  1808. ses = grab_sit_entry_set();
  1809. ses->start_segno = start_segno;
  1810. ses->entry_cnt++;
  1811. list_add(&ses->set_list, head);
  1812. }
  1813. static void add_sits_in_set(struct f2fs_sb_info *sbi)
  1814. {
  1815. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1816. struct list_head *set_list = &sm_info->sit_entry_set;
  1817. unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
  1818. unsigned int segno;
  1819. for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
  1820. add_sit_entry(segno, set_list);
  1821. }
  1822. static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
  1823. {
  1824. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1825. struct f2fs_journal *journal = curseg->journal;
  1826. int i;
  1827. down_write(&curseg->journal_rwsem);
  1828. for (i = 0; i < sits_in_cursum(journal); i++) {
  1829. unsigned int segno;
  1830. bool dirtied;
  1831. segno = le32_to_cpu(segno_in_journal(journal, i));
  1832. dirtied = __mark_sit_entry_dirty(sbi, segno);
  1833. if (!dirtied)
  1834. add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
  1835. }
  1836. update_sits_in_cursum(journal, -i);
  1837. up_write(&curseg->journal_rwsem);
  1838. }
  1839. /*
  1840. * CP calls this function, which flushes SIT entries including sit_journal,
  1841. * and moves prefree segs to free segs.
  1842. */
  1843. void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  1844. {
  1845. struct sit_info *sit_i = SIT_I(sbi);
  1846. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  1847. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1848. struct f2fs_journal *journal = curseg->journal;
  1849. struct sit_entry_set *ses, *tmp;
  1850. struct list_head *head = &SM_I(sbi)->sit_entry_set;
  1851. bool to_journal = true;
  1852. struct seg_entry *se;
  1853. mutex_lock(&sit_i->sentry_lock);
  1854. if (!sit_i->dirty_sentries)
  1855. goto out;
  1856. /*
  1857. * add and account sit entries of dirty bitmap in sit entry
  1858. * set temporarily
  1859. */
  1860. add_sits_in_set(sbi);
  1861. /*
  1862. * if there are no enough space in journal to store dirty sit
  1863. * entries, remove all entries from journal and add and account
  1864. * them in sit entry set.
  1865. */
  1866. if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
  1867. remove_sits_in_journal(sbi);
  1868. /*
  1869. * there are two steps to flush sit entries:
  1870. * #1, flush sit entries to journal in current cold data summary block.
  1871. * #2, flush sit entries to sit page.
  1872. */
  1873. list_for_each_entry_safe(ses, tmp, head, set_list) {
  1874. struct page *page = NULL;
  1875. struct f2fs_sit_block *raw_sit = NULL;
  1876. unsigned int start_segno = ses->start_segno;
  1877. unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
  1878. (unsigned long)MAIN_SEGS(sbi));
  1879. unsigned int segno = start_segno;
  1880. if (to_journal &&
  1881. !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
  1882. to_journal = false;
  1883. if (to_journal) {
  1884. down_write(&curseg->journal_rwsem);
  1885. } else {
  1886. page = get_next_sit_page(sbi, start_segno);
  1887. raw_sit = page_address(page);
  1888. }
  1889. /* flush dirty sit entries in region of current sit set */
  1890. for_each_set_bit_from(segno, bitmap, end) {
  1891. int offset, sit_offset;
  1892. se = get_seg_entry(sbi, segno);
  1893. /* add discard candidates */
  1894. if (cpc->reason != CP_DISCARD) {
  1895. cpc->trim_start = segno;
  1896. add_discard_addrs(sbi, cpc, false);
  1897. }
  1898. if (to_journal) {
  1899. offset = lookup_journal_in_cursum(journal,
  1900. SIT_JOURNAL, segno, 1);
  1901. f2fs_bug_on(sbi, offset < 0);
  1902. segno_in_journal(journal, offset) =
  1903. cpu_to_le32(segno);
  1904. seg_info_to_raw_sit(se,
  1905. &sit_in_journal(journal, offset));
  1906. } else {
  1907. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  1908. seg_info_to_raw_sit(se,
  1909. &raw_sit->entries[sit_offset]);
  1910. }
  1911. __clear_bit(segno, bitmap);
  1912. sit_i->dirty_sentries--;
  1913. ses->entry_cnt--;
  1914. }
  1915. if (to_journal)
  1916. up_write(&curseg->journal_rwsem);
  1917. else
  1918. f2fs_put_page(page, 1);
  1919. f2fs_bug_on(sbi, ses->entry_cnt);
  1920. release_sit_entry_set(ses);
  1921. }
  1922. f2fs_bug_on(sbi, !list_empty(head));
  1923. f2fs_bug_on(sbi, sit_i->dirty_sentries);
  1924. out:
  1925. if (cpc->reason == CP_DISCARD) {
  1926. __u64 trim_start = cpc->trim_start;
  1927. for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
  1928. add_discard_addrs(sbi, cpc, false);
  1929. cpc->trim_start = trim_start;
  1930. }
  1931. mutex_unlock(&sit_i->sentry_lock);
  1932. set_prefree_as_free_segments(sbi);
  1933. }
  1934. static int build_sit_info(struct f2fs_sb_info *sbi)
  1935. {
  1936. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1937. struct sit_info *sit_i;
  1938. unsigned int sit_segs, start;
  1939. char *src_bitmap, *dst_bitmap;
  1940. unsigned int bitmap_size;
  1941. /* allocate memory for SIT information */
  1942. sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
  1943. if (!sit_i)
  1944. return -ENOMEM;
  1945. SM_I(sbi)->sit_info = sit_i;
  1946. sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
  1947. sizeof(struct seg_entry), GFP_KERNEL);
  1948. if (!sit_i->sentries)
  1949. return -ENOMEM;
  1950. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  1951. sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
  1952. if (!sit_i->dirty_sentries_bitmap)
  1953. return -ENOMEM;
  1954. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  1955. sit_i->sentries[start].cur_valid_map
  1956. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1957. sit_i->sentries[start].ckpt_valid_map
  1958. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1959. if (!sit_i->sentries[start].cur_valid_map ||
  1960. !sit_i->sentries[start].ckpt_valid_map)
  1961. return -ENOMEM;
  1962. if (f2fs_discard_en(sbi)) {
  1963. sit_i->sentries[start].discard_map
  1964. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1965. if (!sit_i->sentries[start].discard_map)
  1966. return -ENOMEM;
  1967. }
  1968. }
  1969. sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1970. if (!sit_i->tmp_map)
  1971. return -ENOMEM;
  1972. if (sbi->segs_per_sec > 1) {
  1973. sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
  1974. sizeof(struct sec_entry), GFP_KERNEL);
  1975. if (!sit_i->sec_entries)
  1976. return -ENOMEM;
  1977. }
  1978. /* get information related with SIT */
  1979. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  1980. /* setup SIT bitmap from ckeckpoint pack */
  1981. bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  1982. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  1983. dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  1984. if (!dst_bitmap)
  1985. return -ENOMEM;
  1986. /* init SIT information */
  1987. sit_i->s_ops = &default_salloc_ops;
  1988. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  1989. sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
  1990. sit_i->written_valid_blocks = 0;
  1991. sit_i->sit_bitmap = dst_bitmap;
  1992. sit_i->bitmap_size = bitmap_size;
  1993. sit_i->dirty_sentries = 0;
  1994. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  1995. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  1996. sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
  1997. mutex_init(&sit_i->sentry_lock);
  1998. return 0;
  1999. }
  2000. static int build_free_segmap(struct f2fs_sb_info *sbi)
  2001. {
  2002. struct free_segmap_info *free_i;
  2003. unsigned int bitmap_size, sec_bitmap_size;
  2004. /* allocate memory for free segmap information */
  2005. free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
  2006. if (!free_i)
  2007. return -ENOMEM;
  2008. SM_I(sbi)->free_info = free_i;
  2009. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  2010. free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
  2011. if (!free_i->free_segmap)
  2012. return -ENOMEM;
  2013. sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  2014. free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
  2015. if (!free_i->free_secmap)
  2016. return -ENOMEM;
  2017. /* set all segments as dirty temporarily */
  2018. memset(free_i->free_segmap, 0xff, bitmap_size);
  2019. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  2020. /* init free segmap information */
  2021. free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
  2022. free_i->free_segments = 0;
  2023. free_i->free_sections = 0;
  2024. spin_lock_init(&free_i->segmap_lock);
  2025. return 0;
  2026. }
  2027. static int build_curseg(struct f2fs_sb_info *sbi)
  2028. {
  2029. struct curseg_info *array;
  2030. int i;
  2031. array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
  2032. if (!array)
  2033. return -ENOMEM;
  2034. SM_I(sbi)->curseg_array = array;
  2035. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  2036. mutex_init(&array[i].curseg_mutex);
  2037. array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
  2038. if (!array[i].sum_blk)
  2039. return -ENOMEM;
  2040. init_rwsem(&array[i].journal_rwsem);
  2041. array[i].journal = kzalloc(sizeof(struct f2fs_journal),
  2042. GFP_KERNEL);
  2043. if (!array[i].journal)
  2044. return -ENOMEM;
  2045. array[i].segno = NULL_SEGNO;
  2046. array[i].next_blkoff = 0;
  2047. }
  2048. return restore_curseg_summaries(sbi);
  2049. }
  2050. static void build_sit_entries(struct f2fs_sb_info *sbi)
  2051. {
  2052. struct sit_info *sit_i = SIT_I(sbi);
  2053. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  2054. struct f2fs_journal *journal = curseg->journal;
  2055. struct seg_entry *se;
  2056. struct f2fs_sit_entry sit;
  2057. int sit_blk_cnt = SIT_BLK_CNT(sbi);
  2058. unsigned int i, start, end;
  2059. unsigned int readed, start_blk = 0;
  2060. do {
  2061. readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
  2062. META_SIT, true);
  2063. start = start_blk * sit_i->sents_per_block;
  2064. end = (start_blk + readed) * sit_i->sents_per_block;
  2065. for (; start < end && start < MAIN_SEGS(sbi); start++) {
  2066. struct f2fs_sit_block *sit_blk;
  2067. struct page *page;
  2068. se = &sit_i->sentries[start];
  2069. page = get_current_sit_page(sbi, start);
  2070. sit_blk = (struct f2fs_sit_block *)page_address(page);
  2071. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  2072. f2fs_put_page(page, 1);
  2073. check_block_count(sbi, start, &sit);
  2074. seg_info_from_raw_sit(se, &sit);
  2075. /* build discard map only one time */
  2076. if (f2fs_discard_en(sbi)) {
  2077. memcpy(se->discard_map, se->cur_valid_map,
  2078. SIT_VBLOCK_MAP_SIZE);
  2079. sbi->discard_blks += sbi->blocks_per_seg -
  2080. se->valid_blocks;
  2081. }
  2082. if (sbi->segs_per_sec > 1)
  2083. get_sec_entry(sbi, start)->valid_blocks +=
  2084. se->valid_blocks;
  2085. }
  2086. start_blk += readed;
  2087. } while (start_blk < sit_blk_cnt);
  2088. down_read(&curseg->journal_rwsem);
  2089. for (i = 0; i < sits_in_cursum(journal); i++) {
  2090. unsigned int old_valid_blocks;
  2091. start = le32_to_cpu(segno_in_journal(journal, i));
  2092. se = &sit_i->sentries[start];
  2093. sit = sit_in_journal(journal, i);
  2094. old_valid_blocks = se->valid_blocks;
  2095. check_block_count(sbi, start, &sit);
  2096. seg_info_from_raw_sit(se, &sit);
  2097. if (f2fs_discard_en(sbi)) {
  2098. memcpy(se->discard_map, se->cur_valid_map,
  2099. SIT_VBLOCK_MAP_SIZE);
  2100. sbi->discard_blks += old_valid_blocks -
  2101. se->valid_blocks;
  2102. }
  2103. if (sbi->segs_per_sec > 1)
  2104. get_sec_entry(sbi, start)->valid_blocks +=
  2105. se->valid_blocks - old_valid_blocks;
  2106. }
  2107. up_read(&curseg->journal_rwsem);
  2108. }
  2109. static void init_free_segmap(struct f2fs_sb_info *sbi)
  2110. {
  2111. unsigned int start;
  2112. int type;
  2113. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  2114. struct seg_entry *sentry = get_seg_entry(sbi, start);
  2115. if (!sentry->valid_blocks)
  2116. __set_free(sbi, start);
  2117. else
  2118. SIT_I(sbi)->written_valid_blocks +=
  2119. sentry->valid_blocks;
  2120. }
  2121. /* set use the current segments */
  2122. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  2123. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  2124. __set_test_and_inuse(sbi, curseg_t->segno);
  2125. }
  2126. }
  2127. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  2128. {
  2129. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2130. struct free_segmap_info *free_i = FREE_I(sbi);
  2131. unsigned int segno = 0, offset = 0;
  2132. unsigned short valid_blocks;
  2133. while (1) {
  2134. /* find dirty segment based on free segmap */
  2135. segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
  2136. if (segno >= MAIN_SEGS(sbi))
  2137. break;
  2138. offset = segno + 1;
  2139. valid_blocks = get_valid_blocks(sbi, segno, 0);
  2140. if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
  2141. continue;
  2142. if (valid_blocks > sbi->blocks_per_seg) {
  2143. f2fs_bug_on(sbi, 1);
  2144. continue;
  2145. }
  2146. mutex_lock(&dirty_i->seglist_lock);
  2147. __locate_dirty_segment(sbi, segno, DIRTY);
  2148. mutex_unlock(&dirty_i->seglist_lock);
  2149. }
  2150. }
  2151. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  2152. {
  2153. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2154. unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  2155. dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
  2156. if (!dirty_i->victim_secmap)
  2157. return -ENOMEM;
  2158. return 0;
  2159. }
  2160. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  2161. {
  2162. struct dirty_seglist_info *dirty_i;
  2163. unsigned int bitmap_size, i;
  2164. /* allocate memory for dirty segments list information */
  2165. dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
  2166. if (!dirty_i)
  2167. return -ENOMEM;
  2168. SM_I(sbi)->dirty_info = dirty_i;
  2169. mutex_init(&dirty_i->seglist_lock);
  2170. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  2171. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  2172. dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
  2173. if (!dirty_i->dirty_segmap[i])
  2174. return -ENOMEM;
  2175. }
  2176. init_dirty_segmap(sbi);
  2177. return init_victim_secmap(sbi);
  2178. }
  2179. /*
  2180. * Update min, max modified time for cost-benefit GC algorithm
  2181. */
  2182. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  2183. {
  2184. struct sit_info *sit_i = SIT_I(sbi);
  2185. unsigned int segno;
  2186. mutex_lock(&sit_i->sentry_lock);
  2187. sit_i->min_mtime = LLONG_MAX;
  2188. for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
  2189. unsigned int i;
  2190. unsigned long long mtime = 0;
  2191. for (i = 0; i < sbi->segs_per_sec; i++)
  2192. mtime += get_seg_entry(sbi, segno + i)->mtime;
  2193. mtime = div_u64(mtime, sbi->segs_per_sec);
  2194. if (sit_i->min_mtime > mtime)
  2195. sit_i->min_mtime = mtime;
  2196. }
  2197. sit_i->max_mtime = get_mtime(sbi);
  2198. mutex_unlock(&sit_i->sentry_lock);
  2199. }
  2200. int build_segment_manager(struct f2fs_sb_info *sbi)
  2201. {
  2202. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  2203. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2204. struct f2fs_sm_info *sm_info;
  2205. int err;
  2206. sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
  2207. if (!sm_info)
  2208. return -ENOMEM;
  2209. /* init sm info */
  2210. sbi->sm_info = sm_info;
  2211. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  2212. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  2213. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  2214. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  2215. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  2216. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  2217. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  2218. sm_info->rec_prefree_segments = sm_info->main_segments *
  2219. DEF_RECLAIM_PREFREE_SEGMENTS / 100;
  2220. if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
  2221. sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
  2222. if (!test_opt(sbi, LFS))
  2223. sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
  2224. sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
  2225. sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
  2226. INIT_LIST_HEAD(&sm_info->discard_list);
  2227. INIT_LIST_HEAD(&sm_info->wait_list);
  2228. sm_info->nr_discards = 0;
  2229. sm_info->max_discards = 0;
  2230. sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
  2231. INIT_LIST_HEAD(&sm_info->sit_entry_set);
  2232. if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
  2233. err = create_flush_cmd_control(sbi);
  2234. if (err)
  2235. return err;
  2236. }
  2237. err = build_sit_info(sbi);
  2238. if (err)
  2239. return err;
  2240. err = build_free_segmap(sbi);
  2241. if (err)
  2242. return err;
  2243. err = build_curseg(sbi);
  2244. if (err)
  2245. return err;
  2246. /* reinit free segmap based on SIT */
  2247. build_sit_entries(sbi);
  2248. init_free_segmap(sbi);
  2249. err = build_dirty_segmap(sbi);
  2250. if (err)
  2251. return err;
  2252. init_min_max_mtime(sbi);
  2253. return 0;
  2254. }
  2255. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  2256. enum dirty_type dirty_type)
  2257. {
  2258. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2259. mutex_lock(&dirty_i->seglist_lock);
  2260. kvfree(dirty_i->dirty_segmap[dirty_type]);
  2261. dirty_i->nr_dirty[dirty_type] = 0;
  2262. mutex_unlock(&dirty_i->seglist_lock);
  2263. }
  2264. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  2265. {
  2266. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2267. kvfree(dirty_i->victim_secmap);
  2268. }
  2269. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  2270. {
  2271. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2272. int i;
  2273. if (!dirty_i)
  2274. return;
  2275. /* discard pre-free/dirty segments list */
  2276. for (i = 0; i < NR_DIRTY_TYPE; i++)
  2277. discard_dirty_segmap(sbi, i);
  2278. destroy_victim_secmap(sbi);
  2279. SM_I(sbi)->dirty_info = NULL;
  2280. kfree(dirty_i);
  2281. }
  2282. static void destroy_curseg(struct f2fs_sb_info *sbi)
  2283. {
  2284. struct curseg_info *array = SM_I(sbi)->curseg_array;
  2285. int i;
  2286. if (!array)
  2287. return;
  2288. SM_I(sbi)->curseg_array = NULL;
  2289. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  2290. kfree(array[i].sum_blk);
  2291. kfree(array[i].journal);
  2292. }
  2293. kfree(array);
  2294. }
  2295. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  2296. {
  2297. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  2298. if (!free_i)
  2299. return;
  2300. SM_I(sbi)->free_info = NULL;
  2301. kvfree(free_i->free_segmap);
  2302. kvfree(free_i->free_secmap);
  2303. kfree(free_i);
  2304. }
  2305. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  2306. {
  2307. struct sit_info *sit_i = SIT_I(sbi);
  2308. unsigned int start;
  2309. if (!sit_i)
  2310. return;
  2311. if (sit_i->sentries) {
  2312. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  2313. kfree(sit_i->sentries[start].cur_valid_map);
  2314. kfree(sit_i->sentries[start].ckpt_valid_map);
  2315. kfree(sit_i->sentries[start].discard_map);
  2316. }
  2317. }
  2318. kfree(sit_i->tmp_map);
  2319. kvfree(sit_i->sentries);
  2320. kvfree(sit_i->sec_entries);
  2321. kvfree(sit_i->dirty_sentries_bitmap);
  2322. SM_I(sbi)->sit_info = NULL;
  2323. kfree(sit_i->sit_bitmap);
  2324. kfree(sit_i);
  2325. }
  2326. void destroy_segment_manager(struct f2fs_sb_info *sbi)
  2327. {
  2328. struct f2fs_sm_info *sm_info = SM_I(sbi);
  2329. if (!sm_info)
  2330. return;
  2331. destroy_flush_cmd_control(sbi, true);
  2332. destroy_dirty_segmap(sbi);
  2333. destroy_curseg(sbi);
  2334. destroy_free_segmap(sbi);
  2335. destroy_sit_info(sbi);
  2336. sbi->sm_info = NULL;
  2337. kfree(sm_info);
  2338. }
  2339. int __init create_segment_manager_caches(void)
  2340. {
  2341. discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
  2342. sizeof(struct discard_entry));
  2343. if (!discard_entry_slab)
  2344. goto fail;
  2345. bio_entry_slab = f2fs_kmem_cache_create("bio_entry",
  2346. sizeof(struct bio_entry));
  2347. if (!bio_entry_slab)
  2348. goto destroy_discard_entry;
  2349. sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
  2350. sizeof(struct sit_entry_set));
  2351. if (!sit_entry_set_slab)
  2352. goto destroy_bio_entry;
  2353. inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
  2354. sizeof(struct inmem_pages));
  2355. if (!inmem_entry_slab)
  2356. goto destroy_sit_entry_set;
  2357. return 0;
  2358. destroy_sit_entry_set:
  2359. kmem_cache_destroy(sit_entry_set_slab);
  2360. destroy_bio_entry:
  2361. kmem_cache_destroy(bio_entry_slab);
  2362. destroy_discard_entry:
  2363. kmem_cache_destroy(discard_entry_slab);
  2364. fail:
  2365. return -ENOMEM;
  2366. }
  2367. void destroy_segment_manager_caches(void)
  2368. {
  2369. kmem_cache_destroy(sit_entry_set_slab);
  2370. kmem_cache_destroy(bio_entry_slab);
  2371. kmem_cache_destroy(discard_entry_slab);
  2372. kmem_cache_destroy(inmem_entry_slab);
  2373. }