dlmmaster.c 92 KB

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