segment.c 72 KB

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