segment.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  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 becasue
  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 = -1;
  378. unsigned int total_segs = TOTAL_SEGS(sbi);
  379. mutex_lock(&dirty_i->seglist_lock);
  380. while (1) {
  381. segno = find_next_bit(dirty_i->dirty_segmap[PRE], total_segs,
  382. segno + 1);
  383. if (segno >= total_segs)
  384. break;
  385. __set_test_and_free(sbi, segno);
  386. }
  387. mutex_unlock(&dirty_i->seglist_lock);
  388. }
  389. void clear_prefree_segments(struct f2fs_sb_info *sbi)
  390. {
  391. struct list_head *head = &(SM_I(sbi)->discard_list);
  392. struct discard_entry *entry, *this;
  393. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  394. unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
  395. unsigned int total_segs = TOTAL_SEGS(sbi);
  396. unsigned int start = 0, end = -1;
  397. mutex_lock(&dirty_i->seglist_lock);
  398. while (1) {
  399. int i;
  400. start = find_next_bit(prefree_map, total_segs, end + 1);
  401. if (start >= total_segs)
  402. break;
  403. end = find_next_zero_bit(prefree_map, total_segs, start + 1);
  404. for (i = start; i < end; i++)
  405. clear_bit(i, prefree_map);
  406. dirty_i->nr_dirty[PRE] -= end - start;
  407. if (!test_opt(sbi, DISCARD))
  408. continue;
  409. f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
  410. (end - start) << sbi->log_blocks_per_seg);
  411. }
  412. mutex_unlock(&dirty_i->seglist_lock);
  413. /* send small discards */
  414. list_for_each_entry_safe(entry, this, head, list) {
  415. f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
  416. list_del(&entry->list);
  417. SM_I(sbi)->nr_discards -= entry->len;
  418. kmem_cache_free(discard_entry_slab, entry);
  419. }
  420. }
  421. static void __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
  422. {
  423. struct sit_info *sit_i = SIT_I(sbi);
  424. if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap))
  425. sit_i->dirty_sentries++;
  426. }
  427. static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
  428. unsigned int segno, int modified)
  429. {
  430. struct seg_entry *se = get_seg_entry(sbi, segno);
  431. se->type = type;
  432. if (modified)
  433. __mark_sit_entry_dirty(sbi, segno);
  434. }
  435. static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
  436. {
  437. struct seg_entry *se;
  438. unsigned int segno, offset;
  439. long int new_vblocks;
  440. segno = GET_SEGNO(sbi, blkaddr);
  441. se = get_seg_entry(sbi, segno);
  442. new_vblocks = se->valid_blocks + del;
  443. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  444. f2fs_bug_on((new_vblocks >> (sizeof(unsigned short) << 3) ||
  445. (new_vblocks > sbi->blocks_per_seg)));
  446. se->valid_blocks = new_vblocks;
  447. se->mtime = get_mtime(sbi);
  448. SIT_I(sbi)->max_mtime = se->mtime;
  449. /* Update valid block bitmap */
  450. if (del > 0) {
  451. if (f2fs_set_bit(offset, se->cur_valid_map))
  452. BUG();
  453. } else {
  454. if (!f2fs_clear_bit(offset, se->cur_valid_map))
  455. BUG();
  456. }
  457. if (!f2fs_test_bit(offset, se->ckpt_valid_map))
  458. se->ckpt_valid_blocks += del;
  459. __mark_sit_entry_dirty(sbi, segno);
  460. /* update total number of valid blocks to be written in ckpt area */
  461. SIT_I(sbi)->written_valid_blocks += del;
  462. if (sbi->segs_per_sec > 1)
  463. get_sec_entry(sbi, segno)->valid_blocks += del;
  464. }
  465. void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
  466. {
  467. update_sit_entry(sbi, new, 1);
  468. if (GET_SEGNO(sbi, old) != NULL_SEGNO)
  469. update_sit_entry(sbi, old, -1);
  470. locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
  471. locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
  472. }
  473. void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
  474. {
  475. unsigned int segno = GET_SEGNO(sbi, addr);
  476. struct sit_info *sit_i = SIT_I(sbi);
  477. f2fs_bug_on(addr == NULL_ADDR);
  478. if (addr == NEW_ADDR)
  479. return;
  480. /* add it into sit main buffer */
  481. mutex_lock(&sit_i->sentry_lock);
  482. update_sit_entry(sbi, addr, -1);
  483. /* add it into dirty seglist */
  484. locate_dirty_segment(sbi, segno);
  485. mutex_unlock(&sit_i->sentry_lock);
  486. }
  487. /*
  488. * This function should be resided under the curseg_mutex lock
  489. */
  490. static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
  491. struct f2fs_summary *sum)
  492. {
  493. struct curseg_info *curseg = CURSEG_I(sbi, type);
  494. void *addr = curseg->sum_blk;
  495. addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
  496. memcpy(addr, sum, sizeof(struct f2fs_summary));
  497. }
  498. /*
  499. * Calculate the number of current summary pages for writing
  500. */
  501. int npages_for_summary_flush(struct f2fs_sb_info *sbi)
  502. {
  503. int valid_sum_count = 0;
  504. int i, sum_in_page;
  505. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  506. if (sbi->ckpt->alloc_type[i] == SSR)
  507. valid_sum_count += sbi->blocks_per_seg;
  508. else
  509. valid_sum_count += curseg_blkoff(sbi, i);
  510. }
  511. sum_in_page = (PAGE_CACHE_SIZE - 2 * SUM_JOURNAL_SIZE -
  512. SUM_FOOTER_SIZE) / SUMMARY_SIZE;
  513. if (valid_sum_count <= sum_in_page)
  514. return 1;
  515. else if ((valid_sum_count - sum_in_page) <=
  516. (PAGE_CACHE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
  517. return 2;
  518. return 3;
  519. }
  520. /*
  521. * Caller should put this summary page
  522. */
  523. struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
  524. {
  525. return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
  526. }
  527. static void write_sum_page(struct f2fs_sb_info *sbi,
  528. struct f2fs_summary_block *sum_blk, block_t blk_addr)
  529. {
  530. struct page *page = grab_meta_page(sbi, blk_addr);
  531. void *kaddr = page_address(page);
  532. memcpy(kaddr, sum_blk, PAGE_CACHE_SIZE);
  533. set_page_dirty(page);
  534. f2fs_put_page(page, 1);
  535. }
  536. static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
  537. {
  538. struct curseg_info *curseg = CURSEG_I(sbi, type);
  539. unsigned int segno = curseg->segno + 1;
  540. struct free_segmap_info *free_i = FREE_I(sbi);
  541. if (segno < TOTAL_SEGS(sbi) && segno % sbi->segs_per_sec)
  542. return !test_bit(segno, free_i->free_segmap);
  543. return 0;
  544. }
  545. /*
  546. * Find a new segment from the free segments bitmap to right order
  547. * This function should be returned with success, otherwise BUG
  548. */
  549. static void get_new_segment(struct f2fs_sb_info *sbi,
  550. unsigned int *newseg, bool new_sec, int dir)
  551. {
  552. struct free_segmap_info *free_i = FREE_I(sbi);
  553. unsigned int segno, secno, zoneno;
  554. unsigned int total_zones = TOTAL_SECS(sbi) / sbi->secs_per_zone;
  555. unsigned int hint = *newseg / sbi->segs_per_sec;
  556. unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
  557. unsigned int left_start = hint;
  558. bool init = true;
  559. int go_left = 0;
  560. int i;
  561. write_lock(&free_i->segmap_lock);
  562. if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
  563. segno = find_next_zero_bit(free_i->free_segmap,
  564. TOTAL_SEGS(sbi), *newseg + 1);
  565. if (segno - *newseg < sbi->segs_per_sec -
  566. (*newseg % sbi->segs_per_sec))
  567. goto got_it;
  568. }
  569. find_other_zone:
  570. secno = find_next_zero_bit(free_i->free_secmap, TOTAL_SECS(sbi), hint);
  571. if (secno >= TOTAL_SECS(sbi)) {
  572. if (dir == ALLOC_RIGHT) {
  573. secno = find_next_zero_bit(free_i->free_secmap,
  574. TOTAL_SECS(sbi), 0);
  575. f2fs_bug_on(secno >= TOTAL_SECS(sbi));
  576. } else {
  577. go_left = 1;
  578. left_start = hint - 1;
  579. }
  580. }
  581. if (go_left == 0)
  582. goto skip_left;
  583. while (test_bit(left_start, free_i->free_secmap)) {
  584. if (left_start > 0) {
  585. left_start--;
  586. continue;
  587. }
  588. left_start = find_next_zero_bit(free_i->free_secmap,
  589. TOTAL_SECS(sbi), 0);
  590. f2fs_bug_on(left_start >= TOTAL_SECS(sbi));
  591. break;
  592. }
  593. secno = left_start;
  594. skip_left:
  595. hint = secno;
  596. segno = secno * sbi->segs_per_sec;
  597. zoneno = secno / sbi->secs_per_zone;
  598. /* give up on finding another zone */
  599. if (!init)
  600. goto got_it;
  601. if (sbi->secs_per_zone == 1)
  602. goto got_it;
  603. if (zoneno == old_zoneno)
  604. goto got_it;
  605. if (dir == ALLOC_LEFT) {
  606. if (!go_left && zoneno + 1 >= total_zones)
  607. goto got_it;
  608. if (go_left && zoneno == 0)
  609. goto got_it;
  610. }
  611. for (i = 0; i < NR_CURSEG_TYPE; i++)
  612. if (CURSEG_I(sbi, i)->zone == zoneno)
  613. break;
  614. if (i < NR_CURSEG_TYPE) {
  615. /* zone is in user, try another */
  616. if (go_left)
  617. hint = zoneno * sbi->secs_per_zone - 1;
  618. else if (zoneno + 1 >= total_zones)
  619. hint = 0;
  620. else
  621. hint = (zoneno + 1) * sbi->secs_per_zone;
  622. init = false;
  623. goto find_other_zone;
  624. }
  625. got_it:
  626. /* set it as dirty segment in free segmap */
  627. f2fs_bug_on(test_bit(segno, free_i->free_segmap));
  628. __set_inuse(sbi, segno);
  629. *newseg = segno;
  630. write_unlock(&free_i->segmap_lock);
  631. }
  632. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  633. {
  634. struct curseg_info *curseg = CURSEG_I(sbi, type);
  635. struct summary_footer *sum_footer;
  636. curseg->segno = curseg->next_segno;
  637. curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
  638. curseg->next_blkoff = 0;
  639. curseg->next_segno = NULL_SEGNO;
  640. sum_footer = &(curseg->sum_blk->footer);
  641. memset(sum_footer, 0, sizeof(struct summary_footer));
  642. if (IS_DATASEG(type))
  643. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  644. if (IS_NODESEG(type))
  645. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  646. __set_sit_entry_type(sbi, type, curseg->segno, modified);
  647. }
  648. /*
  649. * Allocate a current working segment.
  650. * This function always allocates a free segment in LFS manner.
  651. */
  652. static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  653. {
  654. struct curseg_info *curseg = CURSEG_I(sbi, type);
  655. unsigned int segno = curseg->segno;
  656. int dir = ALLOC_LEFT;
  657. write_sum_page(sbi, curseg->sum_blk,
  658. GET_SUM_BLOCK(sbi, segno));
  659. if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
  660. dir = ALLOC_RIGHT;
  661. if (test_opt(sbi, NOHEAP))
  662. dir = ALLOC_RIGHT;
  663. get_new_segment(sbi, &segno, new_sec, dir);
  664. curseg->next_segno = segno;
  665. reset_curseg(sbi, type, 1);
  666. curseg->alloc_type = LFS;
  667. }
  668. static void __next_free_blkoff(struct f2fs_sb_info *sbi,
  669. struct curseg_info *seg, block_t start)
  670. {
  671. struct seg_entry *se = get_seg_entry(sbi, seg->segno);
  672. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  673. unsigned long target_map[entries];
  674. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  675. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  676. int i, pos;
  677. for (i = 0; i < entries; i++)
  678. target_map[i] = ckpt_map[i] | cur_map[i];
  679. pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
  680. seg->next_blkoff = pos;
  681. }
  682. /*
  683. * If a segment is written by LFS manner, next block offset is just obtained
  684. * by increasing the current block offset. However, if a segment is written by
  685. * SSR manner, next block offset obtained by calling __next_free_blkoff
  686. */
  687. static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
  688. struct curseg_info *seg)
  689. {
  690. if (seg->alloc_type == SSR)
  691. __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
  692. else
  693. seg->next_blkoff++;
  694. }
  695. /*
  696. * This function always allocates a used segment (from dirty seglist) by SSR
  697. * manner, so it should recover the existing segment information of valid blocks
  698. */
  699. static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
  700. {
  701. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  702. struct curseg_info *curseg = CURSEG_I(sbi, type);
  703. unsigned int new_segno = curseg->next_segno;
  704. struct f2fs_summary_block *sum_node;
  705. struct page *sum_page;
  706. write_sum_page(sbi, curseg->sum_blk,
  707. GET_SUM_BLOCK(sbi, curseg->segno));
  708. __set_test_and_inuse(sbi, new_segno);
  709. mutex_lock(&dirty_i->seglist_lock);
  710. __remove_dirty_segment(sbi, new_segno, PRE);
  711. __remove_dirty_segment(sbi, new_segno, DIRTY);
  712. mutex_unlock(&dirty_i->seglist_lock);
  713. reset_curseg(sbi, type, 1);
  714. curseg->alloc_type = SSR;
  715. __next_free_blkoff(sbi, curseg, 0);
  716. if (reuse) {
  717. sum_page = get_sum_page(sbi, new_segno);
  718. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  719. memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
  720. f2fs_put_page(sum_page, 1);
  721. }
  722. }
  723. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
  724. {
  725. struct curseg_info *curseg = CURSEG_I(sbi, type);
  726. const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
  727. if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0))
  728. return v_ops->get_victim(sbi,
  729. &(curseg)->next_segno, BG_GC, type, SSR);
  730. /* For data segments, let's do SSR more intensively */
  731. for (; type >= CURSEG_HOT_DATA; type--)
  732. if (v_ops->get_victim(sbi, &(curseg)->next_segno,
  733. BG_GC, type, SSR))
  734. return 1;
  735. return 0;
  736. }
  737. /*
  738. * flush out current segment and replace it with new segment
  739. * This function should be returned with success, otherwise BUG
  740. */
  741. static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
  742. int type, bool force)
  743. {
  744. struct curseg_info *curseg = CURSEG_I(sbi, type);
  745. if (force)
  746. new_curseg(sbi, type, true);
  747. else if (type == CURSEG_WARM_NODE)
  748. new_curseg(sbi, type, false);
  749. else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
  750. new_curseg(sbi, type, false);
  751. else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
  752. change_curseg(sbi, type, true);
  753. else
  754. new_curseg(sbi, type, false);
  755. stat_inc_seg_type(sbi, curseg);
  756. }
  757. void allocate_new_segments(struct f2fs_sb_info *sbi)
  758. {
  759. struct curseg_info *curseg;
  760. unsigned int old_curseg;
  761. int i;
  762. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  763. curseg = CURSEG_I(sbi, i);
  764. old_curseg = curseg->segno;
  765. SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
  766. locate_dirty_segment(sbi, old_curseg);
  767. }
  768. }
  769. static const struct segment_allocation default_salloc_ops = {
  770. .allocate_segment = allocate_segment_by_default,
  771. };
  772. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  773. {
  774. struct curseg_info *curseg = CURSEG_I(sbi, type);
  775. if (curseg->next_blkoff < sbi->blocks_per_seg)
  776. return true;
  777. return false;
  778. }
  779. static int __get_segment_type_2(struct page *page, enum page_type p_type)
  780. {
  781. if (p_type == DATA)
  782. return CURSEG_HOT_DATA;
  783. else
  784. return CURSEG_HOT_NODE;
  785. }
  786. static int __get_segment_type_4(struct page *page, enum page_type p_type)
  787. {
  788. if (p_type == DATA) {
  789. struct inode *inode = page->mapping->host;
  790. if (S_ISDIR(inode->i_mode))
  791. return CURSEG_HOT_DATA;
  792. else
  793. return CURSEG_COLD_DATA;
  794. } else {
  795. if (IS_DNODE(page) && !is_cold_node(page))
  796. return CURSEG_HOT_NODE;
  797. else
  798. return CURSEG_COLD_NODE;
  799. }
  800. }
  801. static int __get_segment_type_6(struct page *page, enum page_type p_type)
  802. {
  803. if (p_type == DATA) {
  804. struct inode *inode = page->mapping->host;
  805. if (S_ISDIR(inode->i_mode))
  806. return CURSEG_HOT_DATA;
  807. else if (is_cold_data(page) || file_is_cold(inode))
  808. return CURSEG_COLD_DATA;
  809. else
  810. return CURSEG_WARM_DATA;
  811. } else {
  812. if (IS_DNODE(page))
  813. return is_cold_node(page) ? CURSEG_WARM_NODE :
  814. CURSEG_HOT_NODE;
  815. else
  816. return CURSEG_COLD_NODE;
  817. }
  818. }
  819. static int __get_segment_type(struct page *page, enum page_type p_type)
  820. {
  821. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  822. switch (sbi->active_logs) {
  823. case 2:
  824. return __get_segment_type_2(page, p_type);
  825. case 4:
  826. return __get_segment_type_4(page, p_type);
  827. }
  828. /* NR_CURSEG_TYPE(6) logs by default */
  829. f2fs_bug_on(sbi->active_logs != NR_CURSEG_TYPE);
  830. return __get_segment_type_6(page, p_type);
  831. }
  832. void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  833. block_t old_blkaddr, block_t *new_blkaddr,
  834. struct f2fs_summary *sum, int type)
  835. {
  836. struct sit_info *sit_i = SIT_I(sbi);
  837. struct curseg_info *curseg;
  838. curseg = CURSEG_I(sbi, type);
  839. mutex_lock(&curseg->curseg_mutex);
  840. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  841. /*
  842. * __add_sum_entry should be resided under the curseg_mutex
  843. * because, this function updates a summary entry in the
  844. * current summary block.
  845. */
  846. __add_sum_entry(sbi, type, sum);
  847. mutex_lock(&sit_i->sentry_lock);
  848. __refresh_next_blkoff(sbi, curseg);
  849. stat_inc_block_count(sbi, curseg);
  850. if (!__has_curseg_space(sbi, type))
  851. sit_i->s_ops->allocate_segment(sbi, type, false);
  852. /*
  853. * SIT information should be updated before segment allocation,
  854. * since SSR needs latest valid block information.
  855. */
  856. refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
  857. mutex_unlock(&sit_i->sentry_lock);
  858. if (page && IS_NODESEG(type))
  859. fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
  860. mutex_unlock(&curseg->curseg_mutex);
  861. }
  862. static void do_write_page(struct f2fs_sb_info *sbi, struct page *page,
  863. block_t old_blkaddr, block_t *new_blkaddr,
  864. struct f2fs_summary *sum, struct f2fs_io_info *fio)
  865. {
  866. int type = __get_segment_type(page, fio->type);
  867. allocate_data_block(sbi, page, old_blkaddr, new_blkaddr, sum, type);
  868. /* writeout dirty page into bdev */
  869. f2fs_submit_page_mbio(sbi, page, *new_blkaddr, fio);
  870. }
  871. void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
  872. {
  873. struct f2fs_io_info fio = {
  874. .type = META,
  875. .rw = WRITE_SYNC | REQ_META | REQ_PRIO
  876. };
  877. set_page_writeback(page);
  878. f2fs_submit_page_mbio(sbi, page, page->index, &fio);
  879. }
  880. void write_node_page(struct f2fs_sb_info *sbi, struct page *page,
  881. struct f2fs_io_info *fio,
  882. unsigned int nid, block_t old_blkaddr, block_t *new_blkaddr)
  883. {
  884. struct f2fs_summary sum;
  885. set_summary(&sum, nid, 0, 0);
  886. do_write_page(sbi, page, old_blkaddr, new_blkaddr, &sum, fio);
  887. }
  888. void write_data_page(struct page *page, struct dnode_of_data *dn,
  889. block_t *new_blkaddr, struct f2fs_io_info *fio)
  890. {
  891. struct f2fs_sb_info *sbi = F2FS_SB(dn->inode->i_sb);
  892. struct f2fs_summary sum;
  893. struct node_info ni;
  894. f2fs_bug_on(dn->data_blkaddr == NULL_ADDR);
  895. get_node_info(sbi, dn->nid, &ni);
  896. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  897. do_write_page(sbi, page, dn->data_blkaddr, new_blkaddr, &sum, fio);
  898. }
  899. void rewrite_data_page(struct page *page, block_t old_blkaddr,
  900. struct f2fs_io_info *fio)
  901. {
  902. struct inode *inode = page->mapping->host;
  903. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  904. f2fs_submit_page_mbio(sbi, page, old_blkaddr, fio);
  905. }
  906. void recover_data_page(struct f2fs_sb_info *sbi,
  907. struct page *page, struct f2fs_summary *sum,
  908. block_t old_blkaddr, block_t new_blkaddr)
  909. {
  910. struct sit_info *sit_i = SIT_I(sbi);
  911. struct curseg_info *curseg;
  912. unsigned int segno, old_cursegno;
  913. struct seg_entry *se;
  914. int type;
  915. segno = GET_SEGNO(sbi, new_blkaddr);
  916. se = get_seg_entry(sbi, segno);
  917. type = se->type;
  918. if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
  919. if (old_blkaddr == NULL_ADDR)
  920. type = CURSEG_COLD_DATA;
  921. else
  922. type = CURSEG_WARM_DATA;
  923. }
  924. curseg = CURSEG_I(sbi, type);
  925. mutex_lock(&curseg->curseg_mutex);
  926. mutex_lock(&sit_i->sentry_lock);
  927. old_cursegno = curseg->segno;
  928. /* change the current segment */
  929. if (segno != curseg->segno) {
  930. curseg->next_segno = segno;
  931. change_curseg(sbi, type, true);
  932. }
  933. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  934. __add_sum_entry(sbi, type, sum);
  935. refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
  936. locate_dirty_segment(sbi, old_cursegno);
  937. mutex_unlock(&sit_i->sentry_lock);
  938. mutex_unlock(&curseg->curseg_mutex);
  939. }
  940. void rewrite_node_page(struct f2fs_sb_info *sbi,
  941. struct page *page, struct f2fs_summary *sum,
  942. block_t old_blkaddr, block_t new_blkaddr)
  943. {
  944. struct sit_info *sit_i = SIT_I(sbi);
  945. int type = CURSEG_WARM_NODE;
  946. struct curseg_info *curseg;
  947. unsigned int segno, old_cursegno;
  948. block_t next_blkaddr = next_blkaddr_of_node(page);
  949. unsigned int next_segno = GET_SEGNO(sbi, next_blkaddr);
  950. struct f2fs_io_info fio = {
  951. .type = NODE,
  952. .rw = WRITE_SYNC,
  953. };
  954. curseg = CURSEG_I(sbi, type);
  955. mutex_lock(&curseg->curseg_mutex);
  956. mutex_lock(&sit_i->sentry_lock);
  957. segno = GET_SEGNO(sbi, new_blkaddr);
  958. old_cursegno = curseg->segno;
  959. /* change the current segment */
  960. if (segno != curseg->segno) {
  961. curseg->next_segno = segno;
  962. change_curseg(sbi, type, true);
  963. }
  964. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  965. __add_sum_entry(sbi, type, sum);
  966. /* change the current log to the next block addr in advance */
  967. if (next_segno != segno) {
  968. curseg->next_segno = next_segno;
  969. change_curseg(sbi, type, true);
  970. }
  971. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, next_blkaddr);
  972. /* rewrite node page */
  973. set_page_writeback(page);
  974. f2fs_submit_page_mbio(sbi, page, new_blkaddr, &fio);
  975. f2fs_submit_merged_bio(sbi, NODE, WRITE);
  976. refresh_sit_entry(sbi, old_blkaddr, new_blkaddr);
  977. locate_dirty_segment(sbi, old_cursegno);
  978. mutex_unlock(&sit_i->sentry_lock);
  979. mutex_unlock(&curseg->curseg_mutex);
  980. }
  981. static inline bool is_merged_page(struct f2fs_sb_info *sbi,
  982. struct page *page, enum page_type type)
  983. {
  984. enum page_type btype = PAGE_TYPE_OF_BIO(type);
  985. struct f2fs_bio_info *io = &sbi->write_io[btype];
  986. struct bio_vec *bvec;
  987. int i;
  988. down_read(&io->io_rwsem);
  989. if (!io->bio)
  990. goto out;
  991. bio_for_each_segment_all(bvec, io->bio, i) {
  992. if (page == bvec->bv_page) {
  993. up_read(&io->io_rwsem);
  994. return true;
  995. }
  996. }
  997. out:
  998. up_read(&io->io_rwsem);
  999. return false;
  1000. }
  1001. void f2fs_wait_on_page_writeback(struct page *page,
  1002. enum page_type type)
  1003. {
  1004. struct f2fs_sb_info *sbi = F2FS_SB(page->mapping->host->i_sb);
  1005. if (PageWriteback(page)) {
  1006. if (is_merged_page(sbi, page, type))
  1007. f2fs_submit_merged_bio(sbi, type, WRITE);
  1008. wait_on_page_writeback(page);
  1009. }
  1010. }
  1011. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  1012. {
  1013. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1014. struct curseg_info *seg_i;
  1015. unsigned char *kaddr;
  1016. struct page *page;
  1017. block_t start;
  1018. int i, j, offset;
  1019. start = start_sum_block(sbi);
  1020. page = get_meta_page(sbi, start++);
  1021. kaddr = (unsigned char *)page_address(page);
  1022. /* Step 1: restore nat cache */
  1023. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1024. memcpy(&seg_i->sum_blk->n_nats, kaddr, SUM_JOURNAL_SIZE);
  1025. /* Step 2: restore sit cache */
  1026. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1027. memcpy(&seg_i->sum_blk->n_sits, kaddr + SUM_JOURNAL_SIZE,
  1028. SUM_JOURNAL_SIZE);
  1029. offset = 2 * SUM_JOURNAL_SIZE;
  1030. /* Step 3: restore summary entries */
  1031. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1032. unsigned short blk_off;
  1033. unsigned int segno;
  1034. seg_i = CURSEG_I(sbi, i);
  1035. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  1036. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  1037. seg_i->next_segno = segno;
  1038. reset_curseg(sbi, i, 0);
  1039. seg_i->alloc_type = ckpt->alloc_type[i];
  1040. seg_i->next_blkoff = blk_off;
  1041. if (seg_i->alloc_type == SSR)
  1042. blk_off = sbi->blocks_per_seg;
  1043. for (j = 0; j < blk_off; j++) {
  1044. struct f2fs_summary *s;
  1045. s = (struct f2fs_summary *)(kaddr + offset);
  1046. seg_i->sum_blk->entries[j] = *s;
  1047. offset += SUMMARY_SIZE;
  1048. if (offset + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  1049. SUM_FOOTER_SIZE)
  1050. continue;
  1051. f2fs_put_page(page, 1);
  1052. page = NULL;
  1053. page = get_meta_page(sbi, start++);
  1054. kaddr = (unsigned char *)page_address(page);
  1055. offset = 0;
  1056. }
  1057. }
  1058. f2fs_put_page(page, 1);
  1059. return 0;
  1060. }
  1061. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  1062. {
  1063. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1064. struct f2fs_summary_block *sum;
  1065. struct curseg_info *curseg;
  1066. struct page *new;
  1067. unsigned short blk_off;
  1068. unsigned int segno = 0;
  1069. block_t blk_addr = 0;
  1070. /* get segment number and block addr */
  1071. if (IS_DATASEG(type)) {
  1072. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  1073. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  1074. CURSEG_HOT_DATA]);
  1075. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  1076. blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
  1077. else
  1078. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  1079. } else {
  1080. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  1081. CURSEG_HOT_NODE]);
  1082. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  1083. CURSEG_HOT_NODE]);
  1084. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG))
  1085. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  1086. type - CURSEG_HOT_NODE);
  1087. else
  1088. blk_addr = GET_SUM_BLOCK(sbi, segno);
  1089. }
  1090. new = get_meta_page(sbi, blk_addr);
  1091. sum = (struct f2fs_summary_block *)page_address(new);
  1092. if (IS_NODESEG(type)) {
  1093. if (is_set_ckpt_flags(ckpt, CP_UMOUNT_FLAG)) {
  1094. struct f2fs_summary *ns = &sum->entries[0];
  1095. int i;
  1096. for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
  1097. ns->version = 0;
  1098. ns->ofs_in_node = 0;
  1099. }
  1100. } else {
  1101. int err;
  1102. err = restore_node_summary(sbi, segno, sum);
  1103. if (err) {
  1104. f2fs_put_page(new, 1);
  1105. return err;
  1106. }
  1107. }
  1108. }
  1109. /* set uncompleted segment to curseg */
  1110. curseg = CURSEG_I(sbi, type);
  1111. mutex_lock(&curseg->curseg_mutex);
  1112. memcpy(curseg->sum_blk, sum, PAGE_CACHE_SIZE);
  1113. curseg->next_segno = segno;
  1114. reset_curseg(sbi, type, 0);
  1115. curseg->alloc_type = ckpt->alloc_type[type];
  1116. curseg->next_blkoff = blk_off;
  1117. mutex_unlock(&curseg->curseg_mutex);
  1118. f2fs_put_page(new, 1);
  1119. return 0;
  1120. }
  1121. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  1122. {
  1123. int type = CURSEG_HOT_DATA;
  1124. int err;
  1125. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG)) {
  1126. /* restore for compacted data summary */
  1127. if (read_compacted_summaries(sbi))
  1128. return -EINVAL;
  1129. type = CURSEG_HOT_NODE;
  1130. }
  1131. for (; type <= CURSEG_COLD_NODE; type++) {
  1132. err = read_normal_summaries(sbi, type);
  1133. if (err)
  1134. return err;
  1135. }
  1136. return 0;
  1137. }
  1138. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  1139. {
  1140. struct page *page;
  1141. unsigned char *kaddr;
  1142. struct f2fs_summary *summary;
  1143. struct curseg_info *seg_i;
  1144. int written_size = 0;
  1145. int i, j;
  1146. page = grab_meta_page(sbi, blkaddr++);
  1147. kaddr = (unsigned char *)page_address(page);
  1148. /* Step 1: write nat cache */
  1149. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1150. memcpy(kaddr, &seg_i->sum_blk->n_nats, SUM_JOURNAL_SIZE);
  1151. written_size += SUM_JOURNAL_SIZE;
  1152. /* Step 2: write sit cache */
  1153. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1154. memcpy(kaddr + written_size, &seg_i->sum_blk->n_sits,
  1155. SUM_JOURNAL_SIZE);
  1156. written_size += SUM_JOURNAL_SIZE;
  1157. /* Step 3: write summary entries */
  1158. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1159. unsigned short blkoff;
  1160. seg_i = CURSEG_I(sbi, i);
  1161. if (sbi->ckpt->alloc_type[i] == SSR)
  1162. blkoff = sbi->blocks_per_seg;
  1163. else
  1164. blkoff = curseg_blkoff(sbi, i);
  1165. for (j = 0; j < blkoff; j++) {
  1166. if (!page) {
  1167. page = grab_meta_page(sbi, blkaddr++);
  1168. kaddr = (unsigned char *)page_address(page);
  1169. written_size = 0;
  1170. }
  1171. summary = (struct f2fs_summary *)(kaddr + written_size);
  1172. *summary = seg_i->sum_blk->entries[j];
  1173. written_size += SUMMARY_SIZE;
  1174. if (written_size + SUMMARY_SIZE <= PAGE_CACHE_SIZE -
  1175. SUM_FOOTER_SIZE)
  1176. continue;
  1177. set_page_dirty(page);
  1178. f2fs_put_page(page, 1);
  1179. page = NULL;
  1180. }
  1181. }
  1182. if (page) {
  1183. set_page_dirty(page);
  1184. f2fs_put_page(page, 1);
  1185. }
  1186. }
  1187. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  1188. block_t blkaddr, int type)
  1189. {
  1190. int i, end;
  1191. if (IS_DATASEG(type))
  1192. end = type + NR_CURSEG_DATA_TYPE;
  1193. else
  1194. end = type + NR_CURSEG_NODE_TYPE;
  1195. for (i = type; i < end; i++) {
  1196. struct curseg_info *sum = CURSEG_I(sbi, i);
  1197. mutex_lock(&sum->curseg_mutex);
  1198. write_sum_page(sbi, sum->sum_blk, blkaddr + (i - type));
  1199. mutex_unlock(&sum->curseg_mutex);
  1200. }
  1201. }
  1202. void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1203. {
  1204. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_COMPACT_SUM_FLAG))
  1205. write_compacted_summaries(sbi, start_blk);
  1206. else
  1207. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  1208. }
  1209. void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1210. {
  1211. if (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG))
  1212. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  1213. }
  1214. int lookup_journal_in_cursum(struct f2fs_summary_block *sum, int type,
  1215. unsigned int val, int alloc)
  1216. {
  1217. int i;
  1218. if (type == NAT_JOURNAL) {
  1219. for (i = 0; i < nats_in_cursum(sum); i++) {
  1220. if (le32_to_cpu(nid_in_journal(sum, i)) == val)
  1221. return i;
  1222. }
  1223. if (alloc && nats_in_cursum(sum) < NAT_JOURNAL_ENTRIES)
  1224. return update_nats_in_cursum(sum, 1);
  1225. } else if (type == SIT_JOURNAL) {
  1226. for (i = 0; i < sits_in_cursum(sum); i++)
  1227. if (le32_to_cpu(segno_in_journal(sum, i)) == val)
  1228. return i;
  1229. if (alloc && sits_in_cursum(sum) < SIT_JOURNAL_ENTRIES)
  1230. return update_sits_in_cursum(sum, 1);
  1231. }
  1232. return -1;
  1233. }
  1234. static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
  1235. unsigned int segno)
  1236. {
  1237. struct sit_info *sit_i = SIT_I(sbi);
  1238. unsigned int offset = SIT_BLOCK_OFFSET(sit_i, segno);
  1239. block_t blk_addr = sit_i->sit_base_addr + offset;
  1240. check_seg_range(sbi, segno);
  1241. /* calculate sit block address */
  1242. if (f2fs_test_bit(offset, sit_i->sit_bitmap))
  1243. blk_addr += sit_i->sit_blocks;
  1244. return get_meta_page(sbi, blk_addr);
  1245. }
  1246. static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
  1247. unsigned int start)
  1248. {
  1249. struct sit_info *sit_i = SIT_I(sbi);
  1250. struct page *src_page, *dst_page;
  1251. pgoff_t src_off, dst_off;
  1252. void *src_addr, *dst_addr;
  1253. src_off = current_sit_addr(sbi, start);
  1254. dst_off = next_sit_addr(sbi, src_off);
  1255. /* get current sit block page without lock */
  1256. src_page = get_meta_page(sbi, src_off);
  1257. dst_page = grab_meta_page(sbi, dst_off);
  1258. f2fs_bug_on(PageDirty(src_page));
  1259. src_addr = page_address(src_page);
  1260. dst_addr = page_address(dst_page);
  1261. memcpy(dst_addr, src_addr, PAGE_CACHE_SIZE);
  1262. set_page_dirty(dst_page);
  1263. f2fs_put_page(src_page, 1);
  1264. set_to_next_sit(sit_i, start);
  1265. return dst_page;
  1266. }
  1267. static bool flush_sits_in_journal(struct f2fs_sb_info *sbi)
  1268. {
  1269. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1270. struct f2fs_summary_block *sum = curseg->sum_blk;
  1271. int i;
  1272. /*
  1273. * If the journal area in the current summary is full of sit entries,
  1274. * all the sit entries will be flushed. Otherwise the sit entries
  1275. * are not able to replace with newly hot sit entries.
  1276. */
  1277. if (sits_in_cursum(sum) >= SIT_JOURNAL_ENTRIES) {
  1278. for (i = sits_in_cursum(sum) - 1; i >= 0; i--) {
  1279. unsigned int segno;
  1280. segno = le32_to_cpu(segno_in_journal(sum, i));
  1281. __mark_sit_entry_dirty(sbi, segno);
  1282. }
  1283. update_sits_in_cursum(sum, -sits_in_cursum(sum));
  1284. return true;
  1285. }
  1286. return false;
  1287. }
  1288. /*
  1289. * CP calls this function, which flushes SIT entries including sit_journal,
  1290. * and moves prefree segs to free segs.
  1291. */
  1292. void flush_sit_entries(struct f2fs_sb_info *sbi)
  1293. {
  1294. struct sit_info *sit_i = SIT_I(sbi);
  1295. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  1296. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1297. struct f2fs_summary_block *sum = curseg->sum_blk;
  1298. unsigned long nsegs = TOTAL_SEGS(sbi);
  1299. struct page *page = NULL;
  1300. struct f2fs_sit_block *raw_sit = NULL;
  1301. unsigned int start = 0, end = 0;
  1302. unsigned int segno = -1;
  1303. bool flushed;
  1304. mutex_lock(&curseg->curseg_mutex);
  1305. mutex_lock(&sit_i->sentry_lock);
  1306. /*
  1307. * "flushed" indicates whether sit entries in journal are flushed
  1308. * to the SIT area or not.
  1309. */
  1310. flushed = flush_sits_in_journal(sbi);
  1311. while ((segno = find_next_bit(bitmap, nsegs, segno + 1)) < nsegs) {
  1312. struct seg_entry *se = get_seg_entry(sbi, segno);
  1313. int sit_offset, offset;
  1314. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  1315. /* add discard candidates */
  1316. if (SM_I(sbi)->nr_discards < SM_I(sbi)->max_discards)
  1317. add_discard_addrs(sbi, segno, se);
  1318. if (flushed)
  1319. goto to_sit_page;
  1320. offset = lookup_journal_in_cursum(sum, SIT_JOURNAL, segno, 1);
  1321. if (offset >= 0) {
  1322. segno_in_journal(sum, offset) = cpu_to_le32(segno);
  1323. seg_info_to_raw_sit(se, &sit_in_journal(sum, offset));
  1324. goto flush_done;
  1325. }
  1326. to_sit_page:
  1327. if (!page || (start > segno) || (segno > end)) {
  1328. if (page) {
  1329. f2fs_put_page(page, 1);
  1330. page = NULL;
  1331. }
  1332. start = START_SEGNO(sit_i, segno);
  1333. end = start + SIT_ENTRY_PER_BLOCK - 1;
  1334. /* read sit block that will be updated */
  1335. page = get_next_sit_page(sbi, start);
  1336. raw_sit = page_address(page);
  1337. }
  1338. /* udpate entry in SIT block */
  1339. seg_info_to_raw_sit(se, &raw_sit->entries[sit_offset]);
  1340. flush_done:
  1341. __clear_bit(segno, bitmap);
  1342. sit_i->dirty_sentries--;
  1343. }
  1344. mutex_unlock(&sit_i->sentry_lock);
  1345. mutex_unlock(&curseg->curseg_mutex);
  1346. /* writeout last modified SIT block */
  1347. f2fs_put_page(page, 1);
  1348. set_prefree_as_free_segments(sbi);
  1349. }
  1350. static int build_sit_info(struct f2fs_sb_info *sbi)
  1351. {
  1352. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1353. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1354. struct sit_info *sit_i;
  1355. unsigned int sit_segs, start;
  1356. char *src_bitmap, *dst_bitmap;
  1357. unsigned int bitmap_size;
  1358. /* allocate memory for SIT information */
  1359. sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
  1360. if (!sit_i)
  1361. return -ENOMEM;
  1362. SM_I(sbi)->sit_info = sit_i;
  1363. sit_i->sentries = vzalloc(TOTAL_SEGS(sbi) * sizeof(struct seg_entry));
  1364. if (!sit_i->sentries)
  1365. return -ENOMEM;
  1366. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1367. sit_i->dirty_sentries_bitmap = kzalloc(bitmap_size, GFP_KERNEL);
  1368. if (!sit_i->dirty_sentries_bitmap)
  1369. return -ENOMEM;
  1370. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1371. sit_i->sentries[start].cur_valid_map
  1372. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1373. sit_i->sentries[start].ckpt_valid_map
  1374. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1375. if (!sit_i->sentries[start].cur_valid_map
  1376. || !sit_i->sentries[start].ckpt_valid_map)
  1377. return -ENOMEM;
  1378. }
  1379. if (sbi->segs_per_sec > 1) {
  1380. sit_i->sec_entries = vzalloc(TOTAL_SECS(sbi) *
  1381. sizeof(struct sec_entry));
  1382. if (!sit_i->sec_entries)
  1383. return -ENOMEM;
  1384. }
  1385. /* get information related with SIT */
  1386. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  1387. /* setup SIT bitmap from ckeckpoint pack */
  1388. bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  1389. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  1390. dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  1391. if (!dst_bitmap)
  1392. return -ENOMEM;
  1393. /* init SIT information */
  1394. sit_i->s_ops = &default_salloc_ops;
  1395. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  1396. sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
  1397. sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
  1398. sit_i->sit_bitmap = dst_bitmap;
  1399. sit_i->bitmap_size = bitmap_size;
  1400. sit_i->dirty_sentries = 0;
  1401. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  1402. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  1403. sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
  1404. mutex_init(&sit_i->sentry_lock);
  1405. return 0;
  1406. }
  1407. static int build_free_segmap(struct f2fs_sb_info *sbi)
  1408. {
  1409. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1410. struct free_segmap_info *free_i;
  1411. unsigned int bitmap_size, sec_bitmap_size;
  1412. /* allocate memory for free segmap information */
  1413. free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
  1414. if (!free_i)
  1415. return -ENOMEM;
  1416. SM_I(sbi)->free_info = free_i;
  1417. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1418. free_i->free_segmap = kmalloc(bitmap_size, GFP_KERNEL);
  1419. if (!free_i->free_segmap)
  1420. return -ENOMEM;
  1421. sec_bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
  1422. free_i->free_secmap = kmalloc(sec_bitmap_size, GFP_KERNEL);
  1423. if (!free_i->free_secmap)
  1424. return -ENOMEM;
  1425. /* set all segments as dirty temporarily */
  1426. memset(free_i->free_segmap, 0xff, bitmap_size);
  1427. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  1428. /* init free segmap information */
  1429. free_i->start_segno =
  1430. (unsigned int) GET_SEGNO_FROM_SEG0(sbi, sm_info->main_blkaddr);
  1431. free_i->free_segments = 0;
  1432. free_i->free_sections = 0;
  1433. rwlock_init(&free_i->segmap_lock);
  1434. return 0;
  1435. }
  1436. static int build_curseg(struct f2fs_sb_info *sbi)
  1437. {
  1438. struct curseg_info *array;
  1439. int i;
  1440. array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
  1441. if (!array)
  1442. return -ENOMEM;
  1443. SM_I(sbi)->curseg_array = array;
  1444. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  1445. mutex_init(&array[i].curseg_mutex);
  1446. array[i].sum_blk = kzalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
  1447. if (!array[i].sum_blk)
  1448. return -ENOMEM;
  1449. array[i].segno = NULL_SEGNO;
  1450. array[i].next_blkoff = 0;
  1451. }
  1452. return restore_curseg_summaries(sbi);
  1453. }
  1454. static void build_sit_entries(struct f2fs_sb_info *sbi)
  1455. {
  1456. struct sit_info *sit_i = SIT_I(sbi);
  1457. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1458. struct f2fs_summary_block *sum = curseg->sum_blk;
  1459. int sit_blk_cnt = SIT_BLK_CNT(sbi);
  1460. unsigned int i, start, end;
  1461. unsigned int readed, start_blk = 0;
  1462. int nrpages = MAX_BIO_BLOCKS(max_hw_blocks(sbi));
  1463. do {
  1464. readed = ra_meta_pages(sbi, start_blk, nrpages, META_SIT);
  1465. start = start_blk * sit_i->sents_per_block;
  1466. end = (start_blk + readed) * sit_i->sents_per_block;
  1467. for (; start < end && start < TOTAL_SEGS(sbi); start++) {
  1468. struct seg_entry *se = &sit_i->sentries[start];
  1469. struct f2fs_sit_block *sit_blk;
  1470. struct f2fs_sit_entry sit;
  1471. struct page *page;
  1472. mutex_lock(&curseg->curseg_mutex);
  1473. for (i = 0; i < sits_in_cursum(sum); i++) {
  1474. if (le32_to_cpu(segno_in_journal(sum, i))
  1475. == start) {
  1476. sit = sit_in_journal(sum, i);
  1477. mutex_unlock(&curseg->curseg_mutex);
  1478. goto got_it;
  1479. }
  1480. }
  1481. mutex_unlock(&curseg->curseg_mutex);
  1482. page = get_current_sit_page(sbi, start);
  1483. sit_blk = (struct f2fs_sit_block *)page_address(page);
  1484. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  1485. f2fs_put_page(page, 1);
  1486. got_it:
  1487. check_block_count(sbi, start, &sit);
  1488. seg_info_from_raw_sit(se, &sit);
  1489. if (sbi->segs_per_sec > 1) {
  1490. struct sec_entry *e = get_sec_entry(sbi, start);
  1491. e->valid_blocks += se->valid_blocks;
  1492. }
  1493. }
  1494. start_blk += readed;
  1495. } while (start_blk < sit_blk_cnt);
  1496. }
  1497. static void init_free_segmap(struct f2fs_sb_info *sbi)
  1498. {
  1499. unsigned int start;
  1500. int type;
  1501. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1502. struct seg_entry *sentry = get_seg_entry(sbi, start);
  1503. if (!sentry->valid_blocks)
  1504. __set_free(sbi, start);
  1505. }
  1506. /* set use the current segments */
  1507. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  1508. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  1509. __set_test_and_inuse(sbi, curseg_t->segno);
  1510. }
  1511. }
  1512. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  1513. {
  1514. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1515. struct free_segmap_info *free_i = FREE_I(sbi);
  1516. unsigned int segno = 0, offset = 0, total_segs = TOTAL_SEGS(sbi);
  1517. unsigned short valid_blocks;
  1518. while (1) {
  1519. /* find dirty segment based on free segmap */
  1520. segno = find_next_inuse(free_i, total_segs, offset);
  1521. if (segno >= total_segs)
  1522. break;
  1523. offset = segno + 1;
  1524. valid_blocks = get_valid_blocks(sbi, segno, 0);
  1525. if (valid_blocks >= sbi->blocks_per_seg || !valid_blocks)
  1526. continue;
  1527. mutex_lock(&dirty_i->seglist_lock);
  1528. __locate_dirty_segment(sbi, segno, DIRTY);
  1529. mutex_unlock(&dirty_i->seglist_lock);
  1530. }
  1531. }
  1532. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  1533. {
  1534. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1535. unsigned int bitmap_size = f2fs_bitmap_size(TOTAL_SECS(sbi));
  1536. dirty_i->victim_secmap = kzalloc(bitmap_size, GFP_KERNEL);
  1537. if (!dirty_i->victim_secmap)
  1538. return -ENOMEM;
  1539. return 0;
  1540. }
  1541. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  1542. {
  1543. struct dirty_seglist_info *dirty_i;
  1544. unsigned int bitmap_size, i;
  1545. /* allocate memory for dirty segments list information */
  1546. dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
  1547. if (!dirty_i)
  1548. return -ENOMEM;
  1549. SM_I(sbi)->dirty_info = dirty_i;
  1550. mutex_init(&dirty_i->seglist_lock);
  1551. bitmap_size = f2fs_bitmap_size(TOTAL_SEGS(sbi));
  1552. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  1553. dirty_i->dirty_segmap[i] = kzalloc(bitmap_size, GFP_KERNEL);
  1554. if (!dirty_i->dirty_segmap[i])
  1555. return -ENOMEM;
  1556. }
  1557. init_dirty_segmap(sbi);
  1558. return init_victim_secmap(sbi);
  1559. }
  1560. /*
  1561. * Update min, max modified time for cost-benefit GC algorithm
  1562. */
  1563. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  1564. {
  1565. struct sit_info *sit_i = SIT_I(sbi);
  1566. unsigned int segno;
  1567. mutex_lock(&sit_i->sentry_lock);
  1568. sit_i->min_mtime = LLONG_MAX;
  1569. for (segno = 0; segno < TOTAL_SEGS(sbi); segno += sbi->segs_per_sec) {
  1570. unsigned int i;
  1571. unsigned long long mtime = 0;
  1572. for (i = 0; i < sbi->segs_per_sec; i++)
  1573. mtime += get_seg_entry(sbi, segno + i)->mtime;
  1574. mtime = div_u64(mtime, sbi->segs_per_sec);
  1575. if (sit_i->min_mtime > mtime)
  1576. sit_i->min_mtime = mtime;
  1577. }
  1578. sit_i->max_mtime = get_mtime(sbi);
  1579. mutex_unlock(&sit_i->sentry_lock);
  1580. }
  1581. int build_segment_manager(struct f2fs_sb_info *sbi)
  1582. {
  1583. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1584. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1585. struct f2fs_sm_info *sm_info;
  1586. int err;
  1587. sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
  1588. if (!sm_info)
  1589. return -ENOMEM;
  1590. /* init sm info */
  1591. sbi->sm_info = sm_info;
  1592. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  1593. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  1594. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  1595. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  1596. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  1597. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  1598. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  1599. sm_info->rec_prefree_segments = sm_info->main_segments *
  1600. DEF_RECLAIM_PREFREE_SEGMENTS / 100;
  1601. sm_info->ipu_policy = F2FS_IPU_DISABLE;
  1602. sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
  1603. INIT_LIST_HEAD(&sm_info->discard_list);
  1604. sm_info->nr_discards = 0;
  1605. sm_info->max_discards = 0;
  1606. if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
  1607. err = create_flush_cmd_control(sbi);
  1608. if (err)
  1609. return err;
  1610. }
  1611. err = build_sit_info(sbi);
  1612. if (err)
  1613. return err;
  1614. err = build_free_segmap(sbi);
  1615. if (err)
  1616. return err;
  1617. err = build_curseg(sbi);
  1618. if (err)
  1619. return err;
  1620. /* reinit free segmap based on SIT */
  1621. build_sit_entries(sbi);
  1622. init_free_segmap(sbi);
  1623. err = build_dirty_segmap(sbi);
  1624. if (err)
  1625. return err;
  1626. init_min_max_mtime(sbi);
  1627. return 0;
  1628. }
  1629. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  1630. enum dirty_type dirty_type)
  1631. {
  1632. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1633. mutex_lock(&dirty_i->seglist_lock);
  1634. kfree(dirty_i->dirty_segmap[dirty_type]);
  1635. dirty_i->nr_dirty[dirty_type] = 0;
  1636. mutex_unlock(&dirty_i->seglist_lock);
  1637. }
  1638. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  1639. {
  1640. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1641. kfree(dirty_i->victim_secmap);
  1642. }
  1643. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  1644. {
  1645. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1646. int i;
  1647. if (!dirty_i)
  1648. return;
  1649. /* discard pre-free/dirty segments list */
  1650. for (i = 0; i < NR_DIRTY_TYPE; i++)
  1651. discard_dirty_segmap(sbi, i);
  1652. destroy_victim_secmap(sbi);
  1653. SM_I(sbi)->dirty_info = NULL;
  1654. kfree(dirty_i);
  1655. }
  1656. static void destroy_curseg(struct f2fs_sb_info *sbi)
  1657. {
  1658. struct curseg_info *array = SM_I(sbi)->curseg_array;
  1659. int i;
  1660. if (!array)
  1661. return;
  1662. SM_I(sbi)->curseg_array = NULL;
  1663. for (i = 0; i < NR_CURSEG_TYPE; i++)
  1664. kfree(array[i].sum_blk);
  1665. kfree(array);
  1666. }
  1667. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  1668. {
  1669. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  1670. if (!free_i)
  1671. return;
  1672. SM_I(sbi)->free_info = NULL;
  1673. kfree(free_i->free_segmap);
  1674. kfree(free_i->free_secmap);
  1675. kfree(free_i);
  1676. }
  1677. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  1678. {
  1679. struct sit_info *sit_i = SIT_I(sbi);
  1680. unsigned int start;
  1681. if (!sit_i)
  1682. return;
  1683. if (sit_i->sentries) {
  1684. for (start = 0; start < TOTAL_SEGS(sbi); start++) {
  1685. kfree(sit_i->sentries[start].cur_valid_map);
  1686. kfree(sit_i->sentries[start].ckpt_valid_map);
  1687. }
  1688. }
  1689. vfree(sit_i->sentries);
  1690. vfree(sit_i->sec_entries);
  1691. kfree(sit_i->dirty_sentries_bitmap);
  1692. SM_I(sbi)->sit_info = NULL;
  1693. kfree(sit_i->sit_bitmap);
  1694. kfree(sit_i);
  1695. }
  1696. void destroy_segment_manager(struct f2fs_sb_info *sbi)
  1697. {
  1698. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1699. if (!sm_info)
  1700. return;
  1701. destroy_flush_cmd_control(sbi);
  1702. destroy_dirty_segmap(sbi);
  1703. destroy_curseg(sbi);
  1704. destroy_free_segmap(sbi);
  1705. destroy_sit_info(sbi);
  1706. sbi->sm_info = NULL;
  1707. kfree(sm_info);
  1708. }
  1709. int __init create_segment_manager_caches(void)
  1710. {
  1711. discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
  1712. sizeof(struct discard_entry));
  1713. if (!discard_entry_slab)
  1714. return -ENOMEM;
  1715. return 0;
  1716. }
  1717. void destroy_segment_manager_caches(void)
  1718. {
  1719. kmem_cache_destroy(discard_entry_slab);
  1720. }