super.c 101 KB

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