super.c 165 KB

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