lock.c 158 KB

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