super.c 80 KB

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