f2fs.h 165 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * fs/f2fs/f2fs.h
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #ifndef _LINUX_F2FS_H
  9. #define _LINUX_F2FS_H
  10. #include <linux/uio.h>
  11. #include <linux/types.h>
  12. #include <linux/page-flags.h>
  13. #include <linux/slab.h>
  14. #include <linux/crc32.h>
  15. #include <linux/magic.h>
  16. #include <linux/kobject.h>
  17. #include <linux/sched.h>
  18. #include <linux/cred.h>
  19. #include <linux/sched/mm.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/bio.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/quotaops.h>
  24. #include <linux/part_stat.h>
  25. #include <linux/rw_hint.h>
  26. #include <linux/fscrypt.h>
  27. #include <linux/fsverity.h>
  28. struct pagevec;
  29. #ifdef CONFIG_F2FS_CHECK_FS
  30. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  31. #else
  32. #define f2fs_bug_on(sbi, condition) \
  33. do { \
  34. if (WARN_ON(condition)) \
  35. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  36. } while (0)
  37. #endif
  38. enum {
  39. FAULT_KMALLOC,
  40. FAULT_KVMALLOC,
  41. FAULT_PAGE_ALLOC,
  42. FAULT_PAGE_GET,
  43. FAULT_ALLOC_BIO, /* it's obsolete due to bio_alloc() will never fail */
  44. FAULT_ALLOC_NID,
  45. FAULT_ORPHAN,
  46. FAULT_BLOCK,
  47. FAULT_DIR_DEPTH,
  48. FAULT_EVICT_INODE,
  49. FAULT_TRUNCATE,
  50. FAULT_READ_IO,
  51. FAULT_CHECKPOINT,
  52. FAULT_DISCARD, /* it's obsolete due to __blkdev_issue_discard() will never fail */
  53. FAULT_WRITE_IO,
  54. FAULT_SLAB_ALLOC,
  55. FAULT_DQUOT_INIT,
  56. FAULT_LOCK_OP,
  57. FAULT_BLKADDR_VALIDITY,
  58. FAULT_BLKADDR_CONSISTENCE,
  59. FAULT_NO_SEGMENT,
  60. FAULT_INCONSISTENT_FOOTER,
  61. FAULT_ATOMIC_TIMEOUT,
  62. FAULT_VMALLOC,
  63. FAULT_LOCK_TIMEOUT,
  64. FAULT_SKIP_WRITE,
  65. FAULT_MAX,
  66. };
  67. /* indicate which option to update */
  68. enum fault_option {
  69. FAULT_RATE = 1, /* only update fault rate */
  70. FAULT_TYPE = 2, /* only update fault type */
  71. FAULT_TIMEOUT = 4, /* only update fault timeout type */
  72. FAULT_ALL = 8, /* reset all fault injection options/stats */
  73. };
  74. #ifdef CONFIG_F2FS_FAULT_INJECTION
  75. struct f2fs_fault_info {
  76. atomic_t inject_ops;
  77. int inject_rate;
  78. unsigned int inject_type;
  79. /* Used to account total count of injection for each type */
  80. unsigned int inject_count[FAULT_MAX];
  81. unsigned int inject_lock_timeout; /* inject lock timeout */
  82. };
  83. extern const char *f2fs_fault_name[FAULT_MAX];
  84. #define IS_FAULT_SET(fi, type) ((fi)->inject_type & BIT(type))
  85. /* maximum retry count for injected failure */
  86. #define DEFAULT_FAILURE_RETRY_COUNT 8
  87. #else
  88. #define DEFAULT_FAILURE_RETRY_COUNT 1
  89. #endif
  90. /*
  91. * For mount options
  92. */
  93. enum f2fs_mount_opt {
  94. F2FS_MOUNT_DISABLE_ROLL_FORWARD,
  95. F2FS_MOUNT_DISCARD,
  96. F2FS_MOUNT_NOHEAP,
  97. F2FS_MOUNT_XATTR_USER,
  98. F2FS_MOUNT_POSIX_ACL,
  99. F2FS_MOUNT_DISABLE_EXT_IDENTIFY,
  100. F2FS_MOUNT_INLINE_XATTR,
  101. F2FS_MOUNT_INLINE_DATA,
  102. F2FS_MOUNT_INLINE_DENTRY,
  103. F2FS_MOUNT_FLUSH_MERGE,
  104. F2FS_MOUNT_NOBARRIER,
  105. F2FS_MOUNT_FASTBOOT,
  106. F2FS_MOUNT_READ_EXTENT_CACHE,
  107. F2FS_MOUNT_DATA_FLUSH,
  108. F2FS_MOUNT_FAULT_INJECTION,
  109. F2FS_MOUNT_USRQUOTA,
  110. F2FS_MOUNT_GRPQUOTA,
  111. F2FS_MOUNT_PRJQUOTA,
  112. F2FS_MOUNT_QUOTA,
  113. F2FS_MOUNT_INLINE_XATTR_SIZE,
  114. F2FS_MOUNT_RESERVE_ROOT,
  115. F2FS_MOUNT_DISABLE_CHECKPOINT,
  116. F2FS_MOUNT_NORECOVERY,
  117. F2FS_MOUNT_ATGC,
  118. F2FS_MOUNT_MERGE_CHECKPOINT,
  119. F2FS_MOUNT_GC_MERGE,
  120. F2FS_MOUNT_COMPRESS_CACHE,
  121. F2FS_MOUNT_AGE_EXTENT_CACHE,
  122. F2FS_MOUNT_NAT_BITS,
  123. F2FS_MOUNT_INLINECRYPT,
  124. /*
  125. * Some f2fs environments expect to be able to pass the "lazytime" option
  126. * string rather than using the MS_LAZYTIME flag, so this must remain.
  127. */
  128. F2FS_MOUNT_LAZYTIME,
  129. F2FS_MOUNT_RESERVE_NODE,
  130. };
  131. #define F2FS_OPTION(sbi) ((sbi)->mount_opt)
  132. #define clear_opt(sbi, option) \
  133. (F2FS_OPTION(sbi).opt &= ~BIT(F2FS_MOUNT_##option))
  134. #define set_opt(sbi, option) \
  135. (F2FS_OPTION(sbi).opt |= BIT(F2FS_MOUNT_##option))
  136. #define test_opt(sbi, option) \
  137. (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
  138. #define ver_after(a, b) (typecheck(unsigned long long, a) && \
  139. typecheck(unsigned long long, b) && \
  140. ((long long)((a) - (b)) > 0))
  141. typedef u32 block_t; /*
  142. * should not change u32, since it is the on-disk block
  143. * address format, __le32.
  144. */
  145. typedef u32 nid_t;
  146. #define COMPRESS_EXT_NUM 16
  147. enum blkzone_allocation_policy {
  148. BLKZONE_ALLOC_PRIOR_SEQ, /* Prioritize writing to sequential zones */
  149. BLKZONE_ALLOC_ONLY_SEQ, /* Only allow writing to sequential zones */
  150. BLKZONE_ALLOC_PRIOR_CONV, /* Prioritize writing to conventional zones */
  151. };
  152. enum bggc_io_aware_policy {
  153. AWARE_ALL_IO, /* skip background GC if there is any kind of pending IO */
  154. AWARE_READ_IO, /* skip background GC if there is pending read IO */
  155. AWARE_NONE, /* don't aware IO for background GC */
  156. };
  157. enum device_allocation_policy {
  158. ALLOCATE_FORWARD_NOHINT,
  159. ALLOCATE_FORWARD_WITHIN_HINT,
  160. ALLOCATE_FORWARD_FROM_HINT,
  161. };
  162. enum f2fs_lock_name {
  163. LOCK_NAME_NONE,
  164. LOCK_NAME_CP_RWSEM,
  165. LOCK_NAME_NODE_CHANGE,
  166. LOCK_NAME_NODE_WRITE,
  167. LOCK_NAME_GC_LOCK,
  168. LOCK_NAME_CP_GLOBAL,
  169. LOCK_NAME_IO_RWSEM,
  170. LOCK_NAME_MAX,
  171. };
  172. enum f2fs_timeout_type {
  173. TIMEOUT_TYPE_NONE,
  174. TIMEOUT_TYPE_RUNNING,
  175. TIMEOUT_TYPE_IO_SLEEP,
  176. TIMEOUT_TYPE_NONIO_SLEEP,
  177. TIMEOUT_TYPE_RUNNABLE,
  178. TIMEOUT_TYPE_MAX,
  179. };
  180. /*
  181. * An implementation of an rwsem that is explicitly unfair to readers. This
  182. * prevents priority inversion when a low-priority reader acquires the read lock
  183. * while sleeping on the write lock but the write lock is needed by
  184. * higher-priority clients.
  185. */
  186. struct f2fs_rwsem {
  187. struct f2fs_sb_info *sbi;
  188. enum f2fs_lock_name name;
  189. struct rw_semaphore internal_rwsem;
  190. #ifdef CONFIG_F2FS_UNFAIR_RWSEM
  191. wait_queue_head_t read_waiters;
  192. #endif
  193. };
  194. struct f2fs_mount_info {
  195. unsigned long long opt;
  196. block_t root_reserved_blocks; /* root reserved blocks */
  197. block_t root_reserved_nodes; /* root reserved nodes */
  198. kuid_t s_resuid; /* reserved blocks for uid */
  199. kgid_t s_resgid; /* reserved blocks for gid */
  200. int active_logs; /* # of active logs */
  201. int inline_xattr_size; /* inline xattr size */
  202. #ifdef CONFIG_F2FS_FAULT_INJECTION
  203. struct f2fs_fault_info fault_info; /* For fault injection */
  204. #endif
  205. #ifdef CONFIG_QUOTA
  206. /* Names of quota files with journalled quota */
  207. char *s_qf_names[MAXQUOTAS];
  208. int s_jquota_fmt; /* Format of quota to use */
  209. #endif
  210. /* For which write hints are passed down to block layer */
  211. int alloc_mode; /* segment allocation policy */
  212. int fsync_mode; /* fsync policy */
  213. int fs_mode; /* fs mode: LFS or ADAPTIVE */
  214. int bggc_mode; /* bggc mode: off, on or sync */
  215. int memory_mode; /* memory mode */
  216. int errors; /* errors parameter */
  217. int discard_unit; /*
  218. * discard command's offset/size should
  219. * be aligned to this unit: block,
  220. * segment or section
  221. */
  222. struct fscrypt_dummy_policy dummy_enc_policy; /* test dummy encryption */
  223. block_t unusable_cap_perc; /* percentage for cap */
  224. block_t unusable_cap; /* Amount of space allowed to be
  225. * unusable when disabling checkpoint
  226. */
  227. /* For compression */
  228. unsigned char compress_algorithm; /* algorithm type */
  229. unsigned char compress_log_size; /* cluster log size */
  230. unsigned char compress_level; /* compress level */
  231. bool compress_chksum; /* compressed data chksum */
  232. unsigned char compress_ext_cnt; /* extension count */
  233. unsigned char nocompress_ext_cnt; /* nocompress extension count */
  234. int compress_mode; /* compression mode */
  235. unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
  236. unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
  237. unsigned int lookup_mode;
  238. };
  239. #define F2FS_FEATURE_ENCRYPT 0x00000001
  240. #define F2FS_FEATURE_BLKZONED 0x00000002
  241. #define F2FS_FEATURE_ATOMIC_WRITE 0x00000004
  242. #define F2FS_FEATURE_EXTRA_ATTR 0x00000008
  243. #define F2FS_FEATURE_PRJQUOTA 0x00000010
  244. #define F2FS_FEATURE_INODE_CHKSUM 0x00000020
  245. #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x00000040
  246. #define F2FS_FEATURE_QUOTA_INO 0x00000080
  247. #define F2FS_FEATURE_INODE_CRTIME 0x00000100
  248. #define F2FS_FEATURE_LOST_FOUND 0x00000200
  249. #define F2FS_FEATURE_VERITY 0x00000400
  250. #define F2FS_FEATURE_SB_CHKSUM 0x00000800
  251. #define F2FS_FEATURE_CASEFOLD 0x00001000
  252. #define F2FS_FEATURE_COMPRESSION 0x00002000
  253. #define F2FS_FEATURE_RO 0x00004000
  254. #define F2FS_FEATURE_DEVICE_ALIAS 0x00008000
  255. #define F2FS_FEATURE_PACKED_SSA 0x00010000
  256. #define __F2FS_HAS_FEATURE(raw_super, mask) \
  257. ((raw_super->feature & cpu_to_le32(mask)) != 0)
  258. #define F2FS_HAS_FEATURE(sbi, mask) __F2FS_HAS_FEATURE(sbi->raw_super, mask)
  259. /*
  260. * Default values for user and/or group using reserved blocks
  261. */
  262. #define F2FS_DEF_RESUID 0
  263. #define F2FS_DEF_RESGID 0
  264. /*
  265. * For checkpoint manager
  266. */
  267. enum {
  268. NAT_BITMAP,
  269. SIT_BITMAP
  270. };
  271. #define CP_UMOUNT 0x00000001
  272. #define CP_FASTBOOT 0x00000002
  273. #define CP_SYNC 0x00000004
  274. #define CP_RECOVERY 0x00000008
  275. #define CP_DISCARD 0x00000010
  276. #define CP_TRIMMED 0x00000020
  277. #define CP_PAUSE 0x00000040
  278. #define CP_RESIZE 0x00000080
  279. #define DEF_MAX_DISCARD_REQUEST 8 /* issue 8 discards per round */
  280. #define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */
  281. #define DEF_MID_DISCARD_ISSUE_TIME 500 /* 500 ms, if device busy */
  282. #define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */
  283. #define DEF_DISCARD_URGENT_UTIL 80 /* do more discard over 80% */
  284. #define DEF_CP_INTERVAL 60 /* 60 secs */
  285. #define DEF_IDLE_INTERVAL 5 /* 5 secs */
  286. #define DEF_DISABLE_INTERVAL 5 /* 5 secs */
  287. #define DEF_DISABLE_QUICK_INTERVAL 1 /* 1 secs */
  288. #define DEF_UMOUNT_DISCARD_TIMEOUT 5 /* 5 secs */
  289. enum cp_time {
  290. CP_TIME_START, /* begin */
  291. CP_TIME_LOCK, /* after cp_global_sem */
  292. CP_TIME_OP_LOCK, /* after block_operation */
  293. CP_TIME_MERGE_WRITE, /* after flush DATA/NODE/META */
  294. CP_TIME_FLUSH_NAT, /* after flush nat */
  295. CP_TIME_FLUSH_SIT, /* after flush sit */
  296. CP_TIME_SYNC_META, /* after sync_meta_pages */
  297. CP_TIME_SYNC_CP_META, /* after sync cp meta pages */
  298. CP_TIME_WAIT_DIRTY_META,/* after wait on dirty meta */
  299. CP_TIME_WAIT_CP_DATA, /* after wait on cp data */
  300. CP_TIME_FLUSH_DEVICE, /* after flush device cache */
  301. CP_TIME_WAIT_LAST_CP, /* after wait on last cp pack */
  302. CP_TIME_END, /* after unblock_operation */
  303. CP_TIME_MAX,
  304. };
  305. /* time cost stats of checkpoint */
  306. struct cp_stats {
  307. ktime_t times[CP_TIME_MAX];
  308. };
  309. struct cp_control {
  310. int reason;
  311. __u64 trim_start;
  312. __u64 trim_end;
  313. __u64 trim_minlen;
  314. struct cp_stats stats;
  315. };
  316. enum f2fs_cp_phase {
  317. CP_PHASE_START_BLOCK_OPS,
  318. CP_PHASE_FINISH_BLOCK_OPS,
  319. CP_PHASE_FINISH_CHECKPOINT,
  320. };
  321. /*
  322. * indicate meta/data type
  323. */
  324. enum {
  325. META_CP,
  326. META_NAT,
  327. META_SIT,
  328. META_SSA,
  329. META_MAX,
  330. META_POR,
  331. DATA_GENERIC, /* check range only */
  332. DATA_GENERIC_ENHANCE, /* strong check on range and segment bitmap */
  333. DATA_GENERIC_ENHANCE_READ, /*
  334. * strong check on range and segment
  335. * bitmap but no warning due to race
  336. * condition of read on truncated area
  337. * by extent_cache
  338. */
  339. DATA_GENERIC_ENHANCE_UPDATE, /*
  340. * strong check on range and segment
  341. * bitmap for update case
  342. */
  343. META_GENERIC,
  344. };
  345. /* for the list of ino */
  346. enum {
  347. ORPHAN_INO, /* for orphan ino list */
  348. APPEND_INO, /* for append ino list */
  349. UPDATE_INO, /* for update ino list */
  350. TRANS_DIR_INO, /* for transactions dir ino list */
  351. XATTR_DIR_INO, /* for xattr updated dir ino list */
  352. FLUSH_INO, /* for multiple device flushing */
  353. MAX_INO_ENTRY, /* max. list */
  354. };
  355. struct ino_entry {
  356. struct list_head list; /* list head */
  357. nid_t ino; /* inode number */
  358. unsigned int dirty_device; /* dirty device bitmap */
  359. };
  360. /* for the list of inodes to be GCed */
  361. struct inode_entry {
  362. struct list_head list; /* list head */
  363. struct inode *inode; /* vfs inode pointer */
  364. };
  365. struct fsync_node_entry {
  366. struct list_head list; /* list head */
  367. struct folio *folio; /* warm node folio pointer */
  368. unsigned int seq_id; /* sequence id */
  369. };
  370. struct ckpt_req {
  371. struct completion wait; /* completion for checkpoint done */
  372. struct llist_node llnode; /* llist_node to be linked in wait queue */
  373. int ret; /* return code of checkpoint */
  374. union {
  375. ktime_t queue_time; /* request queued time */
  376. ktime_t delta_time; /* time in queue */
  377. };
  378. };
  379. struct ckpt_req_control {
  380. struct task_struct *f2fs_issue_ckpt; /* checkpoint task */
  381. int ckpt_thread_ioprio; /* checkpoint merge thread ioprio */
  382. wait_queue_head_t ckpt_wait_queue; /* waiting queue for wake-up */
  383. atomic_t issued_ckpt; /* # of actually issued ckpts */
  384. atomic_t total_ckpt; /* # of total ckpts */
  385. atomic_t queued_ckpt; /* # of queued ckpts */
  386. struct llist_head issue_list; /* list for command issue */
  387. spinlock_t stat_lock; /* lock for below checkpoint time stats */
  388. unsigned int cur_time; /* cur wait time in msec for currently issued checkpoint */
  389. unsigned int peak_time; /* peak wait time in msec until now */
  390. };
  391. /* a time threshold that checkpoint was blocked for, unit: ms */
  392. #define CP_LONG_LATENCY_THRESHOLD 5000
  393. /* for the bitmap indicate blocks to be discarded */
  394. struct discard_entry {
  395. struct list_head list; /* list head */
  396. block_t start_blkaddr; /* start blockaddr of current segment */
  397. unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
  398. };
  399. /* minimum discard granularity, unit: block count */
  400. #define MIN_DISCARD_GRANULARITY 1
  401. /* default discard granularity of inner discard thread, unit: block count */
  402. #define DEFAULT_DISCARD_GRANULARITY 16
  403. /* default maximum discard granularity of ordered discard, unit: block count */
  404. #define DEFAULT_MAX_ORDERED_DISCARD_GRANULARITY 16
  405. /* default interval of periodical discard submission */
  406. #define DEFAULT_DISCARD_INTERVAL (msecs_to_jiffies(20))
  407. /* max discard pend list number */
  408. #define MAX_PLIST_NUM 512
  409. #define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
  410. (MAX_PLIST_NUM - 1) : ((blk_num) - 1))
  411. enum {
  412. D_PREP, /* initial */
  413. D_PARTIAL, /* partially submitted */
  414. D_SUBMIT, /* all submitted */
  415. D_DONE, /* finished */
  416. };
  417. struct discard_info {
  418. block_t lstart; /* logical start address */
  419. block_t len; /* length */
  420. block_t start; /* actual start address in dev */
  421. };
  422. struct discard_cmd {
  423. struct rb_node rb_node; /* rb node located in rb-tree */
  424. struct discard_info di; /* discard info */
  425. struct list_head list; /* command list */
  426. struct completion wait; /* completion */
  427. struct block_device *bdev; /* bdev */
  428. unsigned short ref; /* reference count */
  429. unsigned char state; /* state */
  430. unsigned char queued; /* queued discard */
  431. int error; /* bio error */
  432. spinlock_t lock; /* for state/bio_ref updating */
  433. unsigned short bio_ref; /* bio reference count */
  434. };
  435. enum {
  436. DPOLICY_BG,
  437. DPOLICY_FORCE,
  438. DPOLICY_FSTRIM,
  439. DPOLICY_UMOUNT,
  440. MAX_DPOLICY,
  441. };
  442. enum {
  443. DPOLICY_IO_AWARE_DISABLE, /* force to not be aware of IO */
  444. DPOLICY_IO_AWARE_ENABLE, /* force to be aware of IO */
  445. DPOLICY_IO_AWARE_MAX,
  446. };
  447. struct discard_policy {
  448. int type; /* type of discard */
  449. unsigned int min_interval; /* used for candidates exist */
  450. unsigned int mid_interval; /* used for device busy */
  451. unsigned int max_interval; /* used for candidates not exist */
  452. unsigned int max_requests; /* # of discards issued per round */
  453. unsigned int io_aware_gran; /* minimum granularity discard not be aware of I/O */
  454. bool io_aware; /* issue discard in idle time */
  455. bool sync; /* submit discard with REQ_SYNC flag */
  456. bool ordered; /* issue discard by lba order */
  457. bool timeout; /* discard timeout for put_super */
  458. unsigned int granularity; /* discard granularity */
  459. };
  460. struct discard_cmd_control {
  461. struct task_struct *f2fs_issue_discard; /* discard thread */
  462. struct list_head entry_list; /* 4KB discard entry list */
  463. struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
  464. struct list_head wait_list; /* store on-flushing entries */
  465. struct list_head fstrim_list; /* in-flight discard from fstrim */
  466. wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
  467. struct mutex cmd_lock;
  468. unsigned int nr_discards; /* # of discards in the list */
  469. unsigned int max_discards; /* max. discards to be issued */
  470. unsigned int max_discard_request; /* max. discard request per round */
  471. unsigned int min_discard_issue_time; /* min. interval between discard issue */
  472. unsigned int mid_discard_issue_time; /* mid. interval between discard issue */
  473. unsigned int max_discard_issue_time; /* max. interval between discard issue */
  474. unsigned int discard_io_aware_gran; /* minimum discard granularity not be aware of I/O */
  475. unsigned int discard_urgent_util; /* utilization which issue discard proactively */
  476. unsigned int discard_granularity; /* discard granularity */
  477. unsigned int max_ordered_discard; /* maximum discard granularity issued by lba order */
  478. unsigned int discard_io_aware; /* io_aware policy */
  479. unsigned int undiscard_blks; /* # of undiscard blocks */
  480. unsigned int next_pos; /* next discard position */
  481. atomic_t issued_discard; /* # of issued discard */
  482. atomic_t queued_discard; /* # of queued discard */
  483. atomic_t discard_cmd_cnt; /* # of cached cmd count */
  484. struct rb_root_cached root; /* root of discard rb-tree */
  485. bool rbtree_check; /* config for consistence check */
  486. bool discard_wake; /* to wake up discard thread */
  487. };
  488. /* for the list of fsync inodes, used only during recovery */
  489. struct fsync_inode_entry {
  490. struct list_head list; /* list head */
  491. struct inode *inode; /* vfs inode pointer */
  492. block_t blkaddr; /* block address locating the last fsync */
  493. block_t last_dentry; /* block address locating the last dentry */
  494. };
  495. #define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats))
  496. #define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits))
  497. #define nat_in_journal(jnl, i) \
  498. (((struct nat_journal_entry *)(jnl)->nat_j.entries)[i].ne)
  499. #define nid_in_journal(jnl, i) \
  500. (((struct nat_journal_entry *)(jnl)->nat_j.entries)[i].nid)
  501. #define sit_in_journal(jnl, i) \
  502. (((struct sit_journal_entry *)(jnl)->sit_j.entries)[i].se)
  503. #define segno_in_journal(jnl, i) \
  504. (((struct sit_journal_entry *)(jnl)->sit_j.entries)[i].segno)
  505. #define sum_entries(sum) ((struct f2fs_summary *)(sum))
  506. #define sum_journal(sbi, sum) \
  507. ((struct f2fs_journal *)((char *)(sum) + \
  508. ((sbi)->entries_in_sum * sizeof(struct f2fs_summary))))
  509. #define sum_footer(sbi, sum) \
  510. ((struct summary_footer *)((char *)(sum) + (sbi)->sum_blocksize - \
  511. sizeof(struct summary_footer)))
  512. #define MAX_NAT_JENTRIES(sbi, jnl) ((sbi)->nat_journal_entries - nats_in_cursum(jnl))
  513. #define MAX_SIT_JENTRIES(sbi, jnl) ((sbi)->sit_journal_entries - sits_in_cursum(jnl))
  514. static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
  515. {
  516. int before = nats_in_cursum(journal);
  517. journal->n_nats = cpu_to_le16(before + i);
  518. return before;
  519. }
  520. static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
  521. {
  522. int before = sits_in_cursum(journal);
  523. journal->n_sits = cpu_to_le16(before + i);
  524. return before;
  525. }
  526. /* for inline stuff */
  527. #define DEF_INLINE_RESERVED_SIZE 1
  528. static inline int get_extra_isize(struct inode *inode);
  529. static inline int get_inline_xattr_addrs(struct inode *inode);
  530. #define MAX_INLINE_DATA(inode) (sizeof(__le32) * \
  531. (CUR_ADDRS_PER_INODE(inode) - \
  532. get_inline_xattr_addrs(inode) - \
  533. DEF_INLINE_RESERVED_SIZE))
  534. /* for inline dir */
  535. #define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
  536. ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
  537. BITS_PER_BYTE + 1))
  538. #define INLINE_DENTRY_BITMAP_SIZE(inode) \
  539. DIV_ROUND_UP(NR_INLINE_DENTRY(inode), BITS_PER_BYTE)
  540. #define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \
  541. ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
  542. NR_INLINE_DENTRY(inode) + \
  543. INLINE_DENTRY_BITMAP_SIZE(inode)))
  544. /*
  545. * For INODE and NODE manager
  546. */
  547. /* for directory operations */
  548. struct f2fs_filename {
  549. /*
  550. * The filename the user specified. This is NULL for some
  551. * filesystem-internal operations, e.g. converting an inline directory
  552. * to a non-inline one, or roll-forward recovering an encrypted dentry.
  553. */
  554. const struct qstr *usr_fname;
  555. /*
  556. * The on-disk filename. For encrypted directories, this is encrypted.
  557. * This may be NULL for lookups in an encrypted dir without the key.
  558. */
  559. struct fscrypt_str disk_name;
  560. /* The dirhash of this filename */
  561. f2fs_hash_t hash;
  562. #ifdef CONFIG_FS_ENCRYPTION
  563. /*
  564. * For lookups in encrypted directories: either the buffer backing
  565. * disk_name, or a buffer that holds the decoded no-key name.
  566. */
  567. struct fscrypt_str crypto_buf;
  568. #endif
  569. #if IS_ENABLED(CONFIG_UNICODE)
  570. /*
  571. * For casefolded directories: the casefolded name, but it's left NULL
  572. * if the original name is not valid Unicode, if the original name is
  573. * "." or "..", if the directory is both casefolded and encrypted and
  574. * its encryption key is unavailable, or if the filesystem is doing an
  575. * internal operation where usr_fname is also NULL. In all these cases
  576. * we fall back to treating the name as an opaque byte sequence.
  577. */
  578. struct qstr cf_name;
  579. #endif
  580. };
  581. struct f2fs_dentry_ptr {
  582. struct inode *inode;
  583. void *bitmap;
  584. struct f2fs_dir_entry *dentry;
  585. __u8 (*filename)[F2FS_SLOT_LEN];
  586. int max;
  587. int nr_bitmap;
  588. };
  589. static inline void make_dentry_ptr_block(struct inode *inode,
  590. struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
  591. {
  592. d->inode = inode;
  593. d->max = NR_DENTRY_IN_BLOCK;
  594. d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
  595. d->bitmap = t->dentry_bitmap;
  596. d->dentry = t->dentry;
  597. d->filename = t->filename;
  598. }
  599. static inline void make_dentry_ptr_inline(struct inode *inode,
  600. struct f2fs_dentry_ptr *d, void *t)
  601. {
  602. int entry_cnt = NR_INLINE_DENTRY(inode);
  603. int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
  604. int reserved_size = INLINE_RESERVED_SIZE(inode);
  605. d->inode = inode;
  606. d->max = entry_cnt;
  607. d->nr_bitmap = bitmap_size;
  608. d->bitmap = t;
  609. d->dentry = t + bitmap_size + reserved_size;
  610. d->filename = t + bitmap_size + reserved_size +
  611. SIZE_OF_DIR_ENTRY * entry_cnt;
  612. }
  613. /*
  614. * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
  615. * as its node offset to distinguish from index node blocks.
  616. * But some bits are used to mark the node block.
  617. */
  618. #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
  619. >> OFFSET_BIT_SHIFT)
  620. enum {
  621. ALLOC_NODE, /* allocate a new node page if needed */
  622. LOOKUP_NODE, /* look up a node without readahead */
  623. LOOKUP_NODE_RA, /*
  624. * look up a node with readahead called
  625. * by get_data_block.
  626. */
  627. };
  628. #define DEFAULT_RETRY_IO_COUNT 8 /* maximum retry read IO or flush count */
  629. #define MAX_FLUSH_RETRY_COUNT 3 /* maximum flush retry count in f2fs_enable_checkpoint() */
  630. /* IO/non-IO congestion wait timeout value, default: 1 jiffies */
  631. #define DEFAULT_SCHEDULE_TIMEOUT 1
  632. /* timeout value injected, default: 1000ms */
  633. #define DEFAULT_FAULT_TIMEOUT (msecs_to_jiffies(1000))
  634. /* maximum retry quota flush count */
  635. #define DEFAULT_RETRY_QUOTA_FLUSH_COUNT 8
  636. /* maximum retry of EIO'ed page */
  637. #define MAX_RETRY_PAGE_EIO 100
  638. #define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
  639. #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
  640. /* dirty segments threshold for triggering CP */
  641. #define DEFAULT_DIRTY_THRESHOLD 4
  642. #define RECOVERY_MAX_RA_BLOCKS BIO_MAX_VECS
  643. #define RECOVERY_MIN_RA_BLOCKS 1
  644. #define F2FS_ONSTACK_PAGES 16 /* nr of onstack pages */
  645. /* for in-memory extent cache entry */
  646. #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
  647. /* number of extent info in extent cache we try to shrink */
  648. #define READ_EXTENT_CACHE_SHRINK_NUMBER 128
  649. /* number of age extent info in extent cache we try to shrink */
  650. #define AGE_EXTENT_CACHE_SHRINK_NUMBER 128
  651. #define LAST_AGE_WEIGHT 30
  652. #define SAME_AGE_REGION 1024
  653. /*
  654. * Define data block with age less than 1GB as hot data
  655. * define data block with age less than 10GB but more than 1GB as warm data
  656. */
  657. #define DEF_HOT_DATA_AGE_THRESHOLD 262144
  658. #define DEF_WARM_DATA_AGE_THRESHOLD 2621440
  659. /* default max read extent count per inode */
  660. #define DEF_MAX_READ_EXTENT_COUNT 10240
  661. /* extent cache type */
  662. enum extent_type {
  663. EX_READ,
  664. EX_BLOCK_AGE,
  665. NR_EXTENT_CACHES,
  666. };
  667. /*
  668. * Reserved value to mark invalid age extents, hence valid block range
  669. * from 0 to ULLONG_MAX-1
  670. */
  671. #define F2FS_EXTENT_AGE_INVALID ULLONG_MAX
  672. struct extent_info {
  673. unsigned int fofs; /* start offset in a file */
  674. unsigned int len; /* length of the extent */
  675. union {
  676. /* read extent_cache */
  677. struct {
  678. /* start block address of the extent */
  679. block_t blk;
  680. #ifdef CONFIG_F2FS_FS_COMPRESSION
  681. /* physical extent length of compressed blocks */
  682. unsigned int c_len;
  683. #endif
  684. };
  685. /* block age extent_cache */
  686. struct {
  687. /* block age of the extent */
  688. unsigned long long age;
  689. /* last total blocks allocated */
  690. unsigned long long last_blocks;
  691. };
  692. };
  693. };
  694. struct extent_node {
  695. struct rb_node rb_node; /* rb node located in rb-tree */
  696. struct extent_info ei; /* extent info */
  697. struct list_head list; /* node in global extent list of sbi */
  698. struct extent_tree *et; /* extent tree pointer */
  699. };
  700. struct extent_tree {
  701. nid_t ino; /* inode number */
  702. enum extent_type type; /* keep the extent tree type */
  703. struct rb_root_cached root; /* root of extent info rb-tree */
  704. struct extent_node *cached_en; /* recently accessed extent node */
  705. struct list_head list; /* to be used by sbi->zombie_list */
  706. rwlock_t lock; /* protect extent info rb-tree */
  707. atomic_t node_cnt; /* # of extent node in rb-tree*/
  708. bool largest_updated; /* largest extent updated */
  709. struct extent_info largest; /* largest cached extent for EX_READ */
  710. };
  711. struct extent_tree_info {
  712. struct radix_tree_root extent_tree_root;/* cache extent cache entries */
  713. struct mutex extent_tree_lock; /* locking extent radix tree */
  714. struct list_head extent_list; /* lru list for shrinker */
  715. spinlock_t extent_lock; /* locking extent lru list */
  716. atomic_t total_ext_tree; /* extent tree count */
  717. struct list_head zombie_list; /* extent zombie tree list */
  718. atomic_t total_zombie_tree; /* extent zombie tree count */
  719. atomic_t total_ext_node; /* extent info count */
  720. };
  721. /*
  722. * State of block returned by f2fs_map_blocks.
  723. */
  724. #define F2FS_MAP_NEW (1U << 0)
  725. #define F2FS_MAP_MAPPED (1U << 1)
  726. #define F2FS_MAP_DELALLOC (1U << 2)
  727. #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
  728. F2FS_MAP_DELALLOC)
  729. struct f2fs_map_blocks {
  730. struct block_device *m_bdev; /* for multi-device dio */
  731. block_t m_pblk;
  732. block_t m_lblk;
  733. unsigned int m_len;
  734. unsigned int m_flags;
  735. unsigned long m_last_pblk; /* last allocated block, only used for DIO in LFS mode */
  736. pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
  737. pgoff_t *m_next_extent; /* point to next possible extent */
  738. int m_seg_type;
  739. bool m_may_create; /* indicate it is from write path */
  740. bool m_multidev_dio; /* indicate it allows multi-device dio */
  741. };
  742. /* for flag in get_data_block */
  743. enum {
  744. F2FS_GET_BLOCK_DEFAULT,
  745. F2FS_GET_BLOCK_FIEMAP,
  746. F2FS_GET_BLOCK_BMAP,
  747. F2FS_GET_BLOCK_DIO,
  748. F2FS_GET_BLOCK_PRE_DIO,
  749. F2FS_GET_BLOCK_PRE_AIO,
  750. F2FS_GET_BLOCK_PRECACHE,
  751. };
  752. /*
  753. * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
  754. */
  755. #define FADVISE_COLD_BIT 0x01
  756. #define FADVISE_LOST_PINO_BIT 0x02
  757. #define FADVISE_ENCRYPT_BIT 0x04
  758. #define FADVISE_ENC_NAME_BIT 0x08
  759. #define FADVISE_KEEP_SIZE_BIT 0x10
  760. #define FADVISE_HOT_BIT 0x20
  761. #define FADVISE_VERITY_BIT 0x40
  762. #define FADVISE_TRUNC_BIT 0x80
  763. #define FADVISE_MODIFIABLE_BITS (FADVISE_COLD_BIT | FADVISE_HOT_BIT)
  764. #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
  765. #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
  766. #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
  767. #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
  768. #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
  769. #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
  770. #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
  771. #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
  772. #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
  773. #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
  774. #define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
  775. #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
  776. #define file_is_hot(inode) is_file(inode, FADVISE_HOT_BIT)
  777. #define file_set_hot(inode) set_file(inode, FADVISE_HOT_BIT)
  778. #define file_clear_hot(inode) clear_file(inode, FADVISE_HOT_BIT)
  779. #define file_is_verity(inode) is_file(inode, FADVISE_VERITY_BIT)
  780. #define file_set_verity(inode) set_file(inode, FADVISE_VERITY_BIT)
  781. #define file_should_truncate(inode) is_file(inode, FADVISE_TRUNC_BIT)
  782. #define file_need_truncate(inode) set_file(inode, FADVISE_TRUNC_BIT)
  783. #define file_dont_truncate(inode) clear_file(inode, FADVISE_TRUNC_BIT)
  784. #define DEF_DIR_LEVEL 0
  785. /* used for f2fs_inode_info->flags */
  786. enum {
  787. FI_NEW_INODE, /* indicate newly allocated inode */
  788. FI_DIRTY_INODE, /* indicate inode is dirty or not */
  789. FI_AUTO_RECOVER, /* indicate inode is recoverable */
  790. FI_DIRTY_DIR, /* indicate directory has dirty pages */
  791. FI_INC_LINK, /* need to increment i_nlink */
  792. FI_ACL_MODE, /* indicate acl mode */
  793. FI_NO_ALLOC, /* should not allocate any blocks */
  794. FI_FREE_NID, /* free allocated nide */
  795. FI_NO_EXTENT, /* not to use the extent cache */
  796. FI_INLINE_XATTR, /* used for inline xattr */
  797. FI_INLINE_DATA, /* used for inline data*/
  798. FI_INLINE_DENTRY, /* used for inline dentry */
  799. FI_APPEND_WRITE, /* inode has appended data */
  800. FI_UPDATE_WRITE, /* inode has in-place-update data */
  801. FI_NEED_IPU, /* used for ipu per file */
  802. FI_ATOMIC_FILE, /* indicate atomic file */
  803. FI_DATA_EXIST, /* indicate data exists */
  804. FI_SKIP_WRITES, /* should skip data page writeback */
  805. FI_OPU_WRITE, /* used for opu per file */
  806. FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
  807. FI_PREALLOCATED_ALL, /* all blocks for write were preallocated */
  808. FI_HOT_DATA, /* indicate file is hot */
  809. FI_EXTRA_ATTR, /* indicate file has extra attribute */
  810. FI_PROJ_INHERIT, /* indicate file inherits projectid */
  811. FI_PIN_FILE, /* indicate file should not be gced */
  812. FI_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */
  813. FI_COMPRESSED_FILE, /* indicate file's data can be compressed */
  814. FI_COMPRESS_CORRUPT, /* indicate compressed cluster is corrupted */
  815. FI_MMAP_FILE, /* indicate file was mmapped */
  816. FI_ENABLE_COMPRESS, /* enable compression in "user" compression mode */
  817. FI_COMPRESS_RELEASED, /* compressed blocks were released */
  818. FI_ALIGNED_WRITE, /* enable aligned write */
  819. FI_COW_FILE, /* indicate COW file */
  820. FI_ATOMIC_COMMITTED, /* indicate atomic commit completed except disk sync */
  821. FI_ATOMIC_DIRTIED, /* indicate atomic file is dirtied */
  822. FI_ATOMIC_REPLACE, /* indicate atomic replace */
  823. FI_OPENED_FILE, /* indicate file has been opened */
  824. FI_DONATE_FINISHED, /* indicate page donation of file has been finished */
  825. FI_MAX, /* max flag, never be used */
  826. };
  827. struct f2fs_inode_info {
  828. struct inode vfs_inode; /* serve a vfs inode */
  829. unsigned long i_flags; /* keep an inode flags for ioctl */
  830. unsigned char i_advise; /* use to give file attribute hints */
  831. unsigned char i_dir_level; /* use for dentry level for large dir */
  832. union {
  833. unsigned int i_current_depth; /* only for directory depth */
  834. unsigned short i_gc_failures; /* for gc failure statistic */
  835. };
  836. unsigned int i_pino; /* parent inode number */
  837. umode_t i_acl_mode; /* keep file acl mode temporarily */
  838. /* Use below internally in f2fs*/
  839. unsigned long flags[BITS_TO_LONGS(FI_MAX)]; /* use to pass per-file flags */
  840. unsigned int ioprio_hint; /* hint for IO priority */
  841. struct f2fs_rwsem i_sem; /* protect fi info */
  842. atomic_t dirty_pages; /* # of dirty pages */
  843. f2fs_hash_t chash; /* hash value of given file name */
  844. unsigned int clevel; /* maximum level of given file name */
  845. struct task_struct *task; /* lookup and create consistency */
  846. struct task_struct *cp_task; /* separate cp/wb IO stats*/
  847. struct task_struct *wb_task; /* indicate inode is in context of writeback */
  848. nid_t i_xattr_nid; /* node id that contains xattrs */
  849. loff_t last_disk_size; /* lastly written file size */
  850. spinlock_t i_size_lock; /* protect last_disk_size */
  851. #ifdef CONFIG_QUOTA
  852. struct dquot __rcu *i_dquot[MAXQUOTAS];
  853. /* quota space reservation, managed internally by quota code */
  854. qsize_t i_reserved_quota;
  855. #endif
  856. struct list_head dirty_list; /* dirty list for dirs and files */
  857. struct list_head gdirty_list; /* linked in global dirty list */
  858. /* linked in global inode list for cache donation */
  859. struct list_head gdonate_list;
  860. pgoff_t donate_start, donate_end; /* inclusive */
  861. atomic_t open_count; /* # of open files */
  862. struct task_struct *atomic_write_task; /* store atomic write task */
  863. struct extent_tree *extent_tree[NR_EXTENT_CACHES];
  864. /* cached extent_tree entry */
  865. union {
  866. struct inode *cow_inode; /* copy-on-write inode for atomic write */
  867. struct inode *atomic_inode;
  868. /* point to atomic_inode, available only for cow_inode */
  869. };
  870. /* avoid racing between foreground op and gc */
  871. struct f2fs_rwsem i_gc_rwsem[2];
  872. struct f2fs_rwsem i_xattr_sem; /* avoid racing between reading and changing EAs */
  873. int i_extra_isize; /* size of extra space located in i_addr */
  874. kprojid_t i_projid; /* id for project quota */
  875. int i_inline_xattr_size; /* inline xattr size */
  876. struct timespec64 i_crtime; /* inode creation time */
  877. struct timespec64 i_disk_time[3];/* inode disk times */
  878. /* for file compress */
  879. atomic_t i_compr_blocks; /* # of compressed blocks */
  880. unsigned char i_compress_algorithm; /* algorithm type */
  881. unsigned char i_log_cluster_size; /* log of cluster size */
  882. unsigned char i_compress_level; /* compress level (lz4hc,zstd) */
  883. unsigned char i_compress_flag; /* compress flag */
  884. unsigned int i_cluster_size; /* cluster size */
  885. atomic_t writeback; /* count # of writeback thread */
  886. unsigned int atomic_write_cnt;
  887. loff_t original_i_size; /* original i_size before atomic write */
  888. #ifdef CONFIG_FS_ENCRYPTION
  889. struct fscrypt_inode_info *i_crypt_info; /* filesystem encryption info */
  890. #endif
  891. };
  892. static inline void get_read_extent_info(struct extent_info *ext,
  893. struct f2fs_extent *i_ext)
  894. {
  895. ext->fofs = le32_to_cpu(i_ext->fofs);
  896. ext->blk = le32_to_cpu(i_ext->blk);
  897. ext->len = le32_to_cpu(i_ext->len);
  898. }
  899. static inline void set_raw_read_extent(struct extent_info *ext,
  900. struct f2fs_extent *i_ext)
  901. {
  902. i_ext->fofs = cpu_to_le32(ext->fofs);
  903. i_ext->blk = cpu_to_le32(ext->blk);
  904. i_ext->len = cpu_to_le32(ext->len);
  905. }
  906. static inline bool __is_discard_mergeable(struct discard_info *back,
  907. struct discard_info *front, unsigned int max_len)
  908. {
  909. return (back->lstart + back->len == front->lstart) &&
  910. (back->len + front->len <= max_len);
  911. }
  912. static inline bool __is_discard_back_mergeable(struct discard_info *cur,
  913. struct discard_info *back, unsigned int max_len)
  914. {
  915. return __is_discard_mergeable(back, cur, max_len);
  916. }
  917. static inline bool __is_discard_front_mergeable(struct discard_info *cur,
  918. struct discard_info *front, unsigned int max_len)
  919. {
  920. return __is_discard_mergeable(cur, front, max_len);
  921. }
  922. /*
  923. * For free nid management
  924. */
  925. enum nid_state {
  926. FREE_NID, /* newly added to free nid list */
  927. PREALLOC_NID, /* it is preallocated */
  928. MAX_NID_STATE,
  929. };
  930. enum nat_state {
  931. TOTAL_NAT,
  932. DIRTY_NAT,
  933. RECLAIMABLE_NAT,
  934. MAX_NAT_STATE,
  935. };
  936. struct f2fs_nm_info {
  937. block_t nat_blkaddr; /* base disk address of NAT */
  938. nid_t max_nid; /* maximum possible node ids */
  939. nid_t available_nids; /* # of available node ids */
  940. nid_t next_scan_nid; /* the next nid to be scanned */
  941. nid_t max_rf_node_blocks; /* max # of nodes for recovery */
  942. unsigned int ram_thresh; /* control the memory footprint */
  943. unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
  944. unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
  945. /* NAT cache management */
  946. struct radix_tree_root nat_root;/* root of the nat entry cache */
  947. struct radix_tree_root nat_set_root;/* root of the nat set cache */
  948. struct f2fs_rwsem nat_tree_lock; /* protect nat entry tree */
  949. struct list_head nat_entries; /* cached nat entry list (clean) */
  950. spinlock_t nat_list_lock; /* protect clean nat entry list */
  951. unsigned int nat_cnt[MAX_NAT_STATE]; /* the # of cached nat entries */
  952. unsigned int nat_blocks; /* # of nat blocks */
  953. /* free node ids management */
  954. struct radix_tree_root free_nid_root;/* root of the free_nid cache */
  955. struct list_head free_nid_list; /* list for free nids excluding preallocated nids */
  956. unsigned int nid_cnt[MAX_NID_STATE]; /* the number of free node id */
  957. spinlock_t nid_list_lock; /* protect nid lists ops */
  958. struct mutex build_lock; /* lock for build free nids */
  959. unsigned char **free_nid_bitmap;
  960. unsigned char *nat_block_bitmap;
  961. unsigned short *free_nid_count; /* free nid count of NAT block */
  962. /* for checkpoint */
  963. char *nat_bitmap; /* NAT bitmap pointer */
  964. unsigned int nat_bits_blocks; /* # of nat bits blocks */
  965. unsigned char *nat_bits; /* NAT bits blocks */
  966. unsigned char *full_nat_bits; /* full NAT pages */
  967. unsigned char *empty_nat_bits; /* empty NAT pages */
  968. #ifdef CONFIG_F2FS_CHECK_FS
  969. char *nat_bitmap_mir; /* NAT bitmap mirror */
  970. #endif
  971. int bitmap_size; /* bitmap size */
  972. };
  973. /*
  974. * this structure is used as one of function parameters.
  975. * all the information are dedicated to a given direct node block determined
  976. * by the data offset in a file.
  977. */
  978. struct dnode_of_data {
  979. struct inode *inode; /* vfs inode pointer */
  980. struct folio *inode_folio; /* its inode folio, NULL is possible */
  981. struct folio *node_folio; /* cached direct node folio */
  982. nid_t nid; /* node id of the direct node block */
  983. unsigned int ofs_in_node; /* data offset in the node page */
  984. bool inode_folio_locked; /* inode folio is locked or not */
  985. bool node_changed; /* is node block changed */
  986. char cur_level; /* level of hole node page */
  987. char max_level; /* level of current page located */
  988. block_t data_blkaddr; /* block address of the node block */
  989. };
  990. static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
  991. struct folio *ifolio, struct folio *nfolio, nid_t nid)
  992. {
  993. memset(dn, 0, sizeof(*dn));
  994. dn->inode = inode;
  995. dn->inode_folio = ifolio;
  996. dn->node_folio = nfolio;
  997. dn->nid = nid;
  998. }
  999. /*
  1000. * For SIT manager
  1001. *
  1002. * By default, there are 6 active log areas across the whole main area.
  1003. * When considering hot and cold data separation to reduce cleaning overhead,
  1004. * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
  1005. * respectively.
  1006. * In the current design, you should not change the numbers intentionally.
  1007. * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
  1008. * logs individually according to the underlying devices. (default: 6)
  1009. * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
  1010. * data and 8 for node logs.
  1011. */
  1012. #define NR_CURSEG_DATA_TYPE (3)
  1013. #define NR_CURSEG_NODE_TYPE (3)
  1014. #define NR_CURSEG_INMEM_TYPE (2)
  1015. #define NR_CURSEG_RO_TYPE (2)
  1016. #define NR_CURSEG_PERSIST_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
  1017. #define NR_CURSEG_TYPE (NR_CURSEG_INMEM_TYPE + NR_CURSEG_PERSIST_TYPE)
  1018. enum log_type {
  1019. CURSEG_HOT_DATA = 0, /* directory entry blocks */
  1020. CURSEG_WARM_DATA, /* data blocks */
  1021. CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
  1022. CURSEG_HOT_NODE, /* direct node blocks of directory files */
  1023. CURSEG_WARM_NODE, /* direct node blocks of normal files */
  1024. CURSEG_COLD_NODE, /* indirect node blocks */
  1025. NR_PERSISTENT_LOG, /* number of persistent log */
  1026. CURSEG_COLD_DATA_PINNED = NR_PERSISTENT_LOG,
  1027. /* pinned file that needs consecutive block address */
  1028. CURSEG_ALL_DATA_ATGC, /* SSR alloctor in hot/warm/cold data area */
  1029. NO_CHECK_TYPE, /* number of persistent & inmem log */
  1030. };
  1031. struct flush_cmd {
  1032. struct completion wait;
  1033. struct llist_node llnode;
  1034. nid_t ino;
  1035. int ret;
  1036. };
  1037. struct flush_cmd_control {
  1038. struct task_struct *f2fs_issue_flush; /* flush thread */
  1039. wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
  1040. atomic_t issued_flush; /* # of issued flushes */
  1041. atomic_t queued_flush; /* # of queued flushes */
  1042. struct llist_head issue_list; /* list for command issue */
  1043. struct llist_node *dispatch_list; /* list for command dispatch */
  1044. };
  1045. struct f2fs_sm_info {
  1046. struct sit_info *sit_info; /* whole segment information */
  1047. struct free_segmap_info *free_info; /* free segment information */
  1048. struct dirty_seglist_info *dirty_info; /* dirty segment information */
  1049. struct curseg_info *curseg_array; /* active segment information */
  1050. struct f2fs_rwsem curseg_lock; /* for preventing curseg change */
  1051. block_t seg0_blkaddr; /* block address of 0'th segment */
  1052. block_t main_blkaddr; /* start block address of main area */
  1053. block_t ssa_blkaddr; /* start block address of SSA area */
  1054. unsigned int segment_count; /* total # of segments */
  1055. unsigned int main_segments; /* # of segments in main area */
  1056. unsigned int reserved_segments; /* # of reserved segments */
  1057. unsigned int ovp_segments; /* # of overprovision segments */
  1058. /* a threshold to reclaim prefree segments */
  1059. unsigned int rec_prefree_segments;
  1060. struct list_head sit_entry_set; /* sit entry set list */
  1061. unsigned int ipu_policy; /* in-place-update policy */
  1062. unsigned int min_ipu_util; /* in-place-update threshold */
  1063. unsigned int min_fsync_blocks; /* threshold for fsync */
  1064. unsigned int min_seq_blocks; /* threshold for sequential blocks */
  1065. unsigned int min_hot_blocks; /* threshold for hot block allocation */
  1066. unsigned int min_ssr_sections; /* threshold to trigger SSR allocation */
  1067. /* for flush command control */
  1068. struct flush_cmd_control *fcc_info;
  1069. /* for discard command control */
  1070. struct discard_cmd_control *dcc_info;
  1071. };
  1072. /*
  1073. * For superblock
  1074. */
  1075. /*
  1076. * COUNT_TYPE for monitoring
  1077. *
  1078. * f2fs monitors the number of several block types such as on-writeback,
  1079. * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
  1080. */
  1081. #define WB_DATA_TYPE(folio, f) \
  1082. (f || f2fs_is_cp_guaranteed(folio) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
  1083. enum count_type {
  1084. F2FS_DIRTY_DENTS,
  1085. F2FS_DIRTY_DATA,
  1086. F2FS_DIRTY_QDATA,
  1087. F2FS_DIRTY_NODES,
  1088. F2FS_DIRTY_META,
  1089. F2FS_DIRTY_IMETA,
  1090. F2FS_WB_CP_DATA,
  1091. F2FS_WB_DATA,
  1092. F2FS_RD_DATA,
  1093. F2FS_RD_NODE,
  1094. F2FS_RD_META,
  1095. F2FS_DIO_WRITE,
  1096. F2FS_DIO_READ,
  1097. F2FS_SKIPPED_WRITE, /* skip or fail during f2fs_enable_checkpoint() */
  1098. NR_COUNT_TYPE,
  1099. };
  1100. /*
  1101. * The below are the page types of bios used in submit_bio().
  1102. * The available types are:
  1103. * DATA User data pages. It operates as async mode.
  1104. * NODE Node pages. It operates as async mode.
  1105. * META FS metadata pages such as SIT, NAT, CP.
  1106. * NR_PAGE_TYPE The number of page types.
  1107. * META_FLUSH Make sure the previous pages are written
  1108. * with waiting the bio's completion
  1109. * ... Only can be used with META.
  1110. */
  1111. #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
  1112. #define PAGE_TYPE_ON_MAIN(type) ((type) == DATA || (type) == NODE)
  1113. enum page_type {
  1114. DATA = 0,
  1115. NODE = 1, /* should not change this */
  1116. META,
  1117. NR_PAGE_TYPE,
  1118. META_FLUSH,
  1119. IPU, /* the below types are used by tracepoints only. */
  1120. OPU,
  1121. };
  1122. enum temp_type {
  1123. HOT = 0, /* must be zero for meta bio */
  1124. WARM,
  1125. COLD,
  1126. NR_TEMP_TYPE,
  1127. };
  1128. enum need_lock_type {
  1129. LOCK_REQ = 0,
  1130. LOCK_DONE,
  1131. LOCK_RETRY,
  1132. };
  1133. enum cp_reason_type {
  1134. CP_NO_NEEDED,
  1135. CP_NON_REGULAR,
  1136. CP_COMPRESSED,
  1137. CP_HARDLINK,
  1138. CP_SB_NEED_CP,
  1139. CP_WRONG_PINO,
  1140. CP_NO_SPC_ROLL,
  1141. CP_NODE_NEED_CP,
  1142. CP_FASTBOOT_MODE,
  1143. CP_SPEC_LOG_NUM,
  1144. CP_RECOVER_DIR,
  1145. CP_XATTR_DIR,
  1146. };
  1147. enum iostat_type {
  1148. /* WRITE IO */
  1149. APP_DIRECT_IO, /* app direct write IOs */
  1150. APP_BUFFERED_IO, /* app buffered write IOs */
  1151. APP_WRITE_IO, /* app write IOs */
  1152. APP_MAPPED_IO, /* app mapped IOs */
  1153. APP_BUFFERED_CDATA_IO, /* app buffered write IOs on compressed file */
  1154. APP_MAPPED_CDATA_IO, /* app mapped write IOs on compressed file */
  1155. FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */
  1156. FS_CDATA_IO, /* data IOs from kworker/fsync/reclaimer on compressed file */
  1157. FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */
  1158. FS_META_IO, /* meta IOs from kworker/reclaimer */
  1159. FS_GC_DATA_IO, /* data IOs from forground gc */
  1160. FS_GC_NODE_IO, /* node IOs from forground gc */
  1161. FS_CP_DATA_IO, /* data IOs from checkpoint */
  1162. FS_CP_NODE_IO, /* node IOs from checkpoint */
  1163. FS_CP_META_IO, /* meta IOs from checkpoint */
  1164. /* READ IO */
  1165. APP_DIRECT_READ_IO, /* app direct read IOs */
  1166. APP_BUFFERED_READ_IO, /* app buffered read IOs */
  1167. APP_READ_IO, /* app read IOs */
  1168. APP_MAPPED_READ_IO, /* app mapped read IOs */
  1169. APP_BUFFERED_CDATA_READ_IO, /* app buffered read IOs on compressed file */
  1170. APP_MAPPED_CDATA_READ_IO, /* app mapped read IOs on compressed file */
  1171. FS_DATA_READ_IO, /* data read IOs */
  1172. FS_GDATA_READ_IO, /* data read IOs from background gc */
  1173. FS_CDATA_READ_IO, /* compressed data read IOs */
  1174. FS_NODE_READ_IO, /* node read IOs */
  1175. FS_META_READ_IO, /* meta read IOs */
  1176. /* other */
  1177. FS_DISCARD_IO, /* discard */
  1178. FS_FLUSH_IO, /* flush */
  1179. FS_ZONE_RESET_IO, /* zone reset */
  1180. NR_IO_TYPE,
  1181. };
  1182. struct f2fs_io_info {
  1183. struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
  1184. nid_t ino; /* inode number */
  1185. enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
  1186. enum temp_type temp; /* contains HOT/WARM/COLD */
  1187. enum req_op op; /* contains REQ_OP_ */
  1188. blk_opf_t op_flags; /* req_flag_bits */
  1189. block_t new_blkaddr; /* new block address to be written */
  1190. block_t old_blkaddr; /* old block address before Cow */
  1191. union {
  1192. struct page *page; /* page to be written */
  1193. struct folio *folio;
  1194. };
  1195. struct page *encrypted_page; /* encrypted page */
  1196. struct page *compressed_page; /* compressed page */
  1197. struct list_head list; /* serialize IOs */
  1198. unsigned int compr_blocks; /* # of compressed block addresses */
  1199. unsigned int need_lock:8; /* indicate we need to lock cp_rwsem */
  1200. unsigned int version:8; /* version of the node */
  1201. unsigned int submitted:1; /* indicate IO submission */
  1202. unsigned int in_list:1; /* indicate fio is in io_list */
  1203. unsigned int is_por:1; /* indicate IO is from recovery or not */
  1204. unsigned int encrypted:1; /* indicate file is encrypted */
  1205. unsigned int meta_gc:1; /* require meta inode GC */
  1206. enum iostat_type io_type; /* io type */
  1207. struct writeback_control *io_wbc; /* writeback control */
  1208. struct bio **bio; /* bio for ipu */
  1209. sector_t *last_block; /* last block number in bio */
  1210. };
  1211. struct bio_entry {
  1212. struct bio *bio;
  1213. struct list_head list;
  1214. };
  1215. #define is_read_io(rw) ((rw) == READ)
  1216. struct f2fs_bio_info {
  1217. struct f2fs_sb_info *sbi; /* f2fs superblock */
  1218. struct bio *bio; /* bios to merge */
  1219. sector_t last_block_in_bio; /* last block number */
  1220. struct f2fs_io_info fio; /* store buffered io info. */
  1221. #ifdef CONFIG_BLK_DEV_ZONED
  1222. struct completion zone_wait; /* condition value for the previous open zone to close */
  1223. struct bio *zone_pending_bio; /* pending bio for the previous zone */
  1224. void *bi_private; /* previous bi_private for pending bio */
  1225. #endif
  1226. struct f2fs_rwsem io_rwsem; /* blocking op for bio */
  1227. spinlock_t io_lock; /* serialize DATA/NODE IOs */
  1228. struct list_head io_list; /* track fios */
  1229. struct list_head bio_list; /* bio entry list head */
  1230. struct f2fs_rwsem bio_list_lock; /* lock to protect bio entry list */
  1231. };
  1232. #define FDEV(i) (sbi->devs[i])
  1233. #define RDEV(i) (raw_super->devs[i])
  1234. struct f2fs_dev_info {
  1235. struct file *bdev_file;
  1236. struct block_device *bdev;
  1237. char path[MAX_PATH_LEN + 1];
  1238. unsigned int total_segments;
  1239. block_t start_blk;
  1240. block_t end_blk;
  1241. #ifdef CONFIG_BLK_DEV_ZONED
  1242. unsigned int nr_blkz; /* Total number of zones */
  1243. unsigned long *blkz_seq; /* Bitmap indicating sequential zones */
  1244. #endif
  1245. };
  1246. enum inode_type {
  1247. DIR_INODE, /* for dirty dir inode */
  1248. FILE_INODE, /* for dirty regular/symlink inode */
  1249. DIRTY_META, /* for all dirtied inode metadata */
  1250. DONATE_INODE, /* for all inode to donate pages */
  1251. NR_INODE_TYPE,
  1252. };
  1253. /* for inner inode cache management */
  1254. struct inode_management {
  1255. struct radix_tree_root ino_root; /* ino entry array */
  1256. spinlock_t ino_lock; /* for ino entry lock */
  1257. struct list_head ino_list; /* inode list head */
  1258. unsigned long ino_num; /* number of entries */
  1259. };
  1260. /* for GC_AT */
  1261. struct atgc_management {
  1262. bool atgc_enabled; /* ATGC is enabled or not */
  1263. struct rb_root_cached root; /* root of victim rb-tree */
  1264. struct list_head victim_list; /* linked with all victim entries */
  1265. unsigned int victim_count; /* victim count in rb-tree */
  1266. unsigned int candidate_ratio; /* candidate ratio */
  1267. unsigned int max_candidate_count; /* max candidate count */
  1268. unsigned int age_weight; /* age weight, vblock_weight = 100 - age_weight */
  1269. unsigned long long age_threshold; /* age threshold */
  1270. };
  1271. struct f2fs_time_stat {
  1272. unsigned long long total_time; /* total wall clock time */
  1273. #ifdef CONFIG_64BIT
  1274. unsigned long long running_time; /* running time */
  1275. #endif
  1276. #if defined(CONFIG_SCHED_INFO) && defined(CONFIG_SCHEDSTATS)
  1277. unsigned long long runnable_time; /* runnable(including preempted) time */
  1278. #endif
  1279. #ifdef CONFIG_TASK_DELAY_ACCT
  1280. unsigned long long io_sleep_time; /* IO sleep time */
  1281. #endif
  1282. };
  1283. struct f2fs_lock_context {
  1284. struct f2fs_time_stat ts;
  1285. int orig_nice;
  1286. int new_nice;
  1287. bool lock_trace;
  1288. bool need_restore;
  1289. };
  1290. struct f2fs_gc_control {
  1291. unsigned int victim_segno; /* target victim segment number */
  1292. int init_gc_type; /* FG_GC or BG_GC */
  1293. bool no_bg_gc; /* check the space and stop bg_gc */
  1294. bool should_migrate_blocks; /* should migrate blocks */
  1295. bool err_gc_skipped; /* return EAGAIN if GC skipped */
  1296. bool one_time; /* require one time GC in one migration unit */
  1297. unsigned int nr_free_secs; /* # of free sections to do GC */
  1298. struct f2fs_lock_context lc; /* lock context for gc_lock */
  1299. };
  1300. /*
  1301. * For s_flag in struct f2fs_sb_info
  1302. * Modification on enum should be synchronized with s_flag array
  1303. */
  1304. enum {
  1305. SBI_IS_DIRTY, /* dirty flag for checkpoint */
  1306. SBI_IS_CLOSE, /* specify unmounting */
  1307. SBI_NEED_FSCK, /* need fsck.f2fs to fix */
  1308. SBI_POR_DOING, /* recovery is doing or not */
  1309. SBI_NEED_SB_WRITE, /* need to recover superblock */
  1310. SBI_NEED_CP, /* need to checkpoint */
  1311. SBI_IS_SHUTDOWN, /* shutdown by ioctl */
  1312. SBI_IS_RECOVERED, /* recovered orphan/data */
  1313. SBI_CP_DISABLED, /* CP was disabled last mount */
  1314. SBI_CP_DISABLED_QUICK, /* CP was disabled quickly */
  1315. SBI_QUOTA_NEED_FLUSH, /* need to flush quota info in CP */
  1316. SBI_QUOTA_SKIP_FLUSH, /* skip flushing quota in current CP */
  1317. SBI_QUOTA_NEED_REPAIR, /* quota file may be corrupted */
  1318. SBI_IS_RESIZEFS, /* resizefs is in process */
  1319. SBI_IS_FREEZING, /* freezefs is in process */
  1320. SBI_IS_WRITABLE, /* remove ro mountoption transiently */
  1321. SBI_ENABLE_CHECKPOINT, /* indicate it's during f2fs_enable_checkpoint() */
  1322. MAX_SBI_FLAG,
  1323. };
  1324. enum {
  1325. CP_TIME,
  1326. REQ_TIME,
  1327. DISCARD_TIME,
  1328. GC_TIME,
  1329. DISABLE_TIME,
  1330. UMOUNT_DISCARD_TIMEOUT,
  1331. MAX_TIME,
  1332. };
  1333. /* Note that you need to keep synchronization with this gc_mode_names array */
  1334. enum {
  1335. GC_NORMAL,
  1336. GC_IDLE_CB,
  1337. GC_IDLE_GREEDY,
  1338. GC_IDLE_AT,
  1339. GC_URGENT_HIGH,
  1340. GC_URGENT_LOW,
  1341. GC_URGENT_MID,
  1342. MAX_GC_MODE,
  1343. };
  1344. enum {
  1345. BGGC_MODE_ON, /* background gc is on */
  1346. BGGC_MODE_OFF, /* background gc is off */
  1347. BGGC_MODE_SYNC, /*
  1348. * background gc is on, migrating blocks
  1349. * like foreground gc
  1350. */
  1351. };
  1352. enum {
  1353. FS_MODE_ADAPTIVE, /* use both lfs/ssr allocation */
  1354. FS_MODE_LFS, /* use lfs allocation only */
  1355. FS_MODE_FRAGMENT_SEG, /* segment fragmentation mode */
  1356. FS_MODE_FRAGMENT_BLK, /* block fragmentation mode */
  1357. };
  1358. enum {
  1359. ALLOC_MODE_DEFAULT, /* stay default */
  1360. ALLOC_MODE_REUSE, /* reuse segments as much as possible */
  1361. };
  1362. enum fsync_mode {
  1363. FSYNC_MODE_POSIX, /* fsync follows posix semantics */
  1364. FSYNC_MODE_STRICT, /* fsync behaves in line with ext4 */
  1365. FSYNC_MODE_NOBARRIER, /* fsync behaves nobarrier based on posix */
  1366. };
  1367. enum {
  1368. COMPR_MODE_FS, /*
  1369. * automatically compress compression
  1370. * enabled files
  1371. */
  1372. COMPR_MODE_USER, /*
  1373. * automatical compression is disabled.
  1374. * user can control the file compression
  1375. * using ioctls
  1376. */
  1377. };
  1378. enum {
  1379. DISCARD_UNIT_BLOCK, /* basic discard unit is block */
  1380. DISCARD_UNIT_SEGMENT, /* basic discard unit is segment */
  1381. DISCARD_UNIT_SECTION, /* basic discard unit is section */
  1382. };
  1383. enum {
  1384. MEMORY_MODE_NORMAL, /* memory mode for normal devices */
  1385. MEMORY_MODE_LOW, /* memory mode for low memory devices */
  1386. };
  1387. enum errors_option {
  1388. MOUNT_ERRORS_READONLY, /* remount fs ro on errors */
  1389. MOUNT_ERRORS_CONTINUE, /* continue on errors */
  1390. MOUNT_ERRORS_PANIC, /* panic on errors */
  1391. };
  1392. enum {
  1393. BACKGROUND,
  1394. FOREGROUND,
  1395. MAX_CALL_TYPE,
  1396. TOTAL_CALL = FOREGROUND,
  1397. };
  1398. enum f2fs_lookup_mode {
  1399. LOOKUP_PERF,
  1400. LOOKUP_COMPAT,
  1401. LOOKUP_AUTO,
  1402. };
  1403. /* For node type in __get_node_folio() */
  1404. enum node_type {
  1405. NODE_TYPE_REGULAR,
  1406. NODE_TYPE_INODE,
  1407. NODE_TYPE_XATTR,
  1408. NODE_TYPE_NON_INODE,
  1409. };
  1410. /* a threshold of maximum elapsed time in critical region to print tracepoint */
  1411. #define MAX_LOCK_ELAPSED_TIME 500
  1412. #define F2FS_DEFAULT_TASK_PRIORITY (DEFAULT_PRIO)
  1413. #define F2FS_CRITICAL_TASK_PRIORITY NICE_TO_PRIO(0)
  1414. static inline int f2fs_test_bit(unsigned int nr, char *addr);
  1415. static inline void f2fs_set_bit(unsigned int nr, char *addr);
  1416. static inline void f2fs_clear_bit(unsigned int nr, char *addr);
  1417. /*
  1418. * Layout of f2fs page.private:
  1419. *
  1420. * Layout A: lowest bit should be 1
  1421. * | bit0 = 1 | bit1 | bit2 | ... | bit MAX | private data .... |
  1422. * bit 0 PAGE_PRIVATE_NOT_POINTER
  1423. * bit 1 PAGE_PRIVATE_ONGOING_MIGRATION
  1424. * bit 2 PAGE_PRIVATE_INLINE_INODE
  1425. * bit 3 PAGE_PRIVATE_REF_RESOURCE
  1426. * bit 4 PAGE_PRIVATE_ATOMIC_WRITE
  1427. * bit 5- f2fs private data
  1428. *
  1429. * Layout B: lowest bit should be 0
  1430. * page.private is a wrapped pointer.
  1431. */
  1432. enum {
  1433. PAGE_PRIVATE_NOT_POINTER, /* private contains non-pointer data */
  1434. PAGE_PRIVATE_ONGOING_MIGRATION, /* data page which is on-going migrating */
  1435. PAGE_PRIVATE_INLINE_INODE, /* inode page contains inline data */
  1436. PAGE_PRIVATE_REF_RESOURCE, /* dirty page has referenced resources */
  1437. PAGE_PRIVATE_ATOMIC_WRITE, /* data page from atomic write path */
  1438. PAGE_PRIVATE_MAX
  1439. };
  1440. /* For compression */
  1441. enum compress_algorithm_type {
  1442. COMPRESS_LZO,
  1443. COMPRESS_LZ4,
  1444. COMPRESS_ZSTD,
  1445. COMPRESS_LZORLE,
  1446. COMPRESS_MAX,
  1447. };
  1448. enum compress_flag {
  1449. COMPRESS_CHKSUM,
  1450. COMPRESS_MAX_FLAG,
  1451. };
  1452. #define COMPRESS_WATERMARK 20
  1453. #define COMPRESS_PERCENT 20
  1454. #define COMPRESS_DATA_RESERVED_SIZE 4
  1455. struct compress_data {
  1456. __le32 clen; /* compressed data size */
  1457. __le32 chksum; /* compressed data checksum */
  1458. __le32 reserved[COMPRESS_DATA_RESERVED_SIZE]; /* reserved */
  1459. u8 cdata[]; /* compressed data */
  1460. };
  1461. #define COMPRESS_HEADER_SIZE (sizeof(struct compress_data))
  1462. #define F2FS_COMPRESSED_PAGE_MAGIC 0xF5F2C000
  1463. #define F2FS_ZSTD_DEFAULT_CLEVEL 1
  1464. #define COMPRESS_LEVEL_OFFSET 8
  1465. /* compress context */
  1466. struct compress_ctx {
  1467. struct inode *inode; /* inode the context belong to */
  1468. pgoff_t cluster_idx; /* cluster index number */
  1469. unsigned int cluster_size; /* page count in cluster */
  1470. unsigned int log_cluster_size; /* log of cluster size */
  1471. struct page **rpages; /* pages store raw data in cluster */
  1472. unsigned int nr_rpages; /* total page number in rpages */
  1473. struct page **cpages; /* pages store compressed data in cluster */
  1474. unsigned int nr_cpages; /* total page number in cpages */
  1475. unsigned int valid_nr_cpages; /* valid page number in cpages */
  1476. void *rbuf; /* virtual mapped address on rpages */
  1477. struct compress_data *cbuf; /* virtual mapped address on cpages */
  1478. size_t rlen; /* valid data length in rbuf */
  1479. size_t clen; /* valid data length in cbuf */
  1480. void *private; /* payload buffer for specified compression algorithm */
  1481. void *private2; /* extra payload buffer */
  1482. struct fsverity_info *vi; /* verity info if needed */
  1483. };
  1484. /* compress context for write IO path */
  1485. struct compress_io_ctx {
  1486. u32 magic; /* magic number to indicate page is compressed */
  1487. struct inode *inode; /* inode the context belong to */
  1488. struct page **rpages; /* pages store raw data in cluster */
  1489. unsigned int nr_rpages; /* total page number in rpages */
  1490. atomic_t pending_pages; /* in-flight compressed page count */
  1491. };
  1492. /* Context for decompressing one cluster on the read IO path */
  1493. struct decompress_io_ctx {
  1494. u32 magic; /* magic number to indicate page is compressed */
  1495. struct inode *inode; /* inode the context belong to */
  1496. struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
  1497. pgoff_t cluster_idx; /* cluster index number */
  1498. unsigned int cluster_size; /* page count in cluster */
  1499. unsigned int log_cluster_size; /* log of cluster size */
  1500. struct page **rpages; /* pages store raw data in cluster */
  1501. unsigned int nr_rpages; /* total page number in rpages */
  1502. struct page **cpages; /* pages store compressed data in cluster */
  1503. unsigned int nr_cpages; /* total page number in cpages */
  1504. struct page **tpages; /* temp pages to pad holes in cluster */
  1505. void *rbuf; /* virtual mapped address on rpages */
  1506. struct compress_data *cbuf; /* virtual mapped address on cpages */
  1507. size_t rlen; /* valid data length in rbuf */
  1508. size_t clen; /* valid data length in cbuf */
  1509. /*
  1510. * The number of compressed pages remaining to be read in this cluster.
  1511. * This is initially nr_cpages. It is decremented by 1 each time a page
  1512. * has been read (or failed to be read). When it reaches 0, the cluster
  1513. * is decompressed (or an error is reported).
  1514. *
  1515. * If an error occurs before all the pages have been submitted for I/O,
  1516. * then this will never reach 0. In this case the I/O submitter is
  1517. * responsible for calling f2fs_decompress_end_io() instead.
  1518. */
  1519. atomic_t remaining_pages;
  1520. /*
  1521. * Number of references to this decompress_io_ctx.
  1522. *
  1523. * One reference is held for I/O completion. This reference is dropped
  1524. * after the pagecache pages are updated and unlocked -- either after
  1525. * decompression (and verity if enabled), or after an error.
  1526. *
  1527. * In addition, each compressed page holds a reference while it is in a
  1528. * bio. These references are necessary prevent compressed pages from
  1529. * being freed while they are still in a bio.
  1530. */
  1531. refcount_t refcnt;
  1532. bool failed; /* IO error occurred before decompression? */
  1533. struct fsverity_info *vi; /* fs-verity context if needed */
  1534. unsigned char compress_algorithm; /* backup algorithm type */
  1535. void *private; /* payload buffer for specified decompression algorithm */
  1536. void *private2; /* extra payload buffer */
  1537. struct work_struct verity_work; /* work to verify the decompressed pages */
  1538. struct work_struct free_work; /* work for late free this structure itself */
  1539. };
  1540. #define NULL_CLUSTER ((unsigned int)(~0))
  1541. #define MIN_COMPRESS_LOG_SIZE 2
  1542. #define MAX_COMPRESS_LOG_SIZE 8
  1543. #define MAX_COMPRESS_WINDOW_SIZE(log_size) ((PAGE_SIZE) << (log_size))
  1544. struct f2fs_sb_info {
  1545. struct super_block *sb; /* pointer to VFS super block */
  1546. struct proc_dir_entry *s_proc; /* proc entry */
  1547. struct f2fs_super_block *raw_super; /* raw super block pointer */
  1548. struct f2fs_rwsem sb_lock; /* lock for raw super block */
  1549. int valid_super_block; /* valid super block no */
  1550. unsigned long s_flag; /* flags for sbi */
  1551. struct mutex writepages; /* mutex for writepages() */
  1552. #ifdef CONFIG_BLK_DEV_ZONED
  1553. unsigned int blocks_per_blkz; /* F2FS blocks per zone */
  1554. unsigned int unusable_blocks_per_sec; /* unusable blocks per section */
  1555. unsigned int max_open_zones; /* max open zone resources of the zoned device */
  1556. /* For adjust the priority writing position of data in zone UFS */
  1557. unsigned int blkzone_alloc_policy;
  1558. #endif
  1559. /* for node-related operations */
  1560. struct f2fs_nm_info *nm_info; /* node manager */
  1561. struct inode *node_inode; /* cache node blocks */
  1562. /* for segment-related operations */
  1563. struct f2fs_sm_info *sm_info; /* segment manager */
  1564. /* for bio operations */
  1565. struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
  1566. /* keep migration IO order for LFS mode */
  1567. struct f2fs_rwsem io_order_lock;
  1568. pgoff_t page_eio_ofs[NR_PAGE_TYPE]; /* EIO page offset */
  1569. int page_eio_cnt[NR_PAGE_TYPE]; /* EIO count */
  1570. /* for checkpoint */
  1571. struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
  1572. int cur_cp_pack; /* remain current cp pack */
  1573. spinlock_t cp_lock; /* for flag in ckpt */
  1574. struct inode *meta_inode; /* cache meta blocks */
  1575. struct f2fs_rwsem cp_global_sem; /* checkpoint procedure lock */
  1576. struct f2fs_rwsem cp_rwsem; /* blocking FS operations */
  1577. struct f2fs_rwsem node_write; /* locking node writes */
  1578. struct f2fs_rwsem node_change; /* locking node change */
  1579. wait_queue_head_t cp_wait;
  1580. unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
  1581. long interval_time[MAX_TIME]; /* to store thresholds */
  1582. struct ckpt_req_control cprc_info; /* for checkpoint request control */
  1583. struct cp_stats cp_stats; /* for time stat of checkpoint */
  1584. struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
  1585. spinlock_t fsync_node_lock; /* for node entry lock */
  1586. struct list_head fsync_node_list; /* node list head */
  1587. unsigned int fsync_seg_id; /* sequence id */
  1588. unsigned int fsync_node_num; /* number of node entries */
  1589. /* for orphan inode, use 0'th array */
  1590. unsigned int max_orphans; /* max orphan inodes */
  1591. /* for inode management */
  1592. struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
  1593. spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
  1594. struct mutex flush_lock; /* for flush exclusion */
  1595. /* for extent tree cache */
  1596. struct extent_tree_info extent_tree[NR_EXTENT_CACHES];
  1597. atomic64_t allocated_data_blocks; /* for block age extent_cache */
  1598. unsigned int max_read_extent_count; /* max read extent count per inode */
  1599. /* The threshold used for hot and warm data seperation*/
  1600. unsigned int hot_data_age_threshold;
  1601. unsigned int warm_data_age_threshold;
  1602. unsigned int last_age_weight;
  1603. /* control donate caches */
  1604. unsigned int donate_files;
  1605. /* basic filesystem units */
  1606. unsigned int log_sectors_per_block; /* log2 sectors per block */
  1607. unsigned int log_blocksize; /* log2 block size */
  1608. unsigned int blocksize; /* block size */
  1609. unsigned int root_ino_num; /* root inode number*/
  1610. unsigned int node_ino_num; /* node inode number*/
  1611. unsigned int meta_ino_num; /* meta inode number*/
  1612. unsigned int log_blocks_per_seg; /* log2 blocks per segment */
  1613. unsigned int blocks_per_seg; /* blocks per segment */
  1614. unsigned int segs_per_sec; /* segments per section */
  1615. unsigned int secs_per_zone; /* sections per zone */
  1616. unsigned int total_sections; /* total section count */
  1617. unsigned int total_node_count; /* total node block count */
  1618. unsigned int total_valid_node_count; /* valid node block count */
  1619. int dir_level; /* directory level */
  1620. bool readdir_ra; /* readahead inode in readdir */
  1621. unsigned int max_io_bytes; /* max io bytes to merge IOs */
  1622. /* variable summary block units */
  1623. unsigned int sum_blocksize; /* sum block size */
  1624. unsigned int sums_per_block; /* sum block count per block */
  1625. unsigned int entries_in_sum; /* entry count in sum block */
  1626. unsigned int sum_entry_size; /* total entry size in sum block */
  1627. unsigned int sum_journal_size; /* journal size in sum block */
  1628. unsigned int nat_journal_entries; /* nat journal entry count in the journal */
  1629. unsigned int sit_journal_entries; /* sit journal entry count in the journal */
  1630. block_t user_block_count; /* # of user blocks */
  1631. block_t total_valid_block_count; /* # of valid blocks */
  1632. block_t discard_blks; /* discard command candidats */
  1633. block_t last_valid_block_count; /* for recovery */
  1634. block_t reserved_blocks; /* configurable reserved blocks */
  1635. block_t current_reserved_blocks; /* current reserved blocks */
  1636. /* Additional tracking for no checkpoint mode */
  1637. block_t unusable_block_count; /* # of blocks saved by last cp */
  1638. unsigned int nquota_files; /* # of quota sysfile */
  1639. struct f2fs_rwsem quota_sem; /* blocking cp for flags */
  1640. struct task_struct *umount_lock_holder; /* s_umount lock holder */
  1641. /* # of pages, see count_type */
  1642. atomic_t nr_pages[NR_COUNT_TYPE];
  1643. /* # of allocated blocks */
  1644. struct percpu_counter alloc_valid_block_count;
  1645. /* # of node block writes as roll forward recovery */
  1646. struct percpu_counter rf_node_block_count;
  1647. /* writeback control */
  1648. atomic_t wb_sync_req[META]; /* count # of WB_SYNC threads */
  1649. /* valid inode count */
  1650. struct percpu_counter total_valid_inode_count;
  1651. struct f2fs_mount_info mount_opt; /* mount options */
  1652. /* for cleaning operations */
  1653. struct f2fs_rwsem gc_lock; /*
  1654. * semaphore for GC, avoid
  1655. * race between GC and GC or CP
  1656. */
  1657. struct f2fs_gc_kthread *gc_thread; /* GC thread */
  1658. struct atgc_management am; /* atgc management */
  1659. unsigned int cur_victim_sec; /* current victim section num */
  1660. unsigned int gc_mode; /* current GC state */
  1661. unsigned int next_victim_seg[2]; /* next segment in victim section */
  1662. spinlock_t gc_remaining_trials_lock;
  1663. /* remaining trial count for GC_URGENT_* and GC_IDLE_* */
  1664. unsigned int gc_remaining_trials;
  1665. /* for skip statistic */
  1666. unsigned long long skipped_gc_rwsem; /* FG_GC only */
  1667. /* free sections reserved for pinned file */
  1668. unsigned int reserved_pin_section;
  1669. /* threshold for gc trials on pinned files */
  1670. unsigned short gc_pin_file_threshold;
  1671. struct f2fs_rwsem pin_sem;
  1672. /* maximum # of trials to find a victim segment for SSR and GC */
  1673. unsigned int max_victim_search;
  1674. /* migration granularity of garbage collection, unit: segment */
  1675. unsigned int migration_granularity;
  1676. /* migration window granularity of garbage collection, unit: segment */
  1677. unsigned int migration_window_granularity;
  1678. /*
  1679. * for stat information.
  1680. * one is for the LFS mode, and the other is for the SSR mode.
  1681. */
  1682. #ifdef CONFIG_F2FS_STAT_FS
  1683. struct f2fs_stat_info *stat_info; /* FS status information */
  1684. atomic_t meta_count[META_MAX]; /* # of meta blocks */
  1685. unsigned int segment_count[2]; /* # of allocated segments */
  1686. unsigned int block_count[2]; /* # of allocated blocks */
  1687. atomic_t inplace_count; /* # of inplace update */
  1688. /* # of lookup extent cache */
  1689. atomic64_t total_hit_ext[NR_EXTENT_CACHES];
  1690. /* # of hit rbtree extent node */
  1691. atomic64_t read_hit_rbtree[NR_EXTENT_CACHES];
  1692. /* # of hit cached extent node */
  1693. atomic64_t read_hit_cached[NR_EXTENT_CACHES];
  1694. /* # of hit largest extent node in read extent cache */
  1695. atomic64_t read_hit_largest;
  1696. atomic_t inline_xattr; /* # of inline_xattr inodes */
  1697. atomic_t inline_inode; /* # of inline_data inodes */
  1698. atomic_t inline_dir; /* # of inline_dentry inodes */
  1699. atomic_t compr_inode; /* # of compressed inodes */
  1700. atomic64_t compr_blocks; /* # of compressed blocks */
  1701. atomic_t swapfile_inode; /* # of swapfile inodes */
  1702. atomic_t atomic_files; /* # of opened atomic file */
  1703. atomic_t max_aw_cnt; /* max # of atomic writes */
  1704. unsigned int io_skip_bggc; /* skip background gc for in-flight IO */
  1705. unsigned int other_skip_bggc; /* skip background gc for other reasons */
  1706. unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
  1707. atomic_t cp_call_count[MAX_CALL_TYPE]; /* # of cp call */
  1708. #endif
  1709. spinlock_t stat_lock; /* lock for stat operations */
  1710. /* to attach REQ_META|REQ_FUA flags */
  1711. unsigned int data_io_flag;
  1712. unsigned int node_io_flag;
  1713. /* For sysfs support */
  1714. struct kobject s_kobj; /* /sys/fs/f2fs/<devname> */
  1715. struct completion s_kobj_unregister;
  1716. struct kobject s_stat_kobj; /* /sys/fs/f2fs/<devname>/stat */
  1717. struct completion s_stat_kobj_unregister;
  1718. struct kobject s_feature_list_kobj; /* /sys/fs/f2fs/<devname>/feature_list */
  1719. struct completion s_feature_list_kobj_unregister;
  1720. /* For shrinker support */
  1721. struct list_head s_list;
  1722. struct mutex umount_mutex;
  1723. unsigned int shrinker_run_no;
  1724. /* For multi devices */
  1725. int s_ndevs; /* number of devices */
  1726. struct f2fs_dev_info *devs; /* for device list */
  1727. unsigned int dirty_device; /* for checkpoint data flush */
  1728. spinlock_t dev_lock; /* protect dirty_device */
  1729. bool aligned_blksize; /* all devices has the same logical blksize */
  1730. unsigned int first_seq_zone_segno; /* first segno in sequential zone */
  1731. unsigned int bggc_io_aware; /* For adjust the BG_GC priority when pending IO */
  1732. unsigned int allocate_section_hint; /* the boundary position between devices */
  1733. unsigned int allocate_section_policy; /* determine the section writing priority */
  1734. /* For write statistics */
  1735. u64 sectors_written_start;
  1736. u64 kbytes_written;
  1737. /* Precomputed FS UUID checksum for seeding other checksums */
  1738. __u32 s_chksum_seed;
  1739. struct workqueue_struct *post_read_wq; /* post read workqueue */
  1740. /*
  1741. * If we are in irq context, let's update error information into
  1742. * on-disk superblock in the work.
  1743. */
  1744. struct work_struct s_error_work;
  1745. unsigned char errors[MAX_F2FS_ERRORS]; /* error flags */
  1746. unsigned char stop_reason[MAX_STOP_REASON]; /* stop reason */
  1747. spinlock_t error_lock; /* protect errors/stop_reason array */
  1748. bool error_dirty; /* errors of sb is dirty */
  1749. /* For reclaimed segs statistics per each GC mode */
  1750. unsigned int gc_segment_mode; /* GC state for reclaimed segments */
  1751. unsigned int gc_reclaimed_segs[MAX_GC_MODE]; /* Reclaimed segs for each mode */
  1752. unsigned int seq_file_ra_mul; /* multiplier for ra_pages of seq. files in fadvise */
  1753. int max_fragment_chunk; /* max chunk size for block fragmentation mode */
  1754. int max_fragment_hole; /* max hole size for block fragmentation mode */
  1755. /* For atomic write statistics */
  1756. atomic64_t current_atomic_write;
  1757. s64 peak_atomic_write;
  1758. u64 committed_atomic_block;
  1759. u64 revoked_atomic_block;
  1760. /* carve out reserved_blocks from total blocks */
  1761. bool carve_out;
  1762. /* max elapsed time threshold in critical region that lock covered */
  1763. unsigned long long max_lock_elapsed_time;
  1764. /* enable/disable to adjust task priority in critical region covered by lock */
  1765. unsigned int adjust_lock_priority;
  1766. /* adjust priority for task which is in critical region covered by lock */
  1767. unsigned int lock_duration_priority;
  1768. /* priority for critical task, e.g. f2fs_ckpt, f2fs_gc threads */
  1769. long critical_task_priority;
  1770. #ifdef CONFIG_F2FS_FS_COMPRESSION
  1771. struct kmem_cache *page_array_slab; /* page array entry */
  1772. unsigned int page_array_slab_size; /* default page array slab size */
  1773. /* For runtime compression statistics */
  1774. u64 compr_written_block;
  1775. u64 compr_saved_block;
  1776. u32 compr_new_inode;
  1777. /* For compressed block cache */
  1778. struct inode *compress_inode; /* cache compressed blocks */
  1779. unsigned int compress_percent; /* cache page percentage */
  1780. unsigned int compress_watermark; /* cache page watermark */
  1781. atomic_t compress_page_hit; /* cache hit count */
  1782. #endif
  1783. #ifdef CONFIG_F2FS_IOSTAT
  1784. /* For app/fs IO statistics */
  1785. spinlock_t iostat_lock;
  1786. unsigned long long iostat_count[NR_IO_TYPE];
  1787. unsigned long long iostat_bytes[NR_IO_TYPE];
  1788. unsigned long long prev_iostat_bytes[NR_IO_TYPE];
  1789. bool iostat_enable;
  1790. unsigned long iostat_next_period;
  1791. unsigned int iostat_period_ms;
  1792. /* For io latency related statistics info in one iostat period */
  1793. spinlock_t iostat_lat_lock;
  1794. struct iostat_lat_info *iostat_io_lat;
  1795. #endif
  1796. };
  1797. /* Definitions to access f2fs_sb_info */
  1798. #define SEGS_TO_BLKS(sbi, segs) \
  1799. ((segs) << (sbi)->log_blocks_per_seg)
  1800. #define BLKS_TO_SEGS(sbi, blks) \
  1801. ((blks) >> (sbi)->log_blocks_per_seg)
  1802. #define BLKS_PER_SEG(sbi) ((sbi)->blocks_per_seg)
  1803. #define BLKS_PER_SEC(sbi) (SEGS_TO_BLKS(sbi, (sbi)->segs_per_sec))
  1804. #define SEGS_PER_SEC(sbi) ((sbi)->segs_per_sec)
  1805. __printf(3, 4)
  1806. void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate, const char *fmt, ...);
  1807. #define f2fs_err(sbi, fmt, ...) \
  1808. f2fs_printk(sbi, false, KERN_ERR fmt, ##__VA_ARGS__)
  1809. #define f2fs_warn(sbi, fmt, ...) \
  1810. f2fs_printk(sbi, false, KERN_WARNING fmt, ##__VA_ARGS__)
  1811. #define f2fs_notice(sbi, fmt, ...) \
  1812. f2fs_printk(sbi, false, KERN_NOTICE fmt, ##__VA_ARGS__)
  1813. #define f2fs_info(sbi, fmt, ...) \
  1814. f2fs_printk(sbi, false, KERN_INFO fmt, ##__VA_ARGS__)
  1815. #define f2fs_debug(sbi, fmt, ...) \
  1816. f2fs_printk(sbi, false, KERN_DEBUG fmt, ##__VA_ARGS__)
  1817. #define f2fs_err_ratelimited(sbi, fmt, ...) \
  1818. f2fs_printk(sbi, true, KERN_ERR fmt, ##__VA_ARGS__)
  1819. #define f2fs_warn_ratelimited(sbi, fmt, ...) \
  1820. f2fs_printk(sbi, true, KERN_WARNING fmt, ##__VA_ARGS__)
  1821. #define f2fs_info_ratelimited(sbi, fmt, ...) \
  1822. f2fs_printk(sbi, true, KERN_INFO fmt, ##__VA_ARGS__)
  1823. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1824. #define time_to_inject(sbi, type) __time_to_inject(sbi, type, __func__, \
  1825. __builtin_return_address(0))
  1826. static inline bool __time_to_inject(struct f2fs_sb_info *sbi, int type,
  1827. const char *func, const char *parent_func)
  1828. {
  1829. struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
  1830. if (!ffi->inject_rate)
  1831. return false;
  1832. if (!IS_FAULT_SET(ffi, type))
  1833. return false;
  1834. atomic_inc(&ffi->inject_ops);
  1835. if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
  1836. atomic_set(&ffi->inject_ops, 0);
  1837. ffi->inject_count[type]++;
  1838. f2fs_info_ratelimited(sbi, "inject %s in %s of %pS",
  1839. f2fs_fault_name[type], func, parent_func);
  1840. return true;
  1841. }
  1842. return false;
  1843. }
  1844. #else
  1845. static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
  1846. {
  1847. return false;
  1848. }
  1849. #endif
  1850. /*
  1851. * Test if the mounted volume is a multi-device volume.
  1852. * - For a single regular disk volume, sbi->s_ndevs is 0.
  1853. * - For a single zoned disk volume, sbi->s_ndevs is 1.
  1854. * - For a multi-device volume, sbi->s_ndevs is always 2 or more.
  1855. */
  1856. static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
  1857. {
  1858. return sbi->s_ndevs > 1;
  1859. }
  1860. static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
  1861. {
  1862. unsigned long now = jiffies;
  1863. sbi->last_time[type] = now;
  1864. /* DISCARD_TIME and GC_TIME are based on REQ_TIME */
  1865. if (type == REQ_TIME) {
  1866. sbi->last_time[DISCARD_TIME] = now;
  1867. sbi->last_time[GC_TIME] = now;
  1868. }
  1869. }
  1870. static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
  1871. {
  1872. unsigned long interval = sbi->interval_time[type] * HZ;
  1873. return time_after(jiffies, sbi->last_time[type] + interval);
  1874. }
  1875. static inline unsigned int f2fs_time_to_wait(struct f2fs_sb_info *sbi,
  1876. int type)
  1877. {
  1878. unsigned long interval = sbi->interval_time[type] * HZ;
  1879. unsigned int wait_ms = 0;
  1880. long delta;
  1881. delta = (sbi->last_time[type] + interval) - jiffies;
  1882. if (delta > 0)
  1883. wait_ms = jiffies_to_msecs(delta);
  1884. return wait_ms;
  1885. }
  1886. /*
  1887. * Inline functions
  1888. */
  1889. static inline u32 __f2fs_crc32(u32 crc, const void *address,
  1890. unsigned int length)
  1891. {
  1892. return crc32(crc, address, length);
  1893. }
  1894. static inline u32 f2fs_crc32(const void *address, unsigned int length)
  1895. {
  1896. return __f2fs_crc32(F2FS_SUPER_MAGIC, address, length);
  1897. }
  1898. static inline u32 f2fs_chksum(u32 crc, const void *address, unsigned int length)
  1899. {
  1900. return __f2fs_crc32(crc, address, length);
  1901. }
  1902. static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
  1903. {
  1904. return container_of(inode, struct f2fs_inode_info, vfs_inode);
  1905. }
  1906. static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
  1907. {
  1908. return sb->s_fs_info;
  1909. }
  1910. static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
  1911. {
  1912. return F2FS_SB(inode->i_sb);
  1913. }
  1914. static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
  1915. {
  1916. return F2FS_I_SB(mapping->host);
  1917. }
  1918. static inline struct f2fs_sb_info *F2FS_F_SB(const struct folio *folio)
  1919. {
  1920. return F2FS_M_SB(folio->mapping);
  1921. }
  1922. static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
  1923. {
  1924. return (struct f2fs_super_block *)(sbi->raw_super);
  1925. }
  1926. static inline struct f2fs_super_block *F2FS_SUPER_BLOCK(struct folio *folio,
  1927. pgoff_t index)
  1928. {
  1929. pgoff_t idx_in_folio = index % folio_nr_pages(folio);
  1930. return (struct f2fs_super_block *)
  1931. (page_address(folio_page(folio, idx_in_folio)) +
  1932. F2FS_SUPER_OFFSET);
  1933. }
  1934. static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
  1935. {
  1936. return (struct f2fs_checkpoint *)(sbi->ckpt);
  1937. }
  1938. static inline struct f2fs_node *F2FS_NODE(const struct folio *folio)
  1939. {
  1940. return (struct f2fs_node *)folio_address(folio);
  1941. }
  1942. static inline struct f2fs_inode *F2FS_INODE(const struct folio *folio)
  1943. {
  1944. return &((struct f2fs_node *)folio_address(folio))->i;
  1945. }
  1946. static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
  1947. {
  1948. return (struct f2fs_nm_info *)(sbi->nm_info);
  1949. }
  1950. static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
  1951. {
  1952. return (struct f2fs_sm_info *)(sbi->sm_info);
  1953. }
  1954. static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
  1955. {
  1956. return (struct sit_info *)(SM_I(sbi)->sit_info);
  1957. }
  1958. static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
  1959. {
  1960. return (struct free_segmap_info *)(SM_I(sbi)->free_info);
  1961. }
  1962. static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
  1963. {
  1964. return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
  1965. }
  1966. static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
  1967. {
  1968. return sbi->meta_inode->i_mapping;
  1969. }
  1970. static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
  1971. {
  1972. return sbi->node_inode->i_mapping;
  1973. }
  1974. static inline bool is_meta_folio(struct folio *folio)
  1975. {
  1976. return folio->mapping == META_MAPPING(F2FS_F_SB(folio));
  1977. }
  1978. static inline bool is_node_folio(struct folio *folio)
  1979. {
  1980. return folio->mapping == NODE_MAPPING(F2FS_F_SB(folio));
  1981. }
  1982. static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
  1983. {
  1984. return test_bit(type, &sbi->s_flag);
  1985. }
  1986. static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  1987. {
  1988. set_bit(type, &sbi->s_flag);
  1989. }
  1990. static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  1991. {
  1992. clear_bit(type, &sbi->s_flag);
  1993. }
  1994. static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
  1995. {
  1996. return le64_to_cpu(cp->checkpoint_ver);
  1997. }
  1998. static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
  1999. {
  2000. if (type < F2FS_MAX_QUOTAS)
  2001. return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
  2002. return 0;
  2003. }
  2004. static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
  2005. {
  2006. size_t crc_offset = le32_to_cpu(cp->checksum_offset);
  2007. return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
  2008. }
  2009. static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  2010. {
  2011. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  2012. return ckpt_flags & f;
  2013. }
  2014. static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  2015. {
  2016. return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
  2017. }
  2018. static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  2019. {
  2020. unsigned int ckpt_flags;
  2021. ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  2022. ckpt_flags |= f;
  2023. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  2024. }
  2025. static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  2026. {
  2027. unsigned long flags;
  2028. spin_lock_irqsave(&sbi->cp_lock, flags);
  2029. __set_ckpt_flags(F2FS_CKPT(sbi), f);
  2030. spin_unlock_irqrestore(&sbi->cp_lock, flags);
  2031. }
  2032. static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  2033. {
  2034. unsigned int ckpt_flags;
  2035. ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  2036. ckpt_flags &= (~f);
  2037. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  2038. }
  2039. static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  2040. {
  2041. unsigned long flags;
  2042. spin_lock_irqsave(&sbi->cp_lock, flags);
  2043. __clear_ckpt_flags(F2FS_CKPT(sbi), f);
  2044. spin_unlock_irqrestore(&sbi->cp_lock, flags);
  2045. }
  2046. #define init_f2fs_rwsem(sem) __init_f2fs_rwsem(sem, NULL, LOCK_NAME_NONE)
  2047. #define init_f2fs_rwsem_trace __init_f2fs_rwsem
  2048. #define __init_f2fs_rwsem(sem, sbi, name) \
  2049. do { \
  2050. static struct lock_class_key __key; \
  2051. \
  2052. do_init_f2fs_rwsem((sem), #sem, &__key, sbi, name); \
  2053. } while (0)
  2054. static inline void do_init_f2fs_rwsem(struct f2fs_rwsem *sem,
  2055. const char *sem_name, struct lock_class_key *key,
  2056. struct f2fs_sb_info *sbi, enum f2fs_lock_name name)
  2057. {
  2058. sem->sbi = sbi;
  2059. sem->name = name;
  2060. __init_rwsem(&sem->internal_rwsem, sem_name, key);
  2061. #ifdef CONFIG_F2FS_UNFAIR_RWSEM
  2062. init_waitqueue_head(&sem->read_waiters);
  2063. #endif
  2064. }
  2065. static inline int f2fs_rwsem_is_locked(struct f2fs_rwsem *sem)
  2066. {
  2067. return rwsem_is_locked(&sem->internal_rwsem);
  2068. }
  2069. static inline int f2fs_rwsem_is_contended(struct f2fs_rwsem *sem)
  2070. {
  2071. return rwsem_is_contended(&sem->internal_rwsem);
  2072. }
  2073. static inline void f2fs_down_read(struct f2fs_rwsem *sem)
  2074. {
  2075. #ifdef CONFIG_F2FS_UNFAIR_RWSEM
  2076. wait_event(sem->read_waiters, down_read_trylock(&sem->internal_rwsem));
  2077. #else
  2078. down_read(&sem->internal_rwsem);
  2079. #endif
  2080. }
  2081. static inline int f2fs_down_read_trylock(struct f2fs_rwsem *sem)
  2082. {
  2083. return down_read_trylock(&sem->internal_rwsem);
  2084. }
  2085. static inline void f2fs_up_read(struct f2fs_rwsem *sem)
  2086. {
  2087. up_read(&sem->internal_rwsem);
  2088. }
  2089. static inline void f2fs_down_write(struct f2fs_rwsem *sem)
  2090. {
  2091. down_write(&sem->internal_rwsem);
  2092. }
  2093. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  2094. static inline void f2fs_down_read_nested(struct f2fs_rwsem *sem, int subclass)
  2095. {
  2096. down_read_nested(&sem->internal_rwsem, subclass);
  2097. }
  2098. static inline void f2fs_down_write_nested(struct f2fs_rwsem *sem, int subclass)
  2099. {
  2100. down_write_nested(&sem->internal_rwsem, subclass);
  2101. }
  2102. #else
  2103. #define f2fs_down_read_nested(sem, subclass) f2fs_down_read(sem)
  2104. #define f2fs_down_write_nested(sem, subclass) f2fs_down_write(sem)
  2105. #endif
  2106. static inline int f2fs_down_write_trylock(struct f2fs_rwsem *sem)
  2107. {
  2108. return down_write_trylock(&sem->internal_rwsem);
  2109. }
  2110. static inline void f2fs_up_write(struct f2fs_rwsem *sem)
  2111. {
  2112. up_write(&sem->internal_rwsem);
  2113. #ifdef CONFIG_F2FS_UNFAIR_RWSEM
  2114. wake_up_all(&sem->read_waiters);
  2115. #endif
  2116. }
  2117. void f2fs_down_read_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc);
  2118. int f2fs_down_read_trylock_trace(struct f2fs_rwsem *sem,
  2119. struct f2fs_lock_context *lc);
  2120. void f2fs_up_read_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc);
  2121. void f2fs_down_write_trace(struct f2fs_rwsem *sem,
  2122. struct f2fs_lock_context *lc);
  2123. int f2fs_down_write_trylock_trace(struct f2fs_rwsem *sem,
  2124. struct f2fs_lock_context *lc);
  2125. void f2fs_up_write_trace(struct f2fs_rwsem *sem, struct f2fs_lock_context *lc);
  2126. static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
  2127. {
  2128. unsigned long flags;
  2129. unsigned char *nat_bits;
  2130. /*
  2131. * In order to re-enable nat_bits we need to call fsck.f2fs by
  2132. * set_sbi_flag(sbi, SBI_NEED_FSCK). But it may give huge cost,
  2133. * so let's rely on regular fsck or unclean shutdown.
  2134. */
  2135. if (lock)
  2136. spin_lock_irqsave(&sbi->cp_lock, flags);
  2137. __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
  2138. nat_bits = NM_I(sbi)->nat_bits;
  2139. NM_I(sbi)->nat_bits = NULL;
  2140. if (lock)
  2141. spin_unlock_irqrestore(&sbi->cp_lock, flags);
  2142. kvfree(nat_bits);
  2143. }
  2144. static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
  2145. struct cp_control *cpc)
  2146. {
  2147. bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
  2148. return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
  2149. }
  2150. static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
  2151. {
  2152. int reason = CP_SYNC;
  2153. if (test_opt(sbi, FASTBOOT))
  2154. reason = CP_FASTBOOT;
  2155. if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
  2156. reason = CP_UMOUNT;
  2157. return reason;
  2158. }
  2159. static inline bool __remain_node_summaries(int reason)
  2160. {
  2161. return (reason & (CP_UMOUNT | CP_FASTBOOT));
  2162. }
  2163. static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
  2164. {
  2165. return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
  2166. is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
  2167. }
  2168. /*
  2169. * Check whether the inode has blocks or not
  2170. */
  2171. static inline int F2FS_HAS_BLOCKS(struct inode *inode)
  2172. {
  2173. block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
  2174. return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
  2175. }
  2176. static inline bool f2fs_has_xattr_block(unsigned int ofs)
  2177. {
  2178. return ofs == XATTR_NODE_OFFSET;
  2179. }
  2180. static inline bool __allow_reserved_root(struct f2fs_sb_info *sbi,
  2181. struct inode *inode, bool cap)
  2182. {
  2183. if (!inode)
  2184. return true;
  2185. if (IS_NOQUOTA(inode))
  2186. return true;
  2187. if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
  2188. return true;
  2189. if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
  2190. in_group_p(F2FS_OPTION(sbi).s_resgid))
  2191. return true;
  2192. if (cap && capable(CAP_SYS_RESOURCE))
  2193. return true;
  2194. return false;
  2195. }
  2196. static inline unsigned int get_available_block_count(struct f2fs_sb_info *sbi,
  2197. struct inode *inode, bool cap)
  2198. {
  2199. block_t avail_user_block_count;
  2200. avail_user_block_count = sbi->user_block_count -
  2201. sbi->current_reserved_blocks;
  2202. if (test_opt(sbi, RESERVE_ROOT) && !__allow_reserved_root(sbi, inode, cap))
  2203. avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
  2204. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
  2205. if (avail_user_block_count > sbi->unusable_block_count)
  2206. avail_user_block_count -= sbi->unusable_block_count;
  2207. else
  2208. avail_user_block_count = 0;
  2209. }
  2210. return avail_user_block_count;
  2211. }
  2212. static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
  2213. static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
  2214. struct inode *inode, blkcnt_t *count, bool partial)
  2215. {
  2216. long long diff = 0, release = 0;
  2217. block_t avail_user_block_count;
  2218. int ret;
  2219. ret = dquot_reserve_block(inode, *count);
  2220. if (ret)
  2221. return ret;
  2222. if (time_to_inject(sbi, FAULT_BLOCK)) {
  2223. release = *count;
  2224. goto release_quota;
  2225. }
  2226. /*
  2227. * let's increase this in prior to actual block count change in order
  2228. * for f2fs_sync_file to avoid data races when deciding checkpoint.
  2229. */
  2230. percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
  2231. spin_lock(&sbi->stat_lock);
  2232. avail_user_block_count = get_available_block_count(sbi, inode, true);
  2233. diff = (long long)sbi->total_valid_block_count + *count -
  2234. avail_user_block_count;
  2235. if (unlikely(diff > 0)) {
  2236. if (!partial) {
  2237. spin_unlock(&sbi->stat_lock);
  2238. release = *count;
  2239. goto enospc;
  2240. }
  2241. if (diff > *count)
  2242. diff = *count;
  2243. *count -= diff;
  2244. release = diff;
  2245. if (!*count) {
  2246. spin_unlock(&sbi->stat_lock);
  2247. goto enospc;
  2248. }
  2249. }
  2250. sbi->total_valid_block_count += (block_t)(*count);
  2251. spin_unlock(&sbi->stat_lock);
  2252. if (unlikely(release)) {
  2253. percpu_counter_sub(&sbi->alloc_valid_block_count, release);
  2254. dquot_release_reservation_block(inode, release);
  2255. }
  2256. f2fs_i_blocks_write(inode, *count, true, true);
  2257. return 0;
  2258. enospc:
  2259. percpu_counter_sub(&sbi->alloc_valid_block_count, release);
  2260. release_quota:
  2261. dquot_release_reservation_block(inode, release);
  2262. return -ENOSPC;
  2263. }
  2264. #define PAGE_PRIVATE_GET_FUNC(name, flagname) \
  2265. static inline bool folio_test_f2fs_##name(const struct folio *folio) \
  2266. { \
  2267. unsigned long priv = (unsigned long)folio->private; \
  2268. unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \
  2269. (1UL << PAGE_PRIVATE_##flagname); \
  2270. return (priv & v) == v; \
  2271. } \
  2272. static inline bool page_private_##name(struct page *page) \
  2273. { \
  2274. return PagePrivate(page) && \
  2275. test_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)) && \
  2276. test_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
  2277. }
  2278. #define PAGE_PRIVATE_SET_FUNC(name, flagname) \
  2279. static inline void folio_set_f2fs_##name(struct folio *folio) \
  2280. { \
  2281. unsigned long v = (1UL << PAGE_PRIVATE_NOT_POINTER) | \
  2282. (1UL << PAGE_PRIVATE_##flagname); \
  2283. if (!folio->private) \
  2284. folio_attach_private(folio, (void *)v); \
  2285. else { \
  2286. v |= (unsigned long)folio->private; \
  2287. folio->private = (void *)v; \
  2288. } \
  2289. } \
  2290. static inline void set_page_private_##name(struct page *page) \
  2291. { \
  2292. if (!PagePrivate(page)) \
  2293. attach_page_private(page, (void *)0); \
  2294. set_bit(PAGE_PRIVATE_NOT_POINTER, &page_private(page)); \
  2295. set_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
  2296. }
  2297. #define PAGE_PRIVATE_CLEAR_FUNC(name, flagname) \
  2298. static inline void folio_clear_f2fs_##name(struct folio *folio) \
  2299. { \
  2300. unsigned long v = (unsigned long)folio->private; \
  2301. \
  2302. v &= ~(1UL << PAGE_PRIVATE_##flagname); \
  2303. if (v == (1UL << PAGE_PRIVATE_NOT_POINTER)) \
  2304. folio_detach_private(folio); \
  2305. else \
  2306. folio->private = (void *)v; \
  2307. } \
  2308. static inline void clear_page_private_##name(struct page *page) \
  2309. { \
  2310. clear_bit(PAGE_PRIVATE_##flagname, &page_private(page)); \
  2311. if (page_private(page) == BIT(PAGE_PRIVATE_NOT_POINTER)) \
  2312. detach_page_private(page); \
  2313. }
  2314. PAGE_PRIVATE_GET_FUNC(nonpointer, NOT_POINTER);
  2315. PAGE_PRIVATE_GET_FUNC(inline, INLINE_INODE);
  2316. PAGE_PRIVATE_GET_FUNC(gcing, ONGOING_MIGRATION);
  2317. PAGE_PRIVATE_GET_FUNC(atomic, ATOMIC_WRITE);
  2318. PAGE_PRIVATE_SET_FUNC(reference, REF_RESOURCE);
  2319. PAGE_PRIVATE_SET_FUNC(inline, INLINE_INODE);
  2320. PAGE_PRIVATE_SET_FUNC(gcing, ONGOING_MIGRATION);
  2321. PAGE_PRIVATE_SET_FUNC(atomic, ATOMIC_WRITE);
  2322. PAGE_PRIVATE_CLEAR_FUNC(reference, REF_RESOURCE);
  2323. PAGE_PRIVATE_CLEAR_FUNC(inline, INLINE_INODE);
  2324. PAGE_PRIVATE_CLEAR_FUNC(gcing, ONGOING_MIGRATION);
  2325. PAGE_PRIVATE_CLEAR_FUNC(atomic, ATOMIC_WRITE);
  2326. static inline unsigned long folio_get_f2fs_data(struct folio *folio)
  2327. {
  2328. unsigned long data = (unsigned long)folio->private;
  2329. if (!test_bit(PAGE_PRIVATE_NOT_POINTER, &data))
  2330. return 0;
  2331. return data >> PAGE_PRIVATE_MAX;
  2332. }
  2333. static inline void folio_set_f2fs_data(struct folio *folio, unsigned long data)
  2334. {
  2335. data = (1UL << PAGE_PRIVATE_NOT_POINTER) | (data << PAGE_PRIVATE_MAX);
  2336. if (!folio_test_private(folio))
  2337. folio_attach_private(folio, (void *)data);
  2338. else
  2339. folio->private = (void *)((unsigned long)folio->private | data);
  2340. }
  2341. static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
  2342. struct inode *inode,
  2343. block_t count)
  2344. {
  2345. blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
  2346. spin_lock(&sbi->stat_lock);
  2347. if (unlikely(sbi->total_valid_block_count < count)) {
  2348. f2fs_warn(sbi, "Inconsistent total_valid_block_count:%u, ino:%lu, count:%u",
  2349. sbi->total_valid_block_count, inode->i_ino, count);
  2350. sbi->total_valid_block_count = 0;
  2351. set_sbi_flag(sbi, SBI_NEED_FSCK);
  2352. } else {
  2353. sbi->total_valid_block_count -= count;
  2354. }
  2355. if (sbi->reserved_blocks &&
  2356. sbi->current_reserved_blocks < sbi->reserved_blocks)
  2357. sbi->current_reserved_blocks = min(sbi->reserved_blocks,
  2358. sbi->current_reserved_blocks + count);
  2359. spin_unlock(&sbi->stat_lock);
  2360. if (unlikely(inode->i_blocks < sectors)) {
  2361. f2fs_warn(sbi, "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
  2362. inode->i_ino,
  2363. (unsigned long long)inode->i_blocks,
  2364. (unsigned long long)sectors);
  2365. set_sbi_flag(sbi, SBI_NEED_FSCK);
  2366. return;
  2367. }
  2368. f2fs_i_blocks_write(inode, count, false, true);
  2369. }
  2370. static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
  2371. {
  2372. atomic_inc(&sbi->nr_pages[count_type]);
  2373. if (count_type == F2FS_DIRTY_DENTS ||
  2374. count_type == F2FS_DIRTY_NODES ||
  2375. count_type == F2FS_DIRTY_META ||
  2376. count_type == F2FS_DIRTY_QDATA ||
  2377. count_type == F2FS_DIRTY_IMETA)
  2378. set_sbi_flag(sbi, SBI_IS_DIRTY);
  2379. }
  2380. static inline void inode_inc_dirty_pages(struct inode *inode)
  2381. {
  2382. atomic_inc(&F2FS_I(inode)->dirty_pages);
  2383. inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
  2384. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
  2385. if (IS_NOQUOTA(inode))
  2386. inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
  2387. }
  2388. static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
  2389. {
  2390. atomic_dec(&sbi->nr_pages[count_type]);
  2391. }
  2392. static inline void inode_dec_dirty_pages(struct inode *inode)
  2393. {
  2394. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  2395. !S_ISLNK(inode->i_mode))
  2396. return;
  2397. atomic_dec(&F2FS_I(inode)->dirty_pages);
  2398. dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
  2399. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
  2400. if (IS_NOQUOTA(inode))
  2401. dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
  2402. }
  2403. static inline void inc_atomic_write_cnt(struct inode *inode)
  2404. {
  2405. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2406. struct f2fs_inode_info *fi = F2FS_I(inode);
  2407. u64 current_write;
  2408. fi->atomic_write_cnt++;
  2409. atomic64_inc(&sbi->current_atomic_write);
  2410. current_write = atomic64_read(&sbi->current_atomic_write);
  2411. if (current_write > sbi->peak_atomic_write)
  2412. sbi->peak_atomic_write = current_write;
  2413. }
  2414. static inline void release_atomic_write_cnt(struct inode *inode)
  2415. {
  2416. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2417. struct f2fs_inode_info *fi = F2FS_I(inode);
  2418. atomic64_sub(fi->atomic_write_cnt, &sbi->current_atomic_write);
  2419. fi->atomic_write_cnt = 0;
  2420. }
  2421. static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
  2422. {
  2423. return atomic_read(&sbi->nr_pages[count_type]);
  2424. }
  2425. static inline int get_dirty_pages(struct inode *inode)
  2426. {
  2427. return atomic_read(&F2FS_I(inode)->dirty_pages);
  2428. }
  2429. static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
  2430. {
  2431. return div_u64(get_pages(sbi, block_type) + BLKS_PER_SEC(sbi) - 1,
  2432. BLKS_PER_SEC(sbi));
  2433. }
  2434. static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
  2435. {
  2436. return sbi->total_valid_block_count;
  2437. }
  2438. static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
  2439. {
  2440. return sbi->discard_blks;
  2441. }
  2442. static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
  2443. {
  2444. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2445. /* return NAT or SIT bitmap */
  2446. if (flag == NAT_BITMAP)
  2447. return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  2448. else if (flag == SIT_BITMAP)
  2449. return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  2450. return 0;
  2451. }
  2452. static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
  2453. {
  2454. return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
  2455. }
  2456. static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
  2457. {
  2458. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  2459. void *tmp_ptr = &ckpt->sit_nat_version_bitmap;
  2460. int offset;
  2461. if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
  2462. offset = (flag == SIT_BITMAP) ?
  2463. le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
  2464. /*
  2465. * if large_nat_bitmap feature is enabled, leave checksum
  2466. * protection for all nat/sit bitmaps.
  2467. */
  2468. return tmp_ptr + offset + sizeof(__le32);
  2469. }
  2470. if (__cp_payload(sbi) > 0) {
  2471. if (flag == NAT_BITMAP)
  2472. return tmp_ptr;
  2473. else
  2474. return (unsigned char *)ckpt + F2FS_BLKSIZE;
  2475. } else {
  2476. offset = (flag == NAT_BITMAP) ?
  2477. le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
  2478. return tmp_ptr + offset;
  2479. }
  2480. }
  2481. static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
  2482. {
  2483. block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  2484. if (sbi->cur_cp_pack == 2)
  2485. start_addr += BLKS_PER_SEG(sbi);
  2486. return start_addr;
  2487. }
  2488. static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
  2489. {
  2490. block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  2491. if (sbi->cur_cp_pack == 1)
  2492. start_addr += BLKS_PER_SEG(sbi);
  2493. return start_addr;
  2494. }
  2495. static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
  2496. {
  2497. sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
  2498. }
  2499. static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
  2500. {
  2501. return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  2502. }
  2503. static inline bool __has_cursum_space(struct f2fs_sb_info *sbi,
  2504. struct f2fs_journal *journal, unsigned int size, int type)
  2505. {
  2506. if (type == NAT_JOURNAL)
  2507. return size <= MAX_NAT_JENTRIES(sbi, journal);
  2508. return size <= MAX_SIT_JENTRIES(sbi, journal);
  2509. }
  2510. extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
  2511. static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
  2512. struct inode *inode, bool is_inode)
  2513. {
  2514. block_t valid_block_count;
  2515. unsigned int valid_node_count, avail_user_node_count;
  2516. unsigned int avail_user_block_count;
  2517. int err;
  2518. if (is_inode) {
  2519. if (inode) {
  2520. err = dquot_alloc_inode(inode);
  2521. if (err)
  2522. return err;
  2523. }
  2524. } else {
  2525. err = dquot_reserve_block(inode, 1);
  2526. if (err)
  2527. return err;
  2528. }
  2529. if (time_to_inject(sbi, FAULT_BLOCK))
  2530. goto enospc;
  2531. spin_lock(&sbi->stat_lock);
  2532. valid_block_count = sbi->total_valid_block_count + 1;
  2533. avail_user_block_count = get_available_block_count(sbi, inode,
  2534. test_opt(sbi, RESERVE_NODE));
  2535. if (unlikely(valid_block_count > avail_user_block_count)) {
  2536. spin_unlock(&sbi->stat_lock);
  2537. goto enospc;
  2538. }
  2539. avail_user_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
  2540. if (test_opt(sbi, RESERVE_NODE) &&
  2541. !__allow_reserved_root(sbi, inode, true))
  2542. avail_user_node_count -= F2FS_OPTION(sbi).root_reserved_nodes;
  2543. valid_node_count = sbi->total_valid_node_count + 1;
  2544. if (unlikely(valid_node_count > avail_user_node_count)) {
  2545. spin_unlock(&sbi->stat_lock);
  2546. goto enospc;
  2547. }
  2548. sbi->total_valid_node_count++;
  2549. sbi->total_valid_block_count++;
  2550. spin_unlock(&sbi->stat_lock);
  2551. if (inode) {
  2552. if (is_inode)
  2553. f2fs_mark_inode_dirty_sync(inode, true);
  2554. else
  2555. f2fs_i_blocks_write(inode, 1, true, true);
  2556. }
  2557. percpu_counter_inc(&sbi->alloc_valid_block_count);
  2558. return 0;
  2559. enospc:
  2560. if (is_inode) {
  2561. if (inode)
  2562. dquot_free_inode(inode);
  2563. } else {
  2564. dquot_release_reservation_block(inode, 1);
  2565. }
  2566. return -ENOSPC;
  2567. }
  2568. static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
  2569. struct inode *inode, bool is_inode)
  2570. {
  2571. spin_lock(&sbi->stat_lock);
  2572. if (unlikely(!sbi->total_valid_block_count ||
  2573. !sbi->total_valid_node_count)) {
  2574. f2fs_warn(sbi, "dec_valid_node_count: inconsistent block counts, total_valid_block:%u, total_valid_node:%u",
  2575. sbi->total_valid_block_count,
  2576. sbi->total_valid_node_count);
  2577. set_sbi_flag(sbi, SBI_NEED_FSCK);
  2578. } else {
  2579. sbi->total_valid_block_count--;
  2580. sbi->total_valid_node_count--;
  2581. }
  2582. if (sbi->reserved_blocks &&
  2583. sbi->current_reserved_blocks < sbi->reserved_blocks)
  2584. sbi->current_reserved_blocks++;
  2585. spin_unlock(&sbi->stat_lock);
  2586. if (is_inode) {
  2587. dquot_free_inode(inode);
  2588. } else {
  2589. if (unlikely(inode->i_blocks == 0)) {
  2590. f2fs_warn(sbi, "dec_valid_node_count: inconsistent i_blocks, ino:%lu, iblocks:%llu",
  2591. inode->i_ino,
  2592. (unsigned long long)inode->i_blocks);
  2593. set_sbi_flag(sbi, SBI_NEED_FSCK);
  2594. return;
  2595. }
  2596. f2fs_i_blocks_write(inode, 1, false, true);
  2597. }
  2598. }
  2599. static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
  2600. {
  2601. return sbi->total_valid_node_count;
  2602. }
  2603. static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
  2604. {
  2605. percpu_counter_inc(&sbi->total_valid_inode_count);
  2606. }
  2607. static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
  2608. {
  2609. percpu_counter_dec(&sbi->total_valid_inode_count);
  2610. }
  2611. static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
  2612. {
  2613. return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
  2614. }
  2615. static inline struct folio *f2fs_grab_cache_folio(struct address_space *mapping,
  2616. pgoff_t index, bool for_write)
  2617. {
  2618. struct folio *folio;
  2619. unsigned int flags;
  2620. if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
  2621. fgf_t fgf_flags;
  2622. if (!for_write)
  2623. fgf_flags = FGP_LOCK | FGP_ACCESSED;
  2624. else
  2625. fgf_flags = FGP_LOCK;
  2626. folio = __filemap_get_folio(mapping, index, fgf_flags, 0);
  2627. if (!IS_ERR(folio))
  2628. return folio;
  2629. if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC))
  2630. return ERR_PTR(-ENOMEM);
  2631. }
  2632. if (!for_write)
  2633. return filemap_grab_folio(mapping, index);
  2634. flags = memalloc_nofs_save();
  2635. folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
  2636. mapping_gfp_mask(mapping));
  2637. memalloc_nofs_restore(flags);
  2638. return folio;
  2639. }
  2640. static inline struct folio *f2fs_filemap_get_folio(
  2641. struct address_space *mapping, pgoff_t index,
  2642. fgf_t fgp_flags, gfp_t gfp_mask)
  2643. {
  2644. if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET))
  2645. return ERR_PTR(-ENOMEM);
  2646. return __filemap_get_folio(mapping, index, fgp_flags, gfp_mask);
  2647. }
  2648. static inline void f2fs_folio_put(struct folio *folio, bool unlock)
  2649. {
  2650. if (IS_ERR_OR_NULL(folio))
  2651. return;
  2652. if (unlock) {
  2653. f2fs_bug_on(F2FS_F_SB(folio), !folio_test_locked(folio));
  2654. folio_unlock(folio);
  2655. }
  2656. folio_put(folio);
  2657. }
  2658. static inline void f2fs_put_page(struct page *page, bool unlock)
  2659. {
  2660. if (!page)
  2661. return;
  2662. f2fs_folio_put(page_folio(page), unlock);
  2663. }
  2664. static inline void f2fs_put_dnode(struct dnode_of_data *dn)
  2665. {
  2666. if (dn->node_folio)
  2667. f2fs_folio_put(dn->node_folio, true);
  2668. if (dn->inode_folio && dn->node_folio != dn->inode_folio)
  2669. f2fs_folio_put(dn->inode_folio, false);
  2670. dn->node_folio = NULL;
  2671. dn->inode_folio = NULL;
  2672. }
  2673. static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
  2674. size_t size)
  2675. {
  2676. return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
  2677. }
  2678. static inline void *f2fs_kmem_cache_alloc_nofail(struct kmem_cache *cachep,
  2679. gfp_t flags)
  2680. {
  2681. void *entry;
  2682. entry = kmem_cache_alloc(cachep, flags);
  2683. if (!entry)
  2684. entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
  2685. return entry;
  2686. }
  2687. static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
  2688. gfp_t flags, bool nofail, struct f2fs_sb_info *sbi)
  2689. {
  2690. if (nofail)
  2691. return f2fs_kmem_cache_alloc_nofail(cachep, flags);
  2692. if (time_to_inject(sbi, FAULT_SLAB_ALLOC))
  2693. return NULL;
  2694. return kmem_cache_alloc(cachep, flags);
  2695. }
  2696. static inline bool is_inflight_io(struct f2fs_sb_info *sbi, int type)
  2697. {
  2698. if (get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_RD_NODE) ||
  2699. get_pages(sbi, F2FS_RD_META) || get_pages(sbi, F2FS_WB_DATA) ||
  2700. get_pages(sbi, F2FS_WB_CP_DATA) ||
  2701. get_pages(sbi, F2FS_DIO_READ) ||
  2702. get_pages(sbi, F2FS_DIO_WRITE))
  2703. return true;
  2704. if (type != DISCARD_TIME && SM_I(sbi) && SM_I(sbi)->dcc_info &&
  2705. atomic_read(&SM_I(sbi)->dcc_info->queued_discard))
  2706. return true;
  2707. if (SM_I(sbi) && SM_I(sbi)->fcc_info &&
  2708. atomic_read(&SM_I(sbi)->fcc_info->queued_flush))
  2709. return true;
  2710. return false;
  2711. }
  2712. static inline bool is_inflight_read_io(struct f2fs_sb_info *sbi)
  2713. {
  2714. return get_pages(sbi, F2FS_RD_DATA) || get_pages(sbi, F2FS_DIO_READ);
  2715. }
  2716. static inline bool is_idle(struct f2fs_sb_info *sbi, int type)
  2717. {
  2718. bool zoned_gc = (type == GC_TIME &&
  2719. F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_BLKZONED));
  2720. if (sbi->gc_mode == GC_URGENT_HIGH)
  2721. return true;
  2722. if (sbi->bggc_io_aware == AWARE_READ_IO && is_inflight_read_io(sbi))
  2723. return false;
  2724. if (sbi->bggc_io_aware == AWARE_ALL_IO && is_inflight_io(sbi, type))
  2725. return false;
  2726. if (sbi->gc_mode == GC_URGENT_MID)
  2727. return true;
  2728. if (sbi->gc_mode == GC_URGENT_LOW &&
  2729. (type == DISCARD_TIME || type == GC_TIME))
  2730. return true;
  2731. if (zoned_gc)
  2732. return true;
  2733. return f2fs_time_over(sbi, type);
  2734. }
  2735. static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
  2736. unsigned long index, void *item)
  2737. {
  2738. while (radix_tree_insert(root, index, item))
  2739. cond_resched();
  2740. }
  2741. #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
  2742. static inline bool IS_INODE(const struct folio *folio)
  2743. {
  2744. struct f2fs_node *p = F2FS_NODE(folio);
  2745. return RAW_IS_INODE(p);
  2746. }
  2747. static inline int offset_in_addr(struct f2fs_inode *i)
  2748. {
  2749. return (i->i_inline & F2FS_EXTRA_ATTR) ?
  2750. (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
  2751. }
  2752. static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
  2753. {
  2754. return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
  2755. }
  2756. static inline int f2fs_has_extra_attr(struct inode *inode);
  2757. static inline unsigned int get_dnode_base(struct inode *inode,
  2758. struct folio *node_folio)
  2759. {
  2760. if (!IS_INODE(node_folio))
  2761. return 0;
  2762. return inode ? get_extra_isize(inode) :
  2763. offset_in_addr(&F2FS_NODE(node_folio)->i);
  2764. }
  2765. static inline __le32 *get_dnode_addr(struct inode *inode,
  2766. struct folio *node_folio)
  2767. {
  2768. return blkaddr_in_node(F2FS_NODE(node_folio)) +
  2769. get_dnode_base(inode, node_folio);
  2770. }
  2771. static inline block_t data_blkaddr(struct inode *inode,
  2772. struct folio *node_folio, unsigned int offset)
  2773. {
  2774. return le32_to_cpu(*(get_dnode_addr(inode, node_folio) + offset));
  2775. }
  2776. static inline block_t f2fs_data_blkaddr(struct dnode_of_data *dn)
  2777. {
  2778. return data_blkaddr(dn->inode, dn->node_folio, dn->ofs_in_node);
  2779. }
  2780. static inline int f2fs_test_bit(unsigned int nr, char *addr)
  2781. {
  2782. int mask;
  2783. addr += (nr >> 3);
  2784. mask = BIT(7 - (nr & 0x07));
  2785. return mask & *addr;
  2786. }
  2787. static inline void f2fs_set_bit(unsigned int nr, char *addr)
  2788. {
  2789. int mask;
  2790. addr += (nr >> 3);
  2791. mask = BIT(7 - (nr & 0x07));
  2792. *addr |= mask;
  2793. }
  2794. static inline void f2fs_clear_bit(unsigned int nr, char *addr)
  2795. {
  2796. int mask;
  2797. addr += (nr >> 3);
  2798. mask = BIT(7 - (nr & 0x07));
  2799. *addr &= ~mask;
  2800. }
  2801. static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
  2802. {
  2803. int mask;
  2804. int ret;
  2805. addr += (nr >> 3);
  2806. mask = BIT(7 - (nr & 0x07));
  2807. ret = mask & *addr;
  2808. *addr |= mask;
  2809. return ret;
  2810. }
  2811. static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
  2812. {
  2813. int mask;
  2814. int ret;
  2815. addr += (nr >> 3);
  2816. mask = BIT(7 - (nr & 0x07));
  2817. ret = mask & *addr;
  2818. *addr &= ~mask;
  2819. return ret;
  2820. }
  2821. static inline void f2fs_change_bit(unsigned int nr, char *addr)
  2822. {
  2823. int mask;
  2824. addr += (nr >> 3);
  2825. mask = BIT(7 - (nr & 0x07));
  2826. *addr ^= mask;
  2827. }
  2828. /*
  2829. * On-disk inode flags (f2fs_inode::i_flags)
  2830. */
  2831. #define F2FS_COMPR_FL 0x00000004 /* Compress file */
  2832. #define F2FS_SYNC_FL 0x00000008 /* Synchronous updates */
  2833. #define F2FS_IMMUTABLE_FL 0x00000010 /* Immutable file */
  2834. #define F2FS_APPEND_FL 0x00000020 /* writes to file may only append */
  2835. #define F2FS_NODUMP_FL 0x00000040 /* do not dump file */
  2836. #define F2FS_NOATIME_FL 0x00000080 /* do not update atime */
  2837. #define F2FS_NOCOMP_FL 0x00000400 /* Don't compress */
  2838. #define F2FS_INDEX_FL 0x00001000 /* hash-indexed directory */
  2839. #define F2FS_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
  2840. #define F2FS_PROJINHERIT_FL 0x20000000 /* Create with parents projid */
  2841. #define F2FS_CASEFOLD_FL 0x40000000 /* Casefolded file */
  2842. #define F2FS_DEVICE_ALIAS_FL 0x80000000 /* File for aliasing a device */
  2843. #define F2FS_QUOTA_DEFAULT_FL (F2FS_NOATIME_FL | F2FS_IMMUTABLE_FL)
  2844. /* Flags that should be inherited by new inodes from their parent. */
  2845. #define F2FS_FL_INHERITED (F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL | \
  2846. F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
  2847. F2FS_CASEFOLD_FL)
  2848. /* Flags that are appropriate for regular files (all but dir-specific ones). */
  2849. #define F2FS_REG_FLMASK (~(F2FS_DIRSYNC_FL | F2FS_PROJINHERIT_FL | \
  2850. F2FS_CASEFOLD_FL))
  2851. /* Flags that are appropriate for non-directories/regular files. */
  2852. #define F2FS_OTHER_FLMASK (F2FS_NODUMP_FL | F2FS_NOATIME_FL)
  2853. #define IS_DEVICE_ALIASING(inode) (F2FS_I(inode)->i_flags & F2FS_DEVICE_ALIAS_FL)
  2854. static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
  2855. {
  2856. if (S_ISDIR(mode))
  2857. return flags;
  2858. else if (S_ISREG(mode))
  2859. return flags & F2FS_REG_FLMASK;
  2860. else
  2861. return flags & F2FS_OTHER_FLMASK;
  2862. }
  2863. static inline void __mark_inode_dirty_flag(struct inode *inode,
  2864. int flag, bool set)
  2865. {
  2866. switch (flag) {
  2867. case FI_INLINE_XATTR:
  2868. case FI_INLINE_DATA:
  2869. case FI_INLINE_DENTRY:
  2870. case FI_NEW_INODE:
  2871. if (set)
  2872. return;
  2873. fallthrough;
  2874. case FI_DATA_EXIST:
  2875. case FI_PIN_FILE:
  2876. case FI_COMPRESS_RELEASED:
  2877. f2fs_mark_inode_dirty_sync(inode, true);
  2878. }
  2879. }
  2880. static inline void set_inode_flag(struct inode *inode, int flag)
  2881. {
  2882. set_bit(flag, F2FS_I(inode)->flags);
  2883. __mark_inode_dirty_flag(inode, flag, true);
  2884. }
  2885. static inline int is_inode_flag_set(struct inode *inode, int flag)
  2886. {
  2887. return test_bit(flag, F2FS_I(inode)->flags);
  2888. }
  2889. static inline void clear_inode_flag(struct inode *inode, int flag)
  2890. {
  2891. clear_bit(flag, F2FS_I(inode)->flags);
  2892. __mark_inode_dirty_flag(inode, flag, false);
  2893. }
  2894. static inline bool f2fs_verity_in_progress(struct inode *inode)
  2895. {
  2896. return IS_ENABLED(CONFIG_FS_VERITY) &&
  2897. is_inode_flag_set(inode, FI_VERITY_IN_PROGRESS);
  2898. }
  2899. static inline void set_acl_inode(struct inode *inode, umode_t mode)
  2900. {
  2901. F2FS_I(inode)->i_acl_mode = mode;
  2902. set_inode_flag(inode, FI_ACL_MODE);
  2903. f2fs_mark_inode_dirty_sync(inode, false);
  2904. }
  2905. static inline void f2fs_i_links_write(struct inode *inode, bool inc)
  2906. {
  2907. if (inc)
  2908. inc_nlink(inode);
  2909. else
  2910. drop_nlink(inode);
  2911. f2fs_mark_inode_dirty_sync(inode, true);
  2912. }
  2913. static inline void f2fs_i_blocks_write(struct inode *inode,
  2914. block_t diff, bool add, bool claim)
  2915. {
  2916. bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
  2917. bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
  2918. /* add = 1, claim = 1 should be dquot_reserve_block in pair */
  2919. if (add) {
  2920. if (claim)
  2921. dquot_claim_block(inode, diff);
  2922. else
  2923. dquot_alloc_block_nofail(inode, diff);
  2924. } else {
  2925. dquot_free_block(inode, diff);
  2926. }
  2927. f2fs_mark_inode_dirty_sync(inode, true);
  2928. if (clean || recover)
  2929. set_inode_flag(inode, FI_AUTO_RECOVER);
  2930. }
  2931. static inline bool f2fs_is_atomic_file(struct inode *inode);
  2932. static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
  2933. {
  2934. bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
  2935. bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
  2936. if (i_size_read(inode) == i_size)
  2937. return;
  2938. i_size_write(inode, i_size);
  2939. if (f2fs_is_atomic_file(inode))
  2940. return;
  2941. f2fs_mark_inode_dirty_sync(inode, true);
  2942. if (clean || recover)
  2943. set_inode_flag(inode, FI_AUTO_RECOVER);
  2944. }
  2945. static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
  2946. {
  2947. F2FS_I(inode)->i_current_depth = depth;
  2948. f2fs_mark_inode_dirty_sync(inode, true);
  2949. }
  2950. static inline void f2fs_i_gc_failures_write(struct inode *inode,
  2951. unsigned int count)
  2952. {
  2953. F2FS_I(inode)->i_gc_failures = count;
  2954. f2fs_mark_inode_dirty_sync(inode, true);
  2955. }
  2956. static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
  2957. {
  2958. F2FS_I(inode)->i_xattr_nid = xnid;
  2959. f2fs_mark_inode_dirty_sync(inode, true);
  2960. }
  2961. static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
  2962. {
  2963. F2FS_I(inode)->i_pino = pino;
  2964. f2fs_mark_inode_dirty_sync(inode, true);
  2965. }
  2966. static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
  2967. {
  2968. struct f2fs_inode_info *fi = F2FS_I(inode);
  2969. if (ri->i_inline & F2FS_INLINE_XATTR)
  2970. set_bit(FI_INLINE_XATTR, fi->flags);
  2971. if (ri->i_inline & F2FS_INLINE_DATA)
  2972. set_bit(FI_INLINE_DATA, fi->flags);
  2973. if (ri->i_inline & F2FS_INLINE_DENTRY)
  2974. set_bit(FI_INLINE_DENTRY, fi->flags);
  2975. if (ri->i_inline & F2FS_DATA_EXIST)
  2976. set_bit(FI_DATA_EXIST, fi->flags);
  2977. if (ri->i_inline & F2FS_EXTRA_ATTR)
  2978. set_bit(FI_EXTRA_ATTR, fi->flags);
  2979. if (ri->i_inline & F2FS_PIN_FILE)
  2980. set_bit(FI_PIN_FILE, fi->flags);
  2981. if (ri->i_inline & F2FS_COMPRESS_RELEASED)
  2982. set_bit(FI_COMPRESS_RELEASED, fi->flags);
  2983. }
  2984. static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
  2985. {
  2986. ri->i_inline = 0;
  2987. if (is_inode_flag_set(inode, FI_INLINE_XATTR))
  2988. ri->i_inline |= F2FS_INLINE_XATTR;
  2989. if (is_inode_flag_set(inode, FI_INLINE_DATA))
  2990. ri->i_inline |= F2FS_INLINE_DATA;
  2991. if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
  2992. ri->i_inline |= F2FS_INLINE_DENTRY;
  2993. if (is_inode_flag_set(inode, FI_DATA_EXIST))
  2994. ri->i_inline |= F2FS_DATA_EXIST;
  2995. if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
  2996. ri->i_inline |= F2FS_EXTRA_ATTR;
  2997. if (is_inode_flag_set(inode, FI_PIN_FILE))
  2998. ri->i_inline |= F2FS_PIN_FILE;
  2999. if (is_inode_flag_set(inode, FI_COMPRESS_RELEASED))
  3000. ri->i_inline |= F2FS_COMPRESS_RELEASED;
  3001. }
  3002. static inline int f2fs_has_extra_attr(struct inode *inode)
  3003. {
  3004. return is_inode_flag_set(inode, FI_EXTRA_ATTR);
  3005. }
  3006. static inline int f2fs_has_inline_xattr(struct inode *inode)
  3007. {
  3008. return is_inode_flag_set(inode, FI_INLINE_XATTR);
  3009. }
  3010. static inline int f2fs_compressed_file(struct inode *inode)
  3011. {
  3012. return S_ISREG(inode->i_mode) &&
  3013. is_inode_flag_set(inode, FI_COMPRESSED_FILE);
  3014. }
  3015. static inline bool f2fs_need_compress_data(struct inode *inode)
  3016. {
  3017. int compress_mode = F2FS_OPTION(F2FS_I_SB(inode)).compress_mode;
  3018. if (!f2fs_compressed_file(inode))
  3019. return false;
  3020. if (compress_mode == COMPR_MODE_FS)
  3021. return true;
  3022. else if (compress_mode == COMPR_MODE_USER &&
  3023. is_inode_flag_set(inode, FI_ENABLE_COMPRESS))
  3024. return true;
  3025. return false;
  3026. }
  3027. static inline unsigned int addrs_per_page(struct inode *inode,
  3028. bool is_inode)
  3029. {
  3030. unsigned int addrs = is_inode ? (CUR_ADDRS_PER_INODE(inode) -
  3031. get_inline_xattr_addrs(inode)) : DEF_ADDRS_PER_BLOCK;
  3032. if (f2fs_compressed_file(inode))
  3033. return ALIGN_DOWN(addrs, F2FS_I(inode)->i_cluster_size);
  3034. return addrs;
  3035. }
  3036. static inline
  3037. void *inline_xattr_addr(struct inode *inode, const struct folio *folio)
  3038. {
  3039. struct f2fs_inode *ri = F2FS_INODE(folio);
  3040. return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
  3041. get_inline_xattr_addrs(inode)]);
  3042. }
  3043. static inline int inline_xattr_size(struct inode *inode)
  3044. {
  3045. if (f2fs_has_inline_xattr(inode))
  3046. return get_inline_xattr_addrs(inode) * sizeof(__le32);
  3047. return 0;
  3048. }
  3049. /*
  3050. * Notice: check inline_data flag without inode page lock is unsafe.
  3051. * It could change at any time by f2fs_convert_inline_folio().
  3052. */
  3053. static inline int f2fs_has_inline_data(struct inode *inode)
  3054. {
  3055. return is_inode_flag_set(inode, FI_INLINE_DATA);
  3056. }
  3057. static inline int f2fs_exist_data(struct inode *inode)
  3058. {
  3059. return is_inode_flag_set(inode, FI_DATA_EXIST);
  3060. }
  3061. static inline int f2fs_is_mmap_file(struct inode *inode)
  3062. {
  3063. return is_inode_flag_set(inode, FI_MMAP_FILE);
  3064. }
  3065. static inline bool f2fs_is_pinned_file(struct inode *inode)
  3066. {
  3067. return is_inode_flag_set(inode, FI_PIN_FILE);
  3068. }
  3069. static inline bool f2fs_is_atomic_file(struct inode *inode)
  3070. {
  3071. return is_inode_flag_set(inode, FI_ATOMIC_FILE);
  3072. }
  3073. static inline bool f2fs_is_cow_file(struct inode *inode)
  3074. {
  3075. return is_inode_flag_set(inode, FI_COW_FILE);
  3076. }
  3077. static inline void *inline_data_addr(struct inode *inode, struct folio *folio)
  3078. {
  3079. __le32 *addr = get_dnode_addr(inode, folio);
  3080. return (void *)(addr + DEF_INLINE_RESERVED_SIZE);
  3081. }
  3082. static inline int f2fs_has_inline_dentry(struct inode *inode)
  3083. {
  3084. return is_inode_flag_set(inode, FI_INLINE_DENTRY);
  3085. }
  3086. static inline int is_file(struct inode *inode, int type)
  3087. {
  3088. return F2FS_I(inode)->i_advise & type;
  3089. }
  3090. static inline void set_file(struct inode *inode, int type)
  3091. {
  3092. if (is_file(inode, type))
  3093. return;
  3094. F2FS_I(inode)->i_advise |= type;
  3095. f2fs_mark_inode_dirty_sync(inode, true);
  3096. }
  3097. static inline void clear_file(struct inode *inode, int type)
  3098. {
  3099. if (!is_file(inode, type))
  3100. return;
  3101. F2FS_I(inode)->i_advise &= ~type;
  3102. f2fs_mark_inode_dirty_sync(inode, true);
  3103. }
  3104. static inline bool f2fs_is_time_consistent(struct inode *inode)
  3105. {
  3106. struct timespec64 ts = inode_get_atime(inode);
  3107. if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &ts))
  3108. return false;
  3109. ts = inode_get_ctime(inode);
  3110. if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &ts))
  3111. return false;
  3112. ts = inode_get_mtime(inode);
  3113. if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &ts))
  3114. return false;
  3115. return true;
  3116. }
  3117. static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
  3118. {
  3119. bool ret;
  3120. if (dsync) {
  3121. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3122. spin_lock(&sbi->inode_lock[DIRTY_META]);
  3123. ret = list_empty(&F2FS_I(inode)->gdirty_list);
  3124. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  3125. return ret;
  3126. }
  3127. if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
  3128. file_keep_isize(inode) ||
  3129. i_size_read(inode) & ~PAGE_MASK)
  3130. return false;
  3131. if (!f2fs_is_time_consistent(inode))
  3132. return false;
  3133. spin_lock(&F2FS_I(inode)->i_size_lock);
  3134. ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
  3135. spin_unlock(&F2FS_I(inode)->i_size_lock);
  3136. return ret;
  3137. }
  3138. static inline bool f2fs_readonly(struct super_block *sb)
  3139. {
  3140. return sb_rdonly(sb);
  3141. }
  3142. static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
  3143. {
  3144. return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
  3145. }
  3146. static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
  3147. size_t size, gfp_t flags)
  3148. {
  3149. if (time_to_inject(sbi, FAULT_KMALLOC))
  3150. return NULL;
  3151. return kmalloc(size, flags);
  3152. }
  3153. static inline void *f2fs_getname(struct f2fs_sb_info *sbi)
  3154. {
  3155. if (time_to_inject(sbi, FAULT_KMALLOC))
  3156. return NULL;
  3157. return __getname();
  3158. }
  3159. static inline void f2fs_putname(char *buf)
  3160. {
  3161. __putname(buf);
  3162. }
  3163. static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
  3164. size_t size, gfp_t flags)
  3165. {
  3166. return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
  3167. }
  3168. static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
  3169. size_t size, gfp_t flags)
  3170. {
  3171. if (time_to_inject(sbi, FAULT_KVMALLOC))
  3172. return NULL;
  3173. return kvmalloc(size, flags);
  3174. }
  3175. static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
  3176. size_t size, gfp_t flags)
  3177. {
  3178. return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
  3179. }
  3180. static inline void *f2fs_vmalloc(struct f2fs_sb_info *sbi, size_t size)
  3181. {
  3182. if (time_to_inject(sbi, FAULT_VMALLOC))
  3183. return NULL;
  3184. return vmalloc(size);
  3185. }
  3186. static inline int get_extra_isize(struct inode *inode)
  3187. {
  3188. return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
  3189. }
  3190. static inline int get_inline_xattr_addrs(struct inode *inode)
  3191. {
  3192. return F2FS_I(inode)->i_inline_xattr_size;
  3193. }
  3194. #define f2fs_get_inode_mode(i) \
  3195. ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
  3196. (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
  3197. #define F2FS_MIN_EXTRA_ATTR_SIZE (sizeof(__le32))
  3198. #define F2FS_TOTAL_EXTRA_ATTR_SIZE \
  3199. (offsetof(struct f2fs_inode, i_extra_end) - \
  3200. offsetof(struct f2fs_inode, i_extra_isize)) \
  3201. #define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr))
  3202. #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \
  3203. ((offsetof(typeof(*(f2fs_inode)), field) + \
  3204. sizeof((f2fs_inode)->field)) \
  3205. <= (F2FS_OLD_ATTRIBUTE_SIZE + (extra_isize))) \
  3206. #define __is_large_section(sbi) (SEGS_PER_SEC(sbi) > 1)
  3207. #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO((fio)->type) == META)
  3208. bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
  3209. block_t blkaddr, int type);
  3210. static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
  3211. block_t blkaddr, int type)
  3212. {
  3213. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type))
  3214. f2fs_err(sbi, "invalid blkaddr: %u, type: %d, run fsck to fix.",
  3215. blkaddr, type);
  3216. }
  3217. static inline bool __is_valid_data_blkaddr(block_t blkaddr)
  3218. {
  3219. if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR ||
  3220. blkaddr == COMPRESS_ADDR)
  3221. return false;
  3222. return true;
  3223. }
  3224. /*
  3225. * file.c
  3226. */
  3227. int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
  3228. int f2fs_do_truncate_blocks(struct inode *inode, u64 from, bool lock);
  3229. int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
  3230. int f2fs_truncate(struct inode *inode);
  3231. int f2fs_getattr(struct mnt_idmap *idmap, const struct path *path,
  3232. struct kstat *stat, u32 request_mask, unsigned int flags);
  3233. int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  3234. struct iattr *attr);
  3235. int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
  3236. void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
  3237. int f2fs_do_shutdown(struct f2fs_sb_info *sbi, unsigned int flag,
  3238. bool readonly, bool need_lock);
  3239. int f2fs_precache_extents(struct inode *inode);
  3240. int f2fs_fileattr_get(struct dentry *dentry, struct file_kattr *fa);
  3241. int f2fs_fileattr_set(struct mnt_idmap *idmap,
  3242. struct dentry *dentry, struct file_kattr *fa);
  3243. long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
  3244. long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  3245. int f2fs_transfer_project_quota(struct inode *inode, kprojid_t kprojid);
  3246. int f2fs_pin_file_control(struct inode *inode, bool inc);
  3247. /*
  3248. * inode.c
  3249. */
  3250. void f2fs_set_inode_flags(struct inode *inode);
  3251. bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct folio *folio);
  3252. void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct folio *folio);
  3253. struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
  3254. struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
  3255. int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
  3256. void f2fs_update_inode(struct inode *inode, struct folio *node_folio);
  3257. void f2fs_update_inode_page(struct inode *inode);
  3258. int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
  3259. void f2fs_remove_donate_inode(struct inode *inode);
  3260. void f2fs_evict_inode(struct inode *inode);
  3261. void f2fs_handle_failed_inode(struct inode *inode, struct f2fs_lock_context *lc);
  3262. /*
  3263. * namei.c
  3264. */
  3265. int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
  3266. bool hot, bool set);
  3267. struct dentry *f2fs_get_parent(struct dentry *child);
  3268. int f2fs_get_tmpfile(struct mnt_idmap *idmap, struct inode *dir,
  3269. struct inode **new_inode);
  3270. /*
  3271. * dir.c
  3272. */
  3273. #if IS_ENABLED(CONFIG_UNICODE)
  3274. int f2fs_init_casefolded_name(const struct inode *dir,
  3275. struct f2fs_filename *fname);
  3276. void f2fs_free_casefolded_name(struct f2fs_filename *fname);
  3277. #else
  3278. static inline int f2fs_init_casefolded_name(const struct inode *dir,
  3279. struct f2fs_filename *fname)
  3280. {
  3281. return 0;
  3282. }
  3283. static inline void f2fs_free_casefolded_name(struct f2fs_filename *fname)
  3284. {
  3285. }
  3286. #endif /* CONFIG_UNICODE */
  3287. int f2fs_setup_filename(struct inode *dir, const struct qstr *iname,
  3288. int lookup, struct f2fs_filename *fname);
  3289. int f2fs_prepare_lookup(struct inode *dir, struct dentry *dentry,
  3290. struct f2fs_filename *fname);
  3291. void f2fs_free_filename(struct f2fs_filename *fname);
  3292. struct f2fs_dir_entry *f2fs_find_target_dentry(const struct f2fs_dentry_ptr *d,
  3293. const struct f2fs_filename *fname, int *max_slots,
  3294. bool use_hash);
  3295. int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
  3296. unsigned int start_pos, struct fscrypt_str *fstr);
  3297. void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
  3298. struct f2fs_dentry_ptr *d);
  3299. struct folio *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
  3300. const struct f2fs_filename *fname, struct folio *dfolio);
  3301. void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
  3302. unsigned int current_depth);
  3303. int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
  3304. void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
  3305. struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
  3306. const struct f2fs_filename *fname, struct folio **res_folio);
  3307. struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
  3308. const struct qstr *child, struct folio **res_folio);
  3309. struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct folio **f);
  3310. ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
  3311. struct folio **folio);
  3312. void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
  3313. struct folio *folio, struct inode *inode);
  3314. bool f2fs_has_enough_room(struct inode *dir, struct folio *ifolio,
  3315. const struct f2fs_filename *fname);
  3316. void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
  3317. const struct fscrypt_str *name, f2fs_hash_t name_hash,
  3318. unsigned int bit_pos);
  3319. int f2fs_add_regular_entry(struct inode *dir, const struct f2fs_filename *fname,
  3320. struct inode *inode, nid_t ino, umode_t mode);
  3321. int f2fs_add_dentry(struct inode *dir, const struct f2fs_filename *fname,
  3322. struct inode *inode, nid_t ino, umode_t mode);
  3323. int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
  3324. struct inode *inode, nid_t ino, umode_t mode);
  3325. void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct folio *folio,
  3326. struct inode *dir, struct inode *inode);
  3327. int f2fs_do_tmpfile(struct inode *inode, struct inode *dir,
  3328. struct f2fs_filename *fname);
  3329. bool f2fs_empty_dir(struct inode *dir);
  3330. static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
  3331. {
  3332. if (fscrypt_is_nokey_name(dentry))
  3333. return -ENOKEY;
  3334. return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
  3335. inode, inode->i_ino, inode->i_mode);
  3336. }
  3337. /*
  3338. * super.c
  3339. */
  3340. int f2fs_inode_dirtied(struct inode *inode, bool sync);
  3341. void f2fs_inode_synced(struct inode *inode);
  3342. int f2fs_dquot_initialize(struct inode *inode);
  3343. int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
  3344. int f2fs_do_quota_sync(struct super_block *sb, int type);
  3345. loff_t max_file_blocks(struct inode *inode);
  3346. void f2fs_quota_off_umount(struct super_block *sb);
  3347. void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag);
  3348. void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason);
  3349. void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error);
  3350. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
  3351. int f2fs_sync_fs(struct super_block *sb, int sync);
  3352. int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
  3353. /*
  3354. * hash.c
  3355. */
  3356. void f2fs_hash_filename(const struct inode *dir, struct f2fs_filename *fname);
  3357. /*
  3358. * node.c
  3359. */
  3360. struct node_info;
  3361. enum node_type;
  3362. int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
  3363. bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
  3364. bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct folio *folio);
  3365. void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
  3366. void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct folio *folio);
  3367. void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
  3368. int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
  3369. bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
  3370. bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
  3371. int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
  3372. struct node_info *ni, bool checkpoint_context);
  3373. pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
  3374. int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
  3375. int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
  3376. int f2fs_truncate_xattr_node(struct inode *inode);
  3377. int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
  3378. unsigned int seq_id);
  3379. int f2fs_remove_inode_page(struct inode *inode);
  3380. struct folio *f2fs_new_inode_folio(struct inode *inode);
  3381. struct folio *f2fs_new_node_folio(struct dnode_of_data *dn, unsigned int ofs);
  3382. void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
  3383. struct folio *f2fs_get_node_folio(struct f2fs_sb_info *sbi, pgoff_t nid,
  3384. enum node_type node_type);
  3385. int f2fs_sanity_check_node_footer(struct f2fs_sb_info *sbi,
  3386. struct folio *folio, pgoff_t nid,
  3387. enum node_type ntype, bool in_irq);
  3388. struct folio *f2fs_get_inode_folio(struct f2fs_sb_info *sbi, pgoff_t ino);
  3389. struct folio *f2fs_get_xnode_folio(struct f2fs_sb_info *sbi, pgoff_t xnid);
  3390. int f2fs_move_node_folio(struct folio *node_folio, int gc_type);
  3391. void f2fs_flush_inline_data(struct f2fs_sb_info *sbi);
  3392. int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
  3393. struct writeback_control *wbc, bool atomic,
  3394. unsigned int *seq_id);
  3395. int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
  3396. struct writeback_control *wbc,
  3397. bool do_balance, enum iostat_type io_type);
  3398. int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
  3399. bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
  3400. void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
  3401. void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
  3402. int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
  3403. int f2fs_recover_inline_xattr(struct inode *inode, struct folio *folio);
  3404. int f2fs_recover_xattr_data(struct inode *inode, struct folio *folio);
  3405. int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct folio *folio);
  3406. int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
  3407. unsigned int segno, struct f2fs_summary_block *sum);
  3408. int f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  3409. int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
  3410. void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
  3411. int __init f2fs_create_node_manager_caches(void);
  3412. void f2fs_destroy_node_manager_caches(void);
  3413. /*
  3414. * segment.c
  3415. */
  3416. bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
  3417. int f2fs_commit_atomic_write(struct inode *inode);
  3418. void f2fs_abort_atomic_write(struct inode *inode, bool clean);
  3419. void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
  3420. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi, bool from_bg);
  3421. int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
  3422. int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
  3423. int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
  3424. void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
  3425. void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
  3426. unsigned int len);
  3427. bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
  3428. int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
  3429. void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
  3430. void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
  3431. bool f2fs_issue_discard_timeout(struct f2fs_sb_info *sbi);
  3432. void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
  3433. struct cp_control *cpc);
  3434. void f2fs_dirty_to_prefree(struct f2fs_sb_info *sbi);
  3435. block_t f2fs_get_unusable_blocks(struct f2fs_sb_info *sbi);
  3436. int f2fs_disable_cp_again(struct f2fs_sb_info *sbi, block_t unusable);
  3437. void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
  3438. int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
  3439. bool f2fs_segment_has_free_slot(struct f2fs_sb_info *sbi, int segno);
  3440. int f2fs_init_inmem_curseg(struct f2fs_sb_info *sbi);
  3441. int f2fs_reinit_atgc_curseg(struct f2fs_sb_info *sbi);
  3442. void f2fs_save_inmem_curseg(struct f2fs_sb_info *sbi);
  3443. void f2fs_restore_inmem_curseg(struct f2fs_sb_info *sbi);
  3444. int f2fs_allocate_segment_for_resize(struct f2fs_sb_info *sbi, int type,
  3445. unsigned int start, unsigned int end);
  3446. int f2fs_allocate_new_section(struct f2fs_sb_info *sbi, int type, bool force);
  3447. int f2fs_allocate_pinning_section(struct f2fs_sb_info *sbi);
  3448. int f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
  3449. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
  3450. bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
  3451. struct cp_control *cpc);
  3452. struct folio *f2fs_get_sum_folio(struct f2fs_sb_info *sbi, unsigned int segno);
  3453. void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
  3454. block_t blk_addr);
  3455. void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct folio *folio,
  3456. enum iostat_type io_type);
  3457. void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
  3458. void f2fs_outplace_write_data(struct dnode_of_data *dn,
  3459. struct f2fs_io_info *fio);
  3460. int f2fs_inplace_write_data(struct f2fs_io_info *fio);
  3461. void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  3462. block_t old_blkaddr, block_t new_blkaddr,
  3463. bool recover_curseg, bool recover_newaddr,
  3464. bool from_gc);
  3465. void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
  3466. block_t old_addr, block_t new_addr,
  3467. unsigned char version, bool recover_curseg,
  3468. bool recover_newaddr);
  3469. enum temp_type f2fs_get_segment_temp(struct f2fs_sb_info *sbi,
  3470. enum log_type seg_type);
  3471. int f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct folio *folio,
  3472. block_t old_blkaddr, block_t *new_blkaddr,
  3473. struct f2fs_summary *sum, int type,
  3474. struct f2fs_io_info *fio);
  3475. void f2fs_update_device_state(struct f2fs_sb_info *sbi, nid_t ino,
  3476. block_t blkaddr, unsigned int blkcnt);
  3477. void f2fs_folio_wait_writeback(struct folio *folio, enum page_type type,
  3478. bool ordered, bool locked);
  3479. #define f2fs_wait_on_page_writeback(page, type, ordered, locked) \
  3480. f2fs_folio_wait_writeback(page_folio(page), type, ordered, locked)
  3481. void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
  3482. void f2fs_wait_on_block_writeback_range(struct inode *inode, block_t blkaddr,
  3483. block_t len);
  3484. void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
  3485. void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
  3486. int f2fs_lookup_journal_in_cursum(struct f2fs_sb_info *sbi,
  3487. struct f2fs_journal *journal, int type,
  3488. unsigned int val, int alloc);
  3489. void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  3490. int f2fs_check_and_fix_write_pointer(struct f2fs_sb_info *sbi);
  3491. int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
  3492. void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
  3493. int __init f2fs_create_segment_manager_caches(void);
  3494. void f2fs_destroy_segment_manager_caches(void);
  3495. int f2fs_rw_hint_to_seg_type(struct f2fs_sb_info *sbi, enum rw_hint hint);
  3496. enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
  3497. enum page_type type, enum temp_type temp);
  3498. unsigned int f2fs_usable_segs_in_sec(struct f2fs_sb_info *sbi);
  3499. unsigned int f2fs_usable_blks_in_seg(struct f2fs_sb_info *sbi,
  3500. unsigned int segno);
  3501. unsigned long long f2fs_get_section_mtime(struct f2fs_sb_info *sbi,
  3502. unsigned int segno);
  3503. static inline struct inode *fio_inode(struct f2fs_io_info *fio)
  3504. {
  3505. return fio->folio->mapping->host;
  3506. }
  3507. #define DEF_FRAGMENT_SIZE 4
  3508. #define MIN_FRAGMENT_SIZE 1
  3509. #define MAX_FRAGMENT_SIZE 512
  3510. static inline bool f2fs_need_rand_seg(struct f2fs_sb_info *sbi)
  3511. {
  3512. return F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG ||
  3513. F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK;
  3514. }
  3515. /*
  3516. * checkpoint.c
  3517. */
  3518. void f2fs_lock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc);
  3519. int f2fs_trylock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc);
  3520. void f2fs_unlock_op(struct f2fs_sb_info *sbi, struct f2fs_lock_context *lc);
  3521. void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io,
  3522. unsigned char reason);
  3523. void f2fs_flush_ckpt_thread(struct f2fs_sb_info *sbi);
  3524. struct folio *f2fs_grab_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index);
  3525. struct folio *f2fs_get_meta_folio(struct f2fs_sb_info *sbi, pgoff_t index);
  3526. struct folio *f2fs_get_meta_folio_retry(struct f2fs_sb_info *sbi, pgoff_t index);
  3527. struct folio *f2fs_get_tmp_folio(struct f2fs_sb_info *sbi, pgoff_t index);
  3528. bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
  3529. block_t blkaddr, int type);
  3530. bool f2fs_is_valid_blkaddr_raw(struct f2fs_sb_info *sbi,
  3531. block_t blkaddr, int type);
  3532. int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
  3533. int type, bool sync);
  3534. void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index,
  3535. unsigned int ra_blocks);
  3536. long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, long nr_to_write,
  3537. enum iostat_type io_type);
  3538. void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
  3539. void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
  3540. void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
  3541. bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
  3542. void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
  3543. unsigned int devidx, int type);
  3544. bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
  3545. unsigned int devidx, int type);
  3546. int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
  3547. void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
  3548. void f2fs_add_orphan_inode(struct inode *inode);
  3549. void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
  3550. int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
  3551. int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
  3552. void f2fs_update_dirty_folio(struct inode *inode, struct folio *folio);
  3553. void f2fs_remove_dirty_inode(struct inode *inode);
  3554. int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type,
  3555. bool from_cp);
  3556. void f2fs_wait_on_all_pages(struct f2fs_sb_info *sbi, int type);
  3557. u64 f2fs_get_sectors_written(struct f2fs_sb_info *sbi);
  3558. int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  3559. void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
  3560. int __init f2fs_create_checkpoint_caches(void);
  3561. void f2fs_destroy_checkpoint_caches(void);
  3562. int f2fs_issue_checkpoint(struct f2fs_sb_info *sbi);
  3563. int f2fs_start_ckpt_thread(struct f2fs_sb_info *sbi);
  3564. void f2fs_stop_ckpt_thread(struct f2fs_sb_info *sbi);
  3565. void f2fs_init_ckpt_req_control(struct f2fs_sb_info *sbi);
  3566. /*
  3567. * data.c
  3568. */
  3569. int __init f2fs_init_bioset(void);
  3570. void f2fs_destroy_bioset(void);
  3571. bool f2fs_is_cp_guaranteed(const struct folio *folio);
  3572. int f2fs_init_bio_entry_cache(void);
  3573. void f2fs_destroy_bio_entry_cache(void);
  3574. void f2fs_submit_read_bio(struct f2fs_sb_info *sbi, struct bio *bio,
  3575. enum page_type type);
  3576. int f2fs_init_write_merge_io(struct f2fs_sb_info *sbi);
  3577. void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
  3578. void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
  3579. struct inode *inode, struct folio *folio,
  3580. nid_t ino, enum page_type type);
  3581. void f2fs_submit_merged_write_folio(struct f2fs_sb_info *sbi,
  3582. struct folio *folio, enum page_type type);
  3583. void f2fs_submit_merged_ipu_write(struct f2fs_sb_info *sbi,
  3584. struct bio **bio, struct folio *folio);
  3585. void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
  3586. int f2fs_submit_page_bio(struct f2fs_io_info *fio);
  3587. int f2fs_merge_page_bio(struct f2fs_io_info *fio);
  3588. void f2fs_submit_page_write(struct f2fs_io_info *fio);
  3589. struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
  3590. block_t blk_addr, sector_t *sector);
  3591. int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
  3592. void f2fs_set_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
  3593. void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
  3594. int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
  3595. int f2fs_reserve_new_block(struct dnode_of_data *dn);
  3596. int f2fs_get_block_locked(struct dnode_of_data *dn, pgoff_t index);
  3597. int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
  3598. struct folio *f2fs_get_read_data_folio(struct inode *inode, pgoff_t index,
  3599. blk_opf_t op_flags, bool for_write, pgoff_t *next_pgofs);
  3600. struct folio *f2fs_find_data_folio(struct inode *inode, pgoff_t index,
  3601. pgoff_t *next_pgofs);
  3602. struct folio *f2fs_get_lock_data_folio(struct inode *inode, pgoff_t index,
  3603. bool for_write);
  3604. struct folio *f2fs_get_new_data_folio(struct inode *inode,
  3605. struct folio *ifolio, pgoff_t index, bool new_i_size);
  3606. int f2fs_do_write_data_page(struct f2fs_io_info *fio);
  3607. int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, int flag);
  3608. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3609. u64 start, u64 len);
  3610. int f2fs_encrypt_one_page(struct f2fs_io_info *fio);
  3611. bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
  3612. bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
  3613. int f2fs_write_single_data_page(struct folio *folio, int *submitted,
  3614. struct bio **bio, sector_t *last_block,
  3615. struct writeback_control *wbc,
  3616. enum iostat_type io_type,
  3617. int compr_blocks, bool allow_balance);
  3618. void f2fs_write_failed(struct inode *inode, loff_t to);
  3619. void f2fs_invalidate_folio(struct folio *folio, size_t offset, size_t length);
  3620. bool f2fs_release_folio(struct folio *folio, gfp_t wait);
  3621. bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
  3622. void f2fs_clear_page_cache_dirty_tag(struct folio *folio);
  3623. int f2fs_init_post_read_processing(void);
  3624. void f2fs_destroy_post_read_processing(void);
  3625. int f2fs_init_post_read_wq(struct f2fs_sb_info *sbi);
  3626. void f2fs_destroy_post_read_wq(struct f2fs_sb_info *sbi);
  3627. extern const struct iomap_ops f2fs_iomap_ops;
  3628. /*
  3629. * gc.c
  3630. */
  3631. int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
  3632. void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
  3633. block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
  3634. int f2fs_gc(struct f2fs_sb_info *sbi, struct f2fs_gc_control *gc_control);
  3635. void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
  3636. int f2fs_gc_range(struct f2fs_sb_info *sbi,
  3637. unsigned int start_seg, unsigned int end_seg,
  3638. bool dry_run, unsigned int dry_run_sections);
  3639. int f2fs_resize_fs(struct file *filp, __u64 block_count);
  3640. int __init f2fs_create_garbage_collection_cache(void);
  3641. void f2fs_destroy_garbage_collection_cache(void);
  3642. /* victim selection function for cleaning and SSR */
  3643. int f2fs_get_victim(struct f2fs_sb_info *sbi, unsigned int *result,
  3644. int gc_type, int type, char alloc_mode,
  3645. unsigned long long age, bool one_time);
  3646. /*
  3647. * recovery.c
  3648. */
  3649. int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
  3650. bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
  3651. int __init f2fs_create_recovery_cache(void);
  3652. void f2fs_destroy_recovery_cache(void);
  3653. /*
  3654. * debug.c
  3655. */
  3656. #ifdef CONFIG_F2FS_STAT_FS
  3657. enum {
  3658. DEVSTAT_INUSE,
  3659. DEVSTAT_DIRTY,
  3660. DEVSTAT_FULL,
  3661. DEVSTAT_FREE,
  3662. DEVSTAT_PREFREE,
  3663. DEVSTAT_MAX,
  3664. };
  3665. struct f2fs_dev_stats {
  3666. unsigned int devstats[2][DEVSTAT_MAX]; /* 0: segs, 1: secs */
  3667. };
  3668. struct f2fs_stat_info {
  3669. struct list_head stat_list;
  3670. struct f2fs_sb_info *sbi;
  3671. int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
  3672. int main_area_segs, main_area_sections, main_area_zones;
  3673. unsigned long long hit_cached[NR_EXTENT_CACHES];
  3674. unsigned long long hit_rbtree[NR_EXTENT_CACHES];
  3675. unsigned long long total_ext[NR_EXTENT_CACHES];
  3676. unsigned long long hit_total[NR_EXTENT_CACHES];
  3677. int ext_tree[NR_EXTENT_CACHES];
  3678. int zombie_tree[NR_EXTENT_CACHES];
  3679. int ext_node[NR_EXTENT_CACHES];
  3680. /* to count memory footprint */
  3681. unsigned long long ext_mem[NR_EXTENT_CACHES];
  3682. /* for read extent cache */
  3683. unsigned long long hit_largest;
  3684. /* for block age extent cache */
  3685. unsigned long long allocated_data_blocks;
  3686. int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
  3687. int ndirty_data, ndirty_qdata;
  3688. unsigned int ndirty_dirs, ndirty_files, ndirty_all;
  3689. unsigned int nquota_files, ndonate_files;
  3690. int nats, dirty_nats, sits, dirty_sits;
  3691. int free_nids, avail_nids, alloc_nids;
  3692. int total_count, utilization;
  3693. int nr_wb_cp_data, nr_wb_data;
  3694. int nr_rd_data, nr_rd_node, nr_rd_meta;
  3695. int nr_dio_read, nr_dio_write;
  3696. unsigned int io_skip_bggc, other_skip_bggc;
  3697. int nr_flushing, nr_flushed, flush_list_empty;
  3698. int nr_discarding, nr_discarded;
  3699. int nr_discard_cmd;
  3700. unsigned int undiscard_blks;
  3701. int nr_issued_ckpt, nr_total_ckpt, nr_queued_ckpt;
  3702. unsigned int cur_ckpt_time, peak_ckpt_time;
  3703. int inline_xattr, inline_inode, inline_dir, append, update, orphans;
  3704. int compr_inode, swapfile_inode;
  3705. unsigned long long compr_blocks;
  3706. int aw_cnt, max_aw_cnt;
  3707. unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
  3708. unsigned int bimodal, avg_vblocks;
  3709. int util_free, util_valid, util_invalid;
  3710. int rsvd_segs, overp_segs;
  3711. int dirty_count, node_pages, meta_pages, compress_pages;
  3712. int compress_page_hit;
  3713. int prefree_count, free_segs, free_secs;
  3714. int cp_call_count[MAX_CALL_TYPE], cp_count;
  3715. int gc_call_count[MAX_CALL_TYPE];
  3716. int gc_segs[2][2];
  3717. int gc_secs[2][2];
  3718. int tot_blks, data_blks, node_blks;
  3719. int bg_data_blks, bg_node_blks;
  3720. int blkoff[NR_CURSEG_TYPE];
  3721. int curseg[NR_CURSEG_TYPE];
  3722. int cursec[NR_CURSEG_TYPE];
  3723. int curzone[NR_CURSEG_TYPE];
  3724. unsigned int dirty_seg[NR_CURSEG_TYPE];
  3725. unsigned int full_seg[NR_CURSEG_TYPE];
  3726. unsigned int valid_blks[NR_CURSEG_TYPE];
  3727. unsigned int meta_count[META_MAX];
  3728. unsigned int segment_count[2];
  3729. unsigned int block_count[2];
  3730. unsigned int inplace_count;
  3731. unsigned long long base_mem, cache_mem, page_mem;
  3732. struct f2fs_dev_stats *dev_stats;
  3733. };
  3734. static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
  3735. {
  3736. return (struct f2fs_stat_info *)sbi->stat_info;
  3737. }
  3738. #define stat_inc_cp_call_count(sbi, foreground) \
  3739. atomic_inc(&sbi->cp_call_count[(foreground)])
  3740. #define stat_inc_cp_count(sbi) (F2FS_STAT(sbi)->cp_count++)
  3741. #define stat_io_skip_bggc_count(sbi) ((sbi)->io_skip_bggc++)
  3742. #define stat_other_skip_bggc_count(sbi) ((sbi)->other_skip_bggc++)
  3743. #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
  3744. #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
  3745. #define stat_inc_total_hit(sbi, type) (atomic64_inc(&(sbi)->total_hit_ext[type]))
  3746. #define stat_inc_rbtree_node_hit(sbi, type) (atomic64_inc(&(sbi)->read_hit_rbtree[type]))
  3747. #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
  3748. #define stat_inc_cached_node_hit(sbi, type) (atomic64_inc(&(sbi)->read_hit_cached[type]))
  3749. #define stat_inc_inline_xattr(inode) \
  3750. do { \
  3751. if (f2fs_has_inline_xattr(inode)) \
  3752. (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
  3753. } while (0)
  3754. #define stat_dec_inline_xattr(inode) \
  3755. do { \
  3756. if (f2fs_has_inline_xattr(inode)) \
  3757. (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
  3758. } while (0)
  3759. #define stat_inc_inline_inode(inode) \
  3760. do { \
  3761. if (f2fs_has_inline_data(inode)) \
  3762. (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
  3763. } while (0)
  3764. #define stat_dec_inline_inode(inode) \
  3765. do { \
  3766. if (f2fs_has_inline_data(inode)) \
  3767. (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
  3768. } while (0)
  3769. #define stat_inc_inline_dir(inode) \
  3770. do { \
  3771. if (f2fs_has_inline_dentry(inode)) \
  3772. (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
  3773. } while (0)
  3774. #define stat_dec_inline_dir(inode) \
  3775. do { \
  3776. if (f2fs_has_inline_dentry(inode)) \
  3777. (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
  3778. } while (0)
  3779. #define stat_inc_compr_inode(inode) \
  3780. do { \
  3781. if (f2fs_compressed_file(inode)) \
  3782. (atomic_inc(&F2FS_I_SB(inode)->compr_inode)); \
  3783. } while (0)
  3784. #define stat_dec_compr_inode(inode) \
  3785. do { \
  3786. if (f2fs_compressed_file(inode)) \
  3787. (atomic_dec(&F2FS_I_SB(inode)->compr_inode)); \
  3788. } while (0)
  3789. #define stat_add_compr_blocks(inode, blocks) \
  3790. (atomic64_add(blocks, &F2FS_I_SB(inode)->compr_blocks))
  3791. #define stat_sub_compr_blocks(inode, blocks) \
  3792. (atomic64_sub(blocks, &F2FS_I_SB(inode)->compr_blocks))
  3793. #define stat_inc_swapfile_inode(inode) \
  3794. (atomic_inc(&F2FS_I_SB(inode)->swapfile_inode))
  3795. #define stat_dec_swapfile_inode(inode) \
  3796. (atomic_dec(&F2FS_I_SB(inode)->swapfile_inode))
  3797. #define stat_inc_atomic_inode(inode) \
  3798. (atomic_inc(&F2FS_I_SB(inode)->atomic_files))
  3799. #define stat_dec_atomic_inode(inode) \
  3800. (atomic_dec(&F2FS_I_SB(inode)->atomic_files))
  3801. #define stat_inc_meta_count(sbi, blkaddr) \
  3802. do { \
  3803. if (blkaddr < SIT_I(sbi)->sit_base_addr) \
  3804. atomic_inc(&(sbi)->meta_count[META_CP]); \
  3805. else if (blkaddr < NM_I(sbi)->nat_blkaddr) \
  3806. atomic_inc(&(sbi)->meta_count[META_SIT]); \
  3807. else if (blkaddr < SM_I(sbi)->ssa_blkaddr) \
  3808. atomic_inc(&(sbi)->meta_count[META_NAT]); \
  3809. else if (blkaddr < SM_I(sbi)->main_blkaddr) \
  3810. atomic_inc(&(sbi)->meta_count[META_SSA]); \
  3811. } while (0)
  3812. #define stat_inc_seg_type(sbi, curseg) \
  3813. ((sbi)->segment_count[(curseg)->alloc_type]++)
  3814. #define stat_inc_block_count(sbi, curseg) \
  3815. ((sbi)->block_count[(curseg)->alloc_type]++)
  3816. #define stat_inc_inplace_blocks(sbi) \
  3817. (atomic_inc(&(sbi)->inplace_count))
  3818. #define stat_update_max_atomic_write(inode) \
  3819. do { \
  3820. int cur = atomic_read(&F2FS_I_SB(inode)->atomic_files); \
  3821. int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
  3822. if (cur > max) \
  3823. atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
  3824. } while (0)
  3825. #define stat_inc_gc_call_count(sbi, foreground) \
  3826. (F2FS_STAT(sbi)->gc_call_count[(foreground)]++)
  3827. #define stat_inc_gc_sec_count(sbi, type, gc_type) \
  3828. (F2FS_STAT(sbi)->gc_secs[(type)][(gc_type)]++)
  3829. #define stat_inc_gc_seg_count(sbi, type, gc_type) \
  3830. (F2FS_STAT(sbi)->gc_segs[(type)][(gc_type)]++)
  3831. #define stat_inc_tot_blk_count(si, blks) \
  3832. ((si)->tot_blks += (blks))
  3833. #define stat_inc_data_blk_count(sbi, blks, gc_type) \
  3834. do { \
  3835. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  3836. stat_inc_tot_blk_count(si, blks); \
  3837. si->data_blks += (blks); \
  3838. si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
  3839. } while (0)
  3840. #define stat_inc_node_blk_count(sbi, blks, gc_type) \
  3841. do { \
  3842. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  3843. stat_inc_tot_blk_count(si, blks); \
  3844. si->node_blks += (blks); \
  3845. si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
  3846. } while (0)
  3847. int f2fs_build_stats(struct f2fs_sb_info *sbi);
  3848. void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
  3849. void __init f2fs_create_root_stats(void);
  3850. void f2fs_destroy_root_stats(void);
  3851. void f2fs_update_sit_info(struct f2fs_sb_info *sbi);
  3852. #else
  3853. #define stat_inc_cp_call_count(sbi, foreground) do { } while (0)
  3854. #define stat_inc_cp_count(sbi) do { } while (0)
  3855. #define stat_io_skip_bggc_count(sbi) do { } while (0)
  3856. #define stat_other_skip_bggc_count(sbi) do { } while (0)
  3857. #define stat_inc_dirty_inode(sbi, type) do { } while (0)
  3858. #define stat_dec_dirty_inode(sbi, type) do { } while (0)
  3859. #define stat_inc_total_hit(sbi, type) do { } while (0)
  3860. #define stat_inc_rbtree_node_hit(sbi, type) do { } while (0)
  3861. #define stat_inc_largest_node_hit(sbi) do { } while (0)
  3862. #define stat_inc_cached_node_hit(sbi, type) do { } while (0)
  3863. #define stat_inc_inline_xattr(inode) do { } while (0)
  3864. #define stat_dec_inline_xattr(inode) do { } while (0)
  3865. #define stat_inc_inline_inode(inode) do { } while (0)
  3866. #define stat_dec_inline_inode(inode) do { } while (0)
  3867. #define stat_inc_inline_dir(inode) do { } while (0)
  3868. #define stat_dec_inline_dir(inode) do { } while (0)
  3869. #define stat_inc_compr_inode(inode) do { } while (0)
  3870. #define stat_dec_compr_inode(inode) do { } while (0)
  3871. #define stat_add_compr_blocks(inode, blocks) do { } while (0)
  3872. #define stat_sub_compr_blocks(inode, blocks) do { } while (0)
  3873. #define stat_inc_swapfile_inode(inode) do { } while (0)
  3874. #define stat_dec_swapfile_inode(inode) do { } while (0)
  3875. #define stat_inc_atomic_inode(inode) do { } while (0)
  3876. #define stat_dec_atomic_inode(inode) do { } while (0)
  3877. #define stat_update_max_atomic_write(inode) do { } while (0)
  3878. #define stat_inc_meta_count(sbi, blkaddr) do { } while (0)
  3879. #define stat_inc_seg_type(sbi, curseg) do { } while (0)
  3880. #define stat_inc_block_count(sbi, curseg) do { } while (0)
  3881. #define stat_inc_inplace_blocks(sbi) do { } while (0)
  3882. #define stat_inc_gc_call_count(sbi, foreground) do { } while (0)
  3883. #define stat_inc_gc_sec_count(sbi, type, gc_type) do { } while (0)
  3884. #define stat_inc_gc_seg_count(sbi, type, gc_type) do { } while (0)
  3885. #define stat_inc_tot_blk_count(si, blks) do { } while (0)
  3886. #define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
  3887. #define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
  3888. static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
  3889. static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
  3890. static inline void __init f2fs_create_root_stats(void) { }
  3891. static inline void f2fs_destroy_root_stats(void) { }
  3892. static inline void f2fs_update_sit_info(struct f2fs_sb_info *sbi) {}
  3893. #endif
  3894. extern const struct file_operations f2fs_dir_operations;
  3895. extern const struct file_operations f2fs_file_operations;
  3896. extern const struct inode_operations f2fs_file_inode_operations;
  3897. extern const struct address_space_operations f2fs_dblock_aops;
  3898. extern const struct address_space_operations f2fs_node_aops;
  3899. extern const struct address_space_operations f2fs_meta_aops;
  3900. extern const struct inode_operations f2fs_dir_inode_operations;
  3901. extern const struct inode_operations f2fs_symlink_inode_operations;
  3902. extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
  3903. extern const struct inode_operations f2fs_special_inode_operations;
  3904. extern struct kmem_cache *f2fs_inode_entry_slab;
  3905. /*
  3906. * inline.c
  3907. */
  3908. bool f2fs_may_inline_data(struct inode *inode);
  3909. bool f2fs_sanity_check_inline_data(struct inode *inode, struct folio *ifolio);
  3910. bool f2fs_may_inline_dentry(struct inode *inode);
  3911. void f2fs_do_read_inline_data(struct folio *folio, struct folio *ifolio);
  3912. void f2fs_truncate_inline_inode(struct inode *inode, struct folio *ifolio,
  3913. u64 from);
  3914. int f2fs_read_inline_data(struct inode *inode, struct folio *folio);
  3915. int f2fs_convert_inline_folio(struct dnode_of_data *dn, struct folio *folio);
  3916. int f2fs_convert_inline_inode(struct inode *inode);
  3917. int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry);
  3918. int f2fs_write_inline_data(struct inode *inode, struct folio *folio);
  3919. int f2fs_recover_inline_data(struct inode *inode, struct folio *nfolio);
  3920. struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
  3921. const struct f2fs_filename *fname, struct folio **res_folio,
  3922. bool use_hash);
  3923. int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
  3924. struct folio *ifolio);
  3925. int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
  3926. struct inode *inode, nid_t ino, umode_t mode);
  3927. void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
  3928. struct folio *folio, struct inode *dir, struct inode *inode);
  3929. bool f2fs_empty_inline_dir(struct inode *dir);
  3930. int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
  3931. struct fscrypt_str *fstr);
  3932. int f2fs_inline_data_fiemap(struct inode *inode,
  3933. struct fiemap_extent_info *fieinfo,
  3934. __u64 start, __u64 len);
  3935. /*
  3936. * shrinker.c
  3937. */
  3938. unsigned long f2fs_shrink_count(struct shrinker *shrink,
  3939. struct shrink_control *sc);
  3940. unsigned long f2fs_shrink_scan(struct shrinker *shrink,
  3941. struct shrink_control *sc);
  3942. unsigned int f2fs_donate_files(void);
  3943. void f2fs_reclaim_caches(unsigned int reclaim_caches_kb);
  3944. void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
  3945. void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
  3946. /*
  3947. * extent_cache.c
  3948. */
  3949. bool sanity_check_extent_cache(struct inode *inode, struct folio *ifolio);
  3950. void f2fs_init_extent_tree(struct inode *inode);
  3951. void f2fs_drop_extent_tree(struct inode *inode);
  3952. void f2fs_destroy_extent_node(struct inode *inode);
  3953. void f2fs_destroy_extent_tree(struct inode *inode);
  3954. void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
  3955. int __init f2fs_create_extent_cache(void);
  3956. void f2fs_destroy_extent_cache(void);
  3957. /* read extent cache ops */
  3958. void f2fs_init_read_extent_tree(struct inode *inode, struct folio *ifolio);
  3959. bool f2fs_lookup_read_extent_cache(struct inode *inode, pgoff_t pgofs,
  3960. struct extent_info *ei);
  3961. bool f2fs_lookup_read_extent_cache_block(struct inode *inode, pgoff_t index,
  3962. block_t *blkaddr);
  3963. void f2fs_update_read_extent_cache(struct dnode_of_data *dn);
  3964. void f2fs_update_read_extent_cache_range(struct dnode_of_data *dn,
  3965. pgoff_t fofs, block_t blkaddr, unsigned int len);
  3966. unsigned int f2fs_shrink_read_extent_tree(struct f2fs_sb_info *sbi,
  3967. int nr_shrink);
  3968. /* block age extent cache ops */
  3969. void f2fs_init_age_extent_tree(struct inode *inode);
  3970. bool f2fs_lookup_age_extent_cache(struct inode *inode, pgoff_t pgofs,
  3971. struct extent_info *ei);
  3972. void f2fs_update_age_extent_cache(struct dnode_of_data *dn);
  3973. void f2fs_update_age_extent_cache_range(struct dnode_of_data *dn,
  3974. pgoff_t fofs, unsigned int len);
  3975. unsigned int f2fs_shrink_age_extent_tree(struct f2fs_sb_info *sbi,
  3976. int nr_shrink);
  3977. /*
  3978. * sysfs.c
  3979. */
  3980. #define MIN_RA_MUL 2
  3981. #define MAX_RA_MUL 256
  3982. int __init f2fs_init_sysfs(void);
  3983. void f2fs_exit_sysfs(void);
  3984. int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
  3985. void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
  3986. /* verity.c */
  3987. extern const struct fsverity_operations f2fs_verityops;
  3988. /*
  3989. * crypto support
  3990. */
  3991. static inline bool f2fs_encrypted_file(struct inode *inode)
  3992. {
  3993. return IS_ENCRYPTED(inode) && S_ISREG(inode->i_mode);
  3994. }
  3995. static inline void f2fs_set_encrypted_inode(struct inode *inode)
  3996. {
  3997. #ifdef CONFIG_FS_ENCRYPTION
  3998. file_set_encrypt(inode);
  3999. f2fs_set_inode_flags(inode);
  4000. #endif
  4001. }
  4002. /*
  4003. * Returns true if the reads of the inode's data need to undergo some
  4004. * postprocessing step, like decryption or authenticity verification.
  4005. */
  4006. static inline bool f2fs_post_read_required(struct inode *inode)
  4007. {
  4008. return f2fs_encrypted_file(inode) || fsverity_active(inode) ||
  4009. f2fs_compressed_file(inode);
  4010. }
  4011. static inline bool f2fs_used_in_atomic_write(struct inode *inode)
  4012. {
  4013. return f2fs_is_atomic_file(inode) || f2fs_is_cow_file(inode);
  4014. }
  4015. static inline bool f2fs_meta_inode_gc_required(struct inode *inode)
  4016. {
  4017. return f2fs_post_read_required(inode) || f2fs_used_in_atomic_write(inode);
  4018. }
  4019. /*
  4020. * compress.c
  4021. */
  4022. #ifdef CONFIG_F2FS_FS_COMPRESSION
  4023. enum cluster_check_type {
  4024. CLUSTER_IS_COMPR, /* check only if compressed cluster */
  4025. CLUSTER_COMPR_BLKS, /* return # of compressed blocks in a cluster */
  4026. CLUSTER_RAW_BLKS /* return # of raw blocks in a cluster */
  4027. };
  4028. bool f2fs_is_compressed_page(struct folio *folio);
  4029. struct folio *f2fs_compress_control_folio(struct folio *folio);
  4030. int f2fs_prepare_compress_overwrite(struct inode *inode,
  4031. struct page **pagep, pgoff_t index, void **fsdata);
  4032. bool f2fs_compress_write_end(struct inode *inode, void *fsdata,
  4033. pgoff_t index, unsigned copied);
  4034. int f2fs_truncate_partial_cluster(struct inode *inode, u64 from, bool lock);
  4035. void f2fs_compress_write_end_io(struct bio *bio, struct folio *folio);
  4036. bool f2fs_is_compress_backend_ready(struct inode *inode);
  4037. bool f2fs_is_compress_level_valid(int alg, int lvl);
  4038. int __init f2fs_init_compress_mempool(void);
  4039. void f2fs_destroy_compress_mempool(void);
  4040. void f2fs_decompress_cluster(struct decompress_io_ctx *dic, bool in_task);
  4041. void f2fs_end_read_compressed_page(struct folio *folio, bool failed,
  4042. block_t blkaddr, bool in_task);
  4043. bool f2fs_cluster_is_empty(struct compress_ctx *cc);
  4044. bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index);
  4045. bool f2fs_all_cluster_page_ready(struct compress_ctx *cc, struct page **pages,
  4046. int index, int nr_pages, bool uptodate);
  4047. bool f2fs_sanity_check_cluster(struct dnode_of_data *dn);
  4048. void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct folio *folio);
  4049. int f2fs_write_multi_pages(struct compress_ctx *cc,
  4050. int *submitted,
  4051. struct writeback_control *wbc,
  4052. enum iostat_type io_type);
  4053. int f2fs_is_compressed_cluster(struct inode *inode, pgoff_t index);
  4054. bool f2fs_is_sparse_cluster(struct inode *inode, pgoff_t index);
  4055. void f2fs_update_read_extent_tree_range_compressed(struct inode *inode,
  4056. pgoff_t fofs, block_t blkaddr,
  4057. unsigned int llen, unsigned int c_len);
  4058. int f2fs_read_multi_pages(struct compress_ctx *cc, struct bio **bio_ret,
  4059. unsigned nr_pages, sector_t *last_block_in_bio,
  4060. struct readahead_control *rac, bool for_write);
  4061. struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc);
  4062. void f2fs_decompress_end_io(struct decompress_io_ctx *dic, bool failed,
  4063. bool in_task);
  4064. void f2fs_put_folio_dic(struct folio *folio, bool in_task);
  4065. unsigned int f2fs_cluster_blocks_are_contiguous(struct dnode_of_data *dn,
  4066. unsigned int ofs_in_node);
  4067. int f2fs_init_compress_ctx(struct compress_ctx *cc);
  4068. void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse);
  4069. void f2fs_init_compress_info(struct f2fs_sb_info *sbi);
  4070. int f2fs_init_compress_inode(struct f2fs_sb_info *sbi);
  4071. void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi);
  4072. int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi);
  4073. void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi);
  4074. int __init f2fs_init_compress_cache(void);
  4075. void f2fs_destroy_compress_cache(void);
  4076. struct address_space *COMPRESS_MAPPING(struct f2fs_sb_info *sbi);
  4077. void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
  4078. block_t blkaddr, unsigned int len);
  4079. bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi, struct folio *folio,
  4080. block_t blkaddr);
  4081. void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi, nid_t ino);
  4082. #define inc_compr_inode_stat(inode) \
  4083. do { \
  4084. struct f2fs_sb_info *sbi = F2FS_I_SB(inode); \
  4085. sbi->compr_new_inode++; \
  4086. } while (0)
  4087. #define add_compr_block_stat(inode, blocks) \
  4088. do { \
  4089. struct f2fs_sb_info *sbi = F2FS_I_SB(inode); \
  4090. int diff = F2FS_I(inode)->i_cluster_size - blocks; \
  4091. sbi->compr_written_block += blocks; \
  4092. sbi->compr_saved_block += diff; \
  4093. } while (0)
  4094. #else
  4095. static inline bool f2fs_is_compressed_page(struct folio *folio) { return false; }
  4096. static inline bool f2fs_is_compress_backend_ready(struct inode *inode)
  4097. {
  4098. if (!f2fs_compressed_file(inode))
  4099. return true;
  4100. /* not support compression */
  4101. return false;
  4102. }
  4103. static inline bool f2fs_is_compress_level_valid(int alg, int lvl) { return false; }
  4104. static inline struct folio *f2fs_compress_control_folio(struct folio *folio)
  4105. {
  4106. WARN_ON_ONCE(1);
  4107. return ERR_PTR(-EINVAL);
  4108. }
  4109. static inline int __init f2fs_init_compress_mempool(void) { return 0; }
  4110. static inline void f2fs_destroy_compress_mempool(void) { }
  4111. static inline void f2fs_decompress_cluster(struct decompress_io_ctx *dic,
  4112. bool in_task) { }
  4113. static inline void f2fs_end_read_compressed_page(struct folio *folio,
  4114. bool failed, block_t blkaddr, bool in_task)
  4115. {
  4116. WARN_ON_ONCE(1);
  4117. }
  4118. static inline void f2fs_put_folio_dic(struct folio *folio, bool in_task)
  4119. {
  4120. WARN_ON_ONCE(1);
  4121. }
  4122. static inline unsigned int f2fs_cluster_blocks_are_contiguous(
  4123. struct dnode_of_data *dn, unsigned int ofs_in_node) { return 0; }
  4124. static inline bool f2fs_sanity_check_cluster(struct dnode_of_data *dn) { return false; }
  4125. static inline int f2fs_init_compress_inode(struct f2fs_sb_info *sbi) { return 0; }
  4126. static inline void f2fs_destroy_compress_inode(struct f2fs_sb_info *sbi) { }
  4127. static inline int f2fs_init_page_array_cache(struct f2fs_sb_info *sbi) { return 0; }
  4128. static inline void f2fs_destroy_page_array_cache(struct f2fs_sb_info *sbi) { }
  4129. static inline int __init f2fs_init_compress_cache(void) { return 0; }
  4130. static inline void f2fs_destroy_compress_cache(void) { }
  4131. static inline void f2fs_invalidate_compress_pages_range(struct f2fs_sb_info *sbi,
  4132. block_t blkaddr, unsigned int len) { }
  4133. static inline bool f2fs_load_compressed_folio(struct f2fs_sb_info *sbi,
  4134. struct folio *folio, block_t blkaddr) { return false; }
  4135. static inline void f2fs_invalidate_compress_pages(struct f2fs_sb_info *sbi,
  4136. nid_t ino) { }
  4137. #define inc_compr_inode_stat(inode) do { } while (0)
  4138. static inline int f2fs_is_compressed_cluster(
  4139. struct inode *inode,
  4140. pgoff_t index) { return 0; }
  4141. static inline bool f2fs_is_sparse_cluster(
  4142. struct inode *inode,
  4143. pgoff_t index) { return true; }
  4144. static inline void f2fs_update_read_extent_tree_range_compressed(
  4145. struct inode *inode,
  4146. pgoff_t fofs, block_t blkaddr,
  4147. unsigned int llen, unsigned int c_len) { }
  4148. #endif
  4149. static inline int set_compress_context(struct inode *inode)
  4150. {
  4151. #ifdef CONFIG_F2FS_FS_COMPRESSION
  4152. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  4153. struct f2fs_inode_info *fi = F2FS_I(inode);
  4154. fi->i_compress_algorithm = F2FS_OPTION(sbi).compress_algorithm;
  4155. fi->i_log_cluster_size = F2FS_OPTION(sbi).compress_log_size;
  4156. fi->i_compress_flag = F2FS_OPTION(sbi).compress_chksum ?
  4157. BIT(COMPRESS_CHKSUM) : 0;
  4158. fi->i_cluster_size = BIT(fi->i_log_cluster_size);
  4159. if ((fi->i_compress_algorithm == COMPRESS_LZ4 ||
  4160. fi->i_compress_algorithm == COMPRESS_ZSTD) &&
  4161. F2FS_OPTION(sbi).compress_level)
  4162. fi->i_compress_level = F2FS_OPTION(sbi).compress_level;
  4163. fi->i_flags |= F2FS_COMPR_FL;
  4164. set_inode_flag(inode, FI_COMPRESSED_FILE);
  4165. stat_inc_compr_inode(inode);
  4166. inc_compr_inode_stat(inode);
  4167. f2fs_mark_inode_dirty_sync(inode, true);
  4168. return 0;
  4169. #else
  4170. return -EOPNOTSUPP;
  4171. #endif
  4172. }
  4173. static inline bool f2fs_disable_compressed_file(struct inode *inode)
  4174. {
  4175. struct f2fs_inode_info *fi = F2FS_I(inode);
  4176. f2fs_down_write(&fi->i_sem);
  4177. if (!f2fs_compressed_file(inode)) {
  4178. f2fs_up_write(&fi->i_sem);
  4179. return true;
  4180. }
  4181. if (f2fs_is_mmap_file(inode) || atomic_read(&fi->writeback) ||
  4182. (S_ISREG(inode->i_mode) && F2FS_HAS_BLOCKS(inode))) {
  4183. f2fs_up_write(&fi->i_sem);
  4184. return false;
  4185. }
  4186. fi->i_flags &= ~F2FS_COMPR_FL;
  4187. stat_dec_compr_inode(inode);
  4188. clear_inode_flag(inode, FI_COMPRESSED_FILE);
  4189. f2fs_mark_inode_dirty_sync(inode, true);
  4190. f2fs_up_write(&fi->i_sem);
  4191. return true;
  4192. }
  4193. #define F2FS_FEATURE_FUNCS(name, flagname) \
  4194. static inline bool f2fs_sb_has_##name(struct f2fs_sb_info *sbi) \
  4195. { \
  4196. return F2FS_HAS_FEATURE(sbi, F2FS_FEATURE_##flagname); \
  4197. }
  4198. F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
  4199. F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
  4200. F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
  4201. F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
  4202. F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
  4203. F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
  4204. F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
  4205. F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
  4206. F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
  4207. F2FS_FEATURE_FUNCS(verity, VERITY);
  4208. F2FS_FEATURE_FUNCS(sb_chksum, SB_CHKSUM);
  4209. F2FS_FEATURE_FUNCS(casefold, CASEFOLD);
  4210. F2FS_FEATURE_FUNCS(compression, COMPRESSION);
  4211. F2FS_FEATURE_FUNCS(readonly, RO);
  4212. F2FS_FEATURE_FUNCS(device_alias, DEVICE_ALIAS);
  4213. F2FS_FEATURE_FUNCS(packed_ssa, PACKED_SSA);
  4214. #ifdef CONFIG_BLK_DEV_ZONED
  4215. static inline bool f2fs_zone_is_seq(struct f2fs_sb_info *sbi, int devi,
  4216. unsigned int zone)
  4217. {
  4218. return test_bit(zone, FDEV(devi).blkz_seq);
  4219. }
  4220. static inline bool f2fs_blkz_is_seq(struct f2fs_sb_info *sbi, int devi,
  4221. block_t blkaddr)
  4222. {
  4223. return f2fs_zone_is_seq(sbi, devi, blkaddr / sbi->blocks_per_blkz);
  4224. }
  4225. #endif
  4226. static inline int f2fs_bdev_index(struct f2fs_sb_info *sbi,
  4227. struct block_device *bdev)
  4228. {
  4229. int i;
  4230. if (!f2fs_is_multi_device(sbi))
  4231. return 0;
  4232. for (i = 0; i < sbi->s_ndevs; i++)
  4233. if (FDEV(i).bdev == bdev)
  4234. return i;
  4235. WARN_ON(1);
  4236. return -1;
  4237. }
  4238. static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
  4239. {
  4240. return f2fs_sb_has_blkzoned(sbi);
  4241. }
  4242. static inline bool f2fs_bdev_support_discard(struct block_device *bdev)
  4243. {
  4244. return bdev_max_discard_sectors(bdev) || bdev_is_zoned(bdev);
  4245. }
  4246. static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
  4247. {
  4248. int i;
  4249. if (!f2fs_is_multi_device(sbi))
  4250. return f2fs_bdev_support_discard(sbi->sb->s_bdev);
  4251. for (i = 0; i < sbi->s_ndevs; i++)
  4252. if (f2fs_bdev_support_discard(FDEV(i).bdev))
  4253. return true;
  4254. return false;
  4255. }
  4256. static inline unsigned int f2fs_hw_discard_granularity(struct f2fs_sb_info *sbi)
  4257. {
  4258. int i = 1;
  4259. unsigned int discard_granularity = bdev_discard_granularity(sbi->sb->s_bdev);
  4260. if (f2fs_is_multi_device(sbi))
  4261. for (; i < sbi->s_ndevs && !bdev_is_zoned(FDEV(i).bdev); i++)
  4262. discard_granularity = max_t(unsigned int, discard_granularity,
  4263. bdev_discard_granularity(FDEV(i).bdev));
  4264. return discard_granularity;
  4265. }
  4266. static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
  4267. {
  4268. return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
  4269. f2fs_hw_should_discard(sbi);
  4270. }
  4271. static inline bool f2fs_hw_is_readonly(struct f2fs_sb_info *sbi)
  4272. {
  4273. int i;
  4274. if (!f2fs_is_multi_device(sbi))
  4275. return bdev_read_only(sbi->sb->s_bdev);
  4276. for (i = 0; i < sbi->s_ndevs; i++)
  4277. if (bdev_read_only(FDEV(i).bdev))
  4278. return true;
  4279. return false;
  4280. }
  4281. static inline bool f2fs_dev_is_readonly(struct f2fs_sb_info *sbi)
  4282. {
  4283. return f2fs_sb_has_readonly(sbi) || f2fs_hw_is_readonly(sbi);
  4284. }
  4285. static inline bool f2fs_lfs_mode(struct f2fs_sb_info *sbi)
  4286. {
  4287. return F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS;
  4288. }
  4289. static inline bool f2fs_is_sequential_zone_area(struct f2fs_sb_info *sbi,
  4290. block_t blkaddr)
  4291. {
  4292. if (f2fs_sb_has_blkzoned(sbi)) {
  4293. #ifdef CONFIG_BLK_DEV_ZONED
  4294. int devi = f2fs_target_device_index(sbi, blkaddr);
  4295. if (!bdev_is_zoned(FDEV(devi).bdev))
  4296. return false;
  4297. if (f2fs_is_multi_device(sbi)) {
  4298. if (blkaddr < FDEV(devi).start_blk ||
  4299. blkaddr > FDEV(devi).end_blk) {
  4300. f2fs_err(sbi, "Invalid block %x", blkaddr);
  4301. return false;
  4302. }
  4303. blkaddr -= FDEV(devi).start_blk;
  4304. }
  4305. return f2fs_blkz_is_seq(sbi, devi, blkaddr);
  4306. #else
  4307. return false;
  4308. #endif
  4309. }
  4310. return false;
  4311. }
  4312. static inline bool f2fs_low_mem_mode(struct f2fs_sb_info *sbi)
  4313. {
  4314. return F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW;
  4315. }
  4316. static inline bool f2fs_may_compress(struct inode *inode)
  4317. {
  4318. if (IS_SWAPFILE(inode) || f2fs_is_pinned_file(inode) ||
  4319. f2fs_is_atomic_file(inode) || f2fs_has_inline_data(inode) ||
  4320. f2fs_is_mmap_file(inode))
  4321. return false;
  4322. return S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode);
  4323. }
  4324. static inline void f2fs_i_compr_blocks_update(struct inode *inode,
  4325. u64 blocks, bool add)
  4326. {
  4327. struct f2fs_inode_info *fi = F2FS_I(inode);
  4328. int diff = fi->i_cluster_size - blocks;
  4329. /* don't update i_compr_blocks if saved blocks were released */
  4330. if (!add && !atomic_read(&fi->i_compr_blocks))
  4331. return;
  4332. if (add) {
  4333. atomic_add(diff, &fi->i_compr_blocks);
  4334. stat_add_compr_blocks(inode, diff);
  4335. } else {
  4336. atomic_sub(diff, &fi->i_compr_blocks);
  4337. stat_sub_compr_blocks(inode, diff);
  4338. }
  4339. f2fs_mark_inode_dirty_sync(inode, true);
  4340. }
  4341. static inline bool f2fs_allow_multi_device_dio(struct f2fs_sb_info *sbi,
  4342. int flag)
  4343. {
  4344. if (!f2fs_is_multi_device(sbi))
  4345. return false;
  4346. if (flag != F2FS_GET_BLOCK_DIO)
  4347. return false;
  4348. return sbi->aligned_blksize;
  4349. }
  4350. #ifdef CONFIG_F2FS_FAULT_INJECTION
  4351. extern int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
  4352. unsigned long type, enum fault_option fo);
  4353. extern void f2fs_simulate_lock_timeout(struct f2fs_sb_info *sbi);
  4354. #else
  4355. static inline int f2fs_build_fault_attr(struct f2fs_sb_info *sbi,
  4356. unsigned long rate, unsigned long type,
  4357. enum fault_option fo)
  4358. {
  4359. return 0;
  4360. }
  4361. static inline void f2fs_simulate_lock_timeout(struct f2fs_sb_info *sbi)
  4362. {
  4363. return;
  4364. }
  4365. #endif
  4366. static inline bool is_journalled_quota(struct f2fs_sb_info *sbi)
  4367. {
  4368. #ifdef CONFIG_QUOTA
  4369. if (f2fs_sb_has_quota_ino(sbi))
  4370. return true;
  4371. if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
  4372. F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
  4373. F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
  4374. return true;
  4375. #endif
  4376. return false;
  4377. }
  4378. static inline bool f2fs_quota_file(struct f2fs_sb_info *sbi, nid_t ino)
  4379. {
  4380. #ifdef CONFIG_QUOTA
  4381. int i;
  4382. if (!f2fs_sb_has_quota_ino(sbi))
  4383. return false;
  4384. for (i = 0; i < MAXQUOTAS; i++) {
  4385. if (f2fs_qf_ino(sbi->sb, i) == ino)
  4386. return true;
  4387. }
  4388. #endif
  4389. return false;
  4390. }
  4391. static inline bool f2fs_block_unit_discard(struct f2fs_sb_info *sbi)
  4392. {
  4393. return F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK;
  4394. }
  4395. static inline void __f2fs_schedule_timeout(long timeout, bool io)
  4396. {
  4397. set_current_state(TASK_UNINTERRUPTIBLE);
  4398. if (io)
  4399. io_schedule_timeout(timeout);
  4400. else
  4401. schedule_timeout(timeout);
  4402. }
  4403. #define f2fs_io_schedule_timeout(timeout) \
  4404. __f2fs_schedule_timeout(timeout, true)
  4405. #define f2fs_schedule_timeout(timeout) \
  4406. __f2fs_schedule_timeout(timeout, false)
  4407. static inline void f2fs_schedule_timeout_killable(long timeout, bool io)
  4408. {
  4409. unsigned long last_time = jiffies + timeout;
  4410. while (jiffies < last_time) {
  4411. if (fatal_signal_pending(current))
  4412. return;
  4413. __f2fs_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT, io);
  4414. }
  4415. }
  4416. static inline void f2fs_handle_page_eio(struct f2fs_sb_info *sbi,
  4417. struct folio *folio, enum page_type type)
  4418. {
  4419. pgoff_t ofs = folio->index;
  4420. if (unlikely(f2fs_cp_error(sbi)))
  4421. return;
  4422. if (ofs == sbi->page_eio_ofs[type]) {
  4423. if (sbi->page_eio_cnt[type]++ == MAX_RETRY_PAGE_EIO)
  4424. set_ckpt_flags(sbi, CP_ERROR_FLAG);
  4425. } else {
  4426. sbi->page_eio_ofs[type] = ofs;
  4427. sbi->page_eio_cnt[type] = 0;
  4428. }
  4429. }
  4430. static inline bool f2fs_is_readonly(struct f2fs_sb_info *sbi)
  4431. {
  4432. return f2fs_sb_has_readonly(sbi) || f2fs_readonly(sbi->sb);
  4433. }
  4434. static inline void f2fs_truncate_meta_inode_pages(struct f2fs_sb_info *sbi,
  4435. block_t blkaddr, unsigned int cnt)
  4436. {
  4437. bool need_submit = false;
  4438. int i = 0;
  4439. do {
  4440. struct folio *folio;
  4441. folio = filemap_get_folio(META_MAPPING(sbi), blkaddr + i);
  4442. if (!IS_ERR(folio)) {
  4443. if (folio_test_writeback(folio))
  4444. need_submit = true;
  4445. f2fs_folio_put(folio, false);
  4446. }
  4447. } while (++i < cnt && !need_submit);
  4448. if (need_submit)
  4449. f2fs_submit_merged_write_cond(sbi, sbi->meta_inode,
  4450. NULL, 0, DATA);
  4451. truncate_inode_pages_range(META_MAPPING(sbi),
  4452. F2FS_BLK_TO_BYTES((loff_t)blkaddr),
  4453. F2FS_BLK_END_BYTES((loff_t)(blkaddr + cnt - 1)));
  4454. }
  4455. static inline void f2fs_invalidate_internal_cache(struct f2fs_sb_info *sbi,
  4456. block_t blkaddr, unsigned int len)
  4457. {
  4458. f2fs_truncate_meta_inode_pages(sbi, blkaddr, len);
  4459. f2fs_invalidate_compress_pages_range(sbi, blkaddr, len);
  4460. }
  4461. #endif /* _LINUX_F2FS_H */