super.c 140 KB

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