super.c 85 KB

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