super.c 108 KB

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