drbd_receiver.c 128 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679
  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 <asm/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_req.h"
  39. #include "drbd_vli.h"
  40. enum finish_epoch {
  41. FE_STILL_LIVE,
  42. FE_DESTROYED,
  43. FE_RECYCLED,
  44. };
  45. static int drbd_do_handshake(struct drbd_conf *mdev);
  46. static int drbd_do_auth(struct drbd_conf *mdev);
  47. static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *, struct drbd_epoch *, enum epoch_event);
  48. static int e_end_block(struct drbd_conf *, struct drbd_work *, int);
  49. #define GFP_TRY (__GFP_HIGHMEM | __GFP_NOWARN)
  50. /*
  51. * some helper functions to deal with single linked page lists,
  52. * page->private being our "next" pointer.
  53. */
  54. /* If at least n pages are linked at head, get n pages off.
  55. * Otherwise, don't modify head, and return NULL.
  56. * Locking is the responsibility of the caller.
  57. */
  58. static struct page *page_chain_del(struct page **head, int n)
  59. {
  60. struct page *page;
  61. struct page *tmp;
  62. BUG_ON(!n);
  63. BUG_ON(!head);
  64. page = *head;
  65. if (!page)
  66. return NULL;
  67. while (page) {
  68. tmp = page_chain_next(page);
  69. if (--n == 0)
  70. break; /* found sufficient pages */
  71. if (tmp == NULL)
  72. /* insufficient pages, don't use any of them. */
  73. return NULL;
  74. page = tmp;
  75. }
  76. /* add end of list marker for the returned list */
  77. set_page_private(page, 0);
  78. /* actual return value, and adjustment of head */
  79. page = *head;
  80. *head = tmp;
  81. return page;
  82. }
  83. /* may be used outside of locks to find the tail of a (usually short)
  84. * "private" page chain, before adding it back to a global chain head
  85. * with page_chain_add() under a spinlock. */
  86. static struct page *page_chain_tail(struct page *page, int *len)
  87. {
  88. struct page *tmp;
  89. int i = 1;
  90. while ((tmp = page_chain_next(page)))
  91. ++i, page = tmp;
  92. if (len)
  93. *len = i;
  94. return page;
  95. }
  96. static int page_chain_free(struct page *page)
  97. {
  98. struct page *tmp;
  99. int i = 0;
  100. page_chain_for_each_safe(page, tmp) {
  101. put_page(page);
  102. ++i;
  103. }
  104. return i;
  105. }
  106. static void page_chain_add(struct page **head,
  107. struct page *chain_first, struct page *chain_last)
  108. {
  109. #if 1
  110. struct page *tmp;
  111. tmp = page_chain_tail(chain_first, NULL);
  112. BUG_ON(tmp != chain_last);
  113. #endif
  114. /* add chain to head */
  115. set_page_private(chain_last, (unsigned long)*head);
  116. *head = chain_first;
  117. }
  118. static struct page *drbd_pp_first_pages_or_try_alloc(struct drbd_conf *mdev, int number)
  119. {
  120. struct page *page = NULL;
  121. struct page *tmp = NULL;
  122. int i = 0;
  123. /* Yes, testing drbd_pp_vacant outside the lock is racy.
  124. * So what. It saves a spin_lock. */
  125. if (drbd_pp_vacant >= number) {
  126. spin_lock(&drbd_pp_lock);
  127. page = page_chain_del(&drbd_pp_pool, number);
  128. if (page)
  129. drbd_pp_vacant -= number;
  130. spin_unlock(&drbd_pp_lock);
  131. if (page)
  132. return page;
  133. }
  134. /* GFP_TRY, because we must not cause arbitrary write-out: in a DRBD
  135. * "criss-cross" setup, that might cause write-out on some other DRBD,
  136. * which in turn might block on the other node at this very place. */
  137. for (i = 0; i < number; i++) {
  138. tmp = alloc_page(GFP_TRY);
  139. if (!tmp)
  140. break;
  141. set_page_private(tmp, (unsigned long)page);
  142. page = tmp;
  143. }
  144. if (i == number)
  145. return page;
  146. /* Not enough pages immediately available this time.
  147. * No need to jump around here, drbd_pp_alloc will retry this
  148. * function "soon". */
  149. if (page) {
  150. tmp = page_chain_tail(page, NULL);
  151. spin_lock(&drbd_pp_lock);
  152. page_chain_add(&drbd_pp_pool, page, tmp);
  153. drbd_pp_vacant += i;
  154. spin_unlock(&drbd_pp_lock);
  155. }
  156. return NULL;
  157. }
  158. static void reclaim_net_ee(struct drbd_conf *mdev, struct list_head *to_be_freed)
  159. {
  160. struct drbd_epoch_entry *e;
  161. struct list_head *le, *tle;
  162. /* The EEs are always appended to the end of the list. Since
  163. they are sent in order over the wire, they have to finish
  164. in order. As soon as we see the first not finished we can
  165. stop to examine the list... */
  166. list_for_each_safe(le, tle, &mdev->net_ee) {
  167. e = list_entry(le, struct drbd_epoch_entry, w.list);
  168. if (drbd_ee_has_active_page(e))
  169. break;
  170. list_move(le, to_be_freed);
  171. }
  172. }
  173. static void drbd_kick_lo_and_reclaim_net(struct drbd_conf *mdev)
  174. {
  175. LIST_HEAD(reclaimed);
  176. struct drbd_epoch_entry *e, *t;
  177. spin_lock_irq(&mdev->req_lock);
  178. reclaim_net_ee(mdev, &reclaimed);
  179. spin_unlock_irq(&mdev->req_lock);
  180. list_for_each_entry_safe(e, t, &reclaimed, w.list)
  181. drbd_free_net_ee(mdev, e);
  182. }
  183. /**
  184. * drbd_pp_alloc() - Returns @number pages, retries forever (or until signalled)
  185. * @mdev: DRBD device.
  186. * @number: number of pages requested
  187. * @retry: whether to retry, if not enough pages are available right now
  188. *
  189. * Tries to allocate number pages, first from our own page pool, then from
  190. * the kernel, unless this allocation would exceed the max_buffers setting.
  191. * Possibly retry until DRBD frees sufficient pages somewhere else.
  192. *
  193. * Returns a page chain linked via page->private.
  194. */
  195. static struct page *drbd_pp_alloc(struct drbd_conf *mdev, unsigned number, bool retry)
  196. {
  197. struct page *page = NULL;
  198. DEFINE_WAIT(wait);
  199. /* Yes, we may run up to @number over max_buffers. If we
  200. * follow it strictly, the admin will get it wrong anyways. */
  201. if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers)
  202. page = drbd_pp_first_pages_or_try_alloc(mdev, number);
  203. while (page == NULL) {
  204. prepare_to_wait(&drbd_pp_wait, &wait, TASK_INTERRUPTIBLE);
  205. drbd_kick_lo_and_reclaim_net(mdev);
  206. if (atomic_read(&mdev->pp_in_use) < mdev->net_conf->max_buffers) {
  207. page = drbd_pp_first_pages_or_try_alloc(mdev, number);
  208. if (page)
  209. break;
  210. }
  211. if (!retry)
  212. break;
  213. if (signal_pending(current)) {
  214. dev_warn(DEV, "drbd_pp_alloc interrupted!\n");
  215. break;
  216. }
  217. schedule();
  218. }
  219. finish_wait(&drbd_pp_wait, &wait);
  220. if (page)
  221. atomic_add(number, &mdev->pp_in_use);
  222. return page;
  223. }
  224. /* Must not be used from irq, as that may deadlock: see drbd_pp_alloc.
  225. * Is also used from inside an other spin_lock_irq(&mdev->req_lock);
  226. * Either links the page chain back to the global pool,
  227. * or returns all pages to the system. */
  228. static void drbd_pp_free(struct drbd_conf *mdev, struct page *page, int is_net)
  229. {
  230. atomic_t *a = is_net ? &mdev->pp_in_use_by_net : &mdev->pp_in_use;
  231. int i;
  232. if (drbd_pp_vacant > (DRBD_MAX_BIO_SIZE/PAGE_SIZE)*minor_count)
  233. i = page_chain_free(page);
  234. else {
  235. struct page *tmp;
  236. tmp = page_chain_tail(page, &i);
  237. spin_lock(&drbd_pp_lock);
  238. page_chain_add(&drbd_pp_pool, page, tmp);
  239. drbd_pp_vacant += i;
  240. spin_unlock(&drbd_pp_lock);
  241. }
  242. i = atomic_sub_return(i, a);
  243. if (i < 0)
  244. dev_warn(DEV, "ASSERTION FAILED: %s: %d < 0\n",
  245. is_net ? "pp_in_use_by_net" : "pp_in_use", i);
  246. wake_up(&drbd_pp_wait);
  247. }
  248. /*
  249. You need to hold the req_lock:
  250. _drbd_wait_ee_list_empty()
  251. You must not have the req_lock:
  252. drbd_free_ee()
  253. drbd_alloc_ee()
  254. drbd_init_ee()
  255. drbd_release_ee()
  256. drbd_ee_fix_bhs()
  257. drbd_process_done_ee()
  258. drbd_clear_done_ee()
  259. drbd_wait_ee_list_empty()
  260. */
  261. struct drbd_epoch_entry *drbd_alloc_ee(struct drbd_conf *mdev,
  262. u64 id,
  263. sector_t sector,
  264. unsigned int data_size,
  265. gfp_t gfp_mask) __must_hold(local)
  266. {
  267. struct drbd_epoch_entry *e;
  268. struct page *page;
  269. unsigned nr_pages = (data_size + PAGE_SIZE -1) >> PAGE_SHIFT;
  270. if (drbd_insert_fault(mdev, DRBD_FAULT_AL_EE))
  271. return NULL;
  272. e = mempool_alloc(drbd_ee_mempool, gfp_mask & ~__GFP_HIGHMEM);
  273. if (!e) {
  274. if (!(gfp_mask & __GFP_NOWARN))
  275. dev_err(DEV, "alloc_ee: Allocation of an EE failed\n");
  276. return NULL;
  277. }
  278. page = drbd_pp_alloc(mdev, nr_pages, (gfp_mask & __GFP_WAIT));
  279. if (!page)
  280. goto fail;
  281. INIT_HLIST_NODE(&e->collision);
  282. e->epoch = NULL;
  283. e->mdev = mdev;
  284. e->pages = page;
  285. atomic_set(&e->pending_bios, 0);
  286. e->size = data_size;
  287. e->flags = 0;
  288. e->sector = sector;
  289. e->block_id = id;
  290. return e;
  291. fail:
  292. mempool_free(e, drbd_ee_mempool);
  293. return NULL;
  294. }
  295. void drbd_free_some_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e, int is_net)
  296. {
  297. if (e->flags & EE_HAS_DIGEST)
  298. kfree(e->digest);
  299. drbd_pp_free(mdev, e->pages, is_net);
  300. D_ASSERT(atomic_read(&e->pending_bios) == 0);
  301. D_ASSERT(hlist_unhashed(&e->collision));
  302. mempool_free(e, drbd_ee_mempool);
  303. }
  304. int drbd_release_ee(struct drbd_conf *mdev, struct list_head *list)
  305. {
  306. LIST_HEAD(work_list);
  307. struct drbd_epoch_entry *e, *t;
  308. int count = 0;
  309. int is_net = list == &mdev->net_ee;
  310. spin_lock_irq(&mdev->req_lock);
  311. list_splice_init(list, &work_list);
  312. spin_unlock_irq(&mdev->req_lock);
  313. list_for_each_entry_safe(e, t, &work_list, w.list) {
  314. drbd_free_some_ee(mdev, e, is_net);
  315. count++;
  316. }
  317. return count;
  318. }
  319. /*
  320. * This function is called from _asender only_
  321. * but see also comments in _req_mod(,barrier_acked)
  322. * and receive_Barrier.
  323. *
  324. * Move entries from net_ee to done_ee, if ready.
  325. * Grab done_ee, call all callbacks, free the entries.
  326. * The callbacks typically send out ACKs.
  327. */
  328. static int drbd_process_done_ee(struct drbd_conf *mdev)
  329. {
  330. LIST_HEAD(work_list);
  331. LIST_HEAD(reclaimed);
  332. struct drbd_epoch_entry *e, *t;
  333. int ok = (mdev->state.conn >= C_WF_REPORT_PARAMS);
  334. spin_lock_irq(&mdev->req_lock);
  335. reclaim_net_ee(mdev, &reclaimed);
  336. list_splice_init(&mdev->done_ee, &work_list);
  337. spin_unlock_irq(&mdev->req_lock);
  338. list_for_each_entry_safe(e, t, &reclaimed, w.list)
  339. drbd_free_net_ee(mdev, e);
  340. /* possible callbacks here:
  341. * e_end_block, and e_end_resync_block, e_send_discard_ack.
  342. * all ignore the last argument.
  343. */
  344. list_for_each_entry_safe(e, t, &work_list, w.list) {
  345. /* list_del not necessary, next/prev members not touched */
  346. ok = e->w.cb(mdev, &e->w, !ok) && ok;
  347. drbd_free_ee(mdev, e);
  348. }
  349. wake_up(&mdev->ee_wait);
  350. return ok;
  351. }
  352. void _drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
  353. {
  354. DEFINE_WAIT(wait);
  355. /* avoids spin_lock/unlock
  356. * and calling prepare_to_wait in the fast path */
  357. while (!list_empty(head)) {
  358. prepare_to_wait(&mdev->ee_wait, &wait, TASK_UNINTERRUPTIBLE);
  359. spin_unlock_irq(&mdev->req_lock);
  360. io_schedule();
  361. finish_wait(&mdev->ee_wait, &wait);
  362. spin_lock_irq(&mdev->req_lock);
  363. }
  364. }
  365. void drbd_wait_ee_list_empty(struct drbd_conf *mdev, struct list_head *head)
  366. {
  367. spin_lock_irq(&mdev->req_lock);
  368. _drbd_wait_ee_list_empty(mdev, head);
  369. spin_unlock_irq(&mdev->req_lock);
  370. }
  371. /* see also kernel_accept; which is only present since 2.6.18.
  372. * also we want to log which part of it failed, exactly */
  373. static int drbd_accept(struct drbd_conf *mdev, const char **what,
  374. struct socket *sock, struct socket **newsock)
  375. {
  376. struct sock *sk = sock->sk;
  377. int err = 0;
  378. *what = "listen";
  379. err = sock->ops->listen(sock, 5);
  380. if (err < 0)
  381. goto out;
  382. *what = "sock_create_lite";
  383. err = sock_create_lite(sk->sk_family, sk->sk_type, sk->sk_protocol,
  384. newsock);
  385. if (err < 0)
  386. goto out;
  387. *what = "accept";
  388. err = sock->ops->accept(sock, *newsock, 0);
  389. if (err < 0) {
  390. sock_release(*newsock);
  391. *newsock = NULL;
  392. goto out;
  393. }
  394. (*newsock)->ops = sock->ops;
  395. out:
  396. return err;
  397. }
  398. static int drbd_recv_short(struct drbd_conf *mdev, struct socket *sock,
  399. void *buf, size_t size, int flags)
  400. {
  401. mm_segment_t oldfs;
  402. struct kvec iov = {
  403. .iov_base = buf,
  404. .iov_len = size,
  405. };
  406. struct msghdr msg = {
  407. .msg_iovlen = 1,
  408. .msg_iov = (struct iovec *)&iov,
  409. .msg_flags = (flags ? flags : MSG_WAITALL | MSG_NOSIGNAL)
  410. };
  411. int rv;
  412. oldfs = get_fs();
  413. set_fs(KERNEL_DS);
  414. rv = sock_recvmsg(sock, &msg, size, msg.msg_flags);
  415. set_fs(oldfs);
  416. return rv;
  417. }
  418. static int drbd_recv(struct drbd_conf *mdev, void *buf, size_t size)
  419. {
  420. mm_segment_t oldfs;
  421. struct kvec iov = {
  422. .iov_base = buf,
  423. .iov_len = size,
  424. };
  425. struct msghdr msg = {
  426. .msg_iovlen = 1,
  427. .msg_iov = (struct iovec *)&iov,
  428. .msg_flags = MSG_WAITALL | MSG_NOSIGNAL
  429. };
  430. int rv;
  431. oldfs = get_fs();
  432. set_fs(KERNEL_DS);
  433. for (;;) {
  434. rv = sock_recvmsg(mdev->data.socket, &msg, size, msg.msg_flags);
  435. if (rv == size)
  436. break;
  437. /* Note:
  438. * ECONNRESET other side closed the connection
  439. * ERESTARTSYS (on sock) we got a signal
  440. */
  441. if (rv < 0) {
  442. if (rv == -ECONNRESET)
  443. dev_info(DEV, "sock was reset by peer\n");
  444. else if (rv != -ERESTARTSYS)
  445. dev_err(DEV, "sock_recvmsg returned %d\n", rv);
  446. break;
  447. } else if (rv == 0) {
  448. dev_info(DEV, "sock was shut down by peer\n");
  449. break;
  450. } else {
  451. /* signal came in, or peer/link went down,
  452. * after we read a partial message
  453. */
  454. /* D_ASSERT(signal_pending(current)); */
  455. break;
  456. }
  457. };
  458. set_fs(oldfs);
  459. if (rv != size)
  460. drbd_force_state(mdev, NS(conn, C_BROKEN_PIPE));
  461. return rv;
  462. }
  463. /* quoting tcp(7):
  464. * On individual connections, the socket buffer size must be set prior to the
  465. * listen(2) or connect(2) calls in order to have it take effect.
  466. * This is our wrapper to do so.
  467. */
  468. static void drbd_setbufsize(struct socket *sock, unsigned int snd,
  469. unsigned int rcv)
  470. {
  471. /* open coded SO_SNDBUF, SO_RCVBUF */
  472. if (snd) {
  473. sock->sk->sk_sndbuf = snd;
  474. sock->sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
  475. }
  476. if (rcv) {
  477. sock->sk->sk_rcvbuf = rcv;
  478. sock->sk->sk_userlocks |= SOCK_RCVBUF_LOCK;
  479. }
  480. }
  481. static struct socket *drbd_try_connect(struct drbd_conf *mdev)
  482. {
  483. const char *what;
  484. struct socket *sock;
  485. struct sockaddr_in6 src_in6;
  486. int err;
  487. int disconnect_on_error = 1;
  488. if (!get_net_conf(mdev))
  489. return NULL;
  490. what = "sock_create_kern";
  491. err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family,
  492. SOCK_STREAM, IPPROTO_TCP, &sock);
  493. if (err < 0) {
  494. sock = NULL;
  495. goto out;
  496. }
  497. sock->sk->sk_rcvtimeo =
  498. sock->sk->sk_sndtimeo = mdev->net_conf->try_connect_int*HZ;
  499. drbd_setbufsize(sock, mdev->net_conf->sndbuf_size,
  500. mdev->net_conf->rcvbuf_size);
  501. /* explicitly bind to the configured IP as source IP
  502. * for the outgoing connections.
  503. * This is needed for multihomed hosts and to be
  504. * able to use lo: interfaces for drbd.
  505. * Make sure to use 0 as port number, so linux selects
  506. * a free one dynamically.
  507. */
  508. memcpy(&src_in6, mdev->net_conf->my_addr,
  509. min_t(int, mdev->net_conf->my_addr_len, sizeof(src_in6)));
  510. if (((struct sockaddr *)mdev->net_conf->my_addr)->sa_family == AF_INET6)
  511. src_in6.sin6_port = 0;
  512. else
  513. ((struct sockaddr_in *)&src_in6)->sin_port = 0; /* AF_INET & AF_SCI */
  514. what = "bind before connect";
  515. err = sock->ops->bind(sock,
  516. (struct sockaddr *) &src_in6,
  517. mdev->net_conf->my_addr_len);
  518. if (err < 0)
  519. goto out;
  520. /* connect may fail, peer not yet available.
  521. * stay C_WF_CONNECTION, don't go Disconnecting! */
  522. disconnect_on_error = 0;
  523. what = "connect";
  524. err = sock->ops->connect(sock,
  525. (struct sockaddr *)mdev->net_conf->peer_addr,
  526. mdev->net_conf->peer_addr_len, 0);
  527. out:
  528. if (err < 0) {
  529. if (sock) {
  530. sock_release(sock);
  531. sock = NULL;
  532. }
  533. switch (-err) {
  534. /* timeout, busy, signal pending */
  535. case ETIMEDOUT: case EAGAIN: case EINPROGRESS:
  536. case EINTR: case ERESTARTSYS:
  537. /* peer not (yet) available, network problem */
  538. case ECONNREFUSED: case ENETUNREACH:
  539. case EHOSTDOWN: case EHOSTUNREACH:
  540. disconnect_on_error = 0;
  541. break;
  542. default:
  543. dev_err(DEV, "%s failed, err = %d\n", what, err);
  544. }
  545. if (disconnect_on_error)
  546. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  547. }
  548. put_net_conf(mdev);
  549. return sock;
  550. }
  551. static struct socket *drbd_wait_for_connect(struct drbd_conf *mdev)
  552. {
  553. int timeo, err;
  554. struct socket *s_estab = NULL, *s_listen;
  555. const char *what;
  556. if (!get_net_conf(mdev))
  557. return NULL;
  558. what = "sock_create_kern";
  559. err = sock_create_kern(((struct sockaddr *)mdev->net_conf->my_addr)->sa_family,
  560. SOCK_STREAM, IPPROTO_TCP, &s_listen);
  561. if (err) {
  562. s_listen = NULL;
  563. goto out;
  564. }
  565. timeo = mdev->net_conf->try_connect_int * HZ;
  566. timeo += (random32() & 1) ? timeo / 7 : -timeo / 7; /* 28.5% random jitter */
  567. s_listen->sk->sk_reuse = 1; /* SO_REUSEADDR */
  568. s_listen->sk->sk_rcvtimeo = timeo;
  569. s_listen->sk->sk_sndtimeo = timeo;
  570. drbd_setbufsize(s_listen, mdev->net_conf->sndbuf_size,
  571. mdev->net_conf->rcvbuf_size);
  572. what = "bind before listen";
  573. err = s_listen->ops->bind(s_listen,
  574. (struct sockaddr *) mdev->net_conf->my_addr,
  575. mdev->net_conf->my_addr_len);
  576. if (err < 0)
  577. goto out;
  578. err = drbd_accept(mdev, &what, s_listen, &s_estab);
  579. out:
  580. if (s_listen)
  581. sock_release(s_listen);
  582. if (err < 0) {
  583. if (err != -EAGAIN && err != -EINTR && err != -ERESTARTSYS) {
  584. dev_err(DEV, "%s failed, err = %d\n", what, err);
  585. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  586. }
  587. }
  588. put_net_conf(mdev);
  589. return s_estab;
  590. }
  591. static int drbd_send_fp(struct drbd_conf *mdev,
  592. struct socket *sock, enum drbd_packets cmd)
  593. {
  594. struct p_header80 *h = &mdev->data.sbuf.header.h80;
  595. return _drbd_send_cmd(mdev, sock, cmd, h, sizeof(*h), 0);
  596. }
  597. static enum drbd_packets drbd_recv_fp(struct drbd_conf *mdev, struct socket *sock)
  598. {
  599. struct p_header80 *h = &mdev->data.rbuf.header.h80;
  600. int rr;
  601. rr = drbd_recv_short(mdev, sock, h, sizeof(*h), 0);
  602. if (rr == sizeof(*h) && h->magic == BE_DRBD_MAGIC)
  603. return be16_to_cpu(h->command);
  604. return 0xffff;
  605. }
  606. /**
  607. * drbd_socket_okay() - Free the socket if its connection is not okay
  608. * @mdev: DRBD device.
  609. * @sock: pointer to the pointer to the socket.
  610. */
  611. static int drbd_socket_okay(struct drbd_conf *mdev, struct socket **sock)
  612. {
  613. int rr;
  614. char tb[4];
  615. if (!*sock)
  616. return false;
  617. rr = drbd_recv_short(mdev, *sock, tb, 4, MSG_DONTWAIT | MSG_PEEK);
  618. if (rr > 0 || rr == -EAGAIN) {
  619. return true;
  620. } else {
  621. sock_release(*sock);
  622. *sock = NULL;
  623. return false;
  624. }
  625. }
  626. /*
  627. * return values:
  628. * 1 yes, we have a valid connection
  629. * 0 oops, did not work out, please try again
  630. * -1 peer talks different language,
  631. * no point in trying again, please go standalone.
  632. * -2 We do not have a network config...
  633. */
  634. static int drbd_connect(struct drbd_conf *mdev)
  635. {
  636. struct socket *s, *sock, *msock;
  637. int try, h, ok;
  638. D_ASSERT(!mdev->data.socket);
  639. if (drbd_request_state(mdev, NS(conn, C_WF_CONNECTION)) < SS_SUCCESS)
  640. return -2;
  641. clear_bit(DISCARD_CONCURRENT, &mdev->flags);
  642. sock = NULL;
  643. msock = NULL;
  644. do {
  645. for (try = 0;;) {
  646. /* 3 tries, this should take less than a second! */
  647. s = drbd_try_connect(mdev);
  648. if (s || ++try >= 3)
  649. break;
  650. /* give the other side time to call bind() & listen() */
  651. schedule_timeout_interruptible(HZ / 10);
  652. }
  653. if (s) {
  654. if (!sock) {
  655. drbd_send_fp(mdev, s, P_HAND_SHAKE_S);
  656. sock = s;
  657. s = NULL;
  658. } else if (!msock) {
  659. drbd_send_fp(mdev, s, P_HAND_SHAKE_M);
  660. msock = s;
  661. s = NULL;
  662. } else {
  663. dev_err(DEV, "Logic error in drbd_connect()\n");
  664. goto out_release_sockets;
  665. }
  666. }
  667. if (sock && msock) {
  668. schedule_timeout_interruptible(mdev->net_conf->ping_timeo*HZ/10);
  669. ok = drbd_socket_okay(mdev, &sock);
  670. ok = drbd_socket_okay(mdev, &msock) && ok;
  671. if (ok)
  672. break;
  673. }
  674. retry:
  675. s = drbd_wait_for_connect(mdev);
  676. if (s) {
  677. try = drbd_recv_fp(mdev, s);
  678. drbd_socket_okay(mdev, &sock);
  679. drbd_socket_okay(mdev, &msock);
  680. switch (try) {
  681. case P_HAND_SHAKE_S:
  682. if (sock) {
  683. dev_warn(DEV, "initial packet S crossed\n");
  684. sock_release(sock);
  685. }
  686. sock = s;
  687. break;
  688. case P_HAND_SHAKE_M:
  689. if (msock) {
  690. dev_warn(DEV, "initial packet M crossed\n");
  691. sock_release(msock);
  692. }
  693. msock = s;
  694. set_bit(DISCARD_CONCURRENT, &mdev->flags);
  695. break;
  696. default:
  697. dev_warn(DEV, "Error receiving initial packet\n");
  698. sock_release(s);
  699. if (random32() & 1)
  700. goto retry;
  701. }
  702. }
  703. if (mdev->state.conn <= C_DISCONNECTING)
  704. goto out_release_sockets;
  705. if (signal_pending(current)) {
  706. flush_signals(current);
  707. smp_rmb();
  708. if (get_t_state(&mdev->receiver) == Exiting)
  709. goto out_release_sockets;
  710. }
  711. if (sock && msock) {
  712. ok = drbd_socket_okay(mdev, &sock);
  713. ok = drbd_socket_okay(mdev, &msock) && ok;
  714. if (ok)
  715. break;
  716. }
  717. } while (1);
  718. msock->sk->sk_reuse = 1; /* SO_REUSEADDR */
  719. sock->sk->sk_reuse = 1; /* SO_REUSEADDR */
  720. sock->sk->sk_allocation = GFP_NOIO;
  721. msock->sk->sk_allocation = GFP_NOIO;
  722. sock->sk->sk_priority = TC_PRIO_INTERACTIVE_BULK;
  723. msock->sk->sk_priority = TC_PRIO_INTERACTIVE;
  724. /* NOT YET ...
  725. * sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
  726. * sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  727. * first set it to the P_HAND_SHAKE timeout,
  728. * which we set to 4x the configured ping_timeout. */
  729. sock->sk->sk_sndtimeo =
  730. sock->sk->sk_rcvtimeo = mdev->net_conf->ping_timeo*4*HZ/10;
  731. msock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
  732. msock->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ;
  733. /* we don't want delays.
  734. * we use TCP_CORK where appropriate, though */
  735. drbd_tcp_nodelay(sock);
  736. drbd_tcp_nodelay(msock);
  737. mdev->data.socket = sock;
  738. mdev->meta.socket = msock;
  739. mdev->last_received = jiffies;
  740. D_ASSERT(mdev->asender.task == NULL);
  741. h = drbd_do_handshake(mdev);
  742. if (h <= 0)
  743. return h;
  744. if (mdev->cram_hmac_tfm) {
  745. /* drbd_request_state(mdev, NS(conn, WFAuth)); */
  746. switch (drbd_do_auth(mdev)) {
  747. case -1:
  748. dev_err(DEV, "Authentication of peer failed\n");
  749. return -1;
  750. case 0:
  751. dev_err(DEV, "Authentication of peer failed, trying again.\n");
  752. return 0;
  753. }
  754. }
  755. if (drbd_request_state(mdev, NS(conn, C_WF_REPORT_PARAMS)) < SS_SUCCESS)
  756. return 0;
  757. sock->sk->sk_sndtimeo = mdev->net_conf->timeout*HZ/10;
  758. sock->sk->sk_rcvtimeo = MAX_SCHEDULE_TIMEOUT;
  759. atomic_set(&mdev->packet_seq, 0);
  760. mdev->peer_seq = 0;
  761. drbd_thread_start(&mdev->asender);
  762. if (drbd_send_protocol(mdev) == -1)
  763. return -1;
  764. drbd_send_sync_param(mdev, &mdev->sync_conf);
  765. drbd_send_sizes(mdev, 0, 0);
  766. drbd_send_uuids(mdev);
  767. drbd_send_state(mdev);
  768. clear_bit(USE_DEGR_WFC_T, &mdev->flags);
  769. clear_bit(RESIZE_PENDING, &mdev->flags);
  770. mod_timer(&mdev->request_timer, jiffies + HZ); /* just start it here. */
  771. return 1;
  772. out_release_sockets:
  773. if (sock)
  774. sock_release(sock);
  775. if (msock)
  776. sock_release(msock);
  777. return -1;
  778. }
  779. static int drbd_recv_header(struct drbd_conf *mdev, enum drbd_packets *cmd, unsigned int *packet_size)
  780. {
  781. union p_header *h = &mdev->data.rbuf.header;
  782. int r;
  783. r = drbd_recv(mdev, h, sizeof(*h));
  784. if (unlikely(r != sizeof(*h))) {
  785. if (!signal_pending(current))
  786. dev_warn(DEV, "short read expecting header on sock: r=%d\n", r);
  787. return false;
  788. }
  789. if (likely(h->h80.magic == BE_DRBD_MAGIC)) {
  790. *cmd = be16_to_cpu(h->h80.command);
  791. *packet_size = be16_to_cpu(h->h80.length);
  792. } else if (h->h95.magic == BE_DRBD_MAGIC_BIG) {
  793. *cmd = be16_to_cpu(h->h95.command);
  794. *packet_size = be32_to_cpu(h->h95.length);
  795. } else {
  796. dev_err(DEV, "magic?? on data m: 0x%08x c: %d l: %d\n",
  797. be32_to_cpu(h->h80.magic),
  798. be16_to_cpu(h->h80.command),
  799. be16_to_cpu(h->h80.length));
  800. return false;
  801. }
  802. mdev->last_received = jiffies;
  803. return true;
  804. }
  805. static void drbd_flush(struct drbd_conf *mdev)
  806. {
  807. int rv;
  808. if (mdev->write_ordering >= WO_bdev_flush && get_ldev(mdev)) {
  809. rv = blkdev_issue_flush(mdev->ldev->backing_bdev, GFP_KERNEL,
  810. NULL);
  811. if (rv) {
  812. dev_err(DEV, "local disk flush failed with status %d\n", rv);
  813. /* would rather check on EOPNOTSUPP, but that is not reliable.
  814. * don't try again for ANY return value != 0
  815. * if (rv == -EOPNOTSUPP) */
  816. drbd_bump_write_ordering(mdev, WO_drain_io);
  817. }
  818. put_ldev(mdev);
  819. }
  820. }
  821. /**
  822. * drbd_may_finish_epoch() - Applies an epoch_event to the epoch's state, eventually finishes it.
  823. * @mdev: DRBD device.
  824. * @epoch: Epoch object.
  825. * @ev: Epoch event.
  826. */
  827. static enum finish_epoch drbd_may_finish_epoch(struct drbd_conf *mdev,
  828. struct drbd_epoch *epoch,
  829. enum epoch_event ev)
  830. {
  831. int epoch_size;
  832. struct drbd_epoch *next_epoch;
  833. enum finish_epoch rv = FE_STILL_LIVE;
  834. spin_lock(&mdev->epoch_lock);
  835. do {
  836. next_epoch = NULL;
  837. epoch_size = atomic_read(&epoch->epoch_size);
  838. switch (ev & ~EV_CLEANUP) {
  839. case EV_PUT:
  840. atomic_dec(&epoch->active);
  841. break;
  842. case EV_GOT_BARRIER_NR:
  843. set_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags);
  844. break;
  845. case EV_BECAME_LAST:
  846. /* nothing to do*/
  847. break;
  848. }
  849. if (epoch_size != 0 &&
  850. atomic_read(&epoch->active) == 0 &&
  851. test_bit(DE_HAVE_BARRIER_NUMBER, &epoch->flags)) {
  852. if (!(ev & EV_CLEANUP)) {
  853. spin_unlock(&mdev->epoch_lock);
  854. drbd_send_b_ack(mdev, epoch->barrier_nr, epoch_size);
  855. spin_lock(&mdev->epoch_lock);
  856. }
  857. dec_unacked(mdev);
  858. if (mdev->current_epoch != epoch) {
  859. next_epoch = list_entry(epoch->list.next, struct drbd_epoch, list);
  860. list_del(&epoch->list);
  861. ev = EV_BECAME_LAST | (ev & EV_CLEANUP);
  862. mdev->epochs--;
  863. kfree(epoch);
  864. if (rv == FE_STILL_LIVE)
  865. rv = FE_DESTROYED;
  866. } else {
  867. epoch->flags = 0;
  868. atomic_set(&epoch->epoch_size, 0);
  869. /* atomic_set(&epoch->active, 0); is already zero */
  870. if (rv == FE_STILL_LIVE)
  871. rv = FE_RECYCLED;
  872. wake_up(&mdev->ee_wait);
  873. }
  874. }
  875. if (!next_epoch)
  876. break;
  877. epoch = next_epoch;
  878. } while (1);
  879. spin_unlock(&mdev->epoch_lock);
  880. return rv;
  881. }
  882. /**
  883. * drbd_bump_write_ordering() - Fall back to an other write ordering method
  884. * @mdev: DRBD device.
  885. * @wo: Write ordering method to try.
  886. */
  887. void drbd_bump_write_ordering(struct drbd_conf *mdev, enum write_ordering_e wo) __must_hold(local)
  888. {
  889. enum write_ordering_e pwo;
  890. static char *write_ordering_str[] = {
  891. [WO_none] = "none",
  892. [WO_drain_io] = "drain",
  893. [WO_bdev_flush] = "flush",
  894. };
  895. pwo = mdev->write_ordering;
  896. wo = min(pwo, wo);
  897. if (wo == WO_bdev_flush && mdev->ldev->dc.no_disk_flush)
  898. wo = WO_drain_io;
  899. if (wo == WO_drain_io && mdev->ldev->dc.no_disk_drain)
  900. wo = WO_none;
  901. mdev->write_ordering = wo;
  902. if (pwo != mdev->write_ordering || wo == WO_bdev_flush)
  903. dev_info(DEV, "Method to ensure write ordering: %s\n", write_ordering_str[mdev->write_ordering]);
  904. }
  905. /**
  906. * drbd_submit_ee()
  907. * @mdev: DRBD device.
  908. * @e: epoch entry
  909. * @rw: flag field, see bio->bi_rw
  910. *
  911. * May spread the pages to multiple bios,
  912. * depending on bio_add_page restrictions.
  913. *
  914. * Returns 0 if all bios have been submitted,
  915. * -ENOMEM if we could not allocate enough bios,
  916. * -ENOSPC (any better suggestion?) if we have not been able to bio_add_page a
  917. * single page to an empty bio (which should never happen and likely indicates
  918. * that the lower level IO stack is in some way broken). This has been observed
  919. * on certain Xen deployments.
  920. */
  921. /* TODO allocate from our own bio_set. */
  922. int drbd_submit_ee(struct drbd_conf *mdev, struct drbd_epoch_entry *e,
  923. const unsigned rw, const int fault_type)
  924. {
  925. struct bio *bios = NULL;
  926. struct bio *bio;
  927. struct page *page = e->pages;
  928. sector_t sector = e->sector;
  929. unsigned ds = e->size;
  930. unsigned n_bios = 0;
  931. unsigned nr_pages = (ds + PAGE_SIZE -1) >> PAGE_SHIFT;
  932. int err = -ENOMEM;
  933. /* In most cases, we will only need one bio. But in case the lower
  934. * level restrictions happen to be different at this offset on this
  935. * side than those of the sending peer, we may need to submit the
  936. * request in more than one bio. */
  937. next_bio:
  938. bio = bio_alloc(GFP_NOIO, nr_pages);
  939. if (!bio) {
  940. dev_err(DEV, "submit_ee: Allocation of a bio failed\n");
  941. goto fail;
  942. }
  943. /* > e->sector, unless this is the first bio */
  944. bio->bi_sector = sector;
  945. bio->bi_bdev = mdev->ldev->backing_bdev;
  946. bio->bi_rw = rw;
  947. bio->bi_private = e;
  948. bio->bi_end_io = drbd_endio_sec;
  949. bio->bi_next = bios;
  950. bios = bio;
  951. ++n_bios;
  952. page_chain_for_each(page) {
  953. unsigned len = min_t(unsigned, ds, PAGE_SIZE);
  954. if (!bio_add_page(bio, page, len, 0)) {
  955. /* A single page must always be possible!
  956. * But in case it fails anyways,
  957. * we deal with it, and complain (below). */
  958. if (bio->bi_vcnt == 0) {
  959. dev_err(DEV,
  960. "bio_add_page failed for len=%u, "
  961. "bi_vcnt=0 (bi_sector=%llu)\n",
  962. len, (unsigned long long)bio->bi_sector);
  963. err = -ENOSPC;
  964. goto fail;
  965. }
  966. goto next_bio;
  967. }
  968. ds -= len;
  969. sector += len >> 9;
  970. --nr_pages;
  971. }
  972. D_ASSERT(page == NULL);
  973. D_ASSERT(ds == 0);
  974. atomic_set(&e->pending_bios, n_bios);
  975. do {
  976. bio = bios;
  977. bios = bios->bi_next;
  978. bio->bi_next = NULL;
  979. drbd_generic_make_request(mdev, fault_type, bio);
  980. } while (bios);
  981. return 0;
  982. fail:
  983. while (bios) {
  984. bio = bios;
  985. bios = bios->bi_next;
  986. bio_put(bio);
  987. }
  988. return err;
  989. }
  990. static int receive_Barrier(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  991. {
  992. int rv;
  993. struct p_barrier *p = &mdev->data.rbuf.barrier;
  994. struct drbd_epoch *epoch;
  995. inc_unacked(mdev);
  996. mdev->current_epoch->barrier_nr = p->barrier;
  997. rv = drbd_may_finish_epoch(mdev, mdev->current_epoch, EV_GOT_BARRIER_NR);
  998. /* P_BARRIER_ACK may imply that the corresponding extent is dropped from
  999. * the activity log, which means it would not be resynced in case the
  1000. * R_PRIMARY crashes now.
  1001. * Therefore we must send the barrier_ack after the barrier request was
  1002. * completed. */
  1003. switch (mdev->write_ordering) {
  1004. case WO_none:
  1005. if (rv == FE_RECYCLED)
  1006. return true;
  1007. /* receiver context, in the writeout path of the other node.
  1008. * avoid potential distributed deadlock */
  1009. epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
  1010. if (epoch)
  1011. break;
  1012. else
  1013. dev_warn(DEV, "Allocation of an epoch failed, slowing down\n");
  1014. /* Fall through */
  1015. case WO_bdev_flush:
  1016. case WO_drain_io:
  1017. drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
  1018. drbd_flush(mdev);
  1019. if (atomic_read(&mdev->current_epoch->epoch_size)) {
  1020. epoch = kmalloc(sizeof(struct drbd_epoch), GFP_NOIO);
  1021. if (epoch)
  1022. break;
  1023. }
  1024. epoch = mdev->current_epoch;
  1025. wait_event(mdev->ee_wait, atomic_read(&epoch->epoch_size) == 0);
  1026. D_ASSERT(atomic_read(&epoch->active) == 0);
  1027. D_ASSERT(epoch->flags == 0);
  1028. return true;
  1029. default:
  1030. dev_err(DEV, "Strangeness in mdev->write_ordering %d\n", mdev->write_ordering);
  1031. return false;
  1032. }
  1033. epoch->flags = 0;
  1034. atomic_set(&epoch->epoch_size, 0);
  1035. atomic_set(&epoch->active, 0);
  1036. spin_lock(&mdev->epoch_lock);
  1037. if (atomic_read(&mdev->current_epoch->epoch_size)) {
  1038. list_add(&epoch->list, &mdev->current_epoch->list);
  1039. mdev->current_epoch = epoch;
  1040. mdev->epochs++;
  1041. } else {
  1042. /* The current_epoch got recycled while we allocated this one... */
  1043. kfree(epoch);
  1044. }
  1045. spin_unlock(&mdev->epoch_lock);
  1046. return true;
  1047. }
  1048. /* used from receive_RSDataReply (recv_resync_read)
  1049. * and from receive_Data */
  1050. static struct drbd_epoch_entry *
  1051. read_in_block(struct drbd_conf *mdev, u64 id, sector_t sector, int data_size) __must_hold(local)
  1052. {
  1053. const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
  1054. struct drbd_epoch_entry *e;
  1055. struct page *page;
  1056. int dgs, ds, rr;
  1057. void *dig_in = mdev->int_dig_in;
  1058. void *dig_vv = mdev->int_dig_vv;
  1059. unsigned long *data;
  1060. dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ?
  1061. crypto_hash_digestsize(mdev->integrity_r_tfm) : 0;
  1062. if (dgs) {
  1063. rr = drbd_recv(mdev, dig_in, dgs);
  1064. if (rr != dgs) {
  1065. if (!signal_pending(current))
  1066. dev_warn(DEV,
  1067. "short read receiving data digest: read %d expected %d\n",
  1068. rr, dgs);
  1069. return NULL;
  1070. }
  1071. }
  1072. data_size -= dgs;
  1073. ERR_IF(data_size == 0) return NULL;
  1074. ERR_IF(data_size & 0x1ff) return NULL;
  1075. ERR_IF(data_size > DRBD_MAX_BIO_SIZE) return NULL;
  1076. /* even though we trust out peer,
  1077. * we sometimes have to double check. */
  1078. if (sector + (data_size>>9) > capacity) {
  1079. dev_err(DEV, "request from peer beyond end of local disk: "
  1080. "capacity: %llus < sector: %llus + size: %u\n",
  1081. (unsigned long long)capacity,
  1082. (unsigned long long)sector, data_size);
  1083. return NULL;
  1084. }
  1085. /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
  1086. * "criss-cross" setup, that might cause write-out on some other DRBD,
  1087. * which in turn might block on the other node at this very place. */
  1088. e = drbd_alloc_ee(mdev, id, sector, data_size, GFP_NOIO);
  1089. if (!e)
  1090. return NULL;
  1091. ds = data_size;
  1092. page = e->pages;
  1093. page_chain_for_each(page) {
  1094. unsigned len = min_t(int, ds, PAGE_SIZE);
  1095. data = kmap(page);
  1096. rr = drbd_recv(mdev, data, len);
  1097. if (drbd_insert_fault(mdev, DRBD_FAULT_RECEIVE)) {
  1098. dev_err(DEV, "Fault injection: Corrupting data on receive\n");
  1099. data[0] = data[0] ^ (unsigned long)-1;
  1100. }
  1101. kunmap(page);
  1102. if (rr != len) {
  1103. drbd_free_ee(mdev, e);
  1104. if (!signal_pending(current))
  1105. dev_warn(DEV, "short read receiving data: read %d expected %d\n",
  1106. rr, len);
  1107. return NULL;
  1108. }
  1109. ds -= rr;
  1110. }
  1111. if (dgs) {
  1112. drbd_csum_ee(mdev, mdev->integrity_r_tfm, e, dig_vv);
  1113. if (memcmp(dig_in, dig_vv, dgs)) {
  1114. dev_err(DEV, "Digest integrity check FAILED: %llus +%u\n",
  1115. (unsigned long long)sector, data_size);
  1116. drbd_bcast_ee(mdev, "digest failed",
  1117. dgs, dig_in, dig_vv, e);
  1118. drbd_free_ee(mdev, e);
  1119. return NULL;
  1120. }
  1121. }
  1122. mdev->recv_cnt += data_size>>9;
  1123. return e;
  1124. }
  1125. /* drbd_drain_block() just takes a data block
  1126. * out of the socket input buffer, and discards it.
  1127. */
  1128. static int drbd_drain_block(struct drbd_conf *mdev, int data_size)
  1129. {
  1130. struct page *page;
  1131. int rr, rv = 1;
  1132. void *data;
  1133. if (!data_size)
  1134. return true;
  1135. page = drbd_pp_alloc(mdev, 1, 1);
  1136. data = kmap(page);
  1137. while (data_size) {
  1138. rr = drbd_recv(mdev, data, min_t(int, data_size, PAGE_SIZE));
  1139. if (rr != min_t(int, data_size, PAGE_SIZE)) {
  1140. rv = 0;
  1141. if (!signal_pending(current))
  1142. dev_warn(DEV,
  1143. "short read receiving data: read %d expected %d\n",
  1144. rr, min_t(int, data_size, PAGE_SIZE));
  1145. break;
  1146. }
  1147. data_size -= rr;
  1148. }
  1149. kunmap(page);
  1150. drbd_pp_free(mdev, page, 0);
  1151. return rv;
  1152. }
  1153. static int recv_dless_read(struct drbd_conf *mdev, struct drbd_request *req,
  1154. sector_t sector, int data_size)
  1155. {
  1156. struct bio_vec *bvec;
  1157. struct bio *bio;
  1158. int dgs, rr, i, expect;
  1159. void *dig_in = mdev->int_dig_in;
  1160. void *dig_vv = mdev->int_dig_vv;
  1161. dgs = (mdev->agreed_pro_version >= 87 && mdev->integrity_r_tfm) ?
  1162. crypto_hash_digestsize(mdev->integrity_r_tfm) : 0;
  1163. if (dgs) {
  1164. rr = drbd_recv(mdev, dig_in, dgs);
  1165. if (rr != dgs) {
  1166. if (!signal_pending(current))
  1167. dev_warn(DEV,
  1168. "short read receiving data reply digest: read %d expected %d\n",
  1169. rr, dgs);
  1170. return 0;
  1171. }
  1172. }
  1173. data_size -= dgs;
  1174. /* optimistically update recv_cnt. if receiving fails below,
  1175. * we disconnect anyways, and counters will be reset. */
  1176. mdev->recv_cnt += data_size>>9;
  1177. bio = req->master_bio;
  1178. D_ASSERT(sector == bio->bi_sector);
  1179. bio_for_each_segment(bvec, bio, i) {
  1180. expect = min_t(int, data_size, bvec->bv_len);
  1181. rr = drbd_recv(mdev,
  1182. kmap(bvec->bv_page)+bvec->bv_offset,
  1183. expect);
  1184. kunmap(bvec->bv_page);
  1185. if (rr != expect) {
  1186. if (!signal_pending(current))
  1187. dev_warn(DEV, "short read receiving data reply: "
  1188. "read %d expected %d\n",
  1189. rr, expect);
  1190. return 0;
  1191. }
  1192. data_size -= rr;
  1193. }
  1194. if (dgs) {
  1195. drbd_csum_bio(mdev, mdev->integrity_r_tfm, bio, dig_vv);
  1196. if (memcmp(dig_in, dig_vv, dgs)) {
  1197. dev_err(DEV, "Digest integrity check FAILED. Broken NICs?\n");
  1198. return 0;
  1199. }
  1200. }
  1201. D_ASSERT(data_size == 0);
  1202. return 1;
  1203. }
  1204. /* e_end_resync_block() is called via
  1205. * drbd_process_done_ee() by asender only */
  1206. static int e_end_resync_block(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  1207. {
  1208. struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
  1209. sector_t sector = e->sector;
  1210. int ok;
  1211. D_ASSERT(hlist_unhashed(&e->collision));
  1212. if (likely((e->flags & EE_WAS_ERROR) == 0)) {
  1213. drbd_set_in_sync(mdev, sector, e->size);
  1214. ok = drbd_send_ack(mdev, P_RS_WRITE_ACK, e);
  1215. } else {
  1216. /* Record failure to sync */
  1217. drbd_rs_failed_io(mdev, sector, e->size);
  1218. ok = drbd_send_ack(mdev, P_NEG_ACK, e);
  1219. }
  1220. dec_unacked(mdev);
  1221. return ok;
  1222. }
  1223. static int recv_resync_read(struct drbd_conf *mdev, sector_t sector, int data_size) __releases(local)
  1224. {
  1225. struct drbd_epoch_entry *e;
  1226. e = read_in_block(mdev, ID_SYNCER, sector, data_size);
  1227. if (!e)
  1228. goto fail;
  1229. dec_rs_pending(mdev);
  1230. inc_unacked(mdev);
  1231. /* corresponding dec_unacked() in e_end_resync_block()
  1232. * respective _drbd_clear_done_ee */
  1233. e->w.cb = e_end_resync_block;
  1234. spin_lock_irq(&mdev->req_lock);
  1235. list_add(&e->w.list, &mdev->sync_ee);
  1236. spin_unlock_irq(&mdev->req_lock);
  1237. atomic_add(data_size >> 9, &mdev->rs_sect_ev);
  1238. if (drbd_submit_ee(mdev, e, WRITE, DRBD_FAULT_RS_WR) == 0)
  1239. return true;
  1240. /* don't care for the reason here */
  1241. dev_err(DEV, "submit failed, triggering re-connect\n");
  1242. spin_lock_irq(&mdev->req_lock);
  1243. list_del(&e->w.list);
  1244. spin_unlock_irq(&mdev->req_lock);
  1245. drbd_free_ee(mdev, e);
  1246. fail:
  1247. put_ldev(mdev);
  1248. return false;
  1249. }
  1250. static int receive_DataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  1251. {
  1252. struct drbd_request *req;
  1253. sector_t sector;
  1254. int ok;
  1255. struct p_data *p = &mdev->data.rbuf.data;
  1256. sector = be64_to_cpu(p->sector);
  1257. spin_lock_irq(&mdev->req_lock);
  1258. req = _ar_id_to_req(mdev, p->block_id, sector);
  1259. spin_unlock_irq(&mdev->req_lock);
  1260. if (unlikely(!req)) {
  1261. dev_err(DEV, "Got a corrupt block_id/sector pair(1).\n");
  1262. return false;
  1263. }
  1264. /* hlist_del(&req->collision) is done in _req_may_be_done, to avoid
  1265. * special casing it there for the various failure cases.
  1266. * still no race with drbd_fail_pending_reads */
  1267. ok = recv_dless_read(mdev, req, sector, data_size);
  1268. if (ok)
  1269. req_mod(req, data_received);
  1270. /* else: nothing. handled from drbd_disconnect...
  1271. * I don't think we may complete this just yet
  1272. * in case we are "on-disconnect: freeze" */
  1273. return ok;
  1274. }
  1275. static int receive_RSDataReply(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  1276. {
  1277. sector_t sector;
  1278. int ok;
  1279. struct p_data *p = &mdev->data.rbuf.data;
  1280. sector = be64_to_cpu(p->sector);
  1281. D_ASSERT(p->block_id == ID_SYNCER);
  1282. if (get_ldev(mdev)) {
  1283. /* data is submitted to disk within recv_resync_read.
  1284. * corresponding put_ldev done below on error,
  1285. * or in drbd_endio_write_sec. */
  1286. ok = recv_resync_read(mdev, sector, data_size);
  1287. } else {
  1288. if (__ratelimit(&drbd_ratelimit_state))
  1289. dev_err(DEV, "Can not write resync data to local disk.\n");
  1290. ok = drbd_drain_block(mdev, data_size);
  1291. drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
  1292. }
  1293. atomic_add(data_size >> 9, &mdev->rs_sect_in);
  1294. return ok;
  1295. }
  1296. /* e_end_block() is called via drbd_process_done_ee().
  1297. * this means this function only runs in the asender thread
  1298. */
  1299. static int e_end_block(struct drbd_conf *mdev, struct drbd_work *w, int cancel)
  1300. {
  1301. struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
  1302. sector_t sector = e->sector;
  1303. int ok = 1, pcmd;
  1304. if (mdev->net_conf->wire_protocol == DRBD_PROT_C) {
  1305. if (likely((e->flags & EE_WAS_ERROR) == 0)) {
  1306. pcmd = (mdev->state.conn >= C_SYNC_SOURCE &&
  1307. mdev->state.conn <= C_PAUSED_SYNC_T &&
  1308. e->flags & EE_MAY_SET_IN_SYNC) ?
  1309. P_RS_WRITE_ACK : P_WRITE_ACK;
  1310. ok &= drbd_send_ack(mdev, pcmd, e);
  1311. if (pcmd == P_RS_WRITE_ACK)
  1312. drbd_set_in_sync(mdev, sector, e->size);
  1313. } else {
  1314. ok = drbd_send_ack(mdev, P_NEG_ACK, e);
  1315. /* we expect it to be marked out of sync anyways...
  1316. * maybe assert this? */
  1317. }
  1318. dec_unacked(mdev);
  1319. }
  1320. /* we delete from the conflict detection hash _after_ we sent out the
  1321. * P_WRITE_ACK / P_NEG_ACK, to get the sequence number right. */
  1322. if (mdev->net_conf->two_primaries) {
  1323. spin_lock_irq(&mdev->req_lock);
  1324. D_ASSERT(!hlist_unhashed(&e->collision));
  1325. hlist_del_init(&e->collision);
  1326. spin_unlock_irq(&mdev->req_lock);
  1327. } else {
  1328. D_ASSERT(hlist_unhashed(&e->collision));
  1329. }
  1330. drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + (cancel ? EV_CLEANUP : 0));
  1331. return ok;
  1332. }
  1333. static int e_send_discard_ack(struct drbd_conf *mdev, struct drbd_work *w, int unused)
  1334. {
  1335. struct drbd_epoch_entry *e = (struct drbd_epoch_entry *)w;
  1336. int ok = 1;
  1337. D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
  1338. ok = drbd_send_ack(mdev, P_DISCARD_ACK, e);
  1339. spin_lock_irq(&mdev->req_lock);
  1340. D_ASSERT(!hlist_unhashed(&e->collision));
  1341. hlist_del_init(&e->collision);
  1342. spin_unlock_irq(&mdev->req_lock);
  1343. dec_unacked(mdev);
  1344. return ok;
  1345. }
  1346. /* Called from receive_Data.
  1347. * Synchronize packets on sock with packets on msock.
  1348. *
  1349. * This is here so even when a P_DATA packet traveling via sock overtook an Ack
  1350. * packet traveling on msock, they are still processed in the order they have
  1351. * been sent.
  1352. *
  1353. * Note: we don't care for Ack packets overtaking P_DATA packets.
  1354. *
  1355. * In case packet_seq is larger than mdev->peer_seq number, there are
  1356. * outstanding packets on the msock. We wait for them to arrive.
  1357. * In case we are the logically next packet, we update mdev->peer_seq
  1358. * ourselves. Correctly handles 32bit wrap around.
  1359. *
  1360. * Assume we have a 10 GBit connection, that is about 1<<30 byte per second,
  1361. * about 1<<21 sectors per second. So "worst" case, we have 1<<3 == 8 seconds
  1362. * for the 24bit wrap (historical atomic_t guarantee on some archs), and we have
  1363. * 1<<9 == 512 seconds aka ages for the 32bit wrap around...
  1364. *
  1365. * returns 0 if we may process the packet,
  1366. * -ERESTARTSYS if we were interrupted (by disconnect signal). */
  1367. static int drbd_wait_peer_seq(struct drbd_conf *mdev, const u32 packet_seq)
  1368. {
  1369. DEFINE_WAIT(wait);
  1370. unsigned int p_seq;
  1371. long timeout;
  1372. int ret = 0;
  1373. spin_lock(&mdev->peer_seq_lock);
  1374. for (;;) {
  1375. prepare_to_wait(&mdev->seq_wait, &wait, TASK_INTERRUPTIBLE);
  1376. if (seq_le(packet_seq, mdev->peer_seq+1))
  1377. break;
  1378. if (signal_pending(current)) {
  1379. ret = -ERESTARTSYS;
  1380. break;
  1381. }
  1382. p_seq = mdev->peer_seq;
  1383. spin_unlock(&mdev->peer_seq_lock);
  1384. timeout = schedule_timeout(30*HZ);
  1385. spin_lock(&mdev->peer_seq_lock);
  1386. if (timeout == 0 && p_seq == mdev->peer_seq) {
  1387. ret = -ETIMEDOUT;
  1388. dev_err(DEV, "ASSERT FAILED waited 30 seconds for sequence update, forcing reconnect\n");
  1389. break;
  1390. }
  1391. }
  1392. finish_wait(&mdev->seq_wait, &wait);
  1393. if (mdev->peer_seq+1 == packet_seq)
  1394. mdev->peer_seq++;
  1395. spin_unlock(&mdev->peer_seq_lock);
  1396. return ret;
  1397. }
  1398. /* see also bio_flags_to_wire()
  1399. * DRBD_REQ_*, because we need to semantically map the flags to data packet
  1400. * flags and back. We may replicate to other kernel versions. */
  1401. static unsigned long wire_flags_to_bio(struct drbd_conf *mdev, u32 dpf)
  1402. {
  1403. return (dpf & DP_RW_SYNC ? REQ_SYNC : 0) |
  1404. (dpf & DP_FUA ? REQ_FUA : 0) |
  1405. (dpf & DP_FLUSH ? REQ_FLUSH : 0) |
  1406. (dpf & DP_DISCARD ? REQ_DISCARD : 0);
  1407. }
  1408. /* mirrored write */
  1409. static int receive_Data(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  1410. {
  1411. sector_t sector;
  1412. struct drbd_epoch_entry *e;
  1413. struct p_data *p = &mdev->data.rbuf.data;
  1414. int rw = WRITE;
  1415. u32 dp_flags;
  1416. if (!get_ldev(mdev)) {
  1417. spin_lock(&mdev->peer_seq_lock);
  1418. if (mdev->peer_seq+1 == be32_to_cpu(p->seq_num))
  1419. mdev->peer_seq++;
  1420. spin_unlock(&mdev->peer_seq_lock);
  1421. drbd_send_ack_dp(mdev, P_NEG_ACK, p, data_size);
  1422. atomic_inc(&mdev->current_epoch->epoch_size);
  1423. return drbd_drain_block(mdev, data_size);
  1424. }
  1425. /* get_ldev(mdev) successful.
  1426. * Corresponding put_ldev done either below (on various errors),
  1427. * or in drbd_endio_write_sec, if we successfully submit the data at
  1428. * the end of this function. */
  1429. sector = be64_to_cpu(p->sector);
  1430. e = read_in_block(mdev, p->block_id, sector, data_size);
  1431. if (!e) {
  1432. put_ldev(mdev);
  1433. return false;
  1434. }
  1435. e->w.cb = e_end_block;
  1436. dp_flags = be32_to_cpu(p->dp_flags);
  1437. rw |= wire_flags_to_bio(mdev, dp_flags);
  1438. if (dp_flags & DP_MAY_SET_IN_SYNC)
  1439. e->flags |= EE_MAY_SET_IN_SYNC;
  1440. spin_lock(&mdev->epoch_lock);
  1441. e->epoch = mdev->current_epoch;
  1442. atomic_inc(&e->epoch->epoch_size);
  1443. atomic_inc(&e->epoch->active);
  1444. spin_unlock(&mdev->epoch_lock);
  1445. /* I'm the receiver, I do hold a net_cnt reference. */
  1446. if (!mdev->net_conf->two_primaries) {
  1447. spin_lock_irq(&mdev->req_lock);
  1448. } else {
  1449. /* don't get the req_lock yet,
  1450. * we may sleep in drbd_wait_peer_seq */
  1451. const int size = e->size;
  1452. const int discard = test_bit(DISCARD_CONCURRENT, &mdev->flags);
  1453. DEFINE_WAIT(wait);
  1454. struct drbd_request *i;
  1455. struct hlist_node *n;
  1456. struct hlist_head *slot;
  1457. int first;
  1458. D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
  1459. BUG_ON(mdev->ee_hash == NULL);
  1460. BUG_ON(mdev->tl_hash == NULL);
  1461. /* conflict detection and handling:
  1462. * 1. wait on the sequence number,
  1463. * in case this data packet overtook ACK packets.
  1464. * 2. check our hash tables for conflicting requests.
  1465. * we only need to walk the tl_hash, since an ee can not
  1466. * have a conflict with an other ee: on the submitting
  1467. * node, the corresponding req had already been conflicting,
  1468. * and a conflicting req is never sent.
  1469. *
  1470. * Note: for two_primaries, we are protocol C,
  1471. * so there cannot be any request that is DONE
  1472. * but still on the transfer log.
  1473. *
  1474. * unconditionally add to the ee_hash.
  1475. *
  1476. * if no conflicting request is found:
  1477. * submit.
  1478. *
  1479. * if any conflicting request is found
  1480. * that has not yet been acked,
  1481. * AND I have the "discard concurrent writes" flag:
  1482. * queue (via done_ee) the P_DISCARD_ACK; OUT.
  1483. *
  1484. * if any conflicting request is found:
  1485. * block the receiver, waiting on misc_wait
  1486. * until no more conflicting requests are there,
  1487. * or we get interrupted (disconnect).
  1488. *
  1489. * we do not just write after local io completion of those
  1490. * requests, but only after req is done completely, i.e.
  1491. * we wait for the P_DISCARD_ACK to arrive!
  1492. *
  1493. * then proceed normally, i.e. submit.
  1494. */
  1495. if (drbd_wait_peer_seq(mdev, be32_to_cpu(p->seq_num)))
  1496. goto out_interrupted;
  1497. spin_lock_irq(&mdev->req_lock);
  1498. hlist_add_head(&e->collision, ee_hash_slot(mdev, sector));
  1499. #define OVERLAPS overlaps(i->sector, i->size, sector, size)
  1500. slot = tl_hash_slot(mdev, sector);
  1501. first = 1;
  1502. for (;;) {
  1503. int have_unacked = 0;
  1504. int have_conflict = 0;
  1505. prepare_to_wait(&mdev->misc_wait, &wait,
  1506. TASK_INTERRUPTIBLE);
  1507. hlist_for_each_entry(i, n, slot, collision) {
  1508. if (OVERLAPS) {
  1509. /* only ALERT on first iteration,
  1510. * we may be woken up early... */
  1511. if (first)
  1512. dev_alert(DEV, "%s[%u] Concurrent local write detected!"
  1513. " new: %llus +%u; pending: %llus +%u\n",
  1514. current->comm, current->pid,
  1515. (unsigned long long)sector, size,
  1516. (unsigned long long)i->sector, i->size);
  1517. if (i->rq_state & RQ_NET_PENDING)
  1518. ++have_unacked;
  1519. ++have_conflict;
  1520. }
  1521. }
  1522. #undef OVERLAPS
  1523. if (!have_conflict)
  1524. break;
  1525. /* Discard Ack only for the _first_ iteration */
  1526. if (first && discard && have_unacked) {
  1527. dev_alert(DEV, "Concurrent write! [DISCARD BY FLAG] sec=%llus\n",
  1528. (unsigned long long)sector);
  1529. inc_unacked(mdev);
  1530. e->w.cb = e_send_discard_ack;
  1531. list_add_tail(&e->w.list, &mdev->done_ee);
  1532. spin_unlock_irq(&mdev->req_lock);
  1533. /* we could probably send that P_DISCARD_ACK ourselves,
  1534. * but I don't like the receiver using the msock */
  1535. put_ldev(mdev);
  1536. wake_asender(mdev);
  1537. finish_wait(&mdev->misc_wait, &wait);
  1538. return true;
  1539. }
  1540. if (signal_pending(current)) {
  1541. hlist_del_init(&e->collision);
  1542. spin_unlock_irq(&mdev->req_lock);
  1543. finish_wait(&mdev->misc_wait, &wait);
  1544. goto out_interrupted;
  1545. }
  1546. spin_unlock_irq(&mdev->req_lock);
  1547. if (first) {
  1548. first = 0;
  1549. dev_alert(DEV, "Concurrent write! [W AFTERWARDS] "
  1550. "sec=%llus\n", (unsigned long long)sector);
  1551. } else if (discard) {
  1552. /* we had none on the first iteration.
  1553. * there must be none now. */
  1554. D_ASSERT(have_unacked == 0);
  1555. }
  1556. schedule();
  1557. spin_lock_irq(&mdev->req_lock);
  1558. }
  1559. finish_wait(&mdev->misc_wait, &wait);
  1560. }
  1561. list_add(&e->w.list, &mdev->active_ee);
  1562. spin_unlock_irq(&mdev->req_lock);
  1563. switch (mdev->net_conf->wire_protocol) {
  1564. case DRBD_PROT_C:
  1565. inc_unacked(mdev);
  1566. /* corresponding dec_unacked() in e_end_block()
  1567. * respective _drbd_clear_done_ee */
  1568. break;
  1569. case DRBD_PROT_B:
  1570. /* I really don't like it that the receiver thread
  1571. * sends on the msock, but anyways */
  1572. drbd_send_ack(mdev, P_RECV_ACK, e);
  1573. break;
  1574. case DRBD_PROT_A:
  1575. /* nothing to do */
  1576. break;
  1577. }
  1578. if (mdev->state.pdsk < D_INCONSISTENT) {
  1579. /* In case we have the only disk of the cluster, */
  1580. drbd_set_out_of_sync(mdev, e->sector, e->size);
  1581. e->flags |= EE_CALL_AL_COMPLETE_IO;
  1582. e->flags &= ~EE_MAY_SET_IN_SYNC;
  1583. drbd_al_begin_io(mdev, e->sector);
  1584. }
  1585. if (drbd_submit_ee(mdev, e, rw, DRBD_FAULT_DT_WR) == 0)
  1586. return true;
  1587. /* don't care for the reason here */
  1588. dev_err(DEV, "submit failed, triggering re-connect\n");
  1589. spin_lock_irq(&mdev->req_lock);
  1590. list_del(&e->w.list);
  1591. hlist_del_init(&e->collision);
  1592. spin_unlock_irq(&mdev->req_lock);
  1593. if (e->flags & EE_CALL_AL_COMPLETE_IO)
  1594. drbd_al_complete_io(mdev, e->sector);
  1595. out_interrupted:
  1596. drbd_may_finish_epoch(mdev, e->epoch, EV_PUT + EV_CLEANUP);
  1597. put_ldev(mdev);
  1598. drbd_free_ee(mdev, e);
  1599. return false;
  1600. }
  1601. /* We may throttle resync, if the lower device seems to be busy,
  1602. * and current sync rate is above c_min_rate.
  1603. *
  1604. * To decide whether or not the lower device is busy, we use a scheme similar
  1605. * to MD RAID is_mddev_idle(): if the partition stats reveal "significant"
  1606. * (more than 64 sectors) of activity we cannot account for with our own resync
  1607. * activity, it obviously is "busy".
  1608. *
  1609. * The current sync rate used here uses only the most recent two step marks,
  1610. * to have a short time average so we can react faster.
  1611. */
  1612. int drbd_rs_should_slow_down(struct drbd_conf *mdev, sector_t sector)
  1613. {
  1614. struct gendisk *disk = mdev->ldev->backing_bdev->bd_contains->bd_disk;
  1615. unsigned long db, dt, dbdt;
  1616. struct lc_element *tmp;
  1617. int curr_events;
  1618. int throttle = 0;
  1619. /* feature disabled? */
  1620. if (mdev->sync_conf.c_min_rate == 0)
  1621. return 0;
  1622. spin_lock_irq(&mdev->al_lock);
  1623. tmp = lc_find(mdev->resync, BM_SECT_TO_EXT(sector));
  1624. if (tmp) {
  1625. struct bm_extent *bm_ext = lc_entry(tmp, struct bm_extent, lce);
  1626. if (test_bit(BME_PRIORITY, &bm_ext->flags)) {
  1627. spin_unlock_irq(&mdev->al_lock);
  1628. return 0;
  1629. }
  1630. /* Do not slow down if app IO is already waiting for this extent */
  1631. }
  1632. spin_unlock_irq(&mdev->al_lock);
  1633. curr_events = (int)part_stat_read(&disk->part0, sectors[0]) +
  1634. (int)part_stat_read(&disk->part0, sectors[1]) -
  1635. atomic_read(&mdev->rs_sect_ev);
  1636. if (!mdev->rs_last_events || curr_events - mdev->rs_last_events > 64) {
  1637. unsigned long rs_left;
  1638. int i;
  1639. mdev->rs_last_events = curr_events;
  1640. /* sync speed average over the last 2*DRBD_SYNC_MARK_STEP,
  1641. * approx. */
  1642. i = (mdev->rs_last_mark + DRBD_SYNC_MARKS-1) % DRBD_SYNC_MARKS;
  1643. if (mdev->state.conn == C_VERIFY_S || mdev->state.conn == C_VERIFY_T)
  1644. rs_left = mdev->ov_left;
  1645. else
  1646. rs_left = drbd_bm_total_weight(mdev) - mdev->rs_failed;
  1647. dt = ((long)jiffies - (long)mdev->rs_mark_time[i]) / HZ;
  1648. if (!dt)
  1649. dt++;
  1650. db = mdev->rs_mark_left[i] - rs_left;
  1651. dbdt = Bit2KB(db/dt);
  1652. if (dbdt > mdev->sync_conf.c_min_rate)
  1653. throttle = 1;
  1654. }
  1655. return throttle;
  1656. }
  1657. static int receive_DataRequest(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int digest_size)
  1658. {
  1659. sector_t sector;
  1660. const sector_t capacity = drbd_get_capacity(mdev->this_bdev);
  1661. struct drbd_epoch_entry *e;
  1662. struct digest_info *di = NULL;
  1663. int size, verb;
  1664. unsigned int fault_type;
  1665. struct p_block_req *p = &mdev->data.rbuf.block_req;
  1666. sector = be64_to_cpu(p->sector);
  1667. size = be32_to_cpu(p->blksize);
  1668. if (size <= 0 || (size & 0x1ff) != 0 || size > DRBD_MAX_BIO_SIZE) {
  1669. dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
  1670. (unsigned long long)sector, size);
  1671. return false;
  1672. }
  1673. if (sector + (size>>9) > capacity) {
  1674. dev_err(DEV, "%s:%d: sector: %llus, size: %u\n", __FILE__, __LINE__,
  1675. (unsigned long long)sector, size);
  1676. return false;
  1677. }
  1678. if (!get_ldev_if_state(mdev, D_UP_TO_DATE)) {
  1679. verb = 1;
  1680. switch (cmd) {
  1681. case P_DATA_REQUEST:
  1682. drbd_send_ack_rp(mdev, P_NEG_DREPLY, p);
  1683. break;
  1684. case P_RS_DATA_REQUEST:
  1685. case P_CSUM_RS_REQUEST:
  1686. case P_OV_REQUEST:
  1687. drbd_send_ack_rp(mdev, P_NEG_RS_DREPLY , p);
  1688. break;
  1689. case P_OV_REPLY:
  1690. verb = 0;
  1691. dec_rs_pending(mdev);
  1692. drbd_send_ack_ex(mdev, P_OV_RESULT, sector, size, ID_IN_SYNC);
  1693. break;
  1694. default:
  1695. dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
  1696. cmdname(cmd));
  1697. }
  1698. if (verb && __ratelimit(&drbd_ratelimit_state))
  1699. dev_err(DEV, "Can not satisfy peer's read request, "
  1700. "no local data.\n");
  1701. /* drain possibly payload */
  1702. return drbd_drain_block(mdev, digest_size);
  1703. }
  1704. /* GFP_NOIO, because we must not cause arbitrary write-out: in a DRBD
  1705. * "criss-cross" setup, that might cause write-out on some other DRBD,
  1706. * which in turn might block on the other node at this very place. */
  1707. e = drbd_alloc_ee(mdev, p->block_id, sector, size, GFP_NOIO);
  1708. if (!e) {
  1709. put_ldev(mdev);
  1710. return false;
  1711. }
  1712. switch (cmd) {
  1713. case P_DATA_REQUEST:
  1714. e->w.cb = w_e_end_data_req;
  1715. fault_type = DRBD_FAULT_DT_RD;
  1716. /* application IO, don't drbd_rs_begin_io */
  1717. goto submit;
  1718. case P_RS_DATA_REQUEST:
  1719. e->w.cb = w_e_end_rsdata_req;
  1720. fault_type = DRBD_FAULT_RS_RD;
  1721. /* used in the sector offset progress display */
  1722. mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
  1723. break;
  1724. case P_OV_REPLY:
  1725. case P_CSUM_RS_REQUEST:
  1726. fault_type = DRBD_FAULT_RS_RD;
  1727. di = kmalloc(sizeof(*di) + digest_size, GFP_NOIO);
  1728. if (!di)
  1729. goto out_free_e;
  1730. di->digest_size = digest_size;
  1731. di->digest = (((char *)di)+sizeof(struct digest_info));
  1732. e->digest = di;
  1733. e->flags |= EE_HAS_DIGEST;
  1734. if (drbd_recv(mdev, di->digest, digest_size) != digest_size)
  1735. goto out_free_e;
  1736. if (cmd == P_CSUM_RS_REQUEST) {
  1737. D_ASSERT(mdev->agreed_pro_version >= 89);
  1738. e->w.cb = w_e_end_csum_rs_req;
  1739. /* used in the sector offset progress display */
  1740. mdev->bm_resync_fo = BM_SECT_TO_BIT(sector);
  1741. } else if (cmd == P_OV_REPLY) {
  1742. /* track progress, we may need to throttle */
  1743. atomic_add(size >> 9, &mdev->rs_sect_in);
  1744. e->w.cb = w_e_end_ov_reply;
  1745. dec_rs_pending(mdev);
  1746. /* drbd_rs_begin_io done when we sent this request,
  1747. * but accounting still needs to be done. */
  1748. goto submit_for_resync;
  1749. }
  1750. break;
  1751. case P_OV_REQUEST:
  1752. if (mdev->ov_start_sector == ~(sector_t)0 &&
  1753. mdev->agreed_pro_version >= 90) {
  1754. unsigned long now = jiffies;
  1755. int i;
  1756. mdev->ov_start_sector = sector;
  1757. mdev->ov_position = sector;
  1758. mdev->ov_left = drbd_bm_bits(mdev) - BM_SECT_TO_BIT(sector);
  1759. mdev->rs_total = mdev->ov_left;
  1760. for (i = 0; i < DRBD_SYNC_MARKS; i++) {
  1761. mdev->rs_mark_left[i] = mdev->ov_left;
  1762. mdev->rs_mark_time[i] = now;
  1763. }
  1764. dev_info(DEV, "Online Verify start sector: %llu\n",
  1765. (unsigned long long)sector);
  1766. }
  1767. e->w.cb = w_e_end_ov_req;
  1768. fault_type = DRBD_FAULT_RS_RD;
  1769. break;
  1770. default:
  1771. dev_err(DEV, "unexpected command (%s) in receive_DataRequest\n",
  1772. cmdname(cmd));
  1773. fault_type = DRBD_FAULT_MAX;
  1774. goto out_free_e;
  1775. }
  1776. /* Throttle, drbd_rs_begin_io and submit should become asynchronous
  1777. * wrt the receiver, but it is not as straightforward as it may seem.
  1778. * Various places in the resync start and stop logic assume resync
  1779. * requests are processed in order, requeuing this on the worker thread
  1780. * introduces a bunch of new code for synchronization between threads.
  1781. *
  1782. * Unlimited throttling before drbd_rs_begin_io may stall the resync
  1783. * "forever", throttling after drbd_rs_begin_io will lock that extent
  1784. * for application writes for the same time. For now, just throttle
  1785. * here, where the rest of the code expects the receiver to sleep for
  1786. * a while, anyways.
  1787. */
  1788. /* Throttle before drbd_rs_begin_io, as that locks out application IO;
  1789. * this defers syncer requests for some time, before letting at least
  1790. * on request through. The resync controller on the receiving side
  1791. * will adapt to the incoming rate accordingly.
  1792. *
  1793. * We cannot throttle here if remote is Primary/SyncTarget:
  1794. * we would also throttle its application reads.
  1795. * In that case, throttling is done on the SyncTarget only.
  1796. */
  1797. if (mdev->state.peer != R_PRIMARY && drbd_rs_should_slow_down(mdev, sector))
  1798. schedule_timeout_uninterruptible(HZ/10);
  1799. if (drbd_rs_begin_io(mdev, sector))
  1800. goto out_free_e;
  1801. submit_for_resync:
  1802. atomic_add(size >> 9, &mdev->rs_sect_ev);
  1803. submit:
  1804. inc_unacked(mdev);
  1805. spin_lock_irq(&mdev->req_lock);
  1806. list_add_tail(&e->w.list, &mdev->read_ee);
  1807. spin_unlock_irq(&mdev->req_lock);
  1808. if (drbd_submit_ee(mdev, e, READ, fault_type) == 0)
  1809. return true;
  1810. /* don't care for the reason here */
  1811. dev_err(DEV, "submit failed, triggering re-connect\n");
  1812. spin_lock_irq(&mdev->req_lock);
  1813. list_del(&e->w.list);
  1814. spin_unlock_irq(&mdev->req_lock);
  1815. /* no drbd_rs_complete_io(), we are dropping the connection anyways */
  1816. out_free_e:
  1817. put_ldev(mdev);
  1818. drbd_free_ee(mdev, e);
  1819. return false;
  1820. }
  1821. static int drbd_asb_recover_0p(struct drbd_conf *mdev) __must_hold(local)
  1822. {
  1823. int self, peer, rv = -100;
  1824. unsigned long ch_self, ch_peer;
  1825. self = mdev->ldev->md.uuid[UI_BITMAP] & 1;
  1826. peer = mdev->p_uuid[UI_BITMAP] & 1;
  1827. ch_peer = mdev->p_uuid[UI_SIZE];
  1828. ch_self = mdev->comm_bm_set;
  1829. switch (mdev->net_conf->after_sb_0p) {
  1830. case ASB_CONSENSUS:
  1831. case ASB_DISCARD_SECONDARY:
  1832. case ASB_CALL_HELPER:
  1833. dev_err(DEV, "Configuration error.\n");
  1834. break;
  1835. case ASB_DISCONNECT:
  1836. break;
  1837. case ASB_DISCARD_YOUNGER_PRI:
  1838. if (self == 0 && peer == 1) {
  1839. rv = -1;
  1840. break;
  1841. }
  1842. if (self == 1 && peer == 0) {
  1843. rv = 1;
  1844. break;
  1845. }
  1846. /* Else fall through to one of the other strategies... */
  1847. case ASB_DISCARD_OLDER_PRI:
  1848. if (self == 0 && peer == 1) {
  1849. rv = 1;
  1850. break;
  1851. }
  1852. if (self == 1 && peer == 0) {
  1853. rv = -1;
  1854. break;
  1855. }
  1856. /* Else fall through to one of the other strategies... */
  1857. dev_warn(DEV, "Discard younger/older primary did not find a decision\n"
  1858. "Using discard-least-changes instead\n");
  1859. case ASB_DISCARD_ZERO_CHG:
  1860. if (ch_peer == 0 && ch_self == 0) {
  1861. rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
  1862. ? -1 : 1;
  1863. break;
  1864. } else {
  1865. if (ch_peer == 0) { rv = 1; break; }
  1866. if (ch_self == 0) { rv = -1; break; }
  1867. }
  1868. if (mdev->net_conf->after_sb_0p == ASB_DISCARD_ZERO_CHG)
  1869. break;
  1870. case ASB_DISCARD_LEAST_CHG:
  1871. if (ch_self < ch_peer)
  1872. rv = -1;
  1873. else if (ch_self > ch_peer)
  1874. rv = 1;
  1875. else /* ( ch_self == ch_peer ) */
  1876. /* Well, then use something else. */
  1877. rv = test_bit(DISCARD_CONCURRENT, &mdev->flags)
  1878. ? -1 : 1;
  1879. break;
  1880. case ASB_DISCARD_LOCAL:
  1881. rv = -1;
  1882. break;
  1883. case ASB_DISCARD_REMOTE:
  1884. rv = 1;
  1885. }
  1886. return rv;
  1887. }
  1888. static int drbd_asb_recover_1p(struct drbd_conf *mdev) __must_hold(local)
  1889. {
  1890. int hg, rv = -100;
  1891. switch (mdev->net_conf->after_sb_1p) {
  1892. case ASB_DISCARD_YOUNGER_PRI:
  1893. case ASB_DISCARD_OLDER_PRI:
  1894. case ASB_DISCARD_LEAST_CHG:
  1895. case ASB_DISCARD_LOCAL:
  1896. case ASB_DISCARD_REMOTE:
  1897. dev_err(DEV, "Configuration error.\n");
  1898. break;
  1899. case ASB_DISCONNECT:
  1900. break;
  1901. case ASB_CONSENSUS:
  1902. hg = drbd_asb_recover_0p(mdev);
  1903. if (hg == -1 && mdev->state.role == R_SECONDARY)
  1904. rv = hg;
  1905. if (hg == 1 && mdev->state.role == R_PRIMARY)
  1906. rv = hg;
  1907. break;
  1908. case ASB_VIOLENTLY:
  1909. rv = drbd_asb_recover_0p(mdev);
  1910. break;
  1911. case ASB_DISCARD_SECONDARY:
  1912. return mdev->state.role == R_PRIMARY ? 1 : -1;
  1913. case ASB_CALL_HELPER:
  1914. hg = drbd_asb_recover_0p(mdev);
  1915. if (hg == -1 && mdev->state.role == R_PRIMARY) {
  1916. enum drbd_state_rv rv2;
  1917. drbd_set_role(mdev, R_SECONDARY, 0);
  1918. /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
  1919. * we might be here in C_WF_REPORT_PARAMS which is transient.
  1920. * we do not need to wait for the after state change work either. */
  1921. rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
  1922. if (rv2 != SS_SUCCESS) {
  1923. drbd_khelper(mdev, "pri-lost-after-sb");
  1924. } else {
  1925. dev_warn(DEV, "Successfully gave up primary role.\n");
  1926. rv = hg;
  1927. }
  1928. } else
  1929. rv = hg;
  1930. }
  1931. return rv;
  1932. }
  1933. static int drbd_asb_recover_2p(struct drbd_conf *mdev) __must_hold(local)
  1934. {
  1935. int hg, rv = -100;
  1936. switch (mdev->net_conf->after_sb_2p) {
  1937. case ASB_DISCARD_YOUNGER_PRI:
  1938. case ASB_DISCARD_OLDER_PRI:
  1939. case ASB_DISCARD_LEAST_CHG:
  1940. case ASB_DISCARD_LOCAL:
  1941. case ASB_DISCARD_REMOTE:
  1942. case ASB_CONSENSUS:
  1943. case ASB_DISCARD_SECONDARY:
  1944. dev_err(DEV, "Configuration error.\n");
  1945. break;
  1946. case ASB_VIOLENTLY:
  1947. rv = drbd_asb_recover_0p(mdev);
  1948. break;
  1949. case ASB_DISCONNECT:
  1950. break;
  1951. case ASB_CALL_HELPER:
  1952. hg = drbd_asb_recover_0p(mdev);
  1953. if (hg == -1) {
  1954. enum drbd_state_rv rv2;
  1955. /* drbd_change_state() does not sleep while in SS_IN_TRANSIENT_STATE,
  1956. * we might be here in C_WF_REPORT_PARAMS which is transient.
  1957. * we do not need to wait for the after state change work either. */
  1958. rv2 = drbd_change_state(mdev, CS_VERBOSE, NS(role, R_SECONDARY));
  1959. if (rv2 != SS_SUCCESS) {
  1960. drbd_khelper(mdev, "pri-lost-after-sb");
  1961. } else {
  1962. dev_warn(DEV, "Successfully gave up primary role.\n");
  1963. rv = hg;
  1964. }
  1965. } else
  1966. rv = hg;
  1967. }
  1968. return rv;
  1969. }
  1970. static void drbd_uuid_dump(struct drbd_conf *mdev, char *text, u64 *uuid,
  1971. u64 bits, u64 flags)
  1972. {
  1973. if (!uuid) {
  1974. dev_info(DEV, "%s uuid info vanished while I was looking!\n", text);
  1975. return;
  1976. }
  1977. dev_info(DEV, "%s %016llX:%016llX:%016llX:%016llX bits:%llu flags:%llX\n",
  1978. text,
  1979. (unsigned long long)uuid[UI_CURRENT],
  1980. (unsigned long long)uuid[UI_BITMAP],
  1981. (unsigned long long)uuid[UI_HISTORY_START],
  1982. (unsigned long long)uuid[UI_HISTORY_END],
  1983. (unsigned long long)bits,
  1984. (unsigned long long)flags);
  1985. }
  1986. /*
  1987. 100 after split brain try auto recover
  1988. 2 C_SYNC_SOURCE set BitMap
  1989. 1 C_SYNC_SOURCE use BitMap
  1990. 0 no Sync
  1991. -1 C_SYNC_TARGET use BitMap
  1992. -2 C_SYNC_TARGET set BitMap
  1993. -100 after split brain, disconnect
  1994. -1000 unrelated data
  1995. -1091 requires proto 91
  1996. -1096 requires proto 96
  1997. */
  1998. static int drbd_uuid_compare(struct drbd_conf *mdev, int *rule_nr) __must_hold(local)
  1999. {
  2000. u64 self, peer;
  2001. int i, j;
  2002. self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
  2003. peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
  2004. *rule_nr = 10;
  2005. if (self == UUID_JUST_CREATED && peer == UUID_JUST_CREATED)
  2006. return 0;
  2007. *rule_nr = 20;
  2008. if ((self == UUID_JUST_CREATED || self == (u64)0) &&
  2009. peer != UUID_JUST_CREATED)
  2010. return -2;
  2011. *rule_nr = 30;
  2012. if (self != UUID_JUST_CREATED &&
  2013. (peer == UUID_JUST_CREATED || peer == (u64)0))
  2014. return 2;
  2015. if (self == peer) {
  2016. int rct, dc; /* roles at crash time */
  2017. if (mdev->p_uuid[UI_BITMAP] == (u64)0 && mdev->ldev->md.uuid[UI_BITMAP] != (u64)0) {
  2018. if (mdev->agreed_pro_version < 91)
  2019. return -1091;
  2020. if ((mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) &&
  2021. (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1))) {
  2022. dev_info(DEV, "was SyncSource, missed the resync finished event, corrected myself:\n");
  2023. drbd_uuid_set_bm(mdev, 0UL);
  2024. drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
  2025. mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
  2026. *rule_nr = 34;
  2027. } else {
  2028. dev_info(DEV, "was SyncSource (peer failed to write sync_uuid)\n");
  2029. *rule_nr = 36;
  2030. }
  2031. return 1;
  2032. }
  2033. if (mdev->ldev->md.uuid[UI_BITMAP] == (u64)0 && mdev->p_uuid[UI_BITMAP] != (u64)0) {
  2034. if (mdev->agreed_pro_version < 91)
  2035. return -1091;
  2036. if ((mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) == (mdev->p_uuid[UI_BITMAP] & ~((u64)1)) &&
  2037. (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) == (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1))) {
  2038. dev_info(DEV, "was SyncTarget, peer missed the resync finished event, corrected peer:\n");
  2039. mdev->p_uuid[UI_HISTORY_START + 1] = mdev->p_uuid[UI_HISTORY_START];
  2040. mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_BITMAP];
  2041. mdev->p_uuid[UI_BITMAP] = 0UL;
  2042. drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
  2043. *rule_nr = 35;
  2044. } else {
  2045. dev_info(DEV, "was SyncTarget (failed to write sync_uuid)\n");
  2046. *rule_nr = 37;
  2047. }
  2048. return -1;
  2049. }
  2050. /* Common power [off|failure] */
  2051. rct = (test_bit(CRASHED_PRIMARY, &mdev->flags) ? 1 : 0) +
  2052. (mdev->p_uuid[UI_FLAGS] & 2);
  2053. /* lowest bit is set when we were primary,
  2054. * next bit (weight 2) is set when peer was primary */
  2055. *rule_nr = 40;
  2056. switch (rct) {
  2057. case 0: /* !self_pri && !peer_pri */ return 0;
  2058. case 1: /* self_pri && !peer_pri */ return 1;
  2059. case 2: /* !self_pri && peer_pri */ return -1;
  2060. case 3: /* self_pri && peer_pri */
  2061. dc = test_bit(DISCARD_CONCURRENT, &mdev->flags);
  2062. return dc ? -1 : 1;
  2063. }
  2064. }
  2065. *rule_nr = 50;
  2066. peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
  2067. if (self == peer)
  2068. return -1;
  2069. *rule_nr = 51;
  2070. peer = mdev->p_uuid[UI_HISTORY_START] & ~((u64)1);
  2071. if (self == peer) {
  2072. if (mdev->agreed_pro_version < 96 ?
  2073. (mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1)) ==
  2074. (mdev->p_uuid[UI_HISTORY_START + 1] & ~((u64)1)) :
  2075. peer + UUID_NEW_BM_OFFSET == (mdev->p_uuid[UI_BITMAP] & ~((u64)1))) {
  2076. /* The last P_SYNC_UUID did not get though. Undo the last start of
  2077. resync as sync source modifications of the peer's UUIDs. */
  2078. if (mdev->agreed_pro_version < 91)
  2079. return -1091;
  2080. mdev->p_uuid[UI_BITMAP] = mdev->p_uuid[UI_HISTORY_START];
  2081. mdev->p_uuid[UI_HISTORY_START] = mdev->p_uuid[UI_HISTORY_START + 1];
  2082. dev_info(DEV, "Did not got last syncUUID packet, corrected:\n");
  2083. drbd_uuid_dump(mdev, "peer", mdev->p_uuid, mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
  2084. return -1;
  2085. }
  2086. }
  2087. *rule_nr = 60;
  2088. self = mdev->ldev->md.uuid[UI_CURRENT] & ~((u64)1);
  2089. for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
  2090. peer = mdev->p_uuid[i] & ~((u64)1);
  2091. if (self == peer)
  2092. return -2;
  2093. }
  2094. *rule_nr = 70;
  2095. self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
  2096. peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
  2097. if (self == peer)
  2098. return 1;
  2099. *rule_nr = 71;
  2100. self = mdev->ldev->md.uuid[UI_HISTORY_START] & ~((u64)1);
  2101. if (self == peer) {
  2102. if (mdev->agreed_pro_version < 96 ?
  2103. (mdev->ldev->md.uuid[UI_HISTORY_START + 1] & ~((u64)1)) ==
  2104. (mdev->p_uuid[UI_HISTORY_START] & ~((u64)1)) :
  2105. self + UUID_NEW_BM_OFFSET == (mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1))) {
  2106. /* The last P_SYNC_UUID did not get though. Undo the last start of
  2107. resync as sync source modifications of our UUIDs. */
  2108. if (mdev->agreed_pro_version < 91)
  2109. return -1091;
  2110. _drbd_uuid_set(mdev, UI_BITMAP, mdev->ldev->md.uuid[UI_HISTORY_START]);
  2111. _drbd_uuid_set(mdev, UI_HISTORY_START, mdev->ldev->md.uuid[UI_HISTORY_START + 1]);
  2112. dev_info(DEV, "Last syncUUID did not get through, corrected:\n");
  2113. drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid,
  2114. mdev->state.disk >= D_NEGOTIATING ? drbd_bm_total_weight(mdev) : 0, 0);
  2115. return 1;
  2116. }
  2117. }
  2118. *rule_nr = 80;
  2119. peer = mdev->p_uuid[UI_CURRENT] & ~((u64)1);
  2120. for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
  2121. self = mdev->ldev->md.uuid[i] & ~((u64)1);
  2122. if (self == peer)
  2123. return 2;
  2124. }
  2125. *rule_nr = 90;
  2126. self = mdev->ldev->md.uuid[UI_BITMAP] & ~((u64)1);
  2127. peer = mdev->p_uuid[UI_BITMAP] & ~((u64)1);
  2128. if (self == peer && self != ((u64)0))
  2129. return 100;
  2130. *rule_nr = 100;
  2131. for (i = UI_HISTORY_START; i <= UI_HISTORY_END; i++) {
  2132. self = mdev->ldev->md.uuid[i] & ~((u64)1);
  2133. for (j = UI_HISTORY_START; j <= UI_HISTORY_END; j++) {
  2134. peer = mdev->p_uuid[j] & ~((u64)1);
  2135. if (self == peer)
  2136. return -100;
  2137. }
  2138. }
  2139. return -1000;
  2140. }
  2141. /* drbd_sync_handshake() returns the new conn state on success, or
  2142. CONN_MASK (-1) on failure.
  2143. */
  2144. static enum drbd_conns drbd_sync_handshake(struct drbd_conf *mdev, enum drbd_role peer_role,
  2145. enum drbd_disk_state peer_disk) __must_hold(local)
  2146. {
  2147. int hg, rule_nr;
  2148. enum drbd_conns rv = C_MASK;
  2149. enum drbd_disk_state mydisk;
  2150. mydisk = mdev->state.disk;
  2151. if (mydisk == D_NEGOTIATING)
  2152. mydisk = mdev->new_state_tmp.disk;
  2153. dev_info(DEV, "drbd_sync_handshake:\n");
  2154. drbd_uuid_dump(mdev, "self", mdev->ldev->md.uuid, mdev->comm_bm_set, 0);
  2155. drbd_uuid_dump(mdev, "peer", mdev->p_uuid,
  2156. mdev->p_uuid[UI_SIZE], mdev->p_uuid[UI_FLAGS]);
  2157. hg = drbd_uuid_compare(mdev, &rule_nr);
  2158. dev_info(DEV, "uuid_compare()=%d by rule %d\n", hg, rule_nr);
  2159. if (hg == -1000) {
  2160. dev_alert(DEV, "Unrelated data, aborting!\n");
  2161. return C_MASK;
  2162. }
  2163. if (hg < -1000) {
  2164. dev_alert(DEV, "To resolve this both sides have to support at least protocol %d\n", -hg - 1000);
  2165. return C_MASK;
  2166. }
  2167. if ((mydisk == D_INCONSISTENT && peer_disk > D_INCONSISTENT) ||
  2168. (peer_disk == D_INCONSISTENT && mydisk > D_INCONSISTENT)) {
  2169. int f = (hg == -100) || abs(hg) == 2;
  2170. hg = mydisk > D_INCONSISTENT ? 1 : -1;
  2171. if (f)
  2172. hg = hg*2;
  2173. dev_info(DEV, "Becoming sync %s due to disk states.\n",
  2174. hg > 0 ? "source" : "target");
  2175. }
  2176. if (abs(hg) == 100)
  2177. drbd_khelper(mdev, "initial-split-brain");
  2178. if (hg == 100 || (hg == -100 && mdev->net_conf->always_asbp)) {
  2179. int pcount = (mdev->state.role == R_PRIMARY)
  2180. + (peer_role == R_PRIMARY);
  2181. int forced = (hg == -100);
  2182. switch (pcount) {
  2183. case 0:
  2184. hg = drbd_asb_recover_0p(mdev);
  2185. break;
  2186. case 1:
  2187. hg = drbd_asb_recover_1p(mdev);
  2188. break;
  2189. case 2:
  2190. hg = drbd_asb_recover_2p(mdev);
  2191. break;
  2192. }
  2193. if (abs(hg) < 100) {
  2194. dev_warn(DEV, "Split-Brain detected, %d primaries, "
  2195. "automatically solved. Sync from %s node\n",
  2196. pcount, (hg < 0) ? "peer" : "this");
  2197. if (forced) {
  2198. dev_warn(DEV, "Doing a full sync, since"
  2199. " UUIDs where ambiguous.\n");
  2200. hg = hg*2;
  2201. }
  2202. }
  2203. }
  2204. if (hg == -100) {
  2205. if (mdev->net_conf->want_lose && !(mdev->p_uuid[UI_FLAGS]&1))
  2206. hg = -1;
  2207. if (!mdev->net_conf->want_lose && (mdev->p_uuid[UI_FLAGS]&1))
  2208. hg = 1;
  2209. if (abs(hg) < 100)
  2210. dev_warn(DEV, "Split-Brain detected, manually solved. "
  2211. "Sync from %s node\n",
  2212. (hg < 0) ? "peer" : "this");
  2213. }
  2214. if (hg == -100) {
  2215. /* FIXME this log message is not correct if we end up here
  2216. * after an attempted attach on a diskless node.
  2217. * We just refuse to attach -- well, we drop the "connection"
  2218. * to that disk, in a way... */
  2219. dev_alert(DEV, "Split-Brain detected but unresolved, dropping connection!\n");
  2220. drbd_khelper(mdev, "split-brain");
  2221. return C_MASK;
  2222. }
  2223. if (hg > 0 && mydisk <= D_INCONSISTENT) {
  2224. dev_err(DEV, "I shall become SyncSource, but I am inconsistent!\n");
  2225. return C_MASK;
  2226. }
  2227. if (hg < 0 && /* by intention we do not use mydisk here. */
  2228. mdev->state.role == R_PRIMARY && mdev->state.disk >= D_CONSISTENT) {
  2229. switch (mdev->net_conf->rr_conflict) {
  2230. case ASB_CALL_HELPER:
  2231. drbd_khelper(mdev, "pri-lost");
  2232. /* fall through */
  2233. case ASB_DISCONNECT:
  2234. dev_err(DEV, "I shall become SyncTarget, but I am primary!\n");
  2235. return C_MASK;
  2236. case ASB_VIOLENTLY:
  2237. dev_warn(DEV, "Becoming SyncTarget, violating the stable-data"
  2238. "assumption\n");
  2239. }
  2240. }
  2241. if (mdev->net_conf->dry_run || test_bit(CONN_DRY_RUN, &mdev->flags)) {
  2242. if (hg == 0)
  2243. dev_info(DEV, "dry-run connect: No resync, would become Connected immediately.\n");
  2244. else
  2245. dev_info(DEV, "dry-run connect: Would become %s, doing a %s resync.",
  2246. drbd_conn_str(hg > 0 ? C_SYNC_SOURCE : C_SYNC_TARGET),
  2247. abs(hg) >= 2 ? "full" : "bit-map based");
  2248. return C_MASK;
  2249. }
  2250. if (abs(hg) >= 2) {
  2251. dev_info(DEV, "Writing the whole bitmap, full sync required after drbd_sync_handshake.\n");
  2252. if (drbd_bitmap_io(mdev, &drbd_bmio_set_n_write, "set_n_write from sync_handshake",
  2253. BM_LOCKED_SET_ALLOWED))
  2254. return C_MASK;
  2255. }
  2256. if (hg > 0) { /* become sync source. */
  2257. rv = C_WF_BITMAP_S;
  2258. } else if (hg < 0) { /* become sync target */
  2259. rv = C_WF_BITMAP_T;
  2260. } else {
  2261. rv = C_CONNECTED;
  2262. if (drbd_bm_total_weight(mdev)) {
  2263. dev_info(DEV, "No resync, but %lu bits in bitmap!\n",
  2264. drbd_bm_total_weight(mdev));
  2265. }
  2266. }
  2267. return rv;
  2268. }
  2269. /* returns 1 if invalid */
  2270. static int cmp_after_sb(enum drbd_after_sb_p peer, enum drbd_after_sb_p self)
  2271. {
  2272. /* ASB_DISCARD_REMOTE - ASB_DISCARD_LOCAL is valid */
  2273. if ((peer == ASB_DISCARD_REMOTE && self == ASB_DISCARD_LOCAL) ||
  2274. (self == ASB_DISCARD_REMOTE && peer == ASB_DISCARD_LOCAL))
  2275. return 0;
  2276. /* any other things with ASB_DISCARD_REMOTE or ASB_DISCARD_LOCAL are invalid */
  2277. if (peer == ASB_DISCARD_REMOTE || peer == ASB_DISCARD_LOCAL ||
  2278. self == ASB_DISCARD_REMOTE || self == ASB_DISCARD_LOCAL)
  2279. return 1;
  2280. /* everything else is valid if they are equal on both sides. */
  2281. if (peer == self)
  2282. return 0;
  2283. /* everything es is invalid. */
  2284. return 1;
  2285. }
  2286. static int receive_protocol(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  2287. {
  2288. struct p_protocol *p = &mdev->data.rbuf.protocol;
  2289. int p_proto, p_after_sb_0p, p_after_sb_1p, p_after_sb_2p;
  2290. int p_want_lose, p_two_primaries, cf;
  2291. char p_integrity_alg[SHARED_SECRET_MAX] = "";
  2292. p_proto = be32_to_cpu(p->protocol);
  2293. p_after_sb_0p = be32_to_cpu(p->after_sb_0p);
  2294. p_after_sb_1p = be32_to_cpu(p->after_sb_1p);
  2295. p_after_sb_2p = be32_to_cpu(p->after_sb_2p);
  2296. p_two_primaries = be32_to_cpu(p->two_primaries);
  2297. cf = be32_to_cpu(p->conn_flags);
  2298. p_want_lose = cf & CF_WANT_LOSE;
  2299. clear_bit(CONN_DRY_RUN, &mdev->flags);
  2300. if (cf & CF_DRY_RUN)
  2301. set_bit(CONN_DRY_RUN, &mdev->flags);
  2302. if (p_proto != mdev->net_conf->wire_protocol) {
  2303. dev_err(DEV, "incompatible communication protocols\n");
  2304. goto disconnect;
  2305. }
  2306. if (cmp_after_sb(p_after_sb_0p, mdev->net_conf->after_sb_0p)) {
  2307. dev_err(DEV, "incompatible after-sb-0pri settings\n");
  2308. goto disconnect;
  2309. }
  2310. if (cmp_after_sb(p_after_sb_1p, mdev->net_conf->after_sb_1p)) {
  2311. dev_err(DEV, "incompatible after-sb-1pri settings\n");
  2312. goto disconnect;
  2313. }
  2314. if (cmp_after_sb(p_after_sb_2p, mdev->net_conf->after_sb_2p)) {
  2315. dev_err(DEV, "incompatible after-sb-2pri settings\n");
  2316. goto disconnect;
  2317. }
  2318. if (p_want_lose && mdev->net_conf->want_lose) {
  2319. dev_err(DEV, "both sides have the 'want_lose' flag set\n");
  2320. goto disconnect;
  2321. }
  2322. if (p_two_primaries != mdev->net_conf->two_primaries) {
  2323. dev_err(DEV, "incompatible setting of the two-primaries options\n");
  2324. goto disconnect;
  2325. }
  2326. if (mdev->agreed_pro_version >= 87) {
  2327. unsigned char *my_alg = mdev->net_conf->integrity_alg;
  2328. if (drbd_recv(mdev, p_integrity_alg, data_size) != data_size)
  2329. return false;
  2330. p_integrity_alg[SHARED_SECRET_MAX-1] = 0;
  2331. if (strcmp(p_integrity_alg, my_alg)) {
  2332. dev_err(DEV, "incompatible setting of the data-integrity-alg\n");
  2333. goto disconnect;
  2334. }
  2335. dev_info(DEV, "data-integrity-alg: %s\n",
  2336. my_alg[0] ? my_alg : (unsigned char *)"<not-used>");
  2337. }
  2338. return true;
  2339. disconnect:
  2340. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  2341. return false;
  2342. }
  2343. /* helper function
  2344. * input: alg name, feature name
  2345. * return: NULL (alg name was "")
  2346. * ERR_PTR(error) if something goes wrong
  2347. * or the crypto hash ptr, if it worked out ok. */
  2348. struct crypto_hash *drbd_crypto_alloc_digest_safe(const struct drbd_conf *mdev,
  2349. const char *alg, const char *name)
  2350. {
  2351. struct crypto_hash *tfm;
  2352. if (!alg[0])
  2353. return NULL;
  2354. tfm = crypto_alloc_hash(alg, 0, CRYPTO_ALG_ASYNC);
  2355. if (IS_ERR(tfm)) {
  2356. dev_err(DEV, "Can not allocate \"%s\" as %s (reason: %ld)\n",
  2357. alg, name, PTR_ERR(tfm));
  2358. return tfm;
  2359. }
  2360. if (!drbd_crypto_is_hash(crypto_hash_tfm(tfm))) {
  2361. crypto_free_hash(tfm);
  2362. dev_err(DEV, "\"%s\" is not a digest (%s)\n", alg, name);
  2363. return ERR_PTR(-EINVAL);
  2364. }
  2365. return tfm;
  2366. }
  2367. static int receive_SyncParam(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int packet_size)
  2368. {
  2369. int ok = true;
  2370. struct p_rs_param_95 *p = &mdev->data.rbuf.rs_param_95;
  2371. unsigned int header_size, data_size, exp_max_sz;
  2372. struct crypto_hash *verify_tfm = NULL;
  2373. struct crypto_hash *csums_tfm = NULL;
  2374. const int apv = mdev->agreed_pro_version;
  2375. int *rs_plan_s = NULL;
  2376. int fifo_size = 0;
  2377. exp_max_sz = apv <= 87 ? sizeof(struct p_rs_param)
  2378. : apv == 88 ? sizeof(struct p_rs_param)
  2379. + SHARED_SECRET_MAX
  2380. : apv <= 94 ? sizeof(struct p_rs_param_89)
  2381. : /* apv >= 95 */ sizeof(struct p_rs_param_95);
  2382. if (packet_size > exp_max_sz) {
  2383. dev_err(DEV, "SyncParam packet too long: received %u, expected <= %u bytes\n",
  2384. packet_size, exp_max_sz);
  2385. return false;
  2386. }
  2387. if (apv <= 88) {
  2388. header_size = sizeof(struct p_rs_param) - sizeof(struct p_header80);
  2389. data_size = packet_size - header_size;
  2390. } else if (apv <= 94) {
  2391. header_size = sizeof(struct p_rs_param_89) - sizeof(struct p_header80);
  2392. data_size = packet_size - header_size;
  2393. D_ASSERT(data_size == 0);
  2394. } else {
  2395. header_size = sizeof(struct p_rs_param_95) - sizeof(struct p_header80);
  2396. data_size = packet_size - header_size;
  2397. D_ASSERT(data_size == 0);
  2398. }
  2399. /* initialize verify_alg and csums_alg */
  2400. memset(p->verify_alg, 0, 2 * SHARED_SECRET_MAX);
  2401. if (drbd_recv(mdev, &p->head.payload, header_size) != header_size)
  2402. return false;
  2403. mdev->sync_conf.rate = be32_to_cpu(p->rate);
  2404. if (apv >= 88) {
  2405. if (apv == 88) {
  2406. if (data_size > SHARED_SECRET_MAX) {
  2407. dev_err(DEV, "verify-alg too long, "
  2408. "peer wants %u, accepting only %u byte\n",
  2409. data_size, SHARED_SECRET_MAX);
  2410. return false;
  2411. }
  2412. if (drbd_recv(mdev, p->verify_alg, data_size) != data_size)
  2413. return false;
  2414. /* we expect NUL terminated string */
  2415. /* but just in case someone tries to be evil */
  2416. D_ASSERT(p->verify_alg[data_size-1] == 0);
  2417. p->verify_alg[data_size-1] = 0;
  2418. } else /* apv >= 89 */ {
  2419. /* we still expect NUL terminated strings */
  2420. /* but just in case someone tries to be evil */
  2421. D_ASSERT(p->verify_alg[SHARED_SECRET_MAX-1] == 0);
  2422. D_ASSERT(p->csums_alg[SHARED_SECRET_MAX-1] == 0);
  2423. p->verify_alg[SHARED_SECRET_MAX-1] = 0;
  2424. p->csums_alg[SHARED_SECRET_MAX-1] = 0;
  2425. }
  2426. if (strcmp(mdev->sync_conf.verify_alg, p->verify_alg)) {
  2427. if (mdev->state.conn == C_WF_REPORT_PARAMS) {
  2428. dev_err(DEV, "Different verify-alg settings. me=\"%s\" peer=\"%s\"\n",
  2429. mdev->sync_conf.verify_alg, p->verify_alg);
  2430. goto disconnect;
  2431. }
  2432. verify_tfm = drbd_crypto_alloc_digest_safe(mdev,
  2433. p->verify_alg, "verify-alg");
  2434. if (IS_ERR(verify_tfm)) {
  2435. verify_tfm = NULL;
  2436. goto disconnect;
  2437. }
  2438. }
  2439. if (apv >= 89 && strcmp(mdev->sync_conf.csums_alg, p->csums_alg)) {
  2440. if (mdev->state.conn == C_WF_REPORT_PARAMS) {
  2441. dev_err(DEV, "Different csums-alg settings. me=\"%s\" peer=\"%s\"\n",
  2442. mdev->sync_conf.csums_alg, p->csums_alg);
  2443. goto disconnect;
  2444. }
  2445. csums_tfm = drbd_crypto_alloc_digest_safe(mdev,
  2446. p->csums_alg, "csums-alg");
  2447. if (IS_ERR(csums_tfm)) {
  2448. csums_tfm = NULL;
  2449. goto disconnect;
  2450. }
  2451. }
  2452. if (apv > 94) {
  2453. mdev->sync_conf.rate = be32_to_cpu(p->rate);
  2454. mdev->sync_conf.c_plan_ahead = be32_to_cpu(p->c_plan_ahead);
  2455. mdev->sync_conf.c_delay_target = be32_to_cpu(p->c_delay_target);
  2456. mdev->sync_conf.c_fill_target = be32_to_cpu(p->c_fill_target);
  2457. mdev->sync_conf.c_max_rate = be32_to_cpu(p->c_max_rate);
  2458. fifo_size = (mdev->sync_conf.c_plan_ahead * 10 * SLEEP_TIME) / HZ;
  2459. if (fifo_size != mdev->rs_plan_s.size && fifo_size > 0) {
  2460. rs_plan_s = kzalloc(sizeof(int) * fifo_size, GFP_KERNEL);
  2461. if (!rs_plan_s) {
  2462. dev_err(DEV, "kmalloc of fifo_buffer failed");
  2463. goto disconnect;
  2464. }
  2465. }
  2466. }
  2467. spin_lock(&mdev->peer_seq_lock);
  2468. /* lock against drbd_nl_syncer_conf() */
  2469. if (verify_tfm) {
  2470. strcpy(mdev->sync_conf.verify_alg, p->verify_alg);
  2471. mdev->sync_conf.verify_alg_len = strlen(p->verify_alg) + 1;
  2472. crypto_free_hash(mdev->verify_tfm);
  2473. mdev->verify_tfm = verify_tfm;
  2474. dev_info(DEV, "using verify-alg: \"%s\"\n", p->verify_alg);
  2475. }
  2476. if (csums_tfm) {
  2477. strcpy(mdev->sync_conf.csums_alg, p->csums_alg);
  2478. mdev->sync_conf.csums_alg_len = strlen(p->csums_alg) + 1;
  2479. crypto_free_hash(mdev->csums_tfm);
  2480. mdev->csums_tfm = csums_tfm;
  2481. dev_info(DEV, "using csums-alg: \"%s\"\n", p->csums_alg);
  2482. }
  2483. if (fifo_size != mdev->rs_plan_s.size) {
  2484. kfree(mdev->rs_plan_s.values);
  2485. mdev->rs_plan_s.values = rs_plan_s;
  2486. mdev->rs_plan_s.size = fifo_size;
  2487. mdev->rs_planed = 0;
  2488. }
  2489. spin_unlock(&mdev->peer_seq_lock);
  2490. }
  2491. return ok;
  2492. disconnect:
  2493. /* just for completeness: actually not needed,
  2494. * as this is not reached if csums_tfm was ok. */
  2495. crypto_free_hash(csums_tfm);
  2496. /* but free the verify_tfm again, if csums_tfm did not work out */
  2497. crypto_free_hash(verify_tfm);
  2498. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  2499. return false;
  2500. }
  2501. /* warn if the arguments differ by more than 12.5% */
  2502. static void warn_if_differ_considerably(struct drbd_conf *mdev,
  2503. const char *s, sector_t a, sector_t b)
  2504. {
  2505. sector_t d;
  2506. if (a == 0 || b == 0)
  2507. return;
  2508. d = (a > b) ? (a - b) : (b - a);
  2509. if (d > (a>>3) || d > (b>>3))
  2510. dev_warn(DEV, "Considerable difference in %s: %llus vs. %llus\n", s,
  2511. (unsigned long long)a, (unsigned long long)b);
  2512. }
  2513. static int receive_sizes(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  2514. {
  2515. struct p_sizes *p = &mdev->data.rbuf.sizes;
  2516. enum determine_dev_size dd = unchanged;
  2517. sector_t p_size, p_usize, my_usize;
  2518. int ldsc = 0; /* local disk size changed */
  2519. enum dds_flags ddsf;
  2520. p_size = be64_to_cpu(p->d_size);
  2521. p_usize = be64_to_cpu(p->u_size);
  2522. if (p_size == 0 && mdev->state.disk == D_DISKLESS) {
  2523. dev_err(DEV, "some backing storage is needed\n");
  2524. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  2525. return false;
  2526. }
  2527. /* just store the peer's disk size for now.
  2528. * we still need to figure out whether we accept that. */
  2529. mdev->p_size = p_size;
  2530. if (get_ldev(mdev)) {
  2531. warn_if_differ_considerably(mdev, "lower level device sizes",
  2532. p_size, drbd_get_max_capacity(mdev->ldev));
  2533. warn_if_differ_considerably(mdev, "user requested size",
  2534. p_usize, mdev->ldev->dc.disk_size);
  2535. /* if this is the first connect, or an otherwise expected
  2536. * param exchange, choose the minimum */
  2537. if (mdev->state.conn == C_WF_REPORT_PARAMS)
  2538. p_usize = min_not_zero((sector_t)mdev->ldev->dc.disk_size,
  2539. p_usize);
  2540. my_usize = mdev->ldev->dc.disk_size;
  2541. if (mdev->ldev->dc.disk_size != p_usize) {
  2542. mdev->ldev->dc.disk_size = p_usize;
  2543. dev_info(DEV, "Peer sets u_size to %lu sectors\n",
  2544. (unsigned long)mdev->ldev->dc.disk_size);
  2545. }
  2546. /* Never shrink a device with usable data during connect.
  2547. But allow online shrinking if we are connected. */
  2548. if (drbd_new_dev_size(mdev, mdev->ldev, 0) <
  2549. drbd_get_capacity(mdev->this_bdev) &&
  2550. mdev->state.disk >= D_OUTDATED &&
  2551. mdev->state.conn < C_CONNECTED) {
  2552. dev_err(DEV, "The peer's disk size is too small!\n");
  2553. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  2554. mdev->ldev->dc.disk_size = my_usize;
  2555. put_ldev(mdev);
  2556. return false;
  2557. }
  2558. put_ldev(mdev);
  2559. }
  2560. ddsf = be16_to_cpu(p->dds_flags);
  2561. if (get_ldev(mdev)) {
  2562. dd = drbd_determine_dev_size(mdev, ddsf);
  2563. put_ldev(mdev);
  2564. if (dd == dev_size_error)
  2565. return false;
  2566. drbd_md_sync(mdev);
  2567. } else {
  2568. /* I am diskless, need to accept the peer's size. */
  2569. drbd_set_my_capacity(mdev, p_size);
  2570. }
  2571. mdev->peer_max_bio_size = be32_to_cpu(p->max_bio_size);
  2572. drbd_reconsider_max_bio_size(mdev);
  2573. if (get_ldev(mdev)) {
  2574. if (mdev->ldev->known_size != drbd_get_capacity(mdev->ldev->backing_bdev)) {
  2575. mdev->ldev->known_size = drbd_get_capacity(mdev->ldev->backing_bdev);
  2576. ldsc = 1;
  2577. }
  2578. put_ldev(mdev);
  2579. }
  2580. if (mdev->state.conn > C_WF_REPORT_PARAMS) {
  2581. if (be64_to_cpu(p->c_size) !=
  2582. drbd_get_capacity(mdev->this_bdev) || ldsc) {
  2583. /* we have different sizes, probably peer
  2584. * needs to know my new size... */
  2585. drbd_send_sizes(mdev, 0, ddsf);
  2586. }
  2587. if (test_and_clear_bit(RESIZE_PENDING, &mdev->flags) ||
  2588. (dd == grew && mdev->state.conn == C_CONNECTED)) {
  2589. if (mdev->state.pdsk >= D_INCONSISTENT &&
  2590. mdev->state.disk >= D_INCONSISTENT) {
  2591. if (ddsf & DDSF_NO_RESYNC)
  2592. dev_info(DEV, "Resync of new storage suppressed with --assume-clean\n");
  2593. else
  2594. resync_after_online_grow(mdev);
  2595. } else
  2596. set_bit(RESYNC_AFTER_NEG, &mdev->flags);
  2597. }
  2598. }
  2599. return true;
  2600. }
  2601. static int receive_uuids(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  2602. {
  2603. struct p_uuids *p = &mdev->data.rbuf.uuids;
  2604. u64 *p_uuid;
  2605. int i, updated_uuids = 0;
  2606. p_uuid = kmalloc(sizeof(u64)*UI_EXTENDED_SIZE, GFP_NOIO);
  2607. for (i = UI_CURRENT; i < UI_EXTENDED_SIZE; i++)
  2608. p_uuid[i] = be64_to_cpu(p->uuid[i]);
  2609. kfree(mdev->p_uuid);
  2610. mdev->p_uuid = p_uuid;
  2611. if (mdev->state.conn < C_CONNECTED &&
  2612. mdev->state.disk < D_INCONSISTENT &&
  2613. mdev->state.role == R_PRIMARY &&
  2614. (mdev->ed_uuid & ~((u64)1)) != (p_uuid[UI_CURRENT] & ~((u64)1))) {
  2615. dev_err(DEV, "Can only connect to data with current UUID=%016llX\n",
  2616. (unsigned long long)mdev->ed_uuid);
  2617. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  2618. return false;
  2619. }
  2620. if (get_ldev(mdev)) {
  2621. int skip_initial_sync =
  2622. mdev->state.conn == C_CONNECTED &&
  2623. mdev->agreed_pro_version >= 90 &&
  2624. mdev->ldev->md.uuid[UI_CURRENT] == UUID_JUST_CREATED &&
  2625. (p_uuid[UI_FLAGS] & 8);
  2626. if (skip_initial_sync) {
  2627. dev_info(DEV, "Accepted new current UUID, preparing to skip initial sync\n");
  2628. drbd_bitmap_io(mdev, &drbd_bmio_clear_n_write,
  2629. "clear_n_write from receive_uuids",
  2630. BM_LOCKED_TEST_ALLOWED);
  2631. _drbd_uuid_set(mdev, UI_CURRENT, p_uuid[UI_CURRENT]);
  2632. _drbd_uuid_set(mdev, UI_BITMAP, 0);
  2633. _drbd_set_state(_NS2(mdev, disk, D_UP_TO_DATE, pdsk, D_UP_TO_DATE),
  2634. CS_VERBOSE, NULL);
  2635. drbd_md_sync(mdev);
  2636. updated_uuids = 1;
  2637. }
  2638. put_ldev(mdev);
  2639. } else if (mdev->state.disk < D_INCONSISTENT &&
  2640. mdev->state.role == R_PRIMARY) {
  2641. /* I am a diskless primary, the peer just created a new current UUID
  2642. for me. */
  2643. updated_uuids = drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
  2644. }
  2645. /* Before we test for the disk state, we should wait until an eventually
  2646. ongoing cluster wide state change is finished. That is important if
  2647. we are primary and are detaching from our disk. We need to see the
  2648. new disk state... */
  2649. wait_event(mdev->misc_wait, !test_bit(CLUSTER_ST_CHANGE, &mdev->flags));
  2650. if (mdev->state.conn >= C_CONNECTED && mdev->state.disk < D_INCONSISTENT)
  2651. updated_uuids |= drbd_set_ed_uuid(mdev, p_uuid[UI_CURRENT]);
  2652. if (updated_uuids)
  2653. drbd_print_uuids(mdev, "receiver updated UUIDs to");
  2654. return true;
  2655. }
  2656. /**
  2657. * convert_state() - Converts the peer's view of the cluster state to our point of view
  2658. * @ps: The state as seen by the peer.
  2659. */
  2660. static union drbd_state convert_state(union drbd_state ps)
  2661. {
  2662. union drbd_state ms;
  2663. static enum drbd_conns c_tab[] = {
  2664. [C_CONNECTED] = C_CONNECTED,
  2665. [C_STARTING_SYNC_S] = C_STARTING_SYNC_T,
  2666. [C_STARTING_SYNC_T] = C_STARTING_SYNC_S,
  2667. [C_DISCONNECTING] = C_TEAR_DOWN, /* C_NETWORK_FAILURE, */
  2668. [C_VERIFY_S] = C_VERIFY_T,
  2669. [C_MASK] = C_MASK,
  2670. };
  2671. ms.i = ps.i;
  2672. ms.conn = c_tab[ps.conn];
  2673. ms.peer = ps.role;
  2674. ms.role = ps.peer;
  2675. ms.pdsk = ps.disk;
  2676. ms.disk = ps.pdsk;
  2677. ms.peer_isp = (ps.aftr_isp | ps.user_isp);
  2678. return ms;
  2679. }
  2680. static int receive_req_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  2681. {
  2682. struct p_req_state *p = &mdev->data.rbuf.req_state;
  2683. union drbd_state mask, val;
  2684. enum drbd_state_rv rv;
  2685. mask.i = be32_to_cpu(p->mask);
  2686. val.i = be32_to_cpu(p->val);
  2687. if (test_bit(DISCARD_CONCURRENT, &mdev->flags) &&
  2688. test_bit(CLUSTER_ST_CHANGE, &mdev->flags)) {
  2689. drbd_send_sr_reply(mdev, SS_CONCURRENT_ST_CHG);
  2690. return true;
  2691. }
  2692. mask = convert_state(mask);
  2693. val = convert_state(val);
  2694. rv = drbd_change_state(mdev, CS_VERBOSE, mask, val);
  2695. drbd_send_sr_reply(mdev, rv);
  2696. drbd_md_sync(mdev);
  2697. return true;
  2698. }
  2699. static int receive_state(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  2700. {
  2701. struct p_state *p = &mdev->data.rbuf.state;
  2702. union drbd_state os, ns, peer_state;
  2703. enum drbd_disk_state real_peer_disk;
  2704. enum chg_state_flags cs_flags;
  2705. int rv;
  2706. peer_state.i = be32_to_cpu(p->state);
  2707. real_peer_disk = peer_state.disk;
  2708. if (peer_state.disk == D_NEGOTIATING) {
  2709. real_peer_disk = mdev->p_uuid[UI_FLAGS] & 4 ? D_INCONSISTENT : D_CONSISTENT;
  2710. dev_info(DEV, "real peer disk state = %s\n", drbd_disk_str(real_peer_disk));
  2711. }
  2712. spin_lock_irq(&mdev->req_lock);
  2713. retry:
  2714. os = ns = mdev->state;
  2715. spin_unlock_irq(&mdev->req_lock);
  2716. /* peer says his disk is uptodate, while we think it is inconsistent,
  2717. * and this happens while we think we have a sync going on. */
  2718. if (os.pdsk == D_INCONSISTENT && real_peer_disk == D_UP_TO_DATE &&
  2719. os.conn > C_CONNECTED && os.disk == D_UP_TO_DATE) {
  2720. /* If we are (becoming) SyncSource, but peer is still in sync
  2721. * preparation, ignore its uptodate-ness to avoid flapping, it
  2722. * will change to inconsistent once the peer reaches active
  2723. * syncing states.
  2724. * It may have changed syncer-paused flags, however, so we
  2725. * cannot ignore this completely. */
  2726. if (peer_state.conn > C_CONNECTED &&
  2727. peer_state.conn < C_SYNC_SOURCE)
  2728. real_peer_disk = D_INCONSISTENT;
  2729. /* if peer_state changes to connected at the same time,
  2730. * it explicitly notifies us that it finished resync.
  2731. * Maybe we should finish it up, too? */
  2732. else if (os.conn >= C_SYNC_SOURCE &&
  2733. peer_state.conn == C_CONNECTED) {
  2734. if (drbd_bm_total_weight(mdev) <= mdev->rs_failed)
  2735. drbd_resync_finished(mdev);
  2736. return true;
  2737. }
  2738. }
  2739. /* peer says his disk is inconsistent, while we think it is uptodate,
  2740. * and this happens while the peer still thinks we have a sync going on,
  2741. * but we think we are already done with the sync.
  2742. * We ignore this to avoid flapping pdsk.
  2743. * This should not happen, if the peer is a recent version of drbd. */
  2744. if (os.pdsk == D_UP_TO_DATE && real_peer_disk == D_INCONSISTENT &&
  2745. os.conn == C_CONNECTED && peer_state.conn > C_SYNC_SOURCE)
  2746. real_peer_disk = D_UP_TO_DATE;
  2747. if (ns.conn == C_WF_REPORT_PARAMS)
  2748. ns.conn = C_CONNECTED;
  2749. if (peer_state.conn == C_AHEAD)
  2750. ns.conn = C_BEHIND;
  2751. if (mdev->p_uuid && peer_state.disk >= D_NEGOTIATING &&
  2752. get_ldev_if_state(mdev, D_NEGOTIATING)) {
  2753. int cr; /* consider resync */
  2754. /* if we established a new connection */
  2755. cr = (os.conn < C_CONNECTED);
  2756. /* if we had an established connection
  2757. * and one of the nodes newly attaches a disk */
  2758. cr |= (os.conn == C_CONNECTED &&
  2759. (peer_state.disk == D_NEGOTIATING ||
  2760. os.disk == D_NEGOTIATING));
  2761. /* if we have both been inconsistent, and the peer has been
  2762. * forced to be UpToDate with --overwrite-data */
  2763. cr |= test_bit(CONSIDER_RESYNC, &mdev->flags);
  2764. /* if we had been plain connected, and the admin requested to
  2765. * start a sync by "invalidate" or "invalidate-remote" */
  2766. cr |= (os.conn == C_CONNECTED &&
  2767. (peer_state.conn >= C_STARTING_SYNC_S &&
  2768. peer_state.conn <= C_WF_BITMAP_T));
  2769. if (cr)
  2770. ns.conn = drbd_sync_handshake(mdev, peer_state.role, real_peer_disk);
  2771. put_ldev(mdev);
  2772. if (ns.conn == C_MASK) {
  2773. ns.conn = C_CONNECTED;
  2774. if (mdev->state.disk == D_NEGOTIATING) {
  2775. drbd_force_state(mdev, NS(disk, D_FAILED));
  2776. } else if (peer_state.disk == D_NEGOTIATING) {
  2777. dev_err(DEV, "Disk attach process on the peer node was aborted.\n");
  2778. peer_state.disk = D_DISKLESS;
  2779. real_peer_disk = D_DISKLESS;
  2780. } else {
  2781. if (test_and_clear_bit(CONN_DRY_RUN, &mdev->flags))
  2782. return false;
  2783. D_ASSERT(os.conn == C_WF_REPORT_PARAMS);
  2784. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  2785. return false;
  2786. }
  2787. }
  2788. }
  2789. spin_lock_irq(&mdev->req_lock);
  2790. if (mdev->state.i != os.i)
  2791. goto retry;
  2792. clear_bit(CONSIDER_RESYNC, &mdev->flags);
  2793. ns.peer = peer_state.role;
  2794. ns.pdsk = real_peer_disk;
  2795. ns.peer_isp = (peer_state.aftr_isp | peer_state.user_isp);
  2796. if ((ns.conn == C_CONNECTED || ns.conn == C_WF_BITMAP_S) && ns.disk == D_NEGOTIATING)
  2797. ns.disk = mdev->new_state_tmp.disk;
  2798. cs_flags = CS_VERBOSE + (os.conn < C_CONNECTED && ns.conn >= C_CONNECTED ? 0 : CS_HARD);
  2799. if (ns.pdsk == D_CONSISTENT && is_susp(ns) && ns.conn == C_CONNECTED && os.conn < C_CONNECTED &&
  2800. test_bit(NEW_CUR_UUID, &mdev->flags)) {
  2801. /* Do not allow tl_restart(resend) for a rebooted peer. We can only allow this
  2802. for temporal network outages! */
  2803. spin_unlock_irq(&mdev->req_lock);
  2804. dev_err(DEV, "Aborting Connect, can not thaw IO with an only Consistent peer\n");
  2805. tl_clear(mdev);
  2806. drbd_uuid_new_current(mdev);
  2807. clear_bit(NEW_CUR_UUID, &mdev->flags);
  2808. drbd_force_state(mdev, NS2(conn, C_PROTOCOL_ERROR, susp, 0));
  2809. return false;
  2810. }
  2811. rv = _drbd_set_state(mdev, ns, cs_flags, NULL);
  2812. ns = mdev->state;
  2813. spin_unlock_irq(&mdev->req_lock);
  2814. if (rv < SS_SUCCESS) {
  2815. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  2816. return false;
  2817. }
  2818. if (os.conn > C_WF_REPORT_PARAMS) {
  2819. if (ns.conn > C_CONNECTED && peer_state.conn <= C_CONNECTED &&
  2820. peer_state.disk != D_NEGOTIATING ) {
  2821. /* we want resync, peer has not yet decided to sync... */
  2822. /* Nowadays only used when forcing a node into primary role and
  2823. setting its disk to UpToDate with that */
  2824. drbd_send_uuids(mdev);
  2825. drbd_send_state(mdev);
  2826. }
  2827. }
  2828. mdev->net_conf->want_lose = 0;
  2829. drbd_md_sync(mdev); /* update connected indicator, la_size, ... */
  2830. return true;
  2831. }
  2832. static int receive_sync_uuid(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  2833. {
  2834. struct p_rs_uuid *p = &mdev->data.rbuf.rs_uuid;
  2835. wait_event(mdev->misc_wait,
  2836. mdev->state.conn == C_WF_SYNC_UUID ||
  2837. mdev->state.conn == C_BEHIND ||
  2838. mdev->state.conn < C_CONNECTED ||
  2839. mdev->state.disk < D_NEGOTIATING);
  2840. /* D_ASSERT( mdev->state.conn == C_WF_SYNC_UUID ); */
  2841. /* Here the _drbd_uuid_ functions are right, current should
  2842. _not_ be rotated into the history */
  2843. if (get_ldev_if_state(mdev, D_NEGOTIATING)) {
  2844. _drbd_uuid_set(mdev, UI_CURRENT, be64_to_cpu(p->uuid));
  2845. _drbd_uuid_set(mdev, UI_BITMAP, 0UL);
  2846. drbd_print_uuids(mdev, "updated sync uuid");
  2847. drbd_start_resync(mdev, C_SYNC_TARGET);
  2848. put_ldev(mdev);
  2849. } else
  2850. dev_err(DEV, "Ignoring SyncUUID packet!\n");
  2851. return true;
  2852. }
  2853. /**
  2854. * receive_bitmap_plain
  2855. *
  2856. * Return 0 when done, 1 when another iteration is needed, and a negative error
  2857. * code upon failure.
  2858. */
  2859. static int
  2860. receive_bitmap_plain(struct drbd_conf *mdev, unsigned int data_size,
  2861. unsigned long *buffer, struct bm_xfer_ctx *c)
  2862. {
  2863. unsigned num_words = min_t(size_t, BM_PACKET_WORDS, c->bm_words - c->word_offset);
  2864. unsigned want = num_words * sizeof(long);
  2865. int err;
  2866. if (want != data_size) {
  2867. dev_err(DEV, "%s:want (%u) != data_size (%u)\n", __func__, want, data_size);
  2868. return -EIO;
  2869. }
  2870. if (want == 0)
  2871. return 0;
  2872. err = drbd_recv(mdev, buffer, want);
  2873. if (err != want) {
  2874. if (err >= 0)
  2875. err = -EIO;
  2876. return err;
  2877. }
  2878. drbd_bm_merge_lel(mdev, c->word_offset, num_words, buffer);
  2879. c->word_offset += num_words;
  2880. c->bit_offset = c->word_offset * BITS_PER_LONG;
  2881. if (c->bit_offset > c->bm_bits)
  2882. c->bit_offset = c->bm_bits;
  2883. return 1;
  2884. }
  2885. /**
  2886. * recv_bm_rle_bits
  2887. *
  2888. * Return 0 when done, 1 when another iteration is needed, and a negative error
  2889. * code upon failure.
  2890. */
  2891. static int
  2892. recv_bm_rle_bits(struct drbd_conf *mdev,
  2893. struct p_compressed_bm *p,
  2894. struct bm_xfer_ctx *c)
  2895. {
  2896. struct bitstream bs;
  2897. u64 look_ahead;
  2898. u64 rl;
  2899. u64 tmp;
  2900. unsigned long s = c->bit_offset;
  2901. unsigned long e;
  2902. int len = be16_to_cpu(p->head.length) - (sizeof(*p) - sizeof(p->head));
  2903. int toggle = DCBP_get_start(p);
  2904. int have;
  2905. int bits;
  2906. bitstream_init(&bs, p->code, len, DCBP_get_pad_bits(p));
  2907. bits = bitstream_get_bits(&bs, &look_ahead, 64);
  2908. if (bits < 0)
  2909. return -EIO;
  2910. for (have = bits; have > 0; s += rl, toggle = !toggle) {
  2911. bits = vli_decode_bits(&rl, look_ahead);
  2912. if (bits <= 0)
  2913. return -EIO;
  2914. if (toggle) {
  2915. e = s + rl -1;
  2916. if (e >= c->bm_bits) {
  2917. dev_err(DEV, "bitmap overflow (e:%lu) while decoding bm RLE packet\n", e);
  2918. return -EIO;
  2919. }
  2920. _drbd_bm_set_bits(mdev, s, e);
  2921. }
  2922. if (have < bits) {
  2923. dev_err(DEV, "bitmap decoding error: h:%d b:%d la:0x%08llx l:%u/%u\n",
  2924. have, bits, look_ahead,
  2925. (unsigned int)(bs.cur.b - p->code),
  2926. (unsigned int)bs.buf_len);
  2927. return -EIO;
  2928. }
  2929. look_ahead >>= bits;
  2930. have -= bits;
  2931. bits = bitstream_get_bits(&bs, &tmp, 64 - have);
  2932. if (bits < 0)
  2933. return -EIO;
  2934. look_ahead |= tmp << have;
  2935. have += bits;
  2936. }
  2937. c->bit_offset = s;
  2938. bm_xfer_ctx_bit_to_word_offset(c);
  2939. return (s != c->bm_bits);
  2940. }
  2941. /**
  2942. * decode_bitmap_c
  2943. *
  2944. * Return 0 when done, 1 when another iteration is needed, and a negative error
  2945. * code upon failure.
  2946. */
  2947. static int
  2948. decode_bitmap_c(struct drbd_conf *mdev,
  2949. struct p_compressed_bm *p,
  2950. struct bm_xfer_ctx *c)
  2951. {
  2952. if (DCBP_get_code(p) == RLE_VLI_Bits)
  2953. return recv_bm_rle_bits(mdev, p, c);
  2954. /* other variants had been implemented for evaluation,
  2955. * but have been dropped as this one turned out to be "best"
  2956. * during all our tests. */
  2957. dev_err(DEV, "receive_bitmap_c: unknown encoding %u\n", p->encoding);
  2958. drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
  2959. return -EIO;
  2960. }
  2961. void INFO_bm_xfer_stats(struct drbd_conf *mdev,
  2962. const char *direction, struct bm_xfer_ctx *c)
  2963. {
  2964. /* what would it take to transfer it "plaintext" */
  2965. unsigned plain = sizeof(struct p_header80) *
  2966. ((c->bm_words+BM_PACKET_WORDS-1)/BM_PACKET_WORDS+1)
  2967. + c->bm_words * sizeof(long);
  2968. unsigned total = c->bytes[0] + c->bytes[1];
  2969. unsigned r;
  2970. /* total can not be zero. but just in case: */
  2971. if (total == 0)
  2972. return;
  2973. /* don't report if not compressed */
  2974. if (total >= plain)
  2975. return;
  2976. /* total < plain. check for overflow, still */
  2977. r = (total > UINT_MAX/1000) ? (total / (plain/1000))
  2978. : (1000 * total / plain);
  2979. if (r > 1000)
  2980. r = 1000;
  2981. r = 1000 - r;
  2982. dev_info(DEV, "%s bitmap stats [Bytes(packets)]: plain %u(%u), RLE %u(%u), "
  2983. "total %u; compression: %u.%u%%\n",
  2984. direction,
  2985. c->bytes[1], c->packets[1],
  2986. c->bytes[0], c->packets[0],
  2987. total, r/10, r % 10);
  2988. }
  2989. /* Since we are processing the bitfield from lower addresses to higher,
  2990. it does not matter if the process it in 32 bit chunks or 64 bit
  2991. chunks as long as it is little endian. (Understand it as byte stream,
  2992. beginning with the lowest byte...) If we would use big endian
  2993. we would need to process it from the highest address to the lowest,
  2994. in order to be agnostic to the 32 vs 64 bits issue.
  2995. returns 0 on failure, 1 if we successfully received it. */
  2996. static int receive_bitmap(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  2997. {
  2998. struct bm_xfer_ctx c;
  2999. void *buffer;
  3000. int err;
  3001. int ok = false;
  3002. struct p_header80 *h = &mdev->data.rbuf.header.h80;
  3003. drbd_bm_lock(mdev, "receive bitmap", BM_LOCKED_SET_ALLOWED);
  3004. /* you are supposed to send additional out-of-sync information
  3005. * if you actually set bits during this phase */
  3006. /* maybe we should use some per thread scratch page,
  3007. * and allocate that during initial device creation? */
  3008. buffer = (unsigned long *) __get_free_page(GFP_NOIO);
  3009. if (!buffer) {
  3010. dev_err(DEV, "failed to allocate one page buffer in %s\n", __func__);
  3011. goto out;
  3012. }
  3013. c = (struct bm_xfer_ctx) {
  3014. .bm_bits = drbd_bm_bits(mdev),
  3015. .bm_words = drbd_bm_words(mdev),
  3016. };
  3017. for(;;) {
  3018. if (cmd == P_BITMAP) {
  3019. err = receive_bitmap_plain(mdev, data_size, buffer, &c);
  3020. } else if (cmd == P_COMPRESSED_BITMAP) {
  3021. /* MAYBE: sanity check that we speak proto >= 90,
  3022. * and the feature is enabled! */
  3023. struct p_compressed_bm *p;
  3024. if (data_size > BM_PACKET_PAYLOAD_BYTES) {
  3025. dev_err(DEV, "ReportCBitmap packet too large\n");
  3026. goto out;
  3027. }
  3028. /* use the page buff */
  3029. p = buffer;
  3030. memcpy(p, h, sizeof(*h));
  3031. if (drbd_recv(mdev, p->head.payload, data_size) != data_size)
  3032. goto out;
  3033. if (data_size <= (sizeof(*p) - sizeof(p->head))) {
  3034. dev_err(DEV, "ReportCBitmap packet too small (l:%u)\n", data_size);
  3035. goto out;
  3036. }
  3037. err = decode_bitmap_c(mdev, p, &c);
  3038. } else {
  3039. dev_warn(DEV, "receive_bitmap: cmd neither ReportBitMap nor ReportCBitMap (is 0x%x)", cmd);
  3040. goto out;
  3041. }
  3042. c.packets[cmd == P_BITMAP]++;
  3043. c.bytes[cmd == P_BITMAP] += sizeof(struct p_header80) + data_size;
  3044. if (err <= 0) {
  3045. if (err < 0)
  3046. goto out;
  3047. break;
  3048. }
  3049. if (!drbd_recv_header(mdev, &cmd, &data_size))
  3050. goto out;
  3051. }
  3052. INFO_bm_xfer_stats(mdev, "receive", &c);
  3053. if (mdev->state.conn == C_WF_BITMAP_T) {
  3054. enum drbd_state_rv rv;
  3055. ok = !drbd_send_bitmap(mdev);
  3056. if (!ok)
  3057. goto out;
  3058. /* Omit CS_ORDERED with this state transition to avoid deadlocks. */
  3059. rv = _drbd_request_state(mdev, NS(conn, C_WF_SYNC_UUID), CS_VERBOSE);
  3060. D_ASSERT(rv == SS_SUCCESS);
  3061. } else if (mdev->state.conn != C_WF_BITMAP_S) {
  3062. /* admin may have requested C_DISCONNECTING,
  3063. * other threads may have noticed network errors */
  3064. dev_info(DEV, "unexpected cstate (%s) in receive_bitmap\n",
  3065. drbd_conn_str(mdev->state.conn));
  3066. }
  3067. ok = true;
  3068. out:
  3069. drbd_bm_unlock(mdev);
  3070. if (ok && mdev->state.conn == C_WF_BITMAP_S)
  3071. drbd_start_resync(mdev, C_SYNC_SOURCE);
  3072. free_page((unsigned long) buffer);
  3073. return ok;
  3074. }
  3075. static int receive_skip(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  3076. {
  3077. /* TODO zero copy sink :) */
  3078. static char sink[128];
  3079. int size, want, r;
  3080. dev_warn(DEV, "skipping unknown optional packet type %d, l: %d!\n",
  3081. cmd, data_size);
  3082. size = data_size;
  3083. while (size > 0) {
  3084. want = min_t(int, size, sizeof(sink));
  3085. r = drbd_recv(mdev, sink, want);
  3086. ERR_IF(r <= 0) break;
  3087. size -= r;
  3088. }
  3089. return size == 0;
  3090. }
  3091. static int receive_UnplugRemote(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  3092. {
  3093. /* Make sure we've acked all the TCP data associated
  3094. * with the data requests being unplugged */
  3095. drbd_tcp_quickack(mdev->data.socket);
  3096. return true;
  3097. }
  3098. static int receive_out_of_sync(struct drbd_conf *mdev, enum drbd_packets cmd, unsigned int data_size)
  3099. {
  3100. struct p_block_desc *p = &mdev->data.rbuf.block_desc;
  3101. switch (mdev->state.conn) {
  3102. case C_WF_SYNC_UUID:
  3103. case C_WF_BITMAP_T:
  3104. case C_BEHIND:
  3105. break;
  3106. default:
  3107. dev_err(DEV, "ASSERT FAILED cstate = %s, expected: WFSyncUUID|WFBitMapT|Behind\n",
  3108. drbd_conn_str(mdev->state.conn));
  3109. }
  3110. drbd_set_out_of_sync(mdev, be64_to_cpu(p->sector), be32_to_cpu(p->blksize));
  3111. return true;
  3112. }
  3113. typedef int (*drbd_cmd_handler_f)(struct drbd_conf *, enum drbd_packets cmd, unsigned int to_receive);
  3114. struct data_cmd {
  3115. int expect_payload;
  3116. size_t pkt_size;
  3117. drbd_cmd_handler_f function;
  3118. };
  3119. static struct data_cmd drbd_cmd_handler[] = {
  3120. [P_DATA] = { 1, sizeof(struct p_data), receive_Data },
  3121. [P_DATA_REPLY] = { 1, sizeof(struct p_data), receive_DataReply },
  3122. [P_RS_DATA_REPLY] = { 1, sizeof(struct p_data), receive_RSDataReply } ,
  3123. [P_BARRIER] = { 0, sizeof(struct p_barrier), receive_Barrier } ,
  3124. [P_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } ,
  3125. [P_COMPRESSED_BITMAP] = { 1, sizeof(struct p_header80), receive_bitmap } ,
  3126. [P_UNPLUG_REMOTE] = { 0, sizeof(struct p_header80), receive_UnplugRemote },
  3127. [P_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
  3128. [P_RS_DATA_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
  3129. [P_SYNC_PARAM] = { 1, sizeof(struct p_header80), receive_SyncParam },
  3130. [P_SYNC_PARAM89] = { 1, sizeof(struct p_header80), receive_SyncParam },
  3131. [P_PROTOCOL] = { 1, sizeof(struct p_protocol), receive_protocol },
  3132. [P_UUIDS] = { 0, sizeof(struct p_uuids), receive_uuids },
  3133. [P_SIZES] = { 0, sizeof(struct p_sizes), receive_sizes },
  3134. [P_STATE] = { 0, sizeof(struct p_state), receive_state },
  3135. [P_STATE_CHG_REQ] = { 0, sizeof(struct p_req_state), receive_req_state },
  3136. [P_SYNC_UUID] = { 0, sizeof(struct p_rs_uuid), receive_sync_uuid },
  3137. [P_OV_REQUEST] = { 0, sizeof(struct p_block_req), receive_DataRequest },
  3138. [P_OV_REPLY] = { 1, sizeof(struct p_block_req), receive_DataRequest },
  3139. [P_CSUM_RS_REQUEST] = { 1, sizeof(struct p_block_req), receive_DataRequest },
  3140. [P_DELAY_PROBE] = { 0, sizeof(struct p_delay_probe93), receive_skip },
  3141. [P_OUT_OF_SYNC] = { 0, sizeof(struct p_block_desc), receive_out_of_sync },
  3142. /* anything missing from this table is in
  3143. * the asender_tbl, see get_asender_cmd */
  3144. [P_MAX_CMD] = { 0, 0, NULL },
  3145. };
  3146. /* All handler functions that expect a sub-header get that sub-heder in
  3147. mdev->data.rbuf.header.head.payload.
  3148. Usually in mdev->data.rbuf.header.head the callback can find the usual
  3149. p_header, but they may not rely on that. Since there is also p_header95 !
  3150. */
  3151. static void drbdd(struct drbd_conf *mdev)
  3152. {
  3153. union p_header *header = &mdev->data.rbuf.header;
  3154. unsigned int packet_size;
  3155. enum drbd_packets cmd;
  3156. size_t shs; /* sub header size */
  3157. int rv;
  3158. while (get_t_state(&mdev->receiver) == Running) {
  3159. drbd_thread_current_set_cpu(mdev);
  3160. if (!drbd_recv_header(mdev, &cmd, &packet_size))
  3161. goto err_out;
  3162. if (unlikely(cmd >= P_MAX_CMD || !drbd_cmd_handler[cmd].function)) {
  3163. dev_err(DEV, "unknown packet type %d, l: %d!\n", cmd, packet_size);
  3164. goto err_out;
  3165. }
  3166. shs = drbd_cmd_handler[cmd].pkt_size - sizeof(union p_header);
  3167. if (packet_size - shs > 0 && !drbd_cmd_handler[cmd].expect_payload) {
  3168. dev_err(DEV, "No payload expected %s l:%d\n", cmdname(cmd), packet_size);
  3169. goto err_out;
  3170. }
  3171. if (shs) {
  3172. rv = drbd_recv(mdev, &header->h80.payload, shs);
  3173. if (unlikely(rv != shs)) {
  3174. if (!signal_pending(current))
  3175. dev_warn(DEV, "short read while reading sub header: rv=%d\n", rv);
  3176. goto err_out;
  3177. }
  3178. }
  3179. rv = drbd_cmd_handler[cmd].function(mdev, cmd, packet_size - shs);
  3180. if (unlikely(!rv)) {
  3181. dev_err(DEV, "error receiving %s, l: %d!\n",
  3182. cmdname(cmd), packet_size);
  3183. goto err_out;
  3184. }
  3185. }
  3186. if (0) {
  3187. err_out:
  3188. drbd_force_state(mdev, NS(conn, C_PROTOCOL_ERROR));
  3189. }
  3190. /* If we leave here, we probably want to update at least the
  3191. * "Connected" indicator on stable storage. Do so explicitly here. */
  3192. drbd_md_sync(mdev);
  3193. }
  3194. void drbd_flush_workqueue(struct drbd_conf *mdev)
  3195. {
  3196. struct drbd_wq_barrier barr;
  3197. barr.w.cb = w_prev_work_done;
  3198. init_completion(&barr.done);
  3199. drbd_queue_work(&mdev->data.work, &barr.w);
  3200. wait_for_completion(&barr.done);
  3201. }
  3202. void drbd_free_tl_hash(struct drbd_conf *mdev)
  3203. {
  3204. struct hlist_head *h;
  3205. spin_lock_irq(&mdev->req_lock);
  3206. if (!mdev->tl_hash || mdev->state.conn != C_STANDALONE) {
  3207. spin_unlock_irq(&mdev->req_lock);
  3208. return;
  3209. }
  3210. /* paranoia code */
  3211. for (h = mdev->ee_hash; h < mdev->ee_hash + mdev->ee_hash_s; h++)
  3212. if (h->first)
  3213. dev_err(DEV, "ASSERT FAILED ee_hash[%u].first == %p, expected NULL\n",
  3214. (int)(h - mdev->ee_hash), h->first);
  3215. kfree(mdev->ee_hash);
  3216. mdev->ee_hash = NULL;
  3217. mdev->ee_hash_s = 0;
  3218. /* paranoia code */
  3219. for (h = mdev->tl_hash; h < mdev->tl_hash + mdev->tl_hash_s; h++)
  3220. if (h->first)
  3221. dev_err(DEV, "ASSERT FAILED tl_hash[%u] == %p, expected NULL\n",
  3222. (int)(h - mdev->tl_hash), h->first);
  3223. kfree(mdev->tl_hash);
  3224. mdev->tl_hash = NULL;
  3225. mdev->tl_hash_s = 0;
  3226. spin_unlock_irq(&mdev->req_lock);
  3227. }
  3228. static void drbd_disconnect(struct drbd_conf *mdev)
  3229. {
  3230. enum drbd_fencing_p fp;
  3231. union drbd_state os, ns;
  3232. int rv = SS_UNKNOWN_ERROR;
  3233. unsigned int i;
  3234. if (mdev->state.conn == C_STANDALONE)
  3235. return;
  3236. /* asender does not clean up anything. it must not interfere, either */
  3237. drbd_thread_stop(&mdev->asender);
  3238. drbd_free_sock(mdev);
  3239. /* wait for current activity to cease. */
  3240. spin_lock_irq(&mdev->req_lock);
  3241. _drbd_wait_ee_list_empty(mdev, &mdev->active_ee);
  3242. _drbd_wait_ee_list_empty(mdev, &mdev->sync_ee);
  3243. _drbd_wait_ee_list_empty(mdev, &mdev->read_ee);
  3244. spin_unlock_irq(&mdev->req_lock);
  3245. /* We do not have data structures that would allow us to
  3246. * get the rs_pending_cnt down to 0 again.
  3247. * * On C_SYNC_TARGET we do not have any data structures describing
  3248. * the pending RSDataRequest's we have sent.
  3249. * * On C_SYNC_SOURCE there is no data structure that tracks
  3250. * the P_RS_DATA_REPLY blocks that we sent to the SyncTarget.
  3251. * And no, it is not the sum of the reference counts in the
  3252. * resync_LRU. The resync_LRU tracks the whole operation including
  3253. * the disk-IO, while the rs_pending_cnt only tracks the blocks
  3254. * on the fly. */
  3255. drbd_rs_cancel_all(mdev);
  3256. mdev->rs_total = 0;
  3257. mdev->rs_failed = 0;
  3258. atomic_set(&mdev->rs_pending_cnt, 0);
  3259. wake_up(&mdev->misc_wait);
  3260. del_timer(&mdev->request_timer);
  3261. /* make sure syncer is stopped and w_resume_next_sg queued */
  3262. del_timer_sync(&mdev->resync_timer);
  3263. resync_timer_fn((unsigned long)mdev);
  3264. /* wait for all w_e_end_data_req, w_e_end_rsdata_req, w_send_barrier,
  3265. * w_make_resync_request etc. which may still be on the worker queue
  3266. * to be "canceled" */
  3267. drbd_flush_workqueue(mdev);
  3268. /* This also does reclaim_net_ee(). If we do this too early, we might
  3269. * miss some resync ee and pages.*/
  3270. drbd_process_done_ee(mdev);
  3271. kfree(mdev->p_uuid);
  3272. mdev->p_uuid = NULL;
  3273. if (!is_susp(mdev->state))
  3274. tl_clear(mdev);
  3275. dev_info(DEV, "Connection closed\n");
  3276. drbd_md_sync(mdev);
  3277. fp = FP_DONT_CARE;
  3278. if (get_ldev(mdev)) {
  3279. fp = mdev->ldev->dc.fencing;
  3280. put_ldev(mdev);
  3281. }
  3282. if (mdev->state.role == R_PRIMARY && fp >= FP_RESOURCE && mdev->state.pdsk >= D_UNKNOWN)
  3283. drbd_try_outdate_peer_async(mdev);
  3284. spin_lock_irq(&mdev->req_lock);
  3285. os = mdev->state;
  3286. if (os.conn >= C_UNCONNECTED) {
  3287. /* Do not restart in case we are C_DISCONNECTING */
  3288. ns = os;
  3289. ns.conn = C_UNCONNECTED;
  3290. rv = _drbd_set_state(mdev, ns, CS_VERBOSE, NULL);
  3291. }
  3292. spin_unlock_irq(&mdev->req_lock);
  3293. if (os.conn == C_DISCONNECTING) {
  3294. wait_event(mdev->net_cnt_wait, atomic_read(&mdev->net_cnt) == 0);
  3295. crypto_free_hash(mdev->cram_hmac_tfm);
  3296. mdev->cram_hmac_tfm = NULL;
  3297. kfree(mdev->net_conf);
  3298. mdev->net_conf = NULL;
  3299. drbd_request_state(mdev, NS(conn, C_STANDALONE));
  3300. }
  3301. /* serialize with bitmap writeout triggered by the state change,
  3302. * if any. */
  3303. wait_event(mdev->misc_wait, !test_bit(BITMAP_IO, &mdev->flags));
  3304. /* tcp_close and release of sendpage pages can be deferred. I don't
  3305. * want to use SO_LINGER, because apparently it can be deferred for
  3306. * more than 20 seconds (longest time I checked).
  3307. *
  3308. * Actually we don't care for exactly when the network stack does its
  3309. * put_page(), but release our reference on these pages right here.
  3310. */
  3311. i = drbd_release_ee(mdev, &mdev->net_ee);
  3312. if (i)
  3313. dev_info(DEV, "net_ee not empty, killed %u entries\n", i);
  3314. i = atomic_read(&mdev->pp_in_use_by_net);
  3315. if (i)
  3316. dev_info(DEV, "pp_in_use_by_net = %d, expected 0\n", i);
  3317. i = atomic_read(&mdev->pp_in_use);
  3318. if (i)
  3319. dev_info(DEV, "pp_in_use = %d, expected 0\n", i);
  3320. D_ASSERT(list_empty(&mdev->read_ee));
  3321. D_ASSERT(list_empty(&mdev->active_ee));
  3322. D_ASSERT(list_empty(&mdev->sync_ee));
  3323. D_ASSERT(list_empty(&mdev->done_ee));
  3324. /* ok, no more ee's on the fly, it is safe to reset the epoch_size */
  3325. atomic_set(&mdev->current_epoch->epoch_size, 0);
  3326. D_ASSERT(list_empty(&mdev->current_epoch->list));
  3327. }
  3328. /*
  3329. * We support PRO_VERSION_MIN to PRO_VERSION_MAX. The protocol version
  3330. * we can agree on is stored in agreed_pro_version.
  3331. *
  3332. * feature flags and the reserved array should be enough room for future
  3333. * enhancements of the handshake protocol, and possible plugins...
  3334. *
  3335. * for now, they are expected to be zero, but ignored.
  3336. */
  3337. static int drbd_send_handshake(struct drbd_conf *mdev)
  3338. {
  3339. /* ASSERT current == mdev->receiver ... */
  3340. struct p_handshake *p = &mdev->data.sbuf.handshake;
  3341. int ok;
  3342. if (mutex_lock_interruptible(&mdev->data.mutex)) {
  3343. dev_err(DEV, "interrupted during initial handshake\n");
  3344. return 0; /* interrupted. not ok. */
  3345. }
  3346. if (mdev->data.socket == NULL) {
  3347. mutex_unlock(&mdev->data.mutex);
  3348. return 0;
  3349. }
  3350. memset(p, 0, sizeof(*p));
  3351. p->protocol_min = cpu_to_be32(PRO_VERSION_MIN);
  3352. p->protocol_max = cpu_to_be32(PRO_VERSION_MAX);
  3353. ok = _drbd_send_cmd( mdev, mdev->data.socket, P_HAND_SHAKE,
  3354. (struct p_header80 *)p, sizeof(*p), 0 );
  3355. mutex_unlock(&mdev->data.mutex);
  3356. return ok;
  3357. }
  3358. /*
  3359. * return values:
  3360. * 1 yes, we have a valid connection
  3361. * 0 oops, did not work out, please try again
  3362. * -1 peer talks different language,
  3363. * no point in trying again, please go standalone.
  3364. */
  3365. static int drbd_do_handshake(struct drbd_conf *mdev)
  3366. {
  3367. /* ASSERT current == mdev->receiver ... */
  3368. struct p_handshake *p = &mdev->data.rbuf.handshake;
  3369. const int expect = sizeof(struct p_handshake) - sizeof(struct p_header80);
  3370. unsigned int length;
  3371. enum drbd_packets cmd;
  3372. int rv;
  3373. rv = drbd_send_handshake(mdev);
  3374. if (!rv)
  3375. return 0;
  3376. rv = drbd_recv_header(mdev, &cmd, &length);
  3377. if (!rv)
  3378. return 0;
  3379. if (cmd != P_HAND_SHAKE) {
  3380. dev_err(DEV, "expected HandShake packet, received: %s (0x%04x)\n",
  3381. cmdname(cmd), cmd);
  3382. return -1;
  3383. }
  3384. if (length != expect) {
  3385. dev_err(DEV, "expected HandShake length: %u, received: %u\n",
  3386. expect, length);
  3387. return -1;
  3388. }
  3389. rv = drbd_recv(mdev, &p->head.payload, expect);
  3390. if (rv != expect) {
  3391. if (!signal_pending(current))
  3392. dev_warn(DEV, "short read receiving handshake packet: l=%u\n", rv);
  3393. return 0;
  3394. }
  3395. p->protocol_min = be32_to_cpu(p->protocol_min);
  3396. p->protocol_max = be32_to_cpu(p->protocol_max);
  3397. if (p->protocol_max == 0)
  3398. p->protocol_max = p->protocol_min;
  3399. if (PRO_VERSION_MAX < p->protocol_min ||
  3400. PRO_VERSION_MIN > p->protocol_max)
  3401. goto incompat;
  3402. mdev->agreed_pro_version = min_t(int, PRO_VERSION_MAX, p->protocol_max);
  3403. dev_info(DEV, "Handshake successful: "
  3404. "Agreed network protocol version %d\n", mdev->agreed_pro_version);
  3405. return 1;
  3406. incompat:
  3407. dev_err(DEV, "incompatible DRBD dialects: "
  3408. "I support %d-%d, peer supports %d-%d\n",
  3409. PRO_VERSION_MIN, PRO_VERSION_MAX,
  3410. p->protocol_min, p->protocol_max);
  3411. return -1;
  3412. }
  3413. #if !defined(CONFIG_CRYPTO_HMAC) && !defined(CONFIG_CRYPTO_HMAC_MODULE)
  3414. static int drbd_do_auth(struct drbd_conf *mdev)
  3415. {
  3416. dev_err(DEV, "This kernel was build without CONFIG_CRYPTO_HMAC.\n");
  3417. dev_err(DEV, "You need to disable 'cram-hmac-alg' in drbd.conf.\n");
  3418. return -1;
  3419. }
  3420. #else
  3421. #define CHALLENGE_LEN 64
  3422. /* Return value:
  3423. 1 - auth succeeded,
  3424. 0 - failed, try again (network error),
  3425. -1 - auth failed, don't try again.
  3426. */
  3427. static int drbd_do_auth(struct drbd_conf *mdev)
  3428. {
  3429. char my_challenge[CHALLENGE_LEN]; /* 64 Bytes... */
  3430. struct scatterlist sg;
  3431. char *response = NULL;
  3432. char *right_response = NULL;
  3433. char *peers_ch = NULL;
  3434. unsigned int key_len = strlen(mdev->net_conf->shared_secret);
  3435. unsigned int resp_size;
  3436. struct hash_desc desc;
  3437. enum drbd_packets cmd;
  3438. unsigned int length;
  3439. int rv;
  3440. desc.tfm = mdev->cram_hmac_tfm;
  3441. desc.flags = 0;
  3442. rv = crypto_hash_setkey(mdev->cram_hmac_tfm,
  3443. (u8 *)mdev->net_conf->shared_secret, key_len);
  3444. if (rv) {
  3445. dev_err(DEV, "crypto_hash_setkey() failed with %d\n", rv);
  3446. rv = -1;
  3447. goto fail;
  3448. }
  3449. get_random_bytes(my_challenge, CHALLENGE_LEN);
  3450. rv = drbd_send_cmd2(mdev, P_AUTH_CHALLENGE, my_challenge, CHALLENGE_LEN);
  3451. if (!rv)
  3452. goto fail;
  3453. rv = drbd_recv_header(mdev, &cmd, &length);
  3454. if (!rv)
  3455. goto fail;
  3456. if (cmd != P_AUTH_CHALLENGE) {
  3457. dev_err(DEV, "expected AuthChallenge packet, received: %s (0x%04x)\n",
  3458. cmdname(cmd), cmd);
  3459. rv = 0;
  3460. goto fail;
  3461. }
  3462. if (length > CHALLENGE_LEN * 2) {
  3463. dev_err(DEV, "expected AuthChallenge payload too big.\n");
  3464. rv = -1;
  3465. goto fail;
  3466. }
  3467. peers_ch = kmalloc(length, GFP_NOIO);
  3468. if (peers_ch == NULL) {
  3469. dev_err(DEV, "kmalloc of peers_ch failed\n");
  3470. rv = -1;
  3471. goto fail;
  3472. }
  3473. rv = drbd_recv(mdev, peers_ch, length);
  3474. if (rv != length) {
  3475. if (!signal_pending(current))
  3476. dev_warn(DEV, "short read AuthChallenge: l=%u\n", rv);
  3477. rv = 0;
  3478. goto fail;
  3479. }
  3480. resp_size = crypto_hash_digestsize(mdev->cram_hmac_tfm);
  3481. response = kmalloc(resp_size, GFP_NOIO);
  3482. if (response == NULL) {
  3483. dev_err(DEV, "kmalloc of response failed\n");
  3484. rv = -1;
  3485. goto fail;
  3486. }
  3487. sg_init_table(&sg, 1);
  3488. sg_set_buf(&sg, peers_ch, length);
  3489. rv = crypto_hash_digest(&desc, &sg, sg.length, response);
  3490. if (rv) {
  3491. dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
  3492. rv = -1;
  3493. goto fail;
  3494. }
  3495. rv = drbd_send_cmd2(mdev, P_AUTH_RESPONSE, response, resp_size);
  3496. if (!rv)
  3497. goto fail;
  3498. rv = drbd_recv_header(mdev, &cmd, &length);
  3499. if (!rv)
  3500. goto fail;
  3501. if (cmd != P_AUTH_RESPONSE) {
  3502. dev_err(DEV, "expected AuthResponse packet, received: %s (0x%04x)\n",
  3503. cmdname(cmd), cmd);
  3504. rv = 0;
  3505. goto fail;
  3506. }
  3507. if (length != resp_size) {
  3508. dev_err(DEV, "expected AuthResponse payload of wrong size\n");
  3509. rv = 0;
  3510. goto fail;
  3511. }
  3512. rv = drbd_recv(mdev, response , resp_size);
  3513. if (rv != resp_size) {
  3514. if (!signal_pending(current))
  3515. dev_warn(DEV, "short read receiving AuthResponse: l=%u\n", rv);
  3516. rv = 0;
  3517. goto fail;
  3518. }
  3519. right_response = kmalloc(resp_size, GFP_NOIO);
  3520. if (right_response == NULL) {
  3521. dev_err(DEV, "kmalloc of right_response failed\n");
  3522. rv = -1;
  3523. goto fail;
  3524. }
  3525. sg_set_buf(&sg, my_challenge, CHALLENGE_LEN);
  3526. rv = crypto_hash_digest(&desc, &sg, sg.length, right_response);
  3527. if (rv) {
  3528. dev_err(DEV, "crypto_hash_digest() failed with %d\n", rv);
  3529. rv = -1;
  3530. goto fail;
  3531. }
  3532. rv = !memcmp(response, right_response, resp_size);
  3533. if (rv)
  3534. dev_info(DEV, "Peer authenticated using %d bytes of '%s' HMAC\n",
  3535. resp_size, mdev->net_conf->cram_hmac_alg);
  3536. else
  3537. rv = -1;
  3538. fail:
  3539. kfree(peers_ch);
  3540. kfree(response);
  3541. kfree(right_response);
  3542. return rv;
  3543. }
  3544. #endif
  3545. int drbdd_init(struct drbd_thread *thi)
  3546. {
  3547. struct drbd_conf *mdev = thi->mdev;
  3548. unsigned int minor = mdev_to_minor(mdev);
  3549. int h;
  3550. sprintf(current->comm, "drbd%d_receiver", minor);
  3551. dev_info(DEV, "receiver (re)started\n");
  3552. do {
  3553. h = drbd_connect(mdev);
  3554. if (h == 0) {
  3555. drbd_disconnect(mdev);
  3556. schedule_timeout_interruptible(HZ);
  3557. }
  3558. if (h == -1) {
  3559. dev_warn(DEV, "Discarding network configuration.\n");
  3560. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  3561. }
  3562. } while (h == 0);
  3563. if (h > 0) {
  3564. if (get_net_conf(mdev)) {
  3565. drbdd(mdev);
  3566. put_net_conf(mdev);
  3567. }
  3568. }
  3569. drbd_disconnect(mdev);
  3570. dev_info(DEV, "receiver terminated\n");
  3571. return 0;
  3572. }
  3573. /* ********* acknowledge sender ******** */
  3574. static int got_RqSReply(struct drbd_conf *mdev, struct p_header80 *h)
  3575. {
  3576. struct p_req_state_reply *p = (struct p_req_state_reply *)h;
  3577. int retcode = be32_to_cpu(p->retcode);
  3578. if (retcode >= SS_SUCCESS) {
  3579. set_bit(CL_ST_CHG_SUCCESS, &mdev->flags);
  3580. } else {
  3581. set_bit(CL_ST_CHG_FAIL, &mdev->flags);
  3582. dev_err(DEV, "Requested state change failed by peer: %s (%d)\n",
  3583. drbd_set_st_err_str(retcode), retcode);
  3584. }
  3585. wake_up(&mdev->state_wait);
  3586. return true;
  3587. }
  3588. static int got_Ping(struct drbd_conf *mdev, struct p_header80 *h)
  3589. {
  3590. return drbd_send_ping_ack(mdev);
  3591. }
  3592. static int got_PingAck(struct drbd_conf *mdev, struct p_header80 *h)
  3593. {
  3594. /* restore idle timeout */
  3595. mdev->meta.socket->sk->sk_rcvtimeo = mdev->net_conf->ping_int*HZ;
  3596. if (!test_and_set_bit(GOT_PING_ACK, &mdev->flags))
  3597. wake_up(&mdev->misc_wait);
  3598. return true;
  3599. }
  3600. static int got_IsInSync(struct drbd_conf *mdev, struct p_header80 *h)
  3601. {
  3602. struct p_block_ack *p = (struct p_block_ack *)h;
  3603. sector_t sector = be64_to_cpu(p->sector);
  3604. int blksize = be32_to_cpu(p->blksize);
  3605. D_ASSERT(mdev->agreed_pro_version >= 89);
  3606. update_peer_seq(mdev, be32_to_cpu(p->seq_num));
  3607. if (get_ldev(mdev)) {
  3608. drbd_rs_complete_io(mdev, sector);
  3609. drbd_set_in_sync(mdev, sector, blksize);
  3610. /* rs_same_csums is supposed to count in units of BM_BLOCK_SIZE */
  3611. mdev->rs_same_csum += (blksize >> BM_BLOCK_SHIFT);
  3612. put_ldev(mdev);
  3613. }
  3614. dec_rs_pending(mdev);
  3615. atomic_add(blksize >> 9, &mdev->rs_sect_in);
  3616. return true;
  3617. }
  3618. /* when we receive the ACK for a write request,
  3619. * verify that we actually know about it */
  3620. static struct drbd_request *_ack_id_to_req(struct drbd_conf *mdev,
  3621. u64 id, sector_t sector)
  3622. {
  3623. struct hlist_head *slot = tl_hash_slot(mdev, sector);
  3624. struct hlist_node *n;
  3625. struct drbd_request *req;
  3626. hlist_for_each_entry(req, n, slot, collision) {
  3627. if ((unsigned long)req == (unsigned long)id) {
  3628. if (req->sector != sector) {
  3629. dev_err(DEV, "_ack_id_to_req: found req %p but it has "
  3630. "wrong sector (%llus versus %llus)\n", req,
  3631. (unsigned long long)req->sector,
  3632. (unsigned long long)sector);
  3633. break;
  3634. }
  3635. return req;
  3636. }
  3637. }
  3638. return NULL;
  3639. }
  3640. typedef struct drbd_request *(req_validator_fn)
  3641. (struct drbd_conf *mdev, u64 id, sector_t sector);
  3642. static int validate_req_change_req_state(struct drbd_conf *mdev,
  3643. u64 id, sector_t sector, req_validator_fn validator,
  3644. const char *func, enum drbd_req_event what)
  3645. {
  3646. struct drbd_request *req;
  3647. struct bio_and_error m;
  3648. spin_lock_irq(&mdev->req_lock);
  3649. req = validator(mdev, id, sector);
  3650. if (unlikely(!req)) {
  3651. spin_unlock_irq(&mdev->req_lock);
  3652. dev_err(DEV, "%s: failed to find req %p, sector %llus\n", func,
  3653. (void *)(unsigned long)id, (unsigned long long)sector);
  3654. return false;
  3655. }
  3656. __req_mod(req, what, &m);
  3657. spin_unlock_irq(&mdev->req_lock);
  3658. if (m.bio)
  3659. complete_master_bio(mdev, &m);
  3660. return true;
  3661. }
  3662. static int got_BlockAck(struct drbd_conf *mdev, struct p_header80 *h)
  3663. {
  3664. struct p_block_ack *p = (struct p_block_ack *)h;
  3665. sector_t sector = be64_to_cpu(p->sector);
  3666. int blksize = be32_to_cpu(p->blksize);
  3667. enum drbd_req_event what;
  3668. update_peer_seq(mdev, be32_to_cpu(p->seq_num));
  3669. if (is_syncer_block_id(p->block_id)) {
  3670. drbd_set_in_sync(mdev, sector, blksize);
  3671. dec_rs_pending(mdev);
  3672. return true;
  3673. }
  3674. switch (be16_to_cpu(h->command)) {
  3675. case P_RS_WRITE_ACK:
  3676. D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
  3677. what = write_acked_by_peer_and_sis;
  3678. break;
  3679. case P_WRITE_ACK:
  3680. D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
  3681. what = write_acked_by_peer;
  3682. break;
  3683. case P_RECV_ACK:
  3684. D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_B);
  3685. what = recv_acked_by_peer;
  3686. break;
  3687. case P_DISCARD_ACK:
  3688. D_ASSERT(mdev->net_conf->wire_protocol == DRBD_PROT_C);
  3689. what = conflict_discarded_by_peer;
  3690. break;
  3691. default:
  3692. D_ASSERT(0);
  3693. return false;
  3694. }
  3695. return validate_req_change_req_state(mdev, p->block_id, sector,
  3696. _ack_id_to_req, __func__ , what);
  3697. }
  3698. static int got_NegAck(struct drbd_conf *mdev, struct p_header80 *h)
  3699. {
  3700. struct p_block_ack *p = (struct p_block_ack *)h;
  3701. sector_t sector = be64_to_cpu(p->sector);
  3702. int size = be32_to_cpu(p->blksize);
  3703. struct drbd_request *req;
  3704. struct bio_and_error m;
  3705. update_peer_seq(mdev, be32_to_cpu(p->seq_num));
  3706. if (is_syncer_block_id(p->block_id)) {
  3707. dec_rs_pending(mdev);
  3708. drbd_rs_failed_io(mdev, sector, size);
  3709. return true;
  3710. }
  3711. spin_lock_irq(&mdev->req_lock);
  3712. req = _ack_id_to_req(mdev, p->block_id, sector);
  3713. if (!req) {
  3714. spin_unlock_irq(&mdev->req_lock);
  3715. if (mdev->net_conf->wire_protocol == DRBD_PROT_A ||
  3716. mdev->net_conf->wire_protocol == DRBD_PROT_B) {
  3717. /* Protocol A has no P_WRITE_ACKs, but has P_NEG_ACKs.
  3718. The master bio might already be completed, therefore the
  3719. request is no longer in the collision hash.
  3720. => Do not try to validate block_id as request. */
  3721. /* In Protocol B we might already have got a P_RECV_ACK
  3722. but then get a P_NEG_ACK after wards. */
  3723. drbd_set_out_of_sync(mdev, sector, size);
  3724. return true;
  3725. } else {
  3726. dev_err(DEV, "%s: failed to find req %p, sector %llus\n", __func__,
  3727. (void *)(unsigned long)p->block_id, (unsigned long long)sector);
  3728. return false;
  3729. }
  3730. }
  3731. __req_mod(req, neg_acked, &m);
  3732. spin_unlock_irq(&mdev->req_lock);
  3733. if (m.bio)
  3734. complete_master_bio(mdev, &m);
  3735. return true;
  3736. }
  3737. static int got_NegDReply(struct drbd_conf *mdev, struct p_header80 *h)
  3738. {
  3739. struct p_block_ack *p = (struct p_block_ack *)h;
  3740. sector_t sector = be64_to_cpu(p->sector);
  3741. update_peer_seq(mdev, be32_to_cpu(p->seq_num));
  3742. dev_err(DEV, "Got NegDReply; Sector %llus, len %u; Fail original request.\n",
  3743. (unsigned long long)sector, be32_to_cpu(p->blksize));
  3744. return validate_req_change_req_state(mdev, p->block_id, sector,
  3745. _ar_id_to_req, __func__ , neg_acked);
  3746. }
  3747. static int got_NegRSDReply(struct drbd_conf *mdev, struct p_header80 *h)
  3748. {
  3749. sector_t sector;
  3750. int size;
  3751. struct p_block_ack *p = (struct p_block_ack *)h;
  3752. sector = be64_to_cpu(p->sector);
  3753. size = be32_to_cpu(p->blksize);
  3754. update_peer_seq(mdev, be32_to_cpu(p->seq_num));
  3755. dec_rs_pending(mdev);
  3756. if (get_ldev_if_state(mdev, D_FAILED)) {
  3757. drbd_rs_complete_io(mdev, sector);
  3758. switch (be16_to_cpu(h->command)) {
  3759. case P_NEG_RS_DREPLY:
  3760. drbd_rs_failed_io(mdev, sector, size);
  3761. case P_RS_CANCEL:
  3762. break;
  3763. default:
  3764. D_ASSERT(0);
  3765. put_ldev(mdev);
  3766. return false;
  3767. }
  3768. put_ldev(mdev);
  3769. }
  3770. return true;
  3771. }
  3772. static int got_BarrierAck(struct drbd_conf *mdev, struct p_header80 *h)
  3773. {
  3774. struct p_barrier_ack *p = (struct p_barrier_ack *)h;
  3775. tl_release(mdev, p->barrier, be32_to_cpu(p->set_size));
  3776. if (mdev->state.conn == C_AHEAD &&
  3777. atomic_read(&mdev->ap_in_flight) == 0 &&
  3778. !test_and_set_bit(AHEAD_TO_SYNC_SOURCE, &mdev->current_epoch->flags)) {
  3779. mdev->start_resync_timer.expires = jiffies + HZ;
  3780. add_timer(&mdev->start_resync_timer);
  3781. }
  3782. return true;
  3783. }
  3784. static int got_OVResult(struct drbd_conf *mdev, struct p_header80 *h)
  3785. {
  3786. struct p_block_ack *p = (struct p_block_ack *)h;
  3787. struct drbd_work *w;
  3788. sector_t sector;
  3789. int size;
  3790. sector = be64_to_cpu(p->sector);
  3791. size = be32_to_cpu(p->blksize);
  3792. update_peer_seq(mdev, be32_to_cpu(p->seq_num));
  3793. if (be64_to_cpu(p->block_id) == ID_OUT_OF_SYNC)
  3794. drbd_ov_oos_found(mdev, sector, size);
  3795. else
  3796. ov_oos_print(mdev);
  3797. if (!get_ldev(mdev))
  3798. return true;
  3799. drbd_rs_complete_io(mdev, sector);
  3800. dec_rs_pending(mdev);
  3801. --mdev->ov_left;
  3802. /* let's advance progress step marks only for every other megabyte */
  3803. if ((mdev->ov_left & 0x200) == 0x200)
  3804. drbd_advance_rs_marks(mdev, mdev->ov_left);
  3805. if (mdev->ov_left == 0) {
  3806. w = kmalloc(sizeof(*w), GFP_NOIO);
  3807. if (w) {
  3808. w->cb = w_ov_finished;
  3809. drbd_queue_work_front(&mdev->data.work, w);
  3810. } else {
  3811. dev_err(DEV, "kmalloc(w) failed.");
  3812. ov_oos_print(mdev);
  3813. drbd_resync_finished(mdev);
  3814. }
  3815. }
  3816. put_ldev(mdev);
  3817. return true;
  3818. }
  3819. static int got_skip(struct drbd_conf *mdev, struct p_header80 *h)
  3820. {
  3821. return true;
  3822. }
  3823. struct asender_cmd {
  3824. size_t pkt_size;
  3825. int (*process)(struct drbd_conf *mdev, struct p_header80 *h);
  3826. };
  3827. static struct asender_cmd *get_asender_cmd(int cmd)
  3828. {
  3829. static struct asender_cmd asender_tbl[] = {
  3830. /* anything missing from this table is in
  3831. * the drbd_cmd_handler (drbd_default_handler) table,
  3832. * see the beginning of drbdd() */
  3833. [P_PING] = { sizeof(struct p_header80), got_Ping },
  3834. [P_PING_ACK] = { sizeof(struct p_header80), got_PingAck },
  3835. [P_RECV_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
  3836. [P_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
  3837. [P_RS_WRITE_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
  3838. [P_DISCARD_ACK] = { sizeof(struct p_block_ack), got_BlockAck },
  3839. [P_NEG_ACK] = { sizeof(struct p_block_ack), got_NegAck },
  3840. [P_NEG_DREPLY] = { sizeof(struct p_block_ack), got_NegDReply },
  3841. [P_NEG_RS_DREPLY] = { sizeof(struct p_block_ack), got_NegRSDReply},
  3842. [P_OV_RESULT] = { sizeof(struct p_block_ack), got_OVResult },
  3843. [P_BARRIER_ACK] = { sizeof(struct p_barrier_ack), got_BarrierAck },
  3844. [P_STATE_CHG_REPLY] = { sizeof(struct p_req_state_reply), got_RqSReply },
  3845. [P_RS_IS_IN_SYNC] = { sizeof(struct p_block_ack), got_IsInSync },
  3846. [P_DELAY_PROBE] = { sizeof(struct p_delay_probe93), got_skip },
  3847. [P_RS_CANCEL] = { sizeof(struct p_block_ack), got_NegRSDReply},
  3848. [P_MAX_CMD] = { 0, NULL },
  3849. };
  3850. if (cmd > P_MAX_CMD || asender_tbl[cmd].process == NULL)
  3851. return NULL;
  3852. return &asender_tbl[cmd];
  3853. }
  3854. int drbd_asender(struct drbd_thread *thi)
  3855. {
  3856. struct drbd_conf *mdev = thi->mdev;
  3857. struct p_header80 *h = &mdev->meta.rbuf.header.h80;
  3858. struct asender_cmd *cmd = NULL;
  3859. int rv, len;
  3860. void *buf = h;
  3861. int received = 0;
  3862. int expect = sizeof(struct p_header80);
  3863. int empty;
  3864. int ping_timeout_active = 0;
  3865. sprintf(current->comm, "drbd%d_asender", mdev_to_minor(mdev));
  3866. current->policy = SCHED_RR; /* Make this a realtime task! */
  3867. current->rt_priority = 2; /* more important than all other tasks */
  3868. while (get_t_state(thi) == Running) {
  3869. drbd_thread_current_set_cpu(mdev);
  3870. if (test_and_clear_bit(SEND_PING, &mdev->flags)) {
  3871. ERR_IF(!drbd_send_ping(mdev)) goto reconnect;
  3872. mdev->meta.socket->sk->sk_rcvtimeo =
  3873. mdev->net_conf->ping_timeo*HZ/10;
  3874. ping_timeout_active = 1;
  3875. }
  3876. /* conditionally cork;
  3877. * it may hurt latency if we cork without much to send */
  3878. if (!mdev->net_conf->no_cork &&
  3879. 3 < atomic_read(&mdev->unacked_cnt))
  3880. drbd_tcp_cork(mdev->meta.socket);
  3881. while (1) {
  3882. clear_bit(SIGNAL_ASENDER, &mdev->flags);
  3883. flush_signals(current);
  3884. if (!drbd_process_done_ee(mdev))
  3885. goto reconnect;
  3886. /* to avoid race with newly queued ACKs */
  3887. set_bit(SIGNAL_ASENDER, &mdev->flags);
  3888. spin_lock_irq(&mdev->req_lock);
  3889. empty = list_empty(&mdev->done_ee);
  3890. spin_unlock_irq(&mdev->req_lock);
  3891. /* new ack may have been queued right here,
  3892. * but then there is also a signal pending,
  3893. * and we start over... */
  3894. if (empty)
  3895. break;
  3896. }
  3897. /* but unconditionally uncork unless disabled */
  3898. if (!mdev->net_conf->no_cork)
  3899. drbd_tcp_uncork(mdev->meta.socket);
  3900. /* short circuit, recv_msg would return EINTR anyways. */
  3901. if (signal_pending(current))
  3902. continue;
  3903. rv = drbd_recv_short(mdev, mdev->meta.socket,
  3904. buf, expect-received, 0);
  3905. clear_bit(SIGNAL_ASENDER, &mdev->flags);
  3906. flush_signals(current);
  3907. /* Note:
  3908. * -EINTR (on meta) we got a signal
  3909. * -EAGAIN (on meta) rcvtimeo expired
  3910. * -ECONNRESET other side closed the connection
  3911. * -ERESTARTSYS (on data) we got a signal
  3912. * rv < 0 other than above: unexpected error!
  3913. * rv == expected: full header or command
  3914. * rv < expected: "woken" by signal during receive
  3915. * rv == 0 : "connection shut down by peer"
  3916. */
  3917. if (likely(rv > 0)) {
  3918. received += rv;
  3919. buf += rv;
  3920. } else if (rv == 0) {
  3921. dev_err(DEV, "meta connection shut down by peer.\n");
  3922. goto reconnect;
  3923. } else if (rv == -EAGAIN) {
  3924. /* If the data socket received something meanwhile,
  3925. * that is good enough: peer is still alive. */
  3926. if (time_after(mdev->last_received,
  3927. jiffies - mdev->meta.socket->sk->sk_rcvtimeo))
  3928. continue;
  3929. if (ping_timeout_active) {
  3930. dev_err(DEV, "PingAck did not arrive in time.\n");
  3931. goto reconnect;
  3932. }
  3933. set_bit(SEND_PING, &mdev->flags);
  3934. continue;
  3935. } else if (rv == -EINTR) {
  3936. continue;
  3937. } else {
  3938. dev_err(DEV, "sock_recvmsg returned %d\n", rv);
  3939. goto reconnect;
  3940. }
  3941. if (received == expect && cmd == NULL) {
  3942. if (unlikely(h->magic != BE_DRBD_MAGIC)) {
  3943. dev_err(DEV, "magic?? on meta m: 0x%08x c: %d l: %d\n",
  3944. be32_to_cpu(h->magic),
  3945. be16_to_cpu(h->command),
  3946. be16_to_cpu(h->length));
  3947. goto reconnect;
  3948. }
  3949. cmd = get_asender_cmd(be16_to_cpu(h->command));
  3950. len = be16_to_cpu(h->length);
  3951. if (unlikely(cmd == NULL)) {
  3952. dev_err(DEV, "unknown command?? on meta m: 0x%08x c: %d l: %d\n",
  3953. be32_to_cpu(h->magic),
  3954. be16_to_cpu(h->command),
  3955. be16_to_cpu(h->length));
  3956. goto disconnect;
  3957. }
  3958. expect = cmd->pkt_size;
  3959. ERR_IF(len != expect-sizeof(struct p_header80))
  3960. goto reconnect;
  3961. }
  3962. if (received == expect) {
  3963. mdev->last_received = jiffies;
  3964. D_ASSERT(cmd != NULL);
  3965. if (!cmd->process(mdev, h))
  3966. goto reconnect;
  3967. /* the idle_timeout (ping-int)
  3968. * has been restored in got_PingAck() */
  3969. if (cmd == get_asender_cmd(P_PING_ACK))
  3970. ping_timeout_active = 0;
  3971. buf = h;
  3972. received = 0;
  3973. expect = sizeof(struct p_header80);
  3974. cmd = NULL;
  3975. }
  3976. }
  3977. if (0) {
  3978. reconnect:
  3979. drbd_force_state(mdev, NS(conn, C_NETWORK_FAILURE));
  3980. drbd_md_sync(mdev);
  3981. }
  3982. if (0) {
  3983. disconnect:
  3984. drbd_force_state(mdev, NS(conn, C_DISCONNECTING));
  3985. drbd_md_sync(mdev);
  3986. }
  3987. clear_bit(SIGNAL_ASENDER, &mdev->flags);
  3988. D_ASSERT(mdev->state.conn < C_CONNECTED);
  3989. dev_info(DEV, "asender terminated\n");
  3990. return 0;
  3991. }