super.c 101 KB

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