super.c 173 KB

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