super.c 93 KB

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