super.c 112 KB

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