segment.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395
  1. /*
  2. * fs/f2fs/segment.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/fs.h>
  12. #include <linux/f2fs_fs.h>
  13. #include <linux/bio.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/prefetch.h>
  16. #include <linux/kthread.h>
  17. #include <linux/swap.h>
  18. #include <linux/timer.h>
  19. #include <linux/freezer.h>
  20. #include <linux/sched/signal.h>
  21. #include "f2fs.h"
  22. #include "segment.h"
  23. #include "node.h"
  24. #include "gc.h"
  25. #include "trace.h"
  26. #include <trace/events/f2fs.h>
  27. #define __reverse_ffz(x) __reverse_ffs(~(x))
  28. static struct kmem_cache *discard_entry_slab;
  29. static struct kmem_cache *discard_cmd_slab;
  30. static struct kmem_cache *sit_entry_set_slab;
  31. static struct kmem_cache *inmem_entry_slab;
  32. static unsigned long __reverse_ulong(unsigned char *str)
  33. {
  34. unsigned long tmp = 0;
  35. int shift = 24, idx = 0;
  36. #if BITS_PER_LONG == 64
  37. shift = 56;
  38. #endif
  39. while (shift >= 0) {
  40. tmp |= (unsigned long)str[idx++] << shift;
  41. shift -= BITS_PER_BYTE;
  42. }
  43. return tmp;
  44. }
  45. /*
  46. * __reverse_ffs is copied from include/asm-generic/bitops/__ffs.h since
  47. * MSB and LSB are reversed in a byte by f2fs_set_bit.
  48. */
  49. static inline unsigned long __reverse_ffs(unsigned long word)
  50. {
  51. int num = 0;
  52. #if BITS_PER_LONG == 64
  53. if ((word & 0xffffffff00000000UL) == 0)
  54. num += 32;
  55. else
  56. word >>= 32;
  57. #endif
  58. if ((word & 0xffff0000) == 0)
  59. num += 16;
  60. else
  61. word >>= 16;
  62. if ((word & 0xff00) == 0)
  63. num += 8;
  64. else
  65. word >>= 8;
  66. if ((word & 0xf0) == 0)
  67. num += 4;
  68. else
  69. word >>= 4;
  70. if ((word & 0xc) == 0)
  71. num += 2;
  72. else
  73. word >>= 2;
  74. if ((word & 0x2) == 0)
  75. num += 1;
  76. return num;
  77. }
  78. /*
  79. * __find_rev_next(_zero)_bit is copied from lib/find_next_bit.c because
  80. * f2fs_set_bit makes MSB and LSB reversed in a byte.
  81. * @size must be integral times of unsigned long.
  82. * Example:
  83. * MSB <--> LSB
  84. * f2fs_set_bit(0, bitmap) => 1000 0000
  85. * f2fs_set_bit(7, bitmap) => 0000 0001
  86. */
  87. static unsigned long __find_rev_next_bit(const unsigned long *addr,
  88. unsigned long size, unsigned long offset)
  89. {
  90. const unsigned long *p = addr + BIT_WORD(offset);
  91. unsigned long result = size;
  92. unsigned long tmp;
  93. if (offset >= size)
  94. return size;
  95. size -= (offset & ~(BITS_PER_LONG - 1));
  96. offset %= BITS_PER_LONG;
  97. while (1) {
  98. if (*p == 0)
  99. goto pass;
  100. tmp = __reverse_ulong((unsigned char *)p);
  101. tmp &= ~0UL >> offset;
  102. if (size < BITS_PER_LONG)
  103. tmp &= (~0UL << (BITS_PER_LONG - size));
  104. if (tmp)
  105. goto found;
  106. pass:
  107. if (size <= BITS_PER_LONG)
  108. break;
  109. size -= BITS_PER_LONG;
  110. offset = 0;
  111. p++;
  112. }
  113. return result;
  114. found:
  115. return result - size + __reverse_ffs(tmp);
  116. }
  117. static unsigned long __find_rev_next_zero_bit(const unsigned long *addr,
  118. unsigned long size, unsigned long offset)
  119. {
  120. const unsigned long *p = addr + BIT_WORD(offset);
  121. unsigned long result = size;
  122. unsigned long tmp;
  123. if (offset >= size)
  124. return size;
  125. size -= (offset & ~(BITS_PER_LONG - 1));
  126. offset %= BITS_PER_LONG;
  127. while (1) {
  128. if (*p == ~0UL)
  129. goto pass;
  130. tmp = __reverse_ulong((unsigned char *)p);
  131. if (offset)
  132. tmp |= ~0UL << (BITS_PER_LONG - offset);
  133. if (size < BITS_PER_LONG)
  134. tmp |= ~0UL >> size;
  135. if (tmp != ~0UL)
  136. goto found;
  137. pass:
  138. if (size <= BITS_PER_LONG)
  139. break;
  140. size -= BITS_PER_LONG;
  141. offset = 0;
  142. p++;
  143. }
  144. return result;
  145. found:
  146. return result - size + __reverse_ffz(tmp);
  147. }
  148. bool f2fs_need_SSR(struct f2fs_sb_info *sbi)
  149. {
  150. int node_secs = get_blocktype_secs(sbi, F2FS_DIRTY_NODES);
  151. int dent_secs = get_blocktype_secs(sbi, F2FS_DIRTY_DENTS);
  152. int imeta_secs = get_blocktype_secs(sbi, F2FS_DIRTY_IMETA);
  153. if (test_opt(sbi, LFS))
  154. return false;
  155. if (sbi->gc_mode == GC_URGENT)
  156. return true;
  157. return free_sections(sbi) <= (node_secs + 2 * dent_secs + imeta_secs +
  158. SM_I(sbi)->min_ssr_sections + reserved_sections(sbi));
  159. }
  160. void f2fs_register_inmem_page(struct inode *inode, struct page *page)
  161. {
  162. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  163. struct f2fs_inode_info *fi = F2FS_I(inode);
  164. struct inmem_pages *new;
  165. f2fs_trace_pid(page);
  166. set_page_private(page, (unsigned long)ATOMIC_WRITTEN_PAGE);
  167. SetPagePrivate(page);
  168. new = f2fs_kmem_cache_alloc(inmem_entry_slab, GFP_NOFS);
  169. /* add atomic page indices to the list */
  170. new->page = page;
  171. INIT_LIST_HEAD(&new->list);
  172. /* increase reference count with clean state */
  173. mutex_lock(&fi->inmem_lock);
  174. get_page(page);
  175. list_add_tail(&new->list, &fi->inmem_pages);
  176. spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
  177. if (list_empty(&fi->inmem_ilist))
  178. list_add_tail(&fi->inmem_ilist, &sbi->inode_list[ATOMIC_FILE]);
  179. spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
  180. inc_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  181. mutex_unlock(&fi->inmem_lock);
  182. trace_f2fs_register_inmem_page(page, INMEM);
  183. }
  184. static int __revoke_inmem_pages(struct inode *inode,
  185. struct list_head *head, bool drop, bool recover,
  186. bool trylock)
  187. {
  188. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  189. struct inmem_pages *cur, *tmp;
  190. int err = 0;
  191. list_for_each_entry_safe(cur, tmp, head, list) {
  192. struct page *page = cur->page;
  193. if (drop)
  194. trace_f2fs_commit_inmem_page(page, INMEM_DROP);
  195. if (trylock) {
  196. /*
  197. * to avoid deadlock in between page lock and
  198. * inmem_lock.
  199. */
  200. if (!trylock_page(page))
  201. continue;
  202. } else {
  203. lock_page(page);
  204. }
  205. f2fs_wait_on_page_writeback(page, DATA, true);
  206. if (recover) {
  207. struct dnode_of_data dn;
  208. struct node_info ni;
  209. trace_f2fs_commit_inmem_page(page, INMEM_REVOKE);
  210. retry:
  211. set_new_dnode(&dn, inode, NULL, NULL, 0);
  212. err = f2fs_get_dnode_of_data(&dn, page->index,
  213. LOOKUP_NODE);
  214. if (err) {
  215. if (err == -ENOMEM) {
  216. congestion_wait(BLK_RW_ASYNC, HZ/50);
  217. cond_resched();
  218. goto retry;
  219. }
  220. err = -EAGAIN;
  221. goto next;
  222. }
  223. err = f2fs_get_node_info(sbi, dn.nid, &ni);
  224. if (err) {
  225. f2fs_put_dnode(&dn);
  226. return err;
  227. }
  228. if (cur->old_addr == NEW_ADDR) {
  229. f2fs_invalidate_blocks(sbi, dn.data_blkaddr);
  230. f2fs_update_data_blkaddr(&dn, NEW_ADDR);
  231. } else
  232. f2fs_replace_block(sbi, &dn, dn.data_blkaddr,
  233. cur->old_addr, ni.version, true, true);
  234. f2fs_put_dnode(&dn);
  235. }
  236. next:
  237. /* we don't need to invalidate this in the sccessful status */
  238. if (drop || recover) {
  239. ClearPageUptodate(page);
  240. clear_cold_data(page);
  241. }
  242. set_page_private(page, 0);
  243. ClearPagePrivate(page);
  244. f2fs_put_page(page, 1);
  245. list_del(&cur->list);
  246. kmem_cache_free(inmem_entry_slab, cur);
  247. dec_page_count(F2FS_I_SB(inode), F2FS_INMEM_PAGES);
  248. }
  249. return err;
  250. }
  251. void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure)
  252. {
  253. struct list_head *head = &sbi->inode_list[ATOMIC_FILE];
  254. struct inode *inode;
  255. struct f2fs_inode_info *fi;
  256. next:
  257. spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
  258. if (list_empty(head)) {
  259. spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
  260. return;
  261. }
  262. fi = list_first_entry(head, struct f2fs_inode_info, inmem_ilist);
  263. inode = igrab(&fi->vfs_inode);
  264. spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
  265. if (inode) {
  266. if (gc_failure) {
  267. if (fi->i_gc_failures[GC_FAILURE_ATOMIC])
  268. goto drop;
  269. goto skip;
  270. }
  271. drop:
  272. set_inode_flag(inode, FI_ATOMIC_REVOKE_REQUEST);
  273. f2fs_drop_inmem_pages(inode);
  274. iput(inode);
  275. }
  276. skip:
  277. congestion_wait(BLK_RW_ASYNC, HZ/50);
  278. cond_resched();
  279. goto next;
  280. }
  281. void f2fs_drop_inmem_pages(struct inode *inode)
  282. {
  283. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  284. struct f2fs_inode_info *fi = F2FS_I(inode);
  285. while (!list_empty(&fi->inmem_pages)) {
  286. mutex_lock(&fi->inmem_lock);
  287. __revoke_inmem_pages(inode, &fi->inmem_pages,
  288. true, false, true);
  289. if (list_empty(&fi->inmem_pages)) {
  290. spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
  291. if (!list_empty(&fi->inmem_ilist))
  292. list_del_init(&fi->inmem_ilist);
  293. spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
  294. }
  295. mutex_unlock(&fi->inmem_lock);
  296. }
  297. clear_inode_flag(inode, FI_ATOMIC_FILE);
  298. fi->i_gc_failures[GC_FAILURE_ATOMIC] = 0;
  299. stat_dec_atomic_write(inode);
  300. }
  301. void f2fs_drop_inmem_page(struct inode *inode, struct page *page)
  302. {
  303. struct f2fs_inode_info *fi = F2FS_I(inode);
  304. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  305. struct list_head *head = &fi->inmem_pages;
  306. struct inmem_pages *cur = NULL;
  307. f2fs_bug_on(sbi, !IS_ATOMIC_WRITTEN_PAGE(page));
  308. mutex_lock(&fi->inmem_lock);
  309. list_for_each_entry(cur, head, list) {
  310. if (cur->page == page)
  311. break;
  312. }
  313. f2fs_bug_on(sbi, list_empty(head) || cur->page != page);
  314. list_del(&cur->list);
  315. mutex_unlock(&fi->inmem_lock);
  316. dec_page_count(sbi, F2FS_INMEM_PAGES);
  317. kmem_cache_free(inmem_entry_slab, cur);
  318. ClearPageUptodate(page);
  319. set_page_private(page, 0);
  320. ClearPagePrivate(page);
  321. f2fs_put_page(page, 0);
  322. trace_f2fs_commit_inmem_page(page, INMEM_INVALIDATE);
  323. }
  324. static int __f2fs_commit_inmem_pages(struct inode *inode)
  325. {
  326. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  327. struct f2fs_inode_info *fi = F2FS_I(inode);
  328. struct inmem_pages *cur, *tmp;
  329. struct f2fs_io_info fio = {
  330. .sbi = sbi,
  331. .ino = inode->i_ino,
  332. .type = DATA,
  333. .op = REQ_OP_WRITE,
  334. .op_flags = REQ_SYNC | REQ_PRIO,
  335. .io_type = FS_DATA_IO,
  336. };
  337. struct list_head revoke_list;
  338. pgoff_t last_idx = ULONG_MAX;
  339. int err = 0;
  340. INIT_LIST_HEAD(&revoke_list);
  341. list_for_each_entry_safe(cur, tmp, &fi->inmem_pages, list) {
  342. struct page *page = cur->page;
  343. lock_page(page);
  344. if (page->mapping == inode->i_mapping) {
  345. trace_f2fs_commit_inmem_page(page, INMEM);
  346. set_page_dirty(page);
  347. f2fs_wait_on_page_writeback(page, DATA, true);
  348. if (clear_page_dirty_for_io(page)) {
  349. inode_dec_dirty_pages(inode);
  350. f2fs_remove_dirty_inode(inode);
  351. }
  352. retry:
  353. fio.page = page;
  354. fio.old_blkaddr = NULL_ADDR;
  355. fio.encrypted_page = NULL;
  356. fio.need_lock = LOCK_DONE;
  357. err = f2fs_do_write_data_page(&fio);
  358. if (err) {
  359. if (err == -ENOMEM) {
  360. congestion_wait(BLK_RW_ASYNC, HZ/50);
  361. cond_resched();
  362. goto retry;
  363. }
  364. unlock_page(page);
  365. break;
  366. }
  367. /* record old blkaddr for revoking */
  368. cur->old_addr = fio.old_blkaddr;
  369. last_idx = page->index;
  370. }
  371. unlock_page(page);
  372. list_move_tail(&cur->list, &revoke_list);
  373. }
  374. if (last_idx != ULONG_MAX)
  375. f2fs_submit_merged_write_cond(sbi, inode, 0, last_idx, DATA);
  376. if (err) {
  377. /*
  378. * try to revoke all committed pages, but still we could fail
  379. * due to no memory or other reason, if that happened, EAGAIN
  380. * will be returned, which means in such case, transaction is
  381. * already not integrity, caller should use journal to do the
  382. * recovery or rewrite & commit last transaction. For other
  383. * error number, revoking was done by filesystem itself.
  384. */
  385. err = __revoke_inmem_pages(inode, &revoke_list,
  386. false, true, false);
  387. /* drop all uncommitted pages */
  388. __revoke_inmem_pages(inode, &fi->inmem_pages,
  389. true, false, false);
  390. } else {
  391. __revoke_inmem_pages(inode, &revoke_list,
  392. false, false, false);
  393. }
  394. return err;
  395. }
  396. int f2fs_commit_inmem_pages(struct inode *inode)
  397. {
  398. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  399. struct f2fs_inode_info *fi = F2FS_I(inode);
  400. int err;
  401. f2fs_balance_fs(sbi, true);
  402. down_write(&fi->i_gc_rwsem[WRITE]);
  403. f2fs_lock_op(sbi);
  404. set_inode_flag(inode, FI_ATOMIC_COMMIT);
  405. mutex_lock(&fi->inmem_lock);
  406. err = __f2fs_commit_inmem_pages(inode);
  407. spin_lock(&sbi->inode_lock[ATOMIC_FILE]);
  408. if (!list_empty(&fi->inmem_ilist))
  409. list_del_init(&fi->inmem_ilist);
  410. spin_unlock(&sbi->inode_lock[ATOMIC_FILE]);
  411. mutex_unlock(&fi->inmem_lock);
  412. clear_inode_flag(inode, FI_ATOMIC_COMMIT);
  413. f2fs_unlock_op(sbi);
  414. up_write(&fi->i_gc_rwsem[WRITE]);
  415. return err;
  416. }
  417. /*
  418. * This function balances dirty node and dentry pages.
  419. * In addition, it controls garbage collection.
  420. */
  421. void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need)
  422. {
  423. if (time_to_inject(sbi, FAULT_CHECKPOINT)) {
  424. f2fs_show_injection_info(FAULT_CHECKPOINT);
  425. f2fs_stop_checkpoint(sbi, false);
  426. }
  427. /* balance_fs_bg is able to be pending */
  428. if (need && excess_cached_nats(sbi))
  429. f2fs_balance_fs_bg(sbi);
  430. /*
  431. * We should do GC or end up with checkpoint, if there are so many dirty
  432. * dir/node pages without enough free segments.
  433. */
  434. if (has_not_enough_free_secs(sbi, 0, 0)) {
  435. mutex_lock(&sbi->gc_mutex);
  436. f2fs_gc(sbi, false, false, NULL_SEGNO);
  437. }
  438. }
  439. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi)
  440. {
  441. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  442. return;
  443. /* try to shrink extent cache when there is no enough memory */
  444. if (!f2fs_available_free_memory(sbi, EXTENT_CACHE))
  445. f2fs_shrink_extent_tree(sbi, EXTENT_CACHE_SHRINK_NUMBER);
  446. /* check the # of cached NAT entries */
  447. if (!f2fs_available_free_memory(sbi, NAT_ENTRIES))
  448. f2fs_try_to_free_nats(sbi, NAT_ENTRY_PER_BLOCK);
  449. if (!f2fs_available_free_memory(sbi, FREE_NIDS))
  450. f2fs_try_to_free_nids(sbi, MAX_FREE_NIDS);
  451. else
  452. f2fs_build_free_nids(sbi, false, false);
  453. if (!is_idle(sbi) &&
  454. (!excess_dirty_nats(sbi) && !excess_dirty_nodes(sbi)))
  455. return;
  456. /* checkpoint is the only way to shrink partial cached entries */
  457. if (!f2fs_available_free_memory(sbi, NAT_ENTRIES) ||
  458. !f2fs_available_free_memory(sbi, INO_ENTRIES) ||
  459. excess_prefree_segs(sbi) ||
  460. excess_dirty_nats(sbi) ||
  461. excess_dirty_nodes(sbi) ||
  462. f2fs_time_over(sbi, CP_TIME)) {
  463. if (test_opt(sbi, DATA_FLUSH)) {
  464. struct blk_plug plug;
  465. blk_start_plug(&plug);
  466. f2fs_sync_dirty_inodes(sbi, FILE_INODE);
  467. blk_finish_plug(&plug);
  468. }
  469. f2fs_sync_fs(sbi->sb, true);
  470. stat_inc_bg_cp_count(sbi->stat_info);
  471. }
  472. }
  473. static int __submit_flush_wait(struct f2fs_sb_info *sbi,
  474. struct block_device *bdev)
  475. {
  476. struct bio *bio = f2fs_bio_alloc(sbi, 0, true);
  477. int ret;
  478. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
  479. bio_set_dev(bio, bdev);
  480. ret = submit_bio_wait(bio);
  481. bio_put(bio);
  482. trace_f2fs_issue_flush(bdev, test_opt(sbi, NOBARRIER),
  483. test_opt(sbi, FLUSH_MERGE), ret);
  484. return ret;
  485. }
  486. static int submit_flush_wait(struct f2fs_sb_info *sbi, nid_t ino)
  487. {
  488. int ret = 0;
  489. int i;
  490. if (!f2fs_is_multi_device(sbi))
  491. return __submit_flush_wait(sbi, sbi->sb->s_bdev);
  492. for (i = 0; i < sbi->s_ndevs; i++) {
  493. if (!f2fs_is_dirty_device(sbi, ino, i, FLUSH_INO))
  494. continue;
  495. ret = __submit_flush_wait(sbi, FDEV(i).bdev);
  496. if (ret)
  497. break;
  498. }
  499. return ret;
  500. }
  501. static int issue_flush_thread(void *data)
  502. {
  503. struct f2fs_sb_info *sbi = data;
  504. struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
  505. wait_queue_head_t *q = &fcc->flush_wait_queue;
  506. repeat:
  507. if (kthread_should_stop())
  508. return 0;
  509. sb_start_intwrite(sbi->sb);
  510. if (!llist_empty(&fcc->issue_list)) {
  511. struct flush_cmd *cmd, *next;
  512. int ret;
  513. fcc->dispatch_list = llist_del_all(&fcc->issue_list);
  514. fcc->dispatch_list = llist_reverse_order(fcc->dispatch_list);
  515. cmd = llist_entry(fcc->dispatch_list, struct flush_cmd, llnode);
  516. ret = submit_flush_wait(sbi, cmd->ino);
  517. atomic_inc(&fcc->issued_flush);
  518. llist_for_each_entry_safe(cmd, next,
  519. fcc->dispatch_list, llnode) {
  520. cmd->ret = ret;
  521. complete(&cmd->wait);
  522. }
  523. fcc->dispatch_list = NULL;
  524. }
  525. sb_end_intwrite(sbi->sb);
  526. wait_event_interruptible(*q,
  527. kthread_should_stop() || !llist_empty(&fcc->issue_list));
  528. goto repeat;
  529. }
  530. int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino)
  531. {
  532. struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
  533. struct flush_cmd cmd;
  534. int ret;
  535. if (test_opt(sbi, NOBARRIER))
  536. return 0;
  537. if (!test_opt(sbi, FLUSH_MERGE)) {
  538. atomic_inc(&fcc->issing_flush);
  539. ret = submit_flush_wait(sbi, ino);
  540. atomic_dec(&fcc->issing_flush);
  541. atomic_inc(&fcc->issued_flush);
  542. return ret;
  543. }
  544. if (atomic_inc_return(&fcc->issing_flush) == 1 ||
  545. f2fs_is_multi_device(sbi)) {
  546. ret = submit_flush_wait(sbi, ino);
  547. atomic_dec(&fcc->issing_flush);
  548. atomic_inc(&fcc->issued_flush);
  549. return ret;
  550. }
  551. cmd.ino = ino;
  552. init_completion(&cmd.wait);
  553. llist_add(&cmd.llnode, &fcc->issue_list);
  554. /* update issue_list before we wake up issue_flush thread */
  555. smp_mb();
  556. if (waitqueue_active(&fcc->flush_wait_queue))
  557. wake_up(&fcc->flush_wait_queue);
  558. if (fcc->f2fs_issue_flush) {
  559. wait_for_completion(&cmd.wait);
  560. atomic_dec(&fcc->issing_flush);
  561. } else {
  562. struct llist_node *list;
  563. list = llist_del_all(&fcc->issue_list);
  564. if (!list) {
  565. wait_for_completion(&cmd.wait);
  566. atomic_dec(&fcc->issing_flush);
  567. } else {
  568. struct flush_cmd *tmp, *next;
  569. ret = submit_flush_wait(sbi, ino);
  570. llist_for_each_entry_safe(tmp, next, list, llnode) {
  571. if (tmp == &cmd) {
  572. cmd.ret = ret;
  573. atomic_dec(&fcc->issing_flush);
  574. continue;
  575. }
  576. tmp->ret = ret;
  577. complete(&tmp->wait);
  578. }
  579. }
  580. }
  581. return cmd.ret;
  582. }
  583. int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi)
  584. {
  585. dev_t dev = sbi->sb->s_bdev->bd_dev;
  586. struct flush_cmd_control *fcc;
  587. int err = 0;
  588. if (SM_I(sbi)->fcc_info) {
  589. fcc = SM_I(sbi)->fcc_info;
  590. if (fcc->f2fs_issue_flush)
  591. return err;
  592. goto init_thread;
  593. }
  594. fcc = f2fs_kzalloc(sbi, sizeof(struct flush_cmd_control), GFP_KERNEL);
  595. if (!fcc)
  596. return -ENOMEM;
  597. atomic_set(&fcc->issued_flush, 0);
  598. atomic_set(&fcc->issing_flush, 0);
  599. init_waitqueue_head(&fcc->flush_wait_queue);
  600. init_llist_head(&fcc->issue_list);
  601. SM_I(sbi)->fcc_info = fcc;
  602. if (!test_opt(sbi, FLUSH_MERGE))
  603. return err;
  604. init_thread:
  605. fcc->f2fs_issue_flush = kthread_run(issue_flush_thread, sbi,
  606. "f2fs_flush-%u:%u", MAJOR(dev), MINOR(dev));
  607. if (IS_ERR(fcc->f2fs_issue_flush)) {
  608. err = PTR_ERR(fcc->f2fs_issue_flush);
  609. kfree(fcc);
  610. SM_I(sbi)->fcc_info = NULL;
  611. return err;
  612. }
  613. return err;
  614. }
  615. void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free)
  616. {
  617. struct flush_cmd_control *fcc = SM_I(sbi)->fcc_info;
  618. if (fcc && fcc->f2fs_issue_flush) {
  619. struct task_struct *flush_thread = fcc->f2fs_issue_flush;
  620. fcc->f2fs_issue_flush = NULL;
  621. kthread_stop(flush_thread);
  622. }
  623. if (free) {
  624. kfree(fcc);
  625. SM_I(sbi)->fcc_info = NULL;
  626. }
  627. }
  628. int f2fs_flush_device_cache(struct f2fs_sb_info *sbi)
  629. {
  630. int ret = 0, i;
  631. if (!f2fs_is_multi_device(sbi))
  632. return 0;
  633. for (i = 1; i < sbi->s_ndevs; i++) {
  634. if (!f2fs_test_bit(i, (char *)&sbi->dirty_device))
  635. continue;
  636. ret = __submit_flush_wait(sbi, FDEV(i).bdev);
  637. if (ret)
  638. break;
  639. spin_lock(&sbi->dev_lock);
  640. f2fs_clear_bit(i, (char *)&sbi->dirty_device);
  641. spin_unlock(&sbi->dev_lock);
  642. }
  643. return ret;
  644. }
  645. static void __locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  646. enum dirty_type dirty_type)
  647. {
  648. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  649. /* need not be added */
  650. if (IS_CURSEG(sbi, segno))
  651. return;
  652. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  653. dirty_i->nr_dirty[dirty_type]++;
  654. if (dirty_type == DIRTY) {
  655. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  656. enum dirty_type t = sentry->type;
  657. if (unlikely(t >= DIRTY)) {
  658. f2fs_bug_on(sbi, 1);
  659. return;
  660. }
  661. if (!test_and_set_bit(segno, dirty_i->dirty_segmap[t]))
  662. dirty_i->nr_dirty[t]++;
  663. }
  664. }
  665. static void __remove_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno,
  666. enum dirty_type dirty_type)
  667. {
  668. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  669. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[dirty_type]))
  670. dirty_i->nr_dirty[dirty_type]--;
  671. if (dirty_type == DIRTY) {
  672. struct seg_entry *sentry = get_seg_entry(sbi, segno);
  673. enum dirty_type t = sentry->type;
  674. if (test_and_clear_bit(segno, dirty_i->dirty_segmap[t]))
  675. dirty_i->nr_dirty[t]--;
  676. if (get_valid_blocks(sbi, segno, true) == 0)
  677. clear_bit(GET_SEC_FROM_SEG(sbi, segno),
  678. dirty_i->victim_secmap);
  679. }
  680. }
  681. /*
  682. * Should not occur error such as -ENOMEM.
  683. * Adding dirty entry into seglist is not critical operation.
  684. * If a given segment is one of current working segments, it won't be added.
  685. */
  686. static void locate_dirty_segment(struct f2fs_sb_info *sbi, unsigned int segno)
  687. {
  688. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  689. unsigned short valid_blocks;
  690. if (segno == NULL_SEGNO || IS_CURSEG(sbi, segno))
  691. return;
  692. mutex_lock(&dirty_i->seglist_lock);
  693. valid_blocks = get_valid_blocks(sbi, segno, false);
  694. if (valid_blocks == 0) {
  695. __locate_dirty_segment(sbi, segno, PRE);
  696. __remove_dirty_segment(sbi, segno, DIRTY);
  697. } else if (valid_blocks < sbi->blocks_per_seg) {
  698. __locate_dirty_segment(sbi, segno, DIRTY);
  699. } else {
  700. /* Recovery routine with SSR needs this */
  701. __remove_dirty_segment(sbi, segno, DIRTY);
  702. }
  703. mutex_unlock(&dirty_i->seglist_lock);
  704. }
  705. static struct discard_cmd *__create_discard_cmd(struct f2fs_sb_info *sbi,
  706. struct block_device *bdev, block_t lstart,
  707. block_t start, block_t len)
  708. {
  709. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  710. struct list_head *pend_list;
  711. struct discard_cmd *dc;
  712. f2fs_bug_on(sbi, !len);
  713. pend_list = &dcc->pend_list[plist_idx(len)];
  714. dc = f2fs_kmem_cache_alloc(discard_cmd_slab, GFP_NOFS);
  715. INIT_LIST_HEAD(&dc->list);
  716. dc->bdev = bdev;
  717. dc->lstart = lstart;
  718. dc->start = start;
  719. dc->len = len;
  720. dc->ref = 0;
  721. dc->state = D_PREP;
  722. dc->issuing = 0;
  723. dc->error = 0;
  724. init_completion(&dc->wait);
  725. list_add_tail(&dc->list, pend_list);
  726. spin_lock_init(&dc->lock);
  727. dc->bio_ref = 0;
  728. atomic_inc(&dcc->discard_cmd_cnt);
  729. dcc->undiscard_blks += len;
  730. return dc;
  731. }
  732. static struct discard_cmd *__attach_discard_cmd(struct f2fs_sb_info *sbi,
  733. struct block_device *bdev, block_t lstart,
  734. block_t start, block_t len,
  735. struct rb_node *parent, struct rb_node **p)
  736. {
  737. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  738. struct discard_cmd *dc;
  739. dc = __create_discard_cmd(sbi, bdev, lstart, start, len);
  740. rb_link_node(&dc->rb_node, parent, p);
  741. rb_insert_color(&dc->rb_node, &dcc->root);
  742. return dc;
  743. }
  744. static void __detach_discard_cmd(struct discard_cmd_control *dcc,
  745. struct discard_cmd *dc)
  746. {
  747. if (dc->state == D_DONE)
  748. atomic_sub(dc->issuing, &dcc->issing_discard);
  749. list_del(&dc->list);
  750. rb_erase(&dc->rb_node, &dcc->root);
  751. dcc->undiscard_blks -= dc->len;
  752. kmem_cache_free(discard_cmd_slab, dc);
  753. atomic_dec(&dcc->discard_cmd_cnt);
  754. }
  755. static void __remove_discard_cmd(struct f2fs_sb_info *sbi,
  756. struct discard_cmd *dc)
  757. {
  758. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  759. unsigned long flags;
  760. trace_f2fs_remove_discard(dc->bdev, dc->start, dc->len);
  761. spin_lock_irqsave(&dc->lock, flags);
  762. if (dc->bio_ref) {
  763. spin_unlock_irqrestore(&dc->lock, flags);
  764. return;
  765. }
  766. spin_unlock_irqrestore(&dc->lock, flags);
  767. f2fs_bug_on(sbi, dc->ref);
  768. if (dc->error == -EOPNOTSUPP)
  769. dc->error = 0;
  770. if (dc->error)
  771. f2fs_msg(sbi->sb, KERN_INFO,
  772. "Issue discard(%u, %u, %u) failed, ret: %d",
  773. dc->lstart, dc->start, dc->len, dc->error);
  774. __detach_discard_cmd(dcc, dc);
  775. }
  776. static void f2fs_submit_discard_endio(struct bio *bio)
  777. {
  778. struct discard_cmd *dc = (struct discard_cmd *)bio->bi_private;
  779. unsigned long flags;
  780. dc->error = blk_status_to_errno(bio->bi_status);
  781. spin_lock_irqsave(&dc->lock, flags);
  782. dc->bio_ref--;
  783. if (!dc->bio_ref && dc->state == D_SUBMIT) {
  784. dc->state = D_DONE;
  785. complete_all(&dc->wait);
  786. }
  787. spin_unlock_irqrestore(&dc->lock, flags);
  788. bio_put(bio);
  789. }
  790. static void __check_sit_bitmap(struct f2fs_sb_info *sbi,
  791. block_t start, block_t end)
  792. {
  793. #ifdef CONFIG_F2FS_CHECK_FS
  794. struct seg_entry *sentry;
  795. unsigned int segno;
  796. block_t blk = start;
  797. unsigned long offset, size, max_blocks = sbi->blocks_per_seg;
  798. unsigned long *map;
  799. while (blk < end) {
  800. segno = GET_SEGNO(sbi, blk);
  801. sentry = get_seg_entry(sbi, segno);
  802. offset = GET_BLKOFF_FROM_SEG0(sbi, blk);
  803. if (end < START_BLOCK(sbi, segno + 1))
  804. size = GET_BLKOFF_FROM_SEG0(sbi, end);
  805. else
  806. size = max_blocks;
  807. map = (unsigned long *)(sentry->cur_valid_map);
  808. offset = __find_rev_next_bit(map, size, offset);
  809. f2fs_bug_on(sbi, offset != size);
  810. blk = START_BLOCK(sbi, segno + 1);
  811. }
  812. #endif
  813. }
  814. static void __init_discard_policy(struct f2fs_sb_info *sbi,
  815. struct discard_policy *dpolicy,
  816. int discard_type, unsigned int granularity)
  817. {
  818. /* common policy */
  819. dpolicy->type = discard_type;
  820. dpolicy->sync = true;
  821. dpolicy->ordered = false;
  822. dpolicy->granularity = granularity;
  823. dpolicy->max_requests = DEF_MAX_DISCARD_REQUEST;
  824. dpolicy->io_aware_gran = MAX_PLIST_NUM;
  825. if (discard_type == DPOLICY_BG) {
  826. dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
  827. dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
  828. dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
  829. dpolicy->io_aware = true;
  830. dpolicy->sync = false;
  831. dpolicy->ordered = true;
  832. if (utilization(sbi) > DEF_DISCARD_URGENT_UTIL) {
  833. dpolicy->granularity = 1;
  834. dpolicy->max_interval = DEF_MIN_DISCARD_ISSUE_TIME;
  835. }
  836. } else if (discard_type == DPOLICY_FORCE) {
  837. dpolicy->min_interval = DEF_MIN_DISCARD_ISSUE_TIME;
  838. dpolicy->mid_interval = DEF_MID_DISCARD_ISSUE_TIME;
  839. dpolicy->max_interval = DEF_MAX_DISCARD_ISSUE_TIME;
  840. dpolicy->io_aware = false;
  841. } else if (discard_type == DPOLICY_FSTRIM) {
  842. dpolicy->io_aware = false;
  843. } else if (discard_type == DPOLICY_UMOUNT) {
  844. dpolicy->max_requests = UINT_MAX;
  845. dpolicy->io_aware = false;
  846. }
  847. }
  848. static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
  849. struct block_device *bdev, block_t lstart,
  850. block_t start, block_t len);
  851. /* this function is copied from blkdev_issue_discard from block/blk-lib.c */
  852. static int __submit_discard_cmd(struct f2fs_sb_info *sbi,
  853. struct discard_policy *dpolicy,
  854. struct discard_cmd *dc,
  855. unsigned int *issued)
  856. {
  857. struct block_device *bdev = dc->bdev;
  858. struct request_queue *q = bdev_get_queue(bdev);
  859. unsigned int max_discard_blocks =
  860. SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
  861. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  862. struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
  863. &(dcc->fstrim_list) : &(dcc->wait_list);
  864. int flag = dpolicy->sync ? REQ_SYNC : 0;
  865. block_t lstart, start, len, total_len;
  866. int err = 0;
  867. if (dc->state != D_PREP)
  868. return 0;
  869. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK))
  870. return 0;
  871. trace_f2fs_issue_discard(bdev, dc->start, dc->len);
  872. lstart = dc->lstart;
  873. start = dc->start;
  874. len = dc->len;
  875. total_len = len;
  876. dc->len = 0;
  877. while (total_len && *issued < dpolicy->max_requests && !err) {
  878. struct bio *bio = NULL;
  879. unsigned long flags;
  880. bool last = true;
  881. if (len > max_discard_blocks) {
  882. len = max_discard_blocks;
  883. last = false;
  884. }
  885. (*issued)++;
  886. if (*issued == dpolicy->max_requests)
  887. last = true;
  888. dc->len += len;
  889. if (time_to_inject(sbi, FAULT_DISCARD)) {
  890. f2fs_show_injection_info(FAULT_DISCARD);
  891. err = -EIO;
  892. goto submit;
  893. }
  894. err = __blkdev_issue_discard(bdev,
  895. SECTOR_FROM_BLOCK(start),
  896. SECTOR_FROM_BLOCK(len),
  897. GFP_NOFS, 0, &bio);
  898. submit:
  899. if (err) {
  900. spin_lock_irqsave(&dc->lock, flags);
  901. if (dc->state == D_PARTIAL)
  902. dc->state = D_SUBMIT;
  903. spin_unlock_irqrestore(&dc->lock, flags);
  904. break;
  905. }
  906. f2fs_bug_on(sbi, !bio);
  907. /*
  908. * should keep before submission to avoid D_DONE
  909. * right away
  910. */
  911. spin_lock_irqsave(&dc->lock, flags);
  912. if (last)
  913. dc->state = D_SUBMIT;
  914. else
  915. dc->state = D_PARTIAL;
  916. dc->bio_ref++;
  917. spin_unlock_irqrestore(&dc->lock, flags);
  918. atomic_inc(&dcc->issing_discard);
  919. dc->issuing++;
  920. list_move_tail(&dc->list, wait_list);
  921. /* sanity check on discard range */
  922. __check_sit_bitmap(sbi, lstart, lstart + len);
  923. bio->bi_private = dc;
  924. bio->bi_end_io = f2fs_submit_discard_endio;
  925. bio->bi_opf |= flag;
  926. submit_bio(bio);
  927. atomic_inc(&dcc->issued_discard);
  928. f2fs_update_iostat(sbi, FS_DISCARD, 1);
  929. lstart += len;
  930. start += len;
  931. total_len -= len;
  932. len = total_len;
  933. }
  934. if (!err && len)
  935. __update_discard_tree_range(sbi, bdev, lstart, start, len);
  936. return err;
  937. }
  938. static struct discard_cmd *__insert_discard_tree(struct f2fs_sb_info *sbi,
  939. struct block_device *bdev, block_t lstart,
  940. block_t start, block_t len,
  941. struct rb_node **insert_p,
  942. struct rb_node *insert_parent)
  943. {
  944. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  945. struct rb_node **p;
  946. struct rb_node *parent = NULL;
  947. struct discard_cmd *dc = NULL;
  948. if (insert_p && insert_parent) {
  949. parent = insert_parent;
  950. p = insert_p;
  951. goto do_insert;
  952. }
  953. p = f2fs_lookup_rb_tree_for_insert(sbi, &dcc->root, &parent, lstart);
  954. do_insert:
  955. dc = __attach_discard_cmd(sbi, bdev, lstart, start, len, parent, p);
  956. if (!dc)
  957. return NULL;
  958. return dc;
  959. }
  960. static void __relocate_discard_cmd(struct discard_cmd_control *dcc,
  961. struct discard_cmd *dc)
  962. {
  963. list_move_tail(&dc->list, &dcc->pend_list[plist_idx(dc->len)]);
  964. }
  965. static void __punch_discard_cmd(struct f2fs_sb_info *sbi,
  966. struct discard_cmd *dc, block_t blkaddr)
  967. {
  968. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  969. struct discard_info di = dc->di;
  970. bool modified = false;
  971. if (dc->state == D_DONE || dc->len == 1) {
  972. __remove_discard_cmd(sbi, dc);
  973. return;
  974. }
  975. dcc->undiscard_blks -= di.len;
  976. if (blkaddr > di.lstart) {
  977. dc->len = blkaddr - dc->lstart;
  978. dcc->undiscard_blks += dc->len;
  979. __relocate_discard_cmd(dcc, dc);
  980. modified = true;
  981. }
  982. if (blkaddr < di.lstart + di.len - 1) {
  983. if (modified) {
  984. __insert_discard_tree(sbi, dc->bdev, blkaddr + 1,
  985. di.start + blkaddr + 1 - di.lstart,
  986. di.lstart + di.len - 1 - blkaddr,
  987. NULL, NULL);
  988. } else {
  989. dc->lstart++;
  990. dc->len--;
  991. dc->start++;
  992. dcc->undiscard_blks += dc->len;
  993. __relocate_discard_cmd(dcc, dc);
  994. }
  995. }
  996. }
  997. static void __update_discard_tree_range(struct f2fs_sb_info *sbi,
  998. struct block_device *bdev, block_t lstart,
  999. block_t start, block_t len)
  1000. {
  1001. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1002. struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
  1003. struct discard_cmd *dc;
  1004. struct discard_info di = {0};
  1005. struct rb_node **insert_p = NULL, *insert_parent = NULL;
  1006. struct request_queue *q = bdev_get_queue(bdev);
  1007. unsigned int max_discard_blocks =
  1008. SECTOR_TO_BLOCK(q->limits.max_discard_sectors);
  1009. block_t end = lstart + len;
  1010. dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
  1011. NULL, lstart,
  1012. (struct rb_entry **)&prev_dc,
  1013. (struct rb_entry **)&next_dc,
  1014. &insert_p, &insert_parent, true);
  1015. if (dc)
  1016. prev_dc = dc;
  1017. if (!prev_dc) {
  1018. di.lstart = lstart;
  1019. di.len = next_dc ? next_dc->lstart - lstart : len;
  1020. di.len = min(di.len, len);
  1021. di.start = start;
  1022. }
  1023. while (1) {
  1024. struct rb_node *node;
  1025. bool merged = false;
  1026. struct discard_cmd *tdc = NULL;
  1027. if (prev_dc) {
  1028. di.lstart = prev_dc->lstart + prev_dc->len;
  1029. if (di.lstart < lstart)
  1030. di.lstart = lstart;
  1031. if (di.lstart >= end)
  1032. break;
  1033. if (!next_dc || next_dc->lstart > end)
  1034. di.len = end - di.lstart;
  1035. else
  1036. di.len = next_dc->lstart - di.lstart;
  1037. di.start = start + di.lstart - lstart;
  1038. }
  1039. if (!di.len)
  1040. goto next;
  1041. if (prev_dc && prev_dc->state == D_PREP &&
  1042. prev_dc->bdev == bdev &&
  1043. __is_discard_back_mergeable(&di, &prev_dc->di,
  1044. max_discard_blocks)) {
  1045. prev_dc->di.len += di.len;
  1046. dcc->undiscard_blks += di.len;
  1047. __relocate_discard_cmd(dcc, prev_dc);
  1048. di = prev_dc->di;
  1049. tdc = prev_dc;
  1050. merged = true;
  1051. }
  1052. if (next_dc && next_dc->state == D_PREP &&
  1053. next_dc->bdev == bdev &&
  1054. __is_discard_front_mergeable(&di, &next_dc->di,
  1055. max_discard_blocks)) {
  1056. next_dc->di.lstart = di.lstart;
  1057. next_dc->di.len += di.len;
  1058. next_dc->di.start = di.start;
  1059. dcc->undiscard_blks += di.len;
  1060. __relocate_discard_cmd(dcc, next_dc);
  1061. if (tdc)
  1062. __remove_discard_cmd(sbi, tdc);
  1063. merged = true;
  1064. }
  1065. if (!merged) {
  1066. __insert_discard_tree(sbi, bdev, di.lstart, di.start,
  1067. di.len, NULL, NULL);
  1068. }
  1069. next:
  1070. prev_dc = next_dc;
  1071. if (!prev_dc)
  1072. break;
  1073. node = rb_next(&prev_dc->rb_node);
  1074. next_dc = rb_entry_safe(node, struct discard_cmd, rb_node);
  1075. }
  1076. }
  1077. static int __queue_discard_cmd(struct f2fs_sb_info *sbi,
  1078. struct block_device *bdev, block_t blkstart, block_t blklen)
  1079. {
  1080. block_t lblkstart = blkstart;
  1081. trace_f2fs_queue_discard(bdev, blkstart, blklen);
  1082. if (f2fs_is_multi_device(sbi)) {
  1083. int devi = f2fs_target_device_index(sbi, blkstart);
  1084. blkstart -= FDEV(devi).start_blk;
  1085. }
  1086. mutex_lock(&SM_I(sbi)->dcc_info->cmd_lock);
  1087. __update_discard_tree_range(sbi, bdev, lblkstart, blkstart, blklen);
  1088. mutex_unlock(&SM_I(sbi)->dcc_info->cmd_lock);
  1089. return 0;
  1090. }
  1091. static unsigned int __issue_discard_cmd_orderly(struct f2fs_sb_info *sbi,
  1092. struct discard_policy *dpolicy)
  1093. {
  1094. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1095. struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
  1096. struct rb_node **insert_p = NULL, *insert_parent = NULL;
  1097. struct discard_cmd *dc;
  1098. struct blk_plug plug;
  1099. unsigned int pos = dcc->next_pos;
  1100. unsigned int issued = 0;
  1101. bool io_interrupted = false;
  1102. mutex_lock(&dcc->cmd_lock);
  1103. dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
  1104. NULL, pos,
  1105. (struct rb_entry **)&prev_dc,
  1106. (struct rb_entry **)&next_dc,
  1107. &insert_p, &insert_parent, true);
  1108. if (!dc)
  1109. dc = next_dc;
  1110. blk_start_plug(&plug);
  1111. while (dc) {
  1112. struct rb_node *node;
  1113. int err = 0;
  1114. if (dc->state != D_PREP)
  1115. goto next;
  1116. if (dpolicy->io_aware && !is_idle(sbi)) {
  1117. io_interrupted = true;
  1118. break;
  1119. }
  1120. dcc->next_pos = dc->lstart + dc->len;
  1121. err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
  1122. if (issued >= dpolicy->max_requests)
  1123. break;
  1124. next:
  1125. node = rb_next(&dc->rb_node);
  1126. if (err)
  1127. __remove_discard_cmd(sbi, dc);
  1128. dc = rb_entry_safe(node, struct discard_cmd, rb_node);
  1129. }
  1130. blk_finish_plug(&plug);
  1131. if (!dc)
  1132. dcc->next_pos = 0;
  1133. mutex_unlock(&dcc->cmd_lock);
  1134. if (!issued && io_interrupted)
  1135. issued = -1;
  1136. return issued;
  1137. }
  1138. static int __issue_discard_cmd(struct f2fs_sb_info *sbi,
  1139. struct discard_policy *dpolicy)
  1140. {
  1141. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1142. struct list_head *pend_list;
  1143. struct discard_cmd *dc, *tmp;
  1144. struct blk_plug plug;
  1145. int i, issued = 0;
  1146. bool io_interrupted = false;
  1147. for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
  1148. if (i + 1 < dpolicy->granularity)
  1149. break;
  1150. if (i < DEFAULT_DISCARD_GRANULARITY && dpolicy->ordered)
  1151. return __issue_discard_cmd_orderly(sbi, dpolicy);
  1152. pend_list = &dcc->pend_list[i];
  1153. mutex_lock(&dcc->cmd_lock);
  1154. if (list_empty(pend_list))
  1155. goto next;
  1156. if (unlikely(dcc->rbtree_check))
  1157. f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
  1158. &dcc->root));
  1159. blk_start_plug(&plug);
  1160. list_for_each_entry_safe(dc, tmp, pend_list, list) {
  1161. f2fs_bug_on(sbi, dc->state != D_PREP);
  1162. if (dpolicy->io_aware && i < dpolicy->io_aware_gran &&
  1163. !is_idle(sbi)) {
  1164. io_interrupted = true;
  1165. break;
  1166. }
  1167. __submit_discard_cmd(sbi, dpolicy, dc, &issued);
  1168. if (issued >= dpolicy->max_requests)
  1169. break;
  1170. }
  1171. blk_finish_plug(&plug);
  1172. next:
  1173. mutex_unlock(&dcc->cmd_lock);
  1174. if (issued >= dpolicy->max_requests || io_interrupted)
  1175. break;
  1176. }
  1177. if (!issued && io_interrupted)
  1178. issued = -1;
  1179. return issued;
  1180. }
  1181. static bool __drop_discard_cmd(struct f2fs_sb_info *sbi)
  1182. {
  1183. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1184. struct list_head *pend_list;
  1185. struct discard_cmd *dc, *tmp;
  1186. int i;
  1187. bool dropped = false;
  1188. mutex_lock(&dcc->cmd_lock);
  1189. for (i = MAX_PLIST_NUM - 1; i >= 0; i--) {
  1190. pend_list = &dcc->pend_list[i];
  1191. list_for_each_entry_safe(dc, tmp, pend_list, list) {
  1192. f2fs_bug_on(sbi, dc->state != D_PREP);
  1193. __remove_discard_cmd(sbi, dc);
  1194. dropped = true;
  1195. }
  1196. }
  1197. mutex_unlock(&dcc->cmd_lock);
  1198. return dropped;
  1199. }
  1200. void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi)
  1201. {
  1202. __drop_discard_cmd(sbi);
  1203. }
  1204. static unsigned int __wait_one_discard_bio(struct f2fs_sb_info *sbi,
  1205. struct discard_cmd *dc)
  1206. {
  1207. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1208. unsigned int len = 0;
  1209. wait_for_completion_io(&dc->wait);
  1210. mutex_lock(&dcc->cmd_lock);
  1211. f2fs_bug_on(sbi, dc->state != D_DONE);
  1212. dc->ref--;
  1213. if (!dc->ref) {
  1214. if (!dc->error)
  1215. len = dc->len;
  1216. __remove_discard_cmd(sbi, dc);
  1217. }
  1218. mutex_unlock(&dcc->cmd_lock);
  1219. return len;
  1220. }
  1221. static unsigned int __wait_discard_cmd_range(struct f2fs_sb_info *sbi,
  1222. struct discard_policy *dpolicy,
  1223. block_t start, block_t end)
  1224. {
  1225. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1226. struct list_head *wait_list = (dpolicy->type == DPOLICY_FSTRIM) ?
  1227. &(dcc->fstrim_list) : &(dcc->wait_list);
  1228. struct discard_cmd *dc, *tmp;
  1229. bool need_wait;
  1230. unsigned int trimmed = 0;
  1231. next:
  1232. need_wait = false;
  1233. mutex_lock(&dcc->cmd_lock);
  1234. list_for_each_entry_safe(dc, tmp, wait_list, list) {
  1235. if (dc->lstart + dc->len <= start || end <= dc->lstart)
  1236. continue;
  1237. if (dc->len < dpolicy->granularity)
  1238. continue;
  1239. if (dc->state == D_DONE && !dc->ref) {
  1240. wait_for_completion_io(&dc->wait);
  1241. if (!dc->error)
  1242. trimmed += dc->len;
  1243. __remove_discard_cmd(sbi, dc);
  1244. } else {
  1245. dc->ref++;
  1246. need_wait = true;
  1247. break;
  1248. }
  1249. }
  1250. mutex_unlock(&dcc->cmd_lock);
  1251. if (need_wait) {
  1252. trimmed += __wait_one_discard_bio(sbi, dc);
  1253. goto next;
  1254. }
  1255. return trimmed;
  1256. }
  1257. static unsigned int __wait_all_discard_cmd(struct f2fs_sb_info *sbi,
  1258. struct discard_policy *dpolicy)
  1259. {
  1260. struct discard_policy dp;
  1261. unsigned int discard_blks;
  1262. if (dpolicy)
  1263. return __wait_discard_cmd_range(sbi, dpolicy, 0, UINT_MAX);
  1264. /* wait all */
  1265. __init_discard_policy(sbi, &dp, DPOLICY_FSTRIM, 1);
  1266. discard_blks = __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
  1267. __init_discard_policy(sbi, &dp, DPOLICY_UMOUNT, 1);
  1268. discard_blks += __wait_discard_cmd_range(sbi, &dp, 0, UINT_MAX);
  1269. return discard_blks;
  1270. }
  1271. /* This should be covered by global mutex, &sit_i->sentry_lock */
  1272. static void f2fs_wait_discard_bio(struct f2fs_sb_info *sbi, block_t blkaddr)
  1273. {
  1274. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1275. struct discard_cmd *dc;
  1276. bool need_wait = false;
  1277. mutex_lock(&dcc->cmd_lock);
  1278. dc = (struct discard_cmd *)f2fs_lookup_rb_tree(&dcc->root,
  1279. NULL, blkaddr);
  1280. if (dc) {
  1281. if (dc->state == D_PREP) {
  1282. __punch_discard_cmd(sbi, dc, blkaddr);
  1283. } else {
  1284. dc->ref++;
  1285. need_wait = true;
  1286. }
  1287. }
  1288. mutex_unlock(&dcc->cmd_lock);
  1289. if (need_wait)
  1290. __wait_one_discard_bio(sbi, dc);
  1291. }
  1292. void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi)
  1293. {
  1294. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1295. if (dcc && dcc->f2fs_issue_discard) {
  1296. struct task_struct *discard_thread = dcc->f2fs_issue_discard;
  1297. dcc->f2fs_issue_discard = NULL;
  1298. kthread_stop(discard_thread);
  1299. }
  1300. }
  1301. /* This comes from f2fs_put_super */
  1302. bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi)
  1303. {
  1304. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1305. struct discard_policy dpolicy;
  1306. bool dropped;
  1307. __init_discard_policy(sbi, &dpolicy, DPOLICY_UMOUNT,
  1308. dcc->discard_granularity);
  1309. __issue_discard_cmd(sbi, &dpolicy);
  1310. dropped = __drop_discard_cmd(sbi);
  1311. /* just to make sure there is no pending discard commands */
  1312. __wait_all_discard_cmd(sbi, NULL);
  1313. f2fs_bug_on(sbi, atomic_read(&dcc->discard_cmd_cnt));
  1314. return dropped;
  1315. }
  1316. static int issue_discard_thread(void *data)
  1317. {
  1318. struct f2fs_sb_info *sbi = data;
  1319. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1320. wait_queue_head_t *q = &dcc->discard_wait_queue;
  1321. struct discard_policy dpolicy;
  1322. unsigned int wait_ms = DEF_MIN_DISCARD_ISSUE_TIME;
  1323. int issued;
  1324. set_freezable();
  1325. do {
  1326. __init_discard_policy(sbi, &dpolicy, DPOLICY_BG,
  1327. dcc->discard_granularity);
  1328. wait_event_interruptible_timeout(*q,
  1329. kthread_should_stop() || freezing(current) ||
  1330. dcc->discard_wake,
  1331. msecs_to_jiffies(wait_ms));
  1332. if (dcc->discard_wake)
  1333. dcc->discard_wake = 0;
  1334. if (try_to_freeze())
  1335. continue;
  1336. if (f2fs_readonly(sbi->sb))
  1337. continue;
  1338. if (kthread_should_stop())
  1339. return 0;
  1340. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
  1341. wait_ms = dpolicy.max_interval;
  1342. continue;
  1343. }
  1344. if (sbi->gc_mode == GC_URGENT)
  1345. __init_discard_policy(sbi, &dpolicy, DPOLICY_FORCE, 1);
  1346. sb_start_intwrite(sbi->sb);
  1347. issued = __issue_discard_cmd(sbi, &dpolicy);
  1348. if (issued > 0) {
  1349. __wait_all_discard_cmd(sbi, &dpolicy);
  1350. wait_ms = dpolicy.min_interval;
  1351. } else if (issued == -1){
  1352. wait_ms = dpolicy.mid_interval;
  1353. } else {
  1354. wait_ms = dpolicy.max_interval;
  1355. }
  1356. sb_end_intwrite(sbi->sb);
  1357. } while (!kthread_should_stop());
  1358. return 0;
  1359. }
  1360. #ifdef CONFIG_BLK_DEV_ZONED
  1361. static int __f2fs_issue_discard_zone(struct f2fs_sb_info *sbi,
  1362. struct block_device *bdev, block_t blkstart, block_t blklen)
  1363. {
  1364. sector_t sector, nr_sects;
  1365. block_t lblkstart = blkstart;
  1366. int devi = 0;
  1367. if (f2fs_is_multi_device(sbi)) {
  1368. devi = f2fs_target_device_index(sbi, blkstart);
  1369. blkstart -= FDEV(devi).start_blk;
  1370. }
  1371. /*
  1372. * We need to know the type of the zone: for conventional zones,
  1373. * use regular discard if the drive supports it. For sequential
  1374. * zones, reset the zone write pointer.
  1375. */
  1376. switch (get_blkz_type(sbi, bdev, blkstart)) {
  1377. case BLK_ZONE_TYPE_CONVENTIONAL:
  1378. if (!blk_queue_discard(bdev_get_queue(bdev)))
  1379. return 0;
  1380. return __queue_discard_cmd(sbi, bdev, lblkstart, blklen);
  1381. case BLK_ZONE_TYPE_SEQWRITE_REQ:
  1382. case BLK_ZONE_TYPE_SEQWRITE_PREF:
  1383. sector = SECTOR_FROM_BLOCK(blkstart);
  1384. nr_sects = SECTOR_FROM_BLOCK(blklen);
  1385. if (sector & (bdev_zone_sectors(bdev) - 1) ||
  1386. nr_sects != bdev_zone_sectors(bdev)) {
  1387. f2fs_msg(sbi->sb, KERN_INFO,
  1388. "(%d) %s: Unaligned discard attempted (block %x + %x)",
  1389. devi, sbi->s_ndevs ? FDEV(devi).path: "",
  1390. blkstart, blklen);
  1391. return -EIO;
  1392. }
  1393. trace_f2fs_issue_reset_zone(bdev, blkstart);
  1394. return blkdev_reset_zones(bdev, sector,
  1395. nr_sects, GFP_NOFS);
  1396. default:
  1397. /* Unknown zone type: broken device ? */
  1398. return -EIO;
  1399. }
  1400. }
  1401. #endif
  1402. static int __issue_discard_async(struct f2fs_sb_info *sbi,
  1403. struct block_device *bdev, block_t blkstart, block_t blklen)
  1404. {
  1405. #ifdef CONFIG_BLK_DEV_ZONED
  1406. if (f2fs_sb_has_blkzoned(sbi->sb) &&
  1407. bdev_zoned_model(bdev) != BLK_ZONED_NONE)
  1408. return __f2fs_issue_discard_zone(sbi, bdev, blkstart, blklen);
  1409. #endif
  1410. return __queue_discard_cmd(sbi, bdev, blkstart, blklen);
  1411. }
  1412. static int f2fs_issue_discard(struct f2fs_sb_info *sbi,
  1413. block_t blkstart, block_t blklen)
  1414. {
  1415. sector_t start = blkstart, len = 0;
  1416. struct block_device *bdev;
  1417. struct seg_entry *se;
  1418. unsigned int offset;
  1419. block_t i;
  1420. int err = 0;
  1421. bdev = f2fs_target_device(sbi, blkstart, NULL);
  1422. for (i = blkstart; i < blkstart + blklen; i++, len++) {
  1423. if (i != start) {
  1424. struct block_device *bdev2 =
  1425. f2fs_target_device(sbi, i, NULL);
  1426. if (bdev2 != bdev) {
  1427. err = __issue_discard_async(sbi, bdev,
  1428. start, len);
  1429. if (err)
  1430. return err;
  1431. bdev = bdev2;
  1432. start = i;
  1433. len = 0;
  1434. }
  1435. }
  1436. se = get_seg_entry(sbi, GET_SEGNO(sbi, i));
  1437. offset = GET_BLKOFF_FROM_SEG0(sbi, i);
  1438. if (!f2fs_test_and_set_bit(offset, se->discard_map))
  1439. sbi->discard_blks--;
  1440. }
  1441. if (len)
  1442. err = __issue_discard_async(sbi, bdev, start, len);
  1443. return err;
  1444. }
  1445. static bool add_discard_addrs(struct f2fs_sb_info *sbi, struct cp_control *cpc,
  1446. bool check_only)
  1447. {
  1448. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  1449. int max_blocks = sbi->blocks_per_seg;
  1450. struct seg_entry *se = get_seg_entry(sbi, cpc->trim_start);
  1451. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  1452. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  1453. unsigned long *discard_map = (unsigned long *)se->discard_map;
  1454. unsigned long *dmap = SIT_I(sbi)->tmp_map;
  1455. unsigned int start = 0, end = -1;
  1456. bool force = (cpc->reason & CP_DISCARD);
  1457. struct discard_entry *de = NULL;
  1458. struct list_head *head = &SM_I(sbi)->dcc_info->entry_list;
  1459. int i;
  1460. if (se->valid_blocks == max_blocks || !f2fs_hw_support_discard(sbi))
  1461. return false;
  1462. if (!force) {
  1463. if (!f2fs_realtime_discard_enable(sbi) || !se->valid_blocks ||
  1464. SM_I(sbi)->dcc_info->nr_discards >=
  1465. SM_I(sbi)->dcc_info->max_discards)
  1466. return false;
  1467. }
  1468. /* SIT_VBLOCK_MAP_SIZE should be multiple of sizeof(unsigned long) */
  1469. for (i = 0; i < entries; i++)
  1470. dmap[i] = force ? ~ckpt_map[i] & ~discard_map[i] :
  1471. (cur_map[i] ^ ckpt_map[i]) & ckpt_map[i];
  1472. while (force || SM_I(sbi)->dcc_info->nr_discards <=
  1473. SM_I(sbi)->dcc_info->max_discards) {
  1474. start = __find_rev_next_bit(dmap, max_blocks, end + 1);
  1475. if (start >= max_blocks)
  1476. break;
  1477. end = __find_rev_next_zero_bit(dmap, max_blocks, start + 1);
  1478. if (force && start && end != max_blocks
  1479. && (end - start) < cpc->trim_minlen)
  1480. continue;
  1481. if (check_only)
  1482. return true;
  1483. if (!de) {
  1484. de = f2fs_kmem_cache_alloc(discard_entry_slab,
  1485. GFP_F2FS_ZERO);
  1486. de->start_blkaddr = START_BLOCK(sbi, cpc->trim_start);
  1487. list_add_tail(&de->list, head);
  1488. }
  1489. for (i = start; i < end; i++)
  1490. __set_bit_le(i, (void *)de->discard_map);
  1491. SM_I(sbi)->dcc_info->nr_discards += end - start;
  1492. }
  1493. return false;
  1494. }
  1495. static void release_discard_addr(struct discard_entry *entry)
  1496. {
  1497. list_del(&entry->list);
  1498. kmem_cache_free(discard_entry_slab, entry);
  1499. }
  1500. void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi)
  1501. {
  1502. struct list_head *head = &(SM_I(sbi)->dcc_info->entry_list);
  1503. struct discard_entry *entry, *this;
  1504. /* drop caches */
  1505. list_for_each_entry_safe(entry, this, head, list)
  1506. release_discard_addr(entry);
  1507. }
  1508. /*
  1509. * Should call f2fs_clear_prefree_segments after checkpoint is done.
  1510. */
  1511. static void set_prefree_as_free_segments(struct f2fs_sb_info *sbi)
  1512. {
  1513. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1514. unsigned int segno;
  1515. mutex_lock(&dirty_i->seglist_lock);
  1516. for_each_set_bit(segno, dirty_i->dirty_segmap[PRE], MAIN_SEGS(sbi))
  1517. __set_test_and_free(sbi, segno);
  1518. mutex_unlock(&dirty_i->seglist_lock);
  1519. }
  1520. void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
  1521. struct cp_control *cpc)
  1522. {
  1523. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1524. struct list_head *head = &dcc->entry_list;
  1525. struct discard_entry *entry, *this;
  1526. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  1527. unsigned long *prefree_map = dirty_i->dirty_segmap[PRE];
  1528. unsigned int start = 0, end = -1;
  1529. unsigned int secno, start_segno;
  1530. bool force = (cpc->reason & CP_DISCARD);
  1531. bool need_align = test_opt(sbi, LFS) && sbi->segs_per_sec > 1;
  1532. mutex_lock(&dirty_i->seglist_lock);
  1533. while (1) {
  1534. int i;
  1535. if (need_align && end != -1)
  1536. end--;
  1537. start = find_next_bit(prefree_map, MAIN_SEGS(sbi), end + 1);
  1538. if (start >= MAIN_SEGS(sbi))
  1539. break;
  1540. end = find_next_zero_bit(prefree_map, MAIN_SEGS(sbi),
  1541. start + 1);
  1542. if (need_align) {
  1543. start = rounddown(start, sbi->segs_per_sec);
  1544. end = roundup(end, sbi->segs_per_sec);
  1545. }
  1546. for (i = start; i < end; i++) {
  1547. if (test_and_clear_bit(i, prefree_map))
  1548. dirty_i->nr_dirty[PRE]--;
  1549. }
  1550. if (!f2fs_realtime_discard_enable(sbi))
  1551. continue;
  1552. if (force && start >= cpc->trim_start &&
  1553. (end - 1) <= cpc->trim_end)
  1554. continue;
  1555. if (!test_opt(sbi, LFS) || sbi->segs_per_sec == 1) {
  1556. f2fs_issue_discard(sbi, START_BLOCK(sbi, start),
  1557. (end - start) << sbi->log_blocks_per_seg);
  1558. continue;
  1559. }
  1560. next:
  1561. secno = GET_SEC_FROM_SEG(sbi, start);
  1562. start_segno = GET_SEG_FROM_SEC(sbi, secno);
  1563. if (!IS_CURSEC(sbi, secno) &&
  1564. !get_valid_blocks(sbi, start, true))
  1565. f2fs_issue_discard(sbi, START_BLOCK(sbi, start_segno),
  1566. sbi->segs_per_sec << sbi->log_blocks_per_seg);
  1567. start = start_segno + sbi->segs_per_sec;
  1568. if (start < end)
  1569. goto next;
  1570. else
  1571. end = start - 1;
  1572. }
  1573. mutex_unlock(&dirty_i->seglist_lock);
  1574. /* send small discards */
  1575. list_for_each_entry_safe(entry, this, head, list) {
  1576. unsigned int cur_pos = 0, next_pos, len, total_len = 0;
  1577. bool is_valid = test_bit_le(0, entry->discard_map);
  1578. find_next:
  1579. if (is_valid) {
  1580. next_pos = find_next_zero_bit_le(entry->discard_map,
  1581. sbi->blocks_per_seg, cur_pos);
  1582. len = next_pos - cur_pos;
  1583. if (f2fs_sb_has_blkzoned(sbi->sb) ||
  1584. (force && len < cpc->trim_minlen))
  1585. goto skip;
  1586. f2fs_issue_discard(sbi, entry->start_blkaddr + cur_pos,
  1587. len);
  1588. total_len += len;
  1589. } else {
  1590. next_pos = find_next_bit_le(entry->discard_map,
  1591. sbi->blocks_per_seg, cur_pos);
  1592. }
  1593. skip:
  1594. cur_pos = next_pos;
  1595. is_valid = !is_valid;
  1596. if (cur_pos < sbi->blocks_per_seg)
  1597. goto find_next;
  1598. release_discard_addr(entry);
  1599. dcc->nr_discards -= total_len;
  1600. }
  1601. wake_up_discard_thread(sbi, false);
  1602. }
  1603. static int create_discard_cmd_control(struct f2fs_sb_info *sbi)
  1604. {
  1605. dev_t dev = sbi->sb->s_bdev->bd_dev;
  1606. struct discard_cmd_control *dcc;
  1607. int err = 0, i;
  1608. if (SM_I(sbi)->dcc_info) {
  1609. dcc = SM_I(sbi)->dcc_info;
  1610. goto init_thread;
  1611. }
  1612. dcc = f2fs_kzalloc(sbi, sizeof(struct discard_cmd_control), GFP_KERNEL);
  1613. if (!dcc)
  1614. return -ENOMEM;
  1615. dcc->discard_granularity = DEFAULT_DISCARD_GRANULARITY;
  1616. INIT_LIST_HEAD(&dcc->entry_list);
  1617. for (i = 0; i < MAX_PLIST_NUM; i++)
  1618. INIT_LIST_HEAD(&dcc->pend_list[i]);
  1619. INIT_LIST_HEAD(&dcc->wait_list);
  1620. INIT_LIST_HEAD(&dcc->fstrim_list);
  1621. mutex_init(&dcc->cmd_lock);
  1622. atomic_set(&dcc->issued_discard, 0);
  1623. atomic_set(&dcc->issing_discard, 0);
  1624. atomic_set(&dcc->discard_cmd_cnt, 0);
  1625. dcc->nr_discards = 0;
  1626. dcc->max_discards = MAIN_SEGS(sbi) << sbi->log_blocks_per_seg;
  1627. dcc->undiscard_blks = 0;
  1628. dcc->next_pos = 0;
  1629. dcc->root = RB_ROOT;
  1630. dcc->rbtree_check = false;
  1631. init_waitqueue_head(&dcc->discard_wait_queue);
  1632. SM_I(sbi)->dcc_info = dcc;
  1633. init_thread:
  1634. dcc->f2fs_issue_discard = kthread_run(issue_discard_thread, sbi,
  1635. "f2fs_discard-%u:%u", MAJOR(dev), MINOR(dev));
  1636. if (IS_ERR(dcc->f2fs_issue_discard)) {
  1637. err = PTR_ERR(dcc->f2fs_issue_discard);
  1638. kfree(dcc);
  1639. SM_I(sbi)->dcc_info = NULL;
  1640. return err;
  1641. }
  1642. return err;
  1643. }
  1644. static void destroy_discard_cmd_control(struct f2fs_sb_info *sbi)
  1645. {
  1646. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  1647. if (!dcc)
  1648. return;
  1649. f2fs_stop_discard_thread(sbi);
  1650. kfree(dcc);
  1651. SM_I(sbi)->dcc_info = NULL;
  1652. }
  1653. static bool __mark_sit_entry_dirty(struct f2fs_sb_info *sbi, unsigned int segno)
  1654. {
  1655. struct sit_info *sit_i = SIT_I(sbi);
  1656. if (!__test_and_set_bit(segno, sit_i->dirty_sentries_bitmap)) {
  1657. sit_i->dirty_sentries++;
  1658. return false;
  1659. }
  1660. return true;
  1661. }
  1662. static void __set_sit_entry_type(struct f2fs_sb_info *sbi, int type,
  1663. unsigned int segno, int modified)
  1664. {
  1665. struct seg_entry *se = get_seg_entry(sbi, segno);
  1666. se->type = type;
  1667. if (modified)
  1668. __mark_sit_entry_dirty(sbi, segno);
  1669. }
  1670. static void update_sit_entry(struct f2fs_sb_info *sbi, block_t blkaddr, int del)
  1671. {
  1672. struct seg_entry *se;
  1673. unsigned int segno, offset;
  1674. long int new_vblocks;
  1675. bool exist;
  1676. #ifdef CONFIG_F2FS_CHECK_FS
  1677. bool mir_exist;
  1678. #endif
  1679. segno = GET_SEGNO(sbi, blkaddr);
  1680. se = get_seg_entry(sbi, segno);
  1681. new_vblocks = se->valid_blocks + del;
  1682. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  1683. f2fs_bug_on(sbi, (new_vblocks >> (sizeof(unsigned short) << 3) ||
  1684. (new_vblocks > sbi->blocks_per_seg)));
  1685. se->valid_blocks = new_vblocks;
  1686. se->mtime = get_mtime(sbi, false);
  1687. if (se->mtime > SIT_I(sbi)->max_mtime)
  1688. SIT_I(sbi)->max_mtime = se->mtime;
  1689. /* Update valid block bitmap */
  1690. if (del > 0) {
  1691. exist = f2fs_test_and_set_bit(offset, se->cur_valid_map);
  1692. #ifdef CONFIG_F2FS_CHECK_FS
  1693. mir_exist = f2fs_test_and_set_bit(offset,
  1694. se->cur_valid_map_mir);
  1695. if (unlikely(exist != mir_exist)) {
  1696. f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
  1697. "when setting bitmap, blk:%u, old bit:%d",
  1698. blkaddr, exist);
  1699. f2fs_bug_on(sbi, 1);
  1700. }
  1701. #endif
  1702. if (unlikely(exist)) {
  1703. f2fs_msg(sbi->sb, KERN_ERR,
  1704. "Bitmap was wrongly set, blk:%u", blkaddr);
  1705. f2fs_bug_on(sbi, 1);
  1706. se->valid_blocks--;
  1707. del = 0;
  1708. }
  1709. if (!f2fs_test_and_set_bit(offset, se->discard_map))
  1710. sbi->discard_blks--;
  1711. /* don't overwrite by SSR to keep node chain */
  1712. if (IS_NODESEG(se->type)) {
  1713. if (!f2fs_test_and_set_bit(offset, se->ckpt_valid_map))
  1714. se->ckpt_valid_blocks++;
  1715. }
  1716. } else {
  1717. exist = f2fs_test_and_clear_bit(offset, se->cur_valid_map);
  1718. #ifdef CONFIG_F2FS_CHECK_FS
  1719. mir_exist = f2fs_test_and_clear_bit(offset,
  1720. se->cur_valid_map_mir);
  1721. if (unlikely(exist != mir_exist)) {
  1722. f2fs_msg(sbi->sb, KERN_ERR, "Inconsistent error "
  1723. "when clearing bitmap, blk:%u, old bit:%d",
  1724. blkaddr, exist);
  1725. f2fs_bug_on(sbi, 1);
  1726. }
  1727. #endif
  1728. if (unlikely(!exist)) {
  1729. f2fs_msg(sbi->sb, KERN_ERR,
  1730. "Bitmap was wrongly cleared, blk:%u", blkaddr);
  1731. f2fs_bug_on(sbi, 1);
  1732. se->valid_blocks++;
  1733. del = 0;
  1734. }
  1735. if (f2fs_test_and_clear_bit(offset, se->discard_map))
  1736. sbi->discard_blks++;
  1737. }
  1738. if (!f2fs_test_bit(offset, se->ckpt_valid_map))
  1739. se->ckpt_valid_blocks += del;
  1740. __mark_sit_entry_dirty(sbi, segno);
  1741. /* update total number of valid blocks to be written in ckpt area */
  1742. SIT_I(sbi)->written_valid_blocks += del;
  1743. if (sbi->segs_per_sec > 1)
  1744. get_sec_entry(sbi, segno)->valid_blocks += del;
  1745. }
  1746. void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr)
  1747. {
  1748. unsigned int segno = GET_SEGNO(sbi, addr);
  1749. struct sit_info *sit_i = SIT_I(sbi);
  1750. f2fs_bug_on(sbi, addr == NULL_ADDR);
  1751. if (addr == NEW_ADDR)
  1752. return;
  1753. invalidate_mapping_pages(META_MAPPING(sbi), addr, addr);
  1754. /* add it into sit main buffer */
  1755. down_write(&sit_i->sentry_lock);
  1756. update_sit_entry(sbi, addr, -1);
  1757. /* add it into dirty seglist */
  1758. locate_dirty_segment(sbi, segno);
  1759. up_write(&sit_i->sentry_lock);
  1760. }
  1761. bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr)
  1762. {
  1763. struct sit_info *sit_i = SIT_I(sbi);
  1764. unsigned int segno, offset;
  1765. struct seg_entry *se;
  1766. bool is_cp = false;
  1767. if (!is_valid_data_blkaddr(sbi, blkaddr))
  1768. return true;
  1769. down_read(&sit_i->sentry_lock);
  1770. segno = GET_SEGNO(sbi, blkaddr);
  1771. se = get_seg_entry(sbi, segno);
  1772. offset = GET_BLKOFF_FROM_SEG0(sbi, blkaddr);
  1773. if (f2fs_test_bit(offset, se->ckpt_valid_map))
  1774. is_cp = true;
  1775. up_read(&sit_i->sentry_lock);
  1776. return is_cp;
  1777. }
  1778. /*
  1779. * This function should be resided under the curseg_mutex lock
  1780. */
  1781. static void __add_sum_entry(struct f2fs_sb_info *sbi, int type,
  1782. struct f2fs_summary *sum)
  1783. {
  1784. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1785. void *addr = curseg->sum_blk;
  1786. addr += curseg->next_blkoff * sizeof(struct f2fs_summary);
  1787. memcpy(addr, sum, sizeof(struct f2fs_summary));
  1788. }
  1789. /*
  1790. * Calculate the number of current summary pages for writing
  1791. */
  1792. int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra)
  1793. {
  1794. int valid_sum_count = 0;
  1795. int i, sum_in_page;
  1796. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  1797. if (sbi->ckpt->alloc_type[i] == SSR)
  1798. valid_sum_count += sbi->blocks_per_seg;
  1799. else {
  1800. if (for_ra)
  1801. valid_sum_count += le16_to_cpu(
  1802. F2FS_CKPT(sbi)->cur_data_blkoff[i]);
  1803. else
  1804. valid_sum_count += curseg_blkoff(sbi, i);
  1805. }
  1806. }
  1807. sum_in_page = (PAGE_SIZE - 2 * SUM_JOURNAL_SIZE -
  1808. SUM_FOOTER_SIZE) / SUMMARY_SIZE;
  1809. if (valid_sum_count <= sum_in_page)
  1810. return 1;
  1811. else if ((valid_sum_count - sum_in_page) <=
  1812. (PAGE_SIZE - SUM_FOOTER_SIZE) / SUMMARY_SIZE)
  1813. return 2;
  1814. return 3;
  1815. }
  1816. /*
  1817. * Caller should put this summary page
  1818. */
  1819. struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno)
  1820. {
  1821. return f2fs_get_meta_page_nofail(sbi, GET_SUM_BLOCK(sbi, segno));
  1822. }
  1823. void f2fs_update_meta_page(struct f2fs_sb_info *sbi,
  1824. void *src, block_t blk_addr)
  1825. {
  1826. struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
  1827. memcpy(page_address(page), src, PAGE_SIZE);
  1828. set_page_dirty(page);
  1829. f2fs_put_page(page, 1);
  1830. }
  1831. static void write_sum_page(struct f2fs_sb_info *sbi,
  1832. struct f2fs_summary_block *sum_blk, block_t blk_addr)
  1833. {
  1834. f2fs_update_meta_page(sbi, (void *)sum_blk, blk_addr);
  1835. }
  1836. static void write_current_sum_page(struct f2fs_sb_info *sbi,
  1837. int type, block_t blk_addr)
  1838. {
  1839. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1840. struct page *page = f2fs_grab_meta_page(sbi, blk_addr);
  1841. struct f2fs_summary_block *src = curseg->sum_blk;
  1842. struct f2fs_summary_block *dst;
  1843. dst = (struct f2fs_summary_block *)page_address(page);
  1844. memset(dst, 0, PAGE_SIZE);
  1845. mutex_lock(&curseg->curseg_mutex);
  1846. down_read(&curseg->journal_rwsem);
  1847. memcpy(&dst->journal, curseg->journal, SUM_JOURNAL_SIZE);
  1848. up_read(&curseg->journal_rwsem);
  1849. memcpy(dst->entries, src->entries, SUM_ENTRY_SIZE);
  1850. memcpy(&dst->footer, &src->footer, SUM_FOOTER_SIZE);
  1851. mutex_unlock(&curseg->curseg_mutex);
  1852. set_page_dirty(page);
  1853. f2fs_put_page(page, 1);
  1854. }
  1855. static int is_next_segment_free(struct f2fs_sb_info *sbi, int type)
  1856. {
  1857. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1858. unsigned int segno = curseg->segno + 1;
  1859. struct free_segmap_info *free_i = FREE_I(sbi);
  1860. if (segno < MAIN_SEGS(sbi) && segno % sbi->segs_per_sec)
  1861. return !test_bit(segno, free_i->free_segmap);
  1862. return 0;
  1863. }
  1864. /*
  1865. * Find a new segment from the free segments bitmap to right order
  1866. * This function should be returned with success, otherwise BUG
  1867. */
  1868. static void get_new_segment(struct f2fs_sb_info *sbi,
  1869. unsigned int *newseg, bool new_sec, int dir)
  1870. {
  1871. struct free_segmap_info *free_i = FREE_I(sbi);
  1872. unsigned int segno, secno, zoneno;
  1873. unsigned int total_zones = MAIN_SECS(sbi) / sbi->secs_per_zone;
  1874. unsigned int hint = GET_SEC_FROM_SEG(sbi, *newseg);
  1875. unsigned int old_zoneno = GET_ZONE_FROM_SEG(sbi, *newseg);
  1876. unsigned int left_start = hint;
  1877. bool init = true;
  1878. int go_left = 0;
  1879. int i;
  1880. spin_lock(&free_i->segmap_lock);
  1881. if (!new_sec && ((*newseg + 1) % sbi->segs_per_sec)) {
  1882. segno = find_next_zero_bit(free_i->free_segmap,
  1883. GET_SEG_FROM_SEC(sbi, hint + 1), *newseg + 1);
  1884. if (segno < GET_SEG_FROM_SEC(sbi, hint + 1))
  1885. goto got_it;
  1886. }
  1887. find_other_zone:
  1888. secno = find_next_zero_bit(free_i->free_secmap, MAIN_SECS(sbi), hint);
  1889. if (secno >= MAIN_SECS(sbi)) {
  1890. if (dir == ALLOC_RIGHT) {
  1891. secno = find_next_zero_bit(free_i->free_secmap,
  1892. MAIN_SECS(sbi), 0);
  1893. f2fs_bug_on(sbi, secno >= MAIN_SECS(sbi));
  1894. } else {
  1895. go_left = 1;
  1896. left_start = hint - 1;
  1897. }
  1898. }
  1899. if (go_left == 0)
  1900. goto skip_left;
  1901. while (test_bit(left_start, free_i->free_secmap)) {
  1902. if (left_start > 0) {
  1903. left_start--;
  1904. continue;
  1905. }
  1906. left_start = find_next_zero_bit(free_i->free_secmap,
  1907. MAIN_SECS(sbi), 0);
  1908. f2fs_bug_on(sbi, left_start >= MAIN_SECS(sbi));
  1909. break;
  1910. }
  1911. secno = left_start;
  1912. skip_left:
  1913. segno = GET_SEG_FROM_SEC(sbi, secno);
  1914. zoneno = GET_ZONE_FROM_SEC(sbi, secno);
  1915. /* give up on finding another zone */
  1916. if (!init)
  1917. goto got_it;
  1918. if (sbi->secs_per_zone == 1)
  1919. goto got_it;
  1920. if (zoneno == old_zoneno)
  1921. goto got_it;
  1922. if (dir == ALLOC_LEFT) {
  1923. if (!go_left && zoneno + 1 >= total_zones)
  1924. goto got_it;
  1925. if (go_left && zoneno == 0)
  1926. goto got_it;
  1927. }
  1928. for (i = 0; i < NR_CURSEG_TYPE; i++)
  1929. if (CURSEG_I(sbi, i)->zone == zoneno)
  1930. break;
  1931. if (i < NR_CURSEG_TYPE) {
  1932. /* zone is in user, try another */
  1933. if (go_left)
  1934. hint = zoneno * sbi->secs_per_zone - 1;
  1935. else if (zoneno + 1 >= total_zones)
  1936. hint = 0;
  1937. else
  1938. hint = (zoneno + 1) * sbi->secs_per_zone;
  1939. init = false;
  1940. goto find_other_zone;
  1941. }
  1942. got_it:
  1943. /* set it as dirty segment in free segmap */
  1944. f2fs_bug_on(sbi, test_bit(segno, free_i->free_segmap));
  1945. __set_inuse(sbi, segno);
  1946. *newseg = segno;
  1947. spin_unlock(&free_i->segmap_lock);
  1948. }
  1949. static void reset_curseg(struct f2fs_sb_info *sbi, int type, int modified)
  1950. {
  1951. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1952. struct summary_footer *sum_footer;
  1953. curseg->segno = curseg->next_segno;
  1954. curseg->zone = GET_ZONE_FROM_SEG(sbi, curseg->segno);
  1955. curseg->next_blkoff = 0;
  1956. curseg->next_segno = NULL_SEGNO;
  1957. sum_footer = &(curseg->sum_blk->footer);
  1958. memset(sum_footer, 0, sizeof(struct summary_footer));
  1959. if (IS_DATASEG(type))
  1960. SET_SUM_TYPE(sum_footer, SUM_TYPE_DATA);
  1961. if (IS_NODESEG(type))
  1962. SET_SUM_TYPE(sum_footer, SUM_TYPE_NODE);
  1963. __set_sit_entry_type(sbi, type, curseg->segno, modified);
  1964. }
  1965. static unsigned int __get_next_segno(struct f2fs_sb_info *sbi, int type)
  1966. {
  1967. /* if segs_per_sec is large than 1, we need to keep original policy. */
  1968. if (sbi->segs_per_sec != 1)
  1969. return CURSEG_I(sbi, type)->segno;
  1970. if (test_opt(sbi, NOHEAP) &&
  1971. (type == CURSEG_HOT_DATA || IS_NODESEG(type)))
  1972. return 0;
  1973. if (SIT_I(sbi)->last_victim[ALLOC_NEXT])
  1974. return SIT_I(sbi)->last_victim[ALLOC_NEXT];
  1975. /* find segments from 0 to reuse freed segments */
  1976. if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
  1977. return 0;
  1978. return CURSEG_I(sbi, type)->segno;
  1979. }
  1980. /*
  1981. * Allocate a current working segment.
  1982. * This function always allocates a free segment in LFS manner.
  1983. */
  1984. static void new_curseg(struct f2fs_sb_info *sbi, int type, bool new_sec)
  1985. {
  1986. struct curseg_info *curseg = CURSEG_I(sbi, type);
  1987. unsigned int segno = curseg->segno;
  1988. int dir = ALLOC_LEFT;
  1989. write_sum_page(sbi, curseg->sum_blk,
  1990. GET_SUM_BLOCK(sbi, segno));
  1991. if (type == CURSEG_WARM_DATA || type == CURSEG_COLD_DATA)
  1992. dir = ALLOC_RIGHT;
  1993. if (test_opt(sbi, NOHEAP))
  1994. dir = ALLOC_RIGHT;
  1995. segno = __get_next_segno(sbi, type);
  1996. get_new_segment(sbi, &segno, new_sec, dir);
  1997. curseg->next_segno = segno;
  1998. reset_curseg(sbi, type, 1);
  1999. curseg->alloc_type = LFS;
  2000. }
  2001. static void __next_free_blkoff(struct f2fs_sb_info *sbi,
  2002. struct curseg_info *seg, block_t start)
  2003. {
  2004. struct seg_entry *se = get_seg_entry(sbi, seg->segno);
  2005. int entries = SIT_VBLOCK_MAP_SIZE / sizeof(unsigned long);
  2006. unsigned long *target_map = SIT_I(sbi)->tmp_map;
  2007. unsigned long *ckpt_map = (unsigned long *)se->ckpt_valid_map;
  2008. unsigned long *cur_map = (unsigned long *)se->cur_valid_map;
  2009. int i, pos;
  2010. for (i = 0; i < entries; i++)
  2011. target_map[i] = ckpt_map[i] | cur_map[i];
  2012. pos = __find_rev_next_zero_bit(target_map, sbi->blocks_per_seg, start);
  2013. seg->next_blkoff = pos;
  2014. }
  2015. /*
  2016. * If a segment is written by LFS manner, next block offset is just obtained
  2017. * by increasing the current block offset. However, if a segment is written by
  2018. * SSR manner, next block offset obtained by calling __next_free_blkoff
  2019. */
  2020. static void __refresh_next_blkoff(struct f2fs_sb_info *sbi,
  2021. struct curseg_info *seg)
  2022. {
  2023. if (seg->alloc_type == SSR)
  2024. __next_free_blkoff(sbi, seg, seg->next_blkoff + 1);
  2025. else
  2026. seg->next_blkoff++;
  2027. }
  2028. /*
  2029. * This function always allocates a used segment(from dirty seglist) by SSR
  2030. * manner, so it should recover the existing segment information of valid blocks
  2031. */
  2032. static void change_curseg(struct f2fs_sb_info *sbi, int type)
  2033. {
  2034. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  2035. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2036. unsigned int new_segno = curseg->next_segno;
  2037. struct f2fs_summary_block *sum_node;
  2038. struct page *sum_page;
  2039. write_sum_page(sbi, curseg->sum_blk,
  2040. GET_SUM_BLOCK(sbi, curseg->segno));
  2041. __set_test_and_inuse(sbi, new_segno);
  2042. mutex_lock(&dirty_i->seglist_lock);
  2043. __remove_dirty_segment(sbi, new_segno, PRE);
  2044. __remove_dirty_segment(sbi, new_segno, DIRTY);
  2045. mutex_unlock(&dirty_i->seglist_lock);
  2046. reset_curseg(sbi, type, 1);
  2047. curseg->alloc_type = SSR;
  2048. __next_free_blkoff(sbi, curseg, 0);
  2049. sum_page = f2fs_get_sum_page(sbi, new_segno);
  2050. sum_node = (struct f2fs_summary_block *)page_address(sum_page);
  2051. memcpy(curseg->sum_blk, sum_node, SUM_ENTRY_SIZE);
  2052. f2fs_put_page(sum_page, 1);
  2053. }
  2054. static int get_ssr_segment(struct f2fs_sb_info *sbi, int type)
  2055. {
  2056. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2057. const struct victim_selection *v_ops = DIRTY_I(sbi)->v_ops;
  2058. unsigned segno = NULL_SEGNO;
  2059. int i, cnt;
  2060. bool reversed = false;
  2061. /* f2fs_need_SSR() already forces to do this */
  2062. if (v_ops->get_victim(sbi, &segno, BG_GC, type, SSR)) {
  2063. curseg->next_segno = segno;
  2064. return 1;
  2065. }
  2066. /* For node segments, let's do SSR more intensively */
  2067. if (IS_NODESEG(type)) {
  2068. if (type >= CURSEG_WARM_NODE) {
  2069. reversed = true;
  2070. i = CURSEG_COLD_NODE;
  2071. } else {
  2072. i = CURSEG_HOT_NODE;
  2073. }
  2074. cnt = NR_CURSEG_NODE_TYPE;
  2075. } else {
  2076. if (type >= CURSEG_WARM_DATA) {
  2077. reversed = true;
  2078. i = CURSEG_COLD_DATA;
  2079. } else {
  2080. i = CURSEG_HOT_DATA;
  2081. }
  2082. cnt = NR_CURSEG_DATA_TYPE;
  2083. }
  2084. for (; cnt-- > 0; reversed ? i-- : i++) {
  2085. if (i == type)
  2086. continue;
  2087. if (v_ops->get_victim(sbi, &segno, BG_GC, i, SSR)) {
  2088. curseg->next_segno = segno;
  2089. return 1;
  2090. }
  2091. }
  2092. return 0;
  2093. }
  2094. /*
  2095. * flush out current segment and replace it with new segment
  2096. * This function should be returned with success, otherwise BUG
  2097. */
  2098. static void allocate_segment_by_default(struct f2fs_sb_info *sbi,
  2099. int type, bool force)
  2100. {
  2101. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2102. if (force)
  2103. new_curseg(sbi, type, true);
  2104. else if (!is_set_ckpt_flags(sbi, CP_CRC_RECOVERY_FLAG) &&
  2105. type == CURSEG_WARM_NODE)
  2106. new_curseg(sbi, type, false);
  2107. else if (curseg->alloc_type == LFS && is_next_segment_free(sbi, type))
  2108. new_curseg(sbi, type, false);
  2109. else if (f2fs_need_SSR(sbi) && get_ssr_segment(sbi, type))
  2110. change_curseg(sbi, type);
  2111. else
  2112. new_curseg(sbi, type, false);
  2113. stat_inc_seg_type(sbi, curseg);
  2114. }
  2115. void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi)
  2116. {
  2117. struct curseg_info *curseg;
  2118. unsigned int old_segno;
  2119. int i;
  2120. down_write(&SIT_I(sbi)->sentry_lock);
  2121. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  2122. curseg = CURSEG_I(sbi, i);
  2123. old_segno = curseg->segno;
  2124. SIT_I(sbi)->s_ops->allocate_segment(sbi, i, true);
  2125. locate_dirty_segment(sbi, old_segno);
  2126. }
  2127. up_write(&SIT_I(sbi)->sentry_lock);
  2128. }
  2129. static const struct segment_allocation default_salloc_ops = {
  2130. .allocate_segment = allocate_segment_by_default,
  2131. };
  2132. bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
  2133. struct cp_control *cpc)
  2134. {
  2135. __u64 trim_start = cpc->trim_start;
  2136. bool has_candidate = false;
  2137. down_write(&SIT_I(sbi)->sentry_lock);
  2138. for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++) {
  2139. if (add_discard_addrs(sbi, cpc, true)) {
  2140. has_candidate = true;
  2141. break;
  2142. }
  2143. }
  2144. up_write(&SIT_I(sbi)->sentry_lock);
  2145. cpc->trim_start = trim_start;
  2146. return has_candidate;
  2147. }
  2148. static unsigned int __issue_discard_cmd_range(struct f2fs_sb_info *sbi,
  2149. struct discard_policy *dpolicy,
  2150. unsigned int start, unsigned int end)
  2151. {
  2152. struct discard_cmd_control *dcc = SM_I(sbi)->dcc_info;
  2153. struct discard_cmd *prev_dc = NULL, *next_dc = NULL;
  2154. struct rb_node **insert_p = NULL, *insert_parent = NULL;
  2155. struct discard_cmd *dc;
  2156. struct blk_plug plug;
  2157. int issued;
  2158. unsigned int trimmed = 0;
  2159. next:
  2160. issued = 0;
  2161. mutex_lock(&dcc->cmd_lock);
  2162. if (unlikely(dcc->rbtree_check))
  2163. f2fs_bug_on(sbi, !f2fs_check_rb_tree_consistence(sbi,
  2164. &dcc->root));
  2165. dc = (struct discard_cmd *)f2fs_lookup_rb_tree_ret(&dcc->root,
  2166. NULL, start,
  2167. (struct rb_entry **)&prev_dc,
  2168. (struct rb_entry **)&next_dc,
  2169. &insert_p, &insert_parent, true);
  2170. if (!dc)
  2171. dc = next_dc;
  2172. blk_start_plug(&plug);
  2173. while (dc && dc->lstart <= end) {
  2174. struct rb_node *node;
  2175. int err = 0;
  2176. if (dc->len < dpolicy->granularity)
  2177. goto skip;
  2178. if (dc->state != D_PREP) {
  2179. list_move_tail(&dc->list, &dcc->fstrim_list);
  2180. goto skip;
  2181. }
  2182. err = __submit_discard_cmd(sbi, dpolicy, dc, &issued);
  2183. if (issued >= dpolicy->max_requests) {
  2184. start = dc->lstart + dc->len;
  2185. if (err)
  2186. __remove_discard_cmd(sbi, dc);
  2187. blk_finish_plug(&plug);
  2188. mutex_unlock(&dcc->cmd_lock);
  2189. trimmed += __wait_all_discard_cmd(sbi, NULL);
  2190. congestion_wait(BLK_RW_ASYNC, HZ/50);
  2191. goto next;
  2192. }
  2193. skip:
  2194. node = rb_next(&dc->rb_node);
  2195. if (err)
  2196. __remove_discard_cmd(sbi, dc);
  2197. dc = rb_entry_safe(node, struct discard_cmd, rb_node);
  2198. if (fatal_signal_pending(current))
  2199. break;
  2200. }
  2201. blk_finish_plug(&plug);
  2202. mutex_unlock(&dcc->cmd_lock);
  2203. return trimmed;
  2204. }
  2205. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range)
  2206. {
  2207. __u64 start = F2FS_BYTES_TO_BLK(range->start);
  2208. __u64 end = start + F2FS_BYTES_TO_BLK(range->len) - 1;
  2209. unsigned int start_segno, end_segno;
  2210. block_t start_block, end_block;
  2211. struct cp_control cpc;
  2212. struct discard_policy dpolicy;
  2213. unsigned long long trimmed = 0;
  2214. int err = 0;
  2215. bool need_align = test_opt(sbi, LFS) && sbi->segs_per_sec > 1;
  2216. if (start >= MAX_BLKADDR(sbi) || range->len < sbi->blocksize)
  2217. return -EINVAL;
  2218. if (end < MAIN_BLKADDR(sbi))
  2219. goto out;
  2220. if (is_sbi_flag_set(sbi, SBI_NEED_FSCK)) {
  2221. f2fs_msg(sbi->sb, KERN_WARNING,
  2222. "Found FS corruption, run fsck to fix.");
  2223. return -EFSCORRUPTED;
  2224. }
  2225. /* start/end segment number in main_area */
  2226. start_segno = (start <= MAIN_BLKADDR(sbi)) ? 0 : GET_SEGNO(sbi, start);
  2227. end_segno = (end >= MAX_BLKADDR(sbi)) ? MAIN_SEGS(sbi) - 1 :
  2228. GET_SEGNO(sbi, end);
  2229. if (need_align) {
  2230. start_segno = rounddown(start_segno, sbi->segs_per_sec);
  2231. end_segno = roundup(end_segno + 1, sbi->segs_per_sec) - 1;
  2232. }
  2233. cpc.reason = CP_DISCARD;
  2234. cpc.trim_minlen = max_t(__u64, 1, F2FS_BYTES_TO_BLK(range->minlen));
  2235. cpc.trim_start = start_segno;
  2236. cpc.trim_end = end_segno;
  2237. if (sbi->discard_blks == 0)
  2238. goto out;
  2239. mutex_lock(&sbi->gc_mutex);
  2240. err = f2fs_write_checkpoint(sbi, &cpc);
  2241. mutex_unlock(&sbi->gc_mutex);
  2242. if (err)
  2243. goto out;
  2244. /*
  2245. * We filed discard candidates, but actually we don't need to wait for
  2246. * all of them, since they'll be issued in idle time along with runtime
  2247. * discard option. User configuration looks like using runtime discard
  2248. * or periodic fstrim instead of it.
  2249. */
  2250. if (f2fs_realtime_discard_enable(sbi))
  2251. goto out;
  2252. start_block = START_BLOCK(sbi, start_segno);
  2253. end_block = START_BLOCK(sbi, end_segno + 1);
  2254. __init_discard_policy(sbi, &dpolicy, DPOLICY_FSTRIM, cpc.trim_minlen);
  2255. trimmed = __issue_discard_cmd_range(sbi, &dpolicy,
  2256. start_block, end_block);
  2257. trimmed += __wait_discard_cmd_range(sbi, &dpolicy,
  2258. start_block, end_block);
  2259. out:
  2260. if (!err)
  2261. range->len = F2FS_BLK_TO_BYTES(trimmed);
  2262. return err;
  2263. }
  2264. static bool __has_curseg_space(struct f2fs_sb_info *sbi, int type)
  2265. {
  2266. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2267. if (curseg->next_blkoff < sbi->blocks_per_seg)
  2268. return true;
  2269. return false;
  2270. }
  2271. int f2fs_rw_hint_to_seg_type(enum rw_hint hint)
  2272. {
  2273. switch (hint) {
  2274. case WRITE_LIFE_SHORT:
  2275. return CURSEG_HOT_DATA;
  2276. case WRITE_LIFE_EXTREME:
  2277. return CURSEG_COLD_DATA;
  2278. default:
  2279. return CURSEG_WARM_DATA;
  2280. }
  2281. }
  2282. /* This returns write hints for each segment type. This hints will be
  2283. * passed down to block layer. There are mapping tables which depend on
  2284. * the mount option 'whint_mode'.
  2285. *
  2286. * 1) whint_mode=off. F2FS only passes down WRITE_LIFE_NOT_SET.
  2287. *
  2288. * 2) whint_mode=user-based. F2FS tries to pass down hints given by users.
  2289. *
  2290. * User F2FS Block
  2291. * ---- ---- -----
  2292. * META WRITE_LIFE_NOT_SET
  2293. * HOT_NODE "
  2294. * WARM_NODE "
  2295. * COLD_NODE "
  2296. * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
  2297. * extension list " "
  2298. *
  2299. * -- buffered io
  2300. * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
  2301. * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
  2302. * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
  2303. * WRITE_LIFE_NONE " "
  2304. * WRITE_LIFE_MEDIUM " "
  2305. * WRITE_LIFE_LONG " "
  2306. *
  2307. * -- direct io
  2308. * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
  2309. * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
  2310. * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
  2311. * WRITE_LIFE_NONE " WRITE_LIFE_NONE
  2312. * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
  2313. * WRITE_LIFE_LONG " WRITE_LIFE_LONG
  2314. *
  2315. * 3) whint_mode=fs-based. F2FS passes down hints with its policy.
  2316. *
  2317. * User F2FS Block
  2318. * ---- ---- -----
  2319. * META WRITE_LIFE_MEDIUM;
  2320. * HOT_NODE WRITE_LIFE_NOT_SET
  2321. * WARM_NODE "
  2322. * COLD_NODE WRITE_LIFE_NONE
  2323. * ioctl(COLD) COLD_DATA WRITE_LIFE_EXTREME
  2324. * extension list " "
  2325. *
  2326. * -- buffered io
  2327. * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
  2328. * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
  2329. * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_LONG
  2330. * WRITE_LIFE_NONE " "
  2331. * WRITE_LIFE_MEDIUM " "
  2332. * WRITE_LIFE_LONG " "
  2333. *
  2334. * -- direct io
  2335. * WRITE_LIFE_EXTREME COLD_DATA WRITE_LIFE_EXTREME
  2336. * WRITE_LIFE_SHORT HOT_DATA WRITE_LIFE_SHORT
  2337. * WRITE_LIFE_NOT_SET WARM_DATA WRITE_LIFE_NOT_SET
  2338. * WRITE_LIFE_NONE " WRITE_LIFE_NONE
  2339. * WRITE_LIFE_MEDIUM " WRITE_LIFE_MEDIUM
  2340. * WRITE_LIFE_LONG " WRITE_LIFE_LONG
  2341. */
  2342. enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
  2343. enum page_type type, enum temp_type temp)
  2344. {
  2345. if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_USER) {
  2346. if (type == DATA) {
  2347. if (temp == WARM)
  2348. return WRITE_LIFE_NOT_SET;
  2349. else if (temp == HOT)
  2350. return WRITE_LIFE_SHORT;
  2351. else if (temp == COLD)
  2352. return WRITE_LIFE_EXTREME;
  2353. } else {
  2354. return WRITE_LIFE_NOT_SET;
  2355. }
  2356. } else if (F2FS_OPTION(sbi).whint_mode == WHINT_MODE_FS) {
  2357. if (type == DATA) {
  2358. if (temp == WARM)
  2359. return WRITE_LIFE_LONG;
  2360. else if (temp == HOT)
  2361. return WRITE_LIFE_SHORT;
  2362. else if (temp == COLD)
  2363. return WRITE_LIFE_EXTREME;
  2364. } else if (type == NODE) {
  2365. if (temp == WARM || temp == HOT)
  2366. return WRITE_LIFE_NOT_SET;
  2367. else if (temp == COLD)
  2368. return WRITE_LIFE_NONE;
  2369. } else if (type == META) {
  2370. return WRITE_LIFE_MEDIUM;
  2371. }
  2372. }
  2373. return WRITE_LIFE_NOT_SET;
  2374. }
  2375. static int __get_segment_type_2(struct f2fs_io_info *fio)
  2376. {
  2377. if (fio->type == DATA)
  2378. return CURSEG_HOT_DATA;
  2379. else
  2380. return CURSEG_HOT_NODE;
  2381. }
  2382. static int __get_segment_type_4(struct f2fs_io_info *fio)
  2383. {
  2384. if (fio->type == DATA) {
  2385. struct inode *inode = fio->page->mapping->host;
  2386. if (S_ISDIR(inode->i_mode))
  2387. return CURSEG_HOT_DATA;
  2388. else
  2389. return CURSEG_COLD_DATA;
  2390. } else {
  2391. if (IS_DNODE(fio->page) && is_cold_node(fio->page))
  2392. return CURSEG_WARM_NODE;
  2393. else
  2394. return CURSEG_COLD_NODE;
  2395. }
  2396. }
  2397. static int __get_segment_type_6(struct f2fs_io_info *fio)
  2398. {
  2399. if (fio->type == DATA) {
  2400. struct inode *inode = fio->page->mapping->host;
  2401. if (is_cold_data(fio->page) || file_is_cold(inode))
  2402. return CURSEG_COLD_DATA;
  2403. if (file_is_hot(inode) ||
  2404. is_inode_flag_set(inode, FI_HOT_DATA) ||
  2405. f2fs_is_atomic_file(inode) ||
  2406. f2fs_is_volatile_file(inode))
  2407. return CURSEG_HOT_DATA;
  2408. return f2fs_rw_hint_to_seg_type(inode->i_write_hint);
  2409. } else {
  2410. if (IS_DNODE(fio->page))
  2411. return is_cold_node(fio->page) ? CURSEG_WARM_NODE :
  2412. CURSEG_HOT_NODE;
  2413. return CURSEG_COLD_NODE;
  2414. }
  2415. }
  2416. static int __get_segment_type(struct f2fs_io_info *fio)
  2417. {
  2418. int type = 0;
  2419. switch (F2FS_OPTION(fio->sbi).active_logs) {
  2420. case 2:
  2421. type = __get_segment_type_2(fio);
  2422. break;
  2423. case 4:
  2424. type = __get_segment_type_4(fio);
  2425. break;
  2426. case 6:
  2427. type = __get_segment_type_6(fio);
  2428. break;
  2429. default:
  2430. f2fs_bug_on(fio->sbi, true);
  2431. }
  2432. if (IS_HOT(type))
  2433. fio->temp = HOT;
  2434. else if (IS_WARM(type))
  2435. fio->temp = WARM;
  2436. else
  2437. fio->temp = COLD;
  2438. return type;
  2439. }
  2440. void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  2441. block_t old_blkaddr, block_t *new_blkaddr,
  2442. struct f2fs_summary *sum, int type,
  2443. struct f2fs_io_info *fio, bool add_list)
  2444. {
  2445. struct sit_info *sit_i = SIT_I(sbi);
  2446. struct curseg_info *curseg = CURSEG_I(sbi, type);
  2447. down_read(&SM_I(sbi)->curseg_lock);
  2448. mutex_lock(&curseg->curseg_mutex);
  2449. down_write(&sit_i->sentry_lock);
  2450. *new_blkaddr = NEXT_FREE_BLKADDR(sbi, curseg);
  2451. f2fs_wait_discard_bio(sbi, *new_blkaddr);
  2452. /*
  2453. * __add_sum_entry should be resided under the curseg_mutex
  2454. * because, this function updates a summary entry in the
  2455. * current summary block.
  2456. */
  2457. __add_sum_entry(sbi, type, sum);
  2458. __refresh_next_blkoff(sbi, curseg);
  2459. stat_inc_block_count(sbi, curseg);
  2460. /*
  2461. * SIT information should be updated before segment allocation,
  2462. * since SSR needs latest valid block information.
  2463. */
  2464. update_sit_entry(sbi, *new_blkaddr, 1);
  2465. if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
  2466. update_sit_entry(sbi, old_blkaddr, -1);
  2467. if (!__has_curseg_space(sbi, type))
  2468. sit_i->s_ops->allocate_segment(sbi, type, false);
  2469. /*
  2470. * segment dirty status should be updated after segment allocation,
  2471. * so we just need to update status only one time after previous
  2472. * segment being closed.
  2473. */
  2474. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  2475. locate_dirty_segment(sbi, GET_SEGNO(sbi, *new_blkaddr));
  2476. up_write(&sit_i->sentry_lock);
  2477. if (page && IS_NODESEG(type)) {
  2478. fill_node_footer_blkaddr(page, NEXT_FREE_BLKADDR(sbi, curseg));
  2479. f2fs_inode_chksum_set(sbi, page);
  2480. }
  2481. if (add_list) {
  2482. struct f2fs_bio_info *io;
  2483. INIT_LIST_HEAD(&fio->list);
  2484. fio->in_list = true;
  2485. fio->retry = false;
  2486. io = sbi->write_io[fio->type] + fio->temp;
  2487. spin_lock(&io->io_lock);
  2488. list_add_tail(&fio->list, &io->io_list);
  2489. spin_unlock(&io->io_lock);
  2490. }
  2491. mutex_unlock(&curseg->curseg_mutex);
  2492. up_read(&SM_I(sbi)->curseg_lock);
  2493. }
  2494. static void update_device_state(struct f2fs_io_info *fio)
  2495. {
  2496. struct f2fs_sb_info *sbi = fio->sbi;
  2497. unsigned int devidx;
  2498. if (!f2fs_is_multi_device(sbi))
  2499. return;
  2500. devidx = f2fs_target_device_index(sbi, fio->new_blkaddr);
  2501. /* update device state for fsync */
  2502. f2fs_set_dirty_device(sbi, fio->ino, devidx, FLUSH_INO);
  2503. /* update device state for checkpoint */
  2504. if (!f2fs_test_bit(devidx, (char *)&sbi->dirty_device)) {
  2505. spin_lock(&sbi->dev_lock);
  2506. f2fs_set_bit(devidx, (char *)&sbi->dirty_device);
  2507. spin_unlock(&sbi->dev_lock);
  2508. }
  2509. }
  2510. static void do_write_page(struct f2fs_summary *sum, struct f2fs_io_info *fio)
  2511. {
  2512. int type = __get_segment_type(fio);
  2513. bool keep_order = (test_opt(fio->sbi, LFS) && type == CURSEG_COLD_DATA);
  2514. if (keep_order)
  2515. down_read(&fio->sbi->io_order_lock);
  2516. reallocate:
  2517. f2fs_allocate_data_block(fio->sbi, fio->page, fio->old_blkaddr,
  2518. &fio->new_blkaddr, sum, type, fio, true);
  2519. if (GET_SEGNO(fio->sbi, fio->old_blkaddr) != NULL_SEGNO)
  2520. invalidate_mapping_pages(META_MAPPING(fio->sbi),
  2521. fio->old_blkaddr, fio->old_blkaddr);
  2522. /* writeout dirty page into bdev */
  2523. f2fs_submit_page_write(fio);
  2524. if (fio->retry) {
  2525. fio->old_blkaddr = fio->new_blkaddr;
  2526. goto reallocate;
  2527. }
  2528. update_device_state(fio);
  2529. if (keep_order)
  2530. up_read(&fio->sbi->io_order_lock);
  2531. }
  2532. void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
  2533. enum iostat_type io_type)
  2534. {
  2535. struct f2fs_io_info fio = {
  2536. .sbi = sbi,
  2537. .type = META,
  2538. .temp = HOT,
  2539. .op = REQ_OP_WRITE,
  2540. .op_flags = REQ_SYNC | REQ_META | REQ_PRIO,
  2541. .old_blkaddr = page->index,
  2542. .new_blkaddr = page->index,
  2543. .page = page,
  2544. .encrypted_page = NULL,
  2545. .in_list = false,
  2546. };
  2547. if (unlikely(page->index >= MAIN_BLKADDR(sbi)))
  2548. fio.op_flags &= ~REQ_META;
  2549. set_page_writeback(page);
  2550. ClearPageError(page);
  2551. f2fs_submit_page_write(&fio);
  2552. f2fs_update_iostat(sbi, io_type, F2FS_BLKSIZE);
  2553. }
  2554. void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio)
  2555. {
  2556. struct f2fs_summary sum;
  2557. set_summary(&sum, nid, 0, 0);
  2558. do_write_page(&sum, fio);
  2559. f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
  2560. }
  2561. void f2fs_outplace_write_data(struct dnode_of_data *dn,
  2562. struct f2fs_io_info *fio)
  2563. {
  2564. struct f2fs_sb_info *sbi = fio->sbi;
  2565. struct f2fs_summary sum;
  2566. f2fs_bug_on(sbi, dn->data_blkaddr == NULL_ADDR);
  2567. set_summary(&sum, dn->nid, dn->ofs_in_node, fio->version);
  2568. do_write_page(&sum, fio);
  2569. f2fs_update_data_blkaddr(dn, fio->new_blkaddr);
  2570. f2fs_update_iostat(sbi, fio->io_type, F2FS_BLKSIZE);
  2571. }
  2572. int f2fs_inplace_write_data(struct f2fs_io_info *fio)
  2573. {
  2574. int err;
  2575. struct f2fs_sb_info *sbi = fio->sbi;
  2576. unsigned int segno;
  2577. fio->new_blkaddr = fio->old_blkaddr;
  2578. /* i/o temperature is needed for passing down write hints */
  2579. __get_segment_type(fio);
  2580. segno = GET_SEGNO(sbi, fio->new_blkaddr);
  2581. if (!IS_DATASEG(get_seg_entry(sbi, segno)->type)) {
  2582. set_sbi_flag(sbi, SBI_NEED_FSCK);
  2583. return -EFSCORRUPTED;
  2584. }
  2585. stat_inc_inplace_blocks(fio->sbi);
  2586. err = f2fs_submit_page_bio(fio);
  2587. if (!err)
  2588. update_device_state(fio);
  2589. f2fs_update_iostat(fio->sbi, fio->io_type, F2FS_BLKSIZE);
  2590. return err;
  2591. }
  2592. static inline int __f2fs_get_curseg(struct f2fs_sb_info *sbi,
  2593. unsigned int segno)
  2594. {
  2595. int i;
  2596. for (i = CURSEG_HOT_DATA; i < NO_CHECK_TYPE; i++) {
  2597. if (CURSEG_I(sbi, i)->segno == segno)
  2598. break;
  2599. }
  2600. return i;
  2601. }
  2602. void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  2603. block_t old_blkaddr, block_t new_blkaddr,
  2604. bool recover_curseg, bool recover_newaddr)
  2605. {
  2606. struct sit_info *sit_i = SIT_I(sbi);
  2607. struct curseg_info *curseg;
  2608. unsigned int segno, old_cursegno;
  2609. struct seg_entry *se;
  2610. int type;
  2611. unsigned short old_blkoff;
  2612. segno = GET_SEGNO(sbi, new_blkaddr);
  2613. se = get_seg_entry(sbi, segno);
  2614. type = se->type;
  2615. down_write(&SM_I(sbi)->curseg_lock);
  2616. if (!recover_curseg) {
  2617. /* for recovery flow */
  2618. if (se->valid_blocks == 0 && !IS_CURSEG(sbi, segno)) {
  2619. if (old_blkaddr == NULL_ADDR)
  2620. type = CURSEG_COLD_DATA;
  2621. else
  2622. type = CURSEG_WARM_DATA;
  2623. }
  2624. } else {
  2625. if (IS_CURSEG(sbi, segno)) {
  2626. /* se->type is volatile as SSR allocation */
  2627. type = __f2fs_get_curseg(sbi, segno);
  2628. f2fs_bug_on(sbi, type == NO_CHECK_TYPE);
  2629. } else {
  2630. type = CURSEG_WARM_DATA;
  2631. }
  2632. }
  2633. f2fs_bug_on(sbi, !IS_DATASEG(type));
  2634. curseg = CURSEG_I(sbi, type);
  2635. mutex_lock(&curseg->curseg_mutex);
  2636. down_write(&sit_i->sentry_lock);
  2637. old_cursegno = curseg->segno;
  2638. old_blkoff = curseg->next_blkoff;
  2639. /* change the current segment */
  2640. if (segno != curseg->segno) {
  2641. curseg->next_segno = segno;
  2642. change_curseg(sbi, type);
  2643. }
  2644. curseg->next_blkoff = GET_BLKOFF_FROM_SEG0(sbi, new_blkaddr);
  2645. __add_sum_entry(sbi, type, sum);
  2646. if (!recover_curseg || recover_newaddr)
  2647. update_sit_entry(sbi, new_blkaddr, 1);
  2648. if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO) {
  2649. invalidate_mapping_pages(META_MAPPING(sbi),
  2650. old_blkaddr, old_blkaddr);
  2651. update_sit_entry(sbi, old_blkaddr, -1);
  2652. }
  2653. locate_dirty_segment(sbi, GET_SEGNO(sbi, old_blkaddr));
  2654. locate_dirty_segment(sbi, GET_SEGNO(sbi, new_blkaddr));
  2655. locate_dirty_segment(sbi, old_cursegno);
  2656. if (recover_curseg) {
  2657. if (old_cursegno != curseg->segno) {
  2658. curseg->next_segno = old_cursegno;
  2659. change_curseg(sbi, type);
  2660. }
  2661. curseg->next_blkoff = old_blkoff;
  2662. }
  2663. up_write(&sit_i->sentry_lock);
  2664. mutex_unlock(&curseg->curseg_mutex);
  2665. up_write(&SM_I(sbi)->curseg_lock);
  2666. }
  2667. void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
  2668. block_t old_addr, block_t new_addr,
  2669. unsigned char version, bool recover_curseg,
  2670. bool recover_newaddr)
  2671. {
  2672. struct f2fs_summary sum;
  2673. set_summary(&sum, dn->nid, dn->ofs_in_node, version);
  2674. f2fs_do_replace_block(sbi, &sum, old_addr, new_addr,
  2675. recover_curseg, recover_newaddr);
  2676. f2fs_update_data_blkaddr(dn, new_addr);
  2677. }
  2678. void f2fs_wait_on_page_writeback(struct page *page,
  2679. enum page_type type, bool ordered)
  2680. {
  2681. if (PageWriteback(page)) {
  2682. struct f2fs_sb_info *sbi = F2FS_P_SB(page);
  2683. f2fs_submit_merged_write_cond(sbi, page->mapping->host,
  2684. 0, page->index, type);
  2685. if (ordered)
  2686. wait_on_page_writeback(page);
  2687. else
  2688. wait_for_stable_page(page);
  2689. }
  2690. }
  2691. void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr)
  2692. {
  2693. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2694. struct page *cpage;
  2695. if (!f2fs_post_read_required(inode))
  2696. return;
  2697. if (!is_valid_data_blkaddr(sbi, blkaddr))
  2698. return;
  2699. cpage = find_lock_page(META_MAPPING(sbi), blkaddr);
  2700. if (cpage) {
  2701. f2fs_wait_on_page_writeback(cpage, DATA, true);
  2702. f2fs_put_page(cpage, 1);
  2703. }
  2704. }
  2705. static int read_compacted_summaries(struct f2fs_sb_info *sbi)
  2706. {
  2707. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2708. struct curseg_info *seg_i;
  2709. unsigned char *kaddr;
  2710. struct page *page;
  2711. block_t start;
  2712. int i, j, offset;
  2713. start = start_sum_block(sbi);
  2714. page = f2fs_get_meta_page(sbi, start++);
  2715. if (IS_ERR(page))
  2716. return PTR_ERR(page);
  2717. kaddr = (unsigned char *)page_address(page);
  2718. /* Step 1: restore nat cache */
  2719. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2720. memcpy(seg_i->journal, kaddr, SUM_JOURNAL_SIZE);
  2721. /* Step 2: restore sit cache */
  2722. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  2723. memcpy(seg_i->journal, kaddr + SUM_JOURNAL_SIZE, SUM_JOURNAL_SIZE);
  2724. offset = 2 * SUM_JOURNAL_SIZE;
  2725. /* Step 3: restore summary entries */
  2726. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  2727. unsigned short blk_off;
  2728. unsigned int segno;
  2729. seg_i = CURSEG_I(sbi, i);
  2730. segno = le32_to_cpu(ckpt->cur_data_segno[i]);
  2731. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[i]);
  2732. seg_i->next_segno = segno;
  2733. reset_curseg(sbi, i, 0);
  2734. seg_i->alloc_type = ckpt->alloc_type[i];
  2735. seg_i->next_blkoff = blk_off;
  2736. if (seg_i->alloc_type == SSR)
  2737. blk_off = sbi->blocks_per_seg;
  2738. for (j = 0; j < blk_off; j++) {
  2739. struct f2fs_summary *s;
  2740. s = (struct f2fs_summary *)(kaddr + offset);
  2741. seg_i->sum_blk->entries[j] = *s;
  2742. offset += SUMMARY_SIZE;
  2743. if (offset + SUMMARY_SIZE <= PAGE_SIZE -
  2744. SUM_FOOTER_SIZE)
  2745. continue;
  2746. f2fs_put_page(page, 1);
  2747. page = NULL;
  2748. page = f2fs_get_meta_page(sbi, start++);
  2749. if (IS_ERR(page))
  2750. return PTR_ERR(page);
  2751. kaddr = (unsigned char *)page_address(page);
  2752. offset = 0;
  2753. }
  2754. }
  2755. f2fs_put_page(page, 1);
  2756. return 0;
  2757. }
  2758. static int read_normal_summaries(struct f2fs_sb_info *sbi, int type)
  2759. {
  2760. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2761. struct f2fs_summary_block *sum;
  2762. struct curseg_info *curseg;
  2763. struct page *new;
  2764. unsigned short blk_off;
  2765. unsigned int segno = 0;
  2766. block_t blk_addr = 0;
  2767. int err = 0;
  2768. /* get segment number and block addr */
  2769. if (IS_DATASEG(type)) {
  2770. segno = le32_to_cpu(ckpt->cur_data_segno[type]);
  2771. blk_off = le16_to_cpu(ckpt->cur_data_blkoff[type -
  2772. CURSEG_HOT_DATA]);
  2773. if (__exist_node_summaries(sbi))
  2774. blk_addr = sum_blk_addr(sbi, NR_CURSEG_TYPE, type);
  2775. else
  2776. blk_addr = sum_blk_addr(sbi, NR_CURSEG_DATA_TYPE, type);
  2777. } else {
  2778. segno = le32_to_cpu(ckpt->cur_node_segno[type -
  2779. CURSEG_HOT_NODE]);
  2780. blk_off = le16_to_cpu(ckpt->cur_node_blkoff[type -
  2781. CURSEG_HOT_NODE]);
  2782. if (__exist_node_summaries(sbi))
  2783. blk_addr = sum_blk_addr(sbi, NR_CURSEG_NODE_TYPE,
  2784. type - CURSEG_HOT_NODE);
  2785. else
  2786. blk_addr = GET_SUM_BLOCK(sbi, segno);
  2787. }
  2788. new = f2fs_get_meta_page(sbi, blk_addr);
  2789. if (IS_ERR(new))
  2790. return PTR_ERR(new);
  2791. sum = (struct f2fs_summary_block *)page_address(new);
  2792. if (IS_NODESEG(type)) {
  2793. if (__exist_node_summaries(sbi)) {
  2794. struct f2fs_summary *ns = &sum->entries[0];
  2795. int i;
  2796. for (i = 0; i < sbi->blocks_per_seg; i++, ns++) {
  2797. ns->version = 0;
  2798. ns->ofs_in_node = 0;
  2799. }
  2800. } else {
  2801. err = f2fs_restore_node_summary(sbi, segno, sum);
  2802. if (err)
  2803. goto out;
  2804. }
  2805. }
  2806. /* set uncompleted segment to curseg */
  2807. curseg = CURSEG_I(sbi, type);
  2808. mutex_lock(&curseg->curseg_mutex);
  2809. /* update journal info */
  2810. down_write(&curseg->journal_rwsem);
  2811. memcpy(curseg->journal, &sum->journal, SUM_JOURNAL_SIZE);
  2812. up_write(&curseg->journal_rwsem);
  2813. memcpy(curseg->sum_blk->entries, sum->entries, SUM_ENTRY_SIZE);
  2814. memcpy(&curseg->sum_blk->footer, &sum->footer, SUM_FOOTER_SIZE);
  2815. curseg->next_segno = segno;
  2816. reset_curseg(sbi, type, 0);
  2817. curseg->alloc_type = ckpt->alloc_type[type];
  2818. curseg->next_blkoff = blk_off;
  2819. mutex_unlock(&curseg->curseg_mutex);
  2820. out:
  2821. f2fs_put_page(new, 1);
  2822. return err;
  2823. }
  2824. static int restore_curseg_summaries(struct f2fs_sb_info *sbi)
  2825. {
  2826. struct f2fs_journal *sit_j = CURSEG_I(sbi, CURSEG_COLD_DATA)->journal;
  2827. struct f2fs_journal *nat_j = CURSEG_I(sbi, CURSEG_HOT_DATA)->journal;
  2828. int type = CURSEG_HOT_DATA;
  2829. int err;
  2830. if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG)) {
  2831. int npages = f2fs_npages_for_summary_flush(sbi, true);
  2832. if (npages >= 2)
  2833. f2fs_ra_meta_pages(sbi, start_sum_block(sbi), npages,
  2834. META_CP, true);
  2835. /* restore for compacted data summary */
  2836. err = read_compacted_summaries(sbi);
  2837. if (err)
  2838. return err;
  2839. type = CURSEG_HOT_NODE;
  2840. }
  2841. if (__exist_node_summaries(sbi))
  2842. f2fs_ra_meta_pages(sbi, sum_blk_addr(sbi, NR_CURSEG_TYPE, type),
  2843. NR_CURSEG_TYPE - type, META_CP, true);
  2844. for (; type <= CURSEG_COLD_NODE; type++) {
  2845. err = read_normal_summaries(sbi, type);
  2846. if (err)
  2847. return err;
  2848. }
  2849. /* sanity check for summary blocks */
  2850. if (nats_in_cursum(nat_j) > NAT_JOURNAL_ENTRIES ||
  2851. sits_in_cursum(sit_j) > SIT_JOURNAL_ENTRIES)
  2852. return -EINVAL;
  2853. return 0;
  2854. }
  2855. static void write_compacted_summaries(struct f2fs_sb_info *sbi, block_t blkaddr)
  2856. {
  2857. struct page *page;
  2858. unsigned char *kaddr;
  2859. struct f2fs_summary *summary;
  2860. struct curseg_info *seg_i;
  2861. int written_size = 0;
  2862. int i, j;
  2863. page = f2fs_grab_meta_page(sbi, blkaddr++);
  2864. kaddr = (unsigned char *)page_address(page);
  2865. memset(kaddr, 0, PAGE_SIZE);
  2866. /* Step 1: write nat cache */
  2867. seg_i = CURSEG_I(sbi, CURSEG_HOT_DATA);
  2868. memcpy(kaddr, seg_i->journal, SUM_JOURNAL_SIZE);
  2869. written_size += SUM_JOURNAL_SIZE;
  2870. /* Step 2: write sit cache */
  2871. seg_i = CURSEG_I(sbi, CURSEG_COLD_DATA);
  2872. memcpy(kaddr + written_size, seg_i->journal, SUM_JOURNAL_SIZE);
  2873. written_size += SUM_JOURNAL_SIZE;
  2874. /* Step 3: write summary entries */
  2875. for (i = CURSEG_HOT_DATA; i <= CURSEG_COLD_DATA; i++) {
  2876. unsigned short blkoff;
  2877. seg_i = CURSEG_I(sbi, i);
  2878. if (sbi->ckpt->alloc_type[i] == SSR)
  2879. blkoff = sbi->blocks_per_seg;
  2880. else
  2881. blkoff = curseg_blkoff(sbi, i);
  2882. for (j = 0; j < blkoff; j++) {
  2883. if (!page) {
  2884. page = f2fs_grab_meta_page(sbi, blkaddr++);
  2885. kaddr = (unsigned char *)page_address(page);
  2886. memset(kaddr, 0, PAGE_SIZE);
  2887. written_size = 0;
  2888. }
  2889. summary = (struct f2fs_summary *)(kaddr + written_size);
  2890. *summary = seg_i->sum_blk->entries[j];
  2891. written_size += SUMMARY_SIZE;
  2892. if (written_size + SUMMARY_SIZE <= PAGE_SIZE -
  2893. SUM_FOOTER_SIZE)
  2894. continue;
  2895. set_page_dirty(page);
  2896. f2fs_put_page(page, 1);
  2897. page = NULL;
  2898. }
  2899. }
  2900. if (page) {
  2901. set_page_dirty(page);
  2902. f2fs_put_page(page, 1);
  2903. }
  2904. }
  2905. static void write_normal_summaries(struct f2fs_sb_info *sbi,
  2906. block_t blkaddr, int type)
  2907. {
  2908. int i, end;
  2909. if (IS_DATASEG(type))
  2910. end = type + NR_CURSEG_DATA_TYPE;
  2911. else
  2912. end = type + NR_CURSEG_NODE_TYPE;
  2913. for (i = type; i < end; i++)
  2914. write_current_sum_page(sbi, i, blkaddr + (i - type));
  2915. }
  2916. void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  2917. {
  2918. if (is_set_ckpt_flags(sbi, CP_COMPACT_SUM_FLAG))
  2919. write_compacted_summaries(sbi, start_blk);
  2920. else
  2921. write_normal_summaries(sbi, start_blk, CURSEG_HOT_DATA);
  2922. }
  2923. void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk)
  2924. {
  2925. write_normal_summaries(sbi, start_blk, CURSEG_HOT_NODE);
  2926. }
  2927. int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
  2928. unsigned int val, int alloc)
  2929. {
  2930. int i;
  2931. if (type == NAT_JOURNAL) {
  2932. for (i = 0; i < nats_in_cursum(journal); i++) {
  2933. if (le32_to_cpu(nid_in_journal(journal, i)) == val)
  2934. return i;
  2935. }
  2936. if (alloc && __has_cursum_space(journal, 1, NAT_JOURNAL))
  2937. return update_nats_in_cursum(journal, 1);
  2938. } else if (type == SIT_JOURNAL) {
  2939. for (i = 0; i < sits_in_cursum(journal); i++)
  2940. if (le32_to_cpu(segno_in_journal(journal, i)) == val)
  2941. return i;
  2942. if (alloc && __has_cursum_space(journal, 1, SIT_JOURNAL))
  2943. return update_sits_in_cursum(journal, 1);
  2944. }
  2945. return -1;
  2946. }
  2947. static struct page *get_current_sit_page(struct f2fs_sb_info *sbi,
  2948. unsigned int segno)
  2949. {
  2950. return f2fs_get_meta_page_nofail(sbi, current_sit_addr(sbi, segno));
  2951. }
  2952. static struct page *get_next_sit_page(struct f2fs_sb_info *sbi,
  2953. unsigned int start)
  2954. {
  2955. struct sit_info *sit_i = SIT_I(sbi);
  2956. struct page *page;
  2957. pgoff_t src_off, dst_off;
  2958. src_off = current_sit_addr(sbi, start);
  2959. dst_off = next_sit_addr(sbi, src_off);
  2960. page = f2fs_grab_meta_page(sbi, dst_off);
  2961. seg_info_to_sit_page(sbi, page, start);
  2962. set_page_dirty(page);
  2963. set_to_next_sit(sit_i, start);
  2964. return page;
  2965. }
  2966. static struct sit_entry_set *grab_sit_entry_set(void)
  2967. {
  2968. struct sit_entry_set *ses =
  2969. f2fs_kmem_cache_alloc(sit_entry_set_slab, GFP_NOFS);
  2970. ses->entry_cnt = 0;
  2971. INIT_LIST_HEAD(&ses->set_list);
  2972. return ses;
  2973. }
  2974. static void release_sit_entry_set(struct sit_entry_set *ses)
  2975. {
  2976. list_del(&ses->set_list);
  2977. kmem_cache_free(sit_entry_set_slab, ses);
  2978. }
  2979. static void adjust_sit_entry_set(struct sit_entry_set *ses,
  2980. struct list_head *head)
  2981. {
  2982. struct sit_entry_set *next = ses;
  2983. if (list_is_last(&ses->set_list, head))
  2984. return;
  2985. list_for_each_entry_continue(next, head, set_list)
  2986. if (ses->entry_cnt <= next->entry_cnt)
  2987. break;
  2988. list_move_tail(&ses->set_list, &next->set_list);
  2989. }
  2990. static void add_sit_entry(unsigned int segno, struct list_head *head)
  2991. {
  2992. struct sit_entry_set *ses;
  2993. unsigned int start_segno = START_SEGNO(segno);
  2994. list_for_each_entry(ses, head, set_list) {
  2995. if (ses->start_segno == start_segno) {
  2996. ses->entry_cnt++;
  2997. adjust_sit_entry_set(ses, head);
  2998. return;
  2999. }
  3000. }
  3001. ses = grab_sit_entry_set();
  3002. ses->start_segno = start_segno;
  3003. ses->entry_cnt++;
  3004. list_add(&ses->set_list, head);
  3005. }
  3006. static void add_sits_in_set(struct f2fs_sb_info *sbi)
  3007. {
  3008. struct f2fs_sm_info *sm_info = SM_I(sbi);
  3009. struct list_head *set_list = &sm_info->sit_entry_set;
  3010. unsigned long *bitmap = SIT_I(sbi)->dirty_sentries_bitmap;
  3011. unsigned int segno;
  3012. for_each_set_bit(segno, bitmap, MAIN_SEGS(sbi))
  3013. add_sit_entry(segno, set_list);
  3014. }
  3015. static void remove_sits_in_journal(struct f2fs_sb_info *sbi)
  3016. {
  3017. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  3018. struct f2fs_journal *journal = curseg->journal;
  3019. int i;
  3020. down_write(&curseg->journal_rwsem);
  3021. for (i = 0; i < sits_in_cursum(journal); i++) {
  3022. unsigned int segno;
  3023. bool dirtied;
  3024. segno = le32_to_cpu(segno_in_journal(journal, i));
  3025. dirtied = __mark_sit_entry_dirty(sbi, segno);
  3026. if (!dirtied)
  3027. add_sit_entry(segno, &SM_I(sbi)->sit_entry_set);
  3028. }
  3029. update_sits_in_cursum(journal, -i);
  3030. up_write(&curseg->journal_rwsem);
  3031. }
  3032. /*
  3033. * CP calls this function, which flushes SIT entries including sit_journal,
  3034. * and moves prefree segs to free segs.
  3035. */
  3036. void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc)
  3037. {
  3038. struct sit_info *sit_i = SIT_I(sbi);
  3039. unsigned long *bitmap = sit_i->dirty_sentries_bitmap;
  3040. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  3041. struct f2fs_journal *journal = curseg->journal;
  3042. struct sit_entry_set *ses, *tmp;
  3043. struct list_head *head = &SM_I(sbi)->sit_entry_set;
  3044. bool to_journal = true;
  3045. struct seg_entry *se;
  3046. down_write(&sit_i->sentry_lock);
  3047. if (!sit_i->dirty_sentries)
  3048. goto out;
  3049. /*
  3050. * add and account sit entries of dirty bitmap in sit entry
  3051. * set temporarily
  3052. */
  3053. add_sits_in_set(sbi);
  3054. /*
  3055. * if there are no enough space in journal to store dirty sit
  3056. * entries, remove all entries from journal and add and account
  3057. * them in sit entry set.
  3058. */
  3059. if (!__has_cursum_space(journal, sit_i->dirty_sentries, SIT_JOURNAL))
  3060. remove_sits_in_journal(sbi);
  3061. /*
  3062. * there are two steps to flush sit entries:
  3063. * #1, flush sit entries to journal in current cold data summary block.
  3064. * #2, flush sit entries to sit page.
  3065. */
  3066. list_for_each_entry_safe(ses, tmp, head, set_list) {
  3067. struct page *page = NULL;
  3068. struct f2fs_sit_block *raw_sit = NULL;
  3069. unsigned int start_segno = ses->start_segno;
  3070. unsigned int end = min(start_segno + SIT_ENTRY_PER_BLOCK,
  3071. (unsigned long)MAIN_SEGS(sbi));
  3072. unsigned int segno = start_segno;
  3073. if (to_journal &&
  3074. !__has_cursum_space(journal, ses->entry_cnt, SIT_JOURNAL))
  3075. to_journal = false;
  3076. if (to_journal) {
  3077. down_write(&curseg->journal_rwsem);
  3078. } else {
  3079. page = get_next_sit_page(sbi, start_segno);
  3080. raw_sit = page_address(page);
  3081. }
  3082. /* flush dirty sit entries in region of current sit set */
  3083. for_each_set_bit_from(segno, bitmap, end) {
  3084. int offset, sit_offset;
  3085. se = get_seg_entry(sbi, segno);
  3086. #ifdef CONFIG_F2FS_CHECK_FS
  3087. if (memcmp(se->cur_valid_map, se->cur_valid_map_mir,
  3088. SIT_VBLOCK_MAP_SIZE))
  3089. f2fs_bug_on(sbi, 1);
  3090. #endif
  3091. /* add discard candidates */
  3092. if (!(cpc->reason & CP_DISCARD)) {
  3093. cpc->trim_start = segno;
  3094. add_discard_addrs(sbi, cpc, false);
  3095. }
  3096. if (to_journal) {
  3097. offset = f2fs_lookup_journal_in_cursum(journal,
  3098. SIT_JOURNAL, segno, 1);
  3099. f2fs_bug_on(sbi, offset < 0);
  3100. segno_in_journal(journal, offset) =
  3101. cpu_to_le32(segno);
  3102. seg_info_to_raw_sit(se,
  3103. &sit_in_journal(journal, offset));
  3104. check_block_count(sbi, segno,
  3105. &sit_in_journal(journal, offset));
  3106. } else {
  3107. sit_offset = SIT_ENTRY_OFFSET(sit_i, segno);
  3108. seg_info_to_raw_sit(se,
  3109. &raw_sit->entries[sit_offset]);
  3110. check_block_count(sbi, segno,
  3111. &raw_sit->entries[sit_offset]);
  3112. }
  3113. __clear_bit(segno, bitmap);
  3114. sit_i->dirty_sentries--;
  3115. ses->entry_cnt--;
  3116. }
  3117. if (to_journal)
  3118. up_write(&curseg->journal_rwsem);
  3119. else
  3120. f2fs_put_page(page, 1);
  3121. f2fs_bug_on(sbi, ses->entry_cnt);
  3122. release_sit_entry_set(ses);
  3123. }
  3124. f2fs_bug_on(sbi, !list_empty(head));
  3125. f2fs_bug_on(sbi, sit_i->dirty_sentries);
  3126. out:
  3127. if (cpc->reason & CP_DISCARD) {
  3128. __u64 trim_start = cpc->trim_start;
  3129. for (; cpc->trim_start <= cpc->trim_end; cpc->trim_start++)
  3130. add_discard_addrs(sbi, cpc, false);
  3131. cpc->trim_start = trim_start;
  3132. }
  3133. up_write(&sit_i->sentry_lock);
  3134. set_prefree_as_free_segments(sbi);
  3135. }
  3136. static int build_sit_info(struct f2fs_sb_info *sbi)
  3137. {
  3138. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  3139. struct sit_info *sit_i;
  3140. unsigned int sit_segs, start;
  3141. char *src_bitmap;
  3142. unsigned int bitmap_size;
  3143. /* allocate memory for SIT information */
  3144. sit_i = f2fs_kzalloc(sbi, sizeof(struct sit_info), GFP_KERNEL);
  3145. if (!sit_i)
  3146. return -ENOMEM;
  3147. SM_I(sbi)->sit_info = sit_i;
  3148. sit_i->sentries =
  3149. f2fs_kvzalloc(sbi, array_size(sizeof(struct seg_entry),
  3150. MAIN_SEGS(sbi)),
  3151. GFP_KERNEL);
  3152. if (!sit_i->sentries)
  3153. return -ENOMEM;
  3154. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  3155. sit_i->dirty_sentries_bitmap = f2fs_kvzalloc(sbi, bitmap_size,
  3156. GFP_KERNEL);
  3157. if (!sit_i->dirty_sentries_bitmap)
  3158. return -ENOMEM;
  3159. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  3160. sit_i->sentries[start].cur_valid_map
  3161. = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  3162. sit_i->sentries[start].ckpt_valid_map
  3163. = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  3164. if (!sit_i->sentries[start].cur_valid_map ||
  3165. !sit_i->sentries[start].ckpt_valid_map)
  3166. return -ENOMEM;
  3167. #ifdef CONFIG_F2FS_CHECK_FS
  3168. sit_i->sentries[start].cur_valid_map_mir
  3169. = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  3170. if (!sit_i->sentries[start].cur_valid_map_mir)
  3171. return -ENOMEM;
  3172. #endif
  3173. sit_i->sentries[start].discard_map
  3174. = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE,
  3175. GFP_KERNEL);
  3176. if (!sit_i->sentries[start].discard_map)
  3177. return -ENOMEM;
  3178. }
  3179. sit_i->tmp_map = f2fs_kzalloc(sbi, SIT_VBLOCK_MAP_SIZE, GFP_KERNEL);
  3180. if (!sit_i->tmp_map)
  3181. return -ENOMEM;
  3182. if (sbi->segs_per_sec > 1) {
  3183. sit_i->sec_entries =
  3184. f2fs_kvzalloc(sbi, array_size(sizeof(struct sec_entry),
  3185. MAIN_SECS(sbi)),
  3186. GFP_KERNEL);
  3187. if (!sit_i->sec_entries)
  3188. return -ENOMEM;
  3189. }
  3190. /* get information related with SIT */
  3191. sit_segs = le32_to_cpu(raw_super->segment_count_sit) >> 1;
  3192. /* setup SIT bitmap from ckeckpoint pack */
  3193. bitmap_size = __bitmap_size(sbi, SIT_BITMAP);
  3194. src_bitmap = __bitmap_ptr(sbi, SIT_BITMAP);
  3195. sit_i->sit_bitmap = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  3196. if (!sit_i->sit_bitmap)
  3197. return -ENOMEM;
  3198. #ifdef CONFIG_F2FS_CHECK_FS
  3199. sit_i->sit_bitmap_mir = kmemdup(src_bitmap, bitmap_size, GFP_KERNEL);
  3200. if (!sit_i->sit_bitmap_mir)
  3201. return -ENOMEM;
  3202. #endif
  3203. /* init SIT information */
  3204. sit_i->s_ops = &default_salloc_ops;
  3205. sit_i->sit_base_addr = le32_to_cpu(raw_super->sit_blkaddr);
  3206. sit_i->sit_blocks = sit_segs << sbi->log_blocks_per_seg;
  3207. sit_i->written_valid_blocks = 0;
  3208. sit_i->bitmap_size = bitmap_size;
  3209. sit_i->dirty_sentries = 0;
  3210. sit_i->sents_per_block = SIT_ENTRY_PER_BLOCK;
  3211. sit_i->elapsed_time = le64_to_cpu(sbi->ckpt->elapsed_time);
  3212. sit_i->mounted_time = ktime_get_real_seconds();
  3213. init_rwsem(&sit_i->sentry_lock);
  3214. return 0;
  3215. }
  3216. static int build_free_segmap(struct f2fs_sb_info *sbi)
  3217. {
  3218. struct free_segmap_info *free_i;
  3219. unsigned int bitmap_size, sec_bitmap_size;
  3220. /* allocate memory for free segmap information */
  3221. free_i = f2fs_kzalloc(sbi, sizeof(struct free_segmap_info), GFP_KERNEL);
  3222. if (!free_i)
  3223. return -ENOMEM;
  3224. SM_I(sbi)->free_info = free_i;
  3225. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  3226. free_i->free_segmap = f2fs_kvmalloc(sbi, bitmap_size, GFP_KERNEL);
  3227. if (!free_i->free_segmap)
  3228. return -ENOMEM;
  3229. sec_bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  3230. free_i->free_secmap = f2fs_kvmalloc(sbi, sec_bitmap_size, GFP_KERNEL);
  3231. if (!free_i->free_secmap)
  3232. return -ENOMEM;
  3233. /* set all segments as dirty temporarily */
  3234. memset(free_i->free_segmap, 0xff, bitmap_size);
  3235. memset(free_i->free_secmap, 0xff, sec_bitmap_size);
  3236. /* init free segmap information */
  3237. free_i->start_segno = GET_SEGNO_FROM_SEG0(sbi, MAIN_BLKADDR(sbi));
  3238. free_i->free_segments = 0;
  3239. free_i->free_sections = 0;
  3240. spin_lock_init(&free_i->segmap_lock);
  3241. return 0;
  3242. }
  3243. static int build_curseg(struct f2fs_sb_info *sbi)
  3244. {
  3245. struct curseg_info *array;
  3246. int i;
  3247. array = f2fs_kzalloc(sbi, array_size(NR_CURSEG_TYPE, sizeof(*array)),
  3248. GFP_KERNEL);
  3249. if (!array)
  3250. return -ENOMEM;
  3251. SM_I(sbi)->curseg_array = array;
  3252. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  3253. mutex_init(&array[i].curseg_mutex);
  3254. array[i].sum_blk = f2fs_kzalloc(sbi, PAGE_SIZE, GFP_KERNEL);
  3255. if (!array[i].sum_blk)
  3256. return -ENOMEM;
  3257. init_rwsem(&array[i].journal_rwsem);
  3258. array[i].journal = f2fs_kzalloc(sbi,
  3259. sizeof(struct f2fs_journal), GFP_KERNEL);
  3260. if (!array[i].journal)
  3261. return -ENOMEM;
  3262. array[i].segno = NULL_SEGNO;
  3263. array[i].next_blkoff = 0;
  3264. }
  3265. return restore_curseg_summaries(sbi);
  3266. }
  3267. static int build_sit_entries(struct f2fs_sb_info *sbi)
  3268. {
  3269. struct sit_info *sit_i = SIT_I(sbi);
  3270. struct curseg_info *curseg = CURSEG_I(sbi, CURSEG_COLD_DATA);
  3271. struct f2fs_journal *journal = curseg->journal;
  3272. struct seg_entry *se;
  3273. struct f2fs_sit_entry sit;
  3274. int sit_blk_cnt = SIT_BLK_CNT(sbi);
  3275. unsigned int i, start, end;
  3276. unsigned int readed, start_blk = 0;
  3277. int err = 0;
  3278. block_t total_node_blocks = 0;
  3279. do {
  3280. readed = f2fs_ra_meta_pages(sbi, start_blk, BIO_MAX_PAGES,
  3281. META_SIT, true);
  3282. start = start_blk * sit_i->sents_per_block;
  3283. end = (start_blk + readed) * sit_i->sents_per_block;
  3284. for (; start < end && start < MAIN_SEGS(sbi); start++) {
  3285. struct f2fs_sit_block *sit_blk;
  3286. struct page *page;
  3287. se = &sit_i->sentries[start];
  3288. page = get_current_sit_page(sbi, start);
  3289. sit_blk = (struct f2fs_sit_block *)page_address(page);
  3290. sit = sit_blk->entries[SIT_ENTRY_OFFSET(sit_i, start)];
  3291. f2fs_put_page(page, 1);
  3292. err = check_block_count(sbi, start, &sit);
  3293. if (err)
  3294. return err;
  3295. seg_info_from_raw_sit(se, &sit);
  3296. if (IS_NODESEG(se->type))
  3297. total_node_blocks += se->valid_blocks;
  3298. /* build discard map only one time */
  3299. if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
  3300. memset(se->discard_map, 0xff,
  3301. SIT_VBLOCK_MAP_SIZE);
  3302. } else {
  3303. memcpy(se->discard_map,
  3304. se->cur_valid_map,
  3305. SIT_VBLOCK_MAP_SIZE);
  3306. sbi->discard_blks +=
  3307. sbi->blocks_per_seg -
  3308. se->valid_blocks;
  3309. }
  3310. if (sbi->segs_per_sec > 1)
  3311. get_sec_entry(sbi, start)->valid_blocks +=
  3312. se->valid_blocks;
  3313. }
  3314. start_blk += readed;
  3315. } while (start_blk < sit_blk_cnt);
  3316. down_read(&curseg->journal_rwsem);
  3317. for (i = 0; i < sits_in_cursum(journal); i++) {
  3318. unsigned int old_valid_blocks;
  3319. start = le32_to_cpu(segno_in_journal(journal, i));
  3320. if (start >= MAIN_SEGS(sbi)) {
  3321. f2fs_msg(sbi->sb, KERN_ERR,
  3322. "Wrong journal entry on segno %u",
  3323. start);
  3324. set_sbi_flag(sbi, SBI_NEED_FSCK);
  3325. err = -EFSCORRUPTED;
  3326. break;
  3327. }
  3328. se = &sit_i->sentries[start];
  3329. sit = sit_in_journal(journal, i);
  3330. old_valid_blocks = se->valid_blocks;
  3331. if (IS_NODESEG(se->type))
  3332. total_node_blocks -= old_valid_blocks;
  3333. err = check_block_count(sbi, start, &sit);
  3334. if (err)
  3335. break;
  3336. seg_info_from_raw_sit(se, &sit);
  3337. if (IS_NODESEG(se->type))
  3338. total_node_blocks += se->valid_blocks;
  3339. if (is_set_ckpt_flags(sbi, CP_TRIMMED_FLAG)) {
  3340. memset(se->discard_map, 0xff, SIT_VBLOCK_MAP_SIZE);
  3341. } else {
  3342. memcpy(se->discard_map, se->cur_valid_map,
  3343. SIT_VBLOCK_MAP_SIZE);
  3344. sbi->discard_blks += old_valid_blocks;
  3345. sbi->discard_blks -= se->valid_blocks;
  3346. }
  3347. if (sbi->segs_per_sec > 1) {
  3348. get_sec_entry(sbi, start)->valid_blocks +=
  3349. se->valid_blocks;
  3350. get_sec_entry(sbi, start)->valid_blocks -=
  3351. old_valid_blocks;
  3352. }
  3353. }
  3354. up_read(&curseg->journal_rwsem);
  3355. if (!err && total_node_blocks != valid_node_count(sbi)) {
  3356. f2fs_msg(sbi->sb, KERN_ERR,
  3357. "SIT is corrupted node# %u vs %u",
  3358. total_node_blocks, valid_node_count(sbi));
  3359. set_sbi_flag(sbi, SBI_NEED_FSCK);
  3360. err = -EFSCORRUPTED;
  3361. }
  3362. return err;
  3363. }
  3364. static void init_free_segmap(struct f2fs_sb_info *sbi)
  3365. {
  3366. unsigned int start;
  3367. int type;
  3368. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  3369. struct seg_entry *sentry = get_seg_entry(sbi, start);
  3370. if (!sentry->valid_blocks)
  3371. __set_free(sbi, start);
  3372. else
  3373. SIT_I(sbi)->written_valid_blocks +=
  3374. sentry->valid_blocks;
  3375. }
  3376. /* set use the current segments */
  3377. for (type = CURSEG_HOT_DATA; type <= CURSEG_COLD_NODE; type++) {
  3378. struct curseg_info *curseg_t = CURSEG_I(sbi, type);
  3379. __set_test_and_inuse(sbi, curseg_t->segno);
  3380. }
  3381. }
  3382. static void init_dirty_segmap(struct f2fs_sb_info *sbi)
  3383. {
  3384. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  3385. struct free_segmap_info *free_i = FREE_I(sbi);
  3386. unsigned int segno = 0, offset = 0;
  3387. unsigned short valid_blocks;
  3388. while (1) {
  3389. /* find dirty segment based on free segmap */
  3390. segno = find_next_inuse(free_i, MAIN_SEGS(sbi), offset);
  3391. if (segno >= MAIN_SEGS(sbi))
  3392. break;
  3393. offset = segno + 1;
  3394. valid_blocks = get_valid_blocks(sbi, segno, false);
  3395. if (valid_blocks == sbi->blocks_per_seg || !valid_blocks)
  3396. continue;
  3397. if (valid_blocks > sbi->blocks_per_seg) {
  3398. f2fs_bug_on(sbi, 1);
  3399. continue;
  3400. }
  3401. mutex_lock(&dirty_i->seglist_lock);
  3402. __locate_dirty_segment(sbi, segno, DIRTY);
  3403. mutex_unlock(&dirty_i->seglist_lock);
  3404. }
  3405. }
  3406. static int init_victim_secmap(struct f2fs_sb_info *sbi)
  3407. {
  3408. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  3409. unsigned int bitmap_size = f2fs_bitmap_size(MAIN_SECS(sbi));
  3410. dirty_i->victim_secmap = f2fs_kvzalloc(sbi, bitmap_size, GFP_KERNEL);
  3411. if (!dirty_i->victim_secmap)
  3412. return -ENOMEM;
  3413. return 0;
  3414. }
  3415. static int build_dirty_segmap(struct f2fs_sb_info *sbi)
  3416. {
  3417. struct dirty_seglist_info *dirty_i;
  3418. unsigned int bitmap_size, i;
  3419. /* allocate memory for dirty segments list information */
  3420. dirty_i = f2fs_kzalloc(sbi, sizeof(struct dirty_seglist_info),
  3421. GFP_KERNEL);
  3422. if (!dirty_i)
  3423. return -ENOMEM;
  3424. SM_I(sbi)->dirty_info = dirty_i;
  3425. mutex_init(&dirty_i->seglist_lock);
  3426. bitmap_size = f2fs_bitmap_size(MAIN_SEGS(sbi));
  3427. for (i = 0; i < NR_DIRTY_TYPE; i++) {
  3428. dirty_i->dirty_segmap[i] = f2fs_kvzalloc(sbi, bitmap_size,
  3429. GFP_KERNEL);
  3430. if (!dirty_i->dirty_segmap[i])
  3431. return -ENOMEM;
  3432. }
  3433. init_dirty_segmap(sbi);
  3434. return init_victim_secmap(sbi);
  3435. }
  3436. static int sanity_check_curseg(struct f2fs_sb_info *sbi)
  3437. {
  3438. int i;
  3439. /*
  3440. * In LFS/SSR curseg, .next_blkoff should point to an unused blkaddr;
  3441. * In LFS curseg, all blkaddr after .next_blkoff should be unused.
  3442. */
  3443. for (i = 0; i < NO_CHECK_TYPE; i++) {
  3444. struct curseg_info *curseg = CURSEG_I(sbi, i);
  3445. struct seg_entry *se = get_seg_entry(sbi, curseg->segno);
  3446. unsigned int blkofs = curseg->next_blkoff;
  3447. if (f2fs_test_bit(blkofs, se->cur_valid_map))
  3448. goto out;
  3449. if (curseg->alloc_type == SSR)
  3450. continue;
  3451. for (blkofs += 1; blkofs < sbi->blocks_per_seg; blkofs++) {
  3452. if (!f2fs_test_bit(blkofs, se->cur_valid_map))
  3453. continue;
  3454. out:
  3455. f2fs_msg(sbi->sb, KERN_ERR,
  3456. "Current segment's next free block offset is "
  3457. "inconsistent with bitmap, logtype:%u, "
  3458. "segno:%u, type:%u, next_blkoff:%u, blkofs:%u",
  3459. i, curseg->segno, curseg->alloc_type,
  3460. curseg->next_blkoff, blkofs);
  3461. return -EFSCORRUPTED;
  3462. }
  3463. }
  3464. return 0;
  3465. }
  3466. /*
  3467. * Update min, max modified time for cost-benefit GC algorithm
  3468. */
  3469. static void init_min_max_mtime(struct f2fs_sb_info *sbi)
  3470. {
  3471. struct sit_info *sit_i = SIT_I(sbi);
  3472. unsigned int segno;
  3473. down_write(&sit_i->sentry_lock);
  3474. sit_i->min_mtime = ULLONG_MAX;
  3475. for (segno = 0; segno < MAIN_SEGS(sbi); segno += sbi->segs_per_sec) {
  3476. unsigned int i;
  3477. unsigned long long mtime = 0;
  3478. for (i = 0; i < sbi->segs_per_sec; i++)
  3479. mtime += get_seg_entry(sbi, segno + i)->mtime;
  3480. mtime = div_u64(mtime, sbi->segs_per_sec);
  3481. if (sit_i->min_mtime > mtime)
  3482. sit_i->min_mtime = mtime;
  3483. }
  3484. sit_i->max_mtime = get_mtime(sbi, false);
  3485. up_write(&sit_i->sentry_lock);
  3486. }
  3487. int f2fs_build_segment_manager(struct f2fs_sb_info *sbi)
  3488. {
  3489. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  3490. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  3491. struct f2fs_sm_info *sm_info;
  3492. int err;
  3493. sm_info = f2fs_kzalloc(sbi, sizeof(struct f2fs_sm_info), GFP_KERNEL);
  3494. if (!sm_info)
  3495. return -ENOMEM;
  3496. /* init sm info */
  3497. sbi->sm_info = sm_info;
  3498. sm_info->seg0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  3499. sm_info->main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  3500. sm_info->segment_count = le32_to_cpu(raw_super->segment_count);
  3501. sm_info->reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  3502. sm_info->ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  3503. sm_info->main_segments = le32_to_cpu(raw_super->segment_count_main);
  3504. sm_info->ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  3505. sm_info->rec_prefree_segments = sm_info->main_segments *
  3506. DEF_RECLAIM_PREFREE_SEGMENTS / 100;
  3507. if (sm_info->rec_prefree_segments > DEF_MAX_RECLAIM_PREFREE_SEGMENTS)
  3508. sm_info->rec_prefree_segments = DEF_MAX_RECLAIM_PREFREE_SEGMENTS;
  3509. if (!test_opt(sbi, LFS))
  3510. sm_info->ipu_policy = 1 << F2FS_IPU_FSYNC;
  3511. sm_info->min_ipu_util = DEF_MIN_IPU_UTIL;
  3512. sm_info->min_fsync_blocks = DEF_MIN_FSYNC_BLOCKS;
  3513. sm_info->min_seq_blocks = sbi->blocks_per_seg * sbi->segs_per_sec;
  3514. sm_info->min_hot_blocks = DEF_MIN_HOT_BLOCKS;
  3515. sm_info->min_ssr_sections = reserved_sections(sbi);
  3516. INIT_LIST_HEAD(&sm_info->sit_entry_set);
  3517. init_rwsem(&sm_info->curseg_lock);
  3518. if (!f2fs_readonly(sbi->sb)) {
  3519. err = f2fs_create_flush_cmd_control(sbi);
  3520. if (err)
  3521. return err;
  3522. }
  3523. err = create_discard_cmd_control(sbi);
  3524. if (err)
  3525. return err;
  3526. err = build_sit_info(sbi);
  3527. if (err)
  3528. return err;
  3529. err = build_free_segmap(sbi);
  3530. if (err)
  3531. return err;
  3532. err = build_curseg(sbi);
  3533. if (err)
  3534. return err;
  3535. /* reinit free segmap based on SIT */
  3536. err = build_sit_entries(sbi);
  3537. if (err)
  3538. return err;
  3539. init_free_segmap(sbi);
  3540. err = build_dirty_segmap(sbi);
  3541. if (err)
  3542. return err;
  3543. err = sanity_check_curseg(sbi);
  3544. if (err)
  3545. return err;
  3546. init_min_max_mtime(sbi);
  3547. return 0;
  3548. }
  3549. static void discard_dirty_segmap(struct f2fs_sb_info *sbi,
  3550. enum dirty_type dirty_type)
  3551. {
  3552. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  3553. mutex_lock(&dirty_i->seglist_lock);
  3554. kvfree(dirty_i->dirty_segmap[dirty_type]);
  3555. dirty_i->nr_dirty[dirty_type] = 0;
  3556. mutex_unlock(&dirty_i->seglist_lock);
  3557. }
  3558. static void destroy_victim_secmap(struct f2fs_sb_info *sbi)
  3559. {
  3560. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  3561. kvfree(dirty_i->victim_secmap);
  3562. }
  3563. static void destroy_dirty_segmap(struct f2fs_sb_info *sbi)
  3564. {
  3565. struct dirty_seglist_info *dirty_i = DIRTY_I(sbi);
  3566. int i;
  3567. if (!dirty_i)
  3568. return;
  3569. /* discard pre-free/dirty segments list */
  3570. for (i = 0; i < NR_DIRTY_TYPE; i++)
  3571. discard_dirty_segmap(sbi, i);
  3572. destroy_victim_secmap(sbi);
  3573. SM_I(sbi)->dirty_info = NULL;
  3574. kfree(dirty_i);
  3575. }
  3576. static void destroy_curseg(struct f2fs_sb_info *sbi)
  3577. {
  3578. struct curseg_info *array = SM_I(sbi)->curseg_array;
  3579. int i;
  3580. if (!array)
  3581. return;
  3582. SM_I(sbi)->curseg_array = NULL;
  3583. for (i = 0; i < NR_CURSEG_TYPE; i++) {
  3584. kfree(array[i].sum_blk);
  3585. kfree(array[i].journal);
  3586. }
  3587. kfree(array);
  3588. }
  3589. static void destroy_free_segmap(struct f2fs_sb_info *sbi)
  3590. {
  3591. struct free_segmap_info *free_i = SM_I(sbi)->free_info;
  3592. if (!free_i)
  3593. return;
  3594. SM_I(sbi)->free_info = NULL;
  3595. kvfree(free_i->free_segmap);
  3596. kvfree(free_i->free_secmap);
  3597. kfree(free_i);
  3598. }
  3599. static void destroy_sit_info(struct f2fs_sb_info *sbi)
  3600. {
  3601. struct sit_info *sit_i = SIT_I(sbi);
  3602. unsigned int start;
  3603. if (!sit_i)
  3604. return;
  3605. if (sit_i->sentries) {
  3606. for (start = 0; start < MAIN_SEGS(sbi); start++) {
  3607. kfree(sit_i->sentries[start].cur_valid_map);
  3608. #ifdef CONFIG_F2FS_CHECK_FS
  3609. kfree(sit_i->sentries[start].cur_valid_map_mir);
  3610. #endif
  3611. kfree(sit_i->sentries[start].ckpt_valid_map);
  3612. kfree(sit_i->sentries[start].discard_map);
  3613. }
  3614. }
  3615. kfree(sit_i->tmp_map);
  3616. kvfree(sit_i->sentries);
  3617. kvfree(sit_i->sec_entries);
  3618. kvfree(sit_i->dirty_sentries_bitmap);
  3619. SM_I(sbi)->sit_info = NULL;
  3620. kfree(sit_i->sit_bitmap);
  3621. #ifdef CONFIG_F2FS_CHECK_FS
  3622. kfree(sit_i->sit_bitmap_mir);
  3623. #endif
  3624. kfree(sit_i);
  3625. }
  3626. void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi)
  3627. {
  3628. struct f2fs_sm_info *sm_info = SM_I(sbi);
  3629. if (!sm_info)
  3630. return;
  3631. f2fs_destroy_flush_cmd_control(sbi, true);
  3632. destroy_discard_cmd_control(sbi);
  3633. destroy_dirty_segmap(sbi);
  3634. destroy_curseg(sbi);
  3635. destroy_free_segmap(sbi);
  3636. destroy_sit_info(sbi);
  3637. sbi->sm_info = NULL;
  3638. kfree(sm_info);
  3639. }
  3640. int __init f2fs_create_segment_manager_caches(void)
  3641. {
  3642. discard_entry_slab = f2fs_kmem_cache_create("discard_entry",
  3643. sizeof(struct discard_entry));
  3644. if (!discard_entry_slab)
  3645. goto fail;
  3646. discard_cmd_slab = f2fs_kmem_cache_create("discard_cmd",
  3647. sizeof(struct discard_cmd));
  3648. if (!discard_cmd_slab)
  3649. goto destroy_discard_entry;
  3650. sit_entry_set_slab = f2fs_kmem_cache_create("sit_entry_set",
  3651. sizeof(struct sit_entry_set));
  3652. if (!sit_entry_set_slab)
  3653. goto destroy_discard_cmd;
  3654. inmem_entry_slab = f2fs_kmem_cache_create("inmem_page_entry",
  3655. sizeof(struct inmem_pages));
  3656. if (!inmem_entry_slab)
  3657. goto destroy_sit_entry_set;
  3658. return 0;
  3659. destroy_sit_entry_set:
  3660. kmem_cache_destroy(sit_entry_set_slab);
  3661. destroy_discard_cmd:
  3662. kmem_cache_destroy(discard_cmd_slab);
  3663. destroy_discard_entry:
  3664. kmem_cache_destroy(discard_entry_slab);
  3665. fail:
  3666. return -ENOMEM;
  3667. }
  3668. void f2fs_destroy_segment_manager_caches(void)
  3669. {
  3670. kmem_cache_destroy(sit_entry_set_slab);
  3671. kmem_cache_destroy(discard_cmd_slab);
  3672. kmem_cache_destroy(discard_entry_slab);
  3673. kmem_cache_destroy(inmem_entry_slab);
  3674. }