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_TRANS);
  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. if (!sb->s_bdev->bd_part)
  2067. return snprintf(buf, PAGE_SIZE, "0\n");
  2068. return snprintf(buf, PAGE_SIZE, "%lu\n",
  2069. (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2070. sbi->s_sectors_written_start) >> 1);
  2071. }
  2072. static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
  2073. struct ext4_sb_info *sbi, char *buf)
  2074. {
  2075. struct super_block *sb = sbi->s_buddy_cache->i_sb;
  2076. if (!sb->s_bdev->bd_part)
  2077. return snprintf(buf, PAGE_SIZE, "0\n");
  2078. return snprintf(buf, PAGE_SIZE, "%llu\n",
  2079. (unsigned long long)(sbi->s_kbytes_written +
  2080. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2081. EXT4_SB(sb)->s_sectors_written_start) >> 1)));
  2082. }
  2083. static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
  2084. struct ext4_sb_info *sbi,
  2085. const char *buf, size_t count)
  2086. {
  2087. unsigned long t;
  2088. if (parse_strtoul(buf, 0x40000000, &t))
  2089. return -EINVAL;
  2090. if (!is_power_of_2(t))
  2091. return -EINVAL;
  2092. sbi->s_inode_readahead_blks = t;
  2093. return count;
  2094. }
  2095. static ssize_t sbi_ui_show(struct ext4_attr *a,
  2096. struct ext4_sb_info *sbi, char *buf)
  2097. {
  2098. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2099. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  2100. }
  2101. static ssize_t sbi_ui_store(struct ext4_attr *a,
  2102. struct ext4_sb_info *sbi,
  2103. const char *buf, size_t count)
  2104. {
  2105. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2106. unsigned long t;
  2107. if (parse_strtoul(buf, 0xffffffff, &t))
  2108. return -EINVAL;
  2109. *ui = t;
  2110. return count;
  2111. }
  2112. #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
  2113. static struct ext4_attr ext4_attr_##_name = { \
  2114. .attr = {.name = __stringify(_name), .mode = _mode }, \
  2115. .show = _show, \
  2116. .store = _store, \
  2117. .offset = offsetof(struct ext4_sb_info, _elname), \
  2118. }
  2119. #define EXT4_ATTR(name, mode, show, store) \
  2120. static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
  2121. #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
  2122. #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
  2123. #define EXT4_RW_ATTR_SBI_UI(name, elname) \
  2124. EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
  2125. #define ATTR_LIST(name) &ext4_attr_##name.attr
  2126. EXT4_RO_ATTR(delayed_allocation_blocks);
  2127. EXT4_RO_ATTR(session_write_kbytes);
  2128. EXT4_RO_ATTR(lifetime_write_kbytes);
  2129. EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
  2130. inode_readahead_blks_store, s_inode_readahead_blks);
  2131. EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
  2132. EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
  2133. EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
  2134. EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
  2135. EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
  2136. EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
  2137. EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
  2138. EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
  2139. static struct attribute *ext4_attrs[] = {
  2140. ATTR_LIST(delayed_allocation_blocks),
  2141. ATTR_LIST(session_write_kbytes),
  2142. ATTR_LIST(lifetime_write_kbytes),
  2143. ATTR_LIST(inode_readahead_blks),
  2144. ATTR_LIST(inode_goal),
  2145. ATTR_LIST(mb_stats),
  2146. ATTR_LIST(mb_max_to_scan),
  2147. ATTR_LIST(mb_min_to_scan),
  2148. ATTR_LIST(mb_order2_req),
  2149. ATTR_LIST(mb_stream_req),
  2150. ATTR_LIST(mb_group_prealloc),
  2151. ATTR_LIST(max_writeback_mb_bump),
  2152. NULL,
  2153. };
  2154. static ssize_t ext4_attr_show(struct kobject *kobj,
  2155. struct attribute *attr, char *buf)
  2156. {
  2157. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2158. s_kobj);
  2159. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2160. return a->show ? a->show(a, sbi, buf) : 0;
  2161. }
  2162. static ssize_t ext4_attr_store(struct kobject *kobj,
  2163. struct attribute *attr,
  2164. const char *buf, size_t len)
  2165. {
  2166. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2167. s_kobj);
  2168. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2169. return a->store ? a->store(a, sbi, buf, len) : 0;
  2170. }
  2171. static void ext4_sb_release(struct kobject *kobj)
  2172. {
  2173. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2174. s_kobj);
  2175. complete(&sbi->s_kobj_unregister);
  2176. }
  2177. static const struct sysfs_ops ext4_attr_ops = {
  2178. .show = ext4_attr_show,
  2179. .store = ext4_attr_store,
  2180. };
  2181. static struct kobj_type ext4_ktype = {
  2182. .default_attrs = ext4_attrs,
  2183. .sysfs_ops = &ext4_attr_ops,
  2184. .release = ext4_sb_release,
  2185. };
  2186. /*
  2187. * Check whether this filesystem can be mounted based on
  2188. * the features present and the RDONLY/RDWR mount requested.
  2189. * Returns 1 if this filesystem can be mounted as requested,
  2190. * 0 if it cannot be.
  2191. */
  2192. static int ext4_feature_set_ok(struct super_block *sb, int readonly)
  2193. {
  2194. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
  2195. ext4_msg(sb, KERN_ERR,
  2196. "Couldn't mount because of "
  2197. "unsupported optional features (%x)",
  2198. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
  2199. ~EXT4_FEATURE_INCOMPAT_SUPP));
  2200. return 0;
  2201. }
  2202. if (readonly)
  2203. return 1;
  2204. /* Check that feature set is OK for a read-write mount */
  2205. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
  2206. ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
  2207. "unsupported optional features (%x)",
  2208. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
  2209. ~EXT4_FEATURE_RO_COMPAT_SUPP));
  2210. return 0;
  2211. }
  2212. /*
  2213. * Large file size enabled file system can only be mounted
  2214. * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
  2215. */
  2216. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  2217. if (sizeof(blkcnt_t) < sizeof(u64)) {
  2218. ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
  2219. "cannot be mounted RDWR without "
  2220. "CONFIG_LBDAF");
  2221. return 0;
  2222. }
  2223. }
  2224. return 1;
  2225. }
  2226. /*
  2227. * This function is called once a day if we have errors logged
  2228. * on the file system
  2229. */
  2230. static void print_daily_error_info(unsigned long arg)
  2231. {
  2232. struct super_block *sb = (struct super_block *) arg;
  2233. struct ext4_sb_info *sbi;
  2234. struct ext4_super_block *es;
  2235. sbi = EXT4_SB(sb);
  2236. es = sbi->s_es;
  2237. if (es->s_error_count)
  2238. ext4_msg(sb, KERN_NOTICE, "error count: %u",
  2239. le32_to_cpu(es->s_error_count));
  2240. if (es->s_first_error_time) {
  2241. printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
  2242. sb->s_id, le32_to_cpu(es->s_first_error_time),
  2243. (int) sizeof(es->s_first_error_func),
  2244. es->s_first_error_func,
  2245. le32_to_cpu(es->s_first_error_line));
  2246. if (es->s_first_error_ino)
  2247. printk(": inode %u",
  2248. le32_to_cpu(es->s_first_error_ino));
  2249. if (es->s_first_error_block)
  2250. printk(": block %llu", (unsigned long long)
  2251. le64_to_cpu(es->s_first_error_block));
  2252. printk("\n");
  2253. }
  2254. if (es->s_last_error_time) {
  2255. printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
  2256. sb->s_id, le32_to_cpu(es->s_last_error_time),
  2257. (int) sizeof(es->s_last_error_func),
  2258. es->s_last_error_func,
  2259. le32_to_cpu(es->s_last_error_line));
  2260. if (es->s_last_error_ino)
  2261. printk(": inode %u",
  2262. le32_to_cpu(es->s_last_error_ino));
  2263. if (es->s_last_error_block)
  2264. printk(": block %llu", (unsigned long long)
  2265. le64_to_cpu(es->s_last_error_block));
  2266. printk("\n");
  2267. }
  2268. mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
  2269. }
  2270. static int ext4_fill_super(struct super_block *sb, void *data, int silent)
  2271. __releases(kernel_lock)
  2272. __acquires(kernel_lock)
  2273. {
  2274. char *orig_data = kstrdup(data, GFP_KERNEL);
  2275. struct buffer_head *bh;
  2276. struct ext4_super_block *es = NULL;
  2277. struct ext4_sb_info *sbi;
  2278. ext4_fsblk_t block;
  2279. ext4_fsblk_t sb_block = get_sb_block(&data);
  2280. ext4_fsblk_t logical_sb_block;
  2281. unsigned long offset = 0;
  2282. unsigned long journal_devnum = 0;
  2283. unsigned long def_mount_opts;
  2284. struct inode *root;
  2285. char *cp;
  2286. const char *descr;
  2287. int ret = -ENOMEM;
  2288. int blocksize;
  2289. unsigned int db_count;
  2290. unsigned int i;
  2291. int needs_recovery, has_huge_files;
  2292. __u64 blocks_count;
  2293. int err;
  2294. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  2295. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  2296. if (!sbi)
  2297. goto out_free_orig;
  2298. sbi->s_blockgroup_lock =
  2299. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  2300. if (!sbi->s_blockgroup_lock) {
  2301. kfree(sbi);
  2302. goto out_free_orig;
  2303. }
  2304. sb->s_fs_info = sbi;
  2305. sbi->s_mount_opt = 0;
  2306. sbi->s_resuid = EXT4_DEF_RESUID;
  2307. sbi->s_resgid = EXT4_DEF_RESGID;
  2308. sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
  2309. sbi->s_sb_block = sb_block;
  2310. if (sb->s_bdev->bd_part)
  2311. sbi->s_sectors_written_start =
  2312. part_stat_read(sb->s_bdev->bd_part, sectors[1]);
  2313. unlock_kernel();
  2314. /* Cleanup superblock name */
  2315. for (cp = sb->s_id; (cp = strchr(cp, '/'));)
  2316. *cp = '!';
  2317. ret = -EINVAL;
  2318. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  2319. if (!blocksize) {
  2320. ext4_msg(sb, KERN_ERR, "unable to set blocksize");
  2321. goto out_fail;
  2322. }
  2323. /*
  2324. * The ext4 superblock will not be buffer aligned for other than 1kB
  2325. * block sizes. We need to calculate the offset from buffer start.
  2326. */
  2327. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  2328. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2329. offset = do_div(logical_sb_block, blocksize);
  2330. } else {
  2331. logical_sb_block = sb_block;
  2332. }
  2333. if (!(bh = sb_bread(sb, logical_sb_block))) {
  2334. ext4_msg(sb, KERN_ERR, "unable to read superblock");
  2335. goto out_fail;
  2336. }
  2337. /*
  2338. * Note: s_es must be initialized as soon as possible because
  2339. * some ext4 macro-instructions depend on its value
  2340. */
  2341. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  2342. sbi->s_es = es;
  2343. sb->s_magic = le16_to_cpu(es->s_magic);
  2344. if (sb->s_magic != EXT4_SUPER_MAGIC)
  2345. goto cantfind_ext4;
  2346. sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
  2347. /* Set defaults before we parse the mount options */
  2348. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  2349. if (def_mount_opts & EXT4_DEFM_DEBUG)
  2350. set_opt(sbi->s_mount_opt, DEBUG);
  2351. if (def_mount_opts & EXT4_DEFM_BSDGROUPS) {
  2352. ext4_msg(sb, KERN_WARNING, deprecated_msg, "bsdgroups",
  2353. "2.6.38");
  2354. set_opt(sbi->s_mount_opt, GRPID);
  2355. }
  2356. if (def_mount_opts & EXT4_DEFM_UID16)
  2357. set_opt(sbi->s_mount_opt, NO_UID32);
  2358. #ifdef CONFIG_EXT4_FS_XATTR
  2359. if (def_mount_opts & EXT4_DEFM_XATTR_USER)
  2360. set_opt(sbi->s_mount_opt, XATTR_USER);
  2361. #endif
  2362. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  2363. if (def_mount_opts & EXT4_DEFM_ACL)
  2364. set_opt(sbi->s_mount_opt, POSIX_ACL);
  2365. #endif
  2366. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  2367. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  2368. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  2369. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  2370. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  2371. set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
  2372. if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
  2373. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  2374. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
  2375. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  2376. else
  2377. set_opt(sbi->s_mount_opt, ERRORS_RO);
  2378. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  2379. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  2380. sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
  2381. sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
  2382. sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
  2383. set_opt(sbi->s_mount_opt, BARRIER);
  2384. /*
  2385. * enable delayed allocation by default
  2386. * Use -o nodelalloc to turn it off
  2387. */
  2388. if (!IS_EXT3_SB(sb))
  2389. set_opt(sbi->s_mount_opt, DELALLOC);
  2390. if (!parse_options((char *) data, sb, &journal_devnum,
  2391. &journal_ioprio, NULL, 0))
  2392. goto failed_mount;
  2393. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2394. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  2395. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  2396. (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
  2397. EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  2398. EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  2399. ext4_msg(sb, KERN_WARNING,
  2400. "feature flags set on rev 0 fs, "
  2401. "running e2fsck is recommended");
  2402. /*
  2403. * Check feature flags regardless of the revision level, since we
  2404. * previously didn't change the revision level when setting the flags,
  2405. * so there is a chance incompat flags are set on a rev 0 filesystem.
  2406. */
  2407. if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
  2408. goto failed_mount;
  2409. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  2410. if (blocksize < EXT4_MIN_BLOCK_SIZE ||
  2411. blocksize > EXT4_MAX_BLOCK_SIZE) {
  2412. ext4_msg(sb, KERN_ERR,
  2413. "Unsupported filesystem blocksize %d", blocksize);
  2414. goto failed_mount;
  2415. }
  2416. if (sb->s_blocksize != blocksize) {
  2417. /* Validate the filesystem blocksize */
  2418. if (!sb_set_blocksize(sb, blocksize)) {
  2419. ext4_msg(sb, KERN_ERR, "bad block size %d",
  2420. blocksize);
  2421. goto failed_mount;
  2422. }
  2423. brelse(bh);
  2424. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2425. offset = do_div(logical_sb_block, blocksize);
  2426. bh = sb_bread(sb, logical_sb_block);
  2427. if (!bh) {
  2428. ext4_msg(sb, KERN_ERR,
  2429. "Can't read superblock on 2nd try");
  2430. goto failed_mount;
  2431. }
  2432. es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
  2433. sbi->s_es = es;
  2434. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  2435. ext4_msg(sb, KERN_ERR,
  2436. "Magic mismatch, very weird!");
  2437. goto failed_mount;
  2438. }
  2439. }
  2440. has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2441. EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
  2442. sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
  2443. has_huge_files);
  2444. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
  2445. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  2446. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  2447. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  2448. } else {
  2449. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  2450. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  2451. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  2452. (!is_power_of_2(sbi->s_inode_size)) ||
  2453. (sbi->s_inode_size > blocksize)) {
  2454. ext4_msg(sb, KERN_ERR,
  2455. "unsupported inode size: %d",
  2456. sbi->s_inode_size);
  2457. goto failed_mount;
  2458. }
  2459. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
  2460. sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
  2461. }
  2462. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  2463. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
  2464. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  2465. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  2466. !is_power_of_2(sbi->s_desc_size)) {
  2467. ext4_msg(sb, KERN_ERR,
  2468. "unsupported descriptor size %lu",
  2469. sbi->s_desc_size);
  2470. goto failed_mount;
  2471. }
  2472. } else
  2473. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  2474. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  2475. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  2476. if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
  2477. goto cantfind_ext4;
  2478. sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
  2479. if (sbi->s_inodes_per_block == 0)
  2480. goto cantfind_ext4;
  2481. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  2482. sbi->s_inodes_per_block;
  2483. sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
  2484. sbi->s_sbh = bh;
  2485. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2486. sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
  2487. sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
  2488. for (i = 0; i < 4; i++)
  2489. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  2490. sbi->s_def_hash_version = es->s_def_hash_version;
  2491. i = le32_to_cpu(es->s_flags);
  2492. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  2493. sbi->s_hash_unsigned = 3;
  2494. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  2495. #ifdef __CHAR_UNSIGNED__
  2496. es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  2497. sbi->s_hash_unsigned = 3;
  2498. #else
  2499. es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  2500. #endif
  2501. sb->s_dirt = 1;
  2502. }
  2503. if (sbi->s_blocks_per_group > blocksize * 8) {
  2504. ext4_msg(sb, KERN_ERR,
  2505. "#blocks per group too big: %lu",
  2506. sbi->s_blocks_per_group);
  2507. goto failed_mount;
  2508. }
  2509. if (sbi->s_inodes_per_group > blocksize * 8) {
  2510. ext4_msg(sb, KERN_ERR,
  2511. "#inodes per group too big: %lu",
  2512. sbi->s_inodes_per_group);
  2513. goto failed_mount;
  2514. }
  2515. /*
  2516. * Test whether we have more sectors than will fit in sector_t,
  2517. * and whether the max offset is addressable by the page cache.
  2518. */
  2519. if ((ext4_blocks_count(es) >
  2520. (sector_t)(~0ULL) >> (sb->s_blocksize_bits - 9)) ||
  2521. (ext4_blocks_count(es) >
  2522. (pgoff_t)(~0ULL) >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits))) {
  2523. ext4_msg(sb, KERN_ERR, "filesystem"
  2524. " too large to mount safely on this system");
  2525. if (sizeof(sector_t) < 8)
  2526. ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
  2527. ret = -EFBIG;
  2528. goto failed_mount;
  2529. }
  2530. if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
  2531. goto cantfind_ext4;
  2532. /* check blocks count against device size */
  2533. blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  2534. if (blocks_count && ext4_blocks_count(es) > blocks_count) {
  2535. ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
  2536. "exceeds size of device (%llu blocks)",
  2537. ext4_blocks_count(es), blocks_count);
  2538. goto failed_mount;
  2539. }
  2540. /*
  2541. * It makes no sense for the first data block to be beyond the end
  2542. * of the filesystem.
  2543. */
  2544. if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
  2545. ext4_msg(sb, KERN_WARNING, "bad geometry: first data"
  2546. "block %u is beyond end of filesystem (%llu)",
  2547. le32_to_cpu(es->s_first_data_block),
  2548. ext4_blocks_count(es));
  2549. goto failed_mount;
  2550. }
  2551. blocks_count = (ext4_blocks_count(es) -
  2552. le32_to_cpu(es->s_first_data_block) +
  2553. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  2554. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  2555. if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
  2556. ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
  2557. "(block count %llu, first data block %u, "
  2558. "blocks per group %lu)", sbi->s_groups_count,
  2559. ext4_blocks_count(es),
  2560. le32_to_cpu(es->s_first_data_block),
  2561. EXT4_BLOCKS_PER_GROUP(sb));
  2562. goto failed_mount;
  2563. }
  2564. sbi->s_groups_count = blocks_count;
  2565. sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
  2566. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  2567. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  2568. EXT4_DESC_PER_BLOCK(sb);
  2569. sbi->s_group_desc = kmalloc(db_count * sizeof(struct buffer_head *),
  2570. GFP_KERNEL);
  2571. if (sbi->s_group_desc == NULL) {
  2572. ext4_msg(sb, KERN_ERR, "not enough memory");
  2573. goto failed_mount;
  2574. }
  2575. #ifdef CONFIG_PROC_FS
  2576. if (ext4_proc_root)
  2577. sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
  2578. #endif
  2579. bgl_lock_init(sbi->s_blockgroup_lock);
  2580. for (i = 0; i < db_count; i++) {
  2581. block = descriptor_loc(sb, logical_sb_block, i);
  2582. sbi->s_group_desc[i] = sb_bread(sb, block);
  2583. if (!sbi->s_group_desc[i]) {
  2584. ext4_msg(sb, KERN_ERR,
  2585. "can't read group descriptor %d", i);
  2586. db_count = i;
  2587. goto failed_mount2;
  2588. }
  2589. }
  2590. if (!ext4_check_descriptors(sb)) {
  2591. ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
  2592. goto failed_mount2;
  2593. }
  2594. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
  2595. if (!ext4_fill_flex_info(sb)) {
  2596. ext4_msg(sb, KERN_ERR,
  2597. "unable to initialize "
  2598. "flex_bg meta info!");
  2599. goto failed_mount2;
  2600. }
  2601. sbi->s_gdb_count = db_count;
  2602. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  2603. spin_lock_init(&sbi->s_next_gen_lock);
  2604. sbi->s_stripe = ext4_get_stripe_size(sbi);
  2605. sbi->s_max_writeback_mb_bump = 128;
  2606. /*
  2607. * set up enough so that it can read an inode
  2608. */
  2609. if (!test_opt(sb, NOLOAD) &&
  2610. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  2611. sb->s_op = &ext4_sops;
  2612. else
  2613. sb->s_op = &ext4_nojournal_sops;
  2614. sb->s_export_op = &ext4_export_ops;
  2615. sb->s_xattr = ext4_xattr_handlers;
  2616. #ifdef CONFIG_QUOTA
  2617. sb->s_qcop = &ext4_qctl_operations;
  2618. sb->dq_op = &ext4_quota_operations;
  2619. #endif
  2620. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  2621. mutex_init(&sbi->s_orphan_lock);
  2622. mutex_init(&sbi->s_resize_lock);
  2623. sb->s_root = NULL;
  2624. needs_recovery = (es->s_last_orphan != 0 ||
  2625. EXT4_HAS_INCOMPAT_FEATURE(sb,
  2626. EXT4_FEATURE_INCOMPAT_RECOVER));
  2627. /*
  2628. * The first inode we look at is the journal inode. Don't try
  2629. * root first: it may be modified in the journal!
  2630. */
  2631. if (!test_opt(sb, NOLOAD) &&
  2632. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  2633. if (ext4_load_journal(sb, es, journal_devnum))
  2634. goto failed_mount3;
  2635. } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
  2636. EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  2637. ext4_msg(sb, KERN_ERR, "required journal recovery "
  2638. "suppressed and not mounted read-only");
  2639. goto failed_mount_wq;
  2640. } else {
  2641. clear_opt(sbi->s_mount_opt, DATA_FLAGS);
  2642. set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
  2643. sbi->s_journal = NULL;
  2644. needs_recovery = 0;
  2645. goto no_journal;
  2646. }
  2647. if (ext4_blocks_count(es) > 0xffffffffULL &&
  2648. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  2649. JBD2_FEATURE_INCOMPAT_64BIT)) {
  2650. ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
  2651. goto failed_mount_wq;
  2652. }
  2653. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  2654. jbd2_journal_set_features(sbi->s_journal,
  2655. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  2656. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2657. } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
  2658. jbd2_journal_set_features(sbi->s_journal,
  2659. JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
  2660. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  2661. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2662. } else {
  2663. jbd2_journal_clear_features(sbi->s_journal,
  2664. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  2665. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  2666. }
  2667. /* We have now updated the journal if required, so we can
  2668. * validate the data journaling mode. */
  2669. switch (test_opt(sb, DATA_FLAGS)) {
  2670. case 0:
  2671. /* No mode set, assume a default based on the journal
  2672. * capabilities: ORDERED_DATA if the journal can
  2673. * cope, else JOURNAL_DATA
  2674. */
  2675. if (jbd2_journal_check_available_features
  2676. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
  2677. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  2678. else
  2679. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  2680. break;
  2681. case EXT4_MOUNT_ORDERED_DATA:
  2682. case EXT4_MOUNT_WRITEBACK_DATA:
  2683. if (!jbd2_journal_check_available_features
  2684. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  2685. ext4_msg(sb, KERN_ERR, "Journal does not support "
  2686. "requested data journaling mode");
  2687. goto failed_mount_wq;
  2688. }
  2689. default:
  2690. break;
  2691. }
  2692. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  2693. no_journal:
  2694. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  2695. ext4_count_free_blocks(sb));
  2696. if (!err)
  2697. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  2698. ext4_count_free_inodes(sb));
  2699. if (!err)
  2700. err = percpu_counter_init(&sbi->s_dirs_counter,
  2701. ext4_count_dirs(sb));
  2702. if (!err)
  2703. err = percpu_counter_init(&sbi->s_dirtyblocks_counter, 0);
  2704. if (err) {
  2705. ext4_msg(sb, KERN_ERR, "insufficient memory");
  2706. goto failed_mount_wq;
  2707. }
  2708. EXT4_SB(sb)->dio_unwritten_wq = create_workqueue("ext4-dio-unwritten");
  2709. if (!EXT4_SB(sb)->dio_unwritten_wq) {
  2710. printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
  2711. goto failed_mount_wq;
  2712. }
  2713. /*
  2714. * The jbd2_journal_load will have done any necessary log recovery,
  2715. * so we can safely mount the rest of the filesystem now.
  2716. */
  2717. root = ext4_iget(sb, EXT4_ROOT_INO);
  2718. if (IS_ERR(root)) {
  2719. ext4_msg(sb, KERN_ERR, "get root inode failed");
  2720. ret = PTR_ERR(root);
  2721. goto failed_mount4;
  2722. }
  2723. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  2724. iput(root);
  2725. ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
  2726. goto failed_mount4;
  2727. }
  2728. sb->s_root = d_alloc_root(root);
  2729. if (!sb->s_root) {
  2730. ext4_msg(sb, KERN_ERR, "get root dentry failed");
  2731. iput(root);
  2732. ret = -ENOMEM;
  2733. goto failed_mount4;
  2734. }
  2735. ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
  2736. /* determine the minimum size of new large inodes, if present */
  2737. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
  2738. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  2739. EXT4_GOOD_OLD_INODE_SIZE;
  2740. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2741. EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
  2742. if (sbi->s_want_extra_isize <
  2743. le16_to_cpu(es->s_want_extra_isize))
  2744. sbi->s_want_extra_isize =
  2745. le16_to_cpu(es->s_want_extra_isize);
  2746. if (sbi->s_want_extra_isize <
  2747. le16_to_cpu(es->s_min_extra_isize))
  2748. sbi->s_want_extra_isize =
  2749. le16_to_cpu(es->s_min_extra_isize);
  2750. }
  2751. }
  2752. /* Check if enough inode space is available */
  2753. if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
  2754. sbi->s_inode_size) {
  2755. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  2756. EXT4_GOOD_OLD_INODE_SIZE;
  2757. ext4_msg(sb, KERN_INFO, "required extra inode space not"
  2758. "available");
  2759. }
  2760. if (test_opt(sb, DELALLOC) &&
  2761. (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)) {
  2762. ext4_msg(sb, KERN_WARNING, "Ignoring delalloc option - "
  2763. "requested data journaling mode");
  2764. clear_opt(sbi->s_mount_opt, DELALLOC);
  2765. }
  2766. if (test_opt(sb, DIOREAD_NOLOCK)) {
  2767. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  2768. ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
  2769. "option - requested data journaling mode");
  2770. clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
  2771. }
  2772. if (sb->s_blocksize < PAGE_SIZE) {
  2773. ext4_msg(sb, KERN_WARNING, "Ignoring dioread_nolock "
  2774. "option - block size is too small");
  2775. clear_opt(sbi->s_mount_opt, DIOREAD_NOLOCK);
  2776. }
  2777. }
  2778. err = ext4_setup_system_zone(sb);
  2779. if (err) {
  2780. ext4_msg(sb, KERN_ERR, "failed to initialize system "
  2781. "zone (%d)", err);
  2782. goto failed_mount4;
  2783. }
  2784. ext4_ext_init(sb);
  2785. err = ext4_mb_init(sb, needs_recovery);
  2786. if (err) {
  2787. ext4_msg(sb, KERN_ERR, "failed to initalize mballoc (%d)",
  2788. err);
  2789. goto failed_mount4;
  2790. }
  2791. sbi->s_kobj.kset = ext4_kset;
  2792. init_completion(&sbi->s_kobj_unregister);
  2793. err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
  2794. "%s", sb->s_id);
  2795. if (err) {
  2796. ext4_mb_release(sb);
  2797. ext4_ext_release(sb);
  2798. goto failed_mount4;
  2799. };
  2800. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  2801. ext4_orphan_cleanup(sb, es);
  2802. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  2803. if (needs_recovery) {
  2804. ext4_msg(sb, KERN_INFO, "recovery complete");
  2805. ext4_mark_recovery_complete(sb, es);
  2806. }
  2807. if (EXT4_SB(sb)->s_journal) {
  2808. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  2809. descr = " journalled data mode";
  2810. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  2811. descr = " ordered data mode";
  2812. else
  2813. descr = " writeback data mode";
  2814. } else
  2815. descr = "out journal";
  2816. ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
  2817. "Opts: %s", descr, orig_data);
  2818. init_timer(&sbi->s_err_report);
  2819. sbi->s_err_report.function = print_daily_error_info;
  2820. sbi->s_err_report.data = (unsigned long) sb;
  2821. if (es->s_error_count)
  2822. mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
  2823. lock_kernel();
  2824. kfree(orig_data);
  2825. return 0;
  2826. cantfind_ext4:
  2827. if (!silent)
  2828. ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
  2829. goto failed_mount;
  2830. failed_mount4:
  2831. ext4_msg(sb, KERN_ERR, "mount failed");
  2832. destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
  2833. failed_mount_wq:
  2834. ext4_release_system_zone(sb);
  2835. if (sbi->s_journal) {
  2836. jbd2_journal_destroy(sbi->s_journal);
  2837. sbi->s_journal = NULL;
  2838. }
  2839. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  2840. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  2841. percpu_counter_destroy(&sbi->s_dirs_counter);
  2842. percpu_counter_destroy(&sbi->s_dirtyblocks_counter);
  2843. failed_mount3:
  2844. if (sbi->s_flex_groups) {
  2845. if (is_vmalloc_addr(sbi->s_flex_groups))
  2846. vfree(sbi->s_flex_groups);
  2847. else
  2848. kfree(sbi->s_flex_groups);
  2849. }
  2850. failed_mount2:
  2851. for (i = 0; i < db_count; i++)
  2852. brelse(sbi->s_group_desc[i]);
  2853. kfree(sbi->s_group_desc);
  2854. failed_mount:
  2855. if (sbi->s_proc) {
  2856. remove_proc_entry(sb->s_id, ext4_proc_root);
  2857. }
  2858. #ifdef CONFIG_QUOTA
  2859. for (i = 0; i < MAXQUOTAS; i++)
  2860. kfree(sbi->s_qf_names[i]);
  2861. #endif
  2862. ext4_blkdev_remove(sbi);
  2863. brelse(bh);
  2864. out_fail:
  2865. sb->s_fs_info = NULL;
  2866. kfree(sbi->s_blockgroup_lock);
  2867. kfree(sbi);
  2868. lock_kernel();
  2869. out_free_orig:
  2870. kfree(orig_data);
  2871. return ret;
  2872. }
  2873. /*
  2874. * Setup any per-fs journal parameters now. We'll do this both on
  2875. * initial mount, once the journal has been initialised but before we've
  2876. * done any recovery; and again on any subsequent remount.
  2877. */
  2878. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  2879. {
  2880. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2881. journal->j_commit_interval = sbi->s_commit_interval;
  2882. journal->j_min_batch_time = sbi->s_min_batch_time;
  2883. journal->j_max_batch_time = sbi->s_max_batch_time;
  2884. spin_lock(&journal->j_state_lock);
  2885. if (test_opt(sb, BARRIER))
  2886. journal->j_flags |= JBD2_BARRIER;
  2887. else
  2888. journal->j_flags &= ~JBD2_BARRIER;
  2889. if (test_opt(sb, DATA_ERR_ABORT))
  2890. journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
  2891. else
  2892. journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
  2893. spin_unlock(&journal->j_state_lock);
  2894. }
  2895. static journal_t *ext4_get_journal(struct super_block *sb,
  2896. unsigned int journal_inum)
  2897. {
  2898. struct inode *journal_inode;
  2899. journal_t *journal;
  2900. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  2901. /* First, test for the existence of a valid inode on disk. Bad
  2902. * things happen if we iget() an unused inode, as the subsequent
  2903. * iput() will try to delete it. */
  2904. journal_inode = ext4_iget(sb, journal_inum);
  2905. if (IS_ERR(journal_inode)) {
  2906. ext4_msg(sb, KERN_ERR, "no journal found");
  2907. return NULL;
  2908. }
  2909. if (!journal_inode->i_nlink) {
  2910. make_bad_inode(journal_inode);
  2911. iput(journal_inode);
  2912. ext4_msg(sb, KERN_ERR, "journal inode is deleted");
  2913. return NULL;
  2914. }
  2915. jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
  2916. journal_inode, journal_inode->i_size);
  2917. if (!S_ISREG(journal_inode->i_mode)) {
  2918. ext4_msg(sb, KERN_ERR, "invalid journal inode");
  2919. iput(journal_inode);
  2920. return NULL;
  2921. }
  2922. journal = jbd2_journal_init_inode(journal_inode);
  2923. if (!journal) {
  2924. ext4_msg(sb, KERN_ERR, "Could not load journal inode");
  2925. iput(journal_inode);
  2926. return NULL;
  2927. }
  2928. journal->j_private = sb;
  2929. ext4_init_journal_params(sb, journal);
  2930. return journal;
  2931. }
  2932. static journal_t *ext4_get_dev_journal(struct super_block *sb,
  2933. dev_t j_dev)
  2934. {
  2935. struct buffer_head *bh;
  2936. journal_t *journal;
  2937. ext4_fsblk_t start;
  2938. ext4_fsblk_t len;
  2939. int hblock, blocksize;
  2940. ext4_fsblk_t sb_block;
  2941. unsigned long offset;
  2942. struct ext4_super_block *es;
  2943. struct block_device *bdev;
  2944. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  2945. bdev = ext4_blkdev_get(j_dev, sb);
  2946. if (bdev == NULL)
  2947. return NULL;
  2948. if (bd_claim(bdev, sb)) {
  2949. ext4_msg(sb, KERN_ERR,
  2950. "failed to claim external journal device");
  2951. blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
  2952. return NULL;
  2953. }
  2954. blocksize = sb->s_blocksize;
  2955. hblock = bdev_logical_block_size(bdev);
  2956. if (blocksize < hblock) {
  2957. ext4_msg(sb, KERN_ERR,
  2958. "blocksize too small for journal device");
  2959. goto out_bdev;
  2960. }
  2961. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  2962. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  2963. set_blocksize(bdev, blocksize);
  2964. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  2965. ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
  2966. "external journal");
  2967. goto out_bdev;
  2968. }
  2969. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  2970. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  2971. !(le32_to_cpu(es->s_feature_incompat) &
  2972. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  2973. ext4_msg(sb, KERN_ERR, "external journal has "
  2974. "bad superblock");
  2975. brelse(bh);
  2976. goto out_bdev;
  2977. }
  2978. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  2979. ext4_msg(sb, KERN_ERR, "journal UUID does not match");
  2980. brelse(bh);
  2981. goto out_bdev;
  2982. }
  2983. len = ext4_blocks_count(es);
  2984. start = sb_block + 1;
  2985. brelse(bh); /* we're done with the superblock */
  2986. journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
  2987. start, len, blocksize);
  2988. if (!journal) {
  2989. ext4_msg(sb, KERN_ERR, "failed to create device journal");
  2990. goto out_bdev;
  2991. }
  2992. journal->j_private = sb;
  2993. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  2994. wait_on_buffer(journal->j_sb_buffer);
  2995. if (!buffer_uptodate(journal->j_sb_buffer)) {
  2996. ext4_msg(sb, KERN_ERR, "I/O error on journal device");
  2997. goto out_journal;
  2998. }
  2999. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  3000. ext4_msg(sb, KERN_ERR, "External journal has more than one "
  3001. "user (unsupported) - %d",
  3002. be32_to_cpu(journal->j_superblock->s_nr_users));
  3003. goto out_journal;
  3004. }
  3005. EXT4_SB(sb)->journal_bdev = bdev;
  3006. ext4_init_journal_params(sb, journal);
  3007. return journal;
  3008. out_journal:
  3009. jbd2_journal_destroy(journal);
  3010. out_bdev:
  3011. ext4_blkdev_put(bdev);
  3012. return NULL;
  3013. }
  3014. static int ext4_load_journal(struct super_block *sb,
  3015. struct ext4_super_block *es,
  3016. unsigned long journal_devnum)
  3017. {
  3018. journal_t *journal;
  3019. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  3020. dev_t journal_dev;
  3021. int err = 0;
  3022. int really_read_only;
  3023. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3024. if (journal_devnum &&
  3025. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3026. ext4_msg(sb, KERN_INFO, "external journal device major/minor "
  3027. "numbers have changed");
  3028. journal_dev = new_decode_dev(journal_devnum);
  3029. } else
  3030. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  3031. really_read_only = bdev_read_only(sb->s_bdev);
  3032. /*
  3033. * Are we loading a blank journal or performing recovery after a
  3034. * crash? For recovery, we need to check in advance whether we
  3035. * can get read-write access to the device.
  3036. */
  3037. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  3038. if (sb->s_flags & MS_RDONLY) {
  3039. ext4_msg(sb, KERN_INFO, "INFO: recovery "
  3040. "required on readonly filesystem");
  3041. if (really_read_only) {
  3042. ext4_msg(sb, KERN_ERR, "write access "
  3043. "unavailable, cannot proceed");
  3044. return -EROFS;
  3045. }
  3046. ext4_msg(sb, KERN_INFO, "write access will "
  3047. "be enabled during recovery");
  3048. }
  3049. }
  3050. if (journal_inum && journal_dev) {
  3051. ext4_msg(sb, KERN_ERR, "filesystem has both journal "
  3052. "and inode journals!");
  3053. return -EINVAL;
  3054. }
  3055. if (journal_inum) {
  3056. if (!(journal = ext4_get_journal(sb, journal_inum)))
  3057. return -EINVAL;
  3058. } else {
  3059. if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
  3060. return -EINVAL;
  3061. }
  3062. if (!(journal->j_flags & JBD2_BARRIER))
  3063. ext4_msg(sb, KERN_INFO, "barriers disabled");
  3064. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  3065. err = jbd2_journal_update_format(journal);
  3066. if (err) {
  3067. ext4_msg(sb, KERN_ERR, "error updating journal");
  3068. jbd2_journal_destroy(journal);
  3069. return err;
  3070. }
  3071. }
  3072. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
  3073. err = jbd2_journal_wipe(journal, !really_read_only);
  3074. if (!err) {
  3075. char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
  3076. if (save)
  3077. memcpy(save, ((char *) es) +
  3078. EXT4_S_ERR_START, EXT4_S_ERR_LEN);
  3079. err = jbd2_journal_load(journal);
  3080. if (save)
  3081. memcpy(((char *) es) + EXT4_S_ERR_START,
  3082. save, EXT4_S_ERR_LEN);
  3083. kfree(save);
  3084. }
  3085. if (err) {
  3086. ext4_msg(sb, KERN_ERR, "error loading journal");
  3087. jbd2_journal_destroy(journal);
  3088. return err;
  3089. }
  3090. EXT4_SB(sb)->s_journal = journal;
  3091. ext4_clear_journal_err(sb, es);
  3092. if (journal_devnum &&
  3093. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3094. es->s_journal_dev = cpu_to_le32(journal_devnum);
  3095. /* Make sure we flush the recovery flag to disk. */
  3096. ext4_commit_super(sb, 1);
  3097. }
  3098. return 0;
  3099. }
  3100. static int ext4_commit_super(struct super_block *sb, int sync)
  3101. {
  3102. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  3103. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  3104. int error = 0;
  3105. if (!sbh)
  3106. return error;
  3107. if (buffer_write_io_error(sbh)) {
  3108. /*
  3109. * Oh, dear. A previous attempt to write the
  3110. * superblock failed. This could happen because the
  3111. * USB device was yanked out. Or it could happen to
  3112. * be a transient write error and maybe the block will
  3113. * be remapped. Nothing we can do but to retry the
  3114. * write and hope for the best.
  3115. */
  3116. ext4_msg(sb, KERN_ERR, "previous I/O error to "
  3117. "superblock detected");
  3118. clear_buffer_write_io_error(sbh);
  3119. set_buffer_uptodate(sbh);
  3120. }
  3121. /*
  3122. * If the file system is mounted read-only, don't update the
  3123. * superblock write time. This avoids updating the superblock
  3124. * write time when we are mounting the root file system
  3125. * read/only but we need to replay the journal; at that point,
  3126. * for people who are east of GMT and who make their clock
  3127. * tick in localtime for Windows bug-for-bug compatibility,
  3128. * the clock is set in the future, and this will cause e2fsck
  3129. * to complain and force a full file system check.
  3130. */
  3131. if (!(sb->s_flags & MS_RDONLY))
  3132. es->s_wtime = cpu_to_le32(get_seconds());
  3133. if (sb->s_bdev->bd_part)
  3134. es->s_kbytes_written =
  3135. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
  3136. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  3137. EXT4_SB(sb)->s_sectors_written_start) >> 1));
  3138. else
  3139. es->s_kbytes_written =
  3140. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
  3141. ext4_free_blocks_count_set(es, percpu_counter_sum_positive(
  3142. &EXT4_SB(sb)->s_freeblocks_counter));
  3143. es->s_free_inodes_count = cpu_to_le32(percpu_counter_sum_positive(
  3144. &EXT4_SB(sb)->s_freeinodes_counter));
  3145. sb->s_dirt = 0;
  3146. BUFFER_TRACE(sbh, "marking dirty");
  3147. mark_buffer_dirty(sbh);
  3148. if (sync) {
  3149. error = sync_dirty_buffer(sbh);
  3150. if (error)
  3151. return error;
  3152. error = buffer_write_io_error(sbh);
  3153. if (error) {
  3154. ext4_msg(sb, KERN_ERR, "I/O error while writing "
  3155. "superblock");
  3156. clear_buffer_write_io_error(sbh);
  3157. set_buffer_uptodate(sbh);
  3158. }
  3159. }
  3160. return error;
  3161. }
  3162. /*
  3163. * Have we just finished recovery? If so, and if we are mounting (or
  3164. * remounting) the filesystem readonly, then we will end up with a
  3165. * consistent fs on disk. Record that fact.
  3166. */
  3167. static void ext4_mark_recovery_complete(struct super_block *sb,
  3168. struct ext4_super_block *es)
  3169. {
  3170. journal_t *journal = EXT4_SB(sb)->s_journal;
  3171. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3172. BUG_ON(journal != NULL);
  3173. return;
  3174. }
  3175. jbd2_journal_lock_updates(journal);
  3176. if (jbd2_journal_flush(journal) < 0)
  3177. goto out;
  3178. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
  3179. sb->s_flags & MS_RDONLY) {
  3180. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3181. ext4_commit_super(sb, 1);
  3182. }
  3183. out:
  3184. jbd2_journal_unlock_updates(journal);
  3185. }
  3186. /*
  3187. * If we are mounting (or read-write remounting) a filesystem whose journal
  3188. * has recorded an error from a previous lifetime, move that error to the
  3189. * main filesystem now.
  3190. */
  3191. static void ext4_clear_journal_err(struct super_block *sb,
  3192. struct ext4_super_block *es)
  3193. {
  3194. journal_t *journal;
  3195. int j_errno;
  3196. const char *errstr;
  3197. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3198. journal = EXT4_SB(sb)->s_journal;
  3199. /*
  3200. * Now check for any error status which may have been recorded in the
  3201. * journal by a prior ext4_error() or ext4_abort()
  3202. */
  3203. j_errno = jbd2_journal_errno(journal);
  3204. if (j_errno) {
  3205. char nbuf[16];
  3206. errstr = ext4_decode_error(sb, j_errno, nbuf);
  3207. ext4_warning(sb, "Filesystem error recorded "
  3208. "from previous mount: %s", errstr);
  3209. ext4_warning(sb, "Marking fs in need of filesystem check.");
  3210. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  3211. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  3212. ext4_commit_super(sb, 1);
  3213. jbd2_journal_clear_err(journal);
  3214. }
  3215. }
  3216. /*
  3217. * Force the running and committing transactions to commit,
  3218. * and wait on the commit.
  3219. */
  3220. int ext4_force_commit(struct super_block *sb)
  3221. {
  3222. journal_t *journal;
  3223. int ret = 0;
  3224. if (sb->s_flags & MS_RDONLY)
  3225. return 0;
  3226. journal = EXT4_SB(sb)->s_journal;
  3227. if (journal) {
  3228. vfs_check_frozen(sb, SB_FREEZE_TRANS);
  3229. ret = ext4_journal_force_commit(journal);
  3230. }
  3231. return ret;
  3232. }
  3233. static void ext4_write_super(struct super_block *sb)
  3234. {
  3235. lock_super(sb);
  3236. ext4_commit_super(sb, 1);
  3237. unlock_super(sb);
  3238. }
  3239. static int ext4_sync_fs(struct super_block *sb, int wait)
  3240. {
  3241. int ret = 0;
  3242. tid_t target;
  3243. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3244. trace_ext4_sync_fs(sb, wait);
  3245. flush_workqueue(sbi->dio_unwritten_wq);
  3246. if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
  3247. if (wait)
  3248. jbd2_log_wait_commit(sbi->s_journal, target);
  3249. }
  3250. return ret;
  3251. }
  3252. /*
  3253. * LVM calls this function before a (read-only) snapshot is created. This
  3254. * gives us a chance to flush the journal completely and mark the fs clean.
  3255. */
  3256. static int ext4_freeze(struct super_block *sb)
  3257. {
  3258. int error = 0;
  3259. journal_t *journal;
  3260. if (sb->s_flags & MS_RDONLY)
  3261. return 0;
  3262. journal = EXT4_SB(sb)->s_journal;
  3263. /* Now we set up the journal barrier. */
  3264. jbd2_journal_lock_updates(journal);
  3265. /*
  3266. * Don't clear the needs_recovery flag if we failed to flush
  3267. * the journal.
  3268. */
  3269. error = jbd2_journal_flush(journal);
  3270. if (error < 0)
  3271. goto out;
  3272. /* Journal blocked and flushed, clear needs_recovery flag. */
  3273. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3274. error = ext4_commit_super(sb, 1);
  3275. out:
  3276. /* we rely on s_frozen to stop further updates */
  3277. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  3278. return error;
  3279. }
  3280. /*
  3281. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  3282. * flag here, even though the filesystem is not technically dirty yet.
  3283. */
  3284. static int ext4_unfreeze(struct super_block *sb)
  3285. {
  3286. if (sb->s_flags & MS_RDONLY)
  3287. return 0;
  3288. lock_super(sb);
  3289. /* Reset the needs_recovery flag before the fs is unlocked. */
  3290. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3291. ext4_commit_super(sb, 1);
  3292. unlock_super(sb);
  3293. return 0;
  3294. }
  3295. static int ext4_remount(struct super_block *sb, int *flags, char *data)
  3296. {
  3297. struct ext4_super_block *es;
  3298. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3299. ext4_fsblk_t n_blocks_count = 0;
  3300. unsigned long old_sb_flags;
  3301. struct ext4_mount_options old_opts;
  3302. int enable_quota = 0;
  3303. ext4_group_t g;
  3304. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  3305. int err;
  3306. #ifdef CONFIG_QUOTA
  3307. int i;
  3308. #endif
  3309. char *orig_data = kstrdup(data, GFP_KERNEL);
  3310. lock_kernel();
  3311. /* Store the original options */
  3312. lock_super(sb);
  3313. old_sb_flags = sb->s_flags;
  3314. old_opts.s_mount_opt = sbi->s_mount_opt;
  3315. old_opts.s_resuid = sbi->s_resuid;
  3316. old_opts.s_resgid = sbi->s_resgid;
  3317. old_opts.s_commit_interval = sbi->s_commit_interval;
  3318. old_opts.s_min_batch_time = sbi->s_min_batch_time;
  3319. old_opts.s_max_batch_time = sbi->s_max_batch_time;
  3320. #ifdef CONFIG_QUOTA
  3321. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  3322. for (i = 0; i < MAXQUOTAS; i++)
  3323. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  3324. #endif
  3325. if (sbi->s_journal && sbi->s_journal->j_task->io_context)
  3326. journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
  3327. /*
  3328. * Allow the "check" option to be passed as a remount option.
  3329. */
  3330. if (!parse_options(data, sb, NULL, &journal_ioprio,
  3331. &n_blocks_count, 1)) {
  3332. err = -EINVAL;
  3333. goto restore_opts;
  3334. }
  3335. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
  3336. ext4_abort(sb, "Abort forced by user");
  3337. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  3338. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  3339. es = sbi->s_es;
  3340. if (sbi->s_journal) {
  3341. ext4_init_journal_params(sb, sbi->s_journal);
  3342. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3343. }
  3344. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  3345. n_blocks_count > ext4_blocks_count(es)) {
  3346. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
  3347. err = -EROFS;
  3348. goto restore_opts;
  3349. }
  3350. if (*flags & MS_RDONLY) {
  3351. err = dquot_suspend(sb, -1);
  3352. if (err < 0)
  3353. goto restore_opts;
  3354. /*
  3355. * First of all, the unconditional stuff we have to do
  3356. * to disable replay of the journal when we next remount
  3357. */
  3358. sb->s_flags |= MS_RDONLY;
  3359. /*
  3360. * OK, test if we are remounting a valid rw partition
  3361. * readonly, and if so set the rdonly flag and then
  3362. * mark the partition as valid again.
  3363. */
  3364. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  3365. (sbi->s_mount_state & EXT4_VALID_FS))
  3366. es->s_state = cpu_to_le16(sbi->s_mount_state);
  3367. if (sbi->s_journal)
  3368. ext4_mark_recovery_complete(sb, es);
  3369. } else {
  3370. /* Make sure we can mount this feature set readwrite */
  3371. if (!ext4_feature_set_ok(sb, 0)) {
  3372. err = -EROFS;
  3373. goto restore_opts;
  3374. }
  3375. /*
  3376. * Make sure the group descriptor checksums
  3377. * are sane. If they aren't, refuse to remount r/w.
  3378. */
  3379. for (g = 0; g < sbi->s_groups_count; g++) {
  3380. struct ext4_group_desc *gdp =
  3381. ext4_get_group_desc(sb, g, NULL);
  3382. if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
  3383. ext4_msg(sb, KERN_ERR,
  3384. "ext4_remount: Checksum for group %u failed (%u!=%u)",
  3385. g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
  3386. le16_to_cpu(gdp->bg_checksum));
  3387. err = -EINVAL;
  3388. goto restore_opts;
  3389. }
  3390. }
  3391. /*
  3392. * If we have an unprocessed orphan list hanging
  3393. * around from a previously readonly bdev mount,
  3394. * require a full umount/remount for now.
  3395. */
  3396. if (es->s_last_orphan) {
  3397. ext4_msg(sb, KERN_WARNING, "Couldn't "
  3398. "remount RDWR because of unprocessed "
  3399. "orphan inode list. Please "
  3400. "umount/remount instead");
  3401. err = -EINVAL;
  3402. goto restore_opts;
  3403. }
  3404. /*
  3405. * Mounting a RDONLY partition read-write, so reread
  3406. * and store the current valid flag. (It may have
  3407. * been changed by e2fsck since we originally mounted
  3408. * the partition.)
  3409. */
  3410. if (sbi->s_journal)
  3411. ext4_clear_journal_err(sb, es);
  3412. sbi->s_mount_state = le16_to_cpu(es->s_state);
  3413. if ((err = ext4_group_extend(sb, es, n_blocks_count)))
  3414. goto restore_opts;
  3415. if (!ext4_setup_super(sb, es, 0))
  3416. sb->s_flags &= ~MS_RDONLY;
  3417. enable_quota = 1;
  3418. }
  3419. }
  3420. ext4_setup_system_zone(sb);
  3421. if (sbi->s_journal == NULL)
  3422. ext4_commit_super(sb, 1);
  3423. #ifdef CONFIG_QUOTA
  3424. /* Release old quota file names */
  3425. for (i = 0; i < MAXQUOTAS; i++)
  3426. if (old_opts.s_qf_names[i] &&
  3427. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3428. kfree(old_opts.s_qf_names[i]);
  3429. #endif
  3430. unlock_super(sb);
  3431. unlock_kernel();
  3432. if (enable_quota)
  3433. dquot_resume(sb, -1);
  3434. ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
  3435. kfree(orig_data);
  3436. return 0;
  3437. restore_opts:
  3438. sb->s_flags = old_sb_flags;
  3439. sbi->s_mount_opt = old_opts.s_mount_opt;
  3440. sbi->s_resuid = old_opts.s_resuid;
  3441. sbi->s_resgid = old_opts.s_resgid;
  3442. sbi->s_commit_interval = old_opts.s_commit_interval;
  3443. sbi->s_min_batch_time = old_opts.s_min_batch_time;
  3444. sbi->s_max_batch_time = old_opts.s_max_batch_time;
  3445. #ifdef CONFIG_QUOTA
  3446. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  3447. for (i = 0; i < MAXQUOTAS; i++) {
  3448. if (sbi->s_qf_names[i] &&
  3449. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3450. kfree(sbi->s_qf_names[i]);
  3451. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  3452. }
  3453. #endif
  3454. unlock_super(sb);
  3455. unlock_kernel();
  3456. kfree(orig_data);
  3457. return err;
  3458. }
  3459. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
  3460. {
  3461. struct super_block *sb = dentry->d_sb;
  3462. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3463. struct ext4_super_block *es = sbi->s_es;
  3464. u64 fsid;
  3465. if (test_opt(sb, MINIX_DF)) {
  3466. sbi->s_overhead_last = 0;
  3467. } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
  3468. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3469. ext4_fsblk_t overhead = 0;
  3470. /*
  3471. * Compute the overhead (FS structures). This is constant
  3472. * for a given filesystem unless the number of block groups
  3473. * changes so we cache the previous value until it does.
  3474. */
  3475. /*
  3476. * All of the blocks before first_data_block are
  3477. * overhead
  3478. */
  3479. overhead = le32_to_cpu(es->s_first_data_block);
  3480. /*
  3481. * Add the overhead attributed to the superblock and
  3482. * block group descriptors. If the sparse superblocks
  3483. * feature is turned on, then not all groups have this.
  3484. */
  3485. for (i = 0; i < ngroups; i++) {
  3486. overhead += ext4_bg_has_super(sb, i) +
  3487. ext4_bg_num_gdb(sb, i);
  3488. cond_resched();
  3489. }
  3490. /*
  3491. * Every block group has an inode bitmap, a block
  3492. * bitmap, and an inode table.
  3493. */
  3494. overhead += ngroups * (2 + sbi->s_itb_per_group);
  3495. sbi->s_overhead_last = overhead;
  3496. smp_wmb();
  3497. sbi->s_blocks_last = ext4_blocks_count(es);
  3498. }
  3499. buf->f_type = EXT4_SUPER_MAGIC;
  3500. buf->f_bsize = sb->s_blocksize;
  3501. buf->f_blocks = ext4_blocks_count(es) - sbi->s_overhead_last;
  3502. buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter) -
  3503. percpu_counter_sum_positive(&sbi->s_dirtyblocks_counter);
  3504. buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
  3505. if (buf->f_bfree < ext4_r_blocks_count(es))
  3506. buf->f_bavail = 0;
  3507. buf->f_files = le32_to_cpu(es->s_inodes_count);
  3508. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  3509. buf->f_namelen = EXT4_NAME_LEN;
  3510. fsid = le64_to_cpup((void *)es->s_uuid) ^
  3511. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  3512. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  3513. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  3514. return 0;
  3515. }
  3516. /* Helper function for writing quotas on sync - we need to start transaction
  3517. * before quota file is locked for write. Otherwise the are possible deadlocks:
  3518. * Process 1 Process 2
  3519. * ext4_create() quota_sync()
  3520. * jbd2_journal_start() write_dquot()
  3521. * dquot_initialize() down(dqio_mutex)
  3522. * down(dqio_mutex) jbd2_journal_start()
  3523. *
  3524. */
  3525. #ifdef CONFIG_QUOTA
  3526. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  3527. {
  3528. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  3529. }
  3530. static int ext4_write_dquot(struct dquot *dquot)
  3531. {
  3532. int ret, err;
  3533. handle_t *handle;
  3534. struct inode *inode;
  3535. inode = dquot_to_inode(dquot);
  3536. handle = ext4_journal_start(inode,
  3537. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  3538. if (IS_ERR(handle))
  3539. return PTR_ERR(handle);
  3540. ret = dquot_commit(dquot);
  3541. err = ext4_journal_stop(handle);
  3542. if (!ret)
  3543. ret = err;
  3544. return ret;
  3545. }
  3546. static int ext4_acquire_dquot(struct dquot *dquot)
  3547. {
  3548. int ret, err;
  3549. handle_t *handle;
  3550. handle = ext4_journal_start(dquot_to_inode(dquot),
  3551. EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  3552. if (IS_ERR(handle))
  3553. return PTR_ERR(handle);
  3554. ret = dquot_acquire(dquot);
  3555. err = ext4_journal_stop(handle);
  3556. if (!ret)
  3557. ret = err;
  3558. return ret;
  3559. }
  3560. static int ext4_release_dquot(struct dquot *dquot)
  3561. {
  3562. int ret, err;
  3563. handle_t *handle;
  3564. handle = ext4_journal_start(dquot_to_inode(dquot),
  3565. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  3566. if (IS_ERR(handle)) {
  3567. /* Release dquot anyway to avoid endless cycle in dqput() */
  3568. dquot_release(dquot);
  3569. return PTR_ERR(handle);
  3570. }
  3571. ret = dquot_release(dquot);
  3572. err = ext4_journal_stop(handle);
  3573. if (!ret)
  3574. ret = err;
  3575. return ret;
  3576. }
  3577. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  3578. {
  3579. /* Are we journaling quotas? */
  3580. if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  3581. EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  3582. dquot_mark_dquot_dirty(dquot);
  3583. return ext4_write_dquot(dquot);
  3584. } else {
  3585. return dquot_mark_dquot_dirty(dquot);
  3586. }
  3587. }
  3588. static int ext4_write_info(struct super_block *sb, int type)
  3589. {
  3590. int ret, err;
  3591. handle_t *handle;
  3592. /* Data block + inode block */
  3593. handle = ext4_journal_start(sb->s_root->d_inode, 2);
  3594. if (IS_ERR(handle))
  3595. return PTR_ERR(handle);
  3596. ret = dquot_commit_info(sb, type);
  3597. err = ext4_journal_stop(handle);
  3598. if (!ret)
  3599. ret = err;
  3600. return ret;
  3601. }
  3602. /*
  3603. * Turn on quotas during mount time - we need to find
  3604. * the quota file and such...
  3605. */
  3606. static int ext4_quota_on_mount(struct super_block *sb, int type)
  3607. {
  3608. return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
  3609. EXT4_SB(sb)->s_jquota_fmt, type);
  3610. }
  3611. /*
  3612. * Standard function to be called on quota_on
  3613. */
  3614. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  3615. char *name)
  3616. {
  3617. int err;
  3618. struct path path;
  3619. if (!test_opt(sb, QUOTA))
  3620. return -EINVAL;
  3621. err = kern_path(name, LOOKUP_FOLLOW, &path);
  3622. if (err)
  3623. return err;
  3624. /* Quotafile not on the same filesystem? */
  3625. if (path.mnt->mnt_sb != sb) {
  3626. path_put(&path);
  3627. return -EXDEV;
  3628. }
  3629. /* Journaling quota? */
  3630. if (EXT4_SB(sb)->s_qf_names[type]) {
  3631. /* Quotafile not in fs root? */
  3632. if (path.dentry->d_parent != sb->s_root)
  3633. ext4_msg(sb, KERN_WARNING,
  3634. "Quota file not on filesystem root. "
  3635. "Journaled quota will not work");
  3636. }
  3637. /*
  3638. * When we journal data on quota file, we have to flush journal to see
  3639. * all updates to the file when we bypass pagecache...
  3640. */
  3641. if (EXT4_SB(sb)->s_journal &&
  3642. ext4_should_journal_data(path.dentry->d_inode)) {
  3643. /*
  3644. * We don't need to lock updates but journal_flush() could
  3645. * otherwise be livelocked...
  3646. */
  3647. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  3648. err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  3649. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  3650. if (err) {
  3651. path_put(&path);
  3652. return err;
  3653. }
  3654. }
  3655. err = dquot_quota_on_path(sb, type, format_id, &path);
  3656. path_put(&path);
  3657. return err;
  3658. }
  3659. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  3660. * acquiring the locks... As quota files are never truncated and quota code
  3661. * itself serializes the operations (and noone else should touch the files)
  3662. * we don't have to be afraid of races */
  3663. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  3664. size_t len, loff_t off)
  3665. {
  3666. struct inode *inode = sb_dqopt(sb)->files[type];
  3667. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  3668. int err = 0;
  3669. int offset = off & (sb->s_blocksize - 1);
  3670. int tocopy;
  3671. size_t toread;
  3672. struct buffer_head *bh;
  3673. loff_t i_size = i_size_read(inode);
  3674. if (off > i_size)
  3675. return 0;
  3676. if (off+len > i_size)
  3677. len = i_size-off;
  3678. toread = len;
  3679. while (toread > 0) {
  3680. tocopy = sb->s_blocksize - offset < toread ?
  3681. sb->s_blocksize - offset : toread;
  3682. bh = ext4_bread(NULL, inode, blk, 0, &err);
  3683. if (err)
  3684. return err;
  3685. if (!bh) /* A hole? */
  3686. memset(data, 0, tocopy);
  3687. else
  3688. memcpy(data, bh->b_data+offset, tocopy);
  3689. brelse(bh);
  3690. offset = 0;
  3691. toread -= tocopy;
  3692. data += tocopy;
  3693. blk++;
  3694. }
  3695. return len;
  3696. }
  3697. /* Write to quotafile (we know the transaction is already started and has
  3698. * enough credits) */
  3699. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  3700. const char *data, size_t len, loff_t off)
  3701. {
  3702. struct inode *inode = sb_dqopt(sb)->files[type];
  3703. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  3704. int err = 0;
  3705. int offset = off & (sb->s_blocksize - 1);
  3706. struct buffer_head *bh;
  3707. handle_t *handle = journal_current_handle();
  3708. if (EXT4_SB(sb)->s_journal && !handle) {
  3709. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  3710. " cancelled because transaction is not started",
  3711. (unsigned long long)off, (unsigned long long)len);
  3712. return -EIO;
  3713. }
  3714. /*
  3715. * Since we account only one data block in transaction credits,
  3716. * then it is impossible to cross a block boundary.
  3717. */
  3718. if (sb->s_blocksize - offset < len) {
  3719. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  3720. " cancelled because not block aligned",
  3721. (unsigned long long)off, (unsigned long long)len);
  3722. return -EIO;
  3723. }
  3724. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  3725. bh = ext4_bread(handle, inode, blk, 1, &err);
  3726. if (!bh)
  3727. goto out;
  3728. err = ext4_journal_get_write_access(handle, bh);
  3729. if (err) {
  3730. brelse(bh);
  3731. goto out;
  3732. }
  3733. lock_buffer(bh);
  3734. memcpy(bh->b_data+offset, data, len);
  3735. flush_dcache_page(bh->b_page);
  3736. unlock_buffer(bh);
  3737. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  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)