segment.c 65 KB

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