super.c 93 KB

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