dlmmaster.c 99 KB

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