dlmmaster.c 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * dlmmod.c
  5. *
  6. * standalone DLM module
  7. *
  8. * Copyright (C) 2004 Oracle. All rights reserved.
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2 of the License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this program; if not, write to the
  22. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  23. * Boston, MA 021110-1307, USA.
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/fs.h>
  28. #include <linux/types.h>
  29. #include <linux/slab.h>
  30. #include <linux/highmem.h>
  31. #include <linux/init.h>
  32. #include <linux/sysctl.h>
  33. #include <linux/random.h>
  34. #include <linux/blkdev.h>
  35. #include <linux/socket.h>
  36. #include <linux/inet.h>
  37. #include <linux/spinlock.h>
  38. #include <linux/delay.h>
  39. #include "cluster/heartbeat.h"
  40. #include "cluster/nodemanager.h"
  41. #include "cluster/tcp.h"
  42. #include "dlmapi.h"
  43. #include "dlmcommon.h"
  44. #include "dlmdomain.h"
  45. #include "dlmdebug.h"
  46. #define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER)
  47. #include "cluster/masklog.h"
  48. static void dlm_mle_node_down(struct dlm_ctxt *dlm,
  49. struct dlm_master_list_entry *mle,
  50. struct o2nm_node *node,
  51. int idx);
  52. static void dlm_mle_node_up(struct dlm_ctxt *dlm,
  53. struct dlm_master_list_entry *mle,
  54. struct o2nm_node *node,
  55. int idx);
  56. static void dlm_assert_master_worker(struct dlm_work_item *item, void *data);
  57. static int dlm_do_assert_master(struct dlm_ctxt *dlm,
  58. struct dlm_lock_resource *res,
  59. void *nodemap, u32 flags);
  60. static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data);
  61. static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
  62. struct dlm_master_list_entry *mle,
  63. const char *name,
  64. unsigned int namelen)
  65. {
  66. if (dlm != mle->dlm)
  67. return 0;
  68. if (namelen != mle->mnamelen ||
  69. memcmp(name, mle->mname, namelen) != 0)
  70. return 0;
  71. return 1;
  72. }
  73. static struct kmem_cache *dlm_lockres_cache;
  74. static struct kmem_cache *dlm_lockname_cache;
  75. static struct kmem_cache *dlm_mle_cache;
  76. static void dlm_mle_release(struct kref *kref);
  77. static void dlm_init_mle(struct dlm_master_list_entry *mle,
  78. enum dlm_mle_type type,
  79. struct dlm_ctxt *dlm,
  80. struct dlm_lock_resource *res,
  81. const char *name,
  82. unsigned int namelen);
  83. static void dlm_put_mle(struct dlm_master_list_entry *mle);
  84. static void __dlm_put_mle(struct dlm_master_list_entry *mle);
  85. static int dlm_find_mle(struct dlm_ctxt *dlm,
  86. struct dlm_master_list_entry **mle,
  87. char *name, unsigned int namelen);
  88. static int dlm_do_master_request(struct dlm_lock_resource *res,
  89. struct dlm_master_list_entry *mle, int to);
  90. static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
  91. struct dlm_lock_resource *res,
  92. struct dlm_master_list_entry *mle,
  93. int *blocked);
  94. static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
  95. struct dlm_lock_resource *res,
  96. struct dlm_master_list_entry *mle,
  97. int blocked);
  98. static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
  99. struct dlm_lock_resource *res,
  100. struct dlm_master_list_entry *mle,
  101. struct dlm_master_list_entry **oldmle,
  102. const char *name, unsigned int namelen,
  103. u8 new_master, u8 master);
  104. static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
  105. struct dlm_lock_resource *res);
  106. static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
  107. struct dlm_lock_resource *res);
  108. static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
  109. struct dlm_lock_resource *res,
  110. u8 target);
  111. static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
  112. struct dlm_lock_resource *res);
  113. int dlm_is_host_down(int errno)
  114. {
  115. switch (errno) {
  116. case -EBADF:
  117. case -ECONNREFUSED:
  118. case -ENOTCONN:
  119. case -ECONNRESET:
  120. case -EPIPE:
  121. case -EHOSTDOWN:
  122. case -EHOSTUNREACH:
  123. case -ETIMEDOUT:
  124. case -ECONNABORTED:
  125. case -ENETDOWN:
  126. case -ENETUNREACH:
  127. case -ENETRESET:
  128. case -ESHUTDOWN:
  129. case -ENOPROTOOPT:
  130. case -EINVAL: /* if returned from our tcp code,
  131. this means there is no socket */
  132. return 1;
  133. }
  134. return 0;
  135. }
  136. /*
  137. * MASTER LIST FUNCTIONS
  138. */
  139. /*
  140. * regarding master list entries and heartbeat callbacks:
  141. *
  142. * in order to avoid sleeping and allocation that occurs in
  143. * heartbeat, master list entries are simply attached to the
  144. * dlm's established heartbeat callbacks. the mle is attached
  145. * when it is created, and since the dlm->spinlock is held at
  146. * that time, any heartbeat event will be properly discovered
  147. * by the mle. the mle needs to be detached from the
  148. * dlm->mle_hb_events list as soon as heartbeat events are no
  149. * longer useful to the mle, and before the mle is freed.
  150. *
  151. * as a general rule, heartbeat events are no longer needed by
  152. * the mle once an "answer" regarding the lock master has been
  153. * received.
  154. */
  155. static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm,
  156. struct dlm_master_list_entry *mle)
  157. {
  158. assert_spin_locked(&dlm->spinlock);
  159. list_add_tail(&mle->hb_events, &dlm->mle_hb_events);
  160. }
  161. static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
  162. struct dlm_master_list_entry *mle)
  163. {
  164. if (!list_empty(&mle->hb_events))
  165. list_del_init(&mle->hb_events);
  166. }
  167. static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
  168. struct dlm_master_list_entry *mle)
  169. {
  170. spin_lock(&dlm->spinlock);
  171. __dlm_mle_detach_hb_events(dlm, mle);
  172. spin_unlock(&dlm->spinlock);
  173. }
  174. static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle)
  175. {
  176. struct dlm_ctxt *dlm;
  177. dlm = mle->dlm;
  178. assert_spin_locked(&dlm->spinlock);
  179. assert_spin_locked(&dlm->master_lock);
  180. mle->inuse++;
  181. kref_get(&mle->mle_refs);
  182. }
  183. static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle)
  184. {
  185. struct dlm_ctxt *dlm;
  186. dlm = mle->dlm;
  187. spin_lock(&dlm->spinlock);
  188. spin_lock(&dlm->master_lock);
  189. mle->inuse--;
  190. __dlm_put_mle(mle);
  191. spin_unlock(&dlm->master_lock);
  192. spin_unlock(&dlm->spinlock);
  193. }
  194. /* remove from list and free */
  195. static void __dlm_put_mle(struct dlm_master_list_entry *mle)
  196. {
  197. struct dlm_ctxt *dlm;
  198. dlm = mle->dlm;
  199. assert_spin_locked(&dlm->spinlock);
  200. assert_spin_locked(&dlm->master_lock);
  201. if (!atomic_read(&mle->mle_refs.refcount)) {
  202. /* this may or may not crash, but who cares.
  203. * it's a BUG. */
  204. mlog(ML_ERROR, "bad mle: %p\n", mle);
  205. dlm_print_one_mle(mle);
  206. BUG();
  207. } else
  208. kref_put(&mle->mle_refs, dlm_mle_release);
  209. }
  210. /* must not have any spinlocks coming in */
  211. static void dlm_put_mle(struct dlm_master_list_entry *mle)
  212. {
  213. struct dlm_ctxt *dlm;
  214. dlm = mle->dlm;
  215. spin_lock(&dlm->spinlock);
  216. spin_lock(&dlm->master_lock);
  217. __dlm_put_mle(mle);
  218. spin_unlock(&dlm->master_lock);
  219. spin_unlock(&dlm->spinlock);
  220. }
  221. static inline void dlm_get_mle(struct dlm_master_list_entry *mle)
  222. {
  223. kref_get(&mle->mle_refs);
  224. }
  225. static void dlm_init_mle(struct dlm_master_list_entry *mle,
  226. enum dlm_mle_type type,
  227. struct dlm_ctxt *dlm,
  228. struct dlm_lock_resource *res,
  229. const char *name,
  230. unsigned int namelen)
  231. {
  232. assert_spin_locked(&dlm->spinlock);
  233. mle->dlm = dlm;
  234. mle->type = type;
  235. INIT_HLIST_NODE(&mle->master_hash_node);
  236. INIT_LIST_HEAD(&mle->hb_events);
  237. memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
  238. spin_lock_init(&mle->spinlock);
  239. init_waitqueue_head(&mle->wq);
  240. atomic_set(&mle->woken, 0);
  241. kref_init(&mle->mle_refs);
  242. memset(mle->response_map, 0, sizeof(mle->response_map));
  243. mle->master = O2NM_MAX_NODES;
  244. mle->new_master = O2NM_MAX_NODES;
  245. mle->inuse = 0;
  246. BUG_ON(mle->type != DLM_MLE_BLOCK &&
  247. mle->type != DLM_MLE_MASTER &&
  248. mle->type != DLM_MLE_MIGRATION);
  249. if (mle->type == DLM_MLE_MASTER) {
  250. BUG_ON(!res);
  251. mle->mleres = res;
  252. memcpy(mle->mname, res->lockname.name, res->lockname.len);
  253. mle->mnamelen = res->lockname.len;
  254. mle->mnamehash = res->lockname.hash;
  255. } else {
  256. BUG_ON(!name);
  257. mle->mleres = NULL;
  258. memcpy(mle->mname, name, namelen);
  259. mle->mnamelen = namelen;
  260. mle->mnamehash = dlm_lockid_hash(name, namelen);
  261. }
  262. atomic_inc(&dlm->mle_tot_count[mle->type]);
  263. atomic_inc(&dlm->mle_cur_count[mle->type]);
  264. /* copy off the node_map and register hb callbacks on our copy */
  265. memcpy(mle->node_map, dlm->domain_map, sizeof(mle->node_map));
  266. memcpy(mle->vote_map, dlm->domain_map, sizeof(mle->vote_map));
  267. clear_bit(dlm->node_num, mle->vote_map);
  268. clear_bit(dlm->node_num, mle->node_map);
  269. /* attach the mle to the domain node up/down events */
  270. __dlm_mle_attach_hb_events(dlm, mle);
  271. }
  272. void __dlm_unlink_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle)
  273. {
  274. assert_spin_locked(&dlm->spinlock);
  275. assert_spin_locked(&dlm->master_lock);
  276. if (!hlist_unhashed(&mle->master_hash_node))
  277. hlist_del_init(&mle->master_hash_node);
  278. }
  279. void __dlm_insert_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle)
  280. {
  281. struct hlist_head *bucket;
  282. assert_spin_locked(&dlm->master_lock);
  283. bucket = dlm_master_hash(dlm, mle->mnamehash);
  284. hlist_add_head(&mle->master_hash_node, bucket);
  285. }
  286. /* returns 1 if found, 0 if not */
  287. static int dlm_find_mle(struct dlm_ctxt *dlm,
  288. struct dlm_master_list_entry **mle,
  289. char *name, unsigned int namelen)
  290. {
  291. struct dlm_master_list_entry *tmpmle;
  292. struct hlist_head *bucket;
  293. unsigned int hash;
  294. assert_spin_locked(&dlm->master_lock);
  295. hash = dlm_lockid_hash(name, namelen);
  296. bucket = dlm_master_hash(dlm, hash);
  297. hlist_for_each_entry(tmpmle, bucket, master_hash_node) {
  298. if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
  299. continue;
  300. dlm_get_mle(tmpmle);
  301. *mle = tmpmle;
  302. return 1;
  303. }
  304. return 0;
  305. }
  306. void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
  307. {
  308. struct dlm_master_list_entry *mle;
  309. assert_spin_locked(&dlm->spinlock);
  310. list_for_each_entry(mle, &dlm->mle_hb_events, hb_events) {
  311. if (node_up)
  312. dlm_mle_node_up(dlm, mle, NULL, idx);
  313. else
  314. dlm_mle_node_down(dlm, mle, NULL, idx);
  315. }
  316. }
  317. static void dlm_mle_node_down(struct dlm_ctxt *dlm,
  318. struct dlm_master_list_entry *mle,
  319. struct o2nm_node *node, int idx)
  320. {
  321. spin_lock(&mle->spinlock);
  322. if (!test_bit(idx, mle->node_map))
  323. mlog(0, "node %u already removed from nodemap!\n", idx);
  324. else
  325. clear_bit(idx, mle->node_map);
  326. spin_unlock(&mle->spinlock);
  327. }
  328. static void dlm_mle_node_up(struct dlm_ctxt *dlm,
  329. struct dlm_master_list_entry *mle,
  330. struct o2nm_node *node, int idx)
  331. {
  332. spin_lock(&mle->spinlock);
  333. if (test_bit(idx, mle->node_map))
  334. mlog(0, "node %u already in node map!\n", idx);
  335. else
  336. set_bit(idx, mle->node_map);
  337. spin_unlock(&mle->spinlock);
  338. }
  339. int dlm_init_mle_cache(void)
  340. {
  341. dlm_mle_cache = kmem_cache_create("o2dlm_mle",
  342. sizeof(struct dlm_master_list_entry),
  343. 0, SLAB_HWCACHE_ALIGN,
  344. NULL);
  345. if (dlm_mle_cache == NULL)
  346. return -ENOMEM;
  347. return 0;
  348. }
  349. void dlm_destroy_mle_cache(void)
  350. {
  351. if (dlm_mle_cache)
  352. kmem_cache_destroy(dlm_mle_cache);
  353. }
  354. static void dlm_mle_release(struct kref *kref)
  355. {
  356. struct dlm_master_list_entry *mle;
  357. struct dlm_ctxt *dlm;
  358. mle = container_of(kref, struct dlm_master_list_entry, mle_refs);
  359. dlm = mle->dlm;
  360. assert_spin_locked(&dlm->spinlock);
  361. assert_spin_locked(&dlm->master_lock);
  362. mlog(0, "Releasing mle for %.*s, type %d\n", mle->mnamelen, mle->mname,
  363. mle->type);
  364. /* remove from list if not already */
  365. __dlm_unlink_mle(dlm, mle);
  366. /* detach the mle from the domain node up/down events */
  367. __dlm_mle_detach_hb_events(dlm, mle);
  368. atomic_dec(&dlm->mle_cur_count[mle->type]);
  369. /* NOTE: kfree under spinlock here.
  370. * if this is bad, we can move this to a freelist. */
  371. kmem_cache_free(dlm_mle_cache, mle);
  372. }
  373. /*
  374. * LOCK RESOURCE FUNCTIONS
  375. */
  376. int dlm_init_master_caches(void)
  377. {
  378. dlm_lockres_cache = kmem_cache_create("o2dlm_lockres",
  379. sizeof(struct dlm_lock_resource),
  380. 0, SLAB_HWCACHE_ALIGN, NULL);
  381. if (!dlm_lockres_cache)
  382. goto bail;
  383. dlm_lockname_cache = kmem_cache_create("o2dlm_lockname",
  384. DLM_LOCKID_NAME_MAX, 0,
  385. SLAB_HWCACHE_ALIGN, NULL);
  386. if (!dlm_lockname_cache)
  387. goto bail;
  388. return 0;
  389. bail:
  390. dlm_destroy_master_caches();
  391. return -ENOMEM;
  392. }
  393. void dlm_destroy_master_caches(void)
  394. {
  395. if (dlm_lockname_cache) {
  396. kmem_cache_destroy(dlm_lockname_cache);
  397. dlm_lockname_cache = NULL;
  398. }
  399. if (dlm_lockres_cache) {
  400. kmem_cache_destroy(dlm_lockres_cache);
  401. dlm_lockres_cache = NULL;
  402. }
  403. }
  404. static void dlm_lockres_release(struct kref *kref)
  405. {
  406. struct dlm_lock_resource *res;
  407. struct dlm_ctxt *dlm;
  408. res = container_of(kref, struct dlm_lock_resource, refs);
  409. dlm = res->dlm;
  410. /* This should not happen -- all lockres' have a name
  411. * associated with them at init time. */
  412. BUG_ON(!res->lockname.name);
  413. mlog(0, "destroying lockres %.*s\n", res->lockname.len,
  414. res->lockname.name);
  415. spin_lock(&dlm->track_lock);
  416. if (!list_empty(&res->tracking))
  417. list_del_init(&res->tracking);
  418. else {
  419. mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
  420. res->lockname.len, res->lockname.name);
  421. dlm_print_one_lock_resource(res);
  422. }
  423. spin_unlock(&dlm->track_lock);
  424. atomic_dec(&dlm->res_cur_count);
  425. if (!hlist_unhashed(&res->hash_node) ||
  426. !list_empty(&res->granted) ||
  427. !list_empty(&res->converting) ||
  428. !list_empty(&res->blocked) ||
  429. !list_empty(&res->dirty) ||
  430. !list_empty(&res->recovering) ||
  431. !list_empty(&res->purge)) {
  432. mlog(ML_ERROR,
  433. "Going to BUG for resource %.*s."
  434. " We're on a list! [%c%c%c%c%c%c%c]\n",
  435. res->lockname.len, res->lockname.name,
  436. !hlist_unhashed(&res->hash_node) ? 'H' : ' ',
  437. !list_empty(&res->granted) ? 'G' : ' ',
  438. !list_empty(&res->converting) ? 'C' : ' ',
  439. !list_empty(&res->blocked) ? 'B' : ' ',
  440. !list_empty(&res->dirty) ? 'D' : ' ',
  441. !list_empty(&res->recovering) ? 'R' : ' ',
  442. !list_empty(&res->purge) ? 'P' : ' ');
  443. dlm_print_one_lock_resource(res);
  444. }
  445. /* By the time we're ready to blow this guy away, we shouldn't
  446. * be on any lists. */
  447. BUG_ON(!hlist_unhashed(&res->hash_node));
  448. BUG_ON(!list_empty(&res->granted));
  449. BUG_ON(!list_empty(&res->converting));
  450. BUG_ON(!list_empty(&res->blocked));
  451. BUG_ON(!list_empty(&res->dirty));
  452. BUG_ON(!list_empty(&res->recovering));
  453. BUG_ON(!list_empty(&res->purge));
  454. kmem_cache_free(dlm_lockname_cache, (void *)res->lockname.name);
  455. kmem_cache_free(dlm_lockres_cache, res);
  456. }
  457. void dlm_lockres_put(struct dlm_lock_resource *res)
  458. {
  459. kref_put(&res->refs, dlm_lockres_release);
  460. }
  461. static void dlm_init_lockres(struct dlm_ctxt *dlm,
  462. struct dlm_lock_resource *res,
  463. const char *name, unsigned int namelen)
  464. {
  465. char *qname;
  466. /* If we memset here, we lose our reference to the kmalloc'd
  467. * res->lockname.name, so be sure to init every field
  468. * correctly! */
  469. qname = (char *) res->lockname.name;
  470. memcpy(qname, name, namelen);
  471. res->lockname.len = namelen;
  472. res->lockname.hash = dlm_lockid_hash(name, namelen);
  473. init_waitqueue_head(&res->wq);
  474. spin_lock_init(&res->spinlock);
  475. INIT_HLIST_NODE(&res->hash_node);
  476. INIT_LIST_HEAD(&res->granted);
  477. INIT_LIST_HEAD(&res->converting);
  478. INIT_LIST_HEAD(&res->blocked);
  479. INIT_LIST_HEAD(&res->dirty);
  480. INIT_LIST_HEAD(&res->recovering);
  481. INIT_LIST_HEAD(&res->purge);
  482. INIT_LIST_HEAD(&res->tracking);
  483. atomic_set(&res->asts_reserved, 0);
  484. res->migration_pending = 0;
  485. res->inflight_locks = 0;
  486. res->inflight_assert_workers = 0;
  487. res->dlm = dlm;
  488. kref_init(&res->refs);
  489. atomic_inc(&dlm->res_tot_count);
  490. atomic_inc(&dlm->res_cur_count);
  491. /* just for consistency */
  492. spin_lock(&res->spinlock);
  493. dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
  494. spin_unlock(&res->spinlock);
  495. res->state = DLM_LOCK_RES_IN_PROGRESS;
  496. res->last_used = 0;
  497. spin_lock(&dlm->spinlock);
  498. list_add_tail(&res->tracking, &dlm->tracking_list);
  499. spin_unlock(&dlm->spinlock);
  500. memset(res->lvb, 0, DLM_LVB_LEN);
  501. memset(res->refmap, 0, sizeof(res->refmap));
  502. }
  503. struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
  504. const char *name,
  505. unsigned int namelen)
  506. {
  507. struct dlm_lock_resource *res = NULL;
  508. res = kmem_cache_zalloc(dlm_lockres_cache, GFP_NOFS);
  509. if (!res)
  510. goto error;
  511. res->lockname.name = kmem_cache_zalloc(dlm_lockname_cache, GFP_NOFS);
  512. if (!res->lockname.name)
  513. goto error;
  514. dlm_init_lockres(dlm, res, name, namelen);
  515. return res;
  516. error:
  517. if (res)
  518. kmem_cache_free(dlm_lockres_cache, res);
  519. return NULL;
  520. }
  521. void dlm_lockres_set_refmap_bit(struct dlm_ctxt *dlm,
  522. struct dlm_lock_resource *res, int bit)
  523. {
  524. assert_spin_locked(&res->spinlock);
  525. mlog(0, "res %.*s, set node %u, %ps()\n", res->lockname.len,
  526. res->lockname.name, bit, __builtin_return_address(0));
  527. set_bit(bit, res->refmap);
  528. }
  529. void dlm_lockres_clear_refmap_bit(struct dlm_ctxt *dlm,
  530. struct dlm_lock_resource *res, int bit)
  531. {
  532. assert_spin_locked(&res->spinlock);
  533. mlog(0, "res %.*s, clr node %u, %ps()\n", res->lockname.len,
  534. res->lockname.name, bit, __builtin_return_address(0));
  535. clear_bit(bit, res->refmap);
  536. }
  537. static void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
  538. struct dlm_lock_resource *res)
  539. {
  540. res->inflight_locks++;
  541. mlog(0, "%s: res %.*s, inflight++: now %u, %ps()\n", dlm->name,
  542. res->lockname.len, res->lockname.name, res->inflight_locks,
  543. __builtin_return_address(0));
  544. }
  545. void dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
  546. struct dlm_lock_resource *res)
  547. {
  548. assert_spin_locked(&res->spinlock);
  549. __dlm_lockres_grab_inflight_ref(dlm, res);
  550. }
  551. void dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
  552. struct dlm_lock_resource *res)
  553. {
  554. assert_spin_locked(&res->spinlock);
  555. BUG_ON(res->inflight_locks == 0);
  556. res->inflight_locks--;
  557. mlog(0, "%s: res %.*s, inflight--: now %u, %ps()\n", dlm->name,
  558. res->lockname.len, res->lockname.name, res->inflight_locks,
  559. __builtin_return_address(0));
  560. wake_up(&res->wq);
  561. }
  562. void __dlm_lockres_grab_inflight_worker(struct dlm_ctxt *dlm,
  563. struct dlm_lock_resource *res)
  564. {
  565. assert_spin_locked(&res->spinlock);
  566. res->inflight_assert_workers++;
  567. mlog(0, "%s:%.*s: inflight assert worker++: now %u\n",
  568. dlm->name, res->lockname.len, res->lockname.name,
  569. res->inflight_assert_workers);
  570. }
  571. static void dlm_lockres_grab_inflight_worker(struct dlm_ctxt *dlm,
  572. struct dlm_lock_resource *res)
  573. {
  574. spin_lock(&res->spinlock);
  575. __dlm_lockres_grab_inflight_worker(dlm, res);
  576. spin_unlock(&res->spinlock);
  577. }
  578. static void __dlm_lockres_drop_inflight_worker(struct dlm_ctxt *dlm,
  579. struct dlm_lock_resource *res)
  580. {
  581. assert_spin_locked(&res->spinlock);
  582. BUG_ON(res->inflight_assert_workers == 0);
  583. res->inflight_assert_workers--;
  584. mlog(0, "%s:%.*s: inflight assert worker--: now %u\n",
  585. dlm->name, res->lockname.len, res->lockname.name,
  586. res->inflight_assert_workers);
  587. }
  588. static void dlm_lockres_drop_inflight_worker(struct dlm_ctxt *dlm,
  589. struct dlm_lock_resource *res)
  590. {
  591. spin_lock(&res->spinlock);
  592. __dlm_lockres_drop_inflight_worker(dlm, res);
  593. spin_unlock(&res->spinlock);
  594. }
  595. /*
  596. * lookup a lock resource by name.
  597. * may already exist in the hashtable.
  598. * lockid is null terminated
  599. *
  600. * if not, allocate enough for the lockres and for
  601. * the temporary structure used in doing the mastering.
  602. *
  603. * also, do a lookup in the dlm->master_list to see
  604. * if another node has begun mastering the same lock.
  605. * if so, there should be a block entry in there
  606. * for this name, and we should *not* attempt to master
  607. * the lock here. need to wait around for that node
  608. * to assert_master (or die).
  609. *
  610. */
  611. struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
  612. const char *lockid,
  613. int namelen,
  614. int flags)
  615. {
  616. struct dlm_lock_resource *tmpres=NULL, *res=NULL;
  617. struct dlm_master_list_entry *mle = NULL;
  618. struct dlm_master_list_entry *alloc_mle = NULL;
  619. int blocked = 0;
  620. int ret, nodenum;
  621. struct dlm_node_iter iter;
  622. unsigned int hash;
  623. int tries = 0;
  624. int bit, wait_on_recovery = 0;
  625. BUG_ON(!lockid);
  626. hash = dlm_lockid_hash(lockid, namelen);
  627. mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
  628. lookup:
  629. spin_lock(&dlm->spinlock);
  630. tmpres = __dlm_lookup_lockres_full(dlm, lockid, namelen, hash);
  631. if (tmpres) {
  632. spin_unlock(&dlm->spinlock);
  633. spin_lock(&tmpres->spinlock);
  634. /*
  635. * Right after dlm spinlock was released, dlm_thread could have
  636. * purged the lockres. Check if lockres got unhashed. If so
  637. * start over.
  638. */
  639. if (hlist_unhashed(&tmpres->hash_node)) {
  640. spin_unlock(&tmpres->spinlock);
  641. dlm_lockres_put(tmpres);
  642. tmpres = NULL;
  643. goto lookup;
  644. }
  645. /* Wait on the thread that is mastering the resource */
  646. if (tmpres->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
  647. __dlm_wait_on_lockres(tmpres);
  648. BUG_ON(tmpres->owner == DLM_LOCK_RES_OWNER_UNKNOWN);
  649. spin_unlock(&tmpres->spinlock);
  650. dlm_lockres_put(tmpres);
  651. tmpres = NULL;
  652. goto lookup;
  653. }
  654. /* Wait on the resource purge to complete before continuing */
  655. if (tmpres->state & DLM_LOCK_RES_DROPPING_REF) {
  656. BUG_ON(tmpres->owner == dlm->node_num);
  657. __dlm_wait_on_lockres_flags(tmpres,
  658. DLM_LOCK_RES_DROPPING_REF);
  659. spin_unlock(&tmpres->spinlock);
  660. dlm_lockres_put(tmpres);
  661. tmpres = NULL;
  662. goto lookup;
  663. }
  664. /* Grab inflight ref to pin the resource */
  665. dlm_lockres_grab_inflight_ref(dlm, tmpres);
  666. spin_unlock(&tmpres->spinlock);
  667. if (res)
  668. dlm_lockres_put(res);
  669. res = tmpres;
  670. goto leave;
  671. }
  672. if (!res) {
  673. spin_unlock(&dlm->spinlock);
  674. mlog(0, "allocating a new resource\n");
  675. /* nothing found and we need to allocate one. */
  676. alloc_mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
  677. if (!alloc_mle)
  678. goto leave;
  679. res = dlm_new_lockres(dlm, lockid, namelen);
  680. if (!res)
  681. goto leave;
  682. goto lookup;
  683. }
  684. mlog(0, "no lockres found, allocated our own: %p\n", res);
  685. if (flags & LKM_LOCAL) {
  686. /* caller knows it's safe to assume it's not mastered elsewhere
  687. * DONE! return right away */
  688. spin_lock(&res->spinlock);
  689. dlm_change_lockres_owner(dlm, res, dlm->node_num);
  690. __dlm_insert_lockres(dlm, res);
  691. dlm_lockres_grab_inflight_ref(dlm, res);
  692. spin_unlock(&res->spinlock);
  693. spin_unlock(&dlm->spinlock);
  694. /* lockres still marked IN_PROGRESS */
  695. goto wake_waiters;
  696. }
  697. /* check master list to see if another node has started mastering it */
  698. spin_lock(&dlm->master_lock);
  699. /* if we found a block, wait for lock to be mastered by another node */
  700. blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
  701. if (blocked) {
  702. int mig;
  703. if (mle->type == DLM_MLE_MASTER) {
  704. mlog(ML_ERROR, "master entry for nonexistent lock!\n");
  705. BUG();
  706. }
  707. mig = (mle->type == DLM_MLE_MIGRATION);
  708. /* if there is a migration in progress, let the migration
  709. * finish before continuing. we can wait for the absence
  710. * of the MIGRATION mle: either the migrate finished or
  711. * one of the nodes died and the mle was cleaned up.
  712. * if there is a BLOCK here, but it already has a master
  713. * set, we are too late. the master does not have a ref
  714. * for us in the refmap. detach the mle and drop it.
  715. * either way, go back to the top and start over. */
  716. if (mig || mle->master != O2NM_MAX_NODES) {
  717. BUG_ON(mig && mle->master == dlm->node_num);
  718. /* we arrived too late. the master does not
  719. * have a ref for us. retry. */
  720. mlog(0, "%s:%.*s: late on %s\n",
  721. dlm->name, namelen, lockid,
  722. mig ? "MIGRATION" : "BLOCK");
  723. spin_unlock(&dlm->master_lock);
  724. spin_unlock(&dlm->spinlock);
  725. /* master is known, detach */
  726. if (!mig)
  727. dlm_mle_detach_hb_events(dlm, mle);
  728. dlm_put_mle(mle);
  729. mle = NULL;
  730. /* this is lame, but we can't wait on either
  731. * the mle or lockres waitqueue here */
  732. if (mig)
  733. msleep(100);
  734. goto lookup;
  735. }
  736. } else {
  737. /* go ahead and try to master lock on this node */
  738. mle = alloc_mle;
  739. /* make sure this does not get freed below */
  740. alloc_mle = NULL;
  741. dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0);
  742. set_bit(dlm->node_num, mle->maybe_map);
  743. __dlm_insert_mle(dlm, mle);
  744. /* still holding the dlm spinlock, check the recovery map
  745. * to see if there are any nodes that still need to be
  746. * considered. these will not appear in the mle nodemap
  747. * but they might own this lockres. wait on them. */
  748. bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
  749. if (bit < O2NM_MAX_NODES) {
  750. mlog(0, "%s: res %.*s, At least one node (%d) "
  751. "to recover before lock mastery can begin\n",
  752. dlm->name, namelen, (char *)lockid, bit);
  753. wait_on_recovery = 1;
  754. }
  755. }
  756. /* at this point there is either a DLM_MLE_BLOCK or a
  757. * DLM_MLE_MASTER on the master list, so it's safe to add the
  758. * lockres to the hashtable. anyone who finds the lock will
  759. * still have to wait on the IN_PROGRESS. */
  760. /* finally add the lockres to its hash bucket */
  761. __dlm_insert_lockres(dlm, res);
  762. /* since this lockres is new it doesn't not require the spinlock */
  763. __dlm_lockres_grab_inflight_ref(dlm, res);
  764. /* get an extra ref on the mle in case this is a BLOCK
  765. * if so, the creator of the BLOCK may try to put the last
  766. * ref at this time in the assert master handler, so we
  767. * need an extra one to keep from a bad ptr deref. */
  768. dlm_get_mle_inuse(mle);
  769. spin_unlock(&dlm->master_lock);
  770. spin_unlock(&dlm->spinlock);
  771. redo_request:
  772. while (wait_on_recovery) {
  773. /* any cluster changes that occurred after dropping the
  774. * dlm spinlock would be detectable be a change on the mle,
  775. * so we only need to clear out the recovery map once. */
  776. if (dlm_is_recovery_lock(lockid, namelen)) {
  777. mlog(0, "%s: Recovery map is not empty, but must "
  778. "master $RECOVERY lock now\n", dlm->name);
  779. if (!dlm_pre_master_reco_lockres(dlm, res))
  780. wait_on_recovery = 0;
  781. else {
  782. mlog(0, "%s: waiting 500ms for heartbeat state "
  783. "change\n", dlm->name);
  784. msleep(500);
  785. }
  786. continue;
  787. }
  788. dlm_kick_recovery_thread(dlm);
  789. msleep(1000);
  790. dlm_wait_for_recovery(dlm);
  791. spin_lock(&dlm->spinlock);
  792. bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
  793. if (bit < O2NM_MAX_NODES) {
  794. mlog(0, "%s: res %.*s, At least one node (%d) "
  795. "to recover before lock mastery can begin\n",
  796. dlm->name, namelen, (char *)lockid, bit);
  797. wait_on_recovery = 1;
  798. } else
  799. wait_on_recovery = 0;
  800. spin_unlock(&dlm->spinlock);
  801. if (wait_on_recovery)
  802. dlm_wait_for_node_recovery(dlm, bit, 10000);
  803. }
  804. /* must wait for lock to be mastered elsewhere */
  805. if (blocked)
  806. goto wait;
  807. ret = -EINVAL;
  808. dlm_node_iter_init(mle->vote_map, &iter);
  809. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  810. ret = dlm_do_master_request(res, mle, nodenum);
  811. if (ret < 0)
  812. mlog_errno(ret);
  813. if (mle->master != O2NM_MAX_NODES) {
  814. /* found a master ! */
  815. if (mle->master <= nodenum)
  816. break;
  817. /* if our master request has not reached the master
  818. * yet, keep going until it does. this is how the
  819. * master will know that asserts are needed back to
  820. * the lower nodes. */
  821. mlog(0, "%s: res %.*s, Requests only up to %u but "
  822. "master is %u, keep going\n", dlm->name, namelen,
  823. lockid, nodenum, mle->master);
  824. }
  825. }
  826. wait:
  827. /* keep going until the response map includes all nodes */
  828. ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
  829. if (ret < 0) {
  830. wait_on_recovery = 1;
  831. mlog(0, "%s: res %.*s, Node map changed, redo the master "
  832. "request now, blocked=%d\n", dlm->name, res->lockname.len,
  833. res->lockname.name, blocked);
  834. if (++tries > 20) {
  835. mlog(ML_ERROR, "%s: res %.*s, Spinning on "
  836. "dlm_wait_for_lock_mastery, blocked = %d\n",
  837. dlm->name, res->lockname.len,
  838. res->lockname.name, blocked);
  839. dlm_print_one_lock_resource(res);
  840. dlm_print_one_mle(mle);
  841. tries = 0;
  842. }
  843. goto redo_request;
  844. }
  845. mlog(0, "%s: res %.*s, Mastered by %u\n", dlm->name, res->lockname.len,
  846. res->lockname.name, res->owner);
  847. /* make sure we never continue without this */
  848. BUG_ON(res->owner == O2NM_MAX_NODES);
  849. /* master is known, detach if not already detached */
  850. dlm_mle_detach_hb_events(dlm, mle);
  851. dlm_put_mle(mle);
  852. /* put the extra ref */
  853. dlm_put_mle_inuse(mle);
  854. wake_waiters:
  855. spin_lock(&res->spinlock);
  856. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  857. spin_unlock(&res->spinlock);
  858. wake_up(&res->wq);
  859. leave:
  860. /* need to free the unused mle */
  861. if (alloc_mle)
  862. kmem_cache_free(dlm_mle_cache, alloc_mle);
  863. return res;
  864. }
  865. #define DLM_MASTERY_TIMEOUT_MS 5000
  866. static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
  867. struct dlm_lock_resource *res,
  868. struct dlm_master_list_entry *mle,
  869. int *blocked)
  870. {
  871. u8 m;
  872. int ret, bit;
  873. int map_changed, voting_done;
  874. int assert, sleep;
  875. recheck:
  876. ret = 0;
  877. assert = 0;
  878. /* check if another node has already become the owner */
  879. spin_lock(&res->spinlock);
  880. if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
  881. mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name,
  882. res->lockname.len, res->lockname.name, res->owner);
  883. spin_unlock(&res->spinlock);
  884. /* this will cause the master to re-assert across
  885. * the whole cluster, freeing up mles */
  886. if (res->owner != dlm->node_num) {
  887. ret = dlm_do_master_request(res, mle, res->owner);
  888. if (ret < 0) {
  889. /* give recovery a chance to run */
  890. mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
  891. msleep(500);
  892. goto recheck;
  893. }
  894. }
  895. ret = 0;
  896. goto leave;
  897. }
  898. spin_unlock(&res->spinlock);
  899. spin_lock(&mle->spinlock);
  900. m = mle->master;
  901. map_changed = (memcmp(mle->vote_map, mle->node_map,
  902. sizeof(mle->vote_map)) != 0);
  903. voting_done = (memcmp(mle->vote_map, mle->response_map,
  904. sizeof(mle->vote_map)) == 0);
  905. /* restart if we hit any errors */
  906. if (map_changed) {
  907. int b;
  908. mlog(0, "%s: %.*s: node map changed, restarting\n",
  909. dlm->name, res->lockname.len, res->lockname.name);
  910. ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked);
  911. b = (mle->type == DLM_MLE_BLOCK);
  912. if ((*blocked && !b) || (!*blocked && b)) {
  913. mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
  914. dlm->name, res->lockname.len, res->lockname.name,
  915. *blocked, b);
  916. *blocked = b;
  917. }
  918. spin_unlock(&mle->spinlock);
  919. if (ret < 0) {
  920. mlog_errno(ret);
  921. goto leave;
  922. }
  923. mlog(0, "%s:%.*s: restart lock mastery succeeded, "
  924. "rechecking now\n", dlm->name, res->lockname.len,
  925. res->lockname.name);
  926. goto recheck;
  927. } else {
  928. if (!voting_done) {
  929. mlog(0, "map not changed and voting not done "
  930. "for %s:%.*s\n", dlm->name, res->lockname.len,
  931. res->lockname.name);
  932. }
  933. }
  934. if (m != O2NM_MAX_NODES) {
  935. /* another node has done an assert!
  936. * all done! */
  937. sleep = 0;
  938. } else {
  939. sleep = 1;
  940. /* have all nodes responded? */
  941. if (voting_done && !*blocked) {
  942. bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
  943. if (dlm->node_num <= bit) {
  944. /* my node number is lowest.
  945. * now tell other nodes that I am
  946. * mastering this. */
  947. mle->master = dlm->node_num;
  948. /* ref was grabbed in get_lock_resource
  949. * will be dropped in dlmlock_master */
  950. assert = 1;
  951. sleep = 0;
  952. }
  953. /* if voting is done, but we have not received
  954. * an assert master yet, we must sleep */
  955. }
  956. }
  957. spin_unlock(&mle->spinlock);
  958. /* sleep if we haven't finished voting yet */
  959. if (sleep) {
  960. unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS);
  961. /*
  962. if (atomic_read(&mle->mle_refs.refcount) < 2)
  963. mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
  964. atomic_read(&mle->mle_refs.refcount),
  965. res->lockname.len, res->lockname.name);
  966. */
  967. atomic_set(&mle->woken, 0);
  968. (void)wait_event_timeout(mle->wq,
  969. (atomic_read(&mle->woken) == 1),
  970. timeo);
  971. if (res->owner == O2NM_MAX_NODES) {
  972. mlog(0, "%s:%.*s: waiting again\n", dlm->name,
  973. res->lockname.len, res->lockname.name);
  974. goto recheck;
  975. }
  976. mlog(0, "done waiting, master is %u\n", res->owner);
  977. ret = 0;
  978. goto leave;
  979. }
  980. ret = 0; /* done */
  981. if (assert) {
  982. m = dlm->node_num;
  983. mlog(0, "about to master %.*s here, this=%u\n",
  984. res->lockname.len, res->lockname.name, m);
  985. ret = dlm_do_assert_master(dlm, res, mle->vote_map, 0);
  986. if (ret) {
  987. /* This is a failure in the network path,
  988. * not in the response to the assert_master
  989. * (any nonzero response is a BUG on this node).
  990. * Most likely a socket just got disconnected
  991. * due to node death. */
  992. mlog_errno(ret);
  993. }
  994. /* no longer need to restart lock mastery.
  995. * all living nodes have been contacted. */
  996. ret = 0;
  997. }
  998. /* set the lockres owner */
  999. spin_lock(&res->spinlock);
  1000. /* mastery reference obtained either during
  1001. * assert_master_handler or in get_lock_resource */
  1002. dlm_change_lockres_owner(dlm, res, m);
  1003. spin_unlock(&res->spinlock);
  1004. leave:
  1005. return ret;
  1006. }
  1007. struct dlm_bitmap_diff_iter
  1008. {
  1009. int curnode;
  1010. unsigned long *orig_bm;
  1011. unsigned long *cur_bm;
  1012. unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1013. };
  1014. enum dlm_node_state_change
  1015. {
  1016. NODE_DOWN = -1,
  1017. NODE_NO_CHANGE = 0,
  1018. NODE_UP
  1019. };
  1020. static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter,
  1021. unsigned long *orig_bm,
  1022. unsigned long *cur_bm)
  1023. {
  1024. unsigned long p1, p2;
  1025. int i;
  1026. iter->curnode = -1;
  1027. iter->orig_bm = orig_bm;
  1028. iter->cur_bm = cur_bm;
  1029. for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) {
  1030. p1 = *(iter->orig_bm + i);
  1031. p2 = *(iter->cur_bm + i);
  1032. iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1);
  1033. }
  1034. }
  1035. static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter,
  1036. enum dlm_node_state_change *state)
  1037. {
  1038. int bit;
  1039. if (iter->curnode >= O2NM_MAX_NODES)
  1040. return -ENOENT;
  1041. bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES,
  1042. iter->curnode+1);
  1043. if (bit >= O2NM_MAX_NODES) {
  1044. iter->curnode = O2NM_MAX_NODES;
  1045. return -ENOENT;
  1046. }
  1047. /* if it was there in the original then this node died */
  1048. if (test_bit(bit, iter->orig_bm))
  1049. *state = NODE_DOWN;
  1050. else
  1051. *state = NODE_UP;
  1052. iter->curnode = bit;
  1053. return bit;
  1054. }
  1055. static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
  1056. struct dlm_lock_resource *res,
  1057. struct dlm_master_list_entry *mle,
  1058. int blocked)
  1059. {
  1060. struct dlm_bitmap_diff_iter bdi;
  1061. enum dlm_node_state_change sc;
  1062. int node;
  1063. int ret = 0;
  1064. mlog(0, "something happened such that the "
  1065. "master process may need to be restarted!\n");
  1066. assert_spin_locked(&mle->spinlock);
  1067. dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map);
  1068. node = dlm_bitmap_diff_iter_next(&bdi, &sc);
  1069. while (node >= 0) {
  1070. if (sc == NODE_UP) {
  1071. /* a node came up. clear any old vote from
  1072. * the response map and set it in the vote map
  1073. * then restart the mastery. */
  1074. mlog(ML_NOTICE, "node %d up while restarting\n", node);
  1075. /* redo the master request, but only for the new node */
  1076. mlog(0, "sending request to new node\n");
  1077. clear_bit(node, mle->response_map);
  1078. set_bit(node, mle->vote_map);
  1079. } else {
  1080. mlog(ML_ERROR, "node down! %d\n", node);
  1081. if (blocked) {
  1082. int lowest = find_next_bit(mle->maybe_map,
  1083. O2NM_MAX_NODES, 0);
  1084. /* act like it was never there */
  1085. clear_bit(node, mle->maybe_map);
  1086. if (node == lowest) {
  1087. mlog(0, "expected master %u died"
  1088. " while this node was blocked "
  1089. "waiting on it!\n", node);
  1090. lowest = find_next_bit(mle->maybe_map,
  1091. O2NM_MAX_NODES,
  1092. lowest+1);
  1093. if (lowest < O2NM_MAX_NODES) {
  1094. mlog(0, "%s:%.*s:still "
  1095. "blocked. waiting on %u "
  1096. "now\n", dlm->name,
  1097. res->lockname.len,
  1098. res->lockname.name,
  1099. lowest);
  1100. } else {
  1101. /* mle is an MLE_BLOCK, but
  1102. * there is now nothing left to
  1103. * block on. we need to return
  1104. * all the way back out and try
  1105. * again with an MLE_MASTER.
  1106. * dlm_do_local_recovery_cleanup
  1107. * has already run, so the mle
  1108. * refcount is ok */
  1109. mlog(0, "%s:%.*s: no "
  1110. "longer blocking. try to "
  1111. "master this here\n",
  1112. dlm->name,
  1113. res->lockname.len,
  1114. res->lockname.name);
  1115. mle->type = DLM_MLE_MASTER;
  1116. mle->mleres = res;
  1117. }
  1118. }
  1119. }
  1120. /* now blank out everything, as if we had never
  1121. * contacted anyone */
  1122. memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
  1123. memset(mle->response_map, 0, sizeof(mle->response_map));
  1124. /* reset the vote_map to the current node_map */
  1125. memcpy(mle->vote_map, mle->node_map,
  1126. sizeof(mle->node_map));
  1127. /* put myself into the maybe map */
  1128. if (mle->type != DLM_MLE_BLOCK)
  1129. set_bit(dlm->node_num, mle->maybe_map);
  1130. }
  1131. ret = -EAGAIN;
  1132. node = dlm_bitmap_diff_iter_next(&bdi, &sc);
  1133. }
  1134. return ret;
  1135. }
  1136. /*
  1137. * DLM_MASTER_REQUEST_MSG
  1138. *
  1139. * returns: 0 on success,
  1140. * -errno on a network error
  1141. *
  1142. * on error, the caller should assume the target node is "dead"
  1143. *
  1144. */
  1145. static int dlm_do_master_request(struct dlm_lock_resource *res,
  1146. struct dlm_master_list_entry *mle, int to)
  1147. {
  1148. struct dlm_ctxt *dlm = mle->dlm;
  1149. struct dlm_master_request request;
  1150. int ret, response=0, resend;
  1151. memset(&request, 0, sizeof(request));
  1152. request.node_idx = dlm->node_num;
  1153. BUG_ON(mle->type == DLM_MLE_MIGRATION);
  1154. request.namelen = (u8)mle->mnamelen;
  1155. memcpy(request.name, mle->mname, request.namelen);
  1156. again:
  1157. ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request,
  1158. sizeof(request), to, &response);
  1159. if (ret < 0) {
  1160. if (ret == -ESRCH) {
  1161. /* should never happen */
  1162. mlog(ML_ERROR, "TCP stack not ready!\n");
  1163. BUG();
  1164. } else if (ret == -EINVAL) {
  1165. mlog(ML_ERROR, "bad args passed to o2net!\n");
  1166. BUG();
  1167. } else if (ret == -ENOMEM) {
  1168. mlog(ML_ERROR, "out of memory while trying to send "
  1169. "network message! retrying\n");
  1170. /* this is totally crude */
  1171. msleep(50);
  1172. goto again;
  1173. } else if (!dlm_is_host_down(ret)) {
  1174. /* not a network error. bad. */
  1175. mlog_errno(ret);
  1176. mlog(ML_ERROR, "unhandled error!");
  1177. BUG();
  1178. }
  1179. /* all other errors should be network errors,
  1180. * and likely indicate node death */
  1181. mlog(ML_ERROR, "link to %d went down!\n", to);
  1182. goto out;
  1183. }
  1184. ret = 0;
  1185. resend = 0;
  1186. spin_lock(&mle->spinlock);
  1187. switch (response) {
  1188. case DLM_MASTER_RESP_YES:
  1189. set_bit(to, mle->response_map);
  1190. mlog(0, "node %u is the master, response=YES\n", to);
  1191. mlog(0, "%s:%.*s: master node %u now knows I have a "
  1192. "reference\n", dlm->name, res->lockname.len,
  1193. res->lockname.name, to);
  1194. mle->master = to;
  1195. break;
  1196. case DLM_MASTER_RESP_NO:
  1197. mlog(0, "node %u not master, response=NO\n", to);
  1198. set_bit(to, mle->response_map);
  1199. break;
  1200. case DLM_MASTER_RESP_MAYBE:
  1201. mlog(0, "node %u not master, response=MAYBE\n", to);
  1202. set_bit(to, mle->response_map);
  1203. set_bit(to, mle->maybe_map);
  1204. break;
  1205. case DLM_MASTER_RESP_ERROR:
  1206. mlog(0, "node %u hit an error, resending\n", to);
  1207. resend = 1;
  1208. response = 0;
  1209. break;
  1210. default:
  1211. mlog(ML_ERROR, "bad response! %u\n", response);
  1212. BUG();
  1213. }
  1214. spin_unlock(&mle->spinlock);
  1215. if (resend) {
  1216. /* this is also totally crude */
  1217. msleep(50);
  1218. goto again;
  1219. }
  1220. out:
  1221. return ret;
  1222. }
  1223. /*
  1224. * locks that can be taken here:
  1225. * dlm->spinlock
  1226. * res->spinlock
  1227. * mle->spinlock
  1228. * dlm->master_list
  1229. *
  1230. * if possible, TRIM THIS DOWN!!!
  1231. */
  1232. int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
  1233. void **ret_data)
  1234. {
  1235. u8 response = DLM_MASTER_RESP_MAYBE;
  1236. struct dlm_ctxt *dlm = data;
  1237. struct dlm_lock_resource *res = NULL;
  1238. struct dlm_master_request *request = (struct dlm_master_request *) msg->buf;
  1239. struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL;
  1240. char *name;
  1241. unsigned int namelen, hash;
  1242. int found, ret;
  1243. int set_maybe;
  1244. int dispatch_assert = 0;
  1245. if (!dlm_grab(dlm))
  1246. return DLM_MASTER_RESP_NO;
  1247. if (!dlm_domain_fully_joined(dlm)) {
  1248. response = DLM_MASTER_RESP_NO;
  1249. goto send_response;
  1250. }
  1251. name = request->name;
  1252. namelen = request->namelen;
  1253. hash = dlm_lockid_hash(name, namelen);
  1254. if (namelen > DLM_LOCKID_NAME_MAX) {
  1255. response = DLM_IVBUFLEN;
  1256. goto send_response;
  1257. }
  1258. way_up_top:
  1259. spin_lock(&dlm->spinlock);
  1260. res = __dlm_lookup_lockres(dlm, name, namelen, hash);
  1261. if (res) {
  1262. spin_unlock(&dlm->spinlock);
  1263. /* take care of the easy cases up front */
  1264. spin_lock(&res->spinlock);
  1265. if (res->state & (DLM_LOCK_RES_RECOVERING|
  1266. DLM_LOCK_RES_MIGRATING)) {
  1267. spin_unlock(&res->spinlock);
  1268. mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
  1269. "being recovered/migrated\n");
  1270. response = DLM_MASTER_RESP_ERROR;
  1271. if (mle)
  1272. kmem_cache_free(dlm_mle_cache, mle);
  1273. goto send_response;
  1274. }
  1275. if (res->owner == dlm->node_num) {
  1276. dlm_lockres_set_refmap_bit(dlm, res, request->node_idx);
  1277. spin_unlock(&res->spinlock);
  1278. response = DLM_MASTER_RESP_YES;
  1279. if (mle)
  1280. kmem_cache_free(dlm_mle_cache, mle);
  1281. /* this node is the owner.
  1282. * there is some extra work that needs to
  1283. * happen now. the requesting node has
  1284. * caused all nodes up to this one to
  1285. * create mles. this node now needs to
  1286. * go back and clean those up. */
  1287. dispatch_assert = 1;
  1288. goto send_response;
  1289. } else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1290. spin_unlock(&res->spinlock);
  1291. // mlog(0, "node %u is the master\n", res->owner);
  1292. response = DLM_MASTER_RESP_NO;
  1293. if (mle)
  1294. kmem_cache_free(dlm_mle_cache, mle);
  1295. goto send_response;
  1296. }
  1297. /* ok, there is no owner. either this node is
  1298. * being blocked, or it is actively trying to
  1299. * master this lock. */
  1300. if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
  1301. mlog(ML_ERROR, "lock with no owner should be "
  1302. "in-progress!\n");
  1303. BUG();
  1304. }
  1305. // mlog(0, "lockres is in progress...\n");
  1306. spin_lock(&dlm->master_lock);
  1307. found = dlm_find_mle(dlm, &tmpmle, name, namelen);
  1308. if (!found) {
  1309. mlog(ML_ERROR, "no mle found for this lock!\n");
  1310. BUG();
  1311. }
  1312. set_maybe = 1;
  1313. spin_lock(&tmpmle->spinlock);
  1314. if (tmpmle->type == DLM_MLE_BLOCK) {
  1315. // mlog(0, "this node is waiting for "
  1316. // "lockres to be mastered\n");
  1317. response = DLM_MASTER_RESP_NO;
  1318. } else if (tmpmle->type == DLM_MLE_MIGRATION) {
  1319. mlog(0, "node %u is master, but trying to migrate to "
  1320. "node %u.\n", tmpmle->master, tmpmle->new_master);
  1321. if (tmpmle->master == dlm->node_num) {
  1322. mlog(ML_ERROR, "no owner on lockres, but this "
  1323. "node is trying to migrate it to %u?!\n",
  1324. tmpmle->new_master);
  1325. BUG();
  1326. } else {
  1327. /* the real master can respond on its own */
  1328. response = DLM_MASTER_RESP_NO;
  1329. }
  1330. } else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1331. set_maybe = 0;
  1332. if (tmpmle->master == dlm->node_num) {
  1333. response = DLM_MASTER_RESP_YES;
  1334. /* this node will be the owner.
  1335. * go back and clean the mles on any
  1336. * other nodes */
  1337. dispatch_assert = 1;
  1338. dlm_lockres_set_refmap_bit(dlm, res,
  1339. request->node_idx);
  1340. } else
  1341. response = DLM_MASTER_RESP_NO;
  1342. } else {
  1343. // mlog(0, "this node is attempting to "
  1344. // "master lockres\n");
  1345. response = DLM_MASTER_RESP_MAYBE;
  1346. }
  1347. if (set_maybe)
  1348. set_bit(request->node_idx, tmpmle->maybe_map);
  1349. spin_unlock(&tmpmle->spinlock);
  1350. spin_unlock(&dlm->master_lock);
  1351. spin_unlock(&res->spinlock);
  1352. /* keep the mle attached to heartbeat events */
  1353. dlm_put_mle(tmpmle);
  1354. if (mle)
  1355. kmem_cache_free(dlm_mle_cache, mle);
  1356. goto send_response;
  1357. }
  1358. /*
  1359. * lockres doesn't exist on this node
  1360. * if there is an MLE_BLOCK, return NO
  1361. * if there is an MLE_MASTER, return MAYBE
  1362. * otherwise, add an MLE_BLOCK, return NO
  1363. */
  1364. spin_lock(&dlm->master_lock);
  1365. found = dlm_find_mle(dlm, &tmpmle, name, namelen);
  1366. if (!found) {
  1367. /* this lockid has never been seen on this node yet */
  1368. // mlog(0, "no mle found\n");
  1369. if (!mle) {
  1370. spin_unlock(&dlm->master_lock);
  1371. spin_unlock(&dlm->spinlock);
  1372. mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
  1373. if (!mle) {
  1374. response = DLM_MASTER_RESP_ERROR;
  1375. mlog_errno(-ENOMEM);
  1376. goto send_response;
  1377. }
  1378. goto way_up_top;
  1379. }
  1380. // mlog(0, "this is second time thru, already allocated, "
  1381. // "add the block.\n");
  1382. dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen);
  1383. set_bit(request->node_idx, mle->maybe_map);
  1384. __dlm_insert_mle(dlm, mle);
  1385. response = DLM_MASTER_RESP_NO;
  1386. } else {
  1387. // mlog(0, "mle was found\n");
  1388. set_maybe = 1;
  1389. spin_lock(&tmpmle->spinlock);
  1390. if (tmpmle->master == dlm->node_num) {
  1391. mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n");
  1392. BUG();
  1393. }
  1394. if (tmpmle->type == DLM_MLE_BLOCK)
  1395. response = DLM_MASTER_RESP_NO;
  1396. else if (tmpmle->type == DLM_MLE_MIGRATION) {
  1397. mlog(0, "migration mle was found (%u->%u)\n",
  1398. tmpmle->master, tmpmle->new_master);
  1399. /* real master can respond on its own */
  1400. response = DLM_MASTER_RESP_NO;
  1401. } else
  1402. response = DLM_MASTER_RESP_MAYBE;
  1403. if (set_maybe)
  1404. set_bit(request->node_idx, tmpmle->maybe_map);
  1405. spin_unlock(&tmpmle->spinlock);
  1406. }
  1407. spin_unlock(&dlm->master_lock);
  1408. spin_unlock(&dlm->spinlock);
  1409. if (found) {
  1410. /* keep the mle attached to heartbeat events */
  1411. dlm_put_mle(tmpmle);
  1412. }
  1413. send_response:
  1414. /*
  1415. * __dlm_lookup_lockres() grabbed a reference to this lockres.
  1416. * The reference is released by dlm_assert_master_worker() under
  1417. * the call to dlm_dispatch_assert_master(). If
  1418. * dlm_assert_master_worker() isn't called, we drop it here.
  1419. */
  1420. if (dispatch_assert) {
  1421. if (response != DLM_MASTER_RESP_YES)
  1422. mlog(ML_ERROR, "invalid response %d\n", response);
  1423. if (!res) {
  1424. mlog(ML_ERROR, "bad lockres while trying to assert!\n");
  1425. BUG();
  1426. }
  1427. mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
  1428. dlm->node_num, res->lockname.len, res->lockname.name);
  1429. ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx,
  1430. DLM_ASSERT_MASTER_MLE_CLEANUP);
  1431. if (ret < 0) {
  1432. mlog(ML_ERROR, "failed to dispatch assert master work\n");
  1433. response = DLM_MASTER_RESP_ERROR;
  1434. dlm_lockres_put(res);
  1435. } else
  1436. dlm_lockres_grab_inflight_worker(dlm, res);
  1437. } else {
  1438. if (res)
  1439. dlm_lockres_put(res);
  1440. }
  1441. dlm_put(dlm);
  1442. return response;
  1443. }
  1444. /*
  1445. * DLM_ASSERT_MASTER_MSG
  1446. */
  1447. /*
  1448. * NOTE: this can be used for debugging
  1449. * can periodically run all locks owned by this node
  1450. * and re-assert across the cluster...
  1451. */
  1452. static int dlm_do_assert_master(struct dlm_ctxt *dlm,
  1453. struct dlm_lock_resource *res,
  1454. void *nodemap, u32 flags)
  1455. {
  1456. struct dlm_assert_master assert;
  1457. int to, tmpret;
  1458. struct dlm_node_iter iter;
  1459. int ret = 0;
  1460. int reassert;
  1461. const char *lockname = res->lockname.name;
  1462. unsigned int namelen = res->lockname.len;
  1463. BUG_ON(namelen > O2NM_MAX_NAME_LEN);
  1464. spin_lock(&res->spinlock);
  1465. res->state |= DLM_LOCK_RES_SETREF_INPROG;
  1466. spin_unlock(&res->spinlock);
  1467. again:
  1468. reassert = 0;
  1469. /* note that if this nodemap is empty, it returns 0 */
  1470. dlm_node_iter_init(nodemap, &iter);
  1471. while ((to = dlm_node_iter_next(&iter)) >= 0) {
  1472. int r = 0;
  1473. struct dlm_master_list_entry *mle = NULL;
  1474. mlog(0, "sending assert master to %d (%.*s)\n", to,
  1475. namelen, lockname);
  1476. memset(&assert, 0, sizeof(assert));
  1477. assert.node_idx = dlm->node_num;
  1478. assert.namelen = namelen;
  1479. memcpy(assert.name, lockname, namelen);
  1480. assert.flags = cpu_to_be32(flags);
  1481. tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key,
  1482. &assert, sizeof(assert), to, &r);
  1483. if (tmpret < 0) {
  1484. mlog(ML_ERROR, "Error %d when sending message %u (key "
  1485. "0x%x) to node %u\n", tmpret,
  1486. DLM_ASSERT_MASTER_MSG, dlm->key, to);
  1487. if (!dlm_is_host_down(tmpret)) {
  1488. mlog(ML_ERROR, "unhandled error=%d!\n", tmpret);
  1489. BUG();
  1490. }
  1491. /* a node died. finish out the rest of the nodes. */
  1492. mlog(0, "link to %d went down!\n", to);
  1493. /* any nonzero status return will do */
  1494. ret = tmpret;
  1495. r = 0;
  1496. } else if (r < 0) {
  1497. /* ok, something horribly messed. kill thyself. */
  1498. mlog(ML_ERROR,"during assert master of %.*s to %u, "
  1499. "got %d.\n", namelen, lockname, to, r);
  1500. spin_lock(&dlm->spinlock);
  1501. spin_lock(&dlm->master_lock);
  1502. if (dlm_find_mle(dlm, &mle, (char *)lockname,
  1503. namelen)) {
  1504. dlm_print_one_mle(mle);
  1505. __dlm_put_mle(mle);
  1506. }
  1507. spin_unlock(&dlm->master_lock);
  1508. spin_unlock(&dlm->spinlock);
  1509. BUG();
  1510. }
  1511. if (r & DLM_ASSERT_RESPONSE_REASSERT &&
  1512. !(r & DLM_ASSERT_RESPONSE_MASTERY_REF)) {
  1513. mlog(ML_ERROR, "%.*s: very strange, "
  1514. "master MLE but no lockres on %u\n",
  1515. namelen, lockname, to);
  1516. }
  1517. if (r & DLM_ASSERT_RESPONSE_REASSERT) {
  1518. mlog(0, "%.*s: node %u create mles on other "
  1519. "nodes and requests a re-assert\n",
  1520. namelen, lockname, to);
  1521. reassert = 1;
  1522. }
  1523. if (r & DLM_ASSERT_RESPONSE_MASTERY_REF) {
  1524. mlog(0, "%.*s: node %u has a reference to this "
  1525. "lockres, set the bit in the refmap\n",
  1526. namelen, lockname, to);
  1527. spin_lock(&res->spinlock);
  1528. dlm_lockres_set_refmap_bit(dlm, res, to);
  1529. spin_unlock(&res->spinlock);
  1530. }
  1531. }
  1532. if (reassert)
  1533. goto again;
  1534. spin_lock(&res->spinlock);
  1535. res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
  1536. spin_unlock(&res->spinlock);
  1537. wake_up(&res->wq);
  1538. return ret;
  1539. }
  1540. /*
  1541. * locks that can be taken here:
  1542. * dlm->spinlock
  1543. * res->spinlock
  1544. * mle->spinlock
  1545. * dlm->master_list
  1546. *
  1547. * if possible, TRIM THIS DOWN!!!
  1548. */
  1549. int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data,
  1550. void **ret_data)
  1551. {
  1552. struct dlm_ctxt *dlm = data;
  1553. struct dlm_master_list_entry *mle = NULL;
  1554. struct dlm_assert_master *assert = (struct dlm_assert_master *)msg->buf;
  1555. struct dlm_lock_resource *res = NULL;
  1556. char *name;
  1557. unsigned int namelen, hash;
  1558. u32 flags;
  1559. int master_request = 0, have_lockres_ref = 0;
  1560. int ret = 0;
  1561. if (!dlm_grab(dlm))
  1562. return 0;
  1563. name = assert->name;
  1564. namelen = assert->namelen;
  1565. hash = dlm_lockid_hash(name, namelen);
  1566. flags = be32_to_cpu(assert->flags);
  1567. if (namelen > DLM_LOCKID_NAME_MAX) {
  1568. mlog(ML_ERROR, "Invalid name length!");
  1569. goto done;
  1570. }
  1571. spin_lock(&dlm->spinlock);
  1572. if (flags)
  1573. mlog(0, "assert_master with flags: %u\n", flags);
  1574. /* find the MLE */
  1575. spin_lock(&dlm->master_lock);
  1576. if (!dlm_find_mle(dlm, &mle, name, namelen)) {
  1577. /* not an error, could be master just re-asserting */
  1578. mlog(0, "just got an assert_master from %u, but no "
  1579. "MLE for it! (%.*s)\n", assert->node_idx,
  1580. namelen, name);
  1581. } else {
  1582. int bit = find_next_bit (mle->maybe_map, O2NM_MAX_NODES, 0);
  1583. if (bit >= O2NM_MAX_NODES) {
  1584. /* not necessarily an error, though less likely.
  1585. * could be master just re-asserting. */
  1586. mlog(0, "no bits set in the maybe_map, but %u "
  1587. "is asserting! (%.*s)\n", assert->node_idx,
  1588. namelen, name);
  1589. } else if (bit != assert->node_idx) {
  1590. if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
  1591. mlog(0, "master %u was found, %u should "
  1592. "back off\n", assert->node_idx, bit);
  1593. } else {
  1594. /* with the fix for bug 569, a higher node
  1595. * number winning the mastery will respond
  1596. * YES to mastery requests, but this node
  1597. * had no way of knowing. let it pass. */
  1598. mlog(0, "%u is the lowest node, "
  1599. "%u is asserting. (%.*s) %u must "
  1600. "have begun after %u won.\n", bit,
  1601. assert->node_idx, namelen, name, bit,
  1602. assert->node_idx);
  1603. }
  1604. }
  1605. if (mle->type == DLM_MLE_MIGRATION) {
  1606. if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
  1607. mlog(0, "%s:%.*s: got cleanup assert"
  1608. " from %u for migration\n",
  1609. dlm->name, namelen, name,
  1610. assert->node_idx);
  1611. } else if (!(flags & DLM_ASSERT_MASTER_FINISH_MIGRATION)) {
  1612. mlog(0, "%s:%.*s: got unrelated assert"
  1613. " from %u for migration, ignoring\n",
  1614. dlm->name, namelen, name,
  1615. assert->node_idx);
  1616. __dlm_put_mle(mle);
  1617. spin_unlock(&dlm->master_lock);
  1618. spin_unlock(&dlm->spinlock);
  1619. goto done;
  1620. }
  1621. }
  1622. }
  1623. spin_unlock(&dlm->master_lock);
  1624. /* ok everything checks out with the MLE
  1625. * now check to see if there is a lockres */
  1626. res = __dlm_lookup_lockres(dlm, name, namelen, hash);
  1627. if (res) {
  1628. spin_lock(&res->spinlock);
  1629. if (res->state & DLM_LOCK_RES_RECOVERING) {
  1630. mlog(ML_ERROR, "%u asserting but %.*s is "
  1631. "RECOVERING!\n", assert->node_idx, namelen, name);
  1632. goto kill;
  1633. }
  1634. if (!mle) {
  1635. if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN &&
  1636. res->owner != assert->node_idx) {
  1637. mlog(ML_ERROR, "DIE! Mastery assert from %u, "
  1638. "but current owner is %u! (%.*s)\n",
  1639. assert->node_idx, res->owner, namelen,
  1640. name);
  1641. __dlm_print_one_lock_resource(res);
  1642. BUG();
  1643. }
  1644. } else if (mle->type != DLM_MLE_MIGRATION) {
  1645. if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1646. /* owner is just re-asserting */
  1647. if (res->owner == assert->node_idx) {
  1648. mlog(0, "owner %u re-asserting on "
  1649. "lock %.*s\n", assert->node_idx,
  1650. namelen, name);
  1651. goto ok;
  1652. }
  1653. mlog(ML_ERROR, "got assert_master from "
  1654. "node %u, but %u is the owner! "
  1655. "(%.*s)\n", assert->node_idx,
  1656. res->owner, namelen, name);
  1657. goto kill;
  1658. }
  1659. if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
  1660. mlog(ML_ERROR, "got assert from %u, but lock "
  1661. "with no owner should be "
  1662. "in-progress! (%.*s)\n",
  1663. assert->node_idx,
  1664. namelen, name);
  1665. goto kill;
  1666. }
  1667. } else /* mle->type == DLM_MLE_MIGRATION */ {
  1668. /* should only be getting an assert from new master */
  1669. if (assert->node_idx != mle->new_master) {
  1670. mlog(ML_ERROR, "got assert from %u, but "
  1671. "new master is %u, and old master "
  1672. "was %u (%.*s)\n",
  1673. assert->node_idx, mle->new_master,
  1674. mle->master, namelen, name);
  1675. goto kill;
  1676. }
  1677. }
  1678. ok:
  1679. spin_unlock(&res->spinlock);
  1680. }
  1681. // mlog(0, "woo! got an assert_master from node %u!\n",
  1682. // assert->node_idx);
  1683. if (mle) {
  1684. int extra_ref = 0;
  1685. int nn = -1;
  1686. int rr, err = 0;
  1687. spin_lock(&mle->spinlock);
  1688. if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION)
  1689. extra_ref = 1;
  1690. else {
  1691. /* MASTER mle: if any bits set in the response map
  1692. * then the calling node needs to re-assert to clear
  1693. * up nodes that this node contacted */
  1694. while ((nn = find_next_bit (mle->response_map, O2NM_MAX_NODES,
  1695. nn+1)) < O2NM_MAX_NODES) {
  1696. if (nn != dlm->node_num && nn != assert->node_idx) {
  1697. master_request = 1;
  1698. break;
  1699. }
  1700. }
  1701. }
  1702. mle->master = assert->node_idx;
  1703. atomic_set(&mle->woken, 1);
  1704. wake_up(&mle->wq);
  1705. spin_unlock(&mle->spinlock);
  1706. if (res) {
  1707. int wake = 0;
  1708. spin_lock(&res->spinlock);
  1709. if (mle->type == DLM_MLE_MIGRATION) {
  1710. mlog(0, "finishing off migration of lockres %.*s, "
  1711. "from %u to %u\n",
  1712. res->lockname.len, res->lockname.name,
  1713. dlm->node_num, mle->new_master);
  1714. res->state &= ~DLM_LOCK_RES_MIGRATING;
  1715. wake = 1;
  1716. dlm_change_lockres_owner(dlm, res, mle->new_master);
  1717. BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
  1718. } else {
  1719. dlm_change_lockres_owner(dlm, res, mle->master);
  1720. }
  1721. spin_unlock(&res->spinlock);
  1722. have_lockres_ref = 1;
  1723. if (wake)
  1724. wake_up(&res->wq);
  1725. }
  1726. /* master is known, detach if not already detached.
  1727. * ensures that only one assert_master call will happen
  1728. * on this mle. */
  1729. spin_lock(&dlm->master_lock);
  1730. rr = atomic_read(&mle->mle_refs.refcount);
  1731. if (mle->inuse > 0) {
  1732. if (extra_ref && rr < 3)
  1733. err = 1;
  1734. else if (!extra_ref && rr < 2)
  1735. err = 1;
  1736. } else {
  1737. if (extra_ref && rr < 2)
  1738. err = 1;
  1739. else if (!extra_ref && rr < 1)
  1740. err = 1;
  1741. }
  1742. if (err) {
  1743. mlog(ML_ERROR, "%s:%.*s: got assert master from %u "
  1744. "that will mess up this node, refs=%d, extra=%d, "
  1745. "inuse=%d\n", dlm->name, namelen, name,
  1746. assert->node_idx, rr, extra_ref, mle->inuse);
  1747. dlm_print_one_mle(mle);
  1748. }
  1749. __dlm_unlink_mle(dlm, mle);
  1750. __dlm_mle_detach_hb_events(dlm, mle);
  1751. __dlm_put_mle(mle);
  1752. if (extra_ref) {
  1753. /* the assert master message now balances the extra
  1754. * ref given by the master / migration request message.
  1755. * if this is the last put, it will be removed
  1756. * from the list. */
  1757. __dlm_put_mle(mle);
  1758. }
  1759. spin_unlock(&dlm->master_lock);
  1760. } else if (res) {
  1761. if (res->owner != assert->node_idx) {
  1762. mlog(0, "assert_master from %u, but current "
  1763. "owner is %u (%.*s), no mle\n", assert->node_idx,
  1764. res->owner, namelen, name);
  1765. }
  1766. }
  1767. spin_unlock(&dlm->spinlock);
  1768. done:
  1769. ret = 0;
  1770. if (res) {
  1771. spin_lock(&res->spinlock);
  1772. res->state |= DLM_LOCK_RES_SETREF_INPROG;
  1773. spin_unlock(&res->spinlock);
  1774. *ret_data = (void *)res;
  1775. }
  1776. dlm_put(dlm);
  1777. if (master_request) {
  1778. mlog(0, "need to tell master to reassert\n");
  1779. /* positive. negative would shoot down the node. */
  1780. ret |= DLM_ASSERT_RESPONSE_REASSERT;
  1781. if (!have_lockres_ref) {
  1782. mlog(ML_ERROR, "strange, got assert from %u, MASTER "
  1783. "mle present here for %s:%.*s, but no lockres!\n",
  1784. assert->node_idx, dlm->name, namelen, name);
  1785. }
  1786. }
  1787. if (have_lockres_ref) {
  1788. /* let the master know we have a reference to the lockres */
  1789. ret |= DLM_ASSERT_RESPONSE_MASTERY_REF;
  1790. mlog(0, "%s:%.*s: got assert from %u, need a ref\n",
  1791. dlm->name, namelen, name, assert->node_idx);
  1792. }
  1793. return ret;
  1794. kill:
  1795. /* kill the caller! */
  1796. mlog(ML_ERROR, "Bad message received from another node. Dumping state "
  1797. "and killing the other node now! This node is OK and can continue.\n");
  1798. __dlm_print_one_lock_resource(res);
  1799. spin_unlock(&res->spinlock);
  1800. spin_lock(&dlm->master_lock);
  1801. if (mle)
  1802. __dlm_put_mle(mle);
  1803. spin_unlock(&dlm->master_lock);
  1804. spin_unlock(&dlm->spinlock);
  1805. *ret_data = (void *)res;
  1806. dlm_put(dlm);
  1807. return -EINVAL;
  1808. }
  1809. void dlm_assert_master_post_handler(int status, void *data, void *ret_data)
  1810. {
  1811. struct dlm_lock_resource *res = (struct dlm_lock_resource *)ret_data;
  1812. if (ret_data) {
  1813. spin_lock(&res->spinlock);
  1814. res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
  1815. spin_unlock(&res->spinlock);
  1816. wake_up(&res->wq);
  1817. dlm_lockres_put(res);
  1818. }
  1819. return;
  1820. }
  1821. int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
  1822. struct dlm_lock_resource *res,
  1823. int ignore_higher, u8 request_from, u32 flags)
  1824. {
  1825. struct dlm_work_item *item;
  1826. item = kzalloc(sizeof(*item), GFP_ATOMIC);
  1827. if (!item)
  1828. return -ENOMEM;
  1829. /* queue up work for dlm_assert_master_worker */
  1830. dlm_grab(dlm); /* get an extra ref for the work item */
  1831. dlm_init_work_item(dlm, item, dlm_assert_master_worker, NULL);
  1832. item->u.am.lockres = res; /* already have a ref */
  1833. /* can optionally ignore node numbers higher than this node */
  1834. item->u.am.ignore_higher = ignore_higher;
  1835. item->u.am.request_from = request_from;
  1836. item->u.am.flags = flags;
  1837. if (ignore_higher)
  1838. mlog(0, "IGNORE HIGHER: %.*s\n", res->lockname.len,
  1839. res->lockname.name);
  1840. spin_lock(&dlm->work_lock);
  1841. list_add_tail(&item->list, &dlm->work_list);
  1842. spin_unlock(&dlm->work_lock);
  1843. queue_work(dlm->dlm_worker, &dlm->dispatched_work);
  1844. return 0;
  1845. }
  1846. static void dlm_assert_master_worker(struct dlm_work_item *item, void *data)
  1847. {
  1848. struct dlm_ctxt *dlm = data;
  1849. int ret = 0;
  1850. struct dlm_lock_resource *res;
  1851. unsigned long nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1852. int ignore_higher;
  1853. int bit;
  1854. u8 request_from;
  1855. u32 flags;
  1856. dlm = item->dlm;
  1857. res = item->u.am.lockres;
  1858. ignore_higher = item->u.am.ignore_higher;
  1859. request_from = item->u.am.request_from;
  1860. flags = item->u.am.flags;
  1861. spin_lock(&dlm->spinlock);
  1862. memcpy(nodemap, dlm->domain_map, sizeof(nodemap));
  1863. spin_unlock(&dlm->spinlock);
  1864. clear_bit(dlm->node_num, nodemap);
  1865. if (ignore_higher) {
  1866. /* if is this just to clear up mles for nodes below
  1867. * this node, do not send the message to the original
  1868. * caller or any node number higher than this */
  1869. clear_bit(request_from, nodemap);
  1870. bit = dlm->node_num;
  1871. while (1) {
  1872. bit = find_next_bit(nodemap, O2NM_MAX_NODES,
  1873. bit+1);
  1874. if (bit >= O2NM_MAX_NODES)
  1875. break;
  1876. clear_bit(bit, nodemap);
  1877. }
  1878. }
  1879. /*
  1880. * If we're migrating this lock to someone else, we are no
  1881. * longer allowed to assert out own mastery. OTOH, we need to
  1882. * prevent migration from starting while we're still asserting
  1883. * our dominance. The reserved ast delays migration.
  1884. */
  1885. spin_lock(&res->spinlock);
  1886. if (res->state & DLM_LOCK_RES_MIGRATING) {
  1887. mlog(0, "Someone asked us to assert mastery, but we're "
  1888. "in the middle of migration. Skipping assert, "
  1889. "the new master will handle that.\n");
  1890. spin_unlock(&res->spinlock);
  1891. goto put;
  1892. } else
  1893. __dlm_lockres_reserve_ast(res);
  1894. spin_unlock(&res->spinlock);
  1895. /* this call now finishes out the nodemap
  1896. * even if one or more nodes die */
  1897. mlog(0, "worker about to master %.*s here, this=%u\n",
  1898. res->lockname.len, res->lockname.name, dlm->node_num);
  1899. ret = dlm_do_assert_master(dlm, res, nodemap, flags);
  1900. if (ret < 0) {
  1901. /* no need to restart, we are done */
  1902. if (!dlm_is_host_down(ret))
  1903. mlog_errno(ret);
  1904. }
  1905. /* Ok, we've asserted ourselves. Let's let migration start. */
  1906. dlm_lockres_release_ast(dlm, res);
  1907. put:
  1908. dlm_lockres_drop_inflight_worker(dlm, res);
  1909. dlm_lockres_put(res);
  1910. mlog(0, "finished with dlm_assert_master_worker\n");
  1911. }
  1912. /* SPECIAL CASE for the $RECOVERY lock used by the recovery thread.
  1913. * We cannot wait for node recovery to complete to begin mastering this
  1914. * lockres because this lockres is used to kick off recovery! ;-)
  1915. * So, do a pre-check on all living nodes to see if any of those nodes
  1916. * think that $RECOVERY is currently mastered by a dead node. If so,
  1917. * we wait a short time to allow that node to get notified by its own
  1918. * heartbeat stack, then check again. All $RECOVERY lock resources
  1919. * mastered by dead nodes are purged when the hearbeat callback is
  1920. * fired, so we can know for sure that it is safe to continue once
  1921. * the node returns a live node or no node. */
  1922. static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
  1923. struct dlm_lock_resource *res)
  1924. {
  1925. struct dlm_node_iter iter;
  1926. int nodenum;
  1927. int ret = 0;
  1928. u8 master = DLM_LOCK_RES_OWNER_UNKNOWN;
  1929. spin_lock(&dlm->spinlock);
  1930. dlm_node_iter_init(dlm->domain_map, &iter);
  1931. spin_unlock(&dlm->spinlock);
  1932. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  1933. /* do not send to self */
  1934. if (nodenum == dlm->node_num)
  1935. continue;
  1936. ret = dlm_do_master_requery(dlm, res, nodenum, &master);
  1937. if (ret < 0) {
  1938. mlog_errno(ret);
  1939. if (!dlm_is_host_down(ret))
  1940. BUG();
  1941. /* host is down, so answer for that node would be
  1942. * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
  1943. ret = 0;
  1944. }
  1945. if (master != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1946. /* check to see if this master is in the recovery map */
  1947. spin_lock(&dlm->spinlock);
  1948. if (test_bit(master, dlm->recovery_map)) {
  1949. mlog(ML_NOTICE, "%s: node %u has not seen "
  1950. "node %u go down yet, and thinks the "
  1951. "dead node is mastering the recovery "
  1952. "lock. must wait.\n", dlm->name,
  1953. nodenum, master);
  1954. ret = -EAGAIN;
  1955. }
  1956. spin_unlock(&dlm->spinlock);
  1957. mlog(0, "%s: reco lock master is %u\n", dlm->name,
  1958. master);
  1959. break;
  1960. }
  1961. }
  1962. return ret;
  1963. }
  1964. /*
  1965. * DLM_DEREF_LOCKRES_MSG
  1966. */
  1967. int dlm_drop_lockres_ref(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
  1968. {
  1969. struct dlm_deref_lockres deref;
  1970. int ret = 0, r;
  1971. const char *lockname;
  1972. unsigned int namelen;
  1973. lockname = res->lockname.name;
  1974. namelen = res->lockname.len;
  1975. BUG_ON(namelen > O2NM_MAX_NAME_LEN);
  1976. memset(&deref, 0, sizeof(deref));
  1977. deref.node_idx = dlm->node_num;
  1978. deref.namelen = namelen;
  1979. memcpy(deref.name, lockname, namelen);
  1980. ret = o2net_send_message(DLM_DEREF_LOCKRES_MSG, dlm->key,
  1981. &deref, sizeof(deref), res->owner, &r);
  1982. if (ret < 0)
  1983. mlog(ML_ERROR, "%s: res %.*s, error %d send DEREF to node %u\n",
  1984. dlm->name, namelen, lockname, ret, res->owner);
  1985. else if (r < 0) {
  1986. /* BAD. other node says I did not have a ref. */
  1987. mlog(ML_ERROR, "%s: res %.*s, DEREF to node %u got %d\n",
  1988. dlm->name, namelen, lockname, res->owner, r);
  1989. dlm_print_one_lock_resource(res);
  1990. BUG();
  1991. }
  1992. return ret;
  1993. }
  1994. int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
  1995. void **ret_data)
  1996. {
  1997. struct dlm_ctxt *dlm = data;
  1998. struct dlm_deref_lockres *deref = (struct dlm_deref_lockres *)msg->buf;
  1999. struct dlm_lock_resource *res = NULL;
  2000. char *name;
  2001. unsigned int namelen;
  2002. int ret = -EINVAL;
  2003. u8 node;
  2004. unsigned int hash;
  2005. struct dlm_work_item *item;
  2006. int cleared = 0;
  2007. int dispatch = 0;
  2008. if (!dlm_grab(dlm))
  2009. return 0;
  2010. name = deref->name;
  2011. namelen = deref->namelen;
  2012. node = deref->node_idx;
  2013. if (namelen > DLM_LOCKID_NAME_MAX) {
  2014. mlog(ML_ERROR, "Invalid name length!");
  2015. goto done;
  2016. }
  2017. if (deref->node_idx >= O2NM_MAX_NODES) {
  2018. mlog(ML_ERROR, "Invalid node number: %u\n", node);
  2019. goto done;
  2020. }
  2021. hash = dlm_lockid_hash(name, namelen);
  2022. spin_lock(&dlm->spinlock);
  2023. res = __dlm_lookup_lockres_full(dlm, name, namelen, hash);
  2024. if (!res) {
  2025. spin_unlock(&dlm->spinlock);
  2026. mlog(ML_ERROR, "%s:%.*s: bad lockres name\n",
  2027. dlm->name, namelen, name);
  2028. goto done;
  2029. }
  2030. spin_unlock(&dlm->spinlock);
  2031. spin_lock(&res->spinlock);
  2032. if (res->state & DLM_LOCK_RES_SETREF_INPROG)
  2033. dispatch = 1;
  2034. else {
  2035. BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
  2036. if (test_bit(node, res->refmap)) {
  2037. dlm_lockres_clear_refmap_bit(dlm, res, node);
  2038. cleared = 1;
  2039. }
  2040. }
  2041. spin_unlock(&res->spinlock);
  2042. if (!dispatch) {
  2043. if (cleared)
  2044. dlm_lockres_calc_usage(dlm, res);
  2045. else {
  2046. mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
  2047. "but it is already dropped!\n", dlm->name,
  2048. res->lockname.len, res->lockname.name, node);
  2049. dlm_print_one_lock_resource(res);
  2050. }
  2051. ret = 0;
  2052. goto done;
  2053. }
  2054. item = kzalloc(sizeof(*item), GFP_NOFS);
  2055. if (!item) {
  2056. ret = -ENOMEM;
  2057. mlog_errno(ret);
  2058. goto done;
  2059. }
  2060. dlm_init_work_item(dlm, item, dlm_deref_lockres_worker, NULL);
  2061. item->u.dl.deref_res = res;
  2062. item->u.dl.deref_node = node;
  2063. spin_lock(&dlm->work_lock);
  2064. list_add_tail(&item->list, &dlm->work_list);
  2065. spin_unlock(&dlm->work_lock);
  2066. queue_work(dlm->dlm_worker, &dlm->dispatched_work);
  2067. return 0;
  2068. done:
  2069. if (res)
  2070. dlm_lockres_put(res);
  2071. dlm_put(dlm);
  2072. return ret;
  2073. }
  2074. static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data)
  2075. {
  2076. struct dlm_ctxt *dlm;
  2077. struct dlm_lock_resource *res;
  2078. u8 node;
  2079. u8 cleared = 0;
  2080. dlm = item->dlm;
  2081. res = item->u.dl.deref_res;
  2082. node = item->u.dl.deref_node;
  2083. spin_lock(&res->spinlock);
  2084. BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
  2085. if (test_bit(node, res->refmap)) {
  2086. __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
  2087. dlm_lockres_clear_refmap_bit(dlm, res, node);
  2088. cleared = 1;
  2089. }
  2090. spin_unlock(&res->spinlock);
  2091. if (cleared) {
  2092. mlog(0, "%s:%.*s node %u ref dropped in dispatch\n",
  2093. dlm->name, res->lockname.len, res->lockname.name, node);
  2094. dlm_lockres_calc_usage(dlm, res);
  2095. } else {
  2096. mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
  2097. "but it is already dropped!\n", dlm->name,
  2098. res->lockname.len, res->lockname.name, node);
  2099. dlm_print_one_lock_resource(res);
  2100. }
  2101. dlm_lockres_put(res);
  2102. }
  2103. /*
  2104. * A migrateable resource is one that is :
  2105. * 1. locally mastered, and,
  2106. * 2. zero local locks, and,
  2107. * 3. one or more non-local locks, or, one or more references
  2108. * Returns 1 if yes, 0 if not.
  2109. */
  2110. static int dlm_is_lockres_migrateable(struct dlm_ctxt *dlm,
  2111. struct dlm_lock_resource *res)
  2112. {
  2113. enum dlm_lockres_list idx;
  2114. int nonlocal = 0, node_ref;
  2115. struct list_head *queue;
  2116. struct dlm_lock *lock;
  2117. u64 cookie;
  2118. assert_spin_locked(&res->spinlock);
  2119. /* delay migration when the lockres is in MIGRATING state */
  2120. if (res->state & DLM_LOCK_RES_MIGRATING)
  2121. return 0;
  2122. /* delay migration when the lockres is in RECOCERING state */
  2123. if (res->state & DLM_LOCK_RES_RECOVERING)
  2124. return 0;
  2125. if (res->owner != dlm->node_num)
  2126. return 0;
  2127. for (idx = DLM_GRANTED_LIST; idx <= DLM_BLOCKED_LIST; idx++) {
  2128. queue = dlm_list_idx_to_ptr(res, idx);
  2129. list_for_each_entry(lock, queue, list) {
  2130. if (lock->ml.node != dlm->node_num) {
  2131. nonlocal++;
  2132. continue;
  2133. }
  2134. cookie = be64_to_cpu(lock->ml.cookie);
  2135. mlog(0, "%s: Not migrateable res %.*s, lock %u:%llu on "
  2136. "%s list\n", dlm->name, res->lockname.len,
  2137. res->lockname.name,
  2138. dlm_get_lock_cookie_node(cookie),
  2139. dlm_get_lock_cookie_seq(cookie),
  2140. dlm_list_in_text(idx));
  2141. return 0;
  2142. }
  2143. }
  2144. if (!nonlocal) {
  2145. node_ref = find_next_bit(res->refmap, O2NM_MAX_NODES, 0);
  2146. if (node_ref >= O2NM_MAX_NODES)
  2147. return 0;
  2148. }
  2149. mlog(0, "%s: res %.*s, Migrateable\n", dlm->name, res->lockname.len,
  2150. res->lockname.name);
  2151. return 1;
  2152. }
  2153. /*
  2154. * DLM_MIGRATE_LOCKRES
  2155. */
  2156. static int dlm_migrate_lockres(struct dlm_ctxt *dlm,
  2157. struct dlm_lock_resource *res, u8 target)
  2158. {
  2159. struct dlm_master_list_entry *mle = NULL;
  2160. struct dlm_master_list_entry *oldmle = NULL;
  2161. struct dlm_migratable_lockres *mres = NULL;
  2162. int ret = 0;
  2163. const char *name;
  2164. unsigned int namelen;
  2165. int mle_added = 0;
  2166. int wake = 0;
  2167. if (!dlm_grab(dlm))
  2168. return -EINVAL;
  2169. BUG_ON(target == O2NM_MAX_NODES);
  2170. name = res->lockname.name;
  2171. namelen = res->lockname.len;
  2172. mlog(0, "%s: Migrating %.*s to node %u\n", dlm->name, namelen, name,
  2173. target);
  2174. /* preallocate up front. if this fails, abort */
  2175. ret = -ENOMEM;
  2176. mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_NOFS);
  2177. if (!mres) {
  2178. mlog_errno(ret);
  2179. goto leave;
  2180. }
  2181. mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
  2182. if (!mle) {
  2183. mlog_errno(ret);
  2184. goto leave;
  2185. }
  2186. ret = 0;
  2187. /*
  2188. * clear any existing master requests and
  2189. * add the migration mle to the list
  2190. */
  2191. spin_lock(&dlm->spinlock);
  2192. spin_lock(&dlm->master_lock);
  2193. ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name,
  2194. namelen, target, dlm->node_num);
  2195. spin_unlock(&dlm->master_lock);
  2196. spin_unlock(&dlm->spinlock);
  2197. if (ret == -EEXIST) {
  2198. mlog(0, "another process is already migrating it\n");
  2199. goto fail;
  2200. }
  2201. mle_added = 1;
  2202. /*
  2203. * set the MIGRATING flag and flush asts
  2204. * if we fail after this we need to re-dirty the lockres
  2205. */
  2206. if (dlm_mark_lockres_migrating(dlm, res, target) < 0) {
  2207. mlog(ML_ERROR, "tried to migrate %.*s to %u, but "
  2208. "the target went down.\n", res->lockname.len,
  2209. res->lockname.name, target);
  2210. spin_lock(&res->spinlock);
  2211. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2212. wake = 1;
  2213. spin_unlock(&res->spinlock);
  2214. ret = -EINVAL;
  2215. }
  2216. fail:
  2217. if (oldmle) {
  2218. /* master is known, detach if not already detached */
  2219. dlm_mle_detach_hb_events(dlm, oldmle);
  2220. dlm_put_mle(oldmle);
  2221. }
  2222. if (ret < 0) {
  2223. if (mle_added) {
  2224. dlm_mle_detach_hb_events(dlm, mle);
  2225. dlm_put_mle(mle);
  2226. } else if (mle) {
  2227. kmem_cache_free(dlm_mle_cache, mle);
  2228. mle = NULL;
  2229. }
  2230. goto leave;
  2231. }
  2232. /*
  2233. * at this point, we have a migration target, an mle
  2234. * in the master list, and the MIGRATING flag set on
  2235. * the lockres
  2236. */
  2237. /* now that remote nodes are spinning on the MIGRATING flag,
  2238. * ensure that all assert_master work is flushed. */
  2239. flush_workqueue(dlm->dlm_worker);
  2240. /* get an extra reference on the mle.
  2241. * otherwise the assert_master from the new
  2242. * master will destroy this.
  2243. * also, make sure that all callers of dlm_get_mle
  2244. * take both dlm->spinlock and dlm->master_lock */
  2245. spin_lock(&dlm->spinlock);
  2246. spin_lock(&dlm->master_lock);
  2247. dlm_get_mle_inuse(mle);
  2248. spin_unlock(&dlm->master_lock);
  2249. spin_unlock(&dlm->spinlock);
  2250. /* notify new node and send all lock state */
  2251. /* call send_one_lockres with migration flag.
  2252. * this serves as notice to the target node that a
  2253. * migration is starting. */
  2254. ret = dlm_send_one_lockres(dlm, res, mres, target,
  2255. DLM_MRES_MIGRATION);
  2256. if (ret < 0) {
  2257. mlog(0, "migration to node %u failed with %d\n",
  2258. target, ret);
  2259. /* migration failed, detach and clean up mle */
  2260. dlm_mle_detach_hb_events(dlm, mle);
  2261. dlm_put_mle(mle);
  2262. dlm_put_mle_inuse(mle);
  2263. spin_lock(&res->spinlock);
  2264. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2265. wake = 1;
  2266. spin_unlock(&res->spinlock);
  2267. if (dlm_is_host_down(ret))
  2268. dlm_wait_for_node_death(dlm, target,
  2269. DLM_NODE_DEATH_WAIT_MAX);
  2270. goto leave;
  2271. }
  2272. /* at this point, the target sends a message to all nodes,
  2273. * (using dlm_do_migrate_request). this node is skipped since
  2274. * we had to put an mle in the list to begin the process. this
  2275. * node now waits for target to do an assert master. this node
  2276. * will be the last one notified, ensuring that the migration
  2277. * is complete everywhere. if the target dies while this is
  2278. * going on, some nodes could potentially see the target as the
  2279. * master, so it is important that my recovery finds the migration
  2280. * mle and sets the master to UNKNOWN. */
  2281. /* wait for new node to assert master */
  2282. while (1) {
  2283. ret = wait_event_interruptible_timeout(mle->wq,
  2284. (atomic_read(&mle->woken) == 1),
  2285. msecs_to_jiffies(5000));
  2286. if (ret >= 0) {
  2287. if (atomic_read(&mle->woken) == 1 ||
  2288. res->owner == target)
  2289. break;
  2290. mlog(0, "%s:%.*s: timed out during migration\n",
  2291. dlm->name, res->lockname.len, res->lockname.name);
  2292. /* avoid hang during shutdown when migrating lockres
  2293. * to a node which also goes down */
  2294. if (dlm_is_node_dead(dlm, target)) {
  2295. mlog(0, "%s:%.*s: expected migration "
  2296. "target %u is no longer up, restarting\n",
  2297. dlm->name, res->lockname.len,
  2298. res->lockname.name, target);
  2299. ret = -EINVAL;
  2300. /* migration failed, detach and clean up mle */
  2301. dlm_mle_detach_hb_events(dlm, mle);
  2302. dlm_put_mle(mle);
  2303. dlm_put_mle_inuse(mle);
  2304. spin_lock(&res->spinlock);
  2305. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2306. wake = 1;
  2307. spin_unlock(&res->spinlock);
  2308. goto leave;
  2309. }
  2310. } else
  2311. mlog(0, "%s:%.*s: caught signal during migration\n",
  2312. dlm->name, res->lockname.len, res->lockname.name);
  2313. }
  2314. /* all done, set the owner, clear the flag */
  2315. spin_lock(&res->spinlock);
  2316. dlm_set_lockres_owner(dlm, res, target);
  2317. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2318. dlm_remove_nonlocal_locks(dlm, res);
  2319. spin_unlock(&res->spinlock);
  2320. wake_up(&res->wq);
  2321. /* master is known, detach if not already detached */
  2322. dlm_mle_detach_hb_events(dlm, mle);
  2323. dlm_put_mle_inuse(mle);
  2324. ret = 0;
  2325. dlm_lockres_calc_usage(dlm, res);
  2326. leave:
  2327. /* re-dirty the lockres if we failed */
  2328. if (ret < 0)
  2329. dlm_kick_thread(dlm, res);
  2330. /* wake up waiters if the MIGRATING flag got set
  2331. * but migration failed */
  2332. if (wake)
  2333. wake_up(&res->wq);
  2334. if (mres)
  2335. free_page((unsigned long)mres);
  2336. dlm_put(dlm);
  2337. mlog(0, "%s: Migrating %.*s to %u, returns %d\n", dlm->name, namelen,
  2338. name, target, ret);
  2339. return ret;
  2340. }
  2341. #define DLM_MIGRATION_RETRY_MS 100
  2342. /*
  2343. * Should be called only after beginning the domain leave process.
  2344. * There should not be any remaining locks on nonlocal lock resources,
  2345. * and there should be no local locks left on locally mastered resources.
  2346. *
  2347. * Called with the dlm spinlock held, may drop it to do migration, but
  2348. * will re-acquire before exit.
  2349. *
  2350. * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped
  2351. */
  2352. int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
  2353. {
  2354. int ret;
  2355. int lock_dropped = 0;
  2356. u8 target = O2NM_MAX_NODES;
  2357. assert_spin_locked(&dlm->spinlock);
  2358. spin_lock(&res->spinlock);
  2359. if (dlm_is_lockres_migrateable(dlm, res))
  2360. target = dlm_pick_migration_target(dlm, res);
  2361. spin_unlock(&res->spinlock);
  2362. if (target == O2NM_MAX_NODES)
  2363. goto leave;
  2364. /* Wheee! Migrate lockres here! Will sleep so drop spinlock. */
  2365. spin_unlock(&dlm->spinlock);
  2366. lock_dropped = 1;
  2367. ret = dlm_migrate_lockres(dlm, res, target);
  2368. if (ret)
  2369. mlog(0, "%s: res %.*s, Migrate to node %u failed with %d\n",
  2370. dlm->name, res->lockname.len, res->lockname.name,
  2371. target, ret);
  2372. spin_lock(&dlm->spinlock);
  2373. leave:
  2374. return lock_dropped;
  2375. }
  2376. int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  2377. {
  2378. int ret;
  2379. spin_lock(&dlm->ast_lock);
  2380. spin_lock(&lock->spinlock);
  2381. ret = (list_empty(&lock->bast_list) && !lock->bast_pending);
  2382. spin_unlock(&lock->spinlock);
  2383. spin_unlock(&dlm->ast_lock);
  2384. return ret;
  2385. }
  2386. static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
  2387. struct dlm_lock_resource *res,
  2388. u8 mig_target)
  2389. {
  2390. int can_proceed;
  2391. spin_lock(&res->spinlock);
  2392. can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING);
  2393. spin_unlock(&res->spinlock);
  2394. /* target has died, so make the caller break out of the
  2395. * wait_event, but caller must recheck the domain_map */
  2396. spin_lock(&dlm->spinlock);
  2397. if (!test_bit(mig_target, dlm->domain_map))
  2398. can_proceed = 1;
  2399. spin_unlock(&dlm->spinlock);
  2400. return can_proceed;
  2401. }
  2402. static int dlm_lockres_is_dirty(struct dlm_ctxt *dlm,
  2403. struct dlm_lock_resource *res)
  2404. {
  2405. int ret;
  2406. spin_lock(&res->spinlock);
  2407. ret = !!(res->state & DLM_LOCK_RES_DIRTY);
  2408. spin_unlock(&res->spinlock);
  2409. return ret;
  2410. }
  2411. static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
  2412. struct dlm_lock_resource *res,
  2413. u8 target)
  2414. {
  2415. int ret = 0;
  2416. mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
  2417. res->lockname.len, res->lockname.name, dlm->node_num,
  2418. target);
  2419. /* need to set MIGRATING flag on lockres. this is done by
  2420. * ensuring that all asts have been flushed for this lockres. */
  2421. spin_lock(&res->spinlock);
  2422. BUG_ON(res->migration_pending);
  2423. res->migration_pending = 1;
  2424. /* strategy is to reserve an extra ast then release
  2425. * it below, letting the release do all of the work */
  2426. __dlm_lockres_reserve_ast(res);
  2427. spin_unlock(&res->spinlock);
  2428. /* now flush all the pending asts */
  2429. dlm_kick_thread(dlm, res);
  2430. /* before waiting on DIRTY, block processes which may
  2431. * try to dirty the lockres before MIGRATING is set */
  2432. spin_lock(&res->spinlock);
  2433. BUG_ON(res->state & DLM_LOCK_RES_BLOCK_DIRTY);
  2434. res->state |= DLM_LOCK_RES_BLOCK_DIRTY;
  2435. spin_unlock(&res->spinlock);
  2436. /* now wait on any pending asts and the DIRTY state */
  2437. wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
  2438. dlm_lockres_release_ast(dlm, res);
  2439. mlog(0, "about to wait on migration_wq, dirty=%s\n",
  2440. res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
  2441. /* if the extra ref we just put was the final one, this
  2442. * will pass thru immediately. otherwise, we need to wait
  2443. * for the last ast to finish. */
  2444. again:
  2445. ret = wait_event_interruptible_timeout(dlm->migration_wq,
  2446. dlm_migration_can_proceed(dlm, res, target),
  2447. msecs_to_jiffies(1000));
  2448. if (ret < 0) {
  2449. mlog(0, "woken again: migrating? %s, dead? %s\n",
  2450. res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
  2451. test_bit(target, dlm->domain_map) ? "no":"yes");
  2452. } else {
  2453. mlog(0, "all is well: migrating? %s, dead? %s\n",
  2454. res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
  2455. test_bit(target, dlm->domain_map) ? "no":"yes");
  2456. }
  2457. if (!dlm_migration_can_proceed(dlm, res, target)) {
  2458. mlog(0, "trying again...\n");
  2459. goto again;
  2460. }
  2461. ret = 0;
  2462. /* did the target go down or die? */
  2463. spin_lock(&dlm->spinlock);
  2464. if (!test_bit(target, dlm->domain_map)) {
  2465. mlog(ML_ERROR, "aha. migration target %u just went down\n",
  2466. target);
  2467. ret = -EHOSTDOWN;
  2468. }
  2469. spin_unlock(&dlm->spinlock);
  2470. /*
  2471. * if target is down, we need to clear DLM_LOCK_RES_BLOCK_DIRTY for
  2472. * another try; otherwise, we are sure the MIGRATING state is there,
  2473. * drop the unneded state which blocked threads trying to DIRTY
  2474. */
  2475. spin_lock(&res->spinlock);
  2476. BUG_ON(!(res->state & DLM_LOCK_RES_BLOCK_DIRTY));
  2477. res->state &= ~DLM_LOCK_RES_BLOCK_DIRTY;
  2478. if (!ret)
  2479. BUG_ON(!(res->state & DLM_LOCK_RES_MIGRATING));
  2480. spin_unlock(&res->spinlock);
  2481. /*
  2482. * at this point:
  2483. *
  2484. * o the DLM_LOCK_RES_MIGRATING flag is set if target not down
  2485. * o there are no pending asts on this lockres
  2486. * o all processes trying to reserve an ast on this
  2487. * lockres must wait for the MIGRATING flag to clear
  2488. */
  2489. return ret;
  2490. }
  2491. /* last step in the migration process.
  2492. * original master calls this to free all of the dlm_lock
  2493. * structures that used to be for other nodes. */
  2494. static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
  2495. struct dlm_lock_resource *res)
  2496. {
  2497. struct list_head *queue = &res->granted;
  2498. int i, bit;
  2499. struct dlm_lock *lock, *next;
  2500. assert_spin_locked(&res->spinlock);
  2501. BUG_ON(res->owner == dlm->node_num);
  2502. for (i=0; i<3; i++) {
  2503. list_for_each_entry_safe(lock, next, queue, list) {
  2504. if (lock->ml.node != dlm->node_num) {
  2505. mlog(0, "putting lock for node %u\n",
  2506. lock->ml.node);
  2507. /* be extra careful */
  2508. BUG_ON(!list_empty(&lock->ast_list));
  2509. BUG_ON(!list_empty(&lock->bast_list));
  2510. BUG_ON(lock->ast_pending);
  2511. BUG_ON(lock->bast_pending);
  2512. dlm_lockres_clear_refmap_bit(dlm, res,
  2513. lock->ml.node);
  2514. list_del_init(&lock->list);
  2515. dlm_lock_put(lock);
  2516. /* In a normal unlock, we would have added a
  2517. * DLM_UNLOCK_FREE_LOCK action. Force it. */
  2518. dlm_lock_put(lock);
  2519. }
  2520. }
  2521. queue++;
  2522. }
  2523. bit = 0;
  2524. while (1) {
  2525. bit = find_next_bit(res->refmap, O2NM_MAX_NODES, bit);
  2526. if (bit >= O2NM_MAX_NODES)
  2527. break;
  2528. /* do not clear the local node reference, if there is a
  2529. * process holding this, let it drop the ref itself */
  2530. if (bit != dlm->node_num) {
  2531. mlog(0, "%s:%.*s: node %u had a ref to this "
  2532. "migrating lockres, clearing\n", dlm->name,
  2533. res->lockname.len, res->lockname.name, bit);
  2534. dlm_lockres_clear_refmap_bit(dlm, res, bit);
  2535. }
  2536. bit++;
  2537. }
  2538. }
  2539. /*
  2540. * Pick a node to migrate the lock resource to. This function selects a
  2541. * potential target based first on the locks and then on refmap. It skips
  2542. * nodes that are in the process of exiting the domain.
  2543. */
  2544. static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
  2545. struct dlm_lock_resource *res)
  2546. {
  2547. enum dlm_lockres_list idx;
  2548. struct list_head *queue = &res->granted;
  2549. struct dlm_lock *lock;
  2550. int noderef;
  2551. u8 nodenum = O2NM_MAX_NODES;
  2552. assert_spin_locked(&dlm->spinlock);
  2553. assert_spin_locked(&res->spinlock);
  2554. /* Go through all the locks */
  2555. for (idx = DLM_GRANTED_LIST; idx <= DLM_BLOCKED_LIST; idx++) {
  2556. queue = dlm_list_idx_to_ptr(res, idx);
  2557. list_for_each_entry(lock, queue, list) {
  2558. if (lock->ml.node == dlm->node_num)
  2559. continue;
  2560. if (test_bit(lock->ml.node, dlm->exit_domain_map))
  2561. continue;
  2562. nodenum = lock->ml.node;
  2563. goto bail;
  2564. }
  2565. }
  2566. /* Go thru the refmap */
  2567. noderef = -1;
  2568. while (1) {
  2569. noderef = find_next_bit(res->refmap, O2NM_MAX_NODES,
  2570. noderef + 1);
  2571. if (noderef >= O2NM_MAX_NODES)
  2572. break;
  2573. if (noderef == dlm->node_num)
  2574. continue;
  2575. if (test_bit(noderef, dlm->exit_domain_map))
  2576. continue;
  2577. nodenum = noderef;
  2578. goto bail;
  2579. }
  2580. bail:
  2581. return nodenum;
  2582. }
  2583. /* this is called by the new master once all lockres
  2584. * data has been received */
  2585. static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
  2586. struct dlm_lock_resource *res,
  2587. u8 master, u8 new_master,
  2588. struct dlm_node_iter *iter)
  2589. {
  2590. struct dlm_migrate_request migrate;
  2591. int ret, skip, status = 0;
  2592. int nodenum;
  2593. memset(&migrate, 0, sizeof(migrate));
  2594. migrate.namelen = res->lockname.len;
  2595. memcpy(migrate.name, res->lockname.name, migrate.namelen);
  2596. migrate.new_master = new_master;
  2597. migrate.master = master;
  2598. ret = 0;
  2599. /* send message to all nodes, except the master and myself */
  2600. while ((nodenum = dlm_node_iter_next(iter)) >= 0) {
  2601. if (nodenum == master ||
  2602. nodenum == new_master)
  2603. continue;
  2604. /* We could race exit domain. If exited, skip. */
  2605. spin_lock(&dlm->spinlock);
  2606. skip = (!test_bit(nodenum, dlm->domain_map));
  2607. spin_unlock(&dlm->spinlock);
  2608. if (skip) {
  2609. clear_bit(nodenum, iter->node_map);
  2610. continue;
  2611. }
  2612. ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  2613. &migrate, sizeof(migrate), nodenum,
  2614. &status);
  2615. if (ret < 0) {
  2616. mlog(ML_ERROR, "%s: res %.*s, Error %d send "
  2617. "MIGRATE_REQUEST to node %u\n", dlm->name,
  2618. migrate.namelen, migrate.name, ret, nodenum);
  2619. if (!dlm_is_host_down(ret)) {
  2620. mlog(ML_ERROR, "unhandled error=%d!\n", ret);
  2621. BUG();
  2622. }
  2623. clear_bit(nodenum, iter->node_map);
  2624. ret = 0;
  2625. } else if (status < 0) {
  2626. mlog(0, "migrate request (node %u) returned %d!\n",
  2627. nodenum, status);
  2628. ret = status;
  2629. } else if (status == DLM_MIGRATE_RESPONSE_MASTERY_REF) {
  2630. /* during the migration request we short-circuited
  2631. * the mastery of the lockres. make sure we have
  2632. * a mastery ref for nodenum */
  2633. mlog(0, "%s:%.*s: need ref for node %u\n",
  2634. dlm->name, res->lockname.len, res->lockname.name,
  2635. nodenum);
  2636. spin_lock(&res->spinlock);
  2637. dlm_lockres_set_refmap_bit(dlm, res, nodenum);
  2638. spin_unlock(&res->spinlock);
  2639. }
  2640. }
  2641. if (ret < 0)
  2642. mlog_errno(ret);
  2643. mlog(0, "returning ret=%d\n", ret);
  2644. return ret;
  2645. }
  2646. /* if there is an existing mle for this lockres, we now know who the master is.
  2647. * (the one who sent us *this* message) we can clear it up right away.
  2648. * since the process that put the mle on the list still has a reference to it,
  2649. * we can unhash it now, set the master and wake the process. as a result,
  2650. * we will have no mle in the list to start with. now we can add an mle for
  2651. * the migration and this should be the only one found for those scanning the
  2652. * list. */
  2653. int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
  2654. void **ret_data)
  2655. {
  2656. struct dlm_ctxt *dlm = data;
  2657. struct dlm_lock_resource *res = NULL;
  2658. struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf;
  2659. struct dlm_master_list_entry *mle = NULL, *oldmle = NULL;
  2660. const char *name;
  2661. unsigned int namelen, hash;
  2662. int ret = 0;
  2663. if (!dlm_grab(dlm))
  2664. return -EINVAL;
  2665. name = migrate->name;
  2666. namelen = migrate->namelen;
  2667. hash = dlm_lockid_hash(name, namelen);
  2668. /* preallocate.. if this fails, abort */
  2669. mle = kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
  2670. if (!mle) {
  2671. ret = -ENOMEM;
  2672. goto leave;
  2673. }
  2674. /* check for pre-existing lock */
  2675. spin_lock(&dlm->spinlock);
  2676. res = __dlm_lookup_lockres(dlm, name, namelen, hash);
  2677. if (res) {
  2678. spin_lock(&res->spinlock);
  2679. if (res->state & DLM_LOCK_RES_RECOVERING) {
  2680. /* if all is working ok, this can only mean that we got
  2681. * a migrate request from a node that we now see as
  2682. * dead. what can we do here? drop it to the floor? */
  2683. spin_unlock(&res->spinlock);
  2684. mlog(ML_ERROR, "Got a migrate request, but the "
  2685. "lockres is marked as recovering!");
  2686. kmem_cache_free(dlm_mle_cache, mle);
  2687. ret = -EINVAL; /* need a better solution */
  2688. goto unlock;
  2689. }
  2690. res->state |= DLM_LOCK_RES_MIGRATING;
  2691. spin_unlock(&res->spinlock);
  2692. }
  2693. spin_lock(&dlm->master_lock);
  2694. /* ignore status. only nonzero status would BUG. */
  2695. ret = dlm_add_migration_mle(dlm, res, mle, &oldmle,
  2696. name, namelen,
  2697. migrate->new_master,
  2698. migrate->master);
  2699. spin_unlock(&dlm->master_lock);
  2700. unlock:
  2701. spin_unlock(&dlm->spinlock);
  2702. if (oldmle) {
  2703. /* master is known, detach if not already detached */
  2704. dlm_mle_detach_hb_events(dlm, oldmle);
  2705. dlm_put_mle(oldmle);
  2706. }
  2707. if (res)
  2708. dlm_lockres_put(res);
  2709. leave:
  2710. dlm_put(dlm);
  2711. return ret;
  2712. }
  2713. /* must be holding dlm->spinlock and dlm->master_lock
  2714. * when adding a migration mle, we can clear any other mles
  2715. * in the master list because we know with certainty that
  2716. * the master is "master". so we remove any old mle from
  2717. * the list after setting it's master field, and then add
  2718. * the new migration mle. this way we can hold with the rule
  2719. * of having only one mle for a given lock name at all times. */
  2720. static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
  2721. struct dlm_lock_resource *res,
  2722. struct dlm_master_list_entry *mle,
  2723. struct dlm_master_list_entry **oldmle,
  2724. const char *name, unsigned int namelen,
  2725. u8 new_master, u8 master)
  2726. {
  2727. int found;
  2728. int ret = 0;
  2729. *oldmle = NULL;
  2730. assert_spin_locked(&dlm->spinlock);
  2731. assert_spin_locked(&dlm->master_lock);
  2732. /* caller is responsible for any ref taken here on oldmle */
  2733. found = dlm_find_mle(dlm, oldmle, (char *)name, namelen);
  2734. if (found) {
  2735. struct dlm_master_list_entry *tmp = *oldmle;
  2736. spin_lock(&tmp->spinlock);
  2737. if (tmp->type == DLM_MLE_MIGRATION) {
  2738. if (master == dlm->node_num) {
  2739. /* ah another process raced me to it */
  2740. mlog(0, "tried to migrate %.*s, but some "
  2741. "process beat me to it\n",
  2742. namelen, name);
  2743. ret = -EEXIST;
  2744. } else {
  2745. /* bad. 2 NODES are trying to migrate! */
  2746. mlog(ML_ERROR, "migration error mle: "
  2747. "master=%u new_master=%u // request: "
  2748. "master=%u new_master=%u // "
  2749. "lockres=%.*s\n",
  2750. tmp->master, tmp->new_master,
  2751. master, new_master,
  2752. namelen, name);
  2753. BUG();
  2754. }
  2755. } else {
  2756. /* this is essentially what assert_master does */
  2757. tmp->master = master;
  2758. atomic_set(&tmp->woken, 1);
  2759. wake_up(&tmp->wq);
  2760. /* remove it so that only one mle will be found */
  2761. __dlm_unlink_mle(dlm, tmp);
  2762. __dlm_mle_detach_hb_events(dlm, tmp);
  2763. if (tmp->type == DLM_MLE_MASTER) {
  2764. ret = DLM_MIGRATE_RESPONSE_MASTERY_REF;
  2765. mlog(0, "%s:%.*s: master=%u, newmaster=%u, "
  2766. "telling master to get ref "
  2767. "for cleared out mle during "
  2768. "migration\n", dlm->name,
  2769. namelen, name, master,
  2770. new_master);
  2771. }
  2772. }
  2773. spin_unlock(&tmp->spinlock);
  2774. }
  2775. /* now add a migration mle to the tail of the list */
  2776. dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
  2777. mle->new_master = new_master;
  2778. /* the new master will be sending an assert master for this.
  2779. * at that point we will get the refmap reference */
  2780. mle->master = master;
  2781. /* do this for consistency with other mle types */
  2782. set_bit(new_master, mle->maybe_map);
  2783. __dlm_insert_mle(dlm, mle);
  2784. return ret;
  2785. }
  2786. /*
  2787. * Sets the owner of the lockres, associated to the mle, to UNKNOWN
  2788. */
  2789. static struct dlm_lock_resource *dlm_reset_mleres_owner(struct dlm_ctxt *dlm,
  2790. struct dlm_master_list_entry *mle)
  2791. {
  2792. struct dlm_lock_resource *res;
  2793. /* Find the lockres associated to the mle and set its owner to UNK */
  2794. res = __dlm_lookup_lockres(dlm, mle->mname, mle->mnamelen,
  2795. mle->mnamehash);
  2796. if (res) {
  2797. spin_unlock(&dlm->master_lock);
  2798. /* move lockres onto recovery list */
  2799. spin_lock(&res->spinlock);
  2800. dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
  2801. dlm_move_lockres_to_recovery_list(dlm, res);
  2802. spin_unlock(&res->spinlock);
  2803. dlm_lockres_put(res);
  2804. /* about to get rid of mle, detach from heartbeat */
  2805. __dlm_mle_detach_hb_events(dlm, mle);
  2806. /* dump the mle */
  2807. spin_lock(&dlm->master_lock);
  2808. __dlm_put_mle(mle);
  2809. spin_unlock(&dlm->master_lock);
  2810. }
  2811. return res;
  2812. }
  2813. static void dlm_clean_migration_mle(struct dlm_ctxt *dlm,
  2814. struct dlm_master_list_entry *mle)
  2815. {
  2816. __dlm_mle_detach_hb_events(dlm, mle);
  2817. spin_lock(&mle->spinlock);
  2818. __dlm_unlink_mle(dlm, mle);
  2819. atomic_set(&mle->woken, 1);
  2820. spin_unlock(&mle->spinlock);
  2821. wake_up(&mle->wq);
  2822. }
  2823. static void dlm_clean_block_mle(struct dlm_ctxt *dlm,
  2824. struct dlm_master_list_entry *mle, u8 dead_node)
  2825. {
  2826. int bit;
  2827. BUG_ON(mle->type != DLM_MLE_BLOCK);
  2828. spin_lock(&mle->spinlock);
  2829. bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
  2830. if (bit != dead_node) {
  2831. mlog(0, "mle found, but dead node %u would not have been "
  2832. "master\n", dead_node);
  2833. spin_unlock(&mle->spinlock);
  2834. } else {
  2835. /* Must drop the refcount by one since the assert_master will
  2836. * never arrive. This may result in the mle being unlinked and
  2837. * freed, but there may still be a process waiting in the
  2838. * dlmlock path which is fine. */
  2839. mlog(0, "node %u was expected master\n", dead_node);
  2840. atomic_set(&mle->woken, 1);
  2841. spin_unlock(&mle->spinlock);
  2842. wake_up(&mle->wq);
  2843. /* Do not need events any longer, so detach from heartbeat */
  2844. __dlm_mle_detach_hb_events(dlm, mle);
  2845. __dlm_put_mle(mle);
  2846. }
  2847. }
  2848. void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
  2849. {
  2850. struct dlm_master_list_entry *mle;
  2851. struct dlm_lock_resource *res;
  2852. struct hlist_head *bucket;
  2853. struct hlist_node *tmp;
  2854. unsigned int i;
  2855. mlog(0, "dlm=%s, dead node=%u\n", dlm->name, dead_node);
  2856. top:
  2857. assert_spin_locked(&dlm->spinlock);
  2858. /* clean the master list */
  2859. spin_lock(&dlm->master_lock);
  2860. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  2861. bucket = dlm_master_hash(dlm, i);
  2862. hlist_for_each_entry_safe(mle, tmp, bucket, master_hash_node) {
  2863. BUG_ON(mle->type != DLM_MLE_BLOCK &&
  2864. mle->type != DLM_MLE_MASTER &&
  2865. mle->type != DLM_MLE_MIGRATION);
  2866. /* MASTER mles are initiated locally. The waiting
  2867. * process will notice the node map change shortly.
  2868. * Let that happen as normal. */
  2869. if (mle->type == DLM_MLE_MASTER)
  2870. continue;
  2871. /* BLOCK mles are initiated by other nodes. Need to
  2872. * clean up if the dead node would have been the
  2873. * master. */
  2874. if (mle->type == DLM_MLE_BLOCK) {
  2875. dlm_clean_block_mle(dlm, mle, dead_node);
  2876. continue;
  2877. }
  2878. /* Everything else is a MIGRATION mle */
  2879. /* The rule for MIGRATION mles is that the master
  2880. * becomes UNKNOWN if *either* the original or the new
  2881. * master dies. All UNKNOWN lockres' are sent to
  2882. * whichever node becomes the recovery master. The new
  2883. * master is responsible for determining if there is
  2884. * still a master for this lockres, or if he needs to
  2885. * take over mastery. Either way, this node should
  2886. * expect another message to resolve this. */
  2887. if (mle->master != dead_node &&
  2888. mle->new_master != dead_node)
  2889. continue;
  2890. /* If we have reached this point, this mle needs to be
  2891. * removed from the list and freed. */
  2892. dlm_clean_migration_mle(dlm, mle);
  2893. mlog(0, "%s: node %u died during migration from "
  2894. "%u to %u!\n", dlm->name, dead_node, mle->master,
  2895. mle->new_master);
  2896. /* If we find a lockres associated with the mle, we've
  2897. * hit this rare case that messes up our lock ordering.
  2898. * If so, we need to drop the master lock so that we can
  2899. * take the lockres lock, meaning that we will have to
  2900. * restart from the head of list. */
  2901. res = dlm_reset_mleres_owner(dlm, mle);
  2902. if (res)
  2903. /* restart */
  2904. goto top;
  2905. /* This may be the last reference */
  2906. __dlm_put_mle(mle);
  2907. }
  2908. }
  2909. spin_unlock(&dlm->master_lock);
  2910. }
  2911. int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  2912. u8 old_master)
  2913. {
  2914. struct dlm_node_iter iter;
  2915. int ret = 0;
  2916. spin_lock(&dlm->spinlock);
  2917. dlm_node_iter_init(dlm->domain_map, &iter);
  2918. clear_bit(old_master, iter.node_map);
  2919. clear_bit(dlm->node_num, iter.node_map);
  2920. spin_unlock(&dlm->spinlock);
  2921. /* ownership of the lockres is changing. account for the
  2922. * mastery reference here since old_master will briefly have
  2923. * a reference after the migration completes */
  2924. spin_lock(&res->spinlock);
  2925. dlm_lockres_set_refmap_bit(dlm, res, old_master);
  2926. spin_unlock(&res->spinlock);
  2927. mlog(0, "now time to do a migrate request to other nodes\n");
  2928. ret = dlm_do_migrate_request(dlm, res, old_master,
  2929. dlm->node_num, &iter);
  2930. if (ret < 0) {
  2931. mlog_errno(ret);
  2932. goto leave;
  2933. }
  2934. mlog(0, "doing assert master of %.*s to all except the original node\n",
  2935. res->lockname.len, res->lockname.name);
  2936. /* this call now finishes out the nodemap
  2937. * even if one or more nodes die */
  2938. ret = dlm_do_assert_master(dlm, res, iter.node_map,
  2939. DLM_ASSERT_MASTER_FINISH_MIGRATION);
  2940. if (ret < 0) {
  2941. /* no longer need to retry. all living nodes contacted. */
  2942. mlog_errno(ret);
  2943. ret = 0;
  2944. }
  2945. memset(iter.node_map, 0, sizeof(iter.node_map));
  2946. set_bit(old_master, iter.node_map);
  2947. mlog(0, "doing assert master of %.*s back to %u\n",
  2948. res->lockname.len, res->lockname.name, old_master);
  2949. ret = dlm_do_assert_master(dlm, res, iter.node_map,
  2950. DLM_ASSERT_MASTER_FINISH_MIGRATION);
  2951. if (ret < 0) {
  2952. mlog(0, "assert master to original master failed "
  2953. "with %d.\n", ret);
  2954. /* the only nonzero status here would be because of
  2955. * a dead original node. we're done. */
  2956. ret = 0;
  2957. }
  2958. /* all done, set the owner, clear the flag */
  2959. spin_lock(&res->spinlock);
  2960. dlm_set_lockres_owner(dlm, res, dlm->node_num);
  2961. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2962. spin_unlock(&res->spinlock);
  2963. /* re-dirty it on the new master */
  2964. dlm_kick_thread(dlm, res);
  2965. wake_up(&res->wq);
  2966. leave:
  2967. return ret;
  2968. }
  2969. /*
  2970. * LOCKRES AST REFCOUNT
  2971. * this is integral to migration
  2972. */
  2973. /* for future intent to call an ast, reserve one ahead of time.
  2974. * this should be called only after waiting on the lockres
  2975. * with dlm_wait_on_lockres, and while still holding the
  2976. * spinlock after the call. */
  2977. void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res)
  2978. {
  2979. assert_spin_locked(&res->spinlock);
  2980. if (res->state & DLM_LOCK_RES_MIGRATING) {
  2981. __dlm_print_one_lock_resource(res);
  2982. }
  2983. BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
  2984. atomic_inc(&res->asts_reserved);
  2985. }
  2986. /*
  2987. * used to drop the reserved ast, either because it went unused,
  2988. * or because the ast/bast was actually called.
  2989. *
  2990. * also, if there is a pending migration on this lockres,
  2991. * and this was the last pending ast on the lockres,
  2992. * atomically set the MIGRATING flag before we drop the lock.
  2993. * this is how we ensure that migration can proceed with no
  2994. * asts in progress. note that it is ok if the state of the
  2995. * queues is such that a lock should be granted in the future
  2996. * or that a bast should be fired, because the new master will
  2997. * shuffle the lists on this lockres as soon as it is migrated.
  2998. */
  2999. void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
  3000. struct dlm_lock_resource *res)
  3001. {
  3002. if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock))
  3003. return;
  3004. if (!res->migration_pending) {
  3005. spin_unlock(&res->spinlock);
  3006. return;
  3007. }
  3008. BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
  3009. res->migration_pending = 0;
  3010. res->state |= DLM_LOCK_RES_MIGRATING;
  3011. spin_unlock(&res->spinlock);
  3012. wake_up(&res->wq);
  3013. wake_up(&dlm->migration_wq);
  3014. }
  3015. void dlm_force_free_mles(struct dlm_ctxt *dlm)
  3016. {
  3017. int i;
  3018. struct hlist_head *bucket;
  3019. struct dlm_master_list_entry *mle;
  3020. struct hlist_node *tmp;
  3021. /*
  3022. * We notified all other nodes that we are exiting the domain and
  3023. * marked the dlm state to DLM_CTXT_LEAVING. If any mles are still
  3024. * around we force free them and wake any processes that are waiting
  3025. * on the mles
  3026. */
  3027. spin_lock(&dlm->spinlock);
  3028. spin_lock(&dlm->master_lock);
  3029. BUG_ON(dlm->dlm_state != DLM_CTXT_LEAVING);
  3030. BUG_ON((find_next_bit(dlm->domain_map, O2NM_MAX_NODES, 0) < O2NM_MAX_NODES));
  3031. for (i = 0; i < DLM_HASH_BUCKETS; i++) {
  3032. bucket = dlm_master_hash(dlm, i);
  3033. hlist_for_each_entry_safe(mle, tmp, bucket, master_hash_node) {
  3034. if (mle->type != DLM_MLE_BLOCK) {
  3035. mlog(ML_ERROR, "bad mle: %p\n", mle);
  3036. dlm_print_one_mle(mle);
  3037. }
  3038. atomic_set(&mle->woken, 1);
  3039. wake_up(&mle->wq);
  3040. __dlm_unlink_mle(dlm, mle);
  3041. __dlm_mle_detach_hb_events(dlm, mle);
  3042. __dlm_put_mle(mle);
  3043. }
  3044. }
  3045. spin_unlock(&dlm->master_lock);
  3046. spin_unlock(&dlm->spinlock);
  3047. }