super.c 84 KB

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