segment.c 69 KB

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