super.c 165 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835
  1. /*
  2. * linux/fs/ext4/super.c
  3. *
  4. * Copyright (C) 1992, 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. *
  9. * from
  10. *
  11. * linux/fs/minix/inode.c
  12. *
  13. * Copyright (C) 1991, 1992 Linus Torvalds
  14. *
  15. * Big-endian to little-endian byte-swapping/bitmaps by
  16. * David S. Miller (davem@caip.rutgers.edu), 1995
  17. */
  18. #include <linux/module.h>
  19. #include <linux/string.h>
  20. #include <linux/fs.h>
  21. #include <linux/time.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/slab.h>
  24. #include <linux/init.h>
  25. #include <linux/blkdev.h>
  26. #include <linux/backing-dev.h>
  27. #include <linux/parser.h>
  28. #include <linux/buffer_head.h>
  29. #include <linux/exportfs.h>
  30. #include <linux/vfs.h>
  31. #include <linux/random.h>
  32. #include <linux/mount.h>
  33. #include <linux/namei.h>
  34. #include <linux/quotaops.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/ctype.h>
  37. #include <linux/log2.h>
  38. #include <linux/crc16.h>
  39. #include <linux/dax.h>
  40. #include <linux/cleancache.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/kthread.h>
  43. #include <linux/freezer.h>
  44. #include "ext4.h"
  45. #include "ext4_extents.h" /* Needed for trace points definition */
  46. #include "ext4_jbd2.h"
  47. #include "xattr.h"
  48. #include "acl.h"
  49. #include "mballoc.h"
  50. #include "fsmap.h"
  51. #define CREATE_TRACE_POINTS
  52. #include <trace/events/ext4.h>
  53. static struct ext4_lazy_init *ext4_li_info;
  54. static struct mutex ext4_li_mtx;
  55. static struct ratelimit_state ext4_mount_msg_ratelimit;
  56. static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
  57. unsigned long journal_devnum);
  58. static int ext4_show_options(struct seq_file *seq, struct dentry *root);
  59. static int ext4_commit_super(struct super_block *sb, int sync);
  60. static void ext4_mark_recovery_complete(struct super_block *sb,
  61. struct ext4_super_block *es);
  62. static void ext4_clear_journal_err(struct super_block *sb,
  63. struct ext4_super_block *es);
  64. static int ext4_sync_fs(struct super_block *sb, int wait);
  65. static int ext4_remount(struct super_block *sb, int *flags, char *data);
  66. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
  67. static int ext4_unfreeze(struct super_block *sb);
  68. static int ext4_freeze(struct super_block *sb);
  69. static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
  70. const char *dev_name, void *data);
  71. static inline int ext2_feature_set_ok(struct super_block *sb);
  72. static inline int ext3_feature_set_ok(struct super_block *sb);
  73. static int ext4_feature_set_ok(struct super_block *sb, int readonly);
  74. static void ext4_destroy_lazyinit_thread(void);
  75. static void ext4_unregister_li_request(struct super_block *sb);
  76. static void ext4_clear_request_list(void);
  77. static struct inode *ext4_get_journal_inode(struct super_block *sb,
  78. unsigned int journal_inum);
  79. /*
  80. * Lock ordering
  81. *
  82. * Note the difference between i_mmap_sem (EXT4_I(inode)->i_mmap_sem) and
  83. * i_mmap_rwsem (inode->i_mmap_rwsem)!
  84. *
  85. * page fault path:
  86. * mmap_sem -> sb_start_pagefault -> i_mmap_sem (r) -> transaction start ->
  87. * page lock -> i_data_sem (rw)
  88. *
  89. * buffered write path:
  90. * sb_start_write -> i_mutex -> mmap_sem
  91. * sb_start_write -> i_mutex -> transaction start -> page lock ->
  92. * i_data_sem (rw)
  93. *
  94. * truncate:
  95. * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (w) -> i_mmap_sem (w) ->
  96. * i_mmap_rwsem (w) -> page lock
  97. * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (w) -> i_mmap_sem (w) ->
  98. * transaction start -> i_data_sem (rw)
  99. *
  100. * direct IO:
  101. * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (r) -> mmap_sem
  102. * sb_start_write -> i_mutex -> EXT4_STATE_DIOREAD_LOCK (r) ->
  103. * transaction start -> i_data_sem (rw)
  104. *
  105. * writepages:
  106. * transaction start -> page lock(s) -> i_data_sem (rw)
  107. */
  108. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
  109. static struct file_system_type ext2_fs_type = {
  110. .owner = THIS_MODULE,
  111. .name = "ext2",
  112. .mount = ext4_mount,
  113. .kill_sb = kill_block_super,
  114. .fs_flags = FS_REQUIRES_DEV,
  115. };
  116. MODULE_ALIAS_FS("ext2");
  117. MODULE_ALIAS("ext2");
  118. #define IS_EXT2_SB(sb) ((sb)->s_bdev->bd_holder == &ext2_fs_type)
  119. #else
  120. #define IS_EXT2_SB(sb) (0)
  121. #endif
  122. static struct file_system_type ext3_fs_type = {
  123. .owner = THIS_MODULE,
  124. .name = "ext3",
  125. .mount = ext4_mount,
  126. .kill_sb = kill_block_super,
  127. .fs_flags = FS_REQUIRES_DEV,
  128. };
  129. MODULE_ALIAS_FS("ext3");
  130. MODULE_ALIAS("ext3");
  131. #define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
  132. static int ext4_verify_csum_type(struct super_block *sb,
  133. struct ext4_super_block *es)
  134. {
  135. if (!ext4_has_feature_metadata_csum(sb))
  136. return 1;
  137. return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
  138. }
  139. static __le32 ext4_superblock_csum(struct super_block *sb,
  140. struct ext4_super_block *es)
  141. {
  142. struct ext4_sb_info *sbi = EXT4_SB(sb);
  143. int offset = offsetof(struct ext4_super_block, s_checksum);
  144. __u32 csum;
  145. csum = ext4_chksum(sbi, ~0, (char *)es, offset);
  146. return cpu_to_le32(csum);
  147. }
  148. static int ext4_superblock_csum_verify(struct super_block *sb,
  149. struct ext4_super_block *es)
  150. {
  151. if (!ext4_has_metadata_csum(sb))
  152. return 1;
  153. return es->s_checksum == ext4_superblock_csum(sb, es);
  154. }
  155. void ext4_superblock_csum_set(struct super_block *sb)
  156. {
  157. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  158. if (!ext4_has_metadata_csum(sb))
  159. return;
  160. es->s_checksum = ext4_superblock_csum(sb, es);
  161. }
  162. void *ext4_kvmalloc(size_t size, gfp_t flags)
  163. {
  164. void *ret;
  165. ret = kmalloc(size, flags | __GFP_NOWARN);
  166. if (!ret)
  167. ret = __vmalloc(size, flags, PAGE_KERNEL);
  168. return ret;
  169. }
  170. void *ext4_kvzalloc(size_t size, gfp_t flags)
  171. {
  172. void *ret;
  173. ret = kzalloc(size, flags | __GFP_NOWARN);
  174. if (!ret)
  175. ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
  176. return ret;
  177. }
  178. ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
  179. struct ext4_group_desc *bg)
  180. {
  181. return le32_to_cpu(bg->bg_block_bitmap_lo) |
  182. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  183. (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
  184. }
  185. ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
  186. struct ext4_group_desc *bg)
  187. {
  188. return le32_to_cpu(bg->bg_inode_bitmap_lo) |
  189. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  190. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
  191. }
  192. ext4_fsblk_t ext4_inode_table(struct super_block *sb,
  193. struct ext4_group_desc *bg)
  194. {
  195. return le32_to_cpu(bg->bg_inode_table_lo) |
  196. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  197. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
  198. }
  199. __u32 ext4_free_group_clusters(struct super_block *sb,
  200. struct ext4_group_desc *bg)
  201. {
  202. return le16_to_cpu(bg->bg_free_blocks_count_lo) |
  203. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  204. (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
  205. }
  206. __u32 ext4_free_inodes_count(struct super_block *sb,
  207. struct ext4_group_desc *bg)
  208. {
  209. return le16_to_cpu(bg->bg_free_inodes_count_lo) |
  210. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  211. (__u32)le16_to_cpu(bg->bg_free_inodes_count_hi) << 16 : 0);
  212. }
  213. __u32 ext4_used_dirs_count(struct super_block *sb,
  214. struct ext4_group_desc *bg)
  215. {
  216. return le16_to_cpu(bg->bg_used_dirs_count_lo) |
  217. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  218. (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
  219. }
  220. __u32 ext4_itable_unused_count(struct super_block *sb,
  221. struct ext4_group_desc *bg)
  222. {
  223. return le16_to_cpu(bg->bg_itable_unused_lo) |
  224. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  225. (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
  226. }
  227. void ext4_block_bitmap_set(struct super_block *sb,
  228. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  229. {
  230. bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
  231. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  232. bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
  233. }
  234. void ext4_inode_bitmap_set(struct super_block *sb,
  235. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  236. {
  237. bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
  238. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  239. bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
  240. }
  241. void ext4_inode_table_set(struct super_block *sb,
  242. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  243. {
  244. bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
  245. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  246. bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
  247. }
  248. void ext4_free_group_clusters_set(struct super_block *sb,
  249. struct ext4_group_desc *bg, __u32 count)
  250. {
  251. bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
  252. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  253. bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
  254. }
  255. void ext4_free_inodes_set(struct super_block *sb,
  256. struct ext4_group_desc *bg, __u32 count)
  257. {
  258. bg->bg_free_inodes_count_lo = cpu_to_le16((__u16)count);
  259. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  260. bg->bg_free_inodes_count_hi = cpu_to_le16(count >> 16);
  261. }
  262. void ext4_used_dirs_set(struct super_block *sb,
  263. struct ext4_group_desc *bg, __u32 count)
  264. {
  265. bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
  266. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  267. bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
  268. }
  269. void ext4_itable_unused_set(struct super_block *sb,
  270. struct ext4_group_desc *bg, __u32 count)
  271. {
  272. bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
  273. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  274. bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
  275. }
  276. static void __save_error_info(struct super_block *sb, const char *func,
  277. unsigned int line)
  278. {
  279. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  280. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  281. if (bdev_read_only(sb->s_bdev))
  282. return;
  283. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  284. es->s_last_error_time = cpu_to_le32(get_seconds());
  285. strncpy(es->s_last_error_func, func, sizeof(es->s_last_error_func));
  286. es->s_last_error_line = cpu_to_le32(line);
  287. if (!es->s_first_error_time) {
  288. es->s_first_error_time = es->s_last_error_time;
  289. strncpy(es->s_first_error_func, func,
  290. sizeof(es->s_first_error_func));
  291. es->s_first_error_line = cpu_to_le32(line);
  292. es->s_first_error_ino = es->s_last_error_ino;
  293. es->s_first_error_block = es->s_last_error_block;
  294. }
  295. /*
  296. * Start the daily error reporting function if it hasn't been
  297. * started already
  298. */
  299. if (!es->s_error_count)
  300. mod_timer(&EXT4_SB(sb)->s_err_report, jiffies + 24*60*60*HZ);
  301. le32_add_cpu(&es->s_error_count, 1);
  302. }
  303. static void save_error_info(struct super_block *sb, const char *func,
  304. unsigned int line)
  305. {
  306. __save_error_info(sb, func, line);
  307. ext4_commit_super(sb, 1);
  308. }
  309. /*
  310. * The del_gendisk() function uninitializes the disk-specific data
  311. * structures, including the bdi structure, without telling anyone
  312. * else. Once this happens, any attempt to call mark_buffer_dirty()
  313. * (for example, by ext4_commit_super), will cause a kernel OOPS.
  314. * This is a kludge to prevent these oops until we can put in a proper
  315. * hook in del_gendisk() to inform the VFS and file system layers.
  316. */
  317. static int block_device_ejected(struct super_block *sb)
  318. {
  319. struct inode *bd_inode = sb->s_bdev->bd_inode;
  320. struct backing_dev_info *bdi = inode_to_bdi(bd_inode);
  321. return bdi->dev == NULL;
  322. }
  323. static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
  324. {
  325. struct super_block *sb = journal->j_private;
  326. struct ext4_sb_info *sbi = EXT4_SB(sb);
  327. int error = is_journal_aborted(journal);
  328. struct ext4_journal_cb_entry *jce;
  329. BUG_ON(txn->t_state == T_FINISHED);
  330. ext4_process_freed_data(sb, txn->t_tid);
  331. spin_lock(&sbi->s_md_lock);
  332. while (!list_empty(&txn->t_private_list)) {
  333. jce = list_entry(txn->t_private_list.next,
  334. struct ext4_journal_cb_entry, jce_list);
  335. list_del_init(&jce->jce_list);
  336. spin_unlock(&sbi->s_md_lock);
  337. jce->jce_func(sb, jce, error);
  338. spin_lock(&sbi->s_md_lock);
  339. }
  340. spin_unlock(&sbi->s_md_lock);
  341. }
  342. /* Deal with the reporting of failure conditions on a filesystem such as
  343. * inconsistencies detected or read IO failures.
  344. *
  345. * On ext2, we can store the error state of the filesystem in the
  346. * superblock. That is not possible on ext4, because we may have other
  347. * write ordering constraints on the superblock which prevent us from
  348. * writing it out straight away; and given that the journal is about to
  349. * be aborted, we can't rely on the current, or future, transactions to
  350. * write out the superblock safely.
  351. *
  352. * We'll just use the jbd2_journal_abort() error code to record an error in
  353. * the journal instead. On recovery, the journal will complain about
  354. * that error until we've noted it down and cleared it.
  355. */
  356. static void ext4_handle_error(struct super_block *sb)
  357. {
  358. if (sb->s_flags & MS_RDONLY)
  359. return;
  360. if (!test_opt(sb, ERRORS_CONT)) {
  361. journal_t *journal = EXT4_SB(sb)->s_journal;
  362. EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
  363. if (journal)
  364. jbd2_journal_abort(journal, -EIO);
  365. }
  366. if (test_opt(sb, ERRORS_RO)) {
  367. ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
  368. /*
  369. * Make sure updated value of ->s_mount_flags will be visible
  370. * before ->s_flags update
  371. */
  372. smp_wmb();
  373. sb->s_flags |= MS_RDONLY;
  374. }
  375. if (test_opt(sb, ERRORS_PANIC)) {
  376. if (EXT4_SB(sb)->s_journal &&
  377. !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
  378. return;
  379. panic("EXT4-fs (device %s): panic forced after error\n",
  380. sb->s_id);
  381. }
  382. }
  383. #define ext4_error_ratelimit(sb) \
  384. ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \
  385. "EXT4-fs error")
  386. void __ext4_error(struct super_block *sb, const char *function,
  387. unsigned int line, const char *fmt, ...)
  388. {
  389. struct va_format vaf;
  390. va_list args;
  391. if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
  392. return;
  393. if (ext4_error_ratelimit(sb)) {
  394. va_start(args, fmt);
  395. vaf.fmt = fmt;
  396. vaf.va = &args;
  397. printk(KERN_CRIT
  398. "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
  399. sb->s_id, function, line, current->comm, &vaf);
  400. va_end(args);
  401. }
  402. save_error_info(sb, function, line);
  403. ext4_handle_error(sb);
  404. }
  405. void __ext4_error_inode(struct inode *inode, const char *function,
  406. unsigned int line, ext4_fsblk_t block,
  407. const char *fmt, ...)
  408. {
  409. va_list args;
  410. struct va_format vaf;
  411. struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es;
  412. if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  413. return;
  414. es->s_last_error_ino = cpu_to_le32(inode->i_ino);
  415. es->s_last_error_block = cpu_to_le64(block);
  416. if (ext4_error_ratelimit(inode->i_sb)) {
  417. va_start(args, fmt);
  418. vaf.fmt = fmt;
  419. vaf.va = &args;
  420. if (block)
  421. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
  422. "inode #%lu: block %llu: comm %s: %pV\n",
  423. inode->i_sb->s_id, function, line, inode->i_ino,
  424. block, current->comm, &vaf);
  425. else
  426. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
  427. "inode #%lu: comm %s: %pV\n",
  428. inode->i_sb->s_id, function, line, inode->i_ino,
  429. current->comm, &vaf);
  430. va_end(args);
  431. }
  432. save_error_info(inode->i_sb, function, line);
  433. ext4_handle_error(inode->i_sb);
  434. }
  435. void __ext4_error_file(struct file *file, const char *function,
  436. unsigned int line, ext4_fsblk_t block,
  437. const char *fmt, ...)
  438. {
  439. va_list args;
  440. struct va_format vaf;
  441. struct ext4_super_block *es;
  442. struct inode *inode = file_inode(file);
  443. char pathname[80], *path;
  444. if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb))))
  445. return;
  446. es = EXT4_SB(inode->i_sb)->s_es;
  447. es->s_last_error_ino = cpu_to_le32(inode->i_ino);
  448. if (ext4_error_ratelimit(inode->i_sb)) {
  449. path = file_path(file, pathname, sizeof(pathname));
  450. if (IS_ERR(path))
  451. path = "(unknown)";
  452. va_start(args, fmt);
  453. vaf.fmt = fmt;
  454. vaf.va = &args;
  455. if (block)
  456. printk(KERN_CRIT
  457. "EXT4-fs error (device %s): %s:%d: inode #%lu: "
  458. "block %llu: comm %s: path %s: %pV\n",
  459. inode->i_sb->s_id, function, line, inode->i_ino,
  460. block, current->comm, path, &vaf);
  461. else
  462. printk(KERN_CRIT
  463. "EXT4-fs error (device %s): %s:%d: inode #%lu: "
  464. "comm %s: path %s: %pV\n",
  465. inode->i_sb->s_id, function, line, inode->i_ino,
  466. current->comm, path, &vaf);
  467. va_end(args);
  468. }
  469. save_error_info(inode->i_sb, function, line);
  470. ext4_handle_error(inode->i_sb);
  471. }
  472. const char *ext4_decode_error(struct super_block *sb, int errno,
  473. char nbuf[16])
  474. {
  475. char *errstr = NULL;
  476. switch (errno) {
  477. case -EFSCORRUPTED:
  478. errstr = "Corrupt filesystem";
  479. break;
  480. case -EFSBADCRC:
  481. errstr = "Filesystem failed CRC";
  482. break;
  483. case -EIO:
  484. errstr = "IO failure";
  485. break;
  486. case -ENOMEM:
  487. errstr = "Out of memory";
  488. break;
  489. case -EROFS:
  490. if (!sb || (EXT4_SB(sb)->s_journal &&
  491. EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
  492. errstr = "Journal has aborted";
  493. else
  494. errstr = "Readonly filesystem";
  495. break;
  496. default:
  497. /* If the caller passed in an extra buffer for unknown
  498. * errors, textualise them now. Else we just return
  499. * NULL. */
  500. if (nbuf) {
  501. /* Check for truncated error codes... */
  502. if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
  503. errstr = nbuf;
  504. }
  505. break;
  506. }
  507. return errstr;
  508. }
  509. /* __ext4_std_error decodes expected errors from journaling functions
  510. * automatically and invokes the appropriate error response. */
  511. void __ext4_std_error(struct super_block *sb, const char *function,
  512. unsigned int line, int errno)
  513. {
  514. char nbuf[16];
  515. const char *errstr;
  516. if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
  517. return;
  518. /* Special case: if the error is EROFS, and we're not already
  519. * inside a transaction, then there's really no point in logging
  520. * an error. */
  521. if (errno == -EROFS && journal_current_handle() == NULL &&
  522. (sb->s_flags & MS_RDONLY))
  523. return;
  524. if (ext4_error_ratelimit(sb)) {
  525. errstr = ext4_decode_error(sb, errno, nbuf);
  526. printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
  527. sb->s_id, function, line, errstr);
  528. }
  529. save_error_info(sb, function, line);
  530. ext4_handle_error(sb);
  531. }
  532. /*
  533. * ext4_abort is a much stronger failure handler than ext4_error. The
  534. * abort function may be used to deal with unrecoverable failures such
  535. * as journal IO errors or ENOMEM at a critical moment in log management.
  536. *
  537. * We unconditionally force the filesystem into an ABORT|READONLY state,
  538. * unless the error response on the fs has been set to panic in which
  539. * case we take the easy way out and panic immediately.
  540. */
  541. void __ext4_abort(struct super_block *sb, const char *function,
  542. unsigned int line, const char *fmt, ...)
  543. {
  544. struct va_format vaf;
  545. va_list args;
  546. if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
  547. return;
  548. save_error_info(sb, function, line);
  549. va_start(args, fmt);
  550. vaf.fmt = fmt;
  551. vaf.va = &args;
  552. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: %pV\n",
  553. sb->s_id, function, line, &vaf);
  554. va_end(args);
  555. if ((sb->s_flags & MS_RDONLY) == 0) {
  556. ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
  557. EXT4_SB(sb)->s_mount_flags |= EXT4_MF_FS_ABORTED;
  558. /*
  559. * Make sure updated value of ->s_mount_flags will be visible
  560. * before ->s_flags update
  561. */
  562. smp_wmb();
  563. sb->s_flags |= MS_RDONLY;
  564. if (EXT4_SB(sb)->s_journal)
  565. jbd2_journal_abort(EXT4_SB(sb)->s_journal, -EIO);
  566. save_error_info(sb, function, line);
  567. }
  568. if (test_opt(sb, ERRORS_PANIC)) {
  569. if (EXT4_SB(sb)->s_journal &&
  570. !(EXT4_SB(sb)->s_journal->j_flags & JBD2_REC_ERR))
  571. return;
  572. panic("EXT4-fs panic from previous error\n");
  573. }
  574. }
  575. void __ext4_msg(struct super_block *sb,
  576. const char *prefix, const char *fmt, ...)
  577. {
  578. struct va_format vaf;
  579. va_list args;
  580. if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state), "EXT4-fs"))
  581. return;
  582. va_start(args, fmt);
  583. vaf.fmt = fmt;
  584. vaf.va = &args;
  585. printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
  586. va_end(args);
  587. }
  588. #define ext4_warning_ratelimit(sb) \
  589. ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state), \
  590. "EXT4-fs warning")
  591. void __ext4_warning(struct super_block *sb, const char *function,
  592. unsigned int line, const char *fmt, ...)
  593. {
  594. struct va_format vaf;
  595. va_list args;
  596. if (!ext4_warning_ratelimit(sb))
  597. return;
  598. va_start(args, fmt);
  599. vaf.fmt = fmt;
  600. vaf.va = &args;
  601. printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
  602. sb->s_id, function, line, &vaf);
  603. va_end(args);
  604. }
  605. void __ext4_warning_inode(const struct inode *inode, const char *function,
  606. unsigned int line, const char *fmt, ...)
  607. {
  608. struct va_format vaf;
  609. va_list args;
  610. if (!ext4_warning_ratelimit(inode->i_sb))
  611. return;
  612. va_start(args, fmt);
  613. vaf.fmt = fmt;
  614. vaf.va = &args;
  615. printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
  616. "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
  617. function, line, inode->i_ino, current->comm, &vaf);
  618. va_end(args);
  619. }
  620. void __ext4_grp_locked_error(const char *function, unsigned int line,
  621. struct super_block *sb, ext4_group_t grp,
  622. unsigned long ino, ext4_fsblk_t block,
  623. const char *fmt, ...)
  624. __releases(bitlock)
  625. __acquires(bitlock)
  626. {
  627. struct va_format vaf;
  628. va_list args;
  629. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  630. if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
  631. return;
  632. es->s_last_error_ino = cpu_to_le32(ino);
  633. es->s_last_error_block = cpu_to_le64(block);
  634. __save_error_info(sb, function, line);
  635. if (ext4_error_ratelimit(sb)) {
  636. va_start(args, fmt);
  637. vaf.fmt = fmt;
  638. vaf.va = &args;
  639. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
  640. sb->s_id, function, line, grp);
  641. if (ino)
  642. printk(KERN_CONT "inode %lu: ", ino);
  643. if (block)
  644. printk(KERN_CONT "block %llu:",
  645. (unsigned long long) block);
  646. printk(KERN_CONT "%pV\n", &vaf);
  647. va_end(args);
  648. }
  649. if (test_opt(sb, ERRORS_CONT)) {
  650. ext4_commit_super(sb, 0);
  651. return;
  652. }
  653. ext4_unlock_group(sb, grp);
  654. ext4_handle_error(sb);
  655. /*
  656. * We only get here in the ERRORS_RO case; relocking the group
  657. * may be dangerous, but nothing bad will happen since the
  658. * filesystem will have already been marked read/only and the
  659. * journal has been aborted. We return 1 as a hint to callers
  660. * who might what to use the return value from
  661. * ext4_grp_locked_error() to distinguish between the
  662. * ERRORS_CONT and ERRORS_RO case, and perhaps return more
  663. * aggressively from the ext4 function in question, with a
  664. * more appropriate error code.
  665. */
  666. ext4_lock_group(sb, grp);
  667. return;
  668. }
  669. void ext4_update_dynamic_rev(struct super_block *sb)
  670. {
  671. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  672. if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
  673. return;
  674. ext4_warning(sb,
  675. "updating to rev %d because of new feature flag, "
  676. "running e2fsck is recommended",
  677. EXT4_DYNAMIC_REV);
  678. es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
  679. es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
  680. es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
  681. /* leave es->s_feature_*compat flags alone */
  682. /* es->s_uuid will be set by e2fsck if empty */
  683. /*
  684. * The rest of the superblock fields should be zero, and if not it
  685. * means they are likely already in use, so leave them alone. We
  686. * can leave it up to e2fsck to clean up any inconsistencies there.
  687. */
  688. }
  689. /*
  690. * Open the external journal device
  691. */
  692. static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
  693. {
  694. struct block_device *bdev;
  695. char b[BDEVNAME_SIZE];
  696. bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
  697. if (IS_ERR(bdev))
  698. goto fail;
  699. return bdev;
  700. fail:
  701. ext4_msg(sb, KERN_ERR, "failed to open journal device %s: %ld",
  702. __bdevname(dev, b), PTR_ERR(bdev));
  703. return NULL;
  704. }
  705. /*
  706. * Release the journal device
  707. */
  708. static void ext4_blkdev_put(struct block_device *bdev)
  709. {
  710. blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
  711. }
  712. static void ext4_blkdev_remove(struct ext4_sb_info *sbi)
  713. {
  714. struct block_device *bdev;
  715. bdev = sbi->journal_bdev;
  716. if (bdev) {
  717. ext4_blkdev_put(bdev);
  718. sbi->journal_bdev = NULL;
  719. }
  720. }
  721. static inline struct inode *orphan_list_entry(struct list_head *l)
  722. {
  723. return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
  724. }
  725. static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
  726. {
  727. struct list_head *l;
  728. ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
  729. le32_to_cpu(sbi->s_es->s_last_orphan));
  730. printk(KERN_ERR "sb_info orphan list:\n");
  731. list_for_each(l, &sbi->s_orphan) {
  732. struct inode *inode = orphan_list_entry(l);
  733. printk(KERN_ERR " "
  734. "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
  735. inode->i_sb->s_id, inode->i_ino, inode,
  736. inode->i_mode, inode->i_nlink,
  737. NEXT_ORPHAN(inode));
  738. }
  739. }
  740. #ifdef CONFIG_QUOTA
  741. static int ext4_quota_off(struct super_block *sb, int type);
  742. static inline void ext4_quota_off_umount(struct super_block *sb)
  743. {
  744. int type;
  745. /* Use our quota_off function to clear inode flags etc. */
  746. for (type = 0; type < EXT4_MAXQUOTAS; type++)
  747. ext4_quota_off(sb, type);
  748. }
  749. #else
  750. static inline void ext4_quota_off_umount(struct super_block *sb)
  751. {
  752. }
  753. #endif
  754. static void ext4_put_super(struct super_block *sb)
  755. {
  756. struct ext4_sb_info *sbi = EXT4_SB(sb);
  757. struct ext4_super_block *es = sbi->s_es;
  758. int aborted = 0;
  759. int i, err;
  760. ext4_unregister_li_request(sb);
  761. ext4_quota_off_umount(sb);
  762. flush_workqueue(sbi->rsv_conversion_wq);
  763. destroy_workqueue(sbi->rsv_conversion_wq);
  764. if (sbi->s_journal) {
  765. aborted = is_journal_aborted(sbi->s_journal);
  766. err = jbd2_journal_destroy(sbi->s_journal);
  767. sbi->s_journal = NULL;
  768. if ((err < 0) && !aborted)
  769. ext4_abort(sb, "Couldn't clean up the journal");
  770. }
  771. ext4_unregister_sysfs(sb);
  772. ext4_es_unregister_shrinker(sbi);
  773. del_timer_sync(&sbi->s_err_report);
  774. ext4_release_system_zone(sb);
  775. ext4_mb_release(sb);
  776. ext4_ext_release(sb);
  777. if (!(sb->s_flags & MS_RDONLY) && !aborted) {
  778. ext4_clear_feature_journal_needs_recovery(sb);
  779. es->s_state = cpu_to_le16(sbi->s_mount_state);
  780. }
  781. if (!(sb->s_flags & MS_RDONLY))
  782. ext4_commit_super(sb, 1);
  783. for (i = 0; i < sbi->s_gdb_count; i++)
  784. brelse(sbi->s_group_desc[i]);
  785. kvfree(sbi->s_group_desc);
  786. kvfree(sbi->s_flex_groups);
  787. percpu_counter_destroy(&sbi->s_freeclusters_counter);
  788. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  789. percpu_counter_destroy(&sbi->s_dirs_counter);
  790. percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
  791. percpu_free_rwsem(&sbi->s_journal_flag_rwsem);
  792. #ifdef CONFIG_QUOTA
  793. for (i = 0; i < EXT4_MAXQUOTAS; i++)
  794. kfree(sbi->s_qf_names[i]);
  795. #endif
  796. /* Debugging code just in case the in-memory inode orphan list
  797. * isn't empty. The on-disk one can be non-empty if we've
  798. * detected an error and taken the fs readonly, but the
  799. * in-memory list had better be clean by this point. */
  800. if (!list_empty(&sbi->s_orphan))
  801. dump_orphan_list(sb, sbi);
  802. J_ASSERT(list_empty(&sbi->s_orphan));
  803. sync_blockdev(sb->s_bdev);
  804. invalidate_bdev(sb->s_bdev);
  805. if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
  806. /*
  807. * Invalidate the journal device's buffers. We don't want them
  808. * floating about in memory - the physical journal device may
  809. * hotswapped, and it breaks the `ro-after' testing code.
  810. */
  811. sync_blockdev(sbi->journal_bdev);
  812. invalidate_bdev(sbi->journal_bdev);
  813. ext4_blkdev_remove(sbi);
  814. }
  815. if (sbi->s_ea_inode_cache) {
  816. ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
  817. sbi->s_ea_inode_cache = NULL;
  818. }
  819. if (sbi->s_ea_block_cache) {
  820. ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
  821. sbi->s_ea_block_cache = NULL;
  822. }
  823. if (sbi->s_mmp_tsk)
  824. kthread_stop(sbi->s_mmp_tsk);
  825. brelse(sbi->s_sbh);
  826. sb->s_fs_info = NULL;
  827. /*
  828. * Now that we are completely done shutting down the
  829. * superblock, we need to actually destroy the kobject.
  830. */
  831. kobject_put(&sbi->s_kobj);
  832. wait_for_completion(&sbi->s_kobj_unregister);
  833. if (sbi->s_chksum_driver)
  834. crypto_free_shash(sbi->s_chksum_driver);
  835. kfree(sbi->s_blockgroup_lock);
  836. kfree(sbi);
  837. }
  838. static struct kmem_cache *ext4_inode_cachep;
  839. /*
  840. * Called inside transaction, so use GFP_NOFS
  841. */
  842. static struct inode *ext4_alloc_inode(struct super_block *sb)
  843. {
  844. struct ext4_inode_info *ei;
  845. ei = kmem_cache_alloc(ext4_inode_cachep, GFP_NOFS);
  846. if (!ei)
  847. return NULL;
  848. ei->vfs_inode.i_version = 1;
  849. spin_lock_init(&ei->i_raw_lock);
  850. INIT_LIST_HEAD(&ei->i_prealloc_list);
  851. spin_lock_init(&ei->i_prealloc_lock);
  852. ext4_es_init_tree(&ei->i_es_tree);
  853. rwlock_init(&ei->i_es_lock);
  854. INIT_LIST_HEAD(&ei->i_es_list);
  855. ei->i_es_all_nr = 0;
  856. ei->i_es_shk_nr = 0;
  857. ei->i_es_shrink_lblk = 0;
  858. ei->i_reserved_data_blocks = 0;
  859. ei->i_reserved_meta_blocks = 0;
  860. ei->i_allocated_meta_blocks = 0;
  861. ei->i_da_metadata_calc_len = 0;
  862. ei->i_da_metadata_calc_last_lblock = 0;
  863. spin_lock_init(&(ei->i_block_reservation_lock));
  864. #ifdef CONFIG_QUOTA
  865. ei->i_reserved_quota = 0;
  866. memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
  867. #endif
  868. ei->jinode = NULL;
  869. INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
  870. spin_lock_init(&ei->i_completed_io_lock);
  871. ei->i_sync_tid = 0;
  872. ei->i_datasync_tid = 0;
  873. atomic_set(&ei->i_unwritten, 0);
  874. INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
  875. return &ei->vfs_inode;
  876. }
  877. static int ext4_drop_inode(struct inode *inode)
  878. {
  879. int drop = generic_drop_inode(inode);
  880. trace_ext4_drop_inode(inode, drop);
  881. return drop;
  882. }
  883. static void ext4_i_callback(struct rcu_head *head)
  884. {
  885. struct inode *inode = container_of(head, struct inode, i_rcu);
  886. kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
  887. }
  888. static void ext4_destroy_inode(struct inode *inode)
  889. {
  890. if (!list_empty(&(EXT4_I(inode)->i_orphan))) {
  891. ext4_msg(inode->i_sb, KERN_ERR,
  892. "Inode %lu (%p): orphan list check failed!",
  893. inode->i_ino, EXT4_I(inode));
  894. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
  895. EXT4_I(inode), sizeof(struct ext4_inode_info),
  896. true);
  897. dump_stack();
  898. }
  899. call_rcu(&inode->i_rcu, ext4_i_callback);
  900. }
  901. static void init_once(void *foo)
  902. {
  903. struct ext4_inode_info *ei = (struct ext4_inode_info *) foo;
  904. INIT_LIST_HEAD(&ei->i_orphan);
  905. init_rwsem(&ei->xattr_sem);
  906. init_rwsem(&ei->i_data_sem);
  907. init_rwsem(&ei->i_mmap_sem);
  908. inode_init_once(&ei->vfs_inode);
  909. }
  910. static int __init init_inodecache(void)
  911. {
  912. ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
  913. sizeof(struct ext4_inode_info),
  914. 0, (SLAB_RECLAIM_ACCOUNT|
  915. SLAB_MEM_SPREAD|SLAB_ACCOUNT),
  916. init_once);
  917. if (ext4_inode_cachep == NULL)
  918. return -ENOMEM;
  919. return 0;
  920. }
  921. static void destroy_inodecache(void)
  922. {
  923. /*
  924. * Make sure all delayed rcu free inodes are flushed before we
  925. * destroy cache.
  926. */
  927. rcu_barrier();
  928. kmem_cache_destroy(ext4_inode_cachep);
  929. }
  930. void ext4_clear_inode(struct inode *inode)
  931. {
  932. invalidate_inode_buffers(inode);
  933. clear_inode(inode);
  934. dquot_drop(inode);
  935. ext4_discard_preallocations(inode);
  936. ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
  937. if (EXT4_I(inode)->jinode) {
  938. jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
  939. EXT4_I(inode)->jinode);
  940. jbd2_free_inode(EXT4_I(inode)->jinode);
  941. EXT4_I(inode)->jinode = NULL;
  942. }
  943. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  944. fscrypt_put_encryption_info(inode, NULL);
  945. #endif
  946. }
  947. static struct inode *ext4_nfs_get_inode(struct super_block *sb,
  948. u64 ino, u32 generation)
  949. {
  950. struct inode *inode;
  951. if (ino < EXT4_FIRST_INO(sb) && ino != EXT4_ROOT_INO)
  952. return ERR_PTR(-ESTALE);
  953. if (ino > le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))
  954. return ERR_PTR(-ESTALE);
  955. /* iget isn't really right if the inode is currently unallocated!!
  956. *
  957. * ext4_read_inode will return a bad_inode if the inode had been
  958. * deleted, so we should be safe.
  959. *
  960. * Currently we don't know the generation for parent directory, so
  961. * a generation of 0 means "accept any"
  962. */
  963. inode = ext4_iget_normal(sb, ino);
  964. if (IS_ERR(inode))
  965. return ERR_CAST(inode);
  966. if (generation && inode->i_generation != generation) {
  967. iput(inode);
  968. return ERR_PTR(-ESTALE);
  969. }
  970. return inode;
  971. }
  972. static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
  973. int fh_len, int fh_type)
  974. {
  975. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  976. ext4_nfs_get_inode);
  977. }
  978. static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
  979. int fh_len, int fh_type)
  980. {
  981. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  982. ext4_nfs_get_inode);
  983. }
  984. /*
  985. * Try to release metadata pages (indirect blocks, directories) which are
  986. * mapped via the block device. Since these pages could have journal heads
  987. * which would prevent try_to_free_buffers() from freeing them, we must use
  988. * jbd2 layer's try_to_free_buffers() function to release them.
  989. */
  990. static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
  991. gfp_t wait)
  992. {
  993. journal_t *journal = EXT4_SB(sb)->s_journal;
  994. WARN_ON(PageChecked(page));
  995. if (!page_has_buffers(page))
  996. return 0;
  997. if (journal)
  998. return jbd2_journal_try_to_free_buffers(journal, page,
  999. wait & ~__GFP_DIRECT_RECLAIM);
  1000. return try_to_free_buffers(page);
  1001. }
  1002. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1003. static int ext4_get_context(struct inode *inode, void *ctx, size_t len)
  1004. {
  1005. return ext4_xattr_get(inode, EXT4_XATTR_INDEX_ENCRYPTION,
  1006. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT, ctx, len);
  1007. }
  1008. static int ext4_set_context(struct inode *inode, const void *ctx, size_t len,
  1009. void *fs_data)
  1010. {
  1011. handle_t *handle = fs_data;
  1012. int res, res2, credits, retries = 0;
  1013. /*
  1014. * Encrypting the root directory is not allowed because e2fsck expects
  1015. * lost+found to exist and be unencrypted, and encrypting the root
  1016. * directory would imply encrypting the lost+found directory as well as
  1017. * the filename "lost+found" itself.
  1018. */
  1019. if (inode->i_ino == EXT4_ROOT_INO)
  1020. return -EPERM;
  1021. res = ext4_convert_inline_data(inode);
  1022. if (res)
  1023. return res;
  1024. /*
  1025. * If a journal handle was specified, then the encryption context is
  1026. * being set on a new inode via inheritance and is part of a larger
  1027. * transaction to create the inode. Otherwise the encryption context is
  1028. * being set on an existing inode in its own transaction. Only in the
  1029. * latter case should the "retry on ENOSPC" logic be used.
  1030. */
  1031. if (handle) {
  1032. res = ext4_xattr_set_handle(handle, inode,
  1033. EXT4_XATTR_INDEX_ENCRYPTION,
  1034. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  1035. ctx, len, 0);
  1036. if (!res) {
  1037. ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
  1038. ext4_clear_inode_state(inode,
  1039. EXT4_STATE_MAY_INLINE_DATA);
  1040. /*
  1041. * Update inode->i_flags - e.g. S_DAX may get disabled
  1042. */
  1043. ext4_set_inode_flags(inode);
  1044. }
  1045. return res;
  1046. }
  1047. res = dquot_initialize(inode);
  1048. if (res)
  1049. return res;
  1050. retry:
  1051. res = ext4_xattr_set_credits(inode, len, false /* is_create */,
  1052. &credits);
  1053. if (res)
  1054. return res;
  1055. handle = ext4_journal_start(inode, EXT4_HT_MISC, credits);
  1056. if (IS_ERR(handle))
  1057. return PTR_ERR(handle);
  1058. res = ext4_xattr_set_handle(handle, inode, EXT4_XATTR_INDEX_ENCRYPTION,
  1059. EXT4_XATTR_NAME_ENCRYPTION_CONTEXT,
  1060. ctx, len, 0);
  1061. if (!res) {
  1062. ext4_set_inode_flag(inode, EXT4_INODE_ENCRYPT);
  1063. /* Update inode->i_flags - e.g. S_DAX may get disabled */
  1064. ext4_set_inode_flags(inode);
  1065. res = ext4_mark_inode_dirty(handle, inode);
  1066. if (res)
  1067. EXT4_ERROR_INODE(inode, "Failed to mark inode dirty");
  1068. }
  1069. res2 = ext4_journal_stop(handle);
  1070. if (res == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  1071. goto retry;
  1072. if (!res)
  1073. res = res2;
  1074. return res;
  1075. }
  1076. static int ext4_dummy_context(struct inode *inode)
  1077. {
  1078. return DUMMY_ENCRYPTION_ENABLED(EXT4_SB(inode->i_sb));
  1079. }
  1080. static unsigned ext4_max_namelen(struct inode *inode)
  1081. {
  1082. return S_ISLNK(inode->i_mode) ? inode->i_sb->s_blocksize :
  1083. EXT4_NAME_LEN;
  1084. }
  1085. static const struct fscrypt_operations ext4_cryptops = {
  1086. .key_prefix = "ext4:",
  1087. .get_context = ext4_get_context,
  1088. .set_context = ext4_set_context,
  1089. .dummy_context = ext4_dummy_context,
  1090. .is_encrypted = ext4_encrypted_inode,
  1091. .empty_dir = ext4_empty_dir,
  1092. .max_namelen = ext4_max_namelen,
  1093. };
  1094. #else
  1095. static const struct fscrypt_operations ext4_cryptops = {
  1096. .is_encrypted = ext4_encrypted_inode,
  1097. };
  1098. #endif
  1099. #ifdef CONFIG_QUOTA
  1100. static const char * const quotatypes[] = INITQFNAMES;
  1101. #define QTYPE2NAME(t) (quotatypes[t])
  1102. static int ext4_write_dquot(struct dquot *dquot);
  1103. static int ext4_acquire_dquot(struct dquot *dquot);
  1104. static int ext4_release_dquot(struct dquot *dquot);
  1105. static int ext4_mark_dquot_dirty(struct dquot *dquot);
  1106. static int ext4_write_info(struct super_block *sb, int type);
  1107. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  1108. const struct path *path);
  1109. static int ext4_quota_on_mount(struct super_block *sb, int type);
  1110. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  1111. size_t len, loff_t off);
  1112. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  1113. const char *data, size_t len, loff_t off);
  1114. static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
  1115. unsigned int flags);
  1116. static int ext4_enable_quotas(struct super_block *sb);
  1117. static int ext4_get_next_id(struct super_block *sb, struct kqid *qid);
  1118. static struct dquot **ext4_get_dquots(struct inode *inode)
  1119. {
  1120. return EXT4_I(inode)->i_dquot;
  1121. }
  1122. static const struct dquot_operations ext4_quota_operations = {
  1123. .get_reserved_space = ext4_get_reserved_space,
  1124. .write_dquot = ext4_write_dquot,
  1125. .acquire_dquot = ext4_acquire_dquot,
  1126. .release_dquot = ext4_release_dquot,
  1127. .mark_dirty = ext4_mark_dquot_dirty,
  1128. .write_info = ext4_write_info,
  1129. .alloc_dquot = dquot_alloc,
  1130. .destroy_dquot = dquot_destroy,
  1131. .get_projid = ext4_get_projid,
  1132. .get_inode_usage = ext4_get_inode_usage,
  1133. .get_next_id = ext4_get_next_id,
  1134. };
  1135. static const struct quotactl_ops ext4_qctl_operations = {
  1136. .quota_on = ext4_quota_on,
  1137. .quota_off = ext4_quota_off,
  1138. .quota_sync = dquot_quota_sync,
  1139. .get_state = dquot_get_state,
  1140. .set_info = dquot_set_dqinfo,
  1141. .get_dqblk = dquot_get_dqblk,
  1142. .set_dqblk = dquot_set_dqblk,
  1143. .get_nextdqblk = dquot_get_next_dqblk,
  1144. };
  1145. #endif
  1146. static const struct super_operations ext4_sops = {
  1147. .alloc_inode = ext4_alloc_inode,
  1148. .destroy_inode = ext4_destroy_inode,
  1149. .write_inode = ext4_write_inode,
  1150. .dirty_inode = ext4_dirty_inode,
  1151. .drop_inode = ext4_drop_inode,
  1152. .evict_inode = ext4_evict_inode,
  1153. .put_super = ext4_put_super,
  1154. .sync_fs = ext4_sync_fs,
  1155. .freeze_fs = ext4_freeze,
  1156. .unfreeze_fs = ext4_unfreeze,
  1157. .statfs = ext4_statfs,
  1158. .remount_fs = ext4_remount,
  1159. .show_options = ext4_show_options,
  1160. #ifdef CONFIG_QUOTA
  1161. .quota_read = ext4_quota_read,
  1162. .quota_write = ext4_quota_write,
  1163. .get_dquots = ext4_get_dquots,
  1164. #endif
  1165. .bdev_try_to_free_page = bdev_try_to_free_page,
  1166. };
  1167. static const struct export_operations ext4_export_ops = {
  1168. .fh_to_dentry = ext4_fh_to_dentry,
  1169. .fh_to_parent = ext4_fh_to_parent,
  1170. .get_parent = ext4_get_parent,
  1171. };
  1172. enum {
  1173. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  1174. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
  1175. Opt_nouid32, Opt_debug, Opt_removed,
  1176. Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
  1177. Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
  1178. Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
  1179. Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
  1180. Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
  1181. Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
  1182. Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
  1183. Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
  1184. Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
  1185. Opt_usrquota, Opt_grpquota, Opt_prjquota, Opt_i_version, Opt_dax,
  1186. Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_mblk_io_submit,
  1187. Opt_lazytime, Opt_nolazytime, Opt_debug_want_extra_isize,
  1188. Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
  1189. Opt_inode_readahead_blks, Opt_journal_ioprio,
  1190. Opt_dioread_nolock, Opt_dioread_lock,
  1191. Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
  1192. Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
  1193. };
  1194. static const match_table_t tokens = {
  1195. {Opt_bsd_df, "bsddf"},
  1196. {Opt_minix_df, "minixdf"},
  1197. {Opt_grpid, "grpid"},
  1198. {Opt_grpid, "bsdgroups"},
  1199. {Opt_nogrpid, "nogrpid"},
  1200. {Opt_nogrpid, "sysvgroups"},
  1201. {Opt_resgid, "resgid=%u"},
  1202. {Opt_resuid, "resuid=%u"},
  1203. {Opt_sb, "sb=%u"},
  1204. {Opt_err_cont, "errors=continue"},
  1205. {Opt_err_panic, "errors=panic"},
  1206. {Opt_err_ro, "errors=remount-ro"},
  1207. {Opt_nouid32, "nouid32"},
  1208. {Opt_debug, "debug"},
  1209. {Opt_removed, "oldalloc"},
  1210. {Opt_removed, "orlov"},
  1211. {Opt_user_xattr, "user_xattr"},
  1212. {Opt_nouser_xattr, "nouser_xattr"},
  1213. {Opt_acl, "acl"},
  1214. {Opt_noacl, "noacl"},
  1215. {Opt_noload, "norecovery"},
  1216. {Opt_noload, "noload"},
  1217. {Opt_removed, "nobh"},
  1218. {Opt_removed, "bh"},
  1219. {Opt_commit, "commit=%u"},
  1220. {Opt_min_batch_time, "min_batch_time=%u"},
  1221. {Opt_max_batch_time, "max_batch_time=%u"},
  1222. {Opt_journal_dev, "journal_dev=%u"},
  1223. {Opt_journal_path, "journal_path=%s"},
  1224. {Opt_journal_checksum, "journal_checksum"},
  1225. {Opt_nojournal_checksum, "nojournal_checksum"},
  1226. {Opt_journal_async_commit, "journal_async_commit"},
  1227. {Opt_abort, "abort"},
  1228. {Opt_data_journal, "data=journal"},
  1229. {Opt_data_ordered, "data=ordered"},
  1230. {Opt_data_writeback, "data=writeback"},
  1231. {Opt_data_err_abort, "data_err=abort"},
  1232. {Opt_data_err_ignore, "data_err=ignore"},
  1233. {Opt_offusrjquota, "usrjquota="},
  1234. {Opt_usrjquota, "usrjquota=%s"},
  1235. {Opt_offgrpjquota, "grpjquota="},
  1236. {Opt_grpjquota, "grpjquota=%s"},
  1237. {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
  1238. {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
  1239. {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
  1240. {Opt_grpquota, "grpquota"},
  1241. {Opt_noquota, "noquota"},
  1242. {Opt_quota, "quota"},
  1243. {Opt_usrquota, "usrquota"},
  1244. {Opt_prjquota, "prjquota"},
  1245. {Opt_barrier, "barrier=%u"},
  1246. {Opt_barrier, "barrier"},
  1247. {Opt_nobarrier, "nobarrier"},
  1248. {Opt_i_version, "i_version"},
  1249. {Opt_dax, "dax"},
  1250. {Opt_stripe, "stripe=%u"},
  1251. {Opt_delalloc, "delalloc"},
  1252. {Opt_lazytime, "lazytime"},
  1253. {Opt_nolazytime, "nolazytime"},
  1254. {Opt_debug_want_extra_isize, "debug_want_extra_isize=%u"},
  1255. {Opt_nodelalloc, "nodelalloc"},
  1256. {Opt_removed, "mblk_io_submit"},
  1257. {Opt_removed, "nomblk_io_submit"},
  1258. {Opt_block_validity, "block_validity"},
  1259. {Opt_noblock_validity, "noblock_validity"},
  1260. {Opt_inode_readahead_blks, "inode_readahead_blks=%u"},
  1261. {Opt_journal_ioprio, "journal_ioprio=%u"},
  1262. {Opt_auto_da_alloc, "auto_da_alloc=%u"},
  1263. {Opt_auto_da_alloc, "auto_da_alloc"},
  1264. {Opt_noauto_da_alloc, "noauto_da_alloc"},
  1265. {Opt_dioread_nolock, "dioread_nolock"},
  1266. {Opt_dioread_lock, "dioread_lock"},
  1267. {Opt_discard, "discard"},
  1268. {Opt_nodiscard, "nodiscard"},
  1269. {Opt_init_itable, "init_itable=%u"},
  1270. {Opt_init_itable, "init_itable"},
  1271. {Opt_noinit_itable, "noinit_itable"},
  1272. {Opt_max_dir_size_kb, "max_dir_size_kb=%u"},
  1273. {Opt_test_dummy_encryption, "test_dummy_encryption"},
  1274. {Opt_nombcache, "nombcache"},
  1275. {Opt_nombcache, "no_mbcache"}, /* for backward compatibility */
  1276. {Opt_removed, "check=none"}, /* mount option from ext2/3 */
  1277. {Opt_removed, "nocheck"}, /* mount option from ext2/3 */
  1278. {Opt_removed, "reservation"}, /* mount option from ext2/3 */
  1279. {Opt_removed, "noreservation"}, /* mount option from ext2/3 */
  1280. {Opt_removed, "journal=%u"}, /* mount option from ext2/3 */
  1281. {Opt_err, NULL},
  1282. };
  1283. static ext4_fsblk_t get_sb_block(void **data)
  1284. {
  1285. ext4_fsblk_t sb_block;
  1286. char *options = (char *) *data;
  1287. if (!options || strncmp(options, "sb=", 3) != 0)
  1288. return 1; /* Default location */
  1289. options += 3;
  1290. /* TODO: use simple_strtoll with >32bit ext4 */
  1291. sb_block = simple_strtoul(options, &options, 0);
  1292. if (*options && *options != ',') {
  1293. printk(KERN_ERR "EXT4-fs: Invalid sb specification: %s\n",
  1294. (char *) *data);
  1295. return 1;
  1296. }
  1297. if (*options == ',')
  1298. options++;
  1299. *data = (void *) options;
  1300. return sb_block;
  1301. }
  1302. #define DEFAULT_JOURNAL_IOPRIO (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 3))
  1303. static const char deprecated_msg[] =
  1304. "Mount option \"%s\" will be removed by %s\n"
  1305. "Contact linux-ext4@vger.kernel.org if you think we should keep it.\n";
  1306. #ifdef CONFIG_QUOTA
  1307. static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
  1308. {
  1309. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1310. char *qname;
  1311. int ret = -1;
  1312. if (sb_any_quota_loaded(sb) &&
  1313. !sbi->s_qf_names[qtype]) {
  1314. ext4_msg(sb, KERN_ERR,
  1315. "Cannot change journaled "
  1316. "quota options when quota turned on");
  1317. return -1;
  1318. }
  1319. if (ext4_has_feature_quota(sb)) {
  1320. ext4_msg(sb, KERN_INFO, "Journaled quota options "
  1321. "ignored when QUOTA feature is enabled");
  1322. return 1;
  1323. }
  1324. qname = match_strdup(args);
  1325. if (!qname) {
  1326. ext4_msg(sb, KERN_ERR,
  1327. "Not enough memory for storing quotafile name");
  1328. return -1;
  1329. }
  1330. if (sbi->s_qf_names[qtype]) {
  1331. if (strcmp(sbi->s_qf_names[qtype], qname) == 0)
  1332. ret = 1;
  1333. else
  1334. ext4_msg(sb, KERN_ERR,
  1335. "%s quota file already specified",
  1336. QTYPE2NAME(qtype));
  1337. goto errout;
  1338. }
  1339. if (strchr(qname, '/')) {
  1340. ext4_msg(sb, KERN_ERR,
  1341. "quotafile must be on filesystem root");
  1342. goto errout;
  1343. }
  1344. sbi->s_qf_names[qtype] = qname;
  1345. set_opt(sb, QUOTA);
  1346. return 1;
  1347. errout:
  1348. kfree(qname);
  1349. return ret;
  1350. }
  1351. static int clear_qf_name(struct super_block *sb, int qtype)
  1352. {
  1353. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1354. if (sb_any_quota_loaded(sb) &&
  1355. sbi->s_qf_names[qtype]) {
  1356. ext4_msg(sb, KERN_ERR, "Cannot change journaled quota options"
  1357. " when quota turned on");
  1358. return -1;
  1359. }
  1360. kfree(sbi->s_qf_names[qtype]);
  1361. sbi->s_qf_names[qtype] = NULL;
  1362. return 1;
  1363. }
  1364. #endif
  1365. #define MOPT_SET 0x0001
  1366. #define MOPT_CLEAR 0x0002
  1367. #define MOPT_NOSUPPORT 0x0004
  1368. #define MOPT_EXPLICIT 0x0008
  1369. #define MOPT_CLEAR_ERR 0x0010
  1370. #define MOPT_GTE0 0x0020
  1371. #ifdef CONFIG_QUOTA
  1372. #define MOPT_Q 0
  1373. #define MOPT_QFMT 0x0040
  1374. #else
  1375. #define MOPT_Q MOPT_NOSUPPORT
  1376. #define MOPT_QFMT MOPT_NOSUPPORT
  1377. #endif
  1378. #define MOPT_DATAJ 0x0080
  1379. #define MOPT_NO_EXT2 0x0100
  1380. #define MOPT_NO_EXT3 0x0200
  1381. #define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3)
  1382. #define MOPT_STRING 0x0400
  1383. static const struct mount_opts {
  1384. int token;
  1385. int mount_opt;
  1386. int flags;
  1387. } ext4_mount_opts[] = {
  1388. {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
  1389. {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
  1390. {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
  1391. {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
  1392. {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
  1393. {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
  1394. {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
  1395. MOPT_EXT4_ONLY | MOPT_SET},
  1396. {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
  1397. MOPT_EXT4_ONLY | MOPT_CLEAR},
  1398. {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
  1399. {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
  1400. {Opt_delalloc, EXT4_MOUNT_DELALLOC,
  1401. MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
  1402. {Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
  1403. MOPT_EXT4_ONLY | MOPT_CLEAR},
  1404. {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
  1405. MOPT_EXT4_ONLY | MOPT_CLEAR},
  1406. {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
  1407. MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
  1408. {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
  1409. EXT4_MOUNT_JOURNAL_CHECKSUM),
  1410. MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
  1411. {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
  1412. {Opt_err_panic, EXT4_MOUNT_ERRORS_PANIC, MOPT_SET | MOPT_CLEAR_ERR},
  1413. {Opt_err_ro, EXT4_MOUNT_ERRORS_RO, MOPT_SET | MOPT_CLEAR_ERR},
  1414. {Opt_err_cont, EXT4_MOUNT_ERRORS_CONT, MOPT_SET | MOPT_CLEAR_ERR},
  1415. {Opt_data_err_abort, EXT4_MOUNT_DATA_ERR_ABORT,
  1416. MOPT_NO_EXT2},
  1417. {Opt_data_err_ignore, EXT4_MOUNT_DATA_ERR_ABORT,
  1418. MOPT_NO_EXT2},
  1419. {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
  1420. {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
  1421. {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
  1422. {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
  1423. {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
  1424. {Opt_commit, 0, MOPT_GTE0},
  1425. {Opt_max_batch_time, 0, MOPT_GTE0},
  1426. {Opt_min_batch_time, 0, MOPT_GTE0},
  1427. {Opt_inode_readahead_blks, 0, MOPT_GTE0},
  1428. {Opt_init_itable, 0, MOPT_GTE0},
  1429. {Opt_dax, EXT4_MOUNT_DAX, MOPT_SET},
  1430. {Opt_stripe, 0, MOPT_GTE0},
  1431. {Opt_resuid, 0, MOPT_GTE0},
  1432. {Opt_resgid, 0, MOPT_GTE0},
  1433. {Opt_journal_dev, 0, MOPT_NO_EXT2 | MOPT_GTE0},
  1434. {Opt_journal_path, 0, MOPT_NO_EXT2 | MOPT_STRING},
  1435. {Opt_journal_ioprio, 0, MOPT_NO_EXT2 | MOPT_GTE0},
  1436. {Opt_data_journal, EXT4_MOUNT_JOURNAL_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
  1437. {Opt_data_ordered, EXT4_MOUNT_ORDERED_DATA, MOPT_NO_EXT2 | MOPT_DATAJ},
  1438. {Opt_data_writeback, EXT4_MOUNT_WRITEBACK_DATA,
  1439. MOPT_NO_EXT2 | MOPT_DATAJ},
  1440. {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
  1441. {Opt_nouser_xattr, EXT4_MOUNT_XATTR_USER, MOPT_CLEAR},
  1442. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  1443. {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
  1444. {Opt_noacl, EXT4_MOUNT_POSIX_ACL, MOPT_CLEAR},
  1445. #else
  1446. {Opt_acl, 0, MOPT_NOSUPPORT},
  1447. {Opt_noacl, 0, MOPT_NOSUPPORT},
  1448. #endif
  1449. {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
  1450. {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
  1451. {Opt_debug_want_extra_isize, 0, MOPT_GTE0},
  1452. {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
  1453. {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
  1454. MOPT_SET | MOPT_Q},
  1455. {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
  1456. MOPT_SET | MOPT_Q},
  1457. {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
  1458. MOPT_SET | MOPT_Q},
  1459. {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
  1460. EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
  1461. MOPT_CLEAR | MOPT_Q},
  1462. {Opt_usrjquota, 0, MOPT_Q},
  1463. {Opt_grpjquota, 0, MOPT_Q},
  1464. {Opt_offusrjquota, 0, MOPT_Q},
  1465. {Opt_offgrpjquota, 0, MOPT_Q},
  1466. {Opt_jqfmt_vfsold, QFMT_VFS_OLD, MOPT_QFMT},
  1467. {Opt_jqfmt_vfsv0, QFMT_VFS_V0, MOPT_QFMT},
  1468. {Opt_jqfmt_vfsv1, QFMT_VFS_V1, MOPT_QFMT},
  1469. {Opt_max_dir_size_kb, 0, MOPT_GTE0},
  1470. {Opt_test_dummy_encryption, 0, MOPT_GTE0},
  1471. {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
  1472. {Opt_err, 0, 0}
  1473. };
  1474. static int handle_mount_opt(struct super_block *sb, char *opt, int token,
  1475. substring_t *args, unsigned long *journal_devnum,
  1476. unsigned int *journal_ioprio, int is_remount)
  1477. {
  1478. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1479. const struct mount_opts *m;
  1480. kuid_t uid;
  1481. kgid_t gid;
  1482. int arg = 0;
  1483. #ifdef CONFIG_QUOTA
  1484. if (token == Opt_usrjquota)
  1485. return set_qf_name(sb, USRQUOTA, &args[0]);
  1486. else if (token == Opt_grpjquota)
  1487. return set_qf_name(sb, GRPQUOTA, &args[0]);
  1488. else if (token == Opt_offusrjquota)
  1489. return clear_qf_name(sb, USRQUOTA);
  1490. else if (token == Opt_offgrpjquota)
  1491. return clear_qf_name(sb, GRPQUOTA);
  1492. #endif
  1493. switch (token) {
  1494. case Opt_noacl:
  1495. case Opt_nouser_xattr:
  1496. ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
  1497. break;
  1498. case Opt_sb:
  1499. return 1; /* handled by get_sb_block() */
  1500. case Opt_removed:
  1501. ext4_msg(sb, KERN_WARNING, "Ignoring removed %s option", opt);
  1502. return 1;
  1503. case Opt_abort:
  1504. sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
  1505. return 1;
  1506. case Opt_i_version:
  1507. sb->s_flags |= MS_I_VERSION;
  1508. return 1;
  1509. case Opt_lazytime:
  1510. sb->s_flags |= MS_LAZYTIME;
  1511. return 1;
  1512. case Opt_nolazytime:
  1513. sb->s_flags &= ~MS_LAZYTIME;
  1514. return 1;
  1515. }
  1516. for (m = ext4_mount_opts; m->token != Opt_err; m++)
  1517. if (token == m->token)
  1518. break;
  1519. if (m->token == Opt_err) {
  1520. ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
  1521. "or missing value", opt);
  1522. return -1;
  1523. }
  1524. if ((m->flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
  1525. ext4_msg(sb, KERN_ERR,
  1526. "Mount option \"%s\" incompatible with ext2", opt);
  1527. return -1;
  1528. }
  1529. if ((m->flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
  1530. ext4_msg(sb, KERN_ERR,
  1531. "Mount option \"%s\" incompatible with ext3", opt);
  1532. return -1;
  1533. }
  1534. if (args->from && !(m->flags & MOPT_STRING) && match_int(args, &arg))
  1535. return -1;
  1536. if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
  1537. return -1;
  1538. if (m->flags & MOPT_EXPLICIT) {
  1539. if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
  1540. set_opt2(sb, EXPLICIT_DELALLOC);
  1541. } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
  1542. set_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM);
  1543. } else
  1544. return -1;
  1545. }
  1546. if (m->flags & MOPT_CLEAR_ERR)
  1547. clear_opt(sb, ERRORS_MASK);
  1548. if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
  1549. ext4_msg(sb, KERN_ERR, "Cannot change quota "
  1550. "options when quota turned on");
  1551. return -1;
  1552. }
  1553. if (m->flags & MOPT_NOSUPPORT) {
  1554. ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
  1555. } else if (token == Opt_commit) {
  1556. if (arg == 0)
  1557. arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
  1558. sbi->s_commit_interval = HZ * arg;
  1559. } else if (token == Opt_debug_want_extra_isize) {
  1560. sbi->s_want_extra_isize = arg;
  1561. } else if (token == Opt_max_batch_time) {
  1562. sbi->s_max_batch_time = arg;
  1563. } else if (token == Opt_min_batch_time) {
  1564. sbi->s_min_batch_time = arg;
  1565. } else if (token == Opt_inode_readahead_blks) {
  1566. if (arg && (arg > (1 << 30) || !is_power_of_2(arg))) {
  1567. ext4_msg(sb, KERN_ERR,
  1568. "EXT4-fs: inode_readahead_blks must be "
  1569. "0 or a power of 2 smaller than 2^31");
  1570. return -1;
  1571. }
  1572. sbi->s_inode_readahead_blks = arg;
  1573. } else if (token == Opt_init_itable) {
  1574. set_opt(sb, INIT_INODE_TABLE);
  1575. if (!args->from)
  1576. arg = EXT4_DEF_LI_WAIT_MULT;
  1577. sbi->s_li_wait_mult = arg;
  1578. } else if (token == Opt_max_dir_size_kb) {
  1579. sbi->s_max_dir_size_kb = arg;
  1580. } else if (token == Opt_stripe) {
  1581. sbi->s_stripe = arg;
  1582. } else if (token == Opt_resuid) {
  1583. uid = make_kuid(current_user_ns(), arg);
  1584. if (!uid_valid(uid)) {
  1585. ext4_msg(sb, KERN_ERR, "Invalid uid value %d", arg);
  1586. return -1;
  1587. }
  1588. sbi->s_resuid = uid;
  1589. } else if (token == Opt_resgid) {
  1590. gid = make_kgid(current_user_ns(), arg);
  1591. if (!gid_valid(gid)) {
  1592. ext4_msg(sb, KERN_ERR, "Invalid gid value %d", arg);
  1593. return -1;
  1594. }
  1595. sbi->s_resgid = gid;
  1596. } else if (token == Opt_journal_dev) {
  1597. if (is_remount) {
  1598. ext4_msg(sb, KERN_ERR,
  1599. "Cannot specify journal on remount");
  1600. return -1;
  1601. }
  1602. *journal_devnum = arg;
  1603. } else if (token == Opt_journal_path) {
  1604. char *journal_path;
  1605. struct inode *journal_inode;
  1606. struct path path;
  1607. int error;
  1608. if (is_remount) {
  1609. ext4_msg(sb, KERN_ERR,
  1610. "Cannot specify journal on remount");
  1611. return -1;
  1612. }
  1613. journal_path = match_strdup(&args[0]);
  1614. if (!journal_path) {
  1615. ext4_msg(sb, KERN_ERR, "error: could not dup "
  1616. "journal device string");
  1617. return -1;
  1618. }
  1619. error = kern_path(journal_path, LOOKUP_FOLLOW, &path);
  1620. if (error) {
  1621. ext4_msg(sb, KERN_ERR, "error: could not find "
  1622. "journal device path: error %d", error);
  1623. kfree(journal_path);
  1624. return -1;
  1625. }
  1626. journal_inode = d_inode(path.dentry);
  1627. if (!S_ISBLK(journal_inode->i_mode)) {
  1628. ext4_msg(sb, KERN_ERR, "error: journal path %s "
  1629. "is not a block device", journal_path);
  1630. path_put(&path);
  1631. kfree(journal_path);
  1632. return -1;
  1633. }
  1634. *journal_devnum = new_encode_dev(journal_inode->i_rdev);
  1635. path_put(&path);
  1636. kfree(journal_path);
  1637. } else if (token == Opt_journal_ioprio) {
  1638. if (arg > 7) {
  1639. ext4_msg(sb, KERN_ERR, "Invalid journal IO priority"
  1640. " (must be 0-7)");
  1641. return -1;
  1642. }
  1643. *journal_ioprio =
  1644. IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
  1645. } else if (token == Opt_test_dummy_encryption) {
  1646. #ifdef CONFIG_EXT4_FS_ENCRYPTION
  1647. sbi->s_mount_flags |= EXT4_MF_TEST_DUMMY_ENCRYPTION;
  1648. ext4_msg(sb, KERN_WARNING,
  1649. "Test dummy encryption mode enabled");
  1650. #else
  1651. ext4_msg(sb, KERN_WARNING,
  1652. "Test dummy encryption mount option ignored");
  1653. #endif
  1654. } else if (m->flags & MOPT_DATAJ) {
  1655. if (is_remount) {
  1656. if (!sbi->s_journal)
  1657. ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
  1658. else if (test_opt(sb, DATA_FLAGS) != m->mount_opt) {
  1659. ext4_msg(sb, KERN_ERR,
  1660. "Cannot change data mode on remount");
  1661. return -1;
  1662. }
  1663. } else {
  1664. clear_opt(sb, DATA_FLAGS);
  1665. sbi->s_mount_opt |= m->mount_opt;
  1666. }
  1667. #ifdef CONFIG_QUOTA
  1668. } else if (m->flags & MOPT_QFMT) {
  1669. if (sb_any_quota_loaded(sb) &&
  1670. sbi->s_jquota_fmt != m->mount_opt) {
  1671. ext4_msg(sb, KERN_ERR, "Cannot change journaled "
  1672. "quota options when quota turned on");
  1673. return -1;
  1674. }
  1675. if (ext4_has_feature_quota(sb)) {
  1676. ext4_msg(sb, KERN_INFO,
  1677. "Quota format mount options ignored "
  1678. "when QUOTA feature is enabled");
  1679. return 1;
  1680. }
  1681. sbi->s_jquota_fmt = m->mount_opt;
  1682. #endif
  1683. } else if (token == Opt_dax) {
  1684. #ifdef CONFIG_FS_DAX
  1685. ext4_msg(sb, KERN_WARNING,
  1686. "DAX enabled. Warning: EXPERIMENTAL, use at your own risk");
  1687. sbi->s_mount_opt |= m->mount_opt;
  1688. #else
  1689. ext4_msg(sb, KERN_INFO, "dax option not supported");
  1690. return -1;
  1691. #endif
  1692. } else if (token == Opt_data_err_abort) {
  1693. sbi->s_mount_opt |= m->mount_opt;
  1694. } else if (token == Opt_data_err_ignore) {
  1695. sbi->s_mount_opt &= ~m->mount_opt;
  1696. } else {
  1697. if (!args->from)
  1698. arg = 1;
  1699. if (m->flags & MOPT_CLEAR)
  1700. arg = !arg;
  1701. else if (unlikely(!(m->flags & MOPT_SET))) {
  1702. ext4_msg(sb, KERN_WARNING,
  1703. "buggy handling of option %s", opt);
  1704. WARN_ON(1);
  1705. return -1;
  1706. }
  1707. if (arg != 0)
  1708. sbi->s_mount_opt |= m->mount_opt;
  1709. else
  1710. sbi->s_mount_opt &= ~m->mount_opt;
  1711. }
  1712. return 1;
  1713. }
  1714. static int parse_options(char *options, struct super_block *sb,
  1715. unsigned long *journal_devnum,
  1716. unsigned int *journal_ioprio,
  1717. int is_remount)
  1718. {
  1719. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1720. char *p;
  1721. substring_t args[MAX_OPT_ARGS];
  1722. int token;
  1723. if (!options)
  1724. return 1;
  1725. while ((p = strsep(&options, ",")) != NULL) {
  1726. if (!*p)
  1727. continue;
  1728. /*
  1729. * Initialize args struct so we know whether arg was
  1730. * found; some options take optional arguments.
  1731. */
  1732. args[0].to = args[0].from = NULL;
  1733. token = match_token(p, tokens, args);
  1734. if (handle_mount_opt(sb, p, token, args, journal_devnum,
  1735. journal_ioprio, is_remount) < 0)
  1736. return 0;
  1737. }
  1738. #ifdef CONFIG_QUOTA
  1739. /*
  1740. * We do the test below only for project quotas. 'usrquota' and
  1741. * 'grpquota' mount options are allowed even without quota feature
  1742. * to support legacy quotas in quota files.
  1743. */
  1744. if (test_opt(sb, PRJQUOTA) && !ext4_has_feature_project(sb)) {
  1745. ext4_msg(sb, KERN_ERR, "Project quota feature not enabled. "
  1746. "Cannot enable project quota enforcement.");
  1747. return 0;
  1748. }
  1749. if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
  1750. if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
  1751. clear_opt(sb, USRQUOTA);
  1752. if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
  1753. clear_opt(sb, GRPQUOTA);
  1754. if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
  1755. ext4_msg(sb, KERN_ERR, "old and new quota "
  1756. "format mixing");
  1757. return 0;
  1758. }
  1759. if (!sbi->s_jquota_fmt) {
  1760. ext4_msg(sb, KERN_ERR, "journaled quota format "
  1761. "not specified");
  1762. return 0;
  1763. }
  1764. }
  1765. #endif
  1766. if (test_opt(sb, DIOREAD_NOLOCK)) {
  1767. int blocksize =
  1768. BLOCK_SIZE << le32_to_cpu(sbi->s_es->s_log_block_size);
  1769. if (blocksize < PAGE_SIZE) {
  1770. ext4_msg(sb, KERN_ERR, "can't mount with "
  1771. "dioread_nolock if block size != PAGE_SIZE");
  1772. return 0;
  1773. }
  1774. }
  1775. return 1;
  1776. }
  1777. static inline void ext4_show_quota_options(struct seq_file *seq,
  1778. struct super_block *sb)
  1779. {
  1780. #if defined(CONFIG_QUOTA)
  1781. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1782. if (sbi->s_jquota_fmt) {
  1783. char *fmtname = "";
  1784. switch (sbi->s_jquota_fmt) {
  1785. case QFMT_VFS_OLD:
  1786. fmtname = "vfsold";
  1787. break;
  1788. case QFMT_VFS_V0:
  1789. fmtname = "vfsv0";
  1790. break;
  1791. case QFMT_VFS_V1:
  1792. fmtname = "vfsv1";
  1793. break;
  1794. }
  1795. seq_printf(seq, ",jqfmt=%s", fmtname);
  1796. }
  1797. if (sbi->s_qf_names[USRQUOTA])
  1798. seq_show_option(seq, "usrjquota", sbi->s_qf_names[USRQUOTA]);
  1799. if (sbi->s_qf_names[GRPQUOTA])
  1800. seq_show_option(seq, "grpjquota", sbi->s_qf_names[GRPQUOTA]);
  1801. #endif
  1802. }
  1803. static const char *token2str(int token)
  1804. {
  1805. const struct match_token *t;
  1806. for (t = tokens; t->token != Opt_err; t++)
  1807. if (t->token == token && !strchr(t->pattern, '='))
  1808. break;
  1809. return t->pattern;
  1810. }
  1811. /*
  1812. * Show an option if
  1813. * - it's set to a non-default value OR
  1814. * - if the per-sb default is different from the global default
  1815. */
  1816. static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
  1817. int nodefs)
  1818. {
  1819. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1820. struct ext4_super_block *es = sbi->s_es;
  1821. int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
  1822. const struct mount_opts *m;
  1823. char sep = nodefs ? '\n' : ',';
  1824. #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
  1825. #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
  1826. if (sbi->s_sb_block != 1)
  1827. SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
  1828. for (m = ext4_mount_opts; m->token != Opt_err; m++) {
  1829. int want_set = m->flags & MOPT_SET;
  1830. if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
  1831. (m->flags & MOPT_CLEAR_ERR))
  1832. continue;
  1833. if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
  1834. continue; /* skip if same as the default */
  1835. if ((want_set &&
  1836. (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
  1837. (!want_set && (sbi->s_mount_opt & m->mount_opt)))
  1838. continue; /* select Opt_noFoo vs Opt_Foo */
  1839. SEQ_OPTS_PRINT("%s", token2str(m->token));
  1840. }
  1841. if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
  1842. le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
  1843. SEQ_OPTS_PRINT("resuid=%u",
  1844. from_kuid_munged(&init_user_ns, sbi->s_resuid));
  1845. if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
  1846. le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
  1847. SEQ_OPTS_PRINT("resgid=%u",
  1848. from_kgid_munged(&init_user_ns, sbi->s_resgid));
  1849. def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
  1850. if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
  1851. SEQ_OPTS_PUTS("errors=remount-ro");
  1852. if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
  1853. SEQ_OPTS_PUTS("errors=continue");
  1854. if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
  1855. SEQ_OPTS_PUTS("errors=panic");
  1856. if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
  1857. SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
  1858. if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
  1859. SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
  1860. if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
  1861. SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
  1862. if (sb->s_flags & MS_I_VERSION)
  1863. SEQ_OPTS_PUTS("i_version");
  1864. if (nodefs || sbi->s_stripe)
  1865. SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
  1866. if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
  1867. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  1868. SEQ_OPTS_PUTS("data=journal");
  1869. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  1870. SEQ_OPTS_PUTS("data=ordered");
  1871. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
  1872. SEQ_OPTS_PUTS("data=writeback");
  1873. }
  1874. if (nodefs ||
  1875. sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
  1876. SEQ_OPTS_PRINT("inode_readahead_blks=%u",
  1877. sbi->s_inode_readahead_blks);
  1878. if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
  1879. (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
  1880. SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
  1881. if (nodefs || sbi->s_max_dir_size_kb)
  1882. SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
  1883. if (test_opt(sb, DATA_ERR_ABORT))
  1884. SEQ_OPTS_PUTS("data_err=abort");
  1885. ext4_show_quota_options(seq, sb);
  1886. return 0;
  1887. }
  1888. static int ext4_show_options(struct seq_file *seq, struct dentry *root)
  1889. {
  1890. return _ext4_show_options(seq, root->d_sb, 0);
  1891. }
  1892. int ext4_seq_options_show(struct seq_file *seq, void *offset)
  1893. {
  1894. struct super_block *sb = seq->private;
  1895. int rc;
  1896. seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw");
  1897. rc = _ext4_show_options(seq, sb, 1);
  1898. seq_puts(seq, "\n");
  1899. return rc;
  1900. }
  1901. static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
  1902. int read_only)
  1903. {
  1904. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1905. int res = 0;
  1906. if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
  1907. ext4_msg(sb, KERN_ERR, "revision level too high, "
  1908. "forcing read-only mode");
  1909. res = MS_RDONLY;
  1910. }
  1911. if (read_only)
  1912. goto done;
  1913. if (!(sbi->s_mount_state & EXT4_VALID_FS))
  1914. ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
  1915. "running e2fsck is recommended");
  1916. else if (sbi->s_mount_state & EXT4_ERROR_FS)
  1917. ext4_msg(sb, KERN_WARNING,
  1918. "warning: mounting fs with errors, "
  1919. "running e2fsck is recommended");
  1920. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
  1921. le16_to_cpu(es->s_mnt_count) >=
  1922. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  1923. ext4_msg(sb, KERN_WARNING,
  1924. "warning: maximal mount count reached, "
  1925. "running e2fsck is recommended");
  1926. else if (le32_to_cpu(es->s_checkinterval) &&
  1927. (le32_to_cpu(es->s_lastcheck) +
  1928. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  1929. ext4_msg(sb, KERN_WARNING,
  1930. "warning: checktime reached, "
  1931. "running e2fsck is recommended");
  1932. if (!sbi->s_journal)
  1933. es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
  1934. if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
  1935. es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
  1936. le16_add_cpu(&es->s_mnt_count, 1);
  1937. es->s_mtime = cpu_to_le32(get_seconds());
  1938. ext4_update_dynamic_rev(sb);
  1939. if (sbi->s_journal)
  1940. ext4_set_feature_journal_needs_recovery(sb);
  1941. ext4_commit_super(sb, 1);
  1942. done:
  1943. if (test_opt(sb, DEBUG))
  1944. printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
  1945. "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
  1946. sb->s_blocksize,
  1947. sbi->s_groups_count,
  1948. EXT4_BLOCKS_PER_GROUP(sb),
  1949. EXT4_INODES_PER_GROUP(sb),
  1950. sbi->s_mount_opt, sbi->s_mount_opt2);
  1951. cleancache_init_fs(sb);
  1952. return res;
  1953. }
  1954. int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
  1955. {
  1956. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1957. struct flex_groups *new_groups;
  1958. int size;
  1959. if (!sbi->s_log_groups_per_flex)
  1960. return 0;
  1961. size = ext4_flex_group(sbi, ngroup - 1) + 1;
  1962. if (size <= sbi->s_flex_groups_allocated)
  1963. return 0;
  1964. size = roundup_pow_of_two(size * sizeof(struct flex_groups));
  1965. new_groups = kvzalloc(size, GFP_KERNEL);
  1966. if (!new_groups) {
  1967. ext4_msg(sb, KERN_ERR, "not enough memory for %d flex groups",
  1968. size / (int) sizeof(struct flex_groups));
  1969. return -ENOMEM;
  1970. }
  1971. if (sbi->s_flex_groups) {
  1972. memcpy(new_groups, sbi->s_flex_groups,
  1973. (sbi->s_flex_groups_allocated *
  1974. sizeof(struct flex_groups)));
  1975. kvfree(sbi->s_flex_groups);
  1976. }
  1977. sbi->s_flex_groups = new_groups;
  1978. sbi->s_flex_groups_allocated = size / sizeof(struct flex_groups);
  1979. return 0;
  1980. }
  1981. static int ext4_fill_flex_info(struct super_block *sb)
  1982. {
  1983. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1984. struct ext4_group_desc *gdp = NULL;
  1985. ext4_group_t flex_group;
  1986. int i, err;
  1987. sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
  1988. if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
  1989. sbi->s_log_groups_per_flex = 0;
  1990. return 1;
  1991. }
  1992. err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
  1993. if (err)
  1994. goto failed;
  1995. for (i = 0; i < sbi->s_groups_count; i++) {
  1996. gdp = ext4_get_group_desc(sb, i, NULL);
  1997. flex_group = ext4_flex_group(sbi, i);
  1998. atomic_add(ext4_free_inodes_count(sb, gdp),
  1999. &sbi->s_flex_groups[flex_group].free_inodes);
  2000. atomic64_add(ext4_free_group_clusters(sb, gdp),
  2001. &sbi->s_flex_groups[flex_group].free_clusters);
  2002. atomic_add(ext4_used_dirs_count(sb, gdp),
  2003. &sbi->s_flex_groups[flex_group].used_dirs);
  2004. }
  2005. return 1;
  2006. failed:
  2007. return 0;
  2008. }
  2009. static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
  2010. struct ext4_group_desc *gdp)
  2011. {
  2012. int offset = offsetof(struct ext4_group_desc, bg_checksum);
  2013. __u16 crc = 0;
  2014. __le32 le_group = cpu_to_le32(block_group);
  2015. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2016. if (ext4_has_metadata_csum(sbi->s_sb)) {
  2017. /* Use new metadata_csum algorithm */
  2018. __u32 csum32;
  2019. __u16 dummy_csum = 0;
  2020. csum32 = ext4_chksum(sbi, sbi->s_csum_seed, (__u8 *)&le_group,
  2021. sizeof(le_group));
  2022. csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp, offset);
  2023. csum32 = ext4_chksum(sbi, csum32, (__u8 *)&dummy_csum,
  2024. sizeof(dummy_csum));
  2025. offset += sizeof(dummy_csum);
  2026. if (offset < sbi->s_desc_size)
  2027. csum32 = ext4_chksum(sbi, csum32, (__u8 *)gdp + offset,
  2028. sbi->s_desc_size - offset);
  2029. crc = csum32 & 0xFFFF;
  2030. goto out;
  2031. }
  2032. /* old crc16 code */
  2033. if (!ext4_has_feature_gdt_csum(sb))
  2034. return 0;
  2035. crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
  2036. crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
  2037. crc = crc16(crc, (__u8 *)gdp, offset);
  2038. offset += sizeof(gdp->bg_checksum); /* skip checksum */
  2039. /* for checksum of struct ext4_group_desc do the rest...*/
  2040. if (ext4_has_feature_64bit(sb) &&
  2041. offset < le16_to_cpu(sbi->s_es->s_desc_size))
  2042. crc = crc16(crc, (__u8 *)gdp + offset,
  2043. le16_to_cpu(sbi->s_es->s_desc_size) -
  2044. offset);
  2045. out:
  2046. return cpu_to_le16(crc);
  2047. }
  2048. int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
  2049. struct ext4_group_desc *gdp)
  2050. {
  2051. if (ext4_has_group_desc_csum(sb) &&
  2052. (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
  2053. return 0;
  2054. return 1;
  2055. }
  2056. void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
  2057. struct ext4_group_desc *gdp)
  2058. {
  2059. if (!ext4_has_group_desc_csum(sb))
  2060. return;
  2061. gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
  2062. }
  2063. /* Called at mount-time, super-block is locked */
  2064. static int ext4_check_descriptors(struct super_block *sb,
  2065. ext4_fsblk_t sb_block,
  2066. ext4_group_t *first_not_zeroed)
  2067. {
  2068. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2069. ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
  2070. ext4_fsblk_t last_block;
  2071. ext4_fsblk_t block_bitmap;
  2072. ext4_fsblk_t inode_bitmap;
  2073. ext4_fsblk_t inode_table;
  2074. int flexbg_flag = 0;
  2075. ext4_group_t i, grp = sbi->s_groups_count;
  2076. if (ext4_has_feature_flex_bg(sb))
  2077. flexbg_flag = 1;
  2078. ext4_debug("Checking group descriptors");
  2079. for (i = 0; i < sbi->s_groups_count; i++) {
  2080. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  2081. if (i == sbi->s_groups_count - 1 || flexbg_flag)
  2082. last_block = ext4_blocks_count(sbi->s_es) - 1;
  2083. else
  2084. last_block = first_block +
  2085. (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  2086. if ((grp == sbi->s_groups_count) &&
  2087. !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2088. grp = i;
  2089. block_bitmap = ext4_block_bitmap(sb, gdp);
  2090. if (block_bitmap == sb_block) {
  2091. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2092. "Block bitmap for group %u overlaps "
  2093. "superblock", i);
  2094. }
  2095. if (block_bitmap < first_block || block_bitmap > last_block) {
  2096. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2097. "Block bitmap for group %u not in group "
  2098. "(block %llu)!", i, block_bitmap);
  2099. return 0;
  2100. }
  2101. inode_bitmap = ext4_inode_bitmap(sb, gdp);
  2102. if (inode_bitmap == sb_block) {
  2103. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2104. "Inode bitmap for group %u overlaps "
  2105. "superblock", i);
  2106. }
  2107. if (inode_bitmap < first_block || inode_bitmap > last_block) {
  2108. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2109. "Inode bitmap for group %u not in group "
  2110. "(block %llu)!", i, inode_bitmap);
  2111. return 0;
  2112. }
  2113. inode_table = ext4_inode_table(sb, gdp);
  2114. if (inode_table == sb_block) {
  2115. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2116. "Inode table for group %u overlaps "
  2117. "superblock", i);
  2118. }
  2119. if (inode_table < first_block ||
  2120. inode_table + sbi->s_itb_per_group - 1 > last_block) {
  2121. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2122. "Inode table for group %u not in group "
  2123. "(block %llu)!", i, inode_table);
  2124. return 0;
  2125. }
  2126. ext4_lock_group(sb, i);
  2127. if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
  2128. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2129. "Checksum for group %u failed (%u!=%u)",
  2130. i, le16_to_cpu(ext4_group_desc_csum(sb, i,
  2131. gdp)), le16_to_cpu(gdp->bg_checksum));
  2132. if (!(sb->s_flags & MS_RDONLY)) {
  2133. ext4_unlock_group(sb, i);
  2134. return 0;
  2135. }
  2136. }
  2137. ext4_unlock_group(sb, i);
  2138. if (!flexbg_flag)
  2139. first_block += EXT4_BLOCKS_PER_GROUP(sb);
  2140. }
  2141. if (NULL != first_not_zeroed)
  2142. *first_not_zeroed = grp;
  2143. return 1;
  2144. }
  2145. /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
  2146. * the superblock) which were deleted from all directories, but held open by
  2147. * a process at the time of a crash. We walk the list and try to delete these
  2148. * inodes at recovery time (only with a read-write filesystem).
  2149. *
  2150. * In order to keep the orphan inode chain consistent during traversal (in
  2151. * case of crash during recovery), we link each inode into the superblock
  2152. * orphan list_head and handle it the same way as an inode deletion during
  2153. * normal operation (which journals the operations for us).
  2154. *
  2155. * We only do an iget() and an iput() on each inode, which is very safe if we
  2156. * accidentally point at an in-use or already deleted inode. The worst that
  2157. * can happen in this case is that we get a "bit already cleared" message from
  2158. * ext4_free_inode(). The only reason we would point at a wrong inode is if
  2159. * e2fsck was run on this filesystem, and it must have already done the orphan
  2160. * inode cleanup for us, so we can safely abort without any further action.
  2161. */
  2162. static void ext4_orphan_cleanup(struct super_block *sb,
  2163. struct ext4_super_block *es)
  2164. {
  2165. unsigned int s_flags = sb->s_flags;
  2166. int ret, nr_orphans = 0, nr_truncates = 0;
  2167. #ifdef CONFIG_QUOTA
  2168. int i;
  2169. #endif
  2170. if (!es->s_last_orphan) {
  2171. jbd_debug(4, "no orphan inodes to clean up\n");
  2172. return;
  2173. }
  2174. if (bdev_read_only(sb->s_bdev)) {
  2175. ext4_msg(sb, KERN_ERR, "write access "
  2176. "unavailable, skipping orphan cleanup");
  2177. return;
  2178. }
  2179. /* Check if feature set would not allow a r/w mount */
  2180. if (!ext4_feature_set_ok(sb, 0)) {
  2181. ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
  2182. "unknown ROCOMPAT features");
  2183. return;
  2184. }
  2185. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  2186. /* don't clear list on RO mount w/ errors */
  2187. if (es->s_last_orphan && !(s_flags & MS_RDONLY)) {
  2188. ext4_msg(sb, KERN_INFO, "Errors on filesystem, "
  2189. "clearing orphan list.\n");
  2190. es->s_last_orphan = 0;
  2191. }
  2192. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  2193. return;
  2194. }
  2195. if (s_flags & MS_RDONLY) {
  2196. ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
  2197. sb->s_flags &= ~MS_RDONLY;
  2198. }
  2199. #ifdef CONFIG_QUOTA
  2200. /* Needed for iput() to work correctly and not trash data */
  2201. sb->s_flags |= MS_ACTIVE;
  2202. /* Turn on quotas so that they are updated correctly */
  2203. for (i = 0; i < EXT4_MAXQUOTAS; i++) {
  2204. if (EXT4_SB(sb)->s_qf_names[i]) {
  2205. int ret = ext4_quota_on_mount(sb, i);
  2206. if (ret < 0)
  2207. ext4_msg(sb, KERN_ERR,
  2208. "Cannot turn on journaled "
  2209. "quota: error %d", ret);
  2210. }
  2211. }
  2212. #endif
  2213. while (es->s_last_orphan) {
  2214. struct inode *inode;
  2215. /*
  2216. * We may have encountered an error during cleanup; if
  2217. * so, skip the rest.
  2218. */
  2219. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  2220. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  2221. es->s_last_orphan = 0;
  2222. break;
  2223. }
  2224. inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
  2225. if (IS_ERR(inode)) {
  2226. es->s_last_orphan = 0;
  2227. break;
  2228. }
  2229. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  2230. dquot_initialize(inode);
  2231. if (inode->i_nlink) {
  2232. if (test_opt(sb, DEBUG))
  2233. ext4_msg(sb, KERN_DEBUG,
  2234. "%s: truncating inode %lu to %lld bytes",
  2235. __func__, inode->i_ino, inode->i_size);
  2236. jbd_debug(2, "truncating inode %lu to %lld bytes\n",
  2237. inode->i_ino, inode->i_size);
  2238. inode_lock(inode);
  2239. truncate_inode_pages(inode->i_mapping, inode->i_size);
  2240. ret = ext4_truncate(inode);
  2241. if (ret)
  2242. ext4_std_error(inode->i_sb, ret);
  2243. inode_unlock(inode);
  2244. nr_truncates++;
  2245. } else {
  2246. if (test_opt(sb, DEBUG))
  2247. ext4_msg(sb, KERN_DEBUG,
  2248. "%s: deleting unreferenced inode %lu",
  2249. __func__, inode->i_ino);
  2250. jbd_debug(2, "deleting unreferenced inode %lu\n",
  2251. inode->i_ino);
  2252. nr_orphans++;
  2253. }
  2254. iput(inode); /* The delete magic happens here! */
  2255. }
  2256. #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
  2257. if (nr_orphans)
  2258. ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
  2259. PLURAL(nr_orphans));
  2260. if (nr_truncates)
  2261. ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
  2262. PLURAL(nr_truncates));
  2263. #ifdef CONFIG_QUOTA
  2264. /* Turn quotas off */
  2265. for (i = 0; i < EXT4_MAXQUOTAS; i++) {
  2266. if (sb_dqopt(sb)->files[i])
  2267. dquot_quota_off(sb, i);
  2268. }
  2269. #endif
  2270. sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  2271. }
  2272. /*
  2273. * Maximal extent format file size.
  2274. * Resulting logical blkno at s_maxbytes must fit in our on-disk
  2275. * extent format containers, within a sector_t, and within i_blocks
  2276. * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
  2277. * so that won't be a limiting factor.
  2278. *
  2279. * However there is other limiting factor. We do store extents in the form
  2280. * of starting block and length, hence the resulting length of the extent
  2281. * covering maximum file size must fit into on-disk format containers as
  2282. * well. Given that length is always by 1 unit bigger than max unit (because
  2283. * we count 0 as well) we have to lower the s_maxbytes by one fs block.
  2284. *
  2285. * Note, this does *not* consider any metadata overhead for vfs i_blocks.
  2286. */
  2287. static loff_t ext4_max_size(int blkbits, int has_huge_files)
  2288. {
  2289. loff_t res;
  2290. loff_t upper_limit = MAX_LFS_FILESIZE;
  2291. /* small i_blocks in vfs inode? */
  2292. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  2293. /*
  2294. * CONFIG_LBDAF is not enabled implies the inode
  2295. * i_block represent total blocks in 512 bytes
  2296. * 32 == size of vfs inode i_blocks * 8
  2297. */
  2298. upper_limit = (1LL << 32) - 1;
  2299. /* total blocks in file system block size */
  2300. upper_limit >>= (blkbits - 9);
  2301. upper_limit <<= blkbits;
  2302. }
  2303. /*
  2304. * 32-bit extent-start container, ee_block. We lower the maxbytes
  2305. * by one fs block, so ee_len can cover the extent of maximum file
  2306. * size
  2307. */
  2308. res = (1LL << 32) - 1;
  2309. res <<= blkbits;
  2310. /* Sanity check against vm- & vfs- imposed limits */
  2311. if (res > upper_limit)
  2312. res = upper_limit;
  2313. return res;
  2314. }
  2315. /*
  2316. * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
  2317. * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
  2318. * We need to be 1 filesystem block less than the 2^48 sector limit.
  2319. */
  2320. static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
  2321. {
  2322. loff_t res = EXT4_NDIR_BLOCKS;
  2323. int meta_blocks;
  2324. loff_t upper_limit;
  2325. /* This is calculated to be the largest file size for a dense, block
  2326. * mapped file such that the file's total number of 512-byte sectors,
  2327. * including data and all indirect blocks, does not exceed (2^48 - 1).
  2328. *
  2329. * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
  2330. * number of 512-byte sectors of the file.
  2331. */
  2332. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  2333. /*
  2334. * !has_huge_files or CONFIG_LBDAF not enabled implies that
  2335. * the inode i_block field represents total file blocks in
  2336. * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
  2337. */
  2338. upper_limit = (1LL << 32) - 1;
  2339. /* total blocks in file system block size */
  2340. upper_limit >>= (bits - 9);
  2341. } else {
  2342. /*
  2343. * We use 48 bit ext4_inode i_blocks
  2344. * With EXT4_HUGE_FILE_FL set the i_blocks
  2345. * represent total number of blocks in
  2346. * file system block size
  2347. */
  2348. upper_limit = (1LL << 48) - 1;
  2349. }
  2350. /* indirect blocks */
  2351. meta_blocks = 1;
  2352. /* double indirect blocks */
  2353. meta_blocks += 1 + (1LL << (bits-2));
  2354. /* tripple indirect blocks */
  2355. meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
  2356. upper_limit -= meta_blocks;
  2357. upper_limit <<= bits;
  2358. res += 1LL << (bits-2);
  2359. res += 1LL << (2*(bits-2));
  2360. res += 1LL << (3*(bits-2));
  2361. res <<= bits;
  2362. if (res > upper_limit)
  2363. res = upper_limit;
  2364. if (res > MAX_LFS_FILESIZE)
  2365. res = MAX_LFS_FILESIZE;
  2366. return res;
  2367. }
  2368. static ext4_fsblk_t descriptor_loc(struct super_block *sb,
  2369. ext4_fsblk_t logical_sb_block, int nr)
  2370. {
  2371. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2372. ext4_group_t bg, first_meta_bg;
  2373. int has_super = 0;
  2374. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  2375. if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
  2376. return logical_sb_block + nr + 1;
  2377. bg = sbi->s_desc_per_block * nr;
  2378. if (ext4_bg_has_super(sb, bg))
  2379. has_super = 1;
  2380. /*
  2381. * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
  2382. * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled
  2383. * on modern mke2fs or blksize > 1k on older mke2fs) then we must
  2384. * compensate.
  2385. */
  2386. if (sb->s_blocksize == 1024 && nr == 0 &&
  2387. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block) == 0)
  2388. has_super++;
  2389. return (has_super + ext4_group_first_block_no(sb, bg));
  2390. }
  2391. /**
  2392. * ext4_get_stripe_size: Get the stripe size.
  2393. * @sbi: In memory super block info
  2394. *
  2395. * If we have specified it via mount option, then
  2396. * use the mount option value. If the value specified at mount time is
  2397. * greater than the blocks per group use the super block value.
  2398. * If the super block value is greater than blocks per group return 0.
  2399. * Allocator needs it be less than blocks per group.
  2400. *
  2401. */
  2402. static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
  2403. {
  2404. unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
  2405. unsigned long stripe_width =
  2406. le32_to_cpu(sbi->s_es->s_raid_stripe_width);
  2407. int ret;
  2408. if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
  2409. ret = sbi->s_stripe;
  2410. else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
  2411. ret = stripe_width;
  2412. else if (stride && stride <= sbi->s_blocks_per_group)
  2413. ret = stride;
  2414. else
  2415. ret = 0;
  2416. /*
  2417. * If the stripe width is 1, this makes no sense and
  2418. * we set it to 0 to turn off stripe handling code.
  2419. */
  2420. if (ret <= 1)
  2421. ret = 0;
  2422. return ret;
  2423. }
  2424. /*
  2425. * Check whether this filesystem can be mounted based on
  2426. * the features present and the RDONLY/RDWR mount requested.
  2427. * Returns 1 if this filesystem can be mounted as requested,
  2428. * 0 if it cannot be.
  2429. */
  2430. static int ext4_feature_set_ok(struct super_block *sb, int readonly)
  2431. {
  2432. if (ext4_has_unknown_ext4_incompat_features(sb)) {
  2433. ext4_msg(sb, KERN_ERR,
  2434. "Couldn't mount because of "
  2435. "unsupported optional features (%x)",
  2436. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
  2437. ~EXT4_FEATURE_INCOMPAT_SUPP));
  2438. return 0;
  2439. }
  2440. if (readonly)
  2441. return 1;
  2442. if (ext4_has_feature_readonly(sb)) {
  2443. ext4_msg(sb, KERN_INFO, "filesystem is read-only");
  2444. sb->s_flags |= MS_RDONLY;
  2445. return 1;
  2446. }
  2447. /* Check that feature set is OK for a read-write mount */
  2448. if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
  2449. ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
  2450. "unsupported optional features (%x)",
  2451. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
  2452. ~EXT4_FEATURE_RO_COMPAT_SUPP));
  2453. return 0;
  2454. }
  2455. /*
  2456. * Large file size enabled file system can only be mounted
  2457. * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
  2458. */
  2459. if (ext4_has_feature_huge_file(sb)) {
  2460. if (sizeof(blkcnt_t) < sizeof(u64)) {
  2461. ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
  2462. "cannot be mounted RDWR without "
  2463. "CONFIG_LBDAF");
  2464. return 0;
  2465. }
  2466. }
  2467. if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
  2468. ext4_msg(sb, KERN_ERR,
  2469. "Can't support bigalloc feature without "
  2470. "extents feature\n");
  2471. return 0;
  2472. }
  2473. #ifndef CONFIG_QUOTA
  2474. if (ext4_has_feature_quota(sb) && !readonly) {
  2475. ext4_msg(sb, KERN_ERR,
  2476. "Filesystem with quota feature cannot be mounted RDWR "
  2477. "without CONFIG_QUOTA");
  2478. return 0;
  2479. }
  2480. if (ext4_has_feature_project(sb) && !readonly) {
  2481. ext4_msg(sb, KERN_ERR,
  2482. "Filesystem with project quota feature cannot be mounted RDWR "
  2483. "without CONFIG_QUOTA");
  2484. return 0;
  2485. }
  2486. #endif /* CONFIG_QUOTA */
  2487. return 1;
  2488. }
  2489. /*
  2490. * This function is called once a day if we have errors logged
  2491. * on the file system
  2492. */
  2493. static void print_daily_error_info(unsigned long arg)
  2494. {
  2495. struct super_block *sb = (struct super_block *) arg;
  2496. struct ext4_sb_info *sbi;
  2497. struct ext4_super_block *es;
  2498. sbi = EXT4_SB(sb);
  2499. es = sbi->s_es;
  2500. if (es->s_error_count)
  2501. /* fsck newer than v1.41.13 is needed to clean this condition. */
  2502. ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
  2503. le32_to_cpu(es->s_error_count));
  2504. if (es->s_first_error_time) {
  2505. printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %u: %.*s:%d",
  2506. sb->s_id, le32_to_cpu(es->s_first_error_time),
  2507. (int) sizeof(es->s_first_error_func),
  2508. es->s_first_error_func,
  2509. le32_to_cpu(es->s_first_error_line));
  2510. if (es->s_first_error_ino)
  2511. printk(KERN_CONT ": inode %u",
  2512. le32_to_cpu(es->s_first_error_ino));
  2513. if (es->s_first_error_block)
  2514. printk(KERN_CONT ": block %llu", (unsigned long long)
  2515. le64_to_cpu(es->s_first_error_block));
  2516. printk(KERN_CONT "\n");
  2517. }
  2518. if (es->s_last_error_time) {
  2519. printk(KERN_NOTICE "EXT4-fs (%s): last error at time %u: %.*s:%d",
  2520. sb->s_id, le32_to_cpu(es->s_last_error_time),
  2521. (int) sizeof(es->s_last_error_func),
  2522. es->s_last_error_func,
  2523. le32_to_cpu(es->s_last_error_line));
  2524. if (es->s_last_error_ino)
  2525. printk(KERN_CONT ": inode %u",
  2526. le32_to_cpu(es->s_last_error_ino));
  2527. if (es->s_last_error_block)
  2528. printk(KERN_CONT ": block %llu", (unsigned long long)
  2529. le64_to_cpu(es->s_last_error_block));
  2530. printk(KERN_CONT "\n");
  2531. }
  2532. mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
  2533. }
  2534. /* Find next suitable group and run ext4_init_inode_table */
  2535. static int ext4_run_li_request(struct ext4_li_request *elr)
  2536. {
  2537. struct ext4_group_desc *gdp = NULL;
  2538. ext4_group_t group, ngroups;
  2539. struct super_block *sb;
  2540. unsigned long timeout = 0;
  2541. int ret = 0;
  2542. sb = elr->lr_super;
  2543. ngroups = EXT4_SB(sb)->s_groups_count;
  2544. for (group = elr->lr_next_group; group < ngroups; group++) {
  2545. gdp = ext4_get_group_desc(sb, group, NULL);
  2546. if (!gdp) {
  2547. ret = 1;
  2548. break;
  2549. }
  2550. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2551. break;
  2552. }
  2553. if (group >= ngroups)
  2554. ret = 1;
  2555. if (!ret) {
  2556. timeout = jiffies;
  2557. ret = ext4_init_inode_table(sb, group,
  2558. elr->lr_timeout ? 0 : 1);
  2559. if (elr->lr_timeout == 0) {
  2560. timeout = (jiffies - timeout) *
  2561. elr->lr_sbi->s_li_wait_mult;
  2562. elr->lr_timeout = timeout;
  2563. }
  2564. elr->lr_next_sched = jiffies + elr->lr_timeout;
  2565. elr->lr_next_group = group + 1;
  2566. }
  2567. return ret;
  2568. }
  2569. /*
  2570. * Remove lr_request from the list_request and free the
  2571. * request structure. Should be called with li_list_mtx held
  2572. */
  2573. static void ext4_remove_li_request(struct ext4_li_request *elr)
  2574. {
  2575. struct ext4_sb_info *sbi;
  2576. if (!elr)
  2577. return;
  2578. sbi = elr->lr_sbi;
  2579. list_del(&elr->lr_request);
  2580. sbi->s_li_request = NULL;
  2581. kfree(elr);
  2582. }
  2583. static void ext4_unregister_li_request(struct super_block *sb)
  2584. {
  2585. mutex_lock(&ext4_li_mtx);
  2586. if (!ext4_li_info) {
  2587. mutex_unlock(&ext4_li_mtx);
  2588. return;
  2589. }
  2590. mutex_lock(&ext4_li_info->li_list_mtx);
  2591. ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
  2592. mutex_unlock(&ext4_li_info->li_list_mtx);
  2593. mutex_unlock(&ext4_li_mtx);
  2594. }
  2595. static struct task_struct *ext4_lazyinit_task;
  2596. /*
  2597. * This is the function where ext4lazyinit thread lives. It walks
  2598. * through the request list searching for next scheduled filesystem.
  2599. * When such a fs is found, run the lazy initialization request
  2600. * (ext4_rn_li_request) and keep track of the time spend in this
  2601. * function. Based on that time we compute next schedule time of
  2602. * the request. When walking through the list is complete, compute
  2603. * next waking time and put itself into sleep.
  2604. */
  2605. static int ext4_lazyinit_thread(void *arg)
  2606. {
  2607. struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
  2608. struct list_head *pos, *n;
  2609. struct ext4_li_request *elr;
  2610. unsigned long next_wakeup, cur;
  2611. BUG_ON(NULL == eli);
  2612. cont_thread:
  2613. while (true) {
  2614. next_wakeup = MAX_JIFFY_OFFSET;
  2615. mutex_lock(&eli->li_list_mtx);
  2616. if (list_empty(&eli->li_request_list)) {
  2617. mutex_unlock(&eli->li_list_mtx);
  2618. goto exit_thread;
  2619. }
  2620. list_for_each_safe(pos, n, &eli->li_request_list) {
  2621. int err = 0;
  2622. int progress = 0;
  2623. elr = list_entry(pos, struct ext4_li_request,
  2624. lr_request);
  2625. if (time_before(jiffies, elr->lr_next_sched)) {
  2626. if (time_before(elr->lr_next_sched, next_wakeup))
  2627. next_wakeup = elr->lr_next_sched;
  2628. continue;
  2629. }
  2630. if (down_read_trylock(&elr->lr_super->s_umount)) {
  2631. if (sb_start_write_trylock(elr->lr_super)) {
  2632. progress = 1;
  2633. /*
  2634. * We hold sb->s_umount, sb can not
  2635. * be removed from the list, it is
  2636. * now safe to drop li_list_mtx
  2637. */
  2638. mutex_unlock(&eli->li_list_mtx);
  2639. err = ext4_run_li_request(elr);
  2640. sb_end_write(elr->lr_super);
  2641. mutex_lock(&eli->li_list_mtx);
  2642. n = pos->next;
  2643. }
  2644. up_read((&elr->lr_super->s_umount));
  2645. }
  2646. /* error, remove the lazy_init job */
  2647. if (err) {
  2648. ext4_remove_li_request(elr);
  2649. continue;
  2650. }
  2651. if (!progress) {
  2652. elr->lr_next_sched = jiffies +
  2653. (prandom_u32()
  2654. % (EXT4_DEF_LI_MAX_START_DELAY * HZ));
  2655. }
  2656. if (time_before(elr->lr_next_sched, next_wakeup))
  2657. next_wakeup = elr->lr_next_sched;
  2658. }
  2659. mutex_unlock(&eli->li_list_mtx);
  2660. try_to_freeze();
  2661. cur = jiffies;
  2662. if ((time_after_eq(cur, next_wakeup)) ||
  2663. (MAX_JIFFY_OFFSET == next_wakeup)) {
  2664. cond_resched();
  2665. continue;
  2666. }
  2667. schedule_timeout_interruptible(next_wakeup - cur);
  2668. if (kthread_should_stop()) {
  2669. ext4_clear_request_list();
  2670. goto exit_thread;
  2671. }
  2672. }
  2673. exit_thread:
  2674. /*
  2675. * It looks like the request list is empty, but we need
  2676. * to check it under the li_list_mtx lock, to prevent any
  2677. * additions into it, and of course we should lock ext4_li_mtx
  2678. * to atomically free the list and ext4_li_info, because at
  2679. * this point another ext4 filesystem could be registering
  2680. * new one.
  2681. */
  2682. mutex_lock(&ext4_li_mtx);
  2683. mutex_lock(&eli->li_list_mtx);
  2684. if (!list_empty(&eli->li_request_list)) {
  2685. mutex_unlock(&eli->li_list_mtx);
  2686. mutex_unlock(&ext4_li_mtx);
  2687. goto cont_thread;
  2688. }
  2689. mutex_unlock(&eli->li_list_mtx);
  2690. kfree(ext4_li_info);
  2691. ext4_li_info = NULL;
  2692. mutex_unlock(&ext4_li_mtx);
  2693. return 0;
  2694. }
  2695. static void ext4_clear_request_list(void)
  2696. {
  2697. struct list_head *pos, *n;
  2698. struct ext4_li_request *elr;
  2699. mutex_lock(&ext4_li_info->li_list_mtx);
  2700. list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
  2701. elr = list_entry(pos, struct ext4_li_request,
  2702. lr_request);
  2703. ext4_remove_li_request(elr);
  2704. }
  2705. mutex_unlock(&ext4_li_info->li_list_mtx);
  2706. }
  2707. static int ext4_run_lazyinit_thread(void)
  2708. {
  2709. ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
  2710. ext4_li_info, "ext4lazyinit");
  2711. if (IS_ERR(ext4_lazyinit_task)) {
  2712. int err = PTR_ERR(ext4_lazyinit_task);
  2713. ext4_clear_request_list();
  2714. kfree(ext4_li_info);
  2715. ext4_li_info = NULL;
  2716. printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
  2717. "initialization thread\n",
  2718. err);
  2719. return err;
  2720. }
  2721. ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
  2722. return 0;
  2723. }
  2724. /*
  2725. * Check whether it make sense to run itable init. thread or not.
  2726. * If there is at least one uninitialized inode table, return
  2727. * corresponding group number, else the loop goes through all
  2728. * groups and return total number of groups.
  2729. */
  2730. static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
  2731. {
  2732. ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
  2733. struct ext4_group_desc *gdp = NULL;
  2734. for (group = 0; group < ngroups; group++) {
  2735. gdp = ext4_get_group_desc(sb, group, NULL);
  2736. if (!gdp)
  2737. continue;
  2738. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2739. break;
  2740. }
  2741. return group;
  2742. }
  2743. static int ext4_li_info_new(void)
  2744. {
  2745. struct ext4_lazy_init *eli = NULL;
  2746. eli = kzalloc(sizeof(*eli), GFP_KERNEL);
  2747. if (!eli)
  2748. return -ENOMEM;
  2749. INIT_LIST_HEAD(&eli->li_request_list);
  2750. mutex_init(&eli->li_list_mtx);
  2751. eli->li_state |= EXT4_LAZYINIT_QUIT;
  2752. ext4_li_info = eli;
  2753. return 0;
  2754. }
  2755. static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
  2756. ext4_group_t start)
  2757. {
  2758. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2759. struct ext4_li_request *elr;
  2760. elr = kzalloc(sizeof(*elr), GFP_KERNEL);
  2761. if (!elr)
  2762. return NULL;
  2763. elr->lr_super = sb;
  2764. elr->lr_sbi = sbi;
  2765. elr->lr_next_group = start;
  2766. /*
  2767. * Randomize first schedule time of the request to
  2768. * spread the inode table initialization requests
  2769. * better.
  2770. */
  2771. elr->lr_next_sched = jiffies + (prandom_u32() %
  2772. (EXT4_DEF_LI_MAX_START_DELAY * HZ));
  2773. return elr;
  2774. }
  2775. int ext4_register_li_request(struct super_block *sb,
  2776. ext4_group_t first_not_zeroed)
  2777. {
  2778. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2779. struct ext4_li_request *elr = NULL;
  2780. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  2781. int ret = 0;
  2782. mutex_lock(&ext4_li_mtx);
  2783. if (sbi->s_li_request != NULL) {
  2784. /*
  2785. * Reset timeout so it can be computed again, because
  2786. * s_li_wait_mult might have changed.
  2787. */
  2788. sbi->s_li_request->lr_timeout = 0;
  2789. goto out;
  2790. }
  2791. if (first_not_zeroed == ngroups ||
  2792. (sb->s_flags & MS_RDONLY) ||
  2793. !test_opt(sb, INIT_INODE_TABLE))
  2794. goto out;
  2795. elr = ext4_li_request_new(sb, first_not_zeroed);
  2796. if (!elr) {
  2797. ret = -ENOMEM;
  2798. goto out;
  2799. }
  2800. if (NULL == ext4_li_info) {
  2801. ret = ext4_li_info_new();
  2802. if (ret)
  2803. goto out;
  2804. }
  2805. mutex_lock(&ext4_li_info->li_list_mtx);
  2806. list_add(&elr->lr_request, &ext4_li_info->li_request_list);
  2807. mutex_unlock(&ext4_li_info->li_list_mtx);
  2808. sbi->s_li_request = elr;
  2809. /*
  2810. * set elr to NULL here since it has been inserted to
  2811. * the request_list and the removal and free of it is
  2812. * handled by ext4_clear_request_list from now on.
  2813. */
  2814. elr = NULL;
  2815. if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
  2816. ret = ext4_run_lazyinit_thread();
  2817. if (ret)
  2818. goto out;
  2819. }
  2820. out:
  2821. mutex_unlock(&ext4_li_mtx);
  2822. if (ret)
  2823. kfree(elr);
  2824. return ret;
  2825. }
  2826. /*
  2827. * We do not need to lock anything since this is called on
  2828. * module unload.
  2829. */
  2830. static void ext4_destroy_lazyinit_thread(void)
  2831. {
  2832. /*
  2833. * If thread exited earlier
  2834. * there's nothing to be done.
  2835. */
  2836. if (!ext4_li_info || !ext4_lazyinit_task)
  2837. return;
  2838. kthread_stop(ext4_lazyinit_task);
  2839. }
  2840. static int set_journal_csum_feature_set(struct super_block *sb)
  2841. {
  2842. int ret = 1;
  2843. int compat, incompat;
  2844. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2845. if (ext4_has_metadata_csum(sb)) {
  2846. /* journal checksum v3 */
  2847. compat = 0;
  2848. incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
  2849. } else {
  2850. /* journal checksum v1 */
  2851. compat = JBD2_FEATURE_COMPAT_CHECKSUM;
  2852. incompat = 0;
  2853. }
  2854. jbd2_journal_clear_features(sbi->s_journal,
  2855. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  2856. JBD2_FEATURE_INCOMPAT_CSUM_V3 |
  2857. JBD2_FEATURE_INCOMPAT_CSUM_V2);
  2858. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  2859. ret = jbd2_journal_set_features(sbi->s_journal,
  2860. compat, 0,
  2861. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
  2862. incompat);
  2863. } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
  2864. ret = jbd2_journal_set_features(sbi->s_journal,
  2865. compat, 0,
  2866. incompat);
  2867. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  2868. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2869. } else {
  2870. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  2871. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2872. }
  2873. return ret;
  2874. }
  2875. /*
  2876. * Note: calculating the overhead so we can be compatible with
  2877. * historical BSD practice is quite difficult in the face of
  2878. * clusters/bigalloc. This is because multiple metadata blocks from
  2879. * different block group can end up in the same allocation cluster.
  2880. * Calculating the exact overhead in the face of clustered allocation
  2881. * requires either O(all block bitmaps) in memory or O(number of block
  2882. * groups**2) in time. We will still calculate the superblock for
  2883. * older file systems --- and if we come across with a bigalloc file
  2884. * system with zero in s_overhead_clusters the estimate will be close to
  2885. * correct especially for very large cluster sizes --- but for newer
  2886. * file systems, it's better to calculate this figure once at mkfs
  2887. * time, and store it in the superblock. If the superblock value is
  2888. * present (even for non-bigalloc file systems), we will use it.
  2889. */
  2890. static int count_overhead(struct super_block *sb, ext4_group_t grp,
  2891. char *buf)
  2892. {
  2893. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2894. struct ext4_group_desc *gdp;
  2895. ext4_fsblk_t first_block, last_block, b;
  2896. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  2897. int s, j, count = 0;
  2898. if (!ext4_has_feature_bigalloc(sb))
  2899. return (ext4_bg_has_super(sb, grp) + ext4_bg_num_gdb(sb, grp) +
  2900. sbi->s_itb_per_group + 2);
  2901. first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
  2902. (grp * EXT4_BLOCKS_PER_GROUP(sb));
  2903. last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
  2904. for (i = 0; i < ngroups; i++) {
  2905. gdp = ext4_get_group_desc(sb, i, NULL);
  2906. b = ext4_block_bitmap(sb, gdp);
  2907. if (b >= first_block && b <= last_block) {
  2908. ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
  2909. count++;
  2910. }
  2911. b = ext4_inode_bitmap(sb, gdp);
  2912. if (b >= first_block && b <= last_block) {
  2913. ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
  2914. count++;
  2915. }
  2916. b = ext4_inode_table(sb, gdp);
  2917. if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
  2918. for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
  2919. int c = EXT4_B2C(sbi, b - first_block);
  2920. ext4_set_bit(c, buf);
  2921. count++;
  2922. }
  2923. if (i != grp)
  2924. continue;
  2925. s = 0;
  2926. if (ext4_bg_has_super(sb, grp)) {
  2927. ext4_set_bit(s++, buf);
  2928. count++;
  2929. }
  2930. j = ext4_bg_num_gdb(sb, grp);
  2931. if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
  2932. ext4_error(sb, "Invalid number of block group "
  2933. "descriptor blocks: %d", j);
  2934. j = EXT4_BLOCKS_PER_GROUP(sb) - s;
  2935. }
  2936. count += j;
  2937. for (; j > 0; j--)
  2938. ext4_set_bit(EXT4_B2C(sbi, s++), buf);
  2939. }
  2940. if (!count)
  2941. return 0;
  2942. return EXT4_CLUSTERS_PER_GROUP(sb) -
  2943. ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
  2944. }
  2945. /*
  2946. * Compute the overhead and stash it in sbi->s_overhead
  2947. */
  2948. int ext4_calculate_overhead(struct super_block *sb)
  2949. {
  2950. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2951. struct ext4_super_block *es = sbi->s_es;
  2952. struct inode *j_inode;
  2953. unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
  2954. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  2955. ext4_fsblk_t overhead = 0;
  2956. char *buf = (char *) get_zeroed_page(GFP_NOFS);
  2957. if (!buf)
  2958. return -ENOMEM;
  2959. /*
  2960. * Compute the overhead (FS structures). This is constant
  2961. * for a given filesystem unless the number of block groups
  2962. * changes so we cache the previous value until it does.
  2963. */
  2964. /*
  2965. * All of the blocks before first_data_block are overhead
  2966. */
  2967. overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
  2968. /*
  2969. * Add the overhead found in each block group
  2970. */
  2971. for (i = 0; i < ngroups; i++) {
  2972. int blks;
  2973. blks = count_overhead(sb, i, buf);
  2974. overhead += blks;
  2975. if (blks)
  2976. memset(buf, 0, PAGE_SIZE);
  2977. cond_resched();
  2978. }
  2979. /*
  2980. * Add the internal journal blocks whether the journal has been
  2981. * loaded or not
  2982. */
  2983. if (sbi->s_journal && !sbi->journal_bdev)
  2984. overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_maxlen);
  2985. else if (ext4_has_feature_journal(sb) && !sbi->s_journal) {
  2986. j_inode = ext4_get_journal_inode(sb, j_inum);
  2987. if (j_inode) {
  2988. j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
  2989. overhead += EXT4_NUM_B2C(sbi, j_blocks);
  2990. iput(j_inode);
  2991. } else {
  2992. ext4_msg(sb, KERN_ERR, "can't get journal size");
  2993. }
  2994. }
  2995. sbi->s_overhead = overhead;
  2996. smp_wmb();
  2997. free_page((unsigned long) buf);
  2998. return 0;
  2999. }
  3000. static void ext4_set_resv_clusters(struct super_block *sb)
  3001. {
  3002. ext4_fsblk_t resv_clusters;
  3003. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3004. /*
  3005. * There's no need to reserve anything when we aren't using extents.
  3006. * The space estimates are exact, there are no unwritten extents,
  3007. * hole punching doesn't need new metadata... This is needed especially
  3008. * to keep ext2/3 backward compatibility.
  3009. */
  3010. if (!ext4_has_feature_extents(sb))
  3011. return;
  3012. /*
  3013. * By default we reserve 2% or 4096 clusters, whichever is smaller.
  3014. * This should cover the situations where we can not afford to run
  3015. * out of space like for example punch hole, or converting
  3016. * unwritten extents in delalloc path. In most cases such
  3017. * allocation would require 1, or 2 blocks, higher numbers are
  3018. * very rare.
  3019. */
  3020. resv_clusters = (ext4_blocks_count(sbi->s_es) >>
  3021. sbi->s_cluster_bits);
  3022. do_div(resv_clusters, 50);
  3023. resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
  3024. atomic64_set(&sbi->s_resv_clusters, resv_clusters);
  3025. }
  3026. static int ext4_fill_super(struct super_block *sb, void *data, int silent)
  3027. {
  3028. char *orig_data = kstrdup(data, GFP_KERNEL);
  3029. struct buffer_head *bh;
  3030. struct ext4_super_block *es = NULL;
  3031. struct ext4_sb_info *sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  3032. ext4_fsblk_t block;
  3033. ext4_fsblk_t sb_block = get_sb_block(&data);
  3034. ext4_fsblk_t logical_sb_block;
  3035. unsigned long offset = 0;
  3036. unsigned long journal_devnum = 0;
  3037. unsigned long def_mount_opts;
  3038. struct inode *root;
  3039. const char *descr;
  3040. int ret = -ENOMEM;
  3041. int blocksize, clustersize;
  3042. unsigned int db_count;
  3043. unsigned int i;
  3044. int needs_recovery, has_huge_files, has_bigalloc;
  3045. __u64 blocks_count;
  3046. int err = 0;
  3047. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  3048. ext4_group_t first_not_zeroed;
  3049. if ((data && !orig_data) || !sbi)
  3050. goto out_free_base;
  3051. sbi->s_blockgroup_lock =
  3052. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  3053. if (!sbi->s_blockgroup_lock)
  3054. goto out_free_base;
  3055. sb->s_fs_info = sbi;
  3056. sbi->s_sb = sb;
  3057. sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
  3058. sbi->s_sb_block = sb_block;
  3059. if (sb->s_bdev->bd_part)
  3060. sbi->s_sectors_written_start =
  3061. part_stat_read(sb->s_bdev->bd_part, sectors[1]);
  3062. /* Cleanup superblock name */
  3063. strreplace(sb->s_id, '/', '!');
  3064. /* -EINVAL is default */
  3065. ret = -EINVAL;
  3066. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  3067. if (!blocksize) {
  3068. ext4_msg(sb, KERN_ERR, "unable to set blocksize");
  3069. goto out_fail;
  3070. }
  3071. /*
  3072. * The ext4 superblock will not be buffer aligned for other than 1kB
  3073. * block sizes. We need to calculate the offset from buffer start.
  3074. */
  3075. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  3076. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  3077. offset = do_div(logical_sb_block, blocksize);
  3078. } else {
  3079. logical_sb_block = sb_block;
  3080. }
  3081. if (!(bh = sb_bread_unmovable(sb, logical_sb_block))) {
  3082. ext4_msg(sb, KERN_ERR, "unable to read superblock");
  3083. goto out_fail;
  3084. }
  3085. /*
  3086. * Note: s_es must be initialized as soon as possible because
  3087. * some ext4 macro-instructions depend on its value
  3088. */
  3089. es = (struct ext4_super_block *) (bh->b_data + offset);
  3090. sbi->s_es = es;
  3091. sb->s_magic = le16_to_cpu(es->s_magic);
  3092. if (sb->s_magic != EXT4_SUPER_MAGIC)
  3093. goto cantfind_ext4;
  3094. sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
  3095. /* Warn if metadata_csum and gdt_csum are both set. */
  3096. if (ext4_has_feature_metadata_csum(sb) &&
  3097. ext4_has_feature_gdt_csum(sb))
  3098. ext4_warning(sb, "metadata_csum and uninit_bg are "
  3099. "redundant flags; please run fsck.");
  3100. /* Check for a known checksum algorithm */
  3101. if (!ext4_verify_csum_type(sb, es)) {
  3102. ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
  3103. "unknown checksum algorithm.");
  3104. silent = 1;
  3105. goto cantfind_ext4;
  3106. }
  3107. /* Load the checksum driver */
  3108. if (ext4_has_feature_metadata_csum(sb) ||
  3109. ext4_has_feature_ea_inode(sb)) {
  3110. sbi->s_chksum_driver = crypto_alloc_shash("crc32c", 0, 0);
  3111. if (IS_ERR(sbi->s_chksum_driver)) {
  3112. ext4_msg(sb, KERN_ERR, "Cannot load crc32c driver.");
  3113. ret = PTR_ERR(sbi->s_chksum_driver);
  3114. sbi->s_chksum_driver = NULL;
  3115. goto failed_mount;
  3116. }
  3117. }
  3118. /* Check superblock checksum */
  3119. if (!ext4_superblock_csum_verify(sb, es)) {
  3120. ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
  3121. "invalid superblock checksum. Run e2fsck?");
  3122. silent = 1;
  3123. ret = -EFSBADCRC;
  3124. goto cantfind_ext4;
  3125. }
  3126. /* Precompute checksum seed for all metadata */
  3127. if (ext4_has_feature_csum_seed(sb))
  3128. sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
  3129. else if (ext4_has_metadata_csum(sb) || ext4_has_feature_ea_inode(sb))
  3130. sbi->s_csum_seed = ext4_chksum(sbi, ~0, es->s_uuid,
  3131. sizeof(es->s_uuid));
  3132. /* Set defaults before we parse the mount options */
  3133. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  3134. set_opt(sb, INIT_INODE_TABLE);
  3135. if (def_mount_opts & EXT4_DEFM_DEBUG)
  3136. set_opt(sb, DEBUG);
  3137. if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
  3138. set_opt(sb, GRPID);
  3139. if (def_mount_opts & EXT4_DEFM_UID16)
  3140. set_opt(sb, NO_UID32);
  3141. /* xattr user namespace & acls are now defaulted on */
  3142. set_opt(sb, XATTR_USER);
  3143. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  3144. set_opt(sb, POSIX_ACL);
  3145. #endif
  3146. /* don't forget to enable journal_csum when metadata_csum is enabled. */
  3147. if (ext4_has_metadata_csum(sb))
  3148. set_opt(sb, JOURNAL_CHECKSUM);
  3149. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  3150. set_opt(sb, JOURNAL_DATA);
  3151. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  3152. set_opt(sb, ORDERED_DATA);
  3153. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  3154. set_opt(sb, WRITEBACK_DATA);
  3155. if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
  3156. set_opt(sb, ERRORS_PANIC);
  3157. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
  3158. set_opt(sb, ERRORS_CONT);
  3159. else
  3160. set_opt(sb, ERRORS_RO);
  3161. /* block_validity enabled by default; disable with noblock_validity */
  3162. set_opt(sb, BLOCK_VALIDITY);
  3163. if (def_mount_opts & EXT4_DEFM_DISCARD)
  3164. set_opt(sb, DISCARD);
  3165. sbi->s_resuid = make_kuid(&init_user_ns, le16_to_cpu(es->s_def_resuid));
  3166. sbi->s_resgid = make_kgid(&init_user_ns, le16_to_cpu(es->s_def_resgid));
  3167. sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
  3168. sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
  3169. sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
  3170. if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
  3171. set_opt(sb, BARRIER);
  3172. /*
  3173. * enable delayed allocation by default
  3174. * Use -o nodelalloc to turn it off
  3175. */
  3176. if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
  3177. ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
  3178. set_opt(sb, DELALLOC);
  3179. /*
  3180. * set default s_li_wait_mult for lazyinit, for the case there is
  3181. * no mount option specified.
  3182. */
  3183. sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
  3184. if (sbi->s_es->s_mount_opts[0]) {
  3185. char *s_mount_opts = kstrndup(sbi->s_es->s_mount_opts,
  3186. sizeof(sbi->s_es->s_mount_opts),
  3187. GFP_KERNEL);
  3188. if (!s_mount_opts)
  3189. goto failed_mount;
  3190. if (!parse_options(s_mount_opts, sb, &journal_devnum,
  3191. &journal_ioprio, 0)) {
  3192. ext4_msg(sb, KERN_WARNING,
  3193. "failed to parse options in superblock: %s",
  3194. s_mount_opts);
  3195. }
  3196. kfree(s_mount_opts);
  3197. }
  3198. sbi->s_def_mount_opt = sbi->s_mount_opt;
  3199. if (!parse_options((char *) data, sb, &journal_devnum,
  3200. &journal_ioprio, 0))
  3201. goto failed_mount;
  3202. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  3203. printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
  3204. "with data=journal disables delayed "
  3205. "allocation and O_DIRECT support!\n");
  3206. if (test_opt2(sb, EXPLICIT_DELALLOC)) {
  3207. ext4_msg(sb, KERN_ERR, "can't mount with "
  3208. "both data=journal and delalloc");
  3209. goto failed_mount;
  3210. }
  3211. if (test_opt(sb, DIOREAD_NOLOCK)) {
  3212. ext4_msg(sb, KERN_ERR, "can't mount with "
  3213. "both data=journal and dioread_nolock");
  3214. goto failed_mount;
  3215. }
  3216. if (test_opt(sb, DAX)) {
  3217. ext4_msg(sb, KERN_ERR, "can't mount with "
  3218. "both data=journal and dax");
  3219. goto failed_mount;
  3220. }
  3221. if (ext4_has_feature_encrypt(sb)) {
  3222. ext4_msg(sb, KERN_WARNING,
  3223. "encrypted files will use data=ordered "
  3224. "instead of data journaling mode");
  3225. }
  3226. if (test_opt(sb, DELALLOC))
  3227. clear_opt(sb, DELALLOC);
  3228. } else {
  3229. sb->s_iflags |= SB_I_CGROUPWB;
  3230. }
  3231. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  3232. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  3233. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  3234. (ext4_has_compat_features(sb) ||
  3235. ext4_has_ro_compat_features(sb) ||
  3236. ext4_has_incompat_features(sb)))
  3237. ext4_msg(sb, KERN_WARNING,
  3238. "feature flags set on rev 0 fs, "
  3239. "running e2fsck is recommended");
  3240. if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
  3241. set_opt2(sb, HURD_COMPAT);
  3242. if (ext4_has_feature_64bit(sb)) {
  3243. ext4_msg(sb, KERN_ERR,
  3244. "The Hurd can't support 64-bit file systems");
  3245. goto failed_mount;
  3246. }
  3247. /*
  3248. * ea_inode feature uses l_i_version field which is not
  3249. * available in HURD_COMPAT mode.
  3250. */
  3251. if (ext4_has_feature_ea_inode(sb)) {
  3252. ext4_msg(sb, KERN_ERR,
  3253. "ea_inode feature is not supported for Hurd");
  3254. goto failed_mount;
  3255. }
  3256. }
  3257. if (IS_EXT2_SB(sb)) {
  3258. if (ext2_feature_set_ok(sb))
  3259. ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
  3260. "using the ext4 subsystem");
  3261. else {
  3262. ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
  3263. "to feature incompatibilities");
  3264. goto failed_mount;
  3265. }
  3266. }
  3267. if (IS_EXT3_SB(sb)) {
  3268. if (ext3_feature_set_ok(sb))
  3269. ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
  3270. "using the ext4 subsystem");
  3271. else {
  3272. ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
  3273. "to feature incompatibilities");
  3274. goto failed_mount;
  3275. }
  3276. }
  3277. /*
  3278. * Check feature flags regardless of the revision level, since we
  3279. * previously didn't change the revision level when setting the flags,
  3280. * so there is a chance incompat flags are set on a rev 0 filesystem.
  3281. */
  3282. if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
  3283. goto failed_mount;
  3284. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  3285. if (blocksize < EXT4_MIN_BLOCK_SIZE ||
  3286. blocksize > EXT4_MAX_BLOCK_SIZE) {
  3287. ext4_msg(sb, KERN_ERR,
  3288. "Unsupported filesystem blocksize %d (%d log_block_size)",
  3289. blocksize, le32_to_cpu(es->s_log_block_size));
  3290. goto failed_mount;
  3291. }
  3292. if (le32_to_cpu(es->s_log_block_size) >
  3293. (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
  3294. ext4_msg(sb, KERN_ERR,
  3295. "Invalid log block size: %u",
  3296. le32_to_cpu(es->s_log_block_size));
  3297. goto failed_mount;
  3298. }
  3299. if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (blocksize / 4)) {
  3300. ext4_msg(sb, KERN_ERR,
  3301. "Number of reserved GDT blocks insanely large: %d",
  3302. le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
  3303. goto failed_mount;
  3304. }
  3305. if (sbi->s_mount_opt & EXT4_MOUNT_DAX) {
  3306. err = bdev_dax_supported(sb, blocksize);
  3307. if (err)
  3308. goto failed_mount;
  3309. }
  3310. if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
  3311. ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
  3312. es->s_encryption_level);
  3313. goto failed_mount;
  3314. }
  3315. if (sb->s_blocksize != blocksize) {
  3316. /* Validate the filesystem blocksize */
  3317. if (!sb_set_blocksize(sb, blocksize)) {
  3318. ext4_msg(sb, KERN_ERR, "bad block size %d",
  3319. blocksize);
  3320. goto failed_mount;
  3321. }
  3322. brelse(bh);
  3323. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  3324. offset = do_div(logical_sb_block, blocksize);
  3325. bh = sb_bread_unmovable(sb, logical_sb_block);
  3326. if (!bh) {
  3327. ext4_msg(sb, KERN_ERR,
  3328. "Can't read superblock on 2nd try");
  3329. goto failed_mount;
  3330. }
  3331. es = (struct ext4_super_block *)(bh->b_data + offset);
  3332. sbi->s_es = es;
  3333. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  3334. ext4_msg(sb, KERN_ERR,
  3335. "Magic mismatch, very weird!");
  3336. goto failed_mount;
  3337. }
  3338. }
  3339. has_huge_files = ext4_has_feature_huge_file(sb);
  3340. sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
  3341. has_huge_files);
  3342. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
  3343. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  3344. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  3345. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  3346. } else {
  3347. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  3348. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  3349. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  3350. (!is_power_of_2(sbi->s_inode_size)) ||
  3351. (sbi->s_inode_size > blocksize)) {
  3352. ext4_msg(sb, KERN_ERR,
  3353. "unsupported inode size: %d",
  3354. sbi->s_inode_size);
  3355. goto failed_mount;
  3356. }
  3357. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
  3358. sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
  3359. }
  3360. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  3361. if (ext4_has_feature_64bit(sb)) {
  3362. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  3363. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  3364. !is_power_of_2(sbi->s_desc_size)) {
  3365. ext4_msg(sb, KERN_ERR,
  3366. "unsupported descriptor size %lu",
  3367. sbi->s_desc_size);
  3368. goto failed_mount;
  3369. }
  3370. } else
  3371. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  3372. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  3373. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  3374. sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
  3375. if (sbi->s_inodes_per_block == 0)
  3376. goto cantfind_ext4;
  3377. if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
  3378. sbi->s_inodes_per_group > blocksize * 8) {
  3379. ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
  3380. sbi->s_blocks_per_group);
  3381. goto failed_mount;
  3382. }
  3383. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  3384. sbi->s_inodes_per_block;
  3385. sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
  3386. sbi->s_sbh = bh;
  3387. sbi->s_mount_state = le16_to_cpu(es->s_state);
  3388. sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
  3389. sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
  3390. for (i = 0; i < 4; i++)
  3391. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  3392. sbi->s_def_hash_version = es->s_def_hash_version;
  3393. if (ext4_has_feature_dir_index(sb)) {
  3394. i = le32_to_cpu(es->s_flags);
  3395. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  3396. sbi->s_hash_unsigned = 3;
  3397. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  3398. #ifdef __CHAR_UNSIGNED__
  3399. if (!(sb->s_flags & MS_RDONLY))
  3400. es->s_flags |=
  3401. cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  3402. sbi->s_hash_unsigned = 3;
  3403. #else
  3404. if (!(sb->s_flags & MS_RDONLY))
  3405. es->s_flags |=
  3406. cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  3407. #endif
  3408. }
  3409. }
  3410. /* Handle clustersize */
  3411. clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
  3412. has_bigalloc = ext4_has_feature_bigalloc(sb);
  3413. if (has_bigalloc) {
  3414. if (clustersize < blocksize) {
  3415. ext4_msg(sb, KERN_ERR,
  3416. "cluster size (%d) smaller than "
  3417. "block size (%d)", clustersize, blocksize);
  3418. goto failed_mount;
  3419. }
  3420. if (le32_to_cpu(es->s_log_cluster_size) >
  3421. (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
  3422. ext4_msg(sb, KERN_ERR,
  3423. "Invalid log cluster size: %u",
  3424. le32_to_cpu(es->s_log_cluster_size));
  3425. goto failed_mount;
  3426. }
  3427. sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
  3428. le32_to_cpu(es->s_log_block_size);
  3429. sbi->s_clusters_per_group =
  3430. le32_to_cpu(es->s_clusters_per_group);
  3431. if (sbi->s_clusters_per_group > blocksize * 8) {
  3432. ext4_msg(sb, KERN_ERR,
  3433. "#clusters per group too big: %lu",
  3434. sbi->s_clusters_per_group);
  3435. goto failed_mount;
  3436. }
  3437. if (sbi->s_blocks_per_group !=
  3438. (sbi->s_clusters_per_group * (clustersize / blocksize))) {
  3439. ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
  3440. "clusters per group (%lu) inconsistent",
  3441. sbi->s_blocks_per_group,
  3442. sbi->s_clusters_per_group);
  3443. goto failed_mount;
  3444. }
  3445. } else {
  3446. if (clustersize != blocksize) {
  3447. ext4_warning(sb, "fragment/cluster size (%d) != "
  3448. "block size (%d)", clustersize,
  3449. blocksize);
  3450. clustersize = blocksize;
  3451. }
  3452. if (sbi->s_blocks_per_group > blocksize * 8) {
  3453. ext4_msg(sb, KERN_ERR,
  3454. "#blocks per group too big: %lu",
  3455. sbi->s_blocks_per_group);
  3456. goto failed_mount;
  3457. }
  3458. sbi->s_clusters_per_group = sbi->s_blocks_per_group;
  3459. sbi->s_cluster_bits = 0;
  3460. }
  3461. sbi->s_cluster_ratio = clustersize / blocksize;
  3462. /* Do we have standard group size of clustersize * 8 blocks ? */
  3463. if (sbi->s_blocks_per_group == clustersize << 3)
  3464. set_opt2(sb, STD_GROUP_SIZE);
  3465. /*
  3466. * Test whether we have more sectors than will fit in sector_t,
  3467. * and whether the max offset is addressable by the page cache.
  3468. */
  3469. err = generic_check_addressable(sb->s_blocksize_bits,
  3470. ext4_blocks_count(es));
  3471. if (err) {
  3472. ext4_msg(sb, KERN_ERR, "filesystem"
  3473. " too large to mount safely on this system");
  3474. if (sizeof(sector_t) < 8)
  3475. ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
  3476. goto failed_mount;
  3477. }
  3478. if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
  3479. goto cantfind_ext4;
  3480. /* check blocks count against device size */
  3481. blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  3482. if (blocks_count && ext4_blocks_count(es) > blocks_count) {
  3483. ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
  3484. "exceeds size of device (%llu blocks)",
  3485. ext4_blocks_count(es), blocks_count);
  3486. goto failed_mount;
  3487. }
  3488. /*
  3489. * It makes no sense for the first data block to be beyond the end
  3490. * of the filesystem.
  3491. */
  3492. if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
  3493. ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
  3494. "block %u is beyond end of filesystem (%llu)",
  3495. le32_to_cpu(es->s_first_data_block),
  3496. ext4_blocks_count(es));
  3497. goto failed_mount;
  3498. }
  3499. blocks_count = (ext4_blocks_count(es) -
  3500. le32_to_cpu(es->s_first_data_block) +
  3501. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  3502. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  3503. if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
  3504. ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
  3505. "(block count %llu, first data block %u, "
  3506. "blocks per group %lu)", sbi->s_groups_count,
  3507. ext4_blocks_count(es),
  3508. le32_to_cpu(es->s_first_data_block),
  3509. EXT4_BLOCKS_PER_GROUP(sb));
  3510. goto failed_mount;
  3511. }
  3512. sbi->s_groups_count = blocks_count;
  3513. sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
  3514. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  3515. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  3516. EXT4_DESC_PER_BLOCK(sb);
  3517. if (ext4_has_feature_meta_bg(sb)) {
  3518. if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
  3519. ext4_msg(sb, KERN_WARNING,
  3520. "first meta block group too large: %u "
  3521. "(group descriptor block count %u)",
  3522. le32_to_cpu(es->s_first_meta_bg), db_count);
  3523. goto failed_mount;
  3524. }
  3525. }
  3526. sbi->s_group_desc = kvmalloc(db_count *
  3527. sizeof(struct buffer_head *),
  3528. GFP_KERNEL);
  3529. if (sbi->s_group_desc == NULL) {
  3530. ext4_msg(sb, KERN_ERR, "not enough memory");
  3531. ret = -ENOMEM;
  3532. goto failed_mount;
  3533. }
  3534. bgl_lock_init(sbi->s_blockgroup_lock);
  3535. /* Pre-read the descriptors into the buffer cache */
  3536. for (i = 0; i < db_count; i++) {
  3537. block = descriptor_loc(sb, logical_sb_block, i);
  3538. sb_breadahead(sb, block);
  3539. }
  3540. for (i = 0; i < db_count; i++) {
  3541. block = descriptor_loc(sb, logical_sb_block, i);
  3542. sbi->s_group_desc[i] = sb_bread_unmovable(sb, block);
  3543. if (!sbi->s_group_desc[i]) {
  3544. ext4_msg(sb, KERN_ERR,
  3545. "can't read group descriptor %d", i);
  3546. db_count = i;
  3547. goto failed_mount2;
  3548. }
  3549. }
  3550. if (!ext4_check_descriptors(sb, logical_sb_block, &first_not_zeroed)) {
  3551. ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
  3552. ret = -EFSCORRUPTED;
  3553. goto failed_mount2;
  3554. }
  3555. sbi->s_gdb_count = db_count;
  3556. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  3557. spin_lock_init(&sbi->s_next_gen_lock);
  3558. setup_timer(&sbi->s_err_report, print_daily_error_info,
  3559. (unsigned long) sb);
  3560. /* Register extent status tree shrinker */
  3561. if (ext4_es_register_shrinker(sbi))
  3562. goto failed_mount3;
  3563. sbi->s_stripe = ext4_get_stripe_size(sbi);
  3564. sbi->s_extent_max_zeroout_kb = 32;
  3565. /*
  3566. * set up enough so that it can read an inode
  3567. */
  3568. sb->s_op = &ext4_sops;
  3569. sb->s_export_op = &ext4_export_ops;
  3570. sb->s_xattr = ext4_xattr_handlers;
  3571. sb->s_cop = &ext4_cryptops;
  3572. #ifdef CONFIG_QUOTA
  3573. sb->dq_op = &ext4_quota_operations;
  3574. if (ext4_has_feature_quota(sb))
  3575. sb->s_qcop = &dquot_quotactl_sysfile_ops;
  3576. else
  3577. sb->s_qcop = &ext4_qctl_operations;
  3578. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
  3579. #endif
  3580. memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
  3581. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  3582. mutex_init(&sbi->s_orphan_lock);
  3583. sb->s_root = NULL;
  3584. needs_recovery = (es->s_last_orphan != 0 ||
  3585. ext4_has_feature_journal_needs_recovery(sb));
  3586. if (ext4_has_feature_mmp(sb) && !(sb->s_flags & MS_RDONLY))
  3587. if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
  3588. goto failed_mount3a;
  3589. /*
  3590. * The first inode we look at is the journal inode. Don't try
  3591. * root first: it may be modified in the journal!
  3592. */
  3593. if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
  3594. err = ext4_load_journal(sb, es, journal_devnum);
  3595. if (err)
  3596. goto failed_mount3a;
  3597. } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
  3598. ext4_has_feature_journal_needs_recovery(sb)) {
  3599. ext4_msg(sb, KERN_ERR, "required journal recovery "
  3600. "suppressed and not mounted read-only");
  3601. goto failed_mount_wq;
  3602. } else {
  3603. /* Nojournal mode, all journal mount options are illegal */
  3604. if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM)) {
  3605. ext4_msg(sb, KERN_ERR, "can't mount with "
  3606. "journal_checksum, fs mounted w/o journal");
  3607. goto failed_mount_wq;
  3608. }
  3609. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  3610. ext4_msg(sb, KERN_ERR, "can't mount with "
  3611. "journal_async_commit, fs mounted w/o journal");
  3612. goto failed_mount_wq;
  3613. }
  3614. if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ) {
  3615. ext4_msg(sb, KERN_ERR, "can't mount with "
  3616. "commit=%lu, fs mounted w/o journal",
  3617. sbi->s_commit_interval / HZ);
  3618. goto failed_mount_wq;
  3619. }
  3620. if (EXT4_MOUNT_DATA_FLAGS &
  3621. (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
  3622. ext4_msg(sb, KERN_ERR, "can't mount with "
  3623. "data=, fs mounted w/o journal");
  3624. goto failed_mount_wq;
  3625. }
  3626. sbi->s_def_mount_opt &= EXT4_MOUNT_JOURNAL_CHECKSUM;
  3627. clear_opt(sb, JOURNAL_CHECKSUM);
  3628. clear_opt(sb, DATA_FLAGS);
  3629. sbi->s_journal = NULL;
  3630. needs_recovery = 0;
  3631. goto no_journal;
  3632. }
  3633. if (ext4_has_feature_64bit(sb) &&
  3634. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  3635. JBD2_FEATURE_INCOMPAT_64BIT)) {
  3636. ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
  3637. goto failed_mount_wq;
  3638. }
  3639. if (!set_journal_csum_feature_set(sb)) {
  3640. ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
  3641. "feature set");
  3642. goto failed_mount_wq;
  3643. }
  3644. /* We have now updated the journal if required, so we can
  3645. * validate the data journaling mode. */
  3646. switch (test_opt(sb, DATA_FLAGS)) {
  3647. case 0:
  3648. /* No mode set, assume a default based on the journal
  3649. * capabilities: ORDERED_DATA if the journal can
  3650. * cope, else JOURNAL_DATA
  3651. */
  3652. if (jbd2_journal_check_available_features
  3653. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
  3654. set_opt(sb, ORDERED_DATA);
  3655. else
  3656. set_opt(sb, JOURNAL_DATA);
  3657. break;
  3658. case EXT4_MOUNT_ORDERED_DATA:
  3659. case EXT4_MOUNT_WRITEBACK_DATA:
  3660. if (!jbd2_journal_check_available_features
  3661. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  3662. ext4_msg(sb, KERN_ERR, "Journal does not support "
  3663. "requested data journaling mode");
  3664. goto failed_mount_wq;
  3665. }
  3666. default:
  3667. break;
  3668. }
  3669. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
  3670. test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  3671. ext4_msg(sb, KERN_ERR, "can't mount with "
  3672. "journal_async_commit in data=ordered mode");
  3673. goto failed_mount_wq;
  3674. }
  3675. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3676. sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
  3677. no_journal:
  3678. if (!test_opt(sb, NO_MBCACHE)) {
  3679. sbi->s_ea_block_cache = ext4_xattr_create_cache();
  3680. if (!sbi->s_ea_block_cache) {
  3681. ext4_msg(sb, KERN_ERR,
  3682. "Failed to create ea_block_cache");
  3683. goto failed_mount_wq;
  3684. }
  3685. if (ext4_has_feature_ea_inode(sb)) {
  3686. sbi->s_ea_inode_cache = ext4_xattr_create_cache();
  3687. if (!sbi->s_ea_inode_cache) {
  3688. ext4_msg(sb, KERN_ERR,
  3689. "Failed to create ea_inode_cache");
  3690. goto failed_mount_wq;
  3691. }
  3692. }
  3693. }
  3694. if ((DUMMY_ENCRYPTION_ENABLED(sbi) || ext4_has_feature_encrypt(sb)) &&
  3695. (blocksize != PAGE_SIZE)) {
  3696. ext4_msg(sb, KERN_ERR,
  3697. "Unsupported blocksize for fs encryption");
  3698. goto failed_mount_wq;
  3699. }
  3700. if (DUMMY_ENCRYPTION_ENABLED(sbi) && !(sb->s_flags & MS_RDONLY) &&
  3701. !ext4_has_feature_encrypt(sb)) {
  3702. ext4_set_feature_encrypt(sb);
  3703. ext4_commit_super(sb, 1);
  3704. }
  3705. /*
  3706. * Get the # of file system overhead blocks from the
  3707. * superblock if present.
  3708. */
  3709. if (es->s_overhead_clusters)
  3710. sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
  3711. else {
  3712. err = ext4_calculate_overhead(sb);
  3713. if (err)
  3714. goto failed_mount_wq;
  3715. }
  3716. /*
  3717. * The maximum number of concurrent works can be high and
  3718. * concurrency isn't really necessary. Limit it to 1.
  3719. */
  3720. EXT4_SB(sb)->rsv_conversion_wq =
  3721. alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  3722. if (!EXT4_SB(sb)->rsv_conversion_wq) {
  3723. printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
  3724. ret = -ENOMEM;
  3725. goto failed_mount4;
  3726. }
  3727. /*
  3728. * The jbd2_journal_load will have done any necessary log recovery,
  3729. * so we can safely mount the rest of the filesystem now.
  3730. */
  3731. root = ext4_iget(sb, EXT4_ROOT_INO);
  3732. if (IS_ERR(root)) {
  3733. ext4_msg(sb, KERN_ERR, "get root inode failed");
  3734. ret = PTR_ERR(root);
  3735. root = NULL;
  3736. goto failed_mount4;
  3737. }
  3738. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  3739. ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
  3740. iput(root);
  3741. goto failed_mount4;
  3742. }
  3743. sb->s_root = d_make_root(root);
  3744. if (!sb->s_root) {
  3745. ext4_msg(sb, KERN_ERR, "get root dentry failed");
  3746. ret = -ENOMEM;
  3747. goto failed_mount4;
  3748. }
  3749. if (ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY))
  3750. sb->s_flags |= MS_RDONLY;
  3751. /* determine the minimum size of new large inodes, if present */
  3752. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE &&
  3753. sbi->s_want_extra_isize == 0) {
  3754. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3755. EXT4_GOOD_OLD_INODE_SIZE;
  3756. if (ext4_has_feature_extra_isize(sb)) {
  3757. if (sbi->s_want_extra_isize <
  3758. le16_to_cpu(es->s_want_extra_isize))
  3759. sbi->s_want_extra_isize =
  3760. le16_to_cpu(es->s_want_extra_isize);
  3761. if (sbi->s_want_extra_isize <
  3762. le16_to_cpu(es->s_min_extra_isize))
  3763. sbi->s_want_extra_isize =
  3764. le16_to_cpu(es->s_min_extra_isize);
  3765. }
  3766. }
  3767. /* Check if enough inode space is available */
  3768. if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
  3769. sbi->s_inode_size) {
  3770. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3771. EXT4_GOOD_OLD_INODE_SIZE;
  3772. ext4_msg(sb, KERN_INFO, "required extra inode space not"
  3773. "available");
  3774. }
  3775. ext4_set_resv_clusters(sb);
  3776. err = ext4_setup_system_zone(sb);
  3777. if (err) {
  3778. ext4_msg(sb, KERN_ERR, "failed to initialize system "
  3779. "zone (%d)", err);
  3780. goto failed_mount4a;
  3781. }
  3782. ext4_ext_init(sb);
  3783. err = ext4_mb_init(sb);
  3784. if (err) {
  3785. ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
  3786. err);
  3787. goto failed_mount5;
  3788. }
  3789. block = ext4_count_free_clusters(sb);
  3790. ext4_free_blocks_count_set(sbi->s_es,
  3791. EXT4_C2B(sbi, block));
  3792. err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
  3793. GFP_KERNEL);
  3794. if (!err) {
  3795. unsigned long freei = ext4_count_free_inodes(sb);
  3796. sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
  3797. err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
  3798. GFP_KERNEL);
  3799. }
  3800. if (!err)
  3801. err = percpu_counter_init(&sbi->s_dirs_counter,
  3802. ext4_count_dirs(sb), GFP_KERNEL);
  3803. if (!err)
  3804. err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
  3805. GFP_KERNEL);
  3806. if (!err)
  3807. err = percpu_init_rwsem(&sbi->s_journal_flag_rwsem);
  3808. if (err) {
  3809. ext4_msg(sb, KERN_ERR, "insufficient memory");
  3810. goto failed_mount6;
  3811. }
  3812. if (ext4_has_feature_flex_bg(sb))
  3813. if (!ext4_fill_flex_info(sb)) {
  3814. ext4_msg(sb, KERN_ERR,
  3815. "unable to initialize "
  3816. "flex_bg meta info!");
  3817. goto failed_mount6;
  3818. }
  3819. err = ext4_register_li_request(sb, first_not_zeroed);
  3820. if (err)
  3821. goto failed_mount6;
  3822. err = ext4_register_sysfs(sb);
  3823. if (err)
  3824. goto failed_mount7;
  3825. #ifdef CONFIG_QUOTA
  3826. /* Enable quota usage during mount. */
  3827. if (ext4_has_feature_quota(sb) && !(sb->s_flags & MS_RDONLY)) {
  3828. err = ext4_enable_quotas(sb);
  3829. if (err)
  3830. goto failed_mount8;
  3831. }
  3832. #endif /* CONFIG_QUOTA */
  3833. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  3834. ext4_orphan_cleanup(sb, es);
  3835. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  3836. if (needs_recovery) {
  3837. ext4_msg(sb, KERN_INFO, "recovery complete");
  3838. ext4_mark_recovery_complete(sb, es);
  3839. }
  3840. if (EXT4_SB(sb)->s_journal) {
  3841. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  3842. descr = " journalled data mode";
  3843. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  3844. descr = " ordered data mode";
  3845. else
  3846. descr = " writeback data mode";
  3847. } else
  3848. descr = "out journal";
  3849. if (test_opt(sb, DISCARD)) {
  3850. struct request_queue *q = bdev_get_queue(sb->s_bdev);
  3851. if (!blk_queue_discard(q))
  3852. ext4_msg(sb, KERN_WARNING,
  3853. "mounting with \"discard\" option, but "
  3854. "the device does not support discard");
  3855. }
  3856. if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
  3857. ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
  3858. "Opts: %.*s%s%s", descr,
  3859. (int) sizeof(sbi->s_es->s_mount_opts),
  3860. sbi->s_es->s_mount_opts,
  3861. *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
  3862. if (es->s_error_count)
  3863. mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
  3864. /* Enable message ratelimiting. Default is 10 messages per 5 secs. */
  3865. ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
  3866. ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
  3867. ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
  3868. kfree(orig_data);
  3869. return 0;
  3870. cantfind_ext4:
  3871. if (!silent)
  3872. ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
  3873. goto failed_mount;
  3874. #ifdef CONFIG_QUOTA
  3875. failed_mount8:
  3876. ext4_unregister_sysfs(sb);
  3877. #endif
  3878. failed_mount7:
  3879. ext4_unregister_li_request(sb);
  3880. failed_mount6:
  3881. ext4_mb_release(sb);
  3882. if (sbi->s_flex_groups)
  3883. kvfree(sbi->s_flex_groups);
  3884. percpu_counter_destroy(&sbi->s_freeclusters_counter);
  3885. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  3886. percpu_counter_destroy(&sbi->s_dirs_counter);
  3887. percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
  3888. failed_mount5:
  3889. ext4_ext_release(sb);
  3890. ext4_release_system_zone(sb);
  3891. failed_mount4a:
  3892. dput(sb->s_root);
  3893. sb->s_root = NULL;
  3894. failed_mount4:
  3895. ext4_msg(sb, KERN_ERR, "mount failed");
  3896. if (EXT4_SB(sb)->rsv_conversion_wq)
  3897. destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
  3898. failed_mount_wq:
  3899. if (sbi->s_ea_inode_cache) {
  3900. ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
  3901. sbi->s_ea_inode_cache = NULL;
  3902. }
  3903. if (sbi->s_ea_block_cache) {
  3904. ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
  3905. sbi->s_ea_block_cache = NULL;
  3906. }
  3907. if (sbi->s_journal) {
  3908. jbd2_journal_destroy(sbi->s_journal);
  3909. sbi->s_journal = NULL;
  3910. }
  3911. failed_mount3a:
  3912. ext4_es_unregister_shrinker(sbi);
  3913. failed_mount3:
  3914. del_timer_sync(&sbi->s_err_report);
  3915. if (sbi->s_mmp_tsk)
  3916. kthread_stop(sbi->s_mmp_tsk);
  3917. failed_mount2:
  3918. for (i = 0; i < db_count; i++)
  3919. brelse(sbi->s_group_desc[i]);
  3920. kvfree(sbi->s_group_desc);
  3921. failed_mount:
  3922. if (sbi->s_chksum_driver)
  3923. crypto_free_shash(sbi->s_chksum_driver);
  3924. #ifdef CONFIG_QUOTA
  3925. for (i = 0; i < EXT4_MAXQUOTAS; i++)
  3926. kfree(sbi->s_qf_names[i]);
  3927. #endif
  3928. ext4_blkdev_remove(sbi);
  3929. brelse(bh);
  3930. out_fail:
  3931. sb->s_fs_info = NULL;
  3932. kfree(sbi->s_blockgroup_lock);
  3933. out_free_base:
  3934. kfree(sbi);
  3935. kfree(orig_data);
  3936. return err ? err : ret;
  3937. }
  3938. /*
  3939. * Setup any per-fs journal parameters now. We'll do this both on
  3940. * initial mount, once the journal has been initialised but before we've
  3941. * done any recovery; and again on any subsequent remount.
  3942. */
  3943. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  3944. {
  3945. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3946. journal->j_commit_interval = sbi->s_commit_interval;
  3947. journal->j_min_batch_time = sbi->s_min_batch_time;
  3948. journal->j_max_batch_time = sbi->s_max_batch_time;
  3949. write_lock(&journal->j_state_lock);
  3950. if (test_opt(sb, BARRIER))
  3951. journal->j_flags |= JBD2_BARRIER;
  3952. else
  3953. journal->j_flags &= ~JBD2_BARRIER;
  3954. if (test_opt(sb, DATA_ERR_ABORT))
  3955. journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
  3956. else
  3957. journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
  3958. write_unlock(&journal->j_state_lock);
  3959. }
  3960. static struct inode *ext4_get_journal_inode(struct super_block *sb,
  3961. unsigned int journal_inum)
  3962. {
  3963. struct inode *journal_inode;
  3964. /*
  3965. * Test for the existence of a valid inode on disk. Bad things
  3966. * happen if we iget() an unused inode, as the subsequent iput()
  3967. * will try to delete it.
  3968. */
  3969. journal_inode = ext4_iget(sb, journal_inum);
  3970. if (IS_ERR(journal_inode)) {
  3971. ext4_msg(sb, KERN_ERR, "no journal found");
  3972. return NULL;
  3973. }
  3974. if (!journal_inode->i_nlink) {
  3975. make_bad_inode(journal_inode);
  3976. iput(journal_inode);
  3977. ext4_msg(sb, KERN_ERR, "journal inode is deleted");
  3978. return NULL;
  3979. }
  3980. jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
  3981. journal_inode, journal_inode->i_size);
  3982. if (!S_ISREG(journal_inode->i_mode)) {
  3983. ext4_msg(sb, KERN_ERR, "invalid journal inode");
  3984. iput(journal_inode);
  3985. return NULL;
  3986. }
  3987. return journal_inode;
  3988. }
  3989. static journal_t *ext4_get_journal(struct super_block *sb,
  3990. unsigned int journal_inum)
  3991. {
  3992. struct inode *journal_inode;
  3993. journal_t *journal;
  3994. BUG_ON(!ext4_has_feature_journal(sb));
  3995. journal_inode = ext4_get_journal_inode(sb, journal_inum);
  3996. if (!journal_inode)
  3997. return NULL;
  3998. journal = jbd2_journal_init_inode(journal_inode);
  3999. if (!journal) {
  4000. ext4_msg(sb, KERN_ERR, "Could not load journal inode");
  4001. iput(journal_inode);
  4002. return NULL;
  4003. }
  4004. journal->j_private = sb;
  4005. ext4_init_journal_params(sb, journal);
  4006. return journal;
  4007. }
  4008. static journal_t *ext4_get_dev_journal(struct super_block *sb,
  4009. dev_t j_dev)
  4010. {
  4011. struct buffer_head *bh;
  4012. journal_t *journal;
  4013. ext4_fsblk_t start;
  4014. ext4_fsblk_t len;
  4015. int hblock, blocksize;
  4016. ext4_fsblk_t sb_block;
  4017. unsigned long offset;
  4018. struct ext4_super_block *es;
  4019. struct block_device *bdev;
  4020. BUG_ON(!ext4_has_feature_journal(sb));
  4021. bdev = ext4_blkdev_get(j_dev, sb);
  4022. if (bdev == NULL)
  4023. return NULL;
  4024. blocksize = sb->s_blocksize;
  4025. hblock = bdev_logical_block_size(bdev);
  4026. if (blocksize < hblock) {
  4027. ext4_msg(sb, KERN_ERR,
  4028. "blocksize too small for journal device");
  4029. goto out_bdev;
  4030. }
  4031. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  4032. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  4033. set_blocksize(bdev, blocksize);
  4034. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  4035. ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
  4036. "external journal");
  4037. goto out_bdev;
  4038. }
  4039. es = (struct ext4_super_block *) (bh->b_data + offset);
  4040. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  4041. !(le32_to_cpu(es->s_feature_incompat) &
  4042. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  4043. ext4_msg(sb, KERN_ERR, "external journal has "
  4044. "bad superblock");
  4045. brelse(bh);
  4046. goto out_bdev;
  4047. }
  4048. if ((le32_to_cpu(es->s_feature_ro_compat) &
  4049. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
  4050. es->s_checksum != ext4_superblock_csum(sb, es)) {
  4051. ext4_msg(sb, KERN_ERR, "external journal has "
  4052. "corrupt superblock");
  4053. brelse(bh);
  4054. goto out_bdev;
  4055. }
  4056. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  4057. ext4_msg(sb, KERN_ERR, "journal UUID does not match");
  4058. brelse(bh);
  4059. goto out_bdev;
  4060. }
  4061. len = ext4_blocks_count(es);
  4062. start = sb_block + 1;
  4063. brelse(bh); /* we're done with the superblock */
  4064. journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
  4065. start, len, blocksize);
  4066. if (!journal) {
  4067. ext4_msg(sb, KERN_ERR, "failed to create device journal");
  4068. goto out_bdev;
  4069. }
  4070. journal->j_private = sb;
  4071. ll_rw_block(REQ_OP_READ, REQ_META | REQ_PRIO, 1, &journal->j_sb_buffer);
  4072. wait_on_buffer(journal->j_sb_buffer);
  4073. if (!buffer_uptodate(journal->j_sb_buffer)) {
  4074. ext4_msg(sb, KERN_ERR, "I/O error on journal device");
  4075. goto out_journal;
  4076. }
  4077. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  4078. ext4_msg(sb, KERN_ERR, "External journal has more than one "
  4079. "user (unsupported) - %d",
  4080. be32_to_cpu(journal->j_superblock->s_nr_users));
  4081. goto out_journal;
  4082. }
  4083. EXT4_SB(sb)->journal_bdev = bdev;
  4084. ext4_init_journal_params(sb, journal);
  4085. return journal;
  4086. out_journal:
  4087. jbd2_journal_destroy(journal);
  4088. out_bdev:
  4089. ext4_blkdev_put(bdev);
  4090. return NULL;
  4091. }
  4092. static int ext4_load_journal(struct super_block *sb,
  4093. struct ext4_super_block *es,
  4094. unsigned long journal_devnum)
  4095. {
  4096. journal_t *journal;
  4097. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  4098. dev_t journal_dev;
  4099. int err = 0;
  4100. int really_read_only;
  4101. BUG_ON(!ext4_has_feature_journal(sb));
  4102. if (journal_devnum &&
  4103. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  4104. ext4_msg(sb, KERN_INFO, "external journal device major/minor "
  4105. "numbers have changed");
  4106. journal_dev = new_decode_dev(journal_devnum);
  4107. } else
  4108. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  4109. really_read_only = bdev_read_only(sb->s_bdev);
  4110. /*
  4111. * Are we loading a blank journal or performing recovery after a
  4112. * crash? For recovery, we need to check in advance whether we
  4113. * can get read-write access to the device.
  4114. */
  4115. if (ext4_has_feature_journal_needs_recovery(sb)) {
  4116. if (sb->s_flags & MS_RDONLY) {
  4117. ext4_msg(sb, KERN_INFO, "INFO: recovery "
  4118. "required on readonly filesystem");
  4119. if (really_read_only) {
  4120. ext4_msg(sb, KERN_ERR, "write access "
  4121. "unavailable, cannot proceed");
  4122. return -EROFS;
  4123. }
  4124. ext4_msg(sb, KERN_INFO, "write access will "
  4125. "be enabled during recovery");
  4126. }
  4127. }
  4128. if (journal_inum && journal_dev) {
  4129. ext4_msg(sb, KERN_ERR, "filesystem has both journal "
  4130. "and inode journals!");
  4131. return -EINVAL;
  4132. }
  4133. if (journal_inum) {
  4134. if (!(journal = ext4_get_journal(sb, journal_inum)))
  4135. return -EINVAL;
  4136. } else {
  4137. if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
  4138. return -EINVAL;
  4139. }
  4140. if (!(journal->j_flags & JBD2_BARRIER))
  4141. ext4_msg(sb, KERN_INFO, "barriers disabled");
  4142. if (!ext4_has_feature_journal_needs_recovery(sb))
  4143. err = jbd2_journal_wipe(journal, !really_read_only);
  4144. if (!err) {
  4145. char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
  4146. if (save)
  4147. memcpy(save, ((char *) es) +
  4148. EXT4_S_ERR_START, EXT4_S_ERR_LEN);
  4149. err = jbd2_journal_load(journal);
  4150. if (save)
  4151. memcpy(((char *) es) + EXT4_S_ERR_START,
  4152. save, EXT4_S_ERR_LEN);
  4153. kfree(save);
  4154. }
  4155. if (err) {
  4156. ext4_msg(sb, KERN_ERR, "error loading journal");
  4157. jbd2_journal_destroy(journal);
  4158. return err;
  4159. }
  4160. EXT4_SB(sb)->s_journal = journal;
  4161. ext4_clear_journal_err(sb, es);
  4162. if (!really_read_only && journal_devnum &&
  4163. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  4164. es->s_journal_dev = cpu_to_le32(journal_devnum);
  4165. /* Make sure we flush the recovery flag to disk. */
  4166. ext4_commit_super(sb, 1);
  4167. }
  4168. return 0;
  4169. }
  4170. static int ext4_commit_super(struct super_block *sb, int sync)
  4171. {
  4172. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  4173. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  4174. int error = 0;
  4175. if (!sbh || block_device_ejected(sb))
  4176. return error;
  4177. /*
  4178. * If the file system is mounted read-only, don't update the
  4179. * superblock write time. This avoids updating the superblock
  4180. * write time when we are mounting the root file system
  4181. * read/only but we need to replay the journal; at that point,
  4182. * for people who are east of GMT and who make their clock
  4183. * tick in localtime for Windows bug-for-bug compatibility,
  4184. * the clock is set in the future, and this will cause e2fsck
  4185. * to complain and force a full file system check.
  4186. */
  4187. if (!(sb->s_flags & MS_RDONLY))
  4188. es->s_wtime = cpu_to_le32(get_seconds());
  4189. if (sb->s_bdev->bd_part)
  4190. es->s_kbytes_written =
  4191. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
  4192. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  4193. EXT4_SB(sb)->s_sectors_written_start) >> 1));
  4194. else
  4195. es->s_kbytes_written =
  4196. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
  4197. if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeclusters_counter))
  4198. ext4_free_blocks_count_set(es,
  4199. EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
  4200. &EXT4_SB(sb)->s_freeclusters_counter)));
  4201. if (percpu_counter_initialized(&EXT4_SB(sb)->s_freeinodes_counter))
  4202. es->s_free_inodes_count =
  4203. cpu_to_le32(percpu_counter_sum_positive(
  4204. &EXT4_SB(sb)->s_freeinodes_counter));
  4205. BUFFER_TRACE(sbh, "marking dirty");
  4206. ext4_superblock_csum_set(sb);
  4207. if (sync)
  4208. lock_buffer(sbh);
  4209. if (buffer_write_io_error(sbh)) {
  4210. /*
  4211. * Oh, dear. A previous attempt to write the
  4212. * superblock failed. This could happen because the
  4213. * USB device was yanked out. Or it could happen to
  4214. * be a transient write error and maybe the block will
  4215. * be remapped. Nothing we can do but to retry the
  4216. * write and hope for the best.
  4217. */
  4218. ext4_msg(sb, KERN_ERR, "previous I/O error to "
  4219. "superblock detected");
  4220. clear_buffer_write_io_error(sbh);
  4221. set_buffer_uptodate(sbh);
  4222. }
  4223. mark_buffer_dirty(sbh);
  4224. if (sync) {
  4225. unlock_buffer(sbh);
  4226. error = __sync_dirty_buffer(sbh,
  4227. REQ_SYNC | (test_opt(sb, BARRIER) ? REQ_FUA : 0));
  4228. if (error)
  4229. return error;
  4230. error = buffer_write_io_error(sbh);
  4231. if (error) {
  4232. ext4_msg(sb, KERN_ERR, "I/O error while writing "
  4233. "superblock");
  4234. clear_buffer_write_io_error(sbh);
  4235. set_buffer_uptodate(sbh);
  4236. }
  4237. }
  4238. return error;
  4239. }
  4240. /*
  4241. * Have we just finished recovery? If so, and if we are mounting (or
  4242. * remounting) the filesystem readonly, then we will end up with a
  4243. * consistent fs on disk. Record that fact.
  4244. */
  4245. static void ext4_mark_recovery_complete(struct super_block *sb,
  4246. struct ext4_super_block *es)
  4247. {
  4248. journal_t *journal = EXT4_SB(sb)->s_journal;
  4249. if (!ext4_has_feature_journal(sb)) {
  4250. BUG_ON(journal != NULL);
  4251. return;
  4252. }
  4253. jbd2_journal_lock_updates(journal);
  4254. if (jbd2_journal_flush(journal) < 0)
  4255. goto out;
  4256. if (ext4_has_feature_journal_needs_recovery(sb) &&
  4257. sb->s_flags & MS_RDONLY) {
  4258. ext4_clear_feature_journal_needs_recovery(sb);
  4259. ext4_commit_super(sb, 1);
  4260. }
  4261. out:
  4262. jbd2_journal_unlock_updates(journal);
  4263. }
  4264. /*
  4265. * If we are mounting (or read-write remounting) a filesystem whose journal
  4266. * has recorded an error from a previous lifetime, move that error to the
  4267. * main filesystem now.
  4268. */
  4269. static void ext4_clear_journal_err(struct super_block *sb,
  4270. struct ext4_super_block *es)
  4271. {
  4272. journal_t *journal;
  4273. int j_errno;
  4274. const char *errstr;
  4275. BUG_ON(!ext4_has_feature_journal(sb));
  4276. journal = EXT4_SB(sb)->s_journal;
  4277. /*
  4278. * Now check for any error status which may have been recorded in the
  4279. * journal by a prior ext4_error() or ext4_abort()
  4280. */
  4281. j_errno = jbd2_journal_errno(journal);
  4282. if (j_errno) {
  4283. char nbuf[16];
  4284. errstr = ext4_decode_error(sb, j_errno, nbuf);
  4285. ext4_warning(sb, "Filesystem error recorded "
  4286. "from previous mount: %s", errstr);
  4287. ext4_warning(sb, "Marking fs in need of filesystem check.");
  4288. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  4289. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  4290. ext4_commit_super(sb, 1);
  4291. jbd2_journal_clear_err(journal);
  4292. jbd2_journal_update_sb_errno(journal);
  4293. }
  4294. }
  4295. /*
  4296. * Force the running and committing transactions to commit,
  4297. * and wait on the commit.
  4298. */
  4299. int ext4_force_commit(struct super_block *sb)
  4300. {
  4301. journal_t *journal;
  4302. if (sb->s_flags & MS_RDONLY)
  4303. return 0;
  4304. journal = EXT4_SB(sb)->s_journal;
  4305. return ext4_journal_force_commit(journal);
  4306. }
  4307. static int ext4_sync_fs(struct super_block *sb, int wait)
  4308. {
  4309. int ret = 0;
  4310. tid_t target;
  4311. bool needs_barrier = false;
  4312. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4313. if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
  4314. return 0;
  4315. trace_ext4_sync_fs(sb, wait);
  4316. flush_workqueue(sbi->rsv_conversion_wq);
  4317. /*
  4318. * Writeback quota in non-journalled quota case - journalled quota has
  4319. * no dirty dquots
  4320. */
  4321. dquot_writeback_dquots(sb, -1);
  4322. /*
  4323. * Data writeback is possible w/o journal transaction, so barrier must
  4324. * being sent at the end of the function. But we can skip it if
  4325. * transaction_commit will do it for us.
  4326. */
  4327. if (sbi->s_journal) {
  4328. target = jbd2_get_latest_transaction(sbi->s_journal);
  4329. if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
  4330. !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
  4331. needs_barrier = true;
  4332. if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
  4333. if (wait)
  4334. ret = jbd2_log_wait_commit(sbi->s_journal,
  4335. target);
  4336. }
  4337. } else if (wait && test_opt(sb, BARRIER))
  4338. needs_barrier = true;
  4339. if (needs_barrier) {
  4340. int err;
  4341. err = blkdev_issue_flush(sb->s_bdev, GFP_KERNEL, NULL);
  4342. if (!ret)
  4343. ret = err;
  4344. }
  4345. return ret;
  4346. }
  4347. /*
  4348. * LVM calls this function before a (read-only) snapshot is created. This
  4349. * gives us a chance to flush the journal completely and mark the fs clean.
  4350. *
  4351. * Note that only this function cannot bring a filesystem to be in a clean
  4352. * state independently. It relies on upper layer to stop all data & metadata
  4353. * modifications.
  4354. */
  4355. static int ext4_freeze(struct super_block *sb)
  4356. {
  4357. int error = 0;
  4358. journal_t *journal;
  4359. if (sb->s_flags & MS_RDONLY)
  4360. return 0;
  4361. journal = EXT4_SB(sb)->s_journal;
  4362. if (journal) {
  4363. /* Now we set up the journal barrier. */
  4364. jbd2_journal_lock_updates(journal);
  4365. /*
  4366. * Don't clear the needs_recovery flag if we failed to
  4367. * flush the journal.
  4368. */
  4369. error = jbd2_journal_flush(journal);
  4370. if (error < 0)
  4371. goto out;
  4372. /* Journal blocked and flushed, clear needs_recovery flag. */
  4373. ext4_clear_feature_journal_needs_recovery(sb);
  4374. }
  4375. error = ext4_commit_super(sb, 1);
  4376. out:
  4377. if (journal)
  4378. /* we rely on upper layer to stop further updates */
  4379. jbd2_journal_unlock_updates(journal);
  4380. return error;
  4381. }
  4382. /*
  4383. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  4384. * flag here, even though the filesystem is not technically dirty yet.
  4385. */
  4386. static int ext4_unfreeze(struct super_block *sb)
  4387. {
  4388. if ((sb->s_flags & MS_RDONLY) || ext4_forced_shutdown(EXT4_SB(sb)))
  4389. return 0;
  4390. if (EXT4_SB(sb)->s_journal) {
  4391. /* Reset the needs_recovery flag before the fs is unlocked. */
  4392. ext4_set_feature_journal_needs_recovery(sb);
  4393. }
  4394. ext4_commit_super(sb, 1);
  4395. return 0;
  4396. }
  4397. /*
  4398. * Structure to save mount options for ext4_remount's benefit
  4399. */
  4400. struct ext4_mount_options {
  4401. unsigned long s_mount_opt;
  4402. unsigned long s_mount_opt2;
  4403. kuid_t s_resuid;
  4404. kgid_t s_resgid;
  4405. unsigned long s_commit_interval;
  4406. u32 s_min_batch_time, s_max_batch_time;
  4407. #ifdef CONFIG_QUOTA
  4408. int s_jquota_fmt;
  4409. char *s_qf_names[EXT4_MAXQUOTAS];
  4410. #endif
  4411. };
  4412. static int ext4_remount(struct super_block *sb, int *flags, char *data)
  4413. {
  4414. struct ext4_super_block *es;
  4415. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4416. unsigned long old_sb_flags;
  4417. struct ext4_mount_options old_opts;
  4418. int enable_quota = 0;
  4419. ext4_group_t g;
  4420. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  4421. int err = 0;
  4422. #ifdef CONFIG_QUOTA
  4423. int i, j;
  4424. #endif
  4425. char *orig_data = kstrdup(data, GFP_KERNEL);
  4426. /* Store the original options */
  4427. old_sb_flags = sb->s_flags;
  4428. old_opts.s_mount_opt = sbi->s_mount_opt;
  4429. old_opts.s_mount_opt2 = sbi->s_mount_opt2;
  4430. old_opts.s_resuid = sbi->s_resuid;
  4431. old_opts.s_resgid = sbi->s_resgid;
  4432. old_opts.s_commit_interval = sbi->s_commit_interval;
  4433. old_opts.s_min_batch_time = sbi->s_min_batch_time;
  4434. old_opts.s_max_batch_time = sbi->s_max_batch_time;
  4435. #ifdef CONFIG_QUOTA
  4436. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  4437. for (i = 0; i < EXT4_MAXQUOTAS; i++)
  4438. if (sbi->s_qf_names[i]) {
  4439. old_opts.s_qf_names[i] = kstrdup(sbi->s_qf_names[i],
  4440. GFP_KERNEL);
  4441. if (!old_opts.s_qf_names[i]) {
  4442. for (j = 0; j < i; j++)
  4443. kfree(old_opts.s_qf_names[j]);
  4444. kfree(orig_data);
  4445. return -ENOMEM;
  4446. }
  4447. } else
  4448. old_opts.s_qf_names[i] = NULL;
  4449. #endif
  4450. if (sbi->s_journal && sbi->s_journal->j_task->io_context)
  4451. journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
  4452. if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
  4453. err = -EINVAL;
  4454. goto restore_opts;
  4455. }
  4456. if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
  4457. test_opt(sb, JOURNAL_CHECKSUM)) {
  4458. ext4_msg(sb, KERN_ERR, "changing journal_checksum "
  4459. "during remount not supported; ignoring");
  4460. sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
  4461. }
  4462. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  4463. if (test_opt2(sb, EXPLICIT_DELALLOC)) {
  4464. ext4_msg(sb, KERN_ERR, "can't mount with "
  4465. "both data=journal and delalloc");
  4466. err = -EINVAL;
  4467. goto restore_opts;
  4468. }
  4469. if (test_opt(sb, DIOREAD_NOLOCK)) {
  4470. ext4_msg(sb, KERN_ERR, "can't mount with "
  4471. "both data=journal and dioread_nolock");
  4472. err = -EINVAL;
  4473. goto restore_opts;
  4474. }
  4475. if (test_opt(sb, DAX)) {
  4476. ext4_msg(sb, KERN_ERR, "can't mount with "
  4477. "both data=journal and dax");
  4478. err = -EINVAL;
  4479. goto restore_opts;
  4480. }
  4481. } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
  4482. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  4483. ext4_msg(sb, KERN_ERR, "can't mount with "
  4484. "journal_async_commit in data=ordered mode");
  4485. err = -EINVAL;
  4486. goto restore_opts;
  4487. }
  4488. }
  4489. if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
  4490. ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
  4491. err = -EINVAL;
  4492. goto restore_opts;
  4493. }
  4494. if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_DAX) {
  4495. ext4_msg(sb, KERN_WARNING, "warning: refusing change of "
  4496. "dax flag with busy inodes while remounting");
  4497. sbi->s_mount_opt ^= EXT4_MOUNT_DAX;
  4498. }
  4499. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
  4500. ext4_abort(sb, "Abort forced by user");
  4501. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  4502. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  4503. es = sbi->s_es;
  4504. if (sbi->s_journal) {
  4505. ext4_init_journal_params(sb, sbi->s_journal);
  4506. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  4507. }
  4508. if (*flags & MS_LAZYTIME)
  4509. sb->s_flags |= MS_LAZYTIME;
  4510. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
  4511. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
  4512. err = -EROFS;
  4513. goto restore_opts;
  4514. }
  4515. if (*flags & MS_RDONLY) {
  4516. err = sync_filesystem(sb);
  4517. if (err < 0)
  4518. goto restore_opts;
  4519. err = dquot_suspend(sb, -1);
  4520. if (err < 0)
  4521. goto restore_opts;
  4522. /*
  4523. * First of all, the unconditional stuff we have to do
  4524. * to disable replay of the journal when we next remount
  4525. */
  4526. sb->s_flags |= MS_RDONLY;
  4527. /*
  4528. * OK, test if we are remounting a valid rw partition
  4529. * readonly, and if so set the rdonly flag and then
  4530. * mark the partition as valid again.
  4531. */
  4532. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  4533. (sbi->s_mount_state & EXT4_VALID_FS))
  4534. es->s_state = cpu_to_le16(sbi->s_mount_state);
  4535. if (sbi->s_journal)
  4536. ext4_mark_recovery_complete(sb, es);
  4537. } else {
  4538. /* Make sure we can mount this feature set readwrite */
  4539. if (ext4_has_feature_readonly(sb) ||
  4540. !ext4_feature_set_ok(sb, 0)) {
  4541. err = -EROFS;
  4542. goto restore_opts;
  4543. }
  4544. /*
  4545. * Make sure the group descriptor checksums
  4546. * are sane. If they aren't, refuse to remount r/w.
  4547. */
  4548. for (g = 0; g < sbi->s_groups_count; g++) {
  4549. struct ext4_group_desc *gdp =
  4550. ext4_get_group_desc(sb, g, NULL);
  4551. if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
  4552. ext4_msg(sb, KERN_ERR,
  4553. "ext4_remount: Checksum for group %u failed (%u!=%u)",
  4554. g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
  4555. le16_to_cpu(gdp->bg_checksum));
  4556. err = -EFSBADCRC;
  4557. goto restore_opts;
  4558. }
  4559. }
  4560. /*
  4561. * If we have an unprocessed orphan list hanging
  4562. * around from a previously readonly bdev mount,
  4563. * require a full umount/remount for now.
  4564. */
  4565. if (es->s_last_orphan) {
  4566. ext4_msg(sb, KERN_WARNING, "Couldn't "
  4567. "remount RDWR because of unprocessed "
  4568. "orphan inode list. Please "
  4569. "umount/remount instead");
  4570. err = -EINVAL;
  4571. goto restore_opts;
  4572. }
  4573. /*
  4574. * Mounting a RDONLY partition read-write, so reread
  4575. * and store the current valid flag. (It may have
  4576. * been changed by e2fsck since we originally mounted
  4577. * the partition.)
  4578. */
  4579. if (sbi->s_journal)
  4580. ext4_clear_journal_err(sb, es);
  4581. sbi->s_mount_state = le16_to_cpu(es->s_state);
  4582. if (!ext4_setup_super(sb, es, 0))
  4583. sb->s_flags &= ~MS_RDONLY;
  4584. if (ext4_has_feature_mmp(sb))
  4585. if (ext4_multi_mount_protect(sb,
  4586. le64_to_cpu(es->s_mmp_block))) {
  4587. err = -EROFS;
  4588. goto restore_opts;
  4589. }
  4590. enable_quota = 1;
  4591. }
  4592. }
  4593. /*
  4594. * Reinitialize lazy itable initialization thread based on
  4595. * current settings
  4596. */
  4597. if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
  4598. ext4_unregister_li_request(sb);
  4599. else {
  4600. ext4_group_t first_not_zeroed;
  4601. first_not_zeroed = ext4_has_uninit_itable(sb);
  4602. ext4_register_li_request(sb, first_not_zeroed);
  4603. }
  4604. ext4_setup_system_zone(sb);
  4605. if (sbi->s_journal == NULL && !(old_sb_flags & MS_RDONLY))
  4606. ext4_commit_super(sb, 1);
  4607. #ifdef CONFIG_QUOTA
  4608. /* Release old quota file names */
  4609. for (i = 0; i < EXT4_MAXQUOTAS; i++)
  4610. kfree(old_opts.s_qf_names[i]);
  4611. if (enable_quota) {
  4612. if (sb_any_quota_suspended(sb))
  4613. dquot_resume(sb, -1);
  4614. else if (ext4_has_feature_quota(sb)) {
  4615. err = ext4_enable_quotas(sb);
  4616. if (err)
  4617. goto restore_opts;
  4618. }
  4619. }
  4620. #endif
  4621. *flags = (*flags & ~MS_LAZYTIME) | (sb->s_flags & MS_LAZYTIME);
  4622. ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
  4623. kfree(orig_data);
  4624. return 0;
  4625. restore_opts:
  4626. sb->s_flags = old_sb_flags;
  4627. sbi->s_mount_opt = old_opts.s_mount_opt;
  4628. sbi->s_mount_opt2 = old_opts.s_mount_opt2;
  4629. sbi->s_resuid = old_opts.s_resuid;
  4630. sbi->s_resgid = old_opts.s_resgid;
  4631. sbi->s_commit_interval = old_opts.s_commit_interval;
  4632. sbi->s_min_batch_time = old_opts.s_min_batch_time;
  4633. sbi->s_max_batch_time = old_opts.s_max_batch_time;
  4634. #ifdef CONFIG_QUOTA
  4635. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  4636. for (i = 0; i < EXT4_MAXQUOTAS; i++) {
  4637. kfree(sbi->s_qf_names[i]);
  4638. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  4639. }
  4640. #endif
  4641. kfree(orig_data);
  4642. return err;
  4643. }
  4644. #ifdef CONFIG_QUOTA
  4645. static int ext4_statfs_project(struct super_block *sb,
  4646. kprojid_t projid, struct kstatfs *buf)
  4647. {
  4648. struct kqid qid;
  4649. struct dquot *dquot;
  4650. u64 limit;
  4651. u64 curblock;
  4652. qid = make_kqid_projid(projid);
  4653. dquot = dqget(sb, qid);
  4654. if (IS_ERR(dquot))
  4655. return PTR_ERR(dquot);
  4656. spin_lock(&dq_data_lock);
  4657. limit = (dquot->dq_dqb.dqb_bsoftlimit ?
  4658. dquot->dq_dqb.dqb_bsoftlimit :
  4659. dquot->dq_dqb.dqb_bhardlimit) >> sb->s_blocksize_bits;
  4660. if (limit && buf->f_blocks > limit) {
  4661. curblock = dquot->dq_dqb.dqb_curspace >> sb->s_blocksize_bits;
  4662. buf->f_blocks = limit;
  4663. buf->f_bfree = buf->f_bavail =
  4664. (buf->f_blocks > curblock) ?
  4665. (buf->f_blocks - curblock) : 0;
  4666. }
  4667. limit = dquot->dq_dqb.dqb_isoftlimit ?
  4668. dquot->dq_dqb.dqb_isoftlimit :
  4669. dquot->dq_dqb.dqb_ihardlimit;
  4670. if (limit && buf->f_files > limit) {
  4671. buf->f_files = limit;
  4672. buf->f_ffree =
  4673. (buf->f_files > dquot->dq_dqb.dqb_curinodes) ?
  4674. (buf->f_files - dquot->dq_dqb.dqb_curinodes) : 0;
  4675. }
  4676. spin_unlock(&dq_data_lock);
  4677. dqput(dquot);
  4678. return 0;
  4679. }
  4680. #endif
  4681. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
  4682. {
  4683. struct super_block *sb = dentry->d_sb;
  4684. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4685. struct ext4_super_block *es = sbi->s_es;
  4686. ext4_fsblk_t overhead = 0, resv_blocks;
  4687. u64 fsid;
  4688. s64 bfree;
  4689. resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
  4690. if (!test_opt(sb, MINIX_DF))
  4691. overhead = sbi->s_overhead;
  4692. buf->f_type = EXT4_SUPER_MAGIC;
  4693. buf->f_bsize = sb->s_blocksize;
  4694. buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
  4695. bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
  4696. percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
  4697. /* prevent underflow in case that few free space is available */
  4698. buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
  4699. buf->f_bavail = buf->f_bfree -
  4700. (ext4_r_blocks_count(es) + resv_blocks);
  4701. if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
  4702. buf->f_bavail = 0;
  4703. buf->f_files = le32_to_cpu(es->s_inodes_count);
  4704. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  4705. buf->f_namelen = EXT4_NAME_LEN;
  4706. fsid = le64_to_cpup((void *)es->s_uuid) ^
  4707. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  4708. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  4709. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  4710. #ifdef CONFIG_QUOTA
  4711. if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
  4712. sb_has_quota_limits_enabled(sb, PRJQUOTA))
  4713. ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
  4714. #endif
  4715. return 0;
  4716. }
  4717. /* Helper function for writing quotas on sync - we need to start transaction
  4718. * before quota file is locked for write. Otherwise the are possible deadlocks:
  4719. * Process 1 Process 2
  4720. * ext4_create() quota_sync()
  4721. * jbd2_journal_start() write_dquot()
  4722. * dquot_initialize() down(dqio_mutex)
  4723. * down(dqio_mutex) jbd2_journal_start()
  4724. *
  4725. */
  4726. #ifdef CONFIG_QUOTA
  4727. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  4728. {
  4729. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
  4730. }
  4731. static int ext4_write_dquot(struct dquot *dquot)
  4732. {
  4733. int ret, err;
  4734. handle_t *handle;
  4735. struct inode *inode;
  4736. inode = dquot_to_inode(dquot);
  4737. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  4738. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  4739. if (IS_ERR(handle))
  4740. return PTR_ERR(handle);
  4741. ret = dquot_commit(dquot);
  4742. err = ext4_journal_stop(handle);
  4743. if (!ret)
  4744. ret = err;
  4745. return ret;
  4746. }
  4747. static int ext4_acquire_dquot(struct dquot *dquot)
  4748. {
  4749. int ret, err;
  4750. handle_t *handle;
  4751. handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
  4752. EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  4753. if (IS_ERR(handle))
  4754. return PTR_ERR(handle);
  4755. ret = dquot_acquire(dquot);
  4756. err = ext4_journal_stop(handle);
  4757. if (!ret)
  4758. ret = err;
  4759. return ret;
  4760. }
  4761. static int ext4_release_dquot(struct dquot *dquot)
  4762. {
  4763. int ret, err;
  4764. handle_t *handle;
  4765. handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
  4766. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  4767. if (IS_ERR(handle)) {
  4768. /* Release dquot anyway to avoid endless cycle in dqput() */
  4769. dquot_release(dquot);
  4770. return PTR_ERR(handle);
  4771. }
  4772. ret = dquot_release(dquot);
  4773. err = ext4_journal_stop(handle);
  4774. if (!ret)
  4775. ret = err;
  4776. return ret;
  4777. }
  4778. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  4779. {
  4780. struct super_block *sb = dquot->dq_sb;
  4781. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4782. /* Are we journaling quotas? */
  4783. if (ext4_has_feature_quota(sb) ||
  4784. sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
  4785. dquot_mark_dquot_dirty(dquot);
  4786. return ext4_write_dquot(dquot);
  4787. } else {
  4788. return dquot_mark_dquot_dirty(dquot);
  4789. }
  4790. }
  4791. static int ext4_write_info(struct super_block *sb, int type)
  4792. {
  4793. int ret, err;
  4794. handle_t *handle;
  4795. /* Data block + inode block */
  4796. handle = ext4_journal_start(d_inode(sb->s_root), EXT4_HT_QUOTA, 2);
  4797. if (IS_ERR(handle))
  4798. return PTR_ERR(handle);
  4799. ret = dquot_commit_info(sb, type);
  4800. err = ext4_journal_stop(handle);
  4801. if (!ret)
  4802. ret = err;
  4803. return ret;
  4804. }
  4805. /*
  4806. * Turn on quotas during mount time - we need to find
  4807. * the quota file and such...
  4808. */
  4809. static int ext4_quota_on_mount(struct super_block *sb, int type)
  4810. {
  4811. return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
  4812. EXT4_SB(sb)->s_jquota_fmt, type);
  4813. }
  4814. static void lockdep_set_quota_inode(struct inode *inode, int subclass)
  4815. {
  4816. struct ext4_inode_info *ei = EXT4_I(inode);
  4817. /* The first argument of lockdep_set_subclass has to be
  4818. * *exactly* the same as the argument to init_rwsem() --- in
  4819. * this case, in init_once() --- or lockdep gets unhappy
  4820. * because the name of the lock is set using the
  4821. * stringification of the argument to init_rwsem().
  4822. */
  4823. (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
  4824. lockdep_set_subclass(&ei->i_data_sem, subclass);
  4825. }
  4826. /*
  4827. * Standard function to be called on quota_on
  4828. */
  4829. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  4830. const struct path *path)
  4831. {
  4832. int err;
  4833. if (!test_opt(sb, QUOTA))
  4834. return -EINVAL;
  4835. /* Quotafile not on the same filesystem? */
  4836. if (path->dentry->d_sb != sb)
  4837. return -EXDEV;
  4838. /* Journaling quota? */
  4839. if (EXT4_SB(sb)->s_qf_names[type]) {
  4840. /* Quotafile not in fs root? */
  4841. if (path->dentry->d_parent != sb->s_root)
  4842. ext4_msg(sb, KERN_WARNING,
  4843. "Quota file not on filesystem root. "
  4844. "Journaled quota will not work");
  4845. }
  4846. /*
  4847. * When we journal data on quota file, we have to flush journal to see
  4848. * all updates to the file when we bypass pagecache...
  4849. */
  4850. if (EXT4_SB(sb)->s_journal &&
  4851. ext4_should_journal_data(d_inode(path->dentry))) {
  4852. /*
  4853. * We don't need to lock updates but journal_flush() could
  4854. * otherwise be livelocked...
  4855. */
  4856. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  4857. err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  4858. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  4859. if (err)
  4860. return err;
  4861. }
  4862. lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
  4863. err = dquot_quota_on(sb, type, format_id, path);
  4864. if (err) {
  4865. lockdep_set_quota_inode(path->dentry->d_inode,
  4866. I_DATA_SEM_NORMAL);
  4867. } else {
  4868. struct inode *inode = d_inode(path->dentry);
  4869. handle_t *handle;
  4870. /*
  4871. * Set inode flags to prevent userspace from messing with quota
  4872. * files. If this fails, we return success anyway since quotas
  4873. * are already enabled and this is not a hard failure.
  4874. */
  4875. inode_lock(inode);
  4876. handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
  4877. if (IS_ERR(handle))
  4878. goto unlock_inode;
  4879. EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
  4880. inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
  4881. S_NOATIME | S_IMMUTABLE);
  4882. ext4_mark_inode_dirty(handle, inode);
  4883. ext4_journal_stop(handle);
  4884. unlock_inode:
  4885. inode_unlock(inode);
  4886. }
  4887. return err;
  4888. }
  4889. static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
  4890. unsigned int flags)
  4891. {
  4892. int err;
  4893. struct inode *qf_inode;
  4894. unsigned long qf_inums[EXT4_MAXQUOTAS] = {
  4895. le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
  4896. le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
  4897. le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
  4898. };
  4899. BUG_ON(!ext4_has_feature_quota(sb));
  4900. if (!qf_inums[type])
  4901. return -EPERM;
  4902. qf_inode = ext4_iget(sb, qf_inums[type]);
  4903. if (IS_ERR(qf_inode)) {
  4904. ext4_error(sb, "Bad quota inode # %lu", qf_inums[type]);
  4905. return PTR_ERR(qf_inode);
  4906. }
  4907. /* Don't account quota for quota files to avoid recursion */
  4908. qf_inode->i_flags |= S_NOQUOTA;
  4909. lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
  4910. err = dquot_enable(qf_inode, type, format_id, flags);
  4911. iput(qf_inode);
  4912. if (err)
  4913. lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
  4914. return err;
  4915. }
  4916. /* Enable usage tracking for all quota types. */
  4917. static int ext4_enable_quotas(struct super_block *sb)
  4918. {
  4919. int type, err = 0;
  4920. unsigned long qf_inums[EXT4_MAXQUOTAS] = {
  4921. le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
  4922. le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
  4923. le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
  4924. };
  4925. bool quota_mopt[EXT4_MAXQUOTAS] = {
  4926. test_opt(sb, USRQUOTA),
  4927. test_opt(sb, GRPQUOTA),
  4928. test_opt(sb, PRJQUOTA),
  4929. };
  4930. sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE;
  4931. for (type = 0; type < EXT4_MAXQUOTAS; type++) {
  4932. if (qf_inums[type]) {
  4933. err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
  4934. DQUOT_USAGE_ENABLED |
  4935. (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
  4936. if (err) {
  4937. ext4_warning(sb,
  4938. "Failed to enable quota tracking "
  4939. "(type=%d, err=%d). Please run "
  4940. "e2fsck to fix.", type, err);
  4941. return err;
  4942. }
  4943. }
  4944. }
  4945. return 0;
  4946. }
  4947. static int ext4_quota_off(struct super_block *sb, int type)
  4948. {
  4949. struct inode *inode = sb_dqopt(sb)->files[type];
  4950. handle_t *handle;
  4951. int err;
  4952. /* Force all delayed allocation blocks to be allocated.
  4953. * Caller already holds s_umount sem */
  4954. if (test_opt(sb, DELALLOC))
  4955. sync_filesystem(sb);
  4956. if (!inode || !igrab(inode))
  4957. goto out;
  4958. err = dquot_quota_off(sb, type);
  4959. if (err || ext4_has_feature_quota(sb))
  4960. goto out_put;
  4961. inode_lock(inode);
  4962. /*
  4963. * Update modification times of quota files when userspace can
  4964. * start looking at them. If we fail, we return success anyway since
  4965. * this is not a hard failure and quotas are already disabled.
  4966. */
  4967. handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
  4968. if (IS_ERR(handle))
  4969. goto out_unlock;
  4970. EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
  4971. inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
  4972. inode->i_mtime = inode->i_ctime = current_time(inode);
  4973. ext4_mark_inode_dirty(handle, inode);
  4974. ext4_journal_stop(handle);
  4975. out_unlock:
  4976. inode_unlock(inode);
  4977. out_put:
  4978. lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
  4979. iput(inode);
  4980. return err;
  4981. out:
  4982. return dquot_quota_off(sb, type);
  4983. }
  4984. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  4985. * acquiring the locks... As quota files are never truncated and quota code
  4986. * itself serializes the operations (and no one else should touch the files)
  4987. * we don't have to be afraid of races */
  4988. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  4989. size_t len, loff_t off)
  4990. {
  4991. struct inode *inode = sb_dqopt(sb)->files[type];
  4992. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  4993. int offset = off & (sb->s_blocksize - 1);
  4994. int tocopy;
  4995. size_t toread;
  4996. struct buffer_head *bh;
  4997. loff_t i_size = i_size_read(inode);
  4998. if (off > i_size)
  4999. return 0;
  5000. if (off+len > i_size)
  5001. len = i_size-off;
  5002. toread = len;
  5003. while (toread > 0) {
  5004. tocopy = sb->s_blocksize - offset < toread ?
  5005. sb->s_blocksize - offset : toread;
  5006. bh = ext4_bread(NULL, inode, blk, 0);
  5007. if (IS_ERR(bh))
  5008. return PTR_ERR(bh);
  5009. if (!bh) /* A hole? */
  5010. memset(data, 0, tocopy);
  5011. else
  5012. memcpy(data, bh->b_data+offset, tocopy);
  5013. brelse(bh);
  5014. offset = 0;
  5015. toread -= tocopy;
  5016. data += tocopy;
  5017. blk++;
  5018. }
  5019. return len;
  5020. }
  5021. /* Write to quotafile (we know the transaction is already started and has
  5022. * enough credits) */
  5023. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  5024. const char *data, size_t len, loff_t off)
  5025. {
  5026. struct inode *inode = sb_dqopt(sb)->files[type];
  5027. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  5028. int err, offset = off & (sb->s_blocksize - 1);
  5029. int retries = 0;
  5030. struct buffer_head *bh;
  5031. handle_t *handle = journal_current_handle();
  5032. if (EXT4_SB(sb)->s_journal && !handle) {
  5033. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  5034. " cancelled because transaction is not started",
  5035. (unsigned long long)off, (unsigned long long)len);
  5036. return -EIO;
  5037. }
  5038. /*
  5039. * Since we account only one data block in transaction credits,
  5040. * then it is impossible to cross a block boundary.
  5041. */
  5042. if (sb->s_blocksize - offset < len) {
  5043. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  5044. " cancelled because not block aligned",
  5045. (unsigned long long)off, (unsigned long long)len);
  5046. return -EIO;
  5047. }
  5048. do {
  5049. bh = ext4_bread(handle, inode, blk,
  5050. EXT4_GET_BLOCKS_CREATE |
  5051. EXT4_GET_BLOCKS_METADATA_NOFAIL);
  5052. } while (IS_ERR(bh) && (PTR_ERR(bh) == -ENOSPC) &&
  5053. ext4_should_retry_alloc(inode->i_sb, &retries));
  5054. if (IS_ERR(bh))
  5055. return PTR_ERR(bh);
  5056. if (!bh)
  5057. goto out;
  5058. BUFFER_TRACE(bh, "get write access");
  5059. err = ext4_journal_get_write_access(handle, bh);
  5060. if (err) {
  5061. brelse(bh);
  5062. return err;
  5063. }
  5064. lock_buffer(bh);
  5065. memcpy(bh->b_data+offset, data, len);
  5066. flush_dcache_page(bh->b_page);
  5067. unlock_buffer(bh);
  5068. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  5069. brelse(bh);
  5070. out:
  5071. if (inode->i_size < off + len) {
  5072. i_size_write(inode, off + len);
  5073. EXT4_I(inode)->i_disksize = inode->i_size;
  5074. ext4_mark_inode_dirty(handle, inode);
  5075. }
  5076. return len;
  5077. }
  5078. static int ext4_get_next_id(struct super_block *sb, struct kqid *qid)
  5079. {
  5080. const struct quota_format_ops *ops;
  5081. if (!sb_has_quota_loaded(sb, qid->type))
  5082. return -ESRCH;
  5083. ops = sb_dqopt(sb)->ops[qid->type];
  5084. if (!ops || !ops->get_next_id)
  5085. return -ENOSYS;
  5086. return dquot_get_next_id(sb, qid);
  5087. }
  5088. #endif
  5089. static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
  5090. const char *dev_name, void *data)
  5091. {
  5092. return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
  5093. }
  5094. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
  5095. static inline void register_as_ext2(void)
  5096. {
  5097. int err = register_filesystem(&ext2_fs_type);
  5098. if (err)
  5099. printk(KERN_WARNING
  5100. "EXT4-fs: Unable to register as ext2 (%d)\n", err);
  5101. }
  5102. static inline void unregister_as_ext2(void)
  5103. {
  5104. unregister_filesystem(&ext2_fs_type);
  5105. }
  5106. static inline int ext2_feature_set_ok(struct super_block *sb)
  5107. {
  5108. if (ext4_has_unknown_ext2_incompat_features(sb))
  5109. return 0;
  5110. if (sb->s_flags & MS_RDONLY)
  5111. return 1;
  5112. if (ext4_has_unknown_ext2_ro_compat_features(sb))
  5113. return 0;
  5114. return 1;
  5115. }
  5116. #else
  5117. static inline void register_as_ext2(void) { }
  5118. static inline void unregister_as_ext2(void) { }
  5119. static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
  5120. #endif
  5121. static inline void register_as_ext3(void)
  5122. {
  5123. int err = register_filesystem(&ext3_fs_type);
  5124. if (err)
  5125. printk(KERN_WARNING
  5126. "EXT4-fs: Unable to register as ext3 (%d)\n", err);
  5127. }
  5128. static inline void unregister_as_ext3(void)
  5129. {
  5130. unregister_filesystem(&ext3_fs_type);
  5131. }
  5132. static inline int ext3_feature_set_ok(struct super_block *sb)
  5133. {
  5134. if (ext4_has_unknown_ext3_incompat_features(sb))
  5135. return 0;
  5136. if (!ext4_has_feature_journal(sb))
  5137. return 0;
  5138. if (sb->s_flags & MS_RDONLY)
  5139. return 1;
  5140. if (ext4_has_unknown_ext3_ro_compat_features(sb))
  5141. return 0;
  5142. return 1;
  5143. }
  5144. static struct file_system_type ext4_fs_type = {
  5145. .owner = THIS_MODULE,
  5146. .name = "ext4",
  5147. .mount = ext4_mount,
  5148. .kill_sb = kill_block_super,
  5149. .fs_flags = FS_REQUIRES_DEV,
  5150. };
  5151. MODULE_ALIAS_FS("ext4");
  5152. /* Shared across all ext4 file systems */
  5153. wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
  5154. static int __init ext4_init_fs(void)
  5155. {
  5156. int i, err;
  5157. ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
  5158. ext4_li_info = NULL;
  5159. mutex_init(&ext4_li_mtx);
  5160. /* Build-time check for flags consistency */
  5161. ext4_check_flag_values();
  5162. for (i = 0; i < EXT4_WQ_HASH_SZ; i++)
  5163. init_waitqueue_head(&ext4__ioend_wq[i]);
  5164. err = ext4_init_es();
  5165. if (err)
  5166. return err;
  5167. err = ext4_init_pageio();
  5168. if (err)
  5169. goto out5;
  5170. err = ext4_init_system_zone();
  5171. if (err)
  5172. goto out4;
  5173. err = ext4_init_sysfs();
  5174. if (err)
  5175. goto out3;
  5176. err = ext4_init_mballoc();
  5177. if (err)
  5178. goto out2;
  5179. err = init_inodecache();
  5180. if (err)
  5181. goto out1;
  5182. register_as_ext3();
  5183. register_as_ext2();
  5184. err = register_filesystem(&ext4_fs_type);
  5185. if (err)
  5186. goto out;
  5187. return 0;
  5188. out:
  5189. unregister_as_ext2();
  5190. unregister_as_ext3();
  5191. destroy_inodecache();
  5192. out1:
  5193. ext4_exit_mballoc();
  5194. out2:
  5195. ext4_exit_sysfs();
  5196. out3:
  5197. ext4_exit_system_zone();
  5198. out4:
  5199. ext4_exit_pageio();
  5200. out5:
  5201. ext4_exit_es();
  5202. return err;
  5203. }
  5204. static void __exit ext4_exit_fs(void)
  5205. {
  5206. ext4_destroy_lazyinit_thread();
  5207. unregister_as_ext2();
  5208. unregister_as_ext3();
  5209. unregister_filesystem(&ext4_fs_type);
  5210. destroy_inodecache();
  5211. ext4_exit_mballoc();
  5212. ext4_exit_sysfs();
  5213. ext4_exit_system_zone();
  5214. ext4_exit_pageio();
  5215. ext4_exit_es();
  5216. }
  5217. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  5218. MODULE_DESCRIPTION("Fourth Extended Filesystem");
  5219. MODULE_LICENSE("GPL");
  5220. module_init(ext4_init_fs)
  5221. module_exit(ext4_exit_fs)