super.c 140 KB

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