segment.c 66 KB

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