super.c 117 KB

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