dlmmaster.c 96 KB

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