drbd_receiver.c 174 KB

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