super.c 101 KB

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