super.c 96 KB

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