segment.c 53 KB

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