super.c 103 KB

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