super.c 121 KB

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