super.c 150 KB

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