lock.c 123 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132
  1. /******************************************************************************
  2. *******************************************************************************
  3. **
  4. ** Copyright (C) 2005-2010 Red Hat, Inc. All rights reserved.
  5. **
  6. ** This copyrighted material is made available to anyone wishing to use,
  7. ** modify, copy, or redistribute it subject to the terms and conditions
  8. ** of the GNU General Public License v.2.
  9. **
  10. *******************************************************************************
  11. ******************************************************************************/
  12. /* Central locking logic has four stages:
  13. dlm_lock()
  14. dlm_unlock()
  15. request_lock(ls, lkb)
  16. convert_lock(ls, lkb)
  17. unlock_lock(ls, lkb)
  18. cancel_lock(ls, lkb)
  19. _request_lock(r, lkb)
  20. _convert_lock(r, lkb)
  21. _unlock_lock(r, lkb)
  22. _cancel_lock(r, lkb)
  23. do_request(r, lkb)
  24. do_convert(r, lkb)
  25. do_unlock(r, lkb)
  26. do_cancel(r, lkb)
  27. Stage 1 (lock, unlock) is mainly about checking input args and
  28. splitting into one of the four main operations:
  29. dlm_lock = request_lock
  30. dlm_lock+CONVERT = convert_lock
  31. dlm_unlock = unlock_lock
  32. dlm_unlock+CANCEL = cancel_lock
  33. Stage 2, xxxx_lock(), just finds and locks the relevant rsb which is
  34. provided to the next stage.
  35. Stage 3, _xxxx_lock(), determines if the operation is local or remote.
  36. When remote, it calls send_xxxx(), when local it calls do_xxxx().
  37. Stage 4, do_xxxx(), is the guts of the operation. It manipulates the
  38. given rsb and lkb and queues callbacks.
  39. For remote operations, send_xxxx() results in the corresponding do_xxxx()
  40. function being executed on the remote node. The connecting send/receive
  41. calls on local (L) and remote (R) nodes:
  42. L: send_xxxx() -> R: receive_xxxx()
  43. R: do_xxxx()
  44. L: receive_xxxx_reply() <- R: send_xxxx_reply()
  45. */
  46. #include <linux/types.h>
  47. #include <linux/slab.h>
  48. #include "dlm_internal.h"
  49. #include <linux/dlm_device.h>
  50. #include "memory.h"
  51. #include "lowcomms.h"
  52. #include "requestqueue.h"
  53. #include "util.h"
  54. #include "dir.h"
  55. #include "member.h"
  56. #include "lockspace.h"
  57. #include "ast.h"
  58. #include "lock.h"
  59. #include "rcom.h"
  60. #include "recover.h"
  61. #include "lvb_table.h"
  62. #include "user.h"
  63. #include "config.h"
  64. static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb);
  65. static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb);
  66. static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb);
  67. static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb);
  68. static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb);
  69. static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode);
  70. static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb);
  71. static int send_remove(struct dlm_rsb *r);
  72. static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
  73. static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb);
  74. static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
  75. struct dlm_message *ms);
  76. static int receive_extralen(struct dlm_message *ms);
  77. static void do_purge(struct dlm_ls *ls, int nodeid, int pid);
  78. static void del_timeout(struct dlm_lkb *lkb);
  79. /*
  80. * Lock compatibilty matrix - thanks Steve
  81. * UN = Unlocked state. Not really a state, used as a flag
  82. * PD = Padding. Used to make the matrix a nice power of two in size
  83. * Other states are the same as the VMS DLM.
  84. * Usage: matrix[grmode+1][rqmode+1] (although m[rq+1][gr+1] is the same)
  85. */
  86. static const int __dlm_compat_matrix[8][8] = {
  87. /* UN NL CR CW PR PW EX PD */
  88. {1, 1, 1, 1, 1, 1, 1, 0}, /* UN */
  89. {1, 1, 1, 1, 1, 1, 1, 0}, /* NL */
  90. {1, 1, 1, 1, 1, 1, 0, 0}, /* CR */
  91. {1, 1, 1, 1, 0, 0, 0, 0}, /* CW */
  92. {1, 1, 1, 0, 1, 0, 0, 0}, /* PR */
  93. {1, 1, 1, 0, 0, 0, 0, 0}, /* PW */
  94. {1, 1, 0, 0, 0, 0, 0, 0}, /* EX */
  95. {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
  96. };
  97. /*
  98. * This defines the direction of transfer of LVB data.
  99. * Granted mode is the row; requested mode is the column.
  100. * Usage: matrix[grmode+1][rqmode+1]
  101. * 1 = LVB is returned to the caller
  102. * 0 = LVB is written to the resource
  103. * -1 = nothing happens to the LVB
  104. */
  105. const int dlm_lvb_operations[8][8] = {
  106. /* UN NL CR CW PR PW EX PD*/
  107. { -1, 1, 1, 1, 1, 1, 1, -1 }, /* UN */
  108. { -1, 1, 1, 1, 1, 1, 1, 0 }, /* NL */
  109. { -1, -1, 1, 1, 1, 1, 1, 0 }, /* CR */
  110. { -1, -1, -1, 1, 1, 1, 1, 0 }, /* CW */
  111. { -1, -1, -1, -1, 1, 1, 1, 0 }, /* PR */
  112. { -1, 0, 0, 0, 0, 0, 1, 0 }, /* PW */
  113. { -1, 0, 0, 0, 0, 0, 0, 0 }, /* EX */
  114. { -1, 0, 0, 0, 0, 0, 0, 0 } /* PD */
  115. };
  116. #define modes_compat(gr, rq) \
  117. __dlm_compat_matrix[(gr)->lkb_grmode + 1][(rq)->lkb_rqmode + 1]
  118. int dlm_modes_compat(int mode1, int mode2)
  119. {
  120. return __dlm_compat_matrix[mode1 + 1][mode2 + 1];
  121. }
  122. /*
  123. * Compatibility matrix for conversions with QUECVT set.
  124. * Granted mode is the row; requested mode is the column.
  125. * Usage: matrix[grmode+1][rqmode+1]
  126. */
  127. static const int __quecvt_compat_matrix[8][8] = {
  128. /* UN NL CR CW PR PW EX PD */
  129. {0, 0, 0, 0, 0, 0, 0, 0}, /* UN */
  130. {0, 0, 1, 1, 1, 1, 1, 0}, /* NL */
  131. {0, 0, 0, 1, 1, 1, 1, 0}, /* CR */
  132. {0, 0, 0, 0, 1, 1, 1, 0}, /* CW */
  133. {0, 0, 0, 1, 0, 1, 1, 0}, /* PR */
  134. {0, 0, 0, 0, 0, 0, 1, 0}, /* PW */
  135. {0, 0, 0, 0, 0, 0, 0, 0}, /* EX */
  136. {0, 0, 0, 0, 0, 0, 0, 0} /* PD */
  137. };
  138. void dlm_print_lkb(struct dlm_lkb *lkb)
  139. {
  140. printk(KERN_ERR "lkb: nodeid %d id %x remid %x exflags %x flags %x\n"
  141. " status %d rqmode %d grmode %d wait_type %d\n",
  142. lkb->lkb_nodeid, lkb->lkb_id, lkb->lkb_remid, lkb->lkb_exflags,
  143. lkb->lkb_flags, lkb->lkb_status, lkb->lkb_rqmode,
  144. lkb->lkb_grmode, lkb->lkb_wait_type);
  145. }
  146. static void dlm_print_rsb(struct dlm_rsb *r)
  147. {
  148. printk(KERN_ERR "rsb: nodeid %d flags %lx first %x rlc %d name %s\n",
  149. r->res_nodeid, r->res_flags, r->res_first_lkid,
  150. r->res_recover_locks_count, r->res_name);
  151. }
  152. void dlm_dump_rsb(struct dlm_rsb *r)
  153. {
  154. struct dlm_lkb *lkb;
  155. dlm_print_rsb(r);
  156. printk(KERN_ERR "rsb: root_list empty %d recover_list empty %d\n",
  157. list_empty(&r->res_root_list), list_empty(&r->res_recover_list));
  158. printk(KERN_ERR "rsb lookup list\n");
  159. list_for_each_entry(lkb, &r->res_lookup, lkb_rsb_lookup)
  160. dlm_print_lkb(lkb);
  161. printk(KERN_ERR "rsb grant queue:\n");
  162. list_for_each_entry(lkb, &r->res_grantqueue, lkb_statequeue)
  163. dlm_print_lkb(lkb);
  164. printk(KERN_ERR "rsb convert queue:\n");
  165. list_for_each_entry(lkb, &r->res_convertqueue, lkb_statequeue)
  166. dlm_print_lkb(lkb);
  167. printk(KERN_ERR "rsb wait queue:\n");
  168. list_for_each_entry(lkb, &r->res_waitqueue, lkb_statequeue)
  169. dlm_print_lkb(lkb);
  170. }
  171. /* Threads cannot use the lockspace while it's being recovered */
  172. static inline void dlm_lock_recovery(struct dlm_ls *ls)
  173. {
  174. down_read(&ls->ls_in_recovery);
  175. }
  176. void dlm_unlock_recovery(struct dlm_ls *ls)
  177. {
  178. up_read(&ls->ls_in_recovery);
  179. }
  180. int dlm_lock_recovery_try(struct dlm_ls *ls)
  181. {
  182. return down_read_trylock(&ls->ls_in_recovery);
  183. }
  184. static inline int can_be_queued(struct dlm_lkb *lkb)
  185. {
  186. return !(lkb->lkb_exflags & DLM_LKF_NOQUEUE);
  187. }
  188. static inline int force_blocking_asts(struct dlm_lkb *lkb)
  189. {
  190. return (lkb->lkb_exflags & DLM_LKF_NOQUEUEBAST);
  191. }
  192. static inline int is_demoted(struct dlm_lkb *lkb)
  193. {
  194. return (lkb->lkb_sbflags & DLM_SBF_DEMOTED);
  195. }
  196. static inline int is_altmode(struct dlm_lkb *lkb)
  197. {
  198. return (lkb->lkb_sbflags & DLM_SBF_ALTMODE);
  199. }
  200. static inline int is_granted(struct dlm_lkb *lkb)
  201. {
  202. return (lkb->lkb_status == DLM_LKSTS_GRANTED);
  203. }
  204. static inline int is_remote(struct dlm_rsb *r)
  205. {
  206. DLM_ASSERT(r->res_nodeid >= 0, dlm_print_rsb(r););
  207. return !!r->res_nodeid;
  208. }
  209. static inline int is_process_copy(struct dlm_lkb *lkb)
  210. {
  211. return (lkb->lkb_nodeid && !(lkb->lkb_flags & DLM_IFL_MSTCPY));
  212. }
  213. static inline int is_master_copy(struct dlm_lkb *lkb)
  214. {
  215. if (lkb->lkb_flags & DLM_IFL_MSTCPY)
  216. DLM_ASSERT(lkb->lkb_nodeid, dlm_print_lkb(lkb););
  217. return (lkb->lkb_flags & DLM_IFL_MSTCPY) ? 1 : 0;
  218. }
  219. static inline int middle_conversion(struct dlm_lkb *lkb)
  220. {
  221. if ((lkb->lkb_grmode==DLM_LOCK_PR && lkb->lkb_rqmode==DLM_LOCK_CW) ||
  222. (lkb->lkb_rqmode==DLM_LOCK_PR && lkb->lkb_grmode==DLM_LOCK_CW))
  223. return 1;
  224. return 0;
  225. }
  226. static inline int down_conversion(struct dlm_lkb *lkb)
  227. {
  228. return (!middle_conversion(lkb) && lkb->lkb_rqmode < lkb->lkb_grmode);
  229. }
  230. static inline int is_overlap_unlock(struct dlm_lkb *lkb)
  231. {
  232. return lkb->lkb_flags & DLM_IFL_OVERLAP_UNLOCK;
  233. }
  234. static inline int is_overlap_cancel(struct dlm_lkb *lkb)
  235. {
  236. return lkb->lkb_flags & DLM_IFL_OVERLAP_CANCEL;
  237. }
  238. static inline int is_overlap(struct dlm_lkb *lkb)
  239. {
  240. return (lkb->lkb_flags & (DLM_IFL_OVERLAP_UNLOCK |
  241. DLM_IFL_OVERLAP_CANCEL));
  242. }
  243. static void queue_cast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
  244. {
  245. if (is_master_copy(lkb))
  246. return;
  247. del_timeout(lkb);
  248. DLM_ASSERT(lkb->lkb_lksb, dlm_print_lkb(lkb););
  249. /* if the operation was a cancel, then return -DLM_ECANCEL, if a
  250. timeout caused the cancel then return -ETIMEDOUT */
  251. if (rv == -DLM_ECANCEL && (lkb->lkb_flags & DLM_IFL_TIMEOUT_CANCEL)) {
  252. lkb->lkb_flags &= ~DLM_IFL_TIMEOUT_CANCEL;
  253. rv = -ETIMEDOUT;
  254. }
  255. if (rv == -DLM_ECANCEL && (lkb->lkb_flags & DLM_IFL_DEADLOCK_CANCEL)) {
  256. lkb->lkb_flags &= ~DLM_IFL_DEADLOCK_CANCEL;
  257. rv = -EDEADLK;
  258. }
  259. dlm_add_cb(lkb, DLM_CB_CAST, lkb->lkb_grmode, rv, lkb->lkb_sbflags);
  260. }
  261. static inline void queue_cast_overlap(struct dlm_rsb *r, struct dlm_lkb *lkb)
  262. {
  263. queue_cast(r, lkb,
  264. is_overlap_unlock(lkb) ? -DLM_EUNLOCK : -DLM_ECANCEL);
  265. }
  266. static void queue_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int rqmode)
  267. {
  268. if (is_master_copy(lkb)) {
  269. send_bast(r, lkb, rqmode);
  270. } else {
  271. dlm_add_cb(lkb, DLM_CB_BAST, rqmode, 0, 0);
  272. }
  273. }
  274. /*
  275. * Basic operations on rsb's and lkb's
  276. */
  277. static int pre_rsb_struct(struct dlm_ls *ls)
  278. {
  279. struct dlm_rsb *r1, *r2;
  280. int count = 0;
  281. spin_lock(&ls->ls_new_rsb_spin);
  282. if (ls->ls_new_rsb_count > dlm_config.ci_new_rsb_count / 2) {
  283. spin_unlock(&ls->ls_new_rsb_spin);
  284. return 0;
  285. }
  286. spin_unlock(&ls->ls_new_rsb_spin);
  287. r1 = dlm_allocate_rsb(ls);
  288. r2 = dlm_allocate_rsb(ls);
  289. spin_lock(&ls->ls_new_rsb_spin);
  290. if (r1) {
  291. list_add(&r1->res_hashchain, &ls->ls_new_rsb);
  292. ls->ls_new_rsb_count++;
  293. }
  294. if (r2) {
  295. list_add(&r2->res_hashchain, &ls->ls_new_rsb);
  296. ls->ls_new_rsb_count++;
  297. }
  298. count = ls->ls_new_rsb_count;
  299. spin_unlock(&ls->ls_new_rsb_spin);
  300. if (!count)
  301. return -ENOMEM;
  302. return 0;
  303. }
  304. /* If ls->ls_new_rsb is empty, return -EAGAIN, so the caller can
  305. unlock any spinlocks, go back and call pre_rsb_struct again.
  306. Otherwise, take an rsb off the list and return it. */
  307. static int get_rsb_struct(struct dlm_ls *ls, char *name, int len,
  308. struct dlm_rsb **r_ret)
  309. {
  310. struct dlm_rsb *r;
  311. int count;
  312. spin_lock(&ls->ls_new_rsb_spin);
  313. if (list_empty(&ls->ls_new_rsb)) {
  314. count = ls->ls_new_rsb_count;
  315. spin_unlock(&ls->ls_new_rsb_spin);
  316. log_debug(ls, "find_rsb retry %d %d %s",
  317. count, dlm_config.ci_new_rsb_count, name);
  318. return -EAGAIN;
  319. }
  320. r = list_first_entry(&ls->ls_new_rsb, struct dlm_rsb, res_hashchain);
  321. list_del(&r->res_hashchain);
  322. ls->ls_new_rsb_count--;
  323. spin_unlock(&ls->ls_new_rsb_spin);
  324. r->res_ls = ls;
  325. r->res_length = len;
  326. memcpy(r->res_name, name, len);
  327. mutex_init(&r->res_mutex);
  328. INIT_LIST_HEAD(&r->res_hashchain);
  329. INIT_LIST_HEAD(&r->res_lookup);
  330. INIT_LIST_HEAD(&r->res_grantqueue);
  331. INIT_LIST_HEAD(&r->res_convertqueue);
  332. INIT_LIST_HEAD(&r->res_waitqueue);
  333. INIT_LIST_HEAD(&r->res_root_list);
  334. INIT_LIST_HEAD(&r->res_recover_list);
  335. *r_ret = r;
  336. return 0;
  337. }
  338. static int search_rsb_list(struct list_head *head, char *name, int len,
  339. unsigned int flags, struct dlm_rsb **r_ret)
  340. {
  341. struct dlm_rsb *r;
  342. int error = 0;
  343. list_for_each_entry(r, head, res_hashchain) {
  344. if (len == r->res_length && !memcmp(name, r->res_name, len))
  345. goto found;
  346. }
  347. *r_ret = NULL;
  348. return -EBADR;
  349. found:
  350. if (r->res_nodeid && (flags & R_MASTER))
  351. error = -ENOTBLK;
  352. *r_ret = r;
  353. return error;
  354. }
  355. static int _search_rsb(struct dlm_ls *ls, char *name, int len, int b,
  356. unsigned int flags, struct dlm_rsb **r_ret)
  357. {
  358. struct dlm_rsb *r;
  359. int error;
  360. error = search_rsb_list(&ls->ls_rsbtbl[b].list, name, len, flags, &r);
  361. if (!error) {
  362. kref_get(&r->res_ref);
  363. goto out;
  364. }
  365. error = search_rsb_list(&ls->ls_rsbtbl[b].toss, name, len, flags, &r);
  366. if (error)
  367. goto out;
  368. list_move(&r->res_hashchain, &ls->ls_rsbtbl[b].list);
  369. if (dlm_no_directory(ls))
  370. goto out;
  371. if (r->res_nodeid == -1) {
  372. rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
  373. r->res_first_lkid = 0;
  374. } else if (r->res_nodeid > 0) {
  375. rsb_set_flag(r, RSB_MASTER_UNCERTAIN);
  376. r->res_first_lkid = 0;
  377. } else {
  378. DLM_ASSERT(r->res_nodeid == 0, dlm_print_rsb(r););
  379. DLM_ASSERT(!rsb_flag(r, RSB_MASTER_UNCERTAIN),);
  380. }
  381. out:
  382. *r_ret = r;
  383. return error;
  384. }
  385. /*
  386. * Find rsb in rsbtbl and potentially create/add one
  387. *
  388. * Delaying the release of rsb's has a similar benefit to applications keeping
  389. * NL locks on an rsb, but without the guarantee that the cached master value
  390. * will still be valid when the rsb is reused. Apps aren't always smart enough
  391. * to keep NL locks on an rsb that they may lock again shortly; this can lead
  392. * to excessive master lookups and removals if we don't delay the release.
  393. *
  394. * Searching for an rsb means looking through both the normal list and toss
  395. * list. When found on the toss list the rsb is moved to the normal list with
  396. * ref count of 1; when found on normal list the ref count is incremented.
  397. */
  398. static int find_rsb(struct dlm_ls *ls, char *name, int namelen,
  399. unsigned int flags, struct dlm_rsb **r_ret)
  400. {
  401. struct dlm_rsb *r = NULL;
  402. uint32_t hash, bucket;
  403. int error;
  404. if (namelen > DLM_RESNAME_MAXLEN) {
  405. error = -EINVAL;
  406. goto out;
  407. }
  408. if (dlm_no_directory(ls))
  409. flags |= R_CREATE;
  410. hash = jhash(name, namelen, 0);
  411. bucket = hash & (ls->ls_rsbtbl_size - 1);
  412. retry:
  413. if (flags & R_CREATE) {
  414. error = pre_rsb_struct(ls);
  415. if (error < 0)
  416. goto out;
  417. }
  418. spin_lock(&ls->ls_rsbtbl[bucket].lock);
  419. error = _search_rsb(ls, name, namelen, bucket, flags, &r);
  420. if (!error)
  421. goto out_unlock;
  422. if (error == -EBADR && !(flags & R_CREATE))
  423. goto out_unlock;
  424. /* the rsb was found but wasn't a master copy */
  425. if (error == -ENOTBLK)
  426. goto out_unlock;
  427. error = get_rsb_struct(ls, name, namelen, &r);
  428. if (error == -EAGAIN) {
  429. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  430. goto retry;
  431. }
  432. if (error)
  433. goto out_unlock;
  434. r->res_hash = hash;
  435. r->res_bucket = bucket;
  436. r->res_nodeid = -1;
  437. kref_init(&r->res_ref);
  438. /* With no directory, the master can be set immediately */
  439. if (dlm_no_directory(ls)) {
  440. int nodeid = dlm_dir_nodeid(r);
  441. if (nodeid == dlm_our_nodeid())
  442. nodeid = 0;
  443. r->res_nodeid = nodeid;
  444. }
  445. list_add(&r->res_hashchain, &ls->ls_rsbtbl[bucket].list);
  446. error = 0;
  447. out_unlock:
  448. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  449. out:
  450. *r_ret = r;
  451. return error;
  452. }
  453. /* This is only called to add a reference when the code already holds
  454. a valid reference to the rsb, so there's no need for locking. */
  455. static inline void hold_rsb(struct dlm_rsb *r)
  456. {
  457. kref_get(&r->res_ref);
  458. }
  459. void dlm_hold_rsb(struct dlm_rsb *r)
  460. {
  461. hold_rsb(r);
  462. }
  463. static void toss_rsb(struct kref *kref)
  464. {
  465. struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
  466. struct dlm_ls *ls = r->res_ls;
  467. DLM_ASSERT(list_empty(&r->res_root_list), dlm_print_rsb(r););
  468. kref_init(&r->res_ref);
  469. list_move(&r->res_hashchain, &ls->ls_rsbtbl[r->res_bucket].toss);
  470. r->res_toss_time = jiffies;
  471. if (r->res_lvbptr) {
  472. dlm_free_lvb(r->res_lvbptr);
  473. r->res_lvbptr = NULL;
  474. }
  475. }
  476. /* When all references to the rsb are gone it's transferred to
  477. the tossed list for later disposal. */
  478. static void put_rsb(struct dlm_rsb *r)
  479. {
  480. struct dlm_ls *ls = r->res_ls;
  481. uint32_t bucket = r->res_bucket;
  482. spin_lock(&ls->ls_rsbtbl[bucket].lock);
  483. kref_put(&r->res_ref, toss_rsb);
  484. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  485. }
  486. void dlm_put_rsb(struct dlm_rsb *r)
  487. {
  488. put_rsb(r);
  489. }
  490. /* See comment for unhold_lkb */
  491. static void unhold_rsb(struct dlm_rsb *r)
  492. {
  493. int rv;
  494. rv = kref_put(&r->res_ref, toss_rsb);
  495. DLM_ASSERT(!rv, dlm_dump_rsb(r););
  496. }
  497. static void kill_rsb(struct kref *kref)
  498. {
  499. struct dlm_rsb *r = container_of(kref, struct dlm_rsb, res_ref);
  500. /* All work is done after the return from kref_put() so we
  501. can release the write_lock before the remove and free. */
  502. DLM_ASSERT(list_empty(&r->res_lookup), dlm_dump_rsb(r););
  503. DLM_ASSERT(list_empty(&r->res_grantqueue), dlm_dump_rsb(r););
  504. DLM_ASSERT(list_empty(&r->res_convertqueue), dlm_dump_rsb(r););
  505. DLM_ASSERT(list_empty(&r->res_waitqueue), dlm_dump_rsb(r););
  506. DLM_ASSERT(list_empty(&r->res_root_list), dlm_dump_rsb(r););
  507. DLM_ASSERT(list_empty(&r->res_recover_list), dlm_dump_rsb(r););
  508. }
  509. /* Attaching/detaching lkb's from rsb's is for rsb reference counting.
  510. The rsb must exist as long as any lkb's for it do. */
  511. static void attach_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
  512. {
  513. hold_rsb(r);
  514. lkb->lkb_resource = r;
  515. }
  516. static void detach_lkb(struct dlm_lkb *lkb)
  517. {
  518. if (lkb->lkb_resource) {
  519. put_rsb(lkb->lkb_resource);
  520. lkb->lkb_resource = NULL;
  521. }
  522. }
  523. static int create_lkb(struct dlm_ls *ls, struct dlm_lkb **lkb_ret)
  524. {
  525. struct dlm_lkb *lkb;
  526. int rv, id;
  527. lkb = dlm_allocate_lkb(ls);
  528. if (!lkb)
  529. return -ENOMEM;
  530. lkb->lkb_nodeid = -1;
  531. lkb->lkb_grmode = DLM_LOCK_IV;
  532. kref_init(&lkb->lkb_ref);
  533. INIT_LIST_HEAD(&lkb->lkb_ownqueue);
  534. INIT_LIST_HEAD(&lkb->lkb_rsb_lookup);
  535. INIT_LIST_HEAD(&lkb->lkb_time_list);
  536. INIT_LIST_HEAD(&lkb->lkb_cb_list);
  537. mutex_init(&lkb->lkb_cb_mutex);
  538. INIT_WORK(&lkb->lkb_cb_work, dlm_callback_work);
  539. retry:
  540. rv = idr_pre_get(&ls->ls_lkbidr, GFP_NOFS);
  541. if (!rv)
  542. return -ENOMEM;
  543. spin_lock(&ls->ls_lkbidr_spin);
  544. rv = idr_get_new_above(&ls->ls_lkbidr, lkb, 1, &id);
  545. if (!rv)
  546. lkb->lkb_id = id;
  547. spin_unlock(&ls->ls_lkbidr_spin);
  548. if (rv == -EAGAIN)
  549. goto retry;
  550. if (rv < 0) {
  551. log_error(ls, "create_lkb idr error %d", rv);
  552. return rv;
  553. }
  554. *lkb_ret = lkb;
  555. return 0;
  556. }
  557. static int find_lkb(struct dlm_ls *ls, uint32_t lkid, struct dlm_lkb **lkb_ret)
  558. {
  559. struct dlm_lkb *lkb;
  560. spin_lock(&ls->ls_lkbidr_spin);
  561. lkb = idr_find(&ls->ls_lkbidr, lkid);
  562. if (lkb)
  563. kref_get(&lkb->lkb_ref);
  564. spin_unlock(&ls->ls_lkbidr_spin);
  565. *lkb_ret = lkb;
  566. return lkb ? 0 : -ENOENT;
  567. }
  568. static void kill_lkb(struct kref *kref)
  569. {
  570. struct dlm_lkb *lkb = container_of(kref, struct dlm_lkb, lkb_ref);
  571. /* All work is done after the return from kref_put() so we
  572. can release the write_lock before the detach_lkb */
  573. DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
  574. }
  575. /* __put_lkb() is used when an lkb may not have an rsb attached to
  576. it so we need to provide the lockspace explicitly */
  577. static int __put_lkb(struct dlm_ls *ls, struct dlm_lkb *lkb)
  578. {
  579. uint32_t lkid = lkb->lkb_id;
  580. spin_lock(&ls->ls_lkbidr_spin);
  581. if (kref_put(&lkb->lkb_ref, kill_lkb)) {
  582. idr_remove(&ls->ls_lkbidr, lkid);
  583. spin_unlock(&ls->ls_lkbidr_spin);
  584. detach_lkb(lkb);
  585. /* for local/process lkbs, lvbptr points to caller's lksb */
  586. if (lkb->lkb_lvbptr && is_master_copy(lkb))
  587. dlm_free_lvb(lkb->lkb_lvbptr);
  588. dlm_free_lkb(lkb);
  589. return 1;
  590. } else {
  591. spin_unlock(&ls->ls_lkbidr_spin);
  592. return 0;
  593. }
  594. }
  595. int dlm_put_lkb(struct dlm_lkb *lkb)
  596. {
  597. struct dlm_ls *ls;
  598. DLM_ASSERT(lkb->lkb_resource, dlm_print_lkb(lkb););
  599. DLM_ASSERT(lkb->lkb_resource->res_ls, dlm_print_lkb(lkb););
  600. ls = lkb->lkb_resource->res_ls;
  601. return __put_lkb(ls, lkb);
  602. }
  603. /* This is only called to add a reference when the code already holds
  604. a valid reference to the lkb, so there's no need for locking. */
  605. static inline void hold_lkb(struct dlm_lkb *lkb)
  606. {
  607. kref_get(&lkb->lkb_ref);
  608. }
  609. /* This is called when we need to remove a reference and are certain
  610. it's not the last ref. e.g. del_lkb is always called between a
  611. find_lkb/put_lkb and is always the inverse of a previous add_lkb.
  612. put_lkb would work fine, but would involve unnecessary locking */
  613. static inline void unhold_lkb(struct dlm_lkb *lkb)
  614. {
  615. int rv;
  616. rv = kref_put(&lkb->lkb_ref, kill_lkb);
  617. DLM_ASSERT(!rv, dlm_print_lkb(lkb););
  618. }
  619. static void lkb_add_ordered(struct list_head *new, struct list_head *head,
  620. int mode)
  621. {
  622. struct dlm_lkb *lkb = NULL;
  623. list_for_each_entry(lkb, head, lkb_statequeue)
  624. if (lkb->lkb_rqmode < mode)
  625. break;
  626. __list_add(new, lkb->lkb_statequeue.prev, &lkb->lkb_statequeue);
  627. }
  628. /* add/remove lkb to rsb's grant/convert/wait queue */
  629. static void add_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int status)
  630. {
  631. kref_get(&lkb->lkb_ref);
  632. DLM_ASSERT(!lkb->lkb_status, dlm_print_lkb(lkb););
  633. lkb->lkb_timestamp = ktime_get();
  634. lkb->lkb_status = status;
  635. switch (status) {
  636. case DLM_LKSTS_WAITING:
  637. if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
  638. list_add(&lkb->lkb_statequeue, &r->res_waitqueue);
  639. else
  640. list_add_tail(&lkb->lkb_statequeue, &r->res_waitqueue);
  641. break;
  642. case DLM_LKSTS_GRANTED:
  643. /* convention says granted locks kept in order of grmode */
  644. lkb_add_ordered(&lkb->lkb_statequeue, &r->res_grantqueue,
  645. lkb->lkb_grmode);
  646. break;
  647. case DLM_LKSTS_CONVERT:
  648. if (lkb->lkb_exflags & DLM_LKF_HEADQUE)
  649. list_add(&lkb->lkb_statequeue, &r->res_convertqueue);
  650. else
  651. list_add_tail(&lkb->lkb_statequeue,
  652. &r->res_convertqueue);
  653. break;
  654. default:
  655. DLM_ASSERT(0, dlm_print_lkb(lkb); printk("sts=%d\n", status););
  656. }
  657. }
  658. static void del_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb)
  659. {
  660. lkb->lkb_status = 0;
  661. list_del(&lkb->lkb_statequeue);
  662. unhold_lkb(lkb);
  663. }
  664. static void move_lkb(struct dlm_rsb *r, struct dlm_lkb *lkb, int sts)
  665. {
  666. hold_lkb(lkb);
  667. del_lkb(r, lkb);
  668. add_lkb(r, lkb, sts);
  669. unhold_lkb(lkb);
  670. }
  671. static int msg_reply_type(int mstype)
  672. {
  673. switch (mstype) {
  674. case DLM_MSG_REQUEST:
  675. return DLM_MSG_REQUEST_REPLY;
  676. case DLM_MSG_CONVERT:
  677. return DLM_MSG_CONVERT_REPLY;
  678. case DLM_MSG_UNLOCK:
  679. return DLM_MSG_UNLOCK_REPLY;
  680. case DLM_MSG_CANCEL:
  681. return DLM_MSG_CANCEL_REPLY;
  682. case DLM_MSG_LOOKUP:
  683. return DLM_MSG_LOOKUP_REPLY;
  684. }
  685. return -1;
  686. }
  687. static int nodeid_warned(int nodeid, int num_nodes, int *warned)
  688. {
  689. int i;
  690. for (i = 0; i < num_nodes; i++) {
  691. if (!warned[i]) {
  692. warned[i] = nodeid;
  693. return 0;
  694. }
  695. if (warned[i] == nodeid)
  696. return 1;
  697. }
  698. return 0;
  699. }
  700. void dlm_scan_waiters(struct dlm_ls *ls)
  701. {
  702. struct dlm_lkb *lkb;
  703. ktime_t zero = ktime_set(0, 0);
  704. s64 us;
  705. s64 debug_maxus = 0;
  706. u32 debug_scanned = 0;
  707. u32 debug_expired = 0;
  708. int num_nodes = 0;
  709. int *warned = NULL;
  710. if (!dlm_config.ci_waitwarn_us)
  711. return;
  712. mutex_lock(&ls->ls_waiters_mutex);
  713. list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
  714. if (ktime_equal(lkb->lkb_wait_time, zero))
  715. continue;
  716. debug_scanned++;
  717. us = ktime_to_us(ktime_sub(ktime_get(), lkb->lkb_wait_time));
  718. if (us < dlm_config.ci_waitwarn_us)
  719. continue;
  720. lkb->lkb_wait_time = zero;
  721. debug_expired++;
  722. if (us > debug_maxus)
  723. debug_maxus = us;
  724. if (!num_nodes) {
  725. num_nodes = ls->ls_num_nodes;
  726. warned = kzalloc(num_nodes * sizeof(int), GFP_KERNEL);
  727. }
  728. if (!warned)
  729. continue;
  730. if (nodeid_warned(lkb->lkb_wait_nodeid, num_nodes, warned))
  731. continue;
  732. log_error(ls, "waitwarn %x %lld %d us check connection to "
  733. "node %d", lkb->lkb_id, (long long)us,
  734. dlm_config.ci_waitwarn_us, lkb->lkb_wait_nodeid);
  735. }
  736. mutex_unlock(&ls->ls_waiters_mutex);
  737. kfree(warned);
  738. if (debug_expired)
  739. log_debug(ls, "scan_waiters %u warn %u over %d us max %lld us",
  740. debug_scanned, debug_expired,
  741. dlm_config.ci_waitwarn_us, (long long)debug_maxus);
  742. }
  743. /* add/remove lkb from global waiters list of lkb's waiting for
  744. a reply from a remote node */
  745. static int add_to_waiters(struct dlm_lkb *lkb, int mstype, int to_nodeid)
  746. {
  747. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  748. int error = 0;
  749. mutex_lock(&ls->ls_waiters_mutex);
  750. if (is_overlap_unlock(lkb) ||
  751. (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL))) {
  752. error = -EINVAL;
  753. goto out;
  754. }
  755. if (lkb->lkb_wait_type || is_overlap_cancel(lkb)) {
  756. switch (mstype) {
  757. case DLM_MSG_UNLOCK:
  758. lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
  759. break;
  760. case DLM_MSG_CANCEL:
  761. lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
  762. break;
  763. default:
  764. error = -EBUSY;
  765. goto out;
  766. }
  767. lkb->lkb_wait_count++;
  768. hold_lkb(lkb);
  769. log_debug(ls, "addwait %x cur %d overlap %d count %d f %x",
  770. lkb->lkb_id, lkb->lkb_wait_type, mstype,
  771. lkb->lkb_wait_count, lkb->lkb_flags);
  772. goto out;
  773. }
  774. DLM_ASSERT(!lkb->lkb_wait_count,
  775. dlm_print_lkb(lkb);
  776. printk("wait_count %d\n", lkb->lkb_wait_count););
  777. lkb->lkb_wait_count++;
  778. lkb->lkb_wait_type = mstype;
  779. lkb->lkb_wait_time = ktime_get();
  780. lkb->lkb_wait_nodeid = to_nodeid; /* for debugging */
  781. hold_lkb(lkb);
  782. list_add(&lkb->lkb_wait_reply, &ls->ls_waiters);
  783. out:
  784. if (error)
  785. log_error(ls, "addwait error %x %d flags %x %d %d %s",
  786. lkb->lkb_id, error, lkb->lkb_flags, mstype,
  787. lkb->lkb_wait_type, lkb->lkb_resource->res_name);
  788. mutex_unlock(&ls->ls_waiters_mutex);
  789. return error;
  790. }
  791. /* We clear the RESEND flag because we might be taking an lkb off the waiters
  792. list as part of process_requestqueue (e.g. a lookup that has an optimized
  793. request reply on the requestqueue) between dlm_recover_waiters_pre() which
  794. set RESEND and dlm_recover_waiters_post() */
  795. static int _remove_from_waiters(struct dlm_lkb *lkb, int mstype,
  796. struct dlm_message *ms)
  797. {
  798. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  799. int overlap_done = 0;
  800. if (is_overlap_unlock(lkb) && (mstype == DLM_MSG_UNLOCK_REPLY)) {
  801. log_debug(ls, "remwait %x unlock_reply overlap", lkb->lkb_id);
  802. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
  803. overlap_done = 1;
  804. goto out_del;
  805. }
  806. if (is_overlap_cancel(lkb) && (mstype == DLM_MSG_CANCEL_REPLY)) {
  807. log_debug(ls, "remwait %x cancel_reply overlap", lkb->lkb_id);
  808. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
  809. overlap_done = 1;
  810. goto out_del;
  811. }
  812. /* Cancel state was preemptively cleared by a successful convert,
  813. see next comment, nothing to do. */
  814. if ((mstype == DLM_MSG_CANCEL_REPLY) &&
  815. (lkb->lkb_wait_type != DLM_MSG_CANCEL)) {
  816. log_debug(ls, "remwait %x cancel_reply wait_type %d",
  817. lkb->lkb_id, lkb->lkb_wait_type);
  818. return -1;
  819. }
  820. /* Remove for the convert reply, and premptively remove for the
  821. cancel reply. A convert has been granted while there's still
  822. an outstanding cancel on it (the cancel is moot and the result
  823. in the cancel reply should be 0). We preempt the cancel reply
  824. because the app gets the convert result and then can follow up
  825. with another op, like convert. This subsequent op would see the
  826. lingering state of the cancel and fail with -EBUSY. */
  827. if ((mstype == DLM_MSG_CONVERT_REPLY) &&
  828. (lkb->lkb_wait_type == DLM_MSG_CONVERT) &&
  829. is_overlap_cancel(lkb) && ms && !ms->m_result) {
  830. log_debug(ls, "remwait %x convert_reply zap overlap_cancel",
  831. lkb->lkb_id);
  832. lkb->lkb_wait_type = 0;
  833. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
  834. lkb->lkb_wait_count--;
  835. goto out_del;
  836. }
  837. /* N.B. type of reply may not always correspond to type of original
  838. msg due to lookup->request optimization, verify others? */
  839. if (lkb->lkb_wait_type) {
  840. lkb->lkb_wait_type = 0;
  841. goto out_del;
  842. }
  843. log_error(ls, "remwait error %x reply %d flags %x no wait_type",
  844. lkb->lkb_id, mstype, lkb->lkb_flags);
  845. return -1;
  846. out_del:
  847. /* the force-unlock/cancel has completed and we haven't recvd a reply
  848. to the op that was in progress prior to the unlock/cancel; we
  849. give up on any reply to the earlier op. FIXME: not sure when/how
  850. this would happen */
  851. if (overlap_done && lkb->lkb_wait_type) {
  852. log_error(ls, "remwait error %x reply %d wait_type %d overlap",
  853. lkb->lkb_id, mstype, lkb->lkb_wait_type);
  854. lkb->lkb_wait_count--;
  855. lkb->lkb_wait_type = 0;
  856. }
  857. DLM_ASSERT(lkb->lkb_wait_count, dlm_print_lkb(lkb););
  858. lkb->lkb_flags &= ~DLM_IFL_RESEND;
  859. lkb->lkb_wait_count--;
  860. if (!lkb->lkb_wait_count)
  861. list_del_init(&lkb->lkb_wait_reply);
  862. unhold_lkb(lkb);
  863. return 0;
  864. }
  865. static int remove_from_waiters(struct dlm_lkb *lkb, int mstype)
  866. {
  867. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  868. int error;
  869. mutex_lock(&ls->ls_waiters_mutex);
  870. error = _remove_from_waiters(lkb, mstype, NULL);
  871. mutex_unlock(&ls->ls_waiters_mutex);
  872. return error;
  873. }
  874. /* Handles situations where we might be processing a "fake" or "stub" reply in
  875. which we can't try to take waiters_mutex again. */
  876. static int remove_from_waiters_ms(struct dlm_lkb *lkb, struct dlm_message *ms)
  877. {
  878. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  879. int error;
  880. if (ms->m_flags != DLM_IFL_STUB_MS)
  881. mutex_lock(&ls->ls_waiters_mutex);
  882. error = _remove_from_waiters(lkb, ms->m_type, ms);
  883. if (ms->m_flags != DLM_IFL_STUB_MS)
  884. mutex_unlock(&ls->ls_waiters_mutex);
  885. return error;
  886. }
  887. static void dir_remove(struct dlm_rsb *r)
  888. {
  889. int to_nodeid;
  890. if (dlm_no_directory(r->res_ls))
  891. return;
  892. to_nodeid = dlm_dir_nodeid(r);
  893. if (to_nodeid != dlm_our_nodeid())
  894. send_remove(r);
  895. else
  896. dlm_dir_remove_entry(r->res_ls, to_nodeid,
  897. r->res_name, r->res_length);
  898. }
  899. /* FIXME: shouldn't this be able to exit as soon as one non-due rsb is
  900. found since they are in order of newest to oldest? */
  901. static int shrink_bucket(struct dlm_ls *ls, int b)
  902. {
  903. struct dlm_rsb *r;
  904. int count = 0, found;
  905. for (;;) {
  906. found = 0;
  907. spin_lock(&ls->ls_rsbtbl[b].lock);
  908. list_for_each_entry_reverse(r, &ls->ls_rsbtbl[b].toss,
  909. res_hashchain) {
  910. if (!time_after_eq(jiffies, r->res_toss_time +
  911. dlm_config.ci_toss_secs * HZ))
  912. continue;
  913. found = 1;
  914. break;
  915. }
  916. if (!found) {
  917. spin_unlock(&ls->ls_rsbtbl[b].lock);
  918. break;
  919. }
  920. if (kref_put(&r->res_ref, kill_rsb)) {
  921. list_del(&r->res_hashchain);
  922. spin_unlock(&ls->ls_rsbtbl[b].lock);
  923. if (is_master(r))
  924. dir_remove(r);
  925. dlm_free_rsb(r);
  926. count++;
  927. } else {
  928. spin_unlock(&ls->ls_rsbtbl[b].lock);
  929. log_error(ls, "tossed rsb in use %s", r->res_name);
  930. }
  931. }
  932. return count;
  933. }
  934. void dlm_scan_rsbs(struct dlm_ls *ls)
  935. {
  936. int i;
  937. for (i = 0; i < ls->ls_rsbtbl_size; i++) {
  938. shrink_bucket(ls, i);
  939. if (dlm_locking_stopped(ls))
  940. break;
  941. cond_resched();
  942. }
  943. }
  944. static void add_timeout(struct dlm_lkb *lkb)
  945. {
  946. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  947. if (is_master_copy(lkb))
  948. return;
  949. if (test_bit(LSFL_TIMEWARN, &ls->ls_flags) &&
  950. !(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
  951. lkb->lkb_flags |= DLM_IFL_WATCH_TIMEWARN;
  952. goto add_it;
  953. }
  954. if (lkb->lkb_exflags & DLM_LKF_TIMEOUT)
  955. goto add_it;
  956. return;
  957. add_it:
  958. DLM_ASSERT(list_empty(&lkb->lkb_time_list), dlm_print_lkb(lkb););
  959. mutex_lock(&ls->ls_timeout_mutex);
  960. hold_lkb(lkb);
  961. list_add_tail(&lkb->lkb_time_list, &ls->ls_timeout);
  962. mutex_unlock(&ls->ls_timeout_mutex);
  963. }
  964. static void del_timeout(struct dlm_lkb *lkb)
  965. {
  966. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  967. mutex_lock(&ls->ls_timeout_mutex);
  968. if (!list_empty(&lkb->lkb_time_list)) {
  969. list_del_init(&lkb->lkb_time_list);
  970. unhold_lkb(lkb);
  971. }
  972. mutex_unlock(&ls->ls_timeout_mutex);
  973. }
  974. /* FIXME: is it safe to look at lkb_exflags, lkb_flags, lkb_timestamp, and
  975. lkb_lksb_timeout without lock_rsb? Note: we can't lock timeout_mutex
  976. and then lock rsb because of lock ordering in add_timeout. We may need
  977. to specify some special timeout-related bits in the lkb that are just to
  978. be accessed under the timeout_mutex. */
  979. void dlm_scan_timeout(struct dlm_ls *ls)
  980. {
  981. struct dlm_rsb *r;
  982. struct dlm_lkb *lkb;
  983. int do_cancel, do_warn;
  984. s64 wait_us;
  985. for (;;) {
  986. if (dlm_locking_stopped(ls))
  987. break;
  988. do_cancel = 0;
  989. do_warn = 0;
  990. mutex_lock(&ls->ls_timeout_mutex);
  991. list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list) {
  992. wait_us = ktime_to_us(ktime_sub(ktime_get(),
  993. lkb->lkb_timestamp));
  994. if ((lkb->lkb_exflags & DLM_LKF_TIMEOUT) &&
  995. wait_us >= (lkb->lkb_timeout_cs * 10000))
  996. do_cancel = 1;
  997. if ((lkb->lkb_flags & DLM_IFL_WATCH_TIMEWARN) &&
  998. wait_us >= dlm_config.ci_timewarn_cs * 10000)
  999. do_warn = 1;
  1000. if (!do_cancel && !do_warn)
  1001. continue;
  1002. hold_lkb(lkb);
  1003. break;
  1004. }
  1005. mutex_unlock(&ls->ls_timeout_mutex);
  1006. if (!do_cancel && !do_warn)
  1007. break;
  1008. r = lkb->lkb_resource;
  1009. hold_rsb(r);
  1010. lock_rsb(r);
  1011. if (do_warn) {
  1012. /* clear flag so we only warn once */
  1013. lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
  1014. if (!(lkb->lkb_exflags & DLM_LKF_TIMEOUT))
  1015. del_timeout(lkb);
  1016. dlm_timeout_warn(lkb);
  1017. }
  1018. if (do_cancel) {
  1019. log_debug(ls, "timeout cancel %x node %d %s",
  1020. lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
  1021. lkb->lkb_flags &= ~DLM_IFL_WATCH_TIMEWARN;
  1022. lkb->lkb_flags |= DLM_IFL_TIMEOUT_CANCEL;
  1023. del_timeout(lkb);
  1024. _cancel_lock(r, lkb);
  1025. }
  1026. unlock_rsb(r);
  1027. unhold_rsb(r);
  1028. dlm_put_lkb(lkb);
  1029. }
  1030. }
  1031. /* This is only called by dlm_recoverd, and we rely on dlm_ls_stop() stopping
  1032. dlm_recoverd before checking/setting ls_recover_begin. */
  1033. void dlm_adjust_timeouts(struct dlm_ls *ls)
  1034. {
  1035. struct dlm_lkb *lkb;
  1036. u64 adj_us = jiffies_to_usecs(jiffies - ls->ls_recover_begin);
  1037. ls->ls_recover_begin = 0;
  1038. mutex_lock(&ls->ls_timeout_mutex);
  1039. list_for_each_entry(lkb, &ls->ls_timeout, lkb_time_list)
  1040. lkb->lkb_timestamp = ktime_add_us(lkb->lkb_timestamp, adj_us);
  1041. mutex_unlock(&ls->ls_timeout_mutex);
  1042. if (!dlm_config.ci_waitwarn_us)
  1043. return;
  1044. mutex_lock(&ls->ls_waiters_mutex);
  1045. list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
  1046. if (ktime_to_us(lkb->lkb_wait_time))
  1047. lkb->lkb_wait_time = ktime_get();
  1048. }
  1049. mutex_unlock(&ls->ls_waiters_mutex);
  1050. }
  1051. /* lkb is master or local copy */
  1052. static void set_lvb_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1053. {
  1054. int b, len = r->res_ls->ls_lvblen;
  1055. /* b=1 lvb returned to caller
  1056. b=0 lvb written to rsb or invalidated
  1057. b=-1 do nothing */
  1058. b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
  1059. if (b == 1) {
  1060. if (!lkb->lkb_lvbptr)
  1061. return;
  1062. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  1063. return;
  1064. if (!r->res_lvbptr)
  1065. return;
  1066. memcpy(lkb->lkb_lvbptr, r->res_lvbptr, len);
  1067. lkb->lkb_lvbseq = r->res_lvbseq;
  1068. } else if (b == 0) {
  1069. if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
  1070. rsb_set_flag(r, RSB_VALNOTVALID);
  1071. return;
  1072. }
  1073. if (!lkb->lkb_lvbptr)
  1074. return;
  1075. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  1076. return;
  1077. if (!r->res_lvbptr)
  1078. r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
  1079. if (!r->res_lvbptr)
  1080. return;
  1081. memcpy(r->res_lvbptr, lkb->lkb_lvbptr, len);
  1082. r->res_lvbseq++;
  1083. lkb->lkb_lvbseq = r->res_lvbseq;
  1084. rsb_clear_flag(r, RSB_VALNOTVALID);
  1085. }
  1086. if (rsb_flag(r, RSB_VALNOTVALID))
  1087. lkb->lkb_sbflags |= DLM_SBF_VALNOTVALID;
  1088. }
  1089. static void set_lvb_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1090. {
  1091. if (lkb->lkb_grmode < DLM_LOCK_PW)
  1092. return;
  1093. if (lkb->lkb_exflags & DLM_LKF_IVVALBLK) {
  1094. rsb_set_flag(r, RSB_VALNOTVALID);
  1095. return;
  1096. }
  1097. if (!lkb->lkb_lvbptr)
  1098. return;
  1099. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  1100. return;
  1101. if (!r->res_lvbptr)
  1102. r->res_lvbptr = dlm_allocate_lvb(r->res_ls);
  1103. if (!r->res_lvbptr)
  1104. return;
  1105. memcpy(r->res_lvbptr, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
  1106. r->res_lvbseq++;
  1107. rsb_clear_flag(r, RSB_VALNOTVALID);
  1108. }
  1109. /* lkb is process copy (pc) */
  1110. static void set_lvb_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
  1111. struct dlm_message *ms)
  1112. {
  1113. int b;
  1114. if (!lkb->lkb_lvbptr)
  1115. return;
  1116. if (!(lkb->lkb_exflags & DLM_LKF_VALBLK))
  1117. return;
  1118. b = dlm_lvb_operations[lkb->lkb_grmode + 1][lkb->lkb_rqmode + 1];
  1119. if (b == 1) {
  1120. int len = receive_extralen(ms);
  1121. if (len > DLM_RESNAME_MAXLEN)
  1122. len = DLM_RESNAME_MAXLEN;
  1123. memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
  1124. lkb->lkb_lvbseq = ms->m_lvbseq;
  1125. }
  1126. }
  1127. /* Manipulate lkb's on rsb's convert/granted/waiting queues
  1128. remove_lock -- used for unlock, removes lkb from granted
  1129. revert_lock -- used for cancel, moves lkb from convert to granted
  1130. grant_lock -- used for request and convert, adds lkb to granted or
  1131. moves lkb from convert or waiting to granted
  1132. Each of these is used for master or local copy lkb's. There is
  1133. also a _pc() variation used to make the corresponding change on
  1134. a process copy (pc) lkb. */
  1135. static void _remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1136. {
  1137. del_lkb(r, lkb);
  1138. lkb->lkb_grmode = DLM_LOCK_IV;
  1139. /* this unhold undoes the original ref from create_lkb()
  1140. so this leads to the lkb being freed */
  1141. unhold_lkb(lkb);
  1142. }
  1143. static void remove_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1144. {
  1145. set_lvb_unlock(r, lkb);
  1146. _remove_lock(r, lkb);
  1147. }
  1148. static void remove_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1149. {
  1150. _remove_lock(r, lkb);
  1151. }
  1152. /* returns: 0 did nothing
  1153. 1 moved lock to granted
  1154. -1 removed lock */
  1155. static int revert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1156. {
  1157. int rv = 0;
  1158. lkb->lkb_rqmode = DLM_LOCK_IV;
  1159. switch (lkb->lkb_status) {
  1160. case DLM_LKSTS_GRANTED:
  1161. break;
  1162. case DLM_LKSTS_CONVERT:
  1163. move_lkb(r, lkb, DLM_LKSTS_GRANTED);
  1164. rv = 1;
  1165. break;
  1166. case DLM_LKSTS_WAITING:
  1167. del_lkb(r, lkb);
  1168. lkb->lkb_grmode = DLM_LOCK_IV;
  1169. /* this unhold undoes the original ref from create_lkb()
  1170. so this leads to the lkb being freed */
  1171. unhold_lkb(lkb);
  1172. rv = -1;
  1173. break;
  1174. default:
  1175. log_print("invalid status for revert %d", lkb->lkb_status);
  1176. }
  1177. return rv;
  1178. }
  1179. static int revert_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1180. {
  1181. return revert_lock(r, lkb);
  1182. }
  1183. static void _grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1184. {
  1185. if (lkb->lkb_grmode != lkb->lkb_rqmode) {
  1186. lkb->lkb_grmode = lkb->lkb_rqmode;
  1187. if (lkb->lkb_status)
  1188. move_lkb(r, lkb, DLM_LKSTS_GRANTED);
  1189. else
  1190. add_lkb(r, lkb, DLM_LKSTS_GRANTED);
  1191. }
  1192. lkb->lkb_rqmode = DLM_LOCK_IV;
  1193. }
  1194. static void grant_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1195. {
  1196. set_lvb_lock(r, lkb);
  1197. _grant_lock(r, lkb);
  1198. lkb->lkb_highbast = 0;
  1199. }
  1200. static void grant_lock_pc(struct dlm_rsb *r, struct dlm_lkb *lkb,
  1201. struct dlm_message *ms)
  1202. {
  1203. set_lvb_lock_pc(r, lkb, ms);
  1204. _grant_lock(r, lkb);
  1205. }
  1206. /* called by grant_pending_locks() which means an async grant message must
  1207. be sent to the requesting node in addition to granting the lock if the
  1208. lkb belongs to a remote node. */
  1209. static void grant_lock_pending(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1210. {
  1211. grant_lock(r, lkb);
  1212. if (is_master_copy(lkb))
  1213. send_grant(r, lkb);
  1214. else
  1215. queue_cast(r, lkb, 0);
  1216. }
  1217. /* The special CONVDEADLK, ALTPR and ALTCW flags allow the master to
  1218. change the granted/requested modes. We're munging things accordingly in
  1219. the process copy.
  1220. CONVDEADLK: our grmode may have been forced down to NL to resolve a
  1221. conversion deadlock
  1222. ALTPR/ALTCW: our rqmode may have been changed to PR or CW to become
  1223. compatible with other granted locks */
  1224. static void munge_demoted(struct dlm_lkb *lkb)
  1225. {
  1226. if (lkb->lkb_rqmode == DLM_LOCK_IV || lkb->lkb_grmode == DLM_LOCK_IV) {
  1227. log_print("munge_demoted %x invalid modes gr %d rq %d",
  1228. lkb->lkb_id, lkb->lkb_grmode, lkb->lkb_rqmode);
  1229. return;
  1230. }
  1231. lkb->lkb_grmode = DLM_LOCK_NL;
  1232. }
  1233. static void munge_altmode(struct dlm_lkb *lkb, struct dlm_message *ms)
  1234. {
  1235. if (ms->m_type != DLM_MSG_REQUEST_REPLY &&
  1236. ms->m_type != DLM_MSG_GRANT) {
  1237. log_print("munge_altmode %x invalid reply type %d",
  1238. lkb->lkb_id, ms->m_type);
  1239. return;
  1240. }
  1241. if (lkb->lkb_exflags & DLM_LKF_ALTPR)
  1242. lkb->lkb_rqmode = DLM_LOCK_PR;
  1243. else if (lkb->lkb_exflags & DLM_LKF_ALTCW)
  1244. lkb->lkb_rqmode = DLM_LOCK_CW;
  1245. else {
  1246. log_print("munge_altmode invalid exflags %x", lkb->lkb_exflags);
  1247. dlm_print_lkb(lkb);
  1248. }
  1249. }
  1250. static inline int first_in_list(struct dlm_lkb *lkb, struct list_head *head)
  1251. {
  1252. struct dlm_lkb *first = list_entry(head->next, struct dlm_lkb,
  1253. lkb_statequeue);
  1254. if (lkb->lkb_id == first->lkb_id)
  1255. return 1;
  1256. return 0;
  1257. }
  1258. /* Check if the given lkb conflicts with another lkb on the queue. */
  1259. static int queue_conflict(struct list_head *head, struct dlm_lkb *lkb)
  1260. {
  1261. struct dlm_lkb *this;
  1262. list_for_each_entry(this, head, lkb_statequeue) {
  1263. if (this == lkb)
  1264. continue;
  1265. if (!modes_compat(this, lkb))
  1266. return 1;
  1267. }
  1268. return 0;
  1269. }
  1270. /*
  1271. * "A conversion deadlock arises with a pair of lock requests in the converting
  1272. * queue for one resource. The granted mode of each lock blocks the requested
  1273. * mode of the other lock."
  1274. *
  1275. * Part 2: if the granted mode of lkb is preventing an earlier lkb in the
  1276. * convert queue from being granted, then deadlk/demote lkb.
  1277. *
  1278. * Example:
  1279. * Granted Queue: empty
  1280. * Convert Queue: NL->EX (first lock)
  1281. * PR->EX (second lock)
  1282. *
  1283. * The first lock can't be granted because of the granted mode of the second
  1284. * lock and the second lock can't be granted because it's not first in the
  1285. * list. We either cancel lkb's conversion (PR->EX) and return EDEADLK, or we
  1286. * demote the granted mode of lkb (from PR to NL) if it has the CONVDEADLK
  1287. * flag set and return DEMOTED in the lksb flags.
  1288. *
  1289. * Originally, this function detected conv-deadlk in a more limited scope:
  1290. * - if !modes_compat(lkb1, lkb2) && !modes_compat(lkb2, lkb1), or
  1291. * - if lkb1 was the first entry in the queue (not just earlier), and was
  1292. * blocked by the granted mode of lkb2, and there was nothing on the
  1293. * granted queue preventing lkb1 from being granted immediately, i.e.
  1294. * lkb2 was the only thing preventing lkb1 from being granted.
  1295. *
  1296. * That second condition meant we'd only say there was conv-deadlk if
  1297. * resolving it (by demotion) would lead to the first lock on the convert
  1298. * queue being granted right away. It allowed conversion deadlocks to exist
  1299. * between locks on the convert queue while they couldn't be granted anyway.
  1300. *
  1301. * Now, we detect and take action on conversion deadlocks immediately when
  1302. * they're created, even if they may not be immediately consequential. If
  1303. * lkb1 exists anywhere in the convert queue and lkb2 comes in with a granted
  1304. * mode that would prevent lkb1's conversion from being granted, we do a
  1305. * deadlk/demote on lkb2 right away and don't let it onto the convert queue.
  1306. * I think this means that the lkb_is_ahead condition below should always
  1307. * be zero, i.e. there will never be conv-deadlk between two locks that are
  1308. * both already on the convert queue.
  1309. */
  1310. static int conversion_deadlock_detect(struct dlm_rsb *r, struct dlm_lkb *lkb2)
  1311. {
  1312. struct dlm_lkb *lkb1;
  1313. int lkb_is_ahead = 0;
  1314. list_for_each_entry(lkb1, &r->res_convertqueue, lkb_statequeue) {
  1315. if (lkb1 == lkb2) {
  1316. lkb_is_ahead = 1;
  1317. continue;
  1318. }
  1319. if (!lkb_is_ahead) {
  1320. if (!modes_compat(lkb2, lkb1))
  1321. return 1;
  1322. } else {
  1323. if (!modes_compat(lkb2, lkb1) &&
  1324. !modes_compat(lkb1, lkb2))
  1325. return 1;
  1326. }
  1327. }
  1328. return 0;
  1329. }
  1330. /*
  1331. * Return 1 if the lock can be granted, 0 otherwise.
  1332. * Also detect and resolve conversion deadlocks.
  1333. *
  1334. * lkb is the lock to be granted
  1335. *
  1336. * now is 1 if the function is being called in the context of the
  1337. * immediate request, it is 0 if called later, after the lock has been
  1338. * queued.
  1339. *
  1340. * References are from chapter 6 of "VAXcluster Principles" by Roy Davis
  1341. */
  1342. static int _can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now)
  1343. {
  1344. int8_t conv = (lkb->lkb_grmode != DLM_LOCK_IV);
  1345. /*
  1346. * 6-10: Version 5.4 introduced an option to address the phenomenon of
  1347. * a new request for a NL mode lock being blocked.
  1348. *
  1349. * 6-11: If the optional EXPEDITE flag is used with the new NL mode
  1350. * request, then it would be granted. In essence, the use of this flag
  1351. * tells the Lock Manager to expedite theis request by not considering
  1352. * what may be in the CONVERTING or WAITING queues... As of this
  1353. * writing, the EXPEDITE flag can be used only with new requests for NL
  1354. * mode locks. This flag is not valid for conversion requests.
  1355. *
  1356. * A shortcut. Earlier checks return an error if EXPEDITE is used in a
  1357. * conversion or used with a non-NL requested mode. We also know an
  1358. * EXPEDITE request is always granted immediately, so now must always
  1359. * be 1. The full condition to grant an expedite request: (now &&
  1360. * !conv && lkb->rqmode == DLM_LOCK_NL && (flags & EXPEDITE)) can
  1361. * therefore be shortened to just checking the flag.
  1362. */
  1363. if (lkb->lkb_exflags & DLM_LKF_EXPEDITE)
  1364. return 1;
  1365. /*
  1366. * A shortcut. Without this, !queue_conflict(grantqueue, lkb) would be
  1367. * added to the remaining conditions.
  1368. */
  1369. if (queue_conflict(&r->res_grantqueue, lkb))
  1370. goto out;
  1371. /*
  1372. * 6-3: By default, a conversion request is immediately granted if the
  1373. * requested mode is compatible with the modes of all other granted
  1374. * locks
  1375. */
  1376. if (queue_conflict(&r->res_convertqueue, lkb))
  1377. goto out;
  1378. /*
  1379. * 6-5: But the default algorithm for deciding whether to grant or
  1380. * queue conversion requests does not by itself guarantee that such
  1381. * requests are serviced on a "first come first serve" basis. This, in
  1382. * turn, can lead to a phenomenon known as "indefinate postponement".
  1383. *
  1384. * 6-7: This issue is dealt with by using the optional QUECVT flag with
  1385. * the system service employed to request a lock conversion. This flag
  1386. * forces certain conversion requests to be queued, even if they are
  1387. * compatible with the granted modes of other locks on the same
  1388. * resource. Thus, the use of this flag results in conversion requests
  1389. * being ordered on a "first come first servce" basis.
  1390. *
  1391. * DCT: This condition is all about new conversions being able to occur
  1392. * "in place" while the lock remains on the granted queue (assuming
  1393. * nothing else conflicts.) IOW if QUECVT isn't set, a conversion
  1394. * doesn't _have_ to go onto the convert queue where it's processed in
  1395. * order. The "now" variable is necessary to distinguish converts
  1396. * being received and processed for the first time now, because once a
  1397. * convert is moved to the conversion queue the condition below applies
  1398. * requiring fifo granting.
  1399. */
  1400. if (now && conv && !(lkb->lkb_exflags & DLM_LKF_QUECVT))
  1401. return 1;
  1402. /*
  1403. * The NOORDER flag is set to avoid the standard vms rules on grant
  1404. * order.
  1405. */
  1406. if (lkb->lkb_exflags & DLM_LKF_NOORDER)
  1407. return 1;
  1408. /*
  1409. * 6-3: Once in that queue [CONVERTING], a conversion request cannot be
  1410. * granted until all other conversion requests ahead of it are granted
  1411. * and/or canceled.
  1412. */
  1413. if (!now && conv && first_in_list(lkb, &r->res_convertqueue))
  1414. return 1;
  1415. /*
  1416. * 6-4: By default, a new request is immediately granted only if all
  1417. * three of the following conditions are satisfied when the request is
  1418. * issued:
  1419. * - The queue of ungranted conversion requests for the resource is
  1420. * empty.
  1421. * - The queue of ungranted new requests for the resource is empty.
  1422. * - The mode of the new request is compatible with the most
  1423. * restrictive mode of all granted locks on the resource.
  1424. */
  1425. if (now && !conv && list_empty(&r->res_convertqueue) &&
  1426. list_empty(&r->res_waitqueue))
  1427. return 1;
  1428. /*
  1429. * 6-4: Once a lock request is in the queue of ungranted new requests,
  1430. * it cannot be granted until the queue of ungranted conversion
  1431. * requests is empty, all ungranted new requests ahead of it are
  1432. * granted and/or canceled, and it is compatible with the granted mode
  1433. * of the most restrictive lock granted on the resource.
  1434. */
  1435. if (!now && !conv && list_empty(&r->res_convertqueue) &&
  1436. first_in_list(lkb, &r->res_waitqueue))
  1437. return 1;
  1438. out:
  1439. return 0;
  1440. }
  1441. static int can_be_granted(struct dlm_rsb *r, struct dlm_lkb *lkb, int now,
  1442. int *err)
  1443. {
  1444. int rv;
  1445. int8_t alt = 0, rqmode = lkb->lkb_rqmode;
  1446. int8_t is_convert = (lkb->lkb_grmode != DLM_LOCK_IV);
  1447. if (err)
  1448. *err = 0;
  1449. rv = _can_be_granted(r, lkb, now);
  1450. if (rv)
  1451. goto out;
  1452. /*
  1453. * The CONVDEADLK flag is non-standard and tells the dlm to resolve
  1454. * conversion deadlocks by demoting grmode to NL, otherwise the dlm
  1455. * cancels one of the locks.
  1456. */
  1457. if (is_convert && can_be_queued(lkb) &&
  1458. conversion_deadlock_detect(r, lkb)) {
  1459. if (lkb->lkb_exflags & DLM_LKF_CONVDEADLK) {
  1460. lkb->lkb_grmode = DLM_LOCK_NL;
  1461. lkb->lkb_sbflags |= DLM_SBF_DEMOTED;
  1462. } else if (!(lkb->lkb_exflags & DLM_LKF_NODLCKWT)) {
  1463. if (err)
  1464. *err = -EDEADLK;
  1465. else {
  1466. log_print("can_be_granted deadlock %x now %d",
  1467. lkb->lkb_id, now);
  1468. dlm_dump_rsb(r);
  1469. }
  1470. }
  1471. goto out;
  1472. }
  1473. /*
  1474. * The ALTPR and ALTCW flags are non-standard and tell the dlm to try
  1475. * to grant a request in a mode other than the normal rqmode. It's a
  1476. * simple way to provide a big optimization to applications that can
  1477. * use them.
  1478. */
  1479. if (rqmode != DLM_LOCK_PR && (lkb->lkb_exflags & DLM_LKF_ALTPR))
  1480. alt = DLM_LOCK_PR;
  1481. else if (rqmode != DLM_LOCK_CW && (lkb->lkb_exflags & DLM_LKF_ALTCW))
  1482. alt = DLM_LOCK_CW;
  1483. if (alt) {
  1484. lkb->lkb_rqmode = alt;
  1485. rv = _can_be_granted(r, lkb, now);
  1486. if (rv)
  1487. lkb->lkb_sbflags |= DLM_SBF_ALTMODE;
  1488. else
  1489. lkb->lkb_rqmode = rqmode;
  1490. }
  1491. out:
  1492. return rv;
  1493. }
  1494. /* FIXME: I don't think that can_be_granted() can/will demote or find deadlock
  1495. for locks pending on the convert list. Once verified (watch for these
  1496. log_prints), we should be able to just call _can_be_granted() and not
  1497. bother with the demote/deadlk cases here (and there's no easy way to deal
  1498. with a deadlk here, we'd have to generate something like grant_lock with
  1499. the deadlk error.) */
  1500. /* Returns the highest requested mode of all blocked conversions; sets
  1501. cw if there's a blocked conversion to DLM_LOCK_CW. */
  1502. static int grant_pending_convert(struct dlm_rsb *r, int high, int *cw)
  1503. {
  1504. struct dlm_lkb *lkb, *s;
  1505. int hi, demoted, quit, grant_restart, demote_restart;
  1506. int deadlk;
  1507. quit = 0;
  1508. restart:
  1509. grant_restart = 0;
  1510. demote_restart = 0;
  1511. hi = DLM_LOCK_IV;
  1512. list_for_each_entry_safe(lkb, s, &r->res_convertqueue, lkb_statequeue) {
  1513. demoted = is_demoted(lkb);
  1514. deadlk = 0;
  1515. if (can_be_granted(r, lkb, 0, &deadlk)) {
  1516. grant_lock_pending(r, lkb);
  1517. grant_restart = 1;
  1518. continue;
  1519. }
  1520. if (!demoted && is_demoted(lkb)) {
  1521. log_print("WARN: pending demoted %x node %d %s",
  1522. lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
  1523. demote_restart = 1;
  1524. continue;
  1525. }
  1526. if (deadlk) {
  1527. log_print("WARN: pending deadlock %x node %d %s",
  1528. lkb->lkb_id, lkb->lkb_nodeid, r->res_name);
  1529. dlm_dump_rsb(r);
  1530. continue;
  1531. }
  1532. hi = max_t(int, lkb->lkb_rqmode, hi);
  1533. if (cw && lkb->lkb_rqmode == DLM_LOCK_CW)
  1534. *cw = 1;
  1535. }
  1536. if (grant_restart)
  1537. goto restart;
  1538. if (demote_restart && !quit) {
  1539. quit = 1;
  1540. goto restart;
  1541. }
  1542. return max_t(int, high, hi);
  1543. }
  1544. static int grant_pending_wait(struct dlm_rsb *r, int high, int *cw)
  1545. {
  1546. struct dlm_lkb *lkb, *s;
  1547. list_for_each_entry_safe(lkb, s, &r->res_waitqueue, lkb_statequeue) {
  1548. if (can_be_granted(r, lkb, 0, NULL))
  1549. grant_lock_pending(r, lkb);
  1550. else {
  1551. high = max_t(int, lkb->lkb_rqmode, high);
  1552. if (lkb->lkb_rqmode == DLM_LOCK_CW)
  1553. *cw = 1;
  1554. }
  1555. }
  1556. return high;
  1557. }
  1558. /* cw of 1 means there's a lock with a rqmode of DLM_LOCK_CW that's blocked
  1559. on either the convert or waiting queue.
  1560. high is the largest rqmode of all locks blocked on the convert or
  1561. waiting queue. */
  1562. static int lock_requires_bast(struct dlm_lkb *gr, int high, int cw)
  1563. {
  1564. if (gr->lkb_grmode == DLM_LOCK_PR && cw) {
  1565. if (gr->lkb_highbast < DLM_LOCK_EX)
  1566. return 1;
  1567. return 0;
  1568. }
  1569. if (gr->lkb_highbast < high &&
  1570. !__dlm_compat_matrix[gr->lkb_grmode+1][high+1])
  1571. return 1;
  1572. return 0;
  1573. }
  1574. static void grant_pending_locks(struct dlm_rsb *r)
  1575. {
  1576. struct dlm_lkb *lkb, *s;
  1577. int high = DLM_LOCK_IV;
  1578. int cw = 0;
  1579. DLM_ASSERT(is_master(r), dlm_dump_rsb(r););
  1580. high = grant_pending_convert(r, high, &cw);
  1581. high = grant_pending_wait(r, high, &cw);
  1582. if (high == DLM_LOCK_IV)
  1583. return;
  1584. /*
  1585. * If there are locks left on the wait/convert queue then send blocking
  1586. * ASTs to granted locks based on the largest requested mode (high)
  1587. * found above.
  1588. */
  1589. list_for_each_entry_safe(lkb, s, &r->res_grantqueue, lkb_statequeue) {
  1590. if (lkb->lkb_bastfn && lock_requires_bast(lkb, high, cw)) {
  1591. if (cw && high == DLM_LOCK_PR &&
  1592. lkb->lkb_grmode == DLM_LOCK_PR)
  1593. queue_bast(r, lkb, DLM_LOCK_CW);
  1594. else
  1595. queue_bast(r, lkb, high);
  1596. lkb->lkb_highbast = high;
  1597. }
  1598. }
  1599. }
  1600. static int modes_require_bast(struct dlm_lkb *gr, struct dlm_lkb *rq)
  1601. {
  1602. if ((gr->lkb_grmode == DLM_LOCK_PR && rq->lkb_rqmode == DLM_LOCK_CW) ||
  1603. (gr->lkb_grmode == DLM_LOCK_CW && rq->lkb_rqmode == DLM_LOCK_PR)) {
  1604. if (gr->lkb_highbast < DLM_LOCK_EX)
  1605. return 1;
  1606. return 0;
  1607. }
  1608. if (gr->lkb_highbast < rq->lkb_rqmode && !modes_compat(gr, rq))
  1609. return 1;
  1610. return 0;
  1611. }
  1612. static void send_bast_queue(struct dlm_rsb *r, struct list_head *head,
  1613. struct dlm_lkb *lkb)
  1614. {
  1615. struct dlm_lkb *gr;
  1616. list_for_each_entry(gr, head, lkb_statequeue) {
  1617. /* skip self when sending basts to convertqueue */
  1618. if (gr == lkb)
  1619. continue;
  1620. if (gr->lkb_bastfn && modes_require_bast(gr, lkb)) {
  1621. queue_bast(r, gr, lkb->lkb_rqmode);
  1622. gr->lkb_highbast = lkb->lkb_rqmode;
  1623. }
  1624. }
  1625. }
  1626. static void send_blocking_asts(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1627. {
  1628. send_bast_queue(r, &r->res_grantqueue, lkb);
  1629. }
  1630. static void send_blocking_asts_all(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1631. {
  1632. send_bast_queue(r, &r->res_grantqueue, lkb);
  1633. send_bast_queue(r, &r->res_convertqueue, lkb);
  1634. }
  1635. /* set_master(r, lkb) -- set the master nodeid of a resource
  1636. The purpose of this function is to set the nodeid field in the given
  1637. lkb using the nodeid field in the given rsb. If the rsb's nodeid is
  1638. known, it can just be copied to the lkb and the function will return
  1639. 0. If the rsb's nodeid is _not_ known, it needs to be looked up
  1640. before it can be copied to the lkb.
  1641. When the rsb nodeid is being looked up remotely, the initial lkb
  1642. causing the lookup is kept on the ls_waiters list waiting for the
  1643. lookup reply. Other lkb's waiting for the same rsb lookup are kept
  1644. on the rsb's res_lookup list until the master is verified.
  1645. Return values:
  1646. 0: nodeid is set in rsb/lkb and the caller should go ahead and use it
  1647. 1: the rsb master is not available and the lkb has been placed on
  1648. a wait queue
  1649. */
  1650. static int set_master(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1651. {
  1652. struct dlm_ls *ls = r->res_ls;
  1653. int i, error, dir_nodeid, ret_nodeid, our_nodeid = dlm_our_nodeid();
  1654. if (rsb_flag(r, RSB_MASTER_UNCERTAIN)) {
  1655. rsb_clear_flag(r, RSB_MASTER_UNCERTAIN);
  1656. r->res_first_lkid = lkb->lkb_id;
  1657. lkb->lkb_nodeid = r->res_nodeid;
  1658. return 0;
  1659. }
  1660. if (r->res_first_lkid && r->res_first_lkid != lkb->lkb_id) {
  1661. list_add_tail(&lkb->lkb_rsb_lookup, &r->res_lookup);
  1662. return 1;
  1663. }
  1664. if (r->res_nodeid == 0) {
  1665. lkb->lkb_nodeid = 0;
  1666. return 0;
  1667. }
  1668. if (r->res_nodeid > 0) {
  1669. lkb->lkb_nodeid = r->res_nodeid;
  1670. return 0;
  1671. }
  1672. DLM_ASSERT(r->res_nodeid == -1, dlm_dump_rsb(r););
  1673. dir_nodeid = dlm_dir_nodeid(r);
  1674. if (dir_nodeid != our_nodeid) {
  1675. r->res_first_lkid = lkb->lkb_id;
  1676. send_lookup(r, lkb);
  1677. return 1;
  1678. }
  1679. for (i = 0; i < 2; i++) {
  1680. /* It's possible for dlm_scand to remove an old rsb for
  1681. this same resource from the toss list, us to create
  1682. a new one, look up the master locally, and find it
  1683. already exists just before dlm_scand does the
  1684. dir_remove() on the previous rsb. */
  1685. error = dlm_dir_lookup(ls, our_nodeid, r->res_name,
  1686. r->res_length, &ret_nodeid);
  1687. if (!error)
  1688. break;
  1689. log_debug(ls, "dir_lookup error %d %s", error, r->res_name);
  1690. schedule();
  1691. }
  1692. if (error && error != -EEXIST)
  1693. return error;
  1694. if (ret_nodeid == our_nodeid) {
  1695. r->res_first_lkid = 0;
  1696. r->res_nodeid = 0;
  1697. lkb->lkb_nodeid = 0;
  1698. } else {
  1699. r->res_first_lkid = lkb->lkb_id;
  1700. r->res_nodeid = ret_nodeid;
  1701. lkb->lkb_nodeid = ret_nodeid;
  1702. }
  1703. return 0;
  1704. }
  1705. static void process_lookup_list(struct dlm_rsb *r)
  1706. {
  1707. struct dlm_lkb *lkb, *safe;
  1708. list_for_each_entry_safe(lkb, safe, &r->res_lookup, lkb_rsb_lookup) {
  1709. list_del_init(&lkb->lkb_rsb_lookup);
  1710. _request_lock(r, lkb);
  1711. schedule();
  1712. }
  1713. }
  1714. /* confirm_master -- confirm (or deny) an rsb's master nodeid */
  1715. static void confirm_master(struct dlm_rsb *r, int error)
  1716. {
  1717. struct dlm_lkb *lkb;
  1718. if (!r->res_first_lkid)
  1719. return;
  1720. switch (error) {
  1721. case 0:
  1722. case -EINPROGRESS:
  1723. r->res_first_lkid = 0;
  1724. process_lookup_list(r);
  1725. break;
  1726. case -EAGAIN:
  1727. case -EBADR:
  1728. case -ENOTBLK:
  1729. /* the remote request failed and won't be retried (it was
  1730. a NOQUEUE, or has been canceled/unlocked); make a waiting
  1731. lkb the first_lkid */
  1732. r->res_first_lkid = 0;
  1733. if (!list_empty(&r->res_lookup)) {
  1734. lkb = list_entry(r->res_lookup.next, struct dlm_lkb,
  1735. lkb_rsb_lookup);
  1736. list_del_init(&lkb->lkb_rsb_lookup);
  1737. r->res_first_lkid = lkb->lkb_id;
  1738. _request_lock(r, lkb);
  1739. }
  1740. break;
  1741. default:
  1742. log_error(r->res_ls, "confirm_master unknown error %d", error);
  1743. }
  1744. }
  1745. static int set_lock_args(int mode, struct dlm_lksb *lksb, uint32_t flags,
  1746. int namelen, unsigned long timeout_cs,
  1747. void (*ast) (void *astparam),
  1748. void *astparam,
  1749. void (*bast) (void *astparam, int mode),
  1750. struct dlm_args *args)
  1751. {
  1752. int rv = -EINVAL;
  1753. /* check for invalid arg usage */
  1754. if (mode < 0 || mode > DLM_LOCK_EX)
  1755. goto out;
  1756. if (!(flags & DLM_LKF_CONVERT) && (namelen > DLM_RESNAME_MAXLEN))
  1757. goto out;
  1758. if (flags & DLM_LKF_CANCEL)
  1759. goto out;
  1760. if (flags & DLM_LKF_QUECVT && !(flags & DLM_LKF_CONVERT))
  1761. goto out;
  1762. if (flags & DLM_LKF_CONVDEADLK && !(flags & DLM_LKF_CONVERT))
  1763. goto out;
  1764. if (flags & DLM_LKF_CONVDEADLK && flags & DLM_LKF_NOQUEUE)
  1765. goto out;
  1766. if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_CONVERT)
  1767. goto out;
  1768. if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_QUECVT)
  1769. goto out;
  1770. if (flags & DLM_LKF_EXPEDITE && flags & DLM_LKF_NOQUEUE)
  1771. goto out;
  1772. if (flags & DLM_LKF_EXPEDITE && mode != DLM_LOCK_NL)
  1773. goto out;
  1774. if (!ast || !lksb)
  1775. goto out;
  1776. if (flags & DLM_LKF_VALBLK && !lksb->sb_lvbptr)
  1777. goto out;
  1778. if (flags & DLM_LKF_CONVERT && !lksb->sb_lkid)
  1779. goto out;
  1780. /* these args will be copied to the lkb in validate_lock_args,
  1781. it cannot be done now because when converting locks, fields in
  1782. an active lkb cannot be modified before locking the rsb */
  1783. args->flags = flags;
  1784. args->astfn = ast;
  1785. args->astparam = astparam;
  1786. args->bastfn = bast;
  1787. args->timeout = timeout_cs;
  1788. args->mode = mode;
  1789. args->lksb = lksb;
  1790. rv = 0;
  1791. out:
  1792. return rv;
  1793. }
  1794. static int set_unlock_args(uint32_t flags, void *astarg, struct dlm_args *args)
  1795. {
  1796. if (flags & ~(DLM_LKF_CANCEL | DLM_LKF_VALBLK | DLM_LKF_IVVALBLK |
  1797. DLM_LKF_FORCEUNLOCK))
  1798. return -EINVAL;
  1799. if (flags & DLM_LKF_CANCEL && flags & DLM_LKF_FORCEUNLOCK)
  1800. return -EINVAL;
  1801. args->flags = flags;
  1802. args->astparam = astarg;
  1803. return 0;
  1804. }
  1805. static int validate_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
  1806. struct dlm_args *args)
  1807. {
  1808. int rv = -EINVAL;
  1809. if (args->flags & DLM_LKF_CONVERT) {
  1810. if (lkb->lkb_flags & DLM_IFL_MSTCPY)
  1811. goto out;
  1812. if (args->flags & DLM_LKF_QUECVT &&
  1813. !__quecvt_compat_matrix[lkb->lkb_grmode+1][args->mode+1])
  1814. goto out;
  1815. rv = -EBUSY;
  1816. if (lkb->lkb_status != DLM_LKSTS_GRANTED)
  1817. goto out;
  1818. if (lkb->lkb_wait_type)
  1819. goto out;
  1820. if (is_overlap(lkb))
  1821. goto out;
  1822. }
  1823. lkb->lkb_exflags = args->flags;
  1824. lkb->lkb_sbflags = 0;
  1825. lkb->lkb_astfn = args->astfn;
  1826. lkb->lkb_astparam = args->astparam;
  1827. lkb->lkb_bastfn = args->bastfn;
  1828. lkb->lkb_rqmode = args->mode;
  1829. lkb->lkb_lksb = args->lksb;
  1830. lkb->lkb_lvbptr = args->lksb->sb_lvbptr;
  1831. lkb->lkb_ownpid = (int) current->pid;
  1832. lkb->lkb_timeout_cs = args->timeout;
  1833. rv = 0;
  1834. out:
  1835. if (rv)
  1836. log_debug(ls, "validate_lock_args %d %x %x %x %d %d %s",
  1837. rv, lkb->lkb_id, lkb->lkb_flags, args->flags,
  1838. lkb->lkb_status, lkb->lkb_wait_type,
  1839. lkb->lkb_resource->res_name);
  1840. return rv;
  1841. }
  1842. /* when dlm_unlock() sees -EBUSY with CANCEL/FORCEUNLOCK it returns 0
  1843. for success */
  1844. /* note: it's valid for lkb_nodeid/res_nodeid to be -1 when we get here
  1845. because there may be a lookup in progress and it's valid to do
  1846. cancel/unlockf on it */
  1847. static int validate_unlock_args(struct dlm_lkb *lkb, struct dlm_args *args)
  1848. {
  1849. struct dlm_ls *ls = lkb->lkb_resource->res_ls;
  1850. int rv = -EINVAL;
  1851. if (lkb->lkb_flags & DLM_IFL_MSTCPY) {
  1852. log_error(ls, "unlock on MSTCPY %x", lkb->lkb_id);
  1853. dlm_print_lkb(lkb);
  1854. goto out;
  1855. }
  1856. /* an lkb may still exist even though the lock is EOL'ed due to a
  1857. cancel, unlock or failed noqueue request; an app can't use these
  1858. locks; return same error as if the lkid had not been found at all */
  1859. if (lkb->lkb_flags & DLM_IFL_ENDOFLIFE) {
  1860. log_debug(ls, "unlock on ENDOFLIFE %x", lkb->lkb_id);
  1861. rv = -ENOENT;
  1862. goto out;
  1863. }
  1864. /* an lkb may be waiting for an rsb lookup to complete where the
  1865. lookup was initiated by another lock */
  1866. if (!list_empty(&lkb->lkb_rsb_lookup)) {
  1867. if (args->flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)) {
  1868. log_debug(ls, "unlock on rsb_lookup %x", lkb->lkb_id);
  1869. list_del_init(&lkb->lkb_rsb_lookup);
  1870. queue_cast(lkb->lkb_resource, lkb,
  1871. args->flags & DLM_LKF_CANCEL ?
  1872. -DLM_ECANCEL : -DLM_EUNLOCK);
  1873. unhold_lkb(lkb); /* undoes create_lkb() */
  1874. }
  1875. /* caller changes -EBUSY to 0 for CANCEL and FORCEUNLOCK */
  1876. rv = -EBUSY;
  1877. goto out;
  1878. }
  1879. /* cancel not allowed with another cancel/unlock in progress */
  1880. if (args->flags & DLM_LKF_CANCEL) {
  1881. if (lkb->lkb_exflags & DLM_LKF_CANCEL)
  1882. goto out;
  1883. if (is_overlap(lkb))
  1884. goto out;
  1885. /* don't let scand try to do a cancel */
  1886. del_timeout(lkb);
  1887. if (lkb->lkb_flags & DLM_IFL_RESEND) {
  1888. lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
  1889. rv = -EBUSY;
  1890. goto out;
  1891. }
  1892. /* there's nothing to cancel */
  1893. if (lkb->lkb_status == DLM_LKSTS_GRANTED &&
  1894. !lkb->lkb_wait_type) {
  1895. rv = -EBUSY;
  1896. goto out;
  1897. }
  1898. switch (lkb->lkb_wait_type) {
  1899. case DLM_MSG_LOOKUP:
  1900. case DLM_MSG_REQUEST:
  1901. lkb->lkb_flags |= DLM_IFL_OVERLAP_CANCEL;
  1902. rv = -EBUSY;
  1903. goto out;
  1904. case DLM_MSG_UNLOCK:
  1905. case DLM_MSG_CANCEL:
  1906. goto out;
  1907. }
  1908. /* add_to_waiters() will set OVERLAP_CANCEL */
  1909. goto out_ok;
  1910. }
  1911. /* do we need to allow a force-unlock if there's a normal unlock
  1912. already in progress? in what conditions could the normal unlock
  1913. fail such that we'd want to send a force-unlock to be sure? */
  1914. if (args->flags & DLM_LKF_FORCEUNLOCK) {
  1915. if (lkb->lkb_exflags & DLM_LKF_FORCEUNLOCK)
  1916. goto out;
  1917. if (is_overlap_unlock(lkb))
  1918. goto out;
  1919. /* don't let scand try to do a cancel */
  1920. del_timeout(lkb);
  1921. if (lkb->lkb_flags & DLM_IFL_RESEND) {
  1922. lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
  1923. rv = -EBUSY;
  1924. goto out;
  1925. }
  1926. switch (lkb->lkb_wait_type) {
  1927. case DLM_MSG_LOOKUP:
  1928. case DLM_MSG_REQUEST:
  1929. lkb->lkb_flags |= DLM_IFL_OVERLAP_UNLOCK;
  1930. rv = -EBUSY;
  1931. goto out;
  1932. case DLM_MSG_UNLOCK:
  1933. goto out;
  1934. }
  1935. /* add_to_waiters() will set OVERLAP_UNLOCK */
  1936. goto out_ok;
  1937. }
  1938. /* normal unlock not allowed if there's any op in progress */
  1939. rv = -EBUSY;
  1940. if (lkb->lkb_wait_type || lkb->lkb_wait_count)
  1941. goto out;
  1942. out_ok:
  1943. /* an overlapping op shouldn't blow away exflags from other op */
  1944. lkb->lkb_exflags |= args->flags;
  1945. lkb->lkb_sbflags = 0;
  1946. lkb->lkb_astparam = args->astparam;
  1947. rv = 0;
  1948. out:
  1949. if (rv)
  1950. log_debug(ls, "validate_unlock_args %d %x %x %x %x %d %s", rv,
  1951. lkb->lkb_id, lkb->lkb_flags, lkb->lkb_exflags,
  1952. args->flags, lkb->lkb_wait_type,
  1953. lkb->lkb_resource->res_name);
  1954. return rv;
  1955. }
  1956. /*
  1957. * Four stage 4 varieties:
  1958. * do_request(), do_convert(), do_unlock(), do_cancel()
  1959. * These are called on the master node for the given lock and
  1960. * from the central locking logic.
  1961. */
  1962. static int do_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1963. {
  1964. int error = 0;
  1965. if (can_be_granted(r, lkb, 1, NULL)) {
  1966. grant_lock(r, lkb);
  1967. queue_cast(r, lkb, 0);
  1968. goto out;
  1969. }
  1970. if (can_be_queued(lkb)) {
  1971. error = -EINPROGRESS;
  1972. add_lkb(r, lkb, DLM_LKSTS_WAITING);
  1973. add_timeout(lkb);
  1974. goto out;
  1975. }
  1976. error = -EAGAIN;
  1977. queue_cast(r, lkb, -EAGAIN);
  1978. out:
  1979. return error;
  1980. }
  1981. static void do_request_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
  1982. int error)
  1983. {
  1984. switch (error) {
  1985. case -EAGAIN:
  1986. if (force_blocking_asts(lkb))
  1987. send_blocking_asts_all(r, lkb);
  1988. break;
  1989. case -EINPROGRESS:
  1990. send_blocking_asts(r, lkb);
  1991. break;
  1992. }
  1993. }
  1994. static int do_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
  1995. {
  1996. int error = 0;
  1997. int deadlk = 0;
  1998. /* changing an existing lock may allow others to be granted */
  1999. if (can_be_granted(r, lkb, 1, &deadlk)) {
  2000. grant_lock(r, lkb);
  2001. queue_cast(r, lkb, 0);
  2002. goto out;
  2003. }
  2004. /* can_be_granted() detected that this lock would block in a conversion
  2005. deadlock, so we leave it on the granted queue and return EDEADLK in
  2006. the ast for the convert. */
  2007. if (deadlk) {
  2008. /* it's left on the granted queue */
  2009. revert_lock(r, lkb);
  2010. queue_cast(r, lkb, -EDEADLK);
  2011. error = -EDEADLK;
  2012. goto out;
  2013. }
  2014. /* is_demoted() means the can_be_granted() above set the grmode
  2015. to NL, and left us on the granted queue. This auto-demotion
  2016. (due to CONVDEADLK) might mean other locks, and/or this lock, are
  2017. now grantable. We have to try to grant other converting locks
  2018. before we try again to grant this one. */
  2019. if (is_demoted(lkb)) {
  2020. grant_pending_convert(r, DLM_LOCK_IV, NULL);
  2021. if (_can_be_granted(r, lkb, 1)) {
  2022. grant_lock(r, lkb);
  2023. queue_cast(r, lkb, 0);
  2024. goto out;
  2025. }
  2026. /* else fall through and move to convert queue */
  2027. }
  2028. if (can_be_queued(lkb)) {
  2029. error = -EINPROGRESS;
  2030. del_lkb(r, lkb);
  2031. add_lkb(r, lkb, DLM_LKSTS_CONVERT);
  2032. add_timeout(lkb);
  2033. goto out;
  2034. }
  2035. error = -EAGAIN;
  2036. queue_cast(r, lkb, -EAGAIN);
  2037. out:
  2038. return error;
  2039. }
  2040. static void do_convert_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
  2041. int error)
  2042. {
  2043. switch (error) {
  2044. case 0:
  2045. grant_pending_locks(r);
  2046. /* grant_pending_locks also sends basts */
  2047. break;
  2048. case -EAGAIN:
  2049. if (force_blocking_asts(lkb))
  2050. send_blocking_asts_all(r, lkb);
  2051. break;
  2052. case -EINPROGRESS:
  2053. send_blocking_asts(r, lkb);
  2054. break;
  2055. }
  2056. }
  2057. static int do_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2058. {
  2059. remove_lock(r, lkb);
  2060. queue_cast(r, lkb, -DLM_EUNLOCK);
  2061. return -DLM_EUNLOCK;
  2062. }
  2063. static void do_unlock_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
  2064. int error)
  2065. {
  2066. grant_pending_locks(r);
  2067. }
  2068. /* returns: 0 did nothing, -DLM_ECANCEL canceled lock */
  2069. static int do_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2070. {
  2071. int error;
  2072. error = revert_lock(r, lkb);
  2073. if (error) {
  2074. queue_cast(r, lkb, -DLM_ECANCEL);
  2075. return -DLM_ECANCEL;
  2076. }
  2077. return 0;
  2078. }
  2079. static void do_cancel_effects(struct dlm_rsb *r, struct dlm_lkb *lkb,
  2080. int error)
  2081. {
  2082. if (error)
  2083. grant_pending_locks(r);
  2084. }
  2085. /*
  2086. * Four stage 3 varieties:
  2087. * _request_lock(), _convert_lock(), _unlock_lock(), _cancel_lock()
  2088. */
  2089. /* add a new lkb to a possibly new rsb, called by requesting process */
  2090. static int _request_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2091. {
  2092. int error;
  2093. /* set_master: sets lkb nodeid from r */
  2094. error = set_master(r, lkb);
  2095. if (error < 0)
  2096. goto out;
  2097. if (error) {
  2098. error = 0;
  2099. goto out;
  2100. }
  2101. if (is_remote(r)) {
  2102. /* receive_request() calls do_request() on remote node */
  2103. error = send_request(r, lkb);
  2104. } else {
  2105. error = do_request(r, lkb);
  2106. /* for remote locks the request_reply is sent
  2107. between do_request and do_request_effects */
  2108. do_request_effects(r, lkb, error);
  2109. }
  2110. out:
  2111. return error;
  2112. }
  2113. /* change some property of an existing lkb, e.g. mode */
  2114. static int _convert_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2115. {
  2116. int error;
  2117. if (is_remote(r)) {
  2118. /* receive_convert() calls do_convert() on remote node */
  2119. error = send_convert(r, lkb);
  2120. } else {
  2121. error = do_convert(r, lkb);
  2122. /* for remote locks the convert_reply is sent
  2123. between do_convert and do_convert_effects */
  2124. do_convert_effects(r, lkb, error);
  2125. }
  2126. return error;
  2127. }
  2128. /* remove an existing lkb from the granted queue */
  2129. static int _unlock_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2130. {
  2131. int error;
  2132. if (is_remote(r)) {
  2133. /* receive_unlock() calls do_unlock() on remote node */
  2134. error = send_unlock(r, lkb);
  2135. } else {
  2136. error = do_unlock(r, lkb);
  2137. /* for remote locks the unlock_reply is sent
  2138. between do_unlock and do_unlock_effects */
  2139. do_unlock_effects(r, lkb, error);
  2140. }
  2141. return error;
  2142. }
  2143. /* remove an existing lkb from the convert or wait queue */
  2144. static int _cancel_lock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2145. {
  2146. int error;
  2147. if (is_remote(r)) {
  2148. /* receive_cancel() calls do_cancel() on remote node */
  2149. error = send_cancel(r, lkb);
  2150. } else {
  2151. error = do_cancel(r, lkb);
  2152. /* for remote locks the cancel_reply is sent
  2153. between do_cancel and do_cancel_effects */
  2154. do_cancel_effects(r, lkb, error);
  2155. }
  2156. return error;
  2157. }
  2158. /*
  2159. * Four stage 2 varieties:
  2160. * request_lock(), convert_lock(), unlock_lock(), cancel_lock()
  2161. */
  2162. static int request_lock(struct dlm_ls *ls, struct dlm_lkb *lkb, char *name,
  2163. int len, struct dlm_args *args)
  2164. {
  2165. struct dlm_rsb *r;
  2166. int error;
  2167. error = validate_lock_args(ls, lkb, args);
  2168. if (error)
  2169. goto out;
  2170. error = find_rsb(ls, name, len, R_CREATE, &r);
  2171. if (error)
  2172. goto out;
  2173. lock_rsb(r);
  2174. attach_lkb(r, lkb);
  2175. lkb->lkb_lksb->sb_lkid = lkb->lkb_id;
  2176. error = _request_lock(r, lkb);
  2177. unlock_rsb(r);
  2178. put_rsb(r);
  2179. out:
  2180. return error;
  2181. }
  2182. static int convert_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
  2183. struct dlm_args *args)
  2184. {
  2185. struct dlm_rsb *r;
  2186. int error;
  2187. r = lkb->lkb_resource;
  2188. hold_rsb(r);
  2189. lock_rsb(r);
  2190. error = validate_lock_args(ls, lkb, args);
  2191. if (error)
  2192. goto out;
  2193. error = _convert_lock(r, lkb);
  2194. out:
  2195. unlock_rsb(r);
  2196. put_rsb(r);
  2197. return error;
  2198. }
  2199. static int unlock_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
  2200. struct dlm_args *args)
  2201. {
  2202. struct dlm_rsb *r;
  2203. int error;
  2204. r = lkb->lkb_resource;
  2205. hold_rsb(r);
  2206. lock_rsb(r);
  2207. error = validate_unlock_args(lkb, args);
  2208. if (error)
  2209. goto out;
  2210. error = _unlock_lock(r, lkb);
  2211. out:
  2212. unlock_rsb(r);
  2213. put_rsb(r);
  2214. return error;
  2215. }
  2216. static int cancel_lock(struct dlm_ls *ls, struct dlm_lkb *lkb,
  2217. struct dlm_args *args)
  2218. {
  2219. struct dlm_rsb *r;
  2220. int error;
  2221. r = lkb->lkb_resource;
  2222. hold_rsb(r);
  2223. lock_rsb(r);
  2224. error = validate_unlock_args(lkb, args);
  2225. if (error)
  2226. goto out;
  2227. error = _cancel_lock(r, lkb);
  2228. out:
  2229. unlock_rsb(r);
  2230. put_rsb(r);
  2231. return error;
  2232. }
  2233. /*
  2234. * Two stage 1 varieties: dlm_lock() and dlm_unlock()
  2235. */
  2236. int dlm_lock(dlm_lockspace_t *lockspace,
  2237. int mode,
  2238. struct dlm_lksb *lksb,
  2239. uint32_t flags,
  2240. void *name,
  2241. unsigned int namelen,
  2242. uint32_t parent_lkid,
  2243. void (*ast) (void *astarg),
  2244. void *astarg,
  2245. void (*bast) (void *astarg, int mode))
  2246. {
  2247. struct dlm_ls *ls;
  2248. struct dlm_lkb *lkb;
  2249. struct dlm_args args;
  2250. int error, convert = flags & DLM_LKF_CONVERT;
  2251. ls = dlm_find_lockspace_local(lockspace);
  2252. if (!ls)
  2253. return -EINVAL;
  2254. dlm_lock_recovery(ls);
  2255. if (convert)
  2256. error = find_lkb(ls, lksb->sb_lkid, &lkb);
  2257. else
  2258. error = create_lkb(ls, &lkb);
  2259. if (error)
  2260. goto out;
  2261. error = set_lock_args(mode, lksb, flags, namelen, 0, ast,
  2262. astarg, bast, &args);
  2263. if (error)
  2264. goto out_put;
  2265. if (convert)
  2266. error = convert_lock(ls, lkb, &args);
  2267. else
  2268. error = request_lock(ls, lkb, name, namelen, &args);
  2269. if (error == -EINPROGRESS)
  2270. error = 0;
  2271. out_put:
  2272. if (convert || error)
  2273. __put_lkb(ls, lkb);
  2274. if (error == -EAGAIN || error == -EDEADLK)
  2275. error = 0;
  2276. out:
  2277. dlm_unlock_recovery(ls);
  2278. dlm_put_lockspace(ls);
  2279. return error;
  2280. }
  2281. int dlm_unlock(dlm_lockspace_t *lockspace,
  2282. uint32_t lkid,
  2283. uint32_t flags,
  2284. struct dlm_lksb *lksb,
  2285. void *astarg)
  2286. {
  2287. struct dlm_ls *ls;
  2288. struct dlm_lkb *lkb;
  2289. struct dlm_args args;
  2290. int error;
  2291. ls = dlm_find_lockspace_local(lockspace);
  2292. if (!ls)
  2293. return -EINVAL;
  2294. dlm_lock_recovery(ls);
  2295. error = find_lkb(ls, lkid, &lkb);
  2296. if (error)
  2297. goto out;
  2298. error = set_unlock_args(flags, astarg, &args);
  2299. if (error)
  2300. goto out_put;
  2301. if (flags & DLM_LKF_CANCEL)
  2302. error = cancel_lock(ls, lkb, &args);
  2303. else
  2304. error = unlock_lock(ls, lkb, &args);
  2305. if (error == -DLM_EUNLOCK || error == -DLM_ECANCEL)
  2306. error = 0;
  2307. if (error == -EBUSY && (flags & (DLM_LKF_CANCEL | DLM_LKF_FORCEUNLOCK)))
  2308. error = 0;
  2309. out_put:
  2310. dlm_put_lkb(lkb);
  2311. out:
  2312. dlm_unlock_recovery(ls);
  2313. dlm_put_lockspace(ls);
  2314. return error;
  2315. }
  2316. /*
  2317. * send/receive routines for remote operations and replies
  2318. *
  2319. * send_args
  2320. * send_common
  2321. * send_request receive_request
  2322. * send_convert receive_convert
  2323. * send_unlock receive_unlock
  2324. * send_cancel receive_cancel
  2325. * send_grant receive_grant
  2326. * send_bast receive_bast
  2327. * send_lookup receive_lookup
  2328. * send_remove receive_remove
  2329. *
  2330. * send_common_reply
  2331. * receive_request_reply send_request_reply
  2332. * receive_convert_reply send_convert_reply
  2333. * receive_unlock_reply send_unlock_reply
  2334. * receive_cancel_reply send_cancel_reply
  2335. * receive_lookup_reply send_lookup_reply
  2336. */
  2337. static int _create_message(struct dlm_ls *ls, int mb_len,
  2338. int to_nodeid, int mstype,
  2339. struct dlm_message **ms_ret,
  2340. struct dlm_mhandle **mh_ret)
  2341. {
  2342. struct dlm_message *ms;
  2343. struct dlm_mhandle *mh;
  2344. char *mb;
  2345. /* get_buffer gives us a message handle (mh) that we need to
  2346. pass into lowcomms_commit and a message buffer (mb) that we
  2347. write our data into */
  2348. mh = dlm_lowcomms_get_buffer(to_nodeid, mb_len, GFP_NOFS, &mb);
  2349. if (!mh)
  2350. return -ENOBUFS;
  2351. memset(mb, 0, mb_len);
  2352. ms = (struct dlm_message *) mb;
  2353. ms->m_header.h_version = (DLM_HEADER_MAJOR | DLM_HEADER_MINOR);
  2354. ms->m_header.h_lockspace = ls->ls_global_id;
  2355. ms->m_header.h_nodeid = dlm_our_nodeid();
  2356. ms->m_header.h_length = mb_len;
  2357. ms->m_header.h_cmd = DLM_MSG;
  2358. ms->m_type = mstype;
  2359. *mh_ret = mh;
  2360. *ms_ret = ms;
  2361. return 0;
  2362. }
  2363. static int create_message(struct dlm_rsb *r, struct dlm_lkb *lkb,
  2364. int to_nodeid, int mstype,
  2365. struct dlm_message **ms_ret,
  2366. struct dlm_mhandle **mh_ret)
  2367. {
  2368. int mb_len = sizeof(struct dlm_message);
  2369. switch (mstype) {
  2370. case DLM_MSG_REQUEST:
  2371. case DLM_MSG_LOOKUP:
  2372. case DLM_MSG_REMOVE:
  2373. mb_len += r->res_length;
  2374. break;
  2375. case DLM_MSG_CONVERT:
  2376. case DLM_MSG_UNLOCK:
  2377. case DLM_MSG_REQUEST_REPLY:
  2378. case DLM_MSG_CONVERT_REPLY:
  2379. case DLM_MSG_GRANT:
  2380. if (lkb && lkb->lkb_lvbptr)
  2381. mb_len += r->res_ls->ls_lvblen;
  2382. break;
  2383. }
  2384. return _create_message(r->res_ls, mb_len, to_nodeid, mstype,
  2385. ms_ret, mh_ret);
  2386. }
  2387. /* further lowcomms enhancements or alternate implementations may make
  2388. the return value from this function useful at some point */
  2389. static int send_message(struct dlm_mhandle *mh, struct dlm_message *ms)
  2390. {
  2391. dlm_message_out(ms);
  2392. dlm_lowcomms_commit_buffer(mh);
  2393. return 0;
  2394. }
  2395. static void send_args(struct dlm_rsb *r, struct dlm_lkb *lkb,
  2396. struct dlm_message *ms)
  2397. {
  2398. ms->m_nodeid = lkb->lkb_nodeid;
  2399. ms->m_pid = lkb->lkb_ownpid;
  2400. ms->m_lkid = lkb->lkb_id;
  2401. ms->m_remid = lkb->lkb_remid;
  2402. ms->m_exflags = lkb->lkb_exflags;
  2403. ms->m_sbflags = lkb->lkb_sbflags;
  2404. ms->m_flags = lkb->lkb_flags;
  2405. ms->m_lvbseq = lkb->lkb_lvbseq;
  2406. ms->m_status = lkb->lkb_status;
  2407. ms->m_grmode = lkb->lkb_grmode;
  2408. ms->m_rqmode = lkb->lkb_rqmode;
  2409. ms->m_hash = r->res_hash;
  2410. /* m_result and m_bastmode are set from function args,
  2411. not from lkb fields */
  2412. if (lkb->lkb_bastfn)
  2413. ms->m_asts |= DLM_CB_BAST;
  2414. if (lkb->lkb_astfn)
  2415. ms->m_asts |= DLM_CB_CAST;
  2416. /* compare with switch in create_message; send_remove() doesn't
  2417. use send_args() */
  2418. switch (ms->m_type) {
  2419. case DLM_MSG_REQUEST:
  2420. case DLM_MSG_LOOKUP:
  2421. memcpy(ms->m_extra, r->res_name, r->res_length);
  2422. break;
  2423. case DLM_MSG_CONVERT:
  2424. case DLM_MSG_UNLOCK:
  2425. case DLM_MSG_REQUEST_REPLY:
  2426. case DLM_MSG_CONVERT_REPLY:
  2427. case DLM_MSG_GRANT:
  2428. if (!lkb->lkb_lvbptr)
  2429. break;
  2430. memcpy(ms->m_extra, lkb->lkb_lvbptr, r->res_ls->ls_lvblen);
  2431. break;
  2432. }
  2433. }
  2434. static int send_common(struct dlm_rsb *r, struct dlm_lkb *lkb, int mstype)
  2435. {
  2436. struct dlm_message *ms;
  2437. struct dlm_mhandle *mh;
  2438. int to_nodeid, error;
  2439. to_nodeid = r->res_nodeid;
  2440. error = add_to_waiters(lkb, mstype, to_nodeid);
  2441. if (error)
  2442. return error;
  2443. error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
  2444. if (error)
  2445. goto fail;
  2446. send_args(r, lkb, ms);
  2447. error = send_message(mh, ms);
  2448. if (error)
  2449. goto fail;
  2450. return 0;
  2451. fail:
  2452. remove_from_waiters(lkb, msg_reply_type(mstype));
  2453. return error;
  2454. }
  2455. static int send_request(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2456. {
  2457. return send_common(r, lkb, DLM_MSG_REQUEST);
  2458. }
  2459. static int send_convert(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2460. {
  2461. int error;
  2462. error = send_common(r, lkb, DLM_MSG_CONVERT);
  2463. /* down conversions go without a reply from the master */
  2464. if (!error && down_conversion(lkb)) {
  2465. remove_from_waiters(lkb, DLM_MSG_CONVERT_REPLY);
  2466. r->res_ls->ls_stub_ms.m_flags = DLM_IFL_STUB_MS;
  2467. r->res_ls->ls_stub_ms.m_type = DLM_MSG_CONVERT_REPLY;
  2468. r->res_ls->ls_stub_ms.m_result = 0;
  2469. __receive_convert_reply(r, lkb, &r->res_ls->ls_stub_ms);
  2470. }
  2471. return error;
  2472. }
  2473. /* FIXME: if this lkb is the only lock we hold on the rsb, then set
  2474. MASTER_UNCERTAIN to force the next request on the rsb to confirm
  2475. that the master is still correct. */
  2476. static int send_unlock(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2477. {
  2478. return send_common(r, lkb, DLM_MSG_UNLOCK);
  2479. }
  2480. static int send_cancel(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2481. {
  2482. return send_common(r, lkb, DLM_MSG_CANCEL);
  2483. }
  2484. static int send_grant(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2485. {
  2486. struct dlm_message *ms;
  2487. struct dlm_mhandle *mh;
  2488. int to_nodeid, error;
  2489. to_nodeid = lkb->lkb_nodeid;
  2490. error = create_message(r, lkb, to_nodeid, DLM_MSG_GRANT, &ms, &mh);
  2491. if (error)
  2492. goto out;
  2493. send_args(r, lkb, ms);
  2494. ms->m_result = 0;
  2495. error = send_message(mh, ms);
  2496. out:
  2497. return error;
  2498. }
  2499. static int send_bast(struct dlm_rsb *r, struct dlm_lkb *lkb, int mode)
  2500. {
  2501. struct dlm_message *ms;
  2502. struct dlm_mhandle *mh;
  2503. int to_nodeid, error;
  2504. to_nodeid = lkb->lkb_nodeid;
  2505. error = create_message(r, NULL, to_nodeid, DLM_MSG_BAST, &ms, &mh);
  2506. if (error)
  2507. goto out;
  2508. send_args(r, lkb, ms);
  2509. ms->m_bastmode = mode;
  2510. error = send_message(mh, ms);
  2511. out:
  2512. return error;
  2513. }
  2514. static int send_lookup(struct dlm_rsb *r, struct dlm_lkb *lkb)
  2515. {
  2516. struct dlm_message *ms;
  2517. struct dlm_mhandle *mh;
  2518. int to_nodeid, error;
  2519. to_nodeid = dlm_dir_nodeid(r);
  2520. error = add_to_waiters(lkb, DLM_MSG_LOOKUP, to_nodeid);
  2521. if (error)
  2522. return error;
  2523. error = create_message(r, NULL, to_nodeid, DLM_MSG_LOOKUP, &ms, &mh);
  2524. if (error)
  2525. goto fail;
  2526. send_args(r, lkb, ms);
  2527. error = send_message(mh, ms);
  2528. if (error)
  2529. goto fail;
  2530. return 0;
  2531. fail:
  2532. remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
  2533. return error;
  2534. }
  2535. static int send_remove(struct dlm_rsb *r)
  2536. {
  2537. struct dlm_message *ms;
  2538. struct dlm_mhandle *mh;
  2539. int to_nodeid, error;
  2540. to_nodeid = dlm_dir_nodeid(r);
  2541. error = create_message(r, NULL, to_nodeid, DLM_MSG_REMOVE, &ms, &mh);
  2542. if (error)
  2543. goto out;
  2544. memcpy(ms->m_extra, r->res_name, r->res_length);
  2545. ms->m_hash = r->res_hash;
  2546. error = send_message(mh, ms);
  2547. out:
  2548. return error;
  2549. }
  2550. static int send_common_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
  2551. int mstype, int rv)
  2552. {
  2553. struct dlm_message *ms;
  2554. struct dlm_mhandle *mh;
  2555. int to_nodeid, error;
  2556. to_nodeid = lkb->lkb_nodeid;
  2557. error = create_message(r, lkb, to_nodeid, mstype, &ms, &mh);
  2558. if (error)
  2559. goto out;
  2560. send_args(r, lkb, ms);
  2561. ms->m_result = rv;
  2562. error = send_message(mh, ms);
  2563. out:
  2564. return error;
  2565. }
  2566. static int send_request_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
  2567. {
  2568. return send_common_reply(r, lkb, DLM_MSG_REQUEST_REPLY, rv);
  2569. }
  2570. static int send_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
  2571. {
  2572. return send_common_reply(r, lkb, DLM_MSG_CONVERT_REPLY, rv);
  2573. }
  2574. static int send_unlock_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
  2575. {
  2576. return send_common_reply(r, lkb, DLM_MSG_UNLOCK_REPLY, rv);
  2577. }
  2578. static int send_cancel_reply(struct dlm_rsb *r, struct dlm_lkb *lkb, int rv)
  2579. {
  2580. return send_common_reply(r, lkb, DLM_MSG_CANCEL_REPLY, rv);
  2581. }
  2582. static int send_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms_in,
  2583. int ret_nodeid, int rv)
  2584. {
  2585. struct dlm_rsb *r = &ls->ls_stub_rsb;
  2586. struct dlm_message *ms;
  2587. struct dlm_mhandle *mh;
  2588. int error, nodeid = ms_in->m_header.h_nodeid;
  2589. error = create_message(r, NULL, nodeid, DLM_MSG_LOOKUP_REPLY, &ms, &mh);
  2590. if (error)
  2591. goto out;
  2592. ms->m_lkid = ms_in->m_lkid;
  2593. ms->m_result = rv;
  2594. ms->m_nodeid = ret_nodeid;
  2595. error = send_message(mh, ms);
  2596. out:
  2597. return error;
  2598. }
  2599. /* which args we save from a received message depends heavily on the type
  2600. of message, unlike the send side where we can safely send everything about
  2601. the lkb for any type of message */
  2602. static void receive_flags(struct dlm_lkb *lkb, struct dlm_message *ms)
  2603. {
  2604. lkb->lkb_exflags = ms->m_exflags;
  2605. lkb->lkb_sbflags = ms->m_sbflags;
  2606. lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
  2607. (ms->m_flags & 0x0000FFFF);
  2608. }
  2609. static void receive_flags_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
  2610. {
  2611. if (ms->m_flags == DLM_IFL_STUB_MS)
  2612. return;
  2613. lkb->lkb_sbflags = ms->m_sbflags;
  2614. lkb->lkb_flags = (lkb->lkb_flags & 0xFFFF0000) |
  2615. (ms->m_flags & 0x0000FFFF);
  2616. }
  2617. static int receive_extralen(struct dlm_message *ms)
  2618. {
  2619. return (ms->m_header.h_length - sizeof(struct dlm_message));
  2620. }
  2621. static int receive_lvb(struct dlm_ls *ls, struct dlm_lkb *lkb,
  2622. struct dlm_message *ms)
  2623. {
  2624. int len;
  2625. if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
  2626. if (!lkb->lkb_lvbptr)
  2627. lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
  2628. if (!lkb->lkb_lvbptr)
  2629. return -ENOMEM;
  2630. len = receive_extralen(ms);
  2631. if (len > DLM_RESNAME_MAXLEN)
  2632. len = DLM_RESNAME_MAXLEN;
  2633. memcpy(lkb->lkb_lvbptr, ms->m_extra, len);
  2634. }
  2635. return 0;
  2636. }
  2637. static void fake_bastfn(void *astparam, int mode)
  2638. {
  2639. log_print("fake_bastfn should not be called");
  2640. }
  2641. static void fake_astfn(void *astparam)
  2642. {
  2643. log_print("fake_astfn should not be called");
  2644. }
  2645. static int receive_request_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
  2646. struct dlm_message *ms)
  2647. {
  2648. lkb->lkb_nodeid = ms->m_header.h_nodeid;
  2649. lkb->lkb_ownpid = ms->m_pid;
  2650. lkb->lkb_remid = ms->m_lkid;
  2651. lkb->lkb_grmode = DLM_LOCK_IV;
  2652. lkb->lkb_rqmode = ms->m_rqmode;
  2653. lkb->lkb_bastfn = (ms->m_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
  2654. lkb->lkb_astfn = (ms->m_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
  2655. if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
  2656. /* lkb was just created so there won't be an lvb yet */
  2657. lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
  2658. if (!lkb->lkb_lvbptr)
  2659. return -ENOMEM;
  2660. }
  2661. return 0;
  2662. }
  2663. static int receive_convert_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
  2664. struct dlm_message *ms)
  2665. {
  2666. if (lkb->lkb_status != DLM_LKSTS_GRANTED)
  2667. return -EBUSY;
  2668. if (receive_lvb(ls, lkb, ms))
  2669. return -ENOMEM;
  2670. lkb->lkb_rqmode = ms->m_rqmode;
  2671. lkb->lkb_lvbseq = ms->m_lvbseq;
  2672. return 0;
  2673. }
  2674. static int receive_unlock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
  2675. struct dlm_message *ms)
  2676. {
  2677. if (receive_lvb(ls, lkb, ms))
  2678. return -ENOMEM;
  2679. return 0;
  2680. }
  2681. /* We fill in the stub-lkb fields with the info that send_xxxx_reply()
  2682. uses to send a reply and that the remote end uses to process the reply. */
  2683. static void setup_stub_lkb(struct dlm_ls *ls, struct dlm_message *ms)
  2684. {
  2685. struct dlm_lkb *lkb = &ls->ls_stub_lkb;
  2686. lkb->lkb_nodeid = ms->m_header.h_nodeid;
  2687. lkb->lkb_remid = ms->m_lkid;
  2688. }
  2689. /* This is called after the rsb is locked so that we can safely inspect
  2690. fields in the lkb. */
  2691. static int validate_message(struct dlm_lkb *lkb, struct dlm_message *ms)
  2692. {
  2693. int from = ms->m_header.h_nodeid;
  2694. int error = 0;
  2695. switch (ms->m_type) {
  2696. case DLM_MSG_CONVERT:
  2697. case DLM_MSG_UNLOCK:
  2698. case DLM_MSG_CANCEL:
  2699. if (!is_master_copy(lkb) || lkb->lkb_nodeid != from)
  2700. error = -EINVAL;
  2701. break;
  2702. case DLM_MSG_CONVERT_REPLY:
  2703. case DLM_MSG_UNLOCK_REPLY:
  2704. case DLM_MSG_CANCEL_REPLY:
  2705. case DLM_MSG_GRANT:
  2706. case DLM_MSG_BAST:
  2707. if (!is_process_copy(lkb) || lkb->lkb_nodeid != from)
  2708. error = -EINVAL;
  2709. break;
  2710. case DLM_MSG_REQUEST_REPLY:
  2711. if (!is_process_copy(lkb))
  2712. error = -EINVAL;
  2713. else if (lkb->lkb_nodeid != -1 && lkb->lkb_nodeid != from)
  2714. error = -EINVAL;
  2715. break;
  2716. default:
  2717. error = -EINVAL;
  2718. }
  2719. if (error)
  2720. log_error(lkb->lkb_resource->res_ls,
  2721. "ignore invalid message %d from %d %x %x %x %d",
  2722. ms->m_type, from, lkb->lkb_id, lkb->lkb_remid,
  2723. lkb->lkb_flags, lkb->lkb_nodeid);
  2724. return error;
  2725. }
  2726. static void receive_request(struct dlm_ls *ls, struct dlm_message *ms)
  2727. {
  2728. struct dlm_lkb *lkb;
  2729. struct dlm_rsb *r;
  2730. int error, namelen;
  2731. error = create_lkb(ls, &lkb);
  2732. if (error)
  2733. goto fail;
  2734. receive_flags(lkb, ms);
  2735. lkb->lkb_flags |= DLM_IFL_MSTCPY;
  2736. error = receive_request_args(ls, lkb, ms);
  2737. if (error) {
  2738. __put_lkb(ls, lkb);
  2739. goto fail;
  2740. }
  2741. namelen = receive_extralen(ms);
  2742. error = find_rsb(ls, ms->m_extra, namelen, R_MASTER, &r);
  2743. if (error) {
  2744. __put_lkb(ls, lkb);
  2745. goto fail;
  2746. }
  2747. lock_rsb(r);
  2748. attach_lkb(r, lkb);
  2749. error = do_request(r, lkb);
  2750. send_request_reply(r, lkb, error);
  2751. do_request_effects(r, lkb, error);
  2752. unlock_rsb(r);
  2753. put_rsb(r);
  2754. if (error == -EINPROGRESS)
  2755. error = 0;
  2756. if (error)
  2757. dlm_put_lkb(lkb);
  2758. return;
  2759. fail:
  2760. setup_stub_lkb(ls, ms);
  2761. send_request_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
  2762. }
  2763. static void receive_convert(struct dlm_ls *ls, struct dlm_message *ms)
  2764. {
  2765. struct dlm_lkb *lkb;
  2766. struct dlm_rsb *r;
  2767. int error, reply = 1;
  2768. error = find_lkb(ls, ms->m_remid, &lkb);
  2769. if (error)
  2770. goto fail;
  2771. r = lkb->lkb_resource;
  2772. hold_rsb(r);
  2773. lock_rsb(r);
  2774. error = validate_message(lkb, ms);
  2775. if (error)
  2776. goto out;
  2777. receive_flags(lkb, ms);
  2778. error = receive_convert_args(ls, lkb, ms);
  2779. if (error) {
  2780. send_convert_reply(r, lkb, error);
  2781. goto out;
  2782. }
  2783. reply = !down_conversion(lkb);
  2784. error = do_convert(r, lkb);
  2785. if (reply)
  2786. send_convert_reply(r, lkb, error);
  2787. do_convert_effects(r, lkb, error);
  2788. out:
  2789. unlock_rsb(r);
  2790. put_rsb(r);
  2791. dlm_put_lkb(lkb);
  2792. return;
  2793. fail:
  2794. setup_stub_lkb(ls, ms);
  2795. send_convert_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
  2796. }
  2797. static void receive_unlock(struct dlm_ls *ls, struct dlm_message *ms)
  2798. {
  2799. struct dlm_lkb *lkb;
  2800. struct dlm_rsb *r;
  2801. int error;
  2802. error = find_lkb(ls, ms->m_remid, &lkb);
  2803. if (error)
  2804. goto fail;
  2805. r = lkb->lkb_resource;
  2806. hold_rsb(r);
  2807. lock_rsb(r);
  2808. error = validate_message(lkb, ms);
  2809. if (error)
  2810. goto out;
  2811. receive_flags(lkb, ms);
  2812. error = receive_unlock_args(ls, lkb, ms);
  2813. if (error) {
  2814. send_unlock_reply(r, lkb, error);
  2815. goto out;
  2816. }
  2817. error = do_unlock(r, lkb);
  2818. send_unlock_reply(r, lkb, error);
  2819. do_unlock_effects(r, lkb, error);
  2820. out:
  2821. unlock_rsb(r);
  2822. put_rsb(r);
  2823. dlm_put_lkb(lkb);
  2824. return;
  2825. fail:
  2826. setup_stub_lkb(ls, ms);
  2827. send_unlock_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
  2828. }
  2829. static void receive_cancel(struct dlm_ls *ls, struct dlm_message *ms)
  2830. {
  2831. struct dlm_lkb *lkb;
  2832. struct dlm_rsb *r;
  2833. int error;
  2834. error = find_lkb(ls, ms->m_remid, &lkb);
  2835. if (error)
  2836. goto fail;
  2837. receive_flags(lkb, ms);
  2838. r = lkb->lkb_resource;
  2839. hold_rsb(r);
  2840. lock_rsb(r);
  2841. error = validate_message(lkb, ms);
  2842. if (error)
  2843. goto out;
  2844. error = do_cancel(r, lkb);
  2845. send_cancel_reply(r, lkb, error);
  2846. do_cancel_effects(r, lkb, error);
  2847. out:
  2848. unlock_rsb(r);
  2849. put_rsb(r);
  2850. dlm_put_lkb(lkb);
  2851. return;
  2852. fail:
  2853. setup_stub_lkb(ls, ms);
  2854. send_cancel_reply(&ls->ls_stub_rsb, &ls->ls_stub_lkb, error);
  2855. }
  2856. static void receive_grant(struct dlm_ls *ls, struct dlm_message *ms)
  2857. {
  2858. struct dlm_lkb *lkb;
  2859. struct dlm_rsb *r;
  2860. int error;
  2861. error = find_lkb(ls, ms->m_remid, &lkb);
  2862. if (error) {
  2863. log_debug(ls, "receive_grant from %d no lkb %x",
  2864. ms->m_header.h_nodeid, ms->m_remid);
  2865. return;
  2866. }
  2867. r = lkb->lkb_resource;
  2868. hold_rsb(r);
  2869. lock_rsb(r);
  2870. error = validate_message(lkb, ms);
  2871. if (error)
  2872. goto out;
  2873. receive_flags_reply(lkb, ms);
  2874. if (is_altmode(lkb))
  2875. munge_altmode(lkb, ms);
  2876. grant_lock_pc(r, lkb, ms);
  2877. queue_cast(r, lkb, 0);
  2878. out:
  2879. unlock_rsb(r);
  2880. put_rsb(r);
  2881. dlm_put_lkb(lkb);
  2882. }
  2883. static void receive_bast(struct dlm_ls *ls, struct dlm_message *ms)
  2884. {
  2885. struct dlm_lkb *lkb;
  2886. struct dlm_rsb *r;
  2887. int error;
  2888. error = find_lkb(ls, ms->m_remid, &lkb);
  2889. if (error) {
  2890. log_debug(ls, "receive_bast from %d no lkb %x",
  2891. ms->m_header.h_nodeid, ms->m_remid);
  2892. return;
  2893. }
  2894. r = lkb->lkb_resource;
  2895. hold_rsb(r);
  2896. lock_rsb(r);
  2897. error = validate_message(lkb, ms);
  2898. if (error)
  2899. goto out;
  2900. queue_bast(r, lkb, ms->m_bastmode);
  2901. out:
  2902. unlock_rsb(r);
  2903. put_rsb(r);
  2904. dlm_put_lkb(lkb);
  2905. }
  2906. static void receive_lookup(struct dlm_ls *ls, struct dlm_message *ms)
  2907. {
  2908. int len, error, ret_nodeid, dir_nodeid, from_nodeid, our_nodeid;
  2909. from_nodeid = ms->m_header.h_nodeid;
  2910. our_nodeid = dlm_our_nodeid();
  2911. len = receive_extralen(ms);
  2912. dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
  2913. if (dir_nodeid != our_nodeid) {
  2914. log_error(ls, "lookup dir_nodeid %d from %d",
  2915. dir_nodeid, from_nodeid);
  2916. error = -EINVAL;
  2917. ret_nodeid = -1;
  2918. goto out;
  2919. }
  2920. error = dlm_dir_lookup(ls, from_nodeid, ms->m_extra, len, &ret_nodeid);
  2921. /* Optimization: we're master so treat lookup as a request */
  2922. if (!error && ret_nodeid == our_nodeid) {
  2923. receive_request(ls, ms);
  2924. return;
  2925. }
  2926. out:
  2927. send_lookup_reply(ls, ms, ret_nodeid, error);
  2928. }
  2929. static void receive_remove(struct dlm_ls *ls, struct dlm_message *ms)
  2930. {
  2931. int len, dir_nodeid, from_nodeid;
  2932. from_nodeid = ms->m_header.h_nodeid;
  2933. len = receive_extralen(ms);
  2934. dir_nodeid = dlm_hash2nodeid(ls, ms->m_hash);
  2935. if (dir_nodeid != dlm_our_nodeid()) {
  2936. log_error(ls, "remove dir entry dir_nodeid %d from %d",
  2937. dir_nodeid, from_nodeid);
  2938. return;
  2939. }
  2940. dlm_dir_remove_entry(ls, from_nodeid, ms->m_extra, len);
  2941. }
  2942. static void receive_purge(struct dlm_ls *ls, struct dlm_message *ms)
  2943. {
  2944. do_purge(ls, ms->m_nodeid, ms->m_pid);
  2945. }
  2946. static void receive_request_reply(struct dlm_ls *ls, struct dlm_message *ms)
  2947. {
  2948. struct dlm_lkb *lkb;
  2949. struct dlm_rsb *r;
  2950. int error, mstype, result;
  2951. error = find_lkb(ls, ms->m_remid, &lkb);
  2952. if (error) {
  2953. log_debug(ls, "receive_request_reply from %d no lkb %x",
  2954. ms->m_header.h_nodeid, ms->m_remid);
  2955. return;
  2956. }
  2957. r = lkb->lkb_resource;
  2958. hold_rsb(r);
  2959. lock_rsb(r);
  2960. error = validate_message(lkb, ms);
  2961. if (error)
  2962. goto out;
  2963. mstype = lkb->lkb_wait_type;
  2964. error = remove_from_waiters(lkb, DLM_MSG_REQUEST_REPLY);
  2965. if (error)
  2966. goto out;
  2967. /* Optimization: the dir node was also the master, so it took our
  2968. lookup as a request and sent request reply instead of lookup reply */
  2969. if (mstype == DLM_MSG_LOOKUP) {
  2970. r->res_nodeid = ms->m_header.h_nodeid;
  2971. lkb->lkb_nodeid = r->res_nodeid;
  2972. }
  2973. /* this is the value returned from do_request() on the master */
  2974. result = ms->m_result;
  2975. switch (result) {
  2976. case -EAGAIN:
  2977. /* request would block (be queued) on remote master */
  2978. queue_cast(r, lkb, -EAGAIN);
  2979. confirm_master(r, -EAGAIN);
  2980. unhold_lkb(lkb); /* undoes create_lkb() */
  2981. break;
  2982. case -EINPROGRESS:
  2983. case 0:
  2984. /* request was queued or granted on remote master */
  2985. receive_flags_reply(lkb, ms);
  2986. lkb->lkb_remid = ms->m_lkid;
  2987. if (is_altmode(lkb))
  2988. munge_altmode(lkb, ms);
  2989. if (result) {
  2990. add_lkb(r, lkb, DLM_LKSTS_WAITING);
  2991. add_timeout(lkb);
  2992. } else {
  2993. grant_lock_pc(r, lkb, ms);
  2994. queue_cast(r, lkb, 0);
  2995. }
  2996. confirm_master(r, result);
  2997. break;
  2998. case -EBADR:
  2999. case -ENOTBLK:
  3000. /* find_rsb failed to find rsb or rsb wasn't master */
  3001. log_debug(ls, "receive_request_reply %x %x master diff %d %d",
  3002. lkb->lkb_id, lkb->lkb_flags, r->res_nodeid, result);
  3003. r->res_nodeid = -1;
  3004. lkb->lkb_nodeid = -1;
  3005. if (is_overlap(lkb)) {
  3006. /* we'll ignore error in cancel/unlock reply */
  3007. queue_cast_overlap(r, lkb);
  3008. confirm_master(r, result);
  3009. unhold_lkb(lkb); /* undoes create_lkb() */
  3010. } else
  3011. _request_lock(r, lkb);
  3012. break;
  3013. default:
  3014. log_error(ls, "receive_request_reply %x error %d",
  3015. lkb->lkb_id, result);
  3016. }
  3017. if (is_overlap_unlock(lkb) && (result == 0 || result == -EINPROGRESS)) {
  3018. log_debug(ls, "receive_request_reply %x result %d unlock",
  3019. lkb->lkb_id, result);
  3020. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
  3021. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
  3022. send_unlock(r, lkb);
  3023. } else if (is_overlap_cancel(lkb) && (result == -EINPROGRESS)) {
  3024. log_debug(ls, "receive_request_reply %x cancel", lkb->lkb_id);
  3025. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
  3026. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
  3027. send_cancel(r, lkb);
  3028. } else {
  3029. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
  3030. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
  3031. }
  3032. out:
  3033. unlock_rsb(r);
  3034. put_rsb(r);
  3035. dlm_put_lkb(lkb);
  3036. }
  3037. static void __receive_convert_reply(struct dlm_rsb *r, struct dlm_lkb *lkb,
  3038. struct dlm_message *ms)
  3039. {
  3040. /* this is the value returned from do_convert() on the master */
  3041. switch (ms->m_result) {
  3042. case -EAGAIN:
  3043. /* convert would block (be queued) on remote master */
  3044. queue_cast(r, lkb, -EAGAIN);
  3045. break;
  3046. case -EDEADLK:
  3047. receive_flags_reply(lkb, ms);
  3048. revert_lock_pc(r, lkb);
  3049. queue_cast(r, lkb, -EDEADLK);
  3050. break;
  3051. case -EINPROGRESS:
  3052. /* convert was queued on remote master */
  3053. receive_flags_reply(lkb, ms);
  3054. if (is_demoted(lkb))
  3055. munge_demoted(lkb);
  3056. del_lkb(r, lkb);
  3057. add_lkb(r, lkb, DLM_LKSTS_CONVERT);
  3058. add_timeout(lkb);
  3059. break;
  3060. case 0:
  3061. /* convert was granted on remote master */
  3062. receive_flags_reply(lkb, ms);
  3063. if (is_demoted(lkb))
  3064. munge_demoted(lkb);
  3065. grant_lock_pc(r, lkb, ms);
  3066. queue_cast(r, lkb, 0);
  3067. break;
  3068. default:
  3069. log_error(r->res_ls, "receive_convert_reply %x error %d",
  3070. lkb->lkb_id, ms->m_result);
  3071. }
  3072. }
  3073. static void _receive_convert_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
  3074. {
  3075. struct dlm_rsb *r = lkb->lkb_resource;
  3076. int error;
  3077. hold_rsb(r);
  3078. lock_rsb(r);
  3079. error = validate_message(lkb, ms);
  3080. if (error)
  3081. goto out;
  3082. /* stub reply can happen with waiters_mutex held */
  3083. error = remove_from_waiters_ms(lkb, ms);
  3084. if (error)
  3085. goto out;
  3086. __receive_convert_reply(r, lkb, ms);
  3087. out:
  3088. unlock_rsb(r);
  3089. put_rsb(r);
  3090. }
  3091. static void receive_convert_reply(struct dlm_ls *ls, struct dlm_message *ms)
  3092. {
  3093. struct dlm_lkb *lkb;
  3094. int error;
  3095. error = find_lkb(ls, ms->m_remid, &lkb);
  3096. if (error) {
  3097. log_debug(ls, "receive_convert_reply from %d no lkb %x",
  3098. ms->m_header.h_nodeid, ms->m_remid);
  3099. return;
  3100. }
  3101. _receive_convert_reply(lkb, ms);
  3102. dlm_put_lkb(lkb);
  3103. }
  3104. static void _receive_unlock_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
  3105. {
  3106. struct dlm_rsb *r = lkb->lkb_resource;
  3107. int error;
  3108. hold_rsb(r);
  3109. lock_rsb(r);
  3110. error = validate_message(lkb, ms);
  3111. if (error)
  3112. goto out;
  3113. /* stub reply can happen with waiters_mutex held */
  3114. error = remove_from_waiters_ms(lkb, ms);
  3115. if (error)
  3116. goto out;
  3117. /* this is the value returned from do_unlock() on the master */
  3118. switch (ms->m_result) {
  3119. case -DLM_EUNLOCK:
  3120. receive_flags_reply(lkb, ms);
  3121. remove_lock_pc(r, lkb);
  3122. queue_cast(r, lkb, -DLM_EUNLOCK);
  3123. break;
  3124. case -ENOENT:
  3125. break;
  3126. default:
  3127. log_error(r->res_ls, "receive_unlock_reply %x error %d",
  3128. lkb->lkb_id, ms->m_result);
  3129. }
  3130. out:
  3131. unlock_rsb(r);
  3132. put_rsb(r);
  3133. }
  3134. static void receive_unlock_reply(struct dlm_ls *ls, struct dlm_message *ms)
  3135. {
  3136. struct dlm_lkb *lkb;
  3137. int error;
  3138. error = find_lkb(ls, ms->m_remid, &lkb);
  3139. if (error) {
  3140. log_debug(ls, "receive_unlock_reply from %d no lkb %x",
  3141. ms->m_header.h_nodeid, ms->m_remid);
  3142. return;
  3143. }
  3144. _receive_unlock_reply(lkb, ms);
  3145. dlm_put_lkb(lkb);
  3146. }
  3147. static void _receive_cancel_reply(struct dlm_lkb *lkb, struct dlm_message *ms)
  3148. {
  3149. struct dlm_rsb *r = lkb->lkb_resource;
  3150. int error;
  3151. hold_rsb(r);
  3152. lock_rsb(r);
  3153. error = validate_message(lkb, ms);
  3154. if (error)
  3155. goto out;
  3156. /* stub reply can happen with waiters_mutex held */
  3157. error = remove_from_waiters_ms(lkb, ms);
  3158. if (error)
  3159. goto out;
  3160. /* this is the value returned from do_cancel() on the master */
  3161. switch (ms->m_result) {
  3162. case -DLM_ECANCEL:
  3163. receive_flags_reply(lkb, ms);
  3164. revert_lock_pc(r, lkb);
  3165. queue_cast(r, lkb, -DLM_ECANCEL);
  3166. break;
  3167. case 0:
  3168. break;
  3169. default:
  3170. log_error(r->res_ls, "receive_cancel_reply %x error %d",
  3171. lkb->lkb_id, ms->m_result);
  3172. }
  3173. out:
  3174. unlock_rsb(r);
  3175. put_rsb(r);
  3176. }
  3177. static void receive_cancel_reply(struct dlm_ls *ls, struct dlm_message *ms)
  3178. {
  3179. struct dlm_lkb *lkb;
  3180. int error;
  3181. error = find_lkb(ls, ms->m_remid, &lkb);
  3182. if (error) {
  3183. log_debug(ls, "receive_cancel_reply from %d no lkb %x",
  3184. ms->m_header.h_nodeid, ms->m_remid);
  3185. return;
  3186. }
  3187. _receive_cancel_reply(lkb, ms);
  3188. dlm_put_lkb(lkb);
  3189. }
  3190. static void receive_lookup_reply(struct dlm_ls *ls, struct dlm_message *ms)
  3191. {
  3192. struct dlm_lkb *lkb;
  3193. struct dlm_rsb *r;
  3194. int error, ret_nodeid;
  3195. error = find_lkb(ls, ms->m_lkid, &lkb);
  3196. if (error) {
  3197. log_error(ls, "receive_lookup_reply no lkb");
  3198. return;
  3199. }
  3200. /* ms->m_result is the value returned by dlm_dir_lookup on dir node
  3201. FIXME: will a non-zero error ever be returned? */
  3202. r = lkb->lkb_resource;
  3203. hold_rsb(r);
  3204. lock_rsb(r);
  3205. error = remove_from_waiters(lkb, DLM_MSG_LOOKUP_REPLY);
  3206. if (error)
  3207. goto out;
  3208. ret_nodeid = ms->m_nodeid;
  3209. if (ret_nodeid == dlm_our_nodeid()) {
  3210. r->res_nodeid = 0;
  3211. ret_nodeid = 0;
  3212. r->res_first_lkid = 0;
  3213. } else {
  3214. /* set_master() will copy res_nodeid to lkb_nodeid */
  3215. r->res_nodeid = ret_nodeid;
  3216. }
  3217. if (is_overlap(lkb)) {
  3218. log_debug(ls, "receive_lookup_reply %x unlock %x",
  3219. lkb->lkb_id, lkb->lkb_flags);
  3220. queue_cast_overlap(r, lkb);
  3221. unhold_lkb(lkb); /* undoes create_lkb() */
  3222. goto out_list;
  3223. }
  3224. _request_lock(r, lkb);
  3225. out_list:
  3226. if (!ret_nodeid)
  3227. process_lookup_list(r);
  3228. out:
  3229. unlock_rsb(r);
  3230. put_rsb(r);
  3231. dlm_put_lkb(lkb);
  3232. }
  3233. static void _receive_message(struct dlm_ls *ls, struct dlm_message *ms)
  3234. {
  3235. if (!dlm_is_member(ls, ms->m_header.h_nodeid)) {
  3236. log_debug(ls, "ignore non-member message %d from %d %x %x %d",
  3237. ms->m_type, ms->m_header.h_nodeid, ms->m_lkid,
  3238. ms->m_remid, ms->m_result);
  3239. return;
  3240. }
  3241. switch (ms->m_type) {
  3242. /* messages sent to a master node */
  3243. case DLM_MSG_REQUEST:
  3244. receive_request(ls, ms);
  3245. break;
  3246. case DLM_MSG_CONVERT:
  3247. receive_convert(ls, ms);
  3248. break;
  3249. case DLM_MSG_UNLOCK:
  3250. receive_unlock(ls, ms);
  3251. break;
  3252. case DLM_MSG_CANCEL:
  3253. receive_cancel(ls, ms);
  3254. break;
  3255. /* messages sent from a master node (replies to above) */
  3256. case DLM_MSG_REQUEST_REPLY:
  3257. receive_request_reply(ls, ms);
  3258. break;
  3259. case DLM_MSG_CONVERT_REPLY:
  3260. receive_convert_reply(ls, ms);
  3261. break;
  3262. case DLM_MSG_UNLOCK_REPLY:
  3263. receive_unlock_reply(ls, ms);
  3264. break;
  3265. case DLM_MSG_CANCEL_REPLY:
  3266. receive_cancel_reply(ls, ms);
  3267. break;
  3268. /* messages sent from a master node (only two types of async msg) */
  3269. case DLM_MSG_GRANT:
  3270. receive_grant(ls, ms);
  3271. break;
  3272. case DLM_MSG_BAST:
  3273. receive_bast(ls, ms);
  3274. break;
  3275. /* messages sent to a dir node */
  3276. case DLM_MSG_LOOKUP:
  3277. receive_lookup(ls, ms);
  3278. break;
  3279. case DLM_MSG_REMOVE:
  3280. receive_remove(ls, ms);
  3281. break;
  3282. /* messages sent from a dir node (remove has no reply) */
  3283. case DLM_MSG_LOOKUP_REPLY:
  3284. receive_lookup_reply(ls, ms);
  3285. break;
  3286. /* other messages */
  3287. case DLM_MSG_PURGE:
  3288. receive_purge(ls, ms);
  3289. break;
  3290. default:
  3291. log_error(ls, "unknown message type %d", ms->m_type);
  3292. }
  3293. }
  3294. /* If the lockspace is in recovery mode (locking stopped), then normal
  3295. messages are saved on the requestqueue for processing after recovery is
  3296. done. When not in recovery mode, we wait for dlm_recoverd to drain saved
  3297. messages off the requestqueue before we process new ones. This occurs right
  3298. after recovery completes when we transition from saving all messages on
  3299. requestqueue, to processing all the saved messages, to processing new
  3300. messages as they arrive. */
  3301. static void dlm_receive_message(struct dlm_ls *ls, struct dlm_message *ms,
  3302. int nodeid)
  3303. {
  3304. if (dlm_locking_stopped(ls)) {
  3305. dlm_add_requestqueue(ls, nodeid, ms);
  3306. } else {
  3307. dlm_wait_requestqueue(ls);
  3308. _receive_message(ls, ms);
  3309. }
  3310. }
  3311. /* This is called by dlm_recoverd to process messages that were saved on
  3312. the requestqueue. */
  3313. void dlm_receive_message_saved(struct dlm_ls *ls, struct dlm_message *ms)
  3314. {
  3315. _receive_message(ls, ms);
  3316. }
  3317. /* This is called by the midcomms layer when something is received for
  3318. the lockspace. It could be either a MSG (normal message sent as part of
  3319. standard locking activity) or an RCOM (recovery message sent as part of
  3320. lockspace recovery). */
  3321. void dlm_receive_buffer(union dlm_packet *p, int nodeid)
  3322. {
  3323. struct dlm_header *hd = &p->header;
  3324. struct dlm_ls *ls;
  3325. int type = 0;
  3326. switch (hd->h_cmd) {
  3327. case DLM_MSG:
  3328. dlm_message_in(&p->message);
  3329. type = p->message.m_type;
  3330. break;
  3331. case DLM_RCOM:
  3332. dlm_rcom_in(&p->rcom);
  3333. type = p->rcom.rc_type;
  3334. break;
  3335. default:
  3336. log_print("invalid h_cmd %d from %u", hd->h_cmd, nodeid);
  3337. return;
  3338. }
  3339. if (hd->h_nodeid != nodeid) {
  3340. log_print("invalid h_nodeid %d from %d lockspace %x",
  3341. hd->h_nodeid, nodeid, hd->h_lockspace);
  3342. return;
  3343. }
  3344. ls = dlm_find_lockspace_global(hd->h_lockspace);
  3345. if (!ls) {
  3346. if (dlm_config.ci_log_debug)
  3347. log_print("invalid lockspace %x from %d cmd %d type %d",
  3348. hd->h_lockspace, nodeid, hd->h_cmd, type);
  3349. if (hd->h_cmd == DLM_RCOM && type == DLM_RCOM_STATUS)
  3350. dlm_send_ls_not_ready(nodeid, &p->rcom);
  3351. return;
  3352. }
  3353. /* this rwsem allows dlm_ls_stop() to wait for all dlm_recv threads to
  3354. be inactive (in this ls) before transitioning to recovery mode */
  3355. down_read(&ls->ls_recv_active);
  3356. if (hd->h_cmd == DLM_MSG)
  3357. dlm_receive_message(ls, &p->message, nodeid);
  3358. else
  3359. dlm_receive_rcom(ls, &p->rcom, nodeid);
  3360. up_read(&ls->ls_recv_active);
  3361. dlm_put_lockspace(ls);
  3362. }
  3363. static void recover_convert_waiter(struct dlm_ls *ls, struct dlm_lkb *lkb,
  3364. struct dlm_message *ms_stub)
  3365. {
  3366. if (middle_conversion(lkb)) {
  3367. hold_lkb(lkb);
  3368. memset(ms_stub, 0, sizeof(struct dlm_message));
  3369. ms_stub->m_flags = DLM_IFL_STUB_MS;
  3370. ms_stub->m_type = DLM_MSG_CONVERT_REPLY;
  3371. ms_stub->m_result = -EINPROGRESS;
  3372. ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
  3373. _receive_convert_reply(lkb, ms_stub);
  3374. /* Same special case as in receive_rcom_lock_args() */
  3375. lkb->lkb_grmode = DLM_LOCK_IV;
  3376. rsb_set_flag(lkb->lkb_resource, RSB_RECOVER_CONVERT);
  3377. unhold_lkb(lkb);
  3378. } else if (lkb->lkb_rqmode >= lkb->lkb_grmode) {
  3379. lkb->lkb_flags |= DLM_IFL_RESEND;
  3380. }
  3381. /* lkb->lkb_rqmode < lkb->lkb_grmode shouldn't happen since down
  3382. conversions are async; there's no reply from the remote master */
  3383. }
  3384. /* A waiting lkb needs recovery if the master node has failed, or
  3385. the master node is changing (only when no directory is used) */
  3386. static int waiter_needs_recovery(struct dlm_ls *ls, struct dlm_lkb *lkb)
  3387. {
  3388. if (dlm_is_removed(ls, lkb->lkb_nodeid))
  3389. return 1;
  3390. if (!dlm_no_directory(ls))
  3391. return 0;
  3392. if (dlm_dir_nodeid(lkb->lkb_resource) != lkb->lkb_nodeid)
  3393. return 1;
  3394. return 0;
  3395. }
  3396. /* Recovery for locks that are waiting for replies from nodes that are now
  3397. gone. We can just complete unlocks and cancels by faking a reply from the
  3398. dead node. Requests and up-conversions we flag to be resent after
  3399. recovery. Down-conversions can just be completed with a fake reply like
  3400. unlocks. Conversions between PR and CW need special attention. */
  3401. void dlm_recover_waiters_pre(struct dlm_ls *ls)
  3402. {
  3403. struct dlm_lkb *lkb, *safe;
  3404. struct dlm_message *ms_stub;
  3405. int wait_type, stub_unlock_result, stub_cancel_result;
  3406. ms_stub = kmalloc(sizeof(struct dlm_message), GFP_KERNEL);
  3407. if (!ms_stub) {
  3408. log_error(ls, "dlm_recover_waiters_pre no mem");
  3409. return;
  3410. }
  3411. mutex_lock(&ls->ls_waiters_mutex);
  3412. list_for_each_entry_safe(lkb, safe, &ls->ls_waiters, lkb_wait_reply) {
  3413. /* exclude debug messages about unlocks because there can be so
  3414. many and they aren't very interesting */
  3415. if (lkb->lkb_wait_type != DLM_MSG_UNLOCK) {
  3416. log_debug(ls, "recover_waiter %x nodeid %d "
  3417. "msg %d to %d", lkb->lkb_id, lkb->lkb_nodeid,
  3418. lkb->lkb_wait_type, lkb->lkb_wait_nodeid);
  3419. }
  3420. /* all outstanding lookups, regardless of destination will be
  3421. resent after recovery is done */
  3422. if (lkb->lkb_wait_type == DLM_MSG_LOOKUP) {
  3423. lkb->lkb_flags |= DLM_IFL_RESEND;
  3424. continue;
  3425. }
  3426. if (!waiter_needs_recovery(ls, lkb))
  3427. continue;
  3428. wait_type = lkb->lkb_wait_type;
  3429. stub_unlock_result = -DLM_EUNLOCK;
  3430. stub_cancel_result = -DLM_ECANCEL;
  3431. /* Main reply may have been received leaving a zero wait_type,
  3432. but a reply for the overlapping op may not have been
  3433. received. In that case we need to fake the appropriate
  3434. reply for the overlap op. */
  3435. if (!wait_type) {
  3436. if (is_overlap_cancel(lkb)) {
  3437. wait_type = DLM_MSG_CANCEL;
  3438. if (lkb->lkb_grmode == DLM_LOCK_IV)
  3439. stub_cancel_result = 0;
  3440. }
  3441. if (is_overlap_unlock(lkb)) {
  3442. wait_type = DLM_MSG_UNLOCK;
  3443. if (lkb->lkb_grmode == DLM_LOCK_IV)
  3444. stub_unlock_result = -ENOENT;
  3445. }
  3446. log_debug(ls, "rwpre overlap %x %x %d %d %d",
  3447. lkb->lkb_id, lkb->lkb_flags, wait_type,
  3448. stub_cancel_result, stub_unlock_result);
  3449. }
  3450. switch (wait_type) {
  3451. case DLM_MSG_REQUEST:
  3452. lkb->lkb_flags |= DLM_IFL_RESEND;
  3453. break;
  3454. case DLM_MSG_CONVERT:
  3455. recover_convert_waiter(ls, lkb, ms_stub);
  3456. break;
  3457. case DLM_MSG_UNLOCK:
  3458. hold_lkb(lkb);
  3459. memset(ms_stub, 0, sizeof(struct dlm_message));
  3460. ms_stub->m_flags = DLM_IFL_STUB_MS;
  3461. ms_stub->m_type = DLM_MSG_UNLOCK_REPLY;
  3462. ms_stub->m_result = stub_unlock_result;
  3463. ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
  3464. _receive_unlock_reply(lkb, ms_stub);
  3465. dlm_put_lkb(lkb);
  3466. break;
  3467. case DLM_MSG_CANCEL:
  3468. hold_lkb(lkb);
  3469. memset(ms_stub, 0, sizeof(struct dlm_message));
  3470. ms_stub->m_flags = DLM_IFL_STUB_MS;
  3471. ms_stub->m_type = DLM_MSG_CANCEL_REPLY;
  3472. ms_stub->m_result = stub_cancel_result;
  3473. ms_stub->m_header.h_nodeid = lkb->lkb_nodeid;
  3474. _receive_cancel_reply(lkb, ms_stub);
  3475. dlm_put_lkb(lkb);
  3476. break;
  3477. default:
  3478. log_error(ls, "invalid lkb wait_type %d %d",
  3479. lkb->lkb_wait_type, wait_type);
  3480. }
  3481. schedule();
  3482. }
  3483. mutex_unlock(&ls->ls_waiters_mutex);
  3484. kfree(ms_stub);
  3485. }
  3486. static struct dlm_lkb *find_resend_waiter(struct dlm_ls *ls)
  3487. {
  3488. struct dlm_lkb *lkb;
  3489. int found = 0;
  3490. mutex_lock(&ls->ls_waiters_mutex);
  3491. list_for_each_entry(lkb, &ls->ls_waiters, lkb_wait_reply) {
  3492. if (lkb->lkb_flags & DLM_IFL_RESEND) {
  3493. hold_lkb(lkb);
  3494. found = 1;
  3495. break;
  3496. }
  3497. }
  3498. mutex_unlock(&ls->ls_waiters_mutex);
  3499. if (!found)
  3500. lkb = NULL;
  3501. return lkb;
  3502. }
  3503. /* Deal with lookups and lkb's marked RESEND from _pre. We may now be the
  3504. master or dir-node for r. Processing the lkb may result in it being placed
  3505. back on waiters. */
  3506. /* We do this after normal locking has been enabled and any saved messages
  3507. (in requestqueue) have been processed. We should be confident that at
  3508. this point we won't get or process a reply to any of these waiting
  3509. operations. But, new ops may be coming in on the rsbs/locks here from
  3510. userspace or remotely. */
  3511. /* there may have been an overlap unlock/cancel prior to recovery or after
  3512. recovery. if before, the lkb may still have a pos wait_count; if after, the
  3513. overlap flag would just have been set and nothing new sent. we can be
  3514. confident here than any replies to either the initial op or overlap ops
  3515. prior to recovery have been received. */
  3516. int dlm_recover_waiters_post(struct dlm_ls *ls)
  3517. {
  3518. struct dlm_lkb *lkb;
  3519. struct dlm_rsb *r;
  3520. int error = 0, mstype, err, oc, ou;
  3521. while (1) {
  3522. if (dlm_locking_stopped(ls)) {
  3523. log_debug(ls, "recover_waiters_post aborted");
  3524. error = -EINTR;
  3525. break;
  3526. }
  3527. lkb = find_resend_waiter(ls);
  3528. if (!lkb)
  3529. break;
  3530. r = lkb->lkb_resource;
  3531. hold_rsb(r);
  3532. lock_rsb(r);
  3533. mstype = lkb->lkb_wait_type;
  3534. oc = is_overlap_cancel(lkb);
  3535. ou = is_overlap_unlock(lkb);
  3536. err = 0;
  3537. log_debug(ls, "recover_waiter %x nodeid %d msg %d r_nodeid %d",
  3538. lkb->lkb_id, lkb->lkb_nodeid, mstype, r->res_nodeid);
  3539. /* At this point we assume that we won't get a reply to any
  3540. previous op or overlap op on this lock. First, do a big
  3541. remove_from_waiters() for all previous ops. */
  3542. lkb->lkb_flags &= ~DLM_IFL_RESEND;
  3543. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_UNLOCK;
  3544. lkb->lkb_flags &= ~DLM_IFL_OVERLAP_CANCEL;
  3545. lkb->lkb_wait_type = 0;
  3546. lkb->lkb_wait_count = 0;
  3547. mutex_lock(&ls->ls_waiters_mutex);
  3548. list_del_init(&lkb->lkb_wait_reply);
  3549. mutex_unlock(&ls->ls_waiters_mutex);
  3550. unhold_lkb(lkb); /* for waiters list */
  3551. if (oc || ou) {
  3552. /* do an unlock or cancel instead of resending */
  3553. switch (mstype) {
  3554. case DLM_MSG_LOOKUP:
  3555. case DLM_MSG_REQUEST:
  3556. queue_cast(r, lkb, ou ? -DLM_EUNLOCK :
  3557. -DLM_ECANCEL);
  3558. unhold_lkb(lkb); /* undoes create_lkb() */
  3559. break;
  3560. case DLM_MSG_CONVERT:
  3561. if (oc) {
  3562. queue_cast(r, lkb, -DLM_ECANCEL);
  3563. } else {
  3564. lkb->lkb_exflags |= DLM_LKF_FORCEUNLOCK;
  3565. _unlock_lock(r, lkb);
  3566. }
  3567. break;
  3568. default:
  3569. err = 1;
  3570. }
  3571. } else {
  3572. switch (mstype) {
  3573. case DLM_MSG_LOOKUP:
  3574. case DLM_MSG_REQUEST:
  3575. _request_lock(r, lkb);
  3576. if (is_master(r))
  3577. confirm_master(r, 0);
  3578. break;
  3579. case DLM_MSG_CONVERT:
  3580. _convert_lock(r, lkb);
  3581. break;
  3582. default:
  3583. err = 1;
  3584. }
  3585. }
  3586. if (err)
  3587. log_error(ls, "recover_waiters_post %x %d %x %d %d",
  3588. lkb->lkb_id, mstype, lkb->lkb_flags, oc, ou);
  3589. unlock_rsb(r);
  3590. put_rsb(r);
  3591. dlm_put_lkb(lkb);
  3592. }
  3593. return error;
  3594. }
  3595. static void purge_queue(struct dlm_rsb *r, struct list_head *queue,
  3596. int (*test)(struct dlm_ls *ls, struct dlm_lkb *lkb))
  3597. {
  3598. struct dlm_ls *ls = r->res_ls;
  3599. struct dlm_lkb *lkb, *safe;
  3600. list_for_each_entry_safe(lkb, safe, queue, lkb_statequeue) {
  3601. if (test(ls, lkb)) {
  3602. rsb_set_flag(r, RSB_LOCKS_PURGED);
  3603. del_lkb(r, lkb);
  3604. /* this put should free the lkb */
  3605. if (!dlm_put_lkb(lkb))
  3606. log_error(ls, "purged lkb not released");
  3607. }
  3608. }
  3609. }
  3610. static int purge_dead_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
  3611. {
  3612. return (is_master_copy(lkb) && dlm_is_removed(ls, lkb->lkb_nodeid));
  3613. }
  3614. static int purge_mstcpy_test(struct dlm_ls *ls, struct dlm_lkb *lkb)
  3615. {
  3616. return is_master_copy(lkb);
  3617. }
  3618. static void purge_dead_locks(struct dlm_rsb *r)
  3619. {
  3620. purge_queue(r, &r->res_grantqueue, &purge_dead_test);
  3621. purge_queue(r, &r->res_convertqueue, &purge_dead_test);
  3622. purge_queue(r, &r->res_waitqueue, &purge_dead_test);
  3623. }
  3624. void dlm_purge_mstcpy_locks(struct dlm_rsb *r)
  3625. {
  3626. purge_queue(r, &r->res_grantqueue, &purge_mstcpy_test);
  3627. purge_queue(r, &r->res_convertqueue, &purge_mstcpy_test);
  3628. purge_queue(r, &r->res_waitqueue, &purge_mstcpy_test);
  3629. }
  3630. /* Get rid of locks held by nodes that are gone. */
  3631. int dlm_purge_locks(struct dlm_ls *ls)
  3632. {
  3633. struct dlm_rsb *r;
  3634. log_debug(ls, "dlm_purge_locks");
  3635. down_write(&ls->ls_root_sem);
  3636. list_for_each_entry(r, &ls->ls_root_list, res_root_list) {
  3637. hold_rsb(r);
  3638. lock_rsb(r);
  3639. if (is_master(r))
  3640. purge_dead_locks(r);
  3641. unlock_rsb(r);
  3642. unhold_rsb(r);
  3643. schedule();
  3644. }
  3645. up_write(&ls->ls_root_sem);
  3646. return 0;
  3647. }
  3648. static struct dlm_rsb *find_purged_rsb(struct dlm_ls *ls, int bucket)
  3649. {
  3650. struct dlm_rsb *r, *r_ret = NULL;
  3651. spin_lock(&ls->ls_rsbtbl[bucket].lock);
  3652. list_for_each_entry(r, &ls->ls_rsbtbl[bucket].list, res_hashchain) {
  3653. if (!rsb_flag(r, RSB_LOCKS_PURGED))
  3654. continue;
  3655. hold_rsb(r);
  3656. rsb_clear_flag(r, RSB_LOCKS_PURGED);
  3657. r_ret = r;
  3658. break;
  3659. }
  3660. spin_unlock(&ls->ls_rsbtbl[bucket].lock);
  3661. return r_ret;
  3662. }
  3663. void dlm_grant_after_purge(struct dlm_ls *ls)
  3664. {
  3665. struct dlm_rsb *r;
  3666. int bucket = 0;
  3667. while (1) {
  3668. r = find_purged_rsb(ls, bucket);
  3669. if (!r) {
  3670. if (bucket == ls->ls_rsbtbl_size - 1)
  3671. break;
  3672. bucket++;
  3673. continue;
  3674. }
  3675. lock_rsb(r);
  3676. if (is_master(r)) {
  3677. grant_pending_locks(r);
  3678. confirm_master(r, 0);
  3679. }
  3680. unlock_rsb(r);
  3681. put_rsb(r);
  3682. schedule();
  3683. }
  3684. }
  3685. static struct dlm_lkb *search_remid_list(struct list_head *head, int nodeid,
  3686. uint32_t remid)
  3687. {
  3688. struct dlm_lkb *lkb;
  3689. list_for_each_entry(lkb, head, lkb_statequeue) {
  3690. if (lkb->lkb_nodeid == nodeid && lkb->lkb_remid == remid)
  3691. return lkb;
  3692. }
  3693. return NULL;
  3694. }
  3695. static struct dlm_lkb *search_remid(struct dlm_rsb *r, int nodeid,
  3696. uint32_t remid)
  3697. {
  3698. struct dlm_lkb *lkb;
  3699. lkb = search_remid_list(&r->res_grantqueue, nodeid, remid);
  3700. if (lkb)
  3701. return lkb;
  3702. lkb = search_remid_list(&r->res_convertqueue, nodeid, remid);
  3703. if (lkb)
  3704. return lkb;
  3705. lkb = search_remid_list(&r->res_waitqueue, nodeid, remid);
  3706. if (lkb)
  3707. return lkb;
  3708. return NULL;
  3709. }
  3710. /* needs at least dlm_rcom + rcom_lock */
  3711. static int receive_rcom_lock_args(struct dlm_ls *ls, struct dlm_lkb *lkb,
  3712. struct dlm_rsb *r, struct dlm_rcom *rc)
  3713. {
  3714. struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
  3715. lkb->lkb_nodeid = rc->rc_header.h_nodeid;
  3716. lkb->lkb_ownpid = le32_to_cpu(rl->rl_ownpid);
  3717. lkb->lkb_remid = le32_to_cpu(rl->rl_lkid);
  3718. lkb->lkb_exflags = le32_to_cpu(rl->rl_exflags);
  3719. lkb->lkb_flags = le32_to_cpu(rl->rl_flags) & 0x0000FFFF;
  3720. lkb->lkb_flags |= DLM_IFL_MSTCPY;
  3721. lkb->lkb_lvbseq = le32_to_cpu(rl->rl_lvbseq);
  3722. lkb->lkb_rqmode = rl->rl_rqmode;
  3723. lkb->lkb_grmode = rl->rl_grmode;
  3724. /* don't set lkb_status because add_lkb wants to itself */
  3725. lkb->lkb_bastfn = (rl->rl_asts & DLM_CB_BAST) ? &fake_bastfn : NULL;
  3726. lkb->lkb_astfn = (rl->rl_asts & DLM_CB_CAST) ? &fake_astfn : NULL;
  3727. if (lkb->lkb_exflags & DLM_LKF_VALBLK) {
  3728. int lvblen = rc->rc_header.h_length - sizeof(struct dlm_rcom) -
  3729. sizeof(struct rcom_lock);
  3730. if (lvblen > ls->ls_lvblen)
  3731. return -EINVAL;
  3732. lkb->lkb_lvbptr = dlm_allocate_lvb(ls);
  3733. if (!lkb->lkb_lvbptr)
  3734. return -ENOMEM;
  3735. memcpy(lkb->lkb_lvbptr, rl->rl_lvb, lvblen);
  3736. }
  3737. /* Conversions between PR and CW (middle modes) need special handling.
  3738. The real granted mode of these converting locks cannot be determined
  3739. until all locks have been rebuilt on the rsb (recover_conversion) */
  3740. if (rl->rl_wait_type == cpu_to_le16(DLM_MSG_CONVERT) &&
  3741. middle_conversion(lkb)) {
  3742. rl->rl_status = DLM_LKSTS_CONVERT;
  3743. lkb->lkb_grmode = DLM_LOCK_IV;
  3744. rsb_set_flag(r, RSB_RECOVER_CONVERT);
  3745. }
  3746. return 0;
  3747. }
  3748. /* This lkb may have been recovered in a previous aborted recovery so we need
  3749. to check if the rsb already has an lkb with the given remote nodeid/lkid.
  3750. If so we just send back a standard reply. If not, we create a new lkb with
  3751. the given values and send back our lkid. We send back our lkid by sending
  3752. back the rcom_lock struct we got but with the remid field filled in. */
  3753. /* needs at least dlm_rcom + rcom_lock */
  3754. int dlm_recover_master_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
  3755. {
  3756. struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
  3757. struct dlm_rsb *r;
  3758. struct dlm_lkb *lkb;
  3759. int error;
  3760. if (rl->rl_parent_lkid) {
  3761. error = -EOPNOTSUPP;
  3762. goto out;
  3763. }
  3764. error = find_rsb(ls, rl->rl_name, le16_to_cpu(rl->rl_namelen),
  3765. R_MASTER, &r);
  3766. if (error)
  3767. goto out;
  3768. lock_rsb(r);
  3769. lkb = search_remid(r, rc->rc_header.h_nodeid, le32_to_cpu(rl->rl_lkid));
  3770. if (lkb) {
  3771. error = -EEXIST;
  3772. goto out_remid;
  3773. }
  3774. error = create_lkb(ls, &lkb);
  3775. if (error)
  3776. goto out_unlock;
  3777. error = receive_rcom_lock_args(ls, lkb, r, rc);
  3778. if (error) {
  3779. __put_lkb(ls, lkb);
  3780. goto out_unlock;
  3781. }
  3782. attach_lkb(r, lkb);
  3783. add_lkb(r, lkb, rl->rl_status);
  3784. error = 0;
  3785. out_remid:
  3786. /* this is the new value returned to the lock holder for
  3787. saving in its process-copy lkb */
  3788. rl->rl_remid = cpu_to_le32(lkb->lkb_id);
  3789. out_unlock:
  3790. unlock_rsb(r);
  3791. put_rsb(r);
  3792. out:
  3793. if (error)
  3794. log_debug(ls, "recover_master_copy %d %x", error,
  3795. le32_to_cpu(rl->rl_lkid));
  3796. rl->rl_result = cpu_to_le32(error);
  3797. return error;
  3798. }
  3799. /* needs at least dlm_rcom + rcom_lock */
  3800. int dlm_recover_process_copy(struct dlm_ls *ls, struct dlm_rcom *rc)
  3801. {
  3802. struct rcom_lock *rl = (struct rcom_lock *) rc->rc_buf;
  3803. struct dlm_rsb *r;
  3804. struct dlm_lkb *lkb;
  3805. int error;
  3806. error = find_lkb(ls, le32_to_cpu(rl->rl_lkid), &lkb);
  3807. if (error) {
  3808. log_error(ls, "recover_process_copy no lkid %x",
  3809. le32_to_cpu(rl->rl_lkid));
  3810. return error;
  3811. }
  3812. DLM_ASSERT(is_process_copy(lkb), dlm_print_lkb(lkb););
  3813. error = le32_to_cpu(rl->rl_result);
  3814. r = lkb->lkb_resource;
  3815. hold_rsb(r);
  3816. lock_rsb(r);
  3817. switch (error) {
  3818. case -EBADR:
  3819. /* There's a chance the new master received our lock before
  3820. dlm_recover_master_reply(), this wouldn't happen if we did
  3821. a barrier between recover_masters and recover_locks. */
  3822. log_debug(ls, "master copy not ready %x r %lx %s", lkb->lkb_id,
  3823. (unsigned long)r, r->res_name);
  3824. dlm_send_rcom_lock(r, lkb);
  3825. goto out;
  3826. case -EEXIST:
  3827. log_debug(ls, "master copy exists %x", lkb->lkb_id);
  3828. /* fall through */
  3829. case 0:
  3830. lkb->lkb_remid = le32_to_cpu(rl->rl_remid);
  3831. break;
  3832. default:
  3833. log_error(ls, "dlm_recover_process_copy unknown error %d %x",
  3834. error, lkb->lkb_id);
  3835. }
  3836. /* an ack for dlm_recover_locks() which waits for replies from
  3837. all the locks it sends to new masters */
  3838. dlm_recovered_lock(r);
  3839. out:
  3840. unlock_rsb(r);
  3841. put_rsb(r);
  3842. dlm_put_lkb(lkb);
  3843. return 0;
  3844. }
  3845. int dlm_user_request(struct dlm_ls *ls, struct dlm_user_args *ua,
  3846. int mode, uint32_t flags, void *name, unsigned int namelen,
  3847. unsigned long timeout_cs)
  3848. {
  3849. struct dlm_lkb *lkb;
  3850. struct dlm_args args;
  3851. int error;
  3852. dlm_lock_recovery(ls);
  3853. error = create_lkb(ls, &lkb);
  3854. if (error) {
  3855. kfree(ua);
  3856. goto out;
  3857. }
  3858. if (flags & DLM_LKF_VALBLK) {
  3859. ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
  3860. if (!ua->lksb.sb_lvbptr) {
  3861. kfree(ua);
  3862. __put_lkb(ls, lkb);
  3863. error = -ENOMEM;
  3864. goto out;
  3865. }
  3866. }
  3867. /* After ua is attached to lkb it will be freed by dlm_free_lkb().
  3868. When DLM_IFL_USER is set, the dlm knows that this is a userspace
  3869. lock and that lkb_astparam is the dlm_user_args structure. */
  3870. error = set_lock_args(mode, &ua->lksb, flags, namelen, timeout_cs,
  3871. fake_astfn, ua, fake_bastfn, &args);
  3872. lkb->lkb_flags |= DLM_IFL_USER;
  3873. if (error) {
  3874. __put_lkb(ls, lkb);
  3875. goto out;
  3876. }
  3877. error = request_lock(ls, lkb, name, namelen, &args);
  3878. switch (error) {
  3879. case 0:
  3880. break;
  3881. case -EINPROGRESS:
  3882. error = 0;
  3883. break;
  3884. case -EAGAIN:
  3885. error = 0;
  3886. /* fall through */
  3887. default:
  3888. __put_lkb(ls, lkb);
  3889. goto out;
  3890. }
  3891. /* add this new lkb to the per-process list of locks */
  3892. spin_lock(&ua->proc->locks_spin);
  3893. hold_lkb(lkb);
  3894. list_add_tail(&lkb->lkb_ownqueue, &ua->proc->locks);
  3895. spin_unlock(&ua->proc->locks_spin);
  3896. out:
  3897. dlm_unlock_recovery(ls);
  3898. return error;
  3899. }
  3900. int dlm_user_convert(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
  3901. int mode, uint32_t flags, uint32_t lkid, char *lvb_in,
  3902. unsigned long timeout_cs)
  3903. {
  3904. struct dlm_lkb *lkb;
  3905. struct dlm_args args;
  3906. struct dlm_user_args *ua;
  3907. int error;
  3908. dlm_lock_recovery(ls);
  3909. error = find_lkb(ls, lkid, &lkb);
  3910. if (error)
  3911. goto out;
  3912. /* user can change the params on its lock when it converts it, or
  3913. add an lvb that didn't exist before */
  3914. ua = lkb->lkb_ua;
  3915. if (flags & DLM_LKF_VALBLK && !ua->lksb.sb_lvbptr) {
  3916. ua->lksb.sb_lvbptr = kzalloc(DLM_USER_LVB_LEN, GFP_NOFS);
  3917. if (!ua->lksb.sb_lvbptr) {
  3918. error = -ENOMEM;
  3919. goto out_put;
  3920. }
  3921. }
  3922. if (lvb_in && ua->lksb.sb_lvbptr)
  3923. memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
  3924. ua->xid = ua_tmp->xid;
  3925. ua->castparam = ua_tmp->castparam;
  3926. ua->castaddr = ua_tmp->castaddr;
  3927. ua->bastparam = ua_tmp->bastparam;
  3928. ua->bastaddr = ua_tmp->bastaddr;
  3929. ua->user_lksb = ua_tmp->user_lksb;
  3930. error = set_lock_args(mode, &ua->lksb, flags, 0, timeout_cs,
  3931. fake_astfn, ua, fake_bastfn, &args);
  3932. if (error)
  3933. goto out_put;
  3934. error = convert_lock(ls, lkb, &args);
  3935. if (error == -EINPROGRESS || error == -EAGAIN || error == -EDEADLK)
  3936. error = 0;
  3937. out_put:
  3938. dlm_put_lkb(lkb);
  3939. out:
  3940. dlm_unlock_recovery(ls);
  3941. kfree(ua_tmp);
  3942. return error;
  3943. }
  3944. int dlm_user_unlock(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
  3945. uint32_t flags, uint32_t lkid, char *lvb_in)
  3946. {
  3947. struct dlm_lkb *lkb;
  3948. struct dlm_args args;
  3949. struct dlm_user_args *ua;
  3950. int error;
  3951. dlm_lock_recovery(ls);
  3952. error = find_lkb(ls, lkid, &lkb);
  3953. if (error)
  3954. goto out;
  3955. ua = lkb->lkb_ua;
  3956. if (lvb_in && ua->lksb.sb_lvbptr)
  3957. memcpy(ua->lksb.sb_lvbptr, lvb_in, DLM_USER_LVB_LEN);
  3958. if (ua_tmp->castparam)
  3959. ua->castparam = ua_tmp->castparam;
  3960. ua->user_lksb = ua_tmp->user_lksb;
  3961. error = set_unlock_args(flags, ua, &args);
  3962. if (error)
  3963. goto out_put;
  3964. error = unlock_lock(ls, lkb, &args);
  3965. if (error == -DLM_EUNLOCK)
  3966. error = 0;
  3967. /* from validate_unlock_args() */
  3968. if (error == -EBUSY && (flags & DLM_LKF_FORCEUNLOCK))
  3969. error = 0;
  3970. if (error)
  3971. goto out_put;
  3972. spin_lock(&ua->proc->locks_spin);
  3973. /* dlm_user_add_cb() may have already taken lkb off the proc list */
  3974. if (!list_empty(&lkb->lkb_ownqueue))
  3975. list_move(&lkb->lkb_ownqueue, &ua->proc->unlocking);
  3976. spin_unlock(&ua->proc->locks_spin);
  3977. out_put:
  3978. dlm_put_lkb(lkb);
  3979. out:
  3980. dlm_unlock_recovery(ls);
  3981. kfree(ua_tmp);
  3982. return error;
  3983. }
  3984. int dlm_user_cancel(struct dlm_ls *ls, struct dlm_user_args *ua_tmp,
  3985. uint32_t flags, uint32_t lkid)
  3986. {
  3987. struct dlm_lkb *lkb;
  3988. struct dlm_args args;
  3989. struct dlm_user_args *ua;
  3990. int error;
  3991. dlm_lock_recovery(ls);
  3992. error = find_lkb(ls, lkid, &lkb);
  3993. if (error)
  3994. goto out;
  3995. ua = lkb->lkb_ua;
  3996. if (ua_tmp->castparam)
  3997. ua->castparam = ua_tmp->castparam;
  3998. ua->user_lksb = ua_tmp->user_lksb;
  3999. error = set_unlock_args(flags, ua, &args);
  4000. if (error)
  4001. goto out_put;
  4002. error = cancel_lock(ls, lkb, &args);
  4003. if (error == -DLM_ECANCEL)
  4004. error = 0;
  4005. /* from validate_unlock_args() */
  4006. if (error == -EBUSY)
  4007. error = 0;
  4008. out_put:
  4009. dlm_put_lkb(lkb);
  4010. out:
  4011. dlm_unlock_recovery(ls);
  4012. kfree(ua_tmp);
  4013. return error;
  4014. }
  4015. int dlm_user_deadlock(struct dlm_ls *ls, uint32_t flags, uint32_t lkid)
  4016. {
  4017. struct dlm_lkb *lkb;
  4018. struct dlm_args args;
  4019. struct dlm_user_args *ua;
  4020. struct dlm_rsb *r;
  4021. int error;
  4022. dlm_lock_recovery(ls);
  4023. error = find_lkb(ls, lkid, &lkb);
  4024. if (error)
  4025. goto out;
  4026. ua = lkb->lkb_ua;
  4027. error = set_unlock_args(flags, ua, &args);
  4028. if (error)
  4029. goto out_put;
  4030. /* same as cancel_lock(), but set DEADLOCK_CANCEL after lock_rsb */
  4031. r = lkb->lkb_resource;
  4032. hold_rsb(r);
  4033. lock_rsb(r);
  4034. error = validate_unlock_args(lkb, &args);
  4035. if (error)
  4036. goto out_r;
  4037. lkb->lkb_flags |= DLM_IFL_DEADLOCK_CANCEL;
  4038. error = _cancel_lock(r, lkb);
  4039. out_r:
  4040. unlock_rsb(r);
  4041. put_rsb(r);
  4042. if (error == -DLM_ECANCEL)
  4043. error = 0;
  4044. /* from validate_unlock_args() */
  4045. if (error == -EBUSY)
  4046. error = 0;
  4047. out_put:
  4048. dlm_put_lkb(lkb);
  4049. out:
  4050. dlm_unlock_recovery(ls);
  4051. return error;
  4052. }
  4053. /* lkb's that are removed from the waiters list by revert are just left on the
  4054. orphans list with the granted orphan locks, to be freed by purge */
  4055. static int orphan_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
  4056. {
  4057. struct dlm_args args;
  4058. int error;
  4059. hold_lkb(lkb);
  4060. mutex_lock(&ls->ls_orphans_mutex);
  4061. list_add_tail(&lkb->lkb_ownqueue, &ls->ls_orphans);
  4062. mutex_unlock(&ls->ls_orphans_mutex);
  4063. set_unlock_args(0, lkb->lkb_ua, &args);
  4064. error = cancel_lock(ls, lkb, &args);
  4065. if (error == -DLM_ECANCEL)
  4066. error = 0;
  4067. return error;
  4068. }
  4069. /* The force flag allows the unlock to go ahead even if the lkb isn't granted.
  4070. Regardless of what rsb queue the lock is on, it's removed and freed. */
  4071. static int unlock_proc_lock(struct dlm_ls *ls, struct dlm_lkb *lkb)
  4072. {
  4073. struct dlm_args args;
  4074. int error;
  4075. set_unlock_args(DLM_LKF_FORCEUNLOCK, lkb->lkb_ua, &args);
  4076. error = unlock_lock(ls, lkb, &args);
  4077. if (error == -DLM_EUNLOCK)
  4078. error = 0;
  4079. return error;
  4080. }
  4081. /* We have to release clear_proc_locks mutex before calling unlock_proc_lock()
  4082. (which does lock_rsb) due to deadlock with receiving a message that does
  4083. lock_rsb followed by dlm_user_add_cb() */
  4084. static struct dlm_lkb *del_proc_lock(struct dlm_ls *ls,
  4085. struct dlm_user_proc *proc)
  4086. {
  4087. struct dlm_lkb *lkb = NULL;
  4088. mutex_lock(&ls->ls_clear_proc_locks);
  4089. if (list_empty(&proc->locks))
  4090. goto out;
  4091. lkb = list_entry(proc->locks.next, struct dlm_lkb, lkb_ownqueue);
  4092. list_del_init(&lkb->lkb_ownqueue);
  4093. if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
  4094. lkb->lkb_flags |= DLM_IFL_ORPHAN;
  4095. else
  4096. lkb->lkb_flags |= DLM_IFL_DEAD;
  4097. out:
  4098. mutex_unlock(&ls->ls_clear_proc_locks);
  4099. return lkb;
  4100. }
  4101. /* The ls_clear_proc_locks mutex protects against dlm_user_add_cb() which
  4102. 1) references lkb->ua which we free here and 2) adds lkbs to proc->asts,
  4103. which we clear here. */
  4104. /* proc CLOSING flag is set so no more device_reads should look at proc->asts
  4105. list, and no more device_writes should add lkb's to proc->locks list; so we
  4106. shouldn't need to take asts_spin or locks_spin here. this assumes that
  4107. device reads/writes/closes are serialized -- FIXME: we may need to serialize
  4108. them ourself. */
  4109. void dlm_clear_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
  4110. {
  4111. struct dlm_lkb *lkb, *safe;
  4112. dlm_lock_recovery(ls);
  4113. while (1) {
  4114. lkb = del_proc_lock(ls, proc);
  4115. if (!lkb)
  4116. break;
  4117. del_timeout(lkb);
  4118. if (lkb->lkb_exflags & DLM_LKF_PERSISTENT)
  4119. orphan_proc_lock(ls, lkb);
  4120. else
  4121. unlock_proc_lock(ls, lkb);
  4122. /* this removes the reference for the proc->locks list
  4123. added by dlm_user_request, it may result in the lkb
  4124. being freed */
  4125. dlm_put_lkb(lkb);
  4126. }
  4127. mutex_lock(&ls->ls_clear_proc_locks);
  4128. /* in-progress unlocks */
  4129. list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
  4130. list_del_init(&lkb->lkb_ownqueue);
  4131. lkb->lkb_flags |= DLM_IFL_DEAD;
  4132. dlm_put_lkb(lkb);
  4133. }
  4134. list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_cb_list) {
  4135. memset(&lkb->lkb_callbacks, 0,
  4136. sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
  4137. list_del_init(&lkb->lkb_cb_list);
  4138. dlm_put_lkb(lkb);
  4139. }
  4140. mutex_unlock(&ls->ls_clear_proc_locks);
  4141. dlm_unlock_recovery(ls);
  4142. }
  4143. static void purge_proc_locks(struct dlm_ls *ls, struct dlm_user_proc *proc)
  4144. {
  4145. struct dlm_lkb *lkb, *safe;
  4146. while (1) {
  4147. lkb = NULL;
  4148. spin_lock(&proc->locks_spin);
  4149. if (!list_empty(&proc->locks)) {
  4150. lkb = list_entry(proc->locks.next, struct dlm_lkb,
  4151. lkb_ownqueue);
  4152. list_del_init(&lkb->lkb_ownqueue);
  4153. }
  4154. spin_unlock(&proc->locks_spin);
  4155. if (!lkb)
  4156. break;
  4157. lkb->lkb_flags |= DLM_IFL_DEAD;
  4158. unlock_proc_lock(ls, lkb);
  4159. dlm_put_lkb(lkb); /* ref from proc->locks list */
  4160. }
  4161. spin_lock(&proc->locks_spin);
  4162. list_for_each_entry_safe(lkb, safe, &proc->unlocking, lkb_ownqueue) {
  4163. list_del_init(&lkb->lkb_ownqueue);
  4164. lkb->lkb_flags |= DLM_IFL_DEAD;
  4165. dlm_put_lkb(lkb);
  4166. }
  4167. spin_unlock(&proc->locks_spin);
  4168. spin_lock(&proc->asts_spin);
  4169. list_for_each_entry_safe(lkb, safe, &proc->asts, lkb_cb_list) {
  4170. memset(&lkb->lkb_callbacks, 0,
  4171. sizeof(struct dlm_callback) * DLM_CALLBACKS_SIZE);
  4172. list_del_init(&lkb->lkb_cb_list);
  4173. dlm_put_lkb(lkb);
  4174. }
  4175. spin_unlock(&proc->asts_spin);
  4176. }
  4177. /* pid of 0 means purge all orphans */
  4178. static void do_purge(struct dlm_ls *ls, int nodeid, int pid)
  4179. {
  4180. struct dlm_lkb *lkb, *safe;
  4181. mutex_lock(&ls->ls_orphans_mutex);
  4182. list_for_each_entry_safe(lkb, safe, &ls->ls_orphans, lkb_ownqueue) {
  4183. if (pid && lkb->lkb_ownpid != pid)
  4184. continue;
  4185. unlock_proc_lock(ls, lkb);
  4186. list_del_init(&lkb->lkb_ownqueue);
  4187. dlm_put_lkb(lkb);
  4188. }
  4189. mutex_unlock(&ls->ls_orphans_mutex);
  4190. }
  4191. static int send_purge(struct dlm_ls *ls, int nodeid, int pid)
  4192. {
  4193. struct dlm_message *ms;
  4194. struct dlm_mhandle *mh;
  4195. int error;
  4196. error = _create_message(ls, sizeof(struct dlm_message), nodeid,
  4197. DLM_MSG_PURGE, &ms, &mh);
  4198. if (error)
  4199. return error;
  4200. ms->m_nodeid = nodeid;
  4201. ms->m_pid = pid;
  4202. return send_message(mh, ms);
  4203. }
  4204. int dlm_user_purge(struct dlm_ls *ls, struct dlm_user_proc *proc,
  4205. int nodeid, int pid)
  4206. {
  4207. int error = 0;
  4208. if (nodeid != dlm_our_nodeid()) {
  4209. error = send_purge(ls, nodeid, pid);
  4210. } else {
  4211. dlm_lock_recovery(ls);
  4212. if (pid == current->pid)
  4213. purge_proc_locks(ls, proc);
  4214. else
  4215. do_purge(ls, nodeid, pid);
  4216. dlm_unlock_recovery(ls);
  4217. }
  4218. return error;
  4219. }