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