super.c 103 KB

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