super.c 96 KB

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