super.c 112 KB

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