dlmmaster.c 95 KB

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