super.c 83 KB

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