super.c 154 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * fs/f2fs/super.c
  4. *
  5. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  6. * http://www.samsung.com/
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/fs.h>
  11. #include <linux/fs_context.h>
  12. #include <linux/sched/mm.h>
  13. #include <linux/statfs.h>
  14. #include <linux/kthread.h>
  15. #include <linux/parser.h>
  16. #include <linux/mount.h>
  17. #include <linux/seq_file.h>
  18. #include <linux/proc_fs.h>
  19. #include <linux/random.h>
  20. #include <linux/exportfs.h>
  21. #include <linux/blkdev.h>
  22. #include <linux/quotaops.h>
  23. #include <linux/f2fs_fs.h>
  24. #include <linux/sysfs.h>
  25. #include <linux/quota.h>
  26. #include <linux/unicode.h>
  27. #include <linux/part_stat.h>
  28. #include <linux/zstd.h>
  29. #include <linux/lz4.h>
  30. #include <linux/ctype.h>
  31. #include <linux/fs_parser.h>
  32. #include "f2fs.h"
  33. #include "node.h"
  34. #include "segment.h"
  35. #include "xattr.h"
  36. #include "gc.h"
  37. #include "iostat.h"
  38. #define CREATE_TRACE_POINTS
  39. #include <trace/events/f2fs.h>
  40. static struct kmem_cache *f2fs_inode_cachep;
  41. #ifdef CONFIG_F2FS_FAULT_INJECTION
  42. const char *f2fs_fault_name[FAULT_MAX] = {
  43. [FAULT_KMALLOC] = "kmalloc",
  44. [FAULT_KVMALLOC] = "kvmalloc",
  45. [FAULT_PAGE_ALLOC] = "page alloc",
  46. [FAULT_PAGE_GET] = "page get",
  47. [FAULT_ALLOC_BIO] = "alloc bio(obsolete)",
  48. [FAULT_ALLOC_NID] = "alloc nid",
  49. [FAULT_ORPHAN] = "orphan",
  50. [FAULT_BLOCK] = "no more block",
  51. [FAULT_DIR_DEPTH] = "too big dir depth",
  52. [FAULT_EVICT_INODE] = "evict_inode fail",
  53. [FAULT_TRUNCATE] = "truncate fail",
  54. [FAULT_READ_IO] = "read IO error",
  55. [FAULT_CHECKPOINT] = "checkpoint error",
  56. [FAULT_DISCARD] = "discard error",
  57. [FAULT_WRITE_IO] = "write IO error",
  58. [FAULT_SLAB_ALLOC] = "slab alloc",
  59. [FAULT_DQUOT_INIT] = "dquot initialize",
  60. [FAULT_LOCK_OP] = "lock_op",
  61. [FAULT_BLKADDR_VALIDITY] = "invalid blkaddr",
  62. [FAULT_BLKADDR_CONSISTENCE] = "inconsistent blkaddr",
  63. [FAULT_NO_SEGMENT] = "no free segment",
  64. [FAULT_INCONSISTENT_FOOTER] = "inconsistent footer",
  65. [FAULT_ATOMIC_TIMEOUT] = "atomic timeout",
  66. [FAULT_VMALLOC] = "vmalloc",
  67. [FAULT_LOCK_TIMEOUT] = "lock timeout",
  68. [FAULT_SKIP_WRITE] = "skip write",
  69. };
  70. int f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned long rate,
  71. unsigned long type, enum fault_option fo)
  72. {
  73. struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
  74. if (fo & FAULT_ALL) {
  75. memset(ffi, 0, sizeof(struct f2fs_fault_info));
  76. return 0;
  77. }
  78. if (fo & FAULT_RATE) {
  79. if (rate > INT_MAX)
  80. return -EINVAL;
  81. atomic_set(&ffi->inject_ops, 0);
  82. ffi->inject_rate = (int)rate;
  83. f2fs_info(sbi, "build fault injection rate: %lu", rate);
  84. }
  85. if (fo & FAULT_TYPE) {
  86. if (type >= BIT(FAULT_MAX))
  87. return -EINVAL;
  88. ffi->inject_type = (unsigned int)type;
  89. f2fs_info(sbi, "build fault injection type: 0x%lx", type);
  90. }
  91. if (fo & FAULT_TIMEOUT) {
  92. if (type >= TIMEOUT_TYPE_MAX)
  93. return -EINVAL;
  94. ffi->inject_lock_timeout = (unsigned int)type;
  95. f2fs_info(sbi, "build fault timeout injection type: 0x%lx", type);
  96. }
  97. return 0;
  98. }
  99. static void inject_timeout(struct f2fs_sb_info *sbi)
  100. {
  101. struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
  102. enum f2fs_timeout_type type = ffi->inject_lock_timeout;
  103. unsigned long start_time = jiffies;
  104. unsigned long timeout = HZ;
  105. switch (type) {
  106. case TIMEOUT_TYPE_RUNNING:
  107. while (!time_after(jiffies, start_time + timeout)) {
  108. if (fatal_signal_pending(current))
  109. return;
  110. ;
  111. }
  112. break;
  113. case TIMEOUT_TYPE_IO_SLEEP:
  114. f2fs_schedule_timeout_killable(timeout, true);
  115. break;
  116. case TIMEOUT_TYPE_NONIO_SLEEP:
  117. f2fs_schedule_timeout_killable(timeout, false);
  118. break;
  119. case TIMEOUT_TYPE_RUNNABLE:
  120. while (!time_after(jiffies, start_time + timeout)) {
  121. if (fatal_signal_pending(current))
  122. return;
  123. schedule();
  124. }
  125. break;
  126. default:
  127. return;
  128. }
  129. }
  130. void f2fs_simulate_lock_timeout(struct f2fs_sb_info *sbi)
  131. {
  132. struct f2fs_lock_context lc;
  133. f2fs_lock_op(sbi, &lc);
  134. inject_timeout(sbi);
  135. f2fs_unlock_op(sbi, &lc);
  136. }
  137. #endif
  138. /* f2fs-wide shrinker description */
  139. static struct shrinker *f2fs_shrinker_info;
  140. static int __init f2fs_init_shrinker(void)
  141. {
  142. f2fs_shrinker_info = shrinker_alloc(0, "f2fs-shrinker");
  143. if (!f2fs_shrinker_info)
  144. return -ENOMEM;
  145. f2fs_shrinker_info->count_objects = f2fs_shrink_count;
  146. f2fs_shrinker_info->scan_objects = f2fs_shrink_scan;
  147. shrinker_register(f2fs_shrinker_info);
  148. return 0;
  149. }
  150. static void f2fs_exit_shrinker(void)
  151. {
  152. shrinker_free(f2fs_shrinker_info);
  153. }
  154. enum {
  155. Opt_gc_background,
  156. Opt_disable_roll_forward,
  157. Opt_norecovery,
  158. Opt_discard,
  159. Opt_noheap,
  160. Opt_heap,
  161. Opt_user_xattr,
  162. Opt_acl,
  163. Opt_active_logs,
  164. Opt_disable_ext_identify,
  165. Opt_inline_xattr,
  166. Opt_inline_xattr_size,
  167. Opt_inline_data,
  168. Opt_inline_dentry,
  169. Opt_flush_merge,
  170. Opt_barrier,
  171. Opt_fastboot,
  172. Opt_extent_cache,
  173. Opt_data_flush,
  174. Opt_reserve_root,
  175. Opt_reserve_node,
  176. Opt_resgid,
  177. Opt_resuid,
  178. Opt_mode,
  179. Opt_fault_injection,
  180. Opt_fault_type,
  181. Opt_lazytime,
  182. Opt_quota,
  183. Opt_usrquota,
  184. Opt_grpquota,
  185. Opt_prjquota,
  186. Opt_usrjquota,
  187. Opt_grpjquota,
  188. Opt_prjjquota,
  189. Opt_alloc,
  190. Opt_fsync,
  191. Opt_test_dummy_encryption,
  192. Opt_inlinecrypt,
  193. Opt_checkpoint_disable,
  194. Opt_checkpoint_disable_cap,
  195. Opt_checkpoint_disable_cap_perc,
  196. Opt_checkpoint_enable,
  197. Opt_checkpoint_merge,
  198. Opt_compress_algorithm,
  199. Opt_compress_log_size,
  200. Opt_nocompress_extension,
  201. Opt_compress_extension,
  202. Opt_compress_chksum,
  203. Opt_compress_mode,
  204. Opt_compress_cache,
  205. Opt_atgc,
  206. Opt_gc_merge,
  207. Opt_discard_unit,
  208. Opt_memory_mode,
  209. Opt_age_extent_cache,
  210. Opt_errors,
  211. Opt_nat_bits,
  212. Opt_jqfmt,
  213. Opt_checkpoint,
  214. Opt_lookup_mode,
  215. Opt_err,
  216. };
  217. static const struct constant_table f2fs_param_background_gc[] = {
  218. {"on", BGGC_MODE_ON},
  219. {"off", BGGC_MODE_OFF},
  220. {"sync", BGGC_MODE_SYNC},
  221. {}
  222. };
  223. static const struct constant_table f2fs_param_mode[] = {
  224. {"adaptive", FS_MODE_ADAPTIVE},
  225. {"lfs", FS_MODE_LFS},
  226. {"fragment:segment", FS_MODE_FRAGMENT_SEG},
  227. {"fragment:block", FS_MODE_FRAGMENT_BLK},
  228. {}
  229. };
  230. static const struct constant_table f2fs_param_jqfmt[] = {
  231. {"vfsold", QFMT_VFS_OLD},
  232. {"vfsv0", QFMT_VFS_V0},
  233. {"vfsv1", QFMT_VFS_V1},
  234. {}
  235. };
  236. static const struct constant_table f2fs_param_alloc_mode[] = {
  237. {"default", ALLOC_MODE_DEFAULT},
  238. {"reuse", ALLOC_MODE_REUSE},
  239. {}
  240. };
  241. static const struct constant_table f2fs_param_fsync_mode[] = {
  242. {"posix", FSYNC_MODE_POSIX},
  243. {"strict", FSYNC_MODE_STRICT},
  244. {"nobarrier", FSYNC_MODE_NOBARRIER},
  245. {}
  246. };
  247. static const struct constant_table f2fs_param_compress_mode[] = {
  248. {"fs", COMPR_MODE_FS},
  249. {"user", COMPR_MODE_USER},
  250. {}
  251. };
  252. static const struct constant_table f2fs_param_discard_unit[] = {
  253. {"block", DISCARD_UNIT_BLOCK},
  254. {"segment", DISCARD_UNIT_SEGMENT},
  255. {"section", DISCARD_UNIT_SECTION},
  256. {}
  257. };
  258. static const struct constant_table f2fs_param_memory_mode[] = {
  259. {"normal", MEMORY_MODE_NORMAL},
  260. {"low", MEMORY_MODE_LOW},
  261. {}
  262. };
  263. static const struct constant_table f2fs_param_errors[] = {
  264. {"remount-ro", MOUNT_ERRORS_READONLY},
  265. {"continue", MOUNT_ERRORS_CONTINUE},
  266. {"panic", MOUNT_ERRORS_PANIC},
  267. {}
  268. };
  269. static const struct constant_table f2fs_param_lookup_mode[] = {
  270. {"perf", LOOKUP_PERF},
  271. {"compat", LOOKUP_COMPAT},
  272. {"auto", LOOKUP_AUTO},
  273. {}
  274. };
  275. static const struct fs_parameter_spec f2fs_param_specs[] = {
  276. fsparam_enum("background_gc", Opt_gc_background, f2fs_param_background_gc),
  277. fsparam_flag("disable_roll_forward", Opt_disable_roll_forward),
  278. fsparam_flag("norecovery", Opt_norecovery),
  279. fsparam_flag_no("discard", Opt_discard),
  280. fsparam_flag("no_heap", Opt_noheap),
  281. fsparam_flag("heap", Opt_heap),
  282. fsparam_flag_no("user_xattr", Opt_user_xattr),
  283. fsparam_flag_no("acl", Opt_acl),
  284. fsparam_s32("active_logs", Opt_active_logs),
  285. fsparam_flag("disable_ext_identify", Opt_disable_ext_identify),
  286. fsparam_flag_no("inline_xattr", Opt_inline_xattr),
  287. fsparam_s32("inline_xattr_size", Opt_inline_xattr_size),
  288. fsparam_flag_no("inline_data", Opt_inline_data),
  289. fsparam_flag_no("inline_dentry", Opt_inline_dentry),
  290. fsparam_flag_no("flush_merge", Opt_flush_merge),
  291. fsparam_flag_no("barrier", Opt_barrier),
  292. fsparam_flag("fastboot", Opt_fastboot),
  293. fsparam_flag_no("extent_cache", Opt_extent_cache),
  294. fsparam_flag("data_flush", Opt_data_flush),
  295. fsparam_u32("reserve_root", Opt_reserve_root),
  296. fsparam_u32("reserve_node", Opt_reserve_node),
  297. fsparam_gid("resgid", Opt_resgid),
  298. fsparam_uid("resuid", Opt_resuid),
  299. fsparam_enum("mode", Opt_mode, f2fs_param_mode),
  300. fsparam_s32("fault_injection", Opt_fault_injection),
  301. fsparam_u32("fault_type", Opt_fault_type),
  302. fsparam_flag_no("lazytime", Opt_lazytime),
  303. fsparam_flag_no("quota", Opt_quota),
  304. fsparam_flag("usrquota", Opt_usrquota),
  305. fsparam_flag("grpquota", Opt_grpquota),
  306. fsparam_flag("prjquota", Opt_prjquota),
  307. fsparam_string_empty("usrjquota", Opt_usrjquota),
  308. fsparam_string_empty("grpjquota", Opt_grpjquota),
  309. fsparam_string_empty("prjjquota", Opt_prjjquota),
  310. fsparam_flag("nat_bits", Opt_nat_bits),
  311. fsparam_enum("jqfmt", Opt_jqfmt, f2fs_param_jqfmt),
  312. fsparam_enum("alloc_mode", Opt_alloc, f2fs_param_alloc_mode),
  313. fsparam_enum("fsync_mode", Opt_fsync, f2fs_param_fsync_mode),
  314. fsparam_string("test_dummy_encryption", Opt_test_dummy_encryption),
  315. fsparam_flag("test_dummy_encryption", Opt_test_dummy_encryption),
  316. fsparam_flag("inlinecrypt", Opt_inlinecrypt),
  317. fsparam_string("checkpoint", Opt_checkpoint),
  318. fsparam_flag_no("checkpoint_merge", Opt_checkpoint_merge),
  319. fsparam_string("compress_algorithm", Opt_compress_algorithm),
  320. fsparam_u32("compress_log_size", Opt_compress_log_size),
  321. fsparam_string("compress_extension", Opt_compress_extension),
  322. fsparam_string("nocompress_extension", Opt_nocompress_extension),
  323. fsparam_flag("compress_chksum", Opt_compress_chksum),
  324. fsparam_enum("compress_mode", Opt_compress_mode, f2fs_param_compress_mode),
  325. fsparam_flag("compress_cache", Opt_compress_cache),
  326. fsparam_flag("atgc", Opt_atgc),
  327. fsparam_flag_no("gc_merge", Opt_gc_merge),
  328. fsparam_enum("discard_unit", Opt_discard_unit, f2fs_param_discard_unit),
  329. fsparam_enum("memory", Opt_memory_mode, f2fs_param_memory_mode),
  330. fsparam_flag("age_extent_cache", Opt_age_extent_cache),
  331. fsparam_enum("errors", Opt_errors, f2fs_param_errors),
  332. fsparam_enum("lookup_mode", Opt_lookup_mode, f2fs_param_lookup_mode),
  333. {}
  334. };
  335. /* Resort to a match_table for this interestingly formatted option */
  336. static match_table_t f2fs_checkpoint_tokens = {
  337. {Opt_checkpoint_disable, "disable"},
  338. {Opt_checkpoint_disable_cap, "disable:%u"},
  339. {Opt_checkpoint_disable_cap_perc, "disable:%u%%"},
  340. {Opt_checkpoint_enable, "enable"},
  341. {Opt_err, NULL},
  342. };
  343. #define F2FS_SPEC_background_gc (1 << 0)
  344. #define F2FS_SPEC_inline_xattr_size (1 << 1)
  345. #define F2FS_SPEC_active_logs (1 << 2)
  346. #define F2FS_SPEC_reserve_root (1 << 3)
  347. #define F2FS_SPEC_resgid (1 << 4)
  348. #define F2FS_SPEC_resuid (1 << 5)
  349. #define F2FS_SPEC_mode (1 << 6)
  350. #define F2FS_SPEC_fault_injection (1 << 7)
  351. #define F2FS_SPEC_fault_type (1 << 8)
  352. #define F2FS_SPEC_jqfmt (1 << 9)
  353. #define F2FS_SPEC_alloc_mode (1 << 10)
  354. #define F2FS_SPEC_fsync_mode (1 << 11)
  355. #define F2FS_SPEC_checkpoint_disable_cap (1 << 12)
  356. #define F2FS_SPEC_checkpoint_disable_cap_perc (1 << 13)
  357. #define F2FS_SPEC_compress_level (1 << 14)
  358. #define F2FS_SPEC_compress_algorithm (1 << 15)
  359. #define F2FS_SPEC_compress_log_size (1 << 16)
  360. #define F2FS_SPEC_compress_extension (1 << 17)
  361. #define F2FS_SPEC_nocompress_extension (1 << 18)
  362. #define F2FS_SPEC_compress_chksum (1 << 19)
  363. #define F2FS_SPEC_compress_mode (1 << 20)
  364. #define F2FS_SPEC_discard_unit (1 << 21)
  365. #define F2FS_SPEC_memory_mode (1 << 22)
  366. #define F2FS_SPEC_errors (1 << 23)
  367. #define F2FS_SPEC_lookup_mode (1 << 24)
  368. #define F2FS_SPEC_reserve_node (1 << 25)
  369. struct f2fs_fs_context {
  370. struct f2fs_mount_info info;
  371. unsigned long long opt_mask; /* Bits changed */
  372. unsigned int spec_mask;
  373. unsigned short qname_mask;
  374. };
  375. #define F2FS_CTX_INFO(ctx) ((ctx)->info)
  376. static inline void ctx_set_opt(struct f2fs_fs_context *ctx,
  377. enum f2fs_mount_opt flag)
  378. {
  379. ctx->info.opt |= BIT(flag);
  380. ctx->opt_mask |= BIT(flag);
  381. }
  382. static inline void ctx_clear_opt(struct f2fs_fs_context *ctx,
  383. enum f2fs_mount_opt flag)
  384. {
  385. ctx->info.opt &= ~BIT(flag);
  386. ctx->opt_mask |= BIT(flag);
  387. }
  388. static inline bool ctx_test_opt(struct f2fs_fs_context *ctx,
  389. enum f2fs_mount_opt flag)
  390. {
  391. return ctx->info.opt & BIT(flag);
  392. }
  393. void f2fs_printk(struct f2fs_sb_info *sbi, bool limit_rate,
  394. const char *fmt, ...)
  395. {
  396. struct va_format vaf;
  397. va_list args;
  398. int level;
  399. va_start(args, fmt);
  400. level = printk_get_level(fmt);
  401. vaf.fmt = printk_skip_level(fmt);
  402. vaf.va = &args;
  403. if (limit_rate)
  404. if (sbi)
  405. printk_ratelimited("%c%cF2FS-fs (%s): %pV\n",
  406. KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
  407. else
  408. printk_ratelimited("%c%cF2FS-fs: %pV\n",
  409. KERN_SOH_ASCII, level, &vaf);
  410. else
  411. if (sbi)
  412. printk("%c%cF2FS-fs (%s): %pV\n",
  413. KERN_SOH_ASCII, level, sbi->sb->s_id, &vaf);
  414. else
  415. printk("%c%cF2FS-fs: %pV\n",
  416. KERN_SOH_ASCII, level, &vaf);
  417. va_end(args);
  418. }
  419. #if IS_ENABLED(CONFIG_UNICODE)
  420. static const struct f2fs_sb_encodings {
  421. __u16 magic;
  422. char *name;
  423. unsigned int version;
  424. } f2fs_sb_encoding_map[] = {
  425. {F2FS_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
  426. };
  427. static const struct f2fs_sb_encodings *
  428. f2fs_sb_read_encoding(const struct f2fs_super_block *sb)
  429. {
  430. __u16 magic = le16_to_cpu(sb->s_encoding);
  431. int i;
  432. for (i = 0; i < ARRAY_SIZE(f2fs_sb_encoding_map); i++)
  433. if (magic == f2fs_sb_encoding_map[i].magic)
  434. return &f2fs_sb_encoding_map[i];
  435. return NULL;
  436. }
  437. struct kmem_cache *f2fs_cf_name_slab;
  438. static int __init f2fs_create_casefold_cache(void)
  439. {
  440. f2fs_cf_name_slab = f2fs_kmem_cache_create("f2fs_casefolded_name",
  441. F2FS_NAME_LEN);
  442. return f2fs_cf_name_slab ? 0 : -ENOMEM;
  443. }
  444. static void f2fs_destroy_casefold_cache(void)
  445. {
  446. kmem_cache_destroy(f2fs_cf_name_slab);
  447. }
  448. #else
  449. static int __init f2fs_create_casefold_cache(void) { return 0; }
  450. static void f2fs_destroy_casefold_cache(void) { }
  451. #endif
  452. static inline void limit_reserve_root(struct f2fs_sb_info *sbi)
  453. {
  454. block_t block_limit = min((sbi->user_block_count >> 3),
  455. sbi->user_block_count - sbi->reserved_blocks);
  456. block_t node_limit = sbi->total_node_count >> 3;
  457. /* limit is 12.5% */
  458. if (test_opt(sbi, RESERVE_ROOT) &&
  459. F2FS_OPTION(sbi).root_reserved_blocks > block_limit) {
  460. F2FS_OPTION(sbi).root_reserved_blocks = block_limit;
  461. f2fs_info(sbi, "Reduce reserved blocks for root = %u",
  462. F2FS_OPTION(sbi).root_reserved_blocks);
  463. }
  464. if (test_opt(sbi, RESERVE_NODE) &&
  465. F2FS_OPTION(sbi).root_reserved_nodes > node_limit) {
  466. F2FS_OPTION(sbi).root_reserved_nodes = node_limit;
  467. f2fs_info(sbi, "Reduce reserved nodes for root = %u",
  468. F2FS_OPTION(sbi).root_reserved_nodes);
  469. }
  470. if (!test_opt(sbi, RESERVE_ROOT) && !test_opt(sbi, RESERVE_NODE) &&
  471. (!uid_eq(F2FS_OPTION(sbi).s_resuid,
  472. make_kuid(&init_user_ns, F2FS_DEF_RESUID)) ||
  473. !gid_eq(F2FS_OPTION(sbi).s_resgid,
  474. make_kgid(&init_user_ns, F2FS_DEF_RESGID))))
  475. f2fs_info(sbi, "Ignore s_resuid=%u, s_resgid=%u w/o reserve_root"
  476. " and reserve_node",
  477. from_kuid_munged(&init_user_ns,
  478. F2FS_OPTION(sbi).s_resuid),
  479. from_kgid_munged(&init_user_ns,
  480. F2FS_OPTION(sbi).s_resgid));
  481. }
  482. static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
  483. {
  484. if (!F2FS_OPTION(sbi).unusable_cap_perc)
  485. return;
  486. if (F2FS_OPTION(sbi).unusable_cap_perc == 100)
  487. F2FS_OPTION(sbi).unusable_cap = sbi->user_block_count;
  488. else
  489. F2FS_OPTION(sbi).unusable_cap = (sbi->user_block_count / 100) *
  490. F2FS_OPTION(sbi).unusable_cap_perc;
  491. f2fs_info(sbi, "Adjust unusable cap for checkpoint=disable = %u / %u%%",
  492. F2FS_OPTION(sbi).unusable_cap,
  493. F2FS_OPTION(sbi).unusable_cap_perc);
  494. }
  495. static void init_once(void *foo)
  496. {
  497. struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
  498. inode_init_once(&fi->vfs_inode);
  499. #ifdef CONFIG_FS_ENCRYPTION
  500. fi->i_crypt_info = NULL;
  501. #endif
  502. }
  503. #ifdef CONFIG_QUOTA
  504. static const char * const quotatypes[] = INITQFNAMES;
  505. #define QTYPE2NAME(t) (quotatypes[t])
  506. /*
  507. * Note the name of the specified quota file.
  508. */
  509. static int f2fs_note_qf_name(struct fs_context *fc, int qtype,
  510. struct fs_parameter *param)
  511. {
  512. struct f2fs_fs_context *ctx = fc->fs_private;
  513. char *qname;
  514. if (param->size < 1) {
  515. f2fs_err(NULL, "Missing quota name");
  516. return -EINVAL;
  517. }
  518. if (strchr(param->string, '/')) {
  519. f2fs_err(NULL, "quotafile must be on filesystem root");
  520. return -EINVAL;
  521. }
  522. if (ctx->info.s_qf_names[qtype]) {
  523. if (strcmp(ctx->info.s_qf_names[qtype], param->string) != 0) {
  524. f2fs_err(NULL, "Quota file already specified");
  525. return -EINVAL;
  526. }
  527. return 0;
  528. }
  529. qname = kmemdup_nul(param->string, param->size, GFP_KERNEL);
  530. if (!qname) {
  531. f2fs_err(NULL, "Not enough memory for storing quotafile name");
  532. return -ENOMEM;
  533. }
  534. F2FS_CTX_INFO(ctx).s_qf_names[qtype] = qname;
  535. ctx->qname_mask |= 1 << qtype;
  536. return 0;
  537. }
  538. /*
  539. * Clear the name of the specified quota file.
  540. */
  541. static int f2fs_unnote_qf_name(struct fs_context *fc, int qtype)
  542. {
  543. struct f2fs_fs_context *ctx = fc->fs_private;
  544. kfree(ctx->info.s_qf_names[qtype]);
  545. ctx->info.s_qf_names[qtype] = NULL;
  546. ctx->qname_mask |= 1 << qtype;
  547. return 0;
  548. }
  549. static void f2fs_unnote_qf_name_all(struct fs_context *fc)
  550. {
  551. int i;
  552. for (i = 0; i < MAXQUOTAS; i++)
  553. f2fs_unnote_qf_name(fc, i);
  554. }
  555. #endif
  556. static int f2fs_parse_test_dummy_encryption(const struct fs_parameter *param,
  557. struct f2fs_fs_context *ctx)
  558. {
  559. int err;
  560. if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
  561. f2fs_warn(NULL, "test_dummy_encryption option not supported");
  562. return -EINVAL;
  563. }
  564. err = fscrypt_parse_test_dummy_encryption(param,
  565. &ctx->info.dummy_enc_policy);
  566. if (err) {
  567. if (err == -EINVAL)
  568. f2fs_warn(NULL, "Value of option \"%s\" is unrecognized",
  569. param->key);
  570. else if (err == -EEXIST)
  571. f2fs_warn(NULL, "Conflicting test_dummy_encryption options");
  572. else
  573. f2fs_warn(NULL, "Error processing option \"%s\" [%d]",
  574. param->key, err);
  575. return -EINVAL;
  576. }
  577. return 0;
  578. }
  579. #ifdef CONFIG_F2FS_FS_COMPRESSION
  580. static bool is_compress_extension_exist(struct f2fs_mount_info *info,
  581. const char *new_ext, bool is_ext)
  582. {
  583. unsigned char (*ext)[F2FS_EXTENSION_LEN];
  584. int ext_cnt;
  585. int i;
  586. if (is_ext) {
  587. ext = info->extensions;
  588. ext_cnt = info->compress_ext_cnt;
  589. } else {
  590. ext = info->noextensions;
  591. ext_cnt = info->nocompress_ext_cnt;
  592. }
  593. for (i = 0; i < ext_cnt; i++) {
  594. if (!strcasecmp(new_ext, ext[i]))
  595. return true;
  596. }
  597. return false;
  598. }
  599. /*
  600. * 1. The same extension name cannot not appear in both compress and non-compress extension
  601. * at the same time.
  602. * 2. If the compress extension specifies all files, the types specified by the non-compress
  603. * extension will be treated as special cases and will not be compressed.
  604. * 3. Don't allow the non-compress extension specifies all files.
  605. */
  606. static int f2fs_test_compress_extension(unsigned char (*noext)[F2FS_EXTENSION_LEN],
  607. int noext_cnt,
  608. unsigned char (*ext)[F2FS_EXTENSION_LEN],
  609. int ext_cnt)
  610. {
  611. int index = 0, no_index = 0;
  612. if (!noext_cnt)
  613. return 0;
  614. for (no_index = 0; no_index < noext_cnt; no_index++) {
  615. if (strlen(noext[no_index]) == 0)
  616. continue;
  617. if (!strcasecmp("*", noext[no_index])) {
  618. f2fs_info(NULL, "Don't allow the nocompress extension specifies all files");
  619. return -EINVAL;
  620. }
  621. for (index = 0; index < ext_cnt; index++) {
  622. if (strlen(ext[index]) == 0)
  623. continue;
  624. if (!strcasecmp(ext[index], noext[no_index])) {
  625. f2fs_info(NULL, "Don't allow the same extension %s appear in both compress and nocompress extension",
  626. ext[index]);
  627. return -EINVAL;
  628. }
  629. }
  630. }
  631. return 0;
  632. }
  633. #ifdef CONFIG_F2FS_FS_LZ4
  634. static int f2fs_set_lz4hc_level(struct f2fs_fs_context *ctx, const char *str)
  635. {
  636. #ifdef CONFIG_F2FS_FS_LZ4HC
  637. unsigned int level;
  638. if (strlen(str) == 3) {
  639. F2FS_CTX_INFO(ctx).compress_level = 0;
  640. ctx->spec_mask |= F2FS_SPEC_compress_level;
  641. return 0;
  642. }
  643. str += 3;
  644. if (str[0] != ':') {
  645. f2fs_info(NULL, "wrong format, e.g. <alg_name>:<compr_level>");
  646. return -EINVAL;
  647. }
  648. if (kstrtouint(str + 1, 10, &level))
  649. return -EINVAL;
  650. if (!f2fs_is_compress_level_valid(COMPRESS_LZ4, level)) {
  651. f2fs_info(NULL, "invalid lz4hc compress level: %d", level);
  652. return -EINVAL;
  653. }
  654. F2FS_CTX_INFO(ctx).compress_level = level;
  655. ctx->spec_mask |= F2FS_SPEC_compress_level;
  656. return 0;
  657. #else
  658. if (strlen(str) == 3) {
  659. F2FS_CTX_INFO(ctx).compress_level = 0;
  660. ctx->spec_mask |= F2FS_SPEC_compress_level;
  661. return 0;
  662. }
  663. f2fs_info(NULL, "kernel doesn't support lz4hc compression");
  664. return -EINVAL;
  665. #endif
  666. }
  667. #endif
  668. #ifdef CONFIG_F2FS_FS_ZSTD
  669. static int f2fs_set_zstd_level(struct f2fs_fs_context *ctx, const char *str)
  670. {
  671. int level;
  672. int len = 4;
  673. if (strlen(str) == len) {
  674. F2FS_CTX_INFO(ctx).compress_level = F2FS_ZSTD_DEFAULT_CLEVEL;
  675. ctx->spec_mask |= F2FS_SPEC_compress_level;
  676. return 0;
  677. }
  678. str += len;
  679. if (str[0] != ':') {
  680. f2fs_info(NULL, "wrong format, e.g. <alg_name>:<compr_level>");
  681. return -EINVAL;
  682. }
  683. if (kstrtoint(str + 1, 10, &level))
  684. return -EINVAL;
  685. /* f2fs does not support negative compress level now */
  686. if (level < 0) {
  687. f2fs_info(NULL, "do not support negative compress level: %d", level);
  688. return -ERANGE;
  689. }
  690. if (!f2fs_is_compress_level_valid(COMPRESS_ZSTD, level)) {
  691. f2fs_info(NULL, "invalid zstd compress level: %d", level);
  692. return -EINVAL;
  693. }
  694. F2FS_CTX_INFO(ctx).compress_level = level;
  695. ctx->spec_mask |= F2FS_SPEC_compress_level;
  696. return 0;
  697. }
  698. #endif
  699. #endif
  700. static int f2fs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  701. {
  702. struct f2fs_fs_context *ctx = fc->fs_private;
  703. #ifdef CONFIG_F2FS_FS_COMPRESSION
  704. unsigned char (*ext)[F2FS_EXTENSION_LEN];
  705. unsigned char (*noext)[F2FS_EXTENSION_LEN];
  706. int ext_cnt, noext_cnt;
  707. char *name;
  708. #endif
  709. substring_t args[MAX_OPT_ARGS];
  710. struct fs_parse_result result;
  711. int token, ret, arg;
  712. token = fs_parse(fc, f2fs_param_specs, param, &result);
  713. if (token < 0)
  714. return token;
  715. switch (token) {
  716. case Opt_gc_background:
  717. F2FS_CTX_INFO(ctx).bggc_mode = result.uint_32;
  718. ctx->spec_mask |= F2FS_SPEC_background_gc;
  719. break;
  720. case Opt_disable_roll_forward:
  721. ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_ROLL_FORWARD);
  722. break;
  723. case Opt_norecovery:
  724. /* requires ro mount, checked in f2fs_validate_options */
  725. ctx_set_opt(ctx, F2FS_MOUNT_NORECOVERY);
  726. break;
  727. case Opt_discard:
  728. if (result.negated)
  729. ctx_clear_opt(ctx, F2FS_MOUNT_DISCARD);
  730. else
  731. ctx_set_opt(ctx, F2FS_MOUNT_DISCARD);
  732. break;
  733. case Opt_noheap:
  734. case Opt_heap:
  735. f2fs_warn(NULL, "heap/no_heap options were deprecated");
  736. break;
  737. #ifdef CONFIG_F2FS_FS_XATTR
  738. case Opt_user_xattr:
  739. if (result.negated)
  740. ctx_clear_opt(ctx, F2FS_MOUNT_XATTR_USER);
  741. else
  742. ctx_set_opt(ctx, F2FS_MOUNT_XATTR_USER);
  743. break;
  744. case Opt_inline_xattr:
  745. if (result.negated)
  746. ctx_clear_opt(ctx, F2FS_MOUNT_INLINE_XATTR);
  747. else
  748. ctx_set_opt(ctx, F2FS_MOUNT_INLINE_XATTR);
  749. break;
  750. case Opt_inline_xattr_size:
  751. if (result.int_32 < MIN_INLINE_XATTR_SIZE ||
  752. result.int_32 > MAX_INLINE_XATTR_SIZE) {
  753. f2fs_err(NULL, "inline xattr size is out of range: %u ~ %u",
  754. (u32)MIN_INLINE_XATTR_SIZE, (u32)MAX_INLINE_XATTR_SIZE);
  755. return -EINVAL;
  756. }
  757. ctx_set_opt(ctx, F2FS_MOUNT_INLINE_XATTR_SIZE);
  758. F2FS_CTX_INFO(ctx).inline_xattr_size = result.int_32;
  759. ctx->spec_mask |= F2FS_SPEC_inline_xattr_size;
  760. break;
  761. #else
  762. case Opt_user_xattr:
  763. case Opt_inline_xattr:
  764. case Opt_inline_xattr_size:
  765. f2fs_info(NULL, "%s options not supported", param->key);
  766. break;
  767. #endif
  768. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  769. case Opt_acl:
  770. if (result.negated)
  771. ctx_clear_opt(ctx, F2FS_MOUNT_POSIX_ACL);
  772. else
  773. ctx_set_opt(ctx, F2FS_MOUNT_POSIX_ACL);
  774. break;
  775. #else
  776. case Opt_acl:
  777. f2fs_info(NULL, "%s options not supported", param->key);
  778. break;
  779. #endif
  780. case Opt_active_logs:
  781. if (result.int_32 != 2 && result.int_32 != 4 &&
  782. result.int_32 != NR_CURSEG_PERSIST_TYPE)
  783. return -EINVAL;
  784. ctx->spec_mask |= F2FS_SPEC_active_logs;
  785. F2FS_CTX_INFO(ctx).active_logs = result.int_32;
  786. break;
  787. case Opt_disable_ext_identify:
  788. ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_EXT_IDENTIFY);
  789. break;
  790. case Opt_inline_data:
  791. if (result.negated)
  792. ctx_clear_opt(ctx, F2FS_MOUNT_INLINE_DATA);
  793. else
  794. ctx_set_opt(ctx, F2FS_MOUNT_INLINE_DATA);
  795. break;
  796. case Opt_inline_dentry:
  797. if (result.negated)
  798. ctx_clear_opt(ctx, F2FS_MOUNT_INLINE_DENTRY);
  799. else
  800. ctx_set_opt(ctx, F2FS_MOUNT_INLINE_DENTRY);
  801. break;
  802. case Opt_flush_merge:
  803. if (result.negated)
  804. ctx_clear_opt(ctx, F2FS_MOUNT_FLUSH_MERGE);
  805. else
  806. ctx_set_opt(ctx, F2FS_MOUNT_FLUSH_MERGE);
  807. break;
  808. case Opt_barrier:
  809. if (result.negated)
  810. ctx_set_opt(ctx, F2FS_MOUNT_NOBARRIER);
  811. else
  812. ctx_clear_opt(ctx, F2FS_MOUNT_NOBARRIER);
  813. break;
  814. case Opt_fastboot:
  815. ctx_set_opt(ctx, F2FS_MOUNT_FASTBOOT);
  816. break;
  817. case Opt_extent_cache:
  818. if (result.negated)
  819. ctx_clear_opt(ctx, F2FS_MOUNT_READ_EXTENT_CACHE);
  820. else
  821. ctx_set_opt(ctx, F2FS_MOUNT_READ_EXTENT_CACHE);
  822. break;
  823. case Opt_data_flush:
  824. ctx_set_opt(ctx, F2FS_MOUNT_DATA_FLUSH);
  825. break;
  826. case Opt_reserve_root:
  827. ctx_set_opt(ctx, F2FS_MOUNT_RESERVE_ROOT);
  828. F2FS_CTX_INFO(ctx).root_reserved_blocks = result.uint_32;
  829. ctx->spec_mask |= F2FS_SPEC_reserve_root;
  830. break;
  831. case Opt_reserve_node:
  832. ctx_set_opt(ctx, F2FS_MOUNT_RESERVE_NODE);
  833. F2FS_CTX_INFO(ctx).root_reserved_nodes = result.uint_32;
  834. ctx->spec_mask |= F2FS_SPEC_reserve_node;
  835. break;
  836. case Opt_resuid:
  837. F2FS_CTX_INFO(ctx).s_resuid = result.uid;
  838. ctx->spec_mask |= F2FS_SPEC_resuid;
  839. break;
  840. case Opt_resgid:
  841. F2FS_CTX_INFO(ctx).s_resgid = result.gid;
  842. ctx->spec_mask |= F2FS_SPEC_resgid;
  843. break;
  844. case Opt_mode:
  845. F2FS_CTX_INFO(ctx).fs_mode = result.uint_32;
  846. ctx->spec_mask |= F2FS_SPEC_mode;
  847. break;
  848. #ifdef CONFIG_F2FS_FAULT_INJECTION
  849. case Opt_fault_injection:
  850. F2FS_CTX_INFO(ctx).fault_info.inject_rate = result.int_32;
  851. ctx->spec_mask |= F2FS_SPEC_fault_injection;
  852. ctx_set_opt(ctx, F2FS_MOUNT_FAULT_INJECTION);
  853. break;
  854. case Opt_fault_type:
  855. if (result.uint_32 > BIT(FAULT_MAX))
  856. return -EINVAL;
  857. F2FS_CTX_INFO(ctx).fault_info.inject_type = result.uint_32;
  858. ctx->spec_mask |= F2FS_SPEC_fault_type;
  859. ctx_set_opt(ctx, F2FS_MOUNT_FAULT_INJECTION);
  860. break;
  861. #else
  862. case Opt_fault_injection:
  863. case Opt_fault_type:
  864. f2fs_info(NULL, "%s options not supported", param->key);
  865. break;
  866. #endif
  867. case Opt_lazytime:
  868. if (result.negated)
  869. ctx_clear_opt(ctx, F2FS_MOUNT_LAZYTIME);
  870. else
  871. ctx_set_opt(ctx, F2FS_MOUNT_LAZYTIME);
  872. break;
  873. #ifdef CONFIG_QUOTA
  874. case Opt_quota:
  875. if (result.negated) {
  876. ctx_clear_opt(ctx, F2FS_MOUNT_QUOTA);
  877. ctx_clear_opt(ctx, F2FS_MOUNT_USRQUOTA);
  878. ctx_clear_opt(ctx, F2FS_MOUNT_GRPQUOTA);
  879. ctx_clear_opt(ctx, F2FS_MOUNT_PRJQUOTA);
  880. } else
  881. ctx_set_opt(ctx, F2FS_MOUNT_USRQUOTA);
  882. break;
  883. case Opt_usrquota:
  884. ctx_set_opt(ctx, F2FS_MOUNT_USRQUOTA);
  885. break;
  886. case Opt_grpquota:
  887. ctx_set_opt(ctx, F2FS_MOUNT_GRPQUOTA);
  888. break;
  889. case Opt_prjquota:
  890. ctx_set_opt(ctx, F2FS_MOUNT_PRJQUOTA);
  891. break;
  892. case Opt_usrjquota:
  893. if (!*param->string)
  894. ret = f2fs_unnote_qf_name(fc, USRQUOTA);
  895. else
  896. ret = f2fs_note_qf_name(fc, USRQUOTA, param);
  897. if (ret)
  898. return ret;
  899. break;
  900. case Opt_grpjquota:
  901. if (!*param->string)
  902. ret = f2fs_unnote_qf_name(fc, GRPQUOTA);
  903. else
  904. ret = f2fs_note_qf_name(fc, GRPQUOTA, param);
  905. if (ret)
  906. return ret;
  907. break;
  908. case Opt_prjjquota:
  909. if (!*param->string)
  910. ret = f2fs_unnote_qf_name(fc, PRJQUOTA);
  911. else
  912. ret = f2fs_note_qf_name(fc, PRJQUOTA, param);
  913. if (ret)
  914. return ret;
  915. break;
  916. case Opt_jqfmt:
  917. F2FS_CTX_INFO(ctx).s_jquota_fmt = result.int_32;
  918. ctx->spec_mask |= F2FS_SPEC_jqfmt;
  919. break;
  920. #else
  921. case Opt_quota:
  922. case Opt_usrquota:
  923. case Opt_grpquota:
  924. case Opt_prjquota:
  925. case Opt_usrjquota:
  926. case Opt_grpjquota:
  927. case Opt_prjjquota:
  928. f2fs_info(NULL, "quota operations not supported");
  929. break;
  930. #endif
  931. case Opt_alloc:
  932. F2FS_CTX_INFO(ctx).alloc_mode = result.uint_32;
  933. ctx->spec_mask |= F2FS_SPEC_alloc_mode;
  934. break;
  935. case Opt_fsync:
  936. F2FS_CTX_INFO(ctx).fsync_mode = result.uint_32;
  937. ctx->spec_mask |= F2FS_SPEC_fsync_mode;
  938. break;
  939. case Opt_test_dummy_encryption:
  940. ret = f2fs_parse_test_dummy_encryption(param, ctx);
  941. if (ret)
  942. return ret;
  943. break;
  944. case Opt_inlinecrypt:
  945. #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
  946. ctx_set_opt(ctx, F2FS_MOUNT_INLINECRYPT);
  947. #else
  948. f2fs_info(NULL, "inline encryption not supported");
  949. #endif
  950. break;
  951. case Opt_checkpoint:
  952. /*
  953. * Initialize args struct so we know whether arg was
  954. * found; some options take optional arguments.
  955. */
  956. args[0].from = args[0].to = NULL;
  957. arg = 0;
  958. /* revert to match_table for checkpoint= options */
  959. token = match_token(param->string, f2fs_checkpoint_tokens, args);
  960. switch (token) {
  961. case Opt_checkpoint_disable_cap_perc:
  962. if (args->from && match_int(args, &arg))
  963. return -EINVAL;
  964. if (arg < 0 || arg > 100)
  965. return -EINVAL;
  966. F2FS_CTX_INFO(ctx).unusable_cap_perc = arg;
  967. ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap_perc;
  968. ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT);
  969. break;
  970. case Opt_checkpoint_disable_cap:
  971. if (args->from && match_int(args, &arg))
  972. return -EINVAL;
  973. F2FS_CTX_INFO(ctx).unusable_cap = arg;
  974. ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap;
  975. ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT);
  976. break;
  977. case Opt_checkpoint_disable:
  978. ctx_set_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT);
  979. break;
  980. case Opt_checkpoint_enable:
  981. F2FS_CTX_INFO(ctx).unusable_cap_perc = 0;
  982. ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap_perc;
  983. F2FS_CTX_INFO(ctx).unusable_cap = 0;
  984. ctx->spec_mask |= F2FS_SPEC_checkpoint_disable_cap;
  985. ctx_clear_opt(ctx, F2FS_MOUNT_DISABLE_CHECKPOINT);
  986. break;
  987. default:
  988. return -EINVAL;
  989. }
  990. break;
  991. case Opt_checkpoint_merge:
  992. if (result.negated)
  993. ctx_clear_opt(ctx, F2FS_MOUNT_MERGE_CHECKPOINT);
  994. else
  995. ctx_set_opt(ctx, F2FS_MOUNT_MERGE_CHECKPOINT);
  996. break;
  997. #ifdef CONFIG_F2FS_FS_COMPRESSION
  998. case Opt_compress_algorithm:
  999. name = param->string;
  1000. if (!strcmp(name, "lzo")) {
  1001. #ifdef CONFIG_F2FS_FS_LZO
  1002. F2FS_CTX_INFO(ctx).compress_level = 0;
  1003. F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_LZO;
  1004. ctx->spec_mask |= F2FS_SPEC_compress_level;
  1005. ctx->spec_mask |= F2FS_SPEC_compress_algorithm;
  1006. #else
  1007. f2fs_info(NULL, "kernel doesn't support lzo compression");
  1008. #endif
  1009. } else if (!strncmp(name, "lz4", 3)) {
  1010. #ifdef CONFIG_F2FS_FS_LZ4
  1011. ret = f2fs_set_lz4hc_level(ctx, name);
  1012. if (ret)
  1013. return -EINVAL;
  1014. F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_LZ4;
  1015. ctx->spec_mask |= F2FS_SPEC_compress_algorithm;
  1016. #else
  1017. f2fs_info(NULL, "kernel doesn't support lz4 compression");
  1018. #endif
  1019. } else if (!strncmp(name, "zstd", 4)) {
  1020. #ifdef CONFIG_F2FS_FS_ZSTD
  1021. ret = f2fs_set_zstd_level(ctx, name);
  1022. if (ret)
  1023. return -EINVAL;
  1024. F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_ZSTD;
  1025. ctx->spec_mask |= F2FS_SPEC_compress_algorithm;
  1026. #else
  1027. f2fs_info(NULL, "kernel doesn't support zstd compression");
  1028. #endif
  1029. } else if (!strcmp(name, "lzo-rle")) {
  1030. #ifdef CONFIG_F2FS_FS_LZORLE
  1031. F2FS_CTX_INFO(ctx).compress_level = 0;
  1032. F2FS_CTX_INFO(ctx).compress_algorithm = COMPRESS_LZORLE;
  1033. ctx->spec_mask |= F2FS_SPEC_compress_level;
  1034. ctx->spec_mask |= F2FS_SPEC_compress_algorithm;
  1035. #else
  1036. f2fs_info(NULL, "kernel doesn't support lzorle compression");
  1037. #endif
  1038. } else
  1039. return -EINVAL;
  1040. break;
  1041. case Opt_compress_log_size:
  1042. if (result.uint_32 < MIN_COMPRESS_LOG_SIZE ||
  1043. result.uint_32 > MAX_COMPRESS_LOG_SIZE) {
  1044. f2fs_err(NULL,
  1045. "Compress cluster log size is out of range");
  1046. return -EINVAL;
  1047. }
  1048. F2FS_CTX_INFO(ctx).compress_log_size = result.uint_32;
  1049. ctx->spec_mask |= F2FS_SPEC_compress_log_size;
  1050. break;
  1051. case Opt_compress_extension:
  1052. name = param->string;
  1053. ext = F2FS_CTX_INFO(ctx).extensions;
  1054. ext_cnt = F2FS_CTX_INFO(ctx).compress_ext_cnt;
  1055. if (strlen(name) >= F2FS_EXTENSION_LEN ||
  1056. ext_cnt >= COMPRESS_EXT_NUM) {
  1057. f2fs_err(NULL, "invalid extension length/number");
  1058. return -EINVAL;
  1059. }
  1060. if (is_compress_extension_exist(&ctx->info, name, true))
  1061. break;
  1062. ret = strscpy(ext[ext_cnt], name, F2FS_EXTENSION_LEN);
  1063. if (ret < 0)
  1064. return ret;
  1065. F2FS_CTX_INFO(ctx).compress_ext_cnt++;
  1066. ctx->spec_mask |= F2FS_SPEC_compress_extension;
  1067. break;
  1068. case Opt_nocompress_extension:
  1069. name = param->string;
  1070. noext = F2FS_CTX_INFO(ctx).noextensions;
  1071. noext_cnt = F2FS_CTX_INFO(ctx).nocompress_ext_cnt;
  1072. if (strlen(name) >= F2FS_EXTENSION_LEN ||
  1073. noext_cnt >= COMPRESS_EXT_NUM) {
  1074. f2fs_err(NULL, "invalid extension length/number");
  1075. return -EINVAL;
  1076. }
  1077. if (is_compress_extension_exist(&ctx->info, name, false))
  1078. break;
  1079. ret = strscpy(noext[noext_cnt], name, F2FS_EXTENSION_LEN);
  1080. if (ret < 0)
  1081. return ret;
  1082. F2FS_CTX_INFO(ctx).nocompress_ext_cnt++;
  1083. ctx->spec_mask |= F2FS_SPEC_nocompress_extension;
  1084. break;
  1085. case Opt_compress_chksum:
  1086. F2FS_CTX_INFO(ctx).compress_chksum = true;
  1087. ctx->spec_mask |= F2FS_SPEC_compress_chksum;
  1088. break;
  1089. case Opt_compress_mode:
  1090. F2FS_CTX_INFO(ctx).compress_mode = result.uint_32;
  1091. ctx->spec_mask |= F2FS_SPEC_compress_mode;
  1092. break;
  1093. case Opt_compress_cache:
  1094. ctx_set_opt(ctx, F2FS_MOUNT_COMPRESS_CACHE);
  1095. break;
  1096. #else
  1097. case Opt_compress_algorithm:
  1098. case Opt_compress_log_size:
  1099. case Opt_compress_extension:
  1100. case Opt_nocompress_extension:
  1101. case Opt_compress_chksum:
  1102. case Opt_compress_mode:
  1103. case Opt_compress_cache:
  1104. f2fs_info(NULL, "compression options not supported");
  1105. break;
  1106. #endif
  1107. case Opt_atgc:
  1108. ctx_set_opt(ctx, F2FS_MOUNT_ATGC);
  1109. break;
  1110. case Opt_gc_merge:
  1111. if (result.negated)
  1112. ctx_clear_opt(ctx, F2FS_MOUNT_GC_MERGE);
  1113. else
  1114. ctx_set_opt(ctx, F2FS_MOUNT_GC_MERGE);
  1115. break;
  1116. case Opt_discard_unit:
  1117. F2FS_CTX_INFO(ctx).discard_unit = result.uint_32;
  1118. ctx->spec_mask |= F2FS_SPEC_discard_unit;
  1119. break;
  1120. case Opt_memory_mode:
  1121. F2FS_CTX_INFO(ctx).memory_mode = result.uint_32;
  1122. ctx->spec_mask |= F2FS_SPEC_memory_mode;
  1123. break;
  1124. case Opt_age_extent_cache:
  1125. ctx_set_opt(ctx, F2FS_MOUNT_AGE_EXTENT_CACHE);
  1126. break;
  1127. case Opt_errors:
  1128. F2FS_CTX_INFO(ctx).errors = result.uint_32;
  1129. ctx->spec_mask |= F2FS_SPEC_errors;
  1130. break;
  1131. case Opt_nat_bits:
  1132. ctx_set_opt(ctx, F2FS_MOUNT_NAT_BITS);
  1133. break;
  1134. case Opt_lookup_mode:
  1135. F2FS_CTX_INFO(ctx).lookup_mode = result.uint_32;
  1136. ctx->spec_mask |= F2FS_SPEC_lookup_mode;
  1137. break;
  1138. }
  1139. return 0;
  1140. }
  1141. /*
  1142. * Check quota settings consistency.
  1143. */
  1144. static int f2fs_check_quota_consistency(struct fs_context *fc,
  1145. struct super_block *sb)
  1146. {
  1147. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1148. #ifdef CONFIG_QUOTA
  1149. struct f2fs_fs_context *ctx = fc->fs_private;
  1150. bool quota_feature = f2fs_sb_has_quota_ino(sbi);
  1151. bool quota_turnon = sb_any_quota_loaded(sb);
  1152. char *old_qname, *new_qname;
  1153. bool usr_qf_name, grp_qf_name, prj_qf_name, usrquota, grpquota, prjquota;
  1154. int i;
  1155. /*
  1156. * We do the test below only for project quotas. 'usrquota' and
  1157. * 'grpquota' mount options are allowed even without quota feature
  1158. * to support legacy quotas in quota files.
  1159. */
  1160. if (ctx_test_opt(ctx, F2FS_MOUNT_PRJQUOTA) &&
  1161. !f2fs_sb_has_project_quota(sbi)) {
  1162. f2fs_err(sbi, "Project quota feature not enabled. Cannot enable project quota enforcement.");
  1163. return -EINVAL;
  1164. }
  1165. if (ctx->qname_mask) {
  1166. for (i = 0; i < MAXQUOTAS; i++) {
  1167. if (!(ctx->qname_mask & (1 << i)))
  1168. continue;
  1169. old_qname = F2FS_OPTION(sbi).s_qf_names[i];
  1170. new_qname = F2FS_CTX_INFO(ctx).s_qf_names[i];
  1171. if (quota_turnon &&
  1172. !!old_qname != !!new_qname)
  1173. goto err_jquota_change;
  1174. if (old_qname) {
  1175. if (!new_qname) {
  1176. f2fs_info(sbi, "remove qf_name %s",
  1177. old_qname);
  1178. continue;
  1179. } else if (strcmp(old_qname, new_qname) == 0) {
  1180. ctx->qname_mask &= ~(1 << i);
  1181. continue;
  1182. }
  1183. goto err_jquota_specified;
  1184. }
  1185. if (quota_feature) {
  1186. f2fs_info(sbi, "QUOTA feature is enabled, so ignore qf_name");
  1187. ctx->qname_mask &= ~(1 << i);
  1188. kfree(F2FS_CTX_INFO(ctx).s_qf_names[i]);
  1189. F2FS_CTX_INFO(ctx).s_qf_names[i] = NULL;
  1190. }
  1191. }
  1192. }
  1193. /* Make sure we don't mix old and new quota format */
  1194. usr_qf_name = F2FS_OPTION(sbi).s_qf_names[USRQUOTA] ||
  1195. F2FS_CTX_INFO(ctx).s_qf_names[USRQUOTA];
  1196. grp_qf_name = F2FS_OPTION(sbi).s_qf_names[GRPQUOTA] ||
  1197. F2FS_CTX_INFO(ctx).s_qf_names[GRPQUOTA];
  1198. prj_qf_name = F2FS_OPTION(sbi).s_qf_names[PRJQUOTA] ||
  1199. F2FS_CTX_INFO(ctx).s_qf_names[PRJQUOTA];
  1200. usrquota = test_opt(sbi, USRQUOTA) ||
  1201. ctx_test_opt(ctx, F2FS_MOUNT_USRQUOTA);
  1202. grpquota = test_opt(sbi, GRPQUOTA) ||
  1203. ctx_test_opt(ctx, F2FS_MOUNT_GRPQUOTA);
  1204. prjquota = test_opt(sbi, PRJQUOTA) ||
  1205. ctx_test_opt(ctx, F2FS_MOUNT_PRJQUOTA);
  1206. if (usr_qf_name) {
  1207. ctx_clear_opt(ctx, F2FS_MOUNT_USRQUOTA);
  1208. usrquota = false;
  1209. }
  1210. if (grp_qf_name) {
  1211. ctx_clear_opt(ctx, F2FS_MOUNT_GRPQUOTA);
  1212. grpquota = false;
  1213. }
  1214. if (prj_qf_name) {
  1215. ctx_clear_opt(ctx, F2FS_MOUNT_PRJQUOTA);
  1216. prjquota = false;
  1217. }
  1218. if (usr_qf_name || grp_qf_name || prj_qf_name) {
  1219. if (grpquota || usrquota || prjquota) {
  1220. f2fs_err(sbi, "old and new quota format mixing");
  1221. return -EINVAL;
  1222. }
  1223. if (!(ctx->spec_mask & F2FS_SPEC_jqfmt ||
  1224. F2FS_OPTION(sbi).s_jquota_fmt)) {
  1225. f2fs_err(sbi, "journaled quota format not specified");
  1226. return -EINVAL;
  1227. }
  1228. }
  1229. return 0;
  1230. err_jquota_change:
  1231. f2fs_err(sbi, "Cannot change journaled quota options when quota turned on");
  1232. return -EINVAL;
  1233. err_jquota_specified:
  1234. f2fs_err(sbi, "%s quota file already specified",
  1235. QTYPE2NAME(i));
  1236. return -EINVAL;
  1237. #else
  1238. if (f2fs_readonly(sbi->sb))
  1239. return 0;
  1240. if (f2fs_sb_has_quota_ino(sbi)) {
  1241. f2fs_info(sbi, "Filesystem with quota feature cannot be mounted RDWR without CONFIG_QUOTA");
  1242. return -EINVAL;
  1243. }
  1244. if (f2fs_sb_has_project_quota(sbi)) {
  1245. f2fs_err(sbi, "Filesystem with project quota feature cannot be mounted RDWR without CONFIG_QUOTA");
  1246. return -EINVAL;
  1247. }
  1248. return 0;
  1249. #endif
  1250. }
  1251. static int f2fs_check_test_dummy_encryption(struct fs_context *fc,
  1252. struct super_block *sb)
  1253. {
  1254. struct f2fs_fs_context *ctx = fc->fs_private;
  1255. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1256. if (!fscrypt_is_dummy_policy_set(&F2FS_CTX_INFO(ctx).dummy_enc_policy))
  1257. return 0;
  1258. if (!f2fs_sb_has_encrypt(sbi)) {
  1259. f2fs_err(sbi, "Encrypt feature is off");
  1260. return -EINVAL;
  1261. }
  1262. /*
  1263. * This mount option is just for testing, and it's not worthwhile to
  1264. * implement the extra complexity (e.g. RCU protection) that would be
  1265. * needed to allow it to be set or changed during remount. We do allow
  1266. * it to be specified during remount, but only if there is no change.
  1267. */
  1268. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  1269. if (fscrypt_dummy_policies_equal(&F2FS_OPTION(sbi).dummy_enc_policy,
  1270. &F2FS_CTX_INFO(ctx).dummy_enc_policy))
  1271. return 0;
  1272. f2fs_warn(sbi, "Can't set or change test_dummy_encryption on remount");
  1273. return -EINVAL;
  1274. }
  1275. return 0;
  1276. }
  1277. static inline bool test_compression_spec(unsigned int mask)
  1278. {
  1279. return mask & (F2FS_SPEC_compress_algorithm
  1280. | F2FS_SPEC_compress_log_size
  1281. | F2FS_SPEC_compress_extension
  1282. | F2FS_SPEC_nocompress_extension
  1283. | F2FS_SPEC_compress_chksum
  1284. | F2FS_SPEC_compress_mode);
  1285. }
  1286. static inline void clear_compression_spec(struct f2fs_fs_context *ctx)
  1287. {
  1288. ctx->spec_mask &= ~(F2FS_SPEC_compress_algorithm
  1289. | F2FS_SPEC_compress_log_size
  1290. | F2FS_SPEC_compress_extension
  1291. | F2FS_SPEC_nocompress_extension
  1292. | F2FS_SPEC_compress_chksum
  1293. | F2FS_SPEC_compress_mode);
  1294. }
  1295. static int f2fs_check_compression(struct fs_context *fc,
  1296. struct super_block *sb)
  1297. {
  1298. #ifdef CONFIG_F2FS_FS_COMPRESSION
  1299. struct f2fs_fs_context *ctx = fc->fs_private;
  1300. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1301. int i, cnt;
  1302. if (!f2fs_sb_has_compression(sbi)) {
  1303. if (test_compression_spec(ctx->spec_mask) ||
  1304. ctx_test_opt(ctx, F2FS_MOUNT_COMPRESS_CACHE))
  1305. f2fs_info(sbi, "Image doesn't support compression");
  1306. clear_compression_spec(ctx);
  1307. ctx->opt_mask &= ~BIT(F2FS_MOUNT_COMPRESS_CACHE);
  1308. return 0;
  1309. }
  1310. if (ctx->spec_mask & F2FS_SPEC_compress_extension) {
  1311. cnt = F2FS_CTX_INFO(ctx).compress_ext_cnt;
  1312. for (i = 0; i < F2FS_CTX_INFO(ctx).compress_ext_cnt; i++) {
  1313. if (is_compress_extension_exist(&F2FS_OPTION(sbi),
  1314. F2FS_CTX_INFO(ctx).extensions[i], true)) {
  1315. F2FS_CTX_INFO(ctx).extensions[i][0] = '\0';
  1316. cnt--;
  1317. }
  1318. }
  1319. if (F2FS_OPTION(sbi).compress_ext_cnt + cnt > COMPRESS_EXT_NUM) {
  1320. f2fs_err(sbi, "invalid extension length/number");
  1321. return -EINVAL;
  1322. }
  1323. }
  1324. if (ctx->spec_mask & F2FS_SPEC_nocompress_extension) {
  1325. cnt = F2FS_CTX_INFO(ctx).nocompress_ext_cnt;
  1326. for (i = 0; i < F2FS_CTX_INFO(ctx).nocompress_ext_cnt; i++) {
  1327. if (is_compress_extension_exist(&F2FS_OPTION(sbi),
  1328. F2FS_CTX_INFO(ctx).noextensions[i], false)) {
  1329. F2FS_CTX_INFO(ctx).noextensions[i][0] = '\0';
  1330. cnt--;
  1331. }
  1332. }
  1333. if (F2FS_OPTION(sbi).nocompress_ext_cnt + cnt > COMPRESS_EXT_NUM) {
  1334. f2fs_err(sbi, "invalid noextension length/number");
  1335. return -EINVAL;
  1336. }
  1337. }
  1338. if (f2fs_test_compress_extension(F2FS_CTX_INFO(ctx).noextensions,
  1339. F2FS_CTX_INFO(ctx).nocompress_ext_cnt,
  1340. F2FS_CTX_INFO(ctx).extensions,
  1341. F2FS_CTX_INFO(ctx).compress_ext_cnt)) {
  1342. f2fs_err(sbi, "new noextensions conflicts with new extensions");
  1343. return -EINVAL;
  1344. }
  1345. if (f2fs_test_compress_extension(F2FS_CTX_INFO(ctx).noextensions,
  1346. F2FS_CTX_INFO(ctx).nocompress_ext_cnt,
  1347. F2FS_OPTION(sbi).extensions,
  1348. F2FS_OPTION(sbi).compress_ext_cnt)) {
  1349. f2fs_err(sbi, "new noextensions conflicts with old extensions");
  1350. return -EINVAL;
  1351. }
  1352. if (f2fs_test_compress_extension(F2FS_OPTION(sbi).noextensions,
  1353. F2FS_OPTION(sbi).nocompress_ext_cnt,
  1354. F2FS_CTX_INFO(ctx).extensions,
  1355. F2FS_CTX_INFO(ctx).compress_ext_cnt)) {
  1356. f2fs_err(sbi, "new extensions conflicts with old noextensions");
  1357. return -EINVAL;
  1358. }
  1359. #endif
  1360. return 0;
  1361. }
  1362. static int f2fs_check_opt_consistency(struct fs_context *fc,
  1363. struct super_block *sb)
  1364. {
  1365. struct f2fs_fs_context *ctx = fc->fs_private;
  1366. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1367. int err;
  1368. if (ctx_test_opt(ctx, F2FS_MOUNT_NORECOVERY) && !f2fs_readonly(sb))
  1369. return -EINVAL;
  1370. if (f2fs_hw_should_discard(sbi) &&
  1371. (ctx->opt_mask & BIT(F2FS_MOUNT_DISCARD)) &&
  1372. !ctx_test_opt(ctx, F2FS_MOUNT_DISCARD)) {
  1373. f2fs_warn(sbi, "discard is required for zoned block devices");
  1374. return -EINVAL;
  1375. }
  1376. if (!f2fs_hw_support_discard(sbi) &&
  1377. (ctx->opt_mask & BIT(F2FS_MOUNT_DISCARD)) &&
  1378. ctx_test_opt(ctx, F2FS_MOUNT_DISCARD)) {
  1379. f2fs_warn(sbi, "device does not support discard");
  1380. ctx_clear_opt(ctx, F2FS_MOUNT_DISCARD);
  1381. ctx->opt_mask &= ~BIT(F2FS_MOUNT_DISCARD);
  1382. }
  1383. if (f2fs_sb_has_device_alias(sbi) &&
  1384. (ctx->opt_mask & BIT(F2FS_MOUNT_READ_EXTENT_CACHE)) &&
  1385. !ctx_test_opt(ctx, F2FS_MOUNT_READ_EXTENT_CACHE)) {
  1386. f2fs_err(sbi, "device aliasing requires extent cache");
  1387. return -EINVAL;
  1388. }
  1389. if (test_opt(sbi, RESERVE_ROOT) &&
  1390. (ctx->opt_mask & BIT(F2FS_MOUNT_RESERVE_ROOT)) &&
  1391. ctx_test_opt(ctx, F2FS_MOUNT_RESERVE_ROOT)) {
  1392. f2fs_info(sbi, "Preserve previous reserve_root=%u",
  1393. F2FS_OPTION(sbi).root_reserved_blocks);
  1394. ctx_clear_opt(ctx, F2FS_MOUNT_RESERVE_ROOT);
  1395. ctx->opt_mask &= ~BIT(F2FS_MOUNT_RESERVE_ROOT);
  1396. }
  1397. if (test_opt(sbi, RESERVE_NODE) &&
  1398. (ctx->opt_mask & BIT(F2FS_MOUNT_RESERVE_NODE)) &&
  1399. ctx_test_opt(ctx, F2FS_MOUNT_RESERVE_NODE)) {
  1400. f2fs_info(sbi, "Preserve previous reserve_node=%u",
  1401. F2FS_OPTION(sbi).root_reserved_nodes);
  1402. ctx_clear_opt(ctx, F2FS_MOUNT_RESERVE_NODE);
  1403. ctx->opt_mask &= ~BIT(F2FS_MOUNT_RESERVE_NODE);
  1404. }
  1405. err = f2fs_check_test_dummy_encryption(fc, sb);
  1406. if (err)
  1407. return err;
  1408. err = f2fs_check_compression(fc, sb);
  1409. if (err)
  1410. return err;
  1411. err = f2fs_check_quota_consistency(fc, sb);
  1412. if (err)
  1413. return err;
  1414. if (!IS_ENABLED(CONFIG_UNICODE) && f2fs_sb_has_casefold(sbi)) {
  1415. f2fs_err(sbi,
  1416. "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
  1417. return -EINVAL;
  1418. }
  1419. /*
  1420. * The BLKZONED feature indicates that the drive was formatted with
  1421. * zone alignment optimization. This is optional for host-aware
  1422. * devices, but mandatory for host-managed zoned block devices.
  1423. */
  1424. if (f2fs_sb_has_blkzoned(sbi)) {
  1425. if (F2FS_CTX_INFO(ctx).bggc_mode == BGGC_MODE_OFF) {
  1426. f2fs_warn(sbi, "zoned devices need bggc");
  1427. return -EINVAL;
  1428. }
  1429. #ifdef CONFIG_BLK_DEV_ZONED
  1430. if ((ctx->spec_mask & F2FS_SPEC_discard_unit) &&
  1431. F2FS_CTX_INFO(ctx).discard_unit != DISCARD_UNIT_SECTION) {
  1432. f2fs_info(sbi, "Zoned block device doesn't need small discard, set discard_unit=section by default");
  1433. F2FS_CTX_INFO(ctx).discard_unit = DISCARD_UNIT_SECTION;
  1434. }
  1435. if ((ctx->spec_mask & F2FS_SPEC_mode) &&
  1436. F2FS_CTX_INFO(ctx).fs_mode != FS_MODE_LFS) {
  1437. f2fs_info(sbi, "Only lfs mode is allowed with zoned block device feature");
  1438. return -EINVAL;
  1439. }
  1440. #else
  1441. f2fs_err(sbi, "Zoned block device support is not enabled");
  1442. return -EINVAL;
  1443. #endif
  1444. }
  1445. if (ctx_test_opt(ctx, F2FS_MOUNT_INLINE_XATTR_SIZE)) {
  1446. if (!f2fs_sb_has_extra_attr(sbi) ||
  1447. !f2fs_sb_has_flexible_inline_xattr(sbi)) {
  1448. f2fs_err(sbi, "extra_attr or flexible_inline_xattr feature is off");
  1449. return -EINVAL;
  1450. }
  1451. if (!ctx_test_opt(ctx, F2FS_MOUNT_INLINE_XATTR) && !test_opt(sbi, INLINE_XATTR)) {
  1452. f2fs_err(sbi, "inline_xattr_size option should be set with inline_xattr option");
  1453. return -EINVAL;
  1454. }
  1455. }
  1456. if (ctx_test_opt(ctx, F2FS_MOUNT_ATGC) &&
  1457. F2FS_CTX_INFO(ctx).fs_mode == FS_MODE_LFS) {
  1458. f2fs_err(sbi, "LFS is not compatible with ATGC");
  1459. return -EINVAL;
  1460. }
  1461. if (f2fs_is_readonly(sbi) && ctx_test_opt(ctx, F2FS_MOUNT_FLUSH_MERGE)) {
  1462. f2fs_err(sbi, "FLUSH_MERGE not compatible with readonly mode");
  1463. return -EINVAL;
  1464. }
  1465. if (f2fs_sb_has_readonly(sbi) && !f2fs_readonly(sbi->sb)) {
  1466. f2fs_err(sbi, "Allow to mount readonly mode only");
  1467. return -EROFS;
  1468. }
  1469. return 0;
  1470. }
  1471. static void f2fs_apply_quota_options(struct fs_context *fc,
  1472. struct super_block *sb)
  1473. {
  1474. #ifdef CONFIG_QUOTA
  1475. struct f2fs_fs_context *ctx = fc->fs_private;
  1476. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1477. bool quota_feature = f2fs_sb_has_quota_ino(sbi);
  1478. char *qname;
  1479. int i;
  1480. if (quota_feature)
  1481. return;
  1482. for (i = 0; i < MAXQUOTAS; i++) {
  1483. if (!(ctx->qname_mask & (1 << i)))
  1484. continue;
  1485. qname = F2FS_CTX_INFO(ctx).s_qf_names[i];
  1486. if (qname) {
  1487. qname = kstrdup(F2FS_CTX_INFO(ctx).s_qf_names[i],
  1488. GFP_KERNEL | __GFP_NOFAIL);
  1489. set_opt(sbi, QUOTA);
  1490. }
  1491. F2FS_OPTION(sbi).s_qf_names[i] = qname;
  1492. }
  1493. if (ctx->spec_mask & F2FS_SPEC_jqfmt)
  1494. F2FS_OPTION(sbi).s_jquota_fmt = F2FS_CTX_INFO(ctx).s_jquota_fmt;
  1495. if (quota_feature && F2FS_OPTION(sbi).s_jquota_fmt) {
  1496. f2fs_info(sbi, "QUOTA feature is enabled, so ignore jquota_fmt");
  1497. F2FS_OPTION(sbi).s_jquota_fmt = 0;
  1498. }
  1499. #endif
  1500. }
  1501. static void f2fs_apply_test_dummy_encryption(struct fs_context *fc,
  1502. struct super_block *sb)
  1503. {
  1504. struct f2fs_fs_context *ctx = fc->fs_private;
  1505. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1506. if (!fscrypt_is_dummy_policy_set(&F2FS_CTX_INFO(ctx).dummy_enc_policy) ||
  1507. /* if already set, it was already verified to be the same */
  1508. fscrypt_is_dummy_policy_set(&F2FS_OPTION(sbi).dummy_enc_policy))
  1509. return;
  1510. swap(F2FS_OPTION(sbi).dummy_enc_policy, F2FS_CTX_INFO(ctx).dummy_enc_policy);
  1511. f2fs_warn(sbi, "Test dummy encryption mode enabled");
  1512. }
  1513. static void f2fs_apply_compression(struct fs_context *fc,
  1514. struct super_block *sb)
  1515. {
  1516. #ifdef CONFIG_F2FS_FS_COMPRESSION
  1517. struct f2fs_fs_context *ctx = fc->fs_private;
  1518. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1519. unsigned char (*ctx_ext)[F2FS_EXTENSION_LEN];
  1520. unsigned char (*sbi_ext)[F2FS_EXTENSION_LEN];
  1521. int ctx_cnt, sbi_cnt, i;
  1522. if (ctx->spec_mask & F2FS_SPEC_compress_level)
  1523. F2FS_OPTION(sbi).compress_level =
  1524. F2FS_CTX_INFO(ctx).compress_level;
  1525. if (ctx->spec_mask & F2FS_SPEC_compress_algorithm)
  1526. F2FS_OPTION(sbi).compress_algorithm =
  1527. F2FS_CTX_INFO(ctx).compress_algorithm;
  1528. if (ctx->spec_mask & F2FS_SPEC_compress_log_size)
  1529. F2FS_OPTION(sbi).compress_log_size =
  1530. F2FS_CTX_INFO(ctx).compress_log_size;
  1531. if (ctx->spec_mask & F2FS_SPEC_compress_chksum)
  1532. F2FS_OPTION(sbi).compress_chksum =
  1533. F2FS_CTX_INFO(ctx).compress_chksum;
  1534. if (ctx->spec_mask & F2FS_SPEC_compress_mode)
  1535. F2FS_OPTION(sbi).compress_mode =
  1536. F2FS_CTX_INFO(ctx).compress_mode;
  1537. if (ctx->spec_mask & F2FS_SPEC_compress_extension) {
  1538. ctx_ext = F2FS_CTX_INFO(ctx).extensions;
  1539. ctx_cnt = F2FS_CTX_INFO(ctx).compress_ext_cnt;
  1540. sbi_ext = F2FS_OPTION(sbi).extensions;
  1541. sbi_cnt = F2FS_OPTION(sbi).compress_ext_cnt;
  1542. for (i = 0; i < ctx_cnt; i++) {
  1543. if (strlen(ctx_ext[i]) == 0)
  1544. continue;
  1545. strscpy(sbi_ext[sbi_cnt], ctx_ext[i]);
  1546. sbi_cnt++;
  1547. }
  1548. F2FS_OPTION(sbi).compress_ext_cnt = sbi_cnt;
  1549. }
  1550. if (ctx->spec_mask & F2FS_SPEC_nocompress_extension) {
  1551. ctx_ext = F2FS_CTX_INFO(ctx).noextensions;
  1552. ctx_cnt = F2FS_CTX_INFO(ctx).nocompress_ext_cnt;
  1553. sbi_ext = F2FS_OPTION(sbi).noextensions;
  1554. sbi_cnt = F2FS_OPTION(sbi).nocompress_ext_cnt;
  1555. for (i = 0; i < ctx_cnt; i++) {
  1556. if (strlen(ctx_ext[i]) == 0)
  1557. continue;
  1558. strscpy(sbi_ext[sbi_cnt], ctx_ext[i]);
  1559. sbi_cnt++;
  1560. }
  1561. F2FS_OPTION(sbi).nocompress_ext_cnt = sbi_cnt;
  1562. }
  1563. #endif
  1564. }
  1565. static void f2fs_apply_options(struct fs_context *fc, struct super_block *sb)
  1566. {
  1567. struct f2fs_fs_context *ctx = fc->fs_private;
  1568. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1569. F2FS_OPTION(sbi).opt &= ~ctx->opt_mask;
  1570. F2FS_OPTION(sbi).opt |= F2FS_CTX_INFO(ctx).opt;
  1571. if (ctx->spec_mask & F2FS_SPEC_background_gc)
  1572. F2FS_OPTION(sbi).bggc_mode = F2FS_CTX_INFO(ctx).bggc_mode;
  1573. if (ctx->spec_mask & F2FS_SPEC_inline_xattr_size)
  1574. F2FS_OPTION(sbi).inline_xattr_size =
  1575. F2FS_CTX_INFO(ctx).inline_xattr_size;
  1576. if (ctx->spec_mask & F2FS_SPEC_active_logs)
  1577. F2FS_OPTION(sbi).active_logs = F2FS_CTX_INFO(ctx).active_logs;
  1578. if (ctx->spec_mask & F2FS_SPEC_reserve_root)
  1579. F2FS_OPTION(sbi).root_reserved_blocks =
  1580. F2FS_CTX_INFO(ctx).root_reserved_blocks;
  1581. if (ctx->spec_mask & F2FS_SPEC_reserve_node)
  1582. F2FS_OPTION(sbi).root_reserved_nodes =
  1583. F2FS_CTX_INFO(ctx).root_reserved_nodes;
  1584. if (ctx->spec_mask & F2FS_SPEC_resgid)
  1585. F2FS_OPTION(sbi).s_resgid = F2FS_CTX_INFO(ctx).s_resgid;
  1586. if (ctx->spec_mask & F2FS_SPEC_resuid)
  1587. F2FS_OPTION(sbi).s_resuid = F2FS_CTX_INFO(ctx).s_resuid;
  1588. if (ctx->spec_mask & F2FS_SPEC_mode)
  1589. F2FS_OPTION(sbi).fs_mode = F2FS_CTX_INFO(ctx).fs_mode;
  1590. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1591. if (ctx->spec_mask & F2FS_SPEC_fault_injection)
  1592. (void)f2fs_build_fault_attr(sbi,
  1593. F2FS_CTX_INFO(ctx).fault_info.inject_rate, 0, FAULT_RATE);
  1594. if (ctx->spec_mask & F2FS_SPEC_fault_type)
  1595. (void)f2fs_build_fault_attr(sbi, 0,
  1596. F2FS_CTX_INFO(ctx).fault_info.inject_type, FAULT_TYPE);
  1597. #endif
  1598. if (ctx->spec_mask & F2FS_SPEC_alloc_mode)
  1599. F2FS_OPTION(sbi).alloc_mode = F2FS_CTX_INFO(ctx).alloc_mode;
  1600. if (ctx->spec_mask & F2FS_SPEC_fsync_mode)
  1601. F2FS_OPTION(sbi).fsync_mode = F2FS_CTX_INFO(ctx).fsync_mode;
  1602. if (ctx->spec_mask & F2FS_SPEC_checkpoint_disable_cap)
  1603. F2FS_OPTION(sbi).unusable_cap = F2FS_CTX_INFO(ctx).unusable_cap;
  1604. if (ctx->spec_mask & F2FS_SPEC_checkpoint_disable_cap_perc)
  1605. F2FS_OPTION(sbi).unusable_cap_perc =
  1606. F2FS_CTX_INFO(ctx).unusable_cap_perc;
  1607. if (ctx->spec_mask & F2FS_SPEC_discard_unit)
  1608. F2FS_OPTION(sbi).discard_unit = F2FS_CTX_INFO(ctx).discard_unit;
  1609. if (ctx->spec_mask & F2FS_SPEC_memory_mode)
  1610. F2FS_OPTION(sbi).memory_mode = F2FS_CTX_INFO(ctx).memory_mode;
  1611. if (ctx->spec_mask & F2FS_SPEC_errors)
  1612. F2FS_OPTION(sbi).errors = F2FS_CTX_INFO(ctx).errors;
  1613. if (ctx->spec_mask & F2FS_SPEC_lookup_mode)
  1614. F2FS_OPTION(sbi).lookup_mode = F2FS_CTX_INFO(ctx).lookup_mode;
  1615. f2fs_apply_compression(fc, sb);
  1616. f2fs_apply_test_dummy_encryption(fc, sb);
  1617. f2fs_apply_quota_options(fc, sb);
  1618. }
  1619. static int f2fs_sanity_check_options(struct f2fs_sb_info *sbi, bool remount)
  1620. {
  1621. if (f2fs_sb_has_device_alias(sbi) &&
  1622. !test_opt(sbi, READ_EXTENT_CACHE)) {
  1623. f2fs_err(sbi, "device aliasing requires extent cache");
  1624. return -EINVAL;
  1625. }
  1626. if (!remount)
  1627. return 0;
  1628. #ifdef CONFIG_BLK_DEV_ZONED
  1629. if (f2fs_sb_has_blkzoned(sbi) &&
  1630. sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
  1631. f2fs_err(sbi,
  1632. "zoned: max open zones %u is too small, need at least %u open zones",
  1633. sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
  1634. return -EINVAL;
  1635. }
  1636. #endif
  1637. if (f2fs_lfs_mode(sbi) && !IS_F2FS_IPU_DISABLE(sbi)) {
  1638. f2fs_warn(sbi, "LFS is not compatible with IPU");
  1639. return -EINVAL;
  1640. }
  1641. return 0;
  1642. }
  1643. static struct inode *f2fs_alloc_inode(struct super_block *sb)
  1644. {
  1645. struct f2fs_inode_info *fi;
  1646. if (time_to_inject(F2FS_SB(sb), FAULT_SLAB_ALLOC))
  1647. return NULL;
  1648. fi = alloc_inode_sb(sb, f2fs_inode_cachep, GFP_F2FS_ZERO);
  1649. if (!fi)
  1650. return NULL;
  1651. init_once((void *) fi);
  1652. /* Initialize f2fs-specific inode info */
  1653. atomic_set(&fi->dirty_pages, 0);
  1654. atomic_set(&fi->i_compr_blocks, 0);
  1655. atomic_set(&fi->open_count, 0);
  1656. atomic_set(&fi->writeback, 0);
  1657. init_f2fs_rwsem(&fi->i_sem);
  1658. spin_lock_init(&fi->i_size_lock);
  1659. INIT_LIST_HEAD(&fi->dirty_list);
  1660. INIT_LIST_HEAD(&fi->gdirty_list);
  1661. INIT_LIST_HEAD(&fi->gdonate_list);
  1662. init_f2fs_rwsem(&fi->i_gc_rwsem[READ]);
  1663. init_f2fs_rwsem(&fi->i_gc_rwsem[WRITE]);
  1664. init_f2fs_rwsem(&fi->i_xattr_sem);
  1665. /* Will be used by directory only */
  1666. fi->i_dir_level = F2FS_SB(sb)->dir_level;
  1667. return &fi->vfs_inode;
  1668. }
  1669. static int f2fs_drop_inode(struct inode *inode)
  1670. {
  1671. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1672. int ret;
  1673. /*
  1674. * during filesystem shutdown, if checkpoint is disabled,
  1675. * drop useless meta/node dirty pages.
  1676. */
  1677. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
  1678. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  1679. inode->i_ino == F2FS_META_INO(sbi)) {
  1680. trace_f2fs_drop_inode(inode, 1);
  1681. return 1;
  1682. }
  1683. }
  1684. /*
  1685. * This is to avoid a deadlock condition like below.
  1686. * writeback_single_inode(inode)
  1687. * - f2fs_write_data_page
  1688. * - f2fs_gc -> iput -> evict
  1689. * - inode_wait_for_writeback(inode)
  1690. */
  1691. if ((!inode_unhashed(inode) && inode_state_read(inode) & I_SYNC)) {
  1692. if (!inode->i_nlink && !is_bad_inode(inode)) {
  1693. /* to avoid evict_inode call simultaneously */
  1694. __iget(inode);
  1695. spin_unlock(&inode->i_lock);
  1696. /* should remain fi->extent_tree for writepage */
  1697. f2fs_destroy_extent_node(inode);
  1698. sb_start_intwrite(inode->i_sb);
  1699. f2fs_i_size_write(inode, 0);
  1700. f2fs_submit_merged_write_cond(F2FS_I_SB(inode),
  1701. inode, NULL, 0, DATA);
  1702. truncate_inode_pages_final(inode->i_mapping);
  1703. if (F2FS_HAS_BLOCKS(inode))
  1704. f2fs_truncate(inode);
  1705. sb_end_intwrite(inode->i_sb);
  1706. spin_lock(&inode->i_lock);
  1707. atomic_dec(&inode->i_count);
  1708. }
  1709. trace_f2fs_drop_inode(inode, 0);
  1710. return 0;
  1711. }
  1712. ret = inode_generic_drop(inode);
  1713. if (!ret)
  1714. ret = fscrypt_drop_inode(inode);
  1715. trace_f2fs_drop_inode(inode, ret);
  1716. return ret;
  1717. }
  1718. int f2fs_inode_dirtied(struct inode *inode, bool sync)
  1719. {
  1720. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1721. int ret = 0;
  1722. spin_lock(&sbi->inode_lock[DIRTY_META]);
  1723. if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  1724. ret = 1;
  1725. } else {
  1726. set_inode_flag(inode, FI_DIRTY_INODE);
  1727. stat_inc_dirty_inode(sbi, DIRTY_META);
  1728. }
  1729. if (sync && list_empty(&F2FS_I(inode)->gdirty_list)) {
  1730. list_add_tail(&F2FS_I(inode)->gdirty_list,
  1731. &sbi->inode_list[DIRTY_META]);
  1732. inc_page_count(sbi, F2FS_DIRTY_IMETA);
  1733. }
  1734. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  1735. /* if atomic write is not committed, set inode w/ atomic dirty */
  1736. if (!ret && f2fs_is_atomic_file(inode) &&
  1737. !is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
  1738. set_inode_flag(inode, FI_ATOMIC_DIRTIED);
  1739. return ret;
  1740. }
  1741. void f2fs_inode_synced(struct inode *inode)
  1742. {
  1743. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1744. spin_lock(&sbi->inode_lock[DIRTY_META]);
  1745. if (!is_inode_flag_set(inode, FI_DIRTY_INODE)) {
  1746. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  1747. return;
  1748. }
  1749. if (!list_empty(&F2FS_I(inode)->gdirty_list)) {
  1750. list_del_init(&F2FS_I(inode)->gdirty_list);
  1751. dec_page_count(sbi, F2FS_DIRTY_IMETA);
  1752. }
  1753. clear_inode_flag(inode, FI_DIRTY_INODE);
  1754. clear_inode_flag(inode, FI_AUTO_RECOVER);
  1755. stat_dec_dirty_inode(F2FS_I_SB(inode), DIRTY_META);
  1756. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  1757. }
  1758. /*
  1759. * f2fs_dirty_inode() is called from __mark_inode_dirty()
  1760. *
  1761. * We should call set_dirty_inode to write the dirty inode through write_inode.
  1762. */
  1763. static void f2fs_dirty_inode(struct inode *inode, int flags)
  1764. {
  1765. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1766. if (inode->i_ino == F2FS_NODE_INO(sbi) ||
  1767. inode->i_ino == F2FS_META_INO(sbi))
  1768. return;
  1769. if (is_inode_flag_set(inode, FI_AUTO_RECOVER))
  1770. clear_inode_flag(inode, FI_AUTO_RECOVER);
  1771. f2fs_inode_dirtied(inode, false);
  1772. }
  1773. static void f2fs_free_inode(struct inode *inode)
  1774. {
  1775. fscrypt_free_inode(inode);
  1776. kmem_cache_free(f2fs_inode_cachep, F2FS_I(inode));
  1777. }
  1778. static void destroy_percpu_info(struct f2fs_sb_info *sbi)
  1779. {
  1780. percpu_counter_destroy(&sbi->total_valid_inode_count);
  1781. percpu_counter_destroy(&sbi->rf_node_block_count);
  1782. percpu_counter_destroy(&sbi->alloc_valid_block_count);
  1783. }
  1784. static void destroy_device_list(struct f2fs_sb_info *sbi)
  1785. {
  1786. int i;
  1787. for (i = 0; i < sbi->s_ndevs; i++) {
  1788. if (i > 0)
  1789. bdev_fput(FDEV(i).bdev_file);
  1790. #ifdef CONFIG_BLK_DEV_ZONED
  1791. kvfree(FDEV(i).blkz_seq);
  1792. #endif
  1793. }
  1794. kvfree(sbi->devs);
  1795. }
  1796. static void f2fs_put_super(struct super_block *sb)
  1797. {
  1798. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1799. int i;
  1800. int err = 0;
  1801. bool done;
  1802. /* unregister procfs/sysfs entries in advance to avoid race case */
  1803. f2fs_unregister_sysfs(sbi);
  1804. f2fs_quota_off_umount(sb);
  1805. /* prevent remaining shrinker jobs */
  1806. mutex_lock(&sbi->umount_mutex);
  1807. /*
  1808. * flush all issued checkpoints and stop checkpoint issue thread.
  1809. * after then, all checkpoints should be done by each process context.
  1810. */
  1811. f2fs_stop_ckpt_thread(sbi);
  1812. /*
  1813. * We don't need to do checkpoint when superblock is clean.
  1814. * But, the previous checkpoint was not done by umount, it needs to do
  1815. * clean checkpoint again.
  1816. */
  1817. if ((is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  1818. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG))) {
  1819. struct cp_control cpc = {
  1820. .reason = CP_UMOUNT,
  1821. };
  1822. stat_inc_cp_call_count(sbi, TOTAL_CALL);
  1823. err = f2fs_write_checkpoint(sbi, &cpc);
  1824. }
  1825. /* be sure to wait for any on-going discard commands */
  1826. done = f2fs_issue_discard_timeout(sbi);
  1827. if (f2fs_realtime_discard_enable(sbi) && !sbi->discard_blks && done) {
  1828. struct cp_control cpc = {
  1829. .reason = CP_UMOUNT | CP_TRIMMED,
  1830. };
  1831. stat_inc_cp_call_count(sbi, TOTAL_CALL);
  1832. err = f2fs_write_checkpoint(sbi, &cpc);
  1833. }
  1834. /*
  1835. * normally superblock is clean, so we need to release this.
  1836. * In addition, EIO will skip do checkpoint, we need this as well.
  1837. */
  1838. f2fs_release_ino_entry(sbi, true);
  1839. f2fs_leave_shrinker(sbi);
  1840. mutex_unlock(&sbi->umount_mutex);
  1841. /* our cp_error case, we can wait for any writeback page */
  1842. f2fs_flush_merged_writes(sbi);
  1843. f2fs_wait_on_all_pages(sbi, F2FS_WB_CP_DATA);
  1844. if (err || f2fs_cp_error(sbi)) {
  1845. truncate_inode_pages_final(NODE_MAPPING(sbi));
  1846. truncate_inode_pages_final(META_MAPPING(sbi));
  1847. }
  1848. f2fs_bug_on(sbi, sbi->fsync_node_num);
  1849. f2fs_destroy_compress_inode(sbi);
  1850. iput(sbi->node_inode);
  1851. sbi->node_inode = NULL;
  1852. iput(sbi->meta_inode);
  1853. sbi->meta_inode = NULL;
  1854. /* Should check the page counts after dropping all node/meta pages */
  1855. for (i = 0; i < NR_COUNT_TYPE; i++) {
  1856. if (!get_pages(sbi, i))
  1857. continue;
  1858. f2fs_err(sbi, "detect filesystem reference count leak during "
  1859. "umount, type: %d, count: %lld", i, get_pages(sbi, i));
  1860. f2fs_bug_on(sbi, 1);
  1861. }
  1862. /*
  1863. * iput() can update stat information, if f2fs_write_checkpoint()
  1864. * above failed with error.
  1865. */
  1866. f2fs_destroy_stats(sbi);
  1867. /* destroy f2fs internal modules */
  1868. f2fs_destroy_node_manager(sbi);
  1869. f2fs_destroy_segment_manager(sbi);
  1870. /* flush s_error_work before sbi destroy */
  1871. flush_work(&sbi->s_error_work);
  1872. f2fs_destroy_post_read_wq(sbi);
  1873. kvfree(sbi->ckpt);
  1874. kfree(sbi->raw_super);
  1875. f2fs_destroy_page_array_cache(sbi);
  1876. #ifdef CONFIG_QUOTA
  1877. for (i = 0; i < MAXQUOTAS; i++)
  1878. kfree(F2FS_OPTION(sbi).s_qf_names[i]);
  1879. #endif
  1880. fscrypt_free_dummy_policy(&F2FS_OPTION(sbi).dummy_enc_policy);
  1881. destroy_percpu_info(sbi);
  1882. f2fs_destroy_iostat(sbi);
  1883. for (i = 0; i < NR_PAGE_TYPE; i++)
  1884. kfree(sbi->write_io[i]);
  1885. #if IS_ENABLED(CONFIG_UNICODE)
  1886. utf8_unload(sb->s_encoding);
  1887. #endif
  1888. }
  1889. int f2fs_sync_fs(struct super_block *sb, int sync)
  1890. {
  1891. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1892. int err = 0;
  1893. if (unlikely(f2fs_cp_error(sbi)))
  1894. return 0;
  1895. if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
  1896. return 0;
  1897. trace_f2fs_sync_fs(sb, sync);
  1898. if (unlikely(is_sbi_flag_set(sbi, SBI_POR_DOING)))
  1899. return -EAGAIN;
  1900. if (sync) {
  1901. stat_inc_cp_call_count(sbi, TOTAL_CALL);
  1902. err = f2fs_issue_checkpoint(sbi);
  1903. }
  1904. return err;
  1905. }
  1906. static int f2fs_freeze(struct super_block *sb)
  1907. {
  1908. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1909. if (f2fs_readonly(sb))
  1910. return 0;
  1911. /* IO error happened before */
  1912. if (unlikely(f2fs_cp_error(sbi)))
  1913. return -EIO;
  1914. /* must be clean, since sync_filesystem() was already called */
  1915. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY))
  1916. return -EINVAL;
  1917. sbi->umount_lock_holder = current;
  1918. /* Let's flush checkpoints and stop the thread. */
  1919. f2fs_flush_ckpt_thread(sbi);
  1920. sbi->umount_lock_holder = NULL;
  1921. /* to avoid deadlock on f2fs_evict_inode->SB_FREEZE_FS */
  1922. set_sbi_flag(sbi, SBI_IS_FREEZING);
  1923. return 0;
  1924. }
  1925. static int f2fs_unfreeze(struct super_block *sb)
  1926. {
  1927. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1928. /*
  1929. * It will update discard_max_bytes of mounted lvm device to zero
  1930. * after creating snapshot on this lvm device, let's drop all
  1931. * remained discards.
  1932. * We don't need to disable real-time discard because discard_max_bytes
  1933. * will recover after removal of snapshot.
  1934. */
  1935. if (test_opt(sbi, DISCARD) && !f2fs_hw_support_discard(sbi))
  1936. f2fs_issue_discard_timeout(sbi);
  1937. clear_sbi_flag(F2FS_SB(sb), SBI_IS_FREEZING);
  1938. return 0;
  1939. }
  1940. #ifdef CONFIG_QUOTA
  1941. static int f2fs_statfs_project(struct super_block *sb,
  1942. kprojid_t projid, struct kstatfs *buf)
  1943. {
  1944. struct kqid qid;
  1945. struct dquot *dquot;
  1946. u64 limit;
  1947. u64 curblock;
  1948. qid = make_kqid_projid(projid);
  1949. dquot = dqget(sb, qid);
  1950. if (IS_ERR(dquot))
  1951. return PTR_ERR(dquot);
  1952. spin_lock(&dquot->dq_dqb_lock);
  1953. limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
  1954. dquot->dq_dqb.dqb_bhardlimit);
  1955. limit >>= sb->s_blocksize_bits;
  1956. if (limit) {
  1957. uint64_t remaining = 0;
  1958. curblock = (dquot->dq_dqb.dqb_curspace +
  1959. dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
  1960. if (limit > curblock)
  1961. remaining = limit - curblock;
  1962. buf->f_blocks = min(buf->f_blocks, limit);
  1963. buf->f_bfree = min(buf->f_bfree, remaining);
  1964. buf->f_bavail = min(buf->f_bavail, remaining);
  1965. }
  1966. limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
  1967. dquot->dq_dqb.dqb_ihardlimit);
  1968. if (limit) {
  1969. uint64_t remaining = 0;
  1970. if (limit > dquot->dq_dqb.dqb_curinodes)
  1971. remaining = limit - dquot->dq_dqb.dqb_curinodes;
  1972. buf->f_files = min(buf->f_files, limit);
  1973. buf->f_ffree = min(buf->f_ffree, remaining);
  1974. }
  1975. spin_unlock(&dquot->dq_dqb_lock);
  1976. dqput(dquot);
  1977. return 0;
  1978. }
  1979. #endif
  1980. static int f2fs_statfs(struct dentry *dentry, struct kstatfs *buf)
  1981. {
  1982. struct super_block *sb = dentry->d_sb;
  1983. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  1984. u64 id = huge_encode_dev(sb->s_bdev->bd_dev);
  1985. block_t total_count, user_block_count, start_count;
  1986. u64 avail_node_count;
  1987. unsigned int total_valid_node_count;
  1988. total_count = le64_to_cpu(sbi->raw_super->block_count);
  1989. start_count = le32_to_cpu(sbi->raw_super->segment0_blkaddr);
  1990. buf->f_type = F2FS_SUPER_MAGIC;
  1991. buf->f_bsize = sbi->blocksize;
  1992. buf->f_blocks = total_count - start_count;
  1993. spin_lock(&sbi->stat_lock);
  1994. if (sbi->carve_out)
  1995. buf->f_blocks -= sbi->current_reserved_blocks;
  1996. user_block_count = sbi->user_block_count;
  1997. total_valid_node_count = valid_node_count(sbi);
  1998. avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
  1999. buf->f_bfree = user_block_count - valid_user_blocks(sbi) -
  2000. sbi->current_reserved_blocks;
  2001. if (unlikely(buf->f_bfree <= sbi->unusable_block_count))
  2002. buf->f_bfree = 0;
  2003. else
  2004. buf->f_bfree -= sbi->unusable_block_count;
  2005. spin_unlock(&sbi->stat_lock);
  2006. if (buf->f_bfree > F2FS_OPTION(sbi).root_reserved_blocks)
  2007. buf->f_bavail = buf->f_bfree -
  2008. F2FS_OPTION(sbi).root_reserved_blocks;
  2009. else
  2010. buf->f_bavail = 0;
  2011. if (avail_node_count > user_block_count) {
  2012. buf->f_files = user_block_count;
  2013. buf->f_ffree = buf->f_bavail;
  2014. } else {
  2015. buf->f_files = avail_node_count;
  2016. buf->f_ffree = min(avail_node_count - total_valid_node_count,
  2017. buf->f_bavail);
  2018. }
  2019. buf->f_namelen = F2FS_NAME_LEN;
  2020. buf->f_fsid = u64_to_fsid(id);
  2021. #ifdef CONFIG_QUOTA
  2022. if (is_inode_flag_set(d_inode(dentry), FI_PROJ_INHERIT) &&
  2023. sb_has_quota_limits_enabled(sb, PRJQUOTA)) {
  2024. f2fs_statfs_project(sb, F2FS_I(d_inode(dentry))->i_projid, buf);
  2025. }
  2026. #endif
  2027. return 0;
  2028. }
  2029. static inline void f2fs_show_quota_options(struct seq_file *seq,
  2030. struct super_block *sb)
  2031. {
  2032. #ifdef CONFIG_QUOTA
  2033. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  2034. if (F2FS_OPTION(sbi).s_jquota_fmt) {
  2035. char *fmtname = "";
  2036. switch (F2FS_OPTION(sbi).s_jquota_fmt) {
  2037. case QFMT_VFS_OLD:
  2038. fmtname = "vfsold";
  2039. break;
  2040. case QFMT_VFS_V0:
  2041. fmtname = "vfsv0";
  2042. break;
  2043. case QFMT_VFS_V1:
  2044. fmtname = "vfsv1";
  2045. break;
  2046. }
  2047. seq_printf(seq, ",jqfmt=%s", fmtname);
  2048. }
  2049. if (F2FS_OPTION(sbi).s_qf_names[USRQUOTA])
  2050. seq_show_option(seq, "usrjquota",
  2051. F2FS_OPTION(sbi).s_qf_names[USRQUOTA]);
  2052. if (F2FS_OPTION(sbi).s_qf_names[GRPQUOTA])
  2053. seq_show_option(seq, "grpjquota",
  2054. F2FS_OPTION(sbi).s_qf_names[GRPQUOTA]);
  2055. if (F2FS_OPTION(sbi).s_qf_names[PRJQUOTA])
  2056. seq_show_option(seq, "prjjquota",
  2057. F2FS_OPTION(sbi).s_qf_names[PRJQUOTA]);
  2058. #endif
  2059. }
  2060. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2061. static inline void f2fs_show_compress_options(struct seq_file *seq,
  2062. struct super_block *sb)
  2063. {
  2064. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  2065. char *algtype = "";
  2066. int i;
  2067. if (!f2fs_sb_has_compression(sbi))
  2068. return;
  2069. switch (F2FS_OPTION(sbi).compress_algorithm) {
  2070. case COMPRESS_LZO:
  2071. algtype = "lzo";
  2072. break;
  2073. case COMPRESS_LZ4:
  2074. algtype = "lz4";
  2075. break;
  2076. case COMPRESS_ZSTD:
  2077. algtype = "zstd";
  2078. break;
  2079. case COMPRESS_LZORLE:
  2080. algtype = "lzo-rle";
  2081. break;
  2082. }
  2083. seq_printf(seq, ",compress_algorithm=%s", algtype);
  2084. if (F2FS_OPTION(sbi).compress_level)
  2085. seq_printf(seq, ":%d", F2FS_OPTION(sbi).compress_level);
  2086. seq_printf(seq, ",compress_log_size=%u",
  2087. F2FS_OPTION(sbi).compress_log_size);
  2088. for (i = 0; i < F2FS_OPTION(sbi).compress_ext_cnt; i++) {
  2089. seq_printf(seq, ",compress_extension=%s",
  2090. F2FS_OPTION(sbi).extensions[i]);
  2091. }
  2092. for (i = 0; i < F2FS_OPTION(sbi).nocompress_ext_cnt; i++) {
  2093. seq_printf(seq, ",nocompress_extension=%s",
  2094. F2FS_OPTION(sbi).noextensions[i]);
  2095. }
  2096. if (F2FS_OPTION(sbi).compress_chksum)
  2097. seq_puts(seq, ",compress_chksum");
  2098. if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_FS)
  2099. seq_printf(seq, ",compress_mode=%s", "fs");
  2100. else if (F2FS_OPTION(sbi).compress_mode == COMPR_MODE_USER)
  2101. seq_printf(seq, ",compress_mode=%s", "user");
  2102. if (test_opt(sbi, COMPRESS_CACHE))
  2103. seq_puts(seq, ",compress_cache");
  2104. }
  2105. #endif
  2106. static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
  2107. {
  2108. struct f2fs_sb_info *sbi = F2FS_SB(root->d_sb);
  2109. if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_SYNC)
  2110. seq_printf(seq, ",background_gc=%s", "sync");
  2111. else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_ON)
  2112. seq_printf(seq, ",background_gc=%s", "on");
  2113. else if (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF)
  2114. seq_printf(seq, ",background_gc=%s", "off");
  2115. if (test_opt(sbi, GC_MERGE))
  2116. seq_puts(seq, ",gc_merge");
  2117. else
  2118. seq_puts(seq, ",nogc_merge");
  2119. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  2120. seq_puts(seq, ",disable_roll_forward");
  2121. if (test_opt(sbi, NORECOVERY))
  2122. seq_puts(seq, ",norecovery");
  2123. if (test_opt(sbi, DISCARD)) {
  2124. seq_puts(seq, ",discard");
  2125. if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_BLOCK)
  2126. seq_printf(seq, ",discard_unit=%s", "block");
  2127. else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SEGMENT)
  2128. seq_printf(seq, ",discard_unit=%s", "segment");
  2129. else if (F2FS_OPTION(sbi).discard_unit == DISCARD_UNIT_SECTION)
  2130. seq_printf(seq, ",discard_unit=%s", "section");
  2131. } else {
  2132. seq_puts(seq, ",nodiscard");
  2133. }
  2134. #ifdef CONFIG_F2FS_FS_XATTR
  2135. if (test_opt(sbi, XATTR_USER))
  2136. seq_puts(seq, ",user_xattr");
  2137. else
  2138. seq_puts(seq, ",nouser_xattr");
  2139. if (test_opt(sbi, INLINE_XATTR))
  2140. seq_puts(seq, ",inline_xattr");
  2141. else
  2142. seq_puts(seq, ",noinline_xattr");
  2143. if (test_opt(sbi, INLINE_XATTR_SIZE))
  2144. seq_printf(seq, ",inline_xattr_size=%u",
  2145. F2FS_OPTION(sbi).inline_xattr_size);
  2146. #endif
  2147. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  2148. if (test_opt(sbi, POSIX_ACL))
  2149. seq_puts(seq, ",acl");
  2150. else
  2151. seq_puts(seq, ",noacl");
  2152. #endif
  2153. if (test_opt(sbi, DISABLE_EXT_IDENTIFY))
  2154. seq_puts(seq, ",disable_ext_identify");
  2155. if (test_opt(sbi, INLINE_DATA))
  2156. seq_puts(seq, ",inline_data");
  2157. else
  2158. seq_puts(seq, ",noinline_data");
  2159. if (test_opt(sbi, INLINE_DENTRY))
  2160. seq_puts(seq, ",inline_dentry");
  2161. else
  2162. seq_puts(seq, ",noinline_dentry");
  2163. if (test_opt(sbi, FLUSH_MERGE))
  2164. seq_puts(seq, ",flush_merge");
  2165. else
  2166. seq_puts(seq, ",noflush_merge");
  2167. if (test_opt(sbi, NOBARRIER))
  2168. seq_puts(seq, ",nobarrier");
  2169. else
  2170. seq_puts(seq, ",barrier");
  2171. if (test_opt(sbi, FASTBOOT))
  2172. seq_puts(seq, ",fastboot");
  2173. if (test_opt(sbi, READ_EXTENT_CACHE))
  2174. seq_puts(seq, ",extent_cache");
  2175. else
  2176. seq_puts(seq, ",noextent_cache");
  2177. if (test_opt(sbi, AGE_EXTENT_CACHE))
  2178. seq_puts(seq, ",age_extent_cache");
  2179. if (test_opt(sbi, DATA_FLUSH))
  2180. seq_puts(seq, ",data_flush");
  2181. seq_puts(seq, ",mode=");
  2182. if (F2FS_OPTION(sbi).fs_mode == FS_MODE_ADAPTIVE)
  2183. seq_puts(seq, "adaptive");
  2184. else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_LFS)
  2185. seq_puts(seq, "lfs");
  2186. else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_SEG)
  2187. seq_puts(seq, "fragment:segment");
  2188. else if (F2FS_OPTION(sbi).fs_mode == FS_MODE_FRAGMENT_BLK)
  2189. seq_puts(seq, "fragment:block");
  2190. seq_printf(seq, ",active_logs=%u", F2FS_OPTION(sbi).active_logs);
  2191. if (test_opt(sbi, RESERVE_ROOT) || test_opt(sbi, RESERVE_NODE))
  2192. seq_printf(seq, ",reserve_root=%u,reserve_node=%u,resuid=%u,"
  2193. "resgid=%u",
  2194. F2FS_OPTION(sbi).root_reserved_blocks,
  2195. F2FS_OPTION(sbi).root_reserved_nodes,
  2196. from_kuid_munged(&init_user_ns,
  2197. F2FS_OPTION(sbi).s_resuid),
  2198. from_kgid_munged(&init_user_ns,
  2199. F2FS_OPTION(sbi).s_resgid));
  2200. #ifdef CONFIG_F2FS_FAULT_INJECTION
  2201. if (test_opt(sbi, FAULT_INJECTION)) {
  2202. seq_printf(seq, ",fault_injection=%u",
  2203. F2FS_OPTION(sbi).fault_info.inject_rate);
  2204. seq_printf(seq, ",fault_type=%u",
  2205. F2FS_OPTION(sbi).fault_info.inject_type);
  2206. }
  2207. #endif
  2208. #ifdef CONFIG_QUOTA
  2209. if (test_opt(sbi, QUOTA))
  2210. seq_puts(seq, ",quota");
  2211. if (test_opt(sbi, USRQUOTA))
  2212. seq_puts(seq, ",usrquota");
  2213. if (test_opt(sbi, GRPQUOTA))
  2214. seq_puts(seq, ",grpquota");
  2215. if (test_opt(sbi, PRJQUOTA))
  2216. seq_puts(seq, ",prjquota");
  2217. #endif
  2218. f2fs_show_quota_options(seq, sbi->sb);
  2219. fscrypt_show_test_dummy_encryption(seq, ',', sbi->sb);
  2220. if (sbi->sb->s_flags & SB_INLINECRYPT)
  2221. seq_puts(seq, ",inlinecrypt");
  2222. if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_DEFAULT)
  2223. seq_printf(seq, ",alloc_mode=%s", "default");
  2224. else if (F2FS_OPTION(sbi).alloc_mode == ALLOC_MODE_REUSE)
  2225. seq_printf(seq, ",alloc_mode=%s", "reuse");
  2226. if (test_opt(sbi, DISABLE_CHECKPOINT))
  2227. seq_printf(seq, ",checkpoint=disable:%u",
  2228. F2FS_OPTION(sbi).unusable_cap);
  2229. if (test_opt(sbi, MERGE_CHECKPOINT))
  2230. seq_puts(seq, ",checkpoint_merge");
  2231. else
  2232. seq_puts(seq, ",nocheckpoint_merge");
  2233. if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_POSIX)
  2234. seq_printf(seq, ",fsync_mode=%s", "posix");
  2235. else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_STRICT)
  2236. seq_printf(seq, ",fsync_mode=%s", "strict");
  2237. else if (F2FS_OPTION(sbi).fsync_mode == FSYNC_MODE_NOBARRIER)
  2238. seq_printf(seq, ",fsync_mode=%s", "nobarrier");
  2239. #ifdef CONFIG_F2FS_FS_COMPRESSION
  2240. f2fs_show_compress_options(seq, sbi->sb);
  2241. #endif
  2242. if (test_opt(sbi, ATGC))
  2243. seq_puts(seq, ",atgc");
  2244. if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_NORMAL)
  2245. seq_printf(seq, ",memory=%s", "normal");
  2246. else if (F2FS_OPTION(sbi).memory_mode == MEMORY_MODE_LOW)
  2247. seq_printf(seq, ",memory=%s", "low");
  2248. if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_READONLY)
  2249. seq_printf(seq, ",errors=%s", "remount-ro");
  2250. else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE)
  2251. seq_printf(seq, ",errors=%s", "continue");
  2252. else if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC)
  2253. seq_printf(seq, ",errors=%s", "panic");
  2254. if (test_opt(sbi, NAT_BITS))
  2255. seq_puts(seq, ",nat_bits");
  2256. if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_PERF)
  2257. seq_show_option(seq, "lookup_mode", "perf");
  2258. else if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_COMPAT)
  2259. seq_show_option(seq, "lookup_mode", "compat");
  2260. else if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_AUTO)
  2261. seq_show_option(seq, "lookup_mode", "auto");
  2262. return 0;
  2263. }
  2264. static void default_options(struct f2fs_sb_info *sbi, bool remount)
  2265. {
  2266. /* init some FS parameters */
  2267. if (!remount) {
  2268. set_opt(sbi, READ_EXTENT_CACHE);
  2269. clear_opt(sbi, DISABLE_CHECKPOINT);
  2270. if (f2fs_hw_support_discard(sbi) || f2fs_hw_should_discard(sbi))
  2271. set_opt(sbi, DISCARD);
  2272. if (f2fs_sb_has_blkzoned(sbi))
  2273. F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_SECTION;
  2274. else
  2275. F2FS_OPTION(sbi).discard_unit = DISCARD_UNIT_BLOCK;
  2276. }
  2277. if (f2fs_sb_has_readonly(sbi))
  2278. F2FS_OPTION(sbi).active_logs = NR_CURSEG_RO_TYPE;
  2279. else
  2280. F2FS_OPTION(sbi).active_logs = NR_CURSEG_PERSIST_TYPE;
  2281. F2FS_OPTION(sbi).inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
  2282. if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->segment_count_main) <=
  2283. SMALL_VOLUME_SEGMENTS)
  2284. F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_REUSE;
  2285. else
  2286. F2FS_OPTION(sbi).alloc_mode = ALLOC_MODE_DEFAULT;
  2287. F2FS_OPTION(sbi).fsync_mode = FSYNC_MODE_POSIX;
  2288. F2FS_OPTION(sbi).s_resuid = make_kuid(&init_user_ns, F2FS_DEF_RESUID);
  2289. F2FS_OPTION(sbi).s_resgid = make_kgid(&init_user_ns, F2FS_DEF_RESGID);
  2290. if (f2fs_sb_has_compression(sbi)) {
  2291. F2FS_OPTION(sbi).compress_algorithm = COMPRESS_LZ4;
  2292. F2FS_OPTION(sbi).compress_log_size = MIN_COMPRESS_LOG_SIZE;
  2293. F2FS_OPTION(sbi).compress_ext_cnt = 0;
  2294. F2FS_OPTION(sbi).compress_mode = COMPR_MODE_FS;
  2295. }
  2296. F2FS_OPTION(sbi).bggc_mode = BGGC_MODE_ON;
  2297. F2FS_OPTION(sbi).memory_mode = MEMORY_MODE_NORMAL;
  2298. F2FS_OPTION(sbi).errors = MOUNT_ERRORS_CONTINUE;
  2299. set_opt(sbi, INLINE_XATTR);
  2300. set_opt(sbi, INLINE_DATA);
  2301. set_opt(sbi, INLINE_DENTRY);
  2302. set_opt(sbi, MERGE_CHECKPOINT);
  2303. set_opt(sbi, LAZYTIME);
  2304. F2FS_OPTION(sbi).unusable_cap = 0;
  2305. if (!f2fs_is_readonly(sbi))
  2306. set_opt(sbi, FLUSH_MERGE);
  2307. if (f2fs_sb_has_blkzoned(sbi))
  2308. F2FS_OPTION(sbi).fs_mode = FS_MODE_LFS;
  2309. else
  2310. F2FS_OPTION(sbi).fs_mode = FS_MODE_ADAPTIVE;
  2311. #ifdef CONFIG_F2FS_FS_XATTR
  2312. set_opt(sbi, XATTR_USER);
  2313. #endif
  2314. #ifdef CONFIG_F2FS_FS_POSIX_ACL
  2315. set_opt(sbi, POSIX_ACL);
  2316. #endif
  2317. f2fs_build_fault_attr(sbi, 0, 0, FAULT_ALL);
  2318. F2FS_OPTION(sbi).lookup_mode = LOOKUP_PERF;
  2319. }
  2320. #ifdef CONFIG_QUOTA
  2321. static int f2fs_enable_quotas(struct super_block *sb);
  2322. #endif
  2323. static int f2fs_disable_checkpoint(struct f2fs_sb_info *sbi)
  2324. {
  2325. unsigned int s_flags = sbi->sb->s_flags;
  2326. struct cp_control cpc;
  2327. struct f2fs_lock_context lc;
  2328. unsigned int gc_mode = sbi->gc_mode;
  2329. int err = 0;
  2330. int ret;
  2331. block_t unusable;
  2332. if (s_flags & SB_RDONLY) {
  2333. f2fs_err(sbi, "checkpoint=disable on readonly fs");
  2334. return -EINVAL;
  2335. }
  2336. sbi->sb->s_flags |= SB_ACTIVE;
  2337. /* check if we need more GC first */
  2338. unusable = f2fs_get_unusable_blocks(sbi);
  2339. if (!f2fs_disable_cp_again(sbi, unusable))
  2340. goto skip_gc;
  2341. f2fs_update_time(sbi, DISABLE_TIME);
  2342. sbi->gc_mode = GC_URGENT_HIGH;
  2343. while (!f2fs_time_over(sbi, DISABLE_TIME)) {
  2344. struct f2fs_gc_control gc_control = {
  2345. .victim_segno = NULL_SEGNO,
  2346. .init_gc_type = FG_GC,
  2347. .should_migrate_blocks = false,
  2348. .err_gc_skipped = true,
  2349. .no_bg_gc = true,
  2350. .nr_free_secs = 1 };
  2351. f2fs_down_write_trace(&sbi->gc_lock, &gc_control.lc);
  2352. stat_inc_gc_call_count(sbi, FOREGROUND);
  2353. err = f2fs_gc(sbi, &gc_control);
  2354. if (err == -ENODATA) {
  2355. err = 0;
  2356. break;
  2357. }
  2358. if (err && err != -EAGAIN)
  2359. break;
  2360. }
  2361. ret = sync_filesystem(sbi->sb);
  2362. if (ret || err) {
  2363. err = ret ? ret : err;
  2364. goto restore_flag;
  2365. }
  2366. unusable = f2fs_get_unusable_blocks(sbi);
  2367. if (f2fs_disable_cp_again(sbi, unusable)) {
  2368. err = -EAGAIN;
  2369. goto restore_flag;
  2370. }
  2371. skip_gc:
  2372. f2fs_down_write_trace(&sbi->gc_lock, &lc);
  2373. cpc.reason = CP_PAUSE;
  2374. set_sbi_flag(sbi, SBI_CP_DISABLED);
  2375. stat_inc_cp_call_count(sbi, TOTAL_CALL);
  2376. err = f2fs_write_checkpoint(sbi, &cpc);
  2377. if (err)
  2378. goto out_unlock;
  2379. spin_lock(&sbi->stat_lock);
  2380. sbi->unusable_block_count = unusable;
  2381. spin_unlock(&sbi->stat_lock);
  2382. out_unlock:
  2383. f2fs_up_write_trace(&sbi->gc_lock, &lc);
  2384. restore_flag:
  2385. sbi->gc_mode = gc_mode;
  2386. sbi->sb->s_flags = s_flags; /* Restore SB_RDONLY status */
  2387. f2fs_info(sbi, "f2fs_disable_checkpoint() finish, err:%d", err);
  2388. return err;
  2389. }
  2390. static int f2fs_enable_checkpoint(struct f2fs_sb_info *sbi)
  2391. {
  2392. int retry = MAX_FLUSH_RETRY_COUNT;
  2393. long long start, writeback, end;
  2394. int ret;
  2395. struct f2fs_lock_context lc;
  2396. long long skipped_write, dirty_data;
  2397. f2fs_info(sbi, "f2fs_enable_checkpoint() starts, meta: %lld, node: %lld, data: %lld",
  2398. get_pages(sbi, F2FS_DIRTY_META),
  2399. get_pages(sbi, F2FS_DIRTY_NODES),
  2400. get_pages(sbi, F2FS_DIRTY_DATA));
  2401. start = ktime_get();
  2402. set_sbi_flag(sbi, SBI_ENABLE_CHECKPOINT);
  2403. /* we should flush all the data to keep data consistency */
  2404. do {
  2405. skipped_write = get_pages(sbi, F2FS_SKIPPED_WRITE);
  2406. dirty_data = get_pages(sbi, F2FS_DIRTY_DATA);
  2407. sync_inodes_sb(sbi->sb);
  2408. f2fs_io_schedule_timeout(DEFAULT_SCHEDULE_TIMEOUT);
  2409. f2fs_info(sbi, "sync_inode_sb done, dirty_data: %lld, %lld, "
  2410. "skipped write: %lld, %lld, retry: %d",
  2411. get_pages(sbi, F2FS_DIRTY_DATA),
  2412. dirty_data,
  2413. get_pages(sbi, F2FS_SKIPPED_WRITE),
  2414. skipped_write, retry);
  2415. /*
  2416. * sync_inodes_sb() has retry logic, so let's check dirty_data
  2417. * in prior to skipped_write in case there is no dirty data.
  2418. */
  2419. if (!get_pages(sbi, F2FS_DIRTY_DATA))
  2420. break;
  2421. if (get_pages(sbi, F2FS_SKIPPED_WRITE) == skipped_write)
  2422. break;
  2423. } while (retry--);
  2424. clear_sbi_flag(sbi, SBI_ENABLE_CHECKPOINT);
  2425. writeback = ktime_get();
  2426. if (unlikely(get_pages(sbi, F2FS_DIRTY_DATA) ||
  2427. get_pages(sbi, F2FS_SKIPPED_WRITE)))
  2428. f2fs_warn(sbi, "checkpoint=enable unwritten data: %lld, skipped data: %lld, retry: %d",
  2429. get_pages(sbi, F2FS_DIRTY_DATA),
  2430. get_pages(sbi, F2FS_SKIPPED_WRITE), retry);
  2431. if (get_pages(sbi, F2FS_SKIPPED_WRITE))
  2432. atomic_set(&sbi->nr_pages[F2FS_SKIPPED_WRITE], 0);
  2433. f2fs_down_write_trace(&sbi->gc_lock, &lc);
  2434. f2fs_dirty_to_prefree(sbi);
  2435. clear_sbi_flag(sbi, SBI_CP_DISABLED);
  2436. set_sbi_flag(sbi, SBI_IS_DIRTY);
  2437. f2fs_up_write_trace(&sbi->gc_lock, &lc);
  2438. ret = f2fs_sync_fs(sbi->sb, 1);
  2439. if (ret)
  2440. f2fs_err(sbi, "%s sync_fs failed, ret: %d", __func__, ret);
  2441. /* Let's ensure there's no pending checkpoint anymore */
  2442. f2fs_flush_ckpt_thread(sbi);
  2443. end = ktime_get();
  2444. f2fs_info(sbi, "f2fs_enable_checkpoint() finishes, writeback:%llu, sync:%llu",
  2445. ktime_ms_delta(writeback, start),
  2446. ktime_ms_delta(end, writeback));
  2447. return ret;
  2448. }
  2449. static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
  2450. {
  2451. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  2452. struct f2fs_mount_info org_mount_opt;
  2453. unsigned long old_sb_flags;
  2454. unsigned int flags = fc->sb_flags;
  2455. int err;
  2456. bool need_restart_gc = false, need_stop_gc = false;
  2457. bool need_restart_flush = false, need_stop_flush = false;
  2458. bool need_restart_discard = false, need_stop_discard = false;
  2459. bool need_enable_checkpoint = false, need_disable_checkpoint = false;
  2460. bool no_read_extent_cache = !test_opt(sbi, READ_EXTENT_CACHE);
  2461. bool no_age_extent_cache = !test_opt(sbi, AGE_EXTENT_CACHE);
  2462. bool enable_checkpoint = !test_opt(sbi, DISABLE_CHECKPOINT);
  2463. bool no_atgc = !test_opt(sbi, ATGC);
  2464. bool no_discard = !test_opt(sbi, DISCARD);
  2465. bool no_compress_cache = !test_opt(sbi, COMPRESS_CACHE);
  2466. bool block_unit_discard = f2fs_block_unit_discard(sbi);
  2467. bool no_nat_bits = !test_opt(sbi, NAT_BITS);
  2468. #ifdef CONFIG_QUOTA
  2469. int i, j;
  2470. #endif
  2471. /*
  2472. * Save the old mount options in case we
  2473. * need to restore them.
  2474. */
  2475. org_mount_opt = sbi->mount_opt;
  2476. old_sb_flags = sb->s_flags;
  2477. sbi->umount_lock_holder = current;
  2478. #ifdef CONFIG_QUOTA
  2479. org_mount_opt.s_jquota_fmt = F2FS_OPTION(sbi).s_jquota_fmt;
  2480. for (i = 0; i < MAXQUOTAS; i++) {
  2481. if (F2FS_OPTION(sbi).s_qf_names[i]) {
  2482. org_mount_opt.s_qf_names[i] =
  2483. kstrdup(F2FS_OPTION(sbi).s_qf_names[i],
  2484. GFP_KERNEL);
  2485. if (!org_mount_opt.s_qf_names[i]) {
  2486. for (j = 0; j < i; j++)
  2487. kfree(org_mount_opt.s_qf_names[j]);
  2488. return -ENOMEM;
  2489. }
  2490. } else {
  2491. org_mount_opt.s_qf_names[i] = NULL;
  2492. }
  2493. }
  2494. #endif
  2495. /* recover superblocks we couldn't write due to previous RO mount */
  2496. if (!(flags & SB_RDONLY) && is_sbi_flag_set(sbi, SBI_NEED_SB_WRITE)) {
  2497. err = f2fs_commit_super(sbi, false);
  2498. f2fs_info(sbi, "Try to recover all the superblocks, ret: %d",
  2499. err);
  2500. if (!err)
  2501. clear_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  2502. }
  2503. default_options(sbi, true);
  2504. err = f2fs_check_opt_consistency(fc, sb);
  2505. if (err)
  2506. goto restore_opts;
  2507. f2fs_apply_options(fc, sb);
  2508. err = f2fs_sanity_check_options(sbi, true);
  2509. if (err)
  2510. goto restore_opts;
  2511. /* flush outstanding errors before changing fs state */
  2512. flush_work(&sbi->s_error_work);
  2513. /*
  2514. * Previous and new state of filesystem is RO,
  2515. * so skip checking GC and FLUSH_MERGE conditions.
  2516. */
  2517. if (f2fs_readonly(sb) && (flags & SB_RDONLY))
  2518. goto skip;
  2519. if (f2fs_dev_is_readonly(sbi) && !(flags & SB_RDONLY)) {
  2520. err = -EROFS;
  2521. goto restore_opts;
  2522. }
  2523. #ifdef CONFIG_QUOTA
  2524. if (!f2fs_readonly(sb) && (flags & SB_RDONLY)) {
  2525. err = dquot_suspend(sb, -1);
  2526. if (err < 0)
  2527. goto restore_opts;
  2528. } else if (f2fs_readonly(sb) && !(flags & SB_RDONLY)) {
  2529. /* dquot_resume needs RW */
  2530. sb->s_flags &= ~SB_RDONLY;
  2531. if (sb_any_quota_suspended(sb)) {
  2532. dquot_resume(sb, -1);
  2533. } else if (f2fs_sb_has_quota_ino(sbi)) {
  2534. err = f2fs_enable_quotas(sb);
  2535. if (err)
  2536. goto restore_opts;
  2537. }
  2538. }
  2539. #endif
  2540. /* disallow enable atgc dynamically */
  2541. if (no_atgc == !!test_opt(sbi, ATGC)) {
  2542. err = -EINVAL;
  2543. f2fs_warn(sbi, "switch atgc option is not allowed");
  2544. goto restore_opts;
  2545. }
  2546. /* disallow enable/disable extent_cache dynamically */
  2547. if (no_read_extent_cache == !!test_opt(sbi, READ_EXTENT_CACHE)) {
  2548. err = -EINVAL;
  2549. f2fs_warn(sbi, "switch extent_cache option is not allowed");
  2550. goto restore_opts;
  2551. }
  2552. /* disallow enable/disable age extent_cache dynamically */
  2553. if (no_age_extent_cache == !!test_opt(sbi, AGE_EXTENT_CACHE)) {
  2554. err = -EINVAL;
  2555. f2fs_warn(sbi, "switch age_extent_cache option is not allowed");
  2556. goto restore_opts;
  2557. }
  2558. if (no_compress_cache == !!test_opt(sbi, COMPRESS_CACHE)) {
  2559. err = -EINVAL;
  2560. f2fs_warn(sbi, "switch compress_cache option is not allowed");
  2561. goto restore_opts;
  2562. }
  2563. if (block_unit_discard != f2fs_block_unit_discard(sbi)) {
  2564. err = -EINVAL;
  2565. f2fs_warn(sbi, "switch discard_unit option is not allowed");
  2566. goto restore_opts;
  2567. }
  2568. if (no_nat_bits == !!test_opt(sbi, NAT_BITS)) {
  2569. err = -EINVAL;
  2570. f2fs_warn(sbi, "switch nat_bits option is not allowed");
  2571. goto restore_opts;
  2572. }
  2573. if ((flags & SB_RDONLY) && test_opt(sbi, DISABLE_CHECKPOINT)) {
  2574. err = -EINVAL;
  2575. f2fs_warn(sbi, "disabling checkpoint not compatible with read-only");
  2576. goto restore_opts;
  2577. }
  2578. /*
  2579. * We stop the GC thread if FS is mounted as RO
  2580. * or if background_gc = off is passed in mount
  2581. * option. Also sync the filesystem.
  2582. */
  2583. if ((flags & SB_RDONLY) ||
  2584. (F2FS_OPTION(sbi).bggc_mode == BGGC_MODE_OFF &&
  2585. !test_opt(sbi, GC_MERGE))) {
  2586. if (sbi->gc_thread) {
  2587. f2fs_stop_gc_thread(sbi);
  2588. need_restart_gc = true;
  2589. }
  2590. } else if (!sbi->gc_thread) {
  2591. err = f2fs_start_gc_thread(sbi);
  2592. if (err)
  2593. goto restore_opts;
  2594. need_stop_gc = true;
  2595. }
  2596. if (flags & SB_RDONLY) {
  2597. sync_inodes_sb(sb);
  2598. set_sbi_flag(sbi, SBI_IS_DIRTY);
  2599. set_sbi_flag(sbi, SBI_IS_CLOSE);
  2600. f2fs_sync_fs(sb, 1);
  2601. clear_sbi_flag(sbi, SBI_IS_CLOSE);
  2602. }
  2603. /*
  2604. * We stop issue flush thread if FS is mounted as RO
  2605. * or if flush_merge is not passed in mount option.
  2606. */
  2607. if ((flags & SB_RDONLY) || !test_opt(sbi, FLUSH_MERGE)) {
  2608. clear_opt(sbi, FLUSH_MERGE);
  2609. f2fs_destroy_flush_cmd_control(sbi, false);
  2610. need_restart_flush = true;
  2611. } else {
  2612. err = f2fs_create_flush_cmd_control(sbi);
  2613. if (err)
  2614. goto restore_gc;
  2615. need_stop_flush = true;
  2616. }
  2617. if (no_discard == !!test_opt(sbi, DISCARD)) {
  2618. if (test_opt(sbi, DISCARD)) {
  2619. err = f2fs_start_discard_thread(sbi);
  2620. if (err)
  2621. goto restore_flush;
  2622. need_stop_discard = true;
  2623. } else {
  2624. f2fs_stop_discard_thread(sbi);
  2625. f2fs_issue_discard_timeout(sbi);
  2626. need_restart_discard = true;
  2627. }
  2628. }
  2629. adjust_unusable_cap_perc(sbi);
  2630. if (enable_checkpoint == !!test_opt(sbi, DISABLE_CHECKPOINT)) {
  2631. if (test_opt(sbi, DISABLE_CHECKPOINT)) {
  2632. err = f2fs_disable_checkpoint(sbi);
  2633. if (err)
  2634. goto restore_discard;
  2635. need_enable_checkpoint = true;
  2636. } else {
  2637. err = f2fs_enable_checkpoint(sbi);
  2638. if (err)
  2639. goto restore_discard;
  2640. need_disable_checkpoint = true;
  2641. }
  2642. }
  2643. /*
  2644. * Place this routine at the end, since a new checkpoint would be
  2645. * triggered while remount and we need to take care of it before
  2646. * returning from remount.
  2647. */
  2648. if ((flags & SB_RDONLY) || test_opt(sbi, DISABLE_CHECKPOINT) ||
  2649. !test_opt(sbi, MERGE_CHECKPOINT)) {
  2650. f2fs_stop_ckpt_thread(sbi);
  2651. } else {
  2652. /* Flush if the previous checkpoint, if exists. */
  2653. f2fs_flush_ckpt_thread(sbi);
  2654. err = f2fs_start_ckpt_thread(sbi);
  2655. if (err) {
  2656. f2fs_err(sbi,
  2657. "Failed to start F2FS issue_checkpoint_thread (%d)",
  2658. err);
  2659. goto restore_checkpoint;
  2660. }
  2661. }
  2662. skip:
  2663. #ifdef CONFIG_QUOTA
  2664. /* Release old quota file names */
  2665. for (i = 0; i < MAXQUOTAS; i++)
  2666. kfree(org_mount_opt.s_qf_names[i]);
  2667. #endif
  2668. /* Update the POSIXACL Flag */
  2669. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  2670. (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
  2671. limit_reserve_root(sbi);
  2672. fc->sb_flags = (flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
  2673. sbi->umount_lock_holder = NULL;
  2674. return 0;
  2675. restore_checkpoint:
  2676. if (need_enable_checkpoint) {
  2677. if (f2fs_enable_checkpoint(sbi))
  2678. f2fs_warn(sbi, "checkpoint has not been enabled");
  2679. } else if (need_disable_checkpoint) {
  2680. if (f2fs_disable_checkpoint(sbi))
  2681. f2fs_warn(sbi, "checkpoint has not been disabled");
  2682. }
  2683. restore_discard:
  2684. if (need_restart_discard) {
  2685. if (f2fs_start_discard_thread(sbi))
  2686. f2fs_warn(sbi, "discard has been stopped");
  2687. } else if (need_stop_discard) {
  2688. f2fs_stop_discard_thread(sbi);
  2689. }
  2690. restore_flush:
  2691. if (need_restart_flush) {
  2692. if (f2fs_create_flush_cmd_control(sbi))
  2693. f2fs_warn(sbi, "background flush thread has stopped");
  2694. } else if (need_stop_flush) {
  2695. clear_opt(sbi, FLUSH_MERGE);
  2696. f2fs_destroy_flush_cmd_control(sbi, false);
  2697. }
  2698. restore_gc:
  2699. if (need_restart_gc) {
  2700. if (f2fs_start_gc_thread(sbi))
  2701. f2fs_warn(sbi, "background gc thread has stopped");
  2702. } else if (need_stop_gc) {
  2703. f2fs_stop_gc_thread(sbi);
  2704. }
  2705. restore_opts:
  2706. #ifdef CONFIG_QUOTA
  2707. F2FS_OPTION(sbi).s_jquota_fmt = org_mount_opt.s_jquota_fmt;
  2708. for (i = 0; i < MAXQUOTAS; i++) {
  2709. kfree(F2FS_OPTION(sbi).s_qf_names[i]);
  2710. F2FS_OPTION(sbi).s_qf_names[i] = org_mount_opt.s_qf_names[i];
  2711. }
  2712. #endif
  2713. sbi->mount_opt = org_mount_opt;
  2714. sb->s_flags = old_sb_flags;
  2715. sbi->umount_lock_holder = NULL;
  2716. return err;
  2717. }
  2718. static void f2fs_shutdown(struct super_block *sb)
  2719. {
  2720. f2fs_do_shutdown(F2FS_SB(sb), F2FS_GOING_DOWN_NOSYNC, false, false);
  2721. }
  2722. #ifdef CONFIG_QUOTA
  2723. static bool f2fs_need_recovery(struct f2fs_sb_info *sbi)
  2724. {
  2725. /* need to recovery orphan */
  2726. if (is_set_ckpt_flags(sbi, CP_ORPHAN_PRESENT_FLAG))
  2727. return true;
  2728. /* need to recovery data */
  2729. if (test_opt(sbi, DISABLE_ROLL_FORWARD))
  2730. return false;
  2731. if (test_opt(sbi, NORECOVERY))
  2732. return false;
  2733. return !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG);
  2734. }
  2735. static bool f2fs_recover_quota_begin(struct f2fs_sb_info *sbi)
  2736. {
  2737. bool readonly = f2fs_readonly(sbi->sb);
  2738. if (!f2fs_need_recovery(sbi))
  2739. return false;
  2740. /* it doesn't need to check f2fs_sb_has_readonly() */
  2741. if (f2fs_hw_is_readonly(sbi))
  2742. return false;
  2743. if (readonly) {
  2744. sbi->sb->s_flags &= ~SB_RDONLY;
  2745. set_sbi_flag(sbi, SBI_IS_WRITABLE);
  2746. }
  2747. /*
  2748. * Turn on quotas which were not enabled for read-only mounts if
  2749. * filesystem has quota feature, so that they are updated correctly.
  2750. */
  2751. return f2fs_enable_quota_files(sbi, readonly);
  2752. }
  2753. static void f2fs_recover_quota_end(struct f2fs_sb_info *sbi,
  2754. bool quota_enabled)
  2755. {
  2756. if (quota_enabled)
  2757. f2fs_quota_off_umount(sbi->sb);
  2758. if (is_sbi_flag_set(sbi, SBI_IS_WRITABLE)) {
  2759. clear_sbi_flag(sbi, SBI_IS_WRITABLE);
  2760. sbi->sb->s_flags |= SB_RDONLY;
  2761. }
  2762. }
  2763. /* Read data from quotafile */
  2764. static ssize_t f2fs_quota_read(struct super_block *sb, int type, char *data,
  2765. size_t len, loff_t off)
  2766. {
  2767. struct inode *inode = sb_dqopt(sb)->files[type];
  2768. struct address_space *mapping = inode->i_mapping;
  2769. int tocopy;
  2770. size_t toread;
  2771. loff_t i_size = i_size_read(inode);
  2772. if (off > i_size)
  2773. return 0;
  2774. if (off + len > i_size)
  2775. len = i_size - off;
  2776. toread = len;
  2777. while (toread > 0) {
  2778. struct folio *folio;
  2779. size_t offset;
  2780. repeat:
  2781. folio = mapping_read_folio_gfp(mapping, off >> PAGE_SHIFT,
  2782. GFP_NOFS);
  2783. if (IS_ERR(folio)) {
  2784. if (PTR_ERR(folio) == -ENOMEM) {
  2785. memalloc_retry_wait(GFP_NOFS);
  2786. goto repeat;
  2787. }
  2788. set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
  2789. return PTR_ERR(folio);
  2790. }
  2791. offset = offset_in_folio(folio, off);
  2792. tocopy = min(folio_size(folio) - offset, toread);
  2793. folio_lock(folio);
  2794. if (unlikely(folio->mapping != mapping)) {
  2795. f2fs_folio_put(folio, true);
  2796. goto repeat;
  2797. }
  2798. /*
  2799. * should never happen, just leave f2fs_bug_on() here to catch
  2800. * any potential bug.
  2801. */
  2802. f2fs_bug_on(F2FS_SB(sb), !folio_test_uptodate(folio));
  2803. memcpy_from_folio(data, folio, offset, tocopy);
  2804. f2fs_folio_put(folio, true);
  2805. toread -= tocopy;
  2806. data += tocopy;
  2807. off += tocopy;
  2808. }
  2809. return len;
  2810. }
  2811. /* Write to quotafile */
  2812. static ssize_t f2fs_quota_write(struct super_block *sb, int type,
  2813. const char *data, size_t len, loff_t off)
  2814. {
  2815. struct inode *inode = sb_dqopt(sb)->files[type];
  2816. struct address_space *mapping = inode->i_mapping;
  2817. const struct address_space_operations *a_ops = mapping->a_ops;
  2818. int offset = off & (sb->s_blocksize - 1);
  2819. size_t towrite = len;
  2820. struct folio *folio;
  2821. void *fsdata = NULL;
  2822. int err = 0;
  2823. int tocopy;
  2824. while (towrite > 0) {
  2825. tocopy = min_t(unsigned long, sb->s_blocksize - offset,
  2826. towrite);
  2827. retry:
  2828. err = a_ops->write_begin(NULL, mapping, off, tocopy,
  2829. &folio, &fsdata);
  2830. if (unlikely(err)) {
  2831. if (err == -ENOMEM) {
  2832. memalloc_retry_wait(GFP_NOFS);
  2833. goto retry;
  2834. }
  2835. set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
  2836. break;
  2837. }
  2838. memcpy_to_folio(folio, offset_in_folio(folio, off), data, tocopy);
  2839. a_ops->write_end(NULL, mapping, off, tocopy, tocopy,
  2840. folio, fsdata);
  2841. offset = 0;
  2842. towrite -= tocopy;
  2843. off += tocopy;
  2844. data += tocopy;
  2845. cond_resched();
  2846. }
  2847. if (len == towrite)
  2848. return err;
  2849. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  2850. f2fs_mark_inode_dirty_sync(inode, false);
  2851. return len - towrite;
  2852. }
  2853. int f2fs_dquot_initialize(struct inode *inode)
  2854. {
  2855. if (time_to_inject(F2FS_I_SB(inode), FAULT_DQUOT_INIT))
  2856. return -ESRCH;
  2857. return dquot_initialize(inode);
  2858. }
  2859. static struct dquot __rcu **f2fs_get_dquots(struct inode *inode)
  2860. {
  2861. return F2FS_I(inode)->i_dquot;
  2862. }
  2863. static qsize_t *f2fs_get_reserved_space(struct inode *inode)
  2864. {
  2865. return &F2FS_I(inode)->i_reserved_quota;
  2866. }
  2867. static int f2fs_quota_on_mount(struct f2fs_sb_info *sbi, int type)
  2868. {
  2869. if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
  2870. f2fs_err(sbi, "quota sysfile may be corrupted, skip loading it");
  2871. return 0;
  2872. }
  2873. return dquot_quota_on_mount(sbi->sb, F2FS_OPTION(sbi).s_qf_names[type],
  2874. F2FS_OPTION(sbi).s_jquota_fmt, type);
  2875. }
  2876. int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly)
  2877. {
  2878. int enabled = 0;
  2879. int i, err;
  2880. if (f2fs_sb_has_quota_ino(sbi) && rdonly) {
  2881. err = f2fs_enable_quotas(sbi->sb);
  2882. if (err) {
  2883. f2fs_err(sbi, "Cannot turn on quota_ino: %d", err);
  2884. return 0;
  2885. }
  2886. return 1;
  2887. }
  2888. for (i = 0; i < MAXQUOTAS; i++) {
  2889. if (F2FS_OPTION(sbi).s_qf_names[i]) {
  2890. err = f2fs_quota_on_mount(sbi, i);
  2891. if (!err) {
  2892. enabled = 1;
  2893. continue;
  2894. }
  2895. f2fs_err(sbi, "Cannot turn on quotas: %d on %d",
  2896. err, i);
  2897. }
  2898. }
  2899. return enabled;
  2900. }
  2901. static int f2fs_quota_enable(struct super_block *sb, int type, int format_id,
  2902. unsigned int flags, unsigned long qf_inum)
  2903. {
  2904. struct inode *qf_inode;
  2905. unsigned long qf_flag = F2FS_QUOTA_DEFAULT_FL;
  2906. int err;
  2907. qf_inode = f2fs_iget(sb, qf_inum);
  2908. if (IS_ERR(qf_inode)) {
  2909. f2fs_err(F2FS_SB(sb), "Bad quota inode %u:%lu", type, qf_inum);
  2910. return PTR_ERR(qf_inode);
  2911. }
  2912. /* Don't account quota for quota files to avoid recursion */
  2913. inode_lock(qf_inode);
  2914. qf_inode->i_flags |= S_NOQUOTA;
  2915. if ((F2FS_I(qf_inode)->i_flags & qf_flag) != qf_flag) {
  2916. F2FS_I(qf_inode)->i_flags |= qf_flag;
  2917. f2fs_set_inode_flags(qf_inode);
  2918. }
  2919. inode_unlock(qf_inode);
  2920. err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
  2921. iput(qf_inode);
  2922. return err;
  2923. }
  2924. static int f2fs_enable_quotas(struct super_block *sb)
  2925. {
  2926. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  2927. int type, err = 0;
  2928. unsigned long qf_inum;
  2929. bool quota_mopt[MAXQUOTAS] = {
  2930. test_opt(sbi, USRQUOTA),
  2931. test_opt(sbi, GRPQUOTA),
  2932. test_opt(sbi, PRJQUOTA),
  2933. };
  2934. if (is_set_ckpt_flags(sbi, CP_QUOTA_NEED_FSCK_FLAG)) {
  2935. f2fs_err(sbi, "quota file may be corrupted, skip loading it");
  2936. return 0;
  2937. }
  2938. sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
  2939. for (type = 0; type < MAXQUOTAS; type++) {
  2940. qf_inum = f2fs_qf_ino(sb, type);
  2941. if (qf_inum) {
  2942. err = f2fs_quota_enable(sb, type, QFMT_VFS_V1,
  2943. DQUOT_USAGE_ENABLED |
  2944. (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0), qf_inum);
  2945. if (err) {
  2946. f2fs_err(sbi, "Failed to enable quota tracking (type=%d, err=%d). Please run fsck to fix.",
  2947. type, err);
  2948. for (type--; type >= 0; type--)
  2949. dquot_quota_off(sb, type);
  2950. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  2951. return err;
  2952. }
  2953. }
  2954. }
  2955. return 0;
  2956. }
  2957. static int f2fs_quota_sync_file(struct f2fs_sb_info *sbi, int type)
  2958. {
  2959. struct quota_info *dqopt = sb_dqopt(sbi->sb);
  2960. struct address_space *mapping = dqopt->files[type]->i_mapping;
  2961. int ret = 0;
  2962. ret = dquot_writeback_dquots(sbi->sb, type);
  2963. if (ret)
  2964. goto out;
  2965. ret = filemap_fdatawrite(mapping);
  2966. if (ret)
  2967. goto out;
  2968. /* if we are using journalled quota */
  2969. if (is_journalled_quota(sbi))
  2970. goto out;
  2971. ret = filemap_fdatawait(mapping);
  2972. truncate_inode_pages(&dqopt->files[type]->i_data, 0);
  2973. out:
  2974. if (ret)
  2975. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  2976. return ret;
  2977. }
  2978. int f2fs_do_quota_sync(struct super_block *sb, int type)
  2979. {
  2980. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  2981. struct quota_info *dqopt = sb_dqopt(sb);
  2982. int cnt;
  2983. int ret = 0;
  2984. /*
  2985. * Now when everything is written we can discard the pagecache so
  2986. * that userspace sees the changes.
  2987. */
  2988. for (cnt = 0; cnt < MAXQUOTAS; cnt++) {
  2989. struct f2fs_lock_context lc;
  2990. if (type != -1 && cnt != type)
  2991. continue;
  2992. if (!sb_has_quota_active(sb, cnt))
  2993. continue;
  2994. if (!f2fs_sb_has_quota_ino(sbi))
  2995. inode_lock(dqopt->files[cnt]);
  2996. /*
  2997. * do_quotactl
  2998. * f2fs_quota_sync
  2999. * f2fs_down_read(quota_sem)
  3000. * dquot_writeback_dquots()
  3001. * f2fs_dquot_commit
  3002. * block_operation
  3003. * f2fs_down_read(quota_sem)
  3004. */
  3005. f2fs_lock_op(sbi, &lc);
  3006. f2fs_down_read(&sbi->quota_sem);
  3007. ret = f2fs_quota_sync_file(sbi, cnt);
  3008. f2fs_up_read(&sbi->quota_sem);
  3009. f2fs_unlock_op(sbi, &lc);
  3010. if (!f2fs_sb_has_quota_ino(sbi))
  3011. inode_unlock(dqopt->files[cnt]);
  3012. if (ret)
  3013. break;
  3014. }
  3015. return ret;
  3016. }
  3017. static int f2fs_quota_sync(struct super_block *sb, int type)
  3018. {
  3019. int ret;
  3020. F2FS_SB(sb)->umount_lock_holder = current;
  3021. ret = f2fs_do_quota_sync(sb, type);
  3022. F2FS_SB(sb)->umount_lock_holder = NULL;
  3023. return ret;
  3024. }
  3025. static int f2fs_quota_on(struct super_block *sb, int type, int format_id,
  3026. const struct path *path)
  3027. {
  3028. struct inode *inode;
  3029. int err = 0;
  3030. /* if quota sysfile exists, deny enabling quota with specific file */
  3031. if (f2fs_sb_has_quota_ino(F2FS_SB(sb))) {
  3032. f2fs_err(F2FS_SB(sb), "quota sysfile already exists");
  3033. return -EBUSY;
  3034. }
  3035. if (path->dentry->d_sb != sb)
  3036. return -EXDEV;
  3037. F2FS_SB(sb)->umount_lock_holder = current;
  3038. err = f2fs_do_quota_sync(sb, type);
  3039. if (err)
  3040. goto out;
  3041. inode = d_inode(path->dentry);
  3042. err = filemap_fdatawrite(inode->i_mapping);
  3043. if (err)
  3044. goto out;
  3045. err = filemap_fdatawait(inode->i_mapping);
  3046. if (err)
  3047. goto out;
  3048. err = dquot_quota_on(sb, type, format_id, path);
  3049. if (err)
  3050. goto out;
  3051. inode_lock(inode);
  3052. F2FS_I(inode)->i_flags |= F2FS_QUOTA_DEFAULT_FL;
  3053. f2fs_set_inode_flags(inode);
  3054. inode_unlock(inode);
  3055. f2fs_mark_inode_dirty_sync(inode, false);
  3056. out:
  3057. F2FS_SB(sb)->umount_lock_holder = NULL;
  3058. return err;
  3059. }
  3060. static int __f2fs_quota_off(struct super_block *sb, int type)
  3061. {
  3062. struct inode *inode = sb_dqopt(sb)->files[type];
  3063. int err;
  3064. if (!inode || !igrab(inode))
  3065. return dquot_quota_off(sb, type);
  3066. err = f2fs_do_quota_sync(sb, type);
  3067. if (err)
  3068. goto out_put;
  3069. err = dquot_quota_off(sb, type);
  3070. if (err || f2fs_sb_has_quota_ino(F2FS_SB(sb)))
  3071. goto out_put;
  3072. inode_lock(inode);
  3073. F2FS_I(inode)->i_flags &= ~F2FS_QUOTA_DEFAULT_FL;
  3074. f2fs_set_inode_flags(inode);
  3075. inode_unlock(inode);
  3076. f2fs_mark_inode_dirty_sync(inode, false);
  3077. out_put:
  3078. iput(inode);
  3079. return err;
  3080. }
  3081. static int f2fs_quota_off(struct super_block *sb, int type)
  3082. {
  3083. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  3084. int err;
  3085. F2FS_SB(sb)->umount_lock_holder = current;
  3086. err = __f2fs_quota_off(sb, type);
  3087. /*
  3088. * quotactl can shutdown journalled quota, result in inconsistence
  3089. * between quota record and fs data by following updates, tag the
  3090. * flag to let fsck be aware of it.
  3091. */
  3092. if (is_journalled_quota(sbi))
  3093. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  3094. F2FS_SB(sb)->umount_lock_holder = NULL;
  3095. return err;
  3096. }
  3097. void f2fs_quota_off_umount(struct super_block *sb)
  3098. {
  3099. int type;
  3100. int err;
  3101. for (type = 0; type < MAXQUOTAS; type++) {
  3102. err = __f2fs_quota_off(sb, type);
  3103. if (err) {
  3104. int ret = dquot_quota_off(sb, type);
  3105. f2fs_err(F2FS_SB(sb), "Fail to turn off disk quota (type: %d, err: %d, ret:%d), Please run fsck to fix it.",
  3106. type, err, ret);
  3107. set_sbi_flag(F2FS_SB(sb), SBI_QUOTA_NEED_REPAIR);
  3108. }
  3109. }
  3110. /*
  3111. * In case of checkpoint=disable, we must flush quota blocks.
  3112. * This can cause NULL exception for node_inode in end_io, since
  3113. * put_super already dropped it.
  3114. */
  3115. sync_filesystem(sb);
  3116. }
  3117. static void f2fs_truncate_quota_inode_pages(struct super_block *sb)
  3118. {
  3119. struct quota_info *dqopt = sb_dqopt(sb);
  3120. int type;
  3121. for (type = 0; type < MAXQUOTAS; type++) {
  3122. if (!dqopt->files[type])
  3123. continue;
  3124. f2fs_inode_synced(dqopt->files[type]);
  3125. }
  3126. }
  3127. static int f2fs_dquot_commit(struct dquot *dquot)
  3128. {
  3129. struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
  3130. int ret;
  3131. f2fs_down_read_nested(&sbi->quota_sem, SINGLE_DEPTH_NESTING);
  3132. ret = dquot_commit(dquot);
  3133. if (ret < 0)
  3134. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  3135. f2fs_up_read(&sbi->quota_sem);
  3136. return ret;
  3137. }
  3138. static int f2fs_dquot_acquire(struct dquot *dquot)
  3139. {
  3140. struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
  3141. int ret;
  3142. f2fs_down_read(&sbi->quota_sem);
  3143. ret = dquot_acquire(dquot);
  3144. if (ret < 0)
  3145. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  3146. f2fs_up_read(&sbi->quota_sem);
  3147. return ret;
  3148. }
  3149. static int f2fs_dquot_release(struct dquot *dquot)
  3150. {
  3151. struct f2fs_sb_info *sbi = F2FS_SB(dquot->dq_sb);
  3152. int ret = dquot_release(dquot);
  3153. if (ret < 0)
  3154. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  3155. return ret;
  3156. }
  3157. static int f2fs_dquot_mark_dquot_dirty(struct dquot *dquot)
  3158. {
  3159. struct super_block *sb = dquot->dq_sb;
  3160. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  3161. int ret = dquot_mark_dquot_dirty(dquot);
  3162. /* if we are using journalled quota */
  3163. if (is_journalled_quota(sbi))
  3164. set_sbi_flag(sbi, SBI_QUOTA_NEED_FLUSH);
  3165. return ret;
  3166. }
  3167. static int f2fs_dquot_commit_info(struct super_block *sb, int type)
  3168. {
  3169. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  3170. int ret = dquot_commit_info(sb, type);
  3171. if (ret < 0)
  3172. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  3173. return ret;
  3174. }
  3175. static int f2fs_get_projid(struct inode *inode, kprojid_t *projid)
  3176. {
  3177. *projid = F2FS_I(inode)->i_projid;
  3178. return 0;
  3179. }
  3180. static const struct dquot_operations f2fs_quota_operations = {
  3181. .get_reserved_space = f2fs_get_reserved_space,
  3182. .write_dquot = f2fs_dquot_commit,
  3183. .acquire_dquot = f2fs_dquot_acquire,
  3184. .release_dquot = f2fs_dquot_release,
  3185. .mark_dirty = f2fs_dquot_mark_dquot_dirty,
  3186. .write_info = f2fs_dquot_commit_info,
  3187. .alloc_dquot = dquot_alloc,
  3188. .destroy_dquot = dquot_destroy,
  3189. .get_projid = f2fs_get_projid,
  3190. .get_next_id = dquot_get_next_id,
  3191. };
  3192. static const struct quotactl_ops f2fs_quotactl_ops = {
  3193. .quota_on = f2fs_quota_on,
  3194. .quota_off = f2fs_quota_off,
  3195. .quota_sync = f2fs_quota_sync,
  3196. .get_state = dquot_get_state,
  3197. .set_info = dquot_set_dqinfo,
  3198. .get_dqblk = dquot_get_dqblk,
  3199. .set_dqblk = dquot_set_dqblk,
  3200. .get_nextdqblk = dquot_get_next_dqblk,
  3201. };
  3202. #else
  3203. int f2fs_dquot_initialize(struct inode *inode)
  3204. {
  3205. return 0;
  3206. }
  3207. int f2fs_do_quota_sync(struct super_block *sb, int type)
  3208. {
  3209. return 0;
  3210. }
  3211. void f2fs_quota_off_umount(struct super_block *sb)
  3212. {
  3213. }
  3214. #endif
  3215. static const struct super_operations f2fs_sops = {
  3216. .alloc_inode = f2fs_alloc_inode,
  3217. .free_inode = f2fs_free_inode,
  3218. .drop_inode = f2fs_drop_inode,
  3219. .write_inode = f2fs_write_inode,
  3220. .dirty_inode = f2fs_dirty_inode,
  3221. .show_options = f2fs_show_options,
  3222. #ifdef CONFIG_QUOTA
  3223. .quota_read = f2fs_quota_read,
  3224. .quota_write = f2fs_quota_write,
  3225. .get_dquots = f2fs_get_dquots,
  3226. #endif
  3227. .evict_inode = f2fs_evict_inode,
  3228. .put_super = f2fs_put_super,
  3229. .sync_fs = f2fs_sync_fs,
  3230. .freeze_fs = f2fs_freeze,
  3231. .unfreeze_fs = f2fs_unfreeze,
  3232. .statfs = f2fs_statfs,
  3233. .shutdown = f2fs_shutdown,
  3234. };
  3235. #ifdef CONFIG_FS_ENCRYPTION
  3236. static int f2fs_get_context(struct inode *inode, void *ctx, size_t len)
  3237. {
  3238. return f2fs_getxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  3239. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  3240. ctx, len, NULL);
  3241. }
  3242. static int f2fs_set_context(struct inode *inode, const void *ctx, size_t len,
  3243. void *fs_data)
  3244. {
  3245. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  3246. /*
  3247. * Encrypting the root directory is not allowed because fsck
  3248. * expects lost+found directory to exist and remain unencrypted
  3249. * if LOST_FOUND feature is enabled.
  3250. *
  3251. */
  3252. if (f2fs_sb_has_lost_found(sbi) &&
  3253. inode->i_ino == F2FS_ROOT_INO(sbi))
  3254. return -EPERM;
  3255. return f2fs_setxattr(inode, F2FS_XATTR_INDEX_ENCRYPTION,
  3256. F2FS_XATTR_NAME_ENCRYPTION_CONTEXT,
  3257. ctx, len, fs_data, XATTR_CREATE);
  3258. }
  3259. static const union fscrypt_policy *f2fs_get_dummy_policy(struct super_block *sb)
  3260. {
  3261. return F2FS_OPTION(F2FS_SB(sb)).dummy_enc_policy.policy;
  3262. }
  3263. static bool f2fs_has_stable_inodes(struct super_block *sb)
  3264. {
  3265. return true;
  3266. }
  3267. static struct block_device **f2fs_get_devices(struct super_block *sb,
  3268. unsigned int *num_devs)
  3269. {
  3270. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  3271. struct block_device **devs;
  3272. int i;
  3273. if (!f2fs_is_multi_device(sbi))
  3274. return NULL;
  3275. devs = kmalloc_objs(*devs, sbi->s_ndevs);
  3276. if (!devs)
  3277. return ERR_PTR(-ENOMEM);
  3278. for (i = 0; i < sbi->s_ndevs; i++)
  3279. devs[i] = FDEV(i).bdev;
  3280. *num_devs = sbi->s_ndevs;
  3281. return devs;
  3282. }
  3283. static const struct fscrypt_operations f2fs_cryptops = {
  3284. .inode_info_offs = (int)offsetof(struct f2fs_inode_info, i_crypt_info) -
  3285. (int)offsetof(struct f2fs_inode_info, vfs_inode),
  3286. .needs_bounce_pages = 1,
  3287. .has_32bit_inodes = 1,
  3288. .supports_subblock_data_units = 1,
  3289. .legacy_key_prefix = "f2fs:",
  3290. .get_context = f2fs_get_context,
  3291. .set_context = f2fs_set_context,
  3292. .get_dummy_policy = f2fs_get_dummy_policy,
  3293. .empty_dir = f2fs_empty_dir,
  3294. .has_stable_inodes = f2fs_has_stable_inodes,
  3295. .get_devices = f2fs_get_devices,
  3296. };
  3297. #endif /* CONFIG_FS_ENCRYPTION */
  3298. static struct inode *f2fs_nfs_get_inode(struct super_block *sb,
  3299. u64 ino, u32 generation)
  3300. {
  3301. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  3302. struct inode *inode;
  3303. if (f2fs_check_nid_range(sbi, ino))
  3304. return ERR_PTR(-ESTALE);
  3305. /*
  3306. * f2fs_iget isn't quite right if the inode is currently unallocated!
  3307. * However f2fs_iget currently does appropriate checks to handle stale
  3308. * inodes so everything is OK.
  3309. */
  3310. inode = f2fs_iget(sb, ino);
  3311. if (IS_ERR(inode))
  3312. return ERR_CAST(inode);
  3313. if (unlikely(generation && inode->i_generation != generation)) {
  3314. /* we didn't find the right inode.. */
  3315. iput(inode);
  3316. return ERR_PTR(-ESTALE);
  3317. }
  3318. return inode;
  3319. }
  3320. static struct dentry *f2fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
  3321. int fh_len, int fh_type)
  3322. {
  3323. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  3324. f2fs_nfs_get_inode);
  3325. }
  3326. static struct dentry *f2fs_fh_to_parent(struct super_block *sb, struct fid *fid,
  3327. int fh_len, int fh_type)
  3328. {
  3329. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  3330. f2fs_nfs_get_inode);
  3331. }
  3332. static const struct export_operations f2fs_export_ops = {
  3333. .encode_fh = generic_encode_ino32_fh,
  3334. .fh_to_dentry = f2fs_fh_to_dentry,
  3335. .fh_to_parent = f2fs_fh_to_parent,
  3336. .get_parent = f2fs_get_parent,
  3337. };
  3338. loff_t max_file_blocks(struct inode *inode)
  3339. {
  3340. loff_t result = 0;
  3341. loff_t leaf_count;
  3342. /*
  3343. * note: previously, result is equal to (DEF_ADDRS_PER_INODE -
  3344. * DEFAULT_INLINE_XATTR_ADDRS), but now f2fs try to reserve more
  3345. * space in inode.i_addr, it will be more safe to reassign
  3346. * result as zero.
  3347. */
  3348. if (inode && f2fs_compressed_file(inode))
  3349. leaf_count = ADDRS_PER_BLOCK(inode);
  3350. else
  3351. leaf_count = DEF_ADDRS_PER_BLOCK;
  3352. /* two direct node blocks */
  3353. result += (leaf_count * 2);
  3354. /* two indirect node blocks */
  3355. leaf_count *= NIDS_PER_BLOCK;
  3356. result += (leaf_count * 2);
  3357. /* one double indirect node block */
  3358. leaf_count *= NIDS_PER_BLOCK;
  3359. result += leaf_count;
  3360. /*
  3361. * For compatibility with FSCRYPT_POLICY_FLAG_IV_INO_LBLK_{64,32} with
  3362. * a 4K crypto data unit, we must restrict the max filesize to what can
  3363. * fit within U32_MAX + 1 data units.
  3364. */
  3365. result = umin(result, F2FS_BYTES_TO_BLK(((loff_t)U32_MAX + 1) * 4096));
  3366. return result;
  3367. }
  3368. static int __f2fs_commit_super(struct f2fs_sb_info *sbi, struct folio *folio,
  3369. pgoff_t index, bool update)
  3370. {
  3371. struct bio *bio;
  3372. /* it's rare case, we can do fua all the time */
  3373. blk_opf_t opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH | REQ_FUA;
  3374. int ret;
  3375. folio_lock(folio);
  3376. folio_wait_writeback(folio);
  3377. if (update)
  3378. memcpy(F2FS_SUPER_BLOCK(folio, index), F2FS_RAW_SUPER(sbi),
  3379. sizeof(struct f2fs_super_block));
  3380. folio_mark_dirty(folio);
  3381. folio_clear_dirty_for_io(folio);
  3382. folio_start_writeback(folio);
  3383. folio_unlock(folio);
  3384. bio = bio_alloc(sbi->sb->s_bdev, 1, opf, GFP_NOFS);
  3385. /* it doesn't need to set crypto context for superblock update */
  3386. bio->bi_iter.bi_sector = SECTOR_FROM_BLOCK(folio->index);
  3387. if (!bio_add_folio(bio, folio, folio_size(folio), 0))
  3388. f2fs_bug_on(sbi, 1);
  3389. ret = submit_bio_wait(bio);
  3390. bio_put(bio);
  3391. folio_end_writeback(folio);
  3392. return ret;
  3393. }
  3394. static inline bool sanity_check_area_boundary(struct f2fs_sb_info *sbi,
  3395. struct folio *folio, pgoff_t index)
  3396. {
  3397. struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index);
  3398. struct super_block *sb = sbi->sb;
  3399. u32 segment0_blkaddr = le32_to_cpu(raw_super->segment0_blkaddr);
  3400. u32 cp_blkaddr = le32_to_cpu(raw_super->cp_blkaddr);
  3401. u32 sit_blkaddr = le32_to_cpu(raw_super->sit_blkaddr);
  3402. u32 nat_blkaddr = le32_to_cpu(raw_super->nat_blkaddr);
  3403. u32 ssa_blkaddr = le32_to_cpu(raw_super->ssa_blkaddr);
  3404. u32 main_blkaddr = le32_to_cpu(raw_super->main_blkaddr);
  3405. u32 segment_count_ckpt = le32_to_cpu(raw_super->segment_count_ckpt);
  3406. u32 segment_count_sit = le32_to_cpu(raw_super->segment_count_sit);
  3407. u32 segment_count_nat = le32_to_cpu(raw_super->segment_count_nat);
  3408. u32 segment_count_ssa = le32_to_cpu(raw_super->segment_count_ssa);
  3409. u32 segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  3410. u32 segment_count = le32_to_cpu(raw_super->segment_count);
  3411. u32 log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  3412. u64 main_end_blkaddr = main_blkaddr +
  3413. ((u64)segment_count_main << log_blocks_per_seg);
  3414. u64 seg_end_blkaddr = segment0_blkaddr +
  3415. ((u64)segment_count << log_blocks_per_seg);
  3416. if (segment0_blkaddr != cp_blkaddr) {
  3417. f2fs_info(sbi, "Mismatch start address, segment0(%u) cp_blkaddr(%u)",
  3418. segment0_blkaddr, cp_blkaddr);
  3419. return true;
  3420. }
  3421. if (cp_blkaddr + (segment_count_ckpt << log_blocks_per_seg) !=
  3422. sit_blkaddr) {
  3423. f2fs_info(sbi, "Wrong CP boundary, start(%u) end(%u) blocks(%u)",
  3424. cp_blkaddr, sit_blkaddr,
  3425. segment_count_ckpt << log_blocks_per_seg);
  3426. return true;
  3427. }
  3428. if (sit_blkaddr + (segment_count_sit << log_blocks_per_seg) !=
  3429. nat_blkaddr) {
  3430. f2fs_info(sbi, "Wrong SIT boundary, start(%u) end(%u) blocks(%u)",
  3431. sit_blkaddr, nat_blkaddr,
  3432. segment_count_sit << log_blocks_per_seg);
  3433. return true;
  3434. }
  3435. if (nat_blkaddr + (segment_count_nat << log_blocks_per_seg) !=
  3436. ssa_blkaddr) {
  3437. f2fs_info(sbi, "Wrong NAT boundary, start(%u) end(%u) blocks(%u)",
  3438. nat_blkaddr, ssa_blkaddr,
  3439. segment_count_nat << log_blocks_per_seg);
  3440. return true;
  3441. }
  3442. if (ssa_blkaddr + (segment_count_ssa << log_blocks_per_seg) !=
  3443. main_blkaddr) {
  3444. f2fs_info(sbi, "Wrong SSA boundary, start(%u) end(%u) blocks(%u)",
  3445. ssa_blkaddr, main_blkaddr,
  3446. segment_count_ssa << log_blocks_per_seg);
  3447. return true;
  3448. }
  3449. if (main_end_blkaddr > seg_end_blkaddr) {
  3450. f2fs_info(sbi, "Wrong MAIN_AREA boundary, start(%u) end(%llu) block(%u)",
  3451. main_blkaddr, seg_end_blkaddr,
  3452. segment_count_main << log_blocks_per_seg);
  3453. return true;
  3454. } else if (main_end_blkaddr < seg_end_blkaddr) {
  3455. int err = 0;
  3456. char *res;
  3457. /* fix in-memory information all the time */
  3458. raw_super->segment_count = cpu_to_le32((main_end_blkaddr -
  3459. segment0_blkaddr) >> log_blocks_per_seg);
  3460. if (f2fs_readonly(sb) || f2fs_hw_is_readonly(sbi)) {
  3461. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  3462. res = "internally";
  3463. } else {
  3464. err = __f2fs_commit_super(sbi, folio, index, false);
  3465. res = err ? "failed" : "done";
  3466. }
  3467. f2fs_info(sbi, "Fix alignment : %s, start(%u) end(%llu) block(%u)",
  3468. res, main_blkaddr, seg_end_blkaddr,
  3469. segment_count_main << log_blocks_per_seg);
  3470. if (err)
  3471. return true;
  3472. }
  3473. return false;
  3474. }
  3475. static int sanity_check_raw_super(struct f2fs_sb_info *sbi,
  3476. struct folio *folio, pgoff_t index)
  3477. {
  3478. block_t segment_count, segs_per_sec, secs_per_zone, segment_count_main;
  3479. block_t total_sections, blocks_per_seg;
  3480. struct f2fs_super_block *raw_super = F2FS_SUPER_BLOCK(folio, index);
  3481. size_t crc_offset = 0;
  3482. __u32 crc = 0;
  3483. if (le32_to_cpu(raw_super->magic) != F2FS_SUPER_MAGIC) {
  3484. f2fs_info(sbi, "Magic Mismatch, valid(0x%x) - read(0x%x)",
  3485. F2FS_SUPER_MAGIC, le32_to_cpu(raw_super->magic));
  3486. return -EINVAL;
  3487. }
  3488. /* Check checksum_offset and crc in superblock */
  3489. if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_SB_CHKSUM)) {
  3490. crc_offset = le32_to_cpu(raw_super->checksum_offset);
  3491. if (crc_offset !=
  3492. offsetof(struct f2fs_super_block, crc)) {
  3493. f2fs_info(sbi, "Invalid SB checksum offset: %zu",
  3494. crc_offset);
  3495. return -EFSCORRUPTED;
  3496. }
  3497. crc = le32_to_cpu(raw_super->crc);
  3498. if (crc != f2fs_crc32(raw_super, crc_offset)) {
  3499. f2fs_info(sbi, "Invalid SB checksum value: %u", crc);
  3500. return -EFSCORRUPTED;
  3501. }
  3502. }
  3503. /* only support block_size equals to PAGE_SIZE */
  3504. if (le32_to_cpu(raw_super->log_blocksize) != F2FS_BLKSIZE_BITS) {
  3505. f2fs_info(sbi, "Invalid log_blocksize (%u), supports only %u",
  3506. le32_to_cpu(raw_super->log_blocksize),
  3507. F2FS_BLKSIZE_BITS);
  3508. return -EFSCORRUPTED;
  3509. }
  3510. /* check log blocks per segment */
  3511. if (le32_to_cpu(raw_super->log_blocks_per_seg) != 9) {
  3512. f2fs_info(sbi, "Invalid log blocks per segment (%u)",
  3513. le32_to_cpu(raw_super->log_blocks_per_seg));
  3514. return -EFSCORRUPTED;
  3515. }
  3516. /* Currently, support 512/1024/2048/4096/16K bytes sector size */
  3517. if (le32_to_cpu(raw_super->log_sectorsize) >
  3518. F2FS_MAX_LOG_SECTOR_SIZE ||
  3519. le32_to_cpu(raw_super->log_sectorsize) <
  3520. F2FS_MIN_LOG_SECTOR_SIZE) {
  3521. f2fs_info(sbi, "Invalid log sectorsize (%u)",
  3522. le32_to_cpu(raw_super->log_sectorsize));
  3523. return -EFSCORRUPTED;
  3524. }
  3525. if (le32_to_cpu(raw_super->log_sectors_per_block) +
  3526. le32_to_cpu(raw_super->log_sectorsize) !=
  3527. F2FS_MAX_LOG_SECTOR_SIZE) {
  3528. f2fs_info(sbi, "Invalid log sectors per block(%u) log sectorsize(%u)",
  3529. le32_to_cpu(raw_super->log_sectors_per_block),
  3530. le32_to_cpu(raw_super->log_sectorsize));
  3531. return -EFSCORRUPTED;
  3532. }
  3533. segment_count = le32_to_cpu(raw_super->segment_count);
  3534. segment_count_main = le32_to_cpu(raw_super->segment_count_main);
  3535. segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  3536. secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  3537. total_sections = le32_to_cpu(raw_super->section_count);
  3538. /* blocks_per_seg should be 512, given the above check */
  3539. blocks_per_seg = BIT(le32_to_cpu(raw_super->log_blocks_per_seg));
  3540. if (segment_count > F2FS_MAX_SEGMENT ||
  3541. segment_count < F2FS_MIN_SEGMENTS) {
  3542. f2fs_info(sbi, "Invalid segment count (%u)", segment_count);
  3543. return -EFSCORRUPTED;
  3544. }
  3545. if (total_sections > segment_count_main || total_sections < 1 ||
  3546. segs_per_sec > segment_count || !segs_per_sec) {
  3547. f2fs_info(sbi, "Invalid segment/section count (%u, %u x %u)",
  3548. segment_count, total_sections, segs_per_sec);
  3549. return -EFSCORRUPTED;
  3550. }
  3551. if (segment_count_main != total_sections * segs_per_sec) {
  3552. f2fs_info(sbi, "Invalid segment/section count (%u != %u * %u)",
  3553. segment_count_main, total_sections, segs_per_sec);
  3554. return -EFSCORRUPTED;
  3555. }
  3556. if ((segment_count / segs_per_sec) < total_sections) {
  3557. f2fs_info(sbi, "Small segment_count (%u < %u * %u)",
  3558. segment_count, segs_per_sec, total_sections);
  3559. return -EFSCORRUPTED;
  3560. }
  3561. if (segment_count > (le64_to_cpu(raw_super->block_count) >> 9)) {
  3562. f2fs_info(sbi, "Wrong segment_count / block_count (%u > %llu)",
  3563. segment_count, le64_to_cpu(raw_super->block_count));
  3564. return -EFSCORRUPTED;
  3565. }
  3566. if (RDEV(0).path[0]) {
  3567. block_t dev_seg_count = le32_to_cpu(RDEV(0).total_segments);
  3568. int i = 1;
  3569. while (i < MAX_DEVICES && RDEV(i).path[0]) {
  3570. dev_seg_count += le32_to_cpu(RDEV(i).total_segments);
  3571. i++;
  3572. }
  3573. if (segment_count != dev_seg_count) {
  3574. f2fs_info(sbi, "Segment count (%u) mismatch with total segments from devices (%u)",
  3575. segment_count, dev_seg_count);
  3576. return -EFSCORRUPTED;
  3577. }
  3578. } else {
  3579. if (__F2FS_HAS_FEATURE(raw_super, F2FS_FEATURE_BLKZONED) &&
  3580. !bdev_is_zoned(sbi->sb->s_bdev)) {
  3581. f2fs_info(sbi, "Zoned block device path is missing");
  3582. return -EFSCORRUPTED;
  3583. }
  3584. }
  3585. if (secs_per_zone > total_sections || !secs_per_zone) {
  3586. f2fs_info(sbi, "Wrong secs_per_zone / total_sections (%u, %u)",
  3587. secs_per_zone, total_sections);
  3588. return -EFSCORRUPTED;
  3589. }
  3590. if (le32_to_cpu(raw_super->extension_count) > F2FS_MAX_EXTENSION ||
  3591. raw_super->hot_ext_count > F2FS_MAX_EXTENSION ||
  3592. (le32_to_cpu(raw_super->extension_count) +
  3593. raw_super->hot_ext_count) > F2FS_MAX_EXTENSION) {
  3594. f2fs_info(sbi, "Corrupted extension count (%u + %u > %u)",
  3595. le32_to_cpu(raw_super->extension_count),
  3596. raw_super->hot_ext_count,
  3597. F2FS_MAX_EXTENSION);
  3598. return -EFSCORRUPTED;
  3599. }
  3600. if (le32_to_cpu(raw_super->cp_payload) >=
  3601. (blocks_per_seg - F2FS_CP_PACKS -
  3602. NR_CURSEG_PERSIST_TYPE)) {
  3603. f2fs_info(sbi, "Insane cp_payload (%u >= %u)",
  3604. le32_to_cpu(raw_super->cp_payload),
  3605. blocks_per_seg - F2FS_CP_PACKS -
  3606. NR_CURSEG_PERSIST_TYPE);
  3607. return -EFSCORRUPTED;
  3608. }
  3609. /* check reserved ino info */
  3610. if (le32_to_cpu(raw_super->node_ino) != 1 ||
  3611. le32_to_cpu(raw_super->meta_ino) != 2 ||
  3612. le32_to_cpu(raw_super->root_ino) != 3) {
  3613. f2fs_info(sbi, "Invalid Fs Meta Ino: node(%u) meta(%u) root(%u)",
  3614. le32_to_cpu(raw_super->node_ino),
  3615. le32_to_cpu(raw_super->meta_ino),
  3616. le32_to_cpu(raw_super->root_ino));
  3617. return -EFSCORRUPTED;
  3618. }
  3619. /* check CP/SIT/NAT/SSA/MAIN_AREA area boundary */
  3620. if (sanity_check_area_boundary(sbi, folio, index))
  3621. return -EFSCORRUPTED;
  3622. return 0;
  3623. }
  3624. int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi)
  3625. {
  3626. unsigned int total, fsmeta;
  3627. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  3628. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  3629. unsigned int ovp_segments, reserved_segments;
  3630. unsigned int main_segs, blocks_per_seg;
  3631. unsigned int sit_segs, nat_segs;
  3632. unsigned int sit_bitmap_size, nat_bitmap_size;
  3633. unsigned int log_blocks_per_seg;
  3634. unsigned int segment_count_main;
  3635. unsigned int cp_pack_start_sum, cp_payload;
  3636. block_t user_block_count, valid_user_blocks;
  3637. block_t avail_node_count, valid_node_count;
  3638. unsigned int nat_blocks, nat_bits_bytes, nat_bits_blocks;
  3639. unsigned int sit_blk_cnt;
  3640. int i, j;
  3641. total = le32_to_cpu(raw_super->segment_count);
  3642. fsmeta = le32_to_cpu(raw_super->segment_count_ckpt);
  3643. sit_segs = le32_to_cpu(raw_super->segment_count_sit);
  3644. fsmeta += sit_segs;
  3645. nat_segs = le32_to_cpu(raw_super->segment_count_nat);
  3646. fsmeta += nat_segs;
  3647. fsmeta += le32_to_cpu(ckpt->rsvd_segment_count);
  3648. fsmeta += le32_to_cpu(raw_super->segment_count_ssa);
  3649. if (unlikely(fsmeta >= total))
  3650. return 1;
  3651. ovp_segments = le32_to_cpu(ckpt->overprov_segment_count);
  3652. reserved_segments = le32_to_cpu(ckpt->rsvd_segment_count);
  3653. if (!f2fs_sb_has_readonly(sbi) &&
  3654. unlikely(fsmeta < F2FS_MIN_META_SEGMENTS ||
  3655. ovp_segments == 0 || reserved_segments == 0)) {
  3656. f2fs_err(sbi, "Wrong layout: check mkfs.f2fs version");
  3657. return 1;
  3658. }
  3659. user_block_count = le64_to_cpu(ckpt->user_block_count);
  3660. segment_count_main = le32_to_cpu(raw_super->segment_count_main) +
  3661. (f2fs_sb_has_readonly(sbi) ? 1 : 0);
  3662. log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  3663. if (!user_block_count || user_block_count >=
  3664. segment_count_main << log_blocks_per_seg) {
  3665. f2fs_err(sbi, "Wrong user_block_count: %u",
  3666. user_block_count);
  3667. return 1;
  3668. }
  3669. valid_user_blocks = le64_to_cpu(ckpt->valid_block_count);
  3670. if (valid_user_blocks > user_block_count) {
  3671. f2fs_err(sbi, "Wrong valid_user_blocks: %u, user_block_count: %u",
  3672. valid_user_blocks, user_block_count);
  3673. return 1;
  3674. }
  3675. valid_node_count = le32_to_cpu(ckpt->valid_node_count);
  3676. avail_node_count = sbi->total_node_count - F2FS_RESERVED_NODE_NUM;
  3677. if (valid_node_count > avail_node_count) {
  3678. f2fs_err(sbi, "Wrong valid_node_count: %u, avail_node_count: %u",
  3679. valid_node_count, avail_node_count);
  3680. return 1;
  3681. }
  3682. main_segs = le32_to_cpu(raw_super->segment_count_main);
  3683. blocks_per_seg = BLKS_PER_SEG(sbi);
  3684. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  3685. if (le32_to_cpu(ckpt->cur_node_segno[i]) >= main_segs ||
  3686. le16_to_cpu(ckpt->cur_node_blkoff[i]) >= blocks_per_seg)
  3687. return 1;
  3688. if (f2fs_sb_has_readonly(sbi))
  3689. goto check_data;
  3690. for (j = i + 1; j < NR_CURSEG_NODE_TYPE; j++) {
  3691. if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
  3692. le32_to_cpu(ckpt->cur_node_segno[j])) {
  3693. f2fs_err(sbi, "Node segment (%u, %u) has the same segno: %u",
  3694. i, j,
  3695. le32_to_cpu(ckpt->cur_node_segno[i]));
  3696. return 1;
  3697. }
  3698. }
  3699. }
  3700. check_data:
  3701. for (i = 0; i < NR_CURSEG_DATA_TYPE; i++) {
  3702. if (le32_to_cpu(ckpt->cur_data_segno[i]) >= main_segs ||
  3703. le16_to_cpu(ckpt->cur_data_blkoff[i]) >= blocks_per_seg)
  3704. return 1;
  3705. if (f2fs_sb_has_readonly(sbi))
  3706. goto skip_cross;
  3707. for (j = i + 1; j < NR_CURSEG_DATA_TYPE; j++) {
  3708. if (le32_to_cpu(ckpt->cur_data_segno[i]) ==
  3709. le32_to_cpu(ckpt->cur_data_segno[j])) {
  3710. f2fs_err(sbi, "Data segment (%u, %u) has the same segno: %u",
  3711. i, j,
  3712. le32_to_cpu(ckpt->cur_data_segno[i]));
  3713. return 1;
  3714. }
  3715. }
  3716. }
  3717. for (i = 0; i < NR_CURSEG_NODE_TYPE; i++) {
  3718. for (j = 0; j < NR_CURSEG_DATA_TYPE; j++) {
  3719. if (le32_to_cpu(ckpt->cur_node_segno[i]) ==
  3720. le32_to_cpu(ckpt->cur_data_segno[j])) {
  3721. f2fs_err(sbi, "Node segment (%u) and Data segment (%u) has the same segno: %u",
  3722. i, j,
  3723. le32_to_cpu(ckpt->cur_node_segno[i]));
  3724. return 1;
  3725. }
  3726. }
  3727. }
  3728. skip_cross:
  3729. sit_bitmap_size = le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  3730. nat_bitmap_size = le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  3731. if (sit_bitmap_size != ((sit_segs / 2) << log_blocks_per_seg) / 8 ||
  3732. nat_bitmap_size != ((nat_segs / 2) << log_blocks_per_seg) / 8) {
  3733. f2fs_err(sbi, "Wrong bitmap size: sit: %u, nat:%u",
  3734. sit_bitmap_size, nat_bitmap_size);
  3735. return 1;
  3736. }
  3737. sit_blk_cnt = DIV_ROUND_UP(main_segs, SIT_ENTRY_PER_BLOCK);
  3738. if (sit_bitmap_size * 8 < sit_blk_cnt) {
  3739. f2fs_err(sbi, "Wrong bitmap size: sit: %u, sit_blk_cnt:%u",
  3740. sit_bitmap_size, sit_blk_cnt);
  3741. return 1;
  3742. }
  3743. cp_pack_start_sum = __start_sum_addr(sbi);
  3744. cp_payload = __cp_payload(sbi);
  3745. if (cp_pack_start_sum < cp_payload + 1 ||
  3746. cp_pack_start_sum > blocks_per_seg - 1 -
  3747. NR_CURSEG_PERSIST_TYPE) {
  3748. f2fs_err(sbi, "Wrong cp_pack_start_sum: %u",
  3749. cp_pack_start_sum);
  3750. return 1;
  3751. }
  3752. if (__is_set_ckpt_flags(ckpt, CP_LARGE_NAT_BITMAP_FLAG) &&
  3753. le32_to_cpu(ckpt->checksum_offset) != CP_MIN_CHKSUM_OFFSET) {
  3754. f2fs_warn(sbi, "using deprecated layout of large_nat_bitmap, "
  3755. "please run fsck v1.13.0 or higher to repair, chksum_offset: %u, "
  3756. "fixed with patch: \"f2fs-tools: relocate chksum_offset for large_nat_bitmap feature\"",
  3757. le32_to_cpu(ckpt->checksum_offset));
  3758. return 1;
  3759. }
  3760. nat_blocks = nat_segs << log_blocks_per_seg;
  3761. nat_bits_bytes = nat_blocks / BITS_PER_BYTE;
  3762. nat_bits_blocks = F2FS_BLK_ALIGN((nat_bits_bytes << 1) + 8);
  3763. if (__is_set_ckpt_flags(ckpt, CP_NAT_BITS_FLAG) &&
  3764. (cp_payload + F2FS_CP_PACKS +
  3765. NR_CURSEG_PERSIST_TYPE + nat_bits_blocks >= blocks_per_seg)) {
  3766. f2fs_warn(sbi, "Insane cp_payload: %u, nat_bits_blocks: %u)",
  3767. cp_payload, nat_bits_blocks);
  3768. return 1;
  3769. }
  3770. if (unlikely(f2fs_cp_error(sbi))) {
  3771. f2fs_err(sbi, "A bug case: need to run fsck");
  3772. return 1;
  3773. }
  3774. return 0;
  3775. }
  3776. static void init_sb_info(struct f2fs_sb_info *sbi)
  3777. {
  3778. struct f2fs_super_block *raw_super = sbi->raw_super;
  3779. int i;
  3780. sbi->log_sectors_per_block =
  3781. le32_to_cpu(raw_super->log_sectors_per_block);
  3782. sbi->log_blocksize = le32_to_cpu(raw_super->log_blocksize);
  3783. sbi->blocksize = BIT(sbi->log_blocksize);
  3784. sbi->log_blocks_per_seg = le32_to_cpu(raw_super->log_blocks_per_seg);
  3785. sbi->blocks_per_seg = BIT(sbi->log_blocks_per_seg);
  3786. sbi->segs_per_sec = le32_to_cpu(raw_super->segs_per_sec);
  3787. sbi->secs_per_zone = le32_to_cpu(raw_super->secs_per_zone);
  3788. sbi->total_sections = le32_to_cpu(raw_super->section_count);
  3789. sbi->total_node_count = SEGS_TO_BLKS(sbi,
  3790. ((le32_to_cpu(raw_super->segment_count_nat) / 2) *
  3791. NAT_ENTRY_PER_BLOCK));
  3792. sbi->allocate_section_hint = le32_to_cpu(raw_super->section_count);
  3793. sbi->allocate_section_policy = ALLOCATE_FORWARD_NOHINT;
  3794. F2FS_ROOT_INO(sbi) = le32_to_cpu(raw_super->root_ino);
  3795. F2FS_NODE_INO(sbi) = le32_to_cpu(raw_super->node_ino);
  3796. F2FS_META_INO(sbi) = le32_to_cpu(raw_super->meta_ino);
  3797. sbi->cur_victim_sec = NULL_SECNO;
  3798. sbi->gc_mode = GC_NORMAL;
  3799. sbi->next_victim_seg[BG_GC] = NULL_SEGNO;
  3800. sbi->next_victim_seg[FG_GC] = NULL_SEGNO;
  3801. sbi->max_victim_search = DEF_MAX_VICTIM_SEARCH;
  3802. sbi->migration_granularity = SEGS_PER_SEC(sbi);
  3803. sbi->migration_window_granularity = f2fs_sb_has_blkzoned(sbi) ?
  3804. DEF_MIGRATION_WINDOW_GRANULARITY_ZONED : SEGS_PER_SEC(sbi);
  3805. sbi->seq_file_ra_mul = MIN_RA_MUL;
  3806. sbi->max_fragment_chunk = DEF_FRAGMENT_SIZE;
  3807. sbi->max_fragment_hole = DEF_FRAGMENT_SIZE;
  3808. spin_lock_init(&sbi->gc_remaining_trials_lock);
  3809. atomic64_set(&sbi->current_atomic_write, 0);
  3810. sbi->max_lock_elapsed_time = MAX_LOCK_ELAPSED_TIME;
  3811. sbi->adjust_lock_priority = 0;
  3812. sbi->lock_duration_priority = F2FS_DEFAULT_TASK_PRIORITY;
  3813. sbi->critical_task_priority = F2FS_CRITICAL_TASK_PRIORITY;
  3814. sbi->sum_blocksize = f2fs_sb_has_packed_ssa(sbi) ?
  3815. 4096 : sbi->blocksize;
  3816. sbi->sums_per_block = sbi->blocksize / sbi->sum_blocksize;
  3817. sbi->entries_in_sum = sbi->sum_blocksize / 8;
  3818. sbi->sum_entry_size = SUMMARY_SIZE * sbi->entries_in_sum;
  3819. sbi->sum_journal_size = sbi->sum_blocksize - SUM_FOOTER_SIZE -
  3820. sbi->sum_entry_size;
  3821. sbi->nat_journal_entries = (sbi->sum_journal_size - 2) /
  3822. sizeof(struct nat_journal_entry);
  3823. sbi->sit_journal_entries = (sbi->sum_journal_size - 2) /
  3824. sizeof(struct sit_journal_entry);
  3825. sbi->dir_level = DEF_DIR_LEVEL;
  3826. sbi->interval_time[CP_TIME] = DEF_CP_INTERVAL;
  3827. sbi->interval_time[REQ_TIME] = DEF_IDLE_INTERVAL;
  3828. sbi->interval_time[DISCARD_TIME] = DEF_IDLE_INTERVAL;
  3829. sbi->interval_time[GC_TIME] = DEF_IDLE_INTERVAL;
  3830. sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_INTERVAL;
  3831. sbi->interval_time[UMOUNT_DISCARD_TIMEOUT] =
  3832. DEF_UMOUNT_DISCARD_TIMEOUT;
  3833. clear_sbi_flag(sbi, SBI_NEED_FSCK);
  3834. for (i = 0; i < NR_COUNT_TYPE; i++)
  3835. atomic_set(&sbi->nr_pages[i], 0);
  3836. for (i = 0; i < META; i++)
  3837. atomic_set(&sbi->wb_sync_req[i], 0);
  3838. INIT_LIST_HEAD(&sbi->s_list);
  3839. mutex_init(&sbi->umount_mutex);
  3840. init_f2fs_rwsem(&sbi->io_order_lock);
  3841. spin_lock_init(&sbi->cp_lock);
  3842. sbi->dirty_device = 0;
  3843. spin_lock_init(&sbi->dev_lock);
  3844. init_f2fs_rwsem(&sbi->sb_lock);
  3845. init_f2fs_rwsem(&sbi->pin_sem);
  3846. }
  3847. static int init_percpu_info(struct f2fs_sb_info *sbi)
  3848. {
  3849. int err;
  3850. err = percpu_counter_init(&sbi->alloc_valid_block_count, 0, GFP_KERNEL);
  3851. if (err)
  3852. return err;
  3853. err = percpu_counter_init(&sbi->rf_node_block_count, 0, GFP_KERNEL);
  3854. if (err)
  3855. goto err_valid_block;
  3856. err = percpu_counter_init(&sbi->total_valid_inode_count, 0,
  3857. GFP_KERNEL);
  3858. if (err)
  3859. goto err_node_block;
  3860. return 0;
  3861. err_node_block:
  3862. percpu_counter_destroy(&sbi->rf_node_block_count);
  3863. err_valid_block:
  3864. percpu_counter_destroy(&sbi->alloc_valid_block_count);
  3865. return err;
  3866. }
  3867. #ifdef CONFIG_BLK_DEV_ZONED
  3868. struct f2fs_report_zones_args {
  3869. struct f2fs_sb_info *sbi;
  3870. struct f2fs_dev_info *dev;
  3871. };
  3872. static int f2fs_report_zone_cb(struct blk_zone *zone, unsigned int idx,
  3873. void *data)
  3874. {
  3875. struct f2fs_report_zones_args *rz_args = data;
  3876. block_t unusable_blocks = (zone->len - zone->capacity) >>
  3877. F2FS_LOG_SECTORS_PER_BLOCK;
  3878. if (zone->type == BLK_ZONE_TYPE_CONVENTIONAL)
  3879. return 0;
  3880. set_bit(idx, rz_args->dev->blkz_seq);
  3881. if (!rz_args->sbi->unusable_blocks_per_sec) {
  3882. rz_args->sbi->unusable_blocks_per_sec = unusable_blocks;
  3883. return 0;
  3884. }
  3885. if (rz_args->sbi->unusable_blocks_per_sec != unusable_blocks) {
  3886. f2fs_err(rz_args->sbi, "F2FS supports single zone capacity\n");
  3887. return -EINVAL;
  3888. }
  3889. return 0;
  3890. }
  3891. static int init_blkz_info(struct f2fs_sb_info *sbi, int devi)
  3892. {
  3893. struct block_device *bdev = FDEV(devi).bdev;
  3894. sector_t nr_sectors = bdev_nr_sectors(bdev);
  3895. struct f2fs_report_zones_args rep_zone_arg;
  3896. u64 zone_sectors;
  3897. unsigned int max_open_zones;
  3898. int ret;
  3899. if (!f2fs_sb_has_blkzoned(sbi))
  3900. return 0;
  3901. if (bdev_is_zoned(FDEV(devi).bdev)) {
  3902. max_open_zones = bdev_max_open_zones(bdev);
  3903. if (max_open_zones && (max_open_zones < sbi->max_open_zones))
  3904. sbi->max_open_zones = max_open_zones;
  3905. if (sbi->max_open_zones < F2FS_OPTION(sbi).active_logs) {
  3906. f2fs_err(sbi,
  3907. "zoned: max open zones %u is too small, need at least %u open zones",
  3908. sbi->max_open_zones, F2FS_OPTION(sbi).active_logs);
  3909. return -EINVAL;
  3910. }
  3911. }
  3912. zone_sectors = bdev_zone_sectors(bdev);
  3913. if (sbi->blocks_per_blkz && sbi->blocks_per_blkz !=
  3914. SECTOR_TO_BLOCK(zone_sectors))
  3915. return -EINVAL;
  3916. sbi->blocks_per_blkz = SECTOR_TO_BLOCK(zone_sectors);
  3917. FDEV(devi).nr_blkz = div_u64(SECTOR_TO_BLOCK(nr_sectors),
  3918. sbi->blocks_per_blkz);
  3919. if (nr_sectors & (zone_sectors - 1))
  3920. FDEV(devi).nr_blkz++;
  3921. FDEV(devi).blkz_seq = f2fs_kvzalloc(sbi,
  3922. BITS_TO_LONGS(FDEV(devi).nr_blkz)
  3923. * sizeof(unsigned long),
  3924. GFP_KERNEL);
  3925. if (!FDEV(devi).blkz_seq)
  3926. return -ENOMEM;
  3927. rep_zone_arg.sbi = sbi;
  3928. rep_zone_arg.dev = &FDEV(devi);
  3929. ret = blkdev_report_zones(bdev, 0, BLK_ALL_ZONES, f2fs_report_zone_cb,
  3930. &rep_zone_arg);
  3931. if (ret < 0)
  3932. return ret;
  3933. return 0;
  3934. }
  3935. #endif
  3936. /*
  3937. * Read f2fs raw super block.
  3938. * Because we have two copies of super block, so read both of them
  3939. * to get the first valid one. If any one of them is broken, we pass
  3940. * them recovery flag back to the caller.
  3941. */
  3942. static int read_raw_super_block(struct f2fs_sb_info *sbi,
  3943. struct f2fs_super_block **raw_super,
  3944. int *valid_super_block, int *recovery)
  3945. {
  3946. struct super_block *sb = sbi->sb;
  3947. int block;
  3948. struct folio *folio;
  3949. struct f2fs_super_block *super;
  3950. int err = 0;
  3951. super = kzalloc_obj(struct f2fs_super_block);
  3952. if (!super)
  3953. return -ENOMEM;
  3954. for (block = 0; block < 2; block++) {
  3955. folio = read_mapping_folio(sb->s_bdev->bd_mapping, block, NULL);
  3956. if (IS_ERR(folio)) {
  3957. f2fs_err(sbi, "Unable to read %dth superblock",
  3958. block + 1);
  3959. err = PTR_ERR(folio);
  3960. *recovery = 1;
  3961. continue;
  3962. }
  3963. /* sanity checking of raw super */
  3964. err = sanity_check_raw_super(sbi, folio, block);
  3965. if (err) {
  3966. f2fs_err(sbi, "Can't find valid F2FS filesystem in %dth superblock",
  3967. block + 1);
  3968. folio_put(folio);
  3969. *recovery = 1;
  3970. continue;
  3971. }
  3972. if (!*raw_super) {
  3973. memcpy(super, F2FS_SUPER_BLOCK(folio, block),
  3974. sizeof(*super));
  3975. *valid_super_block = block;
  3976. *raw_super = super;
  3977. }
  3978. folio_put(folio);
  3979. }
  3980. /* No valid superblock */
  3981. if (!*raw_super)
  3982. kfree(super);
  3983. else
  3984. err = 0;
  3985. return err;
  3986. }
  3987. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover)
  3988. {
  3989. struct folio *folio;
  3990. pgoff_t index;
  3991. __u32 crc = 0;
  3992. int err;
  3993. if ((recover && f2fs_readonly(sbi->sb)) ||
  3994. f2fs_hw_is_readonly(sbi)) {
  3995. set_sbi_flag(sbi, SBI_NEED_SB_WRITE);
  3996. return -EROFS;
  3997. }
  3998. /* we should update superblock crc here */
  3999. if (!recover && f2fs_sb_has_sb_chksum(sbi)) {
  4000. crc = f2fs_crc32(F2FS_RAW_SUPER(sbi),
  4001. offsetof(struct f2fs_super_block, crc));
  4002. F2FS_RAW_SUPER(sbi)->crc = cpu_to_le32(crc);
  4003. }
  4004. /* write back-up superblock first */
  4005. index = sbi->valid_super_block ? 0 : 1;
  4006. folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL);
  4007. if (IS_ERR(folio))
  4008. return PTR_ERR(folio);
  4009. err = __f2fs_commit_super(sbi, folio, index, true);
  4010. folio_put(folio);
  4011. /* if we are in recovery path, skip writing valid superblock */
  4012. if (recover || err)
  4013. return err;
  4014. /* write current valid superblock */
  4015. index = sbi->valid_super_block;
  4016. folio = read_mapping_folio(sbi->sb->s_bdev->bd_mapping, index, NULL);
  4017. if (IS_ERR(folio))
  4018. return PTR_ERR(folio);
  4019. err = __f2fs_commit_super(sbi, folio, index, true);
  4020. folio_put(folio);
  4021. return err;
  4022. }
  4023. static void save_stop_reason(struct f2fs_sb_info *sbi, unsigned char reason)
  4024. {
  4025. unsigned long flags;
  4026. spin_lock_irqsave(&sbi->error_lock, flags);
  4027. if (sbi->stop_reason[reason] < GENMASK(BITS_PER_BYTE - 1, 0))
  4028. sbi->stop_reason[reason]++;
  4029. spin_unlock_irqrestore(&sbi->error_lock, flags);
  4030. }
  4031. static void f2fs_record_stop_reason(struct f2fs_sb_info *sbi)
  4032. {
  4033. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  4034. unsigned long flags;
  4035. int err;
  4036. f2fs_down_write(&sbi->sb_lock);
  4037. spin_lock_irqsave(&sbi->error_lock, flags);
  4038. if (sbi->error_dirty) {
  4039. memcpy(F2FS_RAW_SUPER(sbi)->s_errors, sbi->errors,
  4040. MAX_F2FS_ERRORS);
  4041. sbi->error_dirty = false;
  4042. }
  4043. memcpy(raw_super->s_stop_reason, sbi->stop_reason, MAX_STOP_REASON);
  4044. spin_unlock_irqrestore(&sbi->error_lock, flags);
  4045. err = f2fs_commit_super(sbi, false);
  4046. f2fs_up_write(&sbi->sb_lock);
  4047. if (err)
  4048. f2fs_err_ratelimited(sbi,
  4049. "f2fs_commit_super fails to record stop_reason, err:%d",
  4050. err);
  4051. }
  4052. void f2fs_save_errors(struct f2fs_sb_info *sbi, unsigned char flag)
  4053. {
  4054. unsigned long flags;
  4055. spin_lock_irqsave(&sbi->error_lock, flags);
  4056. if (!test_bit(flag, (unsigned long *)sbi->errors)) {
  4057. set_bit(flag, (unsigned long *)sbi->errors);
  4058. sbi->error_dirty = true;
  4059. }
  4060. spin_unlock_irqrestore(&sbi->error_lock, flags);
  4061. }
  4062. void f2fs_handle_error(struct f2fs_sb_info *sbi, unsigned char error)
  4063. {
  4064. f2fs_save_errors(sbi, error);
  4065. if (!sbi->error_dirty)
  4066. return;
  4067. if (!test_bit(error, (unsigned long *)sbi->errors))
  4068. return;
  4069. schedule_work(&sbi->s_error_work);
  4070. }
  4071. static bool system_going_down(void)
  4072. {
  4073. return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
  4074. || system_state == SYSTEM_RESTART;
  4075. }
  4076. void f2fs_handle_critical_error(struct f2fs_sb_info *sbi, unsigned char reason)
  4077. {
  4078. struct super_block *sb = sbi->sb;
  4079. bool shutdown = reason == STOP_CP_REASON_SHUTDOWN;
  4080. bool continue_fs = !shutdown &&
  4081. F2FS_OPTION(sbi).errors == MOUNT_ERRORS_CONTINUE;
  4082. set_ckpt_flags(sbi, CP_ERROR_FLAG);
  4083. if (!f2fs_hw_is_readonly(sbi)) {
  4084. save_stop_reason(sbi, reason);
  4085. /*
  4086. * always create an asynchronous task to record stop_reason
  4087. * in order to avoid potential deadlock when running into
  4088. * f2fs_record_stop_reason() synchronously.
  4089. */
  4090. schedule_work(&sbi->s_error_work);
  4091. }
  4092. /*
  4093. * We force ERRORS_RO behavior when system is rebooting. Otherwise we
  4094. * could panic during 'reboot -f' as the underlying device got already
  4095. * disabled.
  4096. */
  4097. if (F2FS_OPTION(sbi).errors == MOUNT_ERRORS_PANIC &&
  4098. !shutdown && !system_going_down() &&
  4099. !is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN))
  4100. panic("F2FS-fs (device %s): panic forced after error\n",
  4101. sb->s_id);
  4102. if (shutdown)
  4103. set_sbi_flag(sbi, SBI_IS_SHUTDOWN);
  4104. else
  4105. dump_stack();
  4106. /*
  4107. * Continue filesystem operators if errors=continue. Should not set
  4108. * RO by shutdown, since RO bypasses thaw_super which can hang the
  4109. * system.
  4110. */
  4111. if (continue_fs || f2fs_readonly(sb) || shutdown) {
  4112. f2fs_warn(sbi, "Stopped filesystem due to reason: %d", reason);
  4113. return;
  4114. }
  4115. f2fs_warn(sbi, "Remounting filesystem read-only");
  4116. /*
  4117. * We have already set CP_ERROR_FLAG flag to stop all updates
  4118. * to filesystem, so it doesn't need to set SB_RDONLY flag here
  4119. * because the flag should be set covered w/ sb->s_umount semaphore
  4120. * via remount procedure, otherwise, it will confuse code like
  4121. * freeze_super() which will lead to deadlocks and other problems.
  4122. */
  4123. }
  4124. static void f2fs_record_error_work(struct work_struct *work)
  4125. {
  4126. struct f2fs_sb_info *sbi = container_of(work,
  4127. struct f2fs_sb_info, s_error_work);
  4128. f2fs_record_stop_reason(sbi);
  4129. }
  4130. static inline unsigned int get_first_seq_zone_segno(struct f2fs_sb_info *sbi)
  4131. {
  4132. #ifdef CONFIG_BLK_DEV_ZONED
  4133. unsigned int zoneno, total_zones;
  4134. int devi;
  4135. if (!f2fs_sb_has_blkzoned(sbi))
  4136. return NULL_SEGNO;
  4137. for (devi = 0; devi < sbi->s_ndevs; devi++) {
  4138. if (!bdev_is_zoned(FDEV(devi).bdev))
  4139. continue;
  4140. total_zones = GET_ZONE_FROM_SEG(sbi, FDEV(devi).total_segments);
  4141. for (zoneno = 0; zoneno < total_zones; zoneno++) {
  4142. unsigned int segs, blks;
  4143. if (!f2fs_zone_is_seq(sbi, devi, zoneno))
  4144. continue;
  4145. segs = GET_SEG_FROM_SEC(sbi,
  4146. zoneno * sbi->secs_per_zone);
  4147. blks = SEGS_TO_BLKS(sbi, segs);
  4148. return GET_SEGNO(sbi, FDEV(devi).start_blk + blks);
  4149. }
  4150. }
  4151. #endif
  4152. return NULL_SEGNO;
  4153. }
  4154. static int f2fs_scan_devices(struct f2fs_sb_info *sbi)
  4155. {
  4156. struct f2fs_super_block *raw_super = F2FS_RAW_SUPER(sbi);
  4157. unsigned int max_devices = MAX_DEVICES;
  4158. unsigned int logical_blksize;
  4159. blk_mode_t mode = sb_open_mode(sbi->sb->s_flags);
  4160. int i;
  4161. /* Initialize single device information */
  4162. if (!RDEV(0).path[0]) {
  4163. if (!bdev_is_zoned(sbi->sb->s_bdev))
  4164. return 0;
  4165. max_devices = 1;
  4166. }
  4167. /*
  4168. * Initialize multiple devices information, or single
  4169. * zoned block device information.
  4170. */
  4171. sbi->devs = f2fs_kzalloc(sbi,
  4172. array_size(max_devices,
  4173. sizeof(struct f2fs_dev_info)),
  4174. GFP_KERNEL);
  4175. if (!sbi->devs)
  4176. return -ENOMEM;
  4177. logical_blksize = bdev_logical_block_size(sbi->sb->s_bdev);
  4178. sbi->aligned_blksize = true;
  4179. sbi->bggc_io_aware = AWARE_ALL_IO;
  4180. #ifdef CONFIG_BLK_DEV_ZONED
  4181. sbi->max_open_zones = UINT_MAX;
  4182. sbi->blkzone_alloc_policy = BLKZONE_ALLOC_PRIOR_SEQ;
  4183. sbi->bggc_io_aware = AWARE_READ_IO;
  4184. #endif
  4185. for (i = 0; i < max_devices; i++) {
  4186. if (max_devices == 1) {
  4187. FDEV(i).total_segments =
  4188. le32_to_cpu(raw_super->segment_count_main);
  4189. FDEV(i).start_blk = 0;
  4190. FDEV(i).end_blk = FDEV(i).total_segments *
  4191. BLKS_PER_SEG(sbi);
  4192. }
  4193. if (i == 0)
  4194. FDEV(0).bdev_file = sbi->sb->s_bdev_file;
  4195. else if (!RDEV(i).path[0])
  4196. break;
  4197. if (max_devices > 1) {
  4198. /* Multi-device mount */
  4199. memcpy(FDEV(i).path, RDEV(i).path, MAX_PATH_LEN);
  4200. FDEV(i).total_segments =
  4201. le32_to_cpu(RDEV(i).total_segments);
  4202. if (i == 0) {
  4203. FDEV(i).start_blk = 0;
  4204. FDEV(i).end_blk = FDEV(i).start_blk +
  4205. SEGS_TO_BLKS(sbi,
  4206. FDEV(i).total_segments) - 1 +
  4207. le32_to_cpu(raw_super->segment0_blkaddr);
  4208. sbi->allocate_section_hint = FDEV(i).total_segments /
  4209. SEGS_PER_SEC(sbi);
  4210. } else {
  4211. FDEV(i).start_blk = FDEV(i - 1).end_blk + 1;
  4212. FDEV(i).end_blk = FDEV(i).start_blk +
  4213. SEGS_TO_BLKS(sbi,
  4214. FDEV(i).total_segments) - 1;
  4215. FDEV(i).bdev_file = bdev_file_open_by_path(
  4216. FDEV(i).path, mode, sbi->sb, NULL);
  4217. }
  4218. }
  4219. if (IS_ERR(FDEV(i).bdev_file))
  4220. return PTR_ERR(FDEV(i).bdev_file);
  4221. FDEV(i).bdev = file_bdev(FDEV(i).bdev_file);
  4222. /* to release errored devices */
  4223. sbi->s_ndevs = i + 1;
  4224. if (logical_blksize != bdev_logical_block_size(FDEV(i).bdev))
  4225. sbi->aligned_blksize = false;
  4226. #ifdef CONFIG_BLK_DEV_ZONED
  4227. if (bdev_is_zoned(FDEV(i).bdev)) {
  4228. if (!f2fs_sb_has_blkzoned(sbi)) {
  4229. f2fs_err(sbi, "Zoned block device feature not enabled");
  4230. return -EINVAL;
  4231. }
  4232. if (init_blkz_info(sbi, i)) {
  4233. f2fs_err(sbi, "Failed to initialize F2FS blkzone information");
  4234. return -EINVAL;
  4235. }
  4236. if (max_devices == 1)
  4237. break;
  4238. f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x (zone: Host-managed)",
  4239. i, FDEV(i).path,
  4240. FDEV(i).total_segments,
  4241. FDEV(i).start_blk, FDEV(i).end_blk);
  4242. continue;
  4243. }
  4244. #endif
  4245. f2fs_info(sbi, "Mount Device [%2d]: %20s, %8u, %8x - %8x",
  4246. i, FDEV(i).path,
  4247. FDEV(i).total_segments,
  4248. FDEV(i).start_blk, FDEV(i).end_blk);
  4249. }
  4250. return 0;
  4251. }
  4252. static int f2fs_setup_casefold(struct f2fs_sb_info *sbi)
  4253. {
  4254. #if IS_ENABLED(CONFIG_UNICODE)
  4255. if (f2fs_sb_has_casefold(sbi) && !sbi->sb->s_encoding) {
  4256. const struct f2fs_sb_encodings *encoding_info;
  4257. struct unicode_map *encoding;
  4258. __u16 encoding_flags;
  4259. encoding_info = f2fs_sb_read_encoding(sbi->raw_super);
  4260. if (!encoding_info) {
  4261. f2fs_err(sbi,
  4262. "Encoding requested by superblock is unknown");
  4263. return -EINVAL;
  4264. }
  4265. encoding_flags = le16_to_cpu(sbi->raw_super->s_encoding_flags);
  4266. encoding = utf8_load(encoding_info->version);
  4267. if (IS_ERR(encoding)) {
  4268. f2fs_err(sbi,
  4269. "can't mount with superblock charset: %s-%u.%u.%u "
  4270. "not supported by the kernel. flags: 0x%x.",
  4271. encoding_info->name,
  4272. unicode_major(encoding_info->version),
  4273. unicode_minor(encoding_info->version),
  4274. unicode_rev(encoding_info->version),
  4275. encoding_flags);
  4276. return PTR_ERR(encoding);
  4277. }
  4278. f2fs_info(sbi, "Using encoding defined by superblock: "
  4279. "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
  4280. unicode_major(encoding_info->version),
  4281. unicode_minor(encoding_info->version),
  4282. unicode_rev(encoding_info->version),
  4283. encoding_flags);
  4284. sbi->sb->s_encoding = encoding;
  4285. sbi->sb->s_encoding_flags = encoding_flags;
  4286. }
  4287. #else
  4288. if (f2fs_sb_has_casefold(sbi)) {
  4289. f2fs_err(sbi, "Filesystem with casefold feature cannot be mounted without CONFIG_UNICODE");
  4290. return -EINVAL;
  4291. }
  4292. #endif
  4293. return 0;
  4294. }
  4295. static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
  4296. {
  4297. /* adjust parameters according to the volume size */
  4298. if (MAIN_SEGS(sbi) <= SMALL_VOLUME_SEGMENTS) {
  4299. if (f2fs_block_unit_discard(sbi))
  4300. SM_I(sbi)->dcc_info->discard_granularity =
  4301. MIN_DISCARD_GRANULARITY;
  4302. if (!f2fs_lfs_mode(sbi))
  4303. SM_I(sbi)->ipu_policy = BIT(F2FS_IPU_FORCE) |
  4304. BIT(F2FS_IPU_HONOR_OPU_WRITE);
  4305. }
  4306. sbi->readdir_ra = true;
  4307. }
  4308. static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
  4309. {
  4310. struct f2fs_fs_context *ctx = fc->fs_private;
  4311. struct f2fs_sb_info *sbi;
  4312. struct f2fs_super_block *raw_super;
  4313. struct inode *root;
  4314. int err;
  4315. bool skip_recovery = false, need_fsck = false;
  4316. int recovery, i, valid_super_block;
  4317. struct curseg_info *seg_i;
  4318. int retry_cnt = 1;
  4319. #ifdef CONFIG_QUOTA
  4320. bool quota_enabled = false;
  4321. #endif
  4322. try_onemore:
  4323. err = -EINVAL;
  4324. raw_super = NULL;
  4325. valid_super_block = -1;
  4326. recovery = 0;
  4327. /* allocate memory for f2fs-specific super block info */
  4328. sbi = kzalloc_obj(struct f2fs_sb_info);
  4329. if (!sbi)
  4330. return -ENOMEM;
  4331. sbi->sb = sb;
  4332. /* initialize locks within allocated memory */
  4333. init_f2fs_rwsem_trace(&sbi->gc_lock, sbi, LOCK_NAME_GC_LOCK);
  4334. mutex_init(&sbi->writepages);
  4335. init_f2fs_rwsem_trace(&sbi->cp_global_sem, sbi, LOCK_NAME_CP_GLOBAL);
  4336. init_f2fs_rwsem_trace(&sbi->node_write, sbi, LOCK_NAME_NODE_WRITE);
  4337. init_f2fs_rwsem_trace(&sbi->node_change, sbi, LOCK_NAME_NODE_CHANGE);
  4338. spin_lock_init(&sbi->stat_lock);
  4339. init_f2fs_rwsem_trace(&sbi->cp_rwsem, sbi, LOCK_NAME_CP_RWSEM);
  4340. init_f2fs_rwsem(&sbi->quota_sem);
  4341. init_waitqueue_head(&sbi->cp_wait);
  4342. spin_lock_init(&sbi->error_lock);
  4343. for (i = 0; i < NR_INODE_TYPE; i++) {
  4344. INIT_LIST_HEAD(&sbi->inode_list[i]);
  4345. spin_lock_init(&sbi->inode_lock[i]);
  4346. }
  4347. mutex_init(&sbi->flush_lock);
  4348. /* set a block size */
  4349. if (unlikely(!sb_set_blocksize(sb, F2FS_BLKSIZE))) {
  4350. f2fs_err(sbi, "unable to set blocksize");
  4351. goto free_sbi;
  4352. }
  4353. err = read_raw_super_block(sbi, &raw_super, &valid_super_block,
  4354. &recovery);
  4355. if (err)
  4356. goto free_sbi;
  4357. sb->s_fs_info = sbi;
  4358. sbi->raw_super = raw_super;
  4359. INIT_WORK(&sbi->s_error_work, f2fs_record_error_work);
  4360. memcpy(sbi->errors, raw_super->s_errors, MAX_F2FS_ERRORS);
  4361. memcpy(sbi->stop_reason, raw_super->s_stop_reason, MAX_STOP_REASON);
  4362. /* precompute checksum seed for metadata */
  4363. if (f2fs_sb_has_inode_chksum(sbi))
  4364. sbi->s_chksum_seed = f2fs_chksum(~0, raw_super->uuid,
  4365. sizeof(raw_super->uuid));
  4366. default_options(sbi, false);
  4367. err = f2fs_check_opt_consistency(fc, sb);
  4368. if (err)
  4369. goto free_sb_buf;
  4370. f2fs_apply_options(fc, sb);
  4371. err = f2fs_sanity_check_options(sbi, false);
  4372. if (err)
  4373. goto free_options;
  4374. sb->s_maxbytes = max_file_blocks(NULL) <<
  4375. le32_to_cpu(raw_super->log_blocksize);
  4376. sb->s_max_links = F2FS_LINK_MAX;
  4377. err = f2fs_setup_casefold(sbi);
  4378. if (err)
  4379. goto free_options;
  4380. #ifdef CONFIG_QUOTA
  4381. sb->dq_op = &f2fs_quota_operations;
  4382. sb->s_qcop = &f2fs_quotactl_ops;
  4383. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
  4384. if (f2fs_sb_has_quota_ino(sbi)) {
  4385. for (i = 0; i < MAXQUOTAS; i++) {
  4386. if (f2fs_qf_ino(sbi->sb, i))
  4387. sbi->nquota_files++;
  4388. }
  4389. }
  4390. #endif
  4391. sb->s_op = &f2fs_sops;
  4392. #ifdef CONFIG_FS_ENCRYPTION
  4393. sb->s_cop = &f2fs_cryptops;
  4394. #endif
  4395. #ifdef CONFIG_FS_VERITY
  4396. sb->s_vop = &f2fs_verityops;
  4397. #endif
  4398. sb->s_xattr = f2fs_xattr_handlers;
  4399. sb->s_export_op = &f2fs_export_ops;
  4400. sb->s_magic = F2FS_SUPER_MAGIC;
  4401. sb->s_time_gran = 1;
  4402. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  4403. (test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
  4404. if (test_opt(sbi, INLINECRYPT))
  4405. sb->s_flags |= SB_INLINECRYPT;
  4406. if (test_opt(sbi, LAZYTIME))
  4407. sb->s_flags |= SB_LAZYTIME;
  4408. else
  4409. sb->s_flags &= ~SB_LAZYTIME;
  4410. super_set_uuid(sb, (void *) raw_super->uuid, sizeof(raw_super->uuid));
  4411. super_set_sysfs_name_bdev(sb);
  4412. sb->s_iflags |= SB_I_CGROUPWB;
  4413. /* init f2fs-specific super block info */
  4414. sbi->valid_super_block = valid_super_block;
  4415. /* disallow all the data/node/meta page writes */
  4416. set_sbi_flag(sbi, SBI_POR_DOING);
  4417. err = f2fs_init_write_merge_io(sbi);
  4418. if (err)
  4419. goto free_bio_info;
  4420. init_sb_info(sbi);
  4421. err = f2fs_init_iostat(sbi);
  4422. if (err)
  4423. goto free_bio_info;
  4424. err = init_percpu_info(sbi);
  4425. if (err)
  4426. goto free_iostat;
  4427. err = f2fs_init_page_array_cache(sbi);
  4428. if (err)
  4429. goto free_percpu;
  4430. /* get an inode for meta space */
  4431. sbi->meta_inode = f2fs_iget(sb, F2FS_META_INO(sbi));
  4432. if (IS_ERR(sbi->meta_inode)) {
  4433. f2fs_err(sbi, "Failed to read F2FS meta data inode");
  4434. err = PTR_ERR(sbi->meta_inode);
  4435. goto free_page_array_cache;
  4436. }
  4437. err = f2fs_get_valid_checkpoint(sbi);
  4438. if (err) {
  4439. f2fs_err(sbi, "Failed to get valid F2FS checkpoint");
  4440. goto free_meta_inode;
  4441. }
  4442. if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_QUOTA_NEED_FSCK_FLAG))
  4443. set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
  4444. if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_DISABLED_QUICK_FLAG)) {
  4445. set_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
  4446. sbi->interval_time[DISABLE_TIME] = DEF_DISABLE_QUICK_INTERVAL;
  4447. }
  4448. if (__is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FSCK_FLAG))
  4449. set_sbi_flag(sbi, SBI_NEED_FSCK);
  4450. /* Initialize device list */
  4451. err = f2fs_scan_devices(sbi);
  4452. if (err) {
  4453. f2fs_err(sbi, "Failed to find devices");
  4454. goto free_devices;
  4455. }
  4456. err = f2fs_init_post_read_wq(sbi);
  4457. if (err) {
  4458. f2fs_err(sbi, "Failed to initialize post read workqueue");
  4459. goto free_devices;
  4460. }
  4461. sbi->total_valid_node_count =
  4462. le32_to_cpu(sbi->ckpt->valid_node_count);
  4463. percpu_counter_set(&sbi->total_valid_inode_count,
  4464. le32_to_cpu(sbi->ckpt->valid_inode_count));
  4465. sbi->user_block_count = le64_to_cpu(sbi->ckpt->user_block_count);
  4466. sbi->total_valid_block_count =
  4467. le64_to_cpu(sbi->ckpt->valid_block_count);
  4468. sbi->last_valid_block_count = sbi->total_valid_block_count;
  4469. sbi->reserved_blocks = 0;
  4470. sbi->current_reserved_blocks = 0;
  4471. limit_reserve_root(sbi);
  4472. adjust_unusable_cap_perc(sbi);
  4473. f2fs_init_extent_cache_info(sbi);
  4474. f2fs_init_ino_entry_info(sbi);
  4475. f2fs_init_fsync_node_info(sbi);
  4476. /* setup checkpoint request control and start checkpoint issue thread */
  4477. f2fs_init_ckpt_req_control(sbi);
  4478. if (!f2fs_readonly(sb) && !test_opt(sbi, DISABLE_CHECKPOINT) &&
  4479. test_opt(sbi, MERGE_CHECKPOINT)) {
  4480. err = f2fs_start_ckpt_thread(sbi);
  4481. if (err) {
  4482. f2fs_err(sbi,
  4483. "Failed to start F2FS issue_checkpoint_thread (%d)",
  4484. err);
  4485. goto stop_ckpt_thread;
  4486. }
  4487. }
  4488. /* setup f2fs internal modules */
  4489. err = f2fs_build_segment_manager(sbi);
  4490. if (err) {
  4491. f2fs_err(sbi, "Failed to initialize F2FS segment manager (%d)",
  4492. err);
  4493. goto free_sm;
  4494. }
  4495. err = f2fs_build_node_manager(sbi);
  4496. if (err) {
  4497. f2fs_err(sbi, "Failed to initialize F2FS node manager (%d)",
  4498. err);
  4499. goto free_nm;
  4500. }
  4501. /* For write statistics */
  4502. sbi->sectors_written_start = f2fs_get_sectors_written(sbi);
  4503. /* get segno of first zoned block device */
  4504. sbi->first_seq_zone_segno = get_first_seq_zone_segno(sbi);
  4505. sbi->reserved_pin_section = f2fs_sb_has_blkzoned(sbi) ?
  4506. ZONED_PIN_SEC_REQUIRED_COUNT :
  4507. GET_SEC_FROM_SEG(sbi, overprovision_segments(sbi));
  4508. /* Read accumulated write IO statistics if exists */
  4509. seg_i = CURSEG_I(sbi, CURSEG_HOT_NODE);
  4510. if (__exist_node_summaries(sbi))
  4511. sbi->kbytes_written =
  4512. le64_to_cpu(seg_i->journal->info.kbytes_written);
  4513. f2fs_build_gc_manager(sbi);
  4514. err = f2fs_build_stats(sbi);
  4515. if (err)
  4516. goto free_nm;
  4517. /* get an inode for node space */
  4518. sbi->node_inode = f2fs_iget(sb, F2FS_NODE_INO(sbi));
  4519. if (IS_ERR(sbi->node_inode)) {
  4520. f2fs_err(sbi, "Failed to read node inode");
  4521. err = PTR_ERR(sbi->node_inode);
  4522. goto free_stats;
  4523. }
  4524. /* read root inode and dentry */
  4525. root = f2fs_iget(sb, F2FS_ROOT_INO(sbi));
  4526. if (IS_ERR(root)) {
  4527. f2fs_err(sbi, "Failed to read root inode");
  4528. err = PTR_ERR(root);
  4529. goto free_node_inode;
  4530. }
  4531. if (!S_ISDIR(root->i_mode) || !root->i_blocks ||
  4532. !root->i_size || !root->i_nlink) {
  4533. iput(root);
  4534. err = -EINVAL;
  4535. goto free_node_inode;
  4536. }
  4537. generic_set_sb_d_ops(sb);
  4538. sb->s_root = d_make_root(root); /* allocate root dentry */
  4539. if (!sb->s_root) {
  4540. err = -ENOMEM;
  4541. goto free_node_inode;
  4542. }
  4543. err = f2fs_init_compress_inode(sbi);
  4544. if (err)
  4545. goto free_root_inode;
  4546. err = f2fs_register_sysfs(sbi);
  4547. if (err)
  4548. goto free_compress_inode;
  4549. sbi->umount_lock_holder = current;
  4550. #ifdef CONFIG_QUOTA
  4551. /* Enable quota usage during mount */
  4552. if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb)) {
  4553. err = f2fs_enable_quotas(sb);
  4554. if (err)
  4555. f2fs_err(sbi, "Cannot turn on quotas: error %d", err);
  4556. }
  4557. quota_enabled = f2fs_recover_quota_begin(sbi);
  4558. #endif
  4559. /* if there are any orphan inodes, free them */
  4560. err = f2fs_recover_orphan_inodes(sbi);
  4561. if (err)
  4562. goto free_meta;
  4563. if (unlikely(is_set_ckpt_flags(sbi, CP_DISABLED_FLAG))) {
  4564. skip_recovery = true;
  4565. goto reset_checkpoint;
  4566. }
  4567. /* recover fsynced data */
  4568. if (!test_opt(sbi, DISABLE_ROLL_FORWARD) &&
  4569. !test_opt(sbi, NORECOVERY)) {
  4570. /*
  4571. * mount should be failed, when device has readonly mode, and
  4572. * previous checkpoint was not done by clean system shutdown.
  4573. */
  4574. if (f2fs_hw_is_readonly(sbi)) {
  4575. if (!is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  4576. err = f2fs_recover_fsync_data(sbi, true);
  4577. if (err > 0) {
  4578. err = -EROFS;
  4579. f2fs_err(sbi, "Need to recover fsync data, but "
  4580. "write access unavailable, please try "
  4581. "mount w/ disable_roll_forward or norecovery");
  4582. }
  4583. if (err < 0)
  4584. goto free_meta;
  4585. }
  4586. f2fs_info(sbi, "write access unavailable, skipping recovery");
  4587. goto reset_checkpoint;
  4588. }
  4589. if (need_fsck)
  4590. set_sbi_flag(sbi, SBI_NEED_FSCK);
  4591. if (skip_recovery)
  4592. goto reset_checkpoint;
  4593. err = f2fs_recover_fsync_data(sbi, false);
  4594. if (err < 0) {
  4595. if (err != -ENOMEM)
  4596. skip_recovery = true;
  4597. need_fsck = true;
  4598. f2fs_err(sbi, "Cannot recover all fsync data errno=%d",
  4599. err);
  4600. goto free_meta;
  4601. }
  4602. } else {
  4603. err = f2fs_recover_fsync_data(sbi, true);
  4604. if (err > 0) {
  4605. if (!f2fs_readonly(sb)) {
  4606. f2fs_err(sbi, "Need to recover fsync data");
  4607. err = -EINVAL;
  4608. goto free_meta;
  4609. } else {
  4610. f2fs_info(sbi, "drop all fsynced data");
  4611. err = 0;
  4612. }
  4613. }
  4614. }
  4615. reset_checkpoint:
  4616. #ifdef CONFIG_QUOTA
  4617. f2fs_recover_quota_end(sbi, quota_enabled);
  4618. #endif
  4619. /*
  4620. * If the f2fs is not readonly and fsync data recovery succeeds,
  4621. * write pointer consistency of cursegs and other zones are already
  4622. * checked and fixed during recovery. However, if recovery fails,
  4623. * write pointers are left untouched, and retry-mount should check
  4624. * them here.
  4625. */
  4626. if (skip_recovery)
  4627. err = f2fs_check_and_fix_write_pointer(sbi);
  4628. if (err)
  4629. goto free_meta;
  4630. /* f2fs_recover_fsync_data() cleared this already */
  4631. clear_sbi_flag(sbi, SBI_POR_DOING);
  4632. err = f2fs_init_inmem_curseg(sbi);
  4633. if (err)
  4634. goto sync_free_meta;
  4635. if (test_opt(sbi, DISABLE_CHECKPOINT))
  4636. err = f2fs_disable_checkpoint(sbi);
  4637. else if (is_set_ckpt_flags(sbi, CP_DISABLED_FLAG))
  4638. err = f2fs_enable_checkpoint(sbi);
  4639. if (err)
  4640. goto sync_free_meta;
  4641. /*
  4642. * If filesystem is not mounted as read-only then
  4643. * do start the gc_thread.
  4644. */
  4645. if ((F2FS_OPTION(sbi).bggc_mode != BGGC_MODE_OFF ||
  4646. test_opt(sbi, GC_MERGE)) && !f2fs_readonly(sb)) {
  4647. /* After POR, we can run background GC thread.*/
  4648. err = f2fs_start_gc_thread(sbi);
  4649. if (err)
  4650. goto sync_free_meta;
  4651. }
  4652. /* recover broken superblock */
  4653. if (recovery) {
  4654. err = f2fs_commit_super(sbi, true);
  4655. f2fs_info(sbi, "Try to recover %dth superblock, ret: %d",
  4656. sbi->valid_super_block ? 1 : 2, err);
  4657. }
  4658. f2fs_join_shrinker(sbi);
  4659. f2fs_tuning_parameters(sbi);
  4660. f2fs_notice(sbi, "Mounted with checkpoint version = %llx",
  4661. cur_cp_version(F2FS_CKPT(sbi)));
  4662. f2fs_update_time(sbi, CP_TIME);
  4663. f2fs_update_time(sbi, REQ_TIME);
  4664. clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
  4665. sbi->umount_lock_holder = NULL;
  4666. return 0;
  4667. sync_free_meta:
  4668. /* safe to flush all the data */
  4669. sync_filesystem(sbi->sb);
  4670. retry_cnt = 0;
  4671. free_meta:
  4672. #ifdef CONFIG_QUOTA
  4673. f2fs_truncate_quota_inode_pages(sb);
  4674. if (f2fs_sb_has_quota_ino(sbi) && !f2fs_readonly(sb))
  4675. f2fs_quota_off_umount(sbi->sb);
  4676. #endif
  4677. /*
  4678. * Some dirty meta pages can be produced by f2fs_recover_orphan_inodes()
  4679. * failed by EIO. Then, iput(node_inode) can trigger balance_fs_bg()
  4680. * followed by f2fs_write_checkpoint() through f2fs_write_node_pages(), which
  4681. * falls into an infinite loop in f2fs_sync_meta_pages().
  4682. */
  4683. truncate_inode_pages_final(META_MAPPING(sbi));
  4684. /* evict some inodes being cached by GC */
  4685. evict_inodes(sb);
  4686. f2fs_unregister_sysfs(sbi);
  4687. free_compress_inode:
  4688. f2fs_destroy_compress_inode(sbi);
  4689. free_root_inode:
  4690. dput(sb->s_root);
  4691. sb->s_root = NULL;
  4692. free_node_inode:
  4693. f2fs_release_ino_entry(sbi, true);
  4694. truncate_inode_pages_final(NODE_MAPPING(sbi));
  4695. iput(sbi->node_inode);
  4696. sbi->node_inode = NULL;
  4697. free_stats:
  4698. f2fs_destroy_stats(sbi);
  4699. free_nm:
  4700. /* stop discard thread before destroying node manager */
  4701. f2fs_stop_discard_thread(sbi);
  4702. f2fs_destroy_node_manager(sbi);
  4703. free_sm:
  4704. f2fs_destroy_segment_manager(sbi);
  4705. stop_ckpt_thread:
  4706. f2fs_stop_ckpt_thread(sbi);
  4707. /* flush s_error_work before sbi destroy */
  4708. flush_work(&sbi->s_error_work);
  4709. f2fs_destroy_post_read_wq(sbi);
  4710. free_devices:
  4711. destroy_device_list(sbi);
  4712. kvfree(sbi->ckpt);
  4713. free_meta_inode:
  4714. make_bad_inode(sbi->meta_inode);
  4715. iput(sbi->meta_inode);
  4716. sbi->meta_inode = NULL;
  4717. free_page_array_cache:
  4718. f2fs_destroy_page_array_cache(sbi);
  4719. free_percpu:
  4720. destroy_percpu_info(sbi);
  4721. free_iostat:
  4722. f2fs_destroy_iostat(sbi);
  4723. free_bio_info:
  4724. for (i = 0; i < NR_PAGE_TYPE; i++)
  4725. kfree(sbi->write_io[i]);
  4726. #if IS_ENABLED(CONFIG_UNICODE)
  4727. utf8_unload(sb->s_encoding);
  4728. sb->s_encoding = NULL;
  4729. #endif
  4730. free_options:
  4731. #ifdef CONFIG_QUOTA
  4732. for (i = 0; i < MAXQUOTAS; i++)
  4733. kfree(F2FS_OPTION(sbi).s_qf_names[i]);
  4734. #endif
  4735. /* no need to free dummy_enc_policy, we just keep it in ctx when failed */
  4736. swap(F2FS_CTX_INFO(ctx).dummy_enc_policy, F2FS_OPTION(sbi).dummy_enc_policy);
  4737. free_sb_buf:
  4738. kfree(raw_super);
  4739. free_sbi:
  4740. kfree(sbi);
  4741. sb->s_fs_info = NULL;
  4742. /* give only one another chance */
  4743. if (retry_cnt > 0 && skip_recovery) {
  4744. retry_cnt--;
  4745. shrink_dcache_sb(sb);
  4746. goto try_onemore;
  4747. }
  4748. return err;
  4749. }
  4750. static int f2fs_get_tree(struct fs_context *fc)
  4751. {
  4752. return get_tree_bdev(fc, f2fs_fill_super);
  4753. }
  4754. static int f2fs_reconfigure(struct fs_context *fc)
  4755. {
  4756. struct super_block *sb = fc->root->d_sb;
  4757. return __f2fs_remount(fc, sb);
  4758. }
  4759. static void f2fs_fc_free(struct fs_context *fc)
  4760. {
  4761. struct f2fs_fs_context *ctx = fc->fs_private;
  4762. if (!ctx)
  4763. return;
  4764. #ifdef CONFIG_QUOTA
  4765. f2fs_unnote_qf_name_all(fc);
  4766. #endif
  4767. fscrypt_free_dummy_policy(&F2FS_CTX_INFO(ctx).dummy_enc_policy);
  4768. kfree(ctx);
  4769. }
  4770. static const struct fs_context_operations f2fs_context_ops = {
  4771. .parse_param = f2fs_parse_param,
  4772. .get_tree = f2fs_get_tree,
  4773. .reconfigure = f2fs_reconfigure,
  4774. .free = f2fs_fc_free,
  4775. };
  4776. static void kill_f2fs_super(struct super_block *sb)
  4777. {
  4778. struct f2fs_sb_info *sbi = F2FS_SB(sb);
  4779. if (sb->s_root) {
  4780. sbi->umount_lock_holder = current;
  4781. set_sbi_flag(sbi, SBI_IS_CLOSE);
  4782. f2fs_stop_gc_thread(sbi);
  4783. f2fs_stop_discard_thread(sbi);
  4784. #ifdef CONFIG_F2FS_FS_COMPRESSION
  4785. /*
  4786. * latter evict_inode() can bypass checking and invalidating
  4787. * compress inode cache.
  4788. */
  4789. if (test_opt(sbi, COMPRESS_CACHE))
  4790. truncate_inode_pages_final(COMPRESS_MAPPING(sbi));
  4791. #endif
  4792. if (is_sbi_flag_set(sbi, SBI_IS_DIRTY) ||
  4793. !is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG)) {
  4794. struct cp_control cpc = {
  4795. .reason = CP_UMOUNT,
  4796. };
  4797. stat_inc_cp_call_count(sbi, TOTAL_CALL);
  4798. f2fs_write_checkpoint(sbi, &cpc);
  4799. }
  4800. if (is_sbi_flag_set(sbi, SBI_IS_RECOVERED) && f2fs_readonly(sb))
  4801. sb->s_flags &= ~SB_RDONLY;
  4802. }
  4803. kill_block_super(sb);
  4804. /* Release block devices last, after fscrypt_destroy_keyring(). */
  4805. if (sbi) {
  4806. destroy_device_list(sbi);
  4807. kfree(sbi);
  4808. sb->s_fs_info = NULL;
  4809. }
  4810. }
  4811. static int f2fs_init_fs_context(struct fs_context *fc)
  4812. {
  4813. struct f2fs_fs_context *ctx;
  4814. ctx = kzalloc_obj(struct f2fs_fs_context);
  4815. if (!ctx)
  4816. return -ENOMEM;
  4817. fc->fs_private = ctx;
  4818. fc->ops = &f2fs_context_ops;
  4819. return 0;
  4820. }
  4821. static struct file_system_type f2fs_fs_type = {
  4822. .owner = THIS_MODULE,
  4823. .name = "f2fs",
  4824. .init_fs_context = f2fs_init_fs_context,
  4825. .kill_sb = kill_f2fs_super,
  4826. .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP,
  4827. };
  4828. MODULE_ALIAS_FS("f2fs");
  4829. static int __init init_inodecache(void)
  4830. {
  4831. f2fs_inode_cachep = kmem_cache_create("f2fs_inode_cache",
  4832. sizeof(struct f2fs_inode_info), 0,
  4833. SLAB_RECLAIM_ACCOUNT|SLAB_ACCOUNT, NULL);
  4834. return f2fs_inode_cachep ? 0 : -ENOMEM;
  4835. }
  4836. static void destroy_inodecache(void)
  4837. {
  4838. /*
  4839. * Make sure all delayed rcu free inodes are flushed before we
  4840. * destroy cache.
  4841. */
  4842. rcu_barrier();
  4843. kmem_cache_destroy(f2fs_inode_cachep);
  4844. }
  4845. static int __init init_f2fs_fs(void)
  4846. {
  4847. int err;
  4848. err = init_inodecache();
  4849. if (err)
  4850. goto fail;
  4851. err = f2fs_create_node_manager_caches();
  4852. if (err)
  4853. goto free_inodecache;
  4854. err = f2fs_create_segment_manager_caches();
  4855. if (err)
  4856. goto free_node_manager_caches;
  4857. err = f2fs_create_checkpoint_caches();
  4858. if (err)
  4859. goto free_segment_manager_caches;
  4860. err = f2fs_create_recovery_cache();
  4861. if (err)
  4862. goto free_checkpoint_caches;
  4863. err = f2fs_create_extent_cache();
  4864. if (err)
  4865. goto free_recovery_cache;
  4866. err = f2fs_create_garbage_collection_cache();
  4867. if (err)
  4868. goto free_extent_cache;
  4869. err = f2fs_init_sysfs();
  4870. if (err)
  4871. goto free_garbage_collection_cache;
  4872. err = f2fs_init_shrinker();
  4873. if (err)
  4874. goto free_sysfs;
  4875. f2fs_create_root_stats();
  4876. err = f2fs_init_post_read_processing();
  4877. if (err)
  4878. goto free_root_stats;
  4879. err = f2fs_init_iostat_processing();
  4880. if (err)
  4881. goto free_post_read;
  4882. err = f2fs_init_bio_entry_cache();
  4883. if (err)
  4884. goto free_iostat;
  4885. err = f2fs_init_bioset();
  4886. if (err)
  4887. goto free_bio_entry_cache;
  4888. err = f2fs_init_compress_mempool();
  4889. if (err)
  4890. goto free_bioset;
  4891. err = f2fs_init_compress_cache();
  4892. if (err)
  4893. goto free_compress_mempool;
  4894. err = f2fs_create_casefold_cache();
  4895. if (err)
  4896. goto free_compress_cache;
  4897. err = f2fs_init_xattr_cache();
  4898. if (err)
  4899. goto free_casefold_cache;
  4900. err = register_filesystem(&f2fs_fs_type);
  4901. if (err)
  4902. goto free_xattr_cache;
  4903. return 0;
  4904. free_xattr_cache:
  4905. f2fs_destroy_xattr_cache();
  4906. free_casefold_cache:
  4907. f2fs_destroy_casefold_cache();
  4908. free_compress_cache:
  4909. f2fs_destroy_compress_cache();
  4910. free_compress_mempool:
  4911. f2fs_destroy_compress_mempool();
  4912. free_bioset:
  4913. f2fs_destroy_bioset();
  4914. free_bio_entry_cache:
  4915. f2fs_destroy_bio_entry_cache();
  4916. free_iostat:
  4917. f2fs_destroy_iostat_processing();
  4918. free_post_read:
  4919. f2fs_destroy_post_read_processing();
  4920. free_root_stats:
  4921. f2fs_destroy_root_stats();
  4922. f2fs_exit_shrinker();
  4923. free_sysfs:
  4924. f2fs_exit_sysfs();
  4925. free_garbage_collection_cache:
  4926. f2fs_destroy_garbage_collection_cache();
  4927. free_extent_cache:
  4928. f2fs_destroy_extent_cache();
  4929. free_recovery_cache:
  4930. f2fs_destroy_recovery_cache();
  4931. free_checkpoint_caches:
  4932. f2fs_destroy_checkpoint_caches();
  4933. free_segment_manager_caches:
  4934. f2fs_destroy_segment_manager_caches();
  4935. free_node_manager_caches:
  4936. f2fs_destroy_node_manager_caches();
  4937. free_inodecache:
  4938. destroy_inodecache();
  4939. fail:
  4940. return err;
  4941. }
  4942. static void __exit exit_f2fs_fs(void)
  4943. {
  4944. unregister_filesystem(&f2fs_fs_type);
  4945. f2fs_destroy_xattr_cache();
  4946. f2fs_destroy_casefold_cache();
  4947. f2fs_destroy_compress_cache();
  4948. f2fs_destroy_compress_mempool();
  4949. f2fs_destroy_bioset();
  4950. f2fs_destroy_bio_entry_cache();
  4951. f2fs_destroy_iostat_processing();
  4952. f2fs_destroy_post_read_processing();
  4953. f2fs_destroy_root_stats();
  4954. f2fs_exit_shrinker();
  4955. f2fs_exit_sysfs();
  4956. f2fs_destroy_garbage_collection_cache();
  4957. f2fs_destroy_extent_cache();
  4958. f2fs_destroy_recovery_cache();
  4959. f2fs_destroy_checkpoint_caches();
  4960. f2fs_destroy_segment_manager_caches();
  4961. f2fs_destroy_node_manager_caches();
  4962. destroy_inodecache();
  4963. }
  4964. module_init(init_f2fs_fs)
  4965. module_exit(exit_f2fs_fs)
  4966. MODULE_AUTHOR("Samsung Electronics's Praesto Team");
  4967. MODULE_DESCRIPTION("Flash Friendly File System");
  4968. MODULE_LICENSE("GPL");