super.c 163 KB

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