lock.c 158 KB

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