lock.c 125 KB

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