segment.c 52 KB

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