super.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066
  1. /*
  2. * linux/fs/ext3/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/jbd.h>
  23. #include <linux/ext3_fs.h>
  24. #include <linux/ext3_jbd.h>
  25. #include <linux/slab.h>
  26. #include <linux/init.h>
  27. #include <linux/blkdev.h>
  28. #include <linux/parser.h>
  29. #include <linux/buffer_head.h>
  30. #include <linux/exportfs.h>
  31. #include <linux/vfs.h>
  32. #include <linux/random.h>
  33. #include <linux/mount.h>
  34. #include <linux/namei.h>
  35. #include <linux/quotaops.h>
  36. #include <linux/seq_file.h>
  37. #include <linux/log2.h>
  38. #include <asm/uaccess.h>
  39. #include "xattr.h"
  40. #include "acl.h"
  41. #include "namei.h"
  42. #ifdef CONFIG_EXT3_DEFAULTS_TO_ORDERED
  43. #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_ORDERED_DATA
  44. #else
  45. #define EXT3_MOUNT_DEFAULT_DATA_MODE EXT3_MOUNT_WRITEBACK_DATA
  46. #endif
  47. static int ext3_load_journal(struct super_block *, struct ext3_super_block *,
  48. unsigned long journal_devnum);
  49. static int ext3_create_journal(struct super_block *, struct ext3_super_block *,
  50. unsigned int);
  51. static int ext3_commit_super(struct super_block *sb,
  52. struct ext3_super_block *es,
  53. int sync);
  54. static void ext3_mark_recovery_complete(struct super_block * sb,
  55. struct ext3_super_block * es);
  56. static void ext3_clear_journal_err(struct super_block * sb,
  57. struct ext3_super_block * es);
  58. static int ext3_sync_fs(struct super_block *sb, int wait);
  59. static const char *ext3_decode_error(struct super_block * sb, int errno,
  60. char nbuf[16]);
  61. static int ext3_remount (struct super_block * sb, int * flags, char * data);
  62. static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf);
  63. static int ext3_unfreeze(struct super_block *sb);
  64. static int ext3_freeze(struct super_block *sb);
  65. /*
  66. * Wrappers for journal_start/end.
  67. *
  68. * The only special thing we need to do here is to make sure that all
  69. * journal_end calls result in the superblock being marked dirty, so
  70. * that sync() will call the filesystem's write_super callback if
  71. * appropriate.
  72. */
  73. handle_t *ext3_journal_start_sb(struct super_block *sb, int nblocks)
  74. {
  75. journal_t *journal;
  76. if (sb->s_flags & MS_RDONLY)
  77. return ERR_PTR(-EROFS);
  78. /* Special case here: if the journal has aborted behind our
  79. * backs (eg. EIO in the commit thread), then we still need to
  80. * take the FS itself readonly cleanly. */
  81. journal = EXT3_SB(sb)->s_journal;
  82. if (is_journal_aborted(journal)) {
  83. ext3_abort(sb, __func__,
  84. "Detected aborted journal");
  85. return ERR_PTR(-EROFS);
  86. }
  87. return journal_start(journal, nblocks);
  88. }
  89. /*
  90. * The only special thing we need to do here is to make sure that all
  91. * journal_stop calls result in the superblock being marked dirty, so
  92. * that sync() will call the filesystem's write_super callback if
  93. * appropriate.
  94. */
  95. int __ext3_journal_stop(const char *where, handle_t *handle)
  96. {
  97. struct super_block *sb;
  98. int err;
  99. int rc;
  100. sb = handle->h_transaction->t_journal->j_private;
  101. err = handle->h_err;
  102. rc = journal_stop(handle);
  103. if (!err)
  104. err = rc;
  105. if (err)
  106. __ext3_std_error(sb, where, err);
  107. return err;
  108. }
  109. void ext3_journal_abort_handle(const char *caller, const char *err_fn,
  110. struct buffer_head *bh, handle_t *handle, int err)
  111. {
  112. char nbuf[16];
  113. const char *errstr = ext3_decode_error(NULL, err, nbuf);
  114. if (bh)
  115. BUFFER_TRACE(bh, "abort");
  116. if (!handle->h_err)
  117. handle->h_err = err;
  118. if (is_handle_aborted(handle))
  119. return;
  120. printk(KERN_ERR "EXT3-fs: %s: aborting transaction: %s in %s\n",
  121. caller, errstr, err_fn);
  122. journal_abort_handle(handle);
  123. }
  124. void ext3_msg(struct super_block *sb, const char *prefix,
  125. const char *fmt, ...)
  126. {
  127. va_list args;
  128. va_start(args, fmt);
  129. printk("%sEXT3-fs (%s): ", prefix, sb->s_id);
  130. vprintk(fmt, args);
  131. printk("\n");
  132. va_end(args);
  133. }
  134. /* Deal with the reporting of failure conditions on a filesystem such as
  135. * inconsistencies detected or read IO failures.
  136. *
  137. * On ext2, we can store the error state of the filesystem in the
  138. * superblock. That is not possible on ext3, because we may have other
  139. * write ordering constraints on the superblock which prevent us from
  140. * writing it out straight away; and given that the journal is about to
  141. * be aborted, we can't rely on the current, or future, transactions to
  142. * write out the superblock safely.
  143. *
  144. * We'll just use the journal_abort() error code to record an error in
  145. * the journal instead. On recovery, the journal will complain about
  146. * that error until we've noted it down and cleared it.
  147. */
  148. static void ext3_handle_error(struct super_block *sb)
  149. {
  150. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  151. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  152. es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
  153. if (sb->s_flags & MS_RDONLY)
  154. return;
  155. if (!test_opt (sb, ERRORS_CONT)) {
  156. journal_t *journal = EXT3_SB(sb)->s_journal;
  157. set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
  158. if (journal)
  159. journal_abort(journal, -EIO);
  160. }
  161. if (test_opt (sb, ERRORS_RO)) {
  162. ext3_msg(sb, KERN_CRIT,
  163. "error: remounting filesystem read-only");
  164. sb->s_flags |= MS_RDONLY;
  165. }
  166. ext3_commit_super(sb, es, 1);
  167. if (test_opt(sb, ERRORS_PANIC))
  168. panic("EXT3-fs (%s): panic forced after error\n",
  169. sb->s_id);
  170. }
  171. void ext3_error (struct super_block * sb, const char * function,
  172. const char * fmt, ...)
  173. {
  174. va_list args;
  175. va_start(args, fmt);
  176. printk(KERN_CRIT "EXT3-fs error (device %s): %s: ",sb->s_id, function);
  177. vprintk(fmt, args);
  178. printk("\n");
  179. va_end(args);
  180. ext3_handle_error(sb);
  181. }
  182. static const char *ext3_decode_error(struct super_block * sb, int errno,
  183. char nbuf[16])
  184. {
  185. char *errstr = NULL;
  186. switch (errno) {
  187. case -EIO:
  188. errstr = "IO failure";
  189. break;
  190. case -ENOMEM:
  191. errstr = "Out of memory";
  192. break;
  193. case -EROFS:
  194. if (!sb || EXT3_SB(sb)->s_journal->j_flags & JFS_ABORT)
  195. errstr = "Journal has aborted";
  196. else
  197. errstr = "Readonly filesystem";
  198. break;
  199. default:
  200. /* If the caller passed in an extra buffer for unknown
  201. * errors, textualise them now. Else we just return
  202. * NULL. */
  203. if (nbuf) {
  204. /* Check for truncated error codes... */
  205. if (snprintf(nbuf, 16, "error %d", -errno) >= 0)
  206. errstr = nbuf;
  207. }
  208. break;
  209. }
  210. return errstr;
  211. }
  212. /* __ext3_std_error decodes expected errors from journaling functions
  213. * automatically and invokes the appropriate error response. */
  214. void __ext3_std_error (struct super_block * sb, const char * function,
  215. int errno)
  216. {
  217. char nbuf[16];
  218. const char *errstr;
  219. /* Special case: if the error is EROFS, and we're not already
  220. * inside a transaction, then there's really no point in logging
  221. * an error. */
  222. if (errno == -EROFS && journal_current_handle() == NULL &&
  223. (sb->s_flags & MS_RDONLY))
  224. return;
  225. errstr = ext3_decode_error(sb, errno, nbuf);
  226. ext3_msg(sb, KERN_CRIT, "error in %s: %s", function, errstr);
  227. ext3_handle_error(sb);
  228. }
  229. /*
  230. * ext3_abort is a much stronger failure handler than ext3_error. The
  231. * abort function may be used to deal with unrecoverable failures such
  232. * as journal IO errors or ENOMEM at a critical moment in log management.
  233. *
  234. * We unconditionally force the filesystem into an ABORT|READONLY state,
  235. * unless the error response on the fs has been set to panic in which
  236. * case we take the easy way out and panic immediately.
  237. */
  238. void ext3_abort (struct super_block * sb, const char * function,
  239. const char * fmt, ...)
  240. {
  241. va_list args;
  242. va_start(args, fmt);
  243. printk(KERN_CRIT "EXT3-fs (%s): error: %s: ", sb->s_id, function);
  244. vprintk(fmt, args);
  245. printk("\n");
  246. va_end(args);
  247. if (test_opt(sb, ERRORS_PANIC))
  248. panic("EXT3-fs: panic from previous error\n");
  249. if (sb->s_flags & MS_RDONLY)
  250. return;
  251. ext3_msg(sb, KERN_CRIT,
  252. "error: remounting filesystem read-only");
  253. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  254. sb->s_flags |= MS_RDONLY;
  255. set_opt(EXT3_SB(sb)->s_mount_opt, ABORT);
  256. if (EXT3_SB(sb)->s_journal)
  257. journal_abort(EXT3_SB(sb)->s_journal, -EIO);
  258. }
  259. void ext3_warning (struct super_block * sb, const char * function,
  260. const char * fmt, ...)
  261. {
  262. va_list args;
  263. va_start(args, fmt);
  264. printk(KERN_WARNING "EXT3-fs (%s): warning: %s: ",
  265. sb->s_id, function);
  266. vprintk(fmt, args);
  267. printk("\n");
  268. va_end(args);
  269. }
  270. void ext3_update_dynamic_rev(struct super_block *sb)
  271. {
  272. struct ext3_super_block *es = EXT3_SB(sb)->s_es;
  273. if (le32_to_cpu(es->s_rev_level) > EXT3_GOOD_OLD_REV)
  274. return;
  275. ext3_msg(sb, KERN_WARNING,
  276. "warning: updating to rev %d because of "
  277. "new feature flag, running e2fsck is recommended",
  278. EXT3_DYNAMIC_REV);
  279. es->s_first_ino = cpu_to_le32(EXT3_GOOD_OLD_FIRST_INO);
  280. es->s_inode_size = cpu_to_le16(EXT3_GOOD_OLD_INODE_SIZE);
  281. es->s_rev_level = cpu_to_le32(EXT3_DYNAMIC_REV);
  282. /* leave es->s_feature_*compat flags alone */
  283. /* es->s_uuid will be set by e2fsck if empty */
  284. /*
  285. * The rest of the superblock fields should be zero, and if not it
  286. * means they are likely already in use, so leave them alone. We
  287. * can leave it up to e2fsck to clean up any inconsistencies there.
  288. */
  289. }
  290. /*
  291. * Open the external journal device
  292. */
  293. static struct block_device *ext3_blkdev_get(dev_t dev, struct super_block *sb)
  294. {
  295. struct block_device *bdev;
  296. char b[BDEVNAME_SIZE];
  297. bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE);
  298. if (IS_ERR(bdev))
  299. goto fail;
  300. return bdev;
  301. fail:
  302. ext3_msg(sb, "error: failed to open journal device %s: %ld",
  303. __bdevname(dev, b), PTR_ERR(bdev));
  304. return NULL;
  305. }
  306. /*
  307. * Release the journal device
  308. */
  309. static int ext3_blkdev_put(struct block_device *bdev)
  310. {
  311. bd_release(bdev);
  312. return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
  313. }
  314. static int ext3_blkdev_remove(struct ext3_sb_info *sbi)
  315. {
  316. struct block_device *bdev;
  317. int ret = -ENODEV;
  318. bdev = sbi->journal_bdev;
  319. if (bdev) {
  320. ret = ext3_blkdev_put(bdev);
  321. sbi->journal_bdev = NULL;
  322. }
  323. return ret;
  324. }
  325. static inline struct inode *orphan_list_entry(struct list_head *l)
  326. {
  327. return &list_entry(l, struct ext3_inode_info, i_orphan)->vfs_inode;
  328. }
  329. static void dump_orphan_list(struct super_block *sb, struct ext3_sb_info *sbi)
  330. {
  331. struct list_head *l;
  332. ext3_msg(sb, KERN_ERR, "error: sb orphan head is %d",
  333. le32_to_cpu(sbi->s_es->s_last_orphan));
  334. ext3_msg(sb, KERN_ERR, "sb_info orphan list:");
  335. list_for_each(l, &sbi->s_orphan) {
  336. struct inode *inode = orphan_list_entry(l);
  337. ext3_msg(sb, KERN_ERR, " "
  338. "inode %s:%lu at %p: mode %o, nlink %d, next %d\n",
  339. inode->i_sb->s_id, inode->i_ino, inode,
  340. inode->i_mode, inode->i_nlink,
  341. NEXT_ORPHAN(inode));
  342. }
  343. }
  344. static void ext3_put_super (struct super_block * sb)
  345. {
  346. struct ext3_sb_info *sbi = EXT3_SB(sb);
  347. struct ext3_super_block *es = sbi->s_es;
  348. int i, err;
  349. dquot_disable(sb, -1, DQUOT_USAGE_ENABLED | DQUOT_LIMITS_ENABLED);
  350. ext3_xattr_put_super(sb);
  351. err = journal_destroy(sbi->s_journal);
  352. sbi->s_journal = NULL;
  353. if (err < 0)
  354. ext3_abort(sb, __func__, "Couldn't clean up the journal");
  355. if (!(sb->s_flags & MS_RDONLY)) {
  356. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  357. es->s_state = cpu_to_le16(sbi->s_mount_state);
  358. BUFFER_TRACE(sbi->s_sbh, "marking dirty");
  359. mark_buffer_dirty(sbi->s_sbh);
  360. ext3_commit_super(sb, es, 1);
  361. }
  362. for (i = 0; i < sbi->s_gdb_count; i++)
  363. brelse(sbi->s_group_desc[i]);
  364. kfree(sbi->s_group_desc);
  365. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  366. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  367. percpu_counter_destroy(&sbi->s_dirs_counter);
  368. brelse(sbi->s_sbh);
  369. #ifdef CONFIG_QUOTA
  370. for (i = 0; i < MAXQUOTAS; i++)
  371. kfree(sbi->s_qf_names[i]);
  372. #endif
  373. /* Debugging code just in case the in-memory inode orphan list
  374. * isn't empty. The on-disk one can be non-empty if we've
  375. * detected an error and taken the fs readonly, but the
  376. * in-memory list had better be clean by this point. */
  377. if (!list_empty(&sbi->s_orphan))
  378. dump_orphan_list(sb, sbi);
  379. J_ASSERT(list_empty(&sbi->s_orphan));
  380. invalidate_bdev(sb->s_bdev);
  381. if (sbi->journal_bdev && sbi->journal_bdev != sb->s_bdev) {
  382. /*
  383. * Invalidate the journal device's buffers. We don't want them
  384. * floating about in memory - the physical journal device may
  385. * hotswapped, and it breaks the `ro-after' testing code.
  386. */
  387. sync_blockdev(sbi->journal_bdev);
  388. invalidate_bdev(sbi->journal_bdev);
  389. ext3_blkdev_remove(sbi);
  390. }
  391. sb->s_fs_info = NULL;
  392. kfree(sbi->s_blockgroup_lock);
  393. kfree(sbi);
  394. }
  395. static struct kmem_cache *ext3_inode_cachep;
  396. /*
  397. * Called inside transaction, so use GFP_NOFS
  398. */
  399. static struct inode *ext3_alloc_inode(struct super_block *sb)
  400. {
  401. struct ext3_inode_info *ei;
  402. ei = kmem_cache_alloc(ext3_inode_cachep, GFP_NOFS);
  403. if (!ei)
  404. return NULL;
  405. ei->i_block_alloc_info = NULL;
  406. ei->vfs_inode.i_version = 1;
  407. atomic_set(&ei->i_datasync_tid, 0);
  408. atomic_set(&ei->i_sync_tid, 0);
  409. return &ei->vfs_inode;
  410. }
  411. static void ext3_destroy_inode(struct inode *inode)
  412. {
  413. if (!list_empty(&(EXT3_I(inode)->i_orphan))) {
  414. printk("EXT3 Inode %p: orphan list check failed!\n",
  415. EXT3_I(inode));
  416. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_ADDRESS, 16, 4,
  417. EXT3_I(inode), sizeof(struct ext3_inode_info),
  418. false);
  419. dump_stack();
  420. }
  421. kmem_cache_free(ext3_inode_cachep, EXT3_I(inode));
  422. }
  423. static void init_once(void *foo)
  424. {
  425. struct ext3_inode_info *ei = (struct ext3_inode_info *) foo;
  426. INIT_LIST_HEAD(&ei->i_orphan);
  427. #ifdef CONFIG_EXT3_FS_XATTR
  428. init_rwsem(&ei->xattr_sem);
  429. #endif
  430. mutex_init(&ei->truncate_mutex);
  431. inode_init_once(&ei->vfs_inode);
  432. }
  433. static int init_inodecache(void)
  434. {
  435. ext3_inode_cachep = kmem_cache_create("ext3_inode_cache",
  436. sizeof(struct ext3_inode_info),
  437. 0, (SLAB_RECLAIM_ACCOUNT|
  438. SLAB_MEM_SPREAD),
  439. init_once);
  440. if (ext3_inode_cachep == NULL)
  441. return -ENOMEM;
  442. return 0;
  443. }
  444. static void destroy_inodecache(void)
  445. {
  446. kmem_cache_destroy(ext3_inode_cachep);
  447. }
  448. static inline void ext3_show_quota_options(struct seq_file *seq, struct super_block *sb)
  449. {
  450. #if defined(CONFIG_QUOTA)
  451. struct ext3_sb_info *sbi = EXT3_SB(sb);
  452. if (sbi->s_jquota_fmt) {
  453. char *fmtname = "";
  454. switch (sbi->s_jquota_fmt) {
  455. case QFMT_VFS_OLD:
  456. fmtname = "vfsold";
  457. break;
  458. case QFMT_VFS_V0:
  459. fmtname = "vfsv0";
  460. break;
  461. case QFMT_VFS_V1:
  462. fmtname = "vfsv1";
  463. break;
  464. }
  465. seq_printf(seq, ",jqfmt=%s", fmtname);
  466. }
  467. if (sbi->s_qf_names[USRQUOTA])
  468. seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
  469. if (sbi->s_qf_names[GRPQUOTA])
  470. seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
  471. if (test_opt(sb, USRQUOTA))
  472. seq_puts(seq, ",usrquota");
  473. if (test_opt(sb, GRPQUOTA))
  474. seq_puts(seq, ",grpquota");
  475. #endif
  476. }
  477. static char *data_mode_string(unsigned long mode)
  478. {
  479. switch (mode) {
  480. case EXT3_MOUNT_JOURNAL_DATA:
  481. return "journal";
  482. case EXT3_MOUNT_ORDERED_DATA:
  483. return "ordered";
  484. case EXT3_MOUNT_WRITEBACK_DATA:
  485. return "writeback";
  486. }
  487. return "unknown";
  488. }
  489. /*
  490. * Show an option if
  491. * - it's set to a non-default value OR
  492. * - if the per-sb default is different from the global default
  493. */
  494. static int ext3_show_options(struct seq_file *seq, struct vfsmount *vfs)
  495. {
  496. struct super_block *sb = vfs->mnt_sb;
  497. struct ext3_sb_info *sbi = EXT3_SB(sb);
  498. struct ext3_super_block *es = sbi->s_es;
  499. unsigned long def_mount_opts;
  500. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  501. if (sbi->s_sb_block != 1)
  502. seq_printf(seq, ",sb=%lu", sbi->s_sb_block);
  503. if (test_opt(sb, MINIX_DF))
  504. seq_puts(seq, ",minixdf");
  505. if (test_opt(sb, GRPID))
  506. seq_puts(seq, ",grpid");
  507. if (!test_opt(sb, GRPID) && (def_mount_opts & EXT3_DEFM_BSDGROUPS))
  508. seq_puts(seq, ",nogrpid");
  509. if (sbi->s_resuid != EXT3_DEF_RESUID ||
  510. le16_to_cpu(es->s_def_resuid) != EXT3_DEF_RESUID) {
  511. seq_printf(seq, ",resuid=%u", sbi->s_resuid);
  512. }
  513. if (sbi->s_resgid != EXT3_DEF_RESGID ||
  514. le16_to_cpu(es->s_def_resgid) != EXT3_DEF_RESGID) {
  515. seq_printf(seq, ",resgid=%u", sbi->s_resgid);
  516. }
  517. if (test_opt(sb, ERRORS_RO)) {
  518. int def_errors = le16_to_cpu(es->s_errors);
  519. if (def_errors == EXT3_ERRORS_PANIC ||
  520. def_errors == EXT3_ERRORS_CONTINUE) {
  521. seq_puts(seq, ",errors=remount-ro");
  522. }
  523. }
  524. if (test_opt(sb, ERRORS_CONT))
  525. seq_puts(seq, ",errors=continue");
  526. if (test_opt(sb, ERRORS_PANIC))
  527. seq_puts(seq, ",errors=panic");
  528. if (test_opt(sb, NO_UID32))
  529. seq_puts(seq, ",nouid32");
  530. if (test_opt(sb, DEBUG))
  531. seq_puts(seq, ",debug");
  532. if (test_opt(sb, OLDALLOC))
  533. seq_puts(seq, ",oldalloc");
  534. #ifdef CONFIG_EXT3_FS_XATTR
  535. if (test_opt(sb, XATTR_USER))
  536. seq_puts(seq, ",user_xattr");
  537. if (!test_opt(sb, XATTR_USER) &&
  538. (def_mount_opts & EXT3_DEFM_XATTR_USER)) {
  539. seq_puts(seq, ",nouser_xattr");
  540. }
  541. #endif
  542. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  543. if (test_opt(sb, POSIX_ACL))
  544. seq_puts(seq, ",acl");
  545. if (!test_opt(sb, POSIX_ACL) && (def_mount_opts & EXT3_DEFM_ACL))
  546. seq_puts(seq, ",noacl");
  547. #endif
  548. if (!test_opt(sb, RESERVATION))
  549. seq_puts(seq, ",noreservation");
  550. if (sbi->s_commit_interval) {
  551. seq_printf(seq, ",commit=%u",
  552. (unsigned) (sbi->s_commit_interval / HZ));
  553. }
  554. /*
  555. * Always display barrier state so it's clear what the status is.
  556. */
  557. seq_puts(seq, ",barrier=");
  558. seq_puts(seq, test_opt(sb, BARRIER) ? "1" : "0");
  559. seq_printf(seq, ",data=%s", data_mode_string(test_opt(sb, DATA_FLAGS)));
  560. if (test_opt(sb, DATA_ERR_ABORT))
  561. seq_puts(seq, ",data_err=abort");
  562. if (test_opt(sb, NOLOAD))
  563. seq_puts(seq, ",norecovery");
  564. ext3_show_quota_options(seq, sb);
  565. return 0;
  566. }
  567. static struct inode *ext3_nfs_get_inode(struct super_block *sb,
  568. u64 ino, u32 generation)
  569. {
  570. struct inode *inode;
  571. if (ino < EXT3_FIRST_INO(sb) && ino != EXT3_ROOT_INO)
  572. return ERR_PTR(-ESTALE);
  573. if (ino > le32_to_cpu(EXT3_SB(sb)->s_es->s_inodes_count))
  574. return ERR_PTR(-ESTALE);
  575. /* iget isn't really right if the inode is currently unallocated!!
  576. *
  577. * ext3_read_inode will return a bad_inode if the inode had been
  578. * deleted, so we should be safe.
  579. *
  580. * Currently we don't know the generation for parent directory, so
  581. * a generation of 0 means "accept any"
  582. */
  583. inode = ext3_iget(sb, ino);
  584. if (IS_ERR(inode))
  585. return ERR_CAST(inode);
  586. if (generation && inode->i_generation != generation) {
  587. iput(inode);
  588. return ERR_PTR(-ESTALE);
  589. }
  590. return inode;
  591. }
  592. static struct dentry *ext3_fh_to_dentry(struct super_block *sb, struct fid *fid,
  593. int fh_len, int fh_type)
  594. {
  595. return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
  596. ext3_nfs_get_inode);
  597. }
  598. static struct dentry *ext3_fh_to_parent(struct super_block *sb, struct fid *fid,
  599. int fh_len, int fh_type)
  600. {
  601. return generic_fh_to_parent(sb, fid, fh_len, fh_type,
  602. ext3_nfs_get_inode);
  603. }
  604. /*
  605. * Try to release metadata pages (indirect blocks, directories) which are
  606. * mapped via the block device. Since these pages could have journal heads
  607. * which would prevent try_to_free_buffers() from freeing them, we must use
  608. * jbd layer's try_to_free_buffers() function to release them.
  609. */
  610. static int bdev_try_to_free_page(struct super_block *sb, struct page *page,
  611. gfp_t wait)
  612. {
  613. journal_t *journal = EXT3_SB(sb)->s_journal;
  614. WARN_ON(PageChecked(page));
  615. if (!page_has_buffers(page))
  616. return 0;
  617. if (journal)
  618. return journal_try_to_free_buffers(journal, page,
  619. wait & ~__GFP_WAIT);
  620. return try_to_free_buffers(page);
  621. }
  622. #ifdef CONFIG_QUOTA
  623. #define QTYPE2NAME(t) ((t)==USRQUOTA?"user":"group")
  624. #define QTYPE2MOPT(on, t) ((t)==USRQUOTA?((on)##USRJQUOTA):((on)##GRPJQUOTA))
  625. static int ext3_write_dquot(struct dquot *dquot);
  626. static int ext3_acquire_dquot(struct dquot *dquot);
  627. static int ext3_release_dquot(struct dquot *dquot);
  628. static int ext3_mark_dquot_dirty(struct dquot *dquot);
  629. static int ext3_write_info(struct super_block *sb, int type);
  630. static int ext3_quota_on(struct super_block *sb, int type, int format_id,
  631. char *path);
  632. static int ext3_quota_on_mount(struct super_block *sb, int type);
  633. static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
  634. size_t len, loff_t off);
  635. static ssize_t ext3_quota_write(struct super_block *sb, int type,
  636. const char *data, size_t len, loff_t off);
  637. static const struct dquot_operations ext3_quota_operations = {
  638. .write_dquot = ext3_write_dquot,
  639. .acquire_dquot = ext3_acquire_dquot,
  640. .release_dquot = ext3_release_dquot,
  641. .mark_dirty = ext3_mark_dquot_dirty,
  642. .write_info = ext3_write_info,
  643. .alloc_dquot = dquot_alloc,
  644. .destroy_dquot = dquot_destroy,
  645. };
  646. static const struct quotactl_ops ext3_qctl_operations = {
  647. .quota_on = ext3_quota_on,
  648. .quota_off = dquot_quota_off,
  649. .quota_sync = dquot_quota_sync,
  650. .get_info = dquot_get_dqinfo,
  651. .set_info = dquot_set_dqinfo,
  652. .get_dqblk = dquot_get_dqblk,
  653. .set_dqblk = dquot_set_dqblk
  654. };
  655. #endif
  656. static const struct super_operations ext3_sops = {
  657. .alloc_inode = ext3_alloc_inode,
  658. .destroy_inode = ext3_destroy_inode,
  659. .write_inode = ext3_write_inode,
  660. .dirty_inode = ext3_dirty_inode,
  661. .evict_inode = ext3_evict_inode,
  662. .put_super = ext3_put_super,
  663. .sync_fs = ext3_sync_fs,
  664. .freeze_fs = ext3_freeze,
  665. .unfreeze_fs = ext3_unfreeze,
  666. .statfs = ext3_statfs,
  667. .remount_fs = ext3_remount,
  668. .show_options = ext3_show_options,
  669. #ifdef CONFIG_QUOTA
  670. .quota_read = ext3_quota_read,
  671. .quota_write = ext3_quota_write,
  672. #endif
  673. .bdev_try_to_free_page = bdev_try_to_free_page,
  674. };
  675. static const struct export_operations ext3_export_ops = {
  676. .fh_to_dentry = ext3_fh_to_dentry,
  677. .fh_to_parent = ext3_fh_to_parent,
  678. .get_parent = ext3_get_parent,
  679. };
  680. enum {
  681. Opt_bsd_df, Opt_minix_df, Opt_grpid, Opt_nogrpid,
  682. Opt_resgid, Opt_resuid, Opt_sb, Opt_err_cont, Opt_err_panic, Opt_err_ro,
  683. Opt_nouid32, Opt_nocheck, Opt_debug, Opt_oldalloc, Opt_orlov,
  684. Opt_user_xattr, Opt_nouser_xattr, Opt_acl, Opt_noacl,
  685. Opt_reservation, Opt_noreservation, Opt_noload, Opt_nobh, Opt_bh,
  686. Opt_commit, Opt_journal_update, Opt_journal_inum, Opt_journal_dev,
  687. Opt_abort, Opt_data_journal, Opt_data_ordered, Opt_data_writeback,
  688. Opt_data_err_abort, Opt_data_err_ignore,
  689. Opt_usrjquota, Opt_grpjquota, Opt_offusrjquota, Opt_offgrpjquota,
  690. Opt_jqfmt_vfsold, Opt_jqfmt_vfsv0, Opt_jqfmt_vfsv1, Opt_quota,
  691. Opt_noquota, Opt_ignore, Opt_barrier, Opt_nobarrier, Opt_err,
  692. Opt_resize, Opt_usrquota, Opt_grpquota
  693. };
  694. static const match_table_t tokens = {
  695. {Opt_bsd_df, "bsddf"},
  696. {Opt_minix_df, "minixdf"},
  697. {Opt_grpid, "grpid"},
  698. {Opt_grpid, "bsdgroups"},
  699. {Opt_nogrpid, "nogrpid"},
  700. {Opt_nogrpid, "sysvgroups"},
  701. {Opt_resgid, "resgid=%u"},
  702. {Opt_resuid, "resuid=%u"},
  703. {Opt_sb, "sb=%u"},
  704. {Opt_err_cont, "errors=continue"},
  705. {Opt_err_panic, "errors=panic"},
  706. {Opt_err_ro, "errors=remount-ro"},
  707. {Opt_nouid32, "nouid32"},
  708. {Opt_nocheck, "nocheck"},
  709. {Opt_nocheck, "check=none"},
  710. {Opt_debug, "debug"},
  711. {Opt_oldalloc, "oldalloc"},
  712. {Opt_orlov, "orlov"},
  713. {Opt_user_xattr, "user_xattr"},
  714. {Opt_nouser_xattr, "nouser_xattr"},
  715. {Opt_acl, "acl"},
  716. {Opt_noacl, "noacl"},
  717. {Opt_reservation, "reservation"},
  718. {Opt_noreservation, "noreservation"},
  719. {Opt_noload, "noload"},
  720. {Opt_noload, "norecovery"},
  721. {Opt_nobh, "nobh"},
  722. {Opt_bh, "bh"},
  723. {Opt_commit, "commit=%u"},
  724. {Opt_journal_update, "journal=update"},
  725. {Opt_journal_inum, "journal=%u"},
  726. {Opt_journal_dev, "journal_dev=%u"},
  727. {Opt_abort, "abort"},
  728. {Opt_data_journal, "data=journal"},
  729. {Opt_data_ordered, "data=ordered"},
  730. {Opt_data_writeback, "data=writeback"},
  731. {Opt_data_err_abort, "data_err=abort"},
  732. {Opt_data_err_ignore, "data_err=ignore"},
  733. {Opt_offusrjquota, "usrjquota="},
  734. {Opt_usrjquota, "usrjquota=%s"},
  735. {Opt_offgrpjquota, "grpjquota="},
  736. {Opt_grpjquota, "grpjquota=%s"},
  737. {Opt_jqfmt_vfsold, "jqfmt=vfsold"},
  738. {Opt_jqfmt_vfsv0, "jqfmt=vfsv0"},
  739. {Opt_jqfmt_vfsv1, "jqfmt=vfsv1"},
  740. {Opt_grpquota, "grpquota"},
  741. {Opt_noquota, "noquota"},
  742. {Opt_quota, "quota"},
  743. {Opt_usrquota, "usrquota"},
  744. {Opt_barrier, "barrier=%u"},
  745. {Opt_barrier, "barrier"},
  746. {Opt_nobarrier, "nobarrier"},
  747. {Opt_resize, "resize"},
  748. {Opt_err, NULL},
  749. };
  750. static ext3_fsblk_t get_sb_block(void **data, struct super_block *sb)
  751. {
  752. ext3_fsblk_t sb_block;
  753. char *options = (char *) *data;
  754. if (!options || strncmp(options, "sb=", 3) != 0)
  755. return 1; /* Default location */
  756. options += 3;
  757. /*todo: use simple_strtoll with >32bit ext3 */
  758. sb_block = simple_strtoul(options, &options, 0);
  759. if (*options && *options != ',') {
  760. ext3_msg(sb, "error: invalid sb specification: %s",
  761. (char *) *data);
  762. return 1;
  763. }
  764. if (*options == ',')
  765. options++;
  766. *data = (void *) options;
  767. return sb_block;
  768. }
  769. #ifdef CONFIG_QUOTA
  770. static int set_qf_name(struct super_block *sb, int qtype, substring_t *args)
  771. {
  772. struct ext3_sb_info *sbi = EXT3_SB(sb);
  773. char *qname;
  774. if (sb_any_quota_loaded(sb) &&
  775. !sbi->s_qf_names[qtype]) {
  776. ext3_msg(sb, KERN_ERR,
  777. "Cannot change journaled "
  778. "quota options when quota turned on");
  779. return 0;
  780. }
  781. qname = match_strdup(args);
  782. if (!qname) {
  783. ext3_msg(sb, KERN_ERR,
  784. "Not enough memory for storing quotafile name");
  785. return 0;
  786. }
  787. if (sbi->s_qf_names[qtype] &&
  788. strcmp(sbi->s_qf_names[qtype], qname)) {
  789. ext3_msg(sb, KERN_ERR,
  790. "%s quota file already specified", QTYPE2NAME(qtype));
  791. kfree(qname);
  792. return 0;
  793. }
  794. sbi->s_qf_names[qtype] = qname;
  795. if (strchr(sbi->s_qf_names[qtype], '/')) {
  796. ext3_msg(sb, KERN_ERR,
  797. "quotafile must be on filesystem root");
  798. kfree(sbi->s_qf_names[qtype]);
  799. sbi->s_qf_names[qtype] = NULL;
  800. return 0;
  801. }
  802. set_opt(sbi->s_mount_opt, QUOTA);
  803. return 1;
  804. }
  805. static int clear_qf_name(struct super_block *sb, int qtype) {
  806. struct ext3_sb_info *sbi = EXT3_SB(sb);
  807. if (sb_any_quota_loaded(sb) &&
  808. sbi->s_qf_names[qtype]) {
  809. ext3_msg(sb, KERN_ERR, "Cannot change journaled quota options"
  810. " when quota turned on");
  811. return 0;
  812. }
  813. /*
  814. * The space will be released later when all options are confirmed
  815. * to be correct
  816. */
  817. sbi->s_qf_names[qtype] = NULL;
  818. return 1;
  819. }
  820. #endif
  821. static int parse_options (char *options, struct super_block *sb,
  822. unsigned int *inum, unsigned long *journal_devnum,
  823. ext3_fsblk_t *n_blocks_count, int is_remount)
  824. {
  825. struct ext3_sb_info *sbi = EXT3_SB(sb);
  826. char * p;
  827. substring_t args[MAX_OPT_ARGS];
  828. int data_opt = 0;
  829. int option;
  830. #ifdef CONFIG_QUOTA
  831. int qfmt;
  832. #endif
  833. if (!options)
  834. return 1;
  835. while ((p = strsep (&options, ",")) != NULL) {
  836. int token;
  837. if (!*p)
  838. continue;
  839. /*
  840. * Initialize args struct so we know whether arg was
  841. * found; some options take optional arguments.
  842. */
  843. args[0].to = args[0].from = 0;
  844. token = match_token(p, tokens, args);
  845. switch (token) {
  846. case Opt_bsd_df:
  847. clear_opt (sbi->s_mount_opt, MINIX_DF);
  848. break;
  849. case Opt_minix_df:
  850. set_opt (sbi->s_mount_opt, MINIX_DF);
  851. break;
  852. case Opt_grpid:
  853. set_opt (sbi->s_mount_opt, GRPID);
  854. break;
  855. case Opt_nogrpid:
  856. clear_opt (sbi->s_mount_opt, GRPID);
  857. break;
  858. case Opt_resuid:
  859. if (match_int(&args[0], &option))
  860. return 0;
  861. sbi->s_resuid = option;
  862. break;
  863. case Opt_resgid:
  864. if (match_int(&args[0], &option))
  865. return 0;
  866. sbi->s_resgid = option;
  867. break;
  868. case Opt_sb:
  869. /* handled by get_sb_block() instead of here */
  870. /* *sb_block = match_int(&args[0]); */
  871. break;
  872. case Opt_err_panic:
  873. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  874. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  875. set_opt (sbi->s_mount_opt, ERRORS_PANIC);
  876. break;
  877. case Opt_err_ro:
  878. clear_opt (sbi->s_mount_opt, ERRORS_CONT);
  879. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  880. set_opt (sbi->s_mount_opt, ERRORS_RO);
  881. break;
  882. case Opt_err_cont:
  883. clear_opt (sbi->s_mount_opt, ERRORS_RO);
  884. clear_opt (sbi->s_mount_opt, ERRORS_PANIC);
  885. set_opt (sbi->s_mount_opt, ERRORS_CONT);
  886. break;
  887. case Opt_nouid32:
  888. set_opt (sbi->s_mount_opt, NO_UID32);
  889. break;
  890. case Opt_nocheck:
  891. clear_opt (sbi->s_mount_opt, CHECK);
  892. break;
  893. case Opt_debug:
  894. set_opt (sbi->s_mount_opt, DEBUG);
  895. break;
  896. case Opt_oldalloc:
  897. set_opt (sbi->s_mount_opt, OLDALLOC);
  898. break;
  899. case Opt_orlov:
  900. clear_opt (sbi->s_mount_opt, OLDALLOC);
  901. break;
  902. #ifdef CONFIG_EXT3_FS_XATTR
  903. case Opt_user_xattr:
  904. set_opt (sbi->s_mount_opt, XATTR_USER);
  905. break;
  906. case Opt_nouser_xattr:
  907. clear_opt (sbi->s_mount_opt, XATTR_USER);
  908. break;
  909. #else
  910. case Opt_user_xattr:
  911. case Opt_nouser_xattr:
  912. ext3_msg(sb, KERN_INFO,
  913. "(no)user_xattr options not supported");
  914. break;
  915. #endif
  916. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  917. case Opt_acl:
  918. set_opt(sbi->s_mount_opt, POSIX_ACL);
  919. break;
  920. case Opt_noacl:
  921. clear_opt(sbi->s_mount_opt, POSIX_ACL);
  922. break;
  923. #else
  924. case Opt_acl:
  925. case Opt_noacl:
  926. ext3_msg(sb, KERN_INFO,
  927. "(no)acl options not supported");
  928. break;
  929. #endif
  930. case Opt_reservation:
  931. set_opt(sbi->s_mount_opt, RESERVATION);
  932. break;
  933. case Opt_noreservation:
  934. clear_opt(sbi->s_mount_opt, RESERVATION);
  935. break;
  936. case Opt_journal_update:
  937. /* @@@ FIXME */
  938. /* Eventually we will want to be able to create
  939. a journal file here. For now, only allow the
  940. user to specify an existing inode to be the
  941. journal file. */
  942. if (is_remount) {
  943. ext3_msg(sb, KERN_ERR, "error: cannot specify "
  944. "journal on remount");
  945. return 0;
  946. }
  947. set_opt (sbi->s_mount_opt, UPDATE_JOURNAL);
  948. break;
  949. case Opt_journal_inum:
  950. if (is_remount) {
  951. ext3_msg(sb, KERN_ERR, "error: cannot specify "
  952. "journal on remount");
  953. return 0;
  954. }
  955. if (match_int(&args[0], &option))
  956. return 0;
  957. *inum = option;
  958. break;
  959. case Opt_journal_dev:
  960. if (is_remount) {
  961. ext3_msg(sb, KERN_ERR, "error: cannot specify "
  962. "journal on remount");
  963. return 0;
  964. }
  965. if (match_int(&args[0], &option))
  966. return 0;
  967. *journal_devnum = option;
  968. break;
  969. case Opt_noload:
  970. set_opt (sbi->s_mount_opt, NOLOAD);
  971. break;
  972. case Opt_commit:
  973. if (match_int(&args[0], &option))
  974. return 0;
  975. if (option < 0)
  976. return 0;
  977. if (option == 0)
  978. option = JBD_DEFAULT_MAX_COMMIT_AGE;
  979. sbi->s_commit_interval = HZ * option;
  980. break;
  981. case Opt_data_journal:
  982. data_opt = EXT3_MOUNT_JOURNAL_DATA;
  983. goto datacheck;
  984. case Opt_data_ordered:
  985. data_opt = EXT3_MOUNT_ORDERED_DATA;
  986. goto datacheck;
  987. case Opt_data_writeback:
  988. data_opt = EXT3_MOUNT_WRITEBACK_DATA;
  989. datacheck:
  990. if (is_remount) {
  991. if (test_opt(sb, DATA_FLAGS) == data_opt)
  992. break;
  993. ext3_msg(sb, KERN_ERR,
  994. "error: cannot change "
  995. "data mode on remount. The filesystem "
  996. "is mounted in data=%s mode and you "
  997. "try to remount it in data=%s mode.",
  998. data_mode_string(test_opt(sb,
  999. DATA_FLAGS)),
  1000. data_mode_string(data_opt));
  1001. return 0;
  1002. } else {
  1003. clear_opt(sbi->s_mount_opt, DATA_FLAGS);
  1004. sbi->s_mount_opt |= data_opt;
  1005. }
  1006. break;
  1007. case Opt_data_err_abort:
  1008. set_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
  1009. break;
  1010. case Opt_data_err_ignore:
  1011. clear_opt(sbi->s_mount_opt, DATA_ERR_ABORT);
  1012. break;
  1013. #ifdef CONFIG_QUOTA
  1014. case Opt_usrjquota:
  1015. if (!set_qf_name(sb, USRQUOTA, &args[0]))
  1016. return 0;
  1017. break;
  1018. case Opt_grpjquota:
  1019. if (!set_qf_name(sb, GRPQUOTA, &args[0]))
  1020. return 0;
  1021. break;
  1022. case Opt_offusrjquota:
  1023. if (!clear_qf_name(sb, USRQUOTA))
  1024. return 0;
  1025. break;
  1026. case Opt_offgrpjquota:
  1027. if (!clear_qf_name(sb, GRPQUOTA))
  1028. return 0;
  1029. break;
  1030. case Opt_jqfmt_vfsold:
  1031. qfmt = QFMT_VFS_OLD;
  1032. goto set_qf_format;
  1033. case Opt_jqfmt_vfsv0:
  1034. qfmt = QFMT_VFS_V0;
  1035. goto set_qf_format;
  1036. case Opt_jqfmt_vfsv1:
  1037. qfmt = QFMT_VFS_V1;
  1038. set_qf_format:
  1039. if (sb_any_quota_loaded(sb) &&
  1040. sbi->s_jquota_fmt != qfmt) {
  1041. ext3_msg(sb, KERN_ERR, "error: cannot change "
  1042. "journaled quota options when "
  1043. "quota turned on.");
  1044. return 0;
  1045. }
  1046. sbi->s_jquota_fmt = qfmt;
  1047. break;
  1048. case Opt_quota:
  1049. case Opt_usrquota:
  1050. set_opt(sbi->s_mount_opt, QUOTA);
  1051. set_opt(sbi->s_mount_opt, USRQUOTA);
  1052. break;
  1053. case Opt_grpquota:
  1054. set_opt(sbi->s_mount_opt, QUOTA);
  1055. set_opt(sbi->s_mount_opt, GRPQUOTA);
  1056. break;
  1057. case Opt_noquota:
  1058. if (sb_any_quota_loaded(sb)) {
  1059. ext3_msg(sb, KERN_ERR, "error: cannot change "
  1060. "quota options when quota turned on.");
  1061. return 0;
  1062. }
  1063. clear_opt(sbi->s_mount_opt, QUOTA);
  1064. clear_opt(sbi->s_mount_opt, USRQUOTA);
  1065. clear_opt(sbi->s_mount_opt, GRPQUOTA);
  1066. break;
  1067. #else
  1068. case Opt_quota:
  1069. case Opt_usrquota:
  1070. case Opt_grpquota:
  1071. ext3_msg(sb, KERN_ERR,
  1072. "error: quota options not supported.");
  1073. break;
  1074. case Opt_usrjquota:
  1075. case Opt_grpjquota:
  1076. case Opt_offusrjquota:
  1077. case Opt_offgrpjquota:
  1078. case Opt_jqfmt_vfsold:
  1079. case Opt_jqfmt_vfsv0:
  1080. case Opt_jqfmt_vfsv1:
  1081. ext3_msg(sb, KERN_ERR,
  1082. "error: journaled quota options not "
  1083. "supported.");
  1084. break;
  1085. case Opt_noquota:
  1086. break;
  1087. #endif
  1088. case Opt_abort:
  1089. set_opt(sbi->s_mount_opt, ABORT);
  1090. break;
  1091. case Opt_nobarrier:
  1092. clear_opt(sbi->s_mount_opt, BARRIER);
  1093. break;
  1094. case Opt_barrier:
  1095. if (args[0].from) {
  1096. if (match_int(&args[0], &option))
  1097. return 0;
  1098. } else
  1099. option = 1; /* No argument, default to 1 */
  1100. if (option)
  1101. set_opt(sbi->s_mount_opt, BARRIER);
  1102. else
  1103. clear_opt(sbi->s_mount_opt, BARRIER);
  1104. break;
  1105. case Opt_ignore:
  1106. break;
  1107. case Opt_resize:
  1108. if (!is_remount) {
  1109. ext3_msg(sb, KERN_ERR,
  1110. "error: resize option only available "
  1111. "for remount");
  1112. return 0;
  1113. }
  1114. if (match_int(&args[0], &option) != 0)
  1115. return 0;
  1116. *n_blocks_count = option;
  1117. break;
  1118. case Opt_nobh:
  1119. ext3_msg(sb, KERN_WARNING,
  1120. "warning: ignoring deprecated nobh option");
  1121. break;
  1122. case Opt_bh:
  1123. ext3_msg(sb, KERN_WARNING,
  1124. "warning: ignoring deprecated bh option");
  1125. break;
  1126. default:
  1127. ext3_msg(sb, KERN_ERR,
  1128. "error: unrecognized mount option \"%s\" "
  1129. "or missing value", p);
  1130. return 0;
  1131. }
  1132. }
  1133. #ifdef CONFIG_QUOTA
  1134. if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
  1135. if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
  1136. clear_opt(sbi->s_mount_opt, USRQUOTA);
  1137. if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
  1138. clear_opt(sbi->s_mount_opt, GRPQUOTA);
  1139. if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
  1140. ext3_msg(sb, KERN_ERR, "error: old and new quota "
  1141. "format mixing.");
  1142. return 0;
  1143. }
  1144. if (!sbi->s_jquota_fmt) {
  1145. ext3_msg(sb, KERN_ERR, "error: journaled quota format "
  1146. "not specified.");
  1147. return 0;
  1148. }
  1149. } else {
  1150. if (sbi->s_jquota_fmt) {
  1151. ext3_msg(sb, KERN_ERR, "error: journaled quota format "
  1152. "specified with no journaling "
  1153. "enabled.");
  1154. return 0;
  1155. }
  1156. }
  1157. #endif
  1158. return 1;
  1159. }
  1160. static int ext3_setup_super(struct super_block *sb, struct ext3_super_block *es,
  1161. int read_only)
  1162. {
  1163. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1164. int res = 0;
  1165. if (le32_to_cpu(es->s_rev_level) > EXT3_MAX_SUPP_REV) {
  1166. ext3_msg(sb, KERN_ERR,
  1167. "error: revision level too high, "
  1168. "forcing read-only mode");
  1169. res = MS_RDONLY;
  1170. }
  1171. if (read_only)
  1172. return res;
  1173. if (!(sbi->s_mount_state & EXT3_VALID_FS))
  1174. ext3_msg(sb, KERN_WARNING,
  1175. "warning: mounting unchecked fs, "
  1176. "running e2fsck is recommended");
  1177. else if ((sbi->s_mount_state & EXT3_ERROR_FS))
  1178. ext3_msg(sb, KERN_WARNING,
  1179. "warning: mounting fs with errors, "
  1180. "running e2fsck is recommended");
  1181. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
  1182. le16_to_cpu(es->s_mnt_count) >=
  1183. le16_to_cpu(es->s_max_mnt_count))
  1184. ext3_msg(sb, KERN_WARNING,
  1185. "warning: maximal mount count reached, "
  1186. "running e2fsck is recommended");
  1187. else if (le32_to_cpu(es->s_checkinterval) &&
  1188. (le32_to_cpu(es->s_lastcheck) +
  1189. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  1190. ext3_msg(sb, KERN_WARNING,
  1191. "warning: checktime reached, "
  1192. "running e2fsck is recommended");
  1193. #if 0
  1194. /* @@@ We _will_ want to clear the valid bit if we find
  1195. inconsistencies, to force a fsck at reboot. But for
  1196. a plain journaled filesystem we can keep it set as
  1197. valid forever! :) */
  1198. es->s_state &= cpu_to_le16(~EXT3_VALID_FS);
  1199. #endif
  1200. if (!le16_to_cpu(es->s_max_mnt_count))
  1201. es->s_max_mnt_count = cpu_to_le16(EXT3_DFL_MAX_MNT_COUNT);
  1202. le16_add_cpu(&es->s_mnt_count, 1);
  1203. es->s_mtime = cpu_to_le32(get_seconds());
  1204. ext3_update_dynamic_rev(sb);
  1205. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  1206. ext3_commit_super(sb, es, 1);
  1207. if (test_opt(sb, DEBUG))
  1208. ext3_msg(sb, KERN_INFO, "[bs=%lu, gc=%lu, "
  1209. "bpg=%lu, ipg=%lu, mo=%04lx]",
  1210. sb->s_blocksize,
  1211. sbi->s_groups_count,
  1212. EXT3_BLOCKS_PER_GROUP(sb),
  1213. EXT3_INODES_PER_GROUP(sb),
  1214. sbi->s_mount_opt);
  1215. if (EXT3_SB(sb)->s_journal->j_inode == NULL) {
  1216. char b[BDEVNAME_SIZE];
  1217. ext3_msg(sb, KERN_INFO, "using external journal on %s",
  1218. bdevname(EXT3_SB(sb)->s_journal->j_dev, b));
  1219. } else {
  1220. ext3_msg(sb, KERN_INFO, "using internal journal");
  1221. }
  1222. return res;
  1223. }
  1224. /* Called at mount-time, super-block is locked */
  1225. static int ext3_check_descriptors(struct super_block *sb)
  1226. {
  1227. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1228. int i;
  1229. ext3_debug ("Checking group descriptors");
  1230. for (i = 0; i < sbi->s_groups_count; i++) {
  1231. struct ext3_group_desc *gdp = ext3_get_group_desc(sb, i, NULL);
  1232. ext3_fsblk_t first_block = ext3_group_first_block_no(sb, i);
  1233. ext3_fsblk_t last_block;
  1234. if (i == sbi->s_groups_count - 1)
  1235. last_block = le32_to_cpu(sbi->s_es->s_blocks_count) - 1;
  1236. else
  1237. last_block = first_block +
  1238. (EXT3_BLOCKS_PER_GROUP(sb) - 1);
  1239. if (le32_to_cpu(gdp->bg_block_bitmap) < first_block ||
  1240. le32_to_cpu(gdp->bg_block_bitmap) > last_block)
  1241. {
  1242. ext3_error (sb, "ext3_check_descriptors",
  1243. "Block bitmap for group %d"
  1244. " not in group (block %lu)!",
  1245. i, (unsigned long)
  1246. le32_to_cpu(gdp->bg_block_bitmap));
  1247. return 0;
  1248. }
  1249. if (le32_to_cpu(gdp->bg_inode_bitmap) < first_block ||
  1250. le32_to_cpu(gdp->bg_inode_bitmap) > last_block)
  1251. {
  1252. ext3_error (sb, "ext3_check_descriptors",
  1253. "Inode bitmap for group %d"
  1254. " not in group (block %lu)!",
  1255. i, (unsigned long)
  1256. le32_to_cpu(gdp->bg_inode_bitmap));
  1257. return 0;
  1258. }
  1259. if (le32_to_cpu(gdp->bg_inode_table) < first_block ||
  1260. le32_to_cpu(gdp->bg_inode_table) + sbi->s_itb_per_group - 1 >
  1261. last_block)
  1262. {
  1263. ext3_error (sb, "ext3_check_descriptors",
  1264. "Inode table for group %d"
  1265. " not in group (block %lu)!",
  1266. i, (unsigned long)
  1267. le32_to_cpu(gdp->bg_inode_table));
  1268. return 0;
  1269. }
  1270. }
  1271. sbi->s_es->s_free_blocks_count=cpu_to_le32(ext3_count_free_blocks(sb));
  1272. sbi->s_es->s_free_inodes_count=cpu_to_le32(ext3_count_free_inodes(sb));
  1273. return 1;
  1274. }
  1275. /* ext3_orphan_cleanup() walks a singly-linked list of inodes (starting at
  1276. * the superblock) which were deleted from all directories, but held open by
  1277. * a process at the time of a crash. We walk the list and try to delete these
  1278. * inodes at recovery time (only with a read-write filesystem).
  1279. *
  1280. * In order to keep the orphan inode chain consistent during traversal (in
  1281. * case of crash during recovery), we link each inode into the superblock
  1282. * orphan list_head and handle it the same way as an inode deletion during
  1283. * normal operation (which journals the operations for us).
  1284. *
  1285. * We only do an iget() and an iput() on each inode, which is very safe if we
  1286. * accidentally point at an in-use or already deleted inode. The worst that
  1287. * can happen in this case is that we get a "bit already cleared" message from
  1288. * ext3_free_inode(). The only reason we would point at a wrong inode is if
  1289. * e2fsck was run on this filesystem, and it must have already done the orphan
  1290. * inode cleanup for us, so we can safely abort without any further action.
  1291. */
  1292. static void ext3_orphan_cleanup (struct super_block * sb,
  1293. struct ext3_super_block * es)
  1294. {
  1295. unsigned int s_flags = sb->s_flags;
  1296. int nr_orphans = 0, nr_truncates = 0;
  1297. #ifdef CONFIG_QUOTA
  1298. int i;
  1299. #endif
  1300. if (!es->s_last_orphan) {
  1301. jbd_debug(4, "no orphan inodes to clean up\n");
  1302. return;
  1303. }
  1304. if (bdev_read_only(sb->s_bdev)) {
  1305. ext3_msg(sb, KERN_ERR, "error: write access "
  1306. "unavailable, skipping orphan cleanup.");
  1307. return;
  1308. }
  1309. if (EXT3_SB(sb)->s_mount_state & EXT3_ERROR_FS) {
  1310. if (es->s_last_orphan)
  1311. jbd_debug(1, "Errors on filesystem, "
  1312. "clearing orphan list.\n");
  1313. es->s_last_orphan = 0;
  1314. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  1315. return;
  1316. }
  1317. if (s_flags & MS_RDONLY) {
  1318. ext3_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
  1319. sb->s_flags &= ~MS_RDONLY;
  1320. }
  1321. #ifdef CONFIG_QUOTA
  1322. /* Needed for iput() to work correctly and not trash data */
  1323. sb->s_flags |= MS_ACTIVE;
  1324. /* Turn on quotas so that they are updated correctly */
  1325. for (i = 0; i < MAXQUOTAS; i++) {
  1326. if (EXT3_SB(sb)->s_qf_names[i]) {
  1327. int ret = ext3_quota_on_mount(sb, i);
  1328. if (ret < 0)
  1329. ext3_msg(sb, KERN_ERR,
  1330. "error: cannot turn on journaled "
  1331. "quota: %d", ret);
  1332. }
  1333. }
  1334. #endif
  1335. while (es->s_last_orphan) {
  1336. struct inode *inode;
  1337. inode = ext3_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
  1338. if (IS_ERR(inode)) {
  1339. es->s_last_orphan = 0;
  1340. break;
  1341. }
  1342. list_add(&EXT3_I(inode)->i_orphan, &EXT3_SB(sb)->s_orphan);
  1343. dquot_initialize(inode);
  1344. if (inode->i_nlink) {
  1345. printk(KERN_DEBUG
  1346. "%s: truncating inode %lu to %Ld bytes\n",
  1347. __func__, inode->i_ino, inode->i_size);
  1348. jbd_debug(2, "truncating inode %lu to %Ld bytes\n",
  1349. inode->i_ino, inode->i_size);
  1350. ext3_truncate(inode);
  1351. nr_truncates++;
  1352. } else {
  1353. printk(KERN_DEBUG
  1354. "%s: deleting unreferenced inode %lu\n",
  1355. __func__, inode->i_ino);
  1356. jbd_debug(2, "deleting unreferenced inode %lu\n",
  1357. inode->i_ino);
  1358. nr_orphans++;
  1359. }
  1360. iput(inode); /* The delete magic happens here! */
  1361. }
  1362. #define PLURAL(x) (x), ((x)==1) ? "" : "s"
  1363. if (nr_orphans)
  1364. ext3_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
  1365. PLURAL(nr_orphans));
  1366. if (nr_truncates)
  1367. ext3_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
  1368. PLURAL(nr_truncates));
  1369. #ifdef CONFIG_QUOTA
  1370. /* Turn quotas off */
  1371. for (i = 0; i < MAXQUOTAS; i++) {
  1372. if (sb_dqopt(sb)->files[i])
  1373. dquot_quota_off(sb, i);
  1374. }
  1375. #endif
  1376. sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  1377. }
  1378. /*
  1379. * Maximal file size. There is a direct, and {,double-,triple-}indirect
  1380. * block limit, and also a limit of (2^32 - 1) 512-byte sectors in i_blocks.
  1381. * We need to be 1 filesystem block less than the 2^32 sector limit.
  1382. */
  1383. static loff_t ext3_max_size(int bits)
  1384. {
  1385. loff_t res = EXT3_NDIR_BLOCKS;
  1386. int meta_blocks;
  1387. loff_t upper_limit;
  1388. /* This is calculated to be the largest file size for a
  1389. * dense, file such that the total number of
  1390. * sectors in the file, including data and all indirect blocks,
  1391. * does not exceed 2^32 -1
  1392. * __u32 i_blocks representing the total number of
  1393. * 512 bytes blocks of the file
  1394. */
  1395. upper_limit = (1LL << 32) - 1;
  1396. /* total blocks in file system block size */
  1397. upper_limit >>= (bits - 9);
  1398. /* indirect blocks */
  1399. meta_blocks = 1;
  1400. /* double indirect blocks */
  1401. meta_blocks += 1 + (1LL << (bits-2));
  1402. /* tripple indirect blocks */
  1403. meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
  1404. upper_limit -= meta_blocks;
  1405. upper_limit <<= bits;
  1406. res += 1LL << (bits-2);
  1407. res += 1LL << (2*(bits-2));
  1408. res += 1LL << (3*(bits-2));
  1409. res <<= bits;
  1410. if (res > upper_limit)
  1411. res = upper_limit;
  1412. if (res > MAX_LFS_FILESIZE)
  1413. res = MAX_LFS_FILESIZE;
  1414. return res;
  1415. }
  1416. static ext3_fsblk_t descriptor_loc(struct super_block *sb,
  1417. ext3_fsblk_t logic_sb_block,
  1418. int nr)
  1419. {
  1420. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1421. unsigned long bg, first_meta_bg;
  1422. int has_super = 0;
  1423. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  1424. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_META_BG) ||
  1425. nr < first_meta_bg)
  1426. return (logic_sb_block + nr + 1);
  1427. bg = sbi->s_desc_per_block * nr;
  1428. if (ext3_bg_has_super(sb, bg))
  1429. has_super = 1;
  1430. return (has_super + ext3_group_first_block_no(sb, bg));
  1431. }
  1432. static int ext3_fill_super (struct super_block *sb, void *data, int silent)
  1433. {
  1434. struct buffer_head * bh;
  1435. struct ext3_super_block *es = NULL;
  1436. struct ext3_sb_info *sbi;
  1437. ext3_fsblk_t block;
  1438. ext3_fsblk_t sb_block = get_sb_block(&data, sb);
  1439. ext3_fsblk_t logic_sb_block;
  1440. unsigned long offset = 0;
  1441. unsigned int journal_inum = 0;
  1442. unsigned long journal_devnum = 0;
  1443. unsigned long def_mount_opts;
  1444. struct inode *root;
  1445. int blocksize;
  1446. int hblock;
  1447. int db_count;
  1448. int i;
  1449. int needs_recovery;
  1450. int ret = -EINVAL;
  1451. __le32 features;
  1452. int err;
  1453. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  1454. if (!sbi)
  1455. return -ENOMEM;
  1456. sbi->s_blockgroup_lock =
  1457. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  1458. if (!sbi->s_blockgroup_lock) {
  1459. kfree(sbi);
  1460. return -ENOMEM;
  1461. }
  1462. sb->s_fs_info = sbi;
  1463. sbi->s_mount_opt = 0;
  1464. sbi->s_resuid = EXT3_DEF_RESUID;
  1465. sbi->s_resgid = EXT3_DEF_RESGID;
  1466. sbi->s_sb_block = sb_block;
  1467. blocksize = sb_min_blocksize(sb, EXT3_MIN_BLOCK_SIZE);
  1468. if (!blocksize) {
  1469. ext3_msg(sb, KERN_ERR, "error: unable to set blocksize");
  1470. goto out_fail;
  1471. }
  1472. /*
  1473. * The ext3 superblock will not be buffer aligned for other than 1kB
  1474. * block sizes. We need to calculate the offset from buffer start.
  1475. */
  1476. if (blocksize != EXT3_MIN_BLOCK_SIZE) {
  1477. logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
  1478. offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
  1479. } else {
  1480. logic_sb_block = sb_block;
  1481. }
  1482. if (!(bh = sb_bread(sb, logic_sb_block))) {
  1483. ext3_msg(sb, KERN_ERR, "error: unable to read superblock");
  1484. goto out_fail;
  1485. }
  1486. /*
  1487. * Note: s_es must be initialized as soon as possible because
  1488. * some ext3 macro-instructions depend on its value
  1489. */
  1490. es = (struct ext3_super_block *) (bh->b_data + offset);
  1491. sbi->s_es = es;
  1492. sb->s_magic = le16_to_cpu(es->s_magic);
  1493. if (sb->s_magic != EXT3_SUPER_MAGIC)
  1494. goto cantfind_ext3;
  1495. /* Set defaults before we parse the mount options */
  1496. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  1497. if (def_mount_opts & EXT3_DEFM_DEBUG)
  1498. set_opt(sbi->s_mount_opt, DEBUG);
  1499. if (def_mount_opts & EXT3_DEFM_BSDGROUPS)
  1500. set_opt(sbi->s_mount_opt, GRPID);
  1501. if (def_mount_opts & EXT3_DEFM_UID16)
  1502. set_opt(sbi->s_mount_opt, NO_UID32);
  1503. #ifdef CONFIG_EXT3_FS_XATTR
  1504. if (def_mount_opts & EXT3_DEFM_XATTR_USER)
  1505. set_opt(sbi->s_mount_opt, XATTR_USER);
  1506. #endif
  1507. #ifdef CONFIG_EXT3_FS_POSIX_ACL
  1508. if (def_mount_opts & EXT3_DEFM_ACL)
  1509. set_opt(sbi->s_mount_opt, POSIX_ACL);
  1510. #endif
  1511. if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_DATA)
  1512. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  1513. else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_ORDERED)
  1514. set_opt(sbi->s_mount_opt, ORDERED_DATA);
  1515. else if ((def_mount_opts & EXT3_DEFM_JMODE) == EXT3_DEFM_JMODE_WBACK)
  1516. set_opt(sbi->s_mount_opt, WRITEBACK_DATA);
  1517. if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_PANIC)
  1518. set_opt(sbi->s_mount_opt, ERRORS_PANIC);
  1519. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT3_ERRORS_CONTINUE)
  1520. set_opt(sbi->s_mount_opt, ERRORS_CONT);
  1521. else
  1522. set_opt(sbi->s_mount_opt, ERRORS_RO);
  1523. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  1524. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  1525. set_opt(sbi->s_mount_opt, RESERVATION);
  1526. if (!parse_options ((char *) data, sb, &journal_inum, &journal_devnum,
  1527. NULL, 0))
  1528. goto failed_mount;
  1529. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  1530. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  1531. if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV &&
  1532. (EXT3_HAS_COMPAT_FEATURE(sb, ~0U) ||
  1533. EXT3_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  1534. EXT3_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  1535. ext3_msg(sb, KERN_WARNING,
  1536. "warning: feature flags set on rev 0 fs, "
  1537. "running e2fsck is recommended");
  1538. /*
  1539. * Check feature flags regardless of the revision level, since we
  1540. * previously didn't change the revision level when setting the flags,
  1541. * so there is a chance incompat flags are set on a rev 0 filesystem.
  1542. */
  1543. features = EXT3_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP);
  1544. if (features) {
  1545. ext3_msg(sb, KERN_ERR,
  1546. "error: couldn't mount because of unsupported "
  1547. "optional features (%x)", le32_to_cpu(features));
  1548. goto failed_mount;
  1549. }
  1550. features = EXT3_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP);
  1551. if (!(sb->s_flags & MS_RDONLY) && features) {
  1552. ext3_msg(sb, KERN_ERR,
  1553. "error: couldn't mount RDWR because of unsupported "
  1554. "optional features (%x)", le32_to_cpu(features));
  1555. goto failed_mount;
  1556. }
  1557. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  1558. if (blocksize < EXT3_MIN_BLOCK_SIZE ||
  1559. blocksize > EXT3_MAX_BLOCK_SIZE) {
  1560. ext3_msg(sb, KERN_ERR,
  1561. "error: couldn't mount because of unsupported "
  1562. "filesystem blocksize %d", blocksize);
  1563. goto failed_mount;
  1564. }
  1565. hblock = bdev_logical_block_size(sb->s_bdev);
  1566. if (sb->s_blocksize != blocksize) {
  1567. /*
  1568. * Make sure the blocksize for the filesystem is larger
  1569. * than the hardware sectorsize for the machine.
  1570. */
  1571. if (blocksize < hblock) {
  1572. ext3_msg(sb, KERN_ERR,
  1573. "error: fsblocksize %d too small for "
  1574. "hardware sectorsize %d", blocksize, hblock);
  1575. goto failed_mount;
  1576. }
  1577. brelse (bh);
  1578. if (!sb_set_blocksize(sb, blocksize)) {
  1579. ext3_msg(sb, KERN_ERR,
  1580. "error: bad blocksize %d", blocksize);
  1581. goto out_fail;
  1582. }
  1583. logic_sb_block = (sb_block * EXT3_MIN_BLOCK_SIZE) / blocksize;
  1584. offset = (sb_block * EXT3_MIN_BLOCK_SIZE) % blocksize;
  1585. bh = sb_bread(sb, logic_sb_block);
  1586. if (!bh) {
  1587. ext3_msg(sb, KERN_ERR,
  1588. "error: can't read superblock on 2nd try");
  1589. goto failed_mount;
  1590. }
  1591. es = (struct ext3_super_block *)(bh->b_data + offset);
  1592. sbi->s_es = es;
  1593. if (es->s_magic != cpu_to_le16(EXT3_SUPER_MAGIC)) {
  1594. ext3_msg(sb, KERN_ERR,
  1595. "error: magic mismatch");
  1596. goto failed_mount;
  1597. }
  1598. }
  1599. sb->s_maxbytes = ext3_max_size(sb->s_blocksize_bits);
  1600. if (le32_to_cpu(es->s_rev_level) == EXT3_GOOD_OLD_REV) {
  1601. sbi->s_inode_size = EXT3_GOOD_OLD_INODE_SIZE;
  1602. sbi->s_first_ino = EXT3_GOOD_OLD_FIRST_INO;
  1603. } else {
  1604. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  1605. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  1606. if ((sbi->s_inode_size < EXT3_GOOD_OLD_INODE_SIZE) ||
  1607. (!is_power_of_2(sbi->s_inode_size)) ||
  1608. (sbi->s_inode_size > blocksize)) {
  1609. ext3_msg(sb, KERN_ERR,
  1610. "error: unsupported inode size: %d",
  1611. sbi->s_inode_size);
  1612. goto failed_mount;
  1613. }
  1614. }
  1615. sbi->s_frag_size = EXT3_MIN_FRAG_SIZE <<
  1616. le32_to_cpu(es->s_log_frag_size);
  1617. if (blocksize != sbi->s_frag_size) {
  1618. ext3_msg(sb, KERN_ERR,
  1619. "error: fragsize %lu != blocksize %u (unsupported)",
  1620. sbi->s_frag_size, blocksize);
  1621. goto failed_mount;
  1622. }
  1623. sbi->s_frags_per_block = 1;
  1624. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  1625. sbi->s_frags_per_group = le32_to_cpu(es->s_frags_per_group);
  1626. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  1627. if (EXT3_INODE_SIZE(sb) == 0 || EXT3_INODES_PER_GROUP(sb) == 0)
  1628. goto cantfind_ext3;
  1629. sbi->s_inodes_per_block = blocksize / EXT3_INODE_SIZE(sb);
  1630. if (sbi->s_inodes_per_block == 0)
  1631. goto cantfind_ext3;
  1632. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  1633. sbi->s_inodes_per_block;
  1634. sbi->s_desc_per_block = blocksize / sizeof(struct ext3_group_desc);
  1635. sbi->s_sbh = bh;
  1636. sbi->s_mount_state = le16_to_cpu(es->s_state);
  1637. sbi->s_addr_per_block_bits = ilog2(EXT3_ADDR_PER_BLOCK(sb));
  1638. sbi->s_desc_per_block_bits = ilog2(EXT3_DESC_PER_BLOCK(sb));
  1639. for (i=0; i < 4; i++)
  1640. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  1641. sbi->s_def_hash_version = es->s_def_hash_version;
  1642. i = le32_to_cpu(es->s_flags);
  1643. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  1644. sbi->s_hash_unsigned = 3;
  1645. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  1646. #ifdef __CHAR_UNSIGNED__
  1647. es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  1648. sbi->s_hash_unsigned = 3;
  1649. #else
  1650. es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  1651. #endif
  1652. }
  1653. if (sbi->s_blocks_per_group > blocksize * 8) {
  1654. ext3_msg(sb, KERN_ERR,
  1655. "#blocks per group too big: %lu",
  1656. sbi->s_blocks_per_group);
  1657. goto failed_mount;
  1658. }
  1659. if (sbi->s_frags_per_group > blocksize * 8) {
  1660. ext3_msg(sb, KERN_ERR,
  1661. "error: #fragments per group too big: %lu",
  1662. sbi->s_frags_per_group);
  1663. goto failed_mount;
  1664. }
  1665. if (sbi->s_inodes_per_group > blocksize * 8) {
  1666. ext3_msg(sb, KERN_ERR,
  1667. "error: #inodes per group too big: %lu",
  1668. sbi->s_inodes_per_group);
  1669. goto failed_mount;
  1670. }
  1671. if (generic_check_addressable(sb->s_blocksize_bits,
  1672. le32_to_cpu(es->s_blocks_count))) {
  1673. ext3_msg(sb, KERN_ERR,
  1674. "error: filesystem is too large to mount safely");
  1675. if (sizeof(sector_t) < 8)
  1676. ext3_msg(sb, KERN_ERR,
  1677. "error: CONFIG_LBDAF not enabled");
  1678. goto failed_mount;
  1679. }
  1680. if (EXT3_BLOCKS_PER_GROUP(sb) == 0)
  1681. goto cantfind_ext3;
  1682. sbi->s_groups_count = ((le32_to_cpu(es->s_blocks_count) -
  1683. le32_to_cpu(es->s_first_data_block) - 1)
  1684. / EXT3_BLOCKS_PER_GROUP(sb)) + 1;
  1685. db_count = DIV_ROUND_UP(sbi->s_groups_count, EXT3_DESC_PER_BLOCK(sb));
  1686. sbi->s_group_desc = kmalloc(db_count * sizeof (struct buffer_head *),
  1687. GFP_KERNEL);
  1688. if (sbi->s_group_desc == NULL) {
  1689. ext3_msg(sb, KERN_ERR,
  1690. "error: not enough memory");
  1691. ret = -ENOMEM;
  1692. goto failed_mount;
  1693. }
  1694. bgl_lock_init(sbi->s_blockgroup_lock);
  1695. for (i = 0; i < db_count; i++) {
  1696. block = descriptor_loc(sb, logic_sb_block, i);
  1697. sbi->s_group_desc[i] = sb_bread(sb, block);
  1698. if (!sbi->s_group_desc[i]) {
  1699. ext3_msg(sb, KERN_ERR,
  1700. "error: can't read group descriptor %d", i);
  1701. db_count = i;
  1702. goto failed_mount2;
  1703. }
  1704. }
  1705. if (!ext3_check_descriptors (sb)) {
  1706. ext3_msg(sb, KERN_ERR,
  1707. "error: group descriptors corrupted");
  1708. goto failed_mount2;
  1709. }
  1710. sbi->s_gdb_count = db_count;
  1711. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  1712. spin_lock_init(&sbi->s_next_gen_lock);
  1713. /* per fileystem reservation list head & lock */
  1714. spin_lock_init(&sbi->s_rsv_window_lock);
  1715. sbi->s_rsv_window_root = RB_ROOT;
  1716. /* Add a single, static dummy reservation to the start of the
  1717. * reservation window list --- it gives us a placeholder for
  1718. * append-at-start-of-list which makes the allocation logic
  1719. * _much_ simpler. */
  1720. sbi->s_rsv_window_head.rsv_start = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  1721. sbi->s_rsv_window_head.rsv_end = EXT3_RESERVE_WINDOW_NOT_ALLOCATED;
  1722. sbi->s_rsv_window_head.rsv_alloc_hit = 0;
  1723. sbi->s_rsv_window_head.rsv_goal_size = 0;
  1724. ext3_rsv_window_add(sb, &sbi->s_rsv_window_head);
  1725. /*
  1726. * set up enough so that it can read an inode
  1727. */
  1728. sb->s_op = &ext3_sops;
  1729. sb->s_export_op = &ext3_export_ops;
  1730. sb->s_xattr = ext3_xattr_handlers;
  1731. #ifdef CONFIG_QUOTA
  1732. sb->s_qcop = &ext3_qctl_operations;
  1733. sb->dq_op = &ext3_quota_operations;
  1734. #endif
  1735. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  1736. mutex_init(&sbi->s_orphan_lock);
  1737. mutex_init(&sbi->s_resize_lock);
  1738. sb->s_root = NULL;
  1739. needs_recovery = (es->s_last_orphan != 0 ||
  1740. EXT3_HAS_INCOMPAT_FEATURE(sb,
  1741. EXT3_FEATURE_INCOMPAT_RECOVER));
  1742. /*
  1743. * The first inode we look at is the journal inode. Don't try
  1744. * root first: it may be modified in the journal!
  1745. */
  1746. if (!test_opt(sb, NOLOAD) &&
  1747. EXT3_HAS_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL)) {
  1748. if (ext3_load_journal(sb, es, journal_devnum))
  1749. goto failed_mount2;
  1750. } else if (journal_inum) {
  1751. if (ext3_create_journal(sb, es, journal_inum))
  1752. goto failed_mount2;
  1753. } else {
  1754. if (!silent)
  1755. ext3_msg(sb, KERN_ERR,
  1756. "error: no journal found. "
  1757. "mounting ext3 over ext2?");
  1758. goto failed_mount2;
  1759. }
  1760. err = percpu_counter_init(&sbi->s_freeblocks_counter,
  1761. ext3_count_free_blocks(sb));
  1762. if (!err) {
  1763. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  1764. ext3_count_free_inodes(sb));
  1765. }
  1766. if (!err) {
  1767. err = percpu_counter_init(&sbi->s_dirs_counter,
  1768. ext3_count_dirs(sb));
  1769. }
  1770. if (err) {
  1771. ext3_msg(sb, KERN_ERR, "error: insufficient memory");
  1772. ret = err;
  1773. goto failed_mount3;
  1774. }
  1775. /* We have now updated the journal if required, so we can
  1776. * validate the data journaling mode. */
  1777. switch (test_opt(sb, DATA_FLAGS)) {
  1778. case 0:
  1779. /* No mode set, assume a default based on the journal
  1780. capabilities: ORDERED_DATA if the journal can
  1781. cope, else JOURNAL_DATA */
  1782. if (journal_check_available_features
  1783. (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE))
  1784. set_opt(sbi->s_mount_opt, DEFAULT_DATA_MODE);
  1785. else
  1786. set_opt(sbi->s_mount_opt, JOURNAL_DATA);
  1787. break;
  1788. case EXT3_MOUNT_ORDERED_DATA:
  1789. case EXT3_MOUNT_WRITEBACK_DATA:
  1790. if (!journal_check_available_features
  1791. (sbi->s_journal, 0, 0, JFS_FEATURE_INCOMPAT_REVOKE)) {
  1792. ext3_msg(sb, KERN_ERR,
  1793. "error: journal does not support "
  1794. "requested data journaling mode");
  1795. goto failed_mount3;
  1796. }
  1797. default:
  1798. break;
  1799. }
  1800. /*
  1801. * The journal_load will have done any necessary log recovery,
  1802. * so we can safely mount the rest of the filesystem now.
  1803. */
  1804. root = ext3_iget(sb, EXT3_ROOT_INO);
  1805. if (IS_ERR(root)) {
  1806. ext3_msg(sb, KERN_ERR, "error: get root inode failed");
  1807. ret = PTR_ERR(root);
  1808. goto failed_mount3;
  1809. }
  1810. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  1811. iput(root);
  1812. ext3_msg(sb, KERN_ERR, "error: corrupt root inode, run e2fsck");
  1813. goto failed_mount3;
  1814. }
  1815. sb->s_root = d_alloc_root(root);
  1816. if (!sb->s_root) {
  1817. ext3_msg(sb, KERN_ERR, "error: get root dentry failed");
  1818. iput(root);
  1819. ret = -ENOMEM;
  1820. goto failed_mount3;
  1821. }
  1822. ext3_setup_super (sb, es, sb->s_flags & MS_RDONLY);
  1823. EXT3_SB(sb)->s_mount_state |= EXT3_ORPHAN_FS;
  1824. ext3_orphan_cleanup(sb, es);
  1825. EXT3_SB(sb)->s_mount_state &= ~EXT3_ORPHAN_FS;
  1826. if (needs_recovery)
  1827. ext3_msg(sb, KERN_INFO, "recovery complete");
  1828. ext3_mark_recovery_complete(sb, es);
  1829. ext3_msg(sb, KERN_INFO, "mounted filesystem with %s data mode",
  1830. test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_JOURNAL_DATA ? "journal":
  1831. test_opt(sb,DATA_FLAGS) == EXT3_MOUNT_ORDERED_DATA ? "ordered":
  1832. "writeback");
  1833. return 0;
  1834. cantfind_ext3:
  1835. if (!silent)
  1836. ext3_msg(sb, KERN_INFO,
  1837. "error: can't find ext3 filesystem on dev %s.",
  1838. sb->s_id);
  1839. goto failed_mount;
  1840. failed_mount3:
  1841. percpu_counter_destroy(&sbi->s_freeblocks_counter);
  1842. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  1843. percpu_counter_destroy(&sbi->s_dirs_counter);
  1844. journal_destroy(sbi->s_journal);
  1845. failed_mount2:
  1846. for (i = 0; i < db_count; i++)
  1847. brelse(sbi->s_group_desc[i]);
  1848. kfree(sbi->s_group_desc);
  1849. failed_mount:
  1850. #ifdef CONFIG_QUOTA
  1851. for (i = 0; i < MAXQUOTAS; i++)
  1852. kfree(sbi->s_qf_names[i]);
  1853. #endif
  1854. ext3_blkdev_remove(sbi);
  1855. brelse(bh);
  1856. out_fail:
  1857. sb->s_fs_info = NULL;
  1858. kfree(sbi->s_blockgroup_lock);
  1859. kfree(sbi);
  1860. return ret;
  1861. }
  1862. /*
  1863. * Setup any per-fs journal parameters now. We'll do this both on
  1864. * initial mount, once the journal has been initialised but before we've
  1865. * done any recovery; and again on any subsequent remount.
  1866. */
  1867. static void ext3_init_journal_params(struct super_block *sb, journal_t *journal)
  1868. {
  1869. struct ext3_sb_info *sbi = EXT3_SB(sb);
  1870. if (sbi->s_commit_interval)
  1871. journal->j_commit_interval = sbi->s_commit_interval;
  1872. /* We could also set up an ext3-specific default for the commit
  1873. * interval here, but for now we'll just fall back to the jbd
  1874. * default. */
  1875. spin_lock(&journal->j_state_lock);
  1876. if (test_opt(sb, BARRIER))
  1877. journal->j_flags |= JFS_BARRIER;
  1878. else
  1879. journal->j_flags &= ~JFS_BARRIER;
  1880. if (test_opt(sb, DATA_ERR_ABORT))
  1881. journal->j_flags |= JFS_ABORT_ON_SYNCDATA_ERR;
  1882. else
  1883. journal->j_flags &= ~JFS_ABORT_ON_SYNCDATA_ERR;
  1884. spin_unlock(&journal->j_state_lock);
  1885. }
  1886. static journal_t *ext3_get_journal(struct super_block *sb,
  1887. unsigned int journal_inum)
  1888. {
  1889. struct inode *journal_inode;
  1890. journal_t *journal;
  1891. /* First, test for the existence of a valid inode on disk. Bad
  1892. * things happen if we iget() an unused inode, as the subsequent
  1893. * iput() will try to delete it. */
  1894. journal_inode = ext3_iget(sb, journal_inum);
  1895. if (IS_ERR(journal_inode)) {
  1896. ext3_msg(sb, KERN_ERR, "error: no journal found");
  1897. return NULL;
  1898. }
  1899. if (!journal_inode->i_nlink) {
  1900. make_bad_inode(journal_inode);
  1901. iput(journal_inode);
  1902. ext3_msg(sb, KERN_ERR, "error: journal inode is deleted");
  1903. return NULL;
  1904. }
  1905. jbd_debug(2, "Journal inode found at %p: %Ld bytes\n",
  1906. journal_inode, journal_inode->i_size);
  1907. if (!S_ISREG(journal_inode->i_mode)) {
  1908. ext3_msg(sb, KERN_ERR, "error: invalid journal inode");
  1909. iput(journal_inode);
  1910. return NULL;
  1911. }
  1912. journal = journal_init_inode(journal_inode);
  1913. if (!journal) {
  1914. ext3_msg(sb, KERN_ERR, "error: could not load journal inode");
  1915. iput(journal_inode);
  1916. return NULL;
  1917. }
  1918. journal->j_private = sb;
  1919. ext3_init_journal_params(sb, journal);
  1920. return journal;
  1921. }
  1922. static journal_t *ext3_get_dev_journal(struct super_block *sb,
  1923. dev_t j_dev)
  1924. {
  1925. struct buffer_head * bh;
  1926. journal_t *journal;
  1927. ext3_fsblk_t start;
  1928. ext3_fsblk_t len;
  1929. int hblock, blocksize;
  1930. ext3_fsblk_t sb_block;
  1931. unsigned long offset;
  1932. struct ext3_super_block * es;
  1933. struct block_device *bdev;
  1934. bdev = ext3_blkdev_get(j_dev, sb);
  1935. if (bdev == NULL)
  1936. return NULL;
  1937. if (bd_claim(bdev, sb)) {
  1938. ext3_msg(sb, KERN_ERR,
  1939. "error: failed to claim external journal device");
  1940. blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
  1941. return NULL;
  1942. }
  1943. blocksize = sb->s_blocksize;
  1944. hblock = bdev_logical_block_size(bdev);
  1945. if (blocksize < hblock) {
  1946. ext3_msg(sb, KERN_ERR,
  1947. "error: blocksize too small for journal device");
  1948. goto out_bdev;
  1949. }
  1950. sb_block = EXT3_MIN_BLOCK_SIZE / blocksize;
  1951. offset = EXT3_MIN_BLOCK_SIZE % blocksize;
  1952. set_blocksize(bdev, blocksize);
  1953. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  1954. ext3_msg(sb, KERN_ERR, "error: couldn't read superblock of "
  1955. "external journal");
  1956. goto out_bdev;
  1957. }
  1958. es = (struct ext3_super_block *) (bh->b_data + offset);
  1959. if ((le16_to_cpu(es->s_magic) != EXT3_SUPER_MAGIC) ||
  1960. !(le32_to_cpu(es->s_feature_incompat) &
  1961. EXT3_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  1962. ext3_msg(sb, KERN_ERR, "error: external journal has "
  1963. "bad superblock");
  1964. brelse(bh);
  1965. goto out_bdev;
  1966. }
  1967. if (memcmp(EXT3_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  1968. ext3_msg(sb, KERN_ERR, "error: journal UUID does not match");
  1969. brelse(bh);
  1970. goto out_bdev;
  1971. }
  1972. len = le32_to_cpu(es->s_blocks_count);
  1973. start = sb_block + 1;
  1974. brelse(bh); /* we're done with the superblock */
  1975. journal = journal_init_dev(bdev, sb->s_bdev,
  1976. start, len, blocksize);
  1977. if (!journal) {
  1978. ext3_msg(sb, KERN_ERR,
  1979. "error: failed to create device journal");
  1980. goto out_bdev;
  1981. }
  1982. journal->j_private = sb;
  1983. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  1984. wait_on_buffer(journal->j_sb_buffer);
  1985. if (!buffer_uptodate(journal->j_sb_buffer)) {
  1986. ext3_msg(sb, KERN_ERR, "I/O error on journal device");
  1987. goto out_journal;
  1988. }
  1989. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  1990. ext3_msg(sb, KERN_ERR,
  1991. "error: external journal has more than one "
  1992. "user (unsupported) - %d",
  1993. be32_to_cpu(journal->j_superblock->s_nr_users));
  1994. goto out_journal;
  1995. }
  1996. EXT3_SB(sb)->journal_bdev = bdev;
  1997. ext3_init_journal_params(sb, journal);
  1998. return journal;
  1999. out_journal:
  2000. journal_destroy(journal);
  2001. out_bdev:
  2002. ext3_blkdev_put(bdev);
  2003. return NULL;
  2004. }
  2005. static int ext3_load_journal(struct super_block *sb,
  2006. struct ext3_super_block *es,
  2007. unsigned long journal_devnum)
  2008. {
  2009. journal_t *journal;
  2010. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  2011. dev_t journal_dev;
  2012. int err = 0;
  2013. int really_read_only;
  2014. if (journal_devnum &&
  2015. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  2016. ext3_msg(sb, KERN_INFO, "external journal device major/minor "
  2017. "numbers have changed");
  2018. journal_dev = new_decode_dev(journal_devnum);
  2019. } else
  2020. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  2021. really_read_only = bdev_read_only(sb->s_bdev);
  2022. /*
  2023. * Are we loading a blank journal or performing recovery after a
  2024. * crash? For recovery, we need to check in advance whether we
  2025. * can get read-write access to the device.
  2026. */
  2027. if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER)) {
  2028. if (sb->s_flags & MS_RDONLY) {
  2029. ext3_msg(sb, KERN_INFO,
  2030. "recovery required on readonly filesystem");
  2031. if (really_read_only) {
  2032. ext3_msg(sb, KERN_ERR, "error: write access "
  2033. "unavailable, cannot proceed");
  2034. return -EROFS;
  2035. }
  2036. ext3_msg(sb, KERN_INFO,
  2037. "write access will be enabled during recovery");
  2038. }
  2039. }
  2040. if (journal_inum && journal_dev) {
  2041. ext3_msg(sb, KERN_ERR, "error: filesystem has both journal "
  2042. "and inode journals");
  2043. return -EINVAL;
  2044. }
  2045. if (journal_inum) {
  2046. if (!(journal = ext3_get_journal(sb, journal_inum)))
  2047. return -EINVAL;
  2048. } else {
  2049. if (!(journal = ext3_get_dev_journal(sb, journal_dev)))
  2050. return -EINVAL;
  2051. }
  2052. if (!(journal->j_flags & JFS_BARRIER))
  2053. printk(KERN_INFO "EXT3-fs: barriers not enabled\n");
  2054. if (!really_read_only && test_opt(sb, UPDATE_JOURNAL)) {
  2055. err = journal_update_format(journal);
  2056. if (err) {
  2057. ext3_msg(sb, KERN_ERR, "error updating journal");
  2058. journal_destroy(journal);
  2059. return err;
  2060. }
  2061. }
  2062. if (!EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER))
  2063. err = journal_wipe(journal, !really_read_only);
  2064. if (!err)
  2065. err = journal_load(journal);
  2066. if (err) {
  2067. ext3_msg(sb, KERN_ERR, "error loading journal");
  2068. journal_destroy(journal);
  2069. return err;
  2070. }
  2071. EXT3_SB(sb)->s_journal = journal;
  2072. ext3_clear_journal_err(sb, es);
  2073. if (journal_devnum &&
  2074. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  2075. es->s_journal_dev = cpu_to_le32(journal_devnum);
  2076. /* Make sure we flush the recovery flag to disk. */
  2077. ext3_commit_super(sb, es, 1);
  2078. }
  2079. return 0;
  2080. }
  2081. static int ext3_create_journal(struct super_block *sb,
  2082. struct ext3_super_block *es,
  2083. unsigned int journal_inum)
  2084. {
  2085. journal_t *journal;
  2086. int err;
  2087. if (sb->s_flags & MS_RDONLY) {
  2088. ext3_msg(sb, KERN_ERR,
  2089. "error: readonly filesystem when trying to "
  2090. "create journal");
  2091. return -EROFS;
  2092. }
  2093. journal = ext3_get_journal(sb, journal_inum);
  2094. if (!journal)
  2095. return -EINVAL;
  2096. ext3_msg(sb, KERN_INFO, "creating new journal on inode %u",
  2097. journal_inum);
  2098. err = journal_create(journal);
  2099. if (err) {
  2100. ext3_msg(sb, KERN_ERR, "error creating journal");
  2101. journal_destroy(journal);
  2102. return -EIO;
  2103. }
  2104. EXT3_SB(sb)->s_journal = journal;
  2105. ext3_update_dynamic_rev(sb);
  2106. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  2107. EXT3_SET_COMPAT_FEATURE(sb, EXT3_FEATURE_COMPAT_HAS_JOURNAL);
  2108. es->s_journal_inum = cpu_to_le32(journal_inum);
  2109. /* Make sure we flush the recovery flag to disk. */
  2110. ext3_commit_super(sb, es, 1);
  2111. return 0;
  2112. }
  2113. static int ext3_commit_super(struct super_block *sb,
  2114. struct ext3_super_block *es,
  2115. int sync)
  2116. {
  2117. struct buffer_head *sbh = EXT3_SB(sb)->s_sbh;
  2118. int error = 0;
  2119. if (!sbh)
  2120. return error;
  2121. if (buffer_write_io_error(sbh)) {
  2122. /*
  2123. * Oh, dear. A previous attempt to write the
  2124. * superblock failed. This could happen because the
  2125. * USB device was yanked out. Or it could happen to
  2126. * be a transient write error and maybe the block will
  2127. * be remapped. Nothing we can do but to retry the
  2128. * write and hope for the best.
  2129. */
  2130. ext3_msg(sb, KERN_ERR, "previous I/O error to "
  2131. "superblock detected");
  2132. clear_buffer_write_io_error(sbh);
  2133. set_buffer_uptodate(sbh);
  2134. }
  2135. /*
  2136. * If the file system is mounted read-only, don't update the
  2137. * superblock write time. This avoids updating the superblock
  2138. * write time when we are mounting the root file system
  2139. * read/only but we need to replay the journal; at that point,
  2140. * for people who are east of GMT and who make their clock
  2141. * tick in localtime for Windows bug-for-bug compatibility,
  2142. * the clock is set in the future, and this will cause e2fsck
  2143. * to complain and force a full file system check.
  2144. */
  2145. if (!(sb->s_flags & MS_RDONLY))
  2146. es->s_wtime = cpu_to_le32(get_seconds());
  2147. es->s_free_blocks_count = cpu_to_le32(ext3_count_free_blocks(sb));
  2148. es->s_free_inodes_count = cpu_to_le32(ext3_count_free_inodes(sb));
  2149. BUFFER_TRACE(sbh, "marking dirty");
  2150. mark_buffer_dirty(sbh);
  2151. if (sync) {
  2152. error = sync_dirty_buffer(sbh);
  2153. if (buffer_write_io_error(sbh)) {
  2154. ext3_msg(sb, KERN_ERR, "I/O error while writing "
  2155. "superblock");
  2156. clear_buffer_write_io_error(sbh);
  2157. set_buffer_uptodate(sbh);
  2158. }
  2159. }
  2160. return error;
  2161. }
  2162. /*
  2163. * Have we just finished recovery? If so, and if we are mounting (or
  2164. * remounting) the filesystem readonly, then we will end up with a
  2165. * consistent fs on disk. Record that fact.
  2166. */
  2167. static void ext3_mark_recovery_complete(struct super_block * sb,
  2168. struct ext3_super_block * es)
  2169. {
  2170. journal_t *journal = EXT3_SB(sb)->s_journal;
  2171. journal_lock_updates(journal);
  2172. if (journal_flush(journal) < 0)
  2173. goto out;
  2174. if (EXT3_HAS_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER) &&
  2175. sb->s_flags & MS_RDONLY) {
  2176. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  2177. ext3_commit_super(sb, es, 1);
  2178. }
  2179. out:
  2180. journal_unlock_updates(journal);
  2181. }
  2182. /*
  2183. * If we are mounting (or read-write remounting) a filesystem whose journal
  2184. * has recorded an error from a previous lifetime, move that error to the
  2185. * main filesystem now.
  2186. */
  2187. static void ext3_clear_journal_err(struct super_block *sb,
  2188. struct ext3_super_block *es)
  2189. {
  2190. journal_t *journal;
  2191. int j_errno;
  2192. const char *errstr;
  2193. journal = EXT3_SB(sb)->s_journal;
  2194. /*
  2195. * Now check for any error status which may have been recorded in the
  2196. * journal by a prior ext3_error() or ext3_abort()
  2197. */
  2198. j_errno = journal_errno(journal);
  2199. if (j_errno) {
  2200. char nbuf[16];
  2201. errstr = ext3_decode_error(sb, j_errno, nbuf);
  2202. ext3_warning(sb, __func__, "Filesystem error recorded "
  2203. "from previous mount: %s", errstr);
  2204. ext3_warning(sb, __func__, "Marking fs in need of "
  2205. "filesystem check.");
  2206. EXT3_SB(sb)->s_mount_state |= EXT3_ERROR_FS;
  2207. es->s_state |= cpu_to_le16(EXT3_ERROR_FS);
  2208. ext3_commit_super (sb, es, 1);
  2209. journal_clear_err(journal);
  2210. }
  2211. }
  2212. /*
  2213. * Force the running and committing transactions to commit,
  2214. * and wait on the commit.
  2215. */
  2216. int ext3_force_commit(struct super_block *sb)
  2217. {
  2218. journal_t *journal;
  2219. int ret;
  2220. if (sb->s_flags & MS_RDONLY)
  2221. return 0;
  2222. journal = EXT3_SB(sb)->s_journal;
  2223. ret = ext3_journal_force_commit(journal);
  2224. return ret;
  2225. }
  2226. static int ext3_sync_fs(struct super_block *sb, int wait)
  2227. {
  2228. tid_t target;
  2229. if (journal_start_commit(EXT3_SB(sb)->s_journal, &target)) {
  2230. if (wait)
  2231. log_wait_commit(EXT3_SB(sb)->s_journal, target);
  2232. }
  2233. return 0;
  2234. }
  2235. /*
  2236. * LVM calls this function before a (read-only) snapshot is created. This
  2237. * gives us a chance to flush the journal completely and mark the fs clean.
  2238. */
  2239. static int ext3_freeze(struct super_block *sb)
  2240. {
  2241. int error = 0;
  2242. journal_t *journal;
  2243. if (!(sb->s_flags & MS_RDONLY)) {
  2244. journal = EXT3_SB(sb)->s_journal;
  2245. /* Now we set up the journal barrier. */
  2246. journal_lock_updates(journal);
  2247. /*
  2248. * We don't want to clear needs_recovery flag when we failed
  2249. * to flush the journal.
  2250. */
  2251. error = journal_flush(journal);
  2252. if (error < 0)
  2253. goto out;
  2254. /* Journal blocked and flushed, clear needs_recovery flag. */
  2255. EXT3_CLEAR_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  2256. error = ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
  2257. if (error)
  2258. goto out;
  2259. }
  2260. return 0;
  2261. out:
  2262. journal_unlock_updates(journal);
  2263. return error;
  2264. }
  2265. /*
  2266. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  2267. * flag here, even though the filesystem is not technically dirty yet.
  2268. */
  2269. static int ext3_unfreeze(struct super_block *sb)
  2270. {
  2271. if (!(sb->s_flags & MS_RDONLY)) {
  2272. lock_super(sb);
  2273. /* Reser the needs_recovery flag before the fs is unlocked. */
  2274. EXT3_SET_INCOMPAT_FEATURE(sb, EXT3_FEATURE_INCOMPAT_RECOVER);
  2275. ext3_commit_super(sb, EXT3_SB(sb)->s_es, 1);
  2276. unlock_super(sb);
  2277. journal_unlock_updates(EXT3_SB(sb)->s_journal);
  2278. }
  2279. return 0;
  2280. }
  2281. static int ext3_remount (struct super_block * sb, int * flags, char * data)
  2282. {
  2283. struct ext3_super_block * es;
  2284. struct ext3_sb_info *sbi = EXT3_SB(sb);
  2285. ext3_fsblk_t n_blocks_count = 0;
  2286. unsigned long old_sb_flags;
  2287. struct ext3_mount_options old_opts;
  2288. int enable_quota = 0;
  2289. int err;
  2290. #ifdef CONFIG_QUOTA
  2291. int i;
  2292. #endif
  2293. /* Store the original options */
  2294. lock_super(sb);
  2295. old_sb_flags = sb->s_flags;
  2296. old_opts.s_mount_opt = sbi->s_mount_opt;
  2297. old_opts.s_resuid = sbi->s_resuid;
  2298. old_opts.s_resgid = sbi->s_resgid;
  2299. old_opts.s_commit_interval = sbi->s_commit_interval;
  2300. #ifdef CONFIG_QUOTA
  2301. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  2302. for (i = 0; i < MAXQUOTAS; i++)
  2303. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  2304. #endif
  2305. /*
  2306. * Allow the "check" option to be passed as a remount option.
  2307. */
  2308. if (!parse_options(data, sb, NULL, NULL, &n_blocks_count, 1)) {
  2309. err = -EINVAL;
  2310. goto restore_opts;
  2311. }
  2312. if (test_opt(sb, ABORT))
  2313. ext3_abort(sb, __func__, "Abort forced by user");
  2314. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2315. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  2316. es = sbi->s_es;
  2317. ext3_init_journal_params(sb, sbi->s_journal);
  2318. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY) ||
  2319. n_blocks_count > le32_to_cpu(es->s_blocks_count)) {
  2320. if (test_opt(sb, ABORT)) {
  2321. err = -EROFS;
  2322. goto restore_opts;
  2323. }
  2324. if (*flags & MS_RDONLY) {
  2325. err = dquot_suspend(sb, -1);
  2326. if (err < 0)
  2327. goto restore_opts;
  2328. /*
  2329. * First of all, the unconditional stuff we have to do
  2330. * to disable replay of the journal when we next remount
  2331. */
  2332. sb->s_flags |= MS_RDONLY;
  2333. /*
  2334. * OK, test if we are remounting a valid rw partition
  2335. * readonly, and if so set the rdonly flag and then
  2336. * mark the partition as valid again.
  2337. */
  2338. if (!(es->s_state & cpu_to_le16(EXT3_VALID_FS)) &&
  2339. (sbi->s_mount_state & EXT3_VALID_FS))
  2340. es->s_state = cpu_to_le16(sbi->s_mount_state);
  2341. ext3_mark_recovery_complete(sb, es);
  2342. } else {
  2343. __le32 ret;
  2344. if ((ret = EXT3_HAS_RO_COMPAT_FEATURE(sb,
  2345. ~EXT3_FEATURE_RO_COMPAT_SUPP))) {
  2346. ext3_msg(sb, KERN_WARNING,
  2347. "warning: couldn't remount RDWR "
  2348. "because of unsupported optional "
  2349. "features (%x)", le32_to_cpu(ret));
  2350. err = -EROFS;
  2351. goto restore_opts;
  2352. }
  2353. /*
  2354. * If we have an unprocessed orphan list hanging
  2355. * around from a previously readonly bdev mount,
  2356. * require a full umount/remount for now.
  2357. */
  2358. if (es->s_last_orphan) {
  2359. ext3_msg(sb, KERN_WARNING, "warning: couldn't "
  2360. "remount RDWR because of unprocessed "
  2361. "orphan inode list. Please "
  2362. "umount/remount instead.");
  2363. err = -EINVAL;
  2364. goto restore_opts;
  2365. }
  2366. /*
  2367. * Mounting a RDONLY partition read-write, so reread
  2368. * and store the current valid flag. (It may have
  2369. * been changed by e2fsck since we originally mounted
  2370. * the partition.)
  2371. */
  2372. ext3_clear_journal_err(sb, es);
  2373. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2374. if ((err = ext3_group_extend(sb, es, n_blocks_count)))
  2375. goto restore_opts;
  2376. if (!ext3_setup_super (sb, es, 0))
  2377. sb->s_flags &= ~MS_RDONLY;
  2378. enable_quota = 1;
  2379. }
  2380. }
  2381. #ifdef CONFIG_QUOTA
  2382. /* Release old quota file names */
  2383. for (i = 0; i < MAXQUOTAS; i++)
  2384. if (old_opts.s_qf_names[i] &&
  2385. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  2386. kfree(old_opts.s_qf_names[i]);
  2387. #endif
  2388. unlock_super(sb);
  2389. if (enable_quota)
  2390. dquot_resume(sb, -1);
  2391. return 0;
  2392. restore_opts:
  2393. sb->s_flags = old_sb_flags;
  2394. sbi->s_mount_opt = old_opts.s_mount_opt;
  2395. sbi->s_resuid = old_opts.s_resuid;
  2396. sbi->s_resgid = old_opts.s_resgid;
  2397. sbi->s_commit_interval = old_opts.s_commit_interval;
  2398. #ifdef CONFIG_QUOTA
  2399. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  2400. for (i = 0; i < MAXQUOTAS; i++) {
  2401. if (sbi->s_qf_names[i] &&
  2402. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  2403. kfree(sbi->s_qf_names[i]);
  2404. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  2405. }
  2406. #endif
  2407. unlock_super(sb);
  2408. return err;
  2409. }
  2410. static int ext3_statfs (struct dentry * dentry, struct kstatfs * buf)
  2411. {
  2412. struct super_block *sb = dentry->d_sb;
  2413. struct ext3_sb_info *sbi = EXT3_SB(sb);
  2414. struct ext3_super_block *es = sbi->s_es;
  2415. u64 fsid;
  2416. if (test_opt(sb, MINIX_DF)) {
  2417. sbi->s_overhead_last = 0;
  2418. } else if (sbi->s_blocks_last != le32_to_cpu(es->s_blocks_count)) {
  2419. unsigned long ngroups = sbi->s_groups_count, i;
  2420. ext3_fsblk_t overhead = 0;
  2421. smp_rmb();
  2422. /*
  2423. * Compute the overhead (FS structures). This is constant
  2424. * for a given filesystem unless the number of block groups
  2425. * changes so we cache the previous value until it does.
  2426. */
  2427. /*
  2428. * All of the blocks before first_data_block are
  2429. * overhead
  2430. */
  2431. overhead = le32_to_cpu(es->s_first_data_block);
  2432. /*
  2433. * Add the overhead attributed to the superblock and
  2434. * block group descriptors. If the sparse superblocks
  2435. * feature is turned on, then not all groups have this.
  2436. */
  2437. for (i = 0; i < ngroups; i++) {
  2438. overhead += ext3_bg_has_super(sb, i) +
  2439. ext3_bg_num_gdb(sb, i);
  2440. cond_resched();
  2441. }
  2442. /*
  2443. * Every block group has an inode bitmap, a block
  2444. * bitmap, and an inode table.
  2445. */
  2446. overhead += ngroups * (2 + sbi->s_itb_per_group);
  2447. sbi->s_overhead_last = overhead;
  2448. smp_wmb();
  2449. sbi->s_blocks_last = le32_to_cpu(es->s_blocks_count);
  2450. }
  2451. buf->f_type = EXT3_SUPER_MAGIC;
  2452. buf->f_bsize = sb->s_blocksize;
  2453. buf->f_blocks = le32_to_cpu(es->s_blocks_count) - sbi->s_overhead_last;
  2454. buf->f_bfree = percpu_counter_sum_positive(&sbi->s_freeblocks_counter);
  2455. buf->f_bavail = buf->f_bfree - le32_to_cpu(es->s_r_blocks_count);
  2456. if (buf->f_bfree < le32_to_cpu(es->s_r_blocks_count))
  2457. buf->f_bavail = 0;
  2458. buf->f_files = le32_to_cpu(es->s_inodes_count);
  2459. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  2460. buf->f_namelen = EXT3_NAME_LEN;
  2461. fsid = le64_to_cpup((void *)es->s_uuid) ^
  2462. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  2463. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  2464. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  2465. return 0;
  2466. }
  2467. /* Helper function for writing quotas on sync - we need to start transaction before quota file
  2468. * is locked for write. Otherwise the are possible deadlocks:
  2469. * Process 1 Process 2
  2470. * ext3_create() quota_sync()
  2471. * journal_start() write_dquot()
  2472. * dquot_initialize() down(dqio_mutex)
  2473. * down(dqio_mutex) journal_start()
  2474. *
  2475. */
  2476. #ifdef CONFIG_QUOTA
  2477. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  2478. {
  2479. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  2480. }
  2481. static int ext3_write_dquot(struct dquot *dquot)
  2482. {
  2483. int ret, err;
  2484. handle_t *handle;
  2485. struct inode *inode;
  2486. inode = dquot_to_inode(dquot);
  2487. handle = ext3_journal_start(inode,
  2488. EXT3_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  2489. if (IS_ERR(handle))
  2490. return PTR_ERR(handle);
  2491. ret = dquot_commit(dquot);
  2492. err = ext3_journal_stop(handle);
  2493. if (!ret)
  2494. ret = err;
  2495. return ret;
  2496. }
  2497. static int ext3_acquire_dquot(struct dquot *dquot)
  2498. {
  2499. int ret, err;
  2500. handle_t *handle;
  2501. handle = ext3_journal_start(dquot_to_inode(dquot),
  2502. EXT3_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  2503. if (IS_ERR(handle))
  2504. return PTR_ERR(handle);
  2505. ret = dquot_acquire(dquot);
  2506. err = ext3_journal_stop(handle);
  2507. if (!ret)
  2508. ret = err;
  2509. return ret;
  2510. }
  2511. static int ext3_release_dquot(struct dquot *dquot)
  2512. {
  2513. int ret, err;
  2514. handle_t *handle;
  2515. handle = ext3_journal_start(dquot_to_inode(dquot),
  2516. EXT3_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  2517. if (IS_ERR(handle)) {
  2518. /* Release dquot anyway to avoid endless cycle in dqput() */
  2519. dquot_release(dquot);
  2520. return PTR_ERR(handle);
  2521. }
  2522. ret = dquot_release(dquot);
  2523. err = ext3_journal_stop(handle);
  2524. if (!ret)
  2525. ret = err;
  2526. return ret;
  2527. }
  2528. static int ext3_mark_dquot_dirty(struct dquot *dquot)
  2529. {
  2530. /* Are we journaling quotas? */
  2531. if (EXT3_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  2532. EXT3_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  2533. dquot_mark_dquot_dirty(dquot);
  2534. return ext3_write_dquot(dquot);
  2535. } else {
  2536. return dquot_mark_dquot_dirty(dquot);
  2537. }
  2538. }
  2539. static int ext3_write_info(struct super_block *sb, int type)
  2540. {
  2541. int ret, err;
  2542. handle_t *handle;
  2543. /* Data block + inode block */
  2544. handle = ext3_journal_start(sb->s_root->d_inode, 2);
  2545. if (IS_ERR(handle))
  2546. return PTR_ERR(handle);
  2547. ret = dquot_commit_info(sb, type);
  2548. err = ext3_journal_stop(handle);
  2549. if (!ret)
  2550. ret = err;
  2551. return ret;
  2552. }
  2553. /*
  2554. * Turn on quotas during mount time - we need to find
  2555. * the quota file and such...
  2556. */
  2557. static int ext3_quota_on_mount(struct super_block *sb, int type)
  2558. {
  2559. return dquot_quota_on_mount(sb, EXT3_SB(sb)->s_qf_names[type],
  2560. EXT3_SB(sb)->s_jquota_fmt, type);
  2561. }
  2562. /*
  2563. * Standard function to be called on quota_on
  2564. */
  2565. static int ext3_quota_on(struct super_block *sb, int type, int format_id,
  2566. char *name)
  2567. {
  2568. int err;
  2569. struct path path;
  2570. if (!test_opt(sb, QUOTA))
  2571. return -EINVAL;
  2572. err = kern_path(name, LOOKUP_FOLLOW, &path);
  2573. if (err)
  2574. return err;
  2575. /* Quotafile not on the same filesystem? */
  2576. if (path.mnt->mnt_sb != sb) {
  2577. path_put(&path);
  2578. return -EXDEV;
  2579. }
  2580. /* Journaling quota? */
  2581. if (EXT3_SB(sb)->s_qf_names[type]) {
  2582. /* Quotafile not of fs root? */
  2583. if (path.dentry->d_parent != sb->s_root)
  2584. ext3_msg(sb, KERN_WARNING,
  2585. "warning: Quota file not on filesystem root. "
  2586. "Journaled quota will not work.");
  2587. }
  2588. /*
  2589. * When we journal data on quota file, we have to flush journal to see
  2590. * all updates to the file when we bypass pagecache...
  2591. */
  2592. if (ext3_should_journal_data(path.dentry->d_inode)) {
  2593. /*
  2594. * We don't need to lock updates but journal_flush() could
  2595. * otherwise be livelocked...
  2596. */
  2597. journal_lock_updates(EXT3_SB(sb)->s_journal);
  2598. err = journal_flush(EXT3_SB(sb)->s_journal);
  2599. journal_unlock_updates(EXT3_SB(sb)->s_journal);
  2600. if (err) {
  2601. path_put(&path);
  2602. return err;
  2603. }
  2604. }
  2605. err = dquot_quota_on_path(sb, type, format_id, &path);
  2606. path_put(&path);
  2607. return err;
  2608. }
  2609. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  2610. * acquiring the locks... As quota files are never truncated and quota code
  2611. * itself serializes the operations (and noone else should touch the files)
  2612. * we don't have to be afraid of races */
  2613. static ssize_t ext3_quota_read(struct super_block *sb, int type, char *data,
  2614. size_t len, loff_t off)
  2615. {
  2616. struct inode *inode = sb_dqopt(sb)->files[type];
  2617. sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
  2618. int err = 0;
  2619. int offset = off & (sb->s_blocksize - 1);
  2620. int tocopy;
  2621. size_t toread;
  2622. struct buffer_head *bh;
  2623. loff_t i_size = i_size_read(inode);
  2624. if (off > i_size)
  2625. return 0;
  2626. if (off+len > i_size)
  2627. len = i_size-off;
  2628. toread = len;
  2629. while (toread > 0) {
  2630. tocopy = sb->s_blocksize - offset < toread ?
  2631. sb->s_blocksize - offset : toread;
  2632. bh = ext3_bread(NULL, inode, blk, 0, &err);
  2633. if (err)
  2634. return err;
  2635. if (!bh) /* A hole? */
  2636. memset(data, 0, tocopy);
  2637. else
  2638. memcpy(data, bh->b_data+offset, tocopy);
  2639. brelse(bh);
  2640. offset = 0;
  2641. toread -= tocopy;
  2642. data += tocopy;
  2643. blk++;
  2644. }
  2645. return len;
  2646. }
  2647. /* Write to quotafile (we know the transaction is already started and has
  2648. * enough credits) */
  2649. static ssize_t ext3_quota_write(struct super_block *sb, int type,
  2650. const char *data, size_t len, loff_t off)
  2651. {
  2652. struct inode *inode = sb_dqopt(sb)->files[type];
  2653. sector_t blk = off >> EXT3_BLOCK_SIZE_BITS(sb);
  2654. int err = 0;
  2655. int offset = off & (sb->s_blocksize - 1);
  2656. int journal_quota = EXT3_SB(sb)->s_qf_names[type] != NULL;
  2657. struct buffer_head *bh;
  2658. handle_t *handle = journal_current_handle();
  2659. if (!handle) {
  2660. ext3_msg(sb, KERN_WARNING,
  2661. "warning: quota write (off=%llu, len=%llu)"
  2662. " cancelled because transaction is not started.",
  2663. (unsigned long long)off, (unsigned long long)len);
  2664. return -EIO;
  2665. }
  2666. /*
  2667. * Since we account only one data block in transaction credits,
  2668. * then it is impossible to cross a block boundary.
  2669. */
  2670. if (sb->s_blocksize - offset < len) {
  2671. ext3_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  2672. " cancelled because not block aligned",
  2673. (unsigned long long)off, (unsigned long long)len);
  2674. return -EIO;
  2675. }
  2676. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  2677. bh = ext3_bread(handle, inode, blk, 1, &err);
  2678. if (!bh)
  2679. goto out;
  2680. if (journal_quota) {
  2681. err = ext3_journal_get_write_access(handle, bh);
  2682. if (err) {
  2683. brelse(bh);
  2684. goto out;
  2685. }
  2686. }
  2687. lock_buffer(bh);
  2688. memcpy(bh->b_data+offset, data, len);
  2689. flush_dcache_page(bh->b_page);
  2690. unlock_buffer(bh);
  2691. if (journal_quota)
  2692. err = ext3_journal_dirty_metadata(handle, bh);
  2693. else {
  2694. /* Always do at least ordered writes for quotas */
  2695. err = ext3_journal_dirty_data(handle, bh);
  2696. mark_buffer_dirty(bh);
  2697. }
  2698. brelse(bh);
  2699. out:
  2700. if (err) {
  2701. mutex_unlock(&inode->i_mutex);
  2702. return err;
  2703. }
  2704. if (inode->i_size < off + len) {
  2705. i_size_write(inode, off + len);
  2706. EXT3_I(inode)->i_disksize = inode->i_size;
  2707. }
  2708. inode->i_version++;
  2709. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  2710. ext3_mark_inode_dirty(handle, inode);
  2711. mutex_unlock(&inode->i_mutex);
  2712. return len;
  2713. }
  2714. #endif
  2715. static struct dentry *ext3_mount(struct file_system_type *fs_type,
  2716. int flags, const char *dev_name, void *data)
  2717. {
  2718. return mount_bdev(fs_type, flags, dev_name, data, ext3_fill_super);
  2719. }
  2720. static struct file_system_type ext3_fs_type = {
  2721. .owner = THIS_MODULE,
  2722. .name = "ext3",
  2723. .mount = ext3_mount,
  2724. .kill_sb = kill_block_super,
  2725. .fs_flags = FS_REQUIRES_DEV,
  2726. };
  2727. static int __init init_ext3_fs(void)
  2728. {
  2729. int err = init_ext3_xattr();
  2730. if (err)
  2731. return err;
  2732. err = init_inodecache();
  2733. if (err)
  2734. goto out1;
  2735. err = register_filesystem(&ext3_fs_type);
  2736. if (err)
  2737. goto out;
  2738. return 0;
  2739. out:
  2740. destroy_inodecache();
  2741. out1:
  2742. exit_ext3_xattr();
  2743. return err;
  2744. }
  2745. static void __exit exit_ext3_fs(void)
  2746. {
  2747. unregister_filesystem(&ext3_fs_type);
  2748. destroy_inodecache();
  2749. exit_ext3_xattr();
  2750. }
  2751. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  2752. MODULE_DESCRIPTION("Second Extended Filesystem with journaling extensions");
  2753. MODULE_LICENSE("GPL");
  2754. module_init(init_ext3_fs)
  2755. module_exit(exit_ext3_fs)