super.c 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028
  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. * However there is other limiting factor. We do store extents in the form
  2025. * of starting block and length, hence the resulting length of the extent
  2026. * covering maximum file size must fit into on-disk format containers as
  2027. * well. Given that length is always by 1 unit bigger than max unit (because
  2028. * we count 0 as well) we have to lower the s_maxbytes by one fs block.
  2029. *
  2030. * Note, this does *not* consider any metadata overhead for vfs i_blocks.
  2031. */
  2032. static loff_t ext4_max_size(int blkbits, int has_huge_files)
  2033. {
  2034. loff_t res;
  2035. loff_t upper_limit = MAX_LFS_FILESIZE;
  2036. /* small i_blocks in vfs inode? */
  2037. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  2038. /*
  2039. * CONFIG_LBDAF is not enabled implies the inode
  2040. * i_block represent total blocks in 512 bytes
  2041. * 32 == size of vfs inode i_blocks * 8
  2042. */
  2043. upper_limit = (1LL << 32) - 1;
  2044. /* total blocks in file system block size */
  2045. upper_limit >>= (blkbits - 9);
  2046. upper_limit <<= blkbits;
  2047. }
  2048. /*
  2049. * 32-bit extent-start container, ee_block. We lower the maxbytes
  2050. * by one fs block, so ee_len can cover the extent of maximum file
  2051. * size
  2052. */
  2053. res = (1LL << 32) - 1;
  2054. res <<= blkbits;
  2055. /* Sanity check against vm- & vfs- imposed limits */
  2056. if (res > upper_limit)
  2057. res = upper_limit;
  2058. return res;
  2059. }
  2060. /*
  2061. * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
  2062. * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
  2063. * We need to be 1 filesystem block less than the 2^48 sector limit.
  2064. */
  2065. static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
  2066. {
  2067. loff_t res = EXT4_NDIR_BLOCKS;
  2068. int meta_blocks;
  2069. loff_t upper_limit;
  2070. /* This is calculated to be the largest file size for a dense, block
  2071. * mapped file such that the file's total number of 512-byte sectors,
  2072. * including data and all indirect blocks, does not exceed (2^48 - 1).
  2073. *
  2074. * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
  2075. * number of 512-byte sectors of the file.
  2076. */
  2077. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  2078. /*
  2079. * !has_huge_files or CONFIG_LBDAF not enabled implies that
  2080. * the inode i_block field represents total file blocks in
  2081. * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
  2082. */
  2083. upper_limit = (1LL << 32) - 1;
  2084. /* total blocks in file system block size */
  2085. upper_limit >>= (bits - 9);
  2086. } else {
  2087. /*
  2088. * We use 48 bit ext4_inode i_blocks
  2089. * With EXT4_HUGE_FILE_FL set the i_blocks
  2090. * represent total number of blocks in
  2091. * file system block size
  2092. */
  2093. upper_limit = (1LL << 48) - 1;
  2094. }
  2095. /* indirect blocks */
  2096. meta_blocks = 1;
  2097. /* double indirect blocks */
  2098. meta_blocks += 1 + (1LL << (bits-2));
  2099. /* tripple indirect blocks */
  2100. meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
  2101. upper_limit -= meta_blocks;
  2102. upper_limit <<= bits;
  2103. res += 1LL << (bits-2);
  2104. res += 1LL << (2*(bits-2));
  2105. res += 1LL << (3*(bits-2));
  2106. res <<= bits;
  2107. if (res > upper_limit)
  2108. res = upper_limit;
  2109. if (res > MAX_LFS_FILESIZE)
  2110. res = MAX_LFS_FILESIZE;
  2111. return res;
  2112. }
  2113. static ext4_fsblk_t descriptor_loc(struct super_block *sb,
  2114. ext4_fsblk_t logical_sb_block, int nr)
  2115. {
  2116. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2117. ext4_group_t bg, first_meta_bg;
  2118. int has_super = 0;
  2119. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  2120. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
  2121. nr < first_meta_bg)
  2122. return logical_sb_block + nr + 1;
  2123. bg = sbi->s_desc_per_block * nr;
  2124. if (ext4_bg_has_super(sb, bg))
  2125. has_super = 1;
  2126. return (has_super + ext4_group_first_block_no(sb, bg));
  2127. }
  2128. /**
  2129. * ext4_get_stripe_size: Get the stripe size.
  2130. * @sbi: In memory super block info
  2131. *
  2132. * If we have specified it via mount option, then
  2133. * use the mount option value. If the value specified at mount time is
  2134. * greater than the blocks per group use the super block value.
  2135. * If the super block value is greater than blocks per group return 0.
  2136. * Allocator needs it be less than blocks per group.
  2137. *
  2138. */
  2139. static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
  2140. {
  2141. unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
  2142. unsigned long stripe_width =
  2143. le32_to_cpu(sbi->s_es->s_raid_stripe_width);
  2144. if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
  2145. return sbi->s_stripe;
  2146. if (stripe_width <= sbi->s_blocks_per_group)
  2147. return stripe_width;
  2148. if (stride <= sbi->s_blocks_per_group)
  2149. return stride;
  2150. return 0;
  2151. }
  2152. /* sysfs supprt */
  2153. struct ext4_attr {
  2154. struct attribute attr;
  2155. ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
  2156. ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
  2157. const char *, size_t);
  2158. int offset;
  2159. };
  2160. static int parse_strtoul(const char *buf,
  2161. unsigned long max, unsigned long *value)
  2162. {
  2163. char *endp;
  2164. *value = simple_strtoul(skip_spaces(buf), &endp, 0);
  2165. endp = skip_spaces(endp);
  2166. if (*endp || *value > max)
  2167. return -EINVAL;
  2168. return 0;
  2169. }
  2170. static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
  2171. struct ext4_sb_info *sbi,
  2172. char *buf)
  2173. {
  2174. return snprintf(buf, PAGE_SIZE, "%llu\n",
  2175. (s64) percpu_counter_sum(&sbi->s_dirtyblocks_counter));
  2176. }
  2177. static ssize_t session_write_kbytes_show(struct ext4_attr *a,
  2178. struct ext4_sb_info *sbi, char *buf)
  2179. {
  2180. struct super_block *sb = sbi->s_buddy_cache->i_sb;
  2181. if (!sb->s_bdev->bd_part)
  2182. return snprintf(buf, PAGE_SIZE, "0\n");
  2183. return snprintf(buf, PAGE_SIZE, "%lu\n",
  2184. (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2185. sbi->s_sectors_written_start) >> 1);
  2186. }
  2187. static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
  2188. struct ext4_sb_info *sbi, char *buf)
  2189. {
  2190. struct super_block *sb = sbi->s_buddy_cache->i_sb;
  2191. if (!sb->s_bdev->bd_part)
  2192. return snprintf(buf, PAGE_SIZE, "0\n");
  2193. return snprintf(buf, PAGE_SIZE, "%llu\n",
  2194. (unsigned long long)(sbi->s_kbytes_written +
  2195. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2196. EXT4_SB(sb)->s_sectors_written_start) >> 1)));
  2197. }
  2198. static ssize_t extent_cache_hits_show(struct ext4_attr *a,
  2199. struct ext4_sb_info *sbi, char *buf)
  2200. {
  2201. return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_hits);
  2202. }
  2203. static ssize_t extent_cache_misses_show(struct ext4_attr *a,
  2204. struct ext4_sb_info *sbi, char *buf)
  2205. {
  2206. return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_misses);
  2207. }
  2208. static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
  2209. struct ext4_sb_info *sbi,
  2210. const char *buf, size_t count)
  2211. {
  2212. unsigned long t;
  2213. if (parse_strtoul(buf, 0x40000000, &t))
  2214. return -EINVAL;
  2215. if (t && !is_power_of_2(t))
  2216. return -EINVAL;
  2217. sbi->s_inode_readahead_blks = t;
  2218. return count;
  2219. }
  2220. static ssize_t sbi_ui_show(struct ext4_attr *a,
  2221. struct ext4_sb_info *sbi, char *buf)
  2222. {
  2223. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2224. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  2225. }
  2226. static ssize_t sbi_ui_store(struct ext4_attr *a,
  2227. struct ext4_sb_info *sbi,
  2228. const char *buf, size_t count)
  2229. {
  2230. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2231. unsigned long t;
  2232. if (parse_strtoul(buf, 0xffffffff, &t))
  2233. return -EINVAL;
  2234. *ui = t;
  2235. return count;
  2236. }
  2237. #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
  2238. static struct ext4_attr ext4_attr_##_name = { \
  2239. .attr = {.name = __stringify(_name), .mode = _mode }, \
  2240. .show = _show, \
  2241. .store = _store, \
  2242. .offset = offsetof(struct ext4_sb_info, _elname), \
  2243. }
  2244. #define EXT4_ATTR(name, mode, show, store) \
  2245. static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
  2246. #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
  2247. #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
  2248. #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
  2249. #define EXT4_RW_ATTR_SBI_UI(name, elname) \
  2250. EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
  2251. #define ATTR_LIST(name) &ext4_attr_##name.attr
  2252. EXT4_RO_ATTR(delayed_allocation_blocks);
  2253. EXT4_RO_ATTR(session_write_kbytes);
  2254. EXT4_RO_ATTR(lifetime_write_kbytes);
  2255. EXT4_RO_ATTR(extent_cache_hits);
  2256. EXT4_RO_ATTR(extent_cache_misses);
  2257. EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
  2258. inode_readahead_blks_store, s_inode_readahead_blks);
  2259. EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
  2260. EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
  2261. EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
  2262. EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
  2263. EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
  2264. EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
  2265. EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
  2266. EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
  2267. static struct attribute *ext4_attrs[] = {
  2268. ATTR_LIST(delayed_allocation_blocks),
  2269. ATTR_LIST(session_write_kbytes),
  2270. ATTR_LIST(lifetime_write_kbytes),
  2271. ATTR_LIST(extent_cache_hits),
  2272. ATTR_LIST(extent_cache_misses),
  2273. ATTR_LIST(inode_readahead_blks),
  2274. ATTR_LIST(inode_goal),
  2275. ATTR_LIST(mb_stats),
  2276. ATTR_LIST(mb_max_to_scan),
  2277. ATTR_LIST(mb_min_to_scan),
  2278. ATTR_LIST(mb_order2_req),
  2279. ATTR_LIST(mb_stream_req),
  2280. ATTR_LIST(mb_group_prealloc),
  2281. ATTR_LIST(max_writeback_mb_bump),
  2282. NULL,
  2283. };
  2284. /* Features this copy of ext4 supports */
  2285. EXT4_INFO_ATTR(lazy_itable_init);
  2286. EXT4_INFO_ATTR(batched_discard);
  2287. static struct attribute *ext4_feat_attrs[] = {
  2288. ATTR_LIST(lazy_itable_init),
  2289. ATTR_LIST(batched_discard),
  2290. NULL,
  2291. };
  2292. static ssize_t ext4_attr_show(struct kobject *kobj,
  2293. struct attribute *attr, char *buf)
  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->show ? a->show(a, sbi, buf) : 0;
  2299. }
  2300. static ssize_t ext4_attr_store(struct kobject *kobj,
  2301. struct attribute *attr,
  2302. const char *buf, size_t len)
  2303. {
  2304. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2305. s_kobj);
  2306. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2307. return a->store ? a->store(a, sbi, buf, len) : 0;
  2308. }
  2309. static void ext4_sb_release(struct kobject *kobj)
  2310. {
  2311. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2312. s_kobj);
  2313. complete(&sbi->s_kobj_unregister);
  2314. }
  2315. static const struct sysfs_ops ext4_attr_ops = {
  2316. .show = ext4_attr_show,
  2317. .store = ext4_attr_store,
  2318. };
  2319. static struct kobj_type ext4_ktype = {
  2320. .default_attrs = ext4_attrs,
  2321. .sysfs_ops = &ext4_attr_ops,
  2322. .release = ext4_sb_release,
  2323. };
  2324. static void ext4_feat_release(struct kobject *kobj)
  2325. {
  2326. complete(&ext4_feat->f_kobj_unregister);
  2327. }
  2328. static struct kobj_type ext4_feat_ktype = {
  2329. .default_attrs = ext4_feat_attrs,
  2330. .sysfs_ops = &ext4_attr_ops,
  2331. .release = ext4_feat_release,
  2332. };
  2333. /*
  2334. * Check whether this filesystem can be mounted based on
  2335. * the features present and the RDONLY/RDWR mount requested.
  2336. * Returns 1 if this filesystem can be mounted as requested,
  2337. * 0 if it cannot be.
  2338. */
  2339. static int ext4_feature_set_ok(struct super_block *sb, int readonly)
  2340. {
  2341. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
  2342. ext4_msg(sb, KERN_ERR,
  2343. "Couldn't mount because of "
  2344. "unsupported optional features (%x)",
  2345. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
  2346. ~EXT4_FEATURE_INCOMPAT_SUPP));
  2347. return 0;
  2348. }
  2349. if (readonly)
  2350. return 1;
  2351. /* Check that feature set is OK for a read-write mount */
  2352. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
  2353. ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
  2354. "unsupported optional features (%x)",
  2355. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
  2356. ~EXT4_FEATURE_RO_COMPAT_SUPP));
  2357. return 0;
  2358. }
  2359. /*
  2360. * Large file size enabled file system can only be mounted
  2361. * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
  2362. */
  2363. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  2364. if (sizeof(blkcnt_t) < sizeof(u64)) {
  2365. ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
  2366. "cannot be mounted RDWR without "
  2367. "CONFIG_LBDAF");
  2368. return 0;
  2369. }
  2370. }
  2371. return 1;
  2372. }
  2373. /*
  2374. * This function is called once a day if we have errors logged
  2375. * on the file system
  2376. */
  2377. static void print_daily_error_info(unsigned long arg)
  2378. {
  2379. struct super_block *sb = (struct super_block *) arg;
  2380. struct ext4_sb_info *sbi;
  2381. struct ext4_super_block *es;
  2382. sbi = EXT4_SB(sb);
  2383. es = sbi->s_es;
  2384. if (es->s_error_count)
  2385. ext4_msg(sb, KERN_NOTICE, "error count: %u",
  2386. le32_to_cpu(es->s_error_count));
  2387. if (es->s_first_error_time) {
  2388. printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
  2389. sb->s_id, le32_to_cpu(es->s_first_error_time),
  2390. (int) sizeof(es->s_first_error_func),
  2391. es->s_first_error_func,
  2392. le32_to_cpu(es->s_first_error_line));
  2393. if (es->s_first_error_ino)
  2394. printk(": inode %u",
  2395. le32_to_cpu(es->s_first_error_ino));
  2396. if (es->s_first_error_block)
  2397. printk(": block %llu", (unsigned long long)
  2398. le64_to_cpu(es->s_first_error_block));
  2399. printk("\n");
  2400. }
  2401. if (es->s_last_error_time) {
  2402. printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
  2403. sb->s_id, le32_to_cpu(es->s_last_error_time),
  2404. (int) sizeof(es->s_last_error_func),
  2405. es->s_last_error_func,
  2406. le32_to_cpu(es->s_last_error_line));
  2407. if (es->s_last_error_ino)
  2408. printk(": inode %u",
  2409. le32_to_cpu(es->s_last_error_ino));
  2410. if (es->s_last_error_block)
  2411. printk(": block %llu", (unsigned long long)
  2412. le64_to_cpu(es->s_last_error_block));
  2413. printk("\n");
  2414. }
  2415. mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
  2416. }
  2417. /* Find next suitable group and run ext4_init_inode_table */
  2418. static int ext4_run_li_request(struct ext4_li_request *elr)
  2419. {
  2420. struct ext4_group_desc *gdp = NULL;
  2421. ext4_group_t group, ngroups;
  2422. struct super_block *sb;
  2423. unsigned long timeout = 0;
  2424. int ret = 0;
  2425. sb = elr->lr_super;
  2426. ngroups = EXT4_SB(sb)->s_groups_count;
  2427. for (group = elr->lr_next_group; group < ngroups; group++) {
  2428. gdp = ext4_get_group_desc(sb, group, NULL);
  2429. if (!gdp) {
  2430. ret = 1;
  2431. break;
  2432. }
  2433. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2434. break;
  2435. }
  2436. if (group == ngroups)
  2437. ret = 1;
  2438. if (!ret) {
  2439. timeout = jiffies;
  2440. ret = ext4_init_inode_table(sb, group,
  2441. elr->lr_timeout ? 0 : 1);
  2442. if (elr->lr_timeout == 0) {
  2443. timeout = (jiffies - timeout) *
  2444. elr->lr_sbi->s_li_wait_mult;
  2445. elr->lr_timeout = timeout;
  2446. }
  2447. elr->lr_next_sched = jiffies + elr->lr_timeout;
  2448. elr->lr_next_group = group + 1;
  2449. }
  2450. return ret;
  2451. }
  2452. /*
  2453. * Remove lr_request from the list_request and free the
  2454. * request structure. Should be called with li_list_mtx held
  2455. */
  2456. static void ext4_remove_li_request(struct ext4_li_request *elr)
  2457. {
  2458. struct ext4_sb_info *sbi;
  2459. if (!elr)
  2460. return;
  2461. sbi = elr->lr_sbi;
  2462. list_del(&elr->lr_request);
  2463. sbi->s_li_request = NULL;
  2464. kfree(elr);
  2465. }
  2466. static void ext4_unregister_li_request(struct super_block *sb)
  2467. {
  2468. mutex_lock(&ext4_li_mtx);
  2469. if (!ext4_li_info) {
  2470. mutex_unlock(&ext4_li_mtx);
  2471. return;
  2472. }
  2473. mutex_lock(&ext4_li_info->li_list_mtx);
  2474. ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
  2475. mutex_unlock(&ext4_li_info->li_list_mtx);
  2476. mutex_unlock(&ext4_li_mtx);
  2477. }
  2478. static struct task_struct *ext4_lazyinit_task;
  2479. /*
  2480. * This is the function where ext4lazyinit thread lives. It walks
  2481. * through the request list searching for next scheduled filesystem.
  2482. * When such a fs is found, run the lazy initialization request
  2483. * (ext4_rn_li_request) and keep track of the time spend in this
  2484. * function. Based on that time we compute next schedule time of
  2485. * the request. When walking through the list is complete, compute
  2486. * next waking time and put itself into sleep.
  2487. */
  2488. static int ext4_lazyinit_thread(void *arg)
  2489. {
  2490. struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
  2491. struct list_head *pos, *n;
  2492. struct ext4_li_request *elr;
  2493. unsigned long next_wakeup, cur;
  2494. BUG_ON(NULL == eli);
  2495. cont_thread:
  2496. while (true) {
  2497. next_wakeup = MAX_JIFFY_OFFSET;
  2498. mutex_lock(&eli->li_list_mtx);
  2499. if (list_empty(&eli->li_request_list)) {
  2500. mutex_unlock(&eli->li_list_mtx);
  2501. goto exit_thread;
  2502. }
  2503. list_for_each_safe(pos, n, &eli->li_request_list) {
  2504. elr = list_entry(pos, struct ext4_li_request,
  2505. lr_request);
  2506. if (time_after_eq(jiffies, elr->lr_next_sched)) {
  2507. if (ext4_run_li_request(elr) != 0) {
  2508. /* error, remove the lazy_init job */
  2509. ext4_remove_li_request(elr);
  2510. continue;
  2511. }
  2512. }
  2513. if (time_before(elr->lr_next_sched, next_wakeup))
  2514. next_wakeup = elr->lr_next_sched;
  2515. }
  2516. mutex_unlock(&eli->li_list_mtx);
  2517. if (freezing(current))
  2518. refrigerator();
  2519. cur = jiffies;
  2520. if ((time_after_eq(cur, next_wakeup)) ||
  2521. (MAX_JIFFY_OFFSET == next_wakeup)) {
  2522. cond_resched();
  2523. continue;
  2524. }
  2525. schedule_timeout_interruptible(next_wakeup - cur);
  2526. if (kthread_should_stop()) {
  2527. ext4_clear_request_list();
  2528. goto exit_thread;
  2529. }
  2530. }
  2531. exit_thread:
  2532. /*
  2533. * It looks like the request list is empty, but we need
  2534. * to check it under the li_list_mtx lock, to prevent any
  2535. * additions into it, and of course we should lock ext4_li_mtx
  2536. * to atomically free the list and ext4_li_info, because at
  2537. * this point another ext4 filesystem could be registering
  2538. * new one.
  2539. */
  2540. mutex_lock(&ext4_li_mtx);
  2541. mutex_lock(&eli->li_list_mtx);
  2542. if (!list_empty(&eli->li_request_list)) {
  2543. mutex_unlock(&eli->li_list_mtx);
  2544. mutex_unlock(&ext4_li_mtx);
  2545. goto cont_thread;
  2546. }
  2547. mutex_unlock(&eli->li_list_mtx);
  2548. kfree(ext4_li_info);
  2549. ext4_li_info = NULL;
  2550. mutex_unlock(&ext4_li_mtx);
  2551. return 0;
  2552. }
  2553. static void ext4_clear_request_list(void)
  2554. {
  2555. struct list_head *pos, *n;
  2556. struct ext4_li_request *elr;
  2557. mutex_lock(&ext4_li_info->li_list_mtx);
  2558. list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
  2559. elr = list_entry(pos, struct ext4_li_request,
  2560. lr_request);
  2561. ext4_remove_li_request(elr);
  2562. }
  2563. mutex_unlock(&ext4_li_info->li_list_mtx);
  2564. }
  2565. static int ext4_run_lazyinit_thread(void)
  2566. {
  2567. ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
  2568. ext4_li_info, "ext4lazyinit");
  2569. if (IS_ERR(ext4_lazyinit_task)) {
  2570. int err = PTR_ERR(ext4_lazyinit_task);
  2571. ext4_clear_request_list();
  2572. kfree(ext4_li_info);
  2573. ext4_li_info = NULL;
  2574. printk(KERN_CRIT "EXT4: error %d creating inode table "
  2575. "initialization thread\n",
  2576. err);
  2577. return err;
  2578. }
  2579. ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
  2580. return 0;
  2581. }
  2582. /*
  2583. * Check whether it make sense to run itable init. thread or not.
  2584. * If there is at least one uninitialized inode table, return
  2585. * corresponding group number, else the loop goes through all
  2586. * groups and return total number of groups.
  2587. */
  2588. static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
  2589. {
  2590. ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
  2591. struct ext4_group_desc *gdp = NULL;
  2592. for (group = 0; group < ngroups; group++) {
  2593. gdp = ext4_get_group_desc(sb, group, NULL);
  2594. if (!gdp)
  2595. continue;
  2596. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2597. break;
  2598. }
  2599. return group;
  2600. }
  2601. static int ext4_li_info_new(void)
  2602. {
  2603. struct ext4_lazy_init *eli = NULL;
  2604. eli = kzalloc(sizeof(*eli), GFP_KERNEL);
  2605. if (!eli)
  2606. return -ENOMEM;
  2607. INIT_LIST_HEAD(&eli->li_request_list);
  2608. mutex_init(&eli->li_list_mtx);
  2609. eli->li_state |= EXT4_LAZYINIT_QUIT;
  2610. ext4_li_info = eli;
  2611. return 0;
  2612. }
  2613. static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
  2614. ext4_group_t start)
  2615. {
  2616. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2617. struct ext4_li_request *elr;
  2618. unsigned long rnd;
  2619. elr = kzalloc(sizeof(*elr), GFP_KERNEL);
  2620. if (!elr)
  2621. return NULL;
  2622. elr->lr_super = sb;
  2623. elr->lr_sbi = sbi;
  2624. elr->lr_next_group = start;
  2625. /*
  2626. * Randomize first schedule time of the request to
  2627. * spread the inode table initialization requests
  2628. * better.
  2629. */
  2630. get_random_bytes(&rnd, sizeof(rnd));
  2631. elr->lr_next_sched = jiffies + (unsigned long)rnd %
  2632. (EXT4_DEF_LI_MAX_START_DELAY * HZ);
  2633. return elr;
  2634. }
  2635. static int ext4_register_li_request(struct super_block *sb,
  2636. ext4_group_t first_not_zeroed)
  2637. {
  2638. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2639. struct ext4_li_request *elr;
  2640. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  2641. int ret = 0;
  2642. if (sbi->s_li_request != NULL) {
  2643. /*
  2644. * Reset timeout so it can be computed again, because
  2645. * s_li_wait_mult might have changed.
  2646. */
  2647. sbi->s_li_request->lr_timeout = 0;
  2648. return 0;
  2649. }
  2650. if (first_not_zeroed == ngroups ||
  2651. (sb->s_flags & MS_RDONLY) ||
  2652. !test_opt(sb, INIT_INODE_TABLE))
  2653. return 0;
  2654. elr = ext4_li_request_new(sb, first_not_zeroed);
  2655. if (!elr)
  2656. return -ENOMEM;
  2657. mutex_lock(&ext4_li_mtx);
  2658. if (NULL == ext4_li_info) {
  2659. ret = ext4_li_info_new();
  2660. if (ret)
  2661. goto out;
  2662. }
  2663. mutex_lock(&ext4_li_info->li_list_mtx);
  2664. list_add(&elr->lr_request, &ext4_li_info->li_request_list);
  2665. mutex_unlock(&ext4_li_info->li_list_mtx);
  2666. sbi->s_li_request = elr;
  2667. /*
  2668. * set elr to NULL here since it has been inserted to
  2669. * the request_list and the removal and free of it is
  2670. * handled by ext4_clear_request_list from now on.
  2671. */
  2672. elr = NULL;
  2673. if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
  2674. ret = ext4_run_lazyinit_thread();
  2675. if (ret)
  2676. goto out;
  2677. }
  2678. out:
  2679. mutex_unlock(&ext4_li_mtx);
  2680. if (ret)
  2681. kfree(elr);
  2682. return ret;
  2683. }
  2684. /*
  2685. * We do not need to lock anything since this is called on
  2686. * module unload.
  2687. */
  2688. static void ext4_destroy_lazyinit_thread(void)
  2689. {
  2690. /*
  2691. * If thread exited earlier
  2692. * there's nothing to be done.
  2693. */
  2694. if (!ext4_li_info || !ext4_lazyinit_task)
  2695. return;
  2696. kthread_stop(ext4_lazyinit_task);
  2697. }
  2698. static int ext4_fill_super(struct super_block *sb, void *data, int silent)
  2699. __releases(kernel_lock)
  2700. __acquires(kernel_lock)
  2701. {
  2702. char *orig_data = kstrdup(data, GFP_KERNEL);
  2703. struct buffer_head *bh;
  2704. struct ext4_super_block *es = NULL;
  2705. struct ext4_sb_info *sbi;
  2706. ext4_fsblk_t block;
  2707. ext4_fsblk_t sb_block = get_sb_block(&data);
  2708. ext4_fsblk_t logical_sb_block;
  2709. unsigned long offset = 0;
  2710. unsigned long journal_devnum = 0;
  2711. unsigned long def_mount_opts;
  2712. struct inode *root;
  2713. char *cp;
  2714. const char *descr;
  2715. int ret = -ENOMEM;
  2716. int blocksize;
  2717. unsigned int db_count;
  2718. unsigned int i;
  2719. int needs_recovery, has_huge_files;
  2720. __u64 blocks_count;
  2721. int err;
  2722. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  2723. ext4_group_t first_not_zeroed;
  2724. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  2725. if (!sbi)
  2726. goto out_free_orig;
  2727. sbi->s_blockgroup_lock =
  2728. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  2729. if (!sbi->s_blockgroup_lock) {
  2730. kfree(sbi);
  2731. goto out_free_orig;
  2732. }
  2733. sb->s_fs_info = sbi;
  2734. sbi->s_mount_opt = 0;
  2735. sbi->s_resuid = EXT4_DEF_RESUID;
  2736. sbi->s_resgid = EXT4_DEF_RESGID;
  2737. sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
  2738. sbi->s_sb_block = sb_block;
  2739. if (sb->s_bdev->bd_part)
  2740. sbi->s_sectors_written_start =
  2741. part_stat_read(sb->s_bdev->bd_part, sectors[1]);
  2742. /* Cleanup superblock name */
  2743. for (cp = sb->s_id; (cp = strchr(cp, '/'));)
  2744. *cp = '!';
  2745. ret = -EINVAL;
  2746. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  2747. if (!blocksize) {
  2748. ext4_msg(sb, KERN_ERR, "unable to set blocksize");
  2749. goto out_fail;
  2750. }
  2751. /*
  2752. * The ext4 superblock will not be buffer aligned for other than 1kB
  2753. * block sizes. We need to calculate the offset from buffer start.
  2754. */
  2755. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  2756. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2757. offset = do_div(logical_sb_block, blocksize);
  2758. } else {
  2759. logical_sb_block = sb_block;
  2760. }
  2761. if (!(bh = sb_bread(sb, logical_sb_block))) {
  2762. ext4_msg(sb, KERN_ERR, "unable to read superblock");
  2763. goto out_fail;
  2764. }
  2765. /*
  2766. * Note: s_es must be initialized as soon as possible because
  2767. * some ext4 macro-instructions depend on its value
  2768. */
  2769. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  2770. sbi->s_es = es;
  2771. sb->s_magic = le16_to_cpu(es->s_magic);
  2772. if (sb->s_magic != EXT4_SUPER_MAGIC)
  2773. goto cantfind_ext4;
  2774. sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
  2775. /* Set defaults before we parse the mount options */
  2776. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  2777. set_opt(sb, INIT_INODE_TABLE);
  2778. if (def_mount_opts & EXT4_DEFM_DEBUG)
  2779. set_opt(sb, DEBUG);
  2780. if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
  2781. ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
  2782. "2.6.38");
  2783. set_opt(sb, GRPID);
  2784. }
  2785. if (def_mount_opts & EXT4_DEFM_UID16)
  2786. set_opt(sb, NO_UID32);
  2787. /* xattr user namespace & acls are now defaulted on */
  2788. #ifdef CONFIG_EXT4_FS_XATTR
  2789. set_opt(sb, XATTR_USER);
  2790. #endif
  2791. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  2792. set_opt(sb, POSIX_ACL);
  2793. #endif
  2794. set_opt(sb, MBLK_IO_SUBMIT);
  2795. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  2796. set_opt(sb, JOURNAL_DATA);
  2797. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  2798. set_opt(sb, ORDERED_DATA);
  2799. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  2800. set_opt(sb, WRITEBACK_DATA);
  2801. if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
  2802. set_opt(sb, ERRORS_PANIC);
  2803. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
  2804. set_opt(sb, ERRORS_CONT);
  2805. else
  2806. set_opt(sb, ERRORS_RO);
  2807. if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
  2808. set_opt(sb, BLOCK_VALIDITY);
  2809. if (def_mount_opts & EXT4_DEFM_DISCARD)
  2810. set_opt(sb, DISCARD);
  2811. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  2812. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  2813. sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
  2814. sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
  2815. sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
  2816. if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
  2817. set_opt(sb, BARRIER);
  2818. /*
  2819. * enable delayed allocation by default
  2820. * Use -o nodelalloc to turn it off
  2821. */
  2822. if (!IS_EXT3_SB(sb) &&
  2823. ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
  2824. set_opt(sb, DELALLOC);
  2825. /*
  2826. * set default s_li_wait_mult for lazyinit, for the case there is
  2827. * no mount option specified.
  2828. */
  2829. sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
  2830. if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
  2831. &journal_devnum, &journal_ioprio, NULL, 0)) {
  2832. ext4_msg(sb, KERN_WARNING,
  2833. "failed to parse options in superblock: %s",
  2834. sbi->s_es->s_mount_opts);
  2835. }
  2836. if (!parse_options((char *) data, sb, &journal_devnum,
  2837. &journal_ioprio, NULL, 0))
  2838. goto failed_mount;
  2839. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2840. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  2841. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  2842. (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
  2843. EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  2844. EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  2845. ext4_msg(sb, KERN_WARNING,
  2846. "feature flags set on rev 0 fs, "
  2847. "running e2fsck is recommended");
  2848. if (IS_EXT2_SB(sb)) {
  2849. if (ext2_feature_set_ok(sb))
  2850. ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
  2851. "using the ext4 subsystem");
  2852. else {
  2853. ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
  2854. "to feature incompatibilities");
  2855. goto failed_mount;
  2856. }
  2857. }
  2858. if (IS_EXT3_SB(sb)) {
  2859. if (ext3_feature_set_ok(sb))
  2860. ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
  2861. "using the ext4 subsystem");
  2862. else {
  2863. ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
  2864. "to feature incompatibilities");
  2865. goto failed_mount;
  2866. }
  2867. }
  2868. /*
  2869. * Check feature flags regardless of the revision level, since we
  2870. * previously didn't change the revision level when setting the flags,
  2871. * so there is a chance incompat flags are set on a rev 0 filesystem.
  2872. */
  2873. if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
  2874. goto failed_mount;
  2875. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  2876. if (blocksize < EXT4_MIN_BLOCK_SIZE ||
  2877. blocksize > EXT4_MAX_BLOCK_SIZE) {
  2878. ext4_msg(sb, KERN_ERR,
  2879. "Unsupported filesystem blocksize %d", blocksize);
  2880. goto failed_mount;
  2881. }
  2882. if (sb->s_blocksize != blocksize) {
  2883. /* Validate the filesystem blocksize */
  2884. if (!sb_set_blocksize(sb, blocksize)) {
  2885. ext4_msg(sb, KERN_ERR, "bad block size %d",
  2886. blocksize);
  2887. goto failed_mount;
  2888. }
  2889. brelse(bh);
  2890. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2891. offset = do_div(logical_sb_block, blocksize);
  2892. bh = sb_bread(sb, logical_sb_block);
  2893. if (!bh) {
  2894. ext4_msg(sb, KERN_ERR,
  2895. "Can't read superblock on 2nd try");
  2896. goto failed_mount;
  2897. }
  2898. es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
  2899. sbi->s_es = es;
  2900. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  2901. ext4_msg(sb, KERN_ERR,
  2902. "Magic mismatch, very weird!");
  2903. goto failed_mount;
  2904. }
  2905. }
  2906. has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2907. EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
  2908. sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
  2909. has_huge_files);
  2910. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
  2911. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  2912. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  2913. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  2914. } else {
  2915. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  2916. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  2917. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  2918. (!is_power_of_2(sbi->s_inode_size)) ||
  2919. (sbi->s_inode_size > blocksize)) {
  2920. ext4_msg(sb, KERN_ERR,
  2921. "unsupported inode size: %d",
  2922. sbi->s_inode_size);
  2923. goto failed_mount;
  2924. }
  2925. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
  2926. sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
  2927. }
  2928. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  2929. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
  2930. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  2931. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  2932. !is_power_of_2(sbi->s_desc_size)) {
  2933. ext4_msg(sb, KERN_ERR,
  2934. "unsupported descriptor size %lu",
  2935. sbi->s_desc_size);
  2936. goto failed_mount;
  2937. }
  2938. } else
  2939. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  2940. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  2941. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  2942. if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
  2943. goto cantfind_ext4;
  2944. sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
  2945. if (sbi->s_inodes_per_block == 0)
  2946. goto cantfind_ext4;
  2947. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  2948. sbi->s_inodes_per_block;
  2949. sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
  2950. sbi->s_sbh = bh;
  2951. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2952. sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
  2953. sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
  2954. for (i = 0; i < 4; i++)
  2955. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  2956. sbi->s_def_hash_version = es->s_def_hash_version;
  2957. i = le32_to_cpu(es->s_flags);
  2958. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  2959. sbi->s_hash_unsigned = 3;
  2960. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  2961. #ifdef __CHAR_UNSIGNED__
  2962. es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  2963. sbi->s_hash_unsigned = 3;
  2964. #else
  2965. es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  2966. #endif
  2967. sb->s_dirt = 1;
  2968. }
  2969. if (sbi->s_blocks_per_group > blocksize * 8) {
  2970. ext4_msg(sb, KERN_ERR,
  2971. "#blocks per group too big: %lu",
  2972. sbi->s_blocks_per_group);
  2973. goto failed_mount;
  2974. }
  2975. if (sbi->s_inodes_per_group > blocksize * 8) {
  2976. ext4_msg(sb, KERN_ERR,
  2977. "#inodes per group too big: %lu",
  2978. sbi->s_inodes_per_group);
  2979. goto failed_mount;
  2980. }
  2981. /*
  2982. * Test whether we have more sectors than will fit in sector_t,
  2983. * and whether the max offset is addressable by the page cache.
  2984. */
  2985. err = generic_check_addressable(sb->s_blocksize_bits,
  2986. ext4_blocks_count(es));
  2987. if (err) {
  2988. ext4_msg(sb, KERN_ERR, "filesystem"
  2989. " too large to mount safely on this system");
  2990. if (sizeof(sector_t) < 8)
  2991. ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
  2992. ret = err;
  2993. goto failed_mount;
  2994. }
  2995. if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
  2996. goto cantfind_ext4;
  2997. /* check blocks count against device size */
  2998. blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  2999. if (blocks_count && ext4_blocks_count(es) > blocks_count) {
  3000. ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
  3001. "exceeds size of device (%llu blocks)",
  3002. ext4_blocks_count(es), blocks_count);
  3003. goto failed_mount;
  3004. }
  3005. /*
  3006. * It makes no sense for the first data block to be beyond the end
  3007. * of the filesystem.
  3008. */
  3009. if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
  3010. ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
  3011. "block %u is beyond end of filesystem (%llu)",
  3012. le32_to_cpu(es->s_first_data_block),
  3013. ext4_blocks_count(es));
  3014. goto failed_mount;
  3015. }
  3016. blocks_count = (ext4_blocks_count(es) -
  3017. le32_to_cpu(es->s_first_data_block) +
  3018. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  3019. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  3020. if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
  3021. ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
  3022. "(block count %llu, first data block %u, "
  3023. "blocks per group %lu)", sbi->s_groups_count,
  3024. ext4_blocks_count(es),
  3025. le32_to_cpu(es->s_first_data_block),
  3026. EXT4_BLOCKS_PER_GROUP(sb));
  3027. goto failed_mount;
  3028. }
  3029. sbi->s_groups_count = blocks_count;
  3030. sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
  3031. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  3032. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  3033. EXT4_DESC_PER_BLOCK(sb);
  3034. sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
  3035. GFP_KERNEL);
  3036. if (sbi->s_group_desc == NULL) {
  3037. ext4_msg(sb, KERN_ERR, "not enough memory");
  3038. goto failed_mount;
  3039. }
  3040. #ifdef CONFIG_PROC_FS
  3041. if (ext4_proc_root)
  3042. sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
  3043. #endif
  3044. bgl_lock_init(sbi->s_blockgroup_lock);
  3045. for (i = 0; i < db_count; i++) {
  3046. block = descriptor_loc(sb, logical_sb_block, i);
  3047. sbi->s_group_desc[i] = sb_bread(sb, block);
  3048. if (!sbi->s_group_desc[i]) {
  3049. ext4_msg(sb, KERN_ERR,
  3050. "can't read group descriptor %d", i);
  3051. db_count = i;
  3052. goto failed_mount2;
  3053. }
  3054. }
  3055. if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
  3056. ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
  3057. goto failed_mount2;
  3058. }
  3059. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
  3060. if (!ext4_fill_flex_info(sb)) {
  3061. ext4_msg(sb, KERN_ERR,
  3062. "unable to initialize "
  3063. "flex_bg meta info!");
  3064. goto failed_mount2;
  3065. }
  3066. sbi->s_gdb_count = db_count;
  3067. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  3068. spin_lock_init(&sbi->s_next_gen_lock);
  3069. init_timer(&sbi->s_err_report);
  3070. sbi->s_err_report.function = print_daily_error_info;
  3071. sbi->s_err_report.data = (unsigned long) sb;
  3072. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  3073. ext4_count_free_blocks(sb));
  3074. if (!err) {
  3075. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  3076. ext4_count_free_inodes(sb));
  3077. }
  3078. if (!err) {
  3079. err = percpu_counter_init(&sbi->s_dirs_counter,
  3080. ext4_count_dirs(sb));
  3081. }
  3082. if (!err) {
  3083. err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
  3084. }
  3085. if (err) {
  3086. ext4_msg(sb, KERN_ERR, "insufficient memory");
  3087. goto failed_mount3;
  3088. }
  3089. sbi->s_stripe = ext4_get_stripe_size(sbi);
  3090. sbi->s_max_writeback_mb_bump = 128;
  3091. /*
  3092. * set up enough so that it can read an inode
  3093. */
  3094. if (!test_opt(sb, NOLOAD) &&
  3095. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  3096. sb->s_op = &ext4_sops;
  3097. else
  3098. sb->s_op = &ext4_nojournal_sops;
  3099. sb->s_export_op = &ext4_export_ops;
  3100. sb->s_xattr = ext4_xattr_handlers;
  3101. #ifdef CONFIG_QUOTA
  3102. sb->s_qcop = &ext4_qctl_operations;
  3103. sb->dq_op = &ext4_quota_operations;
  3104. #endif
  3105. memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
  3106. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  3107. mutex_init(&sbi->s_orphan_lock);
  3108. mutex_init(&sbi->s_resize_lock);
  3109. sb->s_root = NULL;
  3110. needs_recovery = (es->s_last_orphan != 0 ||
  3111. EXT4_HAS_INCOMPAT_FEATURE(sb,
  3112. EXT4_FEATURE_INCOMPAT_RECOVER));
  3113. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
  3114. !(sb->s_flags & MS_RDONLY))
  3115. if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
  3116. goto failed_mount3;
  3117. /*
  3118. * The first inode we look at is the journal inode. Don't try
  3119. * root first: it may be modified in the journal!
  3120. */
  3121. if (!test_opt(sb, NOLOAD) &&
  3122. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3123. if (ext4_load_journal(sb, es, journal_devnum))
  3124. goto failed_mount3;
  3125. } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
  3126. EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  3127. ext4_msg(sb, KERN_ERR, "required journal recovery "
  3128. "suppressed and not mounted read-only");
  3129. goto failed_mount_wq;
  3130. } else {
  3131. clear_opt(sb, DATA_FLAGS);
  3132. sbi->s_journal = NULL;
  3133. needs_recovery = 0;
  3134. goto no_journal;
  3135. }
  3136. if (ext4_blocks_count(es) > 0xffffffffULL &&
  3137. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  3138. JBD2_FEATURE_INCOMPAT_64BIT)) {
  3139. ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
  3140. goto failed_mount_wq;
  3141. }
  3142. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  3143. jbd2_journal_set_features(sbi->s_journal,
  3144. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  3145. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3146. } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
  3147. jbd2_journal_set_features(sbi->s_journal,
  3148. JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
  3149. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  3150. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3151. } else {
  3152. jbd2_journal_clear_features(sbi->s_journal,
  3153. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  3154. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3155. }
  3156. /* We have now updated the journal if required, so we can
  3157. * validate the data journaling mode. */
  3158. switch (test_opt(sb, DATA_FLAGS)) {
  3159. case 0:
  3160. /* No mode set, assume a default based on the journal
  3161. * capabilities: ORDERED_DATA if the journal can
  3162. * cope, else JOURNAL_DATA
  3163. */
  3164. if (jbd2_journal_check_available_features
  3165. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
  3166. set_opt(sb, ORDERED_DATA);
  3167. else
  3168. set_opt(sb, JOURNAL_DATA);
  3169. break;
  3170. case EXT4_MOUNT_ORDERED_DATA:
  3171. case EXT4_MOUNT_WRITEBACK_DATA:
  3172. if (!jbd2_journal_check_available_features
  3173. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  3174. ext4_msg(sb, KERN_ERR, "Journal does not support "
  3175. "requested data journaling mode");
  3176. goto failed_mount_wq;
  3177. }
  3178. default:
  3179. break;
  3180. }
  3181. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3182. /*
  3183. * The journal may have updated the bg summary counts, so we
  3184. * need to update the global counters.
  3185. */
  3186. percpu_counter_set(&sbi->s_freeblocks_counter,
  3187. ext4_count_free_blocks(sb));
  3188. percpu_counter_set(&sbi->s_freeinodes_counter,
  3189. ext4_count_free_inodes(sb));
  3190. percpu_counter_set(&sbi->s_dirs_counter,
  3191. ext4_count_dirs(sb));
  3192. percpu_counter_set(&sbi->s_dirtyblocks_counter, 0);
  3193. no_journal:
  3194. /*
  3195. * The maximum number of concurrent works can be high and
  3196. * concurrency isn't really necessary. Limit it to 1.
  3197. */
  3198. EXT4_SB(sb)->dio_unwritten_wq =
  3199. alloc_workqueue("ext4-dio-unwritten", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  3200. if (!EXT4_SB(sb)->dio_unwritten_wq) {
  3201. printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
  3202. goto failed_mount_wq;
  3203. }
  3204. /*
  3205. * The jbd2_journal_load will have done any necessary log recovery,
  3206. * so we can safely mount the rest of the filesystem now.
  3207. */
  3208. root = ext4_iget(sb, EXT4_ROOT_INO);
  3209. if (IS_ERR(root)) {
  3210. ext4_msg(sb, KERN_ERR, "get root inode failed");
  3211. ret = PTR_ERR(root);
  3212. root = NULL;
  3213. goto failed_mount4;
  3214. }
  3215. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  3216. ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
  3217. goto failed_mount4;
  3218. }
  3219. sb->s_root = d_alloc_root(root);
  3220. if (!sb->s_root) {
  3221. ext4_msg(sb, KERN_ERR, "get root dentry failed");
  3222. ret = -ENOMEM;
  3223. goto failed_mount4;
  3224. }
  3225. ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
  3226. /* determine the minimum size of new large inodes, if present */
  3227. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
  3228. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3229. EXT4_GOOD_OLD_INODE_SIZE;
  3230. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3231. EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
  3232. if (sbi->s_want_extra_isize <
  3233. le16_to_cpu(es->s_want_extra_isize))
  3234. sbi->s_want_extra_isize =
  3235. le16_to_cpu(es->s_want_extra_isize);
  3236. if (sbi->s_want_extra_isize <
  3237. le16_to_cpu(es->s_min_extra_isize))
  3238. sbi->s_want_extra_isize =
  3239. le16_to_cpu(es->s_min_extra_isize);
  3240. }
  3241. }
  3242. /* Check if enough inode space is available */
  3243. if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
  3244. sbi->s_inode_size) {
  3245. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3246. EXT4_GOOD_OLD_INODE_SIZE;
  3247. ext4_msg(sb, KERN_INFO, "required extra inode space not"
  3248. "available");
  3249. }
  3250. if (test_opt(sb, DELALLOC) &&
  3251. (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
  3252. ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
  3253. "requested data journaling mode");
  3254. clear_opt(sb, DELALLOC);
  3255. }
  3256. if (test_opt(sb, DIOREAD_NOLOCK)) {
  3257. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  3258. ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
  3259. "option - requested data journaling mode");
  3260. clear_opt(sb, DIOREAD_NOLOCK);
  3261. }
  3262. if (sb->s_blocksize < PAGE_SIZE) {
  3263. ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
  3264. "option - block size is too small");
  3265. clear_opt(sb, DIOREAD_NOLOCK);
  3266. }
  3267. }
  3268. err = ext4_setup_system_zone(sb);
  3269. if (err) {
  3270. ext4_msg(sb, KERN_ERR, "failed to initialize system "
  3271. "zone (%d)", err);
  3272. goto failed_mount4;
  3273. }
  3274. ext4_ext_init(sb);
  3275. err = ext4_mb_init(sb, needs_recovery);
  3276. if (err) {
  3277. ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
  3278. err);
  3279. goto failed_mount4;
  3280. }
  3281. err = ext4_register_li_request(sb, first_not_zeroed);
  3282. if (err)
  3283. goto failed_mount4;
  3284. sbi->s_kobj.kset = ext4_kset;
  3285. init_completion(&sbi->s_kobj_unregister);
  3286. err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
  3287. "%s", sb->s_id);
  3288. if (err) {
  3289. ext4_mb_release(sb);
  3290. ext4_ext_release(sb);
  3291. goto failed_mount4;
  3292. };
  3293. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  3294. ext4_orphan_cleanup(sb, es);
  3295. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  3296. if (needs_recovery) {
  3297. ext4_msg(sb, KERN_INFO, "recovery complete");
  3298. ext4_mark_recovery_complete(sb, es);
  3299. }
  3300. if (EXT4_SB(sb)->s_journal) {
  3301. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  3302. descr = " journalled data mode";
  3303. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  3304. descr = " ordered data mode";
  3305. else
  3306. descr = " writeback data mode";
  3307. } else
  3308. descr = "out journal";
  3309. ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
  3310. "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
  3311. *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
  3312. if (es->s_error_count)
  3313. mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
  3314. kfree(orig_data);
  3315. return 0;
  3316. cantfind_ext4:
  3317. if (!silent)
  3318. ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
  3319. goto failed_mount;
  3320. failed_mount4:
  3321. iput(root);
  3322. sb->s_root = NULL;
  3323. ext4_msg(sb, KERN_ERR, "mount failed");
  3324. destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
  3325. failed_mount_wq:
  3326. ext4_release_system_zone(sb);
  3327. if (sbi->s_journal) {
  3328. jbd2_journal_destroy(sbi->s_journal);
  3329. sbi->s_journal = NULL;
  3330. }
  3331. failed_mount3:
  3332. del_timer(&sbi->s_err_report);
  3333. if (sbi->s_flex_groups) {
  3334. if (is_vmalloc_addr(sbi->s_flex_groups))
  3335. vfree(sbi->s_flex_groups);
  3336. else
  3337. kfree(sbi->s_flex_groups);
  3338. }
  3339. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  3340. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  3341. percpu_counter_destroy(&sbi->s_dirs_counter);
  3342. percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
  3343. if (sbi->s_mmp_tsk)
  3344. kthread_stop(sbi->s_mmp_tsk);
  3345. failed_mount2:
  3346. for (i = 0; i < db_count; i++)
  3347. brelse(sbi->s_group_desc[i]);
  3348. kfree(sbi->s_group_desc);
  3349. failed_mount:
  3350. if (sbi->s_proc) {
  3351. remove_proc_entry(sb->s_id, ext4_proc_root);
  3352. }
  3353. #ifdef CONFIG_QUOTA
  3354. for (i = 0; i < MAXQUOTAS; i++)
  3355. kfree(sbi->s_qf_names[i]);
  3356. #endif
  3357. ext4_blkdev_remove(sbi);
  3358. brelse(bh);
  3359. out_fail:
  3360. sb->s_fs_info = NULL;
  3361. kfree(sbi->s_blockgroup_lock);
  3362. kfree(sbi);
  3363. out_free_orig:
  3364. kfree(orig_data);
  3365. return ret;
  3366. }
  3367. /*
  3368. * Setup any per-fs journal parameters now. We'll do this both on
  3369. * initial mount, once the journal has been initialised but before we've
  3370. * done any recovery; and again on any subsequent remount.
  3371. */
  3372. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  3373. {
  3374. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3375. journal->j_commit_interval = sbi->s_commit_interval;
  3376. journal->j_min_batch_time = sbi->s_min_batch_time;
  3377. journal->j_max_batch_time = sbi->s_max_batch_time;
  3378. write_lock(&journal->j_state_lock);
  3379. if (test_opt(sb, BARRIER))
  3380. journal->j_flags |= JBD2_BARRIER;
  3381. else
  3382. journal->j_flags &= ~JBD2_BARRIER;
  3383. if (test_opt(sb, DATA_ERR_ABORT))
  3384. journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
  3385. else
  3386. journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
  3387. write_unlock(&journal->j_state_lock);
  3388. }
  3389. static journal_t *ext4_get_journal(struct super_block *sb,
  3390. unsigned int journal_inum)
  3391. {
  3392. struct inode *journal_inode;
  3393. journal_t *journal;
  3394. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3395. /* First, test for the existence of a valid inode on disk. Bad
  3396. * things happen if we iget() an unused inode, as the subsequent
  3397. * iput() will try to delete it. */
  3398. journal_inode = ext4_iget(sb, journal_inum);
  3399. if (IS_ERR(journal_inode)) {
  3400. ext4_msg(sb, KERN_ERR, "no journal found");
  3401. return NULL;
  3402. }
  3403. if (!journal_inode->i_nlink) {
  3404. make_bad_inode(journal_inode);
  3405. iput(journal_inode);
  3406. ext4_msg(sb, KERN_ERR, "journal inode is deleted");
  3407. return NULL;
  3408. }
  3409. jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
  3410. journal_inode, journal_inode->i_size);
  3411. if (!S_ISREG(journal_inode->i_mode)) {
  3412. ext4_msg(sb, KERN_ERR, "invalid journal inode");
  3413. iput(journal_inode);
  3414. return NULL;
  3415. }
  3416. journal = jbd2_journal_init_inode(journal_inode);
  3417. if (!journal) {
  3418. ext4_msg(sb, KERN_ERR, "Could not load journal inode");
  3419. iput(journal_inode);
  3420. return NULL;
  3421. }
  3422. journal->j_private = sb;
  3423. ext4_init_journal_params(sb, journal);
  3424. return journal;
  3425. }
  3426. static journal_t *ext4_get_dev_journal(struct super_block *sb,
  3427. dev_t j_dev)
  3428. {
  3429. struct buffer_head *bh;
  3430. journal_t *journal;
  3431. ext4_fsblk_t start;
  3432. ext4_fsblk_t len;
  3433. int hblock, blocksize;
  3434. ext4_fsblk_t sb_block;
  3435. unsigned long offset;
  3436. struct ext4_super_block *es;
  3437. struct block_device *bdev;
  3438. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3439. bdev = ext4_blkdev_get(j_dev, sb);
  3440. if (bdev == NULL)
  3441. return NULL;
  3442. blocksize = sb->s_blocksize;
  3443. hblock = bdev_logical_block_size(bdev);
  3444. if (blocksize < hblock) {
  3445. ext4_msg(sb, KERN_ERR,
  3446. "blocksize too small for journal device");
  3447. goto out_bdev;
  3448. }
  3449. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  3450. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  3451. set_blocksize(bdev, blocksize);
  3452. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  3453. ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
  3454. "external journal");
  3455. goto out_bdev;
  3456. }
  3457. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  3458. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  3459. !(le32_to_cpu(es->s_feature_incompat) &
  3460. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  3461. ext4_msg(sb, KERN_ERR, "external journal has "
  3462. "bad superblock");
  3463. brelse(bh);
  3464. goto out_bdev;
  3465. }
  3466. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  3467. ext4_msg(sb, KERN_ERR, "journal UUID does not match");
  3468. brelse(bh);
  3469. goto out_bdev;
  3470. }
  3471. len = ext4_blocks_count(es);
  3472. start = sb_block + 1;
  3473. brelse(bh); /* we're done with the superblock */
  3474. journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
  3475. start, len, blocksize);
  3476. if (!journal) {
  3477. ext4_msg(sb, KERN_ERR, "failed to create device journal");
  3478. goto out_bdev;
  3479. }
  3480. journal->j_private = sb;
  3481. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  3482. wait_on_buffer(journal->j_sb_buffer);
  3483. if (!buffer_uptodate(journal->j_sb_buffer)) {
  3484. ext4_msg(sb, KERN_ERR, "I/O error on journal device");
  3485. goto out_journal;
  3486. }
  3487. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  3488. ext4_msg(sb, KERN_ERR, "External journal has more than one "
  3489. "user (unsupported) - %d",
  3490. be32_to_cpu(journal->j_superblock->s_nr_users));
  3491. goto out_journal;
  3492. }
  3493. EXT4_SB(sb)->journal_bdev = bdev;
  3494. ext4_init_journal_params(sb, journal);
  3495. return journal;
  3496. out_journal:
  3497. jbd2_journal_destroy(journal);
  3498. out_bdev:
  3499. ext4_blkdev_put(bdev);
  3500. return NULL;
  3501. }
  3502. static int ext4_load_journal(struct super_block *sb,
  3503. struct ext4_super_block *es,
  3504. unsigned long journal_devnum)
  3505. {
  3506. journal_t *journal;
  3507. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  3508. dev_t journal_dev;
  3509. int err = 0;
  3510. int really_read_only;
  3511. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3512. if (journal_devnum &&
  3513. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3514. ext4_msg(sb, KERN_INFO, "external journal device major/minor "
  3515. "numbers have changed");
  3516. journal_dev = new_decode_dev(journal_devnum);
  3517. } else
  3518. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  3519. really_read_only = bdev_read_only(sb->s_bdev);
  3520. /*
  3521. * Are we loading a blank journal or performing recovery after a
  3522. * crash? For recovery, we need to check in advance whether we
  3523. * can get read-write access to the device.
  3524. */
  3525. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  3526. if (sb->s_flags & MS_RDONLY) {
  3527. ext4_msg(sb, KERN_INFO, "INFO: recovery "
  3528. "required on readonly filesystem");
  3529. if (really_read_only) {
  3530. ext4_msg(sb, KERN_ERR, "write access "
  3531. "unavailable, cannot proceed");
  3532. return -EROFS;
  3533. }
  3534. ext4_msg(sb, KERN_INFO, "write access will "
  3535. "be enabled during recovery");
  3536. }
  3537. }
  3538. if (journal_inum && journal_dev) {
  3539. ext4_msg(sb, KERN_ERR, "filesystem has both journal "
  3540. "and inode journals!");
  3541. return -EINVAL;
  3542. }
  3543. if (journal_inum) {
  3544. if (!(journal = ext4_get_journal(sb, journal_inum)))
  3545. return -EINVAL;
  3546. } else {
  3547. if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
  3548. return -EINVAL;
  3549. }
  3550. if (!(journal->j_flags & JBD2_BARRIER))
  3551. ext4_msg(sb, KERN_INFO, "barriers disabled");
  3552. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  3553. err = jbd2_journal_update_format(journal);
  3554. if (err) {
  3555. ext4_msg(sb, KERN_ERR, "error updating journal");
  3556. jbd2_journal_destroy(journal);
  3557. return err;
  3558. }
  3559. }
  3560. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
  3561. err = jbd2_journal_wipe(journal, !really_read_only);
  3562. if (!err) {
  3563. char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
  3564. if (save)
  3565. memcpy(save, ((char *) es) +
  3566. EXT4_S_ERR_START, EXT4_S_ERR_LEN);
  3567. err = jbd2_journal_load(journal);
  3568. if (save)
  3569. memcpy(((char *) es) + EXT4_S_ERR_START,
  3570. save, EXT4_S_ERR_LEN);
  3571. kfree(save);
  3572. }
  3573. if (err) {
  3574. ext4_msg(sb, KERN_ERR, "error loading journal");
  3575. jbd2_journal_destroy(journal);
  3576. return err;
  3577. }
  3578. EXT4_SB(sb)->s_journal = journal;
  3579. ext4_clear_journal_err(sb, es);
  3580. if (!really_read_only && journal_devnum &&
  3581. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3582. es->s_journal_dev = cpu_to_le32(journal_devnum);
  3583. /* Make sure we flush the recovery flag to disk. */
  3584. ext4_commit_super(sb, 1);
  3585. }
  3586. return 0;
  3587. }
  3588. static int ext4_commit_super(struct super_block *sb, int sync)
  3589. {
  3590. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  3591. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  3592. int error = 0;
  3593. if (!sbh)
  3594. return error;
  3595. if (buffer_write_io_error(sbh)) {
  3596. /*
  3597. * Oh, dear. A previous attempt to write the
  3598. * superblock failed. This could happen because the
  3599. * USB device was yanked out. Or it could happen to
  3600. * be a transient write error and maybe the block will
  3601. * be remapped. Nothing we can do but to retry the
  3602. * write and hope for the best.
  3603. */
  3604. ext4_msg(sb, KERN_ERR, "previous I/O error to "
  3605. "superblock detected");
  3606. clear_buffer_write_io_error(sbh);
  3607. set_buffer_uptodate(sbh);
  3608. }
  3609. /*
  3610. * If the file system is mounted read-only, don't update the
  3611. * superblock write time. This avoids updating the superblock
  3612. * write time when we are mounting the root file system
  3613. * read/only but we need to replay the journal; at that point,
  3614. * for people who are east of GMT and who make their clock
  3615. * tick in localtime for Windows bug-for-bug compatibility,
  3616. * the clock is set in the future, and this will cause e2fsck
  3617. * to complain and force a full file system check.
  3618. */
  3619. if (!(sb->s_flags & MS_RDONLY))
  3620. es->s_wtime = cpu_to_le32(get_seconds());
  3621. if (sb->s_bdev->bd_part)
  3622. es->s_kbytes_written =
  3623. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
  3624. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  3625. EXT4_SB(sb)->s_sectors_written_start) >> 1));
  3626. else
  3627. es->s_kbytes_written =
  3628. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
  3629. ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
  3630. &EXT4_SB(sb)->s_freeblocks_counter));
  3631. es->s_free_inodes_count =
  3632. cpu_to_le32(percpu_counter_sum_positive(
  3633. &EXT4_SB(sb)->s_freeinodes_counter));
  3634. sb->s_dirt = 0;
  3635. BUFFER_TRACE(sbh, "marking dirty");
  3636. mark_buffer_dirty(sbh);
  3637. if (sync) {
  3638. error = sync_dirty_buffer(sbh);
  3639. if (error)
  3640. return error;
  3641. error = buffer_write_io_error(sbh);
  3642. if (error) {
  3643. ext4_msg(sb, KERN_ERR, "I/O error while writing "
  3644. "superblock");
  3645. clear_buffer_write_io_error(sbh);
  3646. set_buffer_uptodate(sbh);
  3647. }
  3648. }
  3649. return error;
  3650. }
  3651. /*
  3652. * Have we just finished recovery? If so, and if we are mounting (or
  3653. * remounting) the filesystem readonly, then we will end up with a
  3654. * consistent fs on disk. Record that fact.
  3655. */
  3656. static void ext4_mark_recovery_complete(struct super_block *sb,
  3657. struct ext4_super_block *es)
  3658. {
  3659. journal_t *journal = EXT4_SB(sb)->s_journal;
  3660. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3661. BUG_ON(journal != NULL);
  3662. return;
  3663. }
  3664. jbd2_journal_lock_updates(journal);
  3665. if (jbd2_journal_flush(journal) < 0)
  3666. goto out;
  3667. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
  3668. sb->s_flags & MS_RDONLY) {
  3669. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3670. ext4_commit_super(sb, 1);
  3671. }
  3672. out:
  3673. jbd2_journal_unlock_updates(journal);
  3674. }
  3675. /*
  3676. * If we are mounting (or read-write remounting) a filesystem whose journal
  3677. * has recorded an error from a previous lifetime, move that error to the
  3678. * main filesystem now.
  3679. */
  3680. static void ext4_clear_journal_err(struct super_block *sb,
  3681. struct ext4_super_block *es)
  3682. {
  3683. journal_t *journal;
  3684. int j_errno;
  3685. const char *errstr;
  3686. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3687. journal = EXT4_SB(sb)->s_journal;
  3688. /*
  3689. * Now check for any error status which may have been recorded in the
  3690. * journal by a prior ext4_error() or ext4_abort()
  3691. */
  3692. j_errno = jbd2_journal_errno(journal);
  3693. if (j_errno) {
  3694. char nbuf[16];
  3695. errstr = ext4_decode_error(sb, j_errno, nbuf);
  3696. ext4_warning(sb, "Filesystem error recorded "
  3697. "from previous mount: %s", errstr);
  3698. ext4_warning(sb, "Marking fs in need of filesystem check.");
  3699. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  3700. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  3701. ext4_commit_super(sb, 1);
  3702. jbd2_journal_clear_err(journal);
  3703. }
  3704. }
  3705. /*
  3706. * Force the running and committing transactions to commit,
  3707. * and wait on the commit.
  3708. */
  3709. int ext4_force_commit(struct super_block *sb)
  3710. {
  3711. journal_t *journal;
  3712. int ret = 0;
  3713. if (sb->s_flags & MS_RDONLY)
  3714. return 0;
  3715. journal = EXT4_SB(sb)->s_journal;
  3716. if (journal) {
  3717. vfs_check_frozen(sb, SB_FREEZE_TRANS);
  3718. ret = ext4_journal_force_commit(journal);
  3719. }
  3720. return ret;
  3721. }
  3722. static void ext4_write_super(struct super_block *sb)
  3723. {
  3724. lock_super(sb);
  3725. ext4_commit_super(sb, 1);
  3726. unlock_super(sb);
  3727. }
  3728. static int ext4_sync_fs(struct super_block *sb, int wait)
  3729. {
  3730. int ret = 0;
  3731. tid_t target;
  3732. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3733. trace_ext4_sync_fs(sb, wait);
  3734. flush_workqueue(sbi->dio_unwritten_wq);
  3735. if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
  3736. if (wait)
  3737. jbd2_log_wait_commit(sbi->s_journal, target);
  3738. }
  3739. return ret;
  3740. }
  3741. /*
  3742. * LVM calls this function before a (read-only) snapshot is created. This
  3743. * gives us a chance to flush the journal completely and mark the fs clean.
  3744. *
  3745. * Note that only this function cannot bring a filesystem to be in a clean
  3746. * state independently, because ext4 prevents a new handle from being started
  3747. * by @sb->s_frozen, which stays in an upper layer. It thus needs help from
  3748. * the upper layer.
  3749. */
  3750. static int ext4_freeze(struct super_block *sb)
  3751. {
  3752. int error = 0;
  3753. journal_t *journal;
  3754. if (sb->s_flags & MS_RDONLY)
  3755. return 0;
  3756. journal = EXT4_SB(sb)->s_journal;
  3757. /* Now we set up the journal barrier. */
  3758. jbd2_journal_lock_updates(journal);
  3759. /*
  3760. * Don't clear the needs_recovery flag if we failed to flush
  3761. * the journal.
  3762. */
  3763. error = jbd2_journal_flush(journal);
  3764. if (error < 0)
  3765. goto out;
  3766. /* Journal blocked and flushed, clear needs_recovery flag. */
  3767. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3768. error = ext4_commit_super(sb, 1);
  3769. out:
  3770. /* we rely on s_frozen to stop further updates */
  3771. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  3772. return error;
  3773. }
  3774. /*
  3775. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  3776. * flag here, even though the filesystem is not technically dirty yet.
  3777. */
  3778. static int ext4_unfreeze(struct super_block *sb)
  3779. {
  3780. if (sb->s_flags & MS_RDONLY)
  3781. return 0;
  3782. lock_super(sb);
  3783. /* Reset the needs_recovery flag before the fs is unlocked. */
  3784. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3785. ext4_commit_super(sb, 1);
  3786. unlock_super(sb);
  3787. return 0;
  3788. }
  3789. /*
  3790. * Structure to save mount options for ext4_remount's benefit
  3791. */
  3792. struct ext4_mount_options {
  3793. unsigned long s_mount_opt;
  3794. unsigned long s_mount_opt2;
  3795. uid_t s_resuid;
  3796. gid_t s_resgid;
  3797. unsigned long s_commit_interval;
  3798. u32 s_min_batch_time, s_max_batch_time;
  3799. #ifdef CONFIG_QUOTA
  3800. int s_jquota_fmt;
  3801. char *s_qf_names[MAXQUOTAS];
  3802. #endif
  3803. };
  3804. static int ext4_remount(struct super_block *sb, int *flags, char *data)
  3805. {
  3806. struct ext4_super_block *es;
  3807. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3808. ext4_fsblk_t n_blocks_count = 0;
  3809. unsigned long old_sb_flags;
  3810. struct ext4_mount_options old_opts;
  3811. int enable_quota = 0;
  3812. ext4_group_t g;
  3813. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  3814. int err = 0;
  3815. #ifdef CONFIG_QUOTA
  3816. int i;
  3817. #endif
  3818. char *orig_data = kstrdup(data, GFP_KERNEL);
  3819. /* Store the original options */
  3820. lock_super(sb);
  3821. old_sb_flags = sb->s_flags;
  3822. old_opts.s_mount_opt = sbi->s_mount_opt;
  3823. old_opts.s_mount_opt2 = sbi->s_mount_opt2;
  3824. old_opts.s_resuid = sbi->s_resuid;
  3825. old_opts.s_resgid = sbi->s_resgid;
  3826. old_opts.s_commit_interval = sbi->s_commit_interval;
  3827. old_opts.s_min_batch_time = sbi->s_min_batch_time;
  3828. old_opts.s_max_batch_time = sbi->s_max_batch_time;
  3829. #ifdef CONFIG_QUOTA
  3830. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  3831. for (i = 0; i < MAXQUOTAS; i++)
  3832. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  3833. #endif
  3834. if (sbi->s_journal && sbi->s_journal->j_task->io_context)
  3835. journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
  3836. /*
  3837. * Allow the "check" option to be passed as a remount option.
  3838. */
  3839. if (!parse_options(data, sb, NULL, &journal_ioprio,
  3840. &n_blocks_count, 1)) {
  3841. err = -EINVAL;
  3842. goto restore_opts;
  3843. }
  3844. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
  3845. ext4_abort(sb, "Abort forced by user");
  3846. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  3847. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  3848. es = sbi->s_es;
  3849. if (sbi->s_journal) {
  3850. ext4_init_journal_params(sb, sbi->s_journal);
  3851. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3852. }
  3853. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  3854. n_blocks_count > ext4_blocks_count(es)) {
  3855. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
  3856. err = -EROFS;
  3857. goto restore_opts;
  3858. }
  3859. if (*flags & MS_RDONLY) {
  3860. err = dquot_suspend(sb, -1);
  3861. if (err < 0)
  3862. goto restore_opts;
  3863. /*
  3864. * First of all, the unconditional stuff we have to do
  3865. * to disable replay of the journal when we next remount
  3866. */
  3867. sb->s_flags |= MS_RDONLY;
  3868. /*
  3869. * OK, test if we are remounting a valid rw partition
  3870. * readonly, and if so set the rdonly flag and then
  3871. * mark the partition as valid again.
  3872. */
  3873. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  3874. (sbi->s_mount_state & EXT4_VALID_FS))
  3875. es->s_state = cpu_to_le16(sbi->s_mount_state);
  3876. if (sbi->s_journal)
  3877. ext4_mark_recovery_complete(sb, es);
  3878. } else {
  3879. /* Make sure we can mount this feature set readwrite */
  3880. if (!ext4_feature_set_ok(sb, 0)) {
  3881. err = -EROFS;
  3882. goto restore_opts;
  3883. }
  3884. /*
  3885. * Make sure the group descriptor checksums
  3886. * are sane. If they aren't, refuse to remount r/w.
  3887. */
  3888. for (g = 0; g < sbi->s_groups_count; g++) {
  3889. struct ext4_group_desc *gdp =
  3890. ext4_get_group_desc(sb, g, NULL);
  3891. if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
  3892. ext4_msg(sb, KERN_ERR,
  3893. "ext4_remount: Checksum for group %u failed (%u!=%u)",
  3894. g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
  3895. le16_to_cpu(gdp->bg_checksum));
  3896. err = -EINVAL;
  3897. goto restore_opts;
  3898. }
  3899. }
  3900. /*
  3901. * If we have an unprocessed orphan list hanging
  3902. * around from a previously readonly bdev mount,
  3903. * require a full umount/remount for now.
  3904. */
  3905. if (es->s_last_orphan) {
  3906. ext4_msg(sb, KERN_WARNING, "Couldn't "
  3907. "remount RDWR because of unprocessed "
  3908. "orphan inode list. Please "
  3909. "umount/remount instead");
  3910. err = -EINVAL;
  3911. goto restore_opts;
  3912. }
  3913. /*
  3914. * Mounting a RDONLY partition read-write, so reread
  3915. * and store the current valid flag. (It may have
  3916. * been changed by e2fsck since we originally mounted
  3917. * the partition.)
  3918. */
  3919. if (sbi->s_journal)
  3920. ext4_clear_journal_err(sb, es);
  3921. sbi->s_mount_state = le16_to_cpu(es->s_state);
  3922. if ((err = ext4_group_extend(sb, es, n_blocks_count)))
  3923. goto restore_opts;
  3924. if (!ext4_setup_super(sb, es, 0))
  3925. sb->s_flags &= ~MS_RDONLY;
  3926. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  3927. EXT4_FEATURE_INCOMPAT_MMP))
  3928. if (ext4_multi_mount_protect(sb,
  3929. le64_to_cpu(es->s_mmp_block))) {
  3930. err = -EROFS;
  3931. goto restore_opts;
  3932. }
  3933. enable_quota = 1;
  3934. }
  3935. }
  3936. /*
  3937. * Reinitialize lazy itable initialization thread based on
  3938. * current settings
  3939. */
  3940. if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
  3941. ext4_unregister_li_request(sb);
  3942. else {
  3943. ext4_group_t first_not_zeroed;
  3944. first_not_zeroed = ext4_has_uninit_itable(sb);
  3945. ext4_register_li_request(sb, first_not_zeroed);
  3946. }
  3947. ext4_setup_system_zone(sb);
  3948. if (sbi->s_journal == NULL)
  3949. ext4_commit_super(sb, 1);
  3950. #ifdef CONFIG_QUOTA
  3951. /* Release old quota file names */
  3952. for (i = 0; i < MAXQUOTAS; i++)
  3953. if (old_opts.s_qf_names[i] &&
  3954. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3955. kfree(old_opts.s_qf_names[i]);
  3956. #endif
  3957. unlock_super(sb);
  3958. if (enable_quota)
  3959. dquot_resume(sb, -1);
  3960. ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
  3961. kfree(orig_data);
  3962. return 0;
  3963. restore_opts:
  3964. sb->s_flags = old_sb_flags;
  3965. sbi->s_mount_opt = old_opts.s_mount_opt;
  3966. sbi->s_mount_opt2 = old_opts.s_mount_opt2;
  3967. sbi->s_resuid = old_opts.s_resuid;
  3968. sbi->s_resgid = old_opts.s_resgid;
  3969. sbi->s_commit_interval = old_opts.s_commit_interval;
  3970. sbi->s_min_batch_time = old_opts.s_min_batch_time;
  3971. sbi->s_max_batch_time = old_opts.s_max_batch_time;
  3972. #ifdef CONFIG_QUOTA
  3973. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  3974. for (i = 0; i < MAXQUOTAS; i++) {
  3975. if (sbi->s_qf_names[i] &&
  3976. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3977. kfree(sbi->s_qf_names[i]);
  3978. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  3979. }
  3980. #endif
  3981. unlock_super(sb);
  3982. kfree(orig_data);
  3983. return err;
  3984. }
  3985. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
  3986. {
  3987. struct super_block *sb = dentry->d_sb;
  3988. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3989. struct ext4_super_block *es = sbi->s_es;
  3990. u64 fsid;
  3991. s64 bfree;
  3992. if (test_opt(sb, MINIX_DF)) {
  3993. sbi->s_overhead_last = 0;
  3994. } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
  3995. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3996. ext4_fsblk_t overhead = 0;
  3997. /*
  3998. * Compute the overhead (FS structures). This is constant
  3999. * for a given filesystem unless the number of block groups
  4000. * changes so we cache the previous value until it does.
  4001. */
  4002. /*
  4003. * All of the blocks before first_data_block are
  4004. * overhead
  4005. */
  4006. overhead = le32_to_cpu(es->s_first_data_block);
  4007. /*
  4008. * Add the overhead attributed to the superblock and
  4009. * block group descriptors. If the sparse superblocks
  4010. * feature is turned on, then not all groups have this.
  4011. */
  4012. for (i = 0; i < ngroups; i++) {
  4013. overhead += ext4_bg_has_super(sb, i) +
  4014. ext4_bg_num_gdb(sb, i);
  4015. cond_resched();
  4016. }
  4017. /*
  4018. * Every block group has an inode bitmap, a block
  4019. * bitmap, and an inode table.
  4020. */
  4021. overhead += ngroups * (2 + sbi->s_itb_per_group);
  4022. sbi->s_overhead_last = overhead;
  4023. smp_wmb();
  4024. sbi->s_blocks_last = ext4_blocks_count(es);
  4025. }
  4026. buf->f_type = EXT4_SUPER_MAGIC;
  4027. buf->f_bsize = sb->s_blocksize;
  4028. buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
  4029. bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
  4030. percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
  4031. /* prevent underflow in case that few free space is available */
  4032. buf->f_bfree = max_t(s64, bfree, 0);
  4033. buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
  4034. if (buf->f_bfree < ext4_r_blocks_count(es))
  4035. buf->f_bavail = 0;
  4036. buf->f_files = le32_to_cpu(es->s_inodes_count);
  4037. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  4038. buf->f_namelen = EXT4_NAME_LEN;
  4039. fsid = le64_to_cpup((void *)es->s_uuid) ^
  4040. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  4041. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  4042. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  4043. return 0;
  4044. }
  4045. /* Helper function for writing quotas on sync - we need to start transaction
  4046. * before quota file is locked for write. Otherwise the are possible deadlocks:
  4047. * Process 1 Process 2
  4048. * ext4_create() quota_sync()
  4049. * jbd2_journal_start() write_dquot()
  4050. * dquot_initialize() down(dqio_mutex)
  4051. * down(dqio_mutex) jbd2_journal_start()
  4052. *
  4053. */
  4054. #ifdef CONFIG_QUOTA
  4055. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  4056. {
  4057. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  4058. }
  4059. static int ext4_write_dquot(struct dquot *dquot)
  4060. {
  4061. int ret, err;
  4062. handle_t *handle;
  4063. struct inode *inode;
  4064. inode = dquot_to_inode(dquot);
  4065. handle = ext4_journal_start(inode,
  4066. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  4067. if (IS_ERR(handle))
  4068. return PTR_ERR(handle);
  4069. ret = dquot_commit(dquot);
  4070. err = ext4_journal_stop(handle);
  4071. if (!ret)
  4072. ret = err;
  4073. return ret;
  4074. }
  4075. static int ext4_acquire_dquot(struct dquot *dquot)
  4076. {
  4077. int ret, err;
  4078. handle_t *handle;
  4079. handle = ext4_journal_start(dquot_to_inode(dquot),
  4080. EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  4081. if (IS_ERR(handle))
  4082. return PTR_ERR(handle);
  4083. ret = dquot_acquire(dquot);
  4084. err = ext4_journal_stop(handle);
  4085. if (!ret)
  4086. ret = err;
  4087. return ret;
  4088. }
  4089. static int ext4_release_dquot(struct dquot *dquot)
  4090. {
  4091. int ret, err;
  4092. handle_t *handle;
  4093. handle = ext4_journal_start(dquot_to_inode(dquot),
  4094. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  4095. if (IS_ERR(handle)) {
  4096. /* Release dquot anyway to avoid endless cycle in dqput() */
  4097. dquot_release(dquot);
  4098. return PTR_ERR(handle);
  4099. }
  4100. ret = dquot_release(dquot);
  4101. err = ext4_journal_stop(handle);
  4102. if (!ret)
  4103. ret = err;
  4104. return ret;
  4105. }
  4106. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  4107. {
  4108. /* Are we journaling quotas? */
  4109. if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  4110. EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  4111. dquot_mark_dquot_dirty(dquot);
  4112. return ext4_write_dquot(dquot);
  4113. } else {
  4114. return dquot_mark_dquot_dirty(dquot);
  4115. }
  4116. }
  4117. static int ext4_write_info(struct super_block *sb, int type)
  4118. {
  4119. int ret, err;
  4120. handle_t *handle;
  4121. /* Data block + inode block */
  4122. handle = ext4_journal_start(sb->s_root->d_inode, 2);
  4123. if (IS_ERR(handle))
  4124. return PTR_ERR(handle);
  4125. ret = dquot_commit_info(sb, type);
  4126. err = ext4_journal_stop(handle);
  4127. if (!ret)
  4128. ret = err;
  4129. return ret;
  4130. }
  4131. /*
  4132. * Turn on quotas during mount time - we need to find
  4133. * the quota file and such...
  4134. */
  4135. static int ext4_quota_on_mount(struct super_block *sb, int type)
  4136. {
  4137. return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
  4138. EXT4_SB(sb)->s_jquota_fmt, type);
  4139. }
  4140. /*
  4141. * Standard function to be called on quota_on
  4142. */
  4143. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  4144. struct path *path)
  4145. {
  4146. int err;
  4147. if (!test_opt(sb, QUOTA))
  4148. return -EINVAL;
  4149. /* Quotafile not on the same filesystem? */
  4150. if (path->mnt->mnt_sb != sb)
  4151. return -EXDEV;
  4152. /* Journaling quota? */
  4153. if (EXT4_SB(sb)->s_qf_names[type]) {
  4154. /* Quotafile not in fs root? */
  4155. if (path->dentry->d_parent != sb->s_root)
  4156. ext4_msg(sb, KERN_WARNING,
  4157. "Quota file not on filesystem root. "
  4158. "Journaled quota will not work");
  4159. }
  4160. /*
  4161. * When we journal data on quota file, we have to flush journal to see
  4162. * all updates to the file when we bypass pagecache...
  4163. */
  4164. if (EXT4_SB(sb)->s_journal &&
  4165. ext4_should_journal_data(path->dentry->d_inode)) {
  4166. /*
  4167. * We don't need to lock updates but journal_flush() could
  4168. * otherwise be livelocked...
  4169. */
  4170. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  4171. err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  4172. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  4173. if (err)
  4174. return err;
  4175. }
  4176. return dquot_quota_on(sb, type, format_id, path);
  4177. }
  4178. static int ext4_quota_off(struct super_block *sb, int type)
  4179. {
  4180. struct inode *inode = sb_dqopt(sb)->files[type];
  4181. handle_t *handle;
  4182. /* Force all delayed allocation blocks to be allocated.
  4183. * Caller already holds s_umount sem */
  4184. if (test_opt(sb, DELALLOC))
  4185. sync_filesystem(sb);
  4186. if (!inode)
  4187. goto out;
  4188. /* Update modification times of quota files when userspace can
  4189. * start looking at them */
  4190. handle = ext4_journal_start(inode, 1);
  4191. if (IS_ERR(handle))
  4192. goto out;
  4193. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  4194. ext4_mark_inode_dirty(handle, inode);
  4195. ext4_journal_stop(handle);
  4196. out:
  4197. return dquot_quota_off(sb, type);
  4198. }
  4199. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  4200. * acquiring the locks... As quota files are never truncated and quota code
  4201. * itself serializes the operations (and no one else should touch the files)
  4202. * we don't have to be afraid of races */
  4203. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  4204. size_t len, loff_t off)
  4205. {
  4206. struct inode *inode = sb_dqopt(sb)->files[type];
  4207. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  4208. int err = 0;
  4209. int offset = off & (sb->s_blocksize - 1);
  4210. int tocopy;
  4211. size_t toread;
  4212. struct buffer_head *bh;
  4213. loff_t i_size = i_size_read(inode);
  4214. if (off > i_size)
  4215. return 0;
  4216. if (off+len > i_size)
  4217. len = i_size-off;
  4218. toread = len;
  4219. while (toread > 0) {
  4220. tocopy = sb->s_blocksize - offset < toread ?
  4221. sb->s_blocksize - offset : toread;
  4222. bh = ext4_bread(NULL, inode, blk, 0, &err);
  4223. if (err)
  4224. return err;
  4225. if (!bh) /* A hole? */
  4226. memset(data, 0, tocopy);
  4227. else
  4228. memcpy(data, bh->b_data+offset, tocopy);
  4229. brelse(bh);
  4230. offset = 0;
  4231. toread -= tocopy;
  4232. data += tocopy;
  4233. blk++;
  4234. }
  4235. return len;
  4236. }
  4237. /* Write to quotafile (we know the transaction is already started and has
  4238. * enough credits) */
  4239. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  4240. const char *data, size_t len, loff_t off)
  4241. {
  4242. struct inode *inode = sb_dqopt(sb)->files[type];
  4243. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  4244. int err = 0;
  4245. int offset = off & (sb->s_blocksize - 1);
  4246. struct buffer_head *bh;
  4247. handle_t *handle = journal_current_handle();
  4248. if (EXT4_SB(sb)->s_journal && !handle) {
  4249. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  4250. " cancelled because transaction is not started",
  4251. (unsigned long long)off, (unsigned long long)len);
  4252. return -EIO;
  4253. }
  4254. /*
  4255. * Since we account only one data block in transaction credits,
  4256. * then it is impossible to cross a block boundary.
  4257. */
  4258. if (sb->s_blocksize - offset < len) {
  4259. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  4260. " cancelled because not block aligned",
  4261. (unsigned long long)off, (unsigned long long)len);
  4262. return -EIO;
  4263. }
  4264. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  4265. bh = ext4_bread(handle, inode, blk, 1, &err);
  4266. if (!bh)
  4267. goto out;
  4268. err = ext4_journal_get_write_access(handle, bh);
  4269. if (err) {
  4270. brelse(bh);
  4271. goto out;
  4272. }
  4273. lock_buffer(bh);
  4274. memcpy(bh->b_data+offset, data, len);
  4275. flush_dcache_page(bh->b_page);
  4276. unlock_buffer(bh);
  4277. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  4278. brelse(bh);
  4279. out:
  4280. if (err) {
  4281. mutex_unlock(&inode->i_mutex);
  4282. return err;
  4283. }
  4284. if (inode->i_size < off + len) {
  4285. i_size_write(inode, off + len);
  4286. EXT4_I(inode)->i_disksize = inode->i_size;
  4287. ext4_mark_inode_dirty(handle, inode);
  4288. }
  4289. mutex_unlock(&inode->i_mutex);
  4290. return len;
  4291. }
  4292. #endif
  4293. static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
  4294. const char *dev_name, void *data)
  4295. {
  4296. return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
  4297. }
  4298. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  4299. static inline void register_as_ext2(void)
  4300. {
  4301. int err = register_filesystem(&ext2_fs_type);
  4302. if (err)
  4303. printk(KERN_WARNING
  4304. "EXT4-fs: Unable to register as ext2 (%d)\n", err);
  4305. }
  4306. static inline void unregister_as_ext2(void)
  4307. {
  4308. unregister_filesystem(&ext2_fs_type);
  4309. }
  4310. static inline int ext2_feature_set_ok(struct super_block *sb)
  4311. {
  4312. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
  4313. return 0;
  4314. if (sb->s_flags & MS_RDONLY)
  4315. return 1;
  4316. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
  4317. return 0;
  4318. return 1;
  4319. }
  4320. MODULE_ALIAS("ext2");
  4321. #else
  4322. static inline void register_as_ext2(void) { }
  4323. static inline void unregister_as_ext2(void) { }
  4324. static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
  4325. #endif
  4326. #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  4327. static inline void register_as_ext3(void)
  4328. {
  4329. int err = register_filesystem(&ext3_fs_type);
  4330. if (err)
  4331. printk(KERN_WARNING
  4332. "EXT4-fs: Unable to register as ext3 (%d)\n", err);
  4333. }
  4334. static inline void unregister_as_ext3(void)
  4335. {
  4336. unregister_filesystem(&ext3_fs_type);
  4337. }
  4338. static inline int ext3_feature_set_ok(struct super_block *sb)
  4339. {
  4340. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
  4341. return 0;
  4342. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  4343. return 0;
  4344. if (sb->s_flags & MS_RDONLY)
  4345. return 1;
  4346. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
  4347. return 0;
  4348. return 1;
  4349. }
  4350. MODULE_ALIAS("ext3");
  4351. #else
  4352. static inline void register_as_ext3(void) { }
  4353. static inline void unregister_as_ext3(void) { }
  4354. static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
  4355. #endif
  4356. static struct file_system_type ext4_fs_type = {
  4357. .owner = THIS_MODULE,
  4358. .name = "ext4",
  4359. .mount = ext4_mount,
  4360. .kill_sb = kill_block_super,
  4361. .fs_flags = FS_REQUIRES_DEV,
  4362. };
  4363. static int __init ext4_init_feat_adverts(void)
  4364. {
  4365. struct ext4_features *ef;
  4366. int ret = -ENOMEM;
  4367. ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
  4368. if (!ef)
  4369. goto out;
  4370. ef->f_kobj.kset = ext4_kset;
  4371. init_completion(&ef->f_kobj_unregister);
  4372. ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
  4373. "features");
  4374. if (ret) {
  4375. kfree(ef);
  4376. goto out;
  4377. }
  4378. ext4_feat = ef;
  4379. ret = 0;
  4380. out:
  4381. return ret;
  4382. }
  4383. static void ext4_exit_feat_adverts(void)
  4384. {
  4385. kobject_put(&ext4_feat->f_kobj);
  4386. wait_for_completion(&ext4_feat->f_kobj_unregister);
  4387. kfree(ext4_feat);
  4388. }
  4389. /* Shared across all ext4 file systems */
  4390. wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
  4391. struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
  4392. static int __init ext4_init_fs(void)
  4393. {
  4394. int i, err;
  4395. ext4_check_flag_values();
  4396. for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
  4397. mutex_init(&ext4__aio_mutex[i]);
  4398. init_waitqueue_head(&ext4__ioend_wq[i]);
  4399. }
  4400. err = ext4_init_pageio();
  4401. if (err)
  4402. return err;
  4403. err = ext4_init_system_zone();
  4404. if (err)
  4405. goto out7;
  4406. ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
  4407. if (!ext4_kset)
  4408. goto out6;
  4409. ext4_proc_root = proc_mkdir("fs/ext4", NULL);
  4410. if (!ext4_proc_root)
  4411. goto out5;
  4412. err = ext4_init_feat_adverts();
  4413. if (err)
  4414. goto out4;
  4415. err = ext4_init_mballoc();
  4416. if (err)
  4417. goto out3;
  4418. err = ext4_init_xattr();
  4419. if (err)
  4420. goto out2;
  4421. err = init_inodecache();
  4422. if (err)
  4423. goto out1;
  4424. register_as_ext3();
  4425. register_as_ext2();
  4426. err = register_filesystem(&ext4_fs_type);
  4427. if (err)
  4428. goto out;
  4429. ext4_li_info = NULL;
  4430. mutex_init(&ext4_li_mtx);
  4431. return 0;
  4432. out:
  4433. unregister_as_ext2();
  4434. unregister_as_ext3();
  4435. destroy_inodecache();
  4436. out1:
  4437. ext4_exit_xattr();
  4438. out2:
  4439. ext4_exit_mballoc();
  4440. out3:
  4441. ext4_exit_feat_adverts();
  4442. out4:
  4443. remove_proc_entry("fs/ext4", NULL);
  4444. out5:
  4445. kset_unregister(ext4_kset);
  4446. out6:
  4447. ext4_exit_system_zone();
  4448. out7:
  4449. ext4_exit_pageio();
  4450. return err;
  4451. }
  4452. static void __exit ext4_exit_fs(void)
  4453. {
  4454. ext4_destroy_lazyinit_thread();
  4455. unregister_as_ext2();
  4456. unregister_as_ext3();
  4457. unregister_filesystem(&ext4_fs_type);
  4458. destroy_inodecache();
  4459. ext4_exit_xattr();
  4460. ext4_exit_mballoc();
  4461. ext4_exit_feat_adverts();
  4462. remove_proc_entry("fs/ext4", NULL);
  4463. kset_unregister(ext4_kset);
  4464. ext4_exit_system_zone();
  4465. ext4_exit_pageio();
  4466. }
  4467. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  4468. MODULE_DESCRIPTION("Fourth Extended Filesystem");
  4469. MODULE_LICENSE("GPL");
  4470. module_init(ext4_init_fs)
  4471. module_exit(ext4_exit_fs)