super.c 101 KB

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