super.c 85 KB

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