super.c 141 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994
  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 0;
  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 0;
  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 0;
  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 0;
  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 0;
  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. if (args->from && match_int(args, &arg))
  1288. return -1;
  1289. switch (token) {
  1290. case Opt_noacl:
  1291. case Opt_nouser_xattr:
  1292. ext4_msg(sb, KERN_WARNING, deprecated_msg, opt, "3.5");
  1293. break;
  1294. case Opt_sb:
  1295. return 1; /* handled by get_sb_block() */
  1296. case Opt_removed:
  1297. ext4_msg(sb, KERN_WARNING,
  1298. "Ignoring removed %s option", opt);
  1299. return 1;
  1300. case Opt_resuid:
  1301. sbi->s_resuid = arg;
  1302. return 1;
  1303. case Opt_resgid:
  1304. sbi->s_resgid = arg;
  1305. return 1;
  1306. case Opt_abort:
  1307. sbi->s_mount_flags |= EXT4_MF_FS_ABORTED;
  1308. return 1;
  1309. case Opt_i_version:
  1310. sb->s_flags |= MS_I_VERSION;
  1311. return 1;
  1312. case Opt_journal_dev:
  1313. if (is_remount) {
  1314. ext4_msg(sb, KERN_ERR,
  1315. "Cannot specify journal on remount");
  1316. return -1;
  1317. }
  1318. *journal_devnum = arg;
  1319. return 1;
  1320. case Opt_journal_ioprio:
  1321. if (arg < 0 || arg > 7)
  1322. return -1;
  1323. *journal_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, arg);
  1324. return 1;
  1325. }
  1326. for (m = ext4_mount_opts; m->token != Opt_err; m++) {
  1327. if (token != m->token)
  1328. continue;
  1329. if (args->from && (m->flags & MOPT_GTE0) && (arg < 0))
  1330. return -1;
  1331. if (m->flags & MOPT_EXPLICIT)
  1332. set_opt2(sb, EXPLICIT_DELALLOC);
  1333. if (m->flags & MOPT_CLEAR_ERR)
  1334. clear_opt(sb, ERRORS_MASK);
  1335. if (token == Opt_noquota && sb_any_quota_loaded(sb)) {
  1336. ext4_msg(sb, KERN_ERR, "Cannot change quota "
  1337. "options when quota turned on");
  1338. return -1;
  1339. }
  1340. if (m->flags & MOPT_NOSUPPORT) {
  1341. ext4_msg(sb, KERN_ERR, "%s option not supported", opt);
  1342. } else if (token == Opt_commit) {
  1343. if (arg == 0)
  1344. arg = JBD2_DEFAULT_MAX_COMMIT_AGE;
  1345. sbi->s_commit_interval = HZ * arg;
  1346. } else if (token == Opt_max_batch_time) {
  1347. if (arg == 0)
  1348. arg = EXT4_DEF_MAX_BATCH_TIME;
  1349. sbi->s_max_batch_time = arg;
  1350. } else if (token == Opt_min_batch_time) {
  1351. sbi->s_min_batch_time = arg;
  1352. } else if (token == Opt_inode_readahead_blks) {
  1353. if (arg > (1 << 30))
  1354. return -1;
  1355. if (arg && !is_power_of_2(arg)) {
  1356. ext4_msg(sb, KERN_ERR,
  1357. "EXT4-fs: inode_readahead_blks"
  1358. " must be a power of 2");
  1359. return -1;
  1360. }
  1361. sbi->s_inode_readahead_blks = arg;
  1362. } else if (token == Opt_init_itable) {
  1363. set_opt(sb, INIT_INODE_TABLE);
  1364. if (!args->from)
  1365. arg = EXT4_DEF_LI_WAIT_MULT;
  1366. sbi->s_li_wait_mult = arg;
  1367. } else if (token == Opt_stripe) {
  1368. sbi->s_stripe = arg;
  1369. } else if (m->flags & MOPT_DATAJ) {
  1370. if (is_remount) {
  1371. if (!sbi->s_journal)
  1372. ext4_msg(sb, KERN_WARNING, "Remounting file system with no journal so ignoring journalled data option");
  1373. else if (test_opt(sb, DATA_FLAGS) !=
  1374. m->mount_opt) {
  1375. ext4_msg(sb, KERN_ERR,
  1376. "Cannot change data mode on remount");
  1377. return -1;
  1378. }
  1379. } else {
  1380. clear_opt(sb, DATA_FLAGS);
  1381. sbi->s_mount_opt |= m->mount_opt;
  1382. }
  1383. #ifdef CONFIG_QUOTA
  1384. } else if (token == Opt_usrjquota) {
  1385. if (!set_qf_name(sb, USRQUOTA, &args[0]))
  1386. return -1;
  1387. } else if (token == Opt_grpjquota) {
  1388. if (!set_qf_name(sb, GRPQUOTA, &args[0]))
  1389. return -1;
  1390. } else if (token == Opt_offusrjquota) {
  1391. if (!clear_qf_name(sb, USRQUOTA))
  1392. return -1;
  1393. } else if (token == Opt_offgrpjquota) {
  1394. if (!clear_qf_name(sb, GRPQUOTA))
  1395. return -1;
  1396. } else if (m->flags & MOPT_QFMT) {
  1397. if (sb_any_quota_loaded(sb) &&
  1398. sbi->s_jquota_fmt != m->mount_opt) {
  1399. ext4_msg(sb, KERN_ERR, "Cannot "
  1400. "change journaled quota options "
  1401. "when quota turned on");
  1402. return -1;
  1403. }
  1404. sbi->s_jquota_fmt = m->mount_opt;
  1405. #endif
  1406. } else {
  1407. if (!args->from)
  1408. arg = 1;
  1409. if (m->flags & MOPT_CLEAR)
  1410. arg = !arg;
  1411. else if (unlikely(!(m->flags & MOPT_SET))) {
  1412. ext4_msg(sb, KERN_WARNING,
  1413. "buggy handling of option %s", opt);
  1414. WARN_ON(1);
  1415. return -1;
  1416. }
  1417. if (arg != 0)
  1418. sbi->s_mount_opt |= m->mount_opt;
  1419. else
  1420. sbi->s_mount_opt &= ~m->mount_opt;
  1421. }
  1422. return 1;
  1423. }
  1424. ext4_msg(sb, KERN_ERR, "Unrecognized mount option \"%s\" "
  1425. "or missing value", opt);
  1426. return -1;
  1427. }
  1428. static int parse_options(char *options, struct super_block *sb,
  1429. unsigned long *journal_devnum,
  1430. unsigned int *journal_ioprio,
  1431. int is_remount)
  1432. {
  1433. struct ext4_sb_info *sbi = EXT4_SB(sb);
  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 extent_cache_hits_show(struct ext4_attr *a,
  2106. struct ext4_sb_info *sbi, char *buf)
  2107. {
  2108. return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_hits);
  2109. }
  2110. static ssize_t extent_cache_misses_show(struct ext4_attr *a,
  2111. struct ext4_sb_info *sbi, char *buf)
  2112. {
  2113. return snprintf(buf, PAGE_SIZE, "%lu\n", sbi->extent_cache_misses);
  2114. }
  2115. static ssize_t inode_readahead_blks_store(struct ext4_attr *a,
  2116. struct ext4_sb_info *sbi,
  2117. const char *buf, size_t count)
  2118. {
  2119. unsigned long t;
  2120. if (parse_strtoul(buf, 0x40000000, &t))
  2121. return -EINVAL;
  2122. if (t && !is_power_of_2(t))
  2123. return -EINVAL;
  2124. sbi->s_inode_readahead_blks = t;
  2125. return count;
  2126. }
  2127. static ssize_t sbi_ui_show(struct ext4_attr *a,
  2128. struct ext4_sb_info *sbi, char *buf)
  2129. {
  2130. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2131. return snprintf(buf, PAGE_SIZE, "%u\n", *ui);
  2132. }
  2133. static ssize_t sbi_ui_store(struct ext4_attr *a,
  2134. struct ext4_sb_info *sbi,
  2135. const char *buf, size_t count)
  2136. {
  2137. unsigned int *ui = (unsigned int *) (((char *) sbi) + a->offset);
  2138. unsigned long t;
  2139. if (parse_strtoul(buf, 0xffffffff, &t))
  2140. return -EINVAL;
  2141. *ui = t;
  2142. return count;
  2143. }
  2144. #define EXT4_ATTR_OFFSET(_name,_mode,_show,_store,_elname) \
  2145. static struct ext4_attr ext4_attr_##_name = { \
  2146. .attr = {.name = __stringify(_name), .mode = _mode }, \
  2147. .show = _show, \
  2148. .store = _store, \
  2149. .offset = offsetof(struct ext4_sb_info, _elname), \
  2150. }
  2151. #define EXT4_ATTR(name, mode, show, store) \
  2152. static struct ext4_attr ext4_attr_##name = __ATTR(name, mode, show, store)
  2153. #define EXT4_INFO_ATTR(name) EXT4_ATTR(name, 0444, NULL, NULL)
  2154. #define EXT4_RO_ATTR(name) EXT4_ATTR(name, 0444, name##_show, NULL)
  2155. #define EXT4_RW_ATTR(name) EXT4_ATTR(name, 0644, name##_show, name##_store)
  2156. #define EXT4_RW_ATTR_SBI_UI(name, elname) \
  2157. EXT4_ATTR_OFFSET(name, 0644, sbi_ui_show, sbi_ui_store, elname)
  2158. #define ATTR_LIST(name) &ext4_attr_##name.attr
  2159. EXT4_RO_ATTR(delayed_allocation_blocks);
  2160. EXT4_RO_ATTR(session_write_kbytes);
  2161. EXT4_RO_ATTR(lifetime_write_kbytes);
  2162. EXT4_RO_ATTR(extent_cache_hits);
  2163. EXT4_RO_ATTR(extent_cache_misses);
  2164. EXT4_ATTR_OFFSET(inode_readahead_blks, 0644, sbi_ui_show,
  2165. inode_readahead_blks_store, s_inode_readahead_blks);
  2166. EXT4_RW_ATTR_SBI_UI(inode_goal, s_inode_goal);
  2167. EXT4_RW_ATTR_SBI_UI(mb_stats, s_mb_stats);
  2168. EXT4_RW_ATTR_SBI_UI(mb_max_to_scan, s_mb_max_to_scan);
  2169. EXT4_RW_ATTR_SBI_UI(mb_min_to_scan, s_mb_min_to_scan);
  2170. EXT4_RW_ATTR_SBI_UI(mb_order2_req, s_mb_order2_reqs);
  2171. EXT4_RW_ATTR_SBI_UI(mb_stream_req, s_mb_stream_request);
  2172. EXT4_RW_ATTR_SBI_UI(mb_group_prealloc, s_mb_group_prealloc);
  2173. EXT4_RW_ATTR_SBI_UI(max_writeback_mb_bump, s_max_writeback_mb_bump);
  2174. static struct attribute *ext4_attrs[] = {
  2175. ATTR_LIST(delayed_allocation_blocks),
  2176. ATTR_LIST(session_write_kbytes),
  2177. ATTR_LIST(lifetime_write_kbytes),
  2178. ATTR_LIST(extent_cache_hits),
  2179. ATTR_LIST(extent_cache_misses),
  2180. ATTR_LIST(inode_readahead_blks),
  2181. ATTR_LIST(inode_goal),
  2182. ATTR_LIST(mb_stats),
  2183. ATTR_LIST(mb_max_to_scan),
  2184. ATTR_LIST(mb_min_to_scan),
  2185. ATTR_LIST(mb_order2_req),
  2186. ATTR_LIST(mb_stream_req),
  2187. ATTR_LIST(mb_group_prealloc),
  2188. ATTR_LIST(max_writeback_mb_bump),
  2189. NULL,
  2190. };
  2191. /* Features this copy of ext4 supports */
  2192. EXT4_INFO_ATTR(lazy_itable_init);
  2193. EXT4_INFO_ATTR(batched_discard);
  2194. static struct attribute *ext4_feat_attrs[] = {
  2195. ATTR_LIST(lazy_itable_init),
  2196. ATTR_LIST(batched_discard),
  2197. NULL,
  2198. };
  2199. static ssize_t ext4_attr_show(struct kobject *kobj,
  2200. struct attribute *attr, char *buf)
  2201. {
  2202. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2203. s_kobj);
  2204. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2205. return a->show ? a->show(a, sbi, buf) : 0;
  2206. }
  2207. static ssize_t ext4_attr_store(struct kobject *kobj,
  2208. struct attribute *attr,
  2209. const char *buf, size_t len)
  2210. {
  2211. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2212. s_kobj);
  2213. struct ext4_attr *a = container_of(attr, struct ext4_attr, attr);
  2214. return a->store ? a->store(a, sbi, buf, len) : 0;
  2215. }
  2216. static void ext4_sb_release(struct kobject *kobj)
  2217. {
  2218. struct ext4_sb_info *sbi = container_of(kobj, struct ext4_sb_info,
  2219. s_kobj);
  2220. complete(&sbi->s_kobj_unregister);
  2221. }
  2222. static const struct sysfs_ops ext4_attr_ops = {
  2223. .show = ext4_attr_show,
  2224. .store = ext4_attr_store,
  2225. };
  2226. static struct kobj_type ext4_ktype = {
  2227. .default_attrs = ext4_attrs,
  2228. .sysfs_ops = &ext4_attr_ops,
  2229. .release = ext4_sb_release,
  2230. };
  2231. static void ext4_feat_release(struct kobject *kobj)
  2232. {
  2233. complete(&ext4_feat->f_kobj_unregister);
  2234. }
  2235. static struct kobj_type ext4_feat_ktype = {
  2236. .default_attrs = ext4_feat_attrs,
  2237. .sysfs_ops = &ext4_attr_ops,
  2238. .release = ext4_feat_release,
  2239. };
  2240. /*
  2241. * Check whether this filesystem can be mounted based on
  2242. * the features present and the RDONLY/RDWR mount requested.
  2243. * Returns 1 if this filesystem can be mounted as requested,
  2244. * 0 if it cannot be.
  2245. */
  2246. static int ext4_feature_set_ok(struct super_block *sb, int readonly)
  2247. {
  2248. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT4_FEATURE_INCOMPAT_SUPP)) {
  2249. ext4_msg(sb, KERN_ERR,
  2250. "Couldn't mount because of "
  2251. "unsupported optional features (%x)",
  2252. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_incompat) &
  2253. ~EXT4_FEATURE_INCOMPAT_SUPP));
  2254. return 0;
  2255. }
  2256. if (readonly)
  2257. return 1;
  2258. /* Check that feature set is OK for a read-write mount */
  2259. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT4_FEATURE_RO_COMPAT_SUPP)) {
  2260. ext4_msg(sb, KERN_ERR, "couldn't mount RDWR because of "
  2261. "unsupported optional features (%x)",
  2262. (le32_to_cpu(EXT4_SB(sb)->s_es->s_feature_ro_compat) &
  2263. ~EXT4_FEATURE_RO_COMPAT_SUPP));
  2264. return 0;
  2265. }
  2266. /*
  2267. * Large file size enabled file system can only be mounted
  2268. * read-write on 32-bit systems if kernel is built with CONFIG_LBDAF
  2269. */
  2270. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_HUGE_FILE)) {
  2271. if (sizeof(blkcnt_t) < sizeof(u64)) {
  2272. ext4_msg(sb, KERN_ERR, "Filesystem with huge files "
  2273. "cannot be mounted RDWR without "
  2274. "CONFIG_LBDAF");
  2275. return 0;
  2276. }
  2277. }
  2278. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_BIGALLOC) &&
  2279. !EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  2280. ext4_msg(sb, KERN_ERR,
  2281. "Can't support bigalloc feature without "
  2282. "extents feature\n");
  2283. return 0;
  2284. }
  2285. return 1;
  2286. }
  2287. /*
  2288. * This function is called once a day if we have errors logged
  2289. * on the file system
  2290. */
  2291. static void print_daily_error_info(unsigned long arg)
  2292. {
  2293. struct super_block *sb = (struct super_block *) arg;
  2294. struct ext4_sb_info *sbi;
  2295. struct ext4_super_block *es;
  2296. sbi = EXT4_SB(sb);
  2297. es = sbi->s_es;
  2298. if (es->s_error_count)
  2299. ext4_msg(sb, KERN_NOTICE, "error count: %u",
  2300. le32_to_cpu(es->s_error_count));
  2301. if (es->s_first_error_time) {
  2302. printk(KERN_NOTICE "EXT4-fs (%s): initial error at %u: %.*s:%d",
  2303. sb->s_id, le32_to_cpu(es->s_first_error_time),
  2304. (int) sizeof(es->s_first_error_func),
  2305. es->s_first_error_func,
  2306. le32_to_cpu(es->s_first_error_line));
  2307. if (es->s_first_error_ino)
  2308. printk(": inode %u",
  2309. le32_to_cpu(es->s_first_error_ino));
  2310. if (es->s_first_error_block)
  2311. printk(": block %llu", (unsigned long long)
  2312. le64_to_cpu(es->s_first_error_block));
  2313. printk("\n");
  2314. }
  2315. if (es->s_last_error_time) {
  2316. printk(KERN_NOTICE "EXT4-fs (%s): last error at %u: %.*s:%d",
  2317. sb->s_id, le32_to_cpu(es->s_last_error_time),
  2318. (int) sizeof(es->s_last_error_func),
  2319. es->s_last_error_func,
  2320. le32_to_cpu(es->s_last_error_line));
  2321. if (es->s_last_error_ino)
  2322. printk(": inode %u",
  2323. le32_to_cpu(es->s_last_error_ino));
  2324. if (es->s_last_error_block)
  2325. printk(": block %llu", (unsigned long long)
  2326. le64_to_cpu(es->s_last_error_block));
  2327. printk("\n");
  2328. }
  2329. mod_timer(&sbi->s_err_report, jiffies + 24*60*60*HZ); /* Once a day */
  2330. }
  2331. /* Find next suitable group and run ext4_init_inode_table */
  2332. static int ext4_run_li_request(struct ext4_li_request *elr)
  2333. {
  2334. struct ext4_group_desc *gdp = NULL;
  2335. ext4_group_t group, ngroups;
  2336. struct super_block *sb;
  2337. unsigned long timeout = 0;
  2338. int ret = 0;
  2339. sb = elr->lr_super;
  2340. ngroups = EXT4_SB(sb)->s_groups_count;
  2341. for (group = elr->lr_next_group; group < ngroups; group++) {
  2342. gdp = ext4_get_group_desc(sb, group, NULL);
  2343. if (!gdp) {
  2344. ret = 1;
  2345. break;
  2346. }
  2347. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2348. break;
  2349. }
  2350. if (group == ngroups)
  2351. ret = 1;
  2352. if (!ret) {
  2353. timeout = jiffies;
  2354. ret = ext4_init_inode_table(sb, group,
  2355. elr->lr_timeout ? 0 : 1);
  2356. if (elr->lr_timeout == 0) {
  2357. timeout = (jiffies - timeout) *
  2358. elr->lr_sbi->s_li_wait_mult;
  2359. elr->lr_timeout = timeout;
  2360. }
  2361. elr->lr_next_sched = jiffies + elr->lr_timeout;
  2362. elr->lr_next_group = group + 1;
  2363. }
  2364. return ret;
  2365. }
  2366. /*
  2367. * Remove lr_request from the list_request and free the
  2368. * request structure. Should be called with li_list_mtx held
  2369. */
  2370. static void ext4_remove_li_request(struct ext4_li_request *elr)
  2371. {
  2372. struct ext4_sb_info *sbi;
  2373. if (!elr)
  2374. return;
  2375. sbi = elr->lr_sbi;
  2376. list_del(&elr->lr_request);
  2377. sbi->s_li_request = NULL;
  2378. kfree(elr);
  2379. }
  2380. static void ext4_unregister_li_request(struct super_block *sb)
  2381. {
  2382. mutex_lock(&ext4_li_mtx);
  2383. if (!ext4_li_info) {
  2384. mutex_unlock(&ext4_li_mtx);
  2385. return;
  2386. }
  2387. mutex_lock(&ext4_li_info->li_list_mtx);
  2388. ext4_remove_li_request(EXT4_SB(sb)->s_li_request);
  2389. mutex_unlock(&ext4_li_info->li_list_mtx);
  2390. mutex_unlock(&ext4_li_mtx);
  2391. }
  2392. static struct task_struct *ext4_lazyinit_task;
  2393. /*
  2394. * This is the function where ext4lazyinit thread lives. It walks
  2395. * through the request list searching for next scheduled filesystem.
  2396. * When such a fs is found, run the lazy initialization request
  2397. * (ext4_rn_li_request) and keep track of the time spend in this
  2398. * function. Based on that time we compute next schedule time of
  2399. * the request. When walking through the list is complete, compute
  2400. * next waking time and put itself into sleep.
  2401. */
  2402. static int ext4_lazyinit_thread(void *arg)
  2403. {
  2404. struct ext4_lazy_init *eli = (struct ext4_lazy_init *)arg;
  2405. struct list_head *pos, *n;
  2406. struct ext4_li_request *elr;
  2407. unsigned long next_wakeup, cur;
  2408. BUG_ON(NULL == eli);
  2409. cont_thread:
  2410. while (true) {
  2411. next_wakeup = MAX_JIFFY_OFFSET;
  2412. mutex_lock(&eli->li_list_mtx);
  2413. if (list_empty(&eli->li_request_list)) {
  2414. mutex_unlock(&eli->li_list_mtx);
  2415. goto exit_thread;
  2416. }
  2417. list_for_each_safe(pos, n, &eli->li_request_list) {
  2418. elr = list_entry(pos, struct ext4_li_request,
  2419. lr_request);
  2420. if (time_after_eq(jiffies, elr->lr_next_sched)) {
  2421. if (ext4_run_li_request(elr) != 0) {
  2422. /* error, remove the lazy_init job */
  2423. ext4_remove_li_request(elr);
  2424. continue;
  2425. }
  2426. }
  2427. if (time_before(elr->lr_next_sched, next_wakeup))
  2428. next_wakeup = elr->lr_next_sched;
  2429. }
  2430. mutex_unlock(&eli->li_list_mtx);
  2431. try_to_freeze();
  2432. cur = jiffies;
  2433. if ((time_after_eq(cur, next_wakeup)) ||
  2434. (MAX_JIFFY_OFFSET == next_wakeup)) {
  2435. cond_resched();
  2436. continue;
  2437. }
  2438. schedule_timeout_interruptible(next_wakeup - cur);
  2439. if (kthread_should_stop()) {
  2440. ext4_clear_request_list();
  2441. goto exit_thread;
  2442. }
  2443. }
  2444. exit_thread:
  2445. /*
  2446. * It looks like the request list is empty, but we need
  2447. * to check it under the li_list_mtx lock, to prevent any
  2448. * additions into it, and of course we should lock ext4_li_mtx
  2449. * to atomically free the list and ext4_li_info, because at
  2450. * this point another ext4 filesystem could be registering
  2451. * new one.
  2452. */
  2453. mutex_lock(&ext4_li_mtx);
  2454. mutex_lock(&eli->li_list_mtx);
  2455. if (!list_empty(&eli->li_request_list)) {
  2456. mutex_unlock(&eli->li_list_mtx);
  2457. mutex_unlock(&ext4_li_mtx);
  2458. goto cont_thread;
  2459. }
  2460. mutex_unlock(&eli->li_list_mtx);
  2461. kfree(ext4_li_info);
  2462. ext4_li_info = NULL;
  2463. mutex_unlock(&ext4_li_mtx);
  2464. return 0;
  2465. }
  2466. static void ext4_clear_request_list(void)
  2467. {
  2468. struct list_head *pos, *n;
  2469. struct ext4_li_request *elr;
  2470. mutex_lock(&ext4_li_info->li_list_mtx);
  2471. list_for_each_safe(pos, n, &ext4_li_info->li_request_list) {
  2472. elr = list_entry(pos, struct ext4_li_request,
  2473. lr_request);
  2474. ext4_remove_li_request(elr);
  2475. }
  2476. mutex_unlock(&ext4_li_info->li_list_mtx);
  2477. }
  2478. static int ext4_run_lazyinit_thread(void)
  2479. {
  2480. ext4_lazyinit_task = kthread_run(ext4_lazyinit_thread,
  2481. ext4_li_info, "ext4lazyinit");
  2482. if (IS_ERR(ext4_lazyinit_task)) {
  2483. int err = PTR_ERR(ext4_lazyinit_task);
  2484. ext4_clear_request_list();
  2485. kfree(ext4_li_info);
  2486. ext4_li_info = NULL;
  2487. printk(KERN_CRIT "EXT4-fs: error %d creating inode table "
  2488. "initialization thread\n",
  2489. err);
  2490. return err;
  2491. }
  2492. ext4_li_info->li_state |= EXT4_LAZYINIT_RUNNING;
  2493. return 0;
  2494. }
  2495. /*
  2496. * Check whether it make sense to run itable init. thread or not.
  2497. * If there is at least one uninitialized inode table, return
  2498. * corresponding group number, else the loop goes through all
  2499. * groups and return total number of groups.
  2500. */
  2501. static ext4_group_t ext4_has_uninit_itable(struct super_block *sb)
  2502. {
  2503. ext4_group_t group, ngroups = EXT4_SB(sb)->s_groups_count;
  2504. struct ext4_group_desc *gdp = NULL;
  2505. for (group = 0; group < ngroups; group++) {
  2506. gdp = ext4_get_group_desc(sb, group, NULL);
  2507. if (!gdp)
  2508. continue;
  2509. if (!(gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_ZEROED)))
  2510. break;
  2511. }
  2512. return group;
  2513. }
  2514. static int ext4_li_info_new(void)
  2515. {
  2516. struct ext4_lazy_init *eli = NULL;
  2517. eli = kzalloc(sizeof(*eli), GFP_KERNEL);
  2518. if (!eli)
  2519. return -ENOMEM;
  2520. INIT_LIST_HEAD(&eli->li_request_list);
  2521. mutex_init(&eli->li_list_mtx);
  2522. eli->li_state |= EXT4_LAZYINIT_QUIT;
  2523. ext4_li_info = eli;
  2524. return 0;
  2525. }
  2526. static struct ext4_li_request *ext4_li_request_new(struct super_block *sb,
  2527. ext4_group_t start)
  2528. {
  2529. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2530. struct ext4_li_request *elr;
  2531. unsigned long rnd;
  2532. elr = kzalloc(sizeof(*elr), GFP_KERNEL);
  2533. if (!elr)
  2534. return NULL;
  2535. elr->lr_super = sb;
  2536. elr->lr_sbi = sbi;
  2537. elr->lr_next_group = start;
  2538. /*
  2539. * Randomize first schedule time of the request to
  2540. * spread the inode table initialization requests
  2541. * better.
  2542. */
  2543. get_random_bytes(&rnd, sizeof(rnd));
  2544. elr->lr_next_sched = jiffies + (unsigned long)rnd %
  2545. (EXT4_DEF_LI_MAX_START_DELAY * HZ);
  2546. return elr;
  2547. }
  2548. static int ext4_register_li_request(struct super_block *sb,
  2549. ext4_group_t first_not_zeroed)
  2550. {
  2551. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2552. struct ext4_li_request *elr;
  2553. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  2554. int ret = 0;
  2555. if (sbi->s_li_request != NULL) {
  2556. /*
  2557. * Reset timeout so it can be computed again, because
  2558. * s_li_wait_mult might have changed.
  2559. */
  2560. sbi->s_li_request->lr_timeout = 0;
  2561. return 0;
  2562. }
  2563. if (first_not_zeroed == ngroups ||
  2564. (sb->s_flags & MS_RDONLY) ||
  2565. !test_opt(sb, INIT_INODE_TABLE))
  2566. return 0;
  2567. elr = ext4_li_request_new(sb, first_not_zeroed);
  2568. if (!elr)
  2569. return -ENOMEM;
  2570. mutex_lock(&ext4_li_mtx);
  2571. if (NULL == ext4_li_info) {
  2572. ret = ext4_li_info_new();
  2573. if (ret)
  2574. goto out;
  2575. }
  2576. mutex_lock(&ext4_li_info->li_list_mtx);
  2577. list_add(&elr->lr_request, &ext4_li_info->li_request_list);
  2578. mutex_unlock(&ext4_li_info->li_list_mtx);
  2579. sbi->s_li_request = elr;
  2580. /*
  2581. * set elr to NULL here since it has been inserted to
  2582. * the request_list and the removal and free of it is
  2583. * handled by ext4_clear_request_list from now on.
  2584. */
  2585. elr = NULL;
  2586. if (!(ext4_li_info->li_state & EXT4_LAZYINIT_RUNNING)) {
  2587. ret = ext4_run_lazyinit_thread();
  2588. if (ret)
  2589. goto out;
  2590. }
  2591. out:
  2592. mutex_unlock(&ext4_li_mtx);
  2593. if (ret)
  2594. kfree(elr);
  2595. return ret;
  2596. }
  2597. /*
  2598. * We do not need to lock anything since this is called on
  2599. * module unload.
  2600. */
  2601. static void ext4_destroy_lazyinit_thread(void)
  2602. {
  2603. /*
  2604. * If thread exited earlier
  2605. * there's nothing to be done.
  2606. */
  2607. if (!ext4_li_info || !ext4_lazyinit_task)
  2608. return;
  2609. kthread_stop(ext4_lazyinit_task);
  2610. }
  2611. static int ext4_fill_super(struct super_block *sb, void *data, int silent)
  2612. {
  2613. char *orig_data = kstrdup(data, GFP_KERNEL);
  2614. struct buffer_head *bh;
  2615. struct ext4_super_block *es = NULL;
  2616. struct ext4_sb_info *sbi;
  2617. ext4_fsblk_t block;
  2618. ext4_fsblk_t sb_block = get_sb_block(&data);
  2619. ext4_fsblk_t logical_sb_block;
  2620. unsigned long offset = 0;
  2621. unsigned long journal_devnum = 0;
  2622. unsigned long def_mount_opts;
  2623. struct inode *root;
  2624. char *cp;
  2625. const char *descr;
  2626. int ret = -ENOMEM;
  2627. int blocksize, clustersize;
  2628. unsigned int db_count;
  2629. unsigned int i;
  2630. int needs_recovery, has_huge_files, has_bigalloc;
  2631. __u64 blocks_count;
  2632. int err;
  2633. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  2634. ext4_group_t first_not_zeroed;
  2635. sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
  2636. if (!sbi)
  2637. goto out_free_orig;
  2638. sbi->s_blockgroup_lock =
  2639. kzalloc(sizeof(struct blockgroup_lock), GFP_KERNEL);
  2640. if (!sbi->s_blockgroup_lock) {
  2641. kfree(sbi);
  2642. goto out_free_orig;
  2643. }
  2644. sb->s_fs_info = sbi;
  2645. sbi->s_mount_opt = 0;
  2646. sbi->s_resuid = EXT4_DEF_RESUID;
  2647. sbi->s_resgid = EXT4_DEF_RESGID;
  2648. sbi->s_inode_readahead_blks = EXT4_DEF_INODE_READAHEAD_BLKS;
  2649. sbi->s_sb_block = sb_block;
  2650. if (sb->s_bdev->bd_part)
  2651. sbi->s_sectors_written_start =
  2652. part_stat_read(sb->s_bdev->bd_part, sectors[1]);
  2653. /* Cleanup superblock name */
  2654. for (cp = sb->s_id; (cp = strchr(cp, '/'));)
  2655. *cp = '!';
  2656. ret = -EINVAL;
  2657. blocksize = sb_min_blocksize(sb, EXT4_MIN_BLOCK_SIZE);
  2658. if (!blocksize) {
  2659. ext4_msg(sb, KERN_ERR, "unable to set blocksize");
  2660. goto out_fail;
  2661. }
  2662. /*
  2663. * The ext4 superblock will not be buffer aligned for other than 1kB
  2664. * block sizes. We need to calculate the offset from buffer start.
  2665. */
  2666. if (blocksize != EXT4_MIN_BLOCK_SIZE) {
  2667. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2668. offset = do_div(logical_sb_block, blocksize);
  2669. } else {
  2670. logical_sb_block = sb_block;
  2671. }
  2672. if (!(bh = sb_bread(sb, logical_sb_block))) {
  2673. ext4_msg(sb, KERN_ERR, "unable to read superblock");
  2674. goto out_fail;
  2675. }
  2676. /*
  2677. * Note: s_es must be initialized as soon as possible because
  2678. * some ext4 macro-instructions depend on its value
  2679. */
  2680. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  2681. sbi->s_es = es;
  2682. sb->s_magic = le16_to_cpu(es->s_magic);
  2683. if (sb->s_magic != EXT4_SUPER_MAGIC)
  2684. goto cantfind_ext4;
  2685. sbi->s_kbytes_written = le64_to_cpu(es->s_kbytes_written);
  2686. /* Set defaults before we parse the mount options */
  2687. def_mount_opts = le32_to_cpu(es->s_default_mount_opts);
  2688. set_opt(sb, INIT_INODE_TABLE);
  2689. if (def_mount_opts & EXT4_DEFM_DEBUG)
  2690. set_opt(sb, DEBUG);
  2691. if (def_mount_opts & EXT4_DEFM_BSDGROUPS)
  2692. set_opt(sb, GRPID);
  2693. if (def_mount_opts & EXT4_DEFM_UID16)
  2694. set_opt(sb, NO_UID32);
  2695. /* xattr user namespace & acls are now defaulted on */
  2696. #ifdef CONFIG_EXT4_FS_XATTR
  2697. set_opt(sb, XATTR_USER);
  2698. #endif
  2699. #ifdef CONFIG_EXT4_FS_POSIX_ACL
  2700. set_opt(sb, POSIX_ACL);
  2701. #endif
  2702. set_opt(sb, MBLK_IO_SUBMIT);
  2703. if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_DATA)
  2704. set_opt(sb, JOURNAL_DATA);
  2705. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_ORDERED)
  2706. set_opt(sb, ORDERED_DATA);
  2707. else if ((def_mount_opts & EXT4_DEFM_JMODE) == EXT4_DEFM_JMODE_WBACK)
  2708. set_opt(sb, WRITEBACK_DATA);
  2709. if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_PANIC)
  2710. set_opt(sb, ERRORS_PANIC);
  2711. else if (le16_to_cpu(sbi->s_es->s_errors) == EXT4_ERRORS_CONTINUE)
  2712. set_opt(sb, ERRORS_CONT);
  2713. else
  2714. set_opt(sb, ERRORS_RO);
  2715. if (def_mount_opts & EXT4_DEFM_BLOCK_VALIDITY)
  2716. set_opt(sb, BLOCK_VALIDITY);
  2717. if (def_mount_opts & EXT4_DEFM_DISCARD)
  2718. set_opt(sb, DISCARD);
  2719. sbi->s_resuid = le16_to_cpu(es->s_def_resuid);
  2720. sbi->s_resgid = le16_to_cpu(es->s_def_resgid);
  2721. sbi->s_commit_interval = JBD2_DEFAULT_MAX_COMMIT_AGE * HZ;
  2722. sbi->s_min_batch_time = EXT4_DEF_MIN_BATCH_TIME;
  2723. sbi->s_max_batch_time = EXT4_DEF_MAX_BATCH_TIME;
  2724. if ((def_mount_opts & EXT4_DEFM_NOBARRIER) == 0)
  2725. set_opt(sb, BARRIER);
  2726. /*
  2727. * enable delayed allocation by default
  2728. * Use -o nodelalloc to turn it off
  2729. */
  2730. if (!IS_EXT3_SB(sb) &&
  2731. ((def_mount_opts & EXT4_DEFM_NODELALLOC) == 0))
  2732. set_opt(sb, DELALLOC);
  2733. /*
  2734. * set default s_li_wait_mult for lazyinit, for the case there is
  2735. * no mount option specified.
  2736. */
  2737. sbi->s_li_wait_mult = EXT4_DEF_LI_WAIT_MULT;
  2738. if (!parse_options((char *) sbi->s_es->s_mount_opts, sb,
  2739. &journal_devnum, &journal_ioprio, 0)) {
  2740. ext4_msg(sb, KERN_WARNING,
  2741. "failed to parse options in superblock: %s",
  2742. sbi->s_es->s_mount_opts);
  2743. }
  2744. sbi->s_def_mount_opt = sbi->s_mount_opt;
  2745. if (!parse_options((char *) data, sb, &journal_devnum,
  2746. &journal_ioprio, 0))
  2747. goto failed_mount;
  2748. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA) {
  2749. printk_once(KERN_WARNING "EXT4-fs: Warning: mounting "
  2750. "with data=journal disables delayed "
  2751. "allocation and O_DIRECT support!\n");
  2752. if (test_opt2(sb, EXPLICIT_DELALLOC)) {
  2753. ext4_msg(sb, KERN_ERR, "can't mount with "
  2754. "both data=journal and delalloc");
  2755. goto failed_mount;
  2756. }
  2757. if (test_opt(sb, DIOREAD_NOLOCK)) {
  2758. ext4_msg(sb, KERN_ERR, "can't mount with "
  2759. "both data=journal and delalloc");
  2760. goto failed_mount;
  2761. }
  2762. if (test_opt(sb, DELALLOC))
  2763. clear_opt(sb, DELALLOC);
  2764. }
  2765. blocksize = BLOCK_SIZE << le32_to_cpu(es->s_log_block_size);
  2766. if (test_opt(sb, DIOREAD_NOLOCK)) {
  2767. if (blocksize < PAGE_SIZE) {
  2768. ext4_msg(sb, KERN_ERR, "can't mount with "
  2769. "dioread_nolock if block size != PAGE_SIZE");
  2770. goto failed_mount;
  2771. }
  2772. }
  2773. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  2774. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  2775. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV &&
  2776. (EXT4_HAS_COMPAT_FEATURE(sb, ~0U) ||
  2777. EXT4_HAS_RO_COMPAT_FEATURE(sb, ~0U) ||
  2778. EXT4_HAS_INCOMPAT_FEATURE(sb, ~0U)))
  2779. ext4_msg(sb, KERN_WARNING,
  2780. "feature flags set on rev 0 fs, "
  2781. "running e2fsck is recommended");
  2782. if (IS_EXT2_SB(sb)) {
  2783. if (ext2_feature_set_ok(sb))
  2784. ext4_msg(sb, KERN_INFO, "mounting ext2 file system "
  2785. "using the ext4 subsystem");
  2786. else {
  2787. ext4_msg(sb, KERN_ERR, "couldn't mount as ext2 due "
  2788. "to feature incompatibilities");
  2789. goto failed_mount;
  2790. }
  2791. }
  2792. if (IS_EXT3_SB(sb)) {
  2793. if (ext3_feature_set_ok(sb))
  2794. ext4_msg(sb, KERN_INFO, "mounting ext3 file system "
  2795. "using the ext4 subsystem");
  2796. else {
  2797. ext4_msg(sb, KERN_ERR, "couldn't mount as ext3 due "
  2798. "to feature incompatibilities");
  2799. goto failed_mount;
  2800. }
  2801. }
  2802. /*
  2803. * Check feature flags regardless of the revision level, since we
  2804. * previously didn't change the revision level when setting the flags,
  2805. * so there is a chance incompat flags are set on a rev 0 filesystem.
  2806. */
  2807. if (!ext4_feature_set_ok(sb, (sb->s_flags & MS_RDONLY)))
  2808. goto failed_mount;
  2809. if (blocksize < EXT4_MIN_BLOCK_SIZE ||
  2810. blocksize > EXT4_MAX_BLOCK_SIZE) {
  2811. ext4_msg(sb, KERN_ERR,
  2812. "Unsupported filesystem blocksize %d", blocksize);
  2813. goto failed_mount;
  2814. }
  2815. if (sb->s_blocksize != blocksize) {
  2816. /* Validate the filesystem blocksize */
  2817. if (!sb_set_blocksize(sb, blocksize)) {
  2818. ext4_msg(sb, KERN_ERR, "bad block size %d",
  2819. blocksize);
  2820. goto failed_mount;
  2821. }
  2822. brelse(bh);
  2823. logical_sb_block = sb_block * EXT4_MIN_BLOCK_SIZE;
  2824. offset = do_div(logical_sb_block, blocksize);
  2825. bh = sb_bread(sb, logical_sb_block);
  2826. if (!bh) {
  2827. ext4_msg(sb, KERN_ERR,
  2828. "Can't read superblock on 2nd try");
  2829. goto failed_mount;
  2830. }
  2831. es = (struct ext4_super_block *)(((char *)bh->b_data) + offset);
  2832. sbi->s_es = es;
  2833. if (es->s_magic != cpu_to_le16(EXT4_SUPER_MAGIC)) {
  2834. ext4_msg(sb, KERN_ERR,
  2835. "Magic mismatch, very weird!");
  2836. goto failed_mount;
  2837. }
  2838. }
  2839. has_huge_files = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2840. EXT4_FEATURE_RO_COMPAT_HUGE_FILE);
  2841. sbi->s_bitmap_maxbytes = ext4_max_bitmap_size(sb->s_blocksize_bits,
  2842. has_huge_files);
  2843. sb->s_maxbytes = ext4_max_size(sb->s_blocksize_bits, has_huge_files);
  2844. if (le32_to_cpu(es->s_rev_level) == EXT4_GOOD_OLD_REV) {
  2845. sbi->s_inode_size = EXT4_GOOD_OLD_INODE_SIZE;
  2846. sbi->s_first_ino = EXT4_GOOD_OLD_FIRST_INO;
  2847. } else {
  2848. sbi->s_inode_size = le16_to_cpu(es->s_inode_size);
  2849. sbi->s_first_ino = le32_to_cpu(es->s_first_ino);
  2850. if ((sbi->s_inode_size < EXT4_GOOD_OLD_INODE_SIZE) ||
  2851. (!is_power_of_2(sbi->s_inode_size)) ||
  2852. (sbi->s_inode_size > blocksize)) {
  2853. ext4_msg(sb, KERN_ERR,
  2854. "unsupported inode size: %d",
  2855. sbi->s_inode_size);
  2856. goto failed_mount;
  2857. }
  2858. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE)
  2859. sb->s_time_gran = 1 << (EXT4_EPOCH_BITS - 2);
  2860. }
  2861. sbi->s_desc_size = le16_to_cpu(es->s_desc_size);
  2862. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_64BIT)) {
  2863. if (sbi->s_desc_size < EXT4_MIN_DESC_SIZE_64BIT ||
  2864. sbi->s_desc_size > EXT4_MAX_DESC_SIZE ||
  2865. !is_power_of_2(sbi->s_desc_size)) {
  2866. ext4_msg(sb, KERN_ERR,
  2867. "unsupported descriptor size %lu",
  2868. sbi->s_desc_size);
  2869. goto failed_mount;
  2870. }
  2871. } else
  2872. sbi->s_desc_size = EXT4_MIN_DESC_SIZE;
  2873. sbi->s_blocks_per_group = le32_to_cpu(es->s_blocks_per_group);
  2874. sbi->s_inodes_per_group = le32_to_cpu(es->s_inodes_per_group);
  2875. if (EXT4_INODE_SIZE(sb) == 0 || EXT4_INODES_PER_GROUP(sb) == 0)
  2876. goto cantfind_ext4;
  2877. sbi->s_inodes_per_block = blocksize / EXT4_INODE_SIZE(sb);
  2878. if (sbi->s_inodes_per_block == 0)
  2879. goto cantfind_ext4;
  2880. sbi->s_itb_per_group = sbi->s_inodes_per_group /
  2881. sbi->s_inodes_per_block;
  2882. sbi->s_desc_per_block = blocksize / EXT4_DESC_SIZE(sb);
  2883. sbi->s_sbh = bh;
  2884. sbi->s_mount_state = le16_to_cpu(es->s_state);
  2885. sbi->s_addr_per_block_bits = ilog2(EXT4_ADDR_PER_BLOCK(sb));
  2886. sbi->s_desc_per_block_bits = ilog2(EXT4_DESC_PER_BLOCK(sb));
  2887. for (i = 0; i < 4; i++)
  2888. sbi->s_hash_seed[i] = le32_to_cpu(es->s_hash_seed[i]);
  2889. sbi->s_def_hash_version = es->s_def_hash_version;
  2890. i = le32_to_cpu(es->s_flags);
  2891. if (i & EXT2_FLAGS_UNSIGNED_HASH)
  2892. sbi->s_hash_unsigned = 3;
  2893. else if ((i & EXT2_FLAGS_SIGNED_HASH) == 0) {
  2894. #ifdef __CHAR_UNSIGNED__
  2895. es->s_flags |= cpu_to_le32(EXT2_FLAGS_UNSIGNED_HASH);
  2896. sbi->s_hash_unsigned = 3;
  2897. #else
  2898. es->s_flags |= cpu_to_le32(EXT2_FLAGS_SIGNED_HASH);
  2899. #endif
  2900. }
  2901. /* Handle clustersize */
  2902. clustersize = BLOCK_SIZE << le32_to_cpu(es->s_log_cluster_size);
  2903. has_bigalloc = EXT4_HAS_RO_COMPAT_FEATURE(sb,
  2904. EXT4_FEATURE_RO_COMPAT_BIGALLOC);
  2905. if (has_bigalloc) {
  2906. if (clustersize < blocksize) {
  2907. ext4_msg(sb, KERN_ERR,
  2908. "cluster size (%d) smaller than "
  2909. "block size (%d)", clustersize, blocksize);
  2910. goto failed_mount;
  2911. }
  2912. sbi->s_cluster_bits = le32_to_cpu(es->s_log_cluster_size) -
  2913. le32_to_cpu(es->s_log_block_size);
  2914. sbi->s_clusters_per_group =
  2915. le32_to_cpu(es->s_clusters_per_group);
  2916. if (sbi->s_clusters_per_group > blocksize * 8) {
  2917. ext4_msg(sb, KERN_ERR,
  2918. "#clusters per group too big: %lu",
  2919. sbi->s_clusters_per_group);
  2920. goto failed_mount;
  2921. }
  2922. if (sbi->s_blocks_per_group !=
  2923. (sbi->s_clusters_per_group * (clustersize / blocksize))) {
  2924. ext4_msg(sb, KERN_ERR, "blocks per group (%lu) and "
  2925. "clusters per group (%lu) inconsistent",
  2926. sbi->s_blocks_per_group,
  2927. sbi->s_clusters_per_group);
  2928. goto failed_mount;
  2929. }
  2930. } else {
  2931. if (clustersize != blocksize) {
  2932. ext4_warning(sb, "fragment/cluster size (%d) != "
  2933. "block size (%d)", clustersize,
  2934. blocksize);
  2935. clustersize = blocksize;
  2936. }
  2937. if (sbi->s_blocks_per_group > blocksize * 8) {
  2938. ext4_msg(sb, KERN_ERR,
  2939. "#blocks per group too big: %lu",
  2940. sbi->s_blocks_per_group);
  2941. goto failed_mount;
  2942. }
  2943. sbi->s_clusters_per_group = sbi->s_blocks_per_group;
  2944. sbi->s_cluster_bits = 0;
  2945. }
  2946. sbi->s_cluster_ratio = clustersize / blocksize;
  2947. if (sbi->s_inodes_per_group > blocksize * 8) {
  2948. ext4_msg(sb, KERN_ERR,
  2949. "#inodes per group too big: %lu",
  2950. sbi->s_inodes_per_group);
  2951. goto failed_mount;
  2952. }
  2953. /*
  2954. * Test whether we have more sectors than will fit in sector_t,
  2955. * and whether the max offset is addressable by the page cache.
  2956. */
  2957. err = generic_check_addressable(sb->s_blocksize_bits,
  2958. ext4_blocks_count(es));
  2959. if (err) {
  2960. ext4_msg(sb, KERN_ERR, "filesystem"
  2961. " too large to mount safely on this system");
  2962. if (sizeof(sector_t) < 8)
  2963. ext4_msg(sb, KERN_WARNING, "CONFIG_LBDAF not enabled");
  2964. ret = err;
  2965. goto failed_mount;
  2966. }
  2967. if (EXT4_BLOCKS_PER_GROUP(sb) == 0)
  2968. goto cantfind_ext4;
  2969. /* check blocks count against device size */
  2970. blocks_count = sb->s_bdev->bd_inode->i_size >> sb->s_blocksize_bits;
  2971. if (blocks_count && ext4_blocks_count(es) > blocks_count) {
  2972. ext4_msg(sb, KERN_WARNING, "bad geometry: block count %llu "
  2973. "exceeds size of device (%llu blocks)",
  2974. ext4_blocks_count(es), blocks_count);
  2975. goto failed_mount;
  2976. }
  2977. /*
  2978. * It makes no sense for the first data block to be beyond the end
  2979. * of the filesystem.
  2980. */
  2981. if (le32_to_cpu(es->s_first_data_block) >= ext4_blocks_count(es)) {
  2982. ext4_msg(sb, KERN_WARNING, "bad geometry: first data "
  2983. "block %u is beyond end of filesystem (%llu)",
  2984. le32_to_cpu(es->s_first_data_block),
  2985. ext4_blocks_count(es));
  2986. goto failed_mount;
  2987. }
  2988. blocks_count = (ext4_blocks_count(es) -
  2989. le32_to_cpu(es->s_first_data_block) +
  2990. EXT4_BLOCKS_PER_GROUP(sb) - 1);
  2991. do_div(blocks_count, EXT4_BLOCKS_PER_GROUP(sb));
  2992. if (blocks_count > ((uint64_t)1<<32) - EXT4_DESC_PER_BLOCK(sb)) {
  2993. ext4_msg(sb, KERN_WARNING, "groups count too large: %u "
  2994. "(block count %llu, first data block %u, "
  2995. "blocks per group %lu)", sbi->s_groups_count,
  2996. ext4_blocks_count(es),
  2997. le32_to_cpu(es->s_first_data_block),
  2998. EXT4_BLOCKS_PER_GROUP(sb));
  2999. goto failed_mount;
  3000. }
  3001. sbi->s_groups_count = blocks_count;
  3002. sbi->s_blockfile_groups = min_t(ext4_group_t, sbi->s_groups_count,
  3003. (EXT4_MAX_BLOCK_FILE_PHYS / EXT4_BLOCKS_PER_GROUP(sb)));
  3004. db_count = (sbi->s_groups_count + EXT4_DESC_PER_BLOCK(sb) - 1) /
  3005. EXT4_DESC_PER_BLOCK(sb);
  3006. sbi->s_group_desc = ext4_kvmalloc(db_count *
  3007. sizeof(struct buffer_head *),
  3008. GFP_KERNEL);
  3009. if (sbi->s_group_desc == NULL) {
  3010. ext4_msg(sb, KERN_ERR, "not enough memory");
  3011. goto failed_mount;
  3012. }
  3013. if (ext4_proc_root)
  3014. sbi->s_proc = proc_mkdir(sb->s_id, ext4_proc_root);
  3015. if (sbi->s_proc)
  3016. proc_create_data("options", S_IRUGO, sbi->s_proc,
  3017. &ext4_seq_options_fops, sb);
  3018. bgl_lock_init(sbi->s_blockgroup_lock);
  3019. for (i = 0; i < db_count; i++) {
  3020. block = descriptor_loc(sb, logical_sb_block, i);
  3021. sbi->s_group_desc[i] = sb_bread(sb, block);
  3022. if (!sbi->s_group_desc[i]) {
  3023. ext4_msg(sb, KERN_ERR,
  3024. "can't read group descriptor %d", i);
  3025. db_count = i;
  3026. goto failed_mount2;
  3027. }
  3028. }
  3029. if (!ext4_check_descriptors(sb, &first_not_zeroed)) {
  3030. ext4_msg(sb, KERN_ERR, "group descriptors corrupted!");
  3031. goto failed_mount2;
  3032. }
  3033. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
  3034. if (!ext4_fill_flex_info(sb)) {
  3035. ext4_msg(sb, KERN_ERR,
  3036. "unable to initialize "
  3037. "flex_bg meta info!");
  3038. goto failed_mount2;
  3039. }
  3040. sbi->s_gdb_count = db_count;
  3041. get_random_bytes(&sbi->s_next_generation, sizeof(u32));
  3042. spin_lock_init(&sbi->s_next_gen_lock);
  3043. init_timer(&sbi->s_err_report);
  3044. sbi->s_err_report.function = print_daily_error_info;
  3045. sbi->s_err_report.data = (unsigned long) sb;
  3046. err = percpu_counter_init(&sbi->s_freeclusters_counter,
  3047. ext4_count_free_clusters(sb));
  3048. if (!err) {
  3049. err = percpu_counter_init(&sbi->s_freeinodes_counter,
  3050. ext4_count_free_inodes(sb));
  3051. }
  3052. if (!err) {
  3053. err = percpu_counter_init(&sbi->s_dirs_counter,
  3054. ext4_count_dirs(sb));
  3055. }
  3056. if (!err) {
  3057. err = percpu_counter_init(&sbi->s_dirtyclusters_counter, 0);
  3058. }
  3059. if (err) {
  3060. ext4_msg(sb, KERN_ERR, "insufficient memory");
  3061. goto failed_mount3;
  3062. }
  3063. sbi->s_stripe = ext4_get_stripe_size(sbi);
  3064. sbi->s_max_writeback_mb_bump = 128;
  3065. /*
  3066. * set up enough so that it can read an inode
  3067. */
  3068. if (!test_opt(sb, NOLOAD) &&
  3069. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  3070. sb->s_op = &ext4_sops;
  3071. else
  3072. sb->s_op = &ext4_nojournal_sops;
  3073. sb->s_export_op = &ext4_export_ops;
  3074. sb->s_xattr = ext4_xattr_handlers;
  3075. #ifdef CONFIG_QUOTA
  3076. sb->s_qcop = &ext4_qctl_operations;
  3077. sb->dq_op = &ext4_quota_operations;
  3078. #endif
  3079. memcpy(sb->s_uuid, es->s_uuid, sizeof(es->s_uuid));
  3080. INIT_LIST_HEAD(&sbi->s_orphan); /* unlinked but open files */
  3081. mutex_init(&sbi->s_orphan_lock);
  3082. sbi->s_resize_flags = 0;
  3083. sb->s_root = NULL;
  3084. needs_recovery = (es->s_last_orphan != 0 ||
  3085. EXT4_HAS_INCOMPAT_FEATURE(sb,
  3086. EXT4_FEATURE_INCOMPAT_RECOVER));
  3087. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_MMP) &&
  3088. !(sb->s_flags & MS_RDONLY))
  3089. if (ext4_multi_mount_protect(sb, le64_to_cpu(es->s_mmp_block)))
  3090. goto failed_mount3;
  3091. /*
  3092. * The first inode we look at is the journal inode. Don't try
  3093. * root first: it may be modified in the journal!
  3094. */
  3095. if (!test_opt(sb, NOLOAD) &&
  3096. EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3097. if (ext4_load_journal(sb, es, journal_devnum))
  3098. goto failed_mount3;
  3099. } else if (test_opt(sb, NOLOAD) && !(sb->s_flags & MS_RDONLY) &&
  3100. EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  3101. ext4_msg(sb, KERN_ERR, "required journal recovery "
  3102. "suppressed and not mounted read-only");
  3103. goto failed_mount_wq;
  3104. } else {
  3105. clear_opt(sb, DATA_FLAGS);
  3106. sbi->s_journal = NULL;
  3107. needs_recovery = 0;
  3108. goto no_journal;
  3109. }
  3110. if (ext4_blocks_count(es) > 0xffffffffULL &&
  3111. !jbd2_journal_set_features(EXT4_SB(sb)->s_journal, 0, 0,
  3112. JBD2_FEATURE_INCOMPAT_64BIT)) {
  3113. ext4_msg(sb, KERN_ERR, "Failed to set 64-bit journal feature");
  3114. goto failed_mount_wq;
  3115. }
  3116. if (test_opt(sb, JOURNAL_ASYNC_COMMIT)) {
  3117. jbd2_journal_set_features(sbi->s_journal,
  3118. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  3119. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3120. } else if (test_opt(sb, JOURNAL_CHECKSUM)) {
  3121. jbd2_journal_set_features(sbi->s_journal,
  3122. JBD2_FEATURE_COMPAT_CHECKSUM, 0, 0);
  3123. jbd2_journal_clear_features(sbi->s_journal, 0, 0,
  3124. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3125. } else {
  3126. jbd2_journal_clear_features(sbi->s_journal,
  3127. JBD2_FEATURE_COMPAT_CHECKSUM, 0,
  3128. JBD2_FEATURE_INCOMPAT_ASYNC_COMMIT);
  3129. }
  3130. /* We have now updated the journal if required, so we can
  3131. * validate the data journaling mode. */
  3132. switch (test_opt(sb, DATA_FLAGS)) {
  3133. case 0:
  3134. /* No mode set, assume a default based on the journal
  3135. * capabilities: ORDERED_DATA if the journal can
  3136. * cope, else JOURNAL_DATA
  3137. */
  3138. if (jbd2_journal_check_available_features
  3139. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE))
  3140. set_opt(sb, ORDERED_DATA);
  3141. else
  3142. set_opt(sb, JOURNAL_DATA);
  3143. break;
  3144. case EXT4_MOUNT_ORDERED_DATA:
  3145. case EXT4_MOUNT_WRITEBACK_DATA:
  3146. if (!jbd2_journal_check_available_features
  3147. (sbi->s_journal, 0, 0, JBD2_FEATURE_INCOMPAT_REVOKE)) {
  3148. ext4_msg(sb, KERN_ERR, "Journal does not support "
  3149. "requested data journaling mode");
  3150. goto failed_mount_wq;
  3151. }
  3152. default:
  3153. break;
  3154. }
  3155. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3156. sbi->s_journal->j_commit_callback = ext4_journal_commit_callback;
  3157. /*
  3158. * The journal may have updated the bg summary counts, so we
  3159. * need to update the global counters.
  3160. */
  3161. percpu_counter_set(&sbi->s_freeclusters_counter,
  3162. ext4_count_free_clusters(sb));
  3163. percpu_counter_set(&sbi->s_freeinodes_counter,
  3164. ext4_count_free_inodes(sb));
  3165. percpu_counter_set(&sbi->s_dirs_counter,
  3166. ext4_count_dirs(sb));
  3167. percpu_counter_set(&sbi->s_dirtyclusters_counter, 0);
  3168. no_journal:
  3169. /*
  3170. * The maximum number of concurrent works can be high and
  3171. * concurrency isn't really necessary. Limit it to 1.
  3172. */
  3173. EXT4_SB(sb)->dio_unwritten_wq =
  3174. alloc_workqueue("ext4-dio-unwritten", WQ_MEM_RECLAIM | WQ_UNBOUND, 1);
  3175. if (!EXT4_SB(sb)->dio_unwritten_wq) {
  3176. printk(KERN_ERR "EXT4-fs: failed to create DIO workqueue\n");
  3177. goto failed_mount_wq;
  3178. }
  3179. /*
  3180. * The jbd2_journal_load will have done any necessary log recovery,
  3181. * so we can safely mount the rest of the filesystem now.
  3182. */
  3183. root = ext4_iget(sb, EXT4_ROOT_INO);
  3184. if (IS_ERR(root)) {
  3185. ext4_msg(sb, KERN_ERR, "get root inode failed");
  3186. ret = PTR_ERR(root);
  3187. root = NULL;
  3188. goto failed_mount4;
  3189. }
  3190. if (!S_ISDIR(root->i_mode) || !root->i_blocks || !root->i_size) {
  3191. ext4_msg(sb, KERN_ERR, "corrupt root inode, run e2fsck");
  3192. iput(root);
  3193. goto failed_mount4;
  3194. }
  3195. sb->s_root = d_make_root(root);
  3196. if (!sb->s_root) {
  3197. ext4_msg(sb, KERN_ERR, "get root dentry failed");
  3198. ret = -ENOMEM;
  3199. goto failed_mount4;
  3200. }
  3201. ext4_setup_super(sb, es, sb->s_flags & MS_RDONLY);
  3202. /* determine the minimum size of new large inodes, if present */
  3203. if (sbi->s_inode_size > EXT4_GOOD_OLD_INODE_SIZE) {
  3204. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3205. EXT4_GOOD_OLD_INODE_SIZE;
  3206. if (EXT4_HAS_RO_COMPAT_FEATURE(sb,
  3207. EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE)) {
  3208. if (sbi->s_want_extra_isize <
  3209. le16_to_cpu(es->s_want_extra_isize))
  3210. sbi->s_want_extra_isize =
  3211. le16_to_cpu(es->s_want_extra_isize);
  3212. if (sbi->s_want_extra_isize <
  3213. le16_to_cpu(es->s_min_extra_isize))
  3214. sbi->s_want_extra_isize =
  3215. le16_to_cpu(es->s_min_extra_isize);
  3216. }
  3217. }
  3218. /* Check if enough inode space is available */
  3219. if (EXT4_GOOD_OLD_INODE_SIZE + sbi->s_want_extra_isize >
  3220. sbi->s_inode_size) {
  3221. sbi->s_want_extra_isize = sizeof(struct ext4_inode) -
  3222. EXT4_GOOD_OLD_INODE_SIZE;
  3223. ext4_msg(sb, KERN_INFO, "required extra inode space not"
  3224. "available");
  3225. }
  3226. err = ext4_setup_system_zone(sb);
  3227. if (err) {
  3228. ext4_msg(sb, KERN_ERR, "failed to initialize system "
  3229. "zone (%d)", err);
  3230. goto failed_mount4a;
  3231. }
  3232. ext4_ext_init(sb);
  3233. err = ext4_mb_init(sb, needs_recovery);
  3234. if (err) {
  3235. ext4_msg(sb, KERN_ERR, "failed to initialize mballoc (%d)",
  3236. err);
  3237. goto failed_mount5;
  3238. }
  3239. err = ext4_register_li_request(sb, first_not_zeroed);
  3240. if (err)
  3241. goto failed_mount6;
  3242. sbi->s_kobj.kset = ext4_kset;
  3243. init_completion(&sbi->s_kobj_unregister);
  3244. err = kobject_init_and_add(&sbi->s_kobj, &ext4_ktype, NULL,
  3245. "%s", sb->s_id);
  3246. if (err)
  3247. goto failed_mount7;
  3248. EXT4_SB(sb)->s_mount_state |= EXT4_ORPHAN_FS;
  3249. ext4_orphan_cleanup(sb, es);
  3250. EXT4_SB(sb)->s_mount_state &= ~EXT4_ORPHAN_FS;
  3251. if (needs_recovery) {
  3252. ext4_msg(sb, KERN_INFO, "recovery complete");
  3253. ext4_mark_recovery_complete(sb, es);
  3254. }
  3255. if (EXT4_SB(sb)->s_journal) {
  3256. if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA)
  3257. descr = " journalled data mode";
  3258. else if (test_opt(sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  3259. descr = " ordered data mode";
  3260. else
  3261. descr = " writeback data mode";
  3262. } else
  3263. descr = "out journal";
  3264. ext4_msg(sb, KERN_INFO, "mounted filesystem with%s. "
  3265. "Opts: %s%s%s", descr, sbi->s_es->s_mount_opts,
  3266. *sbi->s_es->s_mount_opts ? "; " : "", orig_data);
  3267. if (es->s_error_count)
  3268. mod_timer(&sbi->s_err_report, jiffies + 300*HZ); /* 5 minutes */
  3269. kfree(orig_data);
  3270. return 0;
  3271. cantfind_ext4:
  3272. if (!silent)
  3273. ext4_msg(sb, KERN_ERR, "VFS: Can't find ext4 filesystem");
  3274. goto failed_mount;
  3275. failed_mount7:
  3276. ext4_unregister_li_request(sb);
  3277. failed_mount6:
  3278. ext4_mb_release(sb);
  3279. failed_mount5:
  3280. ext4_ext_release(sb);
  3281. ext4_release_system_zone(sb);
  3282. failed_mount4a:
  3283. dput(sb->s_root);
  3284. sb->s_root = NULL;
  3285. failed_mount4:
  3286. ext4_msg(sb, KERN_ERR, "mount failed");
  3287. destroy_workqueue(EXT4_SB(sb)->dio_unwritten_wq);
  3288. failed_mount_wq:
  3289. if (sbi->s_journal) {
  3290. jbd2_journal_destroy(sbi->s_journal);
  3291. sbi->s_journal = NULL;
  3292. }
  3293. failed_mount3:
  3294. del_timer(&sbi->s_err_report);
  3295. if (sbi->s_flex_groups)
  3296. ext4_kvfree(sbi->s_flex_groups);
  3297. percpu_counter_destroy(&sbi->s_freeclusters_counter);
  3298. percpu_counter_destroy(&sbi->s_freeinodes_counter);
  3299. percpu_counter_destroy(&sbi->s_dirs_counter);
  3300. percpu_counter_destroy(&sbi->s_dirtyclusters_counter);
  3301. if (sbi->s_mmp_tsk)
  3302. kthread_stop(sbi->s_mmp_tsk);
  3303. failed_mount2:
  3304. for (i = 0; i < db_count; i++)
  3305. brelse(sbi->s_group_desc[i]);
  3306. ext4_kvfree(sbi->s_group_desc);
  3307. failed_mount:
  3308. if (sbi->s_proc) {
  3309. remove_proc_entry("options", sbi->s_proc);
  3310. remove_proc_entry(sb->s_id, ext4_proc_root);
  3311. }
  3312. #ifdef CONFIG_QUOTA
  3313. for (i = 0; i < MAXQUOTAS; i++)
  3314. kfree(sbi->s_qf_names[i]);
  3315. #endif
  3316. ext4_blkdev_remove(sbi);
  3317. brelse(bh);
  3318. out_fail:
  3319. sb->s_fs_info = NULL;
  3320. kfree(sbi->s_blockgroup_lock);
  3321. kfree(sbi);
  3322. out_free_orig:
  3323. kfree(orig_data);
  3324. return ret;
  3325. }
  3326. /*
  3327. * Setup any per-fs journal parameters now. We'll do this both on
  3328. * initial mount, once the journal has been initialised but before we've
  3329. * done any recovery; and again on any subsequent remount.
  3330. */
  3331. static void ext4_init_journal_params(struct super_block *sb, journal_t *journal)
  3332. {
  3333. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3334. journal->j_commit_interval = sbi->s_commit_interval;
  3335. journal->j_min_batch_time = sbi->s_min_batch_time;
  3336. journal->j_max_batch_time = sbi->s_max_batch_time;
  3337. write_lock(&journal->j_state_lock);
  3338. if (test_opt(sb, BARRIER))
  3339. journal->j_flags |= JBD2_BARRIER;
  3340. else
  3341. journal->j_flags &= ~JBD2_BARRIER;
  3342. if (test_opt(sb, DATA_ERR_ABORT))
  3343. journal->j_flags |= JBD2_ABORT_ON_SYNCDATA_ERR;
  3344. else
  3345. journal->j_flags &= ~JBD2_ABORT_ON_SYNCDATA_ERR;
  3346. write_unlock(&journal->j_state_lock);
  3347. }
  3348. static journal_t *ext4_get_journal(struct super_block *sb,
  3349. unsigned int journal_inum)
  3350. {
  3351. struct inode *journal_inode;
  3352. journal_t *journal;
  3353. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3354. /* First, test for the existence of a valid inode on disk. Bad
  3355. * things happen if we iget() an unused inode, as the subsequent
  3356. * iput() will try to delete it. */
  3357. journal_inode = ext4_iget(sb, journal_inum);
  3358. if (IS_ERR(journal_inode)) {
  3359. ext4_msg(sb, KERN_ERR, "no journal found");
  3360. return NULL;
  3361. }
  3362. if (!journal_inode->i_nlink) {
  3363. make_bad_inode(journal_inode);
  3364. iput(journal_inode);
  3365. ext4_msg(sb, KERN_ERR, "journal inode is deleted");
  3366. return NULL;
  3367. }
  3368. jbd_debug(2, "Journal inode found at %p: %lld bytes\n",
  3369. journal_inode, journal_inode->i_size);
  3370. if (!S_ISREG(journal_inode->i_mode)) {
  3371. ext4_msg(sb, KERN_ERR, "invalid journal inode");
  3372. iput(journal_inode);
  3373. return NULL;
  3374. }
  3375. journal = jbd2_journal_init_inode(journal_inode);
  3376. if (!journal) {
  3377. ext4_msg(sb, KERN_ERR, "Could not load journal inode");
  3378. iput(journal_inode);
  3379. return NULL;
  3380. }
  3381. journal->j_private = sb;
  3382. ext4_init_journal_params(sb, journal);
  3383. return journal;
  3384. }
  3385. static journal_t *ext4_get_dev_journal(struct super_block *sb,
  3386. dev_t j_dev)
  3387. {
  3388. struct buffer_head *bh;
  3389. journal_t *journal;
  3390. ext4_fsblk_t start;
  3391. ext4_fsblk_t len;
  3392. int hblock, blocksize;
  3393. ext4_fsblk_t sb_block;
  3394. unsigned long offset;
  3395. struct ext4_super_block *es;
  3396. struct block_device *bdev;
  3397. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3398. bdev = ext4_blkdev_get(j_dev, sb);
  3399. if (bdev == NULL)
  3400. return NULL;
  3401. blocksize = sb->s_blocksize;
  3402. hblock = bdev_logical_block_size(bdev);
  3403. if (blocksize < hblock) {
  3404. ext4_msg(sb, KERN_ERR,
  3405. "blocksize too small for journal device");
  3406. goto out_bdev;
  3407. }
  3408. sb_block = EXT4_MIN_BLOCK_SIZE / blocksize;
  3409. offset = EXT4_MIN_BLOCK_SIZE % blocksize;
  3410. set_blocksize(bdev, blocksize);
  3411. if (!(bh = __bread(bdev, sb_block, blocksize))) {
  3412. ext4_msg(sb, KERN_ERR, "couldn't read superblock of "
  3413. "external journal");
  3414. goto out_bdev;
  3415. }
  3416. es = (struct ext4_super_block *) (((char *)bh->b_data) + offset);
  3417. if ((le16_to_cpu(es->s_magic) != EXT4_SUPER_MAGIC) ||
  3418. !(le32_to_cpu(es->s_feature_incompat) &
  3419. EXT4_FEATURE_INCOMPAT_JOURNAL_DEV)) {
  3420. ext4_msg(sb, KERN_ERR, "external journal has "
  3421. "bad superblock");
  3422. brelse(bh);
  3423. goto out_bdev;
  3424. }
  3425. if (memcmp(EXT4_SB(sb)->s_es->s_journal_uuid, es->s_uuid, 16)) {
  3426. ext4_msg(sb, KERN_ERR, "journal UUID does not match");
  3427. brelse(bh);
  3428. goto out_bdev;
  3429. }
  3430. len = ext4_blocks_count(es);
  3431. start = sb_block + 1;
  3432. brelse(bh); /* we're done with the superblock */
  3433. journal = jbd2_journal_init_dev(bdev, sb->s_bdev,
  3434. start, len, blocksize);
  3435. if (!journal) {
  3436. ext4_msg(sb, KERN_ERR, "failed to create device journal");
  3437. goto out_bdev;
  3438. }
  3439. journal->j_private = sb;
  3440. ll_rw_block(READ, 1, &journal->j_sb_buffer);
  3441. wait_on_buffer(journal->j_sb_buffer);
  3442. if (!buffer_uptodate(journal->j_sb_buffer)) {
  3443. ext4_msg(sb, KERN_ERR, "I/O error on journal device");
  3444. goto out_journal;
  3445. }
  3446. if (be32_to_cpu(journal->j_superblock->s_nr_users) != 1) {
  3447. ext4_msg(sb, KERN_ERR, "External journal has more than one "
  3448. "user (unsupported) - %d",
  3449. be32_to_cpu(journal->j_superblock->s_nr_users));
  3450. goto out_journal;
  3451. }
  3452. EXT4_SB(sb)->journal_bdev = bdev;
  3453. ext4_init_journal_params(sb, journal);
  3454. return journal;
  3455. out_journal:
  3456. jbd2_journal_destroy(journal);
  3457. out_bdev:
  3458. ext4_blkdev_put(bdev);
  3459. return NULL;
  3460. }
  3461. static int ext4_load_journal(struct super_block *sb,
  3462. struct ext4_super_block *es,
  3463. unsigned long journal_devnum)
  3464. {
  3465. journal_t *journal;
  3466. unsigned int journal_inum = le32_to_cpu(es->s_journal_inum);
  3467. dev_t journal_dev;
  3468. int err = 0;
  3469. int really_read_only;
  3470. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3471. if (journal_devnum &&
  3472. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3473. ext4_msg(sb, KERN_INFO, "external journal device major/minor "
  3474. "numbers have changed");
  3475. journal_dev = new_decode_dev(journal_devnum);
  3476. } else
  3477. journal_dev = new_decode_dev(le32_to_cpu(es->s_journal_dev));
  3478. really_read_only = bdev_read_only(sb->s_bdev);
  3479. /*
  3480. * Are we loading a blank journal or performing recovery after a
  3481. * crash? For recovery, we need to check in advance whether we
  3482. * can get read-write access to the device.
  3483. */
  3484. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER)) {
  3485. if (sb->s_flags & MS_RDONLY) {
  3486. ext4_msg(sb, KERN_INFO, "INFO: recovery "
  3487. "required on readonly filesystem");
  3488. if (really_read_only) {
  3489. ext4_msg(sb, KERN_ERR, "write access "
  3490. "unavailable, cannot proceed");
  3491. return -EROFS;
  3492. }
  3493. ext4_msg(sb, KERN_INFO, "write access will "
  3494. "be enabled during recovery");
  3495. }
  3496. }
  3497. if (journal_inum && journal_dev) {
  3498. ext4_msg(sb, KERN_ERR, "filesystem has both journal "
  3499. "and inode journals!");
  3500. return -EINVAL;
  3501. }
  3502. if (journal_inum) {
  3503. if (!(journal = ext4_get_journal(sb, journal_inum)))
  3504. return -EINVAL;
  3505. } else {
  3506. if (!(journal = ext4_get_dev_journal(sb, journal_dev)))
  3507. return -EINVAL;
  3508. }
  3509. if (!(journal->j_flags & JBD2_BARRIER))
  3510. ext4_msg(sb, KERN_INFO, "barriers disabled");
  3511. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER))
  3512. err = jbd2_journal_wipe(journal, !really_read_only);
  3513. if (!err) {
  3514. char *save = kmalloc(EXT4_S_ERR_LEN, GFP_KERNEL);
  3515. if (save)
  3516. memcpy(save, ((char *) es) +
  3517. EXT4_S_ERR_START, EXT4_S_ERR_LEN);
  3518. err = jbd2_journal_load(journal);
  3519. if (save)
  3520. memcpy(((char *) es) + EXT4_S_ERR_START,
  3521. save, EXT4_S_ERR_LEN);
  3522. kfree(save);
  3523. }
  3524. if (err) {
  3525. ext4_msg(sb, KERN_ERR, "error loading journal");
  3526. jbd2_journal_destroy(journal);
  3527. return err;
  3528. }
  3529. EXT4_SB(sb)->s_journal = journal;
  3530. ext4_clear_journal_err(sb, es);
  3531. if (!really_read_only && journal_devnum &&
  3532. journal_devnum != le32_to_cpu(es->s_journal_dev)) {
  3533. es->s_journal_dev = cpu_to_le32(journal_devnum);
  3534. /* Make sure we flush the recovery flag to disk. */
  3535. ext4_commit_super(sb, 1);
  3536. }
  3537. return 0;
  3538. }
  3539. static int ext4_commit_super(struct super_block *sb, int sync)
  3540. {
  3541. struct ext4_super_block *es = EXT4_SB(sb)->s_es;
  3542. struct buffer_head *sbh = EXT4_SB(sb)->s_sbh;
  3543. int error = 0;
  3544. if (!sbh || block_device_ejected(sb))
  3545. return error;
  3546. if (buffer_write_io_error(sbh)) {
  3547. /*
  3548. * Oh, dear. A previous attempt to write the
  3549. * superblock failed. This could happen because the
  3550. * USB device was yanked out. Or it could happen to
  3551. * be a transient write error and maybe the block will
  3552. * be remapped. Nothing we can do but to retry the
  3553. * write and hope for the best.
  3554. */
  3555. ext4_msg(sb, KERN_ERR, "previous I/O error to "
  3556. "superblock detected");
  3557. clear_buffer_write_io_error(sbh);
  3558. set_buffer_uptodate(sbh);
  3559. }
  3560. /*
  3561. * If the file system is mounted read-only, don't update the
  3562. * superblock write time. This avoids updating the superblock
  3563. * write time when we are mounting the root file system
  3564. * read/only but we need to replay the journal; at that point,
  3565. * for people who are east of GMT and who make their clock
  3566. * tick in localtime for Windows bug-for-bug compatibility,
  3567. * the clock is set in the future, and this will cause e2fsck
  3568. * to complain and force a full file system check.
  3569. */
  3570. if (!(sb->s_flags & MS_RDONLY))
  3571. es->s_wtime = cpu_to_le32(get_seconds());
  3572. if (sb->s_bdev->bd_part)
  3573. es->s_kbytes_written =
  3574. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written +
  3575. ((part_stat_read(sb->s_bdev->bd_part, sectors[1]) -
  3576. EXT4_SB(sb)->s_sectors_written_start) >> 1));
  3577. else
  3578. es->s_kbytes_written =
  3579. cpu_to_le64(EXT4_SB(sb)->s_kbytes_written);
  3580. ext4_free_blocks_count_set(es,
  3581. EXT4_C2B(EXT4_SB(sb), percpu_counter_sum_positive(
  3582. &EXT4_SB(sb)->s_freeclusters_counter)));
  3583. es->s_free_inodes_count =
  3584. cpu_to_le32(percpu_counter_sum_positive(
  3585. &EXT4_SB(sb)->s_freeinodes_counter));
  3586. sb->s_dirt = 0;
  3587. BUFFER_TRACE(sbh, "marking dirty");
  3588. mark_buffer_dirty(sbh);
  3589. if (sync) {
  3590. error = sync_dirty_buffer(sbh);
  3591. if (error)
  3592. return error;
  3593. error = buffer_write_io_error(sbh);
  3594. if (error) {
  3595. ext4_msg(sb, KERN_ERR, "I/O error while writing "
  3596. "superblock");
  3597. clear_buffer_write_io_error(sbh);
  3598. set_buffer_uptodate(sbh);
  3599. }
  3600. }
  3601. return error;
  3602. }
  3603. /*
  3604. * Have we just finished recovery? If so, and if we are mounting (or
  3605. * remounting) the filesystem readonly, then we will end up with a
  3606. * consistent fs on disk. Record that fact.
  3607. */
  3608. static void ext4_mark_recovery_complete(struct super_block *sb,
  3609. struct ext4_super_block *es)
  3610. {
  3611. journal_t *journal = EXT4_SB(sb)->s_journal;
  3612. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL)) {
  3613. BUG_ON(journal != NULL);
  3614. return;
  3615. }
  3616. jbd2_journal_lock_updates(journal);
  3617. if (jbd2_journal_flush(journal) < 0)
  3618. goto out;
  3619. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER) &&
  3620. sb->s_flags & MS_RDONLY) {
  3621. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3622. ext4_commit_super(sb, 1);
  3623. }
  3624. out:
  3625. jbd2_journal_unlock_updates(journal);
  3626. }
  3627. /*
  3628. * If we are mounting (or read-write remounting) a filesystem whose journal
  3629. * has recorded an error from a previous lifetime, move that error to the
  3630. * main filesystem now.
  3631. */
  3632. static void ext4_clear_journal_err(struct super_block *sb,
  3633. struct ext4_super_block *es)
  3634. {
  3635. journal_t *journal;
  3636. int j_errno;
  3637. const char *errstr;
  3638. BUG_ON(!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL));
  3639. journal = EXT4_SB(sb)->s_journal;
  3640. /*
  3641. * Now check for any error status which may have been recorded in the
  3642. * journal by a prior ext4_error() or ext4_abort()
  3643. */
  3644. j_errno = jbd2_journal_errno(journal);
  3645. if (j_errno) {
  3646. char nbuf[16];
  3647. errstr = ext4_decode_error(sb, j_errno, nbuf);
  3648. ext4_warning(sb, "Filesystem error recorded "
  3649. "from previous mount: %s", errstr);
  3650. ext4_warning(sb, "Marking fs in need of filesystem check.");
  3651. EXT4_SB(sb)->s_mount_state |= EXT4_ERROR_FS;
  3652. es->s_state |= cpu_to_le16(EXT4_ERROR_FS);
  3653. ext4_commit_super(sb, 1);
  3654. jbd2_journal_clear_err(journal);
  3655. }
  3656. }
  3657. /*
  3658. * Force the running and committing transactions to commit,
  3659. * and wait on the commit.
  3660. */
  3661. int ext4_force_commit(struct super_block *sb)
  3662. {
  3663. journal_t *journal;
  3664. int ret = 0;
  3665. if (sb->s_flags & MS_RDONLY)
  3666. return 0;
  3667. journal = EXT4_SB(sb)->s_journal;
  3668. if (journal) {
  3669. vfs_check_frozen(sb, SB_FREEZE_TRANS);
  3670. ret = ext4_journal_force_commit(journal);
  3671. }
  3672. return ret;
  3673. }
  3674. static void ext4_write_super(struct super_block *sb)
  3675. {
  3676. lock_super(sb);
  3677. ext4_commit_super(sb, 1);
  3678. unlock_super(sb);
  3679. }
  3680. static int ext4_sync_fs(struct super_block *sb, int wait)
  3681. {
  3682. int ret = 0;
  3683. tid_t target;
  3684. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3685. trace_ext4_sync_fs(sb, wait);
  3686. flush_workqueue(sbi->dio_unwritten_wq);
  3687. if (jbd2_journal_start_commit(sbi->s_journal, &target)) {
  3688. if (wait)
  3689. jbd2_log_wait_commit(sbi->s_journal, target);
  3690. }
  3691. return ret;
  3692. }
  3693. /*
  3694. * LVM calls this function before a (read-only) snapshot is created. This
  3695. * gives us a chance to flush the journal completely and mark the fs clean.
  3696. *
  3697. * Note that only this function cannot bring a filesystem to be in a clean
  3698. * state independently, because ext4 prevents a new handle from being started
  3699. * by @sb->s_frozen, which stays in an upper layer. It thus needs help from
  3700. * the upper layer.
  3701. */
  3702. static int ext4_freeze(struct super_block *sb)
  3703. {
  3704. int error = 0;
  3705. journal_t *journal;
  3706. if (sb->s_flags & MS_RDONLY)
  3707. return 0;
  3708. journal = EXT4_SB(sb)->s_journal;
  3709. /* Now we set up the journal barrier. */
  3710. jbd2_journal_lock_updates(journal);
  3711. /*
  3712. * Don't clear the needs_recovery flag if we failed to flush
  3713. * the journal.
  3714. */
  3715. error = jbd2_journal_flush(journal);
  3716. if (error < 0)
  3717. goto out;
  3718. /* Journal blocked and flushed, clear needs_recovery flag. */
  3719. EXT4_CLEAR_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3720. error = ext4_commit_super(sb, 1);
  3721. out:
  3722. /* we rely on s_frozen to stop further updates */
  3723. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  3724. return error;
  3725. }
  3726. /*
  3727. * Called by LVM after the snapshot is done. We need to reset the RECOVER
  3728. * flag here, even though the filesystem is not technically dirty yet.
  3729. */
  3730. static int ext4_unfreeze(struct super_block *sb)
  3731. {
  3732. if (sb->s_flags & MS_RDONLY)
  3733. return 0;
  3734. lock_super(sb);
  3735. /* Reset the needs_recovery flag before the fs is unlocked. */
  3736. EXT4_SET_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_RECOVER);
  3737. ext4_commit_super(sb, 1);
  3738. unlock_super(sb);
  3739. return 0;
  3740. }
  3741. /*
  3742. * Structure to save mount options for ext4_remount's benefit
  3743. */
  3744. struct ext4_mount_options {
  3745. unsigned long s_mount_opt;
  3746. unsigned long s_mount_opt2;
  3747. uid_t s_resuid;
  3748. gid_t s_resgid;
  3749. unsigned long s_commit_interval;
  3750. u32 s_min_batch_time, s_max_batch_time;
  3751. #ifdef CONFIG_QUOTA
  3752. int s_jquota_fmt;
  3753. char *s_qf_names[MAXQUOTAS];
  3754. #endif
  3755. };
  3756. static int ext4_remount(struct super_block *sb, int *flags, char *data)
  3757. {
  3758. struct ext4_super_block *es;
  3759. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3760. unsigned long old_sb_flags;
  3761. struct ext4_mount_options old_opts;
  3762. int enable_quota = 0;
  3763. ext4_group_t g;
  3764. unsigned int journal_ioprio = DEFAULT_JOURNAL_IOPRIO;
  3765. int err = 0;
  3766. #ifdef CONFIG_QUOTA
  3767. int i;
  3768. #endif
  3769. char *orig_data = kstrdup(data, GFP_KERNEL);
  3770. /* Store the original options */
  3771. lock_super(sb);
  3772. old_sb_flags = sb->s_flags;
  3773. old_opts.s_mount_opt = sbi->s_mount_opt;
  3774. old_opts.s_mount_opt2 = sbi->s_mount_opt2;
  3775. old_opts.s_resuid = sbi->s_resuid;
  3776. old_opts.s_resgid = sbi->s_resgid;
  3777. old_opts.s_commit_interval = sbi->s_commit_interval;
  3778. old_opts.s_min_batch_time = sbi->s_min_batch_time;
  3779. old_opts.s_max_batch_time = sbi->s_max_batch_time;
  3780. #ifdef CONFIG_QUOTA
  3781. old_opts.s_jquota_fmt = sbi->s_jquota_fmt;
  3782. for (i = 0; i < MAXQUOTAS; i++)
  3783. old_opts.s_qf_names[i] = sbi->s_qf_names[i];
  3784. #endif
  3785. if (sbi->s_journal && sbi->s_journal->j_task->io_context)
  3786. journal_ioprio = sbi->s_journal->j_task->io_context->ioprio;
  3787. /*
  3788. * Allow the "check" option to be passed as a remount option.
  3789. */
  3790. if (!parse_options(data, sb, NULL, &journal_ioprio, 1)) {
  3791. err = -EINVAL;
  3792. goto restore_opts;
  3793. }
  3794. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED)
  3795. ext4_abort(sb, "Abort forced by user");
  3796. sb->s_flags = (sb->s_flags & ~MS_POSIXACL) |
  3797. (test_opt(sb, POSIX_ACL) ? MS_POSIXACL : 0);
  3798. es = sbi->s_es;
  3799. if (sbi->s_journal) {
  3800. ext4_init_journal_params(sb, sbi->s_journal);
  3801. set_task_ioprio(sbi->s_journal->j_task, journal_ioprio);
  3802. }
  3803. if ((*flags & MS_RDONLY) != (sb->s_flags & MS_RDONLY)) {
  3804. if (sbi->s_mount_flags & EXT4_MF_FS_ABORTED) {
  3805. err = -EROFS;
  3806. goto restore_opts;
  3807. }
  3808. if (*flags & MS_RDONLY) {
  3809. err = dquot_suspend(sb, -1);
  3810. if (err < 0)
  3811. goto restore_opts;
  3812. /*
  3813. * First of all, the unconditional stuff we have to do
  3814. * to disable replay of the journal when we next remount
  3815. */
  3816. sb->s_flags |= MS_RDONLY;
  3817. /*
  3818. * OK, test if we are remounting a valid rw partition
  3819. * readonly, and if so set the rdonly flag and then
  3820. * mark the partition as valid again.
  3821. */
  3822. if (!(es->s_state & cpu_to_le16(EXT4_VALID_FS)) &&
  3823. (sbi->s_mount_state & EXT4_VALID_FS))
  3824. es->s_state = cpu_to_le16(sbi->s_mount_state);
  3825. if (sbi->s_journal)
  3826. ext4_mark_recovery_complete(sb, es);
  3827. } else {
  3828. /* Make sure we can mount this feature set readwrite */
  3829. if (!ext4_feature_set_ok(sb, 0)) {
  3830. err = -EROFS;
  3831. goto restore_opts;
  3832. }
  3833. /*
  3834. * Make sure the group descriptor checksums
  3835. * are sane. If they aren't, refuse to remount r/w.
  3836. */
  3837. for (g = 0; g < sbi->s_groups_count; g++) {
  3838. struct ext4_group_desc *gdp =
  3839. ext4_get_group_desc(sb, g, NULL);
  3840. if (!ext4_group_desc_csum_verify(sbi, g, gdp)) {
  3841. ext4_msg(sb, KERN_ERR,
  3842. "ext4_remount: Checksum for group %u failed (%u!=%u)",
  3843. g, le16_to_cpu(ext4_group_desc_csum(sbi, g, gdp)),
  3844. le16_to_cpu(gdp->bg_checksum));
  3845. err = -EINVAL;
  3846. goto restore_opts;
  3847. }
  3848. }
  3849. /*
  3850. * If we have an unprocessed orphan list hanging
  3851. * around from a previously readonly bdev mount,
  3852. * require a full umount/remount for now.
  3853. */
  3854. if (es->s_last_orphan) {
  3855. ext4_msg(sb, KERN_WARNING, "Couldn't "
  3856. "remount RDWR because of unprocessed "
  3857. "orphan inode list. Please "
  3858. "umount/remount instead");
  3859. err = -EINVAL;
  3860. goto restore_opts;
  3861. }
  3862. /*
  3863. * Mounting a RDONLY partition read-write, so reread
  3864. * and store the current valid flag. (It may have
  3865. * been changed by e2fsck since we originally mounted
  3866. * the partition.)
  3867. */
  3868. if (sbi->s_journal)
  3869. ext4_clear_journal_err(sb, es);
  3870. sbi->s_mount_state = le16_to_cpu(es->s_state);
  3871. if (!ext4_setup_super(sb, es, 0))
  3872. sb->s_flags &= ~MS_RDONLY;
  3873. if (EXT4_HAS_INCOMPAT_FEATURE(sb,
  3874. EXT4_FEATURE_INCOMPAT_MMP))
  3875. if (ext4_multi_mount_protect(sb,
  3876. le64_to_cpu(es->s_mmp_block))) {
  3877. err = -EROFS;
  3878. goto restore_opts;
  3879. }
  3880. enable_quota = 1;
  3881. }
  3882. }
  3883. /*
  3884. * Reinitialize lazy itable initialization thread based on
  3885. * current settings
  3886. */
  3887. if ((sb->s_flags & MS_RDONLY) || !test_opt(sb, INIT_INODE_TABLE))
  3888. ext4_unregister_li_request(sb);
  3889. else {
  3890. ext4_group_t first_not_zeroed;
  3891. first_not_zeroed = ext4_has_uninit_itable(sb);
  3892. ext4_register_li_request(sb, first_not_zeroed);
  3893. }
  3894. ext4_setup_system_zone(sb);
  3895. if (sbi->s_journal == NULL)
  3896. ext4_commit_super(sb, 1);
  3897. #ifdef CONFIG_QUOTA
  3898. /* Release old quota file names */
  3899. for (i = 0; i < MAXQUOTAS; i++)
  3900. if (old_opts.s_qf_names[i] &&
  3901. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3902. kfree(old_opts.s_qf_names[i]);
  3903. #endif
  3904. unlock_super(sb);
  3905. if (enable_quota)
  3906. dquot_resume(sb, -1);
  3907. ext4_msg(sb, KERN_INFO, "re-mounted. Opts: %s", orig_data);
  3908. kfree(orig_data);
  3909. return 0;
  3910. restore_opts:
  3911. sb->s_flags = old_sb_flags;
  3912. sbi->s_mount_opt = old_opts.s_mount_opt;
  3913. sbi->s_mount_opt2 = old_opts.s_mount_opt2;
  3914. sbi->s_resuid = old_opts.s_resuid;
  3915. sbi->s_resgid = old_opts.s_resgid;
  3916. sbi->s_commit_interval = old_opts.s_commit_interval;
  3917. sbi->s_min_batch_time = old_opts.s_min_batch_time;
  3918. sbi->s_max_batch_time = old_opts.s_max_batch_time;
  3919. #ifdef CONFIG_QUOTA
  3920. sbi->s_jquota_fmt = old_opts.s_jquota_fmt;
  3921. for (i = 0; i < MAXQUOTAS; i++) {
  3922. if (sbi->s_qf_names[i] &&
  3923. old_opts.s_qf_names[i] != sbi->s_qf_names[i])
  3924. kfree(sbi->s_qf_names[i]);
  3925. sbi->s_qf_names[i] = old_opts.s_qf_names[i];
  3926. }
  3927. #endif
  3928. unlock_super(sb);
  3929. kfree(orig_data);
  3930. return err;
  3931. }
  3932. /*
  3933. * Note: calculating the overhead so we can be compatible with
  3934. * historical BSD practice is quite difficult in the face of
  3935. * clusters/bigalloc. This is because multiple metadata blocks from
  3936. * different block group can end up in the same allocation cluster.
  3937. * Calculating the exact overhead in the face of clustered allocation
  3938. * requires either O(all block bitmaps) in memory or O(number of block
  3939. * groups**2) in time. We will still calculate the superblock for
  3940. * older file systems --- and if we come across with a bigalloc file
  3941. * system with zero in s_overhead_clusters the estimate will be close to
  3942. * correct especially for very large cluster sizes --- but for newer
  3943. * file systems, it's better to calculate this figure once at mkfs
  3944. * time, and store it in the superblock. If the superblock value is
  3945. * present (even for non-bigalloc file systems), we will use it.
  3946. */
  3947. static int ext4_statfs(struct dentry *dentry, struct kstatfs *buf)
  3948. {
  3949. struct super_block *sb = dentry->d_sb;
  3950. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3951. struct ext4_super_block *es = sbi->s_es;
  3952. struct ext4_group_desc *gdp;
  3953. u64 fsid;
  3954. s64 bfree;
  3955. if (test_opt(sb, MINIX_DF)) {
  3956. sbi->s_overhead_last = 0;
  3957. } else if (es->s_overhead_clusters) {
  3958. sbi->s_overhead_last = le32_to_cpu(es->s_overhead_clusters);
  3959. } else if (sbi->s_blocks_last != ext4_blocks_count(es)) {
  3960. ext4_group_t i, ngroups = ext4_get_groups_count(sb);
  3961. ext4_fsblk_t overhead = 0;
  3962. /*
  3963. * Compute the overhead (FS structures). This is constant
  3964. * for a given filesystem unless the number of block groups
  3965. * changes so we cache the previous value until it does.
  3966. */
  3967. /*
  3968. * All of the blocks before first_data_block are
  3969. * overhead
  3970. */
  3971. overhead = EXT4_B2C(sbi, le32_to_cpu(es->s_first_data_block));
  3972. /*
  3973. * Add the overhead found in each block group
  3974. */
  3975. for (i = 0; i < ngroups; i++) {
  3976. gdp = ext4_get_group_desc(sb, i, NULL);
  3977. overhead += ext4_num_overhead_clusters(sb, i, gdp);
  3978. cond_resched();
  3979. }
  3980. sbi->s_overhead_last = overhead;
  3981. smp_wmb();
  3982. sbi->s_blocks_last = ext4_blocks_count(es);
  3983. }
  3984. buf->f_type = EXT4_SUPER_MAGIC;
  3985. buf->f_bsize = sb->s_blocksize;
  3986. buf->f_blocks = (ext4_blocks_count(es) -
  3987. EXT4_C2B(sbi, sbi->s_overhead_last));
  3988. bfree = percpu_counter_sum_positive(&sbi->s_freeclusters_counter) -
  3989. percpu_counter_sum_positive(&sbi->s_dirtyclusters_counter);
  3990. /* prevent underflow in case that few free space is available */
  3991. buf->f_bfree = EXT4_C2B(sbi, max_t(s64, bfree, 0));
  3992. buf->f_bavail = buf->f_bfree - ext4_r_blocks_count(es);
  3993. if (buf->f_bfree < ext4_r_blocks_count(es))
  3994. buf->f_bavail = 0;
  3995. buf->f_files = le32_to_cpu(es->s_inodes_count);
  3996. buf->f_ffree = percpu_counter_sum_positive(&sbi->s_freeinodes_counter);
  3997. buf->f_namelen = EXT4_NAME_LEN;
  3998. fsid = le64_to_cpup((void *)es->s_uuid) ^
  3999. le64_to_cpup((void *)es->s_uuid + sizeof(u64));
  4000. buf->f_fsid.val[0] = fsid & 0xFFFFFFFFUL;
  4001. buf->f_fsid.val[1] = (fsid >> 32) & 0xFFFFFFFFUL;
  4002. return 0;
  4003. }
  4004. /* Helper function for writing quotas on sync - we need to start transaction
  4005. * before quota file is locked for write. Otherwise the are possible deadlocks:
  4006. * Process 1 Process 2
  4007. * ext4_create() quota_sync()
  4008. * jbd2_journal_start() write_dquot()
  4009. * dquot_initialize() down(dqio_mutex)
  4010. * down(dqio_mutex) jbd2_journal_start()
  4011. *
  4012. */
  4013. #ifdef CONFIG_QUOTA
  4014. static inline struct inode *dquot_to_inode(struct dquot *dquot)
  4015. {
  4016. return sb_dqopt(dquot->dq_sb)->files[dquot->dq_type];
  4017. }
  4018. static int ext4_write_dquot(struct dquot *dquot)
  4019. {
  4020. int ret, err;
  4021. handle_t *handle;
  4022. struct inode *inode;
  4023. inode = dquot_to_inode(dquot);
  4024. handle = ext4_journal_start(inode,
  4025. EXT4_QUOTA_TRANS_BLOCKS(dquot->dq_sb));
  4026. if (IS_ERR(handle))
  4027. return PTR_ERR(handle);
  4028. ret = dquot_commit(dquot);
  4029. err = ext4_journal_stop(handle);
  4030. if (!ret)
  4031. ret = err;
  4032. return ret;
  4033. }
  4034. static int ext4_acquire_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_INIT_BLOCKS(dquot->dq_sb));
  4040. if (IS_ERR(handle))
  4041. return PTR_ERR(handle);
  4042. ret = dquot_acquire(dquot);
  4043. err = ext4_journal_stop(handle);
  4044. if (!ret)
  4045. ret = err;
  4046. return ret;
  4047. }
  4048. static int ext4_release_dquot(struct dquot *dquot)
  4049. {
  4050. int ret, err;
  4051. handle_t *handle;
  4052. handle = ext4_journal_start(dquot_to_inode(dquot),
  4053. EXT4_QUOTA_DEL_BLOCKS(dquot->dq_sb));
  4054. if (IS_ERR(handle)) {
  4055. /* Release dquot anyway to avoid endless cycle in dqput() */
  4056. dquot_release(dquot);
  4057. return PTR_ERR(handle);
  4058. }
  4059. ret = dquot_release(dquot);
  4060. err = ext4_journal_stop(handle);
  4061. if (!ret)
  4062. ret = err;
  4063. return ret;
  4064. }
  4065. static int ext4_mark_dquot_dirty(struct dquot *dquot)
  4066. {
  4067. /* Are we journaling quotas? */
  4068. if (EXT4_SB(dquot->dq_sb)->s_qf_names[USRQUOTA] ||
  4069. EXT4_SB(dquot->dq_sb)->s_qf_names[GRPQUOTA]) {
  4070. dquot_mark_dquot_dirty(dquot);
  4071. return ext4_write_dquot(dquot);
  4072. } else {
  4073. return dquot_mark_dquot_dirty(dquot);
  4074. }
  4075. }
  4076. static int ext4_write_info(struct super_block *sb, int type)
  4077. {
  4078. int ret, err;
  4079. handle_t *handle;
  4080. /* Data block + inode block */
  4081. handle = ext4_journal_start(sb->s_root->d_inode, 2);
  4082. if (IS_ERR(handle))
  4083. return PTR_ERR(handle);
  4084. ret = dquot_commit_info(sb, type);
  4085. err = ext4_journal_stop(handle);
  4086. if (!ret)
  4087. ret = err;
  4088. return ret;
  4089. }
  4090. /*
  4091. * Turn on quotas during mount time - we need to find
  4092. * the quota file and such...
  4093. */
  4094. static int ext4_quota_on_mount(struct super_block *sb, int type)
  4095. {
  4096. return dquot_quota_on_mount(sb, EXT4_SB(sb)->s_qf_names[type],
  4097. EXT4_SB(sb)->s_jquota_fmt, type);
  4098. }
  4099. /*
  4100. * Standard function to be called on quota_on
  4101. */
  4102. static int ext4_quota_on(struct super_block *sb, int type, int format_id,
  4103. struct path *path)
  4104. {
  4105. int err;
  4106. if (!test_opt(sb, QUOTA))
  4107. return -EINVAL;
  4108. /* Quotafile not on the same filesystem? */
  4109. if (path->dentry->d_sb != sb)
  4110. return -EXDEV;
  4111. /* Journaling quota? */
  4112. if (EXT4_SB(sb)->s_qf_names[type]) {
  4113. /* Quotafile not in fs root? */
  4114. if (path->dentry->d_parent != sb->s_root)
  4115. ext4_msg(sb, KERN_WARNING,
  4116. "Quota file not on filesystem root. "
  4117. "Journaled quota will not work");
  4118. }
  4119. /*
  4120. * When we journal data on quota file, we have to flush journal to see
  4121. * all updates to the file when we bypass pagecache...
  4122. */
  4123. if (EXT4_SB(sb)->s_journal &&
  4124. ext4_should_journal_data(path->dentry->d_inode)) {
  4125. /*
  4126. * We don't need to lock updates but journal_flush() could
  4127. * otherwise be livelocked...
  4128. */
  4129. jbd2_journal_lock_updates(EXT4_SB(sb)->s_journal);
  4130. err = jbd2_journal_flush(EXT4_SB(sb)->s_journal);
  4131. jbd2_journal_unlock_updates(EXT4_SB(sb)->s_journal);
  4132. if (err)
  4133. return err;
  4134. }
  4135. return dquot_quota_on(sb, type, format_id, path);
  4136. }
  4137. static int ext4_quota_off(struct super_block *sb, int type)
  4138. {
  4139. struct inode *inode = sb_dqopt(sb)->files[type];
  4140. handle_t *handle;
  4141. /* Force all delayed allocation blocks to be allocated.
  4142. * Caller already holds s_umount sem */
  4143. if (test_opt(sb, DELALLOC))
  4144. sync_filesystem(sb);
  4145. if (!inode)
  4146. goto out;
  4147. /* Update modification times of quota files when userspace can
  4148. * start looking at them */
  4149. handle = ext4_journal_start(inode, 1);
  4150. if (IS_ERR(handle))
  4151. goto out;
  4152. inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  4153. ext4_mark_inode_dirty(handle, inode);
  4154. ext4_journal_stop(handle);
  4155. out:
  4156. return dquot_quota_off(sb, type);
  4157. }
  4158. /* Read data from quotafile - avoid pagecache and such because we cannot afford
  4159. * acquiring the locks... As quota files are never truncated and quota code
  4160. * itself serializes the operations (and no one else should touch the files)
  4161. * we don't have to be afraid of races */
  4162. static ssize_t ext4_quota_read(struct super_block *sb, int type, char *data,
  4163. size_t len, loff_t off)
  4164. {
  4165. struct inode *inode = sb_dqopt(sb)->files[type];
  4166. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  4167. int err = 0;
  4168. int offset = off & (sb->s_blocksize - 1);
  4169. int tocopy;
  4170. size_t toread;
  4171. struct buffer_head *bh;
  4172. loff_t i_size = i_size_read(inode);
  4173. if (off > i_size)
  4174. return 0;
  4175. if (off+len > i_size)
  4176. len = i_size-off;
  4177. toread = len;
  4178. while (toread > 0) {
  4179. tocopy = sb->s_blocksize - offset < toread ?
  4180. sb->s_blocksize - offset : toread;
  4181. bh = ext4_bread(NULL, inode, blk, 0, &err);
  4182. if (err)
  4183. return err;
  4184. if (!bh) /* A hole? */
  4185. memset(data, 0, tocopy);
  4186. else
  4187. memcpy(data, bh->b_data+offset, tocopy);
  4188. brelse(bh);
  4189. offset = 0;
  4190. toread -= tocopy;
  4191. data += tocopy;
  4192. blk++;
  4193. }
  4194. return len;
  4195. }
  4196. /* Write to quotafile (we know the transaction is already started and has
  4197. * enough credits) */
  4198. static ssize_t ext4_quota_write(struct super_block *sb, int type,
  4199. const char *data, size_t len, loff_t off)
  4200. {
  4201. struct inode *inode = sb_dqopt(sb)->files[type];
  4202. ext4_lblk_t blk = off >> EXT4_BLOCK_SIZE_BITS(sb);
  4203. int err = 0;
  4204. int offset = off & (sb->s_blocksize - 1);
  4205. struct buffer_head *bh;
  4206. handle_t *handle = journal_current_handle();
  4207. if (EXT4_SB(sb)->s_journal && !handle) {
  4208. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  4209. " cancelled because transaction is not started",
  4210. (unsigned long long)off, (unsigned long long)len);
  4211. return -EIO;
  4212. }
  4213. /*
  4214. * Since we account only one data block in transaction credits,
  4215. * then it is impossible to cross a block boundary.
  4216. */
  4217. if (sb->s_blocksize - offset < len) {
  4218. ext4_msg(sb, KERN_WARNING, "Quota write (off=%llu, len=%llu)"
  4219. " cancelled because not block aligned",
  4220. (unsigned long long)off, (unsigned long long)len);
  4221. return -EIO;
  4222. }
  4223. mutex_lock_nested(&inode->i_mutex, I_MUTEX_QUOTA);
  4224. bh = ext4_bread(handle, inode, blk, 1, &err);
  4225. if (!bh)
  4226. goto out;
  4227. err = ext4_journal_get_write_access(handle, bh);
  4228. if (err) {
  4229. brelse(bh);
  4230. goto out;
  4231. }
  4232. lock_buffer(bh);
  4233. memcpy(bh->b_data+offset, data, len);
  4234. flush_dcache_page(bh->b_page);
  4235. unlock_buffer(bh);
  4236. err = ext4_handle_dirty_metadata(handle, NULL, bh);
  4237. brelse(bh);
  4238. out:
  4239. if (err) {
  4240. mutex_unlock(&inode->i_mutex);
  4241. return err;
  4242. }
  4243. if (inode->i_size < off + len) {
  4244. i_size_write(inode, off + len);
  4245. EXT4_I(inode)->i_disksize = inode->i_size;
  4246. ext4_mark_inode_dirty(handle, inode);
  4247. }
  4248. mutex_unlock(&inode->i_mutex);
  4249. return len;
  4250. }
  4251. #endif
  4252. static struct dentry *ext4_mount(struct file_system_type *fs_type, int flags,
  4253. const char *dev_name, void *data)
  4254. {
  4255. return mount_bdev(fs_type, flags, dev_name, data, ext4_fill_super);
  4256. }
  4257. #if !defined(CONFIG_EXT2_FS) && !defined(CONFIG_EXT2_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  4258. static inline void register_as_ext2(void)
  4259. {
  4260. int err = register_filesystem(&ext2_fs_type);
  4261. if (err)
  4262. printk(KERN_WARNING
  4263. "EXT4-fs: Unable to register as ext2 (%d)\n", err);
  4264. }
  4265. static inline void unregister_as_ext2(void)
  4266. {
  4267. unregister_filesystem(&ext2_fs_type);
  4268. }
  4269. static inline int ext2_feature_set_ok(struct super_block *sb)
  4270. {
  4271. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT2_FEATURE_INCOMPAT_SUPP))
  4272. return 0;
  4273. if (sb->s_flags & MS_RDONLY)
  4274. return 1;
  4275. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT2_FEATURE_RO_COMPAT_SUPP))
  4276. return 0;
  4277. return 1;
  4278. }
  4279. MODULE_ALIAS("ext2");
  4280. #else
  4281. static inline void register_as_ext2(void) { }
  4282. static inline void unregister_as_ext2(void) { }
  4283. static inline int ext2_feature_set_ok(struct super_block *sb) { return 0; }
  4284. #endif
  4285. #if !defined(CONFIG_EXT3_FS) && !defined(CONFIG_EXT3_FS_MODULE) && defined(CONFIG_EXT4_USE_FOR_EXT23)
  4286. static inline void register_as_ext3(void)
  4287. {
  4288. int err = register_filesystem(&ext3_fs_type);
  4289. if (err)
  4290. printk(KERN_WARNING
  4291. "EXT4-fs: Unable to register as ext3 (%d)\n", err);
  4292. }
  4293. static inline void unregister_as_ext3(void)
  4294. {
  4295. unregister_filesystem(&ext3_fs_type);
  4296. }
  4297. static inline int ext3_feature_set_ok(struct super_block *sb)
  4298. {
  4299. if (EXT4_HAS_INCOMPAT_FEATURE(sb, ~EXT3_FEATURE_INCOMPAT_SUPP))
  4300. return 0;
  4301. if (!EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_HAS_JOURNAL))
  4302. return 0;
  4303. if (sb->s_flags & MS_RDONLY)
  4304. return 1;
  4305. if (EXT4_HAS_RO_COMPAT_FEATURE(sb, ~EXT3_FEATURE_RO_COMPAT_SUPP))
  4306. return 0;
  4307. return 1;
  4308. }
  4309. MODULE_ALIAS("ext3");
  4310. #else
  4311. static inline void register_as_ext3(void) { }
  4312. static inline void unregister_as_ext3(void) { }
  4313. static inline int ext3_feature_set_ok(struct super_block *sb) { return 0; }
  4314. #endif
  4315. static struct file_system_type ext4_fs_type = {
  4316. .owner = THIS_MODULE,
  4317. .name = "ext4",
  4318. .mount = ext4_mount,
  4319. .kill_sb = kill_block_super,
  4320. .fs_flags = FS_REQUIRES_DEV,
  4321. };
  4322. static int __init ext4_init_feat_adverts(void)
  4323. {
  4324. struct ext4_features *ef;
  4325. int ret = -ENOMEM;
  4326. ef = kzalloc(sizeof(struct ext4_features), GFP_KERNEL);
  4327. if (!ef)
  4328. goto out;
  4329. ef->f_kobj.kset = ext4_kset;
  4330. init_completion(&ef->f_kobj_unregister);
  4331. ret = kobject_init_and_add(&ef->f_kobj, &ext4_feat_ktype, NULL,
  4332. "features");
  4333. if (ret) {
  4334. kfree(ef);
  4335. goto out;
  4336. }
  4337. ext4_feat = ef;
  4338. ret = 0;
  4339. out:
  4340. return ret;
  4341. }
  4342. static void ext4_exit_feat_adverts(void)
  4343. {
  4344. kobject_put(&ext4_feat->f_kobj);
  4345. wait_for_completion(&ext4_feat->f_kobj_unregister);
  4346. kfree(ext4_feat);
  4347. }
  4348. /* Shared across all ext4 file systems */
  4349. wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
  4350. struct mutex ext4__aio_mutex[EXT4_WQ_HASH_SZ];
  4351. static int __init ext4_init_fs(void)
  4352. {
  4353. int i, err;
  4354. ext4_li_info = NULL;
  4355. mutex_init(&ext4_li_mtx);
  4356. ext4_check_flag_values();
  4357. for (i = 0; i < EXT4_WQ_HASH_SZ; i++) {
  4358. mutex_init(&ext4__aio_mutex[i]);
  4359. init_waitqueue_head(&ext4__ioend_wq[i]);
  4360. }
  4361. err = ext4_init_pageio();
  4362. if (err)
  4363. return err;
  4364. err = ext4_init_system_zone();
  4365. if (err)
  4366. goto out6;
  4367. ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
  4368. if (!ext4_kset)
  4369. goto out5;
  4370. ext4_proc_root = proc_mkdir("fs/ext4", NULL);
  4371. err = ext4_init_feat_adverts();
  4372. if (err)
  4373. goto out4;
  4374. err = ext4_init_mballoc();
  4375. if (err)
  4376. goto out3;
  4377. err = ext4_init_xattr();
  4378. if (err)
  4379. goto out2;
  4380. err = init_inodecache();
  4381. if (err)
  4382. goto out1;
  4383. register_as_ext3();
  4384. register_as_ext2();
  4385. err = register_filesystem(&ext4_fs_type);
  4386. if (err)
  4387. goto out;
  4388. return 0;
  4389. out:
  4390. unregister_as_ext2();
  4391. unregister_as_ext3();
  4392. destroy_inodecache();
  4393. out1:
  4394. ext4_exit_xattr();
  4395. out2:
  4396. ext4_exit_mballoc();
  4397. out3:
  4398. ext4_exit_feat_adverts();
  4399. out4:
  4400. if (ext4_proc_root)
  4401. remove_proc_entry("fs/ext4", NULL);
  4402. kset_unregister(ext4_kset);
  4403. out5:
  4404. ext4_exit_system_zone();
  4405. out6:
  4406. ext4_exit_pageio();
  4407. return err;
  4408. }
  4409. static void __exit ext4_exit_fs(void)
  4410. {
  4411. ext4_destroy_lazyinit_thread();
  4412. unregister_as_ext2();
  4413. unregister_as_ext3();
  4414. unregister_filesystem(&ext4_fs_type);
  4415. destroy_inodecache();
  4416. ext4_exit_xattr();
  4417. ext4_exit_mballoc();
  4418. ext4_exit_feat_adverts();
  4419. remove_proc_entry("fs/ext4", NULL);
  4420. kset_unregister(ext4_kset);
  4421. ext4_exit_system_zone();
  4422. ext4_exit_pageio();
  4423. }
  4424. MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");
  4425. MODULE_DESCRIPTION("Fourth Extended Filesystem");
  4426. MODULE_LICENSE("GPL");
  4427. module_init(ext4_init_fs)
  4428. module_exit(ext4_exit_fs)