super.c 111 KB

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