super.c 116 KB

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