segment.c 57 KB

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