super.c 133 KB

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