super.c 99 KB

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