super.c 102 KB

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