lock.c 163 KB

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