super.c 134 KB

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