super.c 161 KB

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