super.c 117 KB

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