dlmmaster.c 94 KB

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