super.c 74 KB

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