super.c 208 KB

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