super.c 155 KB

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