super.c 106 KB

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