dlmmaster.c 94 KB

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