super.c 166 KB

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