segment.c 69 KB

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