dlmmaster.c 95 KB

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