super.c 107 KB

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