super.c 113 KB

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