super.c 112 KB

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