super.c 114 KB

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