drbd_receiver.c 174 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050
  1. /*
  2. drbd_receiver.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2001-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 1999-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2002-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/uaccess.h>
  21. #include <net/sock.h>
  22. #include <linux/drbd.h>
  23. #include <linux/fs.h>
  24. #include <linux/file.h>
  25. #include <linux/in.h>
  26. #include <linux/mm.h>
  27. #include <linux/memcontrol.h>
  28. #include <linux/mm_inline.h>
  29. #include <linux/slab.h>
  30. #include <uapi/linux/sched/types.h>
  31. #include <linux/sched/signal.h>
  32. #include <linux/pkt_sched.h>
  33. #define __KERNEL_SYSCALLS__
  34. #include <linux/unistd.h>
  35. #include <linux/vmalloc.h>
  36. #include <linux/random.h>
  37. #include <linux/string.h>
  38. #include <linux/scatterlist.h>
  39. #include "drbd_int.h"
  40. #include "drbd_protocol.h"
  41. #include "drbd_req.h"
  42. #include "drbd_vli.h"
  43. #define PRO_FEATURES (DRBD_FF_TRIM|DRBD_FF_THIN_RESYNC|DRBD_FF_WSAME)
  44. struct packet_info {
  45. enum drbd_packet cmd;
  46. unsigned int size;
  47. unsigned int vnr;
  48. void *data;
  49. };
  50. enum finish_epoch {
  51. FE_STILL_LIVE,
  52. FE_DESTROYED,
  53. FE_RECYCLED,
  54. };
  55. static int drbd_do_features(struct drbd_connection *connection);
  56. static int drbd_do_auth(struct drbd_connection *connection);
  57. static int drbd_disconnected(struct drbd_peer_device *);
  58. static void conn_wait_active_ee_empty(struct drbd_connection *connection);
  59. static enum finish_epoch drbd_may_finish_epoch(struct drbd_connection *, struct drbd_epoch *, enum epoch_event);
  60. static int e_end_block(struct drbd_work *, int);
  61. #define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
  62. /*
  63. * some helper functions to deal with single linked page lists,
  64. * page->private being our "next" pointer.
  65. */
  66. /* If at least n pages are linked at head, get n pages off.
  67. * Otherwise, don't modify head, and return NULL.
  68. * Locking is the responsibility of the caller.
  69. */
  70. static struct page *page_chain_del(struct page **head, int n)
  71. {
  72. struct page *page;
  73. struct page *tmp;
  74. BUG_ON(!n);
  75. BUG_ON(!head);
  76. page = *head;
  77. if (!page)
  78. return NULL;
  79. while (page) {
  80. tmp = page_chain_next(page);
  81. if (--n == 0)
  82. break; /* found sufficient pages */
  83. if (tmp == NULL)
  84. /* insufficient pages, don't use any of them. */
  85. return NULL;
  86. page = tmp;
  87. }
  88. /* add end of list marker for the returned list */
  89. set_page_private(page, 0);
  90. /* actual return value, and adjustment of head */
  91. page = *head;
  92. *head = tmp;
  93. return page;
  94. }
  95. /* may be used outside of locks to find the tail of a (usually short)
  96. * "private" page chain, before adding it back to a global chain head
  97. * with page_chain_add() under a spinlock. */
  98. static struct page *page_chain_tail(struct page *page, int *len)
  99. {
  100. struct page *tmp;
  101. int i = 1;
  102. while ((tmp = page_chain_next(page)))
  103. ++i, page = tmp;
  104. if (len)
  105. *len = i;
  106. return page;
  107. }
  108. static int page_chain_free(struct page *page)
  109. {
  110. struct page *tmp;
  111. int i = 0;
  112. page_chain_for_each_safe(page, tmp) {
  113. put_page(page);
  114. ++i;
  115. }
  116. return i;
  117. }
  118. static void page_chain_add(struct page **head,
  119. struct page *chain_first, struct page *chain_last)
  120. {
  121. #if 1
  122. struct page *tmp;
  123. tmp = page_chain_tail(chain_first, NULL);
  124. BUG_ON(tmp != chain_last);
  125. #endif
  126. /* add chain to head */
  127. set_page_private(chain_last, (unsigned long)*head);
  128. *head = chain_first;
  129. }
  130. static struct page *__drbd_alloc_pages(struct drbd_device *device,
  131. unsigned int number)
  132. {
  133. struct page *page = NULL;
  134. struct page *tmp = NULL;
  135. unsigned int i = 0;
  136. /* Yes, testing drbd_pp_vacant outside the lock is racy.
  137. * So what. It saves a spin_lock. */
  138. if (drbd_pp_vacant >= number) {
  139. spin_lock(&drbd_pp_lock);
  140. page = page_chain_del(&drbd_pp_pool, number);
  141. if (page)
  142. drbd_pp_vacant -= number;
  143. spin_unlock(&drbd_pp_lock);
  144. if (page)
  145. return page;
  146. }
  147. /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
  148. * "criss-cross" setup, that might cause write-out on some other DRBD,
  149. * which in turn might block on the other node at this very place. */
  150. for (i = 0; i < number; i++) {
  151. tmp = alloc_page(GFP_TRY);
  152. if (!tmp)
  153. break;
  154. set_page_private(tmp, (unsigned long)page);
  155. page = tmp;
  156. }
  157. if (i == number)
  158. return page;
  159. /* Not enough pages immediately available this time.
  160. * No need to jump around here, drbd_alloc_pages will retry this
  161. * function "soon". */
  162. if (page) {
  163. tmp = page_chain_tail(page, NULL);
  164. spin_lock(&drbd_pp_lock);
  165. page_chain_add(&drbd_pp_pool, page, tmp);
  166. drbd_pp_vacant += i;
  167. spin_unlock(&drbd_pp_lock);
  168. }
  169. return NULL;
  170. }
  171. static void reclaim_finished_net_peer_reqs(struct drbd_device *device,
  172. struct list_head *to_be_freed)
  173. {
  174. struct drbd_peer_request *peer_req, *tmp;
  175. /* The EEs are always appended to the end of the list. Since
  176. they are sent in order over the wire, they have to finish
  177. in order. As soon as we see the first not finished we can
  178. stop to examine the list... */
  179. list_for_each_entry_safe(peer_req, tmp, &device->net_ee, w.list) {
  180. if (drbd_peer_req_has_active_page(peer_req))
  181. break;
  182. list_move(&peer_req->w.list, to_be_freed);
  183. }
  184. }
  185. static void drbd_reclaim_net_peer_reqs(struct drbd_device *device)
  186. {
  187. LIST_HEAD(reclaimed);
  188. struct drbd_peer_request *peer_req, *t;
  189. spin_lock_irq(&device->resource->req_lock);
  190. reclaim_finished_net_peer_reqs(device, &reclaimed);
  191. spin_unlock_irq(&device->resource->req_lock);
  192. list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
  193. drbd_free_net_peer_req(device, peer_req);
  194. }
  195. static void conn_reclaim_net_peer_reqs(struct drbd_connection *connection)
  196. {
  197. struct drbd_peer_device *peer_device;
  198. int vnr;
  199. rcu_read_lock();
  200. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  201. struct drbd_device *device = peer_device->device;
  202. if (!atomic_read(&device->pp_in_use_by_net))
  203. continue;
  204. kref_get(&device->kref);
  205. rcu_read_unlock();
  206. drbd_reclaim_net_peer_reqs(device);
  207. kref_put(&device->kref, drbd_destroy_device);
  208. rcu_read_lock();
  209. }
  210. rcu_read_unlock();
  211. }
  212. /**
  213. * drbd_alloc_pages() - Returns @number pages, retries forever (or until signalled)
  214. * @device: DRBD device.
  215. * @number: number of pages requested
  216. * @retry: whether to retry, if not enough pages are available right now
  217. *
  218. * Tries to allocate number pages, first from our own page pool, then from
  219. * the kernel.
  220. * Possibly retry until DRBD frees sufficient pages somewhere else.
  221. *
  222. * If this allocation would exceed the max_buffers setting, we throttle
  223. * allocation (schedule_timeout) to give the system some room to breathe.
  224. *
  225. * We do not use max-buffers as hard limit, because it could lead to
  226. * congestion and further to a distributed deadlock during online-verify or
  227. * (checksum based) resync, if the max-buffers, socket buffer sizes and
  228. * resync-rate settings are mis-configured.
  229. *
  230. * Returns a page chain linked via page->private.
  231. */
  232. struct page *drbd_alloc_pages(struct drbd_peer_device *peer_device, unsigned int number,
  233. bool retry)
  234. {
  235. struct drbd_device *device = peer_device->device;
  236. struct page *page = NULL;
  237. struct net_conf *nc;
  238. DEFINE_WAIT(wait);
  239. unsigned int mxb;
  240. rcu_read_lock();
  241. nc = rcu_dereference(peer_device->connection->net_conf);
  242. mxb = nc ? nc->max_buffers : 1000000;
  243. rcu_read_unlock();
  244. if (atomic_read(&device->pp_in_use) < mxb)
  245. page = __drbd_alloc_pages(device, number);
  246. /* Try to keep the fast path fast, but occasionally we need
  247. * to reclaim the pages we lended to the network stack. */
  248. if (page && atomic_read(&device->pp_in_use_by_net) > 512)
  249. drbd_reclaim_net_peer_reqs(device);
  250. while (page == NULL) {
  251. prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
  252. drbd_reclaim_net_peer_reqs(device);
  253. if (atomic_read(&device->pp_in_use) < mxb) {
  254. page = __drbd_alloc_pages(device, number);
  255. if (page)
  256. break;
  257. }
  258. if (!retry)
  259. break;
  260. if (signal_pending(current)) {
  261. drbd_warn(device, "drbd_alloc_pages interrupted!\n");
  262. break;
  263. }
  264. if (schedule_timeout(HZ/10) == 0)
  265. mxb = UINT_MAX;
  266. }
  267. finish_wait(&drbd_pp_wait, &wait);
  268. if (page)
  269. atomic_add(number, &device->pp_in_use);
  270. return page;
  271. }
  272. /* Must not be used from irq, as that may deadlock: see drbd_alloc_pages.
  273. * Is also used from inside an other spin_lock_irq(&resource->req_lock);
  274. * Either links the page chain back to the global pool,
  275. * or returns all pages to the system. */
  276. static void drbd_free_pages(struct drbd_device *device, struct page *page, int is_net)
  277. {
  278. atomic_t *a = is_net ? &device->pp_in_use_by_net : &device->pp_in_use;
  279. int i;
  280. if (page == NULL)
  281. return;
  282. if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE) * drbd_minor_count)
  283. i = page_chain_free(page);
  284. else {
  285. struct page *tmp;
  286. tmp = page_chain_tail(page, &i);
  287. spin_lock(&drbd_pp_lock);
  288. page_chain_add(&drbd_pp_pool, page, tmp);
  289. drbd_pp_vacant += i;
  290. spin_unlock(&drbd_pp_lock);
  291. }
  292. i = atomic_sub_return(i, a);
  293. if (i < 0)
  294. drbd_warn(device, "ASSERTION FAILED: %s: %d < 0\n",
  295. is_net ? "pp_in_use_by_net" : "pp_in_use", i);
  296. wake_up(&drbd_pp_wait);
  297. }
  298. /*
  299. You need to hold the req_lock:
  300. _drbd_wait_ee_list_empty()
  301. You must not have the req_lock:
  302. drbd_free_peer_req()
  303. drbd_alloc_peer_req()
  304. drbd_free_peer_reqs()
  305. drbd_ee_fix_bhs()
  306. drbd_finish_peer_reqs()
  307. drbd_clear_done_ee()
  308. drbd_wait_ee_list_empty()
  309. */
  310. /* normal: payload_size == request size (bi_size)
  311. * w_same: payload_size == logical_block_size
  312. * trim: payload_size == 0 */
  313. struct drbd_peer_request *
  314. drbd_alloc_peer_req(struct drbd_peer_device *peer_device, u64 id, sector_t sector,
  315. unsigned int request_size, unsigned int payload_size, gfp_t gfp_mask) __must_hold(local)
  316. {
  317. struct drbd_device *device = peer_device->device;
  318. struct drbd_peer_request *peer_req;
  319. struct page *page = NULL;
  320. unsigned nr_pages = (payload_size + PAGE_SIZE -1) >> PAGE_SHIFT;
  321. if (drbd_insert_fault(device, DRBD_FAULT_AL_EE))
  322. return NULL;
  323. peer_req = mempool_alloc(&drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
  324. if (!peer_req) {
  325. if (!(gfp_mask & __GFP_NOWARN))
  326. drbd_err(device, "%s: allocation failed\n", __func__);
  327. return NULL;
  328. }
  329. if (nr_pages) {
  330. page = drbd_alloc_pages(peer_device, nr_pages,
  331. gfpflags_allow_blocking(gfp_mask));
  332. if (!page)
  333. goto fail;
  334. }
  335. memset(peer_req, 0, sizeof(*peer_req));
  336. INIT_LIST_HEAD(&peer_req->w.list);
  337. drbd_clear_interval(&peer_req->i);
  338. peer_req->i.size = request_size;
  339. peer_req->i.sector = sector;
  340. peer_req->submit_jif = jiffies;
  341. peer_req->peer_device = peer_device;
  342. peer_req->pages = page;
  343. /*
  344. * The block_id is opaque to the receiver. It is not endianness
  345. * converted, and sent back to the sender unchanged.
  346. */
  347. peer_req->block_id = id;
  348. return peer_req;
  349. fail:
  350. mempool_free(peer_req, &drbd_ee_mempool);
  351. return NULL;
  352. }
  353. void __drbd_free_peer_req(struct drbd_device *device, struct drbd_peer_request *peer_req,
  354. int is_net)
  355. {
  356. might_sleep();
  357. if (peer_req->flags & EE_HAS_DIGEST)
  358. kfree(peer_req->digest);
  359. drbd_free_pages(device, peer_req->pages, is_net);
  360. D_ASSERT(device, atomic_read(&peer_req->pending_bios) == 0);
  361. D_ASSERT(device, drbd_interval_empty(&peer_req->i));
  362. if (!expect(!(peer_req->flags & EE_CALL_AL_COMPLETE_IO))) {
  363. peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO;
  364. drbd_al_complete_io(device, &peer_req->i);
  365. }
  366. mempool_free(peer_req, &drbd_ee_mempool);
  367. }
  368. int drbd_free_peer_reqs(struct drbd_device *device, struct list_head *list)
  369. {
  370. LIST_HEAD(work_list);
  371. struct drbd_peer_request *peer_req, *t;
  372. int count = 0;
  373. int is_net = list == &device->net_ee;
  374. spin_lock_irq(&device->resource->req_lock);
  375. list_splice_init(list, &work_list);
  376. spin_unlock_irq(&device->resource->req_lock);
  377. list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
  378. __drbd_free_peer_req(device, peer_req, is_net);
  379. count++;
  380. }
  381. return count;
  382. }
  383. /*
  384. * See also comments in _req_mod(,BARRIER_ACKED) and receive_Barrier.
  385. */
  386. static int drbd_finish_peer_reqs(struct drbd_device *device)
  387. {
  388. LIST_HEAD(work_list);
  389. LIST_HEAD(reclaimed);
  390. struct drbd_peer_request *peer_req, *t;
  391. int err = 0;
  392. spin_lock_irq(&device->resource->req_lock);
  393. reclaim_finished_net_peer_reqs(device, &reclaimed);
  394. list_splice_init(&device->done_ee, &work_list);
  395. spin_unlock_irq(&device->resource->req_lock);
  396. list_for_each_entry_safe(peer_req, t, &reclaimed, w.list)
  397. drbd_free_net_peer_req(device, peer_req);
  398. /* possible callbacks here:
  399. * e_end_block, and e_end_resync_block, e_send_superseded.
  400. * all ignore the last argument.
  401. */
  402. list_for_each_entry_safe(peer_req, t, &work_list, w.list) {
  403. int err2;
  404. /* list_del not necessary, next/prev members not touched */
  405. err2 = peer_req->w.cb(&peer_req->w, !!err);
  406. if (!err)
  407. err = err2;
  408. drbd_free_peer_req(device, peer_req);
  409. }
  410. wake_up(&device->ee_wait);
  411. return err;
  412. }
  413. static void _drbd_wait_ee_list_empty(struct drbd_device *device,
  414. struct list_head *head)
  415. {
  416. DEFINE_WAIT(wait);
  417. /* avoids spin_lock/unlock
  418. * and calling prepare_to_wait in the fast path */
  419. while (!list_empty(head)) {
  420. prepare_to_wait(&device->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
  421. spin_unlock_irq(&device->resource->req_lock);
  422. io_schedule();
  423. finish_wait(&device->ee_wait, &wait);
  424. spin_lock_irq(&device->resource->req_lock);
  425. }
  426. }
  427. static void drbd_wait_ee_list_empty(struct drbd_device *device,
  428. struct list_head *head)
  429. {
  430. spin_lock_irq(&device->resource->req_lock);
  431. _drbd_wait_ee_list_empty(device, head);
  432. spin_unlock_irq(&device->resource->req_lock);
  433. }
  434. static int drbd_recv_short(struct socket *sock, void *buf, size_t size, int flags)
  435. {
  436. struct kvec iov = {
  437. .iov_base = buf,
  438. .iov_len = size,
  439. };
  440. struct msghdr msg = {
  441. .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
  442. };
  443. iov_iter_kvec(&msg.msg_iter, READ | ITER_KVEC, &iov, 1, size);
  444. return sock_recvmsg(sock, &msg, msg.msg_flags);
  445. }
  446. static int drbd_recv(struct drbd_connection *connection, void *buf, size_t size)
  447. {
  448. int rv;
  449. rv = drbd_recv_short(connection->data.socket, buf, size, 0);
  450. if (rv < 0) {
  451. if (rv == -ECONNRESET)
  452. drbd_info(connection, "sock was reset by peer\n");
  453. else if (rv != -ERESTARTSYS)
  454. drbd_err(connection, "sock_recvmsg returned %d\n", rv);
  455. } else if (rv == 0) {
  456. if (test_bit(DISCONNECT_SENT, &connection->flags)) {
  457. long t;
  458. rcu_read_lock();
  459. t = rcu_dereference(connection->net_conf)->ping_timeo * HZ/10;
  460. rcu_read_unlock();
  461. t = wait_event_timeout(connection->ping_wait, connection->cstate < C_WF_REPORT_PARAMS, t);
  462. if (t)
  463. goto out;
  464. }
  465. drbd_info(connection, "sock was shut down by peer\n");
  466. }
  467. if (rv != size)
  468. conn_request_state(connection, NS(conn, C_BROKEN_PIPE), CS_HARD);
  469. out:
  470. return rv;
  471. }
  472. static int drbd_recv_all(struct drbd_connection *connection, void *buf, size_t size)
  473. {
  474. int err;
  475. err = drbd_recv(connection, buf, size);
  476. if (err != size) {
  477. if (err >= 0)
  478. err = -EIO;
  479. } else
  480. err = 0;
  481. return err;
  482. }
  483. static int drbd_recv_all_warn(struct drbd_connection *connection, void *buf, size_t size)
  484. {
  485. int err;
  486. err = drbd_recv_all(connection, buf, size);
  487. if (err && !signal_pending(current))
  488. drbd_warn(connection, "short read (expected size %d)\n", (int)size);
  489. return err;
  490. }
  491. /* quoting tcp(7):
  492. * On individual connections, the socket buffer size must be set prior to the
  493. * listen(2) or connect(2) calls in order to have it take effect.
  494. * This is our wrapper to do so.
  495. */
  496. static void drbd_setbufsize(struct socket *sock, unsigned int snd,
  497. unsigned int rcv)
  498. {
  499. /* open coded SO_SNDBUF, SO_RCVBUF */
  500. if (snd) {
  501. sock->sk->sk_sndbuf = snd;
  502. sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  503. }
  504. if (rcv) {
  505. sock->sk->sk_rcvbuf = rcv;
  506. sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  507. }
  508. }
  509. static struct socket *drbd_try_connect(struct drbd_connection *connection)
  510. {
  511. const char *what;
  512. struct socket *sock;
  513. struct sockaddr_in6 src_in6;
  514. struct sockaddr_in6 peer_in6;
  515. struct net_conf *nc;
  516. int err, peer_addr_len, my_addr_len;
  517. int sndbuf_size, rcvbuf_size, connect_int;
  518. int disconnect_on_error = 1;
  519. rcu_read_lock();
  520. nc = rcu_dereference(connection->net_conf);
  521. if (!nc) {
  522. rcu_read_unlock();
  523. return NULL;
  524. }
  525. sndbuf_size = nc->sndbuf_size;
  526. rcvbuf_size = nc->rcvbuf_size;
  527. connect_int = nc->connect_int;
  528. rcu_read_unlock();
  529. my_addr_len = min_t(int, connection->my_addr_len, sizeof(src_in6));
  530. memcpy(&src_in6, &connection->my_addr, my_addr_len);
  531. if (((struct sockaddr *)&connection->my_addr)->sa_family == AF_INET6)
  532. src_in6.sin6_port = 0;
  533. else
  534. ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
  535. peer_addr_len = min_t(int, connection->peer_addr_len, sizeof(src_in6));
  536. memcpy(&peer_in6, &connection->peer_addr, peer_addr_len);
  537. what = "sock_create_kern";
  538. err = sock_create_kern(&init_net, ((struct sockaddr *)&src_in6)->sa_family,
  539. SOCK_STREAM, IPPROTO_TCP, &sock);
  540. if (err < 0) {
  541. sock = NULL;
  542. goto out;
  543. }
  544. sock->sk->sk_rcvtimeo =
  545. sock->sk->sk_sndtimeo = connect_int * HZ;
  546. drbd_setbufsize(sock, sndbuf_size, rcvbuf_size);
  547. /* explicitly bind to the configured IP as source IP
  548. * for the outgoing connections.
  549. * This is needed for multihomed hosts and to be
  550. * able to use lo: interfaces for drbd.
  551. * Make sure to use 0 as port number, so linux selects
  552. * a free one dynamically.
  553. */
  554. what = "bind before connect";
  555. err = sock->ops->bind(sock, (struct sockaddr *) &src_in6, my_addr_len);
  556. if (err < 0)
  557. goto out;
  558. /* connect may fail, peer not yet available.
  559. * stay C_WF_CONNECTION, don't go Disconnecting! */
  560. disconnect_on_error = 0;
  561. what = "connect";
  562. err = sock->ops->connect(sock, (struct sockaddr *) &peer_in6, peer_addr_len, 0);
  563. out:
  564. if (err < 0) {
  565. if (sock) {
  566. sock_release(sock);
  567. sock = NULL;
  568. }
  569. switch (-err) {
  570. /* timeout, busy, signal pending */
  571. case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
  572. case EINTR: case ERESTARTSYS:
  573. /* peer not (yet) available, network problem */
  574. case ECONNREFUSED: case ENETUNREACH:
  575. case EHOSTDOWN: case EHOSTUNREACH:
  576. disconnect_on_error = 0;
  577. break;
  578. default:
  579. drbd_err(connection, "%s failed, err = %d\n", what, err);
  580. }
  581. if (disconnect_on_error)
  582. conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
  583. }
  584. return sock;
  585. }
  586. struct accept_wait_data {
  587. struct drbd_connection *connection;
  588. struct socket *s_listen;
  589. struct completion door_bell;
  590. void (*original_sk_state_change)(struct sock *sk);
  591. };
  592. static void drbd_incoming_connection(struct sock *sk)
  593. {
  594. struct accept_wait_data *ad = sk->sk_user_data;
  595. void (*state_change)(struct sock *sk);
  596. state_change = ad->original_sk_state_change;
  597. if (sk->sk_state == TCP_ESTABLISHED)
  598. complete(&ad->door_bell);
  599. state_change(sk);
  600. }
  601. static int prepare_listen_socket(struct drbd_connection *connection, struct accept_wait_data *ad)
  602. {
  603. int err, sndbuf_size, rcvbuf_size, my_addr_len;
  604. struct sockaddr_in6 my_addr;
  605. struct socket *s_listen;
  606. struct net_conf *nc;
  607. const char *what;
  608. rcu_read_lock();
  609. nc = rcu_dereference(connection->net_conf);
  610. if (!nc) {
  611. rcu_read_unlock();
  612. return -EIO;
  613. }
  614. sndbuf_size = nc->sndbuf_size;
  615. rcvbuf_size = nc->rcvbuf_size;
  616. rcu_read_unlock();
  617. my_addr_len = min_t(int, connection->my_addr_len, sizeof(struct sockaddr_in6));
  618. memcpy(&my_addr, &connection->my_addr, my_addr_len);
  619. what = "sock_create_kern";
  620. err = sock_create_kern(&init_net, ((struct sockaddr *)&my_addr)->sa_family,
  621. SOCK_STREAM, IPPROTO_TCP, &s_listen);
  622. if (err) {
  623. s_listen = NULL;
  624. goto out;
  625. }
  626. s_listen->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
  627. drbd_setbufsize(s_listen, sndbuf_size, rcvbuf_size);
  628. what = "bind before listen";
  629. err = s_listen->ops->bind(s_listen, (struct sockaddr *)&my_addr, my_addr_len);
  630. if (err < 0)
  631. goto out;
  632. ad->s_listen = s_listen;
  633. write_lock_bh(&s_listen->sk->sk_callback_lock);
  634. ad->original_sk_state_change = s_listen->sk->sk_state_change;
  635. s_listen->sk->sk_state_change = drbd_incoming_connection;
  636. s_listen->sk->sk_user_data = ad;
  637. write_unlock_bh(&s_listen->sk->sk_callback_lock);
  638. what = "listen";
  639. err = s_listen->ops->listen(s_listen, 5);
  640. if (err < 0)
  641. goto out;
  642. return 0;
  643. out:
  644. if (s_listen)
  645. sock_release(s_listen);
  646. if (err < 0) {
  647. if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
  648. drbd_err(connection, "%s failed, err = %d\n", what, err);
  649. conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
  650. }
  651. }
  652. return -EIO;
  653. }
  654. static void unregister_state_change(struct sock *sk, struct accept_wait_data *ad)
  655. {
  656. write_lock_bh(&sk->sk_callback_lock);
  657. sk->sk_state_change = ad->original_sk_state_change;
  658. sk->sk_user_data = NULL;
  659. write_unlock_bh(&sk->sk_callback_lock);
  660. }
  661. static struct socket *drbd_wait_for_connect(struct drbd_connection *connection, struct accept_wait_data *ad)
  662. {
  663. int timeo, connect_int, err = 0;
  664. struct socket *s_estab = NULL;
  665. struct net_conf *nc;
  666. rcu_read_lock();
  667. nc = rcu_dereference(connection->net_conf);
  668. if (!nc) {
  669. rcu_read_unlock();
  670. return NULL;
  671. }
  672. connect_int = nc->connect_int;
  673. rcu_read_unlock();
  674. timeo = connect_int * HZ;
  675. /* 28.5% random jitter */
  676. timeo += (prandom_u32() & 1) ? timeo / 7 : -timeo / 7;
  677. err = wait_for_completion_interruptible_timeout(&ad->door_bell, timeo);
  678. if (err <= 0)
  679. return NULL;
  680. err = kernel_accept(ad->s_listen, &s_estab, 0);
  681. if (err < 0) {
  682. if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
  683. drbd_err(connection, "accept failed, err = %d\n", err);
  684. conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
  685. }
  686. }
  687. if (s_estab)
  688. unregister_state_change(s_estab->sk, ad);
  689. return s_estab;
  690. }
  691. static int decode_header(struct drbd_connection *, void *, struct packet_info *);
  692. static int send_first_packet(struct drbd_connection *connection, struct drbd_socket *sock,
  693. enum drbd_packet cmd)
  694. {
  695. if (!conn_prepare_command(connection, sock))
  696. return -EIO;
  697. return conn_send_command(connection, sock, cmd, 0, NULL, 0);
  698. }
  699. static int receive_first_packet(struct drbd_connection *connection, struct socket *sock)
  700. {
  701. unsigned int header_size = drbd_header_size(connection);
  702. struct packet_info pi;
  703. struct net_conf *nc;
  704. int err;
  705. rcu_read_lock();
  706. nc = rcu_dereference(connection->net_conf);
  707. if (!nc) {
  708. rcu_read_unlock();
  709. return -EIO;
  710. }
  711. sock->sk->sk_rcvtimeo = nc->ping_timeo * 4 * HZ / 10;
  712. rcu_read_unlock();
  713. err = drbd_recv_short(sock, connection->data.rbuf, header_size, 0);
  714. if (err != header_size) {
  715. if (err >= 0)
  716. err = -EIO;
  717. return err;
  718. }
  719. err = decode_header(connection, connection->data.rbuf, &pi);
  720. if (err)
  721. return err;
  722. return pi.cmd;
  723. }
  724. /**
  725. * drbd_socket_okay() - Free the socket if its connection is not okay
  726. * @sock: pointer to the pointer to the socket.
  727. */
  728. static bool drbd_socket_okay(struct socket **sock)
  729. {
  730. int rr;
  731. char tb[4];
  732. if (!*sock)
  733. return false;
  734. rr = drbd_recv_short(*sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
  735. if (rr > 0 || rr == -EAGAIN) {
  736. return true;
  737. } else {
  738. sock_release(*sock);
  739. *sock = NULL;
  740. return false;
  741. }
  742. }
  743. static bool connection_established(struct drbd_connection *connection,
  744. struct socket **sock1,
  745. struct socket **sock2)
  746. {
  747. struct net_conf *nc;
  748. int timeout;
  749. bool ok;
  750. if (!*sock1 || !*sock2)
  751. return false;
  752. rcu_read_lock();
  753. nc = rcu_dereference(connection->net_conf);
  754. timeout = (nc->sock_check_timeo ?: nc->ping_timeo) * HZ / 10;
  755. rcu_read_unlock();
  756. schedule_timeout_interruptible(timeout);
  757. ok = drbd_socket_okay(sock1);
  758. ok = drbd_socket_okay(sock2) && ok;
  759. return ok;
  760. }
  761. /* Gets called if a connection is established, or if a new minor gets created
  762. in a connection */
  763. int drbd_connected(struct drbd_peer_device *peer_device)
  764. {
  765. struct drbd_device *device = peer_device->device;
  766. int err;
  767. atomic_set(&device->packet_seq, 0);
  768. device->peer_seq = 0;
  769. device->state_mutex = peer_device->connection->agreed_pro_version < 100 ?
  770. &peer_device->connection->cstate_mutex :
  771. &device->own_state_mutex;
  772. err = drbd_send_sync_param(peer_device);
  773. if (!err)
  774. err = drbd_send_sizes(peer_device, 0, 0);
  775. if (!err)
  776. err = drbd_send_uuids(peer_device);
  777. if (!err)
  778. err = drbd_send_current_state(peer_device);
  779. clear_bit(USE_DEGR_WFC_T, &device->flags);
  780. clear_bit(RESIZE_PENDING, &device->flags);
  781. atomic_set(&device->ap_in_flight, 0);
  782. mod_timer(&device->request_timer, jiffies + HZ); /* just start it here. */
  783. return err;
  784. }
  785. /*
  786. * return values:
  787. * 1 yes, we have a valid connection
  788. * 0 oops, did not work out, please try again
  789. * -1 peer talks different language,
  790. * no point in trying again, please go standalone.
  791. * -2 We do not have a network config...
  792. */
  793. static int conn_connect(struct drbd_connection *connection)
  794. {
  795. struct drbd_socket sock, msock;
  796. struct drbd_peer_device *peer_device;
  797. struct net_conf *nc;
  798. int vnr, timeout, h;
  799. bool discard_my_data, ok;
  800. enum drbd_state_rv rv;
  801. struct accept_wait_data ad = {
  802. .connection = connection,
  803. .door_bell = COMPLETION_INITIALIZER_ONSTACK(ad.door_bell),
  804. };
  805. clear_bit(DISCONNECT_SENT, &connection->flags);
  806. if (conn_request_state(connection, NS(conn, C_WF_CONNECTION), CS_VERBOSE) < SS_SUCCESS)
  807. return -2;
  808. mutex_init(&sock.mutex);
  809. sock.sbuf = connection->data.sbuf;
  810. sock.rbuf = connection->data.rbuf;
  811. sock.socket = NULL;
  812. mutex_init(&msock.mutex);
  813. msock.sbuf = connection->meta.sbuf;
  814. msock.rbuf = connection->meta.rbuf;
  815. msock.socket = NULL;
  816. /* Assume that the peer only understands protocol 80 until we know better. */
  817. connection->agreed_pro_version = 80;
  818. if (prepare_listen_socket(connection, &ad))
  819. return 0;
  820. do {
  821. struct socket *s;
  822. s = drbd_try_connect(connection);
  823. if (s) {
  824. if (!sock.socket) {
  825. sock.socket = s;
  826. send_first_packet(connection, &sock, P_INITIAL_DATA);
  827. } else if (!msock.socket) {
  828. clear_bit(RESOLVE_CONFLICTS, &connection->flags);
  829. msock.socket = s;
  830. send_first_packet(connection, &msock, P_INITIAL_META);
  831. } else {
  832. drbd_err(connection, "Logic error in conn_connect()\n");
  833. goto out_release_sockets;
  834. }
  835. }
  836. if (connection_established(connection, &sock.socket, &msock.socket))
  837. break;
  838. retry:
  839. s = drbd_wait_for_connect(connection, &ad);
  840. if (s) {
  841. int fp = receive_first_packet(connection, s);
  842. drbd_socket_okay(&sock.socket);
  843. drbd_socket_okay(&msock.socket);
  844. switch (fp) {
  845. case P_INITIAL_DATA:
  846. if (sock.socket) {
  847. drbd_warn(connection, "initial packet S crossed\n");
  848. sock_release(sock.socket);
  849. sock.socket = s;
  850. goto randomize;
  851. }
  852. sock.socket = s;
  853. break;
  854. case P_INITIAL_META:
  855. set_bit(RESOLVE_CONFLICTS, &connection->flags);
  856. if (msock.socket) {
  857. drbd_warn(connection, "initial packet M crossed\n");
  858. sock_release(msock.socket);
  859. msock.socket = s;
  860. goto randomize;
  861. }
  862. msock.socket = s;
  863. break;
  864. default:
  865. drbd_warn(connection, "Error receiving initial packet\n");
  866. sock_release(s);
  867. randomize:
  868. if (prandom_u32() & 1)
  869. goto retry;
  870. }
  871. }
  872. if (connection->cstate <= C_DISCONNECTING)
  873. goto out_release_sockets;
  874. if (signal_pending(current)) {
  875. flush_signals(current);
  876. smp_rmb();
  877. if (get_t_state(&connection->receiver) == EXITING)
  878. goto out_release_sockets;
  879. }
  880. ok = connection_established(connection, &sock.socket, &msock.socket);
  881. } while (!ok);
  882. if (ad.s_listen)
  883. sock_release(ad.s_listen);
  884. sock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
  885. msock.socket->sk->sk_reuse = SK_CAN_REUSE; /* SO_REUSEADDR */
  886. sock.socket->sk->sk_allocation = GFP_NOIO;
  887. msock.socket->sk->sk_allocation = GFP_NOIO;
  888. sock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
  889. msock.socket->sk->sk_priority = TC_PRIO_INTERACTIVE;
  890. /* NOT YET ...
  891. * sock.socket->sk->sk_sndtimeo = connection->net_conf->timeout*HZ/10;
  892. * sock.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  893. * first set it to the P_CONNECTION_FEATURES timeout,
  894. * which we set to 4x the configured ping_timeout. */
  895. rcu_read_lock();
  896. nc = rcu_dereference(connection->net_conf);
  897. sock.socket->sk->sk_sndtimeo =
  898. sock.socket->sk->sk_rcvtimeo = nc->ping_timeo*4*HZ/10;
  899. msock.socket->sk->sk_rcvtimeo = nc->ping_int*HZ;
  900. timeout = nc->timeout * HZ / 10;
  901. discard_my_data = nc->discard_my_data;
  902. rcu_read_unlock();
  903. msock.socket->sk->sk_sndtimeo = timeout;
  904. /* we don't want delays.
  905. * we use TCP_CORK where appropriate, though */
  906. drbd_tcp_nodelay(sock.socket);
  907. drbd_tcp_nodelay(msock.socket);
  908. connection->data.socket = sock.socket;
  909. connection->meta.socket = msock.socket;
  910. connection->last_received = jiffies;
  911. h = drbd_do_features(connection);
  912. if (h <= 0)
  913. return h;
  914. if (connection->cram_hmac_tfm) {
  915. /* drbd_request_state(device, NS(conn, WFAuth)); */
  916. switch (drbd_do_auth(connection)) {
  917. case -1:
  918. drbd_err(connection, "Authentication of peer failed\n");
  919. return -1;
  920. case 0:
  921. drbd_err(connection, "Authentication of peer failed, trying again.\n");
  922. return 0;
  923. }
  924. }
  925. connection->data.socket->sk->sk_sndtimeo = timeout;
  926. connection->data.socket->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  927. if (drbd_send_protocol(connection) == -EOPNOTSUPP)
  928. return -1;
  929. /* Prevent a race between resync-handshake and
  930. * being promoted to Primary.
  931. *
  932. * Grab and release the state mutex, so we know that any current
  933. * drbd_set_role() is finished, and any incoming drbd_set_role
  934. * will see the STATE_SENT flag, and wait for it to be cleared.
  935. */
  936. idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
  937. mutex_lock(peer_device->device->state_mutex);
  938. /* avoid a race with conn_request_state( C_DISCONNECTING ) */
  939. spin_lock_irq(&connection->resource->req_lock);
  940. set_bit(STATE_SENT, &connection->flags);
  941. spin_unlock_irq(&connection->resource->req_lock);
  942. idr_for_each_entry(&connection->peer_devices, peer_device, vnr)
  943. mutex_unlock(peer_device->device->state_mutex);
  944. rcu_read_lock();
  945. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  946. struct drbd_device *device = peer_device->device;
  947. kref_get(&device->kref);
  948. rcu_read_unlock();
  949. if (discard_my_data)
  950. set_bit(DISCARD_MY_DATA, &device->flags);
  951. else
  952. clear_bit(DISCARD_MY_DATA, &device->flags);
  953. drbd_connected(peer_device);
  954. kref_put(&device->kref, drbd_destroy_device);
  955. rcu_read_lock();
  956. }
  957. rcu_read_unlock();
  958. rv = conn_request_state(connection, NS(conn, C_WF_REPORT_PARAMS), CS_VERBOSE);
  959. if (rv < SS_SUCCESS || connection->cstate != C_WF_REPORT_PARAMS) {
  960. clear_bit(STATE_SENT, &connection->flags);
  961. return 0;
  962. }
  963. drbd_thread_start(&connection->ack_receiver);
  964. /* opencoded create_singlethread_workqueue(),
  965. * to be able to use format string arguments */
  966. connection->ack_sender =
  967. alloc_ordered_workqueue("drbd_as_%s", WQ_MEM_RECLAIM, connection->resource->name);
  968. if (!connection->ack_sender) {
  969. drbd_err(connection, "Failed to create workqueue ack_sender\n");
  970. return 0;
  971. }
  972. mutex_lock(&connection->resource->conf_update);
  973. /* The discard_my_data flag is a single-shot modifier to the next
  974. * connection attempt, the handshake of which is now well underway.
  975. * No need for rcu style copying of the whole struct
  976. * just to clear a single value. */
  977. connection->net_conf->discard_my_data = 0;
  978. mutex_unlock(&connection->resource->conf_update);
  979. return h;
  980. out_release_sockets:
  981. if (ad.s_listen)
  982. sock_release(ad.s_listen);
  983. if (sock.socket)
  984. sock_release(sock.socket);
  985. if (msock.socket)
  986. sock_release(msock.socket);
  987. return -1;
  988. }
  989. static int decode_header(struct drbd_connection *connection, void *header, struct packet_info *pi)
  990. {
  991. unsigned int header_size = drbd_header_size(connection);
  992. if (header_size == sizeof(struct p_header100) &&
  993. *(__be32 *)header == cpu_to_be32(DRBD_MAGIC_100)) {
  994. struct p_header100 *h = header;
  995. if (h->pad != 0) {
  996. drbd_err(connection, "Header padding is not zero\n");
  997. return -EINVAL;
  998. }
  999. pi->vnr = be16_to_cpu(h->volume);
  1000. pi->cmd = be16_to_cpu(h->command);
  1001. pi->size = be32_to_cpu(h->length);
  1002. } else if (header_size == sizeof(struct p_header95) &&
  1003. *(__be16 *)header == cpu_to_be16(DRBD_MAGIC_BIG)) {
  1004. struct p_header95 *h = header;
  1005. pi->cmd = be16_to_cpu(h->command);
  1006. pi->size = be32_to_cpu(h->length);
  1007. pi->vnr = 0;
  1008. } else if (header_size == sizeof(struct p_header80) &&
  1009. *(__be32 *)header == cpu_to_be32(DRBD_MAGIC)) {
  1010. struct p_header80 *h = header;
  1011. pi->cmd = be16_to_cpu(h->command);
  1012. pi->size = be16_to_cpu(h->length);
  1013. pi->vnr = 0;
  1014. } else {
  1015. drbd_err(connection, "Wrong magic value 0x%08x in protocol version %d\n",
  1016. be32_to_cpu(*(__be32 *)header),
  1017. connection->agreed_pro_version);
  1018. return -EINVAL;
  1019. }
  1020. pi->data = header + header_size;
  1021. return 0;
  1022. }
  1023. static void drbd_unplug_all_devices(struct drbd_connection *connection)
  1024. {
  1025. if (current->plug == &connection->receiver_plug) {
  1026. blk_finish_plug(&connection->receiver_plug);
  1027. blk_start_plug(&connection->receiver_plug);
  1028. } /* else: maybe just schedule() ?? */
  1029. }
  1030. static int drbd_recv_header(struct drbd_connection *connection, struct packet_info *pi)
  1031. {
  1032. void *buffer = connection->data.rbuf;
  1033. int err;
  1034. err = drbd_recv_all_warn(connection, buffer, drbd_header_size(connection));
  1035. if (err)
  1036. return err;
  1037. err = decode_header(connection, buffer, pi);
  1038. connection->last_received = jiffies;
  1039. return err;
  1040. }
  1041. static int drbd_recv_header_maybe_unplug(struct drbd_connection *connection, struct packet_info *pi)
  1042. {
  1043. void *buffer = connection->data.rbuf;
  1044. unsigned int size = drbd_header_size(connection);
  1045. int err;
  1046. err = drbd_recv_short(connection->data.socket, buffer, size, MSG_NOSIGNAL|MSG_DONTWAIT);
  1047. if (err != size) {
  1048. /* If we have nothing in the receive buffer now, to reduce
  1049. * application latency, try to drain the backend queues as
  1050. * quickly as possible, and let remote TCP know what we have
  1051. * received so far. */
  1052. if (err == -EAGAIN) {
  1053. drbd_tcp_quickack(connection->data.socket);
  1054. drbd_unplug_all_devices(connection);
  1055. }
  1056. if (err > 0) {
  1057. buffer += err;
  1058. size -= err;
  1059. }
  1060. err = drbd_recv_all_warn(connection, buffer, size);
  1061. if (err)
  1062. return err;
  1063. }
  1064. err = decode_header(connection, connection->data.rbuf, pi);
  1065. connection->last_received = jiffies;
  1066. return err;
  1067. }
  1068. /* This is blkdev_issue_flush, but asynchronous.
  1069. * We want to submit to all component volumes in parallel,
  1070. * then wait for all completions.
  1071. */
  1072. struct issue_flush_context {
  1073. atomic_t pending;
  1074. int error;
  1075. struct completion done;
  1076. };
  1077. struct one_flush_context {
  1078. struct drbd_device *device;
  1079. struct issue_flush_context *ctx;
  1080. };
  1081. static void one_flush_endio(struct bio *bio)
  1082. {
  1083. struct one_flush_context *octx = bio->bi_private;
  1084. struct drbd_device *device = octx->device;
  1085. struct issue_flush_context *ctx = octx->ctx;
  1086. if (bio->bi_status) {
  1087. ctx->error = blk_status_to_errno(bio->bi_status);
  1088. drbd_info(device, "local disk FLUSH FAILED with status %d\n", bio->bi_status);
  1089. }
  1090. kfree(octx);
  1091. bio_put(bio);
  1092. clear_bit(FLUSH_PENDING, &device->flags);
  1093. put_ldev(device);
  1094. kref_put(&device->kref, drbd_destroy_device);
  1095. if (atomic_dec_and_test(&ctx->pending))
  1096. complete(&ctx->done);
  1097. }
  1098. static void submit_one_flush(struct drbd_device *device, struct issue_flush_context *ctx)
  1099. {
  1100. struct bio *bio = bio_alloc(GFP_NOIO, 0);
  1101. struct one_flush_context *octx = kmalloc(sizeof(*octx), GFP_NOIO);
  1102. if (!bio || !octx) {
  1103. drbd_warn(device, "Could not allocate a bio, CANNOT ISSUE FLUSH\n");
  1104. /* FIXME: what else can I do now? disconnecting or detaching
  1105. * really does not help to improve the state of the world, either.
  1106. */
  1107. kfree(octx);
  1108. if (bio)
  1109. bio_put(bio);
  1110. ctx->error = -ENOMEM;
  1111. put_ldev(device);
  1112. kref_put(&device->kref, drbd_destroy_device);
  1113. return;
  1114. }
  1115. octx->device = device;
  1116. octx->ctx = ctx;
  1117. bio_set_dev(bio, device->ldev->backing_bdev);
  1118. bio->bi_private = octx;
  1119. bio->bi_end_io = one_flush_endio;
  1120. bio->bi_opf = REQ_OP_FLUSH | REQ_PREFLUSH;
  1121. device->flush_jif = jiffies;
  1122. set_bit(FLUSH_PENDING, &device->flags);
  1123. atomic_inc(&ctx->pending);
  1124. submit_bio(bio);
  1125. }
  1126. static void drbd_flush(struct drbd_connection *connection)
  1127. {
  1128. if (connection->resource->write_ordering >= WO_BDEV_FLUSH) {
  1129. struct drbd_peer_device *peer_device;
  1130. struct issue_flush_context ctx;
  1131. int vnr;
  1132. atomic_set(&ctx.pending, 1);
  1133. ctx.error = 0;
  1134. init_completion(&ctx.done);
  1135. rcu_read_lock();
  1136. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  1137. struct drbd_device *device = peer_device->device;
  1138. if (!get_ldev(device))
  1139. continue;
  1140. kref_get(&device->kref);
  1141. rcu_read_unlock();
  1142. submit_one_flush(device, &ctx);
  1143. rcu_read_lock();
  1144. }
  1145. rcu_read_unlock();
  1146. /* Do we want to add a timeout,
  1147. * if disk-timeout is set? */
  1148. if (!atomic_dec_and_test(&ctx.pending))
  1149. wait_for_completion(&ctx.done);
  1150. if (ctx.error) {
  1151. /* would rather check on EOPNOTSUPP, but that is not reliable.
  1152. * don't try again for ANY return value != 0
  1153. * if (rv == -EOPNOTSUPP) */
  1154. /* Any error is already reported by bio_endio callback. */
  1155. drbd_bump_write_ordering(connection->resource, NULL, WO_DRAIN_IO);
  1156. }
  1157. }
  1158. }
  1159. /**
  1160. * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
  1161. * @device: DRBD device.
  1162. * @epoch: Epoch object.
  1163. * @ev: Epoch event.
  1164. */
  1165. static enum finish_epoch drbd_may_finish_epoch(struct drbd_connection *connection,
  1166. struct drbd_epoch *epoch,
  1167. enum epoch_event ev)
  1168. {
  1169. int epoch_size;
  1170. struct drbd_epoch *next_epoch;
  1171. enum finish_epoch rv = FE_STILL_LIVE;
  1172. spin_lock(&connection->epoch_lock);
  1173. do {
  1174. next_epoch = NULL;
  1175. epoch_size = atomic_read(&epoch->epoch_size);
  1176. switch (ev & ~EV_CLEANUP) {
  1177. case EV_PUT:
  1178. atomic_dec(&epoch->active);
  1179. break;
  1180. case EV_GOT_BARRIER_NR:
  1181. set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
  1182. break;
  1183. case EV_BECAME_LAST:
  1184. /* nothing to do*/
  1185. break;
  1186. }
  1187. if (epoch_size != 0 &&
  1188. atomic_read(&epoch->active) == 0 &&
  1189. (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags) || ev & EV_CLEANUP)) {
  1190. if (!(ev & EV_CLEANUP)) {
  1191. spin_unlock(&connection->epoch_lock);
  1192. drbd_send_b_ack(epoch->connection, epoch->barrier_nr, epoch_size);
  1193. spin_lock(&connection->epoch_lock);
  1194. }
  1195. #if 0
  1196. /* FIXME: dec unacked on connection, once we have
  1197. * something to count pending connection packets in. */
  1198. if (test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags))
  1199. dec_unacked(epoch->connection);
  1200. #endif
  1201. if (connection->current_epoch != epoch) {
  1202. next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
  1203. list_del(&epoch->list);
  1204. ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
  1205. connection->epochs--;
  1206. kfree(epoch);
  1207. if (rv == FE_STILL_LIVE)
  1208. rv = FE_DESTROYED;
  1209. } else {
  1210. epoch->flags = 0;
  1211. atomic_set(&epoch->epoch_size, 0);
  1212. /* atomic_set(&epoch->active, 0); is already zero */
  1213. if (rv == FE_STILL_LIVE)
  1214. rv = FE_RECYCLED;
  1215. }
  1216. }
  1217. if (!next_epoch)
  1218. break;
  1219. epoch = next_epoch;
  1220. } while (1);
  1221. spin_unlock(&connection->epoch_lock);
  1222. return rv;
  1223. }
  1224. static enum write_ordering_e
  1225. max_allowed_wo(struct drbd_backing_dev *bdev, enum write_ordering_e wo)
  1226. {
  1227. struct disk_conf *dc;
  1228. dc = rcu_dereference(bdev->disk_conf);
  1229. if (wo == WO_BDEV_FLUSH && !dc->disk_flushes)
  1230. wo = WO_DRAIN_IO;
  1231. if (wo == WO_DRAIN_IO && !dc->disk_drain)
  1232. wo = WO_NONE;
  1233. return wo;
  1234. }
  1235. /**
  1236. * drbd_bump_write_ordering() - Fall back to an other write ordering method
  1237. * @connection: DRBD connection.
  1238. * @wo: Write ordering method to try.
  1239. */
  1240. void drbd_bump_write_ordering(struct drbd_resource *resource, struct drbd_backing_dev *bdev,
  1241. enum write_ordering_e wo)
  1242. {
  1243. struct drbd_device *device;
  1244. enum write_ordering_e pwo;
  1245. int vnr;
  1246. static char *write_ordering_str[] = {
  1247. [WO_NONE] = "none",
  1248. [WO_DRAIN_IO] = "drain",
  1249. [WO_BDEV_FLUSH] = "flush",
  1250. };
  1251. pwo = resource->write_ordering;
  1252. if (wo != WO_BDEV_FLUSH)
  1253. wo = min(pwo, wo);
  1254. rcu_read_lock();
  1255. idr_for_each_entry(&resource->devices, device, vnr) {
  1256. if (get_ldev(device)) {
  1257. wo = max_allowed_wo(device->ldev, wo);
  1258. if (device->ldev == bdev)
  1259. bdev = NULL;
  1260. put_ldev(device);
  1261. }
  1262. }
  1263. if (bdev)
  1264. wo = max_allowed_wo(bdev, wo);
  1265. rcu_read_unlock();
  1266. resource->write_ordering = wo;
  1267. if (pwo != resource->write_ordering || wo == WO_BDEV_FLUSH)
  1268. drbd_info(resource, "Method to ensure write ordering: %s\n", write_ordering_str[resource->write_ordering]);
  1269. }
  1270. static void drbd_issue_peer_discard(struct drbd_device *device, struct drbd_peer_request *peer_req)
  1271. {
  1272. struct block_device *bdev = device->ldev->backing_bdev;
  1273. if (blkdev_issue_zeroout(bdev, peer_req->i.sector, peer_req->i.size >> 9,
  1274. GFP_NOIO, 0))
  1275. peer_req->flags |= EE_WAS_ERROR;
  1276. drbd_endio_write_sec_final(peer_req);
  1277. }
  1278. static void drbd_issue_peer_wsame(struct drbd_device *device,
  1279. struct drbd_peer_request *peer_req)
  1280. {
  1281. struct block_device *bdev = device->ldev->backing_bdev;
  1282. sector_t s = peer_req->i.sector;
  1283. sector_t nr = peer_req->i.size >> 9;
  1284. if (blkdev_issue_write_same(bdev, s, nr, GFP_NOIO, peer_req->pages))
  1285. peer_req->flags |= EE_WAS_ERROR;
  1286. drbd_endio_write_sec_final(peer_req);
  1287. }
  1288. /**
  1289. * drbd_submit_peer_request()
  1290. * @device: DRBD device.
  1291. * @peer_req: peer request
  1292. * @rw: flag field, see bio->bi_opf
  1293. *
  1294. * May spread the pages to multiple bios,
  1295. * depending on bio_add_page restrictions.
  1296. *
  1297. * Returns 0 if all bios have been submitted,
  1298. * -ENOMEM if we could not allocate enough bios,
  1299. * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
  1300. * single page to an empty bio (which should never happen and likely indicates
  1301. * that the lower level IO stack is in some way broken). This has been observed
  1302. * on certain Xen deployments.
  1303. */
  1304. /* TODO allocate from our own bio_set. */
  1305. int drbd_submit_peer_request(struct drbd_device *device,
  1306. struct drbd_peer_request *peer_req,
  1307. const unsigned op, const unsigned op_flags,
  1308. const int fault_type)
  1309. {
  1310. struct bio *bios = NULL;
  1311. struct bio *bio;
  1312. struct page *page = peer_req->pages;
  1313. sector_t sector = peer_req->i.sector;
  1314. unsigned data_size = peer_req->i.size;
  1315. unsigned n_bios = 0;
  1316. unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
  1317. int err = -ENOMEM;
  1318. /* TRIM/DISCARD: for now, always use the helper function
  1319. * blkdev_issue_zeroout(..., discard=true).
  1320. * It's synchronous, but it does the right thing wrt. bio splitting.
  1321. * Correctness first, performance later. Next step is to code an
  1322. * asynchronous variant of the same.
  1323. */
  1324. if (peer_req->flags & (EE_IS_TRIM|EE_WRITE_SAME)) {
  1325. /* wait for all pending IO completions, before we start
  1326. * zeroing things out. */
  1327. conn_wait_active_ee_empty(peer_req->peer_device->connection);
  1328. /* add it to the active list now,
  1329. * so we can find it to present it in debugfs */
  1330. peer_req->submit_jif = jiffies;
  1331. peer_req->flags |= EE_SUBMITTED;
  1332. /* If this was a resync request from receive_rs_deallocated(),
  1333. * it is already on the sync_ee list */
  1334. if (list_empty(&peer_req->w.list)) {
  1335. spin_lock_irq(&device->resource->req_lock);
  1336. list_add_tail(&peer_req->w.list, &device->active_ee);
  1337. spin_unlock_irq(&device->resource->req_lock);
  1338. }
  1339. if (peer_req->flags & EE_IS_TRIM)
  1340. drbd_issue_peer_discard(device, peer_req);
  1341. else /* EE_WRITE_SAME */
  1342. drbd_issue_peer_wsame(device, peer_req);
  1343. return 0;
  1344. }
  1345. /* In most cases, we will only need one bio. But in case the lower
  1346. * level restrictions happen to be different at this offset on this
  1347. * side than those of the sending peer, we may need to submit the
  1348. * request in more than one bio.
  1349. *
  1350. * Plain bio_alloc is good enough here, this is no DRBD internally
  1351. * generated bio, but a bio allocated on behalf of the peer.
  1352. */
  1353. next_bio:
  1354. bio = bio_alloc(GFP_NOIO, nr_pages);
  1355. if (!bio) {
  1356. drbd_err(device, "submit_ee: Allocation of a bio failed (nr_pages=%u)\n", nr_pages);
  1357. goto fail;
  1358. }
  1359. /* > peer_req->i.sector, unless this is the first bio */
  1360. bio->bi_iter.bi_sector = sector;
  1361. bio_set_dev(bio, device->ldev->backing_bdev);
  1362. bio_set_op_attrs(bio, op, op_flags);
  1363. bio->bi_private = peer_req;
  1364. bio->bi_end_io = drbd_peer_request_endio;
  1365. bio->bi_next = bios;
  1366. bios = bio;
  1367. ++n_bios;
  1368. page_chain_for_each(page) {
  1369. unsigned len = min_t(unsigned, data_size, PAGE_SIZE);
  1370. if (!bio_add_page(bio, page, len, 0))
  1371. goto next_bio;
  1372. data_size -= len;
  1373. sector += len >> 9;
  1374. --nr_pages;
  1375. }
  1376. D_ASSERT(device, data_size == 0);
  1377. D_ASSERT(device, page == NULL);
  1378. atomic_set(&peer_req->pending_bios, n_bios);
  1379. /* for debugfs: update timestamp, mark as submitted */
  1380. peer_req->submit_jif = jiffies;
  1381. peer_req->flags |= EE_SUBMITTED;
  1382. do {
  1383. bio = bios;
  1384. bios = bios->bi_next;
  1385. bio->bi_next = NULL;
  1386. drbd_generic_make_request(device, fault_type, bio);
  1387. } while (bios);
  1388. return 0;
  1389. fail:
  1390. while (bios) {
  1391. bio = bios;
  1392. bios = bios->bi_next;
  1393. bio_put(bio);
  1394. }
  1395. return err;
  1396. }
  1397. static void drbd_remove_epoch_entry_interval(struct drbd_device *device,
  1398. struct drbd_peer_request *peer_req)
  1399. {
  1400. struct drbd_interval *i = &peer_req->i;
  1401. drbd_remove_interval(&device->write_requests, i);
  1402. drbd_clear_interval(i);
  1403. /* Wake up any processes waiting for this peer request to complete. */
  1404. if (i->waiting)
  1405. wake_up(&device->misc_wait);
  1406. }
  1407. static void conn_wait_active_ee_empty(struct drbd_connection *connection)
  1408. {
  1409. struct drbd_peer_device *peer_device;
  1410. int vnr;
  1411. rcu_read_lock();
  1412. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  1413. struct drbd_device *device = peer_device->device;
  1414. kref_get(&device->kref);
  1415. rcu_read_unlock();
  1416. drbd_wait_ee_list_empty(device, &device->active_ee);
  1417. kref_put(&device->kref, drbd_destroy_device);
  1418. rcu_read_lock();
  1419. }
  1420. rcu_read_unlock();
  1421. }
  1422. static int receive_Barrier(struct drbd_connection *connection, struct packet_info *pi)
  1423. {
  1424. int rv;
  1425. struct p_barrier *p = pi->data;
  1426. struct drbd_epoch *epoch;
  1427. /* FIXME these are unacked on connection,
  1428. * not a specific (peer)device.
  1429. */
  1430. connection->current_epoch->barrier_nr = p->barrier;
  1431. connection->current_epoch->connection = connection;
  1432. rv = drbd_may_finish_epoch(connection, connection->current_epoch, EV_GOT_BARRIER_NR);
  1433. /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
  1434. * the activity log, which means it would not be resynced in case the
  1435. * R_PRIMARY crashes now.
  1436. * Therefore we must send the barrier_ack after the barrier request was
  1437. * completed. */
  1438. switch (connection->resource->write_ordering) {
  1439. case WO_NONE:
  1440. if (rv == FE_RECYCLED)
  1441. return 0;
  1442. /* receiver context, in the writeout path of the other node.
  1443. * avoid potential distributed deadlock */
  1444. epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
  1445. if (epoch)
  1446. break;
  1447. else
  1448. drbd_warn(connection, "Allocation of an epoch failed, slowing down\n");
  1449. /* Fall through */
  1450. case WO_BDEV_FLUSH:
  1451. case WO_DRAIN_IO:
  1452. conn_wait_active_ee_empty(connection);
  1453. drbd_flush(connection);
  1454. if (atomic_read(&connection->current_epoch->epoch_size)) {
  1455. epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
  1456. if (epoch)
  1457. break;
  1458. }
  1459. return 0;
  1460. default:
  1461. drbd_err(connection, "Strangeness in connection->write_ordering %d\n",
  1462. connection->resource->write_ordering);
  1463. return -EIO;
  1464. }
  1465. epoch->flags = 0;
  1466. atomic_set(&epoch->epoch_size, 0);
  1467. atomic_set(&epoch->active, 0);
  1468. spin_lock(&connection->epoch_lock);
  1469. if (atomic_read(&connection->current_epoch->epoch_size)) {
  1470. list_add(&epoch->list, &connection->current_epoch->list);
  1471. connection->current_epoch = epoch;
  1472. connection->epochs++;
  1473. } else {
  1474. /* The current_epoch got recycled while we allocated this one... */
  1475. kfree(epoch);
  1476. }
  1477. spin_unlock(&connection->epoch_lock);
  1478. return 0;
  1479. }
  1480. /* quick wrapper in case payload size != request_size (write same) */
  1481. static void drbd_csum_ee_size(struct crypto_ahash *h,
  1482. struct drbd_peer_request *r, void *d,
  1483. unsigned int payload_size)
  1484. {
  1485. unsigned int tmp = r->i.size;
  1486. r->i.size = payload_size;
  1487. drbd_csum_ee(h, r, d);
  1488. r->i.size = tmp;
  1489. }
  1490. /* used from receive_RSDataReply (recv_resync_read)
  1491. * and from receive_Data.
  1492. * data_size: actual payload ("data in")
  1493. * for normal writes that is bi_size.
  1494. * for discards, that is zero.
  1495. * for write same, it is logical_block_size.
  1496. * both trim and write same have the bi_size ("data len to be affected")
  1497. * as extra argument in the packet header.
  1498. */
  1499. static struct drbd_peer_request *
  1500. read_in_block(struct drbd_peer_device *peer_device, u64 id, sector_t sector,
  1501. struct packet_info *pi) __must_hold(local)
  1502. {
  1503. struct drbd_device *device = peer_device->device;
  1504. const sector_t capacity = drbd_get_capacity(device->this_bdev);
  1505. struct drbd_peer_request *peer_req;
  1506. struct page *page;
  1507. int digest_size, err;
  1508. unsigned int data_size = pi->size, ds;
  1509. void *dig_in = peer_device->connection->int_dig_in;
  1510. void *dig_vv = peer_device->connection->int_dig_vv;
  1511. unsigned long *data;
  1512. struct p_trim *trim = (pi->cmd == P_TRIM) ? pi->data : NULL;
  1513. struct p_trim *wsame = (pi->cmd == P_WSAME) ? pi->data : NULL;
  1514. digest_size = 0;
  1515. if (!trim && peer_device->connection->peer_integrity_tfm) {
  1516. digest_size = crypto_ahash_digestsize(peer_device->connection->peer_integrity_tfm);
  1517. /*
  1518. * FIXME: Receive the incoming digest into the receive buffer
  1519. * here, together with its struct p_data?
  1520. */
  1521. err = drbd_recv_all_warn(peer_device->connection, dig_in, digest_size);
  1522. if (err)
  1523. return NULL;
  1524. data_size -= digest_size;
  1525. }
  1526. /* assume request_size == data_size, but special case trim and wsame. */
  1527. ds = data_size;
  1528. if (trim) {
  1529. if (!expect(data_size == 0))
  1530. return NULL;
  1531. ds = be32_to_cpu(trim->size);
  1532. } else if (wsame) {
  1533. if (data_size != queue_logical_block_size(device->rq_queue)) {
  1534. drbd_err(peer_device, "data size (%u) != drbd logical block size (%u)\n",
  1535. data_size, queue_logical_block_size(device->rq_queue));
  1536. return NULL;
  1537. }
  1538. if (data_size != bdev_logical_block_size(device->ldev->backing_bdev)) {
  1539. drbd_err(peer_device, "data size (%u) != backend logical block size (%u)\n",
  1540. data_size, bdev_logical_block_size(device->ldev->backing_bdev));
  1541. return NULL;
  1542. }
  1543. ds = be32_to_cpu(wsame->size);
  1544. }
  1545. if (!expect(IS_ALIGNED(ds, 512)))
  1546. return NULL;
  1547. if (trim || wsame) {
  1548. if (!expect(ds <= (DRBD_MAX_BBIO_SECTORS << 9)))
  1549. return NULL;
  1550. } else if (!expect(ds <= DRBD_MAX_BIO_SIZE))
  1551. return NULL;
  1552. /* even though we trust out peer,
  1553. * we sometimes have to double check. */
  1554. if (sector + (ds>>9) > capacity) {
  1555. drbd_err(device, "request from peer beyond end of local disk: "
  1556. "capacity: %llus < sector: %llus + size: %u\n",
  1557. (unsigned long long)capacity,
  1558. (unsigned long long)sector, ds);
  1559. return NULL;
  1560. }
  1561. /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
  1562. * "criss-cross" setup, that might cause write-out on some other DRBD,
  1563. * which in turn might block on the other node at this very place. */
  1564. peer_req = drbd_alloc_peer_req(peer_device, id, sector, ds, data_size, GFP_NOIO);
  1565. if (!peer_req)
  1566. return NULL;
  1567. peer_req->flags |= EE_WRITE;
  1568. if (trim) {
  1569. peer_req->flags |= EE_IS_TRIM;
  1570. return peer_req;
  1571. }
  1572. if (wsame)
  1573. peer_req->flags |= EE_WRITE_SAME;
  1574. /* receive payload size bytes into page chain */
  1575. ds = data_size;
  1576. page = peer_req->pages;
  1577. page_chain_for_each(page) {
  1578. unsigned len = min_t(int, ds, PAGE_SIZE);
  1579. data = kmap(page);
  1580. err = drbd_recv_all_warn(peer_device->connection, data, len);
  1581. if (drbd_insert_fault(device, DRBD_FAULT_RECEIVE)) {
  1582. drbd_err(device, "Fault injection: Corrupting data on receive\n");
  1583. data[0] = data[0] ^ (unsigned long)-1;
  1584. }
  1585. kunmap(page);
  1586. if (err) {
  1587. drbd_free_peer_req(device, peer_req);
  1588. return NULL;
  1589. }
  1590. ds -= len;
  1591. }
  1592. if (digest_size) {
  1593. drbd_csum_ee_size(peer_device->connection->peer_integrity_tfm, peer_req, dig_vv, data_size);
  1594. if (memcmp(dig_in, dig_vv, digest_size)) {
  1595. drbd_err(device, "Digest integrity check FAILED: %llus +%u\n",
  1596. (unsigned long long)sector, data_size);
  1597. drbd_free_peer_req(device, peer_req);
  1598. return NULL;
  1599. }
  1600. }
  1601. device->recv_cnt += data_size >> 9;
  1602. return peer_req;
  1603. }
  1604. /* drbd_drain_block() just takes a data block
  1605. * out of the socket input buffer, and discards it.
  1606. */
  1607. static int drbd_drain_block(struct drbd_peer_device *peer_device, int data_size)
  1608. {
  1609. struct page *page;
  1610. int err = 0;
  1611. void *data;
  1612. if (!data_size)
  1613. return 0;
  1614. page = drbd_alloc_pages(peer_device, 1, 1);
  1615. data = kmap(page);
  1616. while (data_size) {
  1617. unsigned int len = min_t(int, data_size, PAGE_SIZE);
  1618. err = drbd_recv_all_warn(peer_device->connection, data, len);
  1619. if (err)
  1620. break;
  1621. data_size -= len;
  1622. }
  1623. kunmap(page);
  1624. drbd_free_pages(peer_device->device, page, 0);
  1625. return err;
  1626. }
  1627. static int recv_dless_read(struct drbd_peer_device *peer_device, struct drbd_request *req,
  1628. sector_t sector, int data_size)
  1629. {
  1630. struct bio_vec bvec;
  1631. struct bvec_iter iter;
  1632. struct bio *bio;
  1633. int digest_size, err, expect;
  1634. void *dig_in = peer_device->connection->int_dig_in;
  1635. void *dig_vv = peer_device->connection->int_dig_vv;
  1636. digest_size = 0;
  1637. if (peer_device->connection->peer_integrity_tfm) {
  1638. digest_size = crypto_ahash_digestsize(peer_device->connection->peer_integrity_tfm);
  1639. err = drbd_recv_all_warn(peer_device->connection, dig_in, digest_size);
  1640. if (err)
  1641. return err;
  1642. data_size -= digest_size;
  1643. }
  1644. /* optimistically update recv_cnt. if receiving fails below,
  1645. * we disconnect anyways, and counters will be reset. */
  1646. peer_device->device->recv_cnt += data_size>>9;
  1647. bio = req->master_bio;
  1648. D_ASSERT(peer_device->device, sector == bio->bi_iter.bi_sector);
  1649. bio_for_each_segment(bvec, bio, iter) {
  1650. void *mapped = kmap(bvec.bv_page) + bvec.bv_offset;
  1651. expect = min_t(int, data_size, bvec.bv_len);
  1652. err = drbd_recv_all_warn(peer_device->connection, mapped, expect);
  1653. kunmap(bvec.bv_page);
  1654. if (err)
  1655. return err;
  1656. data_size -= expect;
  1657. }
  1658. if (digest_size) {
  1659. drbd_csum_bio(peer_device->connection->peer_integrity_tfm, bio, dig_vv);
  1660. if (memcmp(dig_in, dig_vv, digest_size)) {
  1661. drbd_err(peer_device, "Digest integrity check FAILED. Broken NICs?\n");
  1662. return -EINVAL;
  1663. }
  1664. }
  1665. D_ASSERT(peer_device->device, data_size == 0);
  1666. return 0;
  1667. }
  1668. /*
  1669. * e_end_resync_block() is called in ack_sender context via
  1670. * drbd_finish_peer_reqs().
  1671. */
  1672. static int e_end_resync_block(struct drbd_work *w, int unused)
  1673. {
  1674. struct drbd_peer_request *peer_req =
  1675. container_of(w, struct drbd_peer_request, w);
  1676. struct drbd_peer_device *peer_device = peer_req->peer_device;
  1677. struct drbd_device *device = peer_device->device;
  1678. sector_t sector = peer_req->i.sector;
  1679. int err;
  1680. D_ASSERT(device, drbd_interval_empty(&peer_req->i));
  1681. if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
  1682. drbd_set_in_sync(device, sector, peer_req->i.size);
  1683. err = drbd_send_ack(peer_device, P_RS_WRITE_ACK, peer_req);
  1684. } else {
  1685. /* Record failure to sync */
  1686. drbd_rs_failed_io(device, sector, peer_req->i.size);
  1687. err = drbd_send_ack(peer_device, P_NEG_ACK, peer_req);
  1688. }
  1689. dec_unacked(device);
  1690. return err;
  1691. }
  1692. static int recv_resync_read(struct drbd_peer_device *peer_device, sector_t sector,
  1693. struct packet_info *pi) __releases(local)
  1694. {
  1695. struct drbd_device *device = peer_device->device;
  1696. struct drbd_peer_request *peer_req;
  1697. peer_req = read_in_block(peer_device, ID_SYNCER, sector, pi);
  1698. if (!peer_req)
  1699. goto fail;
  1700. dec_rs_pending(device);
  1701. inc_unacked(device);
  1702. /* corresponding dec_unacked() in e_end_resync_block()
  1703. * respective _drbd_clear_done_ee */
  1704. peer_req->w.cb = e_end_resync_block;
  1705. peer_req->submit_jif = jiffies;
  1706. spin_lock_irq(&device->resource->req_lock);
  1707. list_add_tail(&peer_req->w.list, &device->sync_ee);
  1708. spin_unlock_irq(&device->resource->req_lock);
  1709. atomic_add(pi->size >> 9, &device->rs_sect_ev);
  1710. if (drbd_submit_peer_request(device, peer_req, REQ_OP_WRITE, 0,
  1711. DRBD_FAULT_RS_WR) == 0)
  1712. return 0;
  1713. /* don't care for the reason here */
  1714. drbd_err(device, "submit failed, triggering re-connect\n");
  1715. spin_lock_irq(&device->resource->req_lock);
  1716. list_del(&peer_req->w.list);
  1717. spin_unlock_irq(&device->resource->req_lock);
  1718. drbd_free_peer_req(device, peer_req);
  1719. fail:
  1720. put_ldev(device);
  1721. return -EIO;
  1722. }
  1723. static struct drbd_request *
  1724. find_request(struct drbd_device *device, struct rb_root *root, u64 id,
  1725. sector_t sector, bool missing_ok, const char *func)
  1726. {
  1727. struct drbd_request *req;
  1728. /* Request object according to our peer */
  1729. req = (struct drbd_request *)(unsigned long)id;
  1730. if (drbd_contains_interval(root, sector, &req->i) && req->i.local)
  1731. return req;
  1732. if (!missing_ok) {
  1733. drbd_err(device, "%s: failed to find request 0x%lx, sector %llus\n", func,
  1734. (unsigned long)id, (unsigned long long)sector);
  1735. }
  1736. return NULL;
  1737. }
  1738. static int receive_DataReply(struct drbd_connection *connection, struct packet_info *pi)
  1739. {
  1740. struct drbd_peer_device *peer_device;
  1741. struct drbd_device *device;
  1742. struct drbd_request *req;
  1743. sector_t sector;
  1744. int err;
  1745. struct p_data *p = pi->data;
  1746. peer_device = conn_peer_device(connection, pi->vnr);
  1747. if (!peer_device)
  1748. return -EIO;
  1749. device = peer_device->device;
  1750. sector = be64_to_cpu(p->sector);
  1751. spin_lock_irq(&device->resource->req_lock);
  1752. req = find_request(device, &device->read_requests, p->block_id, sector, false, __func__);
  1753. spin_unlock_irq(&device->resource->req_lock);
  1754. if (unlikely(!req))
  1755. return -EIO;
  1756. /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
  1757. * special casing it there for the various failure cases.
  1758. * still no race with drbd_fail_pending_reads */
  1759. err = recv_dless_read(peer_device, req, sector, pi->size);
  1760. if (!err)
  1761. req_mod(req, DATA_RECEIVED);
  1762. /* else: nothing. handled from drbd_disconnect...
  1763. * I don't think we may complete this just yet
  1764. * in case we are "on-disconnect: freeze" */
  1765. return err;
  1766. }
  1767. static int receive_RSDataReply(struct drbd_connection *connection, struct packet_info *pi)
  1768. {
  1769. struct drbd_peer_device *peer_device;
  1770. struct drbd_device *device;
  1771. sector_t sector;
  1772. int err;
  1773. struct p_data *p = pi->data;
  1774. peer_device = conn_peer_device(connection, pi->vnr);
  1775. if (!peer_device)
  1776. return -EIO;
  1777. device = peer_device->device;
  1778. sector = be64_to_cpu(p->sector);
  1779. D_ASSERT(device, p->block_id == ID_SYNCER);
  1780. if (get_ldev(device)) {
  1781. /* data is submitted to disk within recv_resync_read.
  1782. * corresponding put_ldev done below on error,
  1783. * or in drbd_peer_request_endio. */
  1784. err = recv_resync_read(peer_device, sector, pi);
  1785. } else {
  1786. if (__ratelimit(&drbd_ratelimit_state))
  1787. drbd_err(device, "Can not write resync data to local disk.\n");
  1788. err = drbd_drain_block(peer_device, pi->size);
  1789. drbd_send_ack_dp(peer_device, P_NEG_ACK, p, pi->size);
  1790. }
  1791. atomic_add(pi->size >> 9, &device->rs_sect_in);
  1792. return err;
  1793. }
  1794. static void restart_conflicting_writes(struct drbd_device *device,
  1795. sector_t sector, int size)
  1796. {
  1797. struct drbd_interval *i;
  1798. struct drbd_request *req;
  1799. drbd_for_each_overlap(i, &device->write_requests, sector, size) {
  1800. if (!i->local)
  1801. continue;
  1802. req = container_of(i, struct drbd_request, i);
  1803. if (req->rq_state & RQ_LOCAL_PENDING ||
  1804. !(req->rq_state & RQ_POSTPONED))
  1805. continue;
  1806. /* as it is RQ_POSTPONED, this will cause it to
  1807. * be queued on the retry workqueue. */
  1808. __req_mod(req, CONFLICT_RESOLVED, NULL);
  1809. }
  1810. }
  1811. /*
  1812. * e_end_block() is called in ack_sender context via drbd_finish_peer_reqs().
  1813. */
  1814. static int e_end_block(struct drbd_work *w, int cancel)
  1815. {
  1816. struct drbd_peer_request *peer_req =
  1817. container_of(w, struct drbd_peer_request, w);
  1818. struct drbd_peer_device *peer_device = peer_req->peer_device;
  1819. struct drbd_device *device = peer_device->device;
  1820. sector_t sector = peer_req->i.sector;
  1821. int err = 0, pcmd;
  1822. if (peer_req->flags & EE_SEND_WRITE_ACK) {
  1823. if (likely((peer_req->flags & EE_WAS_ERROR) == 0)) {
  1824. pcmd = (device->state.conn >= C_SYNC_SOURCE &&
  1825. device->state.conn <= C_PAUSED_SYNC_T &&
  1826. peer_req->flags & EE_MAY_SET_IN_SYNC) ?
  1827. P_RS_WRITE_ACK : P_WRITE_ACK;
  1828. err = drbd_send_ack(peer_device, pcmd, peer_req);
  1829. if (pcmd == P_RS_WRITE_ACK)
  1830. drbd_set_in_sync(device, sector, peer_req->i.size);
  1831. } else {
  1832. err = drbd_send_ack(peer_device, P_NEG_ACK, peer_req);
  1833. /* we expect it to be marked out of sync anyways...
  1834. * maybe assert this? */
  1835. }
  1836. dec_unacked(device);
  1837. }
  1838. /* we delete from the conflict detection hash _after_ we sent out the
  1839. * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
  1840. if (peer_req->flags & EE_IN_INTERVAL_TREE) {
  1841. spin_lock_irq(&device->resource->req_lock);
  1842. D_ASSERT(device, !drbd_interval_empty(&peer_req->i));
  1843. drbd_remove_epoch_entry_interval(device, peer_req);
  1844. if (peer_req->flags & EE_RESTART_REQUESTS)
  1845. restart_conflicting_writes(device, sector, peer_req->i.size);
  1846. spin_unlock_irq(&device->resource->req_lock);
  1847. } else
  1848. D_ASSERT(device, drbd_interval_empty(&peer_req->i));
  1849. drbd_may_finish_epoch(peer_device->connection, peer_req->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
  1850. return err;
  1851. }
  1852. static int e_send_ack(struct drbd_work *w, enum drbd_packet ack)
  1853. {
  1854. struct drbd_peer_request *peer_req =
  1855. container_of(w, struct drbd_peer_request, w);
  1856. struct drbd_peer_device *peer_device = peer_req->peer_device;
  1857. int err;
  1858. err = drbd_send_ack(peer_device, ack, peer_req);
  1859. dec_unacked(peer_device->device);
  1860. return err;
  1861. }
  1862. static int e_send_superseded(struct drbd_work *w, int unused)
  1863. {
  1864. return e_send_ack(w, P_SUPERSEDED);
  1865. }
  1866. static int e_send_retry_write(struct drbd_work *w, int unused)
  1867. {
  1868. struct drbd_peer_request *peer_req =
  1869. container_of(w, struct drbd_peer_request, w);
  1870. struct drbd_connection *connection = peer_req->peer_device->connection;
  1871. return e_send_ack(w, connection->agreed_pro_version >= 100 ?
  1872. P_RETRY_WRITE : P_SUPERSEDED);
  1873. }
  1874. static bool seq_greater(u32 a, u32 b)
  1875. {
  1876. /*
  1877. * We assume 32-bit wrap-around here.
  1878. * For 24-bit wrap-around, we would have to shift:
  1879. * a <<= 8; b <<= 8;
  1880. */
  1881. return (s32)a - (s32)b > 0;
  1882. }
  1883. static u32 seq_max(u32 a, u32 b)
  1884. {
  1885. return seq_greater(a, b) ? a : b;
  1886. }
  1887. static void update_peer_seq(struct drbd_peer_device *peer_device, unsigned int peer_seq)
  1888. {
  1889. struct drbd_device *device = peer_device->device;
  1890. unsigned int newest_peer_seq;
  1891. if (test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)) {
  1892. spin_lock(&device->peer_seq_lock);
  1893. newest_peer_seq = seq_max(device->peer_seq, peer_seq);
  1894. device->peer_seq = newest_peer_seq;
  1895. spin_unlock(&device->peer_seq_lock);
  1896. /* wake up only if we actually changed device->peer_seq */
  1897. if (peer_seq == newest_peer_seq)
  1898. wake_up(&device->seq_wait);
  1899. }
  1900. }
  1901. static inline int overlaps(sector_t s1, int l1, sector_t s2, int l2)
  1902. {
  1903. return !((s1 + (l1>>9) <= s2) || (s1 >= s2 + (l2>>9)));
  1904. }
  1905. /* maybe change sync_ee into interval trees as well? */
  1906. static bool overlapping_resync_write(struct drbd_device *device, struct drbd_peer_request *peer_req)
  1907. {
  1908. struct drbd_peer_request *rs_req;
  1909. bool rv = false;
  1910. spin_lock_irq(&device->resource->req_lock);
  1911. list_for_each_entry(rs_req, &device->sync_ee, w.list) {
  1912. if (overlaps(peer_req->i.sector, peer_req->i.size,
  1913. rs_req->i.sector, rs_req->i.size)) {
  1914. rv = true;
  1915. break;
  1916. }
  1917. }
  1918. spin_unlock_irq(&device->resource->req_lock);
  1919. return rv;
  1920. }
  1921. /* Called from receive_Data.
  1922. * Synchronize packets on sock with packets on msock.
  1923. *
  1924. * This is here so even when a P_DATA packet traveling via sock overtook an Ack
  1925. * packet traveling on msock, they are still processed in the order they have
  1926. * been sent.
  1927. *
  1928. * Note: we don't care for Ack packets overtaking P_DATA packets.
  1929. *
  1930. * In case packet_seq is larger than device->peer_seq number, there are
  1931. * outstanding packets on the msock. We wait for them to arrive.
  1932. * In case we are the logically next packet, we update device->peer_seq
  1933. * ourselves. Correctly handles 32bit wrap around.
  1934. *
  1935. * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
  1936. * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
  1937. * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
  1938. * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
  1939. *
  1940. * returns 0 if we may process the packet,
  1941. * -ERESTARTSYS if we were interrupted (by disconnect signal). */
  1942. static int wait_for_and_update_peer_seq(struct drbd_peer_device *peer_device, const u32 peer_seq)
  1943. {
  1944. struct drbd_device *device = peer_device->device;
  1945. DEFINE_WAIT(wait);
  1946. long timeout;
  1947. int ret = 0, tp;
  1948. if (!test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags))
  1949. return 0;
  1950. spin_lock(&device->peer_seq_lock);
  1951. for (;;) {
  1952. if (!seq_greater(peer_seq - 1, device->peer_seq)) {
  1953. device->peer_seq = seq_max(device->peer_seq, peer_seq);
  1954. break;
  1955. }
  1956. if (signal_pending(current)) {
  1957. ret = -ERESTARTSYS;
  1958. break;
  1959. }
  1960. rcu_read_lock();
  1961. tp = rcu_dereference(peer_device->connection->net_conf)->two_primaries;
  1962. rcu_read_unlock();
  1963. if (!tp)
  1964. break;
  1965. /* Only need to wait if two_primaries is enabled */
  1966. prepare_to_wait(&device->seq_wait, &wait, TASK_INTERRUPTIBLE);
  1967. spin_unlock(&device->peer_seq_lock);
  1968. rcu_read_lock();
  1969. timeout = rcu_dereference(peer_device->connection->net_conf)->ping_timeo*HZ/10;
  1970. rcu_read_unlock();
  1971. timeout = schedule_timeout(timeout);
  1972. spin_lock(&device->peer_seq_lock);
  1973. if (!timeout) {
  1974. ret = -ETIMEDOUT;
  1975. drbd_err(device, "Timed out waiting for missing ack packets; disconnecting\n");
  1976. break;
  1977. }
  1978. }
  1979. spin_unlock(&device->peer_seq_lock);
  1980. finish_wait(&device->seq_wait, &wait);
  1981. return ret;
  1982. }
  1983. /* see also bio_flags_to_wire()
  1984. * DRBD_REQ_*, because we need to semantically map the flags to data packet
  1985. * flags and back. We may replicate to other kernel versions. */
  1986. static unsigned long wire_flags_to_bio_flags(u32 dpf)
  1987. {
  1988. return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
  1989. (dpf & DP_FUA ? REQ_FUA : 0) |
  1990. (dpf & DP_FLUSH ? REQ_PREFLUSH : 0);
  1991. }
  1992. static unsigned long wire_flags_to_bio_op(u32 dpf)
  1993. {
  1994. if (dpf & DP_DISCARD)
  1995. return REQ_OP_WRITE_ZEROES;
  1996. else
  1997. return REQ_OP_WRITE;
  1998. }
  1999. static void fail_postponed_requests(struct drbd_device *device, sector_t sector,
  2000. unsigned int size)
  2001. {
  2002. struct drbd_interval *i;
  2003. repeat:
  2004. drbd_for_each_overlap(i, &device->write_requests, sector, size) {
  2005. struct drbd_request *req;
  2006. struct bio_and_error m;
  2007. if (!i->local)
  2008. continue;
  2009. req = container_of(i, struct drbd_request, i);
  2010. if (!(req->rq_state & RQ_POSTPONED))
  2011. continue;
  2012. req->rq_state &= ~RQ_POSTPONED;
  2013. __req_mod(req, NEG_ACKED, &m);
  2014. spin_unlock_irq(&device->resource->req_lock);
  2015. if (m.bio)
  2016. complete_master_bio(device, &m);
  2017. spin_lock_irq(&device->resource->req_lock);
  2018. goto repeat;
  2019. }
  2020. }
  2021. static int handle_write_conflicts(struct drbd_device *device,
  2022. struct drbd_peer_request *peer_req)
  2023. {
  2024. struct drbd_connection *connection = peer_req->peer_device->connection;
  2025. bool resolve_conflicts = test_bit(RESOLVE_CONFLICTS, &connection->flags);
  2026. sector_t sector = peer_req->i.sector;
  2027. const unsigned int size = peer_req->i.size;
  2028. struct drbd_interval *i;
  2029. bool equal;
  2030. int err;
  2031. /*
  2032. * Inserting the peer request into the write_requests tree will prevent
  2033. * new conflicting local requests from being added.
  2034. */
  2035. drbd_insert_interval(&device->write_requests, &peer_req->i);
  2036. repeat:
  2037. drbd_for_each_overlap(i, &device->write_requests, sector, size) {
  2038. if (i == &peer_req->i)
  2039. continue;
  2040. if (i->completed)
  2041. continue;
  2042. if (!i->local) {
  2043. /*
  2044. * Our peer has sent a conflicting remote request; this
  2045. * should not happen in a two-node setup. Wait for the
  2046. * earlier peer request to complete.
  2047. */
  2048. err = drbd_wait_misc(device, i);
  2049. if (err)
  2050. goto out;
  2051. goto repeat;
  2052. }
  2053. equal = i->sector == sector && i->size == size;
  2054. if (resolve_conflicts) {
  2055. /*
  2056. * If the peer request is fully contained within the
  2057. * overlapping request, it can be considered overwritten
  2058. * and thus superseded; otherwise, it will be retried
  2059. * once all overlapping requests have completed.
  2060. */
  2061. bool superseded = i->sector <= sector && i->sector +
  2062. (i->size >> 9) >= sector + (size >> 9);
  2063. if (!equal)
  2064. drbd_alert(device, "Concurrent writes detected: "
  2065. "local=%llus +%u, remote=%llus +%u, "
  2066. "assuming %s came first\n",
  2067. (unsigned long long)i->sector, i->size,
  2068. (unsigned long long)sector, size,
  2069. superseded ? "local" : "remote");
  2070. peer_req->w.cb = superseded ? e_send_superseded :
  2071. e_send_retry_write;
  2072. list_add_tail(&peer_req->w.list, &device->done_ee);
  2073. queue_work(connection->ack_sender, &peer_req->peer_device->send_acks_work);
  2074. err = -ENOENT;
  2075. goto out;
  2076. } else {
  2077. struct drbd_request *req =
  2078. container_of(i, struct drbd_request, i);
  2079. if (!equal)
  2080. drbd_alert(device, "Concurrent writes detected: "
  2081. "local=%llus +%u, remote=%llus +%u\n",
  2082. (unsigned long long)i->sector, i->size,
  2083. (unsigned long long)sector, size);
  2084. if (req->rq_state & RQ_LOCAL_PENDING ||
  2085. !(req->rq_state & RQ_POSTPONED)) {
  2086. /*
  2087. * Wait for the node with the discard flag to
  2088. * decide if this request has been superseded
  2089. * or needs to be retried.
  2090. * Requests that have been superseded will
  2091. * disappear from the write_requests tree.
  2092. *
  2093. * In addition, wait for the conflicting
  2094. * request to finish locally before submitting
  2095. * the conflicting peer request.
  2096. */
  2097. err = drbd_wait_misc(device, &req->i);
  2098. if (err) {
  2099. _conn_request_state(connection, NS(conn, C_TIMEOUT), CS_HARD);
  2100. fail_postponed_requests(device, sector, size);
  2101. goto out;
  2102. }
  2103. goto repeat;
  2104. }
  2105. /*
  2106. * Remember to restart the conflicting requests after
  2107. * the new peer request has completed.
  2108. */
  2109. peer_req->flags |= EE_RESTART_REQUESTS;
  2110. }
  2111. }
  2112. err = 0;
  2113. out:
  2114. if (err)
  2115. drbd_remove_epoch_entry_interval(device, peer_req);
  2116. return err;
  2117. }
  2118. /* mirrored write */
  2119. static int receive_Data(struct drbd_connection *connection, struct packet_info *pi)
  2120. {
  2121. struct drbd_peer_device *peer_device;
  2122. struct drbd_device *device;
  2123. struct net_conf *nc;
  2124. sector_t sector;
  2125. struct drbd_peer_request *peer_req;
  2126. struct p_data *p = pi->data;
  2127. u32 peer_seq = be32_to_cpu(p->seq_num);
  2128. int op, op_flags;
  2129. u32 dp_flags;
  2130. int err, tp;
  2131. peer_device = conn_peer_device(connection, pi->vnr);
  2132. if (!peer_device)
  2133. return -EIO;
  2134. device = peer_device->device;
  2135. if (!get_ldev(device)) {
  2136. int err2;
  2137. err = wait_for_and_update_peer_seq(peer_device, peer_seq);
  2138. drbd_send_ack_dp(peer_device, P_NEG_ACK, p, pi->size);
  2139. atomic_inc(&connection->current_epoch->epoch_size);
  2140. err2 = drbd_drain_block(peer_device, pi->size);
  2141. if (!err)
  2142. err = err2;
  2143. return err;
  2144. }
  2145. /*
  2146. * Corresponding put_ldev done either below (on various errors), or in
  2147. * drbd_peer_request_endio, if we successfully submit the data at the
  2148. * end of this function.
  2149. */
  2150. sector = be64_to_cpu(p->sector);
  2151. peer_req = read_in_block(peer_device, p->block_id, sector, pi);
  2152. if (!peer_req) {
  2153. put_ldev(device);
  2154. return -EIO;
  2155. }
  2156. peer_req->w.cb = e_end_block;
  2157. peer_req->submit_jif = jiffies;
  2158. peer_req->flags |= EE_APPLICATION;
  2159. dp_flags = be32_to_cpu(p->dp_flags);
  2160. op = wire_flags_to_bio_op(dp_flags);
  2161. op_flags = wire_flags_to_bio_flags(dp_flags);
  2162. if (pi->cmd == P_TRIM) {
  2163. D_ASSERT(peer_device, peer_req->i.size > 0);
  2164. D_ASSERT(peer_device, op == REQ_OP_WRITE_ZEROES);
  2165. D_ASSERT(peer_device, peer_req->pages == NULL);
  2166. } else if (peer_req->pages == NULL) {
  2167. D_ASSERT(device, peer_req->i.size == 0);
  2168. D_ASSERT(device, dp_flags & DP_FLUSH);
  2169. }
  2170. if (dp_flags & DP_MAY_SET_IN_SYNC)
  2171. peer_req->flags |= EE_MAY_SET_IN_SYNC;
  2172. spin_lock(&connection->epoch_lock);
  2173. peer_req->epoch = connection->current_epoch;
  2174. atomic_inc(&peer_req->epoch->epoch_size);
  2175. atomic_inc(&peer_req->epoch->active);
  2176. spin_unlock(&connection->epoch_lock);
  2177. rcu_read_lock();
  2178. nc = rcu_dereference(peer_device->connection->net_conf);
  2179. tp = nc->two_primaries;
  2180. if (peer_device->connection->agreed_pro_version < 100) {
  2181. switch (nc->wire_protocol) {
  2182. case DRBD_PROT_C:
  2183. dp_flags |= DP_SEND_WRITE_ACK;
  2184. break;
  2185. case DRBD_PROT_B:
  2186. dp_flags |= DP_SEND_RECEIVE_ACK;
  2187. break;
  2188. }
  2189. }
  2190. rcu_read_unlock();
  2191. if (dp_flags & DP_SEND_WRITE_ACK) {
  2192. peer_req->flags |= EE_SEND_WRITE_ACK;
  2193. inc_unacked(device);
  2194. /* corresponding dec_unacked() in e_end_block()
  2195. * respective _drbd_clear_done_ee */
  2196. }
  2197. if (dp_flags & DP_SEND_RECEIVE_ACK) {
  2198. /* I really don't like it that the receiver thread
  2199. * sends on the msock, but anyways */
  2200. drbd_send_ack(peer_device, P_RECV_ACK, peer_req);
  2201. }
  2202. if (tp) {
  2203. /* two primaries implies protocol C */
  2204. D_ASSERT(device, dp_flags & DP_SEND_WRITE_ACK);
  2205. peer_req->flags |= EE_IN_INTERVAL_TREE;
  2206. err = wait_for_and_update_peer_seq(peer_device, peer_seq);
  2207. if (err)
  2208. goto out_interrupted;
  2209. spin_lock_irq(&device->resource->req_lock);
  2210. err = handle_write_conflicts(device, peer_req);
  2211. if (err) {
  2212. spin_unlock_irq(&device->resource->req_lock);
  2213. if (err == -ENOENT) {
  2214. put_ldev(device);
  2215. return 0;
  2216. }
  2217. goto out_interrupted;
  2218. }
  2219. } else {
  2220. update_peer_seq(peer_device, peer_seq);
  2221. spin_lock_irq(&device->resource->req_lock);
  2222. }
  2223. /* TRIM and WRITE_SAME are processed synchronously,
  2224. * we wait for all pending requests, respectively wait for
  2225. * active_ee to become empty in drbd_submit_peer_request();
  2226. * better not add ourselves here. */
  2227. if ((peer_req->flags & (EE_IS_TRIM|EE_WRITE_SAME)) == 0)
  2228. list_add_tail(&peer_req->w.list, &device->active_ee);
  2229. spin_unlock_irq(&device->resource->req_lock);
  2230. if (device->state.conn == C_SYNC_TARGET)
  2231. wait_event(device->ee_wait, !overlapping_resync_write(device, peer_req));
  2232. if (device->state.pdsk < D_INCONSISTENT) {
  2233. /* In case we have the only disk of the cluster, */
  2234. drbd_set_out_of_sync(device, peer_req->i.sector, peer_req->i.size);
  2235. peer_req->flags &= ~EE_MAY_SET_IN_SYNC;
  2236. drbd_al_begin_io(device, &peer_req->i);
  2237. peer_req->flags |= EE_CALL_AL_COMPLETE_IO;
  2238. }
  2239. err = drbd_submit_peer_request(device, peer_req, op, op_flags,
  2240. DRBD_FAULT_DT_WR);
  2241. if (!err)
  2242. return 0;
  2243. /* don't care for the reason here */
  2244. drbd_err(device, "submit failed, triggering re-connect\n");
  2245. spin_lock_irq(&device->resource->req_lock);
  2246. list_del(&peer_req->w.list);
  2247. drbd_remove_epoch_entry_interval(device, peer_req);
  2248. spin_unlock_irq(&device->resource->req_lock);
  2249. if (peer_req->flags & EE_CALL_AL_COMPLETE_IO) {
  2250. peer_req->flags &= ~EE_CALL_AL_COMPLETE_IO;
  2251. drbd_al_complete_io(device, &peer_req->i);
  2252. }
  2253. out_interrupted:
  2254. drbd_may_finish_epoch(connection, peer_req->epoch, EV_PUT | EV_CLEANUP);
  2255. put_ldev(device);
  2256. drbd_free_peer_req(device, peer_req);
  2257. return err;
  2258. }
  2259. /* We may throttle resync, if the lower device seems to be busy,
  2260. * and current sync rate is above c_min_rate.
  2261. *
  2262. * To decide whether or not the lower device is busy, we use a scheme similar
  2263. * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
  2264. * (more than 64 sectors) of activity we cannot account for with our own resync
  2265. * activity, it obviously is "busy".
  2266. *
  2267. * The current sync rate used here uses only the most recent two step marks,
  2268. * to have a short time average so we can react faster.
  2269. */
  2270. bool drbd_rs_should_slow_down(struct drbd_device *device, sector_t sector,
  2271. bool throttle_if_app_is_waiting)
  2272. {
  2273. struct lc_element *tmp;
  2274. bool throttle = drbd_rs_c_min_rate_throttle(device);
  2275. if (!throttle || throttle_if_app_is_waiting)
  2276. return throttle;
  2277. spin_lock_irq(&device->al_lock);
  2278. tmp = lc_find(device->resync, BM_SECT_TO_EXT(sector));
  2279. if (tmp) {
  2280. struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
  2281. if (test_bit(BME_PRIORITY, &bm_ext->flags))
  2282. throttle = false;
  2283. /* Do not slow down if app IO is already waiting for this extent,
  2284. * and our progress is necessary for application IO to complete. */
  2285. }
  2286. spin_unlock_irq(&device->al_lock);
  2287. return throttle;
  2288. }
  2289. bool drbd_rs_c_min_rate_throttle(struct drbd_device *device)
  2290. {
  2291. struct gendisk *disk = device->ldev->backing_bdev->bd_contains->bd_disk;
  2292. unsigned long db, dt, dbdt;
  2293. unsigned int c_min_rate;
  2294. int curr_events;
  2295. rcu_read_lock();
  2296. c_min_rate = rcu_dereference(device->ldev->disk_conf)->c_min_rate;
  2297. rcu_read_unlock();
  2298. /* feature disabled? */
  2299. if (c_min_rate == 0)
  2300. return false;
  2301. curr_events = (int)part_stat_read_accum(&disk->part0, sectors) -
  2302. atomic_read(&device->rs_sect_ev);
  2303. if (atomic_read(&device->ap_actlog_cnt)
  2304. || curr_events - device->rs_last_events > 64) {
  2305. unsigned long rs_left;
  2306. int i;
  2307. device->rs_last_events = curr_events;
  2308. /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
  2309. * approx. */
  2310. i = (device->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
  2311. if (device->state.conn == C_VERIFY_S || device->state.conn == C_VERIFY_T)
  2312. rs_left = device->ov_left;
  2313. else
  2314. rs_left = drbd_bm_total_weight(device) - device->rs_failed;
  2315. dt = ((long)jiffies - (long)device->rs_mark_time[i]) / HZ;
  2316. if (!dt)
  2317. dt++;
  2318. db = device->rs_mark_left[i] - rs_left;
  2319. dbdt = Bit2KB(db/dt);
  2320. if (dbdt > c_min_rate)
  2321. return true;
  2322. }
  2323. return false;
  2324. }
  2325. static int receive_DataRequest(struct drbd_connection *connection, struct packet_info *pi)
  2326. {
  2327. struct drbd_peer_device *peer_device;
  2328. struct drbd_device *device;
  2329. sector_t sector;
  2330. sector_t capacity;
  2331. struct drbd_peer_request *peer_req;
  2332. struct digest_info *di = NULL;
  2333. int size, verb;
  2334. unsigned int fault_type;
  2335. struct p_block_req *p = pi->data;
  2336. peer_device = conn_peer_device(connection, pi->vnr);
  2337. if (!peer_device)
  2338. return -EIO;
  2339. device = peer_device->device;
  2340. capacity = drbd_get_capacity(device->this_bdev);
  2341. sector = be64_to_cpu(p->sector);
  2342. size = be32_to_cpu(p->blksize);
  2343. if (size <= 0 || !IS_ALIGNED(size, 512) || size > DRBD_MAX_BIO_SIZE) {
  2344. drbd_err(device, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
  2345. (unsigned long long)sector, size);
  2346. return -EINVAL;
  2347. }
  2348. if (sector + (size>>9) > capacity) {
  2349. drbd_err(device, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
  2350. (unsigned long long)sector, size);
  2351. return -EINVAL;
  2352. }
  2353. if (!get_ldev_if_state(device, D_UP_TO_DATE)) {
  2354. verb = 1;
  2355. switch (pi->cmd) {
  2356. case P_DATA_REQUEST:
  2357. drbd_send_ack_rp(peer_device, P_NEG_DREPLY, p);
  2358. break;
  2359. case P_RS_THIN_REQ:
  2360. case P_RS_DATA_REQUEST:
  2361. case P_CSUM_RS_REQUEST:
  2362. case P_OV_REQUEST:
  2363. drbd_send_ack_rp(peer_device, P_NEG_RS_DREPLY , p);
  2364. break;
  2365. case P_OV_REPLY:
  2366. verb = 0;
  2367. dec_rs_pending(device);
  2368. drbd_send_ack_ex(peer_device, P_OV_RESULT, sector, size, ID_IN_SYNC);
  2369. break;
  2370. default:
  2371. BUG();
  2372. }
  2373. if (verb && __ratelimit(&drbd_ratelimit_state))
  2374. drbd_err(device, "Can not satisfy peer's read request, "
  2375. "no local data.\n");
  2376. /* drain possibly payload */
  2377. return drbd_drain_block(peer_device, pi->size);
  2378. }
  2379. /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
  2380. * "criss-cross" setup, that might cause write-out on some other DRBD,
  2381. * which in turn might block on the other node at this very place. */
  2382. peer_req = drbd_alloc_peer_req(peer_device, p->block_id, sector, size,
  2383. size, GFP_NOIO);
  2384. if (!peer_req) {
  2385. put_ldev(device);
  2386. return -ENOMEM;
  2387. }
  2388. switch (pi->cmd) {
  2389. case P_DATA_REQUEST:
  2390. peer_req->w.cb = w_e_end_data_req;
  2391. fault_type = DRBD_FAULT_DT_RD;
  2392. /* application IO, don't drbd_rs_begin_io */
  2393. peer_req->flags |= EE_APPLICATION;
  2394. goto submit;
  2395. case P_RS_THIN_REQ:
  2396. /* If at some point in the future we have a smart way to
  2397. find out if this data block is completely deallocated,
  2398. then we would do something smarter here than reading
  2399. the block... */
  2400. peer_req->flags |= EE_RS_THIN_REQ;
  2401. /* fall through */
  2402. case P_RS_DATA_REQUEST:
  2403. peer_req->w.cb = w_e_end_rsdata_req;
  2404. fault_type = DRBD_FAULT_RS_RD;
  2405. /* used in the sector offset progress display */
  2406. device->bm_resync_fo = BM_SECT_TO_BIT(sector);
  2407. break;
  2408. case P_OV_REPLY:
  2409. case P_CSUM_RS_REQUEST:
  2410. fault_type = DRBD_FAULT_RS_RD;
  2411. di = kmalloc(sizeof(*di) + pi->size, GFP_NOIO);
  2412. if (!di)
  2413. goto out_free_e;
  2414. di->digest_size = pi->size;
  2415. di->digest = (((char *)di)+sizeof(struct digest_info));
  2416. peer_req->digest = di;
  2417. peer_req->flags |= EE_HAS_DIGEST;
  2418. if (drbd_recv_all(peer_device->connection, di->digest, pi->size))
  2419. goto out_free_e;
  2420. if (pi->cmd == P_CSUM_RS_REQUEST) {
  2421. D_ASSERT(device, peer_device->connection->agreed_pro_version >= 89);
  2422. peer_req->w.cb = w_e_end_csum_rs_req;
  2423. /* used in the sector offset progress display */
  2424. device->bm_resync_fo = BM_SECT_TO_BIT(sector);
  2425. /* remember to report stats in drbd_resync_finished */
  2426. device->use_csums = true;
  2427. } else if (pi->cmd == P_OV_REPLY) {
  2428. /* track progress, we may need to throttle */
  2429. atomic_add(size >> 9, &device->rs_sect_in);
  2430. peer_req->w.cb = w_e_end_ov_reply;
  2431. dec_rs_pending(device);
  2432. /* drbd_rs_begin_io done when we sent this request,
  2433. * but accounting still needs to be done. */
  2434. goto submit_for_resync;
  2435. }
  2436. break;
  2437. case P_OV_REQUEST:
  2438. if (device->ov_start_sector == ~(sector_t)0 &&
  2439. peer_device->connection->agreed_pro_version >= 90) {
  2440. unsigned long now = jiffies;
  2441. int i;
  2442. device->ov_start_sector = sector;
  2443. device->ov_position = sector;
  2444. device->ov_left = drbd_bm_bits(device) - BM_SECT_TO_BIT(sector);
  2445. device->rs_total = device->ov_left;
  2446. for (i = 0; i < DRBD_SYNC_MARKS; i++) {
  2447. device->rs_mark_left[i] = device->ov_left;
  2448. device->rs_mark_time[i] = now;
  2449. }
  2450. drbd_info(device, "Online Verify start sector: %llu\n",
  2451. (unsigned long long)sector);
  2452. }
  2453. peer_req->w.cb = w_e_end_ov_req;
  2454. fault_type = DRBD_FAULT_RS_RD;
  2455. break;
  2456. default:
  2457. BUG();
  2458. }
  2459. /* Throttle, drbd_rs_begin_io and submit should become asynchronous
  2460. * wrt the receiver, but it is not as straightforward as it may seem.
  2461. * Various places in the resync start and stop logic assume resync
  2462. * requests are processed in order, requeuing this on the worker thread
  2463. * introduces a bunch of new code for synchronization between threads.
  2464. *
  2465. * Unlimited throttling before drbd_rs_begin_io may stall the resync
  2466. * "forever", throttling after drbd_rs_begin_io will lock that extent
  2467. * for application writes for the same time. For now, just throttle
  2468. * here, where the rest of the code expects the receiver to sleep for
  2469. * a while, anyways.
  2470. */
  2471. /* Throttle before drbd_rs_begin_io, as that locks out application IO;
  2472. * this defers syncer requests for some time, before letting at least
  2473. * on request through. The resync controller on the receiving side
  2474. * will adapt to the incoming rate accordingly.
  2475. *
  2476. * We cannot throttle here if remote is Primary/SyncTarget:
  2477. * we would also throttle its application reads.
  2478. * In that case, throttling is done on the SyncTarget only.
  2479. */
  2480. /* Even though this may be a resync request, we do add to "read_ee";
  2481. * "sync_ee" is only used for resync WRITEs.
  2482. * Add to list early, so debugfs can find this request
  2483. * even if we have to sleep below. */
  2484. spin_lock_irq(&device->resource->req_lock);
  2485. list_add_tail(&peer_req->w.list, &device->read_ee);
  2486. spin_unlock_irq(&device->resource->req_lock);
  2487. update_receiver_timing_details(connection, drbd_rs_should_slow_down);
  2488. if (device->state.peer != R_PRIMARY
  2489. && drbd_rs_should_slow_down(device, sector, false))
  2490. schedule_timeout_uninterruptible(HZ/10);
  2491. update_receiver_timing_details(connection, drbd_rs_begin_io);
  2492. if (drbd_rs_begin_io(device, sector))
  2493. goto out_free_e;
  2494. submit_for_resync:
  2495. atomic_add(size >> 9, &device->rs_sect_ev);
  2496. submit:
  2497. update_receiver_timing_details(connection, drbd_submit_peer_request);
  2498. inc_unacked(device);
  2499. if (drbd_submit_peer_request(device, peer_req, REQ_OP_READ, 0,
  2500. fault_type) == 0)
  2501. return 0;
  2502. /* don't care for the reason here */
  2503. drbd_err(device, "submit failed, triggering re-connect\n");
  2504. out_free_e:
  2505. spin_lock_irq(&device->resource->req_lock);
  2506. list_del(&peer_req->w.list);
  2507. spin_unlock_irq(&device->resource->req_lock);
  2508. /* no drbd_rs_complete_io(), we are dropping the connection anyways */
  2509. put_ldev(device);
  2510. drbd_free_peer_req(device, peer_req);
  2511. return -EIO;
  2512. }
  2513. /**
  2514. * drbd_asb_recover_0p - Recover after split-brain with no remaining primaries
  2515. */
  2516. static int drbd_asb_recover_0p(struct drbd_peer_device *peer_device) __must_hold(local)
  2517. {
  2518. struct drbd_device *device = peer_device->device;
  2519. int self, peer, rv = -100;
  2520. unsigned long ch_self, ch_peer;
  2521. enum drbd_after_sb_p after_sb_0p;
  2522. self = device->ldev->md.uuid[UI_BITMAP] & 1;
  2523. peer = device->p_uuid[UI_BITMAP] & 1;
  2524. ch_peer = device->p_uuid[UI_SIZE];
  2525. ch_self = device->comm_bm_set;
  2526. rcu_read_lock();
  2527. after_sb_0p = rcu_dereference(peer_device->connection->net_conf)->after_sb_0p;
  2528. rcu_read_unlock();
  2529. switch (after_sb_0p) {
  2530. case ASB_CONSENSUS:
  2531. case ASB_DISCARD_SECONDARY:
  2532. case ASB_CALL_HELPER:
  2533. case ASB_VIOLENTLY:
  2534. drbd_err(device, "Configuration error.\n");
  2535. break;
  2536. case ASB_DISCONNECT:
  2537. break;
  2538. case ASB_DISCARD_YOUNGER_PRI:
  2539. if (self == 0 && peer == 1) {
  2540. rv = -1;
  2541. break;
  2542. }
  2543. if (self == 1 && peer == 0) {
  2544. rv = 1;
  2545. break;
  2546. }
  2547. /* Else fall through to one of the other strategies... */
  2548. case ASB_DISCARD_OLDER_PRI:
  2549. if (self == 0 && peer == 1) {
  2550. rv = 1;
  2551. break;
  2552. }
  2553. if (self == 1 && peer == 0) {
  2554. rv = -1;
  2555. break;
  2556. }
  2557. /* Else fall through to one of the other strategies... */
  2558. drbd_warn(device, "Discard younger/older primary did not find a decision\n"
  2559. "Using discard-least-changes instead\n");
  2560. /* fall through */
  2561. case ASB_DISCARD_ZERO_CHG:
  2562. if (ch_peer == 0 && ch_self == 0) {
  2563. rv = test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)
  2564. ? -1 : 1;
  2565. break;
  2566. } else {
  2567. if (ch_peer == 0) { rv = 1; break; }
  2568. if (ch_self == 0) { rv = -1; break; }
  2569. }
  2570. if (after_sb_0p == ASB_DISCARD_ZERO_CHG)
  2571. break;
  2572. /* else: fall through */
  2573. case ASB_DISCARD_LEAST_CHG:
  2574. if (ch_self < ch_peer)
  2575. rv = -1;
  2576. else if (ch_self > ch_peer)
  2577. rv = 1;
  2578. else /* ( ch_self == ch_peer ) */
  2579. /* Well, then use something else. */
  2580. rv = test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags)
  2581. ? -1 : 1;
  2582. break;
  2583. case ASB_DISCARD_LOCAL:
  2584. rv = -1;
  2585. break;
  2586. case ASB_DISCARD_REMOTE:
  2587. rv = 1;
  2588. }
  2589. return rv;
  2590. }
  2591. /**
  2592. * drbd_asb_recover_1p - Recover after split-brain with one remaining primary
  2593. */
  2594. static int drbd_asb_recover_1p(struct drbd_peer_device *peer_device) __must_hold(local)
  2595. {
  2596. struct drbd_device *device = peer_device->device;
  2597. int hg, rv = -100;
  2598. enum drbd_after_sb_p after_sb_1p;
  2599. rcu_read_lock();
  2600. after_sb_1p = rcu_dereference(peer_device->connection->net_conf)->after_sb_1p;
  2601. rcu_read_unlock();
  2602. switch (after_sb_1p) {
  2603. case ASB_DISCARD_YOUNGER_PRI:
  2604. case ASB_DISCARD_OLDER_PRI:
  2605. case ASB_DISCARD_LEAST_CHG:
  2606. case ASB_DISCARD_LOCAL:
  2607. case ASB_DISCARD_REMOTE:
  2608. case ASB_DISCARD_ZERO_CHG:
  2609. drbd_err(device, "Configuration error.\n");
  2610. break;
  2611. case ASB_DISCONNECT:
  2612. break;
  2613. case ASB_CONSENSUS:
  2614. hg = drbd_asb_recover_0p(peer_device);
  2615. if (hg == -1 && device->state.role == R_SECONDARY)
  2616. rv = hg;
  2617. if (hg == 1 && device->state.role == R_PRIMARY)
  2618. rv = hg;
  2619. break;
  2620. case ASB_VIOLENTLY:
  2621. rv = drbd_asb_recover_0p(peer_device);
  2622. break;
  2623. case ASB_DISCARD_SECONDARY:
  2624. return device->state.role == R_PRIMARY ? 1 : -1;
  2625. case ASB_CALL_HELPER:
  2626. hg = drbd_asb_recover_0p(peer_device);
  2627. if (hg == -1 && device->state.role == R_PRIMARY) {
  2628. enum drbd_state_rv rv2;
  2629. /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
  2630. * we might be here in C_WF_REPORT_PARAMS which is transient.
  2631. * we do not need to wait for the after state change work either. */
  2632. rv2 = drbd_change_state(device, CS_VERBOSE, NS(role, R_SECONDARY));
  2633. if (rv2 != SS_SUCCESS) {
  2634. drbd_khelper(device, "pri-lost-after-sb");
  2635. } else {
  2636. drbd_warn(device, "Successfully gave up primary role.\n");
  2637. rv = hg;
  2638. }
  2639. } else
  2640. rv = hg;
  2641. }
  2642. return rv;
  2643. }
  2644. /**
  2645. * drbd_asb_recover_2p - Recover after split-brain with two remaining primaries
  2646. */
  2647. static int drbd_asb_recover_2p(struct drbd_peer_device *peer_device) __must_hold(local)
  2648. {
  2649. struct drbd_device *device = peer_device->device;
  2650. int hg, rv = -100;
  2651. enum drbd_after_sb_p after_sb_2p;
  2652. rcu_read_lock();
  2653. after_sb_2p = rcu_dereference(peer_device->connection->net_conf)->after_sb_2p;
  2654. rcu_read_unlock();
  2655. switch (after_sb_2p) {
  2656. case ASB_DISCARD_YOUNGER_PRI:
  2657. case ASB_DISCARD_OLDER_PRI:
  2658. case ASB_DISCARD_LEAST_CHG:
  2659. case ASB_DISCARD_LOCAL:
  2660. case ASB_DISCARD_REMOTE:
  2661. case ASB_CONSENSUS:
  2662. case ASB_DISCARD_SECONDARY:
  2663. case ASB_DISCARD_ZERO_CHG:
  2664. drbd_err(device, "Configuration error.\n");
  2665. break;
  2666. case ASB_VIOLENTLY:
  2667. rv = drbd_asb_recover_0p(peer_device);
  2668. break;
  2669. case ASB_DISCONNECT:
  2670. break;
  2671. case ASB_CALL_HELPER:
  2672. hg = drbd_asb_recover_0p(peer_device);
  2673. if (hg == -1) {
  2674. enum drbd_state_rv rv2;
  2675. /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
  2676. * we might be here in C_WF_REPORT_PARAMS which is transient.
  2677. * we do not need to wait for the after state change work either. */
  2678. rv2 = drbd_change_state(device, CS_VERBOSE, NS(role, R_SECONDARY));
  2679. if (rv2 != SS_SUCCESS) {
  2680. drbd_khelper(device, "pri-lost-after-sb");
  2681. } else {
  2682. drbd_warn(device, "Successfully gave up primary role.\n");
  2683. rv = hg;
  2684. }
  2685. } else
  2686. rv = hg;
  2687. }
  2688. return rv;
  2689. }
  2690. static void drbd_uuid_dump(struct drbd_device *device, char *text, u64 *uuid,
  2691. u64 bits, u64 flags)
  2692. {
  2693. if (!uuid) {
  2694. drbd_info(device, "%s uuid info vanished while I was looking!\n", text);
  2695. return;
  2696. }
  2697. drbd_info(device, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
  2698. text,
  2699. (unsigned long long)uuid[UI_CURRENT],
  2700. (unsigned long long)uuid[UI_BITMAP],
  2701. (unsigned long long)uuid[UI_HISTORY_START],
  2702. (unsigned long long)uuid[UI_HISTORY_END],
  2703. (unsigned long long)bits,
  2704. (unsigned long long)flags);
  2705. }
  2706. /*
  2707. 100 after split brain try auto recover
  2708. 2 C_SYNC_SOURCE set BitMap
  2709. 1 C_SYNC_SOURCE use BitMap
  2710. 0 no Sync
  2711. -1 C_SYNC_TARGET use BitMap
  2712. -2 C_SYNC_TARGET set BitMap
  2713. -100 after split brain, disconnect
  2714. -1000 unrelated data
  2715. -1091 requires proto 91
  2716. -1096 requires proto 96
  2717. */
  2718. static int drbd_uuid_compare(struct drbd_device *const device, enum drbd_role const peer_role, int *rule_nr) __must_hold(local)
  2719. {
  2720. struct drbd_peer_device *const peer_device = first_peer_device(device);
  2721. struct drbd_connection *const connection = peer_device ? peer_device->connection : NULL;
  2722. u64 self, peer;
  2723. int i, j;
  2724. self = device->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
  2725. peer = device->p_uuid[UI_CURRENT] & ~((u64)1);
  2726. *rule_nr = 10;
  2727. if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
  2728. return 0;
  2729. *rule_nr = 20;
  2730. if ((self == UUID_JUST_CREATED || self == (u64)0) &&
  2731. peer != UUID_JUST_CREATED)
  2732. return -2;
  2733. *rule_nr = 30;
  2734. if (self != UUID_JUST_CREATED &&
  2735. (peer == UUID_JUST_CREATED || peer == (u64)0))
  2736. return 2;
  2737. if (self == peer) {
  2738. int rct, dc; /* roles at crash time */
  2739. if (device->p_uuid[UI_BITMAP] == (u64)0 && device->ldev->md.uuid[UI_BITMAP] != (u64)0) {
  2740. if (connection->agreed_pro_version < 91)
  2741. return -1091;
  2742. if ((device->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
  2743. (device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
  2744. drbd_info(device, "was SyncSource, missed the resync finished event, corrected myself:\n");
  2745. drbd_uuid_move_history(device);
  2746. device->ldev->md.uuid[UI_HISTORY_START] = device->ldev->md.uuid[UI_BITMAP];
  2747. device->ldev->md.uuid[UI_BITMAP] = 0;
  2748. drbd_uuid_dump(device, "self", device->ldev->md.uuid,
  2749. device->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(device) : 0, 0);
  2750. *rule_nr = 34;
  2751. } else {
  2752. drbd_info(device, "was SyncSource (peer failed to write sync_uuid)\n");
  2753. *rule_nr = 36;
  2754. }
  2755. return 1;
  2756. }
  2757. if (device->ldev->md.uuid[UI_BITMAP] == (u64)0 && device->p_uuid[UI_BITMAP] != (u64)0) {
  2758. if (connection->agreed_pro_version < 91)
  2759. return -1091;
  2760. if ((device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (device->p_uuid[UI_BITMAP] & ~((u64)1)) &&
  2761. (device->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (device->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
  2762. drbd_info(device, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
  2763. device->p_uuid[UI_HISTORY_START + 1] = device->p_uuid[UI_HISTORY_START];
  2764. device->p_uuid[UI_HISTORY_START] = device->p_uuid[UI_BITMAP];
  2765. device->p_uuid[UI_BITMAP] = 0UL;
  2766. drbd_uuid_dump(device, "peer", device->p_uuid, device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]);
  2767. *rule_nr = 35;
  2768. } else {
  2769. drbd_info(device, "was SyncTarget (failed to write sync_uuid)\n");
  2770. *rule_nr = 37;
  2771. }
  2772. return -1;
  2773. }
  2774. /* Common power [off|failure] */
  2775. rct = (test_bit(CRASHED_PRIMARY, &device->flags) ? 1 : 0) +
  2776. (device->p_uuid[UI_FLAGS] & 2);
  2777. /* lowest bit is set when we were primary,
  2778. * next bit (weight 2) is set when peer was primary */
  2779. *rule_nr = 40;
  2780. /* Neither has the "crashed primary" flag set,
  2781. * only a replication link hickup. */
  2782. if (rct == 0)
  2783. return 0;
  2784. /* Current UUID equal and no bitmap uuid; does not necessarily
  2785. * mean this was a "simultaneous hard crash", maybe IO was
  2786. * frozen, so no UUID-bump happened.
  2787. * This is a protocol change, overload DRBD_FF_WSAME as flag
  2788. * for "new-enough" peer DRBD version. */
  2789. if (device->state.role == R_PRIMARY || peer_role == R_PRIMARY) {
  2790. *rule_nr = 41;
  2791. if (!(connection->agreed_features & DRBD_FF_WSAME)) {
  2792. drbd_warn(peer_device, "Equivalent unrotated UUIDs, but current primary present.\n");
  2793. return -(0x10000 | PRO_VERSION_MAX | (DRBD_FF_WSAME << 8));
  2794. }
  2795. if (device->state.role == R_PRIMARY && peer_role == R_PRIMARY) {
  2796. /* At least one has the "crashed primary" bit set,
  2797. * both are primary now, but neither has rotated its UUIDs?
  2798. * "Can not happen." */
  2799. drbd_err(peer_device, "Equivalent unrotated UUIDs, but both are primary. Can not resolve this.\n");
  2800. return -100;
  2801. }
  2802. if (device->state.role == R_PRIMARY)
  2803. return 1;
  2804. return -1;
  2805. }
  2806. /* Both are secondary.
  2807. * Really looks like recovery from simultaneous hard crash.
  2808. * Check which had been primary before, and arbitrate. */
  2809. switch (rct) {
  2810. case 0: /* !self_pri && !peer_pri */ return 0; /* already handled */
  2811. case 1: /* self_pri && !peer_pri */ return 1;
  2812. case 2: /* !self_pri && peer_pri */ return -1;
  2813. case 3: /* self_pri && peer_pri */
  2814. dc = test_bit(RESOLVE_CONFLICTS, &connection->flags);
  2815. return dc ? -1 : 1;
  2816. }
  2817. }
  2818. *rule_nr = 50;
  2819. peer = device->p_uuid[UI_BITMAP] & ~((u64)1);
  2820. if (self == peer)
  2821. return -1;
  2822. *rule_nr = 51;
  2823. peer = device->p_uuid[UI_HISTORY_START] & ~((u64)1);
  2824. if (self == peer) {
  2825. if (connection->agreed_pro_version < 96 ?
  2826. (device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
  2827. (device->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
  2828. peer + UUID_NEW_BM_OFFSET == (device->p_uuid[UI_BITMAP] & ~((u64)1))) {
  2829. /* The last P_SYNC_UUID did not get though. Undo the last start of
  2830. resync as sync source modifications of the peer's UUIDs. */
  2831. if (connection->agreed_pro_version < 91)
  2832. return -1091;
  2833. device->p_uuid[UI_BITMAP] = device->p_uuid[UI_HISTORY_START];
  2834. device->p_uuid[UI_HISTORY_START] = device->p_uuid[UI_HISTORY_START + 1];
  2835. drbd_info(device, "Lost last syncUUID packet, corrected:\n");
  2836. drbd_uuid_dump(device, "peer", device->p_uuid, device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]);
  2837. return -1;
  2838. }
  2839. }
  2840. *rule_nr = 60;
  2841. self = device->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
  2842. for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
  2843. peer = device->p_uuid[i] & ~((u64)1);
  2844. if (self == peer)
  2845. return -2;
  2846. }
  2847. *rule_nr = 70;
  2848. self = device->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
  2849. peer = device->p_uuid[UI_CURRENT] & ~((u64)1);
  2850. if (self == peer)
  2851. return 1;
  2852. *rule_nr = 71;
  2853. self = device->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
  2854. if (self == peer) {
  2855. if (connection->agreed_pro_version < 96 ?
  2856. (device->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
  2857. (device->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
  2858. self + UUID_NEW_BM_OFFSET == (device->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
  2859. /* The last P_SYNC_UUID did not get though. Undo the last start of
  2860. resync as sync source modifications of our UUIDs. */
  2861. if (connection->agreed_pro_version < 91)
  2862. return -1091;
  2863. __drbd_uuid_set(device, UI_BITMAP, device->ldev->md.uuid[UI_HISTORY_START]);
  2864. __drbd_uuid_set(device, UI_HISTORY_START, device->ldev->md.uuid[UI_HISTORY_START + 1]);
  2865. drbd_info(device, "Last syncUUID did not get through, corrected:\n");
  2866. drbd_uuid_dump(device, "self", device->ldev->md.uuid,
  2867. device->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(device) : 0, 0);
  2868. return 1;
  2869. }
  2870. }
  2871. *rule_nr = 80;
  2872. peer = device->p_uuid[UI_CURRENT] & ~((u64)1);
  2873. for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
  2874. self = device->ldev->md.uuid[i] & ~((u64)1);
  2875. if (self == peer)
  2876. return 2;
  2877. }
  2878. *rule_nr = 90;
  2879. self = device->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
  2880. peer = device->p_uuid[UI_BITMAP] & ~((u64)1);
  2881. if (self == peer && self != ((u64)0))
  2882. return 100;
  2883. *rule_nr = 100;
  2884. for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
  2885. self = device->ldev->md.uuid[i] & ~((u64)1);
  2886. for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
  2887. peer = device->p_uuid[j] & ~((u64)1);
  2888. if (self == peer)
  2889. return -100;
  2890. }
  2891. }
  2892. return -1000;
  2893. }
  2894. /* drbd_sync_handshake() returns the new conn state on success, or
  2895. CONN_MASK (-1) on failure.
  2896. */
  2897. static enum drbd_conns drbd_sync_handshake(struct drbd_peer_device *peer_device,
  2898. enum drbd_role peer_role,
  2899. enum drbd_disk_state peer_disk) __must_hold(local)
  2900. {
  2901. struct drbd_device *device = peer_device->device;
  2902. enum drbd_conns rv = C_MASK;
  2903. enum drbd_disk_state mydisk;
  2904. struct net_conf *nc;
  2905. int hg, rule_nr, rr_conflict, tentative, always_asbp;
  2906. mydisk = device->state.disk;
  2907. if (mydisk == D_NEGOTIATING)
  2908. mydisk = device->new_state_tmp.disk;
  2909. drbd_info(device, "drbd_sync_handshake:\n");
  2910. spin_lock_irq(&device->ldev->md.uuid_lock);
  2911. drbd_uuid_dump(device, "self", device->ldev->md.uuid, device->comm_bm_set, 0);
  2912. drbd_uuid_dump(device, "peer", device->p_uuid,
  2913. device->p_uuid[UI_SIZE], device->p_uuid[UI_FLAGS]);
  2914. hg = drbd_uuid_compare(device, peer_role, &rule_nr);
  2915. spin_unlock_irq(&device->ldev->md.uuid_lock);
  2916. drbd_info(device, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
  2917. if (hg == -1000) {
  2918. drbd_alert(device, "Unrelated data, aborting!\n");
  2919. return C_MASK;
  2920. }
  2921. if (hg < -0x10000) {
  2922. int proto, fflags;
  2923. hg = -hg;
  2924. proto = hg & 0xff;
  2925. fflags = (hg >> 8) & 0xff;
  2926. drbd_alert(device, "To resolve this both sides have to support at least protocol %d and feature flags 0x%x\n",
  2927. proto, fflags);
  2928. return C_MASK;
  2929. }
  2930. if (hg < -1000) {
  2931. drbd_alert(device, "To resolve this both sides have to support at least protocol %d\n", -hg - 1000);
  2932. return C_MASK;
  2933. }
  2934. if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
  2935. (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
  2936. int f = (hg == -100) || abs(hg) == 2;
  2937. hg = mydisk > D_INCONSISTENT ? 1 : -1;
  2938. if (f)
  2939. hg = hg*2;
  2940. drbd_info(device, "Becoming sync %s due to disk states.\n",
  2941. hg > 0 ? "source" : "target");
  2942. }
  2943. if (abs(hg) == 100)
  2944. drbd_khelper(device, "initial-split-brain");
  2945. rcu_read_lock();
  2946. nc = rcu_dereference(peer_device->connection->net_conf);
  2947. always_asbp = nc->always_asbp;
  2948. rr_conflict = nc->rr_conflict;
  2949. tentative = nc->tentative;
  2950. rcu_read_unlock();
  2951. if (hg == 100 || (hg == -100 && always_asbp)) {
  2952. int pcount = (device->state.role == R_PRIMARY)
  2953. + (peer_role == R_PRIMARY);
  2954. int forced = (hg == -100);
  2955. switch (pcount) {
  2956. case 0:
  2957. hg = drbd_asb_recover_0p(peer_device);
  2958. break;
  2959. case 1:
  2960. hg = drbd_asb_recover_1p(peer_device);
  2961. break;
  2962. case 2:
  2963. hg = drbd_asb_recover_2p(peer_device);
  2964. break;
  2965. }
  2966. if (abs(hg) < 100) {
  2967. drbd_warn(device, "Split-Brain detected, %d primaries, "
  2968. "automatically solved. Sync from %s node\n",
  2969. pcount, (hg < 0) ? "peer" : "this");
  2970. if (forced) {
  2971. drbd_warn(device, "Doing a full sync, since"
  2972. " UUIDs where ambiguous.\n");
  2973. hg = hg*2;
  2974. }
  2975. }
  2976. }
  2977. if (hg == -100) {
  2978. if (test_bit(DISCARD_MY_DATA, &device->flags) && !(device->p_uuid[UI_FLAGS]&1))
  2979. hg = -1;
  2980. if (!test_bit(DISCARD_MY_DATA, &device->flags) && (device->p_uuid[UI_FLAGS]&1))
  2981. hg = 1;
  2982. if (abs(hg) < 100)
  2983. drbd_warn(device, "Split-Brain detected, manually solved. "
  2984. "Sync from %s node\n",
  2985. (hg < 0) ? "peer" : "this");
  2986. }
  2987. if (hg == -100) {
  2988. /* FIXME this log message is not correct if we end up here
  2989. * after an attempted attach on a diskless node.
  2990. * We just refuse to attach -- well, we drop the "connection"
  2991. * to that disk, in a way... */
  2992. drbd_alert(device, "Split-Brain detected but unresolved, dropping connection!\n");
  2993. drbd_khelper(device, "split-brain");
  2994. return C_MASK;
  2995. }
  2996. if (hg > 0 && mydisk <= D_INCONSISTENT) {
  2997. drbd_err(device, "I shall become SyncSource, but I am inconsistent!\n");
  2998. return C_MASK;
  2999. }
  3000. if (hg < 0 && /* by intention we do not use mydisk here. */
  3001. device->state.role == R_PRIMARY && device->state.disk >= D_CONSISTENT) {
  3002. switch (rr_conflict) {
  3003. case ASB_CALL_HELPER:
  3004. drbd_khelper(device, "pri-lost");
  3005. /* fall through */
  3006. case ASB_DISCONNECT:
  3007. drbd_err(device, "I shall become SyncTarget, but I am primary!\n");
  3008. return C_MASK;
  3009. case ASB_VIOLENTLY:
  3010. drbd_warn(device, "Becoming SyncTarget, violating the stable-data"
  3011. "assumption\n");
  3012. }
  3013. }
  3014. if (tentative || test_bit(CONN_DRY_RUN, &peer_device->connection->flags)) {
  3015. if (hg == 0)
  3016. drbd_info(device, "dry-run connect: No resync, would become Connected immediately.\n");
  3017. else
  3018. drbd_info(device, "dry-run connect: Would become %s, doing a %s resync.",
  3019. drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
  3020. abs(hg) >= 2 ? "full" : "bit-map based");
  3021. return C_MASK;
  3022. }
  3023. if (abs(hg) >= 2) {
  3024. drbd_info(device, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
  3025. if (drbd_bitmap_io(device, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
  3026. BM_LOCKED_SET_ALLOWED))
  3027. return C_MASK;
  3028. }
  3029. if (hg > 0) { /* become sync source. */
  3030. rv = C_WF_BITMAP_S;
  3031. } else if (hg < 0) { /* become sync target */
  3032. rv = C_WF_BITMAP_T;
  3033. } else {
  3034. rv = C_CONNECTED;
  3035. if (drbd_bm_total_weight(device)) {
  3036. drbd_info(device, "No resync, but %lu bits in bitmap!\n",
  3037. drbd_bm_total_weight(device));
  3038. }
  3039. }
  3040. return rv;
  3041. }
  3042. static enum drbd_after_sb_p convert_after_sb(enum drbd_after_sb_p peer)
  3043. {
  3044. /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
  3045. if (peer == ASB_DISCARD_REMOTE)
  3046. return ASB_DISCARD_LOCAL;
  3047. /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
  3048. if (peer == ASB_DISCARD_LOCAL)
  3049. return ASB_DISCARD_REMOTE;
  3050. /* everything else is valid if they are equal on both sides. */
  3051. return peer;
  3052. }
  3053. static int receive_protocol(struct drbd_connection *connection, struct packet_info *pi)
  3054. {
  3055. struct p_protocol *p = pi->data;
  3056. enum drbd_after_sb_p p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
  3057. int p_proto, p_discard_my_data, p_two_primaries, cf;
  3058. struct net_conf *nc, *old_net_conf, *new_net_conf = NULL;
  3059. char integrity_alg[SHARED_SECRET_MAX] = "";
  3060. struct crypto_ahash *peer_integrity_tfm = NULL;
  3061. void *int_dig_in = NULL, *int_dig_vv = NULL;
  3062. p_proto = be32_to_cpu(p->protocol);
  3063. p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
  3064. p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
  3065. p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
  3066. p_two_primaries = be32_to_cpu(p->two_primaries);
  3067. cf = be32_to_cpu(p->conn_flags);
  3068. p_discard_my_data = cf & CF_DISCARD_MY_DATA;
  3069. if (connection->agreed_pro_version >= 87) {
  3070. int err;
  3071. if (pi->size > sizeof(integrity_alg))
  3072. return -EIO;
  3073. err = drbd_recv_all(connection, integrity_alg, pi->size);
  3074. if (err)
  3075. return err;
  3076. integrity_alg[SHARED_SECRET_MAX - 1] = 0;
  3077. }
  3078. if (pi->cmd != P_PROTOCOL_UPDATE) {
  3079. clear_bit(CONN_DRY_RUN, &connection->flags);
  3080. if (cf & CF_DRY_RUN)
  3081. set_bit(CONN_DRY_RUN, &connection->flags);
  3082. rcu_read_lock();
  3083. nc = rcu_dereference(connection->net_conf);
  3084. if (p_proto != nc->wire_protocol) {
  3085. drbd_err(connection, "incompatible %s settings\n", "protocol");
  3086. goto disconnect_rcu_unlock;
  3087. }
  3088. if (convert_after_sb(p_after_sb_0p) != nc->after_sb_0p) {
  3089. drbd_err(connection, "incompatible %s settings\n", "after-sb-0pri");
  3090. goto disconnect_rcu_unlock;
  3091. }
  3092. if (convert_after_sb(p_after_sb_1p) != nc->after_sb_1p) {
  3093. drbd_err(connection, "incompatible %s settings\n", "after-sb-1pri");
  3094. goto disconnect_rcu_unlock;
  3095. }
  3096. if (convert_after_sb(p_after_sb_2p) != nc->after_sb_2p) {
  3097. drbd_err(connection, "incompatible %s settings\n", "after-sb-2pri");
  3098. goto disconnect_rcu_unlock;
  3099. }
  3100. if (p_discard_my_data && nc->discard_my_data) {
  3101. drbd_err(connection, "incompatible %s settings\n", "discard-my-data");
  3102. goto disconnect_rcu_unlock;
  3103. }
  3104. if (p_two_primaries != nc->two_primaries) {
  3105. drbd_err(connection, "incompatible %s settings\n", "allow-two-primaries");
  3106. goto disconnect_rcu_unlock;
  3107. }
  3108. if (strcmp(integrity_alg, nc->integrity_alg)) {
  3109. drbd_err(connection, "incompatible %s settings\n", "data-integrity-alg");
  3110. goto disconnect_rcu_unlock;
  3111. }
  3112. rcu_read_unlock();
  3113. }
  3114. if (integrity_alg[0]) {
  3115. int hash_size;
  3116. /*
  3117. * We can only change the peer data integrity algorithm
  3118. * here. Changing our own data integrity algorithm
  3119. * requires that we send a P_PROTOCOL_UPDATE packet at
  3120. * the same time; otherwise, the peer has no way to
  3121. * tell between which packets the algorithm should
  3122. * change.
  3123. */
  3124. peer_integrity_tfm = crypto_alloc_ahash(integrity_alg, 0, CRYPTO_ALG_ASYNC);
  3125. if (IS_ERR(peer_integrity_tfm)) {
  3126. peer_integrity_tfm = NULL;
  3127. drbd_err(connection, "peer data-integrity-alg %s not supported\n",
  3128. integrity_alg);
  3129. goto disconnect;
  3130. }
  3131. hash_size = crypto_ahash_digestsize(peer_integrity_tfm);
  3132. int_dig_in = kmalloc(hash_size, GFP_KERNEL);
  3133. int_dig_vv = kmalloc(hash_size, GFP_KERNEL);
  3134. if (!(int_dig_in && int_dig_vv)) {
  3135. drbd_err(connection, "Allocation of buffers for data integrity checking failed\n");
  3136. goto disconnect;
  3137. }
  3138. }
  3139. new_net_conf = kmalloc(sizeof(struct net_conf), GFP_KERNEL);
  3140. if (!new_net_conf) {
  3141. drbd_err(connection, "Allocation of new net_conf failed\n");
  3142. goto disconnect;
  3143. }
  3144. mutex_lock(&connection->data.mutex);
  3145. mutex_lock(&connection->resource->conf_update);
  3146. old_net_conf = connection->net_conf;
  3147. *new_net_conf = *old_net_conf;
  3148. new_net_conf->wire_protocol = p_proto;
  3149. new_net_conf->after_sb_0p = convert_after_sb(p_after_sb_0p);
  3150. new_net_conf->after_sb_1p = convert_after_sb(p_after_sb_1p);
  3151. new_net_conf->after_sb_2p = convert_after_sb(p_after_sb_2p);
  3152. new_net_conf->two_primaries = p_two_primaries;
  3153. rcu_assign_pointer(connection->net_conf, new_net_conf);
  3154. mutex_unlock(&connection->resource->conf_update);
  3155. mutex_unlock(&connection->data.mutex);
  3156. crypto_free_ahash(connection->peer_integrity_tfm);
  3157. kfree(connection->int_dig_in);
  3158. kfree(connection->int_dig_vv);
  3159. connection->peer_integrity_tfm = peer_integrity_tfm;
  3160. connection->int_dig_in = int_dig_in;
  3161. connection->int_dig_vv = int_dig_vv;
  3162. if (strcmp(old_net_conf->integrity_alg, integrity_alg))
  3163. drbd_info(connection, "peer data-integrity-alg: %s\n",
  3164. integrity_alg[0] ? integrity_alg : "(none)");
  3165. synchronize_rcu();
  3166. kfree(old_net_conf);
  3167. return 0;
  3168. disconnect_rcu_unlock:
  3169. rcu_read_unlock();
  3170. disconnect:
  3171. crypto_free_ahash(peer_integrity_tfm);
  3172. kfree(int_dig_in);
  3173. kfree(int_dig_vv);
  3174. conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
  3175. return -EIO;
  3176. }
  3177. /* helper function
  3178. * input: alg name, feature name
  3179. * return: NULL (alg name was "")
  3180. * ERR_PTR(error) if something goes wrong
  3181. * or the crypto hash ptr, if it worked out ok. */
  3182. static struct crypto_ahash *drbd_crypto_alloc_digest_safe(const struct drbd_device *device,
  3183. const char *alg, const char *name)
  3184. {
  3185. struct crypto_ahash *tfm;
  3186. if (!alg[0])
  3187. return NULL;
  3188. tfm = crypto_alloc_ahash(alg, 0, CRYPTO_ALG_ASYNC);
  3189. if (IS_ERR(tfm)) {
  3190. drbd_err(device, "Can not allocate \"%s\" as %s (reason: %ld)\n",
  3191. alg, name, PTR_ERR(tfm));
  3192. return tfm;
  3193. }
  3194. return tfm;
  3195. }
  3196. static int ignore_remaining_packet(struct drbd_connection *connection, struct packet_info *pi)
  3197. {
  3198. void *buffer = connection->data.rbuf;
  3199. int size = pi->size;
  3200. while (size) {
  3201. int s = min_t(int, size, DRBD_SOCKET_BUFFER_SIZE);
  3202. s = drbd_recv(connection, buffer, s);
  3203. if (s <= 0) {
  3204. if (s < 0)
  3205. return s;
  3206. break;
  3207. }
  3208. size -= s;
  3209. }
  3210. if (size)
  3211. return -EIO;
  3212. return 0;
  3213. }
  3214. /*
  3215. * config_unknown_volume - device configuration command for unknown volume
  3216. *
  3217. * When a device is added to an existing connection, the node on which the
  3218. * device is added first will send configuration commands to its peer but the
  3219. * peer will not know about the device yet. It will warn and ignore these
  3220. * commands. Once the device is added on the second node, the second node will
  3221. * send the same device configuration commands, but in the other direction.
  3222. *
  3223. * (We can also end up here if drbd is misconfigured.)
  3224. */
  3225. static int config_unknown_volume(struct drbd_connection *connection, struct packet_info *pi)
  3226. {
  3227. drbd_warn(connection, "%s packet received for volume %u, which is not configured locally\n",
  3228. cmdname(pi->cmd), pi->vnr);
  3229. return ignore_remaining_packet(connection, pi);
  3230. }
  3231. static int receive_SyncParam(struct drbd_connection *connection, struct packet_info *pi)
  3232. {
  3233. struct drbd_peer_device *peer_device;
  3234. struct drbd_device *device;
  3235. struct p_rs_param_95 *p;
  3236. unsigned int header_size, data_size, exp_max_sz;
  3237. struct crypto_ahash *verify_tfm = NULL;
  3238. struct crypto_ahash *csums_tfm = NULL;
  3239. struct net_conf *old_net_conf, *new_net_conf = NULL;
  3240. struct disk_conf *old_disk_conf = NULL, *new_disk_conf = NULL;
  3241. const int apv = connection->agreed_pro_version;
  3242. struct fifo_buffer *old_plan = NULL, *new_plan = NULL;
  3243. int fifo_size = 0;
  3244. int err;
  3245. peer_device = conn_peer_device(connection, pi->vnr);
  3246. if (!peer_device)
  3247. return config_unknown_volume(connection, pi);
  3248. device = peer_device->device;
  3249. exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
  3250. : apv == 88 ? sizeof(struct p_rs_param)
  3251. + SHARED_SECRET_MAX
  3252. : apv <= 94 ? sizeof(struct p_rs_param_89)
  3253. : /* apv >= 95 */ sizeof(struct p_rs_param_95);
  3254. if (pi->size > exp_max_sz) {
  3255. drbd_err(device, "SyncParam packet too long: received %u, expected <= %u bytes\n",
  3256. pi->size, exp_max_sz);
  3257. return -EIO;
  3258. }
  3259. if (apv <= 88) {
  3260. header_size = sizeof(struct p_rs_param);
  3261. data_size = pi->size - header_size;
  3262. } else if (apv <= 94) {
  3263. header_size = sizeof(struct p_rs_param_89);
  3264. data_size = pi->size - header_size;
  3265. D_ASSERT(device, data_size == 0);
  3266. } else {
  3267. header_size = sizeof(struct p_rs_param_95);
  3268. data_size = pi->size - header_size;
  3269. D_ASSERT(device, data_size == 0);
  3270. }
  3271. /* initialize verify_alg and csums_alg */
  3272. p = pi->data;
  3273. memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
  3274. err = drbd_recv_all(peer_device->connection, p, header_size);
  3275. if (err)
  3276. return err;
  3277. mutex_lock(&connection->resource->conf_update);
  3278. old_net_conf = peer_device->connection->net_conf;
  3279. if (get_ldev(device)) {
  3280. new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
  3281. if (!new_disk_conf) {
  3282. put_ldev(device);
  3283. mutex_unlock(&connection->resource->conf_update);
  3284. drbd_err(device, "Allocation of new disk_conf failed\n");
  3285. return -ENOMEM;
  3286. }
  3287. old_disk_conf = device->ldev->disk_conf;
  3288. *new_disk_conf = *old_disk_conf;
  3289. new_disk_conf->resync_rate = be32_to_cpu(p->resync_rate);
  3290. }
  3291. if (apv >= 88) {
  3292. if (apv == 88) {
  3293. if (data_size > SHARED_SECRET_MAX || data_size == 0) {
  3294. drbd_err(device, "verify-alg of wrong size, "
  3295. "peer wants %u, accepting only up to %u byte\n",
  3296. data_size, SHARED_SECRET_MAX);
  3297. err = -EIO;
  3298. goto reconnect;
  3299. }
  3300. err = drbd_recv_all(peer_device->connection, p->verify_alg, data_size);
  3301. if (err)
  3302. goto reconnect;
  3303. /* we expect NUL terminated string */
  3304. /* but just in case someone tries to be evil */
  3305. D_ASSERT(device, p->verify_alg[data_size-1] == 0);
  3306. p->verify_alg[data_size-1] = 0;
  3307. } else /* apv >= 89 */ {
  3308. /* we still expect NUL terminated strings */
  3309. /* but just in case someone tries to be evil */
  3310. D_ASSERT(device, p->verify_alg[SHARED_SECRET_MAX-1] == 0);
  3311. D_ASSERT(device, p->csums_alg[SHARED_SECRET_MAX-1] == 0);
  3312. p->verify_alg[SHARED_SECRET_MAX-1] = 0;
  3313. p->csums_alg[SHARED_SECRET_MAX-1] = 0;
  3314. }
  3315. if (strcmp(old_net_conf->verify_alg, p->verify_alg)) {
  3316. if (device->state.conn == C_WF_REPORT_PARAMS) {
  3317. drbd_err(device, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
  3318. old_net_conf->verify_alg, p->verify_alg);
  3319. goto disconnect;
  3320. }
  3321. verify_tfm = drbd_crypto_alloc_digest_safe(device,
  3322. p->verify_alg, "verify-alg");
  3323. if (IS_ERR(verify_tfm)) {
  3324. verify_tfm = NULL;
  3325. goto disconnect;
  3326. }
  3327. }
  3328. if (apv >= 89 && strcmp(old_net_conf->csums_alg, p->csums_alg)) {
  3329. if (device->state.conn == C_WF_REPORT_PARAMS) {
  3330. drbd_err(device, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
  3331. old_net_conf->csums_alg, p->csums_alg);
  3332. goto disconnect;
  3333. }
  3334. csums_tfm = drbd_crypto_alloc_digest_safe(device,
  3335. p->csums_alg, "csums-alg");
  3336. if (IS_ERR(csums_tfm)) {
  3337. csums_tfm = NULL;
  3338. goto disconnect;
  3339. }
  3340. }
  3341. if (apv > 94 && new_disk_conf) {
  3342. new_disk_conf->c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
  3343. new_disk_conf->c_delay_target = be32_to_cpu(p->c_delay_target);
  3344. new_disk_conf->c_fill_target = be32_to_cpu(p->c_fill_target);
  3345. new_disk_conf->c_max_rate = be32_to_cpu(p->c_max_rate);
  3346. fifo_size = (new_disk_conf->c_plan_ahead * 10 * SLEEP_TIME) / HZ;
  3347. if (fifo_size != device->rs_plan_s->size) {
  3348. new_plan = fifo_alloc(fifo_size);
  3349. if (!new_plan) {
  3350. drbd_err(device, "kmalloc of fifo_buffer failed");
  3351. put_ldev(device);
  3352. goto disconnect;
  3353. }
  3354. }
  3355. }
  3356. if (verify_tfm || csums_tfm) {
  3357. new_net_conf = kzalloc(sizeof(struct net_conf), GFP_KERNEL);
  3358. if (!new_net_conf) {
  3359. drbd_err(device, "Allocation of new net_conf failed\n");
  3360. goto disconnect;
  3361. }
  3362. *new_net_conf = *old_net_conf;
  3363. if (verify_tfm) {
  3364. strcpy(new_net_conf->verify_alg, p->verify_alg);
  3365. new_net_conf->verify_alg_len = strlen(p->verify_alg) + 1;
  3366. crypto_free_ahash(peer_device->connection->verify_tfm);
  3367. peer_device->connection->verify_tfm = verify_tfm;
  3368. drbd_info(device, "using verify-alg: \"%s\"\n", p->verify_alg);
  3369. }
  3370. if (csums_tfm) {
  3371. strcpy(new_net_conf->csums_alg, p->csums_alg);
  3372. new_net_conf->csums_alg_len = strlen(p->csums_alg) + 1;
  3373. crypto_free_ahash(peer_device->connection->csums_tfm);
  3374. peer_device->connection->csums_tfm = csums_tfm;
  3375. drbd_info(device, "using csums-alg: \"%s\"\n", p->csums_alg);
  3376. }
  3377. rcu_assign_pointer(connection->net_conf, new_net_conf);
  3378. }
  3379. }
  3380. if (new_disk_conf) {
  3381. rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
  3382. put_ldev(device);
  3383. }
  3384. if (new_plan) {
  3385. old_plan = device->rs_plan_s;
  3386. rcu_assign_pointer(device->rs_plan_s, new_plan);
  3387. }
  3388. mutex_unlock(&connection->resource->conf_update);
  3389. synchronize_rcu();
  3390. if (new_net_conf)
  3391. kfree(old_net_conf);
  3392. kfree(old_disk_conf);
  3393. kfree(old_plan);
  3394. return 0;
  3395. reconnect:
  3396. if (new_disk_conf) {
  3397. put_ldev(device);
  3398. kfree(new_disk_conf);
  3399. }
  3400. mutex_unlock(&connection->resource->conf_update);
  3401. return -EIO;
  3402. disconnect:
  3403. kfree(new_plan);
  3404. if (new_disk_conf) {
  3405. put_ldev(device);
  3406. kfree(new_disk_conf);
  3407. }
  3408. mutex_unlock(&connection->resource->conf_update);
  3409. /* just for completeness: actually not needed,
  3410. * as this is not reached if csums_tfm was ok. */
  3411. crypto_free_ahash(csums_tfm);
  3412. /* but free the verify_tfm again, if csums_tfm did not work out */
  3413. crypto_free_ahash(verify_tfm);
  3414. conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
  3415. return -EIO;
  3416. }
  3417. /* warn if the arguments differ by more than 12.5% */
  3418. static void warn_if_differ_considerably(struct drbd_device *device,
  3419. const char *s, sector_t a, sector_t b)
  3420. {
  3421. sector_t d;
  3422. if (a == 0 || b == 0)
  3423. return;
  3424. d = (a > b) ? (a - b) : (b - a);
  3425. if (d > (a>>3) || d > (b>>3))
  3426. drbd_warn(device, "Considerable difference in %s: %llus vs. %llus\n", s,
  3427. (unsigned long long)a, (unsigned long long)b);
  3428. }
  3429. static int receive_sizes(struct drbd_connection *connection, struct packet_info *pi)
  3430. {
  3431. struct drbd_peer_device *peer_device;
  3432. struct drbd_device *device;
  3433. struct p_sizes *p = pi->data;
  3434. struct o_qlim *o = (connection->agreed_features & DRBD_FF_WSAME) ? p->qlim : NULL;
  3435. enum determine_dev_size dd = DS_UNCHANGED;
  3436. sector_t p_size, p_usize, p_csize, my_usize;
  3437. sector_t new_size, cur_size;
  3438. int ldsc = 0; /* local disk size changed */
  3439. enum dds_flags ddsf;
  3440. peer_device = conn_peer_device(connection, pi->vnr);
  3441. if (!peer_device)
  3442. return config_unknown_volume(connection, pi);
  3443. device = peer_device->device;
  3444. cur_size = drbd_get_capacity(device->this_bdev);
  3445. p_size = be64_to_cpu(p->d_size);
  3446. p_usize = be64_to_cpu(p->u_size);
  3447. p_csize = be64_to_cpu(p->c_size);
  3448. /* just store the peer's disk size for now.
  3449. * we still need to figure out whether we accept that. */
  3450. device->p_size = p_size;
  3451. if (get_ldev(device)) {
  3452. rcu_read_lock();
  3453. my_usize = rcu_dereference(device->ldev->disk_conf)->disk_size;
  3454. rcu_read_unlock();
  3455. warn_if_differ_considerably(device, "lower level device sizes",
  3456. p_size, drbd_get_max_capacity(device->ldev));
  3457. warn_if_differ_considerably(device, "user requested size",
  3458. p_usize, my_usize);
  3459. /* if this is the first connect, or an otherwise expected
  3460. * param exchange, choose the minimum */
  3461. if (device->state.conn == C_WF_REPORT_PARAMS)
  3462. p_usize = min_not_zero(my_usize, p_usize);
  3463. /* Never shrink a device with usable data during connect.
  3464. But allow online shrinking if we are connected. */
  3465. new_size = drbd_new_dev_size(device, device->ldev, p_usize, 0);
  3466. if (new_size < cur_size &&
  3467. device->state.disk >= D_OUTDATED &&
  3468. device->state.conn < C_CONNECTED) {
  3469. drbd_err(device, "The peer's disk size is too small! (%llu < %llu sectors)\n",
  3470. (unsigned long long)new_size, (unsigned long long)cur_size);
  3471. conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
  3472. put_ldev(device);
  3473. return -EIO;
  3474. }
  3475. if (my_usize != p_usize) {
  3476. struct disk_conf *old_disk_conf, *new_disk_conf = NULL;
  3477. new_disk_conf = kzalloc(sizeof(struct disk_conf), GFP_KERNEL);
  3478. if (!new_disk_conf) {
  3479. drbd_err(device, "Allocation of new disk_conf failed\n");
  3480. put_ldev(device);
  3481. return -ENOMEM;
  3482. }
  3483. mutex_lock(&connection->resource->conf_update);
  3484. old_disk_conf = device->ldev->disk_conf;
  3485. *new_disk_conf = *old_disk_conf;
  3486. new_disk_conf->disk_size = p_usize;
  3487. rcu_assign_pointer(device->ldev->disk_conf, new_disk_conf);
  3488. mutex_unlock(&connection->resource->conf_update);
  3489. synchronize_rcu();
  3490. kfree(old_disk_conf);
  3491. drbd_info(device, "Peer sets u_size to %lu sectors\n",
  3492. (unsigned long)my_usize);
  3493. }
  3494. put_ldev(device);
  3495. }
  3496. device->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
  3497. /* Leave drbd_reconsider_queue_parameters() before drbd_determine_dev_size().
  3498. In case we cleared the QUEUE_FLAG_DISCARD from our queue in
  3499. drbd_reconsider_queue_parameters(), we can be sure that after
  3500. drbd_determine_dev_size() no REQ_DISCARDs are in the queue. */
  3501. ddsf = be16_to_cpu(p->dds_flags);
  3502. if (get_ldev(device)) {
  3503. drbd_reconsider_queue_parameters(device, device->ldev, o);
  3504. dd = drbd_determine_dev_size(device, ddsf, NULL);
  3505. put_ldev(device);
  3506. if (dd == DS_ERROR)
  3507. return -EIO;
  3508. drbd_md_sync(device);
  3509. } else {
  3510. /*
  3511. * I am diskless, need to accept the peer's *current* size.
  3512. * I must NOT accept the peers backing disk size,
  3513. * it may have been larger than mine all along...
  3514. *
  3515. * At this point, the peer knows more about my disk, or at
  3516. * least about what we last agreed upon, than myself.
  3517. * So if his c_size is less than his d_size, the most likely
  3518. * reason is that *my* d_size was smaller last time we checked.
  3519. *
  3520. * However, if he sends a zero current size,
  3521. * take his (user-capped or) backing disk size anyways.
  3522. *
  3523. * Unless of course he does not have a disk himself.
  3524. * In which case we ignore this completely.
  3525. */
  3526. sector_t new_size = p_csize ?: p_usize ?: p_size;
  3527. drbd_reconsider_queue_parameters(device, NULL, o);
  3528. if (new_size == 0) {
  3529. /* Ignore, peer does not know nothing. */
  3530. } else if (new_size == cur_size) {
  3531. /* nothing to do */
  3532. } else if (cur_size != 0 && p_size == 0) {
  3533. drbd_warn(device, "Ignored diskless peer device size (peer:%llu != me:%llu sectors)!\n",
  3534. (unsigned long long)new_size, (unsigned long long)cur_size);
  3535. } else if (new_size < cur_size && device->state.role == R_PRIMARY) {
  3536. drbd_err(device, "The peer's device size is too small! (%llu < %llu sectors); demote me first!\n",
  3537. (unsigned long long)new_size, (unsigned long long)cur_size);
  3538. conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
  3539. return -EIO;
  3540. } else {
  3541. /* I believe the peer, if
  3542. * - I don't have a current size myself
  3543. * - we agree on the size anyways
  3544. * - I do have a current size, am Secondary,
  3545. * and he has the only disk
  3546. * - I do have a current size, am Primary,
  3547. * and he has the only disk,
  3548. * which is larger than my current size
  3549. */
  3550. drbd_set_my_capacity(device, new_size);
  3551. }
  3552. }
  3553. if (get_ldev(device)) {
  3554. if (device->ldev->known_size != drbd_get_capacity(device->ldev->backing_bdev)) {
  3555. device->ldev->known_size = drbd_get_capacity(device->ldev->backing_bdev);
  3556. ldsc = 1;
  3557. }
  3558. put_ldev(device);
  3559. }
  3560. if (device->state.conn > C_WF_REPORT_PARAMS) {
  3561. if (be64_to_cpu(p->c_size) !=
  3562. drbd_get_capacity(device->this_bdev) || ldsc) {
  3563. /* we have different sizes, probably peer
  3564. * needs to know my new size... */
  3565. drbd_send_sizes(peer_device, 0, ddsf);
  3566. }
  3567. if (test_and_clear_bit(RESIZE_PENDING, &device->flags) ||
  3568. (dd == DS_GREW && device->state.conn == C_CONNECTED)) {
  3569. if (device->state.pdsk >= D_INCONSISTENT &&
  3570. device->state.disk >= D_INCONSISTENT) {
  3571. if (ddsf & DDSF_NO_RESYNC)
  3572. drbd_info(device, "Resync of new storage suppressed with --assume-clean\n");
  3573. else
  3574. resync_after_online_grow(device);
  3575. } else
  3576. set_bit(RESYNC_AFTER_NEG, &device->flags);
  3577. }
  3578. }
  3579. return 0;
  3580. }
  3581. static int receive_uuids(struct drbd_connection *connection, struct packet_info *pi)
  3582. {
  3583. struct drbd_peer_device *peer_device;
  3584. struct drbd_device *device;
  3585. struct p_uuids *p = pi->data;
  3586. u64 *p_uuid;
  3587. int i, updated_uuids = 0;
  3588. peer_device = conn_peer_device(connection, pi->vnr);
  3589. if (!peer_device)
  3590. return config_unknown_volume(connection, pi);
  3591. device = peer_device->device;
  3592. p_uuid = kmalloc_array(UI_EXTENDED_SIZE, sizeof(*p_uuid), GFP_NOIO);
  3593. if (!p_uuid) {
  3594. drbd_err(device, "kmalloc of p_uuid failed\n");
  3595. return false;
  3596. }
  3597. for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
  3598. p_uuid[i] = be64_to_cpu(p->uuid[i]);
  3599. kfree(device->p_uuid);
  3600. device->p_uuid = p_uuid;
  3601. if ((device->state.conn < C_CONNECTED || device->state.pdsk == D_DISKLESS) &&
  3602. device->state.disk < D_INCONSISTENT &&
  3603. device->state.role == R_PRIMARY &&
  3604. (device->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
  3605. drbd_err(device, "Can only connect to data with current UUID=%016llX\n",
  3606. (unsigned long long)device->ed_uuid);
  3607. conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
  3608. return -EIO;
  3609. }
  3610. if (get_ldev(device)) {
  3611. int skip_initial_sync =
  3612. device->state.conn == C_CONNECTED &&
  3613. peer_device->connection->agreed_pro_version >= 90 &&
  3614. device->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
  3615. (p_uuid[UI_FLAGS] & 8);
  3616. if (skip_initial_sync) {
  3617. drbd_info(device, "Accepted new current UUID, preparing to skip initial sync\n");
  3618. drbd_bitmap_io(device, &drbd_bmio_clear_n_write,
  3619. "clear_n_write from receive_uuids",
  3620. BM_LOCKED_TEST_ALLOWED);
  3621. _drbd_uuid_set(device, UI_CURRENT, p_uuid[UI_CURRENT]);
  3622. _drbd_uuid_set(device, UI_BITMAP, 0);
  3623. _drbd_set_state(_NS2(device, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
  3624. CS_VERBOSE, NULL);
  3625. drbd_md_sync(device);
  3626. updated_uuids = 1;
  3627. }
  3628. put_ldev(device);
  3629. } else if (device->state.disk < D_INCONSISTENT &&
  3630. device->state.role == R_PRIMARY) {
  3631. /* I am a diskless primary, the peer just created a new current UUID
  3632. for me. */
  3633. updated_uuids = drbd_set_ed_uuid(device, p_uuid[UI_CURRENT]);
  3634. }
  3635. /* Before we test for the disk state, we should wait until an eventually
  3636. ongoing cluster wide state change is finished. That is important if
  3637. we are primary and are detaching from our disk. We need to see the
  3638. new disk state... */
  3639. mutex_lock(device->state_mutex);
  3640. mutex_unlock(device->state_mutex);
  3641. if (device->state.conn >= C_CONNECTED && device->state.disk < D_INCONSISTENT)
  3642. updated_uuids |= drbd_set_ed_uuid(device, p_uuid[UI_CURRENT]);
  3643. if (updated_uuids)
  3644. drbd_print_uuids(device, "receiver updated UUIDs to");
  3645. return 0;
  3646. }
  3647. /**
  3648. * convert_state() - Converts the peer's view of the cluster state to our point of view
  3649. * @ps: The state as seen by the peer.
  3650. */
  3651. static union drbd_state convert_state(union drbd_state ps)
  3652. {
  3653. union drbd_state ms;
  3654. static enum drbd_conns c_tab[] = {
  3655. [C_WF_REPORT_PARAMS] = C_WF_REPORT_PARAMS,
  3656. [C_CONNECTED] = C_CONNECTED,
  3657. [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
  3658. [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
  3659. [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
  3660. [C_VERIFY_S] = C_VERIFY_T,
  3661. [C_MASK] = C_MASK,
  3662. };
  3663. ms.i = ps.i;
  3664. ms.conn = c_tab[ps.conn];
  3665. ms.peer = ps.role;
  3666. ms.role = ps.peer;
  3667. ms.pdsk = ps.disk;
  3668. ms.disk = ps.pdsk;
  3669. ms.peer_isp = (ps.aftr_isp | ps.user_isp);
  3670. return ms;
  3671. }
  3672. static int receive_req_state(struct drbd_connection *connection, struct packet_info *pi)
  3673. {
  3674. struct drbd_peer_device *peer_device;
  3675. struct drbd_device *device;
  3676. struct p_req_state *p = pi->data;
  3677. union drbd_state mask, val;
  3678. enum drbd_state_rv rv;
  3679. peer_device = conn_peer_device(connection, pi->vnr);
  3680. if (!peer_device)
  3681. return -EIO;
  3682. device = peer_device->device;
  3683. mask.i = be32_to_cpu(p->mask);
  3684. val.i = be32_to_cpu(p->val);
  3685. if (test_bit(RESOLVE_CONFLICTS, &peer_device->connection->flags) &&
  3686. mutex_is_locked(device->state_mutex)) {
  3687. drbd_send_sr_reply(peer_device, SS_CONCURRENT_ST_CHG);
  3688. return 0;
  3689. }
  3690. mask = convert_state(mask);
  3691. val = convert_state(val);
  3692. rv = drbd_change_state(device, CS_VERBOSE, mask, val);
  3693. drbd_send_sr_reply(peer_device, rv);
  3694. drbd_md_sync(device);
  3695. return 0;
  3696. }
  3697. static int receive_req_conn_state(struct drbd_connection *connection, struct packet_info *pi)
  3698. {
  3699. struct p_req_state *p = pi->data;
  3700. union drbd_state mask, val;
  3701. enum drbd_state_rv rv;
  3702. mask.i = be32_to_cpu(p->mask);
  3703. val.i = be32_to_cpu(p->val);
  3704. if (test_bit(RESOLVE_CONFLICTS, &connection->flags) &&
  3705. mutex_is_locked(&connection->cstate_mutex)) {
  3706. conn_send_sr_reply(connection, SS_CONCURRENT_ST_CHG);
  3707. return 0;
  3708. }
  3709. mask = convert_state(mask);
  3710. val = convert_state(val);
  3711. rv = conn_request_state(connection, mask, val, CS_VERBOSE | CS_LOCAL_ONLY | CS_IGN_OUTD_FAIL);
  3712. conn_send_sr_reply(connection, rv);
  3713. return 0;
  3714. }
  3715. static int receive_state(struct drbd_connection *connection, struct packet_info *pi)
  3716. {
  3717. struct drbd_peer_device *peer_device;
  3718. struct drbd_device *device;
  3719. struct p_state *p = pi->data;
  3720. union drbd_state os, ns, peer_state;
  3721. enum drbd_disk_state real_peer_disk;
  3722. enum chg_state_flags cs_flags;
  3723. int rv;
  3724. peer_device = conn_peer_device(connection, pi->vnr);
  3725. if (!peer_device)
  3726. return config_unknown_volume(connection, pi);
  3727. device = peer_device->device;
  3728. peer_state.i = be32_to_cpu(p->state);
  3729. real_peer_disk = peer_state.disk;
  3730. if (peer_state.disk == D_NEGOTIATING) {
  3731. real_peer_disk = device->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
  3732. drbd_info(device, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
  3733. }
  3734. spin_lock_irq(&device->resource->req_lock);
  3735. retry:
  3736. os = ns = drbd_read_state(device);
  3737. spin_unlock_irq(&device->resource->req_lock);
  3738. /* If some other part of the code (ack_receiver thread, timeout)
  3739. * already decided to close the connection again,
  3740. * we must not "re-establish" it here. */
  3741. if (os.conn <= C_TEAR_DOWN)
  3742. return -ECONNRESET;
  3743. /* If this is the "end of sync" confirmation, usually the peer disk
  3744. * transitions from D_INCONSISTENT to D_UP_TO_DATE. For empty (0 bits
  3745. * set) resync started in PausedSyncT, or if the timing of pause-/
  3746. * unpause-sync events has been "just right", the peer disk may
  3747. * transition from D_CONSISTENT to D_UP_TO_DATE as well.
  3748. */
  3749. if ((os.pdsk == D_INCONSISTENT || os.pdsk == D_CONSISTENT) &&
  3750. real_peer_disk == D_UP_TO_DATE &&
  3751. os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
  3752. /* If we are (becoming) SyncSource, but peer is still in sync
  3753. * preparation, ignore its uptodate-ness to avoid flapping, it
  3754. * will change to inconsistent once the peer reaches active
  3755. * syncing states.
  3756. * It may have changed syncer-paused flags, however, so we
  3757. * cannot ignore this completely. */
  3758. if (peer_state.conn > C_CONNECTED &&
  3759. peer_state.conn < C_SYNC_SOURCE)
  3760. real_peer_disk = D_INCONSISTENT;
  3761. /* if peer_state changes to connected at the same time,
  3762. * it explicitly notifies us that it finished resync.
  3763. * Maybe we should finish it up, too? */
  3764. else if (os.conn >= C_SYNC_SOURCE &&
  3765. peer_state.conn == C_CONNECTED) {
  3766. if (drbd_bm_total_weight(device) <= device->rs_failed)
  3767. drbd_resync_finished(device);
  3768. return 0;
  3769. }
  3770. }
  3771. /* explicit verify finished notification, stop sector reached. */
  3772. if (os.conn == C_VERIFY_T && os.disk == D_UP_TO_DATE &&
  3773. peer_state.conn == C_CONNECTED && real_peer_disk == D_UP_TO_DATE) {
  3774. ov_out_of_sync_print(device);
  3775. drbd_resync_finished(device);
  3776. return 0;
  3777. }
  3778. /* peer says his disk is inconsistent, while we think it is uptodate,
  3779. * and this happens while the peer still thinks we have a sync going on,
  3780. * but we think we are already done with the sync.
  3781. * We ignore this to avoid flapping pdsk.
  3782. * This should not happen, if the peer is a recent version of drbd. */
  3783. if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
  3784. os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
  3785. real_peer_disk = D_UP_TO_DATE;
  3786. if (ns.conn == C_WF_REPORT_PARAMS)
  3787. ns.conn = C_CONNECTED;
  3788. if (peer_state.conn == C_AHEAD)
  3789. ns.conn = C_BEHIND;
  3790. /* TODO:
  3791. * if (primary and diskless and peer uuid != effective uuid)
  3792. * abort attach on peer;
  3793. *
  3794. * If this node does not have good data, was already connected, but
  3795. * the peer did a late attach only now, trying to "negotiate" with me,
  3796. * AND I am currently Primary, possibly frozen, with some specific
  3797. * "effective" uuid, this should never be reached, really, because
  3798. * we first send the uuids, then the current state.
  3799. *
  3800. * In this scenario, we already dropped the connection hard
  3801. * when we received the unsuitable uuids (receive_uuids().
  3802. *
  3803. * Should we want to change this, that is: not drop the connection in
  3804. * receive_uuids() already, then we would need to add a branch here
  3805. * that aborts the attach of "unsuitable uuids" on the peer in case
  3806. * this node is currently Diskless Primary.
  3807. */
  3808. if (device->p_uuid && peer_state.disk >= D_NEGOTIATING &&
  3809. get_ldev_if_state(device, D_NEGOTIATING)) {
  3810. int cr; /* consider resync */
  3811. /* if we established a new connection */
  3812. cr = (os.conn < C_CONNECTED);
  3813. /* if we had an established connection
  3814. * and one of the nodes newly attaches a disk */
  3815. cr |= (os.conn == C_CONNECTED &&
  3816. (peer_state.disk == D_NEGOTIATING ||
  3817. os.disk == D_NEGOTIATING));
  3818. /* if we have both been inconsistent, and the peer has been
  3819. * forced to be UpToDate with --overwrite-data */
  3820. cr |= test_bit(CONSIDER_RESYNC, &device->flags);
  3821. /* if we had been plain connected, and the admin requested to
  3822. * start a sync by "invalidate" or "invalidate-remote" */
  3823. cr |= (os.conn == C_CONNECTED &&
  3824. (peer_state.conn >= C_STARTING_SYNC_S &&
  3825. peer_state.conn <= C_WF_BITMAP_T));
  3826. if (cr)
  3827. ns.conn = drbd_sync_handshake(peer_device, peer_state.role, real_peer_disk);
  3828. put_ldev(device);
  3829. if (ns.conn == C_MASK) {
  3830. ns.conn = C_CONNECTED;
  3831. if (device->state.disk == D_NEGOTIATING) {
  3832. drbd_force_state(device, NS(disk, D_FAILED));
  3833. } else if (peer_state.disk == D_NEGOTIATING) {
  3834. drbd_err(device, "Disk attach process on the peer node was aborted.\n");
  3835. peer_state.disk = D_DISKLESS;
  3836. real_peer_disk = D_DISKLESS;
  3837. } else {
  3838. if (test_and_clear_bit(CONN_DRY_RUN, &peer_device->connection->flags))
  3839. return -EIO;
  3840. D_ASSERT(device, os.conn == C_WF_REPORT_PARAMS);
  3841. conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
  3842. return -EIO;
  3843. }
  3844. }
  3845. }
  3846. spin_lock_irq(&device->resource->req_lock);
  3847. if (os.i != drbd_read_state(device).i)
  3848. goto retry;
  3849. clear_bit(CONSIDER_RESYNC, &device->flags);
  3850. ns.peer = peer_state.role;
  3851. ns.pdsk = real_peer_disk;
  3852. ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
  3853. if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
  3854. ns.disk = device->new_state_tmp.disk;
  3855. cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
  3856. if (ns.pdsk == D_CONSISTENT && drbd_suspended(device) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
  3857. test_bit(NEW_CUR_UUID, &device->flags)) {
  3858. /* Do not allow tl_restart(RESEND) for a rebooted peer. We can only allow this
  3859. for temporal network outages! */
  3860. spin_unlock_irq(&device->resource->req_lock);
  3861. drbd_err(device, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
  3862. tl_clear(peer_device->connection);
  3863. drbd_uuid_new_current(device);
  3864. clear_bit(NEW_CUR_UUID, &device->flags);
  3865. conn_request_state(peer_device->connection, NS2(conn, C_PROTOCOL_ERROR, susp, 0), CS_HARD);
  3866. return -EIO;
  3867. }
  3868. rv = _drbd_set_state(device, ns, cs_flags, NULL);
  3869. ns = drbd_read_state(device);
  3870. spin_unlock_irq(&device->resource->req_lock);
  3871. if (rv < SS_SUCCESS) {
  3872. conn_request_state(peer_device->connection, NS(conn, C_DISCONNECTING), CS_HARD);
  3873. return -EIO;
  3874. }
  3875. if (os.conn > C_WF_REPORT_PARAMS) {
  3876. if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
  3877. peer_state.disk != D_NEGOTIATING ) {
  3878. /* we want resync, peer has not yet decided to sync... */
  3879. /* Nowadays only used when forcing a node into primary role and
  3880. setting its disk to UpToDate with that */
  3881. drbd_send_uuids(peer_device);
  3882. drbd_send_current_state(peer_device);
  3883. }
  3884. }
  3885. clear_bit(DISCARD_MY_DATA, &device->flags);
  3886. drbd_md_sync(device); /* update connected indicator, la_size_sect, ... */
  3887. return 0;
  3888. }
  3889. static int receive_sync_uuid(struct drbd_connection *connection, struct packet_info *pi)
  3890. {
  3891. struct drbd_peer_device *peer_device;
  3892. struct drbd_device *device;
  3893. struct p_rs_uuid *p = pi->data;
  3894. peer_device = conn_peer_device(connection, pi->vnr);
  3895. if (!peer_device)
  3896. return -EIO;
  3897. device = peer_device->device;
  3898. wait_event(device->misc_wait,
  3899. device->state.conn == C_WF_SYNC_UUID ||
  3900. device->state.conn == C_BEHIND ||
  3901. device->state.conn < C_CONNECTED ||
  3902. device->state.disk < D_NEGOTIATING);
  3903. /* D_ASSERT(device, device->state.conn == C_WF_SYNC_UUID ); */
  3904. /* Here the _drbd_uuid_ functions are right, current should
  3905. _not_ be rotated into the history */
  3906. if (get_ldev_if_state(device, D_NEGOTIATING)) {
  3907. _drbd_uuid_set(device, UI_CURRENT, be64_to_cpu(p->uuid));
  3908. _drbd_uuid_set(device, UI_BITMAP, 0UL);
  3909. drbd_print_uuids(device, "updated sync uuid");
  3910. drbd_start_resync(device, C_SYNC_TARGET);
  3911. put_ldev(device);
  3912. } else
  3913. drbd_err(device, "Ignoring SyncUUID packet!\n");
  3914. return 0;
  3915. }
  3916. /**
  3917. * receive_bitmap_plain
  3918. *
  3919. * Return 0 when done, 1 when another iteration is needed, and a negative error
  3920. * code upon failure.
  3921. */
  3922. static int
  3923. receive_bitmap_plain(struct drbd_peer_device *peer_device, unsigned int size,
  3924. unsigned long *p, struct bm_xfer_ctx *c)
  3925. {
  3926. unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE -
  3927. drbd_header_size(peer_device->connection);
  3928. unsigned int num_words = min_t(size_t, data_size / sizeof(*p),
  3929. c->bm_words - c->word_offset);
  3930. unsigned int want = num_words * sizeof(*p);
  3931. int err;
  3932. if (want != size) {
  3933. drbd_err(peer_device, "%s:want (%u) != size (%u)\n", __func__, want, size);
  3934. return -EIO;
  3935. }
  3936. if (want == 0)
  3937. return 0;
  3938. err = drbd_recv_all(peer_device->connection, p, want);
  3939. if (err)
  3940. return err;
  3941. drbd_bm_merge_lel(peer_device->device, c->word_offset, num_words, p);
  3942. c->word_offset += num_words;
  3943. c->bit_offset = c->word_offset * BITS_PER_LONG;
  3944. if (c->bit_offset > c->bm_bits)
  3945. c->bit_offset = c->bm_bits;
  3946. return 1;
  3947. }
  3948. static enum drbd_bitmap_code dcbp_get_code(struct p_compressed_bm *p)
  3949. {
  3950. return (enum drbd_bitmap_code)(p->encoding & 0x0f);
  3951. }
  3952. static int dcbp_get_start(struct p_compressed_bm *p)
  3953. {
  3954. return (p->encoding & 0x80) != 0;
  3955. }
  3956. static int dcbp_get_pad_bits(struct p_compressed_bm *p)
  3957. {
  3958. return (p->encoding >> 4) & 0x7;
  3959. }
  3960. /**
  3961. * recv_bm_rle_bits
  3962. *
  3963. * Return 0 when done, 1 when another iteration is needed, and a negative error
  3964. * code upon failure.
  3965. */
  3966. static int
  3967. recv_bm_rle_bits(struct drbd_peer_device *peer_device,
  3968. struct p_compressed_bm *p,
  3969. struct bm_xfer_ctx *c,
  3970. unsigned int len)
  3971. {
  3972. struct bitstream bs;
  3973. u64 look_ahead;
  3974. u64 rl;
  3975. u64 tmp;
  3976. unsigned long s = c->bit_offset;
  3977. unsigned long e;
  3978. int toggle = dcbp_get_start(p);
  3979. int have;
  3980. int bits;
  3981. bitstream_init(&bs, p->code, len, dcbp_get_pad_bits(p));
  3982. bits = bitstream_get_bits(&bs, &look_ahead, 64);
  3983. if (bits < 0)
  3984. return -EIO;
  3985. for (have = bits; have > 0; s += rl, toggle = !toggle) {
  3986. bits = vli_decode_bits(&rl, look_ahead);
  3987. if (bits <= 0)
  3988. return -EIO;
  3989. if (toggle) {
  3990. e = s + rl -1;
  3991. if (e >= c->bm_bits) {
  3992. drbd_err(peer_device, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
  3993. return -EIO;
  3994. }
  3995. _drbd_bm_set_bits(peer_device->device, s, e);
  3996. }
  3997. if (have < bits) {
  3998. drbd_err(peer_device, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
  3999. have, bits, look_ahead,
  4000. (unsigned int)(bs.cur.b - p->code),
  4001. (unsigned int)bs.buf_len);
  4002. return -EIO;
  4003. }
  4004. /* if we consumed all 64 bits, assign 0; >> 64 is "undefined"; */
  4005. if (likely(bits < 64))
  4006. look_ahead >>= bits;
  4007. else
  4008. look_ahead = 0;
  4009. have -= bits;
  4010. bits = bitstream_get_bits(&bs, &tmp, 64 - have);
  4011. if (bits < 0)
  4012. return -EIO;
  4013. look_ahead |= tmp << have;
  4014. have += bits;
  4015. }
  4016. c->bit_offset = s;
  4017. bm_xfer_ctx_bit_to_word_offset(c);
  4018. return (s != c->bm_bits);
  4019. }
  4020. /**
  4021. * decode_bitmap_c
  4022. *
  4023. * Return 0 when done, 1 when another iteration is needed, and a negative error
  4024. * code upon failure.
  4025. */
  4026. static int
  4027. decode_bitmap_c(struct drbd_peer_device *peer_device,
  4028. struct p_compressed_bm *p,
  4029. struct bm_xfer_ctx *c,
  4030. unsigned int len)
  4031. {
  4032. if (dcbp_get_code(p) == RLE_VLI_Bits)
  4033. return recv_bm_rle_bits(peer_device, p, c, len - sizeof(*p));
  4034. /* other variants had been implemented for evaluation,
  4035. * but have been dropped as this one turned out to be "best"
  4036. * during all our tests. */
  4037. drbd_err(peer_device, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
  4038. conn_request_state(peer_device->connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
  4039. return -EIO;
  4040. }
  4041. void INFO_bm_xfer_stats(struct drbd_device *device,
  4042. const char *direction, struct bm_xfer_ctx *c)
  4043. {
  4044. /* what would it take to transfer it "plaintext" */
  4045. unsigned int header_size = drbd_header_size(first_peer_device(device)->connection);
  4046. unsigned int data_size = DRBD_SOCKET_BUFFER_SIZE - header_size;
  4047. unsigned int plain =
  4048. header_size * (DIV_ROUND_UP(c->bm_words, data_size) + 1) +
  4049. c->bm_words * sizeof(unsigned long);
  4050. unsigned int total = c->bytes[0] + c->bytes[1];
  4051. unsigned int r;
  4052. /* total can not be zero. but just in case: */
  4053. if (total == 0)
  4054. return;
  4055. /* don't report if not compressed */
  4056. if (total >= plain)
  4057. return;
  4058. /* total < plain. check for overflow, still */
  4059. r = (total > UINT_MAX/1000) ? (total / (plain/1000))
  4060. : (1000 * total / plain);
  4061. if (r > 1000)
  4062. r = 1000;
  4063. r = 1000 - r;
  4064. drbd_info(device, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
  4065. "total %u; compression: %u.%u%%\n",
  4066. direction,
  4067. c->bytes[1], c->packets[1],
  4068. c->bytes[0], c->packets[0],
  4069. total, r/10, r % 10);
  4070. }
  4071. /* Since we are processing the bitfield from lower addresses to higher,
  4072. it does not matter if the process it in 32 bit chunks or 64 bit
  4073. chunks as long as it is little endian. (Understand it as byte stream,
  4074. beginning with the lowest byte...) If we would use big endian
  4075. we would need to process it from the highest address to the lowest,
  4076. in order to be agnostic to the 32 vs 64 bits issue.
  4077. returns 0 on failure, 1 if we successfully received it. */
  4078. static int receive_bitmap(struct drbd_connection *connection, struct packet_info *pi)
  4079. {
  4080. struct drbd_peer_device *peer_device;
  4081. struct drbd_device *device;
  4082. struct bm_xfer_ctx c;
  4083. int err;
  4084. peer_device = conn_peer_device(connection, pi->vnr);
  4085. if (!peer_device)
  4086. return -EIO;
  4087. device = peer_device->device;
  4088. drbd_bm_lock(device, "receive bitmap", BM_LOCKED_SET_ALLOWED);
  4089. /* you are supposed to send additional out-of-sync information
  4090. * if you actually set bits during this phase */
  4091. c = (struct bm_xfer_ctx) {
  4092. .bm_bits = drbd_bm_bits(device),
  4093. .bm_words = drbd_bm_words(device),
  4094. };
  4095. for(;;) {
  4096. if (pi->cmd == P_BITMAP)
  4097. err = receive_bitmap_plain(peer_device, pi->size, pi->data, &c);
  4098. else if (pi->cmd == P_COMPRESSED_BITMAP) {
  4099. /* MAYBE: sanity check that we speak proto >= 90,
  4100. * and the feature is enabled! */
  4101. struct p_compressed_bm *p = pi->data;
  4102. if (pi->size > DRBD_SOCKET_BUFFER_SIZE - drbd_header_size(connection)) {
  4103. drbd_err(device, "ReportCBitmap packet too large\n");
  4104. err = -EIO;
  4105. goto out;
  4106. }
  4107. if (pi->size <= sizeof(*p)) {
  4108. drbd_err(device, "ReportCBitmap packet too small (l:%u)\n", pi->size);
  4109. err = -EIO;
  4110. goto out;
  4111. }
  4112. err = drbd_recv_all(peer_device->connection, p, pi->size);
  4113. if (err)
  4114. goto out;
  4115. err = decode_bitmap_c(peer_device, p, &c, pi->size);
  4116. } else {
  4117. drbd_warn(device, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", pi->cmd);
  4118. err = -EIO;
  4119. goto out;
  4120. }
  4121. c.packets[pi->cmd == P_BITMAP]++;
  4122. c.bytes[pi->cmd == P_BITMAP] += drbd_header_size(connection) + pi->size;
  4123. if (err <= 0) {
  4124. if (err < 0)
  4125. goto out;
  4126. break;
  4127. }
  4128. err = drbd_recv_header(peer_device->connection, pi);
  4129. if (err)
  4130. goto out;
  4131. }
  4132. INFO_bm_xfer_stats(device, "receive", &c);
  4133. if (device->state.conn == C_WF_BITMAP_T) {
  4134. enum drbd_state_rv rv;
  4135. err = drbd_send_bitmap(device);
  4136. if (err)
  4137. goto out;
  4138. /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
  4139. rv = _drbd_request_state(device, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
  4140. D_ASSERT(device, rv == SS_SUCCESS);
  4141. } else if (device->state.conn != C_WF_BITMAP_S) {
  4142. /* admin may have requested C_DISCONNECTING,
  4143. * other threads may have noticed network errors */
  4144. drbd_info(device, "unexpected cstate (%s) in receive_bitmap\n",
  4145. drbd_conn_str(device->state.conn));
  4146. }
  4147. err = 0;
  4148. out:
  4149. drbd_bm_unlock(device);
  4150. if (!err && device->state.conn == C_WF_BITMAP_S)
  4151. drbd_start_resync(device, C_SYNC_SOURCE);
  4152. return err;
  4153. }
  4154. static int receive_skip(struct drbd_connection *connection, struct packet_info *pi)
  4155. {
  4156. drbd_warn(connection, "skipping unknown optional packet type %d, l: %d!\n",
  4157. pi->cmd, pi->size);
  4158. return ignore_remaining_packet(connection, pi);
  4159. }
  4160. static int receive_UnplugRemote(struct drbd_connection *connection, struct packet_info *pi)
  4161. {
  4162. /* Make sure we've acked all the TCP data associated
  4163. * with the data requests being unplugged */
  4164. drbd_tcp_quickack(connection->data.socket);
  4165. return 0;
  4166. }
  4167. static int receive_out_of_sync(struct drbd_connection *connection, struct packet_info *pi)
  4168. {
  4169. struct drbd_peer_device *peer_device;
  4170. struct drbd_device *device;
  4171. struct p_block_desc *p = pi->data;
  4172. peer_device = conn_peer_device(connection, pi->vnr);
  4173. if (!peer_device)
  4174. return -EIO;
  4175. device = peer_device->device;
  4176. switch (device->state.conn) {
  4177. case C_WF_SYNC_UUID:
  4178. case C_WF_BITMAP_T:
  4179. case C_BEHIND:
  4180. break;
  4181. default:
  4182. drbd_err(device, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
  4183. drbd_conn_str(device->state.conn));
  4184. }
  4185. drbd_set_out_of_sync(device, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
  4186. return 0;
  4187. }
  4188. static int receive_rs_deallocated(struct drbd_connection *connection, struct packet_info *pi)
  4189. {
  4190. struct drbd_peer_device *peer_device;
  4191. struct p_block_desc *p = pi->data;
  4192. struct drbd_device *device;
  4193. sector_t sector;
  4194. int size, err = 0;
  4195. peer_device = conn_peer_device(connection, pi->vnr);
  4196. if (!peer_device)
  4197. return -EIO;
  4198. device = peer_device->device;
  4199. sector = be64_to_cpu(p->sector);
  4200. size = be32_to_cpu(p->blksize);
  4201. dec_rs_pending(device);
  4202. if (get_ldev(device)) {
  4203. struct drbd_peer_request *peer_req;
  4204. const int op = REQ_OP_WRITE_ZEROES;
  4205. peer_req = drbd_alloc_peer_req(peer_device, ID_SYNCER, sector,
  4206. size, 0, GFP_NOIO);
  4207. if (!peer_req) {
  4208. put_ldev(device);
  4209. return -ENOMEM;
  4210. }
  4211. peer_req->w.cb = e_end_resync_block;
  4212. peer_req->submit_jif = jiffies;
  4213. peer_req->flags |= EE_IS_TRIM;
  4214. spin_lock_irq(&device->resource->req_lock);
  4215. list_add_tail(&peer_req->w.list, &device->sync_ee);
  4216. spin_unlock_irq(&device->resource->req_lock);
  4217. atomic_add(pi->size >> 9, &device->rs_sect_ev);
  4218. err = drbd_submit_peer_request(device, peer_req, op, 0, DRBD_FAULT_RS_WR);
  4219. if (err) {
  4220. spin_lock_irq(&device->resource->req_lock);
  4221. list_del(&peer_req->w.list);
  4222. spin_unlock_irq(&device->resource->req_lock);
  4223. drbd_free_peer_req(device, peer_req);
  4224. put_ldev(device);
  4225. err = 0;
  4226. goto fail;
  4227. }
  4228. inc_unacked(device);
  4229. /* No put_ldev() here. Gets called in drbd_endio_write_sec_final(),
  4230. as well as drbd_rs_complete_io() */
  4231. } else {
  4232. fail:
  4233. drbd_rs_complete_io(device, sector);
  4234. drbd_send_ack_ex(peer_device, P_NEG_ACK, sector, size, ID_SYNCER);
  4235. }
  4236. atomic_add(size >> 9, &device->rs_sect_in);
  4237. return err;
  4238. }
  4239. struct data_cmd {
  4240. int expect_payload;
  4241. unsigned int pkt_size;
  4242. int (*fn)(struct drbd_connection *, struct packet_info *);
  4243. };
  4244. static struct data_cmd drbd_cmd_handler[] = {
  4245. [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
  4246. [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
  4247. [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
  4248. [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
  4249. [P_BITMAP] = { 1, 0, receive_bitmap } ,
  4250. [P_COMPRESSED_BITMAP] = { 1, 0, receive_bitmap } ,
  4251. [P_UNPLUG_REMOTE] = { 0, 0, receive_UnplugRemote },
  4252. [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
  4253. [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
  4254. [P_SYNC_PARAM] = { 1, 0, receive_SyncParam },
  4255. [P_SYNC_PARAM89] = { 1, 0, receive_SyncParam },
  4256. [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
  4257. [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
  4258. [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
  4259. [P_STATE] = { 0, sizeof(struct p_state), receive_state },
  4260. [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
  4261. [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
  4262. [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
  4263. [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
  4264. [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
  4265. [P_RS_THIN_REQ] = { 0, sizeof(struct p_block_req), receive_DataRequest },
  4266. [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
  4267. [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
  4268. [P_CONN_ST_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_conn_state },
  4269. [P_PROTOCOL_UPDATE] = { 1, sizeof(struct p_protocol), receive_protocol },
  4270. [P_TRIM] = { 0, sizeof(struct p_trim), receive_Data },
  4271. [P_RS_DEALLOCATED] = { 0, sizeof(struct p_block_desc), receive_rs_deallocated },
  4272. [P_WSAME] = { 1, sizeof(struct p_wsame), receive_Data },
  4273. };
  4274. static void drbdd(struct drbd_connection *connection)
  4275. {
  4276. struct packet_info pi;
  4277. size_t shs; /* sub header size */
  4278. int err;
  4279. while (get_t_state(&connection->receiver) == RUNNING) {
  4280. struct data_cmd const *cmd;
  4281. drbd_thread_current_set_cpu(&connection->receiver);
  4282. update_receiver_timing_details(connection, drbd_recv_header_maybe_unplug);
  4283. if (drbd_recv_header_maybe_unplug(connection, &pi))
  4284. goto err_out;
  4285. cmd = &drbd_cmd_handler[pi.cmd];
  4286. if (unlikely(pi.cmd >= ARRAY_SIZE(drbd_cmd_handler) || !cmd->fn)) {
  4287. drbd_err(connection, "Unexpected data packet %s (0x%04x)",
  4288. cmdname(pi.cmd), pi.cmd);
  4289. goto err_out;
  4290. }
  4291. shs = cmd->pkt_size;
  4292. if (pi.cmd == P_SIZES && connection->agreed_features & DRBD_FF_WSAME)
  4293. shs += sizeof(struct o_qlim);
  4294. if (pi.size > shs && !cmd->expect_payload) {
  4295. drbd_err(connection, "No payload expected %s l:%d\n",
  4296. cmdname(pi.cmd), pi.size);
  4297. goto err_out;
  4298. }
  4299. if (pi.size < shs) {
  4300. drbd_err(connection, "%s: unexpected packet size, expected:%d received:%d\n",
  4301. cmdname(pi.cmd), (int)shs, pi.size);
  4302. goto err_out;
  4303. }
  4304. if (shs) {
  4305. update_receiver_timing_details(connection, drbd_recv_all_warn);
  4306. err = drbd_recv_all_warn(connection, pi.data, shs);
  4307. if (err)
  4308. goto err_out;
  4309. pi.size -= shs;
  4310. }
  4311. update_receiver_timing_details(connection, cmd->fn);
  4312. err = cmd->fn(connection, &pi);
  4313. if (err) {
  4314. drbd_err(connection, "error receiving %s, e: %d l: %d!\n",
  4315. cmdname(pi.cmd), err, pi.size);
  4316. goto err_out;
  4317. }
  4318. }
  4319. return;
  4320. err_out:
  4321. conn_request_state(connection, NS(conn, C_PROTOCOL_ERROR), CS_HARD);
  4322. }
  4323. static void conn_disconnect(struct drbd_connection *connection)
  4324. {
  4325. struct drbd_peer_device *peer_device;
  4326. enum drbd_conns oc;
  4327. int vnr;
  4328. if (connection->cstate == C_STANDALONE)
  4329. return;
  4330. /* We are about to start the cleanup after connection loss.
  4331. * Make sure drbd_make_request knows about that.
  4332. * Usually we should be in some network failure state already,
  4333. * but just in case we are not, we fix it up here.
  4334. */
  4335. conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD);
  4336. /* ack_receiver does not clean up anything. it must not interfere, either */
  4337. drbd_thread_stop(&connection->ack_receiver);
  4338. if (connection->ack_sender) {
  4339. destroy_workqueue(connection->ack_sender);
  4340. connection->ack_sender = NULL;
  4341. }
  4342. drbd_free_sock(connection);
  4343. rcu_read_lock();
  4344. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  4345. struct drbd_device *device = peer_device->device;
  4346. kref_get(&device->kref);
  4347. rcu_read_unlock();
  4348. drbd_disconnected(peer_device);
  4349. kref_put(&device->kref, drbd_destroy_device);
  4350. rcu_read_lock();
  4351. }
  4352. rcu_read_unlock();
  4353. if (!list_empty(&connection->current_epoch->list))
  4354. drbd_err(connection, "ASSERTION FAILED: connection->current_epoch->list not empty\n");
  4355. /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
  4356. atomic_set(&connection->current_epoch->epoch_size, 0);
  4357. connection->send.seen_any_write_yet = false;
  4358. drbd_info(connection, "Connection closed\n");
  4359. if (conn_highest_role(connection) == R_PRIMARY && conn_highest_pdsk(connection) >= D_UNKNOWN)
  4360. conn_try_outdate_peer_async(connection);
  4361. spin_lock_irq(&connection->resource->req_lock);
  4362. oc = connection->cstate;
  4363. if (oc >= C_UNCONNECTED)
  4364. _conn_request_state(connection, NS(conn, C_UNCONNECTED), CS_VERBOSE);
  4365. spin_unlock_irq(&connection->resource->req_lock);
  4366. if (oc == C_DISCONNECTING)
  4367. conn_request_state(connection, NS(conn, C_STANDALONE), CS_VERBOSE | CS_HARD);
  4368. }
  4369. static int drbd_disconnected(struct drbd_peer_device *peer_device)
  4370. {
  4371. struct drbd_device *device = peer_device->device;
  4372. unsigned int i;
  4373. /* wait for current activity to cease. */
  4374. spin_lock_irq(&device->resource->req_lock);
  4375. _drbd_wait_ee_list_empty(device, &device->active_ee);
  4376. _drbd_wait_ee_list_empty(device, &device->sync_ee);
  4377. _drbd_wait_ee_list_empty(device, &device->read_ee);
  4378. spin_unlock_irq(&device->resource->req_lock);
  4379. /* We do not have data structures that would allow us to
  4380. * get the rs_pending_cnt down to 0 again.
  4381. * * On C_SYNC_TARGET we do not have any data structures describing
  4382. * the pending RSDataRequest's we have sent.
  4383. * * On C_SYNC_SOURCE there is no data structure that tracks
  4384. * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
  4385. * And no, it is not the sum of the reference counts in the
  4386. * resync_LRU. The resync_LRU tracks the whole operation including
  4387. * the disk-IO, while the rs_pending_cnt only tracks the blocks
  4388. * on the fly. */
  4389. drbd_rs_cancel_all(device);
  4390. device->rs_total = 0;
  4391. device->rs_failed = 0;
  4392. atomic_set(&device->rs_pending_cnt, 0);
  4393. wake_up(&device->misc_wait);
  4394. del_timer_sync(&device->resync_timer);
  4395. resync_timer_fn(&device->resync_timer);
  4396. /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
  4397. * w_make_resync_request etc. which may still be on the worker queue
  4398. * to be "canceled" */
  4399. drbd_flush_workqueue(&peer_device->connection->sender_work);
  4400. drbd_finish_peer_reqs(device);
  4401. /* This second workqueue flush is necessary, since drbd_finish_peer_reqs()
  4402. might have issued a work again. The one before drbd_finish_peer_reqs() is
  4403. necessary to reclain net_ee in drbd_finish_peer_reqs(). */
  4404. drbd_flush_workqueue(&peer_device->connection->sender_work);
  4405. /* need to do it again, drbd_finish_peer_reqs() may have populated it
  4406. * again via drbd_try_clear_on_disk_bm(). */
  4407. drbd_rs_cancel_all(device);
  4408. kfree(device->p_uuid);
  4409. device->p_uuid = NULL;
  4410. if (!drbd_suspended(device))
  4411. tl_clear(peer_device->connection);
  4412. drbd_md_sync(device);
  4413. if (get_ldev(device)) {
  4414. drbd_bitmap_io(device, &drbd_bm_write_copy_pages,
  4415. "write from disconnected", BM_LOCKED_CHANGE_ALLOWED);
  4416. put_ldev(device);
  4417. }
  4418. /* tcp_close and release of sendpage pages can be deferred. I don't
  4419. * want to use SO_LINGER, because apparently it can be deferred for
  4420. * more than 20 seconds (longest time I checked).
  4421. *
  4422. * Actually we don't care for exactly when the network stack does its
  4423. * put_page(), but release our reference on these pages right here.
  4424. */
  4425. i = drbd_free_peer_reqs(device, &device->net_ee);
  4426. if (i)
  4427. drbd_info(device, "net_ee not empty, killed %u entries\n", i);
  4428. i = atomic_read(&device->pp_in_use_by_net);
  4429. if (i)
  4430. drbd_info(device, "pp_in_use_by_net = %d, expected 0\n", i);
  4431. i = atomic_read(&device->pp_in_use);
  4432. if (i)
  4433. drbd_info(device, "pp_in_use = %d, expected 0\n", i);
  4434. D_ASSERT(device, list_empty(&device->read_ee));
  4435. D_ASSERT(device, list_empty(&device->active_ee));
  4436. D_ASSERT(device, list_empty(&device->sync_ee));
  4437. D_ASSERT(device, list_empty(&device->done_ee));
  4438. return 0;
  4439. }
  4440. /*
  4441. * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
  4442. * we can agree on is stored in agreed_pro_version.
  4443. *
  4444. * feature flags and the reserved array should be enough room for future
  4445. * enhancements of the handshake protocol, and possible plugins...
  4446. *
  4447. * for now, they are expected to be zero, but ignored.
  4448. */
  4449. static int drbd_send_features(struct drbd_connection *connection)
  4450. {
  4451. struct drbd_socket *sock;
  4452. struct p_connection_features *p;
  4453. sock = &connection->data;
  4454. p = conn_prepare_command(connection, sock);
  4455. if (!p)
  4456. return -EIO;
  4457. memset(p, 0, sizeof(*p));
  4458. p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
  4459. p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
  4460. p->feature_flags = cpu_to_be32(PRO_FEATURES);
  4461. return conn_send_command(connection, sock, P_CONNECTION_FEATURES, sizeof(*p), NULL, 0);
  4462. }
  4463. /*
  4464. * return values:
  4465. * 1 yes, we have a valid connection
  4466. * 0 oops, did not work out, please try again
  4467. * -1 peer talks different language,
  4468. * no point in trying again, please go standalone.
  4469. */
  4470. static int drbd_do_features(struct drbd_connection *connection)
  4471. {
  4472. /* ASSERT current == connection->receiver ... */
  4473. struct p_connection_features *p;
  4474. const int expect = sizeof(struct p_connection_features);
  4475. struct packet_info pi;
  4476. int err;
  4477. err = drbd_send_features(connection);
  4478. if (err)
  4479. return 0;
  4480. err = drbd_recv_header(connection, &pi);
  4481. if (err)
  4482. return 0;
  4483. if (pi.cmd != P_CONNECTION_FEATURES) {
  4484. drbd_err(connection, "expected ConnectionFeatures packet, received: %s (0x%04x)\n",
  4485. cmdname(pi.cmd), pi.cmd);
  4486. return -1;
  4487. }
  4488. if (pi.size != expect) {
  4489. drbd_err(connection, "expected ConnectionFeatures length: %u, received: %u\n",
  4490. expect, pi.size);
  4491. return -1;
  4492. }
  4493. p = pi.data;
  4494. err = drbd_recv_all_warn(connection, p, expect);
  4495. if (err)
  4496. return 0;
  4497. p->protocol_min = be32_to_cpu(p->protocol_min);
  4498. p->protocol_max = be32_to_cpu(p->protocol_max);
  4499. if (p->protocol_max == 0)
  4500. p->protocol_max = p->protocol_min;
  4501. if (PRO_VERSION_MAX < p->protocol_min ||
  4502. PRO_VERSION_MIN > p->protocol_max)
  4503. goto incompat;
  4504. connection->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
  4505. connection->agreed_features = PRO_FEATURES & be32_to_cpu(p->feature_flags);
  4506. drbd_info(connection, "Handshake successful: "
  4507. "Agreed network protocol version %d\n", connection->agreed_pro_version);
  4508. drbd_info(connection, "Feature flags enabled on protocol level: 0x%x%s%s%s.\n",
  4509. connection->agreed_features,
  4510. connection->agreed_features & DRBD_FF_TRIM ? " TRIM" : "",
  4511. connection->agreed_features & DRBD_FF_THIN_RESYNC ? " THIN_RESYNC" : "",
  4512. connection->agreed_features & DRBD_FF_WSAME ? " WRITE_SAME" :
  4513. connection->agreed_features ? "" : " none");
  4514. return 1;
  4515. incompat:
  4516. drbd_err(connection, "incompatible DRBD dialects: "
  4517. "I support %d-%d, peer supports %d-%d\n",
  4518. PRO_VERSION_MIN, PRO_VERSION_MAX,
  4519. p->protocol_min, p->protocol_max);
  4520. return -1;
  4521. }
  4522. #if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
  4523. static int drbd_do_auth(struct drbd_connection *connection)
  4524. {
  4525. drbd_err(connection, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
  4526. drbd_err(connection, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
  4527. return -1;
  4528. }
  4529. #else
  4530. #define CHALLENGE_LEN 64
  4531. /* Return value:
  4532. 1 - auth succeeded,
  4533. 0 - failed, try again (network error),
  4534. -1 - auth failed, don't try again.
  4535. */
  4536. static int drbd_do_auth(struct drbd_connection *connection)
  4537. {
  4538. struct drbd_socket *sock;
  4539. char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
  4540. char *response = NULL;
  4541. char *right_response = NULL;
  4542. char *peers_ch = NULL;
  4543. unsigned int key_len;
  4544. char secret[SHARED_SECRET_MAX]; /* 64 byte */
  4545. unsigned int resp_size;
  4546. struct shash_desc *desc;
  4547. struct packet_info pi;
  4548. struct net_conf *nc;
  4549. int err, rv;
  4550. /* FIXME: Put the challenge/response into the preallocated socket buffer. */
  4551. rcu_read_lock();
  4552. nc = rcu_dereference(connection->net_conf);
  4553. key_len = strlen(nc->shared_secret);
  4554. memcpy(secret, nc->shared_secret, key_len);
  4555. rcu_read_unlock();
  4556. desc = kmalloc(sizeof(struct shash_desc) +
  4557. crypto_shash_descsize(connection->cram_hmac_tfm),
  4558. GFP_KERNEL);
  4559. if (!desc) {
  4560. rv = -1;
  4561. goto fail;
  4562. }
  4563. desc->tfm = connection->cram_hmac_tfm;
  4564. desc->flags = 0;
  4565. rv = crypto_shash_setkey(connection->cram_hmac_tfm, (u8 *)secret, key_len);
  4566. if (rv) {
  4567. drbd_err(connection, "crypto_shash_setkey() failed with %d\n", rv);
  4568. rv = -1;
  4569. goto fail;
  4570. }
  4571. get_random_bytes(my_challenge, CHALLENGE_LEN);
  4572. sock = &connection->data;
  4573. if (!conn_prepare_command(connection, sock)) {
  4574. rv = 0;
  4575. goto fail;
  4576. }
  4577. rv = !conn_send_command(connection, sock, P_AUTH_CHALLENGE, 0,
  4578. my_challenge, CHALLENGE_LEN);
  4579. if (!rv)
  4580. goto fail;
  4581. err = drbd_recv_header(connection, &pi);
  4582. if (err) {
  4583. rv = 0;
  4584. goto fail;
  4585. }
  4586. if (pi.cmd != P_AUTH_CHALLENGE) {
  4587. drbd_err(connection, "expected AuthChallenge packet, received: %s (0x%04x)\n",
  4588. cmdname(pi.cmd), pi.cmd);
  4589. rv = 0;
  4590. goto fail;
  4591. }
  4592. if (pi.size > CHALLENGE_LEN * 2) {
  4593. drbd_err(connection, "expected AuthChallenge payload too big.\n");
  4594. rv = -1;
  4595. goto fail;
  4596. }
  4597. if (pi.size < CHALLENGE_LEN) {
  4598. drbd_err(connection, "AuthChallenge payload too small.\n");
  4599. rv = -1;
  4600. goto fail;
  4601. }
  4602. peers_ch = kmalloc(pi.size, GFP_NOIO);
  4603. if (peers_ch == NULL) {
  4604. drbd_err(connection, "kmalloc of peers_ch failed\n");
  4605. rv = -1;
  4606. goto fail;
  4607. }
  4608. err = drbd_recv_all_warn(connection, peers_ch, pi.size);
  4609. if (err) {
  4610. rv = 0;
  4611. goto fail;
  4612. }
  4613. if (!memcmp(my_challenge, peers_ch, CHALLENGE_LEN)) {
  4614. drbd_err(connection, "Peer presented the same challenge!\n");
  4615. rv = -1;
  4616. goto fail;
  4617. }
  4618. resp_size = crypto_shash_digestsize(connection->cram_hmac_tfm);
  4619. response = kmalloc(resp_size, GFP_NOIO);
  4620. if (response == NULL) {
  4621. drbd_err(connection, "kmalloc of response failed\n");
  4622. rv = -1;
  4623. goto fail;
  4624. }
  4625. rv = crypto_shash_digest(desc, peers_ch, pi.size, response);
  4626. if (rv) {
  4627. drbd_err(connection, "crypto_hash_digest() failed with %d\n", rv);
  4628. rv = -1;
  4629. goto fail;
  4630. }
  4631. if (!conn_prepare_command(connection, sock)) {
  4632. rv = 0;
  4633. goto fail;
  4634. }
  4635. rv = !conn_send_command(connection, sock, P_AUTH_RESPONSE, 0,
  4636. response, resp_size);
  4637. if (!rv)
  4638. goto fail;
  4639. err = drbd_recv_header(connection, &pi);
  4640. if (err) {
  4641. rv = 0;
  4642. goto fail;
  4643. }
  4644. if (pi.cmd != P_AUTH_RESPONSE) {
  4645. drbd_err(connection, "expected AuthResponse packet, received: %s (0x%04x)\n",
  4646. cmdname(pi.cmd), pi.cmd);
  4647. rv = 0;
  4648. goto fail;
  4649. }
  4650. if (pi.size != resp_size) {
  4651. drbd_err(connection, "expected AuthResponse payload of wrong size\n");
  4652. rv = 0;
  4653. goto fail;
  4654. }
  4655. err = drbd_recv_all_warn(connection, response , resp_size);
  4656. if (err) {
  4657. rv = 0;
  4658. goto fail;
  4659. }
  4660. right_response = kmalloc(resp_size, GFP_NOIO);
  4661. if (right_response == NULL) {
  4662. drbd_err(connection, "kmalloc of right_response failed\n");
  4663. rv = -1;
  4664. goto fail;
  4665. }
  4666. rv = crypto_shash_digest(desc, my_challenge, CHALLENGE_LEN,
  4667. right_response);
  4668. if (rv) {
  4669. drbd_err(connection, "crypto_hash_digest() failed with %d\n", rv);
  4670. rv = -1;
  4671. goto fail;
  4672. }
  4673. rv = !memcmp(response, right_response, resp_size);
  4674. if (rv)
  4675. drbd_info(connection, "Peer authenticated using %d bytes HMAC\n",
  4676. resp_size);
  4677. else
  4678. rv = -1;
  4679. fail:
  4680. kfree(peers_ch);
  4681. kfree(response);
  4682. kfree(right_response);
  4683. if (desc) {
  4684. shash_desc_zero(desc);
  4685. kfree(desc);
  4686. }
  4687. return rv;
  4688. }
  4689. #endif
  4690. int drbd_receiver(struct drbd_thread *thi)
  4691. {
  4692. struct drbd_connection *connection = thi->connection;
  4693. int h;
  4694. drbd_info(connection, "receiver (re)started\n");
  4695. do {
  4696. h = conn_connect(connection);
  4697. if (h == 0) {
  4698. conn_disconnect(connection);
  4699. schedule_timeout_interruptible(HZ);
  4700. }
  4701. if (h == -1) {
  4702. drbd_warn(connection, "Discarding network configuration.\n");
  4703. conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
  4704. }
  4705. } while (h == 0);
  4706. if (h > 0) {
  4707. blk_start_plug(&connection->receiver_plug);
  4708. drbdd(connection);
  4709. blk_finish_plug(&connection->receiver_plug);
  4710. }
  4711. conn_disconnect(connection);
  4712. drbd_info(connection, "receiver terminated\n");
  4713. return 0;
  4714. }
  4715. /* ********* acknowledge sender ******** */
  4716. static int got_conn_RqSReply(struct drbd_connection *connection, struct packet_info *pi)
  4717. {
  4718. struct p_req_state_reply *p = pi->data;
  4719. int retcode = be32_to_cpu(p->retcode);
  4720. if (retcode >= SS_SUCCESS) {
  4721. set_bit(CONN_WD_ST_CHG_OKAY, &connection->flags);
  4722. } else {
  4723. set_bit(CONN_WD_ST_CHG_FAIL, &connection->flags);
  4724. drbd_err(connection, "Requested state change failed by peer: %s (%d)\n",
  4725. drbd_set_st_err_str(retcode), retcode);
  4726. }
  4727. wake_up(&connection->ping_wait);
  4728. return 0;
  4729. }
  4730. static int got_RqSReply(struct drbd_connection *connection, struct packet_info *pi)
  4731. {
  4732. struct drbd_peer_device *peer_device;
  4733. struct drbd_device *device;
  4734. struct p_req_state_reply *p = pi->data;
  4735. int retcode = be32_to_cpu(p->retcode);
  4736. peer_device = conn_peer_device(connection, pi->vnr);
  4737. if (!peer_device)
  4738. return -EIO;
  4739. device = peer_device->device;
  4740. if (test_bit(CONN_WD_ST_CHG_REQ, &connection->flags)) {
  4741. D_ASSERT(device, connection->agreed_pro_version < 100);
  4742. return got_conn_RqSReply(connection, pi);
  4743. }
  4744. if (retcode >= SS_SUCCESS) {
  4745. set_bit(CL_ST_CHG_SUCCESS, &device->flags);
  4746. } else {
  4747. set_bit(CL_ST_CHG_FAIL, &device->flags);
  4748. drbd_err(device, "Requested state change failed by peer: %s (%d)\n",
  4749. drbd_set_st_err_str(retcode), retcode);
  4750. }
  4751. wake_up(&device->state_wait);
  4752. return 0;
  4753. }
  4754. static int got_Ping(struct drbd_connection *connection, struct packet_info *pi)
  4755. {
  4756. return drbd_send_ping_ack(connection);
  4757. }
  4758. static int got_PingAck(struct drbd_connection *connection, struct packet_info *pi)
  4759. {
  4760. /* restore idle timeout */
  4761. connection->meta.socket->sk->sk_rcvtimeo = connection->net_conf->ping_int*HZ;
  4762. if (!test_and_set_bit(GOT_PING_ACK, &connection->flags))
  4763. wake_up(&connection->ping_wait);
  4764. return 0;
  4765. }
  4766. static int got_IsInSync(struct drbd_connection *connection, struct packet_info *pi)
  4767. {
  4768. struct drbd_peer_device *peer_device;
  4769. struct drbd_device *device;
  4770. struct p_block_ack *p = pi->data;
  4771. sector_t sector = be64_to_cpu(p->sector);
  4772. int blksize = be32_to_cpu(p->blksize);
  4773. peer_device = conn_peer_device(connection, pi->vnr);
  4774. if (!peer_device)
  4775. return -EIO;
  4776. device = peer_device->device;
  4777. D_ASSERT(device, peer_device->connection->agreed_pro_version >= 89);
  4778. update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
  4779. if (get_ldev(device)) {
  4780. drbd_rs_complete_io(device, sector);
  4781. drbd_set_in_sync(device, sector, blksize);
  4782. /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
  4783. device->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
  4784. put_ldev(device);
  4785. }
  4786. dec_rs_pending(device);
  4787. atomic_add(blksize >> 9, &device->rs_sect_in);
  4788. return 0;
  4789. }
  4790. static int
  4791. validate_req_change_req_state(struct drbd_device *device, u64 id, sector_t sector,
  4792. struct rb_root *root, const char *func,
  4793. enum drbd_req_event what, bool missing_ok)
  4794. {
  4795. struct drbd_request *req;
  4796. struct bio_and_error m;
  4797. spin_lock_irq(&device->resource->req_lock);
  4798. req = find_request(device, root, id, sector, missing_ok, func);
  4799. if (unlikely(!req)) {
  4800. spin_unlock_irq(&device->resource->req_lock);
  4801. return -EIO;
  4802. }
  4803. __req_mod(req, what, &m);
  4804. spin_unlock_irq(&device->resource->req_lock);
  4805. if (m.bio)
  4806. complete_master_bio(device, &m);
  4807. return 0;
  4808. }
  4809. static int got_BlockAck(struct drbd_connection *connection, struct packet_info *pi)
  4810. {
  4811. struct drbd_peer_device *peer_device;
  4812. struct drbd_device *device;
  4813. struct p_block_ack *p = pi->data;
  4814. sector_t sector = be64_to_cpu(p->sector);
  4815. int blksize = be32_to_cpu(p->blksize);
  4816. enum drbd_req_event what;
  4817. peer_device = conn_peer_device(connection, pi->vnr);
  4818. if (!peer_device)
  4819. return -EIO;
  4820. device = peer_device->device;
  4821. update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
  4822. if (p->block_id == ID_SYNCER) {
  4823. drbd_set_in_sync(device, sector, blksize);
  4824. dec_rs_pending(device);
  4825. return 0;
  4826. }
  4827. switch (pi->cmd) {
  4828. case P_RS_WRITE_ACK:
  4829. what = WRITE_ACKED_BY_PEER_AND_SIS;
  4830. break;
  4831. case P_WRITE_ACK:
  4832. what = WRITE_ACKED_BY_PEER;
  4833. break;
  4834. case P_RECV_ACK:
  4835. what = RECV_ACKED_BY_PEER;
  4836. break;
  4837. case P_SUPERSEDED:
  4838. what = CONFLICT_RESOLVED;
  4839. break;
  4840. case P_RETRY_WRITE:
  4841. what = POSTPONE_WRITE;
  4842. break;
  4843. default:
  4844. BUG();
  4845. }
  4846. return validate_req_change_req_state(device, p->block_id, sector,
  4847. &device->write_requests, __func__,
  4848. what, false);
  4849. }
  4850. static int got_NegAck(struct drbd_connection *connection, struct packet_info *pi)
  4851. {
  4852. struct drbd_peer_device *peer_device;
  4853. struct drbd_device *device;
  4854. struct p_block_ack *p = pi->data;
  4855. sector_t sector = be64_to_cpu(p->sector);
  4856. int size = be32_to_cpu(p->blksize);
  4857. int err;
  4858. peer_device = conn_peer_device(connection, pi->vnr);
  4859. if (!peer_device)
  4860. return -EIO;
  4861. device = peer_device->device;
  4862. update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
  4863. if (p->block_id == ID_SYNCER) {
  4864. dec_rs_pending(device);
  4865. drbd_rs_failed_io(device, sector, size);
  4866. return 0;
  4867. }
  4868. err = validate_req_change_req_state(device, p->block_id, sector,
  4869. &device->write_requests, __func__,
  4870. NEG_ACKED, true);
  4871. if (err) {
  4872. /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
  4873. The master bio might already be completed, therefore the
  4874. request is no longer in the collision hash. */
  4875. /* In Protocol B we might already have got a P_RECV_ACK
  4876. but then get a P_NEG_ACK afterwards. */
  4877. drbd_set_out_of_sync(device, sector, size);
  4878. }
  4879. return 0;
  4880. }
  4881. static int got_NegDReply(struct drbd_connection *connection, struct packet_info *pi)
  4882. {
  4883. struct drbd_peer_device *peer_device;
  4884. struct drbd_device *device;
  4885. struct p_block_ack *p = pi->data;
  4886. sector_t sector = be64_to_cpu(p->sector);
  4887. peer_device = conn_peer_device(connection, pi->vnr);
  4888. if (!peer_device)
  4889. return -EIO;
  4890. device = peer_device->device;
  4891. update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
  4892. drbd_err(device, "Got NegDReply; Sector %llus, len %u.\n",
  4893. (unsigned long long)sector, be32_to_cpu(p->blksize));
  4894. return validate_req_change_req_state(device, p->block_id, sector,
  4895. &device->read_requests, __func__,
  4896. NEG_ACKED, false);
  4897. }
  4898. static int got_NegRSDReply(struct drbd_connection *connection, struct packet_info *pi)
  4899. {
  4900. struct drbd_peer_device *peer_device;
  4901. struct drbd_device *device;
  4902. sector_t sector;
  4903. int size;
  4904. struct p_block_ack *p = pi->data;
  4905. peer_device = conn_peer_device(connection, pi->vnr);
  4906. if (!peer_device)
  4907. return -EIO;
  4908. device = peer_device->device;
  4909. sector = be64_to_cpu(p->sector);
  4910. size = be32_to_cpu(p->blksize);
  4911. update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
  4912. dec_rs_pending(device);
  4913. if (get_ldev_if_state(device, D_FAILED)) {
  4914. drbd_rs_complete_io(device, sector);
  4915. switch (pi->cmd) {
  4916. case P_NEG_RS_DREPLY:
  4917. drbd_rs_failed_io(device, sector, size);
  4918. case P_RS_CANCEL:
  4919. break;
  4920. default:
  4921. BUG();
  4922. }
  4923. put_ldev(device);
  4924. }
  4925. return 0;
  4926. }
  4927. static int got_BarrierAck(struct drbd_connection *connection, struct packet_info *pi)
  4928. {
  4929. struct p_barrier_ack *p = pi->data;
  4930. struct drbd_peer_device *peer_device;
  4931. int vnr;
  4932. tl_release(connection, p->barrier, be32_to_cpu(p->set_size));
  4933. rcu_read_lock();
  4934. idr_for_each_entry(&connection->peer_devices, peer_device, vnr) {
  4935. struct drbd_device *device = peer_device->device;
  4936. if (device->state.conn == C_AHEAD &&
  4937. atomic_read(&device->ap_in_flight) == 0 &&
  4938. !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &device->flags)) {
  4939. device->start_resync_timer.expires = jiffies + HZ;
  4940. add_timer(&device->start_resync_timer);
  4941. }
  4942. }
  4943. rcu_read_unlock();
  4944. return 0;
  4945. }
  4946. static int got_OVResult(struct drbd_connection *connection, struct packet_info *pi)
  4947. {
  4948. struct drbd_peer_device *peer_device;
  4949. struct drbd_device *device;
  4950. struct p_block_ack *p = pi->data;
  4951. struct drbd_device_work *dw;
  4952. sector_t sector;
  4953. int size;
  4954. peer_device = conn_peer_device(connection, pi->vnr);
  4955. if (!peer_device)
  4956. return -EIO;
  4957. device = peer_device->device;
  4958. sector = be64_to_cpu(p->sector);
  4959. size = be32_to_cpu(p->blksize);
  4960. update_peer_seq(peer_device, be32_to_cpu(p->seq_num));
  4961. if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
  4962. drbd_ov_out_of_sync_found(device, sector, size);
  4963. else
  4964. ov_out_of_sync_print(device);
  4965. if (!get_ldev(device))
  4966. return 0;
  4967. drbd_rs_complete_io(device, sector);
  4968. dec_rs_pending(device);
  4969. --device->ov_left;
  4970. /* let's advance progress step marks only for every other megabyte */
  4971. if ((device->ov_left & 0x200) == 0x200)
  4972. drbd_advance_rs_marks(device, device->ov_left);
  4973. if (device->ov_left == 0) {
  4974. dw = kmalloc(sizeof(*dw), GFP_NOIO);
  4975. if (dw) {
  4976. dw->w.cb = w_ov_finished;
  4977. dw->device = device;
  4978. drbd_queue_work(&peer_device->connection->sender_work, &dw->w);
  4979. } else {
  4980. drbd_err(device, "kmalloc(dw) failed.");
  4981. ov_out_of_sync_print(device);
  4982. drbd_resync_finished(device);
  4983. }
  4984. }
  4985. put_ldev(device);
  4986. return 0;
  4987. }
  4988. static int got_skip(struct drbd_connection *connection, struct packet_info *pi)
  4989. {
  4990. return 0;
  4991. }
  4992. struct meta_sock_cmd {
  4993. size_t pkt_size;
  4994. int (*fn)(struct drbd_connection *connection, struct packet_info *);
  4995. };
  4996. static void set_rcvtimeo(struct drbd_connection *connection, bool ping_timeout)
  4997. {
  4998. long t;
  4999. struct net_conf *nc;
  5000. rcu_read_lock();
  5001. nc = rcu_dereference(connection->net_conf);
  5002. t = ping_timeout ? nc->ping_timeo : nc->ping_int;
  5003. rcu_read_unlock();
  5004. t *= HZ;
  5005. if (ping_timeout)
  5006. t /= 10;
  5007. connection->meta.socket->sk->sk_rcvtimeo = t;
  5008. }
  5009. static void set_ping_timeout(struct drbd_connection *connection)
  5010. {
  5011. set_rcvtimeo(connection, 1);
  5012. }
  5013. static void set_idle_timeout(struct drbd_connection *connection)
  5014. {
  5015. set_rcvtimeo(connection, 0);
  5016. }
  5017. static struct meta_sock_cmd ack_receiver_tbl[] = {
  5018. [P_PING] = { 0, got_Ping },
  5019. [P_PING_ACK] = { 0, got_PingAck },
  5020. [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
  5021. [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
  5022. [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
  5023. [P_SUPERSEDED] = { sizeof(struct p_block_ack), got_BlockAck },
  5024. [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
  5025. [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
  5026. [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply },
  5027. [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
  5028. [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
  5029. [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
  5030. [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
  5031. [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
  5032. [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply },
  5033. [P_CONN_ST_CHG_REPLY]={ sizeof(struct p_req_state_reply), got_conn_RqSReply },
  5034. [P_RETRY_WRITE] = { sizeof(struct p_block_ack), got_BlockAck },
  5035. };
  5036. int drbd_ack_receiver(struct drbd_thread *thi)
  5037. {
  5038. struct drbd_connection *connection = thi->connection;
  5039. struct meta_sock_cmd *cmd = NULL;
  5040. struct packet_info pi;
  5041. unsigned long pre_recv_jif;
  5042. int rv;
  5043. void *buf = connection->meta.rbuf;
  5044. int received = 0;
  5045. unsigned int header_size = drbd_header_size(connection);
  5046. int expect = header_size;
  5047. bool ping_timeout_active = false;
  5048. struct sched_param param = { .sched_priority = 2 };
  5049. rv = sched_setscheduler(current, SCHED_RR, &param);
  5050. if (rv < 0)
  5051. drbd_err(connection, "drbd_ack_receiver: ERROR set priority, ret=%d\n", rv);
  5052. while (get_t_state(thi) == RUNNING) {
  5053. drbd_thread_current_set_cpu(thi);
  5054. conn_reclaim_net_peer_reqs(connection);
  5055. if (test_and_clear_bit(SEND_PING, &connection->flags)) {
  5056. if (drbd_send_ping(connection)) {
  5057. drbd_err(connection, "drbd_send_ping has failed\n");
  5058. goto reconnect;
  5059. }
  5060. set_ping_timeout(connection);
  5061. ping_timeout_active = true;
  5062. }
  5063. pre_recv_jif = jiffies;
  5064. rv = drbd_recv_short(connection->meta.socket, buf, expect-received, 0);
  5065. /* Note:
  5066. * -EINTR (on meta) we got a signal
  5067. * -EAGAIN (on meta) rcvtimeo expired
  5068. * -ECONNRESET other side closed the connection
  5069. * -ERESTARTSYS (on data) we got a signal
  5070. * rv < 0 other than above: unexpected error!
  5071. * rv == expected: full header or command
  5072. * rv < expected: "woken" by signal during receive
  5073. * rv == 0 : "connection shut down by peer"
  5074. */
  5075. if (likely(rv > 0)) {
  5076. received += rv;
  5077. buf += rv;
  5078. } else if (rv == 0) {
  5079. if (test_bit(DISCONNECT_SENT, &connection->flags)) {
  5080. long t;
  5081. rcu_read_lock();
  5082. t = rcu_dereference(connection->net_conf)->ping_timeo * HZ/10;
  5083. rcu_read_unlock();
  5084. t = wait_event_timeout(connection->ping_wait,
  5085. connection->cstate < C_WF_REPORT_PARAMS,
  5086. t);
  5087. if (t)
  5088. break;
  5089. }
  5090. drbd_err(connection, "meta connection shut down by peer.\n");
  5091. goto reconnect;
  5092. } else if (rv == -EAGAIN) {
  5093. /* If the data socket received something meanwhile,
  5094. * that is good enough: peer is still alive. */
  5095. if (time_after(connection->last_received, pre_recv_jif))
  5096. continue;
  5097. if (ping_timeout_active) {
  5098. drbd_err(connection, "PingAck did not arrive in time.\n");
  5099. goto reconnect;
  5100. }
  5101. set_bit(SEND_PING, &connection->flags);
  5102. continue;
  5103. } else if (rv == -EINTR) {
  5104. /* maybe drbd_thread_stop(): the while condition will notice.
  5105. * maybe woken for send_ping: we'll send a ping above,
  5106. * and change the rcvtimeo */
  5107. flush_signals(current);
  5108. continue;
  5109. } else {
  5110. drbd_err(connection, "sock_recvmsg returned %d\n", rv);
  5111. goto reconnect;
  5112. }
  5113. if (received == expect && cmd == NULL) {
  5114. if (decode_header(connection, connection->meta.rbuf, &pi))
  5115. goto reconnect;
  5116. cmd = &ack_receiver_tbl[pi.cmd];
  5117. if (pi.cmd >= ARRAY_SIZE(ack_receiver_tbl) || !cmd->fn) {
  5118. drbd_err(connection, "Unexpected meta packet %s (0x%04x)\n",
  5119. cmdname(pi.cmd), pi.cmd);
  5120. goto disconnect;
  5121. }
  5122. expect = header_size + cmd->pkt_size;
  5123. if (pi.size != expect - header_size) {
  5124. drbd_err(connection, "Wrong packet size on meta (c: %d, l: %d)\n",
  5125. pi.cmd, pi.size);
  5126. goto reconnect;
  5127. }
  5128. }
  5129. if (received == expect) {
  5130. bool err;
  5131. err = cmd->fn(connection, &pi);
  5132. if (err) {
  5133. drbd_err(connection, "%pf failed\n", cmd->fn);
  5134. goto reconnect;
  5135. }
  5136. connection->last_received = jiffies;
  5137. if (cmd == &ack_receiver_tbl[P_PING_ACK]) {
  5138. set_idle_timeout(connection);
  5139. ping_timeout_active = false;
  5140. }
  5141. buf = connection->meta.rbuf;
  5142. received = 0;
  5143. expect = header_size;
  5144. cmd = NULL;
  5145. }
  5146. }
  5147. if (0) {
  5148. reconnect:
  5149. conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD);
  5150. conn_md_sync(connection);
  5151. }
  5152. if (0) {
  5153. disconnect:
  5154. conn_request_state(connection, NS(conn, C_DISCONNECTING), CS_HARD);
  5155. }
  5156. drbd_info(connection, "ack_receiver terminated\n");
  5157. return 0;
  5158. }
  5159. void drbd_send_acks_wf(struct work_struct *ws)
  5160. {
  5161. struct drbd_peer_device *peer_device =
  5162. container_of(ws, struct drbd_peer_device, send_acks_work);
  5163. struct drbd_connection *connection = peer_device->connection;
  5164. struct drbd_device *device = peer_device->device;
  5165. struct net_conf *nc;
  5166. int tcp_cork, err;
  5167. rcu_read_lock();
  5168. nc = rcu_dereference(connection->net_conf);
  5169. tcp_cork = nc->tcp_cork;
  5170. rcu_read_unlock();
  5171. if (tcp_cork)
  5172. drbd_tcp_cork(connection->meta.socket);
  5173. err = drbd_finish_peer_reqs(device);
  5174. kref_put(&device->kref, drbd_destroy_device);
  5175. /* get is in drbd_endio_write_sec_final(). That is necessary to keep the
  5176. struct work_struct send_acks_work alive, which is in the peer_device object */
  5177. if (err) {
  5178. conn_request_state(connection, NS(conn, C_NETWORK_FAILURE), CS_HARD);
  5179. return;
  5180. }
  5181. if (tcp_cork)
  5182. drbd_tcp_uncork(connection->meta.socket);
  5183. return;
  5184. }