super_ 140 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979
  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. #ifdef CONFIG_QUOTA
  1432. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1433. #endif
  1434. char *p;
  1435. substring_t args[MAX_OPT_ARGS];
  1436. int token;
  1437. if (!options)
  1438. return 1;
  1439. while ((p = strsep(&options, ",")) != NULL) {
  1440. if (!*p)
  1441. continue;
  1442. /*
  1443. * Initialize args struct so we know whether arg was
  1444. * found; some options take optional arguments.
  1445. */
  1446. args[0].to = args[0].from = 0;
  1447. token = match_token(p, tokens, args);
  1448. if (handle_mount_opt(sb, p, token, args, journal_devnum,
  1449. journal_ioprio, is_remount) < 0)
  1450. return 0;
  1451. }
  1452. #ifdef CONFIG_QUOTA
  1453. if (sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]) {
  1454. if (test_opt(sb, USRQUOTA) && sbi->s_qf_names[USRQUOTA])
  1455. clear_opt(sb, USRQUOTA);
  1456. if (test_opt(sb, GRPQUOTA) && sbi->s_qf_names[GRPQUOTA])
  1457. clear_opt(sb, GRPQUOTA);
  1458. if (test_opt(sb, GRPQUOTA) || test_opt(sb, USRQUOTA)) {
  1459. ext4_msg(sb, KERN_ERR, "old and new quota "
  1460. "format mixing");
  1461. return 0;
  1462. }
  1463. if (!sbi->s_jquota_fmt) {
  1464. ext4_msg(sb, KERN_ERR, "journaled quota format "
  1465. "not specified");
  1466. return 0;
  1467. }
  1468. } else {
  1469. if (sbi->s_jquota_fmt) {
  1470. ext4_msg(sb, KERN_ERR, "journaled quota format "
  1471. "specified with no journaling "
  1472. "enabled");
  1473. return 0;
  1474. }
  1475. }
  1476. #endif
  1477. return 1;
  1478. }
  1479. static inline void ext4_show_quota_options(struct seq_file *seq,
  1480. struct super_block *sb)
  1481. {
  1482. #if defined(CONFIG_QUOTA)
  1483. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1484. if (sbi->s_jquota_fmt) {
  1485. char *fmtname = "";
  1486. switch (sbi->s_jquota_fmt) {
  1487. case QFMT_VFS_OLD:
  1488. fmtname = "vfsold";
  1489. break;
  1490. case QFMT_VFS_V0:
  1491. fmtname = "vfsv0";
  1492. break;
  1493. case QFMT_VFS_V1:
  1494. fmtname = "vfsv1";
  1495. break;
  1496. }
  1497. seq_printf(seq, ",jqfmt=%s", fmtname);
  1498. }
  1499. if (sbi->s_qf_names[USRQUOTA])
  1500. seq_printf(seq, ",usrjquota=%s", sbi->s_qf_names[USRQUOTA]);
  1501. if (sbi->s_qf_names[GRPQUOTA])
  1502. seq_printf(seq, ",grpjquota=%s", sbi->s_qf_names[GRPQUOTA]);
  1503. if (test_opt(sb, USRQUOTA))
  1504. seq_puts(seq, ",usrquota");
  1505. if (test_opt(sb, GRPQUOTA))
  1506. seq_puts(seq, ",grpquota");
  1507. #endif
  1508. }
  1509. static const char *token2str(int token)
  1510. {
  1511. static const struct match_token *t;
  1512. for (t = tokens; t->token != Opt_err; t++)
  1513. if (t->token == token && !strchr(t->pattern, '='))
  1514. break;
  1515. return t->pattern;
  1516. }
  1517. /*
  1518. * Show an option if
  1519. * - it's set to a non-default value OR
  1520. * - if the per-sb default is different from the global default
  1521. */
  1522. static int _ext4_show_options(struct seq_file *seq, struct super_block *sb,
  1523. int nodefs)
  1524. {
  1525. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1526. struct ext4_super_block *es = sbi->s_es;
  1527. int def_errors, def_mount_opt = nodefs ? 0 : sbi->s_def_mount_opt;
  1528. const struct mount_opts *m;
  1529. char sep = nodefs ? '\n' : ',';
  1530. #define SEQ_OPTS_PUTS(str) seq_printf(seq, "%c" str, sep)
  1531. #define SEQ_OPTS_PRINT(str, arg) seq_printf(seq, "%c" str, sep, arg)
  1532. if (sbi->s_sb_block != 1)
  1533. SEQ_OPTS_PRINT("sb=%llu", sbi->s_sb_block);
  1534. for (m = ext4_mount_opts; m->token != Opt_err; m++) {
  1535. int want_set = m->flags & MOPT_SET;
  1536. if (((m->flags & (MOPT_SET|MOPT_CLEAR)) == 0) ||
  1537. (m->flags & MOPT_CLEAR_ERR))
  1538. continue;
  1539. if (!(m->mount_opt & (sbi->s_mount_opt ^ def_mount_opt)))
  1540. continue; /* skip if same as the default */
  1541. if ((want_set &&
  1542. (sbi->s_mount_opt & m->mount_opt) != m->mount_opt) ||
  1543. (!want_set && (sbi->s_mount_opt & m->mount_opt)))
  1544. continue; /* select Opt_noFoo vs Opt_Foo */
  1545. SEQ_OPTS_PRINT("%s", token2str(m->token));
  1546. }
  1547. if (nodefs || sbi->s_resuid != EXT4_DEF_RESUID ||
  1548. le16_to_cpu(es->s_def_resuid) != EXT4_DEF_RESUID)
  1549. SEQ_OPTS_PRINT("resuid=%u", sbi->s_resuid);
  1550. if (nodefs || sbi->s_resgid != EXT4_DEF_RESGID ||
  1551. le16_to_cpu(es->s_def_resgid) != EXT4_DEF_RESGID)
  1552. SEQ_OPTS_PRINT("resgid=%u", sbi->s_resgid);
  1553. def_errors = nodefs ? -1 : le16_to_cpu(es->s_errors);
  1554. if (test_opt(sb, ERRORS_RO) && def_errors != EXT4_ERRORS_RO)
  1555. SEQ_OPTS_PUTS("errors=remount-ro");
  1556. if (test_opt(sb, ERRORS_CONT) && def_errors != EXT4_ERRORS_CONTINUE)
  1557. SEQ_OPTS_PUTS("errors=continue");
  1558. if (test_opt(sb, ERRORS_PANIC) && def_errors != EXT4_ERRORS_PANIC)
  1559. SEQ_OPTS_PUTS("errors=panic");
  1560. if (nodefs || sbi->s_commit_interval != JBD2_DEFAULT_MAX_COMMIT_AGE*HZ)
  1561. SEQ_OPTS_PRINT("commit=%lu", sbi->s_commit_interval / HZ);
  1562. if (nodefs || sbi->s_min_batch_time != EXT4_DEF_MIN_BATCH_TIME)
  1563. SEQ_OPTS_PRINT("min_batch_time=%u", sbi->s_min_batch_time);
  1564. if (nodefs || sbi->s_max_batch_time != EXT4_DEF_MAX_BATCH_TIME)
  1565. SEQ_OPTS_PRINT("max_batch_time=%u", sbi->s_max_batch_time);
  1566. if (sb->s_flags & MS_I_VERSION)
  1567. SEQ_OPTS_PUTS("i_version");
  1568. if (nodefs || sbi->s_stripe)
  1569. SEQ_OPTS_PRINT("stripe=%lu", sbi->s_stripe);
  1570. if (EXT4_MOUNT_DATA_FLAGS & (sbi->s_mount_opt ^ def_mount_opt)) {
  1571. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  1572. SEQ_OPTS_PUTS("data=journal");
  1573. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  1574. SEQ_OPTS_PUTS("data=ordered");
  1575. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
  1576. SEQ_OPTS_PUTS("data=writeback");
  1577. }
  1578. if (nodefs ||
  1579. sbi->s_inode_readahead_blks != EXT4_DEF_INODE_READAHEAD_BLKS)
  1580. SEQ_OPTS_PRINT("inode_readahead_blks=%u",
  1581. sbi->s_inode_readahead_blks);
  1582. if (nodefs || (test_opt(sb, INIT_INODE_TABLE) &&
  1583. (sbi->s_li_wait_mult != EXT4_DEF_LI_WAIT_MULT)))
  1584. SEQ_OPTS_PRINT("init_itable=%u", sbi->s_li_wait_mult);
  1585. ext4_show_quota_options(seq, sb);
  1586. return 0;
  1587. }
  1588. static int ext4_show_options(struct seq_file *seq, struct dentry *root)
  1589. {
  1590. return _ext4_show_options(seq, root->d_sb, 0);
  1591. }
  1592. static int options_seq_show(struct seq_file *seq, void *offset)
  1593. {
  1594. struct super_block *sb = seq->private;
  1595. int rc;
  1596. seq_puts(seq, (sb->s_flags & MS_RDONLY) ? "ro" : "rw");
  1597. rc = _ext4_show_options(seq, sb, 1);
  1598. seq_puts(seq, "\n");
  1599. return rc;
  1600. }
  1601. static int options_open_fs(struct inode *inode, struct file *file)
  1602. {
  1603. return single_open(file, options_seq_show, PDE(inode)->data);
  1604. }
  1605. static const struct file_operations ext4_seq_options_fops = {
  1606. .owner = THIS_MODULE,
  1607. .open = options_open_fs,
  1608. .read = seq_read,
  1609. .llseek = seq_lseek,
  1610. .release = single_release,
  1611. };
  1612. static int ext4_setup_super(struct super_block *sb, struct ext4_super_block *es,
  1613. int read_only)
  1614. {
  1615. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1616. int res = 0;
  1617. if (le32_to_cpu(es->s_rev_level) > EXT4_MAX_SUPP_REV) {
  1618. ext4_msg(sb, KERN_ERR, "revision level too high, "
  1619. "forcing read-only mode");
  1620. res = MS_RDONLY;
  1621. }
  1622. if (read_only)
  1623. goto done;
  1624. if (!(sbi->s_mount_state & EXT4_VALID_FS))
  1625. ext4_msg(sb, KERN_WARNING, "warning: mounting unchecked fs, "
  1626. "running e2fsck is recommended");
  1627. else if ((sbi->s_mount_state & EXT4_ERROR_FS))
  1628. ext4_msg(sb, KERN_WARNING,
  1629. "warning: mounting fs with errors, "
  1630. "running e2fsck is recommended");
  1631. else if ((__s16) le16_to_cpu(es->s_max_mnt_count) > 0 &&
  1632. le16_to_cpu(es->s_mnt_count) >=
  1633. (unsigned short) (__s16) le16_to_cpu(es->s_max_mnt_count))
  1634. ext4_msg(sb, KERN_WARNING,
  1635. "warning: maximal mount count reached, "
  1636. "running e2fsck is recommended");
  1637. else if (le32_to_cpu(es->s_checkinterval) &&
  1638. (le32_to_cpu(es->s_lastcheck) +
  1639. le32_to_cpu(es->s_checkinterval) <= get_seconds()))
  1640. ext4_msg(sb, KERN_WARNING,
  1641. "warning: checktime reached, "
  1642. "running e2fsck is recommended");
  1643. if (!sbi->s_journal)
  1644. es->s_state &= cpu_to_le16(~EXT4_VALID_FS);
  1645. if (!(__s16) le16_to_cpu(es->s_max_mnt_count))
  1646. es->s_max_mnt_count = cpu_to_le16(EXT4_DFL_MAX_MNT_COUNT);
  1647. le16_add_cpu(&es->s_mnt_count, 1);
  1648. es->s_mtime = cpu_to_le32(get_seconds());
  1649. ext4_update_dynamic_rev(sb);
  1650. if (sbi->s_journal)
  1651. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  1652. ext4_commit_super(sb, 1);
  1653. done:
  1654. if (test_opt(sb, DEBUG))
  1655. printk(KERN_INFO "[EXT4 FS bs=%lu, gc=%u, "
  1656. "bpg=%lu, ipg=%lu, mo=%04x, mo2=%04x]\n",
  1657. sb->s_blocksize,
  1658. sbi->s_groups_count,
  1659. EXT4_BLOCKS_PER_GROUP(sb),
  1660. EXT4_INODES_PER_GROUP(sb),
  1661. sbi->s_mount_opt, sbi->s_mount_opt2);
  1662. cleancache_init_fs(sb);
  1663. return res;
  1664. }
  1665. static int ext4_fill_flex_info(struct super_block *sb)
  1666. {
  1667. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1668. struct ext4_group_desc *gdp = NULL;
  1669. ext4_group_t flex_group_count;
  1670. ext4_group_t flex_group;
  1671. unsigned int groups_per_flex = 0;
  1672. size_t size;
  1673. int i;
  1674. sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
  1675. if (sbi->s_log_groups_per_flex < 1 || sbi->s_log_groups_per_flex > 31) {
  1676. sbi->s_log_groups_per_flex = 0;
  1677. return 1;
  1678. }
  1679. groups_per_flex = 1 << sbi->s_log_groups_per_flex;
  1680. /* We allocate both existing and potentially added groups */
  1681. flex_group_count = ((sbi->s_groups_count + groups_per_flex - 1) +
  1682. ((le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks) + 1) <<
  1683. EXT4_DESC_PER_BLOCK_BITS(sb))) / groups_per_flex;
  1684. size = flex_group_count * sizeof(struct flex_groups);
  1685. sbi->s_flex_groups = ext4_kvzalloc(size, GFP_KERNEL);
  1686. if (sbi->s_flex_groups == NULL) {
  1687. ext4_msg(sb, KERN_ERR, "not enough memory for %u flex groups",
  1688. flex_group_count);
  1689. goto failed;
  1690. }
  1691. for (i = 0; i < sbi->s_groups_count; i++) {
  1692. gdp = ext4_get_group_desc(sb, i, NULL);
  1693. flex_group = ext4_flex_group(sbi, i);
  1694. atomic_add(ext4_free_inodes_count(sb, gdp),
  1695. &sbi->s_flex_groups[flex_group].free_inodes);
  1696. atomic_add(ext4_free_group_clusters(sb, gdp),
  1697. &sbi->s_flex_groups[flex_group].free_clusters);
  1698. atomic_add(ext4_used_dirs_count(sb, gdp),
  1699. &sbi->s_flex_groups[flex_group].used_dirs);
  1700. }
  1701. return 1;
  1702. failed:
  1703. return 0;
  1704. }
  1705. __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
  1706. struct ext4_group_desc *gdp)
  1707. {
  1708. __u16 crc = 0;
  1709. if (sbi->s_es->s_feature_ro_compat &
  1710. cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
  1711. int offset = offsetof(struct ext4_group_desc, bg_checksum);
  1712. __le32 le_group = cpu_to_le32(block_group);
  1713. crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
  1714. crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
  1715. crc = crc16(crc, (__u8 *)gdp, offset);
  1716. offset += sizeof(gdp->bg_checksum); /* skip checksum */
  1717. /* for checksum of struct ext4_group_desc do the rest...*/
  1718. if ((sbi->s_es->s_feature_incompat &
  1719. cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT)) &&
  1720. offset < le16_to_cpu(sbi->s_es->s_desc_size))
  1721. crc = crc16(crc, (__u8 *)gdp + offset,
  1722. le16_to_cpu(sbi->s_es->s_desc_size) -
  1723. offset);
  1724. }
  1725. return cpu_to_le16(crc);
  1726. }
  1727. int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
  1728. struct ext4_group_desc *gdp)
  1729. {
  1730. if ((sbi->s_es->s_feature_ro_compat &
  1731. cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) &&
  1732. (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp)))
  1733. return 0;
  1734. return 1;
  1735. }
  1736. /* Called at mount-time, super-block is locked */
  1737. static int ext4_check_descriptors(struct super_block *sb,
  1738. ext4_group_t *first_not_zeroed)
  1739. {
  1740. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1741. ext4_fsblk_t first_block = le32_to_cpu(sbi->s_es->s_first_data_block);
  1742. ext4_fsblk_t last_block;
  1743. ext4_fsblk_t block_bitmap;
  1744. ext4_fsblk_t inode_bitmap;
  1745. ext4_fsblk_t inode_table;
  1746. int flexbg_flag = 0;
  1747. ext4_group_t i, grp = sbi->s_groups_count;
  1748. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
  1749. flexbg_flag = 1;
  1750. ext4_debug("Checking group descriptors");
  1751. for (i = 0; i < sbi->s_groups_count; i++) {
  1752. struct ext4_group_desc *gdp = ext4_get_group_desc(sb, i, NULL);
  1753. if (i == sbi->s_groups_count - 1 || flexbg_flag)
  1754. last_block = ext4_blocks_count(sbi->s_es) - 1;
  1755. else
  1756. last_block = first_block +
  1757. (EXT4_BLOCKS_PER_GROUP(sb) - 1);
  1758. if ((grp == sbi->s_groups_count) &&
  1759. !(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  1760. grp = i;
  1761. block_bitmap = ext4_block_bitmap(sb, gdp);
  1762. if (block_bitmap < first_block || block_bitmap > last_block) {
  1763. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1764. "Block bitmap for group %u not in group "
  1765. "(block %llu)!", i, block_bitmap);
  1766. return 0;
  1767. }
  1768. inode_bitmap = ext4_inode_bitmap(sb, gdp);
  1769. if (inode_bitmap < first_block || inode_bitmap > last_block) {
  1770. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1771. "Inode bitmap for group %u not in group "
  1772. "(block %llu)!", i, inode_bitmap);
  1773. return 0;
  1774. }
  1775. inode_table = ext4_inode_table(sb, gdp);
  1776. if (inode_table < first_block ||
  1777. inode_table + sbi->s_itb_per_group - 1 > last_block) {
  1778. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1779. "Inode table for group %u not in group "
  1780. "(block %llu)!", i, inode_table);
  1781. return 0;
  1782. }
  1783. ext4_lock_group(sb, i);
  1784. if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
  1785. ext4_msg(sb, KERN_ERR, "ext4_check_descriptors: "
  1786. "Checksum for group %u failed (%u!=%u)",
  1787. i, le16_to_cpu(ext4_group_desc_csum(sbi, i,
  1788. gdp)), le16_to_cpu(gdp->bg_checksum));
  1789. if (!(sb->s_flags & MS_RDONLY)) {
  1790. ext4_unlock_group(sb, i);
  1791. return 0;
  1792. }
  1793. }
  1794. ext4_unlock_group(sb, i);
  1795. if (!flexbg_flag)
  1796. first_block += EXT4_BLOCKS_PER_GROUP(sb);
  1797. }
  1798. if (NULL != first_not_zeroed)
  1799. *first_not_zeroed = grp;
  1800. ext4_free_blocks_count_set(sbi->s_es,
  1801. EXT4_C2B(sbi, ext4_count_free_clusters(sb)));
  1802. sbi->s_es->s_free_inodes_count =cpu_to_le32(ext4_count_free_inodes(sb));
  1803. return 1;
  1804. }
  1805. /* ext4_orphan_cleanup() walks a singly-linked list of inodes (starting at
  1806. * the superblock) which were deleted from all directories, but held open by
  1807. * a process at the time of a crash. We walk the list and try to delete these
  1808. * inodes at recovery time (only with a read-write filesystem).
  1809. *
  1810. * In order to keep the orphan inode chain consistent during traversal (in
  1811. * case of crash during recovery), we link each inode into the superblock
  1812. * orphan list_head and handle it the same way as an inode deletion during
  1813. * normal operation (which journals the operations for us).
  1814. *
  1815. * We only do an iget() and an iput() on each inode, which is very safe if we
  1816. * accidentally point at an in-use or already deleted inode. The worst that
  1817. * can happen in this case is that we get a "bit already cleared" message from
  1818. * ext4_free_inode(). The only reason we would point at a wrong inode is if
  1819. * e2fsck was run on this filesystem, and it must have already done the orphan
  1820. * inode cleanup for us, so we can safely abort without any further action.
  1821. */
  1822. static void ext4_orphan_cleanup(struct super_block *sb,
  1823. struct ext4_super_block *es)
  1824. {
  1825. unsigned int s_flags = sb->s_flags;
  1826. int nr_orphans = 0, nr_truncates = 0;
  1827. #ifdef CONFIG_QUOTA
  1828. int i;
  1829. #endif
  1830. if (!es->s_last_orphan) {
  1831. jbd_debug(4, "no orphan inodes to clean up\n");
  1832. return;
  1833. }
  1834. if (bdev_read_only(sb->s_bdev)) {
  1835. ext4_msg(sb, KERN_ERR, "write access "
  1836. "unavailable, skipping orphan cleanup");
  1837. return;
  1838. }
  1839. /* Check if feature set would not allow a r/w mount */
  1840. if (!ext4_feature_set_ok(sb, 0)) {
  1841. ext4_msg(sb, KERN_INFO, "Skipping orphan cleanup due to "
  1842. "unknown ROCOMPAT features");
  1843. return;
  1844. }
  1845. if (EXT4_SB(sb)->s_mount_state & EXT4_ERROR_FS) {
  1846. if (es->s_last_orphan)
  1847. jbd_debug(1, "Errors on filesystem, "
  1848. "clearing orphan list.\n");
  1849. es->s_last_orphan = 0;
  1850. jbd_debug(1, "Skipping orphan recovery on fs with errors.\n");
  1851. return;
  1852. }
  1853. if (s_flags & MS_RDONLY) {
  1854. ext4_msg(sb, KERN_INFO, "orphan cleanup on readonly fs");
  1855. sb->s_flags &= ~MS_RDONLY;
  1856. }
  1857. #ifdef CONFIG_QUOTA
  1858. /* Needed for iput() to work correctly and not trash data */
  1859. sb->s_flags |= MS_ACTIVE;
  1860. /* Turn on quotas so that they are updated correctly */
  1861. for (i = 0; i < MAXQUOTAS; i++) {
  1862. if (EXT4_SB(sb)->s_qf_names[i]) {
  1863. int ret = ext4_quota_on_mount(sb, i);
  1864. if (ret < 0)
  1865. ext4_msg(sb, KERN_ERR,
  1866. "Cannot turn on journaled "
  1867. "quota: error %d", ret);
  1868. }
  1869. }
  1870. #endif
  1871. while (es->s_last_orphan) {
  1872. struct inode *inode;
  1873. inode = ext4_orphan_get(sb, le32_to_cpu(es->s_last_orphan));
  1874. if (IS_ERR(inode)) {
  1875. es->s_last_orphan = 0;
  1876. break;
  1877. }
  1878. list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan);
  1879. dquot_initialize(inode);
  1880. if (inode->i_nlink) {
  1881. ext4_msg(sb, KERN_DEBUG,
  1882. "%s: truncating inode %lu to %lld bytes",
  1883. __func__, inode->i_ino, inode->i_size);
  1884. jbd_debug(2, "truncating inode %lu to %lld bytes\n",
  1885. inode->i_ino, inode->i_size);
  1886. ext4_truncate(inode);
  1887. nr_truncates++;
  1888. } else {
  1889. ext4_msg(sb, KERN_DEBUG,
  1890. "%s: deleting unreferenced inode %lu",
  1891. __func__, inode->i_ino);
  1892. jbd_debug(2, "deleting unreferenced inode %lu\n",
  1893. inode->i_ino);
  1894. nr_orphans++;
  1895. }
  1896. iput(inode); /* The delete magic happens here! */
  1897. }
  1898. #define PLURAL(x) (x), ((x) == 1) ? "" : "s"
  1899. if (nr_orphans)
  1900. ext4_msg(sb, KERN_INFO, "%d orphan inode%s deleted",
  1901. PLURAL(nr_orphans));
  1902. if (nr_truncates)
  1903. ext4_msg(sb, KERN_INFO, "%d truncate%s cleaned up",
  1904. PLURAL(nr_truncates));
  1905. #ifdef CONFIG_QUOTA
  1906. /* Turn quotas off */
  1907. for (i = 0; i < MAXQUOTAS; i++) {
  1908. if (sb_dqopt(sb)->files[i])
  1909. dquot_quota_off(sb, i);
  1910. }
  1911. #endif
  1912. sb->s_flags = s_flags; /* Restore MS_RDONLY status */
  1913. }
  1914. /*
  1915. * Maximal extent format file size.
  1916. * Resulting logical blkno at s_maxbytes must fit in our on-disk
  1917. * extent format containers, within a sector_t, and within i_blocks
  1918. * in the vfs. ext4 inode has 48 bits of i_block in fsblock units,
  1919. * so that won't be a limiting factor.
  1920. *
  1921. * However there is other limiting factor. We do store extents in the form
  1922. * of starting block and length, hence the resulting length of the extent
  1923. * covering maximum file size must fit into on-disk format containers as
  1924. * well. Given that length is always by 1 unit bigger than max unit (because
  1925. * we count 0 as well) we have to lower the s_maxbytes by one fs block.
  1926. *
  1927. * Note, this does *not* consider any metadata overhead for vfs i_blocks.
  1928. */
  1929. static loff_t ext4_max_size(int blkbits, int has_huge_files)
  1930. {
  1931. loff_t res;
  1932. loff_t upper_limit = MAX_LFS_FILESIZE;
  1933. /* small i_blocks in vfs inode? */
  1934. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  1935. /*
  1936. * CONFIG_LBDAF is not enabled implies the inode
  1937. * i_block represent total blocks in 512 bytes
  1938. * 32 == size of vfs inode i_blocks * 8
  1939. */
  1940. upper_limit = (1LL << 32) - 1;
  1941. /* total blocks in file system block size */
  1942. upper_limit >>= (blkbits - 9);
  1943. upper_limit <<= blkbits;
  1944. }
  1945. /*
  1946. * 32-bit extent-start container, ee_block. We lower the maxbytes
  1947. * by one fs block, so ee_len can cover the extent of maximum file
  1948. * size
  1949. */
  1950. res = (1LL << 32) - 1;
  1951. res <<= blkbits;
  1952. /* Sanity check against vm- & vfs- imposed limits */
  1953. if (res > upper_limit)
  1954. res = upper_limit;
  1955. return res;
  1956. }
  1957. /*
  1958. * Maximal bitmap file size. There is a direct, and {,double-,triple-}indirect
  1959. * block limit, and also a limit of (2^48 - 1) 512-byte sectors in i_blocks.
  1960. * We need to be 1 filesystem block less than the 2^48 sector limit.
  1961. */
  1962. static loff_t ext4_max_bitmap_size(int bits, int has_huge_files)
  1963. {
  1964. loff_t res = EXT4_NDIR_BLOCKS;
  1965. int meta_blocks;
  1966. loff_t upper_limit;
  1967. /* This is calculated to be the largest file size for a dense, block
  1968. * mapped file such that the file's total number of 512-byte sectors,
  1969. * including data and all indirect blocks, does not exceed (2^48 - 1).
  1970. *
  1971. * __u32 i_blocks_lo and _u16 i_blocks_high represent the total
  1972. * number of 512-byte sectors of the file.
  1973. */
  1974. if (!has_huge_files || sizeof(blkcnt_t) < sizeof(u64)) {
  1975. /*
  1976. * !has_huge_files or CONFIG_LBDAF not enabled implies that
  1977. * the inode i_block field represents total file blocks in
  1978. * 2^32 512-byte sectors == size of vfs inode i_blocks * 8
  1979. */
  1980. upper_limit = (1LL << 32) - 1;
  1981. /* total blocks in file system block size */
  1982. upper_limit >>= (bits - 9);
  1983. } else {
  1984. /*
  1985. * We use 48 bit ext4_inode i_blocks
  1986. * With EXT4_HUGE_FILE_FL set the i_blocks
  1987. * represent total number of blocks in
  1988. * file system block size
  1989. */
  1990. upper_limit = (1LL << 48) - 1;
  1991. }
  1992. /* indirect blocks */
  1993. meta_blocks = 1;
  1994. /* double indirect blocks */
  1995. meta_blocks += 1 + (1LL << (bits-2));
  1996. /* tripple indirect blocks */
  1997. meta_blocks += 1 + (1LL << (bits-2)) + (1LL << (2*(bits-2)));
  1998. upper_limit -= meta_blocks;
  1999. upper_limit <<= bits;
  2000. res += 1LL << (bits-2);
  2001. res += 1LL << (2*(bits-2));
  2002. res += 1LL << (3*(bits-2));
  2003. res <<= bits;
  2004. if (res > upper_limit)
  2005. res = upper_limit;
  2006. if (res > MAX_LFS_FILESIZE)
  2007. res = MAX_LFS_FILESIZE;
  2008. return res;
  2009. }
  2010. static ext4_fsblk_t descriptor_loc(struct super_block *sb,
  2011. ext4_fsblk_t logical_sb_block, int nr)
  2012. {
  2013. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2014. ext4_group_t bg, first_meta_bg;
  2015. int has_super = 0;
  2016. first_meta_bg = le32_to_cpu(sbi->s_es->s_first_meta_bg);
  2017. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
  2018. nr < first_meta_bg)
  2019. return logical_sb_block + nr + 1;
  2020. bg = sbi->s_desc_per_block * nr;
  2021. if (ext4_bg_has_super(sb, bg))
  2022. has_super = 1;
  2023. return (has_super + ext4_group_first_block_no(sb, bg));
  2024. }
  2025. /**
  2026. * ext4_get_stripe_size: Get the stripe size.
  2027. * @sbi: In memory super block info
  2028. *
  2029. * If we have specified it via mount option, then
  2030. * use the mount option value. If the value specified at mount time is
  2031. * greater than the blocks per group use the super block value.
  2032. * If the super block value is greater than blocks per group return 0.
  2033. * Allocator needs it be less than blocks per group.
  2034. *
  2035. */
  2036. static unsigned long ext4_get_stripe_size(struct ext4_sb_info *sbi)
  2037. {
  2038. unsigned long stride = le16_to_cpu(sbi->s_es->s_raid_stride);
  2039. unsigned long stripe_width =
  2040. le32_to_cpu(sbi->s_es->s_raid_stripe_width);
  2041. int ret;
  2042. if (sbi->s_stripe && sbi->s_stripe <= sbi->s_blocks_per_group)
  2043. ret = sbi->s_stripe;
  2044. else if (stripe_width <= sbi->s_blocks_per_group)
  2045. ret = stripe_width;
  2046. else if (stride <= sbi->s_blocks_per_group)
  2047. ret = stride;
  2048. else
  2049. ret = 0;
  2050. /*
  2051. * If the stripe width is 1, this makes no sense and
  2052. * we set it to 0 to turn off stripe handling code.
  2053. */
  2054. if (ret <= 1)
  2055. ret = 0;
  2056. return ret;
  2057. }
  2058. /* sysfs supprt */
  2059. struct ext4_attr {
  2060. struct attribute attr;
  2061. ssize_t (*show)(struct ext4_attr *, struct ext4_sb_info *, char *);
  2062. ssize_t (*store)(struct ext4_attr *, struct ext4_sb_info *,
  2063. const char *, size_t);
  2064. int offset;
  2065. };
  2066. static int parse_strtoul(const char *buf,
  2067. unsigned long max, unsigned long *value)
  2068. {
  2069. char *endp;
  2070. *value = simple_strtoul(skip_spaces(buf), &endp, 0);
  2071. endp = skip_spaces(endp);
  2072. if (*endp || *value > max)
  2073. return -EINVAL;
  2074. return 0;
  2075. }
  2076. static ssize_t delayed_allocation_blocks_show(struct ext4_attr *a,
  2077. struct ext4_sb_info *sbi,
  2078. char *buf)
  2079. {
  2080. return snprintf(buf, PAGE_SIZE, "%llu\n",
  2081. (s64) EXT4_C2B(sbi,
  2082. percpu_counter_sum(&sbi->s_dirtyclusters_counter)));
  2083. }
  2084. static ssize_t session_write_kbytes_show(struct ext4_attr *a,
  2085. struct ext4_sb_info *sbi, char *buf)
  2086. {
  2087. struct super_block *sb = sbi->s_buddy_cache->i_sb;
  2088. if (!sb->s_bdev->bd_part)
  2089. return snprintf(buf, PAGE_SIZE, "0\n");
  2090. return snprintf(buf, PAGE_SIZE, "%lu\n",
  2091. (part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2092. sbi->s_sectors_written_start) >> 1);
  2093. }
  2094. static ssize_t lifetime_write_kbytes_show(struct ext4_attr *a,
  2095. struct ext4_sb_info *sbi, char *buf)
  2096. {
  2097. struct super_block *sb = sbi->s_buddy_cache->i_sb;
  2098. if (!sb->s_bdev->bd_part)
  2099. return snprintf(buf, PAGE_SIZE, "0\n");
  2100. return snprintf(buf, PAGE_SIZE, "%llu\n",
  2101. (unsigned long long)(sbi->s_kbytes_written +
  2102. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  2103. EXT4_SB(sb)->s_sectors_written_start) >> 1)));
  2104. }
  2105. static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
  2106. struct ext4_sb_info *sbi,
  2107. const char *buf, size_t count)
  2108. {
  2109. unsigned long t;
  2110. if (parse_strtoul(buf, 0x40000000, &t))
  2111. return -EINVAL;
  2112. if (t && !is_power_of_2(t))
  2113. return -EINVAL;
  2114. sbi->s_inode_readahead_blks = t;
  2115. return count;
  2116. }
  2117. static ssize_t sbi_ui_show(struct ext4_attr *a,
  2118. struct ext4_sb_info *sbi, char *buf)
  2119. {
  2120. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2121. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  2122. }
  2123. static ssize_t sbi_ui_store(struct ext4_attr *a,
  2124. struct ext4_sb_info *sbi,
  2125. const char *buf, size_t count)
  2126. {
  2127. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2128. unsigned long t;
  2129. if (parse_strtoul(buf, 0xffffffff, &t))
  2130. return -EINVAL;
  2131. *ui = t;
  2132. return count;
  2133. }
  2134. #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
  2135. static struct ext4_attr ext4_attr_##_name = { \
  2136. .attr = {.name = __stringify(_name), .mode = _mode }, \
  2137. .show = _show, \
  2138. .store = _store, \
  2139. .offset = offsetof(struct ext4_sb_info, _elname), \
  2140. }
  2141. #define EXT4_ATTR(name, mode, show, store) \
  2142. static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
  2143. #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
  2144. #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
  2145. #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
  2146. #define EXT4_RW_ATTR_SBI_UI(name, elname) \
  2147. EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
  2148. #define ATTR_LIST(name) &ext4_attr_##name.attr
  2149. EXT4_RO_ATTR(delayed_allocation_blocks);
  2150. EXT4_RO_ATTR(session_write_kbytes);
  2151. EXT4_RO_ATTR(lifetime_write_kbytes);
  2152. EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
  2153. inode_readahead_blks_store, s_inode_readahead_blks);
  2154. EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
  2155. EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
  2156. EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
  2157. EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
  2158. EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
  2159. EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
  2160. EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
  2161. EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
  2162. static struct attribute *ext4_attrs[] = {
  2163. ATTR_LIST(delayed_allocation_blocks),
  2164. ATTR_LIST(session_write_kbytes),
  2165. ATTR_LIST(lifetime_write_kbytes),
  2166. ATTR_LIST(inode_readahead_blks),
  2167. ATTR_LIST(inode_goal),
  2168. ATTR_LIST(mb_stats),
  2169. ATTR_LIST(mb_max_to_scan),
  2170. ATTR_LIST(mb_min_to_scan),
  2171. ATTR_LIST(mb_order2_req),
  2172. ATTR_LIST(mb_stream_req),
  2173. ATTR_LIST(mb_group_prealloc),
  2174. ATTR_LIST(max_writeback_mb_bump),
  2175. NULL,
  2176. };
  2177. /* Features this copy of ext4 supports */
  2178. EXT4_INFO_ATTR(lazy_itable_init);
  2179. EXT4_INFO_ATTR(batched_discard);
  2180. static struct attribute *ext4_feat_attrs[] = {
  2181. ATTR_LIST(lazy_itable_init),
  2182. ATTR_LIST(batched_discard),
  2183. NULL,
  2184. };
  2185. static ssize_t ext4_attr_show(struct kobject *kobj,
  2186. struct attribute *attr, char *buf)
  2187. {
  2188. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2189. s_kobj);
  2190. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2191. return a->show ? a->show(a, sbi, buf) : 0;
  2192. }
  2193. static ssize_t ext4_attr_store(struct kobject *kobj,
  2194. struct attribute *attr,
  2195. const char *buf, size_t len)
  2196. {
  2197. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2198. s_kobj);
  2199. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2200. return a->store ? a->store(a, sbi, buf, len) : 0;
  2201. }
  2202. static void ext4_sb_release(struct kobject *kobj)
  2203. {
  2204. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2205. s_kobj);
  2206. complete(&sbi->s_kobj_unregister);
  2207. }
  2208. static const struct sysfs_ops ext4_attr_ops = {
  2209. .show = ext4_attr_show,
  2210. .store = ext4_attr_store,
  2211. };
  2212. static struct kobj_type ext4_ktype = {
  2213. .default_attrs = ext4_attrs,
  2214. .sysfs_ops = &ext4_attr_ops,
  2215. .release = ext4_sb_release,
  2216. };
  2217. static void ext4_feat_release(struct kobject *kobj)
  2218. {
  2219. complete(&ext4_feat->f_kobj_unregister);
  2220. }
  2221. static struct kobj_type ext4_feat_ktype = {
  2222. .default_attrs = ext4_feat_attrs,
  2223. .sysfs_ops = &ext4_attr_ops,
  2224. .release = ext4_feat_release,
  2225. };
  2226. /*
  2227. * Check whether this filesystem can be mounted based on
  2228. * the features present and the RDONLY/RDWR mount requested.
  2229. * Returns 1 if this filesystem can be mounted as requested,
  2230. * 0 if it cannot be.
  2231. */
  2232. static int ext4_feature_set_ok(struct super_block *sb, int readonly)
  2233. {
  2234. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
  2235. ext4_msg(sb, KERN_ERR,
  2236. "Couldn't mount because of "
  2237. "unsupported optional features (%x)",
  2238. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
  2239. ~EXT4_FEATURE_INCOMPAT_SUPP));
  2240. return 0;
  2241. }
  2242. if (readonly)
  2243. return 1;
  2244. /* Check that feature set is OK for a read-write mount */
  2245. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
  2246. ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
  2247. "unsupported optional features (%x)",
  2248. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
  2249. ~EXT4_FEATURE_RO_COMPAT_SUPP));
  2250. return 0;
  2251. }
  2252. /*
  2253. * Large file size enabled file system can only be mounted
  2254. * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
  2255. */
  2256. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  2257. if (sizeof(blkcnt_t) < sizeof(u64)) {
  2258. ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
  2259. "cannot be mounted RDWR without "
  2260. "CONFIG_LBDAF");
  2261. return 0;
  2262. }
  2263. }
  2264. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
  2265. !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  2266. ext4_msg(sb, KERN_ERR,
  2267. "Can't support bigalloc feature without "
  2268. "extents feature\n");
  2269. return 0;
  2270. }
  2271. return 1;
  2272. }
  2273. /*
  2274. * This function is called once a day if we have errors logged
  2275. * on the file system
  2276. */
  2277. static void print_daily_error_info(unsigned long arg)
  2278. {
  2279. struct super_block *sb = (struct super_block *) arg;
  2280. struct ext4_sb_info *sbi;
  2281. struct ext4_super_block *es;
  2282. sbi = EXT4_SB(sb);
  2283. es = sbi->s_es;
  2284. if (es->s_error_count)
  2285. ext4_msg(sb, KERN_NOTICE, "error count: %u",
  2286. le32_to_cpu(es->s_error_count));
  2287. if (es->s_first_error_time) {
  2288. printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
  2289. sb->s_id, le32_to_cpu(es->s_first_error_time),
  2290. (int) sizeof(es->s_first_error_func),
  2291. es->s_first_error_func,
  2292. le32_to_cpu(es->s_first_error_line));
  2293. if (es->s_first_error_ino)
  2294. printk(": inode %u",
  2295. le32_to_cpu(es->s_first_error_ino));
  2296. if (es->s_first_error_block)
  2297. printk(": block %llu", (unsigned long long)
  2298. le64_to_cpu(es->s_first_error_block));
  2299. printk("\n");
  2300. }
  2301. if (es->s_last_error_time) {
  2302. printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
  2303. sb->s_id, le32_to_cpu(es->s_last_error_time),
  2304. (int) sizeof(es->s_last_error_func),
  2305. es->s_last_error_func,
  2306. le32_to_cpu(es->s_last_error_line));
  2307. if (es->s_last_error_ino)
  2308. printk(": inode %u",
  2309. le32_to_cpu(es->s_last_error_ino));
  2310. if (es->s_last_error_block)
  2311. printk(": block %llu", (unsigned long long)
  2312. le64_to_cpu(es->s_last_error_block));
  2313. printk("\n");
  2314. }
  2315. mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
  2316. }
  2317. /* Find next suitable group and run ext4_init_inode_table */
  2318. static int ext4_run_li_request(struct ext4_li_request *elr)
  2319. {
  2320. struct ext4_group_desc *gdp = NULL;
  2321. ext4_group_t group, ngroups;
  2322. struct super_block *sb;
  2323. unsigned long timeout = 0;
  2324. int ret = 0;
  2325. sb = elr->lr_super;
  2326. ngroups = EXT4_SB(sb)->s_groups_count;
  2327. for (group = elr->lr_next_group; group < ngroups; group++) {
  2328. gdp = ext4_get_group_desc(sb, group, NULL);
  2329. if (!gdp) {
  2330. ret = 1;
  2331. break;
  2332. }
  2333. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2334. break;
  2335. }
  2336. if (group == ngroups)
  2337. ret = 1;
  2338. if (!ret) {
  2339. timeout = jiffies;
  2340. ret = ext4_init_inode_table(sb, group,
  2341. elr->lr_timeout ? 0 : 1);
  2342. if (elr->lr_timeout == 0) {
  2343. timeout = (jiffies - timeout) *
  2344. elr->lr_sbi->s_li_wait_mult;
  2345. elr->lr_timeout = timeout;
  2346. }
  2347. elr->lr_next_sched = jiffies + elr->lr_timeout;
  2348. elr->lr_next_group = group + 1;
  2349. }
  2350. return ret;
  2351. }
  2352. /*
  2353. * Remove lr_request from the list_request and free the
  2354. * request structure. Should be called with li_list_mtx held
  2355. */
  2356. static void ext4_remove_li_request(struct ext4_li_request *elr)
  2357. {
  2358. struct ext4_sb_info *sbi;
  2359. if (!elr)
  2360. return;
  2361. sbi = elr->lr_sbi;
  2362. list_del(&elr->lr_request);
  2363. sbi->s_li_request = NULL;
  2364. kfree(elr);
  2365. }
  2366. static void ext4_unregister_li_request(struct super_block *sb)
  2367. {
  2368. mutex_lock(&ext4_li_mtx);
  2369. if (!ext4_li_info) {
  2370. mutex_unlock(&ext4_li_mtx);
  2371. return;
  2372. }
  2373. mutex_lock(&ext4_li_info->li_list_mtx);
  2374. ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
  2375. mutex_unlock(&ext4_li_info->li_list_mtx);
  2376. mutex_unlock(&ext4_li_mtx);
  2377. }
  2378. static struct task_struct *ext4_lazyinit_task;
  2379. /*
  2380. * This is the function where ext4lazyinit thread lives. It walks
  2381. * through the request list searching for next scheduled filesystem.
  2382. * When such a fs is found, run the lazy initialization request
  2383. * (ext4_rn_li_request) and keep track of the time spend in this
  2384. * function. Based on that time we compute next schedule time of
  2385. * the request. When walking through the list is complete, compute
  2386. * next waking time and put itself into sleep.
  2387. */
  2388. static int ext4_lazyinit_thread(void *arg)
  2389. {
  2390. struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
  2391. struct list_head *pos, *n;
  2392. struct ext4_li_request *elr;
  2393. unsigned long next_wakeup, cur;
  2394. BUG_ON(NULL == eli);
  2395. cont_thread:
  2396. while (true) {
  2397. next_wakeup = MAX_JIFFY_OFFSET;
  2398. mutex_lock(&eli->li_list_mtx);
  2399. if (list_empty(&eli->li_request_list)) {
  2400. mutex_unlock(&eli->li_list_mtx);
  2401. goto exit_thread;
  2402. }
  2403. list_for_each_safe(pos, n, &eli->li_request_list) {
  2404. elr = list_entry(pos, struct ext4_li_request,
  2405. lr_request);
  2406. if (time_after_eq(jiffies, elr->lr_next_sched)) {
  2407. if (ext4_run_li_request(elr) != 0) {
  2408. /* error, remove the lazy_init job */
  2409. ext4_remove_li_request(elr);
  2410. continue;
  2411. }
  2412. }
  2413. if (time_before(elr->lr_next_sched, next_wakeup))
  2414. next_wakeup = elr->lr_next_sched;
  2415. }
  2416. mutex_unlock(&eli->li_list_mtx);
  2417. try_to_freeze();
  2418. cur = jiffies;
  2419. if ((time_after_eq(cur, next_wakeup)) ||
  2420. (MAX_JIFFY_OFFSET == next_wakeup)) {
  2421. cond_resched();
  2422. continue;
  2423. }
  2424. schedule_timeout_interruptible(next_wakeup - cur);
  2425. if (kthread_should_stop()) {
  2426. ext4_clear_request_list();
  2427. goto exit_thread;
  2428. }
  2429. }
  2430. exit_thread:
  2431. /*
  2432. * It looks like the request list is empty, but we need
  2433. * to check it under the li_list_mtx lock, to prevent any
  2434. * additions into it, and of course we should lock ext4_li_mtx
  2435. * to atomically free the list and ext4_li_info, because at
  2436. * this point another ext4 filesystem could be registering
  2437. * new one.
  2438. */
  2439. mutex_lock(&ext4_li_mtx);
  2440. mutex_lock(&eli->li_list_mtx);
  2441. if (!list_empty(&eli->li_request_list)) {
  2442. mutex_unlock(&eli->li_list_mtx);
  2443. mutex_unlock(&ext4_li_mtx);
  2444. goto cont_thread;
  2445. }
  2446. mutex_unlock(&eli->li_list_mtx);
  2447. kfree(ext4_li_info);
  2448. ext4_li_info = NULL;
  2449. mutex_unlock(&ext4_li_mtx);
  2450. return 0;
  2451. }
  2452. static void ext4_clear_request_list(void)
  2453. {
  2454. struct list_head *pos, *n;
  2455. struct ext4_li_request *elr;
  2456. mutex_lock(&ext4_li_info->li_list_mtx);
  2457. list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
  2458. elr = list_entry(pos, struct ext4_li_request,
  2459. lr_request);
  2460. ext4_remove_li_request(elr);
  2461. }
  2462. mutex_unlock(&ext4_li_info->li_list_mtx);
  2463. }
  2464. static int ext4_run_lazyinit_thread(void)
  2465. {
  2466. ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
  2467. ext4_li_info, "ext4lazyinit");
  2468. if (IS_ERR(ext4_lazyinit_task)) {
  2469. int err = PTR_ERR(ext4_lazyinit_task);
  2470. ext4_clear_request_list();
  2471. kfree(ext4_li_info);
  2472. ext4_li_info = NULL;
  2473. printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
  2474. "initialization thread\n",
  2475. err);
  2476. return err;
  2477. }
  2478. ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
  2479. return 0;
  2480. }
  2481. /*
  2482. * Check whether it make sense to run itable init. thread or not.
  2483. * If there is at least one uninitialized inode table, return
  2484. * corresponding group number, else the loop goes through all
  2485. * groups and return total number of groups.
  2486. */
  2487. static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
  2488. {
  2489. ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
  2490. struct ext4_group_desc *gdp = NULL;
  2491. for (group = 0; group < ngroups; group++) {
  2492. gdp = ext4_get_group_desc(sb, group, NULL);
  2493. if (!gdp)
  2494. continue;
  2495. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2496. break;
  2497. }
  2498. return group;
  2499. }
  2500. static int ext4_li_info_new(void)
  2501. {
  2502. struct ext4_lazy_init *eli = NULL;
  2503. eli = kzalloc(sizeof(*eli), GFP_KERNEL);
  2504. if (!eli)
  2505. return -ENOMEM;
  2506. INIT_LIST_HEAD(&eli->li_request_list);
  2507. mutex_init(&eli->li_list_mtx);
  2508. eli->li_state |= EXT4_LAZYINIT_QUIT;
  2509. ext4_li_info = eli;
  2510. return 0;
  2511. }
  2512. static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
  2513. ext4_group_t start)
  2514. {
  2515. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2516. struct ext4_li_request *elr;
  2517. unsigned long rnd;
  2518. elr = kzalloc(sizeof(*elr), GFP_KERNEL);
  2519. if (!elr)
  2520. return NULL;
  2521. elr->lr_super = sb;
  2522. elr->lr_sbi = sbi;
  2523. elr->lr_next_group = start;
  2524. /*
  2525. * Randomize first schedule time of the request to
  2526. * spread the inode table initialization requests
  2527. * better.
  2528. */
  2529. get_random_bytes(&rnd, sizeof(rnd));
  2530. elr->lr_next_sched = jiffies + (unsigned long)rnd %
  2531. (EXT4_DEF_LI_MAX_START_DELAY * HZ);
  2532. return elr;
  2533. }
  2534. static int ext4_register_li_request(struct super_block *sb,
  2535. ext4_group_t first_not_zeroed)
  2536. {
  2537. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2538. struct ext4_li_request *elr;
  2539. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  2540. int ret = 0;
  2541. if (sbi->s_li_request != NULL) {
  2542. /*
  2543. * Reset timeout so it can be computed again, because
  2544. * s_li_wait_mult might have changed.
  2545. */
  2546. sbi->s_li_request->lr_timeout = 0;
  2547. return 0;
  2548. }
  2549. if (first_not_zeroed == ngroups ||
  2550. (sb->s_flags & MS_RDONLY) ||
  2551. !test_opt(sb, INIT_INODE_TABLE))
  2552. return 0;
  2553. elr = ext4_li_request_new(sb, first_not_zeroed);
  2554. if (!elr)
  2555. return -ENOMEM;
  2556. mutex_lock(&ext4_li_mtx);
  2557. if (NULL == ext4_li_info) {
  2558. ret = ext4_li_info_new();
  2559. if (ret)
  2560. goto out;
  2561. }
  2562. mutex_lock(&ext4_li_info->li_list_mtx);
  2563. list_add(&elr->lr_request, &ext4_li_info->li_request_list);
  2564. mutex_unlock(&ext4_li_info->li_list_mtx);
  2565. sbi->s_li_request = elr;
  2566. /*
  2567. * set elr to NULL here since it has been inserted to
  2568. * the request_list and the removal and free of it is
  2569. * handled by ext4_clear_request_list from now on.
  2570. */
  2571. elr = NULL;
  2572. if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
  2573. ret = ext4_run_lazyinit_thread();
  2574. if (ret)
  2575. goto out;
  2576. }
  2577. out:
  2578. mutex_unlock(&ext4_li_mtx);
  2579. if (ret)
  2580. kfree(elr);
  2581. return ret;
  2582. }
  2583. /*
  2584. * We do not need to lock anything since this is called on
  2585. * module unload.
  2586. */
  2587. static void ext4_destroy_lazyinit_thread(void)
  2588. {
  2589. /*
  2590. * If thread exited earlier
  2591. * there's nothing to be done.
  2592. */
  2593. if (!ext4_li_info || !ext4_lazyinit_task)
  2594. return;
  2595. kthread_stop(ext4_lazyinit_task);
  2596. }
  2597. static int ext4_fill_super(struct super_block *sb, void *data, int silent)
  2598. {
  2599. char *orig_data = kstrdup(data, GFP_KERNEL);
  2600. struct buffer_head *bh;
  2601. struct ext4_super_block *es = NULL;
  2602. struct ext4_sb_info *sbi;
  2603. ext4_fsblk_t block;
  2604. ext4_fsblk_t sb_block = get_sb_block(&data);
  2605. ext4_fsblk_t logical_sb_block;
  2606. unsigned long offset = 0;
  2607. unsigned long journal_devnum = 0;
  2608. unsigned long def_mount_opts;
  2609. struct inode *root;
  2610. char *cp;
  2611. const char *descr;
  2612. int ret = -ENOMEM;
  2613. int blocksize, clustersize;
  2614. unsigned int db_count;
  2615. unsigned int i;
  2616. int needs_recovery, has_huge_files, has_bigalloc;
  2617. __u64 blocks_count;
  2618. int err;
  2619. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  2620. ext4_group_t first_not_zeroed;
  2621. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  2622. if (!sbi)
  2623. goto out_free_orig;
  2624. sbi->s_blockgroup_lock =
  2625. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  2626. if (!sbi->s_blockgroup_lock) {
  2627. kfree(sbi);
  2628. goto out_free_orig;
  2629. }
  2630. sb->s_fs_info = sbi;
  2631. sbi->s_mount_opt = 0;
  2632. sbi->s_resuid = EXT4_DEF_RESUID;
  2633. sbi->s_resgid = EXT4_DEF_RESGID;
  2634. sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
  2635. sbi->s_sb_block = sb_block;
  2636. if (sb->s_bdev->bd_part)
  2637. sbi->s_sectors_written_start =
  2638. part_stat_read(sb->s_bdev->bd_part, sectors[1]);
  2639. /* Cleanup superblock name */
  2640. for (cp = sb->s_id; (cp = strchr(cp, '/'));)
  2641. *cp = '!';
  2642. ret = -EINVAL;
  2643. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  2644. if (!blocksize) {
  2645. ext4_msg(sb, KERN_ERR, "unable to set blocksize");
  2646. goto out_fail;
  2647. }
  2648. /*
  2649. * The ext4 superblock will not be buffer aligned for other than 1kB
  2650. * block sizes. We need to calculate the offset from buffer start.
  2651. */
  2652. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  2653. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2654. offset = do_div(logical_sb_block, blocksize);
  2655. } else {
  2656. logical_sb_block = sb_block;
  2657. }
  2658. if (!(bh = sb_bread(sb, logical_sb_block))) {
  2659. ext4_msg(sb, KERN_ERR, "unable to read superblock");
  2660. goto out_fail;
  2661. }
  2662. /*
  2663. * Note: s_es must be initialized as soon as possible because
  2664. * some ext4 macro-instructions depend on its value
  2665. */
  2666. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  2667. sbi->s_es = es;
  2668. sb->s_magic = le16_to_cpu(es->s_magic);
  2669. if (sb->s_magic != EXT4_SUPER_MAGIC)
  2670. goto cantfind_ext4;
  2671. sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
  2672. /* Set defaults before we parse the mount options */
  2673. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  2674. set_opt(sb, INIT_INODE_TABLE);
  2675. if (def_mount_opts & EXT4_DEFM_DEBUG)
  2676. set_opt(sb, DEBUG);
  2677. if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
  2678. set_opt(sb, GRPID);
  2679. if (def_mount_opts & EXT4_DEFM_UID16)
  2680. set_opt(sb, NO_UID32);
  2681. /* xattr user namespace & acls are now defaulted on */
  2682. #ifdef CONFIG_EXT4_FS_XATTR
  2683. set_opt(sb, XATTR_USER);
  2684. #endif
  2685. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  2686. set_opt(sb, POSIX_ACL);
  2687. #endif
  2688. set_opt(sb, MBLK_IO_SUBMIT);
  2689. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  2690. set_opt(sb, JOURNAL_DATA);
  2691. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  2692. set_opt(sb, ORDERED_DATA);
  2693. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  2694. set_opt(sb, WRITEBACK_DATA);
  2695. if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
  2696. set_opt(sb, ERRORS_PANIC);
  2697. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
  2698. set_opt(sb, ERRORS_CONT);
  2699. else
  2700. set_opt(sb, ERRORS_RO);
  2701. if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
  2702. set_opt(sb, BLOCK_VALIDITY);
  2703. if (def_mount_opts & EXT4_DEFM_DISCARD)
  2704. set_opt(sb, DISCARD);
  2705. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  2706. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  2707. sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
  2708. sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
  2709. sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
  2710. if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
  2711. set_opt(sb, BARRIER);
  2712. /*
  2713. * enable delayed allocation by default
  2714. * Use -o nodelalloc to turn it off
  2715. */
  2716. if (!IS_EXT3_SB(sb) &&
  2717. ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
  2718. set_opt(sb, DELALLOC);
  2719. /*
  2720. * set default s_li_wait_mult for lazyinit, for the case there is
  2721. * no mount option specified.
  2722. */
  2723. sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
  2724. if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
  2725. &journal_devnum, &journal_ioprio, 0)) {
  2726. ext4_msg(sb, KERN_WARNING,
  2727. "failed to parse options in superblock: %s",
  2728. sbi->s_es->s_mount_opts);
  2729. }
  2730. sbi->s_def_mount_opt = sbi->s_mount_opt;
  2731. if (!parse_options((char *) data, sb, &journal_devnum,
  2732. &journal_ioprio, 0))
  2733. goto failed_mount;
  2734. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  2735. printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
  2736. "with data=journal disables delayed "
  2737. "allocation and O_DIRECT support!\n");
  2738. if (test_opt2(sb, EXPLICIT_DELALLOC)) {
  2739. ext4_msg(sb, KERN_ERR, "can't mount with "
  2740. "both data=journal and delalloc");
  2741. goto failed_mount;
  2742. }
  2743. if (test_opt(sb, DIOREAD_NOLOCK)) {
  2744. ext4_msg(sb, KERN_ERR, "can't mount with "
  2745. "both data=journal and delalloc");
  2746. goto failed_mount;
  2747. }
  2748. if (test_opt(sb, DELALLOC))
  2749. clear_opt(sb, DELALLOC);
  2750. }
  2751. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  2752. if (test_opt(sb, DIOREAD_NOLOCK)) {
  2753. if (blocksize < PAGE_SIZE) {
  2754. ext4_msg(sb, KERN_ERR, "can't mount with "
  2755. "dioread_nolock if block size != PAGE_SIZE");
  2756. goto failed_mount;
  2757. }
  2758. }
  2759. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2760. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  2761. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  2762. (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
  2763. EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  2764. EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  2765. ext4_msg(sb, KERN_WARNING,
  2766. "feature flags set on rev 0 fs, "
  2767. "running e2fsck is recommended");
  2768. if (IS_EXT2_SB(sb)) {
  2769. if (ext2_feature_set_ok(sb))
  2770. ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
  2771. "using the ext4 subsystem");
  2772. else {
  2773. ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
  2774. "to feature incompatibilities");
  2775. goto failed_mount;
  2776. }
  2777. }
  2778. if (IS_EXT3_SB(sb)) {
  2779. if (ext3_feature_set_ok(sb))
  2780. ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
  2781. "using the ext4 subsystem");
  2782. else {
  2783. ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
  2784. "to feature incompatibilities");
  2785. goto failed_mount;
  2786. }
  2787. }
  2788. /*
  2789. * Check feature flags regardless of the revision level, since we
  2790. * previously didn't change the revision level when setting the flags,
  2791. * so there is a chance incompat flags are set on a rev 0 filesystem.
  2792. */
  2793. if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
  2794. goto failed_mount;
  2795. if (blocksize < EXT4_MIN_BLOCK_SIZE ||
  2796. blocksize > EXT4_MAX_BLOCK_SIZE) {
  2797. ext4_msg(sb, KERN_ERR,
  2798. "Unsupported filesystem blocksize %d", blocksize);
  2799. goto failed_mount;
  2800. }
  2801. if (sb->s_blocksize != blocksize) {
  2802. /* Validate the filesystem blocksize */
  2803. if (!sb_set_blocksize(sb, blocksize)) {
  2804. ext4_msg(sb, KERN_ERR, "bad block size %d",
  2805. blocksize);
  2806. goto failed_mount;
  2807. }
  2808. brelse(bh);
  2809. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2810. offset = do_div(logical_sb_block, blocksize);
  2811. bh = sb_bread(sb, logical_sb_block);
  2812. if (!bh) {
  2813. ext4_msg(sb, KERN_ERR,
  2814. "Can't read superblock on 2nd try");
  2815. goto failed_mount;
  2816. }
  2817. es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
  2818. sbi->s_es = es;
  2819. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  2820. ext4_msg(sb, KERN_ERR,
  2821. "Magic mismatch, very weird!");
  2822. goto failed_mount;
  2823. }
  2824. }
  2825. has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2826. EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
  2827. sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
  2828. has_huge_files);
  2829. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
  2830. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  2831. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  2832. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  2833. } else {
  2834. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  2835. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  2836. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  2837. (!is_power_of_2(sbi->s_inode_size)) ||
  2838. (sbi->s_inode_size > blocksize)) {
  2839. ext4_msg(sb, KERN_ERR,
  2840. "unsupported inode size: %d",
  2841. sbi->s_inode_size);
  2842. goto failed_mount;
  2843. }
  2844. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
  2845. sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
  2846. }
  2847. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  2848. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
  2849. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  2850. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  2851. !is_power_of_2(sbi->s_desc_size)) {
  2852. ext4_msg(sb, KERN_ERR,
  2853. "unsupported descriptor size %lu",
  2854. sbi->s_desc_size);
  2855. goto failed_mount;
  2856. }
  2857. } else
  2858. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  2859. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  2860. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  2861. if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
  2862. goto cantfind_ext4;
  2863. sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
  2864. if (sbi->s_inodes_per_block == 0)
  2865. goto cantfind_ext4;
  2866. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  2867. sbi->s_inodes_per_block;
  2868. sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
  2869. sbi->s_sbh = bh;
  2870. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2871. sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
  2872. sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
  2873. for (i = 0; i < 4; i++)
  2874. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  2875. sbi->s_def_hash_version = es->s_def_hash_version;
  2876. i = le32_to_cpu(es->s_flags);
  2877. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  2878. sbi->s_hash_unsigned = 3;
  2879. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  2880. #ifdef __CHAR_UNSIGNED__
  2881. es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  2882. sbi->s_hash_unsigned = 3;
  2883. #else
  2884. es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  2885. #endif
  2886. }
  2887. /* Handle clustersize */
  2888. clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
  2889. has_bigalloc = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2890. EXT4_FEATURE_RO_COMPAT_BIGALLOC);
  2891. if (has_bigalloc) {
  2892. if (clustersize < blocksize) {
  2893. ext4_msg(sb, KERN_ERR,
  2894. "cluster size (%d) smaller than "
  2895. "block size (%d)", clustersize, blocksize);
  2896. goto failed_mount;
  2897. }
  2898. sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
  2899. le32_to_cpu(es->s_log_block_size);
  2900. sbi->s_clusters_per_group =
  2901. le32_to_cpu(es->s_clusters_per_group);
  2902. if (sbi->s_clusters_per_group > blocksize * 8) {
  2903. ext4_msg(sb, KERN_ERR,
  2904. "#clusters per group too big: %lu",
  2905. sbi->s_clusters_per_group);
  2906. goto failed_mount;
  2907. }
  2908. if (sbi->s_blocks_per_group !=
  2909. (sbi->s_clusters_per_group * (clustersize / blocksize))) {
  2910. ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
  2911. "clusters per group (%lu) inconsistent",
  2912. sbi->s_blocks_per_group,
  2913. sbi->s_clusters_per_group);
  2914. goto failed_mount;
  2915. }
  2916. } else {
  2917. if (clustersize != blocksize) {
  2918. ext4_warning(sb, "fragment/cluster size (%d) != "
  2919. "block size (%d)", clustersize,
  2920. blocksize);
  2921. clustersize = blocksize;
  2922. }
  2923. if (sbi->s_blocks_per_group > blocksize * 8) {
  2924. ext4_msg(sb, KERN_ERR,
  2925. "#blocks per group too big: %lu",
  2926. sbi->s_blocks_per_group);
  2927. goto failed_mount;
  2928. }
  2929. sbi->s_clusters_per_group = sbi->s_blocks_per_group;
  2930. sbi->s_cluster_bits = 0;
  2931. }
  2932. sbi->s_cluster_ratio = clustersize / blocksize;
  2933. if (sbi->s_inodes_per_group > blocksize * 8) {
  2934. ext4_msg(sb, KERN_ERR,
  2935. "#inodes per group too big: %lu",
  2936. sbi->s_inodes_per_group);
  2937. goto failed_mount;
  2938. }
  2939. /*
  2940. * Test whether we have more sectors than will fit in sector_t,
  2941. * and whether the max offset is addressable by the page cache.
  2942. */
  2943. err = generic_check_addressable(sb->s_blocksize_bits,
  2944. ext4_blocks_count(es));
  2945. if (err) {
  2946. ext4_msg(sb, KERN_ERR, "filesystem"
  2947. " too large to mount safely on this system");
  2948. if (sizeof(sector_t) < 8)
  2949. ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
  2950. ret = err;
  2951. goto failed_mount;
  2952. }
  2953. if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
  2954. goto cantfind_ext4;
  2955. /* check blocks count against device size */
  2956. blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  2957. if (blocks_count && ext4_blocks_count(es) > blocks_count) {
  2958. ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
  2959. "exceeds size of device (%llu blocks)",
  2960. ext4_blocks_count(es), blocks_count);
  2961. goto failed_mount;
  2962. }
  2963. /*
  2964. * It makes no sense for the first data block to be beyond the end
  2965. * of the filesystem.
  2966. */
  2967. if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
  2968. ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
  2969. "block %u is beyond end of filesystem (%llu)",
  2970. le32_to_cpu(es->s_first_data_block),
  2971. ext4_blocks_count(es));
  2972. goto failed_mount;
  2973. }
  2974. blocks_count = (ext4_blocks_count(es) -
  2975. le32_to_cpu(es->s_first_data_block) +
  2976. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  2977. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  2978. if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
  2979. ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
  2980. "(block count %llu, first data block %u, "
  2981. "blocks per group %lu)", sbi->s_groups_count,
  2982. ext4_blocks_count(es),
  2983. le32_to_cpu(es->s_first_data_block),
  2984. EXT4_BLOCKS_PER_GROUP(sb));
  2985. goto failed_mount;
  2986. }
  2987. sbi->s_groups_count = blocks_count;
  2988. sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
  2989. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  2990. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  2991. EXT4_DESC_PER_BLOCK(sb);
  2992. sbi->s_group_desc = ext4_kvmalloc(db_count *
  2993. sizeof(struct buffer_head *),
  2994. GFP_KERNEL);
  2995. if (sbi->s_group_desc == NULL) {
  2996. ext4_msg(sb, KERN_ERR, "not enough memory");
  2997. goto failed_mount;
  2998. }
  2999. if (ext4_proc_root)
  3000. sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
  3001. if (sbi->s_proc)
  3002. proc_create_data("options", S_IRUGO, sbi->s_proc,
  3003. &ext4_seq_options_fops, sb);
  3004. bgl_lock_init(sbi->s_blockgroup_lock);
  3005. for (i = 0; i < db_count; i++) {
  3006. block = descriptor_loc(sb, logical_sb_block, i);
  3007. sbi->s_group_desc[i] = sb_bread(sb, block);
  3008. if (!sbi->s_group_desc[i]) {
  3009. ext4_msg(sb, KERN_ERR,
  3010. "can't read group descriptor %d", i);
  3011. db_count = i;
  3012. goto failed_mount2;
  3013. }
  3014. }
  3015. if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
  3016. ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
  3017. goto failed_mount2;
  3018. }
  3019. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
  3020. if (!ext4_fill_flex_info(sb)) {
  3021. ext4_msg(sb, KERN_ERR,
  3022. "unable to initialize "
  3023. "flex_bg meta info!");
  3024. goto failed_mount2;
  3025. }
  3026. sbi->s_gdb_count = db_count;
  3027. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  3028. spin_lock_init(&sbi->s_next_gen_lock);
  3029. init_timer(&sbi->s_err_report);
  3030. sbi->s_err_report.function = print_daily_error_info;
  3031. sbi->s_err_report.data = (unsigned long) sb;
  3032. err = percpu_counter_init(&sbi->s_freeclusters_counter,
  3033. ext4_count_free_clusters(sb));
  3034. if (!err) {
  3035. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  3036. ext4_count_free_inodes(sb));
  3037. }
  3038. if (!err) {
  3039. err = percpu_counter_init(&sbi->s_dirs_counter,
  3040. ext4_count_dirs(sb));
  3041. }
  3042. if (!err) {
  3043. err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0);
  3044. }
  3045. if (err) {
  3046. ext4_msg(sb, KERN_ERR, "insufficient memory");
  3047. goto failed_mount3;
  3048. }
  3049. sbi->s_stripe = ext4_get_stripe_size(sbi);
  3050. sbi->s_max_writeback_mb_bump = 128;
  3051. /*
  3052. * set up enough so that it can read an inode
  3053. */
  3054. if (!test_opt(sb, NOLOAD) &&
  3055. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  3056. sb->s_op = &ext4_sops;
  3057. else
  3058. sb->s_op = &ext4_nojournal_sops;
  3059. sb->s_export_op = &ext4_export_ops;
  3060. sb->s_xattr = ext4_xattr_handlers;
  3061. #ifdef CONFIG_QUOTA
  3062. sb->s_qcop = &ext4_qctl_operations;
  3063. sb->dq_op = &ext4_quota_operations;
  3064. #endif
  3065. memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
  3066. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  3067. mutex_init(&sbi->s_orphan_lock);
  3068. sbi->s_resize_flags = 0;
  3069. sb->s_root = NULL;
  3070. needs_recovery = (es->s_last_orphan != 0 ||
  3071. EXT4_HAS_INCOMPAT_FEATURE(sb,
  3072. EXT4_FEATURE_INCOMPAT_RECOVER));
  3073. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
  3074. !(sb->s_flags & MS_RDONLY))
  3075. if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
  3076. goto failed_mount3;
  3077. /*
  3078. * The first inode we look at is the journal inode. Don't try
  3079. * root first: it may be modified in the journal!
  3080. */
  3081. if (!test_opt(sb, NOLOAD) &&
  3082. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3083. if (ext4_load_journal(sb, es, journal_devnum))
  3084. goto failed_mount3;
  3085. } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
  3086. EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  3087. ext4_msg(sb, KERN_ERR, "required journal recovery "
  3088. "suppressed and not mounted read-only");
  3089. goto failed_mount_wq;
  3090. } else {
  3091. clear_opt(sb, DATA_FLAGS);
  3092. sbi->s_journal = NULL;
  3093. needs_recovery = 0;
  3094. goto no_journal;
  3095. }
  3096. if (ext4_blocks_count(es) > 0xffffffffULL &&
  3097. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  3098. JBD2_FEATURE_INCOMPAT_64BIT)) {
  3099. ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
  3100. goto failed_mount_wq;
  3101. }
  3102. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  3103. jbd2_journal_set_features(sbi->s_journal,
  3104. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  3105. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3106. } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
  3107. jbd2_journal_set_features(sbi->s_journal,
  3108. JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
  3109. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  3110. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3111. } else {
  3112. jbd2_journal_clear_features(sbi->s_journal,
  3113. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  3114. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3115. }
  3116. /* We have now updated the journal if required, so we can
  3117. * validate the data journaling mode. */
  3118. switch (test_opt(sb, DATA_FLAGS)) {
  3119. case 0:
  3120. /* No mode set, assume a default based on the journal
  3121. * capabilities: ORDERED_DATA if the journal can
  3122. * cope, else JOURNAL_DATA
  3123. */
  3124. if (jbd2_journal_check_available_features
  3125. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
  3126. set_opt(sb, ORDERED_DATA);
  3127. else
  3128. set_opt(sb, JOURNAL_DATA);
  3129. break;
  3130. case EXT4_MOUNT_ORDERED_DATA:
  3131. case EXT4_MOUNT_WRITEBACK_DATA:
  3132. if (!jbd2_journal_check_available_features
  3133. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  3134. ext4_msg(sb, KERN_ERR, "Journal does not support "
  3135. "requested data journaling mode");
  3136. goto failed_mount_wq;
  3137. }
  3138. default:
  3139. break;
  3140. }
  3141. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3142. sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
  3143. /*
  3144. * The journal may have updated the bg summary counts, so we
  3145. * need to update the global counters.
  3146. */
  3147. percpu_counter_set(&sbi->s_freeclusters_counter,
  3148. ext4_count_free_clusters(sb));
  3149. percpu_counter_set(&sbi->s_freeinodes_counter,
  3150. ext4_count_free_inodes(sb));
  3151. percpu_counter_set(&sbi->s_dirs_counter,
  3152. ext4_count_dirs(sb));
  3153. percpu_counter_set(&sbi->s_dirtyclusters_counter, 0);
  3154. no_journal:
  3155. /*
  3156. * The maximum number of concurrent works can be high and
  3157. * concurrency isn't really necessary. Limit it to 1.
  3158. */
  3159. EXT4_SB(sb)->dio_unwritten_wq =
  3160. alloc_workqueue("ext4-dio-unwritten", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  3161. if (!EXT4_SB(sb)->dio_unwritten_wq) {
  3162. printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
  3163. goto failed_mount_wq;
  3164. }
  3165. /*
  3166. * The jbd2_journal_load will have done any necessary log recovery,
  3167. * so we can safely mount the rest of the filesystem now.
  3168. */
  3169. root = ext4_iget(sb, EXT4_ROOT_INO);
  3170. if (IS_ERR(root)) {
  3171. ext4_msg(sb, KERN_ERR, "get root inode failed");
  3172. ret = PTR_ERR(root);
  3173. root = NULL;
  3174. goto failed_mount4;
  3175. }
  3176. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  3177. ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
  3178. iput(root);
  3179. goto failed_mount4;
  3180. }
  3181. sb->s_root = d_make_root(root);
  3182. if (!sb->s_root) {
  3183. ext4_msg(sb, KERN_ERR, "get root dentry failed");
  3184. ret = -ENOMEM;
  3185. goto failed_mount4;
  3186. }
  3187. ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
  3188. /* determine the minimum size of new large inodes, if present */
  3189. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
  3190. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3191. EXT4_GOOD_OLD_INODE_SIZE;
  3192. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3193. EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
  3194. if (sbi->s_want_extra_isize <
  3195. le16_to_cpu(es->s_want_extra_isize))
  3196. sbi->s_want_extra_isize =
  3197. le16_to_cpu(es->s_want_extra_isize);
  3198. if (sbi->s_want_extra_isize <
  3199. le16_to_cpu(es->s_min_extra_isize))
  3200. sbi->s_want_extra_isize =
  3201. le16_to_cpu(es->s_min_extra_isize);
  3202. }
  3203. }
  3204. /* Check if enough inode space is available */
  3205. if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
  3206. sbi->s_inode_size) {
  3207. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3208. EXT4_GOOD_OLD_INODE_SIZE;
  3209. ext4_msg(sb, KERN_INFO, "required extra inode space not"
  3210. "available");
  3211. }
  3212. err = ext4_setup_system_zone(sb);
  3213. if (err) {
  3214. ext4_msg(sb, KERN_ERR, "failed to initialize system "
  3215. "zone (%d)", err);
  3216. goto failed_mount4a;
  3217. }
  3218. ext4_ext_init(sb);
  3219. err = ext4_mb_init(sb, needs_recovery);
  3220. if (err) {
  3221. ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
  3222. err);
  3223. goto failed_mount5;
  3224. }
  3225. err = ext4_register_li_request(sb, first_not_zeroed);
  3226. if (err)
  3227. goto failed_mount6;
  3228. sbi->s_kobj.kset = ext4_kset;
  3229. init_completion(&sbi->s_kobj_unregister);
  3230. err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
  3231. "%s", sb->s_id);
  3232. if (err)
  3233. goto failed_mount7;
  3234. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  3235. ext4_orphan_cleanup(sb, es);
  3236. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  3237. if (needs_recovery) {
  3238. ext4_msg(sb, KERN_INFO, "recovery complete");
  3239. ext4_mark_recovery_complete(sb, es);
  3240. }
  3241. if (EXT4_SB(sb)->s_journal) {
  3242. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  3243. descr = " journalled data mode";
  3244. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  3245. descr = " ordered data mode";
  3246. else
  3247. descr = " writeback data mode";
  3248. } else
  3249. descr = "out journal";
  3250. ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
  3251. "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
  3252. *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
  3253. if (es->s_error_count)
  3254. mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
  3255. kfree(orig_data);
  3256. return 0;
  3257. cantfind_ext4:
  3258. if (!silent)
  3259. ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
  3260. goto failed_mount;
  3261. failed_mount7:
  3262. ext4_unregister_li_request(sb);
  3263. failed_mount6:
  3264. ext4_mb_release(sb);
  3265. failed_mount5:
  3266. ext4_ext_release(sb);
  3267. ext4_release_system_zone(sb);
  3268. failed_mount4a:
  3269. dput(sb->s_root);
  3270. sb->s_root = NULL;
  3271. failed_mount4:
  3272. ext4_msg(sb, KERN_ERR, "mount failed");
  3273. destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
  3274. failed_mount_wq:
  3275. if (sbi->s_journal) {
  3276. jbd2_journal_destroy(sbi->s_journal);
  3277. sbi->s_journal = NULL;
  3278. }
  3279. failed_mount3:
  3280. del_timer(&sbi->s_err_report);
  3281. if (sbi->s_flex_groups)
  3282. ext4_kvfree(sbi->s_flex_groups);
  3283. percpu_counter_destroy(&sbi->s_freeclusters_counter);
  3284. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  3285. percpu_counter_destroy(&sbi->s_dirs_counter);
  3286. percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
  3287. if (sbi->s_mmp_tsk)
  3288. kthread_stop(sbi->s_mmp_tsk);
  3289. failed_mount2:
  3290. for (i = 0; i < db_count; i++)
  3291. brelse(sbi->s_group_desc[i]);
  3292. ext4_kvfree(sbi->s_group_desc);
  3293. failed_mount:
  3294. if (sbi->s_proc) {
  3295. remove_proc_entry("options", sbi->s_proc);
  3296. remove_proc_entry(sb->s_id, ext4_proc_root);
  3297. }
  3298. #ifdef CONFIG_QUOTA
  3299. for (i = 0; i < MAXQUOTAS; i++)
  3300. kfree(sbi->s_qf_names[i]);
  3301. #endif
  3302. ext4_blkdev_remove(sbi);
  3303. brelse(bh);
  3304. out_fail:
  3305. sb->s_fs_info = NULL;
  3306. kfree(sbi->s_blockgroup_lock);
  3307. kfree(sbi);
  3308. out_free_orig:
  3309. kfree(orig_data);
  3310. return ret;
  3311. }
  3312. /*
  3313. * Setup any per-fs journal parameters now. We'll do this both on
  3314. * initial mount, once the journal has been initialised but before we've
  3315. * done any recovery; and again on any subsequent remount.
  3316. */
  3317. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  3318. {
  3319. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3320. journal->j_commit_interval = sbi->s_commit_interval;
  3321. journal->j_min_batch_time = sbi->s_min_batch_time;
  3322. journal->j_max_batch_time = sbi->s_max_batch_time;
  3323. write_lock(&journal->j_state_lock);
  3324. if (test_opt(sb, BARRIER))
  3325. journal->j_flags |= JBD2_BARRIER;
  3326. else
  3327. journal->j_flags &= ~JBD2_BARRIER;
  3328. if (test_opt(sb, DATA_ERR_ABORT))
  3329. journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
  3330. else
  3331. journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
  3332. write_unlock(&journal->j_state_lock);
  3333. }
  3334. static journal_t *ext4_get_journal(struct super_block *sb,
  3335. unsigned int journal_inum)
  3336. {
  3337. struct inode *journal_inode;
  3338. journal_t *journal;
  3339. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3340. /* First, test for the existence of a valid inode on disk. Bad
  3341. * things happen if we iget() an unused inode, as the subsequent
  3342. * iput() will try to delete it. */
  3343. journal_inode = ext4_iget(sb, journal_inum);
  3344. if (IS_ERR(journal_inode)) {
  3345. ext4_msg(sb, KERN_ERR, "no journal found");
  3346. return NULL;
  3347. }
  3348. if (!journal_inode->i_nlink) {
  3349. make_bad_inode(journal_inode);
  3350. iput(journal_inode);
  3351. ext4_msg(sb, KERN_ERR, "journal inode is deleted");
  3352. return NULL;
  3353. }
  3354. jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
  3355. journal_inode, journal_inode->i_size);
  3356. if (!S_ISREG(journal_inode->i_mode)) {
  3357. ext4_msg(sb, KERN_ERR, "invalid journal inode");
  3358. iput(journal_inode);
  3359. return NULL;
  3360. }
  3361. journal = jbd2_journal_init_inode(journal_inode);
  3362. if (!journal) {
  3363. ext4_msg(sb, KERN_ERR, "Could not load journal inode");
  3364. iput(journal_inode);
  3365. return NULL;
  3366. }
  3367. journal->j_private = sb;
  3368. ext4_init_journal_params(sb, journal);
  3369. return journal;
  3370. }
  3371. static journal_t *ext4_get_dev_journal(struct super_block *sb,
  3372. dev_t j_dev)
  3373. {
  3374. struct buffer_head *bh;
  3375. journal_t *journal;
  3376. ext4_fsblk_t start;
  3377. ext4_fsblk_t len;
  3378. int hblock, blocksize;
  3379. ext4_fsblk_t sb_block;
  3380. unsigned long offset;
  3381. struct ext4_super_block *es;
  3382. struct block_device *bdev;
  3383. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3384. bdev = ext4_blkdev_get(j_dev, sb);
  3385. if (bdev == NULL)
  3386. return NULL;
  3387. blocksize = sb->s_blocksize;
  3388. hblock = bdev_logical_block_size(bdev);
  3389. if (blocksize < hblock) {
  3390. ext4_msg(sb, KERN_ERR,
  3391. "blocksize too small for journal device");
  3392. goto out_bdev;
  3393. }
  3394. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  3395. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  3396. set_blocksize(bdev, blocksize);
  3397. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  3398. ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
  3399. "external journal");
  3400. goto out_bdev;
  3401. }
  3402. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  3403. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  3404. !(le32_to_cpu(es->s_feature_incompat) &
  3405. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  3406. ext4_msg(sb, KERN_ERR, "external journal has "
  3407. "bad superblock");
  3408. brelse(bh);
  3409. goto out_bdev;
  3410. }
  3411. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  3412. ext4_msg(sb, KERN_ERR, "journal UUID does not match");
  3413. brelse(bh);
  3414. goto out_bdev;
  3415. }
  3416. len = ext4_blocks_count(es);
  3417. start = sb_block + 1;
  3418. brelse(bh); /* we're done with the superblock */
  3419. journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
  3420. start, len, blocksize);
  3421. if (!journal) {
  3422. ext4_msg(sb, KERN_ERR, "failed to create device journal");
  3423. goto out_bdev;
  3424. }
  3425. journal->j_private = sb;
  3426. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  3427. wait_on_buffer(journal->j_sb_buffer);
  3428. if (!buffer_uptodate(journal->j_sb_buffer)) {
  3429. ext4_msg(sb, KERN_ERR, "I/O error on journal device");
  3430. goto out_journal;
  3431. }
  3432. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  3433. ext4_msg(sb, KERN_ERR, "External journal has more than one "
  3434. "user (unsupported) - %d",
  3435. be32_to_cpu(journal->j_superblock->s_nr_users));
  3436. goto out_journal;
  3437. }
  3438. EXT4_SB(sb)->journal_bdev = bdev;
  3439. ext4_init_journal_params(sb, journal);
  3440. return journal;
  3441. out_journal:
  3442. jbd2_journal_destroy(journal);
  3443. out_bdev:
  3444. ext4_blkdev_put(bdev);
  3445. return NULL;
  3446. }
  3447. static int ext4_load_journal(struct super_block *sb,
  3448. struct ext4_super_block *es,
  3449. unsigned long journal_devnum)
  3450. {
  3451. journal_t *journal;
  3452. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  3453. dev_t journal_dev;
  3454. int err = 0;
  3455. int really_read_only;
  3456. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3457. if (journal_devnum &&
  3458. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3459. ext4_msg(sb, KERN_INFO, "external journal device major/minor "
  3460. "numbers have changed");
  3461. journal_dev = new_decode_dev(journal_devnum);
  3462. } else
  3463. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  3464. really_read_only = bdev_read_only(sb->s_bdev);
  3465. /*
  3466. * Are we loading a blank journal or performing recovery after a
  3467. * crash? For recovery, we need to check in advance whether we
  3468. * can get read-write access to the device.
  3469. */
  3470. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  3471. if (sb->s_flags & MS_RDONLY) {
  3472. ext4_msg(sb, KERN_INFO, "INFO: recovery "
  3473. "required on readonly filesystem");
  3474. if (really_read_only) {
  3475. ext4_msg(sb, KERN_ERR, "write access "
  3476. "unavailable, cannot proceed");
  3477. return -EROFS;
  3478. }
  3479. ext4_msg(sb, KERN_INFO, "write access will "
  3480. "be enabled during recovery");
  3481. }
  3482. }
  3483. if (journal_inum && journal_dev) {
  3484. ext4_msg(sb, KERN_ERR, "filesystem has both journal "
  3485. "and inode journals!");
  3486. return -EINVAL;
  3487. }
  3488. if (journal_inum) {
  3489. if (!(journal = ext4_get_journal(sb, journal_inum)))
  3490. return -EINVAL;
  3491. } else {
  3492. if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
  3493. return -EINVAL;
  3494. }
  3495. if (!(journal->j_flags & JBD2_BARRIER))
  3496. ext4_msg(sb, KERN_INFO, "barriers disabled");
  3497. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
  3498. err = jbd2_journal_wipe(journal, !really_read_only);
  3499. if (!err) {
  3500. char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
  3501. if (save)
  3502. memcpy(save, ((char *) es) +
  3503. EXT4_S_ERR_START, EXT4_S_ERR_LEN);
  3504. err = jbd2_journal_load(journal);
  3505. if (save)
  3506. memcpy(((char *) es) + EXT4_S_ERR_START,
  3507. save, EXT4_S_ERR_LEN);
  3508. kfree(save);
  3509. }
  3510. if (err) {
  3511. ext4_msg(sb, KERN_ERR, "error loading journal");
  3512. jbd2_journal_destroy(journal);
  3513. return err;
  3514. }
  3515. EXT4_SB(sb)->s_journal = journal;
  3516. ext4_clear_journal_err(sb, es);
  3517. if (!really_read_only && journal_devnum &&
  3518. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3519. es->s_journal_dev = cpu_to_le32(journal_devnum);
  3520. /* Make sure we flush the recovery flag to disk. */
  3521. ext4_commit_super(sb, 1);
  3522. }
  3523. return 0;
  3524. }
  3525. static int ext4_commit_super(struct super_block *sb, int sync)
  3526. {
  3527. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  3528. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  3529. int error = 0;
  3530. if (!sbh || block_device_ejected(sb))
  3531. return error;
  3532. if (buffer_write_io_error(sbh)) {
  3533. /*
  3534. * Oh, dear. A previous attempt to write the
  3535. * superblock failed. This could happen because the
  3536. * USB device was yanked out. Or it could happen to
  3537. * be a transient write error and maybe the block will
  3538. * be remapped. Nothing we can do but to retry the
  3539. * write and hope for the best.
  3540. */
  3541. ext4_msg(sb, KERN_ERR, "previous I/O error to "
  3542. "superblock detected");
  3543. clear_buffer_write_io_error(sbh);
  3544. set_buffer_uptodate(sbh);
  3545. }
  3546. /*
  3547. * If the file system is mounted read-only, don't update the
  3548. * superblock write time. This avoids updating the superblock
  3549. * write time when we are mounting the root file system
  3550. * read/only but we need to replay the journal; at that point,
  3551. * for people who are east of GMT and who make their clock
  3552. * tick in localtime for Windows bug-for-bug compatibility,
  3553. * the clock is set in the future, and this will cause e2fsck
  3554. * to complain and force a full file system check.
  3555. */
  3556. if (!(sb->s_flags & MS_RDONLY))
  3557. es->s_wtime = cpu_to_le32(get_seconds());
  3558. if (sb->s_bdev->bd_part)
  3559. es->s_kbytes_written =
  3560. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
  3561. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  3562. EXT4_SB(sb)->s_sectors_written_start) >> 1));
  3563. else
  3564. es->s_kbytes_written =
  3565. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
  3566. ext4_free_blocks_count_set(es,
  3567. EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
  3568. &EXT4_SB(sb)->s_freeclusters_counter)));
  3569. es->s_free_inodes_count =
  3570. cpu_to_le32(percpu_counter_sum_positive(
  3571. &EXT4_SB(sb)->s_freeinodes_counter));
  3572. sb->s_dirt = 0;
  3573. BUFFER_TRACE(sbh, "marking dirty");
  3574. mark_buffer_dirty(sbh);
  3575. if (sync) {
  3576. error = sync_dirty_buffer(sbh);
  3577. if (error)
  3578. return error;
  3579. error = buffer_write_io_error(sbh);
  3580. if (error) {
  3581. ext4_msg(sb, KERN_ERR, "I/O error while writing "
  3582. "superblock");
  3583. clear_buffer_write_io_error(sbh);
  3584. set_buffer_uptodate(sbh);
  3585. }
  3586. }
  3587. return error;
  3588. }
  3589. /*
  3590. * Have we just finished recovery? If so, and if we are mounting (or
  3591. * remounting) the filesystem readonly, then we will end up with a
  3592. * consistent fs on disk. Record that fact.
  3593. */
  3594. static void ext4_mark_recovery_complete(struct super_block *sb,
  3595. struct ext4_super_block *es)
  3596. {
  3597. journal_t *journal = EXT4_SB(sb)->s_journal;
  3598. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3599. BUG_ON(journal != NULL);
  3600. return;
  3601. }
  3602. jbd2_journal_lock_updates(journal);
  3603. if (jbd2_journal_flush(journal) < 0)
  3604. goto out;
  3605. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
  3606. sb->s_flags & MS_RDONLY) {
  3607. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3608. ext4_commit_super(sb, 1);
  3609. }
  3610. out:
  3611. jbd2_journal_unlock_updates(journal);
  3612. }
  3613. /*
  3614. * If we are mounting (or read-write remounting) a filesystem whose journal
  3615. * has recorded an error from a previous lifetime, move that error to the
  3616. * main filesystem now.
  3617. */
  3618. static void ext4_clear_journal_err(struct super_block *sb,
  3619. struct ext4_super_block *es)
  3620. {
  3621. journal_t *journal;
  3622. int j_errno;
  3623. const char *errstr;
  3624. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3625. journal = EXT4_SB(sb)->s_journal;
  3626. /*
  3627. * Now check for any error status which may have been recorded in the
  3628. * journal by a prior ext4_error() or ext4_abort()
  3629. */
  3630. j_errno = jbd2_journal_errno(journal);
  3631. if (j_errno) {
  3632. char nbuf[16];
  3633. errstr = ext4_decode_error(sb, j_errno, nbuf);
  3634. ext4_warning(sb, "Filesystem error recorded "
  3635. "from previous mount: %s", errstr);
  3636. ext4_warning(sb, "Marking fs in need of filesystem check.");
  3637. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  3638. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  3639. ext4_commit_super(sb, 1);
  3640. jbd2_journal_clear_err(journal);
  3641. }
  3642. }
  3643. /*
  3644. * Force the running and committing transactions to commit,
  3645. * and wait on the commit.
  3646. */
  3647. int ext4_force_commit(struct super_block *sb)
  3648. {
  3649. journal_t *journal;
  3650. int ret = 0;
  3651. if (sb->s_flags & MS_RDONLY)
  3652. return 0;
  3653. journal = EXT4_SB(sb)->s_journal;
  3654. if (journal) {
  3655. vfs_check_frozen(sb, SB_FREEZE_TRANS);
  3656. ret = ext4_journal_force_commit(journal);
  3657. }
  3658. return ret;
  3659. }
  3660. static void ext4_write_super(struct super_block *sb)
  3661. {
  3662. lock_super(sb);
  3663. ext4_commit_super(sb, 1);
  3664. unlock_super(sb);
  3665. }
  3666. static int ext4_sync_fs(struct super_block *sb, int wait)
  3667. {
  3668. int ret = 0;
  3669. tid_t target;
  3670. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3671. trace_ext4_sync_fs(sb, wait);
  3672. flush_workqueue(sbi->dio_unwritten_wq);
  3673. if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
  3674. if (wait)
  3675. jbd2_log_wait_commit(sbi->s_journal, target);
  3676. }
  3677. return ret;
  3678. }
  3679. /*
  3680. * LVM calls this function before a (read-only) snapshot is created. This
  3681. * gives us a chance to flush the journal completely and mark the fs clean.
  3682. *
  3683. * Note that only this function cannot bring a filesystem to be in a clean
  3684. * state independently, because ext4 prevents a new handle from being started
  3685. * by @sb->s_frozen, which stays in an upper layer. It thus needs help from
  3686. * the upper layer.
  3687. */
  3688. static int ext4_freeze(struct super_block *sb)
  3689. {
  3690. int error = 0;
  3691. journal_t *journal;
  3692. if (sb->s_flags & MS_RDONLY)
  3693. return 0;
  3694. journal = EXT4_SB(sb)->s_journal;
  3695. /* Now we set up the journal barrier. */
  3696. jbd2_journal_lock_updates(journal);
  3697. /*
  3698. * Don't clear the needs_recovery flag if we failed to flush
  3699. * the journal.
  3700. */
  3701. error = jbd2_journal_flush(journal);
  3702. if (error < 0)
  3703. goto out;
  3704. /* Journal blocked and flushed, clear needs_recovery flag. */
  3705. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3706. error = ext4_commit_super(sb, 1);
  3707. out:
  3708. /* we rely on s_frozen to stop further updates */
  3709. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  3710. return error;
  3711. }
  3712. /*
  3713. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  3714. * flag here, even though the filesystem is not technically dirty yet.
  3715. */
  3716. static int ext4_unfreeze(struct super_block *sb)
  3717. {
  3718. if (sb->s_flags & MS_RDONLY)
  3719. return 0;
  3720. lock_super(sb);
  3721. /* Reset the needs_recovery flag before the fs is unlocked. */
  3722. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3723. ext4_commit_super(sb, 1);
  3724. unlock_super(sb);
  3725. return 0;
  3726. }
  3727. /*
  3728. * Structure to save mount options for ext4_remount's benefit
  3729. */
  3730. struct ext4_mount_options {
  3731. unsigned long s_mount_opt;
  3732. unsigned long s_mount_opt2;
  3733. uid_t s_resuid;
  3734. gid_t s_resgid;
  3735. unsigned long s_commit_interval;
  3736. u32 s_min_batch_time, s_max_batch_time;
  3737. #ifdef CONFIG_QUOTA
  3738. int s_jquota_fmt;
  3739. char *s_qf_names[MAXQUOTAS];
  3740. #endif
  3741. };
  3742. static int ext4_remount(struct super_block *sb, int *flags, char *data)
  3743. {
  3744. struct ext4_super_block *es;
  3745. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3746. unsigned long old_sb_flags;
  3747. struct ext4_mount_options old_opts;
  3748. int enable_quota = 0;
  3749. ext4_group_t g;
  3750. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  3751. int err = 0;
  3752. #ifdef CONFIG_QUOTA
  3753. int i;
  3754. #endif
  3755. char *orig_data = kstrdup(data, GFP_KERNEL);
  3756. /* Store the original options */
  3757. lock_super(sb);
  3758. old_sb_flags = sb->s_flags;
  3759. old_opts.s_mount_opt = sbi->s_mount_opt;
  3760. old_opts.s_mount_opt2 = sbi->s_mount_opt2;
  3761. old_opts.s_resuid = sbi->s_resuid;
  3762. old_opts.s_resgid = sbi->s_resgid;
  3763. old_opts.s_commit_interval = sbi->s_commit_interval;
  3764. old_opts.s_min_batch_time = sbi->s_min_batch_time;
  3765. old_opts.s_max_batch_time = sbi->s_max_batch_time;
  3766. #ifdef CONFIG_QUOTA
  3767. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  3768. for (i = 0; i < MAXQUOTAS; i++)
  3769. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  3770. #endif
  3771. if (sbi->s_journal && sbi->s_journal->j_task->io_context)
  3772. journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
  3773. /*
  3774. * Allow the "check" option to be passed as a remount option.
  3775. */
  3776. if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
  3777. err = -EINVAL;
  3778. goto restore_opts;
  3779. }
  3780. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
  3781. ext4_abort(sb, "Abort forced by user");
  3782. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  3783. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  3784. es = sbi->s_es;
  3785. if (sbi->s_journal) {
  3786. ext4_init_journal_params(sb, sbi->s_journal);
  3787. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3788. }
  3789. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
  3790. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
  3791. err = -EROFS;
  3792. goto restore_opts;
  3793. }
  3794. if (*flags & MS_RDONLY) {
  3795. err = dquot_suspend(sb, -1);
  3796. if (err < 0)
  3797. goto restore_opts;
  3798. /*
  3799. * First of all, the unconditional stuff we have to do
  3800. * to disable replay of the journal when we next remount
  3801. */
  3802. sb->s_flags |= MS_RDONLY;
  3803. /*
  3804. * OK, test if we are remounting a valid rw partition
  3805. * readonly, and if so set the rdonly flag and then
  3806. * mark the partition as valid again.
  3807. */
  3808. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  3809. (sbi->s_mount_state & EXT4_VALID_FS))
  3810. es->s_state = cpu_to_le16(sbi->s_mount_state);
  3811. if (sbi->s_journal)
  3812. ext4_mark_recovery_complete(sb, es);
  3813. } else {
  3814. /* Make sure we can mount this feature set readwrite */
  3815. if (!ext4_feature_set_ok(sb, 0)) {
  3816. err = -EROFS;
  3817. goto restore_opts;
  3818. }
  3819. /*
  3820. * Make sure the group descriptor checksums
  3821. * are sane. If they aren't, refuse to remount r/w.
  3822. */
  3823. for (g = 0; g < sbi->s_groups_count; g++) {
  3824. struct ext4_group_desc *gdp =
  3825. ext4_get_group_desc(sb, g, NULL);
  3826. if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
  3827. ext4_msg(sb, KERN_ERR,
  3828. "ext4_remount: Checksum for group %u failed (%u!=%u)",
  3829. g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
  3830. le16_to_cpu(gdp->bg_checksum));
  3831. err = -EINVAL;
  3832. goto restore_opts;
  3833. }
  3834. }
  3835. /*
  3836. * If we have an unprocessed orphan list hanging
  3837. * around from a previously readonly bdev mount,
  3838. * require a full umount/remount for now.
  3839. */
  3840. if (es->s_last_orphan) {
  3841. ext4_msg(sb, KERN_WARNING, "Couldn't "
  3842. "remount RDWR because of unprocessed "
  3843. "orphan inode list. Please "
  3844. "umount/remount instead");
  3845. err = -EINVAL;
  3846. goto restore_opts;
  3847. }
  3848. /*
  3849. * Mounting a RDONLY partition read-write, so reread
  3850. * and store the current valid flag. (It may have
  3851. * been changed by e2fsck since we originally mounted
  3852. * the partition.)
  3853. */
  3854. if (sbi->s_journal)
  3855. ext4_clear_journal_err(sb, es);
  3856. sbi->s_mount_state = le16_to_cpu(es->s_state);
  3857. if (!ext4_setup_super(sb, es, 0))
  3858. sb->s_flags &= ~MS_RDONLY;
  3859. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  3860. EXT4_FEATURE_INCOMPAT_MMP))
  3861. if (ext4_multi_mount_protect(sb,
  3862. le64_to_cpu(es->s_mmp_block))) {
  3863. err = -EROFS;
  3864. goto restore_opts;
  3865. }
  3866. enable_quota = 1;
  3867. }
  3868. }
  3869. /*
  3870. * Reinitialize lazy itable initialization thread based on
  3871. * current settings
  3872. */
  3873. if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
  3874. ext4_unregister_li_request(sb);
  3875. else {
  3876. ext4_group_t first_not_zeroed;
  3877. first_not_zeroed = ext4_has_uninit_itable(sb);
  3878. ext4_register_li_request(sb, first_not_zeroed);
  3879. }
  3880. ext4_setup_system_zone(sb);
  3881. if (sbi->s_journal == NULL)
  3882. ext4_commit_super(sb, 1);
  3883. #ifdef CONFIG_QUOTA
  3884. /* Release old quota file names */
  3885. for (i = 0; i < MAXQUOTAS; i++)
  3886. if (old_opts.s_qf_names[i] &&
  3887. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3888. kfree(old_opts.s_qf_names[i]);
  3889. #endif
  3890. unlock_super(sb);
  3891. if (enable_quota)
  3892. dquot_resume(sb, -1);
  3893. ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
  3894. kfree(orig_data);
  3895. return 0;
  3896. restore_opts:
  3897. sb->s_flags = old_sb_flags;
  3898. sbi->s_mount_opt = old_opts.s_mount_opt;
  3899. sbi->s_mount_opt2 = old_opts.s_mount_opt2;
  3900. sbi->s_resuid = old_opts.s_resuid;
  3901. sbi->s_resgid = old_opts.s_resgid;
  3902. sbi->s_commit_interval = old_opts.s_commit_interval;
  3903. sbi->s_min_batch_time = old_opts.s_min_batch_time;
  3904. sbi->s_max_batch_time = old_opts.s_max_batch_time;
  3905. #ifdef CONFIG_QUOTA
  3906. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  3907. for (i = 0; i < MAXQUOTAS; i++) {
  3908. if (sbi->s_qf_names[i] &&
  3909. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3910. kfree(sbi->s_qf_names[i]);
  3911. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  3912. }
  3913. #endif
  3914. unlock_super(sb);
  3915. kfree(orig_data);
  3916. return err;
  3917. }
  3918. /*
  3919. * Note: calculating the overhead so we can be compatible with
  3920. * historical BSD practice is quite difficult in the face of
  3921. * clusters/bigalloc. This is because multiple metadata blocks from
  3922. * different block group can end up in the same allocation cluster.
  3923. * Calculating the exact overhead in the face of clustered allocation
  3924. * requires either O(all block bitmaps) in memory or O(number of block
  3925. * groups**2) in time. We will still calculate the superblock for
  3926. * older file systems --- and if we come across with a bigalloc file
  3927. * system with zero in s_overhead_clusters the estimate will be close to
  3928. * correct especially for very large cluster sizes --- but for newer
  3929. * file systems, it's better to calculate this figure once at mkfs
  3930. * time, and store it in the superblock. If the superblock value is
  3931. * present (even for non-bigalloc file systems), we will use it.
  3932. */
  3933. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
  3934. {
  3935. struct super_block *sb = dentry->d_sb;
  3936. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3937. struct ext4_super_block *es = sbi->s_es;
  3938. struct ext4_group_desc *gdp;
  3939. u64 fsid;
  3940. s64 bfree;
  3941. if (test_opt(sb, MINIX_DF)) {
  3942. sbi->s_overhead_last = 0;
  3943. } else if (es->s_overhead_clusters) {
  3944. sbi->s_overhead_last = le32_to_cpu(es->s_overhead_clusters);
  3945. } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
  3946. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3947. ext4_fsblk_t overhead = 0;
  3948. /*
  3949. * Compute the overhead (FS structures). This is constant
  3950. * for a given filesystem unless the number of block groups
  3951. * changes so we cache the previous value until it does.
  3952. */
  3953. /*
  3954. * All of the blocks before first_data_block are
  3955. * overhead
  3956. */
  3957. overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
  3958. /*
  3959. * Add the overhead found in each block group
  3960. */
  3961. for (i = 0; i < ngroups; i++) {
  3962. gdp = ext4_get_group_desc(sb, i, NULL);
  3963. overhead += ext4_num_overhead_clusters(sb, i, gdp);
  3964. cond_resched();
  3965. }
  3966. sbi->s_overhead_last = overhead;
  3967. smp_wmb();
  3968. sbi->s_blocks_last = ext4_blocks_count(es);
  3969. }
  3970. buf->f_type = EXT4_SUPER_MAGIC;
  3971. buf->f_bsize = sb->s_blocksize;
  3972. buf->f_blocks = (ext4_blocks_count(es) -
  3973. EXT4_C2B(sbi, sbi->s_overhead_last));
  3974. bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
  3975. percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
  3976. /* prevent underflow in case that few free space is available */
  3977. buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
  3978. buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
  3979. if (buf->f_bfree < ext4_r_blocks_count(es))
  3980. buf->f_bavail = 0;
  3981. buf->f_files = le32_to_cpu(es->s_inodes_count);
  3982. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  3983. buf->f_namelen = EXT4_NAME_LEN;
  3984. fsid = le64_to_cpup((void *)es->s_uuid) ^
  3985. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  3986. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  3987. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  3988. return 0;
  3989. }
  3990. /* Helper function for writing quotas on sync - we need to start transaction
  3991. * before quota file is locked for write. Otherwise the are possible deadlocks:
  3992. * Process 1 Process 2
  3993. * ext4_create() quota_sync()
  3994. * jbd2_journal_start() write_dquot()
  3995. * dquot_initialize() down(dqio_mutex)
  3996. * down(dqio_mutex) jbd2_journal_start()
  3997. *
  3998. */
  3999. #ifdef CONFIG_QUOTA
  4000. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  4001. {
  4002. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  4003. }
  4004. static int ext4_write_dquot(struct dquot *dquot)
  4005. {
  4006. int ret, err;
  4007. handle_t *handle;
  4008. struct inode *inode;
  4009. inode = dquot_to_inode(dquot);
  4010. handle = ext4_journal_start(inode,
  4011. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  4012. if (IS_ERR(handle))
  4013. return PTR_ERR(handle);
  4014. ret = dquot_commit(dquot);
  4015. err = ext4_journal_stop(handle);
  4016. if (!ret)
  4017. ret = err;
  4018. return ret;
  4019. }
  4020. static int ext4_acquire_dquot(struct dquot *dquot)
  4021. {
  4022. int ret, err;
  4023. handle_t *handle;
  4024. handle = ext4_journal_start(dquot_to_inode(dquot),
  4025. EXT4_QUOTA_INIT_BLOCKS(dquot->dq_sb));
  4026. if (IS_ERR(handle))
  4027. return PTR_ERR(handle);
  4028. ret = dquot_acquire(dquot);
  4029. err = ext4_journal_stop(handle);
  4030. if (!ret)
  4031. ret = err;
  4032. return ret;
  4033. }
  4034. static int ext4_release_dquot(struct dquot *dquot)
  4035. {
  4036. int ret, err;
  4037. handle_t *handle;
  4038. handle = ext4_journal_start(dquot_to_inode(dquot),
  4039. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  4040. if (IS_ERR(handle)) {
  4041. /* Release dquot anyway to avoid endless cycle in dqput() */
  4042. dquot_release(dquot);
  4043. return PTR_ERR(handle);
  4044. }
  4045. ret = dquot_release(dquot);
  4046. err = ext4_journal_stop(handle);
  4047. if (!ret)
  4048. ret = err;
  4049. return ret;
  4050. }
  4051. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  4052. {
  4053. /* Are we journaling quotas? */
  4054. if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  4055. EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  4056. dquot_mark_dquot_dirty(dquot);
  4057. return ext4_write_dquot(dquot);
  4058. } else {
  4059. return dquot_mark_dquot_dirty(dquot);
  4060. }
  4061. }
  4062. static int ext4_write_info(struct super_block *sb, int type)
  4063. {
  4064. int ret, err;
  4065. handle_t *handle;
  4066. /* Data block + inode block */
  4067. handle = ext4_journal_start(sb->s_root->d_inode, 2);
  4068. if (IS_ERR(handle))
  4069. return PTR_ERR(handle);
  4070. ret = dquot_commit_info(sb, type);
  4071. err = ext4_journal_stop(handle);
  4072. if (!ret)
  4073. ret = err;
  4074. return ret;
  4075. }
  4076. /*
  4077. * Turn on quotas during mount time - we need to find
  4078. * the quota file and such...
  4079. */
  4080. static int ext4_quota_on_mount(struct super_block *sb, int type)
  4081. {
  4082. return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
  4083. EXT4_SB(sb)->s_jquota_fmt, type);
  4084. }
  4085. /*
  4086. * Standard function to be called on quota_on
  4087. */
  4088. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  4089. struct path *path)
  4090. {
  4091. int err;
  4092. if (!test_opt(sb, QUOTA))
  4093. return -EINVAL;
  4094. /* Quotafile not on the same filesystem? */
  4095. if (path->dentry->d_sb != sb)
  4096. return -EXDEV;
  4097. /* Journaling quota? */
  4098. if (EXT4_SB(sb)->s_qf_names[type]) {
  4099. /* Quotafile not in fs root? */
  4100. if (path->dentry->d_parent != sb->s_root)
  4101. ext4_msg(sb, KERN_WARNING,
  4102. "Quota file not on filesystem root. "
  4103. "Journaled quota will not work");
  4104. }
  4105. /*
  4106. * When we journal data on quota file, we have to flush journal to see
  4107. * all updates to the file when we bypass pagecache...
  4108. */
  4109. if (EXT4_SB(sb)->s_journal &&
  4110. ext4_should_journal_data(path->dentry->d_inode)) {
  4111. /*
  4112. * We don't need to lock updates but journal_flush() could
  4113. * otherwise be livelocked...
  4114. */
  4115. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  4116. err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  4117. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  4118. if (err)
  4119. return err;
  4120. }
  4121. return dquot_quota_on(sb, type, format_id, path);
  4122. }
  4123. static int ext4_quota_off(struct super_block *sb, int type)
  4124. {
  4125. struct inode *inode = sb_dqopt(sb)->files[type];
  4126. handle_t *handle;
  4127. /* Force all delayed allocation blocks to be allocated.
  4128. * Caller already holds s_umount sem */
  4129. if (test_opt(sb, DELALLOC))
  4130. sync_filesystem(sb);
  4131. if (!inode)
  4132. goto out;
  4133. /* Update modification times of quota files when userspace can
  4134. * start looking at them */
  4135. handle = ext4_journal_start(inode, 1);
  4136. if (IS_ERR(handle))
  4137. goto out;
  4138. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  4139. ext4_mark_inode_dirty(handle, inode);
  4140. ext4_journal_stop(handle);
  4141. out:
  4142. return dquot_quota_off(sb, type);
  4143. }
  4144. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  4145. * acquiring the locks... As quota files are never truncated and quota code
  4146. * itself serializes the operations (and no one else should touch the files)
  4147. * we don't have to be afraid of races */
  4148. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  4149. size_t len, loff_t off)
  4150. {
  4151. struct inode *inode = sb_dqopt(sb)->files[type];
  4152. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  4153. int err = 0;
  4154. int offset = off & (sb->s_blocksize - 1);
  4155. int tocopy;
  4156. size_t toread;
  4157. struct buffer_head *bh;
  4158. loff_t i_size = i_size_read(inode);
  4159. if (off > i_size)
  4160. return 0;
  4161. if (off+len > i_size)
  4162. len = i_size-off;
  4163. toread = len;
  4164. while (toread > 0) {
  4165. tocopy = sb->s_blocksize - offset < toread ?
  4166. sb->s_blocksize - offset : toread;
  4167. bh = ext4_bread(NULL, inode, blk, 0, &err);
  4168. if (err)
  4169. return err;
  4170. if (!bh) /* A hole? */
  4171. memset(data, 0, tocopy);
  4172. else
  4173. memcpy(data, bh->b_data+offset, tocopy);
  4174. brelse(bh);
  4175. offset = 0;
  4176. toread -= tocopy;
  4177. data += tocopy;
  4178. blk++;
  4179. }
  4180. return len;
  4181. }
  4182. /* Write to quotafile (we know the transaction is already started and has
  4183. * enough credits) */
  4184. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  4185. const char *data, size_t len, loff_t off)
  4186. {
  4187. struct inode *inode = sb_dqopt(sb)->files[type];
  4188. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  4189. int err = 0;
  4190. int offset = off & (sb->s_blocksize - 1);
  4191. struct buffer_head *bh;
  4192. handle_t *handle = journal_current_handle();
  4193. if (EXT4_SB(sb)->s_journal && !handle) {
  4194. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  4195. " cancelled because transaction is not started",
  4196. (unsigned long long)off, (unsigned long long)len);
  4197. return -EIO;
  4198. }
  4199. /*
  4200. * Since we account only one data block in transaction credits,
  4201. * then it is impossible to cross a block boundary.
  4202. */
  4203. if (sb->s_blocksize - offset < len) {
  4204. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  4205. " cancelled because not block aligned",
  4206. (unsigned long long)off, (unsigned long long)len);
  4207. return -EIO;
  4208. }
  4209. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  4210. bh = ext4_bread(handle, inode, blk, 1, &err);
  4211. if (!bh)
  4212. goto out;
  4213. err = ext4_journal_get_write_access(handle, bh);
  4214. if (err) {
  4215. brelse(bh);
  4216. goto out;
  4217. }
  4218. lock_buffer(bh);
  4219. memcpy(bh->b_data+offset, data, len);
  4220. flush_dcache_page(bh->b_page);
  4221. unlock_buffer(bh);
  4222. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  4223. brelse(bh);
  4224. out:
  4225. if (err) {
  4226. mutex_unlock(&inode->i_mutex);
  4227. return err;
  4228. }
  4229. if (inode->i_size < off + len) {
  4230. i_size_write(inode, off + len);
  4231. EXT4_I(inode)->i_disksize = inode->i_size;
  4232. ext4_mark_inode_dirty(handle, inode);
  4233. }
  4234. mutex_unlock(&inode->i_mutex);
  4235. return len;
  4236. }
  4237. #endif
  4238. static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
  4239. const char *dev_name, void *data)
  4240. {
  4241. return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
  4242. }
  4243. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  4244. static inline void register_as_ext2(void)
  4245. {
  4246. int err = register_filesystem(&ext2_fs_type);
  4247. if (err)
  4248. printk(KERN_WARNING
  4249. "EXT4-fs: Unable to register as ext2 (%d)\n", err);
  4250. }
  4251. static inline void unregister_as_ext2(void)
  4252. {
  4253. unregister_filesystem(&ext2_fs_type);
  4254. }
  4255. static inline int ext2_feature_set_ok(struct super_block *sb)
  4256. {
  4257. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
  4258. return 0;
  4259. if (sb->s_flags & MS_RDONLY)
  4260. return 1;
  4261. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
  4262. return 0;
  4263. return 1;
  4264. }
  4265. MODULE_ALIAS("ext2");
  4266. #else
  4267. static inline void register_as_ext2(void) { }
  4268. static inline void unregister_as_ext2(void) { }
  4269. static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
  4270. #endif
  4271. #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  4272. static inline void register_as_ext3(void)
  4273. {
  4274. int err = register_filesystem(&ext3_fs_type);
  4275. if (err)
  4276. printk(KERN_WARNING
  4277. "EXT4-fs: Unable to register as ext3 (%d)\n", err);
  4278. }
  4279. static inline void unregister_as_ext3(void)
  4280. {
  4281. unregister_filesystem(&ext3_fs_type);
  4282. }
  4283. static inline int ext3_feature_set_ok(struct super_block *sb)
  4284. {
  4285. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
  4286. return 0;
  4287. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  4288. return 0;
  4289. if (sb->s_flags & MS_RDONLY)
  4290. return 1;
  4291. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
  4292. return 0;
  4293. return 1;
  4294. }
  4295. MODULE_ALIAS("ext3");
  4296. #else
  4297. static inline void register_as_ext3(void) { }
  4298. static inline void unregister_as_ext3(void) { }
  4299. static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
  4300. #endif
  4301. static struct file_system_type ext4_fs_type = {
  4302. .owner = THIS_MODULE,
  4303. .name = "ext4",
  4304. .mount = ext4_mount,
  4305. .kill_sb = kill_block_super,
  4306. .fs_flags = FS_REQUIRES_DEV,
  4307. };
  4308. static int __init ext4_init_feat_adverts(void)
  4309. {
  4310. struct ext4_features *ef;
  4311. int ret = -ENOMEM;
  4312. ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
  4313. if (!ef)
  4314. goto out;
  4315. ef->f_kobj.kset = ext4_kset;
  4316. init_completion(&ef->f_kobj_unregister);
  4317. ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
  4318. "features");
  4319. if (ret) {
  4320. kfree(ef);
  4321. goto out;
  4322. }
  4323. ext4_feat = ef;
  4324. ret = 0;
  4325. out:
  4326. return ret;
  4327. }
  4328. static void ext4_exit_feat_adverts(void)
  4329. {
  4330. kobject_put(&ext4_feat->f_kobj);
  4331. wait_for_completion(&ext4_feat->f_kobj_unregister);
  4332. kfree(ext4_feat);
  4333. }
  4334. /* Shared across all ext4 file systems */
  4335. wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
  4336. struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
  4337. static int __init ext4_init_fs(void)
  4338. {
  4339. int i, err;
  4340. ext4_li_info = NULL;
  4341. mutex_init(&ext4_li_mtx);
  4342. ext4_check_flag_values();
  4343. for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
  4344. mutex_init(&ext4__aio_mutex[i]);
  4345. init_waitqueue_head(&ext4__ioend_wq[i]);
  4346. }
  4347. err = ext4_init_pageio();
  4348. if (err)
  4349. return err;
  4350. err = ext4_init_system_zone();
  4351. if (err)
  4352. goto out6;
  4353. ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
  4354. if (!ext4_kset)
  4355. goto out5;
  4356. ext4_proc_root = proc_mkdir("fs/ext4", NULL);
  4357. err = ext4_init_feat_adverts();
  4358. if (err)
  4359. goto out4;
  4360. err = ext4_init_mballoc();
  4361. if (err)
  4362. goto out3;
  4363. err = ext4_init_xattr();
  4364. if (err)
  4365. goto out2;
  4366. err = init_inodecache();
  4367. if (err)
  4368. goto out1;
  4369. register_as_ext3();
  4370. register_as_ext2();
  4371. err = register_filesystem(&ext4_fs_type);
  4372. if (err)
  4373. goto out;
  4374. return 0;
  4375. out:
  4376. unregister_as_ext2();
  4377. unregister_as_ext3();
  4378. destroy_inodecache();
  4379. out1:
  4380. ext4_exit_xattr();
  4381. out2:
  4382. ext4_exit_mballoc();
  4383. out3:
  4384. ext4_exit_feat_adverts();
  4385. out4:
  4386. if (ext4_proc_root)
  4387. remove_proc_entry("fs/ext4", NULL);
  4388. kset_unregister(ext4_kset);
  4389. out5:
  4390. ext4_exit_system_zone();
  4391. out6:
  4392. ext4_exit_pageio();
  4393. return err;
  4394. }
  4395. static void __exit ext4_exit_fs(void)
  4396. {
  4397. ext4_destroy_lazyinit_thread();
  4398. unregister_as_ext2();
  4399. unregister_as_ext3();
  4400. unregister_filesystem(&ext4_fs_type);
  4401. destroy_inodecache();
  4402. ext4_exit_xattr();
  4403. ext4_exit_mballoc();
  4404. ext4_exit_feat_adverts();
  4405. remove_proc_entry("fs/ext4", NULL);
  4406. kset_unregister(ext4_kset);
  4407. ext4_exit_system_zone();
  4408. ext4_exit_pageio();
  4409. }
  4410. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  4411. MODULE_DESCRIPTION("Fourth Extended Filesystem");
  4412. MODULE_LICENSE("GPL");
  4413. module_init(ext4_init_fs)
  4414. module_exit(ext4_exit_fs)