super.c 84 KB

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