super.c 98 KB

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