super.c 112 KB

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