super.c 121 KB

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