super.c 211 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext4/super.c
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * from
  11. *
  12. * linux/fs/minix/inode.c
  13. *
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. *
  16. * Big-endian to little-endian byte-swapping/bitmaps by
  17. * David S. Miller (davem@caip.rutgers.edu), 1995
  18. */
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. #include <linux/fs.h>
  22. #include <linux/time.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/slab.h>
  25. #include <linux/init.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/backing-dev.h>
  28. #include <linux/parser.h>
  29. #include <linux/buffer_head.h>
  30. #include <linux/exportfs.h>
  31. #include <linux/vfs.h>
  32. #include <linux/random.h>
  33. #include <linux/mount.h>
  34. #include <linux/namei.h>
  35. #include <linux/quotaops.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/ctype.h>
  38. #include <linux/log2.h>
  39. #include <linux/crc16.h>
  40. #include <linux/dax.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/iversion.h>
  43. #include <linux/unicode.h>
  44. #include <linux/part_stat.h>
  45. #include <linux/kthread.h>
  46. #include <linux/freezer.h>
  47. #include <linux/fsnotify.h>
  48. #include <linux/fs_context.h>
  49. #include <linux/fs_parser.h>
  50. #include <linux/fserror.h>
  51. #include "ext4.h"
  52. #include "ext4_extents.h" /* Needed for trace points definition */
  53. #include "ext4_jbd2.h"
  54. #include "xattr.h"
  55. #include "acl.h"
  56. #include "mballoc.h"
  57. #include "fsmap.h"
  58. #define CREATE_TRACE_POINTS
  59. #include <trace/events/ext4.h>
  60. static struct ext4_lazy_init *ext4_li_info;
  61. static DEFINE_MUTEX(ext4_li_mtx);
  62. static struct ratelimit_state ext4_mount_msg_ratelimit;
  63. static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
  64. unsigned long journal_devnum);
  65. static int ext4_show_options(struct seq_file *seq, struct dentry *root);
  66. static void ext4_update_super(struct super_block *sb);
  67. static int ext4_commit_super(struct super_block *sb);
  68. static int ext4_mark_recovery_complete(struct super_block *sb,
  69. struct ext4_super_block *es);
  70. static int ext4_clear_journal_err(struct super_block *sb,
  71. struct ext4_super_block *es);
  72. static int ext4_sync_fs(struct super_block *sb, int wait);
  73. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf);
  74. static int ext4_unfreeze(struct super_block *sb);
  75. static int ext4_freeze(struct super_block *sb);
  76. static inline int ext2_feature_set_ok(struct super_block *sb);
  77. static inline int ext3_feature_set_ok(struct super_block *sb);
  78. static void ext4_unregister_li_request(struct super_block *sb);
  79. static void ext4_clear_request_list(void);
  80. static struct inode *ext4_get_journal_inode(struct super_block *sb,
  81. unsigned int journal_inum);
  82. static int ext4_validate_options(struct fs_context *fc);
  83. static int ext4_check_opt_consistency(struct fs_context *fc,
  84. struct super_block *sb);
  85. static void ext4_apply_options(struct fs_context *fc, struct super_block *sb);
  86. static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param);
  87. static int ext4_get_tree(struct fs_context *fc);
  88. static int ext4_reconfigure(struct fs_context *fc);
  89. static void ext4_fc_free(struct fs_context *fc);
  90. static int ext4_init_fs_context(struct fs_context *fc);
  91. static void ext4_kill_sb(struct super_block *sb);
  92. static const struct fs_parameter_spec ext4_param_specs[];
  93. /*
  94. * Lock ordering
  95. *
  96. * page fault path:
  97. * mmap_lock -> sb_start_pagefault -> invalidate_lock (r) -> transaction start
  98. * -> page lock -> i_data_sem (rw)
  99. *
  100. * buffered write path:
  101. * sb_start_write -> i_mutex -> mmap_lock
  102. * sb_start_write -> i_mutex -> transaction start -> page lock ->
  103. * i_data_sem (rw)
  104. *
  105. * truncate:
  106. * sb_start_write -> i_mutex -> invalidate_lock (w) -> i_mmap_rwsem (w) ->
  107. * page lock
  108. * sb_start_write -> i_mutex -> invalidate_lock (w) -> transaction start ->
  109. * i_data_sem (rw)
  110. *
  111. * direct IO:
  112. * sb_start_write -> i_mutex -> mmap_lock
  113. * sb_start_write -> i_mutex -> transaction start -> i_data_sem (rw)
  114. *
  115. * writepages:
  116. * transaction start -> page lock(s) -> i_data_sem (rw)
  117. */
  118. static const struct fs_context_operations ext4_context_ops = {
  119. .parse_param = ext4_parse_param,
  120. .get_tree = ext4_get_tree,
  121. .reconfigure = ext4_reconfigure,
  122. .free = ext4_fc_free,
  123. };
  124. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
  125. static struct file_system_type ext2_fs_type = {
  126. .owner = THIS_MODULE,
  127. .name = "ext2",
  128. .init_fs_context = ext4_init_fs_context,
  129. .parameters = ext4_param_specs,
  130. .kill_sb = ext4_kill_sb,
  131. .fs_flags = FS_REQUIRES_DEV,
  132. };
  133. MODULE_ALIAS_FS("ext2");
  134. MODULE_ALIAS("ext2");
  135. #define IS_EXT2_SB(sb) ((sb)->s_type == &ext2_fs_type)
  136. #else
  137. #define IS_EXT2_SB(sb) (0)
  138. #endif
  139. static struct file_system_type ext3_fs_type = {
  140. .owner = THIS_MODULE,
  141. .name = "ext3",
  142. .init_fs_context = ext4_init_fs_context,
  143. .parameters = ext4_param_specs,
  144. .kill_sb = ext4_kill_sb,
  145. .fs_flags = FS_REQUIRES_DEV,
  146. };
  147. MODULE_ALIAS_FS("ext3");
  148. MODULE_ALIAS("ext3");
  149. #define IS_EXT3_SB(sb) ((sb)->s_type == &ext3_fs_type)
  150. static inline void __ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags,
  151. bh_end_io_t *end_io, bool simu_fail)
  152. {
  153. if (simu_fail) {
  154. clear_buffer_uptodate(bh);
  155. unlock_buffer(bh);
  156. return;
  157. }
  158. /*
  159. * buffer's verified bit is no longer valid after reading from
  160. * disk again due to write out error, clear it to make sure we
  161. * recheck the buffer contents.
  162. */
  163. clear_buffer_verified(bh);
  164. bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
  165. get_bh(bh);
  166. submit_bh(REQ_OP_READ | op_flags, bh);
  167. }
  168. void ext4_read_bh_nowait(struct buffer_head *bh, blk_opf_t op_flags,
  169. bh_end_io_t *end_io, bool simu_fail)
  170. {
  171. BUG_ON(!buffer_locked(bh));
  172. if (ext4_buffer_uptodate(bh)) {
  173. unlock_buffer(bh);
  174. return;
  175. }
  176. __ext4_read_bh(bh, op_flags, end_io, simu_fail);
  177. }
  178. int ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags,
  179. bh_end_io_t *end_io, bool simu_fail)
  180. {
  181. BUG_ON(!buffer_locked(bh));
  182. if (ext4_buffer_uptodate(bh)) {
  183. unlock_buffer(bh);
  184. return 0;
  185. }
  186. __ext4_read_bh(bh, op_flags, end_io, simu_fail);
  187. wait_on_buffer(bh);
  188. if (buffer_uptodate(bh))
  189. return 0;
  190. return -EIO;
  191. }
  192. int ext4_read_bh_lock(struct buffer_head *bh, blk_opf_t op_flags, bool wait)
  193. {
  194. lock_buffer(bh);
  195. if (!wait) {
  196. ext4_read_bh_nowait(bh, op_flags, NULL, false);
  197. return 0;
  198. }
  199. return ext4_read_bh(bh, op_flags, NULL, false);
  200. }
  201. /*
  202. * This works like __bread_gfp() except it uses ERR_PTR for error
  203. * returns. Currently with sb_bread it's impossible to distinguish
  204. * between ENOMEM and EIO situations (since both result in a NULL
  205. * return.
  206. */
  207. static struct buffer_head *__ext4_sb_bread_gfp(struct super_block *sb,
  208. sector_t block,
  209. blk_opf_t op_flags, gfp_t gfp)
  210. {
  211. struct buffer_head *bh;
  212. int ret;
  213. bh = sb_getblk_gfp(sb, block, gfp);
  214. if (bh == NULL)
  215. return ERR_PTR(-ENOMEM);
  216. if (ext4_buffer_uptodate(bh))
  217. return bh;
  218. ret = ext4_read_bh_lock(bh, REQ_META | op_flags, true);
  219. if (ret) {
  220. put_bh(bh);
  221. return ERR_PTR(ret);
  222. }
  223. return bh;
  224. }
  225. struct buffer_head *ext4_sb_bread(struct super_block *sb, sector_t block,
  226. blk_opf_t op_flags)
  227. {
  228. gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping,
  229. ~__GFP_FS) | __GFP_MOVABLE;
  230. return __ext4_sb_bread_gfp(sb, block, op_flags, gfp);
  231. }
  232. struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
  233. sector_t block)
  234. {
  235. gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping,
  236. ~__GFP_FS);
  237. return __ext4_sb_bread_gfp(sb, block, 0, gfp);
  238. }
  239. struct buffer_head *ext4_sb_bread_nofail(struct super_block *sb,
  240. sector_t block)
  241. {
  242. gfp_t gfp = mapping_gfp_constraint(sb->s_bdev->bd_mapping,
  243. ~__GFP_FS) | __GFP_MOVABLE | __GFP_NOFAIL;
  244. return __ext4_sb_bread_gfp(sb, block, 0, gfp);
  245. }
  246. void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block)
  247. {
  248. struct buffer_head *bh = bdev_getblk(sb->s_bdev, block,
  249. sb->s_blocksize, GFP_NOWAIT);
  250. if (likely(bh)) {
  251. if (trylock_buffer(bh))
  252. ext4_read_bh_nowait(bh, REQ_RAHEAD, NULL, false);
  253. brelse(bh);
  254. }
  255. }
  256. static int ext4_verify_csum_type(struct super_block *sb,
  257. struct ext4_super_block *es)
  258. {
  259. if (!ext4_has_feature_metadata_csum(sb))
  260. return 1;
  261. return es->s_checksum_type == EXT4_CRC32C_CHKSUM;
  262. }
  263. __le32 ext4_superblock_csum(struct ext4_super_block *es)
  264. {
  265. int offset = offsetof(struct ext4_super_block, s_checksum);
  266. __u32 csum;
  267. csum = ext4_chksum(~0, (char *)es, offset);
  268. return cpu_to_le32(csum);
  269. }
  270. static int ext4_superblock_csum_verify(struct super_block *sb,
  271. struct ext4_super_block *es)
  272. {
  273. if (!ext4_has_feature_metadata_csum(sb))
  274. return 1;
  275. return es->s_checksum == ext4_superblock_csum(es);
  276. }
  277. void ext4_superblock_csum_set(struct super_block *sb)
  278. {
  279. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  280. if (!ext4_has_feature_metadata_csum(sb))
  281. return;
  282. es->s_checksum = ext4_superblock_csum(es);
  283. }
  284. ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
  285. struct ext4_group_desc *bg)
  286. {
  287. return le32_to_cpu(bg->bg_block_bitmap_lo) |
  288. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  289. (ext4_fsblk_t)le32_to_cpu(bg->bg_block_bitmap_hi) << 32 : 0);
  290. }
  291. ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
  292. struct ext4_group_desc *bg)
  293. {
  294. return le32_to_cpu(bg->bg_inode_bitmap_lo) |
  295. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  296. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_bitmap_hi) << 32 : 0);
  297. }
  298. ext4_fsblk_t ext4_inode_table(struct super_block *sb,
  299. struct ext4_group_desc *bg)
  300. {
  301. return le32_to_cpu(bg->bg_inode_table_lo) |
  302. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  303. (ext4_fsblk_t)le32_to_cpu(bg->bg_inode_table_hi) << 32 : 0);
  304. }
  305. __u32 ext4_free_group_clusters(struct super_block *sb,
  306. struct ext4_group_desc *bg)
  307. {
  308. return le16_to_cpu(bg->bg_free_blocks_count_lo) |
  309. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  310. (__u32)le16_to_cpu(bg->bg_free_blocks_count_hi) << 16 : 0);
  311. }
  312. __u32 ext4_free_inodes_count(struct super_block *sb,
  313. struct ext4_group_desc *bg)
  314. {
  315. return le16_to_cpu(READ_ONCE(bg->bg_free_inodes_count_lo)) |
  316. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  317. (__u32)le16_to_cpu(READ_ONCE(bg->bg_free_inodes_count_hi)) << 16 : 0);
  318. }
  319. __u32 ext4_used_dirs_count(struct super_block *sb,
  320. struct ext4_group_desc *bg)
  321. {
  322. return le16_to_cpu(bg->bg_used_dirs_count_lo) |
  323. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  324. (__u32)le16_to_cpu(bg->bg_used_dirs_count_hi) << 16 : 0);
  325. }
  326. __u32 ext4_itable_unused_count(struct super_block *sb,
  327. struct ext4_group_desc *bg)
  328. {
  329. return le16_to_cpu(bg->bg_itable_unused_lo) |
  330. (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT ?
  331. (__u32)le16_to_cpu(bg->bg_itable_unused_hi) << 16 : 0);
  332. }
  333. void ext4_block_bitmap_set(struct super_block *sb,
  334. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  335. {
  336. bg->bg_block_bitmap_lo = cpu_to_le32((u32)blk);
  337. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  338. bg->bg_block_bitmap_hi = cpu_to_le32(blk >> 32);
  339. }
  340. void ext4_inode_bitmap_set(struct super_block *sb,
  341. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  342. {
  343. bg->bg_inode_bitmap_lo = cpu_to_le32((u32)blk);
  344. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  345. bg->bg_inode_bitmap_hi = cpu_to_le32(blk >> 32);
  346. }
  347. void ext4_inode_table_set(struct super_block *sb,
  348. struct ext4_group_desc *bg, ext4_fsblk_t blk)
  349. {
  350. bg->bg_inode_table_lo = cpu_to_le32((u32)blk);
  351. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  352. bg->bg_inode_table_hi = cpu_to_le32(blk >> 32);
  353. }
  354. void ext4_free_group_clusters_set(struct super_block *sb,
  355. struct ext4_group_desc *bg, __u32 count)
  356. {
  357. bg->bg_free_blocks_count_lo = cpu_to_le16((__u16)count);
  358. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  359. bg->bg_free_blocks_count_hi = cpu_to_le16(count >> 16);
  360. }
  361. void ext4_free_inodes_set(struct super_block *sb,
  362. struct ext4_group_desc *bg, __u32 count)
  363. {
  364. WRITE_ONCE(bg->bg_free_inodes_count_lo, cpu_to_le16((__u16)count));
  365. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  366. WRITE_ONCE(bg->bg_free_inodes_count_hi, cpu_to_le16(count >> 16));
  367. }
  368. void ext4_used_dirs_set(struct super_block *sb,
  369. struct ext4_group_desc *bg, __u32 count)
  370. {
  371. bg->bg_used_dirs_count_lo = cpu_to_le16((__u16)count);
  372. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  373. bg->bg_used_dirs_count_hi = cpu_to_le16(count >> 16);
  374. }
  375. void ext4_itable_unused_set(struct super_block *sb,
  376. struct ext4_group_desc *bg, __u32 count)
  377. {
  378. bg->bg_itable_unused_lo = cpu_to_le16((__u16)count);
  379. if (EXT4_DESC_SIZE(sb) >= EXT4_MIN_DESC_SIZE_64BIT)
  380. bg->bg_itable_unused_hi = cpu_to_le16(count >> 16);
  381. }
  382. static void __ext4_update_tstamp(__le32 *lo, __u8 *hi, time64_t now)
  383. {
  384. now = clamp_val(now, 0, (1ull << 40) - 1);
  385. *lo = cpu_to_le32(lower_32_bits(now));
  386. *hi = upper_32_bits(now);
  387. }
  388. static time64_t __ext4_get_tstamp(__le32 *lo, __u8 *hi)
  389. {
  390. return ((time64_t)(*hi) << 32) + le32_to_cpu(*lo);
  391. }
  392. #define ext4_update_tstamp(es, tstamp) \
  393. __ext4_update_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi, \
  394. ktime_get_real_seconds())
  395. #define ext4_get_tstamp(es, tstamp) \
  396. __ext4_get_tstamp(&(es)->tstamp, &(es)->tstamp ## _hi)
  397. /*
  398. * The ext4_maybe_update_superblock() function checks and updates the
  399. * superblock if needed.
  400. *
  401. * This function is designed to update the on-disk superblock only under
  402. * certain conditions to prevent excessive disk writes and unnecessary
  403. * waking of the disk from sleep. The superblock will be updated if:
  404. * 1. More than sbi->s_sb_update_sec (def: 1 hour) has passed since the last
  405. * superblock update
  406. * 2. More than sbi->s_sb_update_kb (def: 16MB) kbs have been written since the
  407. * last superblock update.
  408. *
  409. * @sb: The superblock
  410. */
  411. static void ext4_maybe_update_superblock(struct super_block *sb)
  412. {
  413. struct ext4_sb_info *sbi = EXT4_SB(sb);
  414. struct ext4_super_block *es = sbi->s_es;
  415. journal_t *journal = sbi->s_journal;
  416. time64_t now;
  417. __u64 last_update;
  418. __u64 lifetime_write_kbytes;
  419. __u64 diff_size;
  420. if (ext4_emergency_state(sb) || sb_rdonly(sb) ||
  421. !(sb->s_flags & SB_ACTIVE) || !journal ||
  422. journal->j_flags & JBD2_UNMOUNT)
  423. return;
  424. now = ktime_get_real_seconds();
  425. last_update = ext4_get_tstamp(es, s_wtime);
  426. if (likely(now - last_update < sbi->s_sb_update_sec))
  427. return;
  428. lifetime_write_kbytes = sbi->s_kbytes_written +
  429. ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
  430. sbi->s_sectors_written_start) >> 1);
  431. /* Get the number of kilobytes not written to disk to account
  432. * for statistics and compare with a multiple of 16 MB. This
  433. * is used to determine when the next superblock commit should
  434. * occur (i.e. not more often than once per 16MB if there was
  435. * less written in an hour).
  436. */
  437. diff_size = lifetime_write_kbytes - le64_to_cpu(es->s_kbytes_written);
  438. if (diff_size > sbi->s_sb_update_kb)
  439. schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
  440. }
  441. static void ext4_journal_commit_callback(journal_t *journal, transaction_t *txn)
  442. {
  443. struct super_block *sb = journal->j_private;
  444. BUG_ON(txn->t_state == T_FINISHED);
  445. ext4_process_freed_data(sb, txn->t_tid);
  446. ext4_maybe_update_superblock(sb);
  447. }
  448. static bool ext4_journalled_writepage_needs_redirty(struct jbd2_inode *jinode,
  449. struct folio *folio)
  450. {
  451. struct buffer_head *bh, *head;
  452. struct journal_head *jh;
  453. bh = head = folio_buffers(folio);
  454. do {
  455. /*
  456. * We have to redirty a page in these cases:
  457. * 1) If buffer is dirty, it means the page was dirty because it
  458. * contains a buffer that needs checkpointing. So the dirty bit
  459. * needs to be preserved so that checkpointing writes the buffer
  460. * properly.
  461. * 2) If buffer is not part of the committing transaction
  462. * (we may have just accidentally come across this buffer because
  463. * inode range tracking is not exact) or if the currently running
  464. * transaction already contains this buffer as well, dirty bit
  465. * needs to be preserved so that the buffer gets writeprotected
  466. * properly on running transaction's commit.
  467. */
  468. jh = bh2jh(bh);
  469. if (buffer_dirty(bh) ||
  470. (jh && (jh->b_transaction != jinode->i_transaction ||
  471. jh->b_next_transaction)))
  472. return true;
  473. } while ((bh = bh->b_this_page) != head);
  474. return false;
  475. }
  476. static int ext4_journalled_submit_inode_data_buffers(struct jbd2_inode *jinode)
  477. {
  478. struct address_space *mapping = jinode->i_vfs_inode->i_mapping;
  479. struct writeback_control wbc = {
  480. .sync_mode = WB_SYNC_ALL,
  481. .nr_to_write = LONG_MAX,
  482. .range_start = jinode->i_dirty_start,
  483. .range_end = jinode->i_dirty_end,
  484. };
  485. struct folio *folio = NULL;
  486. int error;
  487. /*
  488. * writeback_iter() already checks for dirty pages and calls
  489. * folio_clear_dirty_for_io(), which we want to write protect the
  490. * folios.
  491. *
  492. * However, we may have to redirty a folio sometimes.
  493. */
  494. while ((folio = writeback_iter(mapping, &wbc, folio, &error))) {
  495. if (ext4_journalled_writepage_needs_redirty(jinode, folio))
  496. folio_redirty_for_writepage(&wbc, folio);
  497. folio_unlock(folio);
  498. }
  499. return error;
  500. }
  501. static int ext4_journal_submit_inode_data_buffers(struct jbd2_inode *jinode)
  502. {
  503. int ret;
  504. if (ext4_should_journal_data(jinode->i_vfs_inode))
  505. ret = ext4_journalled_submit_inode_data_buffers(jinode);
  506. else
  507. ret = ext4_normal_submit_inode_data_buffers(jinode);
  508. return ret;
  509. }
  510. static int ext4_journal_finish_inode_data_buffers(struct jbd2_inode *jinode)
  511. {
  512. int ret = 0;
  513. if (!ext4_should_journal_data(jinode->i_vfs_inode))
  514. ret = jbd2_journal_finish_inode_data_buffers(jinode);
  515. return ret;
  516. }
  517. static bool system_going_down(void)
  518. {
  519. return system_state == SYSTEM_HALT || system_state == SYSTEM_POWER_OFF
  520. || system_state == SYSTEM_RESTART;
  521. }
  522. struct ext4_err_translation {
  523. int code;
  524. int errno;
  525. };
  526. #define EXT4_ERR_TRANSLATE(err) { .code = EXT4_ERR_##err, .errno = err }
  527. static struct ext4_err_translation err_translation[] = {
  528. EXT4_ERR_TRANSLATE(EIO),
  529. EXT4_ERR_TRANSLATE(ENOMEM),
  530. EXT4_ERR_TRANSLATE(EFSBADCRC),
  531. EXT4_ERR_TRANSLATE(EFSCORRUPTED),
  532. EXT4_ERR_TRANSLATE(ENOSPC),
  533. EXT4_ERR_TRANSLATE(ENOKEY),
  534. EXT4_ERR_TRANSLATE(EROFS),
  535. EXT4_ERR_TRANSLATE(EFBIG),
  536. EXT4_ERR_TRANSLATE(EEXIST),
  537. EXT4_ERR_TRANSLATE(ERANGE),
  538. EXT4_ERR_TRANSLATE(EOVERFLOW),
  539. EXT4_ERR_TRANSLATE(EBUSY),
  540. EXT4_ERR_TRANSLATE(ENOTDIR),
  541. EXT4_ERR_TRANSLATE(ENOTEMPTY),
  542. EXT4_ERR_TRANSLATE(ESHUTDOWN),
  543. EXT4_ERR_TRANSLATE(EFAULT),
  544. };
  545. static int ext4_errno_to_code(int errno)
  546. {
  547. int i;
  548. for (i = 0; i < ARRAY_SIZE(err_translation); i++)
  549. if (err_translation[i].errno == errno)
  550. return err_translation[i].code;
  551. return EXT4_ERR_UNKNOWN;
  552. }
  553. static void save_error_info(struct super_block *sb, int error,
  554. __u32 ino, __u64 block,
  555. const char *func, unsigned int line)
  556. {
  557. struct ext4_sb_info *sbi = EXT4_SB(sb);
  558. /* We default to EFSCORRUPTED error... */
  559. if (error == 0)
  560. error = EFSCORRUPTED;
  561. spin_lock(&sbi->s_error_lock);
  562. sbi->s_add_error_count++;
  563. sbi->s_last_error_code = error;
  564. sbi->s_last_error_line = line;
  565. sbi->s_last_error_ino = ino;
  566. sbi->s_last_error_block = block;
  567. sbi->s_last_error_func = func;
  568. sbi->s_last_error_time = ktime_get_real_seconds();
  569. if (!sbi->s_first_error_time) {
  570. sbi->s_first_error_code = error;
  571. sbi->s_first_error_line = line;
  572. sbi->s_first_error_ino = ino;
  573. sbi->s_first_error_block = block;
  574. sbi->s_first_error_func = func;
  575. sbi->s_first_error_time = sbi->s_last_error_time;
  576. }
  577. spin_unlock(&sbi->s_error_lock);
  578. }
  579. /* Deal with the reporting of failure conditions on a filesystem such as
  580. * inconsistencies detected or read IO failures.
  581. *
  582. * On ext2, we can store the error state of the filesystem in the
  583. * superblock. That is not possible on ext4, because we may have other
  584. * write ordering constraints on the superblock which prevent us from
  585. * writing it out straight away; and given that the journal is about to
  586. * be aborted, we can't rely on the current, or future, transactions to
  587. * write out the superblock safely.
  588. *
  589. * We'll just use the jbd2_journal_abort() error code to record an error in
  590. * the journal instead. On recovery, the journal will complain about
  591. * that error until we've noted it down and cleared it.
  592. *
  593. * If force_ro is set, we unconditionally force the filesystem into an
  594. * ABORT|READONLY state, unless the error response on the fs has been set to
  595. * panic in which case we take the easy way out and panic immediately. This is
  596. * used to deal with unrecoverable failures such as journal IO errors or ENOMEM
  597. * at a critical moment in log management.
  598. */
  599. static void ext4_handle_error(struct super_block *sb, bool force_ro, int error,
  600. __u32 ino, __u64 block,
  601. const char *func, unsigned int line)
  602. {
  603. journal_t *journal = EXT4_SB(sb)->s_journal;
  604. bool continue_fs = !force_ro && test_opt(sb, ERRORS_CONT);
  605. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  606. if (test_opt(sb, WARN_ON_ERROR))
  607. WARN_ON_ONCE(1);
  608. if (!continue_fs && !ext4_emergency_ro(sb) && journal)
  609. jbd2_journal_abort(journal, -error);
  610. if (!bdev_read_only(sb->s_bdev)) {
  611. save_error_info(sb, error, ino, block, func, line);
  612. /*
  613. * In case the fs should keep running, we need to writeout
  614. * superblock through the journal. Due to lock ordering
  615. * constraints, it may not be safe to do it right here so we
  616. * defer superblock flushing to a workqueue. We just need to be
  617. * careful when the journal is already shutting down. If we get
  618. * here in that case, just update the sb directly as the last
  619. * transaction won't commit anyway.
  620. */
  621. if (continue_fs && journal &&
  622. !ext4_test_mount_flag(sb, EXT4_MF_JOURNAL_DESTROY))
  623. schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
  624. else
  625. ext4_commit_super(sb);
  626. }
  627. /*
  628. * We force ERRORS_RO behavior when system is rebooting. Otherwise we
  629. * could panic during 'reboot -f' as the underlying device got already
  630. * disabled.
  631. */
  632. if (test_opt(sb, ERRORS_PANIC) && !system_going_down()) {
  633. panic("EXT4-fs (device %s): panic forced after error\n",
  634. sb->s_id);
  635. }
  636. if (ext4_emergency_ro(sb) || continue_fs)
  637. return;
  638. ext4_msg(sb, KERN_CRIT, "Remounting filesystem read-only");
  639. /*
  640. * We don't set SB_RDONLY because that requires sb->s_umount
  641. * semaphore and setting it without proper remount procedure is
  642. * confusing code such as freeze_super() leading to deadlocks
  643. * and other problems.
  644. */
  645. set_bit(EXT4_FLAGS_EMERGENCY_RO, &EXT4_SB(sb)->s_ext4_flags);
  646. }
  647. static void update_super_work(struct work_struct *work)
  648. {
  649. struct ext4_sb_info *sbi = container_of(work, struct ext4_sb_info,
  650. s_sb_upd_work);
  651. journal_t *journal = sbi->s_journal;
  652. handle_t *handle;
  653. /*
  654. * If the journal is still running, we have to write out superblock
  655. * through the journal to avoid collisions of other journalled sb
  656. * updates.
  657. *
  658. * We use directly jbd2 functions here to avoid recursing back into
  659. * ext4 error handling code during handling of previous errors.
  660. */
  661. if (!ext4_emergency_state(sbi->s_sb) &&
  662. !sb_rdonly(sbi->s_sb) && journal) {
  663. struct buffer_head *sbh = sbi->s_sbh;
  664. bool call_notify_err = false;
  665. handle = jbd2_journal_start(journal, 1);
  666. if (IS_ERR(handle))
  667. goto write_directly;
  668. if (jbd2_journal_get_write_access(handle, sbh)) {
  669. jbd2_journal_stop(handle);
  670. goto write_directly;
  671. }
  672. if (sbi->s_add_error_count > 0)
  673. call_notify_err = true;
  674. ext4_update_super(sbi->s_sb);
  675. if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
  676. ext4_msg(sbi->s_sb, KERN_ERR, "previous I/O error to "
  677. "superblock detected");
  678. clear_buffer_write_io_error(sbh);
  679. set_buffer_uptodate(sbh);
  680. }
  681. if (jbd2_journal_dirty_metadata(handle, sbh)) {
  682. jbd2_journal_stop(handle);
  683. goto write_directly;
  684. }
  685. jbd2_journal_stop(handle);
  686. if (call_notify_err)
  687. ext4_notify_error_sysfs(sbi);
  688. return;
  689. }
  690. write_directly:
  691. /*
  692. * Write through journal failed. Write sb directly to get error info
  693. * out and hope for the best.
  694. */
  695. ext4_commit_super(sbi->s_sb);
  696. ext4_notify_error_sysfs(sbi);
  697. }
  698. #define ext4_error_ratelimit(sb) \
  699. ___ratelimit(&(EXT4_SB(sb)->s_err_ratelimit_state), \
  700. "EXT4-fs error")
  701. void __ext4_error(struct super_block *sb, const char *function,
  702. unsigned int line, bool force_ro, int error, __u64 block,
  703. const char *fmt, ...)
  704. {
  705. struct va_format vaf;
  706. va_list args;
  707. if (unlikely(ext4_emergency_state(sb)))
  708. return;
  709. trace_ext4_error(sb, function, line);
  710. if (ext4_error_ratelimit(sb)) {
  711. va_start(args, fmt);
  712. vaf.fmt = fmt;
  713. vaf.va = &args;
  714. printk(KERN_CRIT
  715. "EXT4-fs error (device %s): %s:%d: comm %s: %pV\n",
  716. sb->s_id, function, line, current->comm, &vaf);
  717. va_end(args);
  718. }
  719. fserror_report_metadata(sb, error ? -abs(error) : -EFSCORRUPTED,
  720. GFP_ATOMIC);
  721. ext4_handle_error(sb, force_ro, error, 0, block, function, line);
  722. }
  723. void __ext4_error_inode(struct inode *inode, const char *function,
  724. unsigned int line, ext4_fsblk_t block, int error,
  725. const char *fmt, ...)
  726. {
  727. va_list args;
  728. struct va_format vaf;
  729. if (unlikely(ext4_emergency_state(inode->i_sb)))
  730. return;
  731. trace_ext4_error(inode->i_sb, function, line);
  732. if (ext4_error_ratelimit(inode->i_sb)) {
  733. va_start(args, fmt);
  734. vaf.fmt = fmt;
  735. vaf.va = &args;
  736. if (block)
  737. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
  738. "inode #%lu: block %llu: comm %s: %pV\n",
  739. inode->i_sb->s_id, function, line, inode->i_ino,
  740. block, current->comm, &vaf);
  741. else
  742. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: "
  743. "inode #%lu: comm %s: %pV\n",
  744. inode->i_sb->s_id, function, line, inode->i_ino,
  745. current->comm, &vaf);
  746. va_end(args);
  747. }
  748. fserror_report_file_metadata(inode,
  749. error ? -abs(error) : -EFSCORRUPTED,
  750. GFP_ATOMIC);
  751. ext4_handle_error(inode->i_sb, false, error, inode->i_ino, block,
  752. function, line);
  753. }
  754. void __ext4_error_file(struct file *file, const char *function,
  755. unsigned int line, ext4_fsblk_t block,
  756. const char *fmt, ...)
  757. {
  758. va_list args;
  759. struct va_format vaf;
  760. struct inode *inode = file_inode(file);
  761. char pathname[80], *path;
  762. if (unlikely(ext4_emergency_state(inode->i_sb)))
  763. return;
  764. trace_ext4_error(inode->i_sb, function, line);
  765. if (ext4_error_ratelimit(inode->i_sb)) {
  766. path = file_path(file, pathname, sizeof(pathname));
  767. if (IS_ERR(path))
  768. path = "(unknown)";
  769. va_start(args, fmt);
  770. vaf.fmt = fmt;
  771. vaf.va = &args;
  772. if (block)
  773. printk(KERN_CRIT
  774. "EXT4-fs error (device %s): %s:%d: inode #%lu: "
  775. "block %llu: comm %s: path %s: %pV\n",
  776. inode->i_sb->s_id, function, line, inode->i_ino,
  777. block, current->comm, path, &vaf);
  778. else
  779. printk(KERN_CRIT
  780. "EXT4-fs error (device %s): %s:%d: inode #%lu: "
  781. "comm %s: path %s: %pV\n",
  782. inode->i_sb->s_id, function, line, inode->i_ino,
  783. current->comm, path, &vaf);
  784. va_end(args);
  785. }
  786. fserror_report_file_metadata(inode, -EFSCORRUPTED, GFP_ATOMIC);
  787. ext4_handle_error(inode->i_sb, false, EFSCORRUPTED, inode->i_ino, block,
  788. function, line);
  789. }
  790. const char *ext4_decode_error(struct super_block *sb, int errno,
  791. char nbuf[16])
  792. {
  793. char *errstr = NULL;
  794. switch (errno) {
  795. case -EFSCORRUPTED:
  796. errstr = "Corrupt filesystem";
  797. break;
  798. case -EFSBADCRC:
  799. errstr = "Filesystem failed CRC";
  800. break;
  801. case -EIO:
  802. errstr = "IO failure";
  803. break;
  804. case -ENOMEM:
  805. errstr = "Out of memory";
  806. break;
  807. case -EROFS:
  808. if (!sb || (EXT4_SB(sb)->s_journal &&
  809. EXT4_SB(sb)->s_journal->j_flags & JBD2_ABORT))
  810. errstr = "Journal has aborted";
  811. else
  812. errstr = "Readonly filesystem";
  813. break;
  814. default:
  815. /* If the caller passed in an extra buffer for unknown
  816. * errors, textualise them now. Else we just return
  817. * NULL. */
  818. if (nbuf) {
  819. /* Check for truncated error codes... */
  820. if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
  821. errstr = nbuf;
  822. }
  823. break;
  824. }
  825. return errstr;
  826. }
  827. /* __ext4_std_error decodes expected errors from journaling functions
  828. * automatically and invokes the appropriate error response. */
  829. void __ext4_std_error(struct super_block *sb, const char *function,
  830. unsigned int line, int errno)
  831. {
  832. char nbuf[16];
  833. const char *errstr;
  834. if (unlikely(ext4_emergency_state(sb)))
  835. return;
  836. /* Special case: if the error is EROFS, and we're not already
  837. * inside a transaction, then there's really no point in logging
  838. * an error. */
  839. if (errno == -EROFS && journal_current_handle() == NULL && sb_rdonly(sb))
  840. return;
  841. if (ext4_error_ratelimit(sb)) {
  842. errstr = ext4_decode_error(sb, errno, nbuf);
  843. printk(KERN_CRIT "EXT4-fs error (device %s) in %s:%d: %s\n",
  844. sb->s_id, function, line, errstr);
  845. }
  846. fserror_report_metadata(sb, errno ? -abs(errno) : -EFSCORRUPTED,
  847. GFP_ATOMIC);
  848. ext4_handle_error(sb, false, -errno, 0, 0, function, line);
  849. }
  850. void __ext4_msg(struct super_block *sb,
  851. const char *prefix, const char *fmt, ...)
  852. {
  853. struct va_format vaf;
  854. va_list args;
  855. if (sb) {
  856. atomic_inc(&EXT4_SB(sb)->s_msg_count);
  857. if (!___ratelimit(&(EXT4_SB(sb)->s_msg_ratelimit_state),
  858. "EXT4-fs"))
  859. return;
  860. }
  861. va_start(args, fmt);
  862. vaf.fmt = fmt;
  863. vaf.va = &args;
  864. if (sb)
  865. printk("%sEXT4-fs (%s): %pV\n", prefix, sb->s_id, &vaf);
  866. else
  867. printk("%sEXT4-fs: %pV\n", prefix, &vaf);
  868. va_end(args);
  869. }
  870. static int ext4_warning_ratelimit(struct super_block *sb)
  871. {
  872. atomic_inc(&EXT4_SB(sb)->s_warning_count);
  873. return ___ratelimit(&(EXT4_SB(sb)->s_warning_ratelimit_state),
  874. "EXT4-fs warning");
  875. }
  876. void __ext4_warning(struct super_block *sb, const char *function,
  877. unsigned int line, const char *fmt, ...)
  878. {
  879. struct va_format vaf;
  880. va_list args;
  881. if (!ext4_warning_ratelimit(sb))
  882. return;
  883. va_start(args, fmt);
  884. vaf.fmt = fmt;
  885. vaf.va = &args;
  886. printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: %pV\n",
  887. sb->s_id, function, line, &vaf);
  888. va_end(args);
  889. }
  890. void __ext4_warning_inode(const struct inode *inode, const char *function,
  891. unsigned int line, const char *fmt, ...)
  892. {
  893. struct va_format vaf;
  894. va_list args;
  895. if (!ext4_warning_ratelimit(inode->i_sb))
  896. return;
  897. va_start(args, fmt);
  898. vaf.fmt = fmt;
  899. vaf.va = &args;
  900. printk(KERN_WARNING "EXT4-fs warning (device %s): %s:%d: "
  901. "inode #%lu: comm %s: %pV\n", inode->i_sb->s_id,
  902. function, line, inode->i_ino, current->comm, &vaf);
  903. va_end(args);
  904. }
  905. void __ext4_grp_locked_error(const char *function, unsigned int line,
  906. struct super_block *sb, ext4_group_t grp,
  907. unsigned long ino, ext4_fsblk_t block,
  908. const char *fmt, ...)
  909. __releases(bitlock)
  910. __acquires(bitlock)
  911. {
  912. struct va_format vaf;
  913. va_list args;
  914. if (unlikely(ext4_emergency_state(sb)))
  915. return;
  916. trace_ext4_error(sb, function, line);
  917. if (ext4_error_ratelimit(sb)) {
  918. va_start(args, fmt);
  919. vaf.fmt = fmt;
  920. vaf.va = &args;
  921. printk(KERN_CRIT "EXT4-fs error (device %s): %s:%d: group %u, ",
  922. sb->s_id, function, line, grp);
  923. if (ino)
  924. printk(KERN_CONT "inode %lu: ", ino);
  925. if (block)
  926. printk(KERN_CONT "block %llu:",
  927. (unsigned long long) block);
  928. printk(KERN_CONT "%pV\n", &vaf);
  929. va_end(args);
  930. }
  931. if (test_opt(sb, ERRORS_CONT)) {
  932. if (test_opt(sb, WARN_ON_ERROR))
  933. WARN_ON_ONCE(1);
  934. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  935. if (!bdev_read_only(sb->s_bdev)) {
  936. save_error_info(sb, EFSCORRUPTED, ino, block, function,
  937. line);
  938. schedule_work(&EXT4_SB(sb)->s_sb_upd_work);
  939. }
  940. return;
  941. }
  942. ext4_unlock_group(sb, grp);
  943. ext4_handle_error(sb, false, EFSCORRUPTED, ino, block, function, line);
  944. /*
  945. * We only get here in the ERRORS_RO case; relocking the group
  946. * may be dangerous, but nothing bad will happen since the
  947. * filesystem will have already been marked read/only and the
  948. * journal has been aborted. We return 1 as a hint to callers
  949. * who might what to use the return value from
  950. * ext4_grp_locked_error() to distinguish between the
  951. * ERRORS_CONT and ERRORS_RO case, and perhaps return more
  952. * aggressively from the ext4 function in question, with a
  953. * more appropriate error code.
  954. */
  955. ext4_lock_group(sb, grp);
  956. return;
  957. }
  958. void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
  959. ext4_group_t group,
  960. unsigned int flags)
  961. {
  962. struct ext4_sb_info *sbi = EXT4_SB(sb);
  963. struct ext4_group_info *grp = ext4_get_group_info(sb, group);
  964. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, group, NULL);
  965. int ret;
  966. if (!grp || !gdp)
  967. return;
  968. if (flags & EXT4_GROUP_INFO_BBITMAP_CORRUPT) {
  969. ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT,
  970. &grp->bb_state);
  971. if (!ret)
  972. percpu_counter_sub(&sbi->s_freeclusters_counter,
  973. grp->bb_free);
  974. }
  975. if (flags & EXT4_GROUP_INFO_IBITMAP_CORRUPT) {
  976. ret = ext4_test_and_set_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT,
  977. &grp->bb_state);
  978. if (!ret && gdp) {
  979. int count;
  980. count = ext4_free_inodes_count(sb, gdp);
  981. percpu_counter_sub(&sbi->s_freeinodes_counter,
  982. count);
  983. }
  984. }
  985. }
  986. void ext4_update_dynamic_rev(struct super_block *sb)
  987. {
  988. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  989. if (le32_to_cpu(es->s_rev_level) > EXT4_GOOD_OLD_REV)
  990. return;
  991. ext4_warning(sb,
  992. "updating to rev %d because of new feature flag, "
  993. "running e2fsck is recommended",
  994. EXT4_DYNAMIC_REV);
  995. es->s_first_ino = cpu_to_le32(EXT4_GOOD_OLD_FIRST_INO);
  996. es->s_inode_size = cpu_to_le16(EXT4_GOOD_OLD_INODE_SIZE);
  997. es->s_rev_level = cpu_to_le32(EXT4_DYNAMIC_REV);
  998. /* leave es->s_feature_*compat flags alone */
  999. /* es->s_uuid will be set by e2fsck if empty */
  1000. /*
  1001. * The rest of the superblock fields should be zero, and if not it
  1002. * means they are likely already in use, so leave them alone. We
  1003. * can leave it up to e2fsck to clean up any inconsistencies there.
  1004. */
  1005. }
  1006. static inline struct inode *orphan_list_entry(struct list_head *l)
  1007. {
  1008. return &list_entry(l, struct ext4_inode_info, i_orphan)->vfs_inode;
  1009. }
  1010. static void dump_orphan_list(struct super_block *sb, struct ext4_sb_info *sbi)
  1011. {
  1012. struct list_head *l;
  1013. ext4_msg(sb, KERN_ERR, "sb orphan head is %d",
  1014. le32_to_cpu(sbi->s_es->s_last_orphan));
  1015. printk(KERN_ERR "sb_info orphan list:\n");
  1016. list_for_each(l, &sbi->s_orphan) {
  1017. struct inode *inode = orphan_list_entry(l);
  1018. printk(KERN_ERR " "
  1019. "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
  1020. inode->i_sb->s_id, inode->i_ino, inode,
  1021. inode->i_mode, inode->i_nlink,
  1022. NEXT_ORPHAN(inode));
  1023. }
  1024. }
  1025. #ifdef CONFIG_QUOTA
  1026. static int ext4_quota_off(struct super_block *sb, int type);
  1027. static inline void ext4_quotas_off(struct super_block *sb, int type)
  1028. {
  1029. BUG_ON(type > EXT4_MAXQUOTAS);
  1030. /* Use our quota_off function to clear inode flags etc. */
  1031. for (type--; type >= 0; type--)
  1032. ext4_quota_off(sb, type);
  1033. }
  1034. /*
  1035. * This is a helper function which is used in the mount/remount
  1036. * codepaths (which holds s_umount) to fetch the quota file name.
  1037. */
  1038. static inline char *get_qf_name(struct super_block *sb,
  1039. struct ext4_sb_info *sbi,
  1040. int type)
  1041. {
  1042. return rcu_dereference_protected(sbi->s_qf_names[type],
  1043. lockdep_is_held(&sb->s_umount));
  1044. }
  1045. #else
  1046. static inline void ext4_quotas_off(struct super_block *sb, int type)
  1047. {
  1048. }
  1049. #endif
  1050. static int ext4_percpu_param_init(struct ext4_sb_info *sbi)
  1051. {
  1052. ext4_fsblk_t block;
  1053. int err;
  1054. block = ext4_count_free_clusters(sbi->s_sb);
  1055. ext4_free_blocks_count_set(sbi->s_es, EXT4_C2B(sbi, block));
  1056. err = percpu_counter_init(&sbi->s_freeclusters_counter, block,
  1057. GFP_KERNEL);
  1058. if (!err) {
  1059. unsigned long freei = ext4_count_free_inodes(sbi->s_sb);
  1060. sbi->s_es->s_free_inodes_count = cpu_to_le32(freei);
  1061. err = percpu_counter_init(&sbi->s_freeinodes_counter, freei,
  1062. GFP_KERNEL);
  1063. }
  1064. if (!err)
  1065. err = percpu_counter_init(&sbi->s_dirs_counter,
  1066. ext4_count_dirs(sbi->s_sb), GFP_KERNEL);
  1067. if (!err)
  1068. err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0,
  1069. GFP_KERNEL);
  1070. if (!err)
  1071. err = percpu_counter_init(&sbi->s_sra_exceeded_retry_limit, 0,
  1072. GFP_KERNEL);
  1073. if (!err)
  1074. err = percpu_init_rwsem(&sbi->s_writepages_rwsem);
  1075. if (err)
  1076. ext4_msg(sbi->s_sb, KERN_ERR, "insufficient memory");
  1077. return err;
  1078. }
  1079. static void ext4_percpu_param_destroy(struct ext4_sb_info *sbi)
  1080. {
  1081. percpu_counter_destroy(&sbi->s_freeclusters_counter);
  1082. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  1083. percpu_counter_destroy(&sbi->s_dirs_counter);
  1084. percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
  1085. percpu_counter_destroy(&sbi->s_sra_exceeded_retry_limit);
  1086. percpu_free_rwsem(&sbi->s_writepages_rwsem);
  1087. }
  1088. static void ext4_group_desc_free(struct ext4_sb_info *sbi)
  1089. {
  1090. struct buffer_head **group_desc;
  1091. int i;
  1092. group_desc = rcu_access_pointer(sbi->s_group_desc);
  1093. for (i = 0; i < sbi->s_gdb_count; i++)
  1094. brelse(group_desc[i]);
  1095. kvfree(group_desc);
  1096. }
  1097. static void ext4_flex_groups_free(struct ext4_sb_info *sbi)
  1098. {
  1099. struct flex_groups **flex_groups;
  1100. int i;
  1101. flex_groups = rcu_access_pointer(sbi->s_flex_groups);
  1102. if (flex_groups) {
  1103. for (i = 0; i < sbi->s_flex_groups_allocated; i++)
  1104. kvfree(flex_groups[i]);
  1105. kvfree(flex_groups);
  1106. }
  1107. }
  1108. static void ext4_put_super(struct super_block *sb)
  1109. {
  1110. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1111. struct ext4_super_block *es = sbi->s_es;
  1112. int aborted = 0;
  1113. int err;
  1114. /*
  1115. * Unregister sysfs before destroying jbd2 journal.
  1116. * Since we could still access attr_journal_task attribute via sysfs
  1117. * path which could have sbi->s_journal->j_task as NULL
  1118. * Unregister sysfs before flush sbi->s_sb_upd_work.
  1119. * Since user may read /proc/fs/ext4/xx/mb_groups during umount, If
  1120. * read metadata verify failed then will queue error work.
  1121. * update_super_work will call start_this_handle may trigger
  1122. * BUG_ON.
  1123. */
  1124. ext4_unregister_sysfs(sb);
  1125. if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs unmount"))
  1126. ext4_msg(sb, KERN_INFO, "unmounting filesystem %pU.",
  1127. &sb->s_uuid);
  1128. ext4_unregister_li_request(sb);
  1129. ext4_quotas_off(sb, EXT4_MAXQUOTAS);
  1130. destroy_workqueue(sbi->rsv_conversion_wq);
  1131. ext4_release_orphan_info(sb);
  1132. if (sbi->s_journal) {
  1133. aborted = is_journal_aborted(sbi->s_journal);
  1134. err = ext4_journal_destroy(sbi, sbi->s_journal);
  1135. if ((err < 0) && !aborted) {
  1136. ext4_abort(sb, -err, "Couldn't clean up the journal");
  1137. }
  1138. } else
  1139. flush_work(&sbi->s_sb_upd_work);
  1140. ext4_es_unregister_shrinker(sbi);
  1141. timer_shutdown_sync(&sbi->s_err_report);
  1142. ext4_release_system_zone(sb);
  1143. ext4_mb_release(sb);
  1144. ext4_ext_release(sb);
  1145. if (!ext4_emergency_state(sb) && !sb_rdonly(sb)) {
  1146. if (!aborted) {
  1147. ext4_clear_feature_journal_needs_recovery(sb);
  1148. ext4_clear_feature_orphan_present(sb);
  1149. es->s_state = cpu_to_le16(sbi->s_mount_state);
  1150. }
  1151. ext4_commit_super(sb);
  1152. }
  1153. ext4_group_desc_free(sbi);
  1154. ext4_flex_groups_free(sbi);
  1155. WARN_ON_ONCE(!(sbi->s_mount_state & EXT4_ERROR_FS) &&
  1156. percpu_counter_sum(&sbi->s_dirtyclusters_counter));
  1157. ext4_percpu_param_destroy(sbi);
  1158. #ifdef CONFIG_QUOTA
  1159. for (int i = 0; i < EXT4_MAXQUOTAS; i++)
  1160. kfree(get_qf_name(sb, sbi, i));
  1161. #endif
  1162. /* Debugging code just in case the in-memory inode orphan list
  1163. * isn't empty. The on-disk one can be non-empty if we've
  1164. * detected an error and taken the fs readonly, but the
  1165. * in-memory list had better be clean by this point. */
  1166. if (!list_empty(&sbi->s_orphan))
  1167. dump_orphan_list(sb, sbi);
  1168. ASSERT(list_empty(&sbi->s_orphan));
  1169. sync_blockdev(sb->s_bdev);
  1170. invalidate_bdev(sb->s_bdev);
  1171. if (sbi->s_journal_bdev_file) {
  1172. /*
  1173. * Invalidate the journal device's buffers. We don't want them
  1174. * floating about in memory - the physical journal device may
  1175. * hotswapped, and it breaks the `ro-after' testing code.
  1176. */
  1177. sync_blockdev(file_bdev(sbi->s_journal_bdev_file));
  1178. invalidate_bdev(file_bdev(sbi->s_journal_bdev_file));
  1179. }
  1180. ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
  1181. sbi->s_ea_inode_cache = NULL;
  1182. ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
  1183. sbi->s_ea_block_cache = NULL;
  1184. ext4_stop_mmpd(sbi);
  1185. brelse(sbi->s_sbh);
  1186. sb->s_fs_info = NULL;
  1187. /*
  1188. * Now that we are completely done shutting down the
  1189. * superblock, we need to actually destroy the kobject.
  1190. */
  1191. kobject_put(&sbi->s_kobj);
  1192. wait_for_completion(&sbi->s_kobj_unregister);
  1193. kfree(sbi->s_blockgroup_lock);
  1194. fs_put_dax(sbi->s_daxdev, NULL);
  1195. fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
  1196. #if IS_ENABLED(CONFIG_UNICODE)
  1197. utf8_unload(sb->s_encoding);
  1198. #endif
  1199. kfree(sbi);
  1200. }
  1201. static struct kmem_cache *ext4_inode_cachep;
  1202. /*
  1203. * Called inside transaction, so use GFP_NOFS
  1204. */
  1205. static struct inode *ext4_alloc_inode(struct super_block *sb)
  1206. {
  1207. struct ext4_inode_info *ei;
  1208. ei = alloc_inode_sb(sb, ext4_inode_cachep, GFP_NOFS);
  1209. if (!ei)
  1210. return NULL;
  1211. inode_set_iversion(&ei->vfs_inode, 1);
  1212. ei->i_flags = 0;
  1213. ext4_clear_state_flags(ei); /* Only relevant on 32-bit archs */
  1214. spin_lock_init(&ei->i_raw_lock);
  1215. ei->i_prealloc_node = RB_ROOT;
  1216. atomic_set(&ei->i_prealloc_active, 0);
  1217. rwlock_init(&ei->i_prealloc_lock);
  1218. ext4_es_init_tree(&ei->i_es_tree);
  1219. rwlock_init(&ei->i_es_lock);
  1220. INIT_LIST_HEAD(&ei->i_es_list);
  1221. ei->i_es_all_nr = 0;
  1222. ei->i_es_shk_nr = 0;
  1223. ei->i_es_shrink_lblk = 0;
  1224. ei->i_es_seq = 0;
  1225. ei->i_reserved_data_blocks = 0;
  1226. spin_lock_init(&(ei->i_block_reservation_lock));
  1227. ext4_init_pending_tree(&ei->i_pending_tree);
  1228. #ifdef CONFIG_QUOTA
  1229. ei->i_reserved_quota = 0;
  1230. memset(&ei->i_dquot, 0, sizeof(ei->i_dquot));
  1231. #endif
  1232. ei->jinode = NULL;
  1233. INIT_LIST_HEAD(&ei->i_rsv_conversion_list);
  1234. spin_lock_init(&ei->i_completed_io_lock);
  1235. ei->i_sync_tid = 0;
  1236. ei->i_datasync_tid = 0;
  1237. INIT_WORK(&ei->i_rsv_conversion_work, ext4_end_io_rsv_work);
  1238. ext4_fc_init_inode(&ei->vfs_inode);
  1239. spin_lock_init(&ei->i_fc_lock);
  1240. return &ei->vfs_inode;
  1241. }
  1242. static int ext4_drop_inode(struct inode *inode)
  1243. {
  1244. int drop = inode_generic_drop(inode);
  1245. if (!drop)
  1246. drop = fscrypt_drop_inode(inode);
  1247. trace_ext4_drop_inode(inode, drop);
  1248. return drop;
  1249. }
  1250. static void ext4_free_in_core_inode(struct inode *inode)
  1251. {
  1252. fscrypt_free_inode(inode);
  1253. if (!list_empty(&(EXT4_I(inode)->i_fc_list))) {
  1254. pr_warn("%s: inode %ld still in fc list",
  1255. __func__, inode->i_ino);
  1256. }
  1257. kmem_cache_free(ext4_inode_cachep, EXT4_I(inode));
  1258. }
  1259. static void ext4_destroy_inode(struct inode *inode)
  1260. {
  1261. if (ext4_inode_orphan_tracked(inode)) {
  1262. ext4_msg(inode->i_sb, KERN_ERR,
  1263. "Inode %lu (%p): inode tracked as orphan!",
  1264. inode->i_ino, EXT4_I(inode));
  1265. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
  1266. EXT4_I(inode), sizeof(struct ext4_inode_info),
  1267. true);
  1268. dump_stack();
  1269. }
  1270. if (!(EXT4_SB(inode->i_sb)->s_mount_state & EXT4_ERROR_FS) &&
  1271. WARN_ON_ONCE(EXT4_I(inode)->i_reserved_data_blocks))
  1272. ext4_msg(inode->i_sb, KERN_ERR,
  1273. "Inode %lu (%p): i_reserved_data_blocks (%u) not cleared!",
  1274. inode->i_ino, EXT4_I(inode),
  1275. EXT4_I(inode)->i_reserved_data_blocks);
  1276. }
  1277. static void ext4_shutdown(struct super_block *sb)
  1278. {
  1279. ext4_force_shutdown(sb, EXT4_GOING_FLAGS_NOLOGFLUSH);
  1280. }
  1281. static void init_once(void *foo)
  1282. {
  1283. struct ext4_inode_info *ei = foo;
  1284. INIT_LIST_HEAD(&ei->i_orphan);
  1285. init_rwsem(&ei->xattr_sem);
  1286. init_rwsem(&ei->i_data_sem);
  1287. inode_init_once(&ei->vfs_inode);
  1288. ext4_fc_init_inode(&ei->vfs_inode);
  1289. #ifdef CONFIG_FS_ENCRYPTION
  1290. ei->i_crypt_info = NULL;
  1291. #endif
  1292. }
  1293. static int __init init_inodecache(void)
  1294. {
  1295. struct kmem_cache_args args = {
  1296. .useroffset = offsetof(struct ext4_inode_info, i_data),
  1297. .usersize = sizeof_field(struct ext4_inode_info, i_data),
  1298. .use_freeptr_offset = true,
  1299. .freeptr_offset = offsetof(struct ext4_inode_info, i_flags),
  1300. .ctor = init_once,
  1301. };
  1302. ext4_inode_cachep = kmem_cache_create("ext4_inode_cache",
  1303. sizeof(struct ext4_inode_info),
  1304. &args,
  1305. SLAB_RECLAIM_ACCOUNT | SLAB_ACCOUNT);
  1306. if (ext4_inode_cachep == NULL)
  1307. return -ENOMEM;
  1308. return 0;
  1309. }
  1310. static void destroy_inodecache(void)
  1311. {
  1312. /*
  1313. * Make sure all delayed rcu free inodes are flushed before we
  1314. * destroy cache.
  1315. */
  1316. rcu_barrier();
  1317. kmem_cache_destroy(ext4_inode_cachep);
  1318. }
  1319. void ext4_clear_inode(struct inode *inode)
  1320. {
  1321. ext4_fc_del(inode);
  1322. invalidate_inode_buffers(inode);
  1323. clear_inode(inode);
  1324. ext4_discard_preallocations(inode);
  1325. /*
  1326. * We must remove the inode from the hash before ext4_free_inode()
  1327. * clears the bit in inode bitmap as otherwise another process reusing
  1328. * the inode will block in insert_inode_hash() waiting for inode
  1329. * eviction to complete while holding transaction handle open, but
  1330. * ext4_evict_inode() still running for that inode could block waiting
  1331. * for transaction commit if the inode is marked as IS_SYNC => deadlock.
  1332. *
  1333. * Removing the inode from the hash here is safe. There are two cases
  1334. * to consider:
  1335. * 1) The inode still has references to it (i_nlink > 0). In that case
  1336. * we are keeping the inode and once we remove the inode from the hash,
  1337. * iget() can create the new inode structure for the same inode number
  1338. * and we are fine with that as all IO on behalf of the inode is
  1339. * finished.
  1340. * 2) We are deleting the inode (i_nlink == 0). In that case inode
  1341. * number cannot be reused until ext4_free_inode() clears the bit in
  1342. * the inode bitmap, at which point all IO is done and reuse is fine
  1343. * again.
  1344. */
  1345. remove_inode_hash(inode);
  1346. ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
  1347. dquot_drop(inode);
  1348. if (EXT4_I(inode)->jinode) {
  1349. jbd2_journal_release_jbd_inode(EXT4_JOURNAL(inode),
  1350. EXT4_I(inode)->jinode);
  1351. jbd2_free_inode(EXT4_I(inode)->jinode);
  1352. EXT4_I(inode)->jinode = NULL;
  1353. }
  1354. fscrypt_put_encryption_info(inode);
  1355. }
  1356. static struct inode *ext4_nfs_get_inode(struct super_block *sb,
  1357. u64 ino, u32 generation)
  1358. {
  1359. struct inode *inode;
  1360. /*
  1361. * Currently we don't know the generation for parent directory, so
  1362. * a generation of 0 means "accept any"
  1363. */
  1364. inode = ext4_iget(sb, ino, EXT4_IGET_HANDLE);
  1365. if (IS_ERR(inode))
  1366. return ERR_CAST(inode);
  1367. if (generation && inode->i_generation != generation) {
  1368. iput(inode);
  1369. return ERR_PTR(-ESTALE);
  1370. }
  1371. return inode;
  1372. }
  1373. static struct dentry *ext4_fh_to_dentry(struct super_block *sb, struct fid *fid,
  1374. int fh_len, int fh_type)
  1375. {
  1376. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  1377. ext4_nfs_get_inode);
  1378. }
  1379. static struct dentry *ext4_fh_to_parent(struct super_block *sb, struct fid *fid,
  1380. int fh_len, int fh_type)
  1381. {
  1382. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  1383. ext4_nfs_get_inode);
  1384. }
  1385. static int ext4_nfs_commit_metadata(struct inode *inode)
  1386. {
  1387. struct writeback_control wbc = {
  1388. .sync_mode = WB_SYNC_ALL
  1389. };
  1390. trace_ext4_nfs_commit_metadata(inode);
  1391. return ext4_write_inode(inode, &wbc);
  1392. }
  1393. #ifdef CONFIG_QUOTA
  1394. static const char * const quotatypes[] = INITQFNAMES;
  1395. #define QTYPE2NAME(t) (quotatypes[t])
  1396. static int ext4_write_dquot(struct dquot *dquot);
  1397. static int ext4_acquire_dquot(struct dquot *dquot);
  1398. static int ext4_release_dquot(struct dquot *dquot);
  1399. static int ext4_mark_dquot_dirty(struct dquot *dquot);
  1400. static int ext4_write_info(struct super_block *sb, int type);
  1401. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  1402. const struct path *path);
  1403. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  1404. size_t len, loff_t off);
  1405. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  1406. const char *data, size_t len, loff_t off);
  1407. static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
  1408. unsigned int flags);
  1409. static struct dquot __rcu **ext4_get_dquots(struct inode *inode)
  1410. {
  1411. return EXT4_I(inode)->i_dquot;
  1412. }
  1413. static const struct dquot_operations ext4_quota_operations = {
  1414. .get_reserved_space = ext4_get_reserved_space,
  1415. .write_dquot = ext4_write_dquot,
  1416. .acquire_dquot = ext4_acquire_dquot,
  1417. .release_dquot = ext4_release_dquot,
  1418. .mark_dirty = ext4_mark_dquot_dirty,
  1419. .write_info = ext4_write_info,
  1420. .alloc_dquot = dquot_alloc,
  1421. .destroy_dquot = dquot_destroy,
  1422. .get_projid = ext4_get_projid,
  1423. .get_inode_usage = ext4_get_inode_usage,
  1424. .get_next_id = dquot_get_next_id,
  1425. };
  1426. static const struct quotactl_ops ext4_qctl_operations = {
  1427. .quota_on = ext4_quota_on,
  1428. .quota_off = ext4_quota_off,
  1429. .quota_sync = dquot_quota_sync,
  1430. .get_state = dquot_get_state,
  1431. .set_info = dquot_set_dqinfo,
  1432. .get_dqblk = dquot_get_dqblk,
  1433. .set_dqblk = dquot_set_dqblk,
  1434. .get_nextdqblk = dquot_get_next_dqblk,
  1435. };
  1436. #endif
  1437. static const struct super_operations ext4_sops = {
  1438. .alloc_inode = ext4_alloc_inode,
  1439. .free_inode = ext4_free_in_core_inode,
  1440. .destroy_inode = ext4_destroy_inode,
  1441. .write_inode = ext4_write_inode,
  1442. .dirty_inode = ext4_dirty_inode,
  1443. .drop_inode = ext4_drop_inode,
  1444. .evict_inode = ext4_evict_inode,
  1445. .put_super = ext4_put_super,
  1446. .sync_fs = ext4_sync_fs,
  1447. .freeze_fs = ext4_freeze,
  1448. .unfreeze_fs = ext4_unfreeze,
  1449. .statfs = ext4_statfs,
  1450. .show_options = ext4_show_options,
  1451. .shutdown = ext4_shutdown,
  1452. #ifdef CONFIG_QUOTA
  1453. .quota_read = ext4_quota_read,
  1454. .quota_write = ext4_quota_write,
  1455. .get_dquots = ext4_get_dquots,
  1456. #endif
  1457. };
  1458. static const struct export_operations ext4_export_ops = {
  1459. .encode_fh = generic_encode_ino32_fh,
  1460. .fh_to_dentry = ext4_fh_to_dentry,
  1461. .fh_to_parent = ext4_fh_to_parent,
  1462. .get_parent = ext4_get_parent,
  1463. .commit_metadata = ext4_nfs_commit_metadata,
  1464. };
  1465. enum {
  1466. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  1467. Opt_resgid, Opt_resuid, Opt_sb,
  1468. Opt_nouid32, Opt_debug, Opt_removed,
  1469. Opt_user_xattr, Opt_acl,
  1470. Opt_auto_da_alloc, Opt_noauto_da_alloc, Opt_noload,
  1471. Opt_commit, Opt_min_batch_time, Opt_max_batch_time, Opt_journal_dev,
  1472. Opt_journal_path, Opt_journal_checksum, Opt_journal_async_commit,
  1473. Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
  1474. Opt_data_err_abort, Opt_data_err_ignore, Opt_test_dummy_encryption,
  1475. Opt_inlinecrypt,
  1476. Opt_usrjquota, Opt_grpjquota, Opt_quota,
  1477. Opt_noquota, Opt_barrier, Opt_nobarrier, Opt_err,
  1478. Opt_usrquota, Opt_grpquota, Opt_prjquota,
  1479. Opt_dax, Opt_dax_always, Opt_dax_inode, Opt_dax_never,
  1480. Opt_stripe, Opt_delalloc, Opt_nodelalloc, Opt_warn_on_error,
  1481. Opt_nowarn_on_error, Opt_mblk_io_submit, Opt_debug_want_extra_isize,
  1482. Opt_nomblk_io_submit, Opt_block_validity, Opt_noblock_validity,
  1483. Opt_inode_readahead_blks, Opt_journal_ioprio,
  1484. Opt_dioread_nolock, Opt_dioread_lock,
  1485. Opt_discard, Opt_nodiscard, Opt_init_itable, Opt_noinit_itable,
  1486. Opt_max_dir_size_kb, Opt_nojournal_checksum, Opt_nombcache,
  1487. Opt_no_prefetch_block_bitmaps, Opt_mb_optimize_scan,
  1488. Opt_errors, Opt_data, Opt_data_err, Opt_jqfmt, Opt_dax_type,
  1489. #ifdef CONFIG_EXT4_DEBUG
  1490. Opt_fc_debug_max_replay, Opt_fc_debug_force
  1491. #endif
  1492. };
  1493. static const struct constant_table ext4_param_errors[] = {
  1494. {"continue", EXT4_MOUNT_ERRORS_CONT},
  1495. {"panic", EXT4_MOUNT_ERRORS_PANIC},
  1496. {"remount-ro", EXT4_MOUNT_ERRORS_RO},
  1497. {}
  1498. };
  1499. static const struct constant_table ext4_param_data[] = {
  1500. {"journal", EXT4_MOUNT_JOURNAL_DATA},
  1501. {"ordered", EXT4_MOUNT_ORDERED_DATA},
  1502. {"writeback", EXT4_MOUNT_WRITEBACK_DATA},
  1503. {}
  1504. };
  1505. static const struct constant_table ext4_param_data_err[] = {
  1506. {"abort", Opt_data_err_abort},
  1507. {"ignore", Opt_data_err_ignore},
  1508. {}
  1509. };
  1510. static const struct constant_table ext4_param_jqfmt[] = {
  1511. {"vfsold", QFMT_VFS_OLD},
  1512. {"vfsv0", QFMT_VFS_V0},
  1513. {"vfsv1", QFMT_VFS_V1},
  1514. {}
  1515. };
  1516. static const struct constant_table ext4_param_dax[] = {
  1517. {"always", Opt_dax_always},
  1518. {"inode", Opt_dax_inode},
  1519. {"never", Opt_dax_never},
  1520. {}
  1521. };
  1522. /*
  1523. * Mount option specification
  1524. * We don't use fsparam_flag_no because of the way we set the
  1525. * options and the way we show them in _ext4_show_options(). To
  1526. * keep the changes to a minimum, let's keep the negative options
  1527. * separate for now.
  1528. */
  1529. static const struct fs_parameter_spec ext4_param_specs[] = {
  1530. fsparam_flag ("bsddf", Opt_bsd_df),
  1531. fsparam_flag ("minixdf", Opt_minix_df),
  1532. fsparam_flag ("grpid", Opt_grpid),
  1533. fsparam_flag ("bsdgroups", Opt_grpid),
  1534. fsparam_flag ("nogrpid", Opt_nogrpid),
  1535. fsparam_flag ("sysvgroups", Opt_nogrpid),
  1536. fsparam_gid ("resgid", Opt_resgid),
  1537. fsparam_uid ("resuid", Opt_resuid),
  1538. fsparam_u32 ("sb", Opt_sb),
  1539. fsparam_enum ("errors", Opt_errors, ext4_param_errors),
  1540. fsparam_flag ("nouid32", Opt_nouid32),
  1541. fsparam_flag ("debug", Opt_debug),
  1542. fsparam_flag ("oldalloc", Opt_removed),
  1543. fsparam_flag ("orlov", Opt_removed),
  1544. fsparam_flag ("user_xattr", Opt_user_xattr),
  1545. fsparam_flag ("acl", Opt_acl),
  1546. fsparam_flag ("norecovery", Opt_noload),
  1547. fsparam_flag ("noload", Opt_noload),
  1548. fsparam_flag ("bh", Opt_removed),
  1549. fsparam_flag ("nobh", Opt_removed),
  1550. fsparam_u32 ("commit", Opt_commit),
  1551. fsparam_u32 ("min_batch_time", Opt_min_batch_time),
  1552. fsparam_u32 ("max_batch_time", Opt_max_batch_time),
  1553. fsparam_u32 ("journal_dev", Opt_journal_dev),
  1554. fsparam_bdev ("journal_path", Opt_journal_path),
  1555. fsparam_flag ("journal_checksum", Opt_journal_checksum),
  1556. fsparam_flag ("nojournal_checksum", Opt_nojournal_checksum),
  1557. fsparam_flag ("journal_async_commit",Opt_journal_async_commit),
  1558. fsparam_flag ("abort", Opt_abort),
  1559. fsparam_enum ("data", Opt_data, ext4_param_data),
  1560. fsparam_enum ("data_err", Opt_data_err,
  1561. ext4_param_data_err),
  1562. fsparam_string_empty
  1563. ("usrjquota", Opt_usrjquota),
  1564. fsparam_string_empty
  1565. ("grpjquota", Opt_grpjquota),
  1566. fsparam_enum ("jqfmt", Opt_jqfmt, ext4_param_jqfmt),
  1567. fsparam_flag ("grpquota", Opt_grpquota),
  1568. fsparam_flag ("quota", Opt_quota),
  1569. fsparam_flag ("noquota", Opt_noquota),
  1570. fsparam_flag ("usrquota", Opt_usrquota),
  1571. fsparam_flag ("prjquota", Opt_prjquota),
  1572. fsparam_flag ("barrier", Opt_barrier),
  1573. fsparam_u32 ("barrier", Opt_barrier),
  1574. fsparam_flag ("nobarrier", Opt_nobarrier),
  1575. fsparam_flag ("i_version", Opt_removed),
  1576. fsparam_flag ("dax", Opt_dax),
  1577. fsparam_enum ("dax", Opt_dax_type, ext4_param_dax),
  1578. fsparam_u32 ("stripe", Opt_stripe),
  1579. fsparam_flag ("delalloc", Opt_delalloc),
  1580. fsparam_flag ("nodelalloc", Opt_nodelalloc),
  1581. fsparam_flag ("warn_on_error", Opt_warn_on_error),
  1582. fsparam_flag ("nowarn_on_error", Opt_nowarn_on_error),
  1583. fsparam_u32 ("debug_want_extra_isize",
  1584. Opt_debug_want_extra_isize),
  1585. fsparam_flag ("mblk_io_submit", Opt_removed),
  1586. fsparam_flag ("nomblk_io_submit", Opt_removed),
  1587. fsparam_flag ("block_validity", Opt_block_validity),
  1588. fsparam_flag ("noblock_validity", Opt_noblock_validity),
  1589. fsparam_u32 ("inode_readahead_blks",
  1590. Opt_inode_readahead_blks),
  1591. fsparam_u32 ("journal_ioprio", Opt_journal_ioprio),
  1592. fsparam_u32 ("auto_da_alloc", Opt_auto_da_alloc),
  1593. fsparam_flag ("auto_da_alloc", Opt_auto_da_alloc),
  1594. fsparam_flag ("noauto_da_alloc", Opt_noauto_da_alloc),
  1595. fsparam_flag ("dioread_nolock", Opt_dioread_nolock),
  1596. fsparam_flag ("nodioread_nolock", Opt_dioread_lock),
  1597. fsparam_flag ("dioread_lock", Opt_dioread_lock),
  1598. fsparam_flag ("discard", Opt_discard),
  1599. fsparam_flag ("nodiscard", Opt_nodiscard),
  1600. fsparam_u32 ("init_itable", Opt_init_itable),
  1601. fsparam_flag ("init_itable", Opt_init_itable),
  1602. fsparam_flag ("noinit_itable", Opt_noinit_itable),
  1603. #ifdef CONFIG_EXT4_DEBUG
  1604. fsparam_flag ("fc_debug_force", Opt_fc_debug_force),
  1605. fsparam_u32 ("fc_debug_max_replay", Opt_fc_debug_max_replay),
  1606. #endif
  1607. fsparam_u32 ("max_dir_size_kb", Opt_max_dir_size_kb),
  1608. fsparam_flag ("test_dummy_encryption",
  1609. Opt_test_dummy_encryption),
  1610. fsparam_string ("test_dummy_encryption",
  1611. Opt_test_dummy_encryption),
  1612. fsparam_flag ("inlinecrypt", Opt_inlinecrypt),
  1613. fsparam_flag ("nombcache", Opt_nombcache),
  1614. fsparam_flag ("no_mbcache", Opt_nombcache), /* for backward compatibility */
  1615. fsparam_flag ("prefetch_block_bitmaps",
  1616. Opt_removed),
  1617. fsparam_flag ("no_prefetch_block_bitmaps",
  1618. Opt_no_prefetch_block_bitmaps),
  1619. fsparam_s32 ("mb_optimize_scan", Opt_mb_optimize_scan),
  1620. fsparam_string ("check", Opt_removed), /* mount option from ext2/3 */
  1621. fsparam_flag ("nocheck", Opt_removed), /* mount option from ext2/3 */
  1622. fsparam_flag ("reservation", Opt_removed), /* mount option from ext2/3 */
  1623. fsparam_flag ("noreservation", Opt_removed), /* mount option from ext2/3 */
  1624. fsparam_u32 ("journal", Opt_removed), /* mount option from ext2/3 */
  1625. {}
  1626. };
  1627. #define MOPT_SET 0x0001
  1628. #define MOPT_CLEAR 0x0002
  1629. #define MOPT_NOSUPPORT 0x0004
  1630. #define MOPT_EXPLICIT 0x0008
  1631. #ifdef CONFIG_QUOTA
  1632. #define MOPT_Q 0
  1633. #define MOPT_QFMT 0x0010
  1634. #else
  1635. #define MOPT_Q MOPT_NOSUPPORT
  1636. #define MOPT_QFMT MOPT_NOSUPPORT
  1637. #endif
  1638. #define MOPT_NO_EXT2 0x0020
  1639. #define MOPT_NO_EXT3 0x0040
  1640. #define MOPT_EXT4_ONLY (MOPT_NO_EXT2 | MOPT_NO_EXT3)
  1641. #define MOPT_SKIP 0x0080
  1642. #define MOPT_2 0x0100
  1643. static const struct mount_opts {
  1644. int token;
  1645. int mount_opt;
  1646. int flags;
  1647. } ext4_mount_opts[] = {
  1648. {Opt_minix_df, EXT4_MOUNT_MINIX_DF, MOPT_SET},
  1649. {Opt_bsd_df, EXT4_MOUNT_MINIX_DF, MOPT_CLEAR},
  1650. {Opt_grpid, EXT4_MOUNT_GRPID, MOPT_SET},
  1651. {Opt_nogrpid, EXT4_MOUNT_GRPID, MOPT_CLEAR},
  1652. {Opt_block_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_SET},
  1653. {Opt_noblock_validity, EXT4_MOUNT_BLOCK_VALIDITY, MOPT_CLEAR},
  1654. {Opt_dioread_nolock, EXT4_MOUNT_DIOREAD_NOLOCK,
  1655. MOPT_EXT4_ONLY | MOPT_SET},
  1656. {Opt_dioread_lock, EXT4_MOUNT_DIOREAD_NOLOCK,
  1657. MOPT_EXT4_ONLY | MOPT_CLEAR},
  1658. {Opt_discard, EXT4_MOUNT_DISCARD, MOPT_SET},
  1659. {Opt_nodiscard, EXT4_MOUNT_DISCARD, MOPT_CLEAR},
  1660. {Opt_delalloc, EXT4_MOUNT_DELALLOC,
  1661. MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
  1662. {Opt_nodelalloc, EXT4_MOUNT_DELALLOC,
  1663. MOPT_EXT4_ONLY | MOPT_CLEAR},
  1664. {Opt_warn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_SET},
  1665. {Opt_nowarn_on_error, EXT4_MOUNT_WARN_ON_ERROR, MOPT_CLEAR},
  1666. {Opt_commit, 0, MOPT_NO_EXT2},
  1667. {Opt_nojournal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
  1668. MOPT_EXT4_ONLY | MOPT_CLEAR},
  1669. {Opt_journal_checksum, EXT4_MOUNT_JOURNAL_CHECKSUM,
  1670. MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
  1671. {Opt_journal_async_commit, (EXT4_MOUNT_JOURNAL_ASYNC_COMMIT |
  1672. EXT4_MOUNT_JOURNAL_CHECKSUM),
  1673. MOPT_EXT4_ONLY | MOPT_SET | MOPT_EXPLICIT},
  1674. {Opt_noload, EXT4_MOUNT_NOLOAD, MOPT_NO_EXT2 | MOPT_SET},
  1675. {Opt_data_err, EXT4_MOUNT_DATA_ERR_ABORT, MOPT_NO_EXT2},
  1676. {Opt_barrier, EXT4_MOUNT_BARRIER, MOPT_SET},
  1677. {Opt_nobarrier, EXT4_MOUNT_BARRIER, MOPT_CLEAR},
  1678. {Opt_noauto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_SET},
  1679. {Opt_auto_da_alloc, EXT4_MOUNT_NO_AUTO_DA_ALLOC, MOPT_CLEAR},
  1680. {Opt_noinit_itable, EXT4_MOUNT_INIT_INODE_TABLE, MOPT_CLEAR},
  1681. {Opt_dax_type, 0, MOPT_EXT4_ONLY},
  1682. {Opt_journal_dev, 0, MOPT_NO_EXT2},
  1683. {Opt_journal_path, 0, MOPT_NO_EXT2},
  1684. {Opt_journal_ioprio, 0, MOPT_NO_EXT2},
  1685. {Opt_data, 0, MOPT_NO_EXT2},
  1686. {Opt_user_xattr, EXT4_MOUNT_XATTR_USER, MOPT_SET},
  1687. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  1688. {Opt_acl, EXT4_MOUNT_POSIX_ACL, MOPT_SET},
  1689. #else
  1690. {Opt_acl, 0, MOPT_NOSUPPORT},
  1691. #endif
  1692. {Opt_nouid32, EXT4_MOUNT_NO_UID32, MOPT_SET},
  1693. {Opt_debug, EXT4_MOUNT_DEBUG, MOPT_SET},
  1694. {Opt_quota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA, MOPT_SET | MOPT_Q},
  1695. {Opt_usrquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA,
  1696. MOPT_SET | MOPT_Q},
  1697. {Opt_grpquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_GRPQUOTA,
  1698. MOPT_SET | MOPT_Q},
  1699. {Opt_prjquota, EXT4_MOUNT_QUOTA | EXT4_MOUNT_PRJQUOTA,
  1700. MOPT_SET | MOPT_Q},
  1701. {Opt_noquota, (EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
  1702. EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA),
  1703. MOPT_CLEAR | MOPT_Q},
  1704. {Opt_usrjquota, 0, MOPT_Q},
  1705. {Opt_grpjquota, 0, MOPT_Q},
  1706. {Opt_jqfmt, 0, MOPT_QFMT},
  1707. {Opt_nombcache, EXT4_MOUNT_NO_MBCACHE, MOPT_SET},
  1708. {Opt_no_prefetch_block_bitmaps, EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS,
  1709. MOPT_SET},
  1710. #ifdef CONFIG_EXT4_DEBUG
  1711. {Opt_fc_debug_force, EXT4_MOUNT2_JOURNAL_FAST_COMMIT,
  1712. MOPT_SET | MOPT_2 | MOPT_EXT4_ONLY},
  1713. #endif
  1714. {Opt_abort, EXT4_MOUNT2_ABORT, MOPT_SET | MOPT_2},
  1715. {Opt_err, 0, 0}
  1716. };
  1717. #if IS_ENABLED(CONFIG_UNICODE)
  1718. static const struct ext4_sb_encodings {
  1719. __u16 magic;
  1720. char *name;
  1721. unsigned int version;
  1722. } ext4_sb_encoding_map[] = {
  1723. {EXT4_ENC_UTF8_12_1, "utf8", UNICODE_AGE(12, 1, 0)},
  1724. };
  1725. static const struct ext4_sb_encodings *
  1726. ext4_sb_read_encoding(const struct ext4_super_block *es)
  1727. {
  1728. __u16 magic = le16_to_cpu(es->s_encoding);
  1729. int i;
  1730. for (i = 0; i < ARRAY_SIZE(ext4_sb_encoding_map); i++)
  1731. if (magic == ext4_sb_encoding_map[i].magic)
  1732. return &ext4_sb_encoding_map[i];
  1733. return NULL;
  1734. }
  1735. #endif
  1736. #define EXT4_SPEC_JQUOTA (1 << 0)
  1737. #define EXT4_SPEC_JQFMT (1 << 1)
  1738. #define EXT4_SPEC_DATAJ (1 << 2)
  1739. #define EXT4_SPEC_SB_BLOCK (1 << 3)
  1740. #define EXT4_SPEC_JOURNAL_DEV (1 << 4)
  1741. #define EXT4_SPEC_JOURNAL_IOPRIO (1 << 5)
  1742. #define EXT4_SPEC_s_want_extra_isize (1 << 7)
  1743. #define EXT4_SPEC_s_max_batch_time (1 << 8)
  1744. #define EXT4_SPEC_s_min_batch_time (1 << 9)
  1745. #define EXT4_SPEC_s_inode_readahead_blks (1 << 10)
  1746. #define EXT4_SPEC_s_li_wait_mult (1 << 11)
  1747. #define EXT4_SPEC_s_max_dir_size_kb (1 << 12)
  1748. #define EXT4_SPEC_s_stripe (1 << 13)
  1749. #define EXT4_SPEC_s_resuid (1 << 14)
  1750. #define EXT4_SPEC_s_resgid (1 << 15)
  1751. #define EXT4_SPEC_s_commit_interval (1 << 16)
  1752. #define EXT4_SPEC_s_fc_debug_max_replay (1 << 17)
  1753. #define EXT4_SPEC_s_sb_block (1 << 18)
  1754. #define EXT4_SPEC_mb_optimize_scan (1 << 19)
  1755. struct ext4_fs_context {
  1756. char *s_qf_names[EXT4_MAXQUOTAS];
  1757. struct fscrypt_dummy_policy dummy_enc_policy;
  1758. int s_jquota_fmt; /* Format of quota to use */
  1759. #ifdef CONFIG_EXT4_DEBUG
  1760. int s_fc_debug_max_replay;
  1761. #endif
  1762. unsigned short qname_spec;
  1763. unsigned long vals_s_flags; /* Bits to set in s_flags */
  1764. unsigned long mask_s_flags; /* Bits changed in s_flags */
  1765. unsigned long journal_devnum;
  1766. unsigned long s_commit_interval;
  1767. unsigned long s_stripe;
  1768. unsigned int s_inode_readahead_blks;
  1769. unsigned int s_want_extra_isize;
  1770. unsigned int s_li_wait_mult;
  1771. unsigned int s_max_dir_size_kb;
  1772. unsigned int journal_ioprio;
  1773. unsigned int vals_s_mount_opt;
  1774. unsigned int mask_s_mount_opt;
  1775. unsigned int vals_s_mount_opt2;
  1776. unsigned int mask_s_mount_opt2;
  1777. unsigned int opt_flags; /* MOPT flags */
  1778. unsigned int spec;
  1779. u32 s_max_batch_time;
  1780. u32 s_min_batch_time;
  1781. kuid_t s_resuid;
  1782. kgid_t s_resgid;
  1783. ext4_fsblk_t s_sb_block;
  1784. };
  1785. static void ext4_fc_free(struct fs_context *fc)
  1786. {
  1787. struct ext4_fs_context *ctx = fc->fs_private;
  1788. int i;
  1789. if (!ctx)
  1790. return;
  1791. for (i = 0; i < EXT4_MAXQUOTAS; i++)
  1792. kfree(ctx->s_qf_names[i]);
  1793. fscrypt_free_dummy_policy(&ctx->dummy_enc_policy);
  1794. kfree(ctx);
  1795. }
  1796. int ext4_init_fs_context(struct fs_context *fc)
  1797. {
  1798. struct ext4_fs_context *ctx;
  1799. ctx = kzalloc_obj(struct ext4_fs_context);
  1800. if (!ctx)
  1801. return -ENOMEM;
  1802. fc->fs_private = ctx;
  1803. fc->ops = &ext4_context_ops;
  1804. /* i_version is always enabled now */
  1805. fc->sb_flags |= SB_I_VERSION;
  1806. return 0;
  1807. }
  1808. #ifdef CONFIG_QUOTA
  1809. /*
  1810. * Note the name of the specified quota file.
  1811. */
  1812. static int note_qf_name(struct fs_context *fc, int qtype,
  1813. struct fs_parameter *param)
  1814. {
  1815. struct ext4_fs_context *ctx = fc->fs_private;
  1816. char *qname;
  1817. if (param->size < 1) {
  1818. ext4_msg(NULL, KERN_ERR, "Missing quota name");
  1819. return -EINVAL;
  1820. }
  1821. if (strchr(param->string, '/')) {
  1822. ext4_msg(NULL, KERN_ERR,
  1823. "quotafile must be on filesystem root");
  1824. return -EINVAL;
  1825. }
  1826. if (ctx->s_qf_names[qtype]) {
  1827. if (strcmp(ctx->s_qf_names[qtype], param->string) != 0) {
  1828. ext4_msg(NULL, KERN_ERR,
  1829. "%s quota file already specified",
  1830. QTYPE2NAME(qtype));
  1831. return -EINVAL;
  1832. }
  1833. return 0;
  1834. }
  1835. qname = kmemdup_nul(param->string, param->size, GFP_KERNEL);
  1836. if (!qname) {
  1837. ext4_msg(NULL, KERN_ERR,
  1838. "Not enough memory for storing quotafile name");
  1839. return -ENOMEM;
  1840. }
  1841. ctx->s_qf_names[qtype] = qname;
  1842. ctx->qname_spec |= 1 << qtype;
  1843. ctx->spec |= EXT4_SPEC_JQUOTA;
  1844. return 0;
  1845. }
  1846. /*
  1847. * Clear the name of the specified quota file.
  1848. */
  1849. static int unnote_qf_name(struct fs_context *fc, int qtype)
  1850. {
  1851. struct ext4_fs_context *ctx = fc->fs_private;
  1852. kfree(ctx->s_qf_names[qtype]);
  1853. ctx->s_qf_names[qtype] = NULL;
  1854. ctx->qname_spec |= 1 << qtype;
  1855. ctx->spec |= EXT4_SPEC_JQUOTA;
  1856. return 0;
  1857. }
  1858. #endif
  1859. static int ext4_parse_test_dummy_encryption(const struct fs_parameter *param,
  1860. struct ext4_fs_context *ctx)
  1861. {
  1862. int err;
  1863. if (!IS_ENABLED(CONFIG_FS_ENCRYPTION)) {
  1864. ext4_msg(NULL, KERN_WARNING,
  1865. "test_dummy_encryption option not supported");
  1866. return -EINVAL;
  1867. }
  1868. err = fscrypt_parse_test_dummy_encryption(param,
  1869. &ctx->dummy_enc_policy);
  1870. if (err == -EINVAL) {
  1871. ext4_msg(NULL, KERN_WARNING,
  1872. "Value of option \"%s\" is unrecognized", param->key);
  1873. } else if (err == -EEXIST) {
  1874. ext4_msg(NULL, KERN_WARNING,
  1875. "Conflicting test_dummy_encryption options");
  1876. return -EINVAL;
  1877. }
  1878. return err;
  1879. }
  1880. #define EXT4_SET_CTX(name) \
  1881. static inline __maybe_unused \
  1882. void ctx_set_##name(struct ext4_fs_context *ctx, unsigned long flag) \
  1883. { \
  1884. ctx->mask_s_##name |= flag; \
  1885. ctx->vals_s_##name |= flag; \
  1886. }
  1887. #define EXT4_CLEAR_CTX(name) \
  1888. static inline __maybe_unused \
  1889. void ctx_clear_##name(struct ext4_fs_context *ctx, unsigned long flag) \
  1890. { \
  1891. ctx->mask_s_##name |= flag; \
  1892. ctx->vals_s_##name &= ~flag; \
  1893. }
  1894. #define EXT4_TEST_CTX(name) \
  1895. static inline unsigned long \
  1896. ctx_test_##name(struct ext4_fs_context *ctx, unsigned long flag) \
  1897. { \
  1898. return (ctx->vals_s_##name & flag); \
  1899. }
  1900. EXT4_SET_CTX(flags); /* set only */
  1901. EXT4_SET_CTX(mount_opt);
  1902. EXT4_CLEAR_CTX(mount_opt);
  1903. EXT4_TEST_CTX(mount_opt);
  1904. EXT4_SET_CTX(mount_opt2);
  1905. EXT4_CLEAR_CTX(mount_opt2);
  1906. EXT4_TEST_CTX(mount_opt2);
  1907. static int ext4_parse_param(struct fs_context *fc, struct fs_parameter *param)
  1908. {
  1909. struct ext4_fs_context *ctx = fc->fs_private;
  1910. struct fs_parse_result result;
  1911. const struct mount_opts *m;
  1912. int is_remount;
  1913. int token;
  1914. token = fs_parse(fc, ext4_param_specs, param, &result);
  1915. if (token < 0)
  1916. return token;
  1917. is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
  1918. for (m = ext4_mount_opts; m->token != Opt_err; m++)
  1919. if (token == m->token)
  1920. break;
  1921. ctx->opt_flags |= m->flags;
  1922. if (m->flags & MOPT_EXPLICIT) {
  1923. if (m->mount_opt & EXT4_MOUNT_DELALLOC) {
  1924. ctx_set_mount_opt2(ctx, EXT4_MOUNT2_EXPLICIT_DELALLOC);
  1925. } else if (m->mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) {
  1926. ctx_set_mount_opt2(ctx,
  1927. EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM);
  1928. } else
  1929. return -EINVAL;
  1930. }
  1931. if (m->flags & MOPT_NOSUPPORT) {
  1932. ext4_msg(NULL, KERN_ERR, "%s option not supported",
  1933. param->key);
  1934. return 0;
  1935. }
  1936. switch (token) {
  1937. #ifdef CONFIG_QUOTA
  1938. case Opt_usrjquota:
  1939. if (!*param->string)
  1940. return unnote_qf_name(fc, USRQUOTA);
  1941. else
  1942. return note_qf_name(fc, USRQUOTA, param);
  1943. case Opt_grpjquota:
  1944. if (!*param->string)
  1945. return unnote_qf_name(fc, GRPQUOTA);
  1946. else
  1947. return note_qf_name(fc, GRPQUOTA, param);
  1948. #endif
  1949. case Opt_sb:
  1950. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  1951. ext4_msg(NULL, KERN_WARNING,
  1952. "Ignoring %s option on remount", param->key);
  1953. } else {
  1954. ctx->s_sb_block = result.uint_32;
  1955. ctx->spec |= EXT4_SPEC_s_sb_block;
  1956. }
  1957. return 0;
  1958. case Opt_removed:
  1959. ext4_msg(NULL, KERN_WARNING, "Ignoring removed %s option",
  1960. param->key);
  1961. return 0;
  1962. case Opt_inlinecrypt:
  1963. #ifdef CONFIG_FS_ENCRYPTION_INLINE_CRYPT
  1964. ctx_set_flags(ctx, SB_INLINECRYPT);
  1965. #else
  1966. ext4_msg(NULL, KERN_ERR, "inline encryption not supported");
  1967. #endif
  1968. return 0;
  1969. case Opt_errors:
  1970. ctx_clear_mount_opt(ctx, EXT4_MOUNT_ERRORS_MASK);
  1971. ctx_set_mount_opt(ctx, result.uint_32);
  1972. return 0;
  1973. #ifdef CONFIG_QUOTA
  1974. case Opt_jqfmt:
  1975. ctx->s_jquota_fmt = result.uint_32;
  1976. ctx->spec |= EXT4_SPEC_JQFMT;
  1977. return 0;
  1978. #endif
  1979. case Opt_data:
  1980. ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
  1981. ctx_set_mount_opt(ctx, result.uint_32);
  1982. ctx->spec |= EXT4_SPEC_DATAJ;
  1983. return 0;
  1984. case Opt_commit:
  1985. if (result.uint_32 == 0)
  1986. result.uint_32 = JBD2_DEFAULT_MAX_COMMIT_AGE;
  1987. else if (result.uint_32 > INT_MAX / HZ) {
  1988. ext4_msg(NULL, KERN_ERR,
  1989. "Invalid commit interval %d, "
  1990. "must be smaller than %d",
  1991. result.uint_32, INT_MAX / HZ);
  1992. return -EINVAL;
  1993. }
  1994. ctx->s_commit_interval = HZ * result.uint_32;
  1995. ctx->spec |= EXT4_SPEC_s_commit_interval;
  1996. return 0;
  1997. case Opt_debug_want_extra_isize:
  1998. if ((result.uint_32 & 1) || (result.uint_32 < 4)) {
  1999. ext4_msg(NULL, KERN_ERR,
  2000. "Invalid want_extra_isize %d", result.uint_32);
  2001. return -EINVAL;
  2002. }
  2003. ctx->s_want_extra_isize = result.uint_32;
  2004. ctx->spec |= EXT4_SPEC_s_want_extra_isize;
  2005. return 0;
  2006. case Opt_max_batch_time:
  2007. ctx->s_max_batch_time = result.uint_32;
  2008. ctx->spec |= EXT4_SPEC_s_max_batch_time;
  2009. return 0;
  2010. case Opt_min_batch_time:
  2011. ctx->s_min_batch_time = result.uint_32;
  2012. ctx->spec |= EXT4_SPEC_s_min_batch_time;
  2013. return 0;
  2014. case Opt_inode_readahead_blks:
  2015. if (result.uint_32 &&
  2016. (result.uint_32 > (1 << 30) ||
  2017. !is_power_of_2(result.uint_32))) {
  2018. ext4_msg(NULL, KERN_ERR,
  2019. "EXT4-fs: inode_readahead_blks must be "
  2020. "0 or a power of 2 smaller than 2^31");
  2021. return -EINVAL;
  2022. }
  2023. ctx->s_inode_readahead_blks = result.uint_32;
  2024. ctx->spec |= EXT4_SPEC_s_inode_readahead_blks;
  2025. return 0;
  2026. case Opt_init_itable:
  2027. ctx_set_mount_opt(ctx, EXT4_MOUNT_INIT_INODE_TABLE);
  2028. ctx->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
  2029. if (param->type == fs_value_is_string)
  2030. ctx->s_li_wait_mult = result.uint_32;
  2031. ctx->spec |= EXT4_SPEC_s_li_wait_mult;
  2032. return 0;
  2033. case Opt_max_dir_size_kb:
  2034. ctx->s_max_dir_size_kb = result.uint_32;
  2035. ctx->spec |= EXT4_SPEC_s_max_dir_size_kb;
  2036. return 0;
  2037. #ifdef CONFIG_EXT4_DEBUG
  2038. case Opt_fc_debug_max_replay:
  2039. ctx->s_fc_debug_max_replay = result.uint_32;
  2040. ctx->spec |= EXT4_SPEC_s_fc_debug_max_replay;
  2041. return 0;
  2042. #endif
  2043. case Opt_stripe:
  2044. ctx->s_stripe = result.uint_32;
  2045. ctx->spec |= EXT4_SPEC_s_stripe;
  2046. return 0;
  2047. case Opt_resuid:
  2048. ctx->s_resuid = result.uid;
  2049. ctx->spec |= EXT4_SPEC_s_resuid;
  2050. return 0;
  2051. case Opt_resgid:
  2052. ctx->s_resgid = result.gid;
  2053. ctx->spec |= EXT4_SPEC_s_resgid;
  2054. return 0;
  2055. case Opt_journal_dev:
  2056. if (is_remount) {
  2057. ext4_msg(NULL, KERN_ERR,
  2058. "Cannot specify journal on remount");
  2059. return -EINVAL;
  2060. }
  2061. ctx->journal_devnum = result.uint_32;
  2062. ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
  2063. return 0;
  2064. case Opt_journal_path:
  2065. {
  2066. struct inode *journal_inode;
  2067. struct path path;
  2068. int error;
  2069. if (is_remount) {
  2070. ext4_msg(NULL, KERN_ERR,
  2071. "Cannot specify journal on remount");
  2072. return -EINVAL;
  2073. }
  2074. error = fs_lookup_param(fc, param, 1, LOOKUP_FOLLOW, &path);
  2075. if (error) {
  2076. ext4_msg(NULL, KERN_ERR, "error: could not find "
  2077. "journal device path");
  2078. return -EINVAL;
  2079. }
  2080. journal_inode = d_inode(path.dentry);
  2081. ctx->journal_devnum = new_encode_dev(journal_inode->i_rdev);
  2082. ctx->spec |= EXT4_SPEC_JOURNAL_DEV;
  2083. path_put(&path);
  2084. return 0;
  2085. }
  2086. case Opt_journal_ioprio:
  2087. if (result.uint_32 > 7) {
  2088. ext4_msg(NULL, KERN_ERR, "Invalid journal IO priority"
  2089. " (must be 0-7)");
  2090. return -EINVAL;
  2091. }
  2092. ctx->journal_ioprio =
  2093. IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, result.uint_32);
  2094. ctx->spec |= EXT4_SPEC_JOURNAL_IOPRIO;
  2095. return 0;
  2096. case Opt_test_dummy_encryption:
  2097. return ext4_parse_test_dummy_encryption(param, ctx);
  2098. case Opt_dax:
  2099. case Opt_dax_type:
  2100. #ifdef CONFIG_FS_DAX
  2101. {
  2102. int type = (token == Opt_dax) ?
  2103. Opt_dax : result.uint_32;
  2104. switch (type) {
  2105. case Opt_dax:
  2106. case Opt_dax_always:
  2107. ctx_set_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
  2108. ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
  2109. break;
  2110. case Opt_dax_never:
  2111. ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
  2112. ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
  2113. break;
  2114. case Opt_dax_inode:
  2115. ctx_clear_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS);
  2116. ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER);
  2117. /* Strictly for printing options */
  2118. ctx_set_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE);
  2119. break;
  2120. }
  2121. return 0;
  2122. }
  2123. #else
  2124. ext4_msg(NULL, KERN_INFO, "dax option not supported");
  2125. return -EINVAL;
  2126. #endif
  2127. case Opt_data_err:
  2128. if (result.uint_32 == Opt_data_err_abort)
  2129. ctx_set_mount_opt(ctx, m->mount_opt);
  2130. else if (result.uint_32 == Opt_data_err_ignore)
  2131. ctx_clear_mount_opt(ctx, m->mount_opt);
  2132. return 0;
  2133. case Opt_mb_optimize_scan:
  2134. if (result.int_32 == 1) {
  2135. ctx_set_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN);
  2136. ctx->spec |= EXT4_SPEC_mb_optimize_scan;
  2137. } else if (result.int_32 == 0) {
  2138. ctx_clear_mount_opt2(ctx, EXT4_MOUNT2_MB_OPTIMIZE_SCAN);
  2139. ctx->spec |= EXT4_SPEC_mb_optimize_scan;
  2140. } else {
  2141. ext4_msg(NULL, KERN_WARNING,
  2142. "mb_optimize_scan should be set to 0 or 1.");
  2143. return -EINVAL;
  2144. }
  2145. return 0;
  2146. }
  2147. /*
  2148. * At this point we should only be getting options requiring MOPT_SET,
  2149. * or MOPT_CLEAR. Anything else is a bug
  2150. */
  2151. if (m->token == Opt_err) {
  2152. ext4_msg(NULL, KERN_WARNING, "buggy handling of option %s",
  2153. param->key);
  2154. WARN_ON(1);
  2155. return -EINVAL;
  2156. }
  2157. else {
  2158. unsigned int set = 0;
  2159. if ((param->type == fs_value_is_flag) ||
  2160. result.uint_32 > 0)
  2161. set = 1;
  2162. if (m->flags & MOPT_CLEAR)
  2163. set = !set;
  2164. else if (unlikely(!(m->flags & MOPT_SET))) {
  2165. ext4_msg(NULL, KERN_WARNING,
  2166. "buggy handling of option %s",
  2167. param->key);
  2168. WARN_ON(1);
  2169. return -EINVAL;
  2170. }
  2171. if (m->flags & MOPT_2) {
  2172. if (set != 0)
  2173. ctx_set_mount_opt2(ctx, m->mount_opt);
  2174. else
  2175. ctx_clear_mount_opt2(ctx, m->mount_opt);
  2176. } else {
  2177. if (set != 0)
  2178. ctx_set_mount_opt(ctx, m->mount_opt);
  2179. else
  2180. ctx_clear_mount_opt(ctx, m->mount_opt);
  2181. }
  2182. }
  2183. return 0;
  2184. }
  2185. static int parse_options(struct fs_context *fc, char *options)
  2186. {
  2187. struct fs_parameter param;
  2188. int ret;
  2189. char *key;
  2190. if (!options)
  2191. return 0;
  2192. while ((key = strsep(&options, ",")) != NULL) {
  2193. if (*key) {
  2194. size_t v_len = 0;
  2195. char *value = strchr(key, '=');
  2196. param.type = fs_value_is_flag;
  2197. param.string = NULL;
  2198. if (value) {
  2199. if (value == key)
  2200. continue;
  2201. *value++ = 0;
  2202. v_len = strlen(value);
  2203. param.string = kmemdup_nul(value, v_len,
  2204. GFP_KERNEL);
  2205. if (!param.string)
  2206. return -ENOMEM;
  2207. param.type = fs_value_is_string;
  2208. }
  2209. param.key = key;
  2210. param.size = v_len;
  2211. ret = ext4_parse_param(fc, &param);
  2212. kfree(param.string);
  2213. if (ret < 0)
  2214. return ret;
  2215. }
  2216. }
  2217. ret = ext4_validate_options(fc);
  2218. if (ret < 0)
  2219. return ret;
  2220. return 0;
  2221. }
  2222. static int parse_apply_sb_mount_options(struct super_block *sb,
  2223. struct ext4_fs_context *m_ctx)
  2224. {
  2225. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2226. char s_mount_opts[64];
  2227. struct ext4_fs_context *s_ctx = NULL;
  2228. struct fs_context *fc = NULL;
  2229. int ret = -ENOMEM;
  2230. if (!sbi->s_es->s_mount_opts[0])
  2231. return 0;
  2232. if (strscpy_pad(s_mount_opts, sbi->s_es->s_mount_opts) < 0)
  2233. return -E2BIG;
  2234. fc = kzalloc_obj(struct fs_context);
  2235. if (!fc)
  2236. return -ENOMEM;
  2237. s_ctx = kzalloc_obj(struct ext4_fs_context);
  2238. if (!s_ctx)
  2239. goto out_free;
  2240. fc->fs_private = s_ctx;
  2241. fc->s_fs_info = sbi;
  2242. ret = parse_options(fc, s_mount_opts);
  2243. if (ret < 0)
  2244. goto parse_failed;
  2245. ret = ext4_check_opt_consistency(fc, sb);
  2246. if (ret < 0) {
  2247. parse_failed:
  2248. ext4_msg(sb, KERN_WARNING,
  2249. "failed to parse options in superblock: %s",
  2250. s_mount_opts);
  2251. ret = 0;
  2252. goto out_free;
  2253. }
  2254. if (s_ctx->spec & EXT4_SPEC_JOURNAL_DEV)
  2255. m_ctx->journal_devnum = s_ctx->journal_devnum;
  2256. if (s_ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)
  2257. m_ctx->journal_ioprio = s_ctx->journal_ioprio;
  2258. ext4_apply_options(fc, sb);
  2259. ret = 0;
  2260. out_free:
  2261. ext4_fc_free(fc);
  2262. kfree(fc);
  2263. return ret;
  2264. }
  2265. static void ext4_apply_quota_options(struct fs_context *fc,
  2266. struct super_block *sb)
  2267. {
  2268. #ifdef CONFIG_QUOTA
  2269. bool quota_feature = ext4_has_feature_quota(sb);
  2270. struct ext4_fs_context *ctx = fc->fs_private;
  2271. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2272. char *qname;
  2273. int i;
  2274. if (quota_feature)
  2275. return;
  2276. if (ctx->spec & EXT4_SPEC_JQUOTA) {
  2277. for (i = 0; i < EXT4_MAXQUOTAS; i++) {
  2278. if (!(ctx->qname_spec & (1 << i)))
  2279. continue;
  2280. qname = ctx->s_qf_names[i]; /* May be NULL */
  2281. if (qname)
  2282. set_opt(sb, QUOTA);
  2283. ctx->s_qf_names[i] = NULL;
  2284. qname = rcu_replace_pointer(sbi->s_qf_names[i], qname,
  2285. lockdep_is_held(&sb->s_umount));
  2286. if (qname)
  2287. kfree_rcu_mightsleep(qname);
  2288. }
  2289. }
  2290. if (ctx->spec & EXT4_SPEC_JQFMT)
  2291. sbi->s_jquota_fmt = ctx->s_jquota_fmt;
  2292. #endif
  2293. }
  2294. /*
  2295. * Check quota settings consistency.
  2296. */
  2297. static int ext4_check_quota_consistency(struct fs_context *fc,
  2298. struct super_block *sb)
  2299. {
  2300. #ifdef CONFIG_QUOTA
  2301. struct ext4_fs_context *ctx = fc->fs_private;
  2302. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2303. bool quota_feature = ext4_has_feature_quota(sb);
  2304. bool quota_loaded = sb_any_quota_loaded(sb);
  2305. bool usr_qf_name, grp_qf_name, usrquota, grpquota;
  2306. int quota_flags, i;
  2307. /*
  2308. * We do the test below only for project quotas. 'usrquota' and
  2309. * 'grpquota' mount options are allowed even without quota feature
  2310. * to support legacy quotas in quota files.
  2311. */
  2312. if (ctx_test_mount_opt(ctx, EXT4_MOUNT_PRJQUOTA) &&
  2313. !ext4_has_feature_project(sb)) {
  2314. ext4_msg(NULL, KERN_ERR, "Project quota feature not enabled. "
  2315. "Cannot enable project quota enforcement.");
  2316. return -EINVAL;
  2317. }
  2318. quota_flags = EXT4_MOUNT_QUOTA | EXT4_MOUNT_USRQUOTA |
  2319. EXT4_MOUNT_GRPQUOTA | EXT4_MOUNT_PRJQUOTA;
  2320. if (quota_loaded &&
  2321. ctx->mask_s_mount_opt & quota_flags &&
  2322. !ctx_test_mount_opt(ctx, quota_flags))
  2323. goto err_quota_change;
  2324. if (ctx->spec & EXT4_SPEC_JQUOTA) {
  2325. for (i = 0; i < EXT4_MAXQUOTAS; i++) {
  2326. if (!(ctx->qname_spec & (1 << i)))
  2327. continue;
  2328. if (quota_loaded &&
  2329. !!sbi->s_qf_names[i] != !!ctx->s_qf_names[i])
  2330. goto err_jquota_change;
  2331. if (sbi->s_qf_names[i] && ctx->s_qf_names[i] &&
  2332. strcmp(get_qf_name(sb, sbi, i),
  2333. ctx->s_qf_names[i]) != 0)
  2334. goto err_jquota_specified;
  2335. }
  2336. if (quota_feature) {
  2337. ext4_msg(NULL, KERN_INFO,
  2338. "Journaled quota options ignored when "
  2339. "QUOTA feature is enabled");
  2340. return 0;
  2341. }
  2342. }
  2343. if (ctx->spec & EXT4_SPEC_JQFMT) {
  2344. if (sbi->s_jquota_fmt != ctx->s_jquota_fmt && quota_loaded)
  2345. goto err_jquota_change;
  2346. if (quota_feature) {
  2347. ext4_msg(NULL, KERN_INFO, "Quota format mount options "
  2348. "ignored when QUOTA feature is enabled");
  2349. return 0;
  2350. }
  2351. }
  2352. /* Make sure we don't mix old and new quota format */
  2353. usr_qf_name = (get_qf_name(sb, sbi, USRQUOTA) ||
  2354. ctx->s_qf_names[USRQUOTA]);
  2355. grp_qf_name = (get_qf_name(sb, sbi, GRPQUOTA) ||
  2356. ctx->s_qf_names[GRPQUOTA]);
  2357. usrquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
  2358. test_opt(sb, USRQUOTA));
  2359. grpquota = (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) ||
  2360. test_opt(sb, GRPQUOTA));
  2361. if (usr_qf_name) {
  2362. ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
  2363. usrquota = false;
  2364. }
  2365. if (grp_qf_name) {
  2366. ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
  2367. grpquota = false;
  2368. }
  2369. if (usr_qf_name || grp_qf_name) {
  2370. if (usrquota || grpquota) {
  2371. ext4_msg(NULL, KERN_ERR, "old and new quota "
  2372. "format mixing");
  2373. return -EINVAL;
  2374. }
  2375. if (!(ctx->spec & EXT4_SPEC_JQFMT || sbi->s_jquota_fmt)) {
  2376. ext4_msg(NULL, KERN_ERR, "journaled quota format "
  2377. "not specified");
  2378. return -EINVAL;
  2379. }
  2380. }
  2381. return 0;
  2382. err_quota_change:
  2383. ext4_msg(NULL, KERN_ERR,
  2384. "Cannot change quota options when quota turned on");
  2385. return -EINVAL;
  2386. err_jquota_change:
  2387. ext4_msg(NULL, KERN_ERR, "Cannot change journaled quota "
  2388. "options when quota turned on");
  2389. return -EINVAL;
  2390. err_jquota_specified:
  2391. ext4_msg(NULL, KERN_ERR, "%s quota file already specified",
  2392. QTYPE2NAME(i));
  2393. return -EINVAL;
  2394. #else
  2395. return 0;
  2396. #endif
  2397. }
  2398. static int ext4_check_test_dummy_encryption(const struct fs_context *fc,
  2399. struct super_block *sb)
  2400. {
  2401. const struct ext4_fs_context *ctx = fc->fs_private;
  2402. const struct ext4_sb_info *sbi = EXT4_SB(sb);
  2403. if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy))
  2404. return 0;
  2405. if (!ext4_has_feature_encrypt(sb)) {
  2406. ext4_msg(NULL, KERN_WARNING,
  2407. "test_dummy_encryption requires encrypt feature");
  2408. return -EINVAL;
  2409. }
  2410. /*
  2411. * This mount option is just for testing, and it's not worthwhile to
  2412. * implement the extra complexity (e.g. RCU protection) that would be
  2413. * needed to allow it to be set or changed during remount. We do allow
  2414. * it to be specified during remount, but only if there is no change.
  2415. */
  2416. if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
  2417. if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy,
  2418. &ctx->dummy_enc_policy))
  2419. return 0;
  2420. ext4_msg(NULL, KERN_WARNING,
  2421. "Can't set or change test_dummy_encryption on remount");
  2422. return -EINVAL;
  2423. }
  2424. /* Also make sure s_mount_opts didn't contain a conflicting value. */
  2425. if (fscrypt_is_dummy_policy_set(&sbi->s_dummy_enc_policy)) {
  2426. if (fscrypt_dummy_policies_equal(&sbi->s_dummy_enc_policy,
  2427. &ctx->dummy_enc_policy))
  2428. return 0;
  2429. ext4_msg(NULL, KERN_WARNING,
  2430. "Conflicting test_dummy_encryption options");
  2431. return -EINVAL;
  2432. }
  2433. return 0;
  2434. }
  2435. static void ext4_apply_test_dummy_encryption(struct ext4_fs_context *ctx,
  2436. struct super_block *sb)
  2437. {
  2438. if (!fscrypt_is_dummy_policy_set(&ctx->dummy_enc_policy) ||
  2439. /* if already set, it was already verified to be the same */
  2440. fscrypt_is_dummy_policy_set(&EXT4_SB(sb)->s_dummy_enc_policy))
  2441. return;
  2442. EXT4_SB(sb)->s_dummy_enc_policy = ctx->dummy_enc_policy;
  2443. memset(&ctx->dummy_enc_policy, 0, sizeof(ctx->dummy_enc_policy));
  2444. ext4_msg(sb, KERN_WARNING, "Test dummy encryption mode enabled");
  2445. }
  2446. static int ext4_check_opt_consistency(struct fs_context *fc,
  2447. struct super_block *sb)
  2448. {
  2449. struct ext4_fs_context *ctx = fc->fs_private;
  2450. struct ext4_sb_info *sbi = fc->s_fs_info;
  2451. int is_remount = fc->purpose == FS_CONTEXT_FOR_RECONFIGURE;
  2452. int err;
  2453. if ((ctx->opt_flags & MOPT_NO_EXT2) && IS_EXT2_SB(sb)) {
  2454. ext4_msg(NULL, KERN_ERR,
  2455. "Mount option(s) incompatible with ext2");
  2456. return -EINVAL;
  2457. }
  2458. if ((ctx->opt_flags & MOPT_NO_EXT3) && IS_EXT3_SB(sb)) {
  2459. ext4_msg(NULL, KERN_ERR,
  2460. "Mount option(s) incompatible with ext3");
  2461. return -EINVAL;
  2462. }
  2463. if (ctx->s_want_extra_isize >
  2464. (sbi->s_inode_size - EXT4_GOOD_OLD_INODE_SIZE)) {
  2465. ext4_msg(NULL, KERN_ERR,
  2466. "Invalid want_extra_isize %d",
  2467. ctx->s_want_extra_isize);
  2468. return -EINVAL;
  2469. }
  2470. err = ext4_check_test_dummy_encryption(fc, sb);
  2471. if (err)
  2472. return err;
  2473. if ((ctx->spec & EXT4_SPEC_DATAJ) && is_remount) {
  2474. if (!sbi->s_journal) {
  2475. ext4_msg(NULL, KERN_WARNING,
  2476. "Remounting file system with no journal "
  2477. "so ignoring journalled data option");
  2478. ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS);
  2479. } else if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DATA_FLAGS) !=
  2480. test_opt(sb, DATA_FLAGS)) {
  2481. ext4_msg(NULL, KERN_ERR, "Cannot change data mode "
  2482. "on remount");
  2483. return -EINVAL;
  2484. }
  2485. }
  2486. if (is_remount) {
  2487. if (!sbi->s_journal &&
  2488. ctx_test_mount_opt(ctx, EXT4_MOUNT_DATA_ERR_ABORT)) {
  2489. ext4_msg(NULL, KERN_WARNING,
  2490. "Remounting fs w/o journal so ignoring data_err option");
  2491. ctx_clear_mount_opt(ctx, EXT4_MOUNT_DATA_ERR_ABORT);
  2492. }
  2493. if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
  2494. (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
  2495. ext4_msg(NULL, KERN_ERR, "can't mount with "
  2496. "both data=journal and dax");
  2497. return -EINVAL;
  2498. }
  2499. if (ctx_test_mount_opt(ctx, EXT4_MOUNT_DAX_ALWAYS) &&
  2500. (!(sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
  2501. (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER))) {
  2502. fail_dax_change_remount:
  2503. ext4_msg(NULL, KERN_ERR, "can't change "
  2504. "dax mount option while remounting");
  2505. return -EINVAL;
  2506. } else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_NEVER) &&
  2507. (!(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
  2508. (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS))) {
  2509. goto fail_dax_change_remount;
  2510. } else if (ctx_test_mount_opt2(ctx, EXT4_MOUNT2_DAX_INODE) &&
  2511. ((sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) ||
  2512. (sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_NEVER) ||
  2513. !(sbi->s_mount_opt2 & EXT4_MOUNT2_DAX_INODE))) {
  2514. goto fail_dax_change_remount;
  2515. }
  2516. }
  2517. return ext4_check_quota_consistency(fc, sb);
  2518. }
  2519. static void ext4_apply_options(struct fs_context *fc, struct super_block *sb)
  2520. {
  2521. struct ext4_fs_context *ctx = fc->fs_private;
  2522. struct ext4_sb_info *sbi = fc->s_fs_info;
  2523. sbi->s_mount_opt &= ~ctx->mask_s_mount_opt;
  2524. sbi->s_mount_opt |= ctx->vals_s_mount_opt;
  2525. sbi->s_mount_opt2 &= ~ctx->mask_s_mount_opt2;
  2526. sbi->s_mount_opt2 |= ctx->vals_s_mount_opt2;
  2527. sb->s_flags &= ~ctx->mask_s_flags;
  2528. sb->s_flags |= ctx->vals_s_flags;
  2529. #define APPLY(X) ({ if (ctx->spec & EXT4_SPEC_##X) sbi->X = ctx->X; })
  2530. APPLY(s_commit_interval);
  2531. APPLY(s_stripe);
  2532. APPLY(s_max_batch_time);
  2533. APPLY(s_min_batch_time);
  2534. APPLY(s_want_extra_isize);
  2535. APPLY(s_inode_readahead_blks);
  2536. APPLY(s_max_dir_size_kb);
  2537. APPLY(s_li_wait_mult);
  2538. APPLY(s_resgid);
  2539. APPLY(s_resuid);
  2540. #ifdef CONFIG_EXT4_DEBUG
  2541. APPLY(s_fc_debug_max_replay);
  2542. #endif
  2543. ext4_apply_quota_options(fc, sb);
  2544. ext4_apply_test_dummy_encryption(ctx, sb);
  2545. }
  2546. static int ext4_validate_options(struct fs_context *fc)
  2547. {
  2548. #ifdef CONFIG_QUOTA
  2549. struct ext4_fs_context *ctx = fc->fs_private;
  2550. char *usr_qf_name, *grp_qf_name;
  2551. usr_qf_name = ctx->s_qf_names[USRQUOTA];
  2552. grp_qf_name = ctx->s_qf_names[GRPQUOTA];
  2553. if (usr_qf_name || grp_qf_name) {
  2554. if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) && usr_qf_name)
  2555. ctx_clear_mount_opt(ctx, EXT4_MOUNT_USRQUOTA);
  2556. if (ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA) && grp_qf_name)
  2557. ctx_clear_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA);
  2558. if (ctx_test_mount_opt(ctx, EXT4_MOUNT_USRQUOTA) ||
  2559. ctx_test_mount_opt(ctx, EXT4_MOUNT_GRPQUOTA)) {
  2560. ext4_msg(NULL, KERN_ERR, "old and new quota "
  2561. "format mixing");
  2562. return -EINVAL;
  2563. }
  2564. }
  2565. #endif
  2566. return 1;
  2567. }
  2568. static inline void ext4_show_quota_options(struct seq_file *seq,
  2569. struct super_block *sb)
  2570. {
  2571. #if defined(CONFIG_QUOTA)
  2572. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2573. char *usr_qf_name, *grp_qf_name;
  2574. if (sbi->s_jquota_fmt) {
  2575. char *fmtname = "";
  2576. switch (sbi->s_jquota_fmt) {
  2577. case QFMT_VFS_OLD:
  2578. fmtname = "vfsold";
  2579. break;
  2580. case QFMT_VFS_V0:
  2581. fmtname = "vfsv0";
  2582. break;
  2583. case QFMT_VFS_V1:
  2584. fmtname = "vfsv1";
  2585. break;
  2586. }
  2587. seq_printf(seq, ",jqfmt=%s", fmtname);
  2588. }
  2589. rcu_read_lock();
  2590. usr_qf_name = rcu_dereference(sbi->s_qf_names[USRQUOTA]);
  2591. grp_qf_name = rcu_dereference(sbi->s_qf_names[GRPQUOTA]);
  2592. if (usr_qf_name)
  2593. seq_show_option(seq, "usrjquota", usr_qf_name);
  2594. if (grp_qf_name)
  2595. seq_show_option(seq, "grpjquota", grp_qf_name);
  2596. rcu_read_unlock();
  2597. #endif
  2598. }
  2599. static const char *token2str(int token)
  2600. {
  2601. const struct fs_parameter_spec *spec;
  2602. for (spec = ext4_param_specs; spec->name != NULL; spec++)
  2603. if (spec->opt == token && !spec->type)
  2604. break;
  2605. return spec->name;
  2606. }
  2607. /*
  2608. * Show an option if
  2609. * - it's set to a non-default value OR
  2610. * - if the per-sb default is different from the global default
  2611. */
  2612. static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
  2613. int nodefs)
  2614. {
  2615. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2616. struct ext4_super_block *es = sbi->s_es;
  2617. int def_errors;
  2618. const struct mount_opts *m;
  2619. char sep = nodefs ? '\n' : ',';
  2620. #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
  2621. #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
  2622. if (sbi->s_sb_block != 1)
  2623. SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
  2624. for (m = ext4_mount_opts; m->token != Opt_err; m++) {
  2625. int want_set = m->flags & MOPT_SET;
  2626. int opt_2 = m->flags & MOPT_2;
  2627. unsigned int mount_opt, def_mount_opt;
  2628. if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
  2629. m->flags & MOPT_SKIP)
  2630. continue;
  2631. if (opt_2) {
  2632. mount_opt = sbi->s_mount_opt2;
  2633. def_mount_opt = sbi->s_def_mount_opt2;
  2634. } else {
  2635. mount_opt = sbi->s_mount_opt;
  2636. def_mount_opt = sbi->s_def_mount_opt;
  2637. }
  2638. /* skip if same as the default */
  2639. if (!nodefs && !(m->mount_opt & (mount_opt ^ def_mount_opt)))
  2640. continue;
  2641. /* select Opt_noFoo vs Opt_Foo */
  2642. if ((want_set &&
  2643. (mount_opt & m->mount_opt) != m->mount_opt) ||
  2644. (!want_set && (mount_opt & m->mount_opt)))
  2645. continue;
  2646. SEQ_OPTS_PRINT("%s", token2str(m->token));
  2647. }
  2648. if (nodefs || !uid_eq(sbi->s_resuid, make_kuid(&init_user_ns, EXT4_DEF_RESUID)) ||
  2649. ext4_get_resuid(es) != EXT4_DEF_RESUID)
  2650. SEQ_OPTS_PRINT("resuid=%u",
  2651. from_kuid_munged(&init_user_ns, sbi->s_resuid));
  2652. if (nodefs || !gid_eq(sbi->s_resgid, make_kgid(&init_user_ns, EXT4_DEF_RESGID)) ||
  2653. ext4_get_resgid(es) != EXT4_DEF_RESGID)
  2654. SEQ_OPTS_PRINT("resgid=%u",
  2655. from_kgid_munged(&init_user_ns, sbi->s_resgid));
  2656. def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
  2657. if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
  2658. SEQ_OPTS_PUTS("errors=remount-ro");
  2659. if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
  2660. SEQ_OPTS_PUTS("errors=continue");
  2661. if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
  2662. SEQ_OPTS_PUTS("errors=panic");
  2663. if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
  2664. SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
  2665. if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
  2666. SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
  2667. if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
  2668. SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
  2669. if (nodefs && sb->s_flags & SB_I_VERSION)
  2670. SEQ_OPTS_PUTS("i_version");
  2671. if (nodefs || sbi->s_stripe)
  2672. SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
  2673. if (nodefs || EXT4_MOUNT_DATA_FLAGS &
  2674. (sbi->s_mount_opt ^ sbi->s_def_mount_opt)) {
  2675. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  2676. SEQ_OPTS_PUTS("data=journal");
  2677. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  2678. SEQ_OPTS_PUTS("data=ordered");
  2679. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
  2680. SEQ_OPTS_PUTS("data=writeback");
  2681. }
  2682. if (nodefs ||
  2683. sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
  2684. SEQ_OPTS_PRINT("inode_readahead_blks=%u",
  2685. sbi->s_inode_readahead_blks);
  2686. if (test_opt(sb, INIT_INODE_TABLE) && (nodefs ||
  2687. (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
  2688. SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
  2689. if (nodefs || sbi->s_max_dir_size_kb)
  2690. SEQ_OPTS_PRINT("max_dir_size_kb=%u", sbi->s_max_dir_size_kb);
  2691. if (test_opt(sb, DATA_ERR_ABORT))
  2692. SEQ_OPTS_PUTS("data_err=abort");
  2693. fscrypt_show_test_dummy_encryption(seq, sep, sb);
  2694. if (sb->s_flags & SB_INLINECRYPT)
  2695. SEQ_OPTS_PUTS("inlinecrypt");
  2696. if (test_opt(sb, DAX_ALWAYS)) {
  2697. if (IS_EXT2_SB(sb))
  2698. SEQ_OPTS_PUTS("dax");
  2699. else
  2700. SEQ_OPTS_PUTS("dax=always");
  2701. } else if (test_opt2(sb, DAX_NEVER)) {
  2702. SEQ_OPTS_PUTS("dax=never");
  2703. } else if (test_opt2(sb, DAX_INODE)) {
  2704. SEQ_OPTS_PUTS("dax=inode");
  2705. }
  2706. if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD &&
  2707. !test_opt2(sb, MB_OPTIMIZE_SCAN)) {
  2708. SEQ_OPTS_PUTS("mb_optimize_scan=0");
  2709. } else if (sbi->s_groups_count < MB_DEFAULT_LINEAR_SCAN_THRESHOLD &&
  2710. test_opt2(sb, MB_OPTIMIZE_SCAN)) {
  2711. SEQ_OPTS_PUTS("mb_optimize_scan=1");
  2712. }
  2713. if (nodefs && !test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS))
  2714. SEQ_OPTS_PUTS("prefetch_block_bitmaps");
  2715. if (ext4_emergency_ro(sb))
  2716. SEQ_OPTS_PUTS("emergency_ro");
  2717. if (ext4_forced_shutdown(sb))
  2718. SEQ_OPTS_PUTS("shutdown");
  2719. ext4_show_quota_options(seq, sb);
  2720. return 0;
  2721. }
  2722. static int ext4_show_options(struct seq_file *seq, struct dentry *root)
  2723. {
  2724. return _ext4_show_options(seq, root->d_sb, 0);
  2725. }
  2726. int ext4_seq_options_show(struct seq_file *seq, void *offset)
  2727. {
  2728. struct super_block *sb = seq->private;
  2729. int rc;
  2730. seq_puts(seq, sb_rdonly(sb) ? "ro" : "rw");
  2731. rc = _ext4_show_options(seq, sb, 1);
  2732. seq_putc(seq, '\n');
  2733. return rc;
  2734. }
  2735. static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
  2736. int read_only)
  2737. {
  2738. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2739. int err = 0;
  2740. if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
  2741. ext4_msg(sb, KERN_ERR, "revision level too high, "
  2742. "forcing read-only mode");
  2743. err = -EROFS;
  2744. goto done;
  2745. }
  2746. if (read_only)
  2747. goto done;
  2748. if (!(sbi->s_mount_state & EXT4_VALID_FS))
  2749. ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
  2750. "running e2fsck is recommended");
  2751. else if (sbi->s_mount_state & EXT4_ERROR_FS)
  2752. ext4_msg(sb, KERN_WARNING,
  2753. "warning: mounting fs with errors, "
  2754. "running e2fsck is recommended");
  2755. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
  2756. le16_to_cpu(es->s_mnt_count) >=
  2757. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  2758. ext4_msg(sb, KERN_WARNING,
  2759. "warning: maximal mount count reached, "
  2760. "running e2fsck is recommended");
  2761. else if (le32_to_cpu(es->s_checkinterval) &&
  2762. (ext4_get_tstamp(es, s_lastcheck) +
  2763. le32_to_cpu(es->s_checkinterval) <= ktime_get_real_seconds()))
  2764. ext4_msg(sb, KERN_WARNING,
  2765. "warning: checktime reached, "
  2766. "running e2fsck is recommended");
  2767. if (!sbi->s_journal)
  2768. es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
  2769. if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
  2770. es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
  2771. le16_add_cpu(&es->s_mnt_count, 1);
  2772. ext4_update_tstamp(es, s_mtime);
  2773. if (sbi->s_journal) {
  2774. ext4_set_feature_journal_needs_recovery(sb);
  2775. if (ext4_has_feature_orphan_file(sb))
  2776. ext4_set_feature_orphan_present(sb);
  2777. }
  2778. err = ext4_commit_super(sb);
  2779. done:
  2780. if (test_opt(sb, DEBUG))
  2781. printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
  2782. "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
  2783. sb->s_blocksize,
  2784. sbi->s_groups_count,
  2785. EXT4_BLOCKS_PER_GROUP(sb),
  2786. EXT4_INODES_PER_GROUP(sb),
  2787. sbi->s_mount_opt, sbi->s_mount_opt2);
  2788. return err;
  2789. }
  2790. int ext4_alloc_flex_bg_array(struct super_block *sb, ext4_group_t ngroup)
  2791. {
  2792. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2793. struct flex_groups **old_groups, **new_groups;
  2794. int size, i, j;
  2795. if (!sbi->s_log_groups_per_flex)
  2796. return 0;
  2797. size = ext4_flex_group(sbi, ngroup - 1) + 1;
  2798. if (size <= sbi->s_flex_groups_allocated)
  2799. return 0;
  2800. new_groups = kvzalloc(roundup_pow_of_two(size *
  2801. sizeof(*sbi->s_flex_groups)), GFP_KERNEL);
  2802. if (!new_groups) {
  2803. ext4_msg(sb, KERN_ERR,
  2804. "not enough memory for %d flex group pointers", size);
  2805. return -ENOMEM;
  2806. }
  2807. for (i = sbi->s_flex_groups_allocated; i < size; i++) {
  2808. new_groups[i] = kvzalloc(roundup_pow_of_two(
  2809. sizeof(struct flex_groups)),
  2810. GFP_KERNEL);
  2811. if (!new_groups[i]) {
  2812. for (j = sbi->s_flex_groups_allocated; j < i; j++)
  2813. kvfree(new_groups[j]);
  2814. kvfree(new_groups);
  2815. ext4_msg(sb, KERN_ERR,
  2816. "not enough memory for %d flex groups", size);
  2817. return -ENOMEM;
  2818. }
  2819. }
  2820. rcu_read_lock();
  2821. old_groups = rcu_dereference(sbi->s_flex_groups);
  2822. if (old_groups)
  2823. memcpy(new_groups, old_groups,
  2824. (sbi->s_flex_groups_allocated *
  2825. sizeof(struct flex_groups *)));
  2826. rcu_read_unlock();
  2827. rcu_assign_pointer(sbi->s_flex_groups, new_groups);
  2828. sbi->s_flex_groups_allocated = size;
  2829. if (old_groups)
  2830. ext4_kvfree_array_rcu(old_groups);
  2831. return 0;
  2832. }
  2833. static int ext4_fill_flex_info(struct super_block *sb)
  2834. {
  2835. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2836. struct ext4_group_desc *gdp = NULL;
  2837. struct flex_groups *fg;
  2838. ext4_group_t flex_group;
  2839. int i, err;
  2840. sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
  2841. if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
  2842. sbi->s_log_groups_per_flex = 0;
  2843. return 1;
  2844. }
  2845. err = ext4_alloc_flex_bg_array(sb, sbi->s_groups_count);
  2846. if (err)
  2847. goto failed;
  2848. for (i = 0; i < sbi->s_groups_count; i++) {
  2849. gdp = ext4_get_group_desc(sb, i, NULL);
  2850. flex_group = ext4_flex_group(sbi, i);
  2851. fg = sbi_array_rcu_deref(sbi, s_flex_groups, flex_group);
  2852. atomic_add(ext4_free_inodes_count(sb, gdp), &fg->free_inodes);
  2853. atomic64_add(ext4_free_group_clusters(sb, gdp),
  2854. &fg->free_clusters);
  2855. atomic_add(ext4_used_dirs_count(sb, gdp), &fg->used_dirs);
  2856. }
  2857. return 1;
  2858. failed:
  2859. return 0;
  2860. }
  2861. static __le16 ext4_group_desc_csum(struct super_block *sb, __u32 block_group,
  2862. struct ext4_group_desc *gdp)
  2863. {
  2864. int offset = offsetof(struct ext4_group_desc, bg_checksum);
  2865. __u16 crc = 0;
  2866. __le32 le_group = cpu_to_le32(block_group);
  2867. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2868. if (ext4_has_feature_metadata_csum(sbi->s_sb)) {
  2869. /* Use new metadata_csum algorithm */
  2870. __u32 csum32;
  2871. __u16 dummy_csum = 0;
  2872. csum32 = ext4_chksum(sbi->s_csum_seed, (__u8 *)&le_group,
  2873. sizeof(le_group));
  2874. csum32 = ext4_chksum(csum32, (__u8 *)gdp, offset);
  2875. csum32 = ext4_chksum(csum32, (__u8 *)&dummy_csum,
  2876. sizeof(dummy_csum));
  2877. offset += sizeof(dummy_csum);
  2878. if (offset < sbi->s_desc_size)
  2879. csum32 = ext4_chksum(csum32, (__u8 *)gdp + offset,
  2880. sbi->s_desc_size - offset);
  2881. crc = csum32 & 0xFFFF;
  2882. goto out;
  2883. }
  2884. /* old crc16 code */
  2885. if (!ext4_has_feature_gdt_csum(sb))
  2886. return 0;
  2887. crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
  2888. crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
  2889. crc = crc16(crc, (__u8 *)gdp, offset);
  2890. offset += sizeof(gdp->bg_checksum); /* skip checksum */
  2891. /* for checksum of struct ext4_group_desc do the rest...*/
  2892. if (ext4_has_feature_64bit(sb) && offset < sbi->s_desc_size)
  2893. crc = crc16(crc, (__u8 *)gdp + offset,
  2894. sbi->s_desc_size - offset);
  2895. out:
  2896. return cpu_to_le16(crc);
  2897. }
  2898. int ext4_group_desc_csum_verify(struct super_block *sb, __u32 block_group,
  2899. struct ext4_group_desc *gdp)
  2900. {
  2901. if (ext4_has_group_desc_csum(sb) &&
  2902. (gdp->bg_checksum != ext4_group_desc_csum(sb, block_group, gdp)))
  2903. return 0;
  2904. return 1;
  2905. }
  2906. void ext4_group_desc_csum_set(struct super_block *sb, __u32 block_group,
  2907. struct ext4_group_desc *gdp)
  2908. {
  2909. if (!ext4_has_group_desc_csum(sb))
  2910. return;
  2911. gdp->bg_checksum = ext4_group_desc_csum(sb, block_group, gdp);
  2912. }
  2913. /* Called at mount-time, super-block is locked */
  2914. static int ext4_check_descriptors(struct super_block *sb,
  2915. ext4_fsblk_t sb_block,
  2916. ext4_group_t *first_not_zeroed)
  2917. {
  2918. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2919. ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
  2920. ext4_fsblk_t last_block;
  2921. ext4_fsblk_t last_bg_block = sb_block + ext4_bg_num_gdb(sb, 0);
  2922. ext4_fsblk_t block_bitmap;
  2923. ext4_fsblk_t inode_bitmap;
  2924. ext4_fsblk_t inode_table;
  2925. int flexbg_flag = 0;
  2926. ext4_group_t i, grp = sbi->s_groups_count;
  2927. if (ext4_has_feature_flex_bg(sb))
  2928. flexbg_flag = 1;
  2929. ext4_debug("Checking group descriptors");
  2930. for (i = 0; i < sbi->s_groups_count; i++) {
  2931. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  2932. if (i == sbi->s_groups_count - 1 || flexbg_flag)
  2933. last_block = ext4_blocks_count(sbi->s_es) - 1;
  2934. else
  2935. last_block = first_block +
  2936. (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  2937. if ((grp == sbi->s_groups_count) &&
  2938. !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2939. grp = i;
  2940. block_bitmap = ext4_block_bitmap(sb, gdp);
  2941. if (block_bitmap == sb_block) {
  2942. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2943. "Block bitmap for group %u overlaps "
  2944. "superblock", i);
  2945. if (!sb_rdonly(sb))
  2946. return 0;
  2947. }
  2948. if (block_bitmap >= sb_block + 1 &&
  2949. block_bitmap <= last_bg_block) {
  2950. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2951. "Block bitmap for group %u overlaps "
  2952. "block group descriptors", i);
  2953. if (!sb_rdonly(sb))
  2954. return 0;
  2955. }
  2956. if (block_bitmap < first_block || block_bitmap > last_block) {
  2957. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2958. "Block bitmap for group %u not in group "
  2959. "(block %llu)!", i, block_bitmap);
  2960. return 0;
  2961. }
  2962. inode_bitmap = ext4_inode_bitmap(sb, gdp);
  2963. if (inode_bitmap == sb_block) {
  2964. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2965. "Inode bitmap for group %u overlaps "
  2966. "superblock", i);
  2967. if (!sb_rdonly(sb))
  2968. return 0;
  2969. }
  2970. if (inode_bitmap >= sb_block + 1 &&
  2971. inode_bitmap <= last_bg_block) {
  2972. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2973. "Inode bitmap for group %u overlaps "
  2974. "block group descriptors", i);
  2975. if (!sb_rdonly(sb))
  2976. return 0;
  2977. }
  2978. if (inode_bitmap < first_block || inode_bitmap > last_block) {
  2979. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2980. "Inode bitmap for group %u not in group "
  2981. "(block %llu)!", i, inode_bitmap);
  2982. return 0;
  2983. }
  2984. inode_table = ext4_inode_table(sb, gdp);
  2985. if (inode_table == sb_block) {
  2986. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2987. "Inode table for group %u overlaps "
  2988. "superblock", i);
  2989. if (!sb_rdonly(sb))
  2990. return 0;
  2991. }
  2992. if (inode_table >= sb_block + 1 &&
  2993. inode_table <= last_bg_block) {
  2994. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  2995. "Inode table for group %u overlaps "
  2996. "block group descriptors", i);
  2997. if (!sb_rdonly(sb))
  2998. return 0;
  2999. }
  3000. if (inode_table < first_block ||
  3001. inode_table + sbi->s_itb_per_group - 1 > last_block) {
  3002. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  3003. "Inode table for group %u not in group "
  3004. "(block %llu)!", i, inode_table);
  3005. return 0;
  3006. }
  3007. ext4_lock_group(sb, i);
  3008. if (!ext4_group_desc_csum_verify(sb, i, gdp)) {
  3009. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  3010. "Checksum for group %u failed (%u!=%u)",
  3011. i, le16_to_cpu(ext4_group_desc_csum(sb, i,
  3012. gdp)), le16_to_cpu(gdp->bg_checksum));
  3013. if (!sb_rdonly(sb)) {
  3014. ext4_unlock_group(sb, i);
  3015. return 0;
  3016. }
  3017. }
  3018. ext4_unlock_group(sb, i);
  3019. if (!flexbg_flag)
  3020. first_block += EXT4_BLOCKS_PER_GROUP(sb);
  3021. }
  3022. if (NULL != first_not_zeroed)
  3023. *first_not_zeroed = grp;
  3024. return 1;
  3025. }
  3026. /*
  3027. * Maximal extent format file size.
  3028. * Resulting logical blkno at s_maxbytes must fit in our on-disk
  3029. * extent format containers, within a sector_t, and within i_blocks
  3030. * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
  3031. * so that won't be a limiting factor.
  3032. *
  3033. * However there is other limiting factor. We do store extents in the form
  3034. * of starting block and length, hence the resulting length of the extent
  3035. * covering maximum file size must fit into on-disk format containers as
  3036. * well. Given that length is always by 1 unit bigger than max unit (because
  3037. * we count 0 as well) we have to lower the s_maxbytes by one fs block.
  3038. *
  3039. * Note, this does *not* consider any metadata overhead for vfs i_blocks.
  3040. */
  3041. static loff_t ext4_max_size(int blkbits, int has_huge_files)
  3042. {
  3043. loff_t res;
  3044. loff_t upper_limit = MAX_LFS_FILESIZE;
  3045. BUILD_BUG_ON(sizeof(blkcnt_t) < sizeof(u64));
  3046. if (!has_huge_files) {
  3047. upper_limit = (1LL << 32) - 1;
  3048. /* total blocks in file system block size */
  3049. upper_limit >>= (blkbits - 9);
  3050. upper_limit <<= blkbits;
  3051. }
  3052. /*
  3053. * 32-bit extent-start container, ee_block. We lower the maxbytes
  3054. * by one fs block, so ee_len can cover the extent of maximum file
  3055. * size
  3056. */
  3057. res = (1LL << 32) - 1;
  3058. res <<= blkbits;
  3059. /* Sanity check against vm- & vfs- imposed limits */
  3060. if (res > upper_limit)
  3061. res = upper_limit;
  3062. return res;
  3063. }
  3064. /*
  3065. * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
  3066. * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
  3067. * We need to be 1 filesystem block less than the 2^48 sector limit.
  3068. */
  3069. static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
  3070. {
  3071. loff_t upper_limit, res = EXT4_NDIR_BLOCKS;
  3072. int meta_blocks;
  3073. unsigned int ppb = 1 << (bits - 2);
  3074. /*
  3075. * This is calculated to be the largest file size for a dense, block
  3076. * mapped file such that the file's total number of 512-byte sectors,
  3077. * including data and all indirect blocks, does not exceed (2^48 - 1).
  3078. *
  3079. * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
  3080. * number of 512-byte sectors of the file.
  3081. */
  3082. if (!has_huge_files) {
  3083. /*
  3084. * !has_huge_files or implies that the inode i_block field
  3085. * represents total file blocks in 2^32 512-byte sectors ==
  3086. * size of vfs inode i_blocks * 8
  3087. */
  3088. upper_limit = (1LL << 32) - 1;
  3089. /* total blocks in file system block size */
  3090. upper_limit >>= (bits - 9);
  3091. } else {
  3092. /*
  3093. * We use 48 bit ext4_inode i_blocks
  3094. * With EXT4_HUGE_FILE_FL set the i_blocks
  3095. * represent total number of blocks in
  3096. * file system block size
  3097. */
  3098. upper_limit = (1LL << 48) - 1;
  3099. }
  3100. /* Compute how many blocks we can address by block tree */
  3101. res += ppb;
  3102. res += ppb * ppb;
  3103. res += ((loff_t)ppb) * ppb * ppb;
  3104. /* Compute how many metadata blocks are needed */
  3105. meta_blocks = 1;
  3106. meta_blocks += 1 + ppb;
  3107. meta_blocks += 1 + ppb + ppb * ppb;
  3108. /* Does block tree limit file size? */
  3109. if (res + meta_blocks <= upper_limit)
  3110. goto check_lfs;
  3111. res = upper_limit;
  3112. /* How many metadata blocks are needed for addressing upper_limit? */
  3113. upper_limit -= EXT4_NDIR_BLOCKS;
  3114. /* indirect blocks */
  3115. meta_blocks = 1;
  3116. upper_limit -= ppb;
  3117. /* double indirect blocks */
  3118. if (upper_limit < ppb * ppb) {
  3119. meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb);
  3120. res -= meta_blocks;
  3121. goto check_lfs;
  3122. }
  3123. meta_blocks += 1 + ppb;
  3124. upper_limit -= ppb * ppb;
  3125. /* tripple indirect blocks for the rest */
  3126. meta_blocks += 1 + DIV_ROUND_UP_ULL(upper_limit, ppb) +
  3127. DIV_ROUND_UP_ULL(upper_limit, ppb*ppb);
  3128. res -= meta_blocks;
  3129. check_lfs:
  3130. res <<= bits;
  3131. if (res > MAX_LFS_FILESIZE)
  3132. res = MAX_LFS_FILESIZE;
  3133. return res;
  3134. }
  3135. static ext4_fsblk_t descriptor_loc(struct super_block *sb,
  3136. ext4_fsblk_t logical_sb_block, int nr)
  3137. {
  3138. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3139. ext4_group_t bg, first_meta_bg;
  3140. int has_super = 0;
  3141. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  3142. if (!ext4_has_feature_meta_bg(sb) || nr < first_meta_bg)
  3143. return logical_sb_block + nr + 1;
  3144. bg = sbi->s_desc_per_block * nr;
  3145. if (ext4_bg_has_super(sb, bg))
  3146. has_super = 1;
  3147. /*
  3148. * If we have a meta_bg fs with 1k blocks, group 0's GDT is at
  3149. * block 2, not 1. If s_first_data_block == 0 (bigalloc is enabled
  3150. * on modern mke2fs or blksize > 1k on older mke2fs) then we must
  3151. * compensate.
  3152. */
  3153. if (sb->s_blocksize == 1024 && nr == 0 &&
  3154. le32_to_cpu(sbi->s_es->s_first_data_block) == 0)
  3155. has_super++;
  3156. return (has_super + ext4_group_first_block_no(sb, bg));
  3157. }
  3158. /**
  3159. * ext4_get_stripe_size: Get the stripe size.
  3160. * @sbi: In memory super block info
  3161. *
  3162. * If we have specified it via mount option, then
  3163. * use the mount option value. If the value specified at mount time is
  3164. * greater than the blocks per group use the super block value.
  3165. * If the super block value is greater than blocks per group return 0.
  3166. * Allocator needs it be less than blocks per group.
  3167. *
  3168. */
  3169. static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
  3170. {
  3171. unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
  3172. unsigned long stripe_width =
  3173. le32_to_cpu(sbi->s_es->s_raid_stripe_width);
  3174. int ret;
  3175. if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
  3176. ret = sbi->s_stripe;
  3177. else if (stripe_width && stripe_width <= sbi->s_blocks_per_group)
  3178. ret = stripe_width;
  3179. else if (stride && stride <= sbi->s_blocks_per_group)
  3180. ret = stride;
  3181. else
  3182. ret = 0;
  3183. /*
  3184. * If the stripe width is 1, this makes no sense and
  3185. * we set it to 0 to turn off stripe handling code.
  3186. */
  3187. if (ret <= 1)
  3188. ret = 0;
  3189. return ret;
  3190. }
  3191. /*
  3192. * Check whether this filesystem can be mounted based on
  3193. * the features present and the RDONLY/RDWR mount requested.
  3194. * Returns 1 if this filesystem can be mounted as requested,
  3195. * 0 if it cannot be.
  3196. */
  3197. int ext4_feature_set_ok(struct super_block *sb, int readonly)
  3198. {
  3199. if (ext4_has_unknown_ext4_incompat_features(sb)) {
  3200. ext4_msg(sb, KERN_ERR,
  3201. "Couldn't mount because of "
  3202. "unsupported optional features (%x)",
  3203. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
  3204. ~EXT4_FEATURE_INCOMPAT_SUPP));
  3205. return 0;
  3206. }
  3207. if (!IS_ENABLED(CONFIG_UNICODE) && ext4_has_feature_casefold(sb)) {
  3208. ext4_msg(sb, KERN_ERR,
  3209. "Filesystem with casefold feature cannot be "
  3210. "mounted without CONFIG_UNICODE");
  3211. return 0;
  3212. }
  3213. if (readonly)
  3214. return 1;
  3215. if (ext4_has_feature_readonly(sb)) {
  3216. ext4_msg(sb, KERN_INFO, "filesystem is read-only");
  3217. sb->s_flags |= SB_RDONLY;
  3218. return 1;
  3219. }
  3220. /* Check that feature set is OK for a read-write mount */
  3221. if (ext4_has_unknown_ext4_ro_compat_features(sb)) {
  3222. ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
  3223. "unsupported optional features (%x)",
  3224. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
  3225. ~EXT4_FEATURE_RO_COMPAT_SUPP));
  3226. return 0;
  3227. }
  3228. if (ext4_has_feature_bigalloc(sb) && !ext4_has_feature_extents(sb)) {
  3229. ext4_msg(sb, KERN_ERR,
  3230. "Can't support bigalloc feature without "
  3231. "extents feature\n");
  3232. return 0;
  3233. }
  3234. if (ext4_has_feature_bigalloc(sb) &&
  3235. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block)) {
  3236. ext4_msg(sb, KERN_WARNING,
  3237. "bad geometry: bigalloc file system with non-zero "
  3238. "first_data_block\n");
  3239. return 0;
  3240. }
  3241. #if !IS_ENABLED(CONFIG_QUOTA) || !IS_ENABLED(CONFIG_QFMT_V2)
  3242. if (!readonly && (ext4_has_feature_quota(sb) ||
  3243. ext4_has_feature_project(sb))) {
  3244. ext4_msg(sb, KERN_ERR,
  3245. "The kernel was not built with CONFIG_QUOTA and CONFIG_QFMT_V2");
  3246. return 0;
  3247. }
  3248. #endif /* CONFIG_QUOTA */
  3249. return 1;
  3250. }
  3251. /*
  3252. * This function is called once a day by default if we have errors logged
  3253. * on the file system.
  3254. * Use the err_report_sec sysfs attribute to disable or adjust its call
  3255. * freequency.
  3256. */
  3257. void print_daily_error_info(struct timer_list *t)
  3258. {
  3259. struct ext4_sb_info *sbi = timer_container_of(sbi, t, s_err_report);
  3260. struct super_block *sb = sbi->s_sb;
  3261. struct ext4_super_block *es = sbi->s_es;
  3262. if (es->s_error_count)
  3263. /* fsck newer than v1.41.13 is needed to clean this condition. */
  3264. ext4_msg(sb, KERN_NOTICE, "error count since last fsck: %u",
  3265. le32_to_cpu(es->s_error_count));
  3266. if (es->s_first_error_time) {
  3267. printk(KERN_NOTICE "EXT4-fs (%s): initial error at time %llu: %.*s:%d",
  3268. sb->s_id,
  3269. ext4_get_tstamp(es, s_first_error_time),
  3270. (int) sizeof(es->s_first_error_func),
  3271. es->s_first_error_func,
  3272. le32_to_cpu(es->s_first_error_line));
  3273. if (es->s_first_error_ino)
  3274. printk(KERN_CONT ": inode %u",
  3275. le32_to_cpu(es->s_first_error_ino));
  3276. if (es->s_first_error_block)
  3277. printk(KERN_CONT ": block %llu", (unsigned long long)
  3278. le64_to_cpu(es->s_first_error_block));
  3279. printk(KERN_CONT "\n");
  3280. }
  3281. if (es->s_last_error_time) {
  3282. printk(KERN_NOTICE "EXT4-fs (%s): last error at time %llu: %.*s:%d",
  3283. sb->s_id,
  3284. ext4_get_tstamp(es, s_last_error_time),
  3285. (int) sizeof(es->s_last_error_func),
  3286. es->s_last_error_func,
  3287. le32_to_cpu(es->s_last_error_line));
  3288. if (es->s_last_error_ino)
  3289. printk(KERN_CONT ": inode %u",
  3290. le32_to_cpu(es->s_last_error_ino));
  3291. if (es->s_last_error_block)
  3292. printk(KERN_CONT ": block %llu", (unsigned long long)
  3293. le64_to_cpu(es->s_last_error_block));
  3294. printk(KERN_CONT "\n");
  3295. }
  3296. if (sbi->s_err_report_sec)
  3297. mod_timer(&sbi->s_err_report, jiffies + secs_to_jiffies(sbi->s_err_report_sec));
  3298. }
  3299. /* Find next suitable group and run ext4_init_inode_table */
  3300. static int ext4_run_li_request(struct ext4_li_request *elr)
  3301. {
  3302. struct ext4_group_desc *gdp = NULL;
  3303. struct super_block *sb = elr->lr_super;
  3304. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  3305. ext4_group_t group = elr->lr_next_group;
  3306. unsigned int prefetch_ios = 0;
  3307. int ret = 0;
  3308. int nr = EXT4_SB(sb)->s_mb_prefetch;
  3309. u64 start_time;
  3310. if (elr->lr_mode == EXT4_LI_MODE_PREFETCH_BBITMAP) {
  3311. elr->lr_next_group = ext4_mb_prefetch(sb, group, nr, &prefetch_ios);
  3312. ext4_mb_prefetch_fini(sb, elr->lr_next_group, nr);
  3313. trace_ext4_prefetch_bitmaps(sb, group, elr->lr_next_group, nr);
  3314. if (group >= elr->lr_next_group) {
  3315. ret = 1;
  3316. if (elr->lr_first_not_zeroed != ngroups &&
  3317. !ext4_emergency_state(sb) && !sb_rdonly(sb) &&
  3318. test_opt(sb, INIT_INODE_TABLE)) {
  3319. elr->lr_next_group = elr->lr_first_not_zeroed;
  3320. elr->lr_mode = EXT4_LI_MODE_ITABLE;
  3321. ret = 0;
  3322. }
  3323. }
  3324. return ret;
  3325. }
  3326. for (; group < ngroups; group++) {
  3327. gdp = ext4_get_group_desc(sb, group, NULL);
  3328. if (!gdp) {
  3329. ret = 1;
  3330. break;
  3331. }
  3332. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  3333. break;
  3334. }
  3335. if (group >= ngroups)
  3336. ret = 1;
  3337. if (!ret) {
  3338. start_time = ktime_get_ns();
  3339. ret = ext4_init_inode_table(sb, group,
  3340. elr->lr_timeout ? 0 : 1);
  3341. trace_ext4_lazy_itable_init(sb, group);
  3342. if (elr->lr_timeout == 0) {
  3343. elr->lr_timeout = nsecs_to_jiffies((ktime_get_ns() - start_time) *
  3344. EXT4_SB(elr->lr_super)->s_li_wait_mult);
  3345. }
  3346. elr->lr_next_sched = jiffies + elr->lr_timeout;
  3347. elr->lr_next_group = group + 1;
  3348. }
  3349. return ret;
  3350. }
  3351. /*
  3352. * Remove lr_request from the list_request and free the
  3353. * request structure. Should be called with li_list_mtx held
  3354. */
  3355. static void ext4_remove_li_request(struct ext4_li_request *elr)
  3356. {
  3357. if (!elr)
  3358. return;
  3359. list_del(&elr->lr_request);
  3360. EXT4_SB(elr->lr_super)->s_li_request = NULL;
  3361. kfree(elr);
  3362. }
  3363. static void ext4_unregister_li_request(struct super_block *sb)
  3364. {
  3365. mutex_lock(&ext4_li_mtx);
  3366. if (!ext4_li_info) {
  3367. mutex_unlock(&ext4_li_mtx);
  3368. return;
  3369. }
  3370. mutex_lock(&ext4_li_info->li_list_mtx);
  3371. ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
  3372. mutex_unlock(&ext4_li_info->li_list_mtx);
  3373. mutex_unlock(&ext4_li_mtx);
  3374. }
  3375. static struct task_struct *ext4_lazyinit_task;
  3376. /*
  3377. * This is the function where ext4lazyinit thread lives. It walks
  3378. * through the request list searching for next scheduled filesystem.
  3379. * When such a fs is found, run the lazy initialization request
  3380. * (ext4_rn_li_request) and keep track of the time spend in this
  3381. * function. Based on that time we compute next schedule time of
  3382. * the request. When walking through the list is complete, compute
  3383. * next waking time and put itself into sleep.
  3384. */
  3385. static int ext4_lazyinit_thread(void *arg)
  3386. {
  3387. struct ext4_lazy_init *eli = arg;
  3388. struct list_head *pos, *n;
  3389. struct ext4_li_request *elr;
  3390. unsigned long next_wakeup, cur;
  3391. BUG_ON(NULL == eli);
  3392. set_freezable();
  3393. cont_thread:
  3394. while (true) {
  3395. bool next_wakeup_initialized = false;
  3396. next_wakeup = 0;
  3397. mutex_lock(&eli->li_list_mtx);
  3398. if (list_empty(&eli->li_request_list)) {
  3399. mutex_unlock(&eli->li_list_mtx);
  3400. goto exit_thread;
  3401. }
  3402. list_for_each_safe(pos, n, &eli->li_request_list) {
  3403. int err = 0;
  3404. int progress = 0;
  3405. elr = list_entry(pos, struct ext4_li_request,
  3406. lr_request);
  3407. if (time_before(jiffies, elr->lr_next_sched)) {
  3408. if (!next_wakeup_initialized ||
  3409. time_before(elr->lr_next_sched, next_wakeup)) {
  3410. next_wakeup = elr->lr_next_sched;
  3411. next_wakeup_initialized = true;
  3412. }
  3413. continue;
  3414. }
  3415. if (down_read_trylock(&elr->lr_super->s_umount)) {
  3416. if (sb_start_write_trylock(elr->lr_super)) {
  3417. progress = 1;
  3418. /*
  3419. * We hold sb->s_umount, sb can not
  3420. * be removed from the list, it is
  3421. * now safe to drop li_list_mtx
  3422. */
  3423. mutex_unlock(&eli->li_list_mtx);
  3424. err = ext4_run_li_request(elr);
  3425. sb_end_write(elr->lr_super);
  3426. mutex_lock(&eli->li_list_mtx);
  3427. n = pos->next;
  3428. }
  3429. up_read((&elr->lr_super->s_umount));
  3430. }
  3431. /* error, remove the lazy_init job */
  3432. if (err) {
  3433. ext4_remove_li_request(elr);
  3434. continue;
  3435. }
  3436. if (!progress) {
  3437. elr->lr_next_sched = jiffies +
  3438. get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ);
  3439. }
  3440. if (!next_wakeup_initialized ||
  3441. time_before(elr->lr_next_sched, next_wakeup)) {
  3442. next_wakeup = elr->lr_next_sched;
  3443. next_wakeup_initialized = true;
  3444. }
  3445. }
  3446. mutex_unlock(&eli->li_list_mtx);
  3447. try_to_freeze();
  3448. cur = jiffies;
  3449. if (!next_wakeup_initialized || time_after_eq(cur, next_wakeup)) {
  3450. cond_resched();
  3451. continue;
  3452. }
  3453. schedule_timeout_interruptible(next_wakeup - cur);
  3454. if (kthread_should_stop()) {
  3455. ext4_clear_request_list();
  3456. goto exit_thread;
  3457. }
  3458. }
  3459. exit_thread:
  3460. /*
  3461. * It looks like the request list is empty, but we need
  3462. * to check it under the li_list_mtx lock, to prevent any
  3463. * additions into it, and of course we should lock ext4_li_mtx
  3464. * to atomically free the list and ext4_li_info, because at
  3465. * this point another ext4 filesystem could be registering
  3466. * new one.
  3467. */
  3468. mutex_lock(&ext4_li_mtx);
  3469. mutex_lock(&eli->li_list_mtx);
  3470. if (!list_empty(&eli->li_request_list)) {
  3471. mutex_unlock(&eli->li_list_mtx);
  3472. mutex_unlock(&ext4_li_mtx);
  3473. goto cont_thread;
  3474. }
  3475. mutex_unlock(&eli->li_list_mtx);
  3476. kfree(ext4_li_info);
  3477. ext4_li_info = NULL;
  3478. mutex_unlock(&ext4_li_mtx);
  3479. return 0;
  3480. }
  3481. static void ext4_clear_request_list(void)
  3482. {
  3483. struct list_head *pos, *n;
  3484. struct ext4_li_request *elr;
  3485. mutex_lock(&ext4_li_info->li_list_mtx);
  3486. list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
  3487. elr = list_entry(pos, struct ext4_li_request,
  3488. lr_request);
  3489. ext4_remove_li_request(elr);
  3490. }
  3491. mutex_unlock(&ext4_li_info->li_list_mtx);
  3492. }
  3493. static int ext4_run_lazyinit_thread(void)
  3494. {
  3495. ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
  3496. ext4_li_info, "ext4lazyinit");
  3497. if (IS_ERR(ext4_lazyinit_task)) {
  3498. int err = PTR_ERR(ext4_lazyinit_task);
  3499. ext4_clear_request_list();
  3500. kfree(ext4_li_info);
  3501. ext4_li_info = NULL;
  3502. printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
  3503. "initialization thread\n",
  3504. err);
  3505. return err;
  3506. }
  3507. ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
  3508. return 0;
  3509. }
  3510. /*
  3511. * Check whether it make sense to run itable init. thread or not.
  3512. * If there is at least one uninitialized inode table, return
  3513. * corresponding group number, else the loop goes through all
  3514. * groups and return total number of groups.
  3515. */
  3516. static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
  3517. {
  3518. ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
  3519. struct ext4_group_desc *gdp = NULL;
  3520. if (!ext4_has_group_desc_csum(sb))
  3521. return ngroups;
  3522. for (group = 0; group < ngroups; group++) {
  3523. gdp = ext4_get_group_desc(sb, group, NULL);
  3524. if (!gdp)
  3525. continue;
  3526. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  3527. break;
  3528. }
  3529. return group;
  3530. }
  3531. static int ext4_li_info_new(void)
  3532. {
  3533. struct ext4_lazy_init *eli = NULL;
  3534. eli = kzalloc_obj(*eli);
  3535. if (!eli)
  3536. return -ENOMEM;
  3537. INIT_LIST_HEAD(&eli->li_request_list);
  3538. mutex_init(&eli->li_list_mtx);
  3539. eli->li_state |= EXT4_LAZYINIT_QUIT;
  3540. ext4_li_info = eli;
  3541. return 0;
  3542. }
  3543. static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
  3544. ext4_group_t start)
  3545. {
  3546. struct ext4_li_request *elr;
  3547. elr = kzalloc_obj(*elr);
  3548. if (!elr)
  3549. return NULL;
  3550. elr->lr_super = sb;
  3551. elr->lr_first_not_zeroed = start;
  3552. if (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS)) {
  3553. elr->lr_mode = EXT4_LI_MODE_ITABLE;
  3554. elr->lr_next_group = start;
  3555. } else {
  3556. elr->lr_mode = EXT4_LI_MODE_PREFETCH_BBITMAP;
  3557. }
  3558. /*
  3559. * Randomize first schedule time of the request to
  3560. * spread the inode table initialization requests
  3561. * better.
  3562. */
  3563. elr->lr_next_sched = jiffies + get_random_u32_below(EXT4_DEF_LI_MAX_START_DELAY * HZ);
  3564. return elr;
  3565. }
  3566. int ext4_register_li_request(struct super_block *sb,
  3567. ext4_group_t first_not_zeroed)
  3568. {
  3569. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3570. struct ext4_li_request *elr = NULL;
  3571. ext4_group_t ngroups = sbi->s_groups_count;
  3572. int ret = 0;
  3573. mutex_lock(&ext4_li_mtx);
  3574. if (sbi->s_li_request != NULL) {
  3575. /*
  3576. * Reset timeout so it can be computed again, because
  3577. * s_li_wait_mult might have changed.
  3578. */
  3579. sbi->s_li_request->lr_timeout = 0;
  3580. goto out;
  3581. }
  3582. if (ext4_emergency_state(sb) || sb_rdonly(sb) ||
  3583. (test_opt(sb, NO_PREFETCH_BLOCK_BITMAPS) &&
  3584. (first_not_zeroed == ngroups || !test_opt(sb, INIT_INODE_TABLE))))
  3585. goto out;
  3586. elr = ext4_li_request_new(sb, first_not_zeroed);
  3587. if (!elr) {
  3588. ret = -ENOMEM;
  3589. goto out;
  3590. }
  3591. if (NULL == ext4_li_info) {
  3592. ret = ext4_li_info_new();
  3593. if (ret)
  3594. goto out;
  3595. }
  3596. mutex_lock(&ext4_li_info->li_list_mtx);
  3597. list_add(&elr->lr_request, &ext4_li_info->li_request_list);
  3598. mutex_unlock(&ext4_li_info->li_list_mtx);
  3599. sbi->s_li_request = elr;
  3600. /*
  3601. * set elr to NULL here since it has been inserted to
  3602. * the request_list and the removal and free of it is
  3603. * handled by ext4_clear_request_list from now on.
  3604. */
  3605. elr = NULL;
  3606. if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
  3607. ret = ext4_run_lazyinit_thread();
  3608. if (ret)
  3609. goto out;
  3610. }
  3611. out:
  3612. mutex_unlock(&ext4_li_mtx);
  3613. if (ret)
  3614. kfree(elr);
  3615. return ret;
  3616. }
  3617. /*
  3618. * We do not need to lock anything since this is called on
  3619. * module unload.
  3620. */
  3621. static void ext4_destroy_lazyinit_thread(void)
  3622. {
  3623. /*
  3624. * If thread exited earlier
  3625. * there's nothing to be done.
  3626. */
  3627. if (!ext4_li_info || !ext4_lazyinit_task)
  3628. return;
  3629. kthread_stop(ext4_lazyinit_task);
  3630. }
  3631. static int set_journal_csum_feature_set(struct super_block *sb)
  3632. {
  3633. int ret = 1;
  3634. int compat, incompat;
  3635. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3636. if (ext4_has_feature_metadata_csum(sb)) {
  3637. /* journal checksum v3 */
  3638. compat = 0;
  3639. incompat = JBD2_FEATURE_INCOMPAT_CSUM_V3;
  3640. } else {
  3641. /* journal checksum v1 */
  3642. compat = JBD2_FEATURE_COMPAT_CHECKSUM;
  3643. incompat = 0;
  3644. }
  3645. jbd2_journal_clear_features(sbi->s_journal,
  3646. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  3647. JBD2_FEATURE_INCOMPAT_CSUM_V3 |
  3648. JBD2_FEATURE_INCOMPAT_CSUM_V2);
  3649. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  3650. ret = jbd2_journal_set_features(sbi->s_journal,
  3651. compat, 0,
  3652. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT |
  3653. incompat);
  3654. } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
  3655. ret = jbd2_journal_set_features(sbi->s_journal,
  3656. compat, 0,
  3657. incompat);
  3658. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  3659. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3660. } else {
  3661. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  3662. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3663. }
  3664. return ret;
  3665. }
  3666. /*
  3667. * Note: calculating the overhead so we can be compatible with
  3668. * historical BSD practice is quite difficult in the face of
  3669. * clusters/bigalloc. This is because multiple metadata blocks from
  3670. * different block group can end up in the same allocation cluster.
  3671. * Calculating the exact overhead in the face of clustered allocation
  3672. * requires either O(all block bitmaps) in memory or O(number of block
  3673. * groups**2) in time. We will still calculate the superblock for
  3674. * older file systems --- and if we come across with a bigalloc file
  3675. * system with zero in s_overhead_clusters the estimate will be close to
  3676. * correct especially for very large cluster sizes --- but for newer
  3677. * file systems, it's better to calculate this figure once at mkfs
  3678. * time, and store it in the superblock. If the superblock value is
  3679. * present (even for non-bigalloc file systems), we will use it.
  3680. */
  3681. static int count_overhead(struct super_block *sb, ext4_group_t grp,
  3682. char *buf)
  3683. {
  3684. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3685. struct ext4_group_desc *gdp;
  3686. ext4_fsblk_t first_block, last_block, b;
  3687. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3688. int s, j, count = 0;
  3689. int has_super = ext4_bg_has_super(sb, grp);
  3690. if (!ext4_has_feature_bigalloc(sb))
  3691. return (has_super + ext4_bg_num_gdb(sb, grp) +
  3692. (has_super ? le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) : 0) +
  3693. sbi->s_itb_per_group + 2);
  3694. first_block = le32_to_cpu(sbi->s_es->s_first_data_block) +
  3695. (grp * EXT4_BLOCKS_PER_GROUP(sb));
  3696. last_block = first_block + EXT4_BLOCKS_PER_GROUP(sb) - 1;
  3697. for (i = 0; i < ngroups; i++) {
  3698. gdp = ext4_get_group_desc(sb, i, NULL);
  3699. b = ext4_block_bitmap(sb, gdp);
  3700. if (b >= first_block && b <= last_block) {
  3701. ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
  3702. count++;
  3703. }
  3704. b = ext4_inode_bitmap(sb, gdp);
  3705. if (b >= first_block && b <= last_block) {
  3706. ext4_set_bit(EXT4_B2C(sbi, b - first_block), buf);
  3707. count++;
  3708. }
  3709. b = ext4_inode_table(sb, gdp);
  3710. if (b >= first_block && b + sbi->s_itb_per_group <= last_block)
  3711. for (j = 0; j < sbi->s_itb_per_group; j++, b++) {
  3712. int c = EXT4_B2C(sbi, b - first_block);
  3713. ext4_set_bit(c, buf);
  3714. count++;
  3715. }
  3716. if (i != grp)
  3717. continue;
  3718. s = 0;
  3719. if (ext4_bg_has_super(sb, grp)) {
  3720. ext4_set_bit(s++, buf);
  3721. count++;
  3722. }
  3723. j = ext4_bg_num_gdb(sb, grp);
  3724. if (s + j > EXT4_BLOCKS_PER_GROUP(sb)) {
  3725. ext4_error(sb, "Invalid number of block group "
  3726. "descriptor blocks: %d", j);
  3727. j = EXT4_BLOCKS_PER_GROUP(sb) - s;
  3728. }
  3729. count += j;
  3730. for (; j > 0; j--)
  3731. ext4_set_bit(EXT4_B2C(sbi, s++), buf);
  3732. }
  3733. if (!count)
  3734. return 0;
  3735. return EXT4_CLUSTERS_PER_GROUP(sb) -
  3736. ext4_count_free(buf, EXT4_CLUSTERS_PER_GROUP(sb) / 8);
  3737. }
  3738. /*
  3739. * Compute the overhead and stash it in sbi->s_overhead
  3740. */
  3741. int ext4_calculate_overhead(struct super_block *sb)
  3742. {
  3743. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3744. struct ext4_super_block *es = sbi->s_es;
  3745. struct inode *j_inode;
  3746. unsigned int j_blocks, j_inum = le32_to_cpu(es->s_journal_inum);
  3747. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3748. ext4_fsblk_t overhead = 0;
  3749. char *buf = kvmalloc(sb->s_blocksize, GFP_NOFS | __GFP_ZERO);
  3750. if (!buf)
  3751. return -ENOMEM;
  3752. /*
  3753. * Compute the overhead (FS structures). This is constant
  3754. * for a given filesystem unless the number of block groups
  3755. * changes so we cache the previous value until it does.
  3756. */
  3757. /*
  3758. * All of the blocks before first_data_block are overhead
  3759. */
  3760. overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
  3761. /*
  3762. * Add the overhead found in each block group
  3763. */
  3764. for (i = 0; i < ngroups; i++) {
  3765. int blks;
  3766. blks = count_overhead(sb, i, buf);
  3767. overhead += blks;
  3768. if (blks)
  3769. memset(buf, 0, sb->s_blocksize);
  3770. cond_resched();
  3771. }
  3772. /*
  3773. * Add the internal journal blocks whether the journal has been
  3774. * loaded or not
  3775. */
  3776. if (sbi->s_journal && !sbi->s_journal_bdev_file)
  3777. overhead += EXT4_NUM_B2C(sbi, sbi->s_journal->j_total_len);
  3778. else if (ext4_has_feature_journal(sb) && !sbi->s_journal && j_inum) {
  3779. /* j_inum for internal journal is non-zero */
  3780. j_inode = ext4_get_journal_inode(sb, j_inum);
  3781. if (!IS_ERR(j_inode)) {
  3782. j_blocks = j_inode->i_size >> sb->s_blocksize_bits;
  3783. overhead += EXT4_NUM_B2C(sbi, j_blocks);
  3784. iput(j_inode);
  3785. } else {
  3786. ext4_msg(sb, KERN_ERR, "can't get journal size");
  3787. }
  3788. }
  3789. sbi->s_overhead = overhead;
  3790. smp_wmb();
  3791. kvfree(buf);
  3792. return 0;
  3793. }
  3794. static void ext4_set_resv_clusters(struct super_block *sb)
  3795. {
  3796. ext4_fsblk_t resv_clusters;
  3797. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3798. /*
  3799. * There's no need to reserve anything when we aren't using extents.
  3800. * The space estimates are exact, there are no unwritten extents,
  3801. * hole punching doesn't need new metadata... This is needed especially
  3802. * to keep ext2/3 backward compatibility.
  3803. */
  3804. if (!ext4_has_feature_extents(sb))
  3805. return;
  3806. /*
  3807. * By default we reserve 2% or 4096 clusters, whichever is smaller.
  3808. * This should cover the situations where we can not afford to run
  3809. * out of space like for example punch hole, or converting
  3810. * unwritten extents in delalloc path. In most cases such
  3811. * allocation would require 1, or 2 blocks, higher numbers are
  3812. * very rare.
  3813. */
  3814. resv_clusters = (ext4_blocks_count(sbi->s_es) >>
  3815. sbi->s_cluster_bits);
  3816. do_div(resv_clusters, 50);
  3817. resv_clusters = min_t(ext4_fsblk_t, resv_clusters, 4096);
  3818. atomic64_set(&sbi->s_resv_clusters, resv_clusters);
  3819. }
  3820. static const char *ext4_quota_mode(struct super_block *sb)
  3821. {
  3822. #ifdef CONFIG_QUOTA
  3823. if (!ext4_quota_capable(sb))
  3824. return "none";
  3825. if (EXT4_SB(sb)->s_journal && ext4_is_quota_journalled(sb))
  3826. return "journalled";
  3827. else
  3828. return "writeback";
  3829. #else
  3830. return "disabled";
  3831. #endif
  3832. }
  3833. static void ext4_setup_csum_trigger(struct super_block *sb,
  3834. enum ext4_journal_trigger_type type,
  3835. void (*trigger)(
  3836. struct jbd2_buffer_trigger_type *type,
  3837. struct buffer_head *bh,
  3838. void *mapped_data,
  3839. size_t size))
  3840. {
  3841. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3842. sbi->s_journal_triggers[type].sb = sb;
  3843. sbi->s_journal_triggers[type].tr_triggers.t_frozen = trigger;
  3844. }
  3845. static void ext4_free_sbi(struct ext4_sb_info *sbi)
  3846. {
  3847. if (!sbi)
  3848. return;
  3849. kfree(sbi->s_blockgroup_lock);
  3850. fs_put_dax(sbi->s_daxdev, NULL);
  3851. kfree(sbi);
  3852. }
  3853. static struct ext4_sb_info *ext4_alloc_sbi(struct super_block *sb)
  3854. {
  3855. struct ext4_sb_info *sbi;
  3856. sbi = kzalloc_obj(*sbi);
  3857. if (!sbi)
  3858. return NULL;
  3859. sbi->s_daxdev = fs_dax_get_by_bdev(sb->s_bdev, &sbi->s_dax_part_off,
  3860. NULL, NULL);
  3861. sbi->s_blockgroup_lock =
  3862. kzalloc_obj(struct blockgroup_lock);
  3863. if (!sbi->s_blockgroup_lock)
  3864. goto err_out;
  3865. sb->s_fs_info = sbi;
  3866. sbi->s_sb = sb;
  3867. return sbi;
  3868. err_out:
  3869. fs_put_dax(sbi->s_daxdev, NULL);
  3870. kfree(sbi);
  3871. return NULL;
  3872. }
  3873. static void ext4_set_def_opts(struct super_block *sb,
  3874. struct ext4_super_block *es)
  3875. {
  3876. unsigned long def_mount_opts;
  3877. /* Set defaults before we parse the mount options */
  3878. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  3879. set_opt(sb, INIT_INODE_TABLE);
  3880. if (def_mount_opts & EXT4_DEFM_DEBUG)
  3881. set_opt(sb, DEBUG);
  3882. if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
  3883. set_opt(sb, GRPID);
  3884. if (def_mount_opts & EXT4_DEFM_UID16)
  3885. set_opt(sb, NO_UID32);
  3886. /* xattr user namespace & acls are now defaulted on */
  3887. set_opt(sb, XATTR_USER);
  3888. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  3889. set_opt(sb, POSIX_ACL);
  3890. #endif
  3891. if (ext4_has_feature_fast_commit(sb))
  3892. set_opt2(sb, JOURNAL_FAST_COMMIT);
  3893. /* don't forget to enable journal_csum when metadata_csum is enabled. */
  3894. if (ext4_has_feature_metadata_csum(sb))
  3895. set_opt(sb, JOURNAL_CHECKSUM);
  3896. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  3897. set_opt(sb, JOURNAL_DATA);
  3898. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  3899. set_opt(sb, ORDERED_DATA);
  3900. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  3901. set_opt(sb, WRITEBACK_DATA);
  3902. if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_PANIC)
  3903. set_opt(sb, ERRORS_PANIC);
  3904. else if (le16_to_cpu(es->s_errors) == EXT4_ERRORS_CONTINUE)
  3905. set_opt(sb, ERRORS_CONT);
  3906. else
  3907. set_opt(sb, ERRORS_RO);
  3908. /* block_validity enabled by default; disable with noblock_validity */
  3909. set_opt(sb, BLOCK_VALIDITY);
  3910. if (def_mount_opts & EXT4_DEFM_DISCARD)
  3911. set_opt(sb, DISCARD);
  3912. if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
  3913. set_opt(sb, BARRIER);
  3914. /*
  3915. * enable delayed allocation by default
  3916. * Use -o nodelalloc to turn it off
  3917. */
  3918. if (!IS_EXT3_SB(sb) && !IS_EXT2_SB(sb) &&
  3919. ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
  3920. set_opt(sb, DELALLOC);
  3921. set_opt(sb, DIOREAD_NOLOCK);
  3922. }
  3923. static int ext4_handle_clustersize(struct super_block *sb)
  3924. {
  3925. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3926. struct ext4_super_block *es = sbi->s_es;
  3927. int clustersize;
  3928. /* Handle clustersize */
  3929. clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
  3930. if (ext4_has_feature_bigalloc(sb)) {
  3931. if (clustersize < sb->s_blocksize) {
  3932. ext4_msg(sb, KERN_ERR,
  3933. "cluster size (%d) smaller than "
  3934. "block size (%lu)", clustersize, sb->s_blocksize);
  3935. return -EINVAL;
  3936. }
  3937. sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
  3938. le32_to_cpu(es->s_log_block_size);
  3939. } else {
  3940. if (clustersize != sb->s_blocksize) {
  3941. ext4_msg(sb, KERN_ERR,
  3942. "fragment/cluster size (%d) != "
  3943. "block size (%lu)", clustersize, sb->s_blocksize);
  3944. return -EINVAL;
  3945. }
  3946. if (sbi->s_blocks_per_group > sb->s_blocksize * 8) {
  3947. ext4_msg(sb, KERN_ERR,
  3948. "#blocks per group too big: %lu",
  3949. sbi->s_blocks_per_group);
  3950. return -EINVAL;
  3951. }
  3952. sbi->s_cluster_bits = 0;
  3953. }
  3954. sbi->s_clusters_per_group = le32_to_cpu(es->s_clusters_per_group);
  3955. if (sbi->s_clusters_per_group > sb->s_blocksize * 8) {
  3956. ext4_msg(sb, KERN_ERR, "#clusters per group too big: %lu",
  3957. sbi->s_clusters_per_group);
  3958. return -EINVAL;
  3959. }
  3960. if (sbi->s_blocks_per_group !=
  3961. (sbi->s_clusters_per_group * (clustersize / sb->s_blocksize))) {
  3962. ext4_msg(sb, KERN_ERR,
  3963. "blocks per group (%lu) and clusters per group (%lu) inconsistent",
  3964. sbi->s_blocks_per_group, sbi->s_clusters_per_group);
  3965. return -EINVAL;
  3966. }
  3967. sbi->s_cluster_ratio = clustersize / sb->s_blocksize;
  3968. /* Do we have standard group size of clustersize * 8 blocks ? */
  3969. if (sbi->s_blocks_per_group == clustersize << 3)
  3970. set_opt2(sb, STD_GROUP_SIZE);
  3971. return 0;
  3972. }
  3973. /*
  3974. * ext4_atomic_write_init: Initializes filesystem min & max atomic write units.
  3975. * With non-bigalloc filesystem awu will be based upon filesystem blocksize
  3976. * & bdev awu units.
  3977. * With bigalloc it will be based upon bigalloc cluster size & bdev awu units.
  3978. * @sb: super block
  3979. */
  3980. static void ext4_atomic_write_init(struct super_block *sb)
  3981. {
  3982. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3983. struct block_device *bdev = sb->s_bdev;
  3984. unsigned int clustersize = EXT4_CLUSTER_SIZE(sb);
  3985. if (!bdev_can_atomic_write(bdev))
  3986. return;
  3987. if (!ext4_has_feature_extents(sb))
  3988. return;
  3989. sbi->s_awu_min = max(sb->s_blocksize,
  3990. bdev_atomic_write_unit_min_bytes(bdev));
  3991. sbi->s_awu_max = min(clustersize,
  3992. bdev_atomic_write_unit_max_bytes(bdev));
  3993. if (sbi->s_awu_min && sbi->s_awu_max &&
  3994. sbi->s_awu_min <= sbi->s_awu_max) {
  3995. ext4_msg(sb, KERN_NOTICE, "Supports (experimental) DIO atomic writes awu_min: %u, awu_max: %u",
  3996. sbi->s_awu_min, sbi->s_awu_max);
  3997. } else {
  3998. sbi->s_awu_min = 0;
  3999. sbi->s_awu_max = 0;
  4000. }
  4001. }
  4002. static void ext4_fast_commit_init(struct super_block *sb)
  4003. {
  4004. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4005. /* Initialize fast commit stuff */
  4006. atomic_set(&sbi->s_fc_subtid, 0);
  4007. INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_MAIN]);
  4008. INIT_LIST_HEAD(&sbi->s_fc_q[FC_Q_STAGING]);
  4009. INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_MAIN]);
  4010. INIT_LIST_HEAD(&sbi->s_fc_dentry_q[FC_Q_STAGING]);
  4011. sbi->s_fc_bytes = 0;
  4012. ext4_clear_mount_flag(sb, EXT4_MF_FC_INELIGIBLE);
  4013. sbi->s_fc_ineligible_tid = 0;
  4014. mutex_init(&sbi->s_fc_lock);
  4015. memset(&sbi->s_fc_stats, 0, sizeof(sbi->s_fc_stats));
  4016. sbi->s_fc_replay_state.fc_regions = NULL;
  4017. sbi->s_fc_replay_state.fc_regions_size = 0;
  4018. sbi->s_fc_replay_state.fc_regions_used = 0;
  4019. sbi->s_fc_replay_state.fc_regions_valid = 0;
  4020. sbi->s_fc_replay_state.fc_modified_inodes = NULL;
  4021. sbi->s_fc_replay_state.fc_modified_inodes_size = 0;
  4022. sbi->s_fc_replay_state.fc_modified_inodes_used = 0;
  4023. }
  4024. static int ext4_inode_info_init(struct super_block *sb,
  4025. struct ext4_super_block *es)
  4026. {
  4027. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4028. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  4029. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  4030. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  4031. } else {
  4032. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  4033. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  4034. if (sbi->s_first_ino < EXT4_GOOD_OLD_FIRST_INO) {
  4035. ext4_msg(sb, KERN_ERR, "invalid first ino: %u",
  4036. sbi->s_first_ino);
  4037. return -EINVAL;
  4038. }
  4039. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  4040. (!is_power_of_2(sbi->s_inode_size)) ||
  4041. (sbi->s_inode_size > sb->s_blocksize)) {
  4042. ext4_msg(sb, KERN_ERR,
  4043. "unsupported inode size: %d",
  4044. sbi->s_inode_size);
  4045. ext4_msg(sb, KERN_ERR, "blocksize: %lu", sb->s_blocksize);
  4046. return -EINVAL;
  4047. }
  4048. /*
  4049. * i_atime_extra is the last extra field available for
  4050. * [acm]times in struct ext4_inode. Checking for that
  4051. * field should suffice to ensure we have extra space
  4052. * for all three.
  4053. */
  4054. if (sbi->s_inode_size >= offsetof(struct ext4_inode, i_atime_extra) +
  4055. sizeof(((struct ext4_inode *)0)->i_atime_extra)) {
  4056. sb->s_time_gran = 1;
  4057. sb->s_time_max = EXT4_EXTRA_TIMESTAMP_MAX;
  4058. } else {
  4059. sb->s_time_gran = NSEC_PER_SEC;
  4060. sb->s_time_max = EXT4_NON_EXTRA_TIMESTAMP_MAX;
  4061. }
  4062. sb->s_time_min = EXT4_TIMESTAMP_MIN;
  4063. }
  4064. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
  4065. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  4066. EXT4_GOOD_OLD_INODE_SIZE;
  4067. if (ext4_has_feature_extra_isize(sb)) {
  4068. unsigned v, max = (sbi->s_inode_size -
  4069. EXT4_GOOD_OLD_INODE_SIZE);
  4070. v = le16_to_cpu(es->s_want_extra_isize);
  4071. if (v > max) {
  4072. ext4_msg(sb, KERN_ERR,
  4073. "bad s_want_extra_isize: %d", v);
  4074. return -EINVAL;
  4075. }
  4076. if (sbi->s_want_extra_isize < v)
  4077. sbi->s_want_extra_isize = v;
  4078. v = le16_to_cpu(es->s_min_extra_isize);
  4079. if (v > max) {
  4080. ext4_msg(sb, KERN_ERR,
  4081. "bad s_min_extra_isize: %d", v);
  4082. return -EINVAL;
  4083. }
  4084. if (sbi->s_want_extra_isize < v)
  4085. sbi->s_want_extra_isize = v;
  4086. }
  4087. }
  4088. return 0;
  4089. }
  4090. #if IS_ENABLED(CONFIG_UNICODE)
  4091. static int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
  4092. {
  4093. const struct ext4_sb_encodings *encoding_info;
  4094. struct unicode_map *encoding;
  4095. __u16 encoding_flags = le16_to_cpu(es->s_encoding_flags);
  4096. if (!ext4_has_feature_casefold(sb) || sb->s_encoding)
  4097. return 0;
  4098. encoding_info = ext4_sb_read_encoding(es);
  4099. if (!encoding_info) {
  4100. ext4_msg(sb, KERN_ERR,
  4101. "Encoding requested by superblock is unknown");
  4102. return -EINVAL;
  4103. }
  4104. encoding = utf8_load(encoding_info->version);
  4105. if (IS_ERR(encoding)) {
  4106. ext4_msg(sb, KERN_ERR,
  4107. "can't mount with superblock charset: %s-%u.%u.%u "
  4108. "not supported by the kernel. flags: 0x%x.",
  4109. encoding_info->name,
  4110. unicode_major(encoding_info->version),
  4111. unicode_minor(encoding_info->version),
  4112. unicode_rev(encoding_info->version),
  4113. encoding_flags);
  4114. return -EINVAL;
  4115. }
  4116. ext4_msg(sb, KERN_INFO,"Using encoding defined by superblock: "
  4117. "%s-%u.%u.%u with flags 0x%hx", encoding_info->name,
  4118. unicode_major(encoding_info->version),
  4119. unicode_minor(encoding_info->version),
  4120. unicode_rev(encoding_info->version),
  4121. encoding_flags);
  4122. sb->s_encoding = encoding;
  4123. sb->s_encoding_flags = encoding_flags;
  4124. return 0;
  4125. }
  4126. #else
  4127. static inline int ext4_encoding_init(struct super_block *sb, struct ext4_super_block *es)
  4128. {
  4129. return 0;
  4130. }
  4131. #endif
  4132. static int ext4_init_metadata_csum(struct super_block *sb, struct ext4_super_block *es)
  4133. {
  4134. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4135. /* Warn if metadata_csum and gdt_csum are both set. */
  4136. if (ext4_has_feature_metadata_csum(sb) &&
  4137. ext4_has_feature_gdt_csum(sb))
  4138. ext4_warning(sb, "metadata_csum and uninit_bg are "
  4139. "redundant flags; please run fsck.");
  4140. /* Check for a known checksum algorithm */
  4141. if (!ext4_verify_csum_type(sb, es)) {
  4142. ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
  4143. "unknown checksum algorithm.");
  4144. return -EINVAL;
  4145. }
  4146. ext4_setup_csum_trigger(sb, EXT4_JTR_ORPHAN_FILE,
  4147. ext4_orphan_file_block_trigger);
  4148. /* Check superblock checksum */
  4149. if (!ext4_superblock_csum_verify(sb, es)) {
  4150. ext4_msg(sb, KERN_ERR, "VFS: Found ext4 filesystem with "
  4151. "invalid superblock checksum. Run e2fsck?");
  4152. return -EFSBADCRC;
  4153. }
  4154. /* Precompute checksum seed for all metadata */
  4155. if (ext4_has_feature_csum_seed(sb))
  4156. sbi->s_csum_seed = le32_to_cpu(es->s_checksum_seed);
  4157. else if (ext4_has_feature_metadata_csum(sb) ||
  4158. ext4_has_feature_ea_inode(sb))
  4159. sbi->s_csum_seed = ext4_chksum(~0, es->s_uuid,
  4160. sizeof(es->s_uuid));
  4161. return 0;
  4162. }
  4163. static int ext4_check_feature_compatibility(struct super_block *sb,
  4164. struct ext4_super_block *es,
  4165. int silent)
  4166. {
  4167. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4168. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  4169. (ext4_has_compat_features(sb) ||
  4170. ext4_has_ro_compat_features(sb) ||
  4171. ext4_has_incompat_features(sb)))
  4172. ext4_msg(sb, KERN_WARNING,
  4173. "feature flags set on rev 0 fs, "
  4174. "running e2fsck is recommended");
  4175. if (es->s_creator_os == cpu_to_le32(EXT4_OS_HURD)) {
  4176. set_opt2(sb, HURD_COMPAT);
  4177. if (ext4_has_feature_64bit(sb)) {
  4178. ext4_msg(sb, KERN_ERR,
  4179. "The Hurd can't support 64-bit file systems");
  4180. return -EINVAL;
  4181. }
  4182. /*
  4183. * ea_inode feature uses l_i_version field which is not
  4184. * available in HURD_COMPAT mode.
  4185. */
  4186. if (ext4_has_feature_ea_inode(sb)) {
  4187. ext4_msg(sb, KERN_ERR,
  4188. "ea_inode feature is not supported for Hurd");
  4189. return -EINVAL;
  4190. }
  4191. }
  4192. if (IS_EXT2_SB(sb)) {
  4193. if (ext2_feature_set_ok(sb))
  4194. ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
  4195. "using the ext4 subsystem");
  4196. else {
  4197. /*
  4198. * If we're probing be silent, if this looks like
  4199. * it's actually an ext[34] filesystem.
  4200. */
  4201. if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
  4202. return -EINVAL;
  4203. ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
  4204. "to feature incompatibilities");
  4205. return -EINVAL;
  4206. }
  4207. }
  4208. if (IS_EXT3_SB(sb)) {
  4209. if (ext3_feature_set_ok(sb))
  4210. ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
  4211. "using the ext4 subsystem");
  4212. else {
  4213. /*
  4214. * If we're probing be silent, if this looks like
  4215. * it's actually an ext4 filesystem.
  4216. */
  4217. if (silent && ext4_feature_set_ok(sb, sb_rdonly(sb)))
  4218. return -EINVAL;
  4219. ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
  4220. "to feature incompatibilities");
  4221. return -EINVAL;
  4222. }
  4223. }
  4224. /*
  4225. * Check feature flags regardless of the revision level, since we
  4226. * previously didn't change the revision level when setting the flags,
  4227. * so there is a chance incompat flags are set on a rev 0 filesystem.
  4228. */
  4229. if (!ext4_feature_set_ok(sb, (sb_rdonly(sb))))
  4230. return -EINVAL;
  4231. if (sbi->s_daxdev) {
  4232. if (sb->s_blocksize == PAGE_SIZE)
  4233. set_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags);
  4234. else
  4235. ext4_msg(sb, KERN_ERR, "unsupported blocksize for DAX\n");
  4236. }
  4237. if (sbi->s_mount_opt & EXT4_MOUNT_DAX_ALWAYS) {
  4238. if (ext4_has_feature_inline_data(sb)) {
  4239. ext4_msg(sb, KERN_ERR, "Cannot use DAX on a filesystem"
  4240. " that may contain inline data");
  4241. return -EINVAL;
  4242. }
  4243. if (!test_bit(EXT4_FLAGS_BDEV_IS_DAX, &sbi->s_ext4_flags)) {
  4244. ext4_msg(sb, KERN_ERR,
  4245. "DAX unsupported by block device.");
  4246. return -EINVAL;
  4247. }
  4248. }
  4249. if (ext4_has_feature_encrypt(sb) && es->s_encryption_level) {
  4250. ext4_msg(sb, KERN_ERR, "Unsupported encryption level %d",
  4251. es->s_encryption_level);
  4252. return -EINVAL;
  4253. }
  4254. return 0;
  4255. }
  4256. static int ext4_check_geometry(struct super_block *sb,
  4257. struct ext4_super_block *es)
  4258. {
  4259. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4260. __u64 blocks_count;
  4261. int err;
  4262. if (le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) > (sb->s_blocksize / 4)) {
  4263. ext4_msg(sb, KERN_ERR,
  4264. "Number of reserved GDT blocks insanely large: %d",
  4265. le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks));
  4266. return -EINVAL;
  4267. }
  4268. /*
  4269. * Test whether we have more sectors than will fit in sector_t,
  4270. * and whether the max offset is addressable by the page cache.
  4271. */
  4272. err = generic_check_addressable(sb->s_blocksize_bits,
  4273. ext4_blocks_count(es));
  4274. if (err) {
  4275. ext4_msg(sb, KERN_ERR, "filesystem"
  4276. " too large to mount safely on this system");
  4277. return err;
  4278. }
  4279. /* check blocks count against device size */
  4280. blocks_count = sb_bdev_nr_blocks(sb);
  4281. if (blocks_count && ext4_blocks_count(es) > blocks_count) {
  4282. ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
  4283. "exceeds size of device (%llu blocks)",
  4284. ext4_blocks_count(es), blocks_count);
  4285. return -EINVAL;
  4286. }
  4287. /*
  4288. * It makes no sense for the first data block to be beyond the end
  4289. * of the filesystem.
  4290. */
  4291. if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
  4292. ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
  4293. "block %u is beyond end of filesystem (%llu)",
  4294. le32_to_cpu(es->s_first_data_block),
  4295. ext4_blocks_count(es));
  4296. return -EINVAL;
  4297. }
  4298. if ((es->s_first_data_block == 0) && (es->s_log_block_size == 0) &&
  4299. (sbi->s_cluster_ratio == 1)) {
  4300. ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
  4301. "block is 0 with a 1k block and cluster size");
  4302. return -EINVAL;
  4303. }
  4304. blocks_count = (ext4_blocks_count(es) -
  4305. le32_to_cpu(es->s_first_data_block) +
  4306. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  4307. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  4308. if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
  4309. ext4_msg(sb, KERN_WARNING, "groups count too large: %llu "
  4310. "(block count %llu, first data block %u, "
  4311. "blocks per group %lu)", blocks_count,
  4312. ext4_blocks_count(es),
  4313. le32_to_cpu(es->s_first_data_block),
  4314. EXT4_BLOCKS_PER_GROUP(sb));
  4315. return -EINVAL;
  4316. }
  4317. sbi->s_groups_count = blocks_count;
  4318. sbi->s_blockfile_groups = min(sbi->s_groups_count,
  4319. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  4320. if (((u64)sbi->s_groups_count * sbi->s_inodes_per_group) !=
  4321. le32_to_cpu(es->s_inodes_count)) {
  4322. ext4_msg(sb, KERN_ERR, "inodes count not valid: %u vs %llu",
  4323. le32_to_cpu(es->s_inodes_count),
  4324. ((u64)sbi->s_groups_count * sbi->s_inodes_per_group));
  4325. return -EINVAL;
  4326. }
  4327. return 0;
  4328. }
  4329. static int ext4_group_desc_init(struct super_block *sb,
  4330. struct ext4_super_block *es,
  4331. ext4_fsblk_t logical_sb_block,
  4332. ext4_group_t *first_not_zeroed)
  4333. {
  4334. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4335. unsigned int db_count;
  4336. ext4_fsblk_t block;
  4337. int i;
  4338. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  4339. EXT4_DESC_PER_BLOCK(sb);
  4340. if (ext4_has_feature_meta_bg(sb)) {
  4341. if (le32_to_cpu(es->s_first_meta_bg) > db_count) {
  4342. ext4_msg(sb, KERN_WARNING,
  4343. "first meta block group too large: %u "
  4344. "(group descriptor block count %u)",
  4345. le32_to_cpu(es->s_first_meta_bg), db_count);
  4346. return -EINVAL;
  4347. }
  4348. }
  4349. rcu_assign_pointer(sbi->s_group_desc,
  4350. kvmalloc_objs(struct buffer_head *, db_count));
  4351. if (sbi->s_group_desc == NULL) {
  4352. ext4_msg(sb, KERN_ERR, "not enough memory");
  4353. return -ENOMEM;
  4354. }
  4355. bgl_lock_init(sbi->s_blockgroup_lock);
  4356. /* Pre-read the descriptors into the buffer cache */
  4357. for (i = 0; i < db_count; i++) {
  4358. block = descriptor_loc(sb, logical_sb_block, i);
  4359. ext4_sb_breadahead_unmovable(sb, block);
  4360. }
  4361. for (i = 0; i < db_count; i++) {
  4362. struct buffer_head *bh;
  4363. block = descriptor_loc(sb, logical_sb_block, i);
  4364. bh = ext4_sb_bread_unmovable(sb, block);
  4365. if (IS_ERR(bh)) {
  4366. ext4_msg(sb, KERN_ERR,
  4367. "can't read group descriptor %d", i);
  4368. sbi->s_gdb_count = i;
  4369. return PTR_ERR(bh);
  4370. }
  4371. rcu_read_lock();
  4372. rcu_dereference(sbi->s_group_desc)[i] = bh;
  4373. rcu_read_unlock();
  4374. }
  4375. sbi->s_gdb_count = db_count;
  4376. if (!ext4_check_descriptors(sb, logical_sb_block, first_not_zeroed)) {
  4377. ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
  4378. return -EFSCORRUPTED;
  4379. }
  4380. return 0;
  4381. }
  4382. static int ext4_load_and_init_journal(struct super_block *sb,
  4383. struct ext4_super_block *es,
  4384. struct ext4_fs_context *ctx)
  4385. {
  4386. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4387. int err;
  4388. err = ext4_load_journal(sb, es, ctx->journal_devnum);
  4389. if (err)
  4390. return err;
  4391. if (ext4_has_feature_64bit(sb) &&
  4392. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  4393. JBD2_FEATURE_INCOMPAT_64BIT)) {
  4394. ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
  4395. goto out;
  4396. }
  4397. if (!set_journal_csum_feature_set(sb)) {
  4398. ext4_msg(sb, KERN_ERR, "Failed to set journal checksum "
  4399. "feature set");
  4400. goto out;
  4401. }
  4402. if (test_opt2(sb, JOURNAL_FAST_COMMIT) &&
  4403. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  4404. JBD2_FEATURE_INCOMPAT_FAST_COMMIT)) {
  4405. ext4_msg(sb, KERN_ERR,
  4406. "Failed to set fast commit journal feature");
  4407. goto out;
  4408. }
  4409. /* We have now updated the journal if required, so we can
  4410. * validate the data journaling mode. */
  4411. switch (test_opt(sb, DATA_FLAGS)) {
  4412. case 0:
  4413. /* No mode set, assume a default based on the journal
  4414. * capabilities: ORDERED_DATA if the journal can
  4415. * cope, else JOURNAL_DATA
  4416. */
  4417. if (jbd2_journal_check_available_features
  4418. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  4419. set_opt(sb, ORDERED_DATA);
  4420. sbi->s_def_mount_opt |= EXT4_MOUNT_ORDERED_DATA;
  4421. } else {
  4422. set_opt(sb, JOURNAL_DATA);
  4423. sbi->s_def_mount_opt |= EXT4_MOUNT_JOURNAL_DATA;
  4424. }
  4425. break;
  4426. case EXT4_MOUNT_ORDERED_DATA:
  4427. case EXT4_MOUNT_WRITEBACK_DATA:
  4428. if (!jbd2_journal_check_available_features
  4429. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  4430. ext4_msg(sb, KERN_ERR, "Journal does not support "
  4431. "requested data journaling mode");
  4432. goto out;
  4433. }
  4434. break;
  4435. default:
  4436. break;
  4437. }
  4438. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA &&
  4439. test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  4440. ext4_msg(sb, KERN_ERR, "can't mount with "
  4441. "journal_async_commit in data=ordered mode");
  4442. goto out;
  4443. }
  4444. set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
  4445. sbi->s_journal->j_submit_inode_data_buffers =
  4446. ext4_journal_submit_inode_data_buffers;
  4447. sbi->s_journal->j_finish_inode_data_buffers =
  4448. ext4_journal_finish_inode_data_buffers;
  4449. return 0;
  4450. out:
  4451. ext4_journal_destroy(sbi, sbi->s_journal);
  4452. return -EINVAL;
  4453. }
  4454. static int ext4_check_journal_data_mode(struct super_block *sb)
  4455. {
  4456. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  4457. printk_once(KERN_WARNING "EXT4-fs: Warning: mounting with "
  4458. "data=journal disables delayed allocation, "
  4459. "dioread_nolock, O_DIRECT and fast_commit support!\n");
  4460. /* can't mount with both data=journal and dioread_nolock. */
  4461. clear_opt(sb, DIOREAD_NOLOCK);
  4462. clear_opt2(sb, JOURNAL_FAST_COMMIT);
  4463. if (test_opt2(sb, EXPLICIT_DELALLOC)) {
  4464. ext4_msg(sb, KERN_ERR, "can't mount with "
  4465. "both data=journal and delalloc");
  4466. return -EINVAL;
  4467. }
  4468. if (test_opt(sb, DAX_ALWAYS)) {
  4469. ext4_msg(sb, KERN_ERR, "can't mount with "
  4470. "both data=journal and dax");
  4471. return -EINVAL;
  4472. }
  4473. if (ext4_has_feature_encrypt(sb)) {
  4474. ext4_msg(sb, KERN_WARNING,
  4475. "encrypted files will use data=ordered "
  4476. "instead of data journaling mode");
  4477. }
  4478. if (test_opt(sb, DELALLOC))
  4479. clear_opt(sb, DELALLOC);
  4480. } else {
  4481. sb->s_iflags |= SB_I_CGROUPWB;
  4482. }
  4483. return 0;
  4484. }
  4485. static const char *ext4_has_journal_option(struct super_block *sb)
  4486. {
  4487. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4488. if (test_opt(sb, JOURNAL_ASYNC_COMMIT))
  4489. return "journal_async_commit";
  4490. if (test_opt2(sb, EXPLICIT_JOURNAL_CHECKSUM))
  4491. return "journal_checksum";
  4492. if (sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
  4493. return "commit=";
  4494. if (EXT4_MOUNT_DATA_FLAGS &
  4495. (sbi->s_mount_opt ^ sbi->s_def_mount_opt))
  4496. return "data=";
  4497. if (test_opt(sb, DATA_ERR_ABORT))
  4498. return "data_err=abort";
  4499. return NULL;
  4500. }
  4501. /*
  4502. * Limit the maximum folio order to 2048 blocks to prevent overestimation
  4503. * of reserve handle credits during the folio writeback in environments
  4504. * where the PAGE_SIZE exceeds 4KB.
  4505. */
  4506. #define EXT4_MAX_PAGECACHE_ORDER(sb) \
  4507. umin(MAX_PAGECACHE_ORDER, (11 + (sb)->s_blocksize_bits - PAGE_SHIFT))
  4508. static void ext4_set_max_mapping_order(struct super_block *sb)
  4509. {
  4510. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4511. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  4512. sbi->s_max_folio_order = sbi->s_min_folio_order;
  4513. else
  4514. sbi->s_max_folio_order = EXT4_MAX_PAGECACHE_ORDER(sb);
  4515. }
  4516. static int ext4_check_large_folio(struct super_block *sb)
  4517. {
  4518. const char *err_str = NULL;
  4519. if (ext4_has_feature_encrypt(sb))
  4520. err_str = "encrypt";
  4521. if (!err_str) {
  4522. ext4_set_max_mapping_order(sb);
  4523. } else if (sb->s_blocksize > PAGE_SIZE) {
  4524. ext4_msg(sb, KERN_ERR, "bs(%lu) > ps(%lu) unsupported for %s",
  4525. sb->s_blocksize, PAGE_SIZE, err_str);
  4526. return -EINVAL;
  4527. }
  4528. return 0;
  4529. }
  4530. static int ext4_load_super(struct super_block *sb, ext4_fsblk_t *lsb,
  4531. int silent)
  4532. {
  4533. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4534. struct ext4_super_block *es;
  4535. ext4_fsblk_t logical_sb_block;
  4536. unsigned long offset = 0;
  4537. struct buffer_head *bh;
  4538. int ret = -EINVAL;
  4539. int blocksize;
  4540. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  4541. if (!blocksize) {
  4542. ext4_msg(sb, KERN_ERR, "unable to set blocksize");
  4543. return -EINVAL;
  4544. }
  4545. /*
  4546. * The ext4 superblock will not be buffer aligned for other than 1kB
  4547. * block sizes. We need to calculate the offset from buffer start.
  4548. */
  4549. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  4550. logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
  4551. offset = do_div(logical_sb_block, blocksize);
  4552. } else {
  4553. logical_sb_block = sbi->s_sb_block;
  4554. }
  4555. bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
  4556. if (IS_ERR(bh)) {
  4557. ext4_msg(sb, KERN_ERR, "unable to read superblock");
  4558. return PTR_ERR(bh);
  4559. }
  4560. /*
  4561. * Note: s_es must be initialized as soon as possible because
  4562. * some ext4 macro-instructions depend on its value
  4563. */
  4564. es = (struct ext4_super_block *) (bh->b_data + offset);
  4565. sbi->s_es = es;
  4566. sb->s_magic = le16_to_cpu(es->s_magic);
  4567. if (sb->s_magic != EXT4_SUPER_MAGIC) {
  4568. if (!silent)
  4569. ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
  4570. goto out;
  4571. }
  4572. if (le32_to_cpu(es->s_log_block_size) >
  4573. (EXT4_MAX_BLOCK_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
  4574. ext4_msg(sb, KERN_ERR,
  4575. "Invalid log block size: %u",
  4576. le32_to_cpu(es->s_log_block_size));
  4577. goto out;
  4578. }
  4579. if (le32_to_cpu(es->s_log_cluster_size) >
  4580. (EXT4_MAX_CLUSTER_LOG_SIZE - EXT4_MIN_BLOCK_LOG_SIZE)) {
  4581. ext4_msg(sb, KERN_ERR,
  4582. "Invalid log cluster size: %u",
  4583. le32_to_cpu(es->s_log_cluster_size));
  4584. goto out;
  4585. }
  4586. blocksize = EXT4_MIN_BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  4587. /*
  4588. * If the default block size is not the same as the real block size,
  4589. * we need to reload it.
  4590. */
  4591. if (sb->s_blocksize == blocksize)
  4592. goto success;
  4593. /*
  4594. * bh must be released before kill_bdev(), otherwise
  4595. * it won't be freed and its page also. kill_bdev()
  4596. * is called by sb_set_blocksize().
  4597. */
  4598. brelse(bh);
  4599. /* Validate the filesystem blocksize */
  4600. if (!sb_set_blocksize(sb, blocksize)) {
  4601. ext4_msg(sb, KERN_ERR, "bad block size %d",
  4602. blocksize);
  4603. bh = NULL;
  4604. goto out;
  4605. }
  4606. logical_sb_block = sbi->s_sb_block * EXT4_MIN_BLOCK_SIZE;
  4607. offset = do_div(logical_sb_block, blocksize);
  4608. bh = ext4_sb_bread_unmovable(sb, logical_sb_block);
  4609. if (IS_ERR(bh)) {
  4610. ext4_msg(sb, KERN_ERR, "Can't read superblock on 2nd try");
  4611. ret = PTR_ERR(bh);
  4612. bh = NULL;
  4613. goto out;
  4614. }
  4615. es = (struct ext4_super_block *)(bh->b_data + offset);
  4616. sbi->s_es = es;
  4617. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  4618. ext4_msg(sb, KERN_ERR, "Magic mismatch, very weird!");
  4619. goto out;
  4620. }
  4621. success:
  4622. sbi->s_min_folio_order = get_order(blocksize);
  4623. *lsb = logical_sb_block;
  4624. sbi->s_sbh = bh;
  4625. return 0;
  4626. out:
  4627. brelse(bh);
  4628. return ret;
  4629. }
  4630. static int ext4_hash_info_init(struct super_block *sb)
  4631. {
  4632. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4633. struct ext4_super_block *es = sbi->s_es;
  4634. unsigned int i;
  4635. sbi->s_def_hash_version = es->s_def_hash_version;
  4636. if (sbi->s_def_hash_version > DX_HASH_LAST) {
  4637. ext4_msg(sb, KERN_ERR,
  4638. "Invalid default hash set in the superblock");
  4639. return -EINVAL;
  4640. } else if (sbi->s_def_hash_version == DX_HASH_SIPHASH) {
  4641. ext4_msg(sb, KERN_ERR,
  4642. "SIPHASH is not a valid default hash value");
  4643. return -EINVAL;
  4644. }
  4645. for (i = 0; i < 4; i++)
  4646. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  4647. if (ext4_has_feature_dir_index(sb)) {
  4648. i = le32_to_cpu(es->s_flags);
  4649. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  4650. sbi->s_hash_unsigned = 3;
  4651. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  4652. #ifdef __CHAR_UNSIGNED__
  4653. if (!sb_rdonly(sb))
  4654. es->s_flags |=
  4655. cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  4656. sbi->s_hash_unsigned = 3;
  4657. #else
  4658. if (!sb_rdonly(sb))
  4659. es->s_flags |=
  4660. cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  4661. #endif
  4662. }
  4663. }
  4664. return 0;
  4665. }
  4666. static int ext4_block_group_meta_init(struct super_block *sb, int silent)
  4667. {
  4668. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4669. struct ext4_super_block *es = sbi->s_es;
  4670. int has_huge_files;
  4671. has_huge_files = ext4_has_feature_huge_file(sb);
  4672. sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
  4673. has_huge_files);
  4674. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
  4675. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  4676. if (ext4_has_feature_64bit(sb)) {
  4677. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  4678. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  4679. !is_power_of_2(sbi->s_desc_size)) {
  4680. ext4_msg(sb, KERN_ERR,
  4681. "unsupported descriptor size %lu",
  4682. sbi->s_desc_size);
  4683. return -EINVAL;
  4684. }
  4685. } else
  4686. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  4687. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  4688. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  4689. sbi->s_inodes_per_block = sb->s_blocksize / EXT4_INODE_SIZE(sb);
  4690. if (sbi->s_inodes_per_block == 0 || sbi->s_blocks_per_group == 0) {
  4691. if (!silent)
  4692. ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
  4693. return -EINVAL;
  4694. }
  4695. if (sbi->s_inodes_per_group < sbi->s_inodes_per_block ||
  4696. sbi->s_inodes_per_group > sb->s_blocksize * 8) {
  4697. ext4_msg(sb, KERN_ERR, "invalid inodes per group: %lu\n",
  4698. sbi->s_inodes_per_group);
  4699. return -EINVAL;
  4700. }
  4701. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  4702. sbi->s_inodes_per_block;
  4703. sbi->s_desc_per_block = sb->s_blocksize / EXT4_DESC_SIZE(sb);
  4704. sbi->s_mount_state = le16_to_cpu(es->s_state) & ~EXT4_FC_REPLAY;
  4705. sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
  4706. sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
  4707. return 0;
  4708. }
  4709. /*
  4710. * It's hard to get stripe aligned blocks if stripe is not aligned with
  4711. * cluster, just disable stripe and alert user to simplify code and avoid
  4712. * stripe aligned allocation which will rarely succeed.
  4713. */
  4714. static bool ext4_is_stripe_incompatible(struct super_block *sb, unsigned long stripe)
  4715. {
  4716. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4717. return (stripe > 0 && sbi->s_cluster_ratio > 1 &&
  4718. stripe % sbi->s_cluster_ratio != 0);
  4719. }
  4720. static int __ext4_fill_super(struct fs_context *fc, struct super_block *sb)
  4721. {
  4722. struct ext4_super_block *es = NULL;
  4723. struct ext4_sb_info *sbi = EXT4_SB(sb);
  4724. ext4_fsblk_t logical_sb_block;
  4725. struct inode *root;
  4726. int needs_recovery;
  4727. int err;
  4728. ext4_group_t first_not_zeroed;
  4729. struct ext4_fs_context *ctx = fc->fs_private;
  4730. int silent = fc->sb_flags & SB_SILENT;
  4731. /* Set defaults for the variables that will be set during parsing */
  4732. if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO))
  4733. ctx->journal_ioprio = EXT4_DEF_JOURNAL_IOPRIO;
  4734. sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
  4735. sbi->s_sectors_written_start =
  4736. part_stat_read(sb->s_bdev, sectors[STAT_WRITE]);
  4737. err = ext4_load_super(sb, &logical_sb_block, silent);
  4738. if (err)
  4739. goto out_fail;
  4740. es = sbi->s_es;
  4741. sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
  4742. err = ext4_init_metadata_csum(sb, es);
  4743. if (err)
  4744. goto failed_mount;
  4745. ext4_set_def_opts(sb, es);
  4746. sbi->s_resuid = make_kuid(&init_user_ns, ext4_get_resuid(es));
  4747. sbi->s_resgid = make_kgid(&init_user_ns, ext4_get_resuid(es));
  4748. sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
  4749. sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
  4750. sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
  4751. sbi->s_sb_update_kb = EXT4_DEF_SB_UPDATE_INTERVAL_KB;
  4752. sbi->s_sb_update_sec = EXT4_DEF_SB_UPDATE_INTERVAL_SEC;
  4753. /*
  4754. * set default s_li_wait_mult for lazyinit, for the case there is
  4755. * no mount option specified.
  4756. */
  4757. sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
  4758. err = ext4_inode_info_init(sb, es);
  4759. if (err)
  4760. goto failed_mount;
  4761. err = parse_apply_sb_mount_options(sb, ctx);
  4762. if (err < 0)
  4763. goto failed_mount;
  4764. sbi->s_def_mount_opt = sbi->s_mount_opt;
  4765. sbi->s_def_mount_opt2 = sbi->s_mount_opt2;
  4766. err = ext4_check_opt_consistency(fc, sb);
  4767. if (err < 0)
  4768. goto failed_mount;
  4769. ext4_apply_options(fc, sb);
  4770. err = ext4_check_large_folio(sb);
  4771. if (err < 0)
  4772. goto failed_mount;
  4773. err = ext4_encoding_init(sb, es);
  4774. if (err)
  4775. goto failed_mount;
  4776. err = ext4_check_journal_data_mode(sb);
  4777. if (err)
  4778. goto failed_mount;
  4779. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  4780. (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
  4781. /* HSM events are allowed by default. */
  4782. sb->s_iflags |= SB_I_ALLOW_HSM;
  4783. err = ext4_check_feature_compatibility(sb, es, silent);
  4784. if (err)
  4785. goto failed_mount;
  4786. err = ext4_block_group_meta_init(sb, silent);
  4787. if (err)
  4788. goto failed_mount;
  4789. err = ext4_hash_info_init(sb);
  4790. if (err)
  4791. goto failed_mount;
  4792. err = ext4_handle_clustersize(sb);
  4793. if (err)
  4794. goto failed_mount;
  4795. err = ext4_check_geometry(sb, es);
  4796. if (err)
  4797. goto failed_mount;
  4798. timer_setup(&sbi->s_err_report, print_daily_error_info, 0);
  4799. spin_lock_init(&sbi->s_error_lock);
  4800. mutex_init(&sbi->s_error_notify_mutex);
  4801. INIT_WORK(&sbi->s_sb_upd_work, update_super_work);
  4802. err = ext4_group_desc_init(sb, es, logical_sb_block, &first_not_zeroed);
  4803. if (err)
  4804. goto failed_mount3;
  4805. err = ext4_es_register_shrinker(sbi);
  4806. if (err)
  4807. goto failed_mount3;
  4808. sbi->s_stripe = ext4_get_stripe_size(sbi);
  4809. if (ext4_is_stripe_incompatible(sb, sbi->s_stripe)) {
  4810. ext4_msg(sb, KERN_WARNING,
  4811. "stripe (%lu) is not aligned with cluster size (%u), "
  4812. "stripe is disabled",
  4813. sbi->s_stripe, sbi->s_cluster_ratio);
  4814. sbi->s_stripe = 0;
  4815. }
  4816. sbi->s_extent_max_zeroout_kb = 32;
  4817. /*
  4818. * set up enough so that it can read an inode
  4819. */
  4820. sb->s_op = &ext4_sops;
  4821. sb->s_export_op = &ext4_export_ops;
  4822. sb->s_xattr = ext4_xattr_handlers;
  4823. #ifdef CONFIG_FS_ENCRYPTION
  4824. sb->s_cop = &ext4_cryptops;
  4825. #endif
  4826. #ifdef CONFIG_FS_VERITY
  4827. sb->s_vop = &ext4_verityops;
  4828. #endif
  4829. #ifdef CONFIG_QUOTA
  4830. sb->dq_op = &ext4_quota_operations;
  4831. if (ext4_has_feature_quota(sb))
  4832. sb->s_qcop = &dquot_quotactl_sysfile_ops;
  4833. else
  4834. sb->s_qcop = &ext4_qctl_operations;
  4835. sb->s_quota_types = QTYPE_MASK_USR | QTYPE_MASK_GRP | QTYPE_MASK_PRJ;
  4836. #endif
  4837. super_set_uuid(sb, es->s_uuid, sizeof(es->s_uuid));
  4838. super_set_sysfs_name_bdev(sb);
  4839. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  4840. mutex_init(&sbi->s_orphan_lock);
  4841. spin_lock_init(&sbi->s_bdev_wb_lock);
  4842. ext4_atomic_write_init(sb);
  4843. ext4_fast_commit_init(sb);
  4844. sb->s_root = NULL;
  4845. needs_recovery = (es->s_last_orphan != 0 ||
  4846. ext4_has_feature_orphan_present(sb) ||
  4847. ext4_has_feature_journal_needs_recovery(sb));
  4848. if (ext4_has_feature_mmp(sb) && !sb_rdonly(sb)) {
  4849. err = ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block));
  4850. if (err)
  4851. goto failed_mount3a;
  4852. }
  4853. err = -EINVAL;
  4854. /*
  4855. * The first inode we look at is the journal inode. Don't try
  4856. * root first: it may be modified in the journal!
  4857. */
  4858. if (!test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb)) {
  4859. err = ext4_load_and_init_journal(sb, es, ctx);
  4860. if (err)
  4861. goto failed_mount3a;
  4862. if (bdev_read_only(sb->s_bdev))
  4863. needs_recovery = 0;
  4864. } else if (test_opt(sb, NOLOAD) && !sb_rdonly(sb) &&
  4865. ext4_has_feature_journal_needs_recovery(sb)) {
  4866. ext4_msg(sb, KERN_ERR, "required journal recovery "
  4867. "suppressed and not mounted read-only");
  4868. goto failed_mount3a;
  4869. } else {
  4870. const char *journal_option;
  4871. /* Nojournal mode, all journal mount options are illegal */
  4872. journal_option = ext4_has_journal_option(sb);
  4873. if (journal_option != NULL) {
  4874. ext4_msg(sb, KERN_ERR,
  4875. "can't mount with %s, fs mounted w/o journal",
  4876. journal_option);
  4877. goto failed_mount3a;
  4878. }
  4879. sbi->s_def_mount_opt &= ~EXT4_MOUNT_JOURNAL_CHECKSUM;
  4880. clear_opt(sb, JOURNAL_CHECKSUM);
  4881. clear_opt(sb, DATA_FLAGS);
  4882. clear_opt2(sb, JOURNAL_FAST_COMMIT);
  4883. sbi->s_journal = NULL;
  4884. needs_recovery = 0;
  4885. }
  4886. if (!test_opt(sb, NO_MBCACHE)) {
  4887. sbi->s_ea_block_cache = ext4_xattr_create_cache();
  4888. if (!sbi->s_ea_block_cache) {
  4889. ext4_msg(sb, KERN_ERR,
  4890. "Failed to create ea_block_cache");
  4891. err = -EINVAL;
  4892. goto failed_mount_wq;
  4893. }
  4894. if (ext4_has_feature_ea_inode(sb)) {
  4895. sbi->s_ea_inode_cache = ext4_xattr_create_cache();
  4896. if (!sbi->s_ea_inode_cache) {
  4897. ext4_msg(sb, KERN_ERR,
  4898. "Failed to create ea_inode_cache");
  4899. err = -EINVAL;
  4900. goto failed_mount_wq;
  4901. }
  4902. }
  4903. }
  4904. /*
  4905. * Get the # of file system overhead blocks from the
  4906. * superblock if present.
  4907. */
  4908. sbi->s_overhead = le32_to_cpu(es->s_overhead_clusters);
  4909. /* ignore the precalculated value if it is ridiculous */
  4910. if (sbi->s_overhead > ext4_blocks_count(es))
  4911. sbi->s_overhead = 0;
  4912. /*
  4913. * If the bigalloc feature is not enabled recalculating the
  4914. * overhead doesn't take long, so we might as well just redo
  4915. * it to make sure we are using the correct value.
  4916. */
  4917. if (!ext4_has_feature_bigalloc(sb))
  4918. sbi->s_overhead = 0;
  4919. if (sbi->s_overhead == 0) {
  4920. err = ext4_calculate_overhead(sb);
  4921. if (err)
  4922. goto failed_mount_wq;
  4923. }
  4924. /*
  4925. * The maximum number of concurrent works can be high and
  4926. * concurrency isn't really necessary. Limit it to 1.
  4927. */
  4928. EXT4_SB(sb)->rsv_conversion_wq =
  4929. alloc_workqueue("ext4-rsv-conversion", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  4930. if (!EXT4_SB(sb)->rsv_conversion_wq) {
  4931. printk(KERN_ERR "EXT4-fs: failed to create workqueue\n");
  4932. err = -ENOMEM;
  4933. goto failed_mount4;
  4934. }
  4935. /*
  4936. * The jbd2_journal_load will have done any necessary log recovery,
  4937. * so we can safely mount the rest of the filesystem now.
  4938. */
  4939. root = ext4_iget(sb, EXT4_ROOT_INO, EXT4_IGET_SPECIAL);
  4940. if (IS_ERR(root)) {
  4941. ext4_msg(sb, KERN_ERR, "get root inode failed");
  4942. err = PTR_ERR(root);
  4943. root = NULL;
  4944. goto failed_mount4;
  4945. }
  4946. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  4947. ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
  4948. iput(root);
  4949. err = -EFSCORRUPTED;
  4950. goto failed_mount4;
  4951. }
  4952. generic_set_sb_d_ops(sb);
  4953. sb->s_root = d_make_root(root);
  4954. if (!sb->s_root) {
  4955. ext4_msg(sb, KERN_ERR, "get root dentry failed");
  4956. err = -ENOMEM;
  4957. goto failed_mount4;
  4958. }
  4959. err = ext4_setup_super(sb, es, sb_rdonly(sb));
  4960. if (err == -EROFS) {
  4961. sb->s_flags |= SB_RDONLY;
  4962. } else if (err)
  4963. goto failed_mount4a;
  4964. ext4_set_resv_clusters(sb);
  4965. if (test_opt(sb, BLOCK_VALIDITY)) {
  4966. err = ext4_setup_system_zone(sb);
  4967. if (err) {
  4968. ext4_msg(sb, KERN_ERR, "failed to initialize system "
  4969. "zone (%d)", err);
  4970. goto failed_mount4a;
  4971. }
  4972. }
  4973. ext4_fc_replay_cleanup(sb);
  4974. ext4_ext_init(sb);
  4975. /*
  4976. * Enable optimize_scan if number of groups is > threshold. This can be
  4977. * turned off by passing "mb_optimize_scan=0". This can also be
  4978. * turned on forcefully by passing "mb_optimize_scan=1".
  4979. */
  4980. if (!(ctx->spec & EXT4_SPEC_mb_optimize_scan)) {
  4981. if (sbi->s_groups_count >= MB_DEFAULT_LINEAR_SCAN_THRESHOLD)
  4982. set_opt2(sb, MB_OPTIMIZE_SCAN);
  4983. else
  4984. clear_opt2(sb, MB_OPTIMIZE_SCAN);
  4985. }
  4986. err = ext4_percpu_param_init(sbi);
  4987. if (err)
  4988. goto failed_mount5;
  4989. err = ext4_mb_init(sb);
  4990. if (err) {
  4991. ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
  4992. err);
  4993. goto failed_mount5;
  4994. }
  4995. /*
  4996. * We can only set up the journal commit callback once
  4997. * mballoc is initialized
  4998. */
  4999. if (sbi->s_journal)
  5000. sbi->s_journal->j_commit_callback =
  5001. ext4_journal_commit_callback;
  5002. if (ext4_has_feature_flex_bg(sb))
  5003. if (!ext4_fill_flex_info(sb)) {
  5004. ext4_msg(sb, KERN_ERR,
  5005. "unable to initialize "
  5006. "flex_bg meta info!");
  5007. err = -ENOMEM;
  5008. goto failed_mount6;
  5009. }
  5010. err = ext4_register_li_request(sb, first_not_zeroed);
  5011. if (err)
  5012. goto failed_mount6;
  5013. err = ext4_init_orphan_info(sb);
  5014. if (err)
  5015. goto failed_mount7;
  5016. #ifdef CONFIG_QUOTA
  5017. /* Enable quota usage during mount. */
  5018. if (ext4_has_feature_quota(sb) && !sb_rdonly(sb)) {
  5019. err = ext4_enable_quotas(sb);
  5020. if (err)
  5021. goto failed_mount8;
  5022. }
  5023. #endif /* CONFIG_QUOTA */
  5024. /*
  5025. * Save the original bdev mapping's wb_err value which could be
  5026. * used to detect the metadata async write error.
  5027. */
  5028. errseq_check_and_advance(&sb->s_bdev->bd_mapping->wb_err,
  5029. &sbi->s_bdev_wb_err);
  5030. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  5031. ext4_orphan_cleanup(sb, es);
  5032. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  5033. /*
  5034. * Update the checksum after updating free space/inode counters and
  5035. * ext4_orphan_cleanup. Otherwise the superblock can have an incorrect
  5036. * checksum in the buffer cache until it is written out and
  5037. * e2fsprogs programs trying to open a file system immediately
  5038. * after it is mounted can fail.
  5039. */
  5040. ext4_superblock_csum_set(sb);
  5041. if (needs_recovery) {
  5042. ext4_msg(sb, KERN_INFO, "recovery complete");
  5043. err = ext4_mark_recovery_complete(sb, es);
  5044. if (err)
  5045. goto failed_mount9;
  5046. }
  5047. if (test_opt(sb, DISCARD) && !bdev_max_discard_sectors(sb->s_bdev)) {
  5048. ext4_msg(sb, KERN_WARNING,
  5049. "mounting with \"discard\" option, but the device does not support discard");
  5050. clear_opt(sb, DISCARD);
  5051. }
  5052. if (es->s_error_count) {
  5053. sbi->s_err_report_sec = 5*60; /* first time 5 minutes */
  5054. mod_timer(&sbi->s_err_report,
  5055. jiffies + secs_to_jiffies(sbi->s_err_report_sec));
  5056. }
  5057. sbi->s_err_report_sec = 24*60*60; /* Once a day */
  5058. /* Enable message ratelimiting. Default is 10 messages per 5 secs. */
  5059. ratelimit_state_init(&sbi->s_err_ratelimit_state, 5 * HZ, 10);
  5060. ratelimit_state_init(&sbi->s_warning_ratelimit_state, 5 * HZ, 10);
  5061. ratelimit_state_init(&sbi->s_msg_ratelimit_state, 5 * HZ, 10);
  5062. atomic_set(&sbi->s_warning_count, 0);
  5063. atomic_set(&sbi->s_msg_count, 0);
  5064. /* Register sysfs after all initializations are complete. */
  5065. err = ext4_register_sysfs(sb);
  5066. if (err)
  5067. goto failed_mount9;
  5068. return 0;
  5069. failed_mount9:
  5070. ext4_quotas_off(sb, EXT4_MAXQUOTAS);
  5071. failed_mount8: __maybe_unused
  5072. ext4_release_orphan_info(sb);
  5073. failed_mount7:
  5074. ext4_unregister_li_request(sb);
  5075. failed_mount6:
  5076. ext4_mb_release(sb);
  5077. ext4_flex_groups_free(sbi);
  5078. failed_mount5:
  5079. ext4_percpu_param_destroy(sbi);
  5080. ext4_ext_release(sb);
  5081. ext4_release_system_zone(sb);
  5082. failed_mount4a:
  5083. dput(sb->s_root);
  5084. sb->s_root = NULL;
  5085. failed_mount4:
  5086. ext4_msg(sb, KERN_ERR, "mount failed");
  5087. if (EXT4_SB(sb)->rsv_conversion_wq)
  5088. destroy_workqueue(EXT4_SB(sb)->rsv_conversion_wq);
  5089. failed_mount_wq:
  5090. ext4_xattr_destroy_cache(sbi->s_ea_inode_cache);
  5091. sbi->s_ea_inode_cache = NULL;
  5092. ext4_xattr_destroy_cache(sbi->s_ea_block_cache);
  5093. sbi->s_ea_block_cache = NULL;
  5094. if (sbi->s_journal) {
  5095. ext4_journal_destroy(sbi, sbi->s_journal);
  5096. }
  5097. failed_mount3a:
  5098. ext4_es_unregister_shrinker(sbi);
  5099. failed_mount3:
  5100. /* flush s_sb_upd_work before sbi destroy */
  5101. flush_work(&sbi->s_sb_upd_work);
  5102. ext4_stop_mmpd(sbi);
  5103. timer_delete_sync(&sbi->s_err_report);
  5104. ext4_group_desc_free(sbi);
  5105. failed_mount:
  5106. #if IS_ENABLED(CONFIG_UNICODE)
  5107. utf8_unload(sb->s_encoding);
  5108. #endif
  5109. #ifdef CONFIG_QUOTA
  5110. for (unsigned int i = 0; i < EXT4_MAXQUOTAS; i++)
  5111. kfree(get_qf_name(sb, sbi, i));
  5112. #endif
  5113. fscrypt_free_dummy_policy(&sbi->s_dummy_enc_policy);
  5114. brelse(sbi->s_sbh);
  5115. if (sbi->s_journal_bdev_file) {
  5116. invalidate_bdev(file_bdev(sbi->s_journal_bdev_file));
  5117. bdev_fput(sbi->s_journal_bdev_file);
  5118. }
  5119. out_fail:
  5120. invalidate_bdev(sb->s_bdev);
  5121. sb->s_fs_info = NULL;
  5122. return err;
  5123. }
  5124. static int ext4_fill_super(struct super_block *sb, struct fs_context *fc)
  5125. {
  5126. struct ext4_fs_context *ctx = fc->fs_private;
  5127. struct ext4_sb_info *sbi;
  5128. const char *descr;
  5129. int ret;
  5130. sbi = ext4_alloc_sbi(sb);
  5131. if (!sbi)
  5132. return -ENOMEM;
  5133. fc->s_fs_info = sbi;
  5134. /* Cleanup superblock name */
  5135. strreplace(sb->s_id, '/', '!');
  5136. sbi->s_sb_block = 1; /* Default super block location */
  5137. if (ctx->spec & EXT4_SPEC_s_sb_block)
  5138. sbi->s_sb_block = ctx->s_sb_block;
  5139. ret = __ext4_fill_super(fc, sb);
  5140. if (ret < 0)
  5141. goto free_sbi;
  5142. if (sbi->s_journal) {
  5143. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  5144. descr = " journalled data mode";
  5145. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  5146. descr = " ordered data mode";
  5147. else
  5148. descr = " writeback data mode";
  5149. } else
  5150. descr = "out journal";
  5151. if (___ratelimit(&ext4_mount_msg_ratelimit, "EXT4-fs mount"))
  5152. ext4_msg(sb, KERN_INFO, "mounted filesystem %pU %s with%s. "
  5153. "Quota mode: %s.", &sb->s_uuid,
  5154. sb_rdonly(sb) ? "ro" : "r/w", descr,
  5155. ext4_quota_mode(sb));
  5156. /* Update the s_overhead_clusters if necessary */
  5157. ext4_update_overhead(sb, false);
  5158. return 0;
  5159. free_sbi:
  5160. ext4_free_sbi(sbi);
  5161. fc->s_fs_info = NULL;
  5162. return ret;
  5163. }
  5164. static int ext4_get_tree(struct fs_context *fc)
  5165. {
  5166. return get_tree_bdev(fc, ext4_fill_super);
  5167. }
  5168. /*
  5169. * Setup any per-fs journal parameters now. We'll do this both on
  5170. * initial mount, once the journal has been initialised but before we've
  5171. * done any recovery; and again on any subsequent remount.
  5172. */
  5173. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  5174. {
  5175. struct ext4_sb_info *sbi = EXT4_SB(sb);
  5176. journal->j_commit_interval = sbi->s_commit_interval;
  5177. journal->j_min_batch_time = sbi->s_min_batch_time;
  5178. journal->j_max_batch_time = sbi->s_max_batch_time;
  5179. ext4_fc_init(sb, journal);
  5180. write_lock(&journal->j_state_lock);
  5181. if (test_opt(sb, BARRIER))
  5182. journal->j_flags |= JBD2_BARRIER;
  5183. else
  5184. journal->j_flags &= ~JBD2_BARRIER;
  5185. /*
  5186. * Always enable journal cycle record option, letting the journal
  5187. * records log transactions continuously between each mount.
  5188. */
  5189. journal->j_flags |= JBD2_CYCLE_RECORD;
  5190. write_unlock(&journal->j_state_lock);
  5191. }
  5192. static struct inode *ext4_get_journal_inode(struct super_block *sb,
  5193. unsigned int journal_inum)
  5194. {
  5195. struct inode *journal_inode;
  5196. /*
  5197. * Test for the existence of a valid inode on disk. Bad things
  5198. * happen if we iget() an unused inode, as the subsequent iput()
  5199. * will try to delete it.
  5200. */
  5201. journal_inode = ext4_iget(sb, journal_inum, EXT4_IGET_SPECIAL);
  5202. if (IS_ERR(journal_inode)) {
  5203. ext4_msg(sb, KERN_ERR, "no journal found");
  5204. return ERR_CAST(journal_inode);
  5205. }
  5206. if (!journal_inode->i_nlink) {
  5207. make_bad_inode(journal_inode);
  5208. iput(journal_inode);
  5209. ext4_msg(sb, KERN_ERR, "journal inode is deleted");
  5210. return ERR_PTR(-EFSCORRUPTED);
  5211. }
  5212. if (!S_ISREG(journal_inode->i_mode) || IS_ENCRYPTED(journal_inode)) {
  5213. ext4_msg(sb, KERN_ERR, "invalid journal inode");
  5214. iput(journal_inode);
  5215. return ERR_PTR(-EFSCORRUPTED);
  5216. }
  5217. ext4_debug("Journal inode found at %p: %lld bytes\n",
  5218. journal_inode, journal_inode->i_size);
  5219. return journal_inode;
  5220. }
  5221. static int ext4_journal_bmap(journal_t *journal, sector_t *block)
  5222. {
  5223. struct ext4_map_blocks map;
  5224. int ret;
  5225. if (journal->j_inode == NULL)
  5226. return 0;
  5227. map.m_lblk = *block;
  5228. map.m_len = 1;
  5229. ret = ext4_map_blocks(NULL, journal->j_inode, &map, 0);
  5230. if (ret <= 0) {
  5231. ext4_msg(journal->j_inode->i_sb, KERN_CRIT,
  5232. "journal bmap failed: block %llu ret %d\n",
  5233. *block, ret);
  5234. jbd2_journal_abort(journal, ret ? ret : -EFSCORRUPTED);
  5235. return ret;
  5236. }
  5237. *block = map.m_pblk;
  5238. return 0;
  5239. }
  5240. static journal_t *ext4_open_inode_journal(struct super_block *sb,
  5241. unsigned int journal_inum)
  5242. {
  5243. struct inode *journal_inode;
  5244. journal_t *journal;
  5245. journal_inode = ext4_get_journal_inode(sb, journal_inum);
  5246. if (IS_ERR(journal_inode))
  5247. return ERR_CAST(journal_inode);
  5248. journal = jbd2_journal_init_inode(journal_inode);
  5249. if (IS_ERR(journal)) {
  5250. ext4_msg(sb, KERN_ERR, "Could not load journal inode");
  5251. iput(journal_inode);
  5252. return ERR_CAST(journal);
  5253. }
  5254. journal->j_private = sb;
  5255. journal->j_bmap = ext4_journal_bmap;
  5256. ext4_init_journal_params(sb, journal);
  5257. return journal;
  5258. }
  5259. static struct file *ext4_get_journal_blkdev(struct super_block *sb,
  5260. dev_t j_dev, ext4_fsblk_t *j_start,
  5261. ext4_fsblk_t *j_len)
  5262. {
  5263. struct buffer_head *bh;
  5264. struct block_device *bdev;
  5265. struct file *bdev_file;
  5266. int hblock, blocksize;
  5267. ext4_fsblk_t sb_block;
  5268. unsigned long offset;
  5269. struct ext4_super_block *es;
  5270. int errno;
  5271. bdev_file = bdev_file_open_by_dev(j_dev,
  5272. BLK_OPEN_READ | BLK_OPEN_WRITE | BLK_OPEN_RESTRICT_WRITES,
  5273. sb, &fs_holder_ops);
  5274. if (IS_ERR(bdev_file)) {
  5275. ext4_msg(sb, KERN_ERR,
  5276. "failed to open journal device unknown-block(%u,%u) %ld",
  5277. MAJOR(j_dev), MINOR(j_dev), PTR_ERR(bdev_file));
  5278. return bdev_file;
  5279. }
  5280. bdev = file_bdev(bdev_file);
  5281. blocksize = sb->s_blocksize;
  5282. hblock = bdev_logical_block_size(bdev);
  5283. if (blocksize < hblock) {
  5284. ext4_msg(sb, KERN_ERR,
  5285. "blocksize too small for journal device");
  5286. errno = -EINVAL;
  5287. goto out_bdev;
  5288. }
  5289. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  5290. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  5291. set_blocksize(bdev_file, blocksize);
  5292. bh = __bread(bdev, sb_block, blocksize);
  5293. if (!bh) {
  5294. ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
  5295. "external journal");
  5296. errno = -EINVAL;
  5297. goto out_bdev;
  5298. }
  5299. es = (struct ext4_super_block *) (bh->b_data + offset);
  5300. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  5301. !(le32_to_cpu(es->s_feature_incompat) &
  5302. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  5303. ext4_msg(sb, KERN_ERR, "external journal has bad superblock");
  5304. errno = -EFSCORRUPTED;
  5305. goto out_bh;
  5306. }
  5307. if ((le32_to_cpu(es->s_feature_ro_compat) &
  5308. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM) &&
  5309. es->s_checksum != ext4_superblock_csum(es)) {
  5310. ext4_msg(sb, KERN_ERR, "external journal has corrupt superblock");
  5311. errno = -EFSCORRUPTED;
  5312. goto out_bh;
  5313. }
  5314. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  5315. ext4_msg(sb, KERN_ERR, "journal UUID does not match");
  5316. errno = -EFSCORRUPTED;
  5317. goto out_bh;
  5318. }
  5319. *j_start = sb_block + 1;
  5320. *j_len = ext4_blocks_count(es);
  5321. brelse(bh);
  5322. return bdev_file;
  5323. out_bh:
  5324. brelse(bh);
  5325. out_bdev:
  5326. bdev_fput(bdev_file);
  5327. return ERR_PTR(errno);
  5328. }
  5329. static journal_t *ext4_open_dev_journal(struct super_block *sb,
  5330. dev_t j_dev)
  5331. {
  5332. journal_t *journal;
  5333. ext4_fsblk_t j_start;
  5334. ext4_fsblk_t j_len;
  5335. struct file *bdev_file;
  5336. int errno = 0;
  5337. bdev_file = ext4_get_journal_blkdev(sb, j_dev, &j_start, &j_len);
  5338. if (IS_ERR(bdev_file))
  5339. return ERR_CAST(bdev_file);
  5340. journal = jbd2_journal_init_dev(file_bdev(bdev_file), sb->s_bdev, j_start,
  5341. j_len, sb->s_blocksize);
  5342. if (IS_ERR(journal)) {
  5343. ext4_msg(sb, KERN_ERR, "failed to create device journal");
  5344. errno = PTR_ERR(journal);
  5345. goto out_bdev;
  5346. }
  5347. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  5348. ext4_msg(sb, KERN_ERR, "External journal has more than one "
  5349. "user (unsupported) - %d",
  5350. be32_to_cpu(journal->j_superblock->s_nr_users));
  5351. errno = -EINVAL;
  5352. goto out_journal;
  5353. }
  5354. journal->j_private = sb;
  5355. EXT4_SB(sb)->s_journal_bdev_file = bdev_file;
  5356. ext4_init_journal_params(sb, journal);
  5357. return journal;
  5358. out_journal:
  5359. ext4_journal_destroy(EXT4_SB(sb), journal);
  5360. out_bdev:
  5361. bdev_fput(bdev_file);
  5362. return ERR_PTR(errno);
  5363. }
  5364. static int ext4_load_journal(struct super_block *sb,
  5365. struct ext4_super_block *es,
  5366. unsigned long journal_devnum)
  5367. {
  5368. journal_t *journal;
  5369. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  5370. dev_t journal_dev;
  5371. int err = 0;
  5372. int really_read_only;
  5373. int journal_dev_ro;
  5374. if (WARN_ON_ONCE(!ext4_has_feature_journal(sb)))
  5375. return -EFSCORRUPTED;
  5376. if (journal_devnum &&
  5377. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  5378. ext4_msg(sb, KERN_INFO, "external journal device major/minor "
  5379. "numbers have changed");
  5380. journal_dev = new_decode_dev(journal_devnum);
  5381. } else
  5382. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  5383. if (journal_inum && journal_dev) {
  5384. ext4_msg(sb, KERN_ERR,
  5385. "filesystem has both journal inode and journal device!");
  5386. return -EINVAL;
  5387. }
  5388. if (journal_inum) {
  5389. journal = ext4_open_inode_journal(sb, journal_inum);
  5390. if (IS_ERR(journal))
  5391. return PTR_ERR(journal);
  5392. } else {
  5393. journal = ext4_open_dev_journal(sb, journal_dev);
  5394. if (IS_ERR(journal))
  5395. return PTR_ERR(journal);
  5396. }
  5397. journal_dev_ro = bdev_read_only(journal->j_dev);
  5398. really_read_only = bdev_read_only(sb->s_bdev) | journal_dev_ro;
  5399. if (journal_dev_ro && !sb_rdonly(sb)) {
  5400. ext4_msg(sb, KERN_ERR,
  5401. "journal device read-only, try mounting with '-o ro'");
  5402. err = -EROFS;
  5403. goto err_out;
  5404. }
  5405. /*
  5406. * Are we loading a blank journal or performing recovery after a
  5407. * crash? For recovery, we need to check in advance whether we
  5408. * can get read-write access to the device.
  5409. */
  5410. if (ext4_has_feature_journal_needs_recovery(sb)) {
  5411. if (sb_rdonly(sb)) {
  5412. ext4_msg(sb, KERN_INFO, "INFO: recovery "
  5413. "required on readonly filesystem");
  5414. if (really_read_only) {
  5415. ext4_msg(sb, KERN_ERR, "write access "
  5416. "unavailable, cannot proceed "
  5417. "(try mounting with noload)");
  5418. err = -EROFS;
  5419. goto err_out;
  5420. }
  5421. ext4_msg(sb, KERN_INFO, "write access will "
  5422. "be enabled during recovery");
  5423. }
  5424. }
  5425. if (!(journal->j_flags & JBD2_BARRIER))
  5426. ext4_msg(sb, KERN_INFO, "barriers disabled");
  5427. if (!ext4_has_feature_journal_needs_recovery(sb))
  5428. err = jbd2_journal_wipe(journal, !really_read_only);
  5429. if (!err) {
  5430. char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
  5431. __le16 orig_state;
  5432. bool changed = false;
  5433. if (save)
  5434. memcpy(save, ((char *) es) +
  5435. EXT4_S_ERR_START, EXT4_S_ERR_LEN);
  5436. err = jbd2_journal_load(journal);
  5437. if (save && memcmp(((char *) es) + EXT4_S_ERR_START,
  5438. save, EXT4_S_ERR_LEN)) {
  5439. memcpy(((char *) es) + EXT4_S_ERR_START,
  5440. save, EXT4_S_ERR_LEN);
  5441. changed = true;
  5442. }
  5443. kfree(save);
  5444. orig_state = es->s_state;
  5445. es->s_state |= cpu_to_le16(EXT4_SB(sb)->s_mount_state &
  5446. EXT4_ERROR_FS);
  5447. if (orig_state != es->s_state)
  5448. changed = true;
  5449. /* Write out restored error information to the superblock */
  5450. if (changed && !really_read_only) {
  5451. int err2;
  5452. err2 = ext4_commit_super(sb);
  5453. err = err ? : err2;
  5454. }
  5455. }
  5456. if (err) {
  5457. ext4_msg(sb, KERN_ERR, "error loading journal");
  5458. goto err_out;
  5459. }
  5460. EXT4_SB(sb)->s_journal = journal;
  5461. err = ext4_clear_journal_err(sb, es);
  5462. if (err) {
  5463. ext4_journal_destroy(EXT4_SB(sb), journal);
  5464. return err;
  5465. }
  5466. if (!really_read_only && journal_devnum &&
  5467. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  5468. es->s_journal_dev = cpu_to_le32(journal_devnum);
  5469. ext4_commit_super(sb);
  5470. }
  5471. if (!really_read_only && journal_inum &&
  5472. journal_inum != le32_to_cpu(es->s_journal_inum)) {
  5473. es->s_journal_inum = cpu_to_le32(journal_inum);
  5474. ext4_commit_super(sb);
  5475. }
  5476. return 0;
  5477. err_out:
  5478. ext4_journal_destroy(EXT4_SB(sb), journal);
  5479. return err;
  5480. }
  5481. /* Copy state of EXT4_SB(sb) into buffer for on-disk superblock */
  5482. static void ext4_update_super(struct super_block *sb)
  5483. {
  5484. struct ext4_sb_info *sbi = EXT4_SB(sb);
  5485. struct ext4_super_block *es = sbi->s_es;
  5486. struct buffer_head *sbh = sbi->s_sbh;
  5487. lock_buffer(sbh);
  5488. /*
  5489. * If the file system is mounted read-only, don't update the
  5490. * superblock write time. This avoids updating the superblock
  5491. * write time when we are mounting the root file system
  5492. * read/only but we need to replay the journal; at that point,
  5493. * for people who are east of GMT and who make their clock
  5494. * tick in localtime for Windows bug-for-bug compatibility,
  5495. * the clock is set in the future, and this will cause e2fsck
  5496. * to complain and force a full file system check.
  5497. */
  5498. if (!sb_rdonly(sb))
  5499. ext4_update_tstamp(es, s_wtime);
  5500. es->s_kbytes_written =
  5501. cpu_to_le64(sbi->s_kbytes_written +
  5502. ((part_stat_read(sb->s_bdev, sectors[STAT_WRITE]) -
  5503. sbi->s_sectors_written_start) >> 1));
  5504. if (percpu_counter_initialized(&sbi->s_freeclusters_counter))
  5505. ext4_free_blocks_count_set(es,
  5506. EXT4_C2B(sbi, percpu_counter_sum_positive(
  5507. &sbi->s_freeclusters_counter)));
  5508. if (percpu_counter_initialized(&sbi->s_freeinodes_counter))
  5509. es->s_free_inodes_count =
  5510. cpu_to_le32(percpu_counter_sum_positive(
  5511. &sbi->s_freeinodes_counter));
  5512. /* Copy error information to the on-disk superblock */
  5513. spin_lock(&sbi->s_error_lock);
  5514. if (sbi->s_add_error_count > 0) {
  5515. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  5516. if (!es->s_first_error_time && !es->s_first_error_time_hi) {
  5517. __ext4_update_tstamp(&es->s_first_error_time,
  5518. &es->s_first_error_time_hi,
  5519. sbi->s_first_error_time);
  5520. strtomem_pad(es->s_first_error_func,
  5521. sbi->s_first_error_func, 0);
  5522. es->s_first_error_line =
  5523. cpu_to_le32(sbi->s_first_error_line);
  5524. es->s_first_error_ino =
  5525. cpu_to_le32(sbi->s_first_error_ino);
  5526. es->s_first_error_block =
  5527. cpu_to_le64(sbi->s_first_error_block);
  5528. es->s_first_error_errcode =
  5529. ext4_errno_to_code(sbi->s_first_error_code);
  5530. }
  5531. __ext4_update_tstamp(&es->s_last_error_time,
  5532. &es->s_last_error_time_hi,
  5533. sbi->s_last_error_time);
  5534. strtomem_pad(es->s_last_error_func, sbi->s_last_error_func, 0);
  5535. es->s_last_error_line = cpu_to_le32(sbi->s_last_error_line);
  5536. es->s_last_error_ino = cpu_to_le32(sbi->s_last_error_ino);
  5537. es->s_last_error_block = cpu_to_le64(sbi->s_last_error_block);
  5538. es->s_last_error_errcode =
  5539. ext4_errno_to_code(sbi->s_last_error_code);
  5540. /*
  5541. * Start the daily error reporting function if it hasn't been
  5542. * started already and sbi->s_err_report_sec is not zero
  5543. */
  5544. if (!es->s_error_count && !sbi->s_err_report_sec)
  5545. mod_timer(&sbi->s_err_report,
  5546. jiffies + secs_to_jiffies(sbi->s_err_report_sec));
  5547. le32_add_cpu(&es->s_error_count, sbi->s_add_error_count);
  5548. sbi->s_add_error_count = 0;
  5549. }
  5550. spin_unlock(&sbi->s_error_lock);
  5551. ext4_superblock_csum_set(sb);
  5552. unlock_buffer(sbh);
  5553. }
  5554. static int ext4_commit_super(struct super_block *sb)
  5555. {
  5556. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  5557. if (!sbh)
  5558. return -EINVAL;
  5559. ext4_update_super(sb);
  5560. lock_buffer(sbh);
  5561. /* Buffer got discarded which means block device got invalidated */
  5562. if (!buffer_mapped(sbh)) {
  5563. unlock_buffer(sbh);
  5564. return -EIO;
  5565. }
  5566. if (buffer_write_io_error(sbh) || !buffer_uptodate(sbh)) {
  5567. /*
  5568. * Oh, dear. A previous attempt to write the
  5569. * superblock failed. This could happen because the
  5570. * USB device was yanked out. Or it could happen to
  5571. * be a transient write error and maybe the block will
  5572. * be remapped. Nothing we can do but to retry the
  5573. * write and hope for the best.
  5574. */
  5575. ext4_msg(sb, KERN_ERR, "previous I/O error to "
  5576. "superblock detected");
  5577. clear_buffer_write_io_error(sbh);
  5578. set_buffer_uptodate(sbh);
  5579. }
  5580. get_bh(sbh);
  5581. /* Clear potential dirty bit if it was journalled update */
  5582. clear_buffer_dirty(sbh);
  5583. sbh->b_end_io = end_buffer_write_sync;
  5584. submit_bh(REQ_OP_WRITE | REQ_SYNC |
  5585. (test_opt(sb, BARRIER) ? REQ_FUA : 0), sbh);
  5586. wait_on_buffer(sbh);
  5587. if (buffer_write_io_error(sbh)) {
  5588. ext4_msg(sb, KERN_ERR, "I/O error while writing "
  5589. "superblock");
  5590. clear_buffer_write_io_error(sbh);
  5591. set_buffer_uptodate(sbh);
  5592. return -EIO;
  5593. }
  5594. return 0;
  5595. }
  5596. /*
  5597. * Have we just finished recovery? If so, and if we are mounting (or
  5598. * remounting) the filesystem readonly, then we will end up with a
  5599. * consistent fs on disk. Record that fact.
  5600. */
  5601. static int ext4_mark_recovery_complete(struct super_block *sb,
  5602. struct ext4_super_block *es)
  5603. {
  5604. int err;
  5605. journal_t *journal = EXT4_SB(sb)->s_journal;
  5606. if (!ext4_has_feature_journal(sb)) {
  5607. if (journal != NULL) {
  5608. ext4_error(sb, "Journal got removed while the fs was "
  5609. "mounted!");
  5610. return -EFSCORRUPTED;
  5611. }
  5612. return 0;
  5613. }
  5614. jbd2_journal_lock_updates(journal);
  5615. err = jbd2_journal_flush(journal, 0);
  5616. if (err < 0)
  5617. goto out;
  5618. if (sb_rdonly(sb) && (ext4_has_feature_journal_needs_recovery(sb) ||
  5619. ext4_has_feature_orphan_present(sb))) {
  5620. if (!ext4_orphan_file_empty(sb)) {
  5621. ext4_error(sb, "Orphan file not empty on read-only fs.");
  5622. err = -EFSCORRUPTED;
  5623. goto out;
  5624. }
  5625. ext4_clear_feature_journal_needs_recovery(sb);
  5626. ext4_clear_feature_orphan_present(sb);
  5627. ext4_commit_super(sb);
  5628. }
  5629. out:
  5630. jbd2_journal_unlock_updates(journal);
  5631. return err;
  5632. }
  5633. /*
  5634. * If we are mounting (or read-write remounting) a filesystem whose journal
  5635. * has recorded an error from a previous lifetime, move that error to the
  5636. * main filesystem now.
  5637. */
  5638. static int ext4_clear_journal_err(struct super_block *sb,
  5639. struct ext4_super_block *es)
  5640. {
  5641. journal_t *journal;
  5642. int j_errno;
  5643. const char *errstr;
  5644. if (!ext4_has_feature_journal(sb)) {
  5645. ext4_error(sb, "Journal got removed while the fs was mounted!");
  5646. return -EFSCORRUPTED;
  5647. }
  5648. journal = EXT4_SB(sb)->s_journal;
  5649. /*
  5650. * Now check for any error status which may have been recorded in the
  5651. * journal by a prior ext4_error() or ext4_abort()
  5652. */
  5653. j_errno = jbd2_journal_errno(journal);
  5654. if (j_errno) {
  5655. char nbuf[16];
  5656. errstr = ext4_decode_error(sb, j_errno, nbuf);
  5657. ext4_warning(sb, "Filesystem error recorded "
  5658. "from previous mount: %s", errstr);
  5659. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  5660. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  5661. j_errno = ext4_commit_super(sb);
  5662. if (j_errno)
  5663. return j_errno;
  5664. ext4_warning(sb, "Marked fs in need of filesystem check.");
  5665. jbd2_journal_clear_err(journal);
  5666. jbd2_journal_update_sb_errno(journal);
  5667. }
  5668. return 0;
  5669. }
  5670. /*
  5671. * Force the running and committing transactions to commit,
  5672. * and wait on the commit.
  5673. */
  5674. int ext4_force_commit(struct super_block *sb)
  5675. {
  5676. return ext4_journal_force_commit(EXT4_SB(sb)->s_journal);
  5677. }
  5678. static int ext4_sync_fs(struct super_block *sb, int wait)
  5679. {
  5680. int ret = 0;
  5681. tid_t target;
  5682. bool needs_barrier = false;
  5683. struct ext4_sb_info *sbi = EXT4_SB(sb);
  5684. ret = ext4_emergency_state(sb);
  5685. if (unlikely(ret))
  5686. return ret;
  5687. trace_ext4_sync_fs(sb, wait);
  5688. flush_workqueue(sbi->rsv_conversion_wq);
  5689. /*
  5690. * Writeback quota in non-journalled quota case - journalled quota has
  5691. * no dirty dquots
  5692. */
  5693. dquot_writeback_dquots(sb, -1);
  5694. /*
  5695. * Data writeback is possible w/o journal transaction, so barrier must
  5696. * being sent at the end of the function. But we can skip it if
  5697. * transaction_commit will do it for us.
  5698. */
  5699. if (sbi->s_journal) {
  5700. target = jbd2_get_latest_transaction(sbi->s_journal);
  5701. if (wait && sbi->s_journal->j_flags & JBD2_BARRIER &&
  5702. !jbd2_trans_will_send_data_barrier(sbi->s_journal, target))
  5703. needs_barrier = true;
  5704. if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
  5705. if (wait)
  5706. ret = jbd2_log_wait_commit(sbi->s_journal,
  5707. target);
  5708. }
  5709. } else if (wait && test_opt(sb, BARRIER))
  5710. needs_barrier = true;
  5711. if (needs_barrier) {
  5712. int err;
  5713. err = blkdev_issue_flush(sb->s_bdev);
  5714. if (!ret)
  5715. ret = err;
  5716. }
  5717. return ret;
  5718. }
  5719. /*
  5720. * LVM calls this function before a (read-only) snapshot is created. This
  5721. * gives us a chance to flush the journal completely and mark the fs clean.
  5722. *
  5723. * Note that only this function cannot bring a filesystem to be in a clean
  5724. * state independently. It relies on upper layer to stop all data & metadata
  5725. * modifications.
  5726. */
  5727. static int ext4_freeze(struct super_block *sb)
  5728. {
  5729. int error = 0;
  5730. journal_t *journal = EXT4_SB(sb)->s_journal;
  5731. if (journal) {
  5732. /* Now we set up the journal barrier. */
  5733. jbd2_journal_lock_updates(journal);
  5734. /*
  5735. * Don't clear the needs_recovery flag if we failed to
  5736. * flush the journal.
  5737. */
  5738. error = jbd2_journal_flush(journal, 0);
  5739. if (error < 0)
  5740. goto out;
  5741. /* Journal blocked and flushed, clear needs_recovery flag. */
  5742. ext4_clear_feature_journal_needs_recovery(sb);
  5743. if (ext4_orphan_file_empty(sb))
  5744. ext4_clear_feature_orphan_present(sb);
  5745. }
  5746. error = ext4_commit_super(sb);
  5747. out:
  5748. if (journal)
  5749. /* we rely on upper layer to stop further updates */
  5750. jbd2_journal_unlock_updates(journal);
  5751. return error;
  5752. }
  5753. /*
  5754. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  5755. * flag here, even though the filesystem is not technically dirty yet.
  5756. */
  5757. static int ext4_unfreeze(struct super_block *sb)
  5758. {
  5759. if (ext4_emergency_state(sb))
  5760. return 0;
  5761. if (EXT4_SB(sb)->s_journal) {
  5762. /* Reset the needs_recovery flag before the fs is unlocked. */
  5763. ext4_set_feature_journal_needs_recovery(sb);
  5764. if (ext4_has_feature_orphan_file(sb))
  5765. ext4_set_feature_orphan_present(sb);
  5766. }
  5767. ext4_commit_super(sb);
  5768. return 0;
  5769. }
  5770. /*
  5771. * Structure to save mount options for ext4_remount's benefit
  5772. */
  5773. struct ext4_mount_options {
  5774. unsigned long s_mount_opt;
  5775. unsigned long s_mount_opt2;
  5776. kuid_t s_resuid;
  5777. kgid_t s_resgid;
  5778. unsigned long s_commit_interval;
  5779. u32 s_min_batch_time, s_max_batch_time;
  5780. #ifdef CONFIG_QUOTA
  5781. int s_jquota_fmt;
  5782. char *s_qf_names[EXT4_MAXQUOTAS];
  5783. #endif
  5784. };
  5785. static int __ext4_remount(struct fs_context *fc, struct super_block *sb)
  5786. {
  5787. struct ext4_fs_context *ctx = fc->fs_private;
  5788. struct ext4_super_block *es;
  5789. struct ext4_sb_info *sbi = EXT4_SB(sb);
  5790. unsigned long old_sb_flags;
  5791. struct ext4_mount_options old_opts;
  5792. ext4_group_t g;
  5793. int err = 0;
  5794. int alloc_ctx;
  5795. #ifdef CONFIG_QUOTA
  5796. int enable_quota = 0;
  5797. int i, j;
  5798. char *to_free[EXT4_MAXQUOTAS];
  5799. #endif
  5800. /* Store the original options */
  5801. old_sb_flags = sb->s_flags;
  5802. old_opts.s_mount_opt = sbi->s_mount_opt;
  5803. old_opts.s_mount_opt2 = sbi->s_mount_opt2;
  5804. old_opts.s_resuid = sbi->s_resuid;
  5805. old_opts.s_resgid = sbi->s_resgid;
  5806. old_opts.s_commit_interval = sbi->s_commit_interval;
  5807. old_opts.s_min_batch_time = sbi->s_min_batch_time;
  5808. old_opts.s_max_batch_time = sbi->s_max_batch_time;
  5809. #ifdef CONFIG_QUOTA
  5810. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  5811. for (i = 0; i < EXT4_MAXQUOTAS; i++)
  5812. if (sbi->s_qf_names[i]) {
  5813. char *qf_name = get_qf_name(sb, sbi, i);
  5814. old_opts.s_qf_names[i] = kstrdup(qf_name, GFP_KERNEL);
  5815. if (!old_opts.s_qf_names[i]) {
  5816. for (j = 0; j < i; j++)
  5817. kfree(old_opts.s_qf_names[j]);
  5818. return -ENOMEM;
  5819. }
  5820. } else
  5821. old_opts.s_qf_names[i] = NULL;
  5822. #endif
  5823. if (!(ctx->spec & EXT4_SPEC_JOURNAL_IOPRIO)) {
  5824. if (sbi->s_journal && sbi->s_journal->j_task->io_context)
  5825. ctx->journal_ioprio =
  5826. sbi->s_journal->j_task->io_context->ioprio;
  5827. else
  5828. ctx->journal_ioprio = EXT4_DEF_JOURNAL_IOPRIO;
  5829. }
  5830. if ((ctx->spec & EXT4_SPEC_s_stripe) &&
  5831. ext4_is_stripe_incompatible(sb, ctx->s_stripe)) {
  5832. ext4_msg(sb, KERN_WARNING,
  5833. "stripe (%lu) is not aligned with cluster size (%u), "
  5834. "stripe is disabled",
  5835. ctx->s_stripe, sbi->s_cluster_ratio);
  5836. ctx->s_stripe = 0;
  5837. }
  5838. /*
  5839. * Changing the DIOREAD_NOLOCK or DELALLOC mount options may cause
  5840. * two calls to ext4_should_dioread_nolock() to return inconsistent
  5841. * values, triggering WARN_ON in ext4_add_complete_io(). we grab
  5842. * here s_writepages_rwsem to avoid race between writepages ops and
  5843. * remount.
  5844. */
  5845. alloc_ctx = ext4_writepages_down_write(sb);
  5846. ext4_apply_options(fc, sb);
  5847. ext4_writepages_up_write(sb, alloc_ctx);
  5848. if ((old_opts.s_mount_opt & EXT4_MOUNT_JOURNAL_CHECKSUM) ^
  5849. test_opt(sb, JOURNAL_CHECKSUM)) {
  5850. ext4_msg(sb, KERN_ERR, "changing journal_checksum "
  5851. "during remount not supported; ignoring");
  5852. sbi->s_mount_opt ^= EXT4_MOUNT_JOURNAL_CHECKSUM;
  5853. }
  5854. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  5855. if (test_opt2(sb, EXPLICIT_DELALLOC)) {
  5856. ext4_msg(sb, KERN_ERR, "can't mount with "
  5857. "both data=journal and delalloc");
  5858. err = -EINVAL;
  5859. goto restore_opts;
  5860. }
  5861. if (test_opt(sb, DIOREAD_NOLOCK)) {
  5862. ext4_msg(sb, KERN_ERR, "can't mount with "
  5863. "both data=journal and dioread_nolock");
  5864. err = -EINVAL;
  5865. goto restore_opts;
  5866. }
  5867. } else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA) {
  5868. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  5869. ext4_msg(sb, KERN_ERR, "can't mount with "
  5870. "journal_async_commit in data=ordered mode");
  5871. err = -EINVAL;
  5872. goto restore_opts;
  5873. }
  5874. }
  5875. if ((sbi->s_mount_opt ^ old_opts.s_mount_opt) & EXT4_MOUNT_NO_MBCACHE) {
  5876. ext4_msg(sb, KERN_ERR, "can't enable nombcache during remount");
  5877. err = -EINVAL;
  5878. goto restore_opts;
  5879. }
  5880. if ((old_opts.s_mount_opt & EXT4_MOUNT_DELALLOC) &&
  5881. !test_opt(sb, DELALLOC)) {
  5882. ext4_msg(sb, KERN_ERR, "can't disable delalloc during remount");
  5883. err = -EINVAL;
  5884. goto restore_opts;
  5885. }
  5886. sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
  5887. (test_opt(sb, POSIX_ACL) ? SB_POSIXACL : 0);
  5888. es = sbi->s_es;
  5889. if (sbi->s_journal) {
  5890. ext4_init_journal_params(sb, sbi->s_journal);
  5891. set_task_ioprio(sbi->s_journal->j_task, ctx->journal_ioprio);
  5892. }
  5893. /* Flush outstanding errors before changing fs state */
  5894. flush_work(&sbi->s_sb_upd_work);
  5895. if ((bool)(fc->sb_flags & SB_RDONLY) != sb_rdonly(sb)) {
  5896. if (ext4_emergency_state(sb)) {
  5897. err = -EROFS;
  5898. goto restore_opts;
  5899. }
  5900. if (fc->sb_flags & SB_RDONLY) {
  5901. err = sync_filesystem(sb);
  5902. if (err < 0)
  5903. goto restore_opts;
  5904. err = dquot_suspend(sb, -1);
  5905. if (err < 0)
  5906. goto restore_opts;
  5907. /*
  5908. * First of all, the unconditional stuff we have to do
  5909. * to disable replay of the journal when we next remount
  5910. */
  5911. sb->s_flags |= SB_RDONLY;
  5912. /*
  5913. * OK, test if we are remounting a valid rw partition
  5914. * readonly, and if so set the rdonly flag and then
  5915. * mark the partition as valid again.
  5916. */
  5917. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  5918. (sbi->s_mount_state & EXT4_VALID_FS))
  5919. es->s_state = cpu_to_le16(sbi->s_mount_state);
  5920. if (sbi->s_journal) {
  5921. /*
  5922. * We let remount-ro finish even if marking fs
  5923. * as clean failed...
  5924. */
  5925. ext4_mark_recovery_complete(sb, es);
  5926. }
  5927. } else {
  5928. /* Make sure we can mount this feature set readwrite */
  5929. if (ext4_has_feature_readonly(sb) ||
  5930. !ext4_feature_set_ok(sb, 0)) {
  5931. err = -EROFS;
  5932. goto restore_opts;
  5933. }
  5934. /*
  5935. * Make sure the group descriptor checksums
  5936. * are sane. If they aren't, refuse to remount r/w.
  5937. */
  5938. for (g = 0; g < sbi->s_groups_count; g++) {
  5939. struct ext4_group_desc *gdp =
  5940. ext4_get_group_desc(sb, g, NULL);
  5941. if (!ext4_group_desc_csum_verify(sb, g, gdp)) {
  5942. ext4_msg(sb, KERN_ERR,
  5943. "ext4_remount: Checksum for group %u failed (%u!=%u)",
  5944. g, le16_to_cpu(ext4_group_desc_csum(sb, g, gdp)),
  5945. le16_to_cpu(gdp->bg_checksum));
  5946. err = -EFSBADCRC;
  5947. goto restore_opts;
  5948. }
  5949. }
  5950. /*
  5951. * If we have an unprocessed orphan list hanging
  5952. * around from a previously readonly bdev mount,
  5953. * require a full umount/remount for now.
  5954. */
  5955. if (es->s_last_orphan || !ext4_orphan_file_empty(sb)) {
  5956. ext4_msg(sb, KERN_WARNING, "Couldn't "
  5957. "remount RDWR because of unprocessed "
  5958. "orphan inode list. Please "
  5959. "umount/remount instead");
  5960. err = -EINVAL;
  5961. goto restore_opts;
  5962. }
  5963. /*
  5964. * Mounting a RDONLY partition read-write, so reread
  5965. * and store the current valid flag. (It may have
  5966. * been changed by e2fsck since we originally mounted
  5967. * the partition.)
  5968. */
  5969. if (sbi->s_journal) {
  5970. err = ext4_clear_journal_err(sb, es);
  5971. if (err)
  5972. goto restore_opts;
  5973. }
  5974. sbi->s_mount_state = (le16_to_cpu(es->s_state) &
  5975. ~EXT4_FC_REPLAY);
  5976. err = ext4_setup_super(sb, es, 0);
  5977. if (err)
  5978. goto restore_opts;
  5979. sb->s_flags &= ~SB_RDONLY;
  5980. if (ext4_has_feature_mmp(sb)) {
  5981. err = ext4_multi_mount_protect(sb,
  5982. le64_to_cpu(es->s_mmp_block));
  5983. if (err)
  5984. goto restore_opts;
  5985. }
  5986. #ifdef CONFIG_QUOTA
  5987. enable_quota = 1;
  5988. #endif
  5989. }
  5990. }
  5991. /*
  5992. * Handle creation of system zone data early because it can fail.
  5993. * Releasing of existing data is done when we are sure remount will
  5994. * succeed.
  5995. */
  5996. if (test_opt(sb, BLOCK_VALIDITY) && !sbi->s_system_blks) {
  5997. err = ext4_setup_system_zone(sb);
  5998. if (err)
  5999. goto restore_opts;
  6000. }
  6001. if (sbi->s_journal == NULL && !(old_sb_flags & SB_RDONLY)) {
  6002. err = ext4_commit_super(sb);
  6003. if (err)
  6004. goto restore_opts;
  6005. }
  6006. #ifdef CONFIG_QUOTA
  6007. if (enable_quota) {
  6008. if (sb_any_quota_suspended(sb))
  6009. dquot_resume(sb, -1);
  6010. else if (ext4_has_feature_quota(sb)) {
  6011. err = ext4_enable_quotas(sb);
  6012. if (err)
  6013. goto restore_opts;
  6014. }
  6015. }
  6016. /* Release old quota file names */
  6017. for (i = 0; i < EXT4_MAXQUOTAS; i++)
  6018. kfree(old_opts.s_qf_names[i]);
  6019. #endif
  6020. if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
  6021. ext4_release_system_zone(sb);
  6022. /*
  6023. * Reinitialize lazy itable initialization thread based on
  6024. * current settings
  6025. */
  6026. if (sb_rdonly(sb) || !test_opt(sb, INIT_INODE_TABLE))
  6027. ext4_unregister_li_request(sb);
  6028. else {
  6029. ext4_group_t first_not_zeroed;
  6030. first_not_zeroed = ext4_has_uninit_itable(sb);
  6031. ext4_register_li_request(sb, first_not_zeroed);
  6032. }
  6033. if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
  6034. ext4_stop_mmpd(sbi);
  6035. /*
  6036. * Handle aborting the filesystem as the last thing during remount to
  6037. * avoid obsure errors during remount when some option changes fail to
  6038. * apply due to shutdown filesystem.
  6039. */
  6040. if (test_opt2(sb, ABORT))
  6041. ext4_abort(sb, ESHUTDOWN, "Abort forced by user");
  6042. return 0;
  6043. restore_opts:
  6044. /*
  6045. * If there was a failing r/w to ro transition, we may need to
  6046. * re-enable quota
  6047. */
  6048. if (sb_rdonly(sb) && !(old_sb_flags & SB_RDONLY) &&
  6049. sb_any_quota_suspended(sb))
  6050. dquot_resume(sb, -1);
  6051. alloc_ctx = ext4_writepages_down_write(sb);
  6052. sb->s_flags = old_sb_flags;
  6053. sbi->s_mount_opt = old_opts.s_mount_opt;
  6054. sbi->s_mount_opt2 = old_opts.s_mount_opt2;
  6055. sbi->s_resuid = old_opts.s_resuid;
  6056. sbi->s_resgid = old_opts.s_resgid;
  6057. sbi->s_commit_interval = old_opts.s_commit_interval;
  6058. sbi->s_min_batch_time = old_opts.s_min_batch_time;
  6059. sbi->s_max_batch_time = old_opts.s_max_batch_time;
  6060. ext4_writepages_up_write(sb, alloc_ctx);
  6061. if (!test_opt(sb, BLOCK_VALIDITY) && sbi->s_system_blks)
  6062. ext4_release_system_zone(sb);
  6063. #ifdef CONFIG_QUOTA
  6064. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  6065. for (i = 0; i < EXT4_MAXQUOTAS; i++) {
  6066. to_free[i] = get_qf_name(sb, sbi, i);
  6067. rcu_assign_pointer(sbi->s_qf_names[i], old_opts.s_qf_names[i]);
  6068. }
  6069. synchronize_rcu();
  6070. for (i = 0; i < EXT4_MAXQUOTAS; i++)
  6071. kfree(to_free[i]);
  6072. #endif
  6073. if (!ext4_has_feature_mmp(sb) || sb_rdonly(sb))
  6074. ext4_stop_mmpd(sbi);
  6075. return err;
  6076. }
  6077. static int ext4_reconfigure(struct fs_context *fc)
  6078. {
  6079. struct super_block *sb = fc->root->d_sb;
  6080. int ret;
  6081. bool old_ro = sb_rdonly(sb);
  6082. fc->s_fs_info = EXT4_SB(sb);
  6083. ret = ext4_check_opt_consistency(fc, sb);
  6084. if (ret < 0)
  6085. return ret;
  6086. ret = __ext4_remount(fc, sb);
  6087. if (ret < 0)
  6088. return ret;
  6089. ext4_msg(sb, KERN_INFO, "re-mounted %pU%s.",
  6090. &sb->s_uuid,
  6091. (old_ro != sb_rdonly(sb)) ? (sb_rdonly(sb) ? " ro" : " r/w") : "");
  6092. return 0;
  6093. }
  6094. #ifdef CONFIG_QUOTA
  6095. static int ext4_statfs_project(struct super_block *sb,
  6096. kprojid_t projid, struct kstatfs *buf)
  6097. {
  6098. struct kqid qid;
  6099. struct dquot *dquot;
  6100. u64 limit;
  6101. u64 curblock;
  6102. qid = make_kqid_projid(projid);
  6103. dquot = dqget(sb, qid);
  6104. if (IS_ERR(dquot))
  6105. return PTR_ERR(dquot);
  6106. spin_lock(&dquot->dq_dqb_lock);
  6107. limit = min_not_zero(dquot->dq_dqb.dqb_bsoftlimit,
  6108. dquot->dq_dqb.dqb_bhardlimit);
  6109. limit >>= sb->s_blocksize_bits;
  6110. if (limit) {
  6111. uint64_t remaining = 0;
  6112. curblock = (dquot->dq_dqb.dqb_curspace +
  6113. dquot->dq_dqb.dqb_rsvspace) >> sb->s_blocksize_bits;
  6114. if (limit > curblock)
  6115. remaining = limit - curblock;
  6116. buf->f_blocks = min(buf->f_blocks, limit);
  6117. buf->f_bfree = min(buf->f_bfree, remaining);
  6118. buf->f_bavail = min(buf->f_bavail, remaining);
  6119. }
  6120. limit = min_not_zero(dquot->dq_dqb.dqb_isoftlimit,
  6121. dquot->dq_dqb.dqb_ihardlimit);
  6122. if (limit) {
  6123. uint64_t remaining = 0;
  6124. if (limit > dquot->dq_dqb.dqb_curinodes)
  6125. remaining = limit - dquot->dq_dqb.dqb_curinodes;
  6126. buf->f_files = min(buf->f_files, limit);
  6127. buf->f_ffree = min(buf->f_ffree, remaining);
  6128. }
  6129. spin_unlock(&dquot->dq_dqb_lock);
  6130. dqput(dquot);
  6131. return 0;
  6132. }
  6133. #endif
  6134. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
  6135. {
  6136. struct super_block *sb = dentry->d_sb;
  6137. struct ext4_sb_info *sbi = EXT4_SB(sb);
  6138. struct ext4_super_block *es = sbi->s_es;
  6139. ext4_fsblk_t overhead = 0, resv_blocks;
  6140. s64 bfree;
  6141. resv_blocks = EXT4_C2B(sbi, atomic64_read(&sbi->s_resv_clusters));
  6142. if (!test_opt(sb, MINIX_DF))
  6143. overhead = sbi->s_overhead;
  6144. buf->f_type = EXT4_SUPER_MAGIC;
  6145. buf->f_bsize = sb->s_blocksize;
  6146. buf->f_blocks = ext4_blocks_count(es) - EXT4_C2B(sbi, overhead);
  6147. bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
  6148. percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
  6149. /* prevent underflow in case that few free space is available */
  6150. buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
  6151. buf->f_bavail = buf->f_bfree -
  6152. (ext4_r_blocks_count(es) + resv_blocks);
  6153. if (buf->f_bfree < (ext4_r_blocks_count(es) + resv_blocks))
  6154. buf->f_bavail = 0;
  6155. buf->f_files = le32_to_cpu(es->s_inodes_count);
  6156. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  6157. buf->f_namelen = EXT4_NAME_LEN;
  6158. buf->f_fsid = uuid_to_fsid(es->s_uuid);
  6159. #ifdef CONFIG_QUOTA
  6160. if (ext4_test_inode_flag(dentry->d_inode, EXT4_INODE_PROJINHERIT) &&
  6161. sb_has_quota_limits_enabled(sb, PRJQUOTA))
  6162. ext4_statfs_project(sb, EXT4_I(dentry->d_inode)->i_projid, buf);
  6163. #endif
  6164. return 0;
  6165. }
  6166. #ifdef CONFIG_QUOTA
  6167. /*
  6168. * Helper functions so that transaction is started before we acquire dqio_sem
  6169. * to keep correct lock ordering of transaction > dqio_sem
  6170. */
  6171. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  6172. {
  6173. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_id.type];
  6174. }
  6175. static int ext4_write_dquot(struct dquot *dquot)
  6176. {
  6177. int ret, err;
  6178. handle_t *handle;
  6179. struct inode *inode;
  6180. inode = dquot_to_inode(dquot);
  6181. handle = ext4_journal_start(inode, EXT4_HT_QUOTA,
  6182. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  6183. if (IS_ERR(handle))
  6184. return PTR_ERR(handle);
  6185. ret = dquot_commit(dquot);
  6186. if (ret < 0)
  6187. ext4_error_err(dquot->dq_sb, -ret,
  6188. "Failed to commit dquot type %d",
  6189. dquot->dq_id.type);
  6190. err = ext4_journal_stop(handle);
  6191. if (!ret)
  6192. ret = err;
  6193. return ret;
  6194. }
  6195. static int ext4_acquire_dquot(struct dquot *dquot)
  6196. {
  6197. int ret, err;
  6198. handle_t *handle;
  6199. handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
  6200. EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  6201. if (IS_ERR(handle))
  6202. return PTR_ERR(handle);
  6203. ret = dquot_acquire(dquot);
  6204. if (ret < 0)
  6205. ext4_error_err(dquot->dq_sb, -ret,
  6206. "Failed to acquire dquot type %d",
  6207. dquot->dq_id.type);
  6208. err = ext4_journal_stop(handle);
  6209. if (!ret)
  6210. ret = err;
  6211. return ret;
  6212. }
  6213. static int ext4_release_dquot(struct dquot *dquot)
  6214. {
  6215. int ret, err;
  6216. handle_t *handle;
  6217. bool freeze_protected = false;
  6218. /*
  6219. * Trying to sb_start_intwrite() in a running transaction
  6220. * can result in a deadlock. Further, running transactions
  6221. * are already protected from freezing.
  6222. */
  6223. if (!ext4_journal_current_handle()) {
  6224. sb_start_intwrite(dquot->dq_sb);
  6225. freeze_protected = true;
  6226. }
  6227. handle = ext4_journal_start(dquot_to_inode(dquot), EXT4_HT_QUOTA,
  6228. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  6229. if (IS_ERR(handle)) {
  6230. /* Release dquot anyway to avoid endless cycle in dqput() */
  6231. dquot_release(dquot);
  6232. if (freeze_protected)
  6233. sb_end_intwrite(dquot->dq_sb);
  6234. return PTR_ERR(handle);
  6235. }
  6236. ret = dquot_release(dquot);
  6237. if (ret < 0)
  6238. ext4_error_err(dquot->dq_sb, -ret,
  6239. "Failed to release dquot type %d",
  6240. dquot->dq_id.type);
  6241. err = ext4_journal_stop(handle);
  6242. if (!ret)
  6243. ret = err;
  6244. if (freeze_protected)
  6245. sb_end_intwrite(dquot->dq_sb);
  6246. return ret;
  6247. }
  6248. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  6249. {
  6250. struct super_block *sb = dquot->dq_sb;
  6251. if (ext4_is_quota_journalled(sb)) {
  6252. dquot_mark_dquot_dirty(dquot);
  6253. return ext4_write_dquot(dquot);
  6254. } else {
  6255. return dquot_mark_dquot_dirty(dquot);
  6256. }
  6257. }
  6258. static int ext4_write_info(struct super_block *sb, int type)
  6259. {
  6260. int ret, err;
  6261. handle_t *handle;
  6262. /* Data block + inode block */
  6263. handle = ext4_journal_start_sb(sb, EXT4_HT_QUOTA, 2);
  6264. if (IS_ERR(handle))
  6265. return PTR_ERR(handle);
  6266. ret = dquot_commit_info(sb, type);
  6267. err = ext4_journal_stop(handle);
  6268. if (!ret)
  6269. ret = err;
  6270. return ret;
  6271. }
  6272. static void lockdep_set_quota_inode(struct inode *inode, int subclass)
  6273. {
  6274. struct ext4_inode_info *ei = EXT4_I(inode);
  6275. /* The first argument of lockdep_set_subclass has to be
  6276. * *exactly* the same as the argument to init_rwsem() --- in
  6277. * this case, in init_once() --- or lockdep gets unhappy
  6278. * because the name of the lock is set using the
  6279. * stringification of the argument to init_rwsem().
  6280. */
  6281. (void) ei; /* shut up clang warning if !CONFIG_LOCKDEP */
  6282. lockdep_set_subclass(&ei->i_data_sem, subclass);
  6283. }
  6284. /*
  6285. * Standard function to be called on quota_on
  6286. */
  6287. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  6288. const struct path *path)
  6289. {
  6290. int err;
  6291. if (!test_opt(sb, QUOTA))
  6292. return -EINVAL;
  6293. /* Quotafile not on the same filesystem? */
  6294. if (path->dentry->d_sb != sb)
  6295. return -EXDEV;
  6296. /* Quota already enabled for this file? */
  6297. if (IS_NOQUOTA(d_inode(path->dentry)))
  6298. return -EBUSY;
  6299. /* Journaling quota? */
  6300. if (EXT4_SB(sb)->s_qf_names[type]) {
  6301. /* Quotafile not in fs root? */
  6302. if (path->dentry->d_parent != sb->s_root)
  6303. ext4_msg(sb, KERN_WARNING,
  6304. "Quota file not on filesystem root. "
  6305. "Journaled quota will not work");
  6306. sb_dqopt(sb)->flags |= DQUOT_NOLIST_DIRTY;
  6307. } else {
  6308. /*
  6309. * Clear the flag just in case mount options changed since
  6310. * last time.
  6311. */
  6312. sb_dqopt(sb)->flags &= ~DQUOT_NOLIST_DIRTY;
  6313. }
  6314. lockdep_set_quota_inode(path->dentry->d_inode, I_DATA_SEM_QUOTA);
  6315. err = dquot_quota_on(sb, type, format_id, path);
  6316. if (!err) {
  6317. struct inode *inode = d_inode(path->dentry);
  6318. handle_t *handle;
  6319. /*
  6320. * Set inode flags to prevent userspace from messing with quota
  6321. * files. If this fails, we return success anyway since quotas
  6322. * are already enabled and this is not a hard failure.
  6323. */
  6324. inode_lock(inode);
  6325. handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
  6326. if (IS_ERR(handle))
  6327. goto unlock_inode;
  6328. EXT4_I(inode)->i_flags |= EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL;
  6329. inode_set_flags(inode, S_NOATIME | S_IMMUTABLE,
  6330. S_NOATIME | S_IMMUTABLE);
  6331. err = ext4_mark_inode_dirty(handle, inode);
  6332. ext4_journal_stop(handle);
  6333. unlock_inode:
  6334. inode_unlock(inode);
  6335. if (err)
  6336. dquot_quota_off(sb, type);
  6337. }
  6338. if (err)
  6339. lockdep_set_quota_inode(path->dentry->d_inode,
  6340. I_DATA_SEM_NORMAL);
  6341. return err;
  6342. }
  6343. static inline bool ext4_check_quota_inum(int type, unsigned long qf_inum)
  6344. {
  6345. switch (type) {
  6346. case USRQUOTA:
  6347. return qf_inum == EXT4_USR_QUOTA_INO;
  6348. case GRPQUOTA:
  6349. return qf_inum == EXT4_GRP_QUOTA_INO;
  6350. case PRJQUOTA:
  6351. return qf_inum >= EXT4_GOOD_OLD_FIRST_INO;
  6352. default:
  6353. BUG();
  6354. }
  6355. }
  6356. static int ext4_quota_enable(struct super_block *sb, int type, int format_id,
  6357. unsigned int flags)
  6358. {
  6359. int err;
  6360. struct inode *qf_inode;
  6361. unsigned long qf_inums[EXT4_MAXQUOTAS] = {
  6362. le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
  6363. le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
  6364. le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
  6365. };
  6366. BUG_ON(!ext4_has_feature_quota(sb));
  6367. if (!qf_inums[type])
  6368. return -EPERM;
  6369. if (!ext4_check_quota_inum(type, qf_inums[type])) {
  6370. ext4_error(sb, "Bad quota inum: %lu, type: %d",
  6371. qf_inums[type], type);
  6372. return -EUCLEAN;
  6373. }
  6374. qf_inode = ext4_iget(sb, qf_inums[type], EXT4_IGET_SPECIAL);
  6375. if (IS_ERR(qf_inode)) {
  6376. ext4_error(sb, "Bad quota inode: %lu, type: %d",
  6377. qf_inums[type], type);
  6378. return PTR_ERR(qf_inode);
  6379. }
  6380. /* Don't account quota for quota files to avoid recursion */
  6381. qf_inode->i_flags |= S_NOQUOTA;
  6382. lockdep_set_quota_inode(qf_inode, I_DATA_SEM_QUOTA);
  6383. err = dquot_load_quota_inode(qf_inode, type, format_id, flags);
  6384. if (err)
  6385. lockdep_set_quota_inode(qf_inode, I_DATA_SEM_NORMAL);
  6386. iput(qf_inode);
  6387. return err;
  6388. }
  6389. /* Enable usage tracking for all quota types. */
  6390. int ext4_enable_quotas(struct super_block *sb)
  6391. {
  6392. int type, err = 0;
  6393. unsigned long qf_inums[EXT4_MAXQUOTAS] = {
  6394. le32_to_cpu(EXT4_SB(sb)->s_es->s_usr_quota_inum),
  6395. le32_to_cpu(EXT4_SB(sb)->s_es->s_grp_quota_inum),
  6396. le32_to_cpu(EXT4_SB(sb)->s_es->s_prj_quota_inum)
  6397. };
  6398. bool quota_mopt[EXT4_MAXQUOTAS] = {
  6399. test_opt(sb, USRQUOTA),
  6400. test_opt(sb, GRPQUOTA),
  6401. test_opt(sb, PRJQUOTA),
  6402. };
  6403. sb_dqopt(sb)->flags |= DQUOT_QUOTA_SYS_FILE | DQUOT_NOLIST_DIRTY;
  6404. for (type = 0; type < EXT4_MAXQUOTAS; type++) {
  6405. if (qf_inums[type]) {
  6406. err = ext4_quota_enable(sb, type, QFMT_VFS_V1,
  6407. DQUOT_USAGE_ENABLED |
  6408. (quota_mopt[type] ? DQUOT_LIMITS_ENABLED : 0));
  6409. if (err) {
  6410. ext4_warning(sb,
  6411. "Failed to enable quota tracking "
  6412. "(type=%d, err=%d, ino=%lu). "
  6413. "Please run e2fsck to fix.", type,
  6414. err, qf_inums[type]);
  6415. ext4_quotas_off(sb, type);
  6416. return err;
  6417. }
  6418. }
  6419. }
  6420. return 0;
  6421. }
  6422. static int ext4_quota_off(struct super_block *sb, int type)
  6423. {
  6424. struct inode *inode = sb_dqopt(sb)->files[type];
  6425. handle_t *handle;
  6426. int err;
  6427. /* Force all delayed allocation blocks to be allocated.
  6428. * Caller already holds s_umount sem */
  6429. if (test_opt(sb, DELALLOC))
  6430. sync_filesystem(sb);
  6431. if (!inode || !igrab(inode))
  6432. goto out;
  6433. err = dquot_quota_off(sb, type);
  6434. if (err || ext4_has_feature_quota(sb))
  6435. goto out_put;
  6436. /*
  6437. * When the filesystem was remounted read-only first, we cannot cleanup
  6438. * inode flags here. Bad luck but people should be using QUOTA feature
  6439. * these days anyway.
  6440. */
  6441. if (sb_rdonly(sb))
  6442. goto out_put;
  6443. inode_lock(inode);
  6444. /*
  6445. * Update modification times of quota files when userspace can
  6446. * start looking at them. If we fail, we return success anyway since
  6447. * this is not a hard failure and quotas are already disabled.
  6448. */
  6449. handle = ext4_journal_start(inode, EXT4_HT_QUOTA, 1);
  6450. if (IS_ERR(handle)) {
  6451. err = PTR_ERR(handle);
  6452. goto out_unlock;
  6453. }
  6454. EXT4_I(inode)->i_flags &= ~(EXT4_NOATIME_FL | EXT4_IMMUTABLE_FL);
  6455. inode_set_flags(inode, 0, S_NOATIME | S_IMMUTABLE);
  6456. inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
  6457. err = ext4_mark_inode_dirty(handle, inode);
  6458. ext4_journal_stop(handle);
  6459. out_unlock:
  6460. inode_unlock(inode);
  6461. out_put:
  6462. lockdep_set_quota_inode(inode, I_DATA_SEM_NORMAL);
  6463. iput(inode);
  6464. return err;
  6465. out:
  6466. return dquot_quota_off(sb, type);
  6467. }
  6468. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  6469. * acquiring the locks... As quota files are never truncated and quota code
  6470. * itself serializes the operations (and no one else should touch the files)
  6471. * we don't have to be afraid of races */
  6472. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  6473. size_t len, loff_t off)
  6474. {
  6475. struct inode *inode = sb_dqopt(sb)->files[type];
  6476. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  6477. int offset = off & (sb->s_blocksize - 1);
  6478. int tocopy;
  6479. size_t toread;
  6480. struct buffer_head *bh;
  6481. loff_t i_size = i_size_read(inode);
  6482. if (off > i_size)
  6483. return 0;
  6484. if (off+len > i_size)
  6485. len = i_size-off;
  6486. toread = len;
  6487. while (toread > 0) {
  6488. tocopy = min_t(unsigned long, sb->s_blocksize - offset, toread);
  6489. bh = ext4_bread(NULL, inode, blk, 0);
  6490. if (IS_ERR(bh))
  6491. return PTR_ERR(bh);
  6492. if (!bh) /* A hole? */
  6493. memset(data, 0, tocopy);
  6494. else
  6495. memcpy(data, bh->b_data+offset, tocopy);
  6496. brelse(bh);
  6497. offset = 0;
  6498. toread -= tocopy;
  6499. data += tocopy;
  6500. blk++;
  6501. }
  6502. return len;
  6503. }
  6504. /* Write to quotafile (we know the transaction is already started and has
  6505. * enough credits) */
  6506. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  6507. const char *data, size_t len, loff_t off)
  6508. {
  6509. struct inode *inode = sb_dqopt(sb)->files[type];
  6510. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  6511. int err = 0, err2 = 0, offset = off & (sb->s_blocksize - 1);
  6512. int retries = 0;
  6513. struct buffer_head *bh;
  6514. handle_t *handle = journal_current_handle();
  6515. if (!handle) {
  6516. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  6517. " cancelled because transaction is not started",
  6518. (unsigned long long)off, (unsigned long long)len);
  6519. return -EIO;
  6520. }
  6521. /*
  6522. * Since we account only one data block in transaction credits,
  6523. * then it is impossible to cross a block boundary.
  6524. */
  6525. if (sb->s_blocksize - offset < len) {
  6526. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  6527. " cancelled because not block aligned",
  6528. (unsigned long long)off, (unsigned long long)len);
  6529. return -EIO;
  6530. }
  6531. do {
  6532. bh = ext4_bread(handle, inode, blk,
  6533. EXT4_GET_BLOCKS_CREATE |
  6534. EXT4_GET_BLOCKS_METADATA_NOFAIL);
  6535. } while (PTR_ERR(bh) == -ENOSPC &&
  6536. ext4_should_retry_alloc(inode->i_sb, &retries));
  6537. if (IS_ERR(bh))
  6538. return PTR_ERR(bh);
  6539. if (!bh)
  6540. goto out;
  6541. BUFFER_TRACE(bh, "get write access");
  6542. err = ext4_journal_get_write_access(handle, sb, bh, EXT4_JTR_NONE);
  6543. if (err) {
  6544. brelse(bh);
  6545. return err;
  6546. }
  6547. lock_buffer(bh);
  6548. memcpy(bh->b_data+offset, data, len);
  6549. flush_dcache_folio(bh->b_folio);
  6550. unlock_buffer(bh);
  6551. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  6552. brelse(bh);
  6553. out:
  6554. if (inode->i_size < off + len) {
  6555. i_size_write(inode, off + len);
  6556. EXT4_I(inode)->i_disksize = inode->i_size;
  6557. err2 = ext4_mark_inode_dirty(handle, inode);
  6558. if (unlikely(err2 && !err))
  6559. err = err2;
  6560. }
  6561. return err ? err : len;
  6562. }
  6563. #endif
  6564. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT2)
  6565. static inline void register_as_ext2(void)
  6566. {
  6567. int err = register_filesystem(&ext2_fs_type);
  6568. if (err)
  6569. printk(KERN_WARNING
  6570. "EXT4-fs: Unable to register as ext2 (%d)\n", err);
  6571. }
  6572. static inline void unregister_as_ext2(void)
  6573. {
  6574. unregister_filesystem(&ext2_fs_type);
  6575. }
  6576. static inline int ext2_feature_set_ok(struct super_block *sb)
  6577. {
  6578. if (ext4_has_unknown_ext2_incompat_features(sb))
  6579. return 0;
  6580. if (sb_rdonly(sb))
  6581. return 1;
  6582. if (ext4_has_unknown_ext2_ro_compat_features(sb))
  6583. return 0;
  6584. return 1;
  6585. }
  6586. #else
  6587. static inline void register_as_ext2(void) { }
  6588. static inline void unregister_as_ext2(void) { }
  6589. static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
  6590. #endif
  6591. static inline void register_as_ext3(void)
  6592. {
  6593. int err = register_filesystem(&ext3_fs_type);
  6594. if (err)
  6595. printk(KERN_WARNING
  6596. "EXT4-fs: Unable to register as ext3 (%d)\n", err);
  6597. }
  6598. static inline void unregister_as_ext3(void)
  6599. {
  6600. unregister_filesystem(&ext3_fs_type);
  6601. }
  6602. static inline int ext3_feature_set_ok(struct super_block *sb)
  6603. {
  6604. if (ext4_has_unknown_ext3_incompat_features(sb))
  6605. return 0;
  6606. if (!ext4_has_feature_journal(sb))
  6607. return 0;
  6608. if (sb_rdonly(sb))
  6609. return 1;
  6610. if (ext4_has_unknown_ext3_ro_compat_features(sb))
  6611. return 0;
  6612. return 1;
  6613. }
  6614. static void ext4_kill_sb(struct super_block *sb)
  6615. {
  6616. struct ext4_sb_info *sbi = EXT4_SB(sb);
  6617. struct file *bdev_file = sbi ? sbi->s_journal_bdev_file : NULL;
  6618. kill_block_super(sb);
  6619. if (bdev_file)
  6620. bdev_fput(bdev_file);
  6621. }
  6622. static struct file_system_type ext4_fs_type = {
  6623. .owner = THIS_MODULE,
  6624. .name = "ext4",
  6625. .init_fs_context = ext4_init_fs_context,
  6626. .parameters = ext4_param_specs,
  6627. .kill_sb = ext4_kill_sb,
  6628. .fs_flags = FS_REQUIRES_DEV | FS_ALLOW_IDMAP | FS_MGTIME |
  6629. FS_LBS,
  6630. };
  6631. MODULE_ALIAS_FS("ext4");
  6632. static int __init ext4_init_fs(void)
  6633. {
  6634. int err;
  6635. ratelimit_state_init(&ext4_mount_msg_ratelimit, 30 * HZ, 64);
  6636. ext4_li_info = NULL;
  6637. /* Build-time check for flags consistency */
  6638. ext4_check_flag_values();
  6639. err = ext4_init_es();
  6640. if (err)
  6641. return err;
  6642. err = ext4_init_pending();
  6643. if (err)
  6644. goto out7;
  6645. err = ext4_init_post_read_processing();
  6646. if (err)
  6647. goto out6;
  6648. err = ext4_init_pageio();
  6649. if (err)
  6650. goto out5;
  6651. err = ext4_init_system_zone();
  6652. if (err)
  6653. goto out4;
  6654. err = ext4_init_sysfs();
  6655. if (err)
  6656. goto out3;
  6657. err = ext4_init_mballoc();
  6658. if (err)
  6659. goto out2;
  6660. err = init_inodecache();
  6661. if (err)
  6662. goto out1;
  6663. err = ext4_fc_init_dentry_cache();
  6664. if (err)
  6665. goto out05;
  6666. register_as_ext3();
  6667. register_as_ext2();
  6668. err = register_filesystem(&ext4_fs_type);
  6669. if (err)
  6670. goto out;
  6671. return 0;
  6672. out:
  6673. unregister_as_ext2();
  6674. unregister_as_ext3();
  6675. ext4_fc_destroy_dentry_cache();
  6676. out05:
  6677. destroy_inodecache();
  6678. out1:
  6679. ext4_exit_mballoc();
  6680. out2:
  6681. ext4_exit_sysfs();
  6682. out3:
  6683. ext4_exit_system_zone();
  6684. out4:
  6685. ext4_exit_pageio();
  6686. out5:
  6687. ext4_exit_post_read_processing();
  6688. out6:
  6689. ext4_exit_pending();
  6690. out7:
  6691. ext4_exit_es();
  6692. return err;
  6693. }
  6694. static void __exit ext4_exit_fs(void)
  6695. {
  6696. ext4_destroy_lazyinit_thread();
  6697. unregister_as_ext2();
  6698. unregister_as_ext3();
  6699. unregister_filesystem(&ext4_fs_type);
  6700. ext4_fc_destroy_dentry_cache();
  6701. destroy_inodecache();
  6702. ext4_exit_mballoc();
  6703. ext4_exit_sysfs();
  6704. ext4_exit_system_zone();
  6705. ext4_exit_pageio();
  6706. ext4_exit_post_read_processing();
  6707. ext4_exit_es();
  6708. ext4_exit_pending();
  6709. }
  6710. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  6711. MODULE_DESCRIPTION("Fourth Extended Filesystem");
  6712. MODULE_LICENSE("GPL");
  6713. module_init(ext4_init_fs)
  6714. module_exit(ext4_exit_fs)