super.c 142 KB

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