super.c 99 KB

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