segment.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745
  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/swap.h>
  18. #include <linux/timer.h>
  19. #include <linux/timer.h>
  20. #include "f2fs.h"
  21. #include "segment.h"
  22. #include "node.h"
  23. #include "trace.h"
  24. #include <trace/events/f2fs.h>
  25. #define __reverse_ffz(x) __reverse_ffs(~(x))
  26. static struct kmem_cache *discard_entry_slab;
  27. static struct kmem_cache *bio_entry_slab;
  28. static struct kmem_cache *sit_entry_set_slab;
  29. static struct kmem_cache *inmem_entry_slab;
  30. static unsigned long __reverse_ulong(unsigned char *str)
  31. {
  32. unsigned long tmp = 0;
  33. int shift = 24, idx = 0;
  34. #if BITS_PER_LONG == 64
  35. shift = 56;
  36. #endif
  37. while (shift >= 0) {
  38. tmp |= (unsigned long)str[idx++] << shift;
  39. shift -= BITS_PER_BYTE;
  40. }
  41. return tmp;
  42. }
  43. /*
  44. * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
  45. * MSB and LSB are reversed in a byte by f2fs_set_bit.
  46. */
  47. static inline unsigned long __reverse_ffs(unsigned long word)
  48. {
  49. int num = 0;
  50. #if BITS_PER_LONG == 64
  51. if ((word & 0xffffffff00000000UL) == 0)
  52. num += 32;
  53. else
  54. word >>= 32;
  55. #endif
  56. if ((word & 0xffff0000) == 0)
  57. num += 16;
  58. else
  59. word >>= 16;
  60. if ((word & 0xff00) == 0)
  61. num += 8;
  62. else
  63. word >>= 8;
  64. if ((word & 0xf0) == 0)
  65. num += 4;
  66. else
  67. word >>= 4;
  68. if ((word & 0xc) == 0)
  69. num += 2;
  70. else
  71. word >>= 2;
  72. if ((word & 0x2) == 0)
  73. num += 1;
  74. return num;
  75. }
  76. /*
  77. * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
  78. * f2fs_set_bit makes MSB and LSB reversed in a byte.
  79. * @size must be integral times of unsigned long.
  80. * Example:
  81. * MSB <--> LSB
  82. * f2fs_set_bit(0, bitmap) => 1000 0000
  83. * f2fs_set_bit(7, bitmap) => 0000 0001
  84. */
  85. static unsigned long __find_rev_next_bit(const unsigned long *addr,
  86. unsigned long size, unsigned long offset)
  87. {
  88. const unsigned long *p = addr + BIT_WORD(offset);
  89. unsigned long result = size;
  90. unsigned long tmp;
  91. if (offset >= size)
  92. return size;
  93. size -= (offset & ~(BITS_PER_LONG - 1));
  94. offset %= BITS_PER_LONG;
  95. while (1) {
  96. if (*p == 0)
  97. goto pass;
  98. tmp = __reverse_ulong((unsigned char *)p);
  99. tmp &= ~0UL >> offset;
  100. if (size < BITS_PER_LONG)
  101. tmp &= (~0UL << (BITS_PER_LONG - size));
  102. if (tmp)
  103. goto found;
  104. pass:
  105. if (size <= BITS_PER_LONG)
  106. break;
  107. size -= BITS_PER_LONG;
  108. offset = 0;
  109. p++;
  110. }
  111. return result;
  112. found:
  113. return result - size + __reverse_ffs(tmp);
  114. }
  115. static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
  116. unsigned long size, unsigned long offset)
  117. {
  118. const unsigned long *p = addr + BIT_WORD(offset);
  119. unsigned long result = size;
  120. unsigned long tmp;
  121. if (offset >= size)
  122. return size;
  123. size -= (offset & ~(BITS_PER_LONG - 1));
  124. offset %= BITS_PER_LONG;
  125. while (1) {
  126. if (*p == ~0UL)
  127. goto pass;
  128. tmp = __reverse_ulong((unsigned char *)p);
  129. if (offset)
  130. tmp |= ~0UL << (BITS_PER_LONG - offset);
  131. if (size < BITS_PER_LONG)
  132. tmp |= ~0UL >> size;
  133. if (tmp != ~0UL)
  134. goto found;
  135. pass:
  136. if (size <= BITS_PER_LONG)
  137. break;
  138. size -= BITS_PER_LONG;
  139. offset = 0;
  140. p++;
  141. }
  142. return result;
  143. found:
  144. return result - size + __reverse_ffz(tmp);
  145. }
  146. void register_inmem_page(struct inode *inode, struct page *page)
  147. {
  148. struct f2fs_inode_info *fi = F2FS_I(inode);
  149. struct inmem_pages *new;
  150. f2fs_trace_pid(page);
  151. set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
  152. SetPagePrivate(page);
  153. new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
  154. /* add atomic page indices to the list */
  155. new->page = page;
  156. INIT_LIST_HEAD(&new->list);
  157. /* increase reference count with clean state */
  158. mutex_lock(&fi->inmem_lock);
  159. get_page(page);
  160. list_add_tail(&new->list, &fi->inmem_pages);
  161. inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  162. mutex_unlock(&fi->inmem_lock);
  163. trace_f2fs_register_inmem_page(page, INMEM);
  164. }
  165. static int __revoke_inmem_pages(struct inode *inode,
  166. struct list_head *head, bool drop, bool recover)
  167. {
  168. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  169. struct inmem_pages *cur, *tmp;
  170. int err = 0;
  171. list_for_each_entry_safe(cur, tmp, head, list) {
  172. struct page *page = cur->page;
  173. if (drop)
  174. trace_f2fs_commit_inmem_page(page, INMEM_DROP);
  175. lock_page(page);
  176. if (recover) {
  177. struct dnode_of_data dn;
  178. struct node_info ni;
  179. trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
  180. set_new_dnode(&dn, inode, NULL, NULL, 0);
  181. if (get_dnode_of_data(&dn, page->index, LOOKUP_NODE)) {
  182. err = -EAGAIN;
  183. goto next;
  184. }
  185. get_node_info(sbi, dn.nid, &ni);
  186. f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
  187. cur->old_addr, ni.version, true, true);
  188. f2fs_put_dnode(&dn);
  189. }
  190. next:
  191. /* we don't need to invalidate this in the sccessful status */
  192. if (drop || recover)
  193. ClearPageUptodate(page);
  194. set_page_private(page, 0);
  195. ClearPagePrivate(page);
  196. f2fs_put_page(page, 1);
  197. list_del(&cur->list);
  198. kmem_cache_free(inmem_entry_slab, cur);
  199. dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  200. }
  201. return err;
  202. }
  203. void drop_inmem_pages(struct inode *inode)
  204. {
  205. struct f2fs_inode_info *fi = F2FS_I(inode);
  206. clear_inode_flag(inode, FI_ATOMIC_FILE);
  207. mutex_lock(&fi->inmem_lock);
  208. __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
  209. mutex_unlock(&fi->inmem_lock);
  210. }
  211. static int __commit_inmem_pages(struct inode *inode,
  212. struct list_head *revoke_list)
  213. {
  214. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  215. struct f2fs_inode_info *fi = F2FS_I(inode);
  216. struct inmem_pages *cur, *tmp;
  217. struct f2fs_io_info fio = {
  218. .sbi = sbi,
  219. .type = DATA,
  220. .op = REQ_OP_WRITE,
  221. .op_flags = WRITE_SYNC | REQ_PRIO,
  222. .encrypted_page = NULL,
  223. };
  224. bool submit_bio = false;
  225. int err = 0;
  226. list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
  227. struct page *page = cur->page;
  228. lock_page(page);
  229. if (page->mapping == inode->i_mapping) {
  230. trace_f2fs_commit_inmem_page(page, INMEM);
  231. set_page_dirty(page);
  232. f2fs_wait_on_page_writeback(page, DATA, true);
  233. if (clear_page_dirty_for_io(page)) {
  234. inode_dec_dirty_pages(inode);
  235. remove_dirty_inode(inode);
  236. }
  237. fio.page = page;
  238. err = do_write_data_page(&fio);
  239. if (err) {
  240. unlock_page(page);
  241. break;
  242. }
  243. /* record old blkaddr for revoking */
  244. cur->old_addr = fio.old_blkaddr;
  245. clear_cold_data(page);
  246. submit_bio = true;
  247. }
  248. unlock_page(page);
  249. list_move_tail(&cur->list, revoke_list);
  250. }
  251. if (submit_bio)
  252. f2fs_submit_merged_bio_cond(sbi, inode, NULL, 0, DATA, WRITE);
  253. if (!err)
  254. __revoke_inmem_pages(inode, revoke_list, false, false);
  255. return err;
  256. }
  257. int commit_inmem_pages(struct inode *inode)
  258. {
  259. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  260. struct f2fs_inode_info *fi = F2FS_I(inode);
  261. struct list_head revoke_list;
  262. int err;
  263. INIT_LIST_HEAD(&revoke_list);
  264. f2fs_balance_fs(sbi, true);
  265. f2fs_lock_op(sbi);
  266. mutex_lock(&fi->inmem_lock);
  267. err = __commit_inmem_pages(inode, &revoke_list);
  268. if (err) {
  269. int ret;
  270. /*
  271. * try to revoke all committed pages, but still we could fail
  272. * due to no memory or other reason, if that happened, EAGAIN
  273. * will be returned, which means in such case, transaction is
  274. * already not integrity, caller should use journal to do the
  275. * recovery or rewrite & commit last transaction. For other
  276. * error number, revoking was done by filesystem itself.
  277. */
  278. ret = __revoke_inmem_pages(inode, &revoke_list, false, true);
  279. if (ret)
  280. err = ret;
  281. /* drop all uncommitted pages */
  282. __revoke_inmem_pages(inode, &fi->inmem_pages, true, false);
  283. }
  284. mutex_unlock(&fi->inmem_lock);
  285. f2fs_unlock_op(sbi);
  286. return err;
  287. }
  288. /*
  289. * This function balances dirty node and dentry pages.
  290. * In addition, it controls garbage collection.
  291. */
  292. void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
  293. {
  294. #ifdef CONFIG_F2FS_FAULT_INJECTION
  295. if (time_to_inject(sbi, FAULT_CHECKPOINT))
  296. f2fs_stop_checkpoint(sbi, false);
  297. #endif
  298. if (!need)
  299. return;
  300. /* balance_fs_bg is able to be pending */
  301. if (excess_cached_nats(sbi))
  302. f2fs_balance_fs_bg(sbi);
  303. /*
  304. * We should do GC or end up with checkpoint, if there are so many dirty
  305. * dir/node pages without enough free segments.
  306. */
  307. if (has_not_enough_free_secs(sbi, 0, 0)) {
  308. mutex_lock(&sbi->gc_mutex);
  309. f2fs_gc(sbi, false);
  310. }
  311. }
  312. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
  313. {
  314. /* try to shrink extent cache when there is no enough memory */
  315. if (!available_free_memory(sbi, EXTENT_CACHE))
  316. f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
  317. /* check the # of cached NAT entries */
  318. if (!available_free_memory(sbi, NAT_ENTRIES))
  319. try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
  320. if (!available_free_memory(sbi, FREE_NIDS))
  321. try_to_free_nids(sbi, MAX_FREE_NIDS);
  322. else
  323. build_free_nids(sbi, false);
  324. /* checkpoint is the only way to shrink partial cached entries */
  325. if (!available_free_memory(sbi, NAT_ENTRIES) ||
  326. !available_free_memory(sbi, INO_ENTRIES) ||
  327. excess_prefree_segs(sbi) ||
  328. excess_dirty_nats(sbi) ||
  329. (is_idle(sbi) && f2fs_time_over(sbi, CP_TIME))) {
  330. if (test_opt(sbi, DATA_FLUSH)) {
  331. struct blk_plug plug;
  332. blk_start_plug(&plug);
  333. sync_dirty_inodes(sbi, FILE_INODE);
  334. blk_finish_plug(&plug);
  335. }
  336. f2fs_sync_fs(sbi->sb, true);
  337. stat_inc_bg_cp_count(sbi->stat_info);
  338. }
  339. }
  340. static int issue_flush_thread(void *data)
  341. {
  342. struct f2fs_sb_info *sbi = data;
  343. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  344. wait_queue_head_t *q = &fcc->flush_wait_queue;
  345. repeat:
  346. if (kthread_should_stop())
  347. return 0;
  348. if (!llist_empty(&fcc->issue_list)) {
  349. struct bio *bio;
  350. struct flush_cmd *cmd, *next;
  351. int ret;
  352. bio = f2fs_bio_alloc(0);
  353. fcc->dispatch_list = llist_del_all(&fcc->issue_list);
  354. fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
  355. bio->bi_bdev = sbi->sb->s_bdev;
  356. bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH);
  357. ret = submit_bio_wait(bio);
  358. llist_for_each_entry_safe(cmd, next,
  359. fcc->dispatch_list, llnode) {
  360. cmd->ret = ret;
  361. complete(&cmd->wait);
  362. }
  363. bio_put(bio);
  364. fcc->dispatch_list = NULL;
  365. }
  366. wait_event_interruptible(*q,
  367. kthread_should_stop() || !llist_empty(&fcc->issue_list));
  368. goto repeat;
  369. }
  370. int f2fs_issue_flush(struct f2fs_sb_info *sbi)
  371. {
  372. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  373. struct flush_cmd cmd;
  374. trace_f2fs_issue_flush(sbi->sb, test_opt(sbi, NOBARRIER),
  375. test_opt(sbi, FLUSH_MERGE));
  376. if (test_opt(sbi, NOBARRIER))
  377. return 0;
  378. if (!test_opt(sbi, FLUSH_MERGE) || !atomic_read(&fcc->submit_flush)) {
  379. struct bio *bio = f2fs_bio_alloc(0);
  380. int ret;
  381. atomic_inc(&fcc->submit_flush);
  382. bio->bi_bdev = sbi->sb->s_bdev;
  383. bio_set_op_attrs(bio, REQ_OP_WRITE, WRITE_FLUSH);
  384. ret = submit_bio_wait(bio);
  385. atomic_dec(&fcc->submit_flush);
  386. bio_put(bio);
  387. return ret;
  388. }
  389. init_completion(&cmd.wait);
  390. atomic_inc(&fcc->submit_flush);
  391. llist_add(&cmd.llnode, &fcc->issue_list);
  392. if (!fcc->dispatch_list)
  393. wake_up(&fcc->flush_wait_queue);
  394. wait_for_completion(&cmd.wait);
  395. atomic_dec(&fcc->submit_flush);
  396. return cmd.ret;
  397. }
  398. int create_flush_cmd_control(struct f2fs_sb_info *sbi)
  399. {
  400. dev_t dev = sbi->sb->s_bdev->bd_dev;
  401. struct flush_cmd_control *fcc;
  402. int err = 0;
  403. fcc = kzalloc(sizeof(struct flush_cmd_control), GFP_KERNEL);
  404. if (!fcc)
  405. return -ENOMEM;
  406. atomic_set(&fcc->submit_flush, 0);
  407. init_waitqueue_head(&fcc->flush_wait_queue);
  408. init_llist_head(&fcc->issue_list);
  409. SM_I(sbi)->cmd_control_info = fcc;
  410. fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
  411. "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
  412. if (IS_ERR(fcc->f2fs_issue_flush)) {
  413. err = PTR_ERR(fcc->f2fs_issue_flush);
  414. kfree(fcc);
  415. SM_I(sbi)->cmd_control_info = NULL;
  416. return err;
  417. }
  418. return err;
  419. }
  420. void destroy_flush_cmd_control(struct f2fs_sb_info *sbi)
  421. {
  422. struct flush_cmd_control *fcc = SM_I(sbi)->cmd_control_info;
  423. if (fcc && fcc->f2fs_issue_flush)
  424. kthread_stop(fcc->f2fs_issue_flush);
  425. kfree(fcc);
  426. SM_I(sbi)->cmd_control_info = NULL;
  427. }
  428. static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  429. enum dirty_type dirty_type)
  430. {
  431. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  432. /* need not be added */
  433. if (IS_CURSEG(sbi, segno))
  434. return;
  435. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  436. dirty_i->nr_dirty[dirty_type]++;
  437. if (dirty_type == DIRTY) {
  438. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  439. enum dirty_type t = sentry->type;
  440. if (unlikely(t >= DIRTY)) {
  441. f2fs_bug_on(sbi, 1);
  442. return;
  443. }
  444. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
  445. dirty_i->nr_dirty[t]++;
  446. }
  447. }
  448. static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  449. enum dirty_type dirty_type)
  450. {
  451. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  452. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  453. dirty_i->nr_dirty[dirty_type]--;
  454. if (dirty_type == DIRTY) {
  455. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  456. enum dirty_type t = sentry->type;
  457. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
  458. dirty_i->nr_dirty[t]--;
  459. if (get_valid_blocks(sbi, segno, sbi->segs_per_sec) == 0)
  460. clear_bit(GET_SECNO(sbi, segno),
  461. dirty_i->victim_secmap);
  462. }
  463. }
  464. /*
  465. * Should not occur error such as -ENOMEM.
  466. * Adding dirty entry into seglist is not critical operation.
  467. * If a given segment is one of current working segments, it won't be added.
  468. */
  469. static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
  470. {
  471. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  472. unsigned short valid_blocks;
  473. if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
  474. return;
  475. mutex_lock(&dirty_i->seglist_lock);
  476. valid_blocks = get_valid_blocks(sbi, segno, 0);
  477. if (valid_blocks == 0) {
  478. __locate_dirty_segment(sbi, segno, PRE);
  479. __remove_dirty_segment(sbi, segno, DIRTY);
  480. } else if (valid_blocks < sbi->blocks_per_seg) {
  481. __locate_dirty_segment(sbi, segno, DIRTY);
  482. } else {
  483. /* Recovery routine with SSR needs this */
  484. __remove_dirty_segment(sbi, segno, DIRTY);
  485. }
  486. mutex_unlock(&dirty_i->seglist_lock);
  487. }
  488. static struct bio_entry *__add_bio_entry(struct f2fs_sb_info *sbi,
  489. struct bio *bio)
  490. {
  491. struct list_head *wait_list = &(SM_I(sbi)->wait_list);
  492. struct bio_entry *be = f2fs_kmem_cache_alloc(bio_entry_slab, GFP_NOFS);
  493. INIT_LIST_HEAD(&be->list);
  494. be->bio = bio;
  495. init_completion(&be->event);
  496. list_add_tail(&be->list, wait_list);
  497. return be;
  498. }
  499. void f2fs_wait_all_discard_bio(struct f2fs_sb_info *sbi)
  500. {
  501. struct list_head *wait_list = &(SM_I(sbi)->wait_list);
  502. struct bio_entry *be, *tmp;
  503. list_for_each_entry_safe(be, tmp, wait_list, list) {
  504. struct bio *bio = be->bio;
  505. int err;
  506. wait_for_completion_io(&be->event);
  507. err = be->error;
  508. if (err == -EOPNOTSUPP)
  509. err = 0;
  510. if (err)
  511. f2fs_msg(sbi->sb, KERN_INFO,
  512. "Issue discard failed, ret: %d", err);
  513. bio_put(bio);
  514. list_del(&be->list);
  515. kmem_cache_free(bio_entry_slab, be);
  516. }
  517. }
  518. static void f2fs_submit_bio_wait_endio(struct bio *bio)
  519. {
  520. struct bio_entry *be = (struct bio_entry *)bio->bi_private;
  521. be->error = bio->bi_error;
  522. complete(&be->event);
  523. }
  524. /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
  525. static int __f2fs_issue_discard_async(struct f2fs_sb_info *sbi,
  526. block_t blkstart, block_t blklen)
  527. {
  528. struct block_device *bdev = sbi->sb->s_bdev;
  529. struct bio *bio = NULL;
  530. int err;
  531. trace_f2fs_issue_discard(sbi->sb, blkstart, blklen);
  532. err = __blkdev_issue_discard(bdev,
  533. SECTOR_FROM_BLOCK(blkstart),
  534. SECTOR_FROM_BLOCK(blklen),
  535. GFP_NOFS, 0, &bio);
  536. if (!err && bio) {
  537. struct bio_entry *be = __add_bio_entry(sbi, bio);
  538. bio->bi_private = be;
  539. bio->bi_end_io = f2fs_submit_bio_wait_endio;
  540. bio->bi_opf |= REQ_SYNC;
  541. submit_bio(bio);
  542. }
  543. return err;
  544. }
  545. #ifdef CONFIG_BLK_DEV_ZONED
  546. static int f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
  547. block_t blkstart, block_t blklen)
  548. {
  549. sector_t sector = SECTOR_FROM_BLOCK(blkstart);
  550. sector_t nr_sects = SECTOR_FROM_BLOCK(blklen);
  551. struct block_device *bdev = sbi->sb->s_bdev;
  552. if (nr_sects != bdev_zone_size(bdev)) {
  553. f2fs_msg(sbi->sb, KERN_INFO,
  554. "Unaligned discard attempted (sector %llu + %llu)",
  555. (unsigned long long)sector,
  556. (unsigned long long)nr_sects);
  557. return -EIO;
  558. }
  559. /*
  560. * We need to know the type of the zone: for conventional zones,
  561. * use regular discard if the drive supports it. For sequential
  562. * zones, reset the zone write pointer.
  563. */
  564. switch (get_blkz_type(sbi, blkstart)) {
  565. case BLK_ZONE_TYPE_CONVENTIONAL:
  566. if (!blk_queue_discard(bdev_get_queue(bdev)))
  567. return 0;
  568. return __f2fs_issue_discard_async(sbi, blkstart,
  569. blklen);
  570. case BLK_ZONE_TYPE_SEQWRITE_REQ:
  571. case BLK_ZONE_TYPE_SEQWRITE_PREF:
  572. return blkdev_reset_zones(bdev, sector,
  573. nr_sects, GFP_NOFS);
  574. default:
  575. /* Unknown zone type: broken device ? */
  576. return -EIO;
  577. }
  578. }
  579. #endif
  580. static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
  581. block_t blkstart, block_t blklen)
  582. {
  583. struct seg_entry *se;
  584. unsigned int offset;
  585. block_t i;
  586. for (i = blkstart; i < blkstart + blklen; i++) {
  587. se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
  588. offset = GET_BLKOFF_FROM_SEG0(sbi, i);
  589. if (!f2fs_test_and_set_bit(offset, se->discard_map))
  590. sbi->discard_blks--;
  591. }
  592. #ifdef CONFIG_BLK_DEV_ZONED
  593. if (f2fs_sb_mounted_blkzoned(sbi->sb))
  594. return f2fs_issue_discard_zone(sbi, blkstart, blklen);
  595. #endif
  596. return __f2fs_issue_discard_async(sbi, blkstart, blklen);
  597. }
  598. static void __add_discard_entry(struct f2fs_sb_info *sbi,
  599. struct cp_control *cpc, struct seg_entry *se,
  600. unsigned int start, unsigned int end)
  601. {
  602. struct list_head *head = &SM_I(sbi)->discard_list;
  603. struct discard_entry *new, *last;
  604. if (!list_empty(head)) {
  605. last = list_last_entry(head, struct discard_entry, list);
  606. if (START_BLOCK(sbi, cpc->trim_start) + start ==
  607. last->blkaddr + last->len) {
  608. last->len += end - start;
  609. goto done;
  610. }
  611. }
  612. new = f2fs_kmem_cache_alloc(discard_entry_slab, GFP_NOFS);
  613. INIT_LIST_HEAD(&new->list);
  614. new->blkaddr = START_BLOCK(sbi, cpc->trim_start) + start;
  615. new->len = end - start;
  616. list_add_tail(&new->list, head);
  617. done:
  618. SM_I(sbi)->nr_discards += end - start;
  619. }
  620. static void add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  621. {
  622. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  623. int max_blocks = sbi->blocks_per_seg;
  624. struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
  625. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  626. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  627. unsigned long *discard_map = (unsigned long *)se->discard_map;
  628. unsigned long *dmap = SIT_I(sbi)->tmp_map;
  629. unsigned int start = 0, end = -1;
  630. bool force = (cpc->reason == CP_DISCARD);
  631. int i;
  632. if (se->valid_blocks == max_blocks || !f2fs_discard_en(sbi))
  633. return;
  634. if (!force) {
  635. if (!test_opt(sbi, DISCARD) || !se->valid_blocks ||
  636. SM_I(sbi)->nr_discards >= SM_I(sbi)->max_discards)
  637. return;
  638. }
  639. /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
  640. for (i = 0; i < entries; i++)
  641. dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
  642. (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
  643. while (force || SM_I(sbi)->nr_discards <= SM_I(sbi)->max_discards) {
  644. start = __find_rev_next_bit(dmap, max_blocks, end + 1);
  645. if (start >= max_blocks)
  646. break;
  647. end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
  648. if (force && start && end != max_blocks
  649. && (end - start) < cpc->trim_minlen)
  650. continue;
  651. __add_discard_entry(sbi, cpc, se, start, end);
  652. }
  653. }
  654. void release_discard_addrs(struct f2fs_sb_info *sbi)
  655. {
  656. struct list_head *head = &(SM_I(sbi)->discard_list);
  657. struct discard_entry *entry, *this;
  658. /* drop caches */
  659. list_for_each_entry_safe(entry, this, head, list) {
  660. list_del(&entry->list);
  661. kmem_cache_free(discard_entry_slab, entry);
  662. }
  663. }
  664. /*
  665. * Should call clear_prefree_segments after checkpoint is done.
  666. */
  667. static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
  668. {
  669. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  670. unsigned int segno;
  671. mutex_lock(&dirty_i->seglist_lock);
  672. for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
  673. __set_test_and_free(sbi, segno);
  674. mutex_unlock(&dirty_i->seglist_lock);
  675. }
  676. void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  677. {
  678. struct list_head *head = &(SM_I(sbi)->discard_list);
  679. struct discard_entry *entry, *this;
  680. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  681. struct blk_plug plug;
  682. unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
  683. unsigned int start = 0, end = -1;
  684. unsigned int secno, start_segno;
  685. bool force = (cpc->reason == CP_DISCARD);
  686. blk_start_plug(&plug);
  687. mutex_lock(&dirty_i->seglist_lock);
  688. while (1) {
  689. int i;
  690. start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
  691. if (start >= MAIN_SEGS(sbi))
  692. break;
  693. end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
  694. start + 1);
  695. for (i = start; i < end; i++)
  696. clear_bit(i, prefree_map);
  697. dirty_i->nr_dirty[PRE] -= end - start;
  698. if (force || !test_opt(sbi, DISCARD))
  699. continue;
  700. if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
  701. f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
  702. (end - start) << sbi->log_blocks_per_seg);
  703. continue;
  704. }
  705. next:
  706. secno = GET_SECNO(sbi, start);
  707. start_segno = secno * sbi->segs_per_sec;
  708. if (!IS_CURSEC(sbi, secno) &&
  709. !get_valid_blocks(sbi, start, sbi->segs_per_sec))
  710. f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
  711. sbi->segs_per_sec << sbi->log_blocks_per_seg);
  712. start = start_segno + sbi->segs_per_sec;
  713. if (start < end)
  714. goto next;
  715. }
  716. mutex_unlock(&dirty_i->seglist_lock);
  717. /* send small discards */
  718. list_for_each_entry_safe(entry, this, head, list) {
  719. if (force && entry->len < cpc->trim_minlen)
  720. goto skip;
  721. f2fs_issue_discard(sbi, entry->blkaddr, entry->len);
  722. cpc->trimmed += entry->len;
  723. skip:
  724. list_del(&entry->list);
  725. SM_I(sbi)->nr_discards -= entry->len;
  726. kmem_cache_free(discard_entry_slab, entry);
  727. }
  728. blk_finish_plug(&plug);
  729. }
  730. static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
  731. {
  732. struct sit_info *sit_i = SIT_I(sbi);
  733. if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
  734. sit_i->dirty_sentries++;
  735. return false;
  736. }
  737. return true;
  738. }
  739. static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
  740. unsigned int segno, int modified)
  741. {
  742. struct seg_entry *se = get_seg_entry(sbi, segno);
  743. se->type = type;
  744. if (modified)
  745. __mark_sit_entry_dirty(sbi, segno);
  746. }
  747. static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
  748. {
  749. struct seg_entry *se;
  750. unsigned int segno, offset;
  751. long int new_vblocks;
  752. segno = GET_SEGNO(sbi, blkaddr);
  753. se = get_seg_entry(sbi, segno);
  754. new_vblocks = se->valid_blocks + del;
  755. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  756. f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
  757. (new_vblocks > sbi->blocks_per_seg)));
  758. se->valid_blocks = new_vblocks;
  759. se->mtime = get_mtime(sbi);
  760. SIT_I(sbi)->max_mtime = se->mtime;
  761. /* Update valid block bitmap */
  762. if (del > 0) {
  763. if (f2fs_test_and_set_bit(offset, se->cur_valid_map))
  764. f2fs_bug_on(sbi, 1);
  765. if (f2fs_discard_en(sbi) &&
  766. !f2fs_test_and_set_bit(offset, se->discard_map))
  767. sbi->discard_blks--;
  768. } else {
  769. if (!f2fs_test_and_clear_bit(offset, se->cur_valid_map))
  770. f2fs_bug_on(sbi, 1);
  771. if (f2fs_discard_en(sbi) &&
  772. f2fs_test_and_clear_bit(offset, se->discard_map))
  773. sbi->discard_blks++;
  774. }
  775. if (!f2fs_test_bit(offset, se->ckpt_valid_map))
  776. se->ckpt_valid_blocks += del;
  777. __mark_sit_entry_dirty(sbi, segno);
  778. /* update total number of valid blocks to be written in ckpt area */
  779. SIT_I(sbi)->written_valid_blocks += del;
  780. if (sbi->segs_per_sec > 1)
  781. get_sec_entry(sbi, segno)->valid_blocks += del;
  782. }
  783. void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new)
  784. {
  785. update_sit_entry(sbi, new, 1);
  786. if (GET_SEGNO(sbi, old) != NULL_SEGNO)
  787. update_sit_entry(sbi, old, -1);
  788. locate_dirty_segment(sbi, GET_SEGNO(sbi, old));
  789. locate_dirty_segment(sbi, GET_SEGNO(sbi, new));
  790. }
  791. void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
  792. {
  793. unsigned int segno = GET_SEGNO(sbi, addr);
  794. struct sit_info *sit_i = SIT_I(sbi);
  795. f2fs_bug_on(sbi, addr == NULL_ADDR);
  796. if (addr == NEW_ADDR)
  797. return;
  798. /* add it into sit main buffer */
  799. mutex_lock(&sit_i->sentry_lock);
  800. update_sit_entry(sbi, addr, -1);
  801. /* add it into dirty seglist */
  802. locate_dirty_segment(sbi, segno);
  803. mutex_unlock(&sit_i->sentry_lock);
  804. }
  805. bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
  806. {
  807. struct sit_info *sit_i = SIT_I(sbi);
  808. unsigned int segno, offset;
  809. struct seg_entry *se;
  810. bool is_cp = false;
  811. if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
  812. return true;
  813. mutex_lock(&sit_i->sentry_lock);
  814. segno = GET_SEGNO(sbi, blkaddr);
  815. se = get_seg_entry(sbi, segno);
  816. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  817. if (f2fs_test_bit(offset, se->ckpt_valid_map))
  818. is_cp = true;
  819. mutex_unlock(&sit_i->sentry_lock);
  820. return is_cp;
  821. }
  822. /*
  823. * This function should be resided under the curseg_mutex lock
  824. */
  825. static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
  826. struct f2fs_summary *sum)
  827. {
  828. struct curseg_info *curseg = CURSEG_I(sbi, type);
  829. void *addr = curseg->sum_blk;
  830. addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
  831. memcpy(addr, sum, sizeof(struct f2fs_summary));
  832. }
  833. /*
  834. * Calculate the number of current summary pages for writing
  835. */
  836. int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
  837. {
  838. int valid_sum_count = 0;
  839. int i, sum_in_page;
  840. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  841. if (sbi->ckpt->alloc_type[i] == SSR)
  842. valid_sum_count += sbi->blocks_per_seg;
  843. else {
  844. if (for_ra)
  845. valid_sum_count += le16_to_cpu(
  846. F2FS_CKPT(sbi)->cur_data_blkoff[i]);
  847. else
  848. valid_sum_count += curseg_blkoff(sbi, i);
  849. }
  850. }
  851. sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
  852. SUM_FOOTER_SIZE) / SUMMARY_SIZE;
  853. if (valid_sum_count <= sum_in_page)
  854. return 1;
  855. else if ((valid_sum_count - sum_in_page) <=
  856. (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
  857. return 2;
  858. return 3;
  859. }
  860. /*
  861. * Caller should put this summary page
  862. */
  863. struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
  864. {
  865. return get_meta_page(sbi, GET_SUM_BLOCK(sbi, segno));
  866. }
  867. void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr)
  868. {
  869. struct page *page = grab_meta_page(sbi, blk_addr);
  870. void *dst = page_address(page);
  871. if (src)
  872. memcpy(dst, src, PAGE_SIZE);
  873. else
  874. memset(dst, 0, PAGE_SIZE);
  875. set_page_dirty(page);
  876. f2fs_put_page(page, 1);
  877. }
  878. static void write_sum_page(struct f2fs_sb_info *sbi,
  879. struct f2fs_summary_block *sum_blk, block_t blk_addr)
  880. {
  881. update_meta_page(sbi, (void *)sum_blk, blk_addr);
  882. }
  883. static void write_current_sum_page(struct f2fs_sb_info *sbi,
  884. int type, block_t blk_addr)
  885. {
  886. struct curseg_info *curseg = CURSEG_I(sbi, type);
  887. struct page *page = grab_meta_page(sbi, blk_addr);
  888. struct f2fs_summary_block *src = curseg->sum_blk;
  889. struct f2fs_summary_block *dst;
  890. dst = (struct f2fs_summary_block *)page_address(page);
  891. mutex_lock(&curseg->curseg_mutex);
  892. down_read(&curseg->journal_rwsem);
  893. memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
  894. up_read(&curseg->journal_rwsem);
  895. memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
  896. memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
  897. mutex_unlock(&curseg->curseg_mutex);
  898. set_page_dirty(page);
  899. f2fs_put_page(page, 1);
  900. }
  901. static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
  902. {
  903. struct curseg_info *curseg = CURSEG_I(sbi, type);
  904. unsigned int segno = curseg->segno + 1;
  905. struct free_segmap_info *free_i = FREE_I(sbi);
  906. if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
  907. return !test_bit(segno, free_i->free_segmap);
  908. return 0;
  909. }
  910. /*
  911. * Find a new segment from the free segments bitmap to right order
  912. * This function should be returned with success, otherwise BUG
  913. */
  914. static void get_new_segment(struct f2fs_sb_info *sbi,
  915. unsigned int *newseg, bool new_sec, int dir)
  916. {
  917. struct free_segmap_info *free_i = FREE_I(sbi);
  918. unsigned int segno, secno, zoneno;
  919. unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
  920. unsigned int hint = *newseg / sbi->segs_per_sec;
  921. unsigned int old_zoneno = GET_ZONENO_FROM_SEGNO(sbi, *newseg);
  922. unsigned int left_start = hint;
  923. bool init = true;
  924. int go_left = 0;
  925. int i;
  926. spin_lock(&free_i->segmap_lock);
  927. if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
  928. segno = find_next_zero_bit(free_i->free_segmap,
  929. (hint + 1) * sbi->segs_per_sec, *newseg + 1);
  930. if (segno < (hint + 1) * sbi->segs_per_sec)
  931. goto got_it;
  932. }
  933. find_other_zone:
  934. secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
  935. if (secno >= MAIN_SECS(sbi)) {
  936. if (dir == ALLOC_RIGHT) {
  937. secno = find_next_zero_bit(free_i->free_secmap,
  938. MAIN_SECS(sbi), 0);
  939. f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
  940. } else {
  941. go_left = 1;
  942. left_start = hint - 1;
  943. }
  944. }
  945. if (go_left == 0)
  946. goto skip_left;
  947. while (test_bit(left_start, free_i->free_secmap)) {
  948. if (left_start > 0) {
  949. left_start--;
  950. continue;
  951. }
  952. left_start = find_next_zero_bit(free_i->free_secmap,
  953. MAIN_SECS(sbi), 0);
  954. f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
  955. break;
  956. }
  957. secno = left_start;
  958. skip_left:
  959. hint = secno;
  960. segno = secno * sbi->segs_per_sec;
  961. zoneno = secno / sbi->secs_per_zone;
  962. /* give up on finding another zone */
  963. if (!init)
  964. goto got_it;
  965. if (sbi->secs_per_zone == 1)
  966. goto got_it;
  967. if (zoneno == old_zoneno)
  968. goto got_it;
  969. if (dir == ALLOC_LEFT) {
  970. if (!go_left && zoneno + 1 >= total_zones)
  971. goto got_it;
  972. if (go_left && zoneno == 0)
  973. goto got_it;
  974. }
  975. for (i = 0; i < NR_CURSEG_TYPE; i++)
  976. if (CURSEG_I(sbi, i)->zone == zoneno)
  977. break;
  978. if (i < NR_CURSEG_TYPE) {
  979. /* zone is in user, try another */
  980. if (go_left)
  981. hint = zoneno * sbi->secs_per_zone - 1;
  982. else if (zoneno + 1 >= total_zones)
  983. hint = 0;
  984. else
  985. hint = (zoneno + 1) * sbi->secs_per_zone;
  986. init = false;
  987. goto find_other_zone;
  988. }
  989. got_it:
  990. /* set it as dirty segment in free segmap */
  991. f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
  992. __set_inuse(sbi, segno);
  993. *newseg = segno;
  994. spin_unlock(&free_i->segmap_lock);
  995. }
  996. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  997. {
  998. struct curseg_info *curseg = CURSEG_I(sbi, type);
  999. struct summary_footer *sum_footer;
  1000. curseg->segno = curseg->next_segno;
  1001. curseg->zone = GET_ZONENO_FROM_SEGNO(sbi, curseg->segno);
  1002. curseg->next_blkoff = 0;
  1003. curseg->next_segno = NULL_SEGNO;
  1004. sum_footer = &(curseg->sum_blk->footer);
  1005. memset(sum_footer, 0, sizeof(struct summary_footer));
  1006. if (IS_DATASEG(type))
  1007. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  1008. if (IS_NODESEG(type))
  1009. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  1010. __set_sit_entry_type(sbi, type, curseg->segno, modified);
  1011. }
  1012. /*
  1013. * Allocate a current working segment.
  1014. * This function always allocates a free segment in LFS manner.
  1015. */
  1016. static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  1017. {
  1018. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1019. unsigned int segno = curseg->segno;
  1020. int dir = ALLOC_LEFT;
  1021. write_sum_page(sbi, curseg->sum_blk,
  1022. GET_SUM_BLOCK(sbi, segno));
  1023. if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
  1024. dir = ALLOC_RIGHT;
  1025. if (test_opt(sbi, NOHEAP))
  1026. dir = ALLOC_RIGHT;
  1027. get_new_segment(sbi, &segno, new_sec, dir);
  1028. curseg->next_segno = segno;
  1029. reset_curseg(sbi, type, 1);
  1030. curseg->alloc_type = LFS;
  1031. }
  1032. static void __next_free_blkoff(struct f2fs_sb_info *sbi,
  1033. struct curseg_info *seg, block_t start)
  1034. {
  1035. struct seg_entry *se = get_seg_entry(sbi, seg->segno);
  1036. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  1037. unsigned long *target_map = SIT_I(sbi)->tmp_map;
  1038. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  1039. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  1040. int i, pos;
  1041. for (i = 0; i < entries; i++)
  1042. target_map[i] = ckpt_map[i] | cur_map[i];
  1043. pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
  1044. seg->next_blkoff = pos;
  1045. }
  1046. /*
  1047. * If a segment is written by LFS manner, next block offset is just obtained
  1048. * by increasing the current block offset. However, if a segment is written by
  1049. * SSR manner, next block offset obtained by calling __next_free_blkoff
  1050. */
  1051. static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
  1052. struct curseg_info *seg)
  1053. {
  1054. if (seg->alloc_type == SSR)
  1055. __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
  1056. else
  1057. seg->next_blkoff++;
  1058. }
  1059. /*
  1060. * This function always allocates a used segment(from dirty seglist) by SSR
  1061. * manner, so it should recover the existing segment information of valid blocks
  1062. */
  1063. static void change_curseg(struct f2fs_sb_info *sbi, int type, bool reuse)
  1064. {
  1065. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1066. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1067. unsigned int new_segno = curseg->next_segno;
  1068. struct f2fs_summary_block *sum_node;
  1069. struct page *sum_page;
  1070. write_sum_page(sbi, curseg->sum_blk,
  1071. GET_SUM_BLOCK(sbi, curseg->segno));
  1072. __set_test_and_inuse(sbi, new_segno);
  1073. mutex_lock(&dirty_i->seglist_lock);
  1074. __remove_dirty_segment(sbi, new_segno, PRE);
  1075. __remove_dirty_segment(sbi, new_segno, DIRTY);
  1076. mutex_unlock(&dirty_i->seglist_lock);
  1077. reset_curseg(sbi, type, 1);
  1078. curseg->alloc_type = SSR;
  1079. __next_free_blkoff(sbi, curseg, 0);
  1080. if (reuse) {
  1081. sum_page = get_sum_page(sbi, new_segno);
  1082. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  1083. memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
  1084. f2fs_put_page(sum_page, 1);
  1085. }
  1086. }
  1087. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
  1088. {
  1089. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1090. const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
  1091. if (IS_NODESEG(type) || !has_not_enough_free_secs(sbi, 0, 0))
  1092. return v_ops->get_victim(sbi,
  1093. &(curseg)->next_segno, BG_GC, type, SSR);
  1094. /* For data segments, let's do SSR more intensively */
  1095. for (; type >= CURSEG_HOT_DATA; type--)
  1096. if (v_ops->get_victim(sbi, &(curseg)->next_segno,
  1097. BG_GC, type, SSR))
  1098. return 1;
  1099. return 0;
  1100. }
  1101. /*
  1102. * flush out current segment and replace it with new segment
  1103. * This function should be returned with success, otherwise BUG
  1104. */
  1105. static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
  1106. int type, bool force)
  1107. {
  1108. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1109. if (force)
  1110. new_curseg(sbi, type, true);
  1111. else if (type == CURSEG_WARM_NODE)
  1112. new_curseg(sbi, type, false);
  1113. else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
  1114. new_curseg(sbi, type, false);
  1115. else if (need_SSR(sbi) && get_ssr_segment(sbi, type))
  1116. change_curseg(sbi, type, true);
  1117. else
  1118. new_curseg(sbi, type, false);
  1119. stat_inc_seg_type(sbi, curseg);
  1120. }
  1121. static void __allocate_new_segments(struct f2fs_sb_info *sbi, int type)
  1122. {
  1123. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1124. unsigned int old_segno;
  1125. old_segno = curseg->segno;
  1126. SIT_I(sbi)->s_ops->allocate_segment(sbi, type, true);
  1127. locate_dirty_segment(sbi, old_segno);
  1128. }
  1129. void allocate_new_segments(struct f2fs_sb_info *sbi)
  1130. {
  1131. int i;
  1132. if (test_opt(sbi, LFS))
  1133. return;
  1134. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++)
  1135. __allocate_new_segments(sbi, i);
  1136. }
  1137. static const struct segment_allocation default_salloc_ops = {
  1138. .allocate_segment = allocate_segment_by_default,
  1139. };
  1140. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
  1141. {
  1142. __u64 start = F2FS_BYTES_TO_BLK(range->start);
  1143. __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
  1144. unsigned int start_segno, end_segno;
  1145. struct cp_control cpc;
  1146. int err = 0;
  1147. if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
  1148. return -EINVAL;
  1149. cpc.trimmed = 0;
  1150. if (end <= MAIN_BLKADDR(sbi))
  1151. goto out;
  1152. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
  1153. f2fs_msg(sbi->sb, KERN_WARNING,
  1154. "Found FS corruption, run fsck to fix.");
  1155. goto out;
  1156. }
  1157. /* start/end segment number in main_area */
  1158. start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
  1159. end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
  1160. GET_SEGNO(sbi, end);
  1161. cpc.reason = CP_DISCARD;
  1162. cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
  1163. /* do checkpoint to issue discard commands safely */
  1164. for (; start_segno <= end_segno; start_segno = cpc.trim_end + 1) {
  1165. cpc.trim_start = start_segno;
  1166. if (sbi->discard_blks == 0)
  1167. break;
  1168. else if (sbi->discard_blks < BATCHED_TRIM_BLOCKS(sbi))
  1169. cpc.trim_end = end_segno;
  1170. else
  1171. cpc.trim_end = min_t(unsigned int,
  1172. rounddown(start_segno +
  1173. BATCHED_TRIM_SEGMENTS(sbi),
  1174. sbi->segs_per_sec) - 1, end_segno);
  1175. mutex_lock(&sbi->gc_mutex);
  1176. err = write_checkpoint(sbi, &cpc);
  1177. mutex_unlock(&sbi->gc_mutex);
  1178. if (err)
  1179. break;
  1180. schedule();
  1181. }
  1182. out:
  1183. range->len = F2FS_BLK_TO_BYTES(cpc.trimmed);
  1184. return err;
  1185. }
  1186. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  1187. {
  1188. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1189. if (curseg->next_blkoff < sbi->blocks_per_seg)
  1190. return true;
  1191. return false;
  1192. }
  1193. static int __get_segment_type_2(struct page *page, enum page_type p_type)
  1194. {
  1195. if (p_type == DATA)
  1196. return CURSEG_HOT_DATA;
  1197. else
  1198. return CURSEG_HOT_NODE;
  1199. }
  1200. static int __get_segment_type_4(struct page *page, enum page_type p_type)
  1201. {
  1202. if (p_type == DATA) {
  1203. struct inode *inode = page->mapping->host;
  1204. if (S_ISDIR(inode->i_mode))
  1205. return CURSEG_HOT_DATA;
  1206. else
  1207. return CURSEG_COLD_DATA;
  1208. } else {
  1209. if (IS_DNODE(page) && is_cold_node(page))
  1210. return CURSEG_WARM_NODE;
  1211. else
  1212. return CURSEG_COLD_NODE;
  1213. }
  1214. }
  1215. static int __get_segment_type_6(struct page *page, enum page_type p_type)
  1216. {
  1217. if (p_type == DATA) {
  1218. struct inode *inode = page->mapping->host;
  1219. if (S_ISDIR(inode->i_mode))
  1220. return CURSEG_HOT_DATA;
  1221. else if (is_cold_data(page) || file_is_cold(inode))
  1222. return CURSEG_COLD_DATA;
  1223. else
  1224. return CURSEG_WARM_DATA;
  1225. } else {
  1226. if (IS_DNODE(page))
  1227. return is_cold_node(page) ? CURSEG_WARM_NODE :
  1228. CURSEG_HOT_NODE;
  1229. else
  1230. return CURSEG_COLD_NODE;
  1231. }
  1232. }
  1233. static int __get_segment_type(struct page *page, enum page_type p_type)
  1234. {
  1235. switch (F2FS_P_SB(page)->active_logs) {
  1236. case 2:
  1237. return __get_segment_type_2(page, p_type);
  1238. case 4:
  1239. return __get_segment_type_4(page, p_type);
  1240. }
  1241. /* NR_CURSEG_TYPE(6) logs by default */
  1242. f2fs_bug_on(F2FS_P_SB(page),
  1243. F2FS_P_SB(page)->active_logs != NR_CURSEG_TYPE);
  1244. return __get_segment_type_6(page, p_type);
  1245. }
  1246. void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  1247. block_t old_blkaddr, block_t *new_blkaddr,
  1248. struct f2fs_summary *sum, int type)
  1249. {
  1250. struct sit_info *sit_i = SIT_I(sbi);
  1251. struct curseg_info *curseg;
  1252. bool direct_io = (type == CURSEG_DIRECT_IO);
  1253. type = direct_io ? CURSEG_WARM_DATA : type;
  1254. curseg = CURSEG_I(sbi, type);
  1255. mutex_lock(&curseg->curseg_mutex);
  1256. mutex_lock(&sit_i->sentry_lock);
  1257. /* direct_io'ed data is aligned to the segment for better performance */
  1258. if (direct_io && curseg->next_blkoff &&
  1259. !has_not_enough_free_secs(sbi, 0, 0))
  1260. __allocate_new_segments(sbi, type);
  1261. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  1262. /*
  1263. * __add_sum_entry should be resided under the curseg_mutex
  1264. * because, this function updates a summary entry in the
  1265. * current summary block.
  1266. */
  1267. __add_sum_entry(sbi, type, sum);
  1268. __refresh_next_blkoff(sbi, curseg);
  1269. stat_inc_block_count(sbi, curseg);
  1270. if (!__has_curseg_space(sbi, type))
  1271. sit_i->s_ops->allocate_segment(sbi, type, false);
  1272. /*
  1273. * SIT information should be updated before segment allocation,
  1274. * since SSR needs latest valid block information.
  1275. */
  1276. refresh_sit_entry(sbi, old_blkaddr, *new_blkaddr);
  1277. mutex_unlock(&sit_i->sentry_lock);
  1278. if (page && IS_NODESEG(type))
  1279. fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
  1280. mutex_unlock(&curseg->curseg_mutex);
  1281. }
  1282. static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
  1283. {
  1284. int type = __get_segment_type(fio->page, fio->type);
  1285. if (fio->type == NODE || fio->type == DATA)
  1286. mutex_lock(&fio->sbi->wio_mutex[fio->type]);
  1287. allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
  1288. &fio->new_blkaddr, sum, type);
  1289. /* writeout dirty page into bdev */
  1290. f2fs_submit_page_mbio(fio);
  1291. if (fio->type == NODE || fio->type == DATA)
  1292. mutex_unlock(&fio->sbi->wio_mutex[fio->type]);
  1293. }
  1294. void write_meta_page(struct f2fs_sb_info *sbi, struct page *page)
  1295. {
  1296. struct f2fs_io_info fio = {
  1297. .sbi = sbi,
  1298. .type = META,
  1299. .op = REQ_OP_WRITE,
  1300. .op_flags = WRITE_SYNC | REQ_META | REQ_PRIO,
  1301. .old_blkaddr = page->index,
  1302. .new_blkaddr = page->index,
  1303. .page = page,
  1304. .encrypted_page = NULL,
  1305. };
  1306. if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
  1307. fio.op_flags &= ~REQ_META;
  1308. set_page_writeback(page);
  1309. f2fs_submit_page_mbio(&fio);
  1310. }
  1311. void write_node_page(unsigned int nid, struct f2fs_io_info *fio)
  1312. {
  1313. struct f2fs_summary sum;
  1314. set_summary(&sum, nid, 0, 0);
  1315. do_write_page(&sum, fio);
  1316. }
  1317. void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio)
  1318. {
  1319. struct f2fs_sb_info *sbi = fio->sbi;
  1320. struct f2fs_summary sum;
  1321. struct node_info ni;
  1322. f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
  1323. get_node_info(sbi, dn->nid, &ni);
  1324. set_summary(&sum, dn->nid, dn->ofs_in_node, ni.version);
  1325. do_write_page(&sum, fio);
  1326. f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
  1327. }
  1328. void rewrite_data_page(struct f2fs_io_info *fio)
  1329. {
  1330. fio->new_blkaddr = fio->old_blkaddr;
  1331. stat_inc_inplace_blocks(fio->sbi);
  1332. f2fs_submit_page_mbio(fio);
  1333. }
  1334. void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  1335. block_t old_blkaddr, block_t new_blkaddr,
  1336. bool recover_curseg, bool recover_newaddr)
  1337. {
  1338. struct sit_info *sit_i = SIT_I(sbi);
  1339. struct curseg_info *curseg;
  1340. unsigned int segno, old_cursegno;
  1341. struct seg_entry *se;
  1342. int type;
  1343. unsigned short old_blkoff;
  1344. segno = GET_SEGNO(sbi, new_blkaddr);
  1345. se = get_seg_entry(sbi, segno);
  1346. type = se->type;
  1347. if (!recover_curseg) {
  1348. /* for recovery flow */
  1349. if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
  1350. if (old_blkaddr == NULL_ADDR)
  1351. type = CURSEG_COLD_DATA;
  1352. else
  1353. type = CURSEG_WARM_DATA;
  1354. }
  1355. } else {
  1356. if (!IS_CURSEG(sbi, segno))
  1357. type = CURSEG_WARM_DATA;
  1358. }
  1359. curseg = CURSEG_I(sbi, type);
  1360. mutex_lock(&curseg->curseg_mutex);
  1361. mutex_lock(&sit_i->sentry_lock);
  1362. old_cursegno = curseg->segno;
  1363. old_blkoff = curseg->next_blkoff;
  1364. /* change the current segment */
  1365. if (segno != curseg->segno) {
  1366. curseg->next_segno = segno;
  1367. change_curseg(sbi, type, true);
  1368. }
  1369. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  1370. __add_sum_entry(sbi, type, sum);
  1371. if (!recover_curseg || recover_newaddr)
  1372. update_sit_entry(sbi, new_blkaddr, 1);
  1373. if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
  1374. update_sit_entry(sbi, old_blkaddr, -1);
  1375. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  1376. locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
  1377. locate_dirty_segment(sbi, old_cursegno);
  1378. if (recover_curseg) {
  1379. if (old_cursegno != curseg->segno) {
  1380. curseg->next_segno = old_cursegno;
  1381. change_curseg(sbi, type, true);
  1382. }
  1383. curseg->next_blkoff = old_blkoff;
  1384. }
  1385. mutex_unlock(&sit_i->sentry_lock);
  1386. mutex_unlock(&curseg->curseg_mutex);
  1387. }
  1388. void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
  1389. block_t old_addr, block_t new_addr,
  1390. unsigned char version, bool recover_curseg,
  1391. bool recover_newaddr)
  1392. {
  1393. struct f2fs_summary sum;
  1394. set_summary(&sum, dn->nid, dn->ofs_in_node, version);
  1395. __f2fs_replace_block(sbi, &sum, old_addr, new_addr,
  1396. recover_curseg, recover_newaddr);
  1397. f2fs_update_data_blkaddr(dn, new_addr);
  1398. }
  1399. void f2fs_wait_on_page_writeback(struct page *page,
  1400. enum page_type type, bool ordered)
  1401. {
  1402. if (PageWriteback(page)) {
  1403. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  1404. f2fs_submit_merged_bio_cond(sbi, NULL, page, 0, type, WRITE);
  1405. if (ordered)
  1406. wait_on_page_writeback(page);
  1407. else
  1408. wait_for_stable_page(page);
  1409. }
  1410. }
  1411. void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *sbi,
  1412. block_t blkaddr)
  1413. {
  1414. struct page *cpage;
  1415. if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
  1416. return;
  1417. cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
  1418. if (cpage) {
  1419. f2fs_wait_on_page_writeback(cpage, DATA, true);
  1420. f2fs_put_page(cpage, 1);
  1421. }
  1422. }
  1423. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  1424. {
  1425. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1426. struct curseg_info *seg_i;
  1427. unsigned char *kaddr;
  1428. struct page *page;
  1429. block_t start;
  1430. int i, j, offset;
  1431. start = start_sum_block(sbi);
  1432. page = get_meta_page(sbi, start++);
  1433. kaddr = (unsigned char *)page_address(page);
  1434. /* Step 1: restore nat cache */
  1435. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1436. memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
  1437. /* Step 2: restore sit cache */
  1438. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1439. memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
  1440. offset = 2 * SUM_JOURNAL_SIZE;
  1441. /* Step 3: restore summary entries */
  1442. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1443. unsigned short blk_off;
  1444. unsigned int segno;
  1445. seg_i = CURSEG_I(sbi, i);
  1446. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  1447. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  1448. seg_i->next_segno = segno;
  1449. reset_curseg(sbi, i, 0);
  1450. seg_i->alloc_type = ckpt->alloc_type[i];
  1451. seg_i->next_blkoff = blk_off;
  1452. if (seg_i->alloc_type == SSR)
  1453. blk_off = sbi->blocks_per_seg;
  1454. for (j = 0; j < blk_off; j++) {
  1455. struct f2fs_summary *s;
  1456. s = (struct f2fs_summary *)(kaddr + offset);
  1457. seg_i->sum_blk->entries[j] = *s;
  1458. offset += SUMMARY_SIZE;
  1459. if (offset + SUMMARY_SIZE <= PAGE_SIZE -
  1460. SUM_FOOTER_SIZE)
  1461. continue;
  1462. f2fs_put_page(page, 1);
  1463. page = NULL;
  1464. page = get_meta_page(sbi, start++);
  1465. kaddr = (unsigned char *)page_address(page);
  1466. offset = 0;
  1467. }
  1468. }
  1469. f2fs_put_page(page, 1);
  1470. return 0;
  1471. }
  1472. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  1473. {
  1474. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1475. struct f2fs_summary_block *sum;
  1476. struct curseg_info *curseg;
  1477. struct page *new;
  1478. unsigned short blk_off;
  1479. unsigned int segno = 0;
  1480. block_t blk_addr = 0;
  1481. /* get segment number and block addr */
  1482. if (IS_DATASEG(type)) {
  1483. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  1484. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  1485. CURSEG_HOT_DATA]);
  1486. if (__exist_node_summaries(sbi))
  1487. blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
  1488. else
  1489. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  1490. } else {
  1491. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  1492. CURSEG_HOT_NODE]);
  1493. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  1494. CURSEG_HOT_NODE]);
  1495. if (__exist_node_summaries(sbi))
  1496. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  1497. type - CURSEG_HOT_NODE);
  1498. else
  1499. blk_addr = GET_SUM_BLOCK(sbi, segno);
  1500. }
  1501. new = get_meta_page(sbi, blk_addr);
  1502. sum = (struct f2fs_summary_block *)page_address(new);
  1503. if (IS_NODESEG(type)) {
  1504. if (__exist_node_summaries(sbi)) {
  1505. struct f2fs_summary *ns = &sum->entries[0];
  1506. int i;
  1507. for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
  1508. ns->version = 0;
  1509. ns->ofs_in_node = 0;
  1510. }
  1511. } else {
  1512. int err;
  1513. err = restore_node_summary(sbi, segno, sum);
  1514. if (err) {
  1515. f2fs_put_page(new, 1);
  1516. return err;
  1517. }
  1518. }
  1519. }
  1520. /* set uncompleted segment to curseg */
  1521. curseg = CURSEG_I(sbi, type);
  1522. mutex_lock(&curseg->curseg_mutex);
  1523. /* update journal info */
  1524. down_write(&curseg->journal_rwsem);
  1525. memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
  1526. up_write(&curseg->journal_rwsem);
  1527. memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
  1528. memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
  1529. curseg->next_segno = segno;
  1530. reset_curseg(sbi, type, 0);
  1531. curseg->alloc_type = ckpt->alloc_type[type];
  1532. curseg->next_blkoff = blk_off;
  1533. mutex_unlock(&curseg->curseg_mutex);
  1534. f2fs_put_page(new, 1);
  1535. return 0;
  1536. }
  1537. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  1538. {
  1539. int type = CURSEG_HOT_DATA;
  1540. int err;
  1541. if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
  1542. int npages = npages_for_summary_flush(sbi, true);
  1543. if (npages >= 2)
  1544. ra_meta_pages(sbi, start_sum_block(sbi), npages,
  1545. META_CP, true);
  1546. /* restore for compacted data summary */
  1547. if (read_compacted_summaries(sbi))
  1548. return -EINVAL;
  1549. type = CURSEG_HOT_NODE;
  1550. }
  1551. if (__exist_node_summaries(sbi))
  1552. ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
  1553. NR_CURSEG_TYPE - type, META_CP, true);
  1554. for (; type <= CURSEG_COLD_NODE; type++) {
  1555. err = read_normal_summaries(sbi, type);
  1556. if (err)
  1557. return err;
  1558. }
  1559. return 0;
  1560. }
  1561. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  1562. {
  1563. struct page *page;
  1564. unsigned char *kaddr;
  1565. struct f2fs_summary *summary;
  1566. struct curseg_info *seg_i;
  1567. int written_size = 0;
  1568. int i, j;
  1569. page = grab_meta_page(sbi, blkaddr++);
  1570. kaddr = (unsigned char *)page_address(page);
  1571. /* Step 1: write nat cache */
  1572. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  1573. memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
  1574. written_size += SUM_JOURNAL_SIZE;
  1575. /* Step 2: write sit cache */
  1576. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1577. memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
  1578. written_size += SUM_JOURNAL_SIZE;
  1579. /* Step 3: write summary entries */
  1580. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1581. unsigned short blkoff;
  1582. seg_i = CURSEG_I(sbi, i);
  1583. if (sbi->ckpt->alloc_type[i] == SSR)
  1584. blkoff = sbi->blocks_per_seg;
  1585. else
  1586. blkoff = curseg_blkoff(sbi, i);
  1587. for (j = 0; j < blkoff; j++) {
  1588. if (!page) {
  1589. page = grab_meta_page(sbi, blkaddr++);
  1590. kaddr = (unsigned char *)page_address(page);
  1591. written_size = 0;
  1592. }
  1593. summary = (struct f2fs_summary *)(kaddr + written_size);
  1594. *summary = seg_i->sum_blk->entries[j];
  1595. written_size += SUMMARY_SIZE;
  1596. if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
  1597. SUM_FOOTER_SIZE)
  1598. continue;
  1599. set_page_dirty(page);
  1600. f2fs_put_page(page, 1);
  1601. page = NULL;
  1602. }
  1603. }
  1604. if (page) {
  1605. set_page_dirty(page);
  1606. f2fs_put_page(page, 1);
  1607. }
  1608. }
  1609. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  1610. block_t blkaddr, int type)
  1611. {
  1612. int i, end;
  1613. if (IS_DATASEG(type))
  1614. end = type + NR_CURSEG_DATA_TYPE;
  1615. else
  1616. end = type + NR_CURSEG_NODE_TYPE;
  1617. for (i = type; i < end; i++)
  1618. write_current_sum_page(sbi, i, blkaddr + (i - type));
  1619. }
  1620. void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1621. {
  1622. if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
  1623. write_compacted_summaries(sbi, start_blk);
  1624. else
  1625. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  1626. }
  1627. void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  1628. {
  1629. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  1630. }
  1631. int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
  1632. unsigned int val, int alloc)
  1633. {
  1634. int i;
  1635. if (type == NAT_JOURNAL) {
  1636. for (i = 0; i < nats_in_cursum(journal); i++) {
  1637. if (le32_to_cpu(nid_in_journal(journal, i)) == val)
  1638. return i;
  1639. }
  1640. if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
  1641. return update_nats_in_cursum(journal, 1);
  1642. } else if (type == SIT_JOURNAL) {
  1643. for (i = 0; i < sits_in_cursum(journal); i++)
  1644. if (le32_to_cpu(segno_in_journal(journal, i)) == val)
  1645. return i;
  1646. if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
  1647. return update_sits_in_cursum(journal, 1);
  1648. }
  1649. return -1;
  1650. }
  1651. static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
  1652. unsigned int segno)
  1653. {
  1654. return get_meta_page(sbi, current_sit_addr(sbi, segno));
  1655. }
  1656. static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
  1657. unsigned int start)
  1658. {
  1659. struct sit_info *sit_i = SIT_I(sbi);
  1660. struct page *src_page, *dst_page;
  1661. pgoff_t src_off, dst_off;
  1662. void *src_addr, *dst_addr;
  1663. src_off = current_sit_addr(sbi, start);
  1664. dst_off = next_sit_addr(sbi, src_off);
  1665. /* get current sit block page without lock */
  1666. src_page = get_meta_page(sbi, src_off);
  1667. dst_page = grab_meta_page(sbi, dst_off);
  1668. f2fs_bug_on(sbi, PageDirty(src_page));
  1669. src_addr = page_address(src_page);
  1670. dst_addr = page_address(dst_page);
  1671. memcpy(dst_addr, src_addr, PAGE_SIZE);
  1672. set_page_dirty(dst_page);
  1673. f2fs_put_page(src_page, 1);
  1674. set_to_next_sit(sit_i, start);
  1675. return dst_page;
  1676. }
  1677. static struct sit_entry_set *grab_sit_entry_set(void)
  1678. {
  1679. struct sit_entry_set *ses =
  1680. f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
  1681. ses->entry_cnt = 0;
  1682. INIT_LIST_HEAD(&ses->set_list);
  1683. return ses;
  1684. }
  1685. static void release_sit_entry_set(struct sit_entry_set *ses)
  1686. {
  1687. list_del(&ses->set_list);
  1688. kmem_cache_free(sit_entry_set_slab, ses);
  1689. }
  1690. static void adjust_sit_entry_set(struct sit_entry_set *ses,
  1691. struct list_head *head)
  1692. {
  1693. struct sit_entry_set *next = ses;
  1694. if (list_is_last(&ses->set_list, head))
  1695. return;
  1696. list_for_each_entry_continue(next, head, set_list)
  1697. if (ses->entry_cnt <= next->entry_cnt)
  1698. break;
  1699. list_move_tail(&ses->set_list, &next->set_list);
  1700. }
  1701. static void add_sit_entry(unsigned int segno, struct list_head *head)
  1702. {
  1703. struct sit_entry_set *ses;
  1704. unsigned int start_segno = START_SEGNO(segno);
  1705. list_for_each_entry(ses, head, set_list) {
  1706. if (ses->start_segno == start_segno) {
  1707. ses->entry_cnt++;
  1708. adjust_sit_entry_set(ses, head);
  1709. return;
  1710. }
  1711. }
  1712. ses = grab_sit_entry_set();
  1713. ses->start_segno = start_segno;
  1714. ses->entry_cnt++;
  1715. list_add(&ses->set_list, head);
  1716. }
  1717. static void add_sits_in_set(struct f2fs_sb_info *sbi)
  1718. {
  1719. struct f2fs_sm_info *sm_info = SM_I(sbi);
  1720. struct list_head *set_list = &sm_info->sit_entry_set;
  1721. unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
  1722. unsigned int segno;
  1723. for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
  1724. add_sit_entry(segno, set_list);
  1725. }
  1726. static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
  1727. {
  1728. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1729. struct f2fs_journal *journal = curseg->journal;
  1730. int i;
  1731. down_write(&curseg->journal_rwsem);
  1732. for (i = 0; i < sits_in_cursum(journal); i++) {
  1733. unsigned int segno;
  1734. bool dirtied;
  1735. segno = le32_to_cpu(segno_in_journal(journal, i));
  1736. dirtied = __mark_sit_entry_dirty(sbi, segno);
  1737. if (!dirtied)
  1738. add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
  1739. }
  1740. update_sits_in_cursum(journal, -i);
  1741. up_write(&curseg->journal_rwsem);
  1742. }
  1743. /*
  1744. * CP calls this function, which flushes SIT entries including sit_journal,
  1745. * and moves prefree segs to free segs.
  1746. */
  1747. void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  1748. {
  1749. struct sit_info *sit_i = SIT_I(sbi);
  1750. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  1751. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1752. struct f2fs_journal *journal = curseg->journal;
  1753. struct sit_entry_set *ses, *tmp;
  1754. struct list_head *head = &SM_I(sbi)->sit_entry_set;
  1755. bool to_journal = true;
  1756. struct seg_entry *se;
  1757. mutex_lock(&sit_i->sentry_lock);
  1758. if (!sit_i->dirty_sentries)
  1759. goto out;
  1760. /*
  1761. * add and account sit entries of dirty bitmap in sit entry
  1762. * set temporarily
  1763. */
  1764. add_sits_in_set(sbi);
  1765. /*
  1766. * if there are no enough space in journal to store dirty sit
  1767. * entries, remove all entries from journal and add and account
  1768. * them in sit entry set.
  1769. */
  1770. if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
  1771. remove_sits_in_journal(sbi);
  1772. /*
  1773. * there are two steps to flush sit entries:
  1774. * #1, flush sit entries to journal in current cold data summary block.
  1775. * #2, flush sit entries to sit page.
  1776. */
  1777. list_for_each_entry_safe(ses, tmp, head, set_list) {
  1778. struct page *page = NULL;
  1779. struct f2fs_sit_block *raw_sit = NULL;
  1780. unsigned int start_segno = ses->start_segno;
  1781. unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
  1782. (unsigned long)MAIN_SEGS(sbi));
  1783. unsigned int segno = start_segno;
  1784. if (to_journal &&
  1785. !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
  1786. to_journal = false;
  1787. if (to_journal) {
  1788. down_write(&curseg->journal_rwsem);
  1789. } else {
  1790. page = get_next_sit_page(sbi, start_segno);
  1791. raw_sit = page_address(page);
  1792. }
  1793. /* flush dirty sit entries in region of current sit set */
  1794. for_each_set_bit_from(segno, bitmap, end) {
  1795. int offset, sit_offset;
  1796. se = get_seg_entry(sbi, segno);
  1797. /* add discard candidates */
  1798. if (cpc->reason != CP_DISCARD) {
  1799. cpc->trim_start = segno;
  1800. add_discard_addrs(sbi, cpc);
  1801. }
  1802. if (to_journal) {
  1803. offset = lookup_journal_in_cursum(journal,
  1804. SIT_JOURNAL, segno, 1);
  1805. f2fs_bug_on(sbi, offset < 0);
  1806. segno_in_journal(journal, offset) =
  1807. cpu_to_le32(segno);
  1808. seg_info_to_raw_sit(se,
  1809. &sit_in_journal(journal, offset));
  1810. } else {
  1811. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  1812. seg_info_to_raw_sit(se,
  1813. &raw_sit->entries[sit_offset]);
  1814. }
  1815. __clear_bit(segno, bitmap);
  1816. sit_i->dirty_sentries--;
  1817. ses->entry_cnt--;
  1818. }
  1819. if (to_journal)
  1820. up_write(&curseg->journal_rwsem);
  1821. else
  1822. f2fs_put_page(page, 1);
  1823. f2fs_bug_on(sbi, ses->entry_cnt);
  1824. release_sit_entry_set(ses);
  1825. }
  1826. f2fs_bug_on(sbi, !list_empty(head));
  1827. f2fs_bug_on(sbi, sit_i->dirty_sentries);
  1828. out:
  1829. if (cpc->reason == CP_DISCARD) {
  1830. for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
  1831. add_discard_addrs(sbi, cpc);
  1832. }
  1833. mutex_unlock(&sit_i->sentry_lock);
  1834. set_prefree_as_free_segments(sbi);
  1835. }
  1836. static int build_sit_info(struct f2fs_sb_info *sbi)
  1837. {
  1838. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  1839. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1840. struct sit_info *sit_i;
  1841. unsigned int sit_segs, start;
  1842. char *src_bitmap, *dst_bitmap;
  1843. unsigned int bitmap_size;
  1844. /* allocate memory for SIT information */
  1845. sit_i = kzalloc(sizeof(struct sit_info), GFP_KERNEL);
  1846. if (!sit_i)
  1847. return -ENOMEM;
  1848. SM_I(sbi)->sit_info = sit_i;
  1849. sit_i->sentries = f2fs_kvzalloc(MAIN_SEGS(sbi) *
  1850. sizeof(struct seg_entry), GFP_KERNEL);
  1851. if (!sit_i->sentries)
  1852. return -ENOMEM;
  1853. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  1854. sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
  1855. if (!sit_i->dirty_sentries_bitmap)
  1856. return -ENOMEM;
  1857. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  1858. sit_i->sentries[start].cur_valid_map
  1859. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1860. sit_i->sentries[start].ckpt_valid_map
  1861. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1862. if (!sit_i->sentries[start].cur_valid_map ||
  1863. !sit_i->sentries[start].ckpt_valid_map)
  1864. return -ENOMEM;
  1865. if (f2fs_discard_en(sbi)) {
  1866. sit_i->sentries[start].discard_map
  1867. = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1868. if (!sit_i->sentries[start].discard_map)
  1869. return -ENOMEM;
  1870. }
  1871. }
  1872. sit_i->tmp_map = kzalloc(SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  1873. if (!sit_i->tmp_map)
  1874. return -ENOMEM;
  1875. if (sbi->segs_per_sec > 1) {
  1876. sit_i->sec_entries = f2fs_kvzalloc(MAIN_SECS(sbi) *
  1877. sizeof(struct sec_entry), GFP_KERNEL);
  1878. if (!sit_i->sec_entries)
  1879. return -ENOMEM;
  1880. }
  1881. /* get information related with SIT */
  1882. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  1883. /* setup SIT bitmap from ckeckpoint pack */
  1884. bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  1885. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  1886. dst_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  1887. if (!dst_bitmap)
  1888. return -ENOMEM;
  1889. /* init SIT information */
  1890. sit_i->s_ops = &default_salloc_ops;
  1891. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  1892. sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
  1893. sit_i->written_valid_blocks = le64_to_cpu(ckpt->valid_block_count);
  1894. sit_i->sit_bitmap = dst_bitmap;
  1895. sit_i->bitmap_size = bitmap_size;
  1896. sit_i->dirty_sentries = 0;
  1897. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  1898. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  1899. sit_i->mounted_time = CURRENT_TIME_SEC.tv_sec;
  1900. mutex_init(&sit_i->sentry_lock);
  1901. return 0;
  1902. }
  1903. static int build_free_segmap(struct f2fs_sb_info *sbi)
  1904. {
  1905. struct free_segmap_info *free_i;
  1906. unsigned int bitmap_size, sec_bitmap_size;
  1907. /* allocate memory for free segmap information */
  1908. free_i = kzalloc(sizeof(struct free_segmap_info), GFP_KERNEL);
  1909. if (!free_i)
  1910. return -ENOMEM;
  1911. SM_I(sbi)->free_info = free_i;
  1912. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  1913. free_i->free_segmap = f2fs_kvmalloc(bitmap_size, GFP_KERNEL);
  1914. if (!free_i->free_segmap)
  1915. return -ENOMEM;
  1916. sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  1917. free_i->free_secmap = f2fs_kvmalloc(sec_bitmap_size, GFP_KERNEL);
  1918. if (!free_i->free_secmap)
  1919. return -ENOMEM;
  1920. /* set all segments as dirty temporarily */
  1921. memset(free_i->free_segmap, 0xff, bitmap_size);
  1922. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  1923. /* init free segmap information */
  1924. free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
  1925. free_i->free_segments = 0;
  1926. free_i->free_sections = 0;
  1927. spin_lock_init(&free_i->segmap_lock);
  1928. return 0;
  1929. }
  1930. static int build_curseg(struct f2fs_sb_info *sbi)
  1931. {
  1932. struct curseg_info *array;
  1933. int i;
  1934. array = kcalloc(NR_CURSEG_TYPE, sizeof(*array), GFP_KERNEL);
  1935. if (!array)
  1936. return -ENOMEM;
  1937. SM_I(sbi)->curseg_array = array;
  1938. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  1939. mutex_init(&array[i].curseg_mutex);
  1940. array[i].sum_blk = kzalloc(PAGE_SIZE, GFP_KERNEL);
  1941. if (!array[i].sum_blk)
  1942. return -ENOMEM;
  1943. init_rwsem(&array[i].journal_rwsem);
  1944. array[i].journal = kzalloc(sizeof(struct f2fs_journal),
  1945. GFP_KERNEL);
  1946. if (!array[i].journal)
  1947. return -ENOMEM;
  1948. array[i].segno = NULL_SEGNO;
  1949. array[i].next_blkoff = 0;
  1950. }
  1951. return restore_curseg_summaries(sbi);
  1952. }
  1953. static void build_sit_entries(struct f2fs_sb_info *sbi)
  1954. {
  1955. struct sit_info *sit_i = SIT_I(sbi);
  1956. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  1957. struct f2fs_journal *journal = curseg->journal;
  1958. struct seg_entry *se;
  1959. struct f2fs_sit_entry sit;
  1960. int sit_blk_cnt = SIT_BLK_CNT(sbi);
  1961. unsigned int i, start, end;
  1962. unsigned int readed, start_blk = 0;
  1963. do {
  1964. readed = ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
  1965. META_SIT, true);
  1966. start = start_blk * sit_i->sents_per_block;
  1967. end = (start_blk + readed) * sit_i->sents_per_block;
  1968. for (; start < end && start < MAIN_SEGS(sbi); start++) {
  1969. struct f2fs_sit_block *sit_blk;
  1970. struct page *page;
  1971. se = &sit_i->sentries[start];
  1972. page = get_current_sit_page(sbi, start);
  1973. sit_blk = (struct f2fs_sit_block *)page_address(page);
  1974. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  1975. f2fs_put_page(page, 1);
  1976. check_block_count(sbi, start, &sit);
  1977. seg_info_from_raw_sit(se, &sit);
  1978. /* build discard map only one time */
  1979. if (f2fs_discard_en(sbi)) {
  1980. memcpy(se->discard_map, se->cur_valid_map,
  1981. SIT_VBLOCK_MAP_SIZE);
  1982. sbi->discard_blks += sbi->blocks_per_seg -
  1983. se->valid_blocks;
  1984. }
  1985. if (sbi->segs_per_sec > 1)
  1986. get_sec_entry(sbi, start)->valid_blocks +=
  1987. se->valid_blocks;
  1988. }
  1989. start_blk += readed;
  1990. } while (start_blk < sit_blk_cnt);
  1991. down_read(&curseg->journal_rwsem);
  1992. for (i = 0; i < sits_in_cursum(journal); i++) {
  1993. unsigned int old_valid_blocks;
  1994. start = le32_to_cpu(segno_in_journal(journal, i));
  1995. se = &sit_i->sentries[start];
  1996. sit = sit_in_journal(journal, i);
  1997. old_valid_blocks = se->valid_blocks;
  1998. check_block_count(sbi, start, &sit);
  1999. seg_info_from_raw_sit(se, &sit);
  2000. if (f2fs_discard_en(sbi)) {
  2001. memcpy(se->discard_map, se->cur_valid_map,
  2002. SIT_VBLOCK_MAP_SIZE);
  2003. sbi->discard_blks += old_valid_blocks -
  2004. se->valid_blocks;
  2005. }
  2006. if (sbi->segs_per_sec > 1)
  2007. get_sec_entry(sbi, start)->valid_blocks +=
  2008. se->valid_blocks - old_valid_blocks;
  2009. }
  2010. up_read(&curseg->journal_rwsem);
  2011. }
  2012. static void init_free_segmap(struct f2fs_sb_info *sbi)
  2013. {
  2014. unsigned int start;
  2015. int type;
  2016. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  2017. struct seg_entry *sentry = get_seg_entry(sbi, start);
  2018. if (!sentry->valid_blocks)
  2019. __set_free(sbi, start);
  2020. }
  2021. /* set use the current segments */
  2022. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  2023. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  2024. __set_test_and_inuse(sbi, curseg_t->segno);
  2025. }
  2026. }
  2027. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  2028. {
  2029. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2030. struct free_segmap_info *free_i = FREE_I(sbi);
  2031. unsigned int segno = 0, offset = 0;
  2032. unsigned short valid_blocks;
  2033. while (1) {
  2034. /* find dirty segment based on free segmap */
  2035. segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
  2036. if (segno >= MAIN_SEGS(sbi))
  2037. break;
  2038. offset = segno + 1;
  2039. valid_blocks = get_valid_blocks(sbi, segno, 0);
  2040. if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
  2041. continue;
  2042. if (valid_blocks > sbi->blocks_per_seg) {
  2043. f2fs_bug_on(sbi, 1);
  2044. continue;
  2045. }
  2046. mutex_lock(&dirty_i->seglist_lock);
  2047. __locate_dirty_segment(sbi, segno, DIRTY);
  2048. mutex_unlock(&dirty_i->seglist_lock);
  2049. }
  2050. }
  2051. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  2052. {
  2053. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2054. unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  2055. dirty_i->victim_secmap = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
  2056. if (!dirty_i->victim_secmap)
  2057. return -ENOMEM;
  2058. return 0;
  2059. }
  2060. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  2061. {
  2062. struct dirty_seglist_info *dirty_i;
  2063. unsigned int bitmap_size, i;
  2064. /* allocate memory for dirty segments list information */
  2065. dirty_i = kzalloc(sizeof(struct dirty_seglist_info), GFP_KERNEL);
  2066. if (!dirty_i)
  2067. return -ENOMEM;
  2068. SM_I(sbi)->dirty_info = dirty_i;
  2069. mutex_init(&dirty_i->seglist_lock);
  2070. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  2071. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  2072. dirty_i->dirty_segmap[i] = f2fs_kvzalloc(bitmap_size, GFP_KERNEL);
  2073. if (!dirty_i->dirty_segmap[i])
  2074. return -ENOMEM;
  2075. }
  2076. init_dirty_segmap(sbi);
  2077. return init_victim_secmap(sbi);
  2078. }
  2079. /*
  2080. * Update min, max modified time for cost-benefit GC algorithm
  2081. */
  2082. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  2083. {
  2084. struct sit_info *sit_i = SIT_I(sbi);
  2085. unsigned int segno;
  2086. mutex_lock(&sit_i->sentry_lock);
  2087. sit_i->min_mtime = LLONG_MAX;
  2088. for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
  2089. unsigned int i;
  2090. unsigned long long mtime = 0;
  2091. for (i = 0; i < sbi->segs_per_sec; i++)
  2092. mtime += get_seg_entry(sbi, segno + i)->mtime;
  2093. mtime = div_u64(mtime, sbi->segs_per_sec);
  2094. if (sit_i->min_mtime > mtime)
  2095. sit_i->min_mtime = mtime;
  2096. }
  2097. sit_i->max_mtime = get_mtime(sbi);
  2098. mutex_unlock(&sit_i->sentry_lock);
  2099. }
  2100. int build_segment_manager(struct f2fs_sb_info *sbi)
  2101. {
  2102. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  2103. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2104. struct f2fs_sm_info *sm_info;
  2105. int err;
  2106. sm_info = kzalloc(sizeof(struct f2fs_sm_info), GFP_KERNEL);
  2107. if (!sm_info)
  2108. return -ENOMEM;
  2109. /* init sm info */
  2110. sbi->sm_info = sm_info;
  2111. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  2112. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  2113. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  2114. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  2115. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  2116. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  2117. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  2118. sm_info->rec_prefree_segments = sm_info->main_segments *
  2119. DEF_RECLAIM_PREFREE_SEGMENTS / 100;
  2120. if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
  2121. sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
  2122. if (!test_opt(sbi, LFS))
  2123. sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
  2124. sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
  2125. sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
  2126. INIT_LIST_HEAD(&sm_info->discard_list);
  2127. INIT_LIST_HEAD(&sm_info->wait_list);
  2128. sm_info->nr_discards = 0;
  2129. sm_info->max_discards = 0;
  2130. sm_info->trim_sections = DEF_BATCHED_TRIM_SECTIONS;
  2131. INIT_LIST_HEAD(&sm_info->sit_entry_set);
  2132. if (test_opt(sbi, FLUSH_MERGE) && !f2fs_readonly(sbi->sb)) {
  2133. err = create_flush_cmd_control(sbi);
  2134. if (err)
  2135. return err;
  2136. }
  2137. err = build_sit_info(sbi);
  2138. if (err)
  2139. return err;
  2140. err = build_free_segmap(sbi);
  2141. if (err)
  2142. return err;
  2143. err = build_curseg(sbi);
  2144. if (err)
  2145. return err;
  2146. /* reinit free segmap based on SIT */
  2147. build_sit_entries(sbi);
  2148. init_free_segmap(sbi);
  2149. err = build_dirty_segmap(sbi);
  2150. if (err)
  2151. return err;
  2152. init_min_max_mtime(sbi);
  2153. return 0;
  2154. }
  2155. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  2156. enum dirty_type dirty_type)
  2157. {
  2158. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2159. mutex_lock(&dirty_i->seglist_lock);
  2160. kvfree(dirty_i->dirty_segmap[dirty_type]);
  2161. dirty_i->nr_dirty[dirty_type] = 0;
  2162. mutex_unlock(&dirty_i->seglist_lock);
  2163. }
  2164. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  2165. {
  2166. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2167. kvfree(dirty_i->victim_secmap);
  2168. }
  2169. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  2170. {
  2171. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2172. int i;
  2173. if (!dirty_i)
  2174. return;
  2175. /* discard pre-free/dirty segments list */
  2176. for (i = 0; i < NR_DIRTY_TYPE; i++)
  2177. discard_dirty_segmap(sbi, i);
  2178. destroy_victim_secmap(sbi);
  2179. SM_I(sbi)->dirty_info = NULL;
  2180. kfree(dirty_i);
  2181. }
  2182. static void destroy_curseg(struct f2fs_sb_info *sbi)
  2183. {
  2184. struct curseg_info *array = SM_I(sbi)->curseg_array;
  2185. int i;
  2186. if (!array)
  2187. return;
  2188. SM_I(sbi)->curseg_array = NULL;
  2189. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  2190. kfree(array[i].sum_blk);
  2191. kfree(array[i].journal);
  2192. }
  2193. kfree(array);
  2194. }
  2195. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  2196. {
  2197. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  2198. if (!free_i)
  2199. return;
  2200. SM_I(sbi)->free_info = NULL;
  2201. kvfree(free_i->free_segmap);
  2202. kvfree(free_i->free_secmap);
  2203. kfree(free_i);
  2204. }
  2205. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  2206. {
  2207. struct sit_info *sit_i = SIT_I(sbi);
  2208. unsigned int start;
  2209. if (!sit_i)
  2210. return;
  2211. if (sit_i->sentries) {
  2212. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  2213. kfree(sit_i->sentries[start].cur_valid_map);
  2214. kfree(sit_i->sentries[start].ckpt_valid_map);
  2215. kfree(sit_i->sentries[start].discard_map);
  2216. }
  2217. }
  2218. kfree(sit_i->tmp_map);
  2219. kvfree(sit_i->sentries);
  2220. kvfree(sit_i->sec_entries);
  2221. kvfree(sit_i->dirty_sentries_bitmap);
  2222. SM_I(sbi)->sit_info = NULL;
  2223. kfree(sit_i->sit_bitmap);
  2224. kfree(sit_i);
  2225. }
  2226. void destroy_segment_manager(struct f2fs_sb_info *sbi)
  2227. {
  2228. struct f2fs_sm_info *sm_info = SM_I(sbi);
  2229. if (!sm_info)
  2230. return;
  2231. destroy_flush_cmd_control(sbi);
  2232. destroy_dirty_segmap(sbi);
  2233. destroy_curseg(sbi);
  2234. destroy_free_segmap(sbi);
  2235. destroy_sit_info(sbi);
  2236. sbi->sm_info = NULL;
  2237. kfree(sm_info);
  2238. }
  2239. int __init create_segment_manager_caches(void)
  2240. {
  2241. discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
  2242. sizeof(struct discard_entry));
  2243. if (!discard_entry_slab)
  2244. goto fail;
  2245. bio_entry_slab = f2fs_kmem_cache_create("bio_entry",
  2246. sizeof(struct bio_entry));
  2247. if (!bio_entry_slab)
  2248. goto destroy_discard_entry;
  2249. sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
  2250. sizeof(struct sit_entry_set));
  2251. if (!sit_entry_set_slab)
  2252. goto destroy_bio_entry;
  2253. inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
  2254. sizeof(struct inmem_pages));
  2255. if (!inmem_entry_slab)
  2256. goto destroy_sit_entry_set;
  2257. return 0;
  2258. destroy_sit_entry_set:
  2259. kmem_cache_destroy(sit_entry_set_slab);
  2260. destroy_bio_entry:
  2261. kmem_cache_destroy(bio_entry_slab);
  2262. destroy_discard_entry:
  2263. kmem_cache_destroy(discard_entry_slab);
  2264. fail:
  2265. return -ENOMEM;
  2266. }
  2267. void destroy_segment_manager_caches(void)
  2268. {
  2269. kmem_cache_destroy(sit_entry_set_slab);
  2270. kmem_cache_destroy(bio_entry_slab);
  2271. kmem_cache_destroy(discard_entry_slab);
  2272. kmem_cache_destroy(inmem_entry_slab);
  2273. }