segment.c 58 KB

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