super.c 112 KB

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