super.c 137 KB

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