segment.c 58 KB

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