segment.c 137 KB

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