f2fs.h 155 KB

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