super.c 107 KB

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