super.c 84 KB

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