super.c 113 KB

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