segment.c 51 KB

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