dlmmaster.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478
  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. struct list_head *iter;
  175. mlog(ML_NOTICE, "dumping all mles for domain %s:\n", dlm->name);
  176. spin_lock(&dlm->master_lock);
  177. list_for_each(iter, &dlm->master_list) {
  178. mle = list_entry(iter, struct dlm_master_list_entry, list);
  179. dlm_print_one_mle(mle);
  180. }
  181. spin_unlock(&dlm->master_lock);
  182. }
  183. int dlm_dump_all_mles(const char __user *data, unsigned int len)
  184. {
  185. struct list_head *iter;
  186. struct dlm_ctxt *dlm;
  187. spin_lock(&dlm_domain_lock);
  188. list_for_each(iter, &dlm_domains) {
  189. dlm = list_entry (iter, struct dlm_ctxt, list);
  190. mlog(ML_NOTICE, "found dlm: %p, name=%s\n", dlm, dlm->name);
  191. dlm_dump_mles(dlm);
  192. }
  193. spin_unlock(&dlm_domain_lock);
  194. return len;
  195. }
  196. EXPORT_SYMBOL_GPL(dlm_dump_all_mles);
  197. #endif /* 0 */
  198. static struct kmem_cache *dlm_mle_cache = NULL;
  199. static void dlm_mle_release(struct kref *kref);
  200. static void dlm_init_mle(struct dlm_master_list_entry *mle,
  201. enum dlm_mle_type type,
  202. struct dlm_ctxt *dlm,
  203. struct dlm_lock_resource *res,
  204. const char *name,
  205. unsigned int namelen);
  206. static void dlm_put_mle(struct dlm_master_list_entry *mle);
  207. static void __dlm_put_mle(struct dlm_master_list_entry *mle);
  208. static int dlm_find_mle(struct dlm_ctxt *dlm,
  209. struct dlm_master_list_entry **mle,
  210. char *name, unsigned int namelen);
  211. static int dlm_do_master_request(struct dlm_lock_resource *res,
  212. struct dlm_master_list_entry *mle, int to);
  213. static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
  214. struct dlm_lock_resource *res,
  215. struct dlm_master_list_entry *mle,
  216. int *blocked);
  217. static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
  218. struct dlm_lock_resource *res,
  219. struct dlm_master_list_entry *mle,
  220. int blocked);
  221. static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
  222. struct dlm_lock_resource *res,
  223. struct dlm_master_list_entry *mle,
  224. struct dlm_master_list_entry **oldmle,
  225. const char *name, unsigned int namelen,
  226. u8 new_master, u8 master);
  227. static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
  228. struct dlm_lock_resource *res);
  229. static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
  230. struct dlm_lock_resource *res);
  231. static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
  232. struct dlm_lock_resource *res,
  233. u8 target);
  234. static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
  235. struct dlm_lock_resource *res);
  236. int dlm_is_host_down(int errno)
  237. {
  238. switch (errno) {
  239. case -EBADF:
  240. case -ECONNREFUSED:
  241. case -ENOTCONN:
  242. case -ECONNRESET:
  243. case -EPIPE:
  244. case -EHOSTDOWN:
  245. case -EHOSTUNREACH:
  246. case -ETIMEDOUT:
  247. case -ECONNABORTED:
  248. case -ENETDOWN:
  249. case -ENETUNREACH:
  250. case -ENETRESET:
  251. case -ESHUTDOWN:
  252. case -ENOPROTOOPT:
  253. case -EINVAL: /* if returned from our tcp code,
  254. this means there is no socket */
  255. return 1;
  256. }
  257. return 0;
  258. }
  259. /*
  260. * MASTER LIST FUNCTIONS
  261. */
  262. /*
  263. * regarding master list entries and heartbeat callbacks:
  264. *
  265. * in order to avoid sleeping and allocation that occurs in
  266. * heartbeat, master list entries are simply attached to the
  267. * dlm's established heartbeat callbacks. the mle is attached
  268. * when it is created, and since the dlm->spinlock is held at
  269. * that time, any heartbeat event will be properly discovered
  270. * by the mle. the mle needs to be detached from the
  271. * dlm->mle_hb_events list as soon as heartbeat events are no
  272. * longer useful to the mle, and before the mle is freed.
  273. *
  274. * as a general rule, heartbeat events are no longer needed by
  275. * the mle once an "answer" regarding the lock master has been
  276. * received.
  277. */
  278. static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm,
  279. struct dlm_master_list_entry *mle)
  280. {
  281. assert_spin_locked(&dlm->spinlock);
  282. list_add_tail(&mle->hb_events, &dlm->mle_hb_events);
  283. }
  284. static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
  285. struct dlm_master_list_entry *mle)
  286. {
  287. if (!list_empty(&mle->hb_events))
  288. list_del_init(&mle->hb_events);
  289. }
  290. static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
  291. struct dlm_master_list_entry *mle)
  292. {
  293. spin_lock(&dlm->spinlock);
  294. __dlm_mle_detach_hb_events(dlm, mle);
  295. spin_unlock(&dlm->spinlock);
  296. }
  297. static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle)
  298. {
  299. struct dlm_ctxt *dlm;
  300. dlm = mle->dlm;
  301. assert_spin_locked(&dlm->spinlock);
  302. assert_spin_locked(&dlm->master_lock);
  303. mle->inuse++;
  304. kref_get(&mle->mle_refs);
  305. }
  306. static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle)
  307. {
  308. struct dlm_ctxt *dlm;
  309. dlm = mle->dlm;
  310. spin_lock(&dlm->spinlock);
  311. spin_lock(&dlm->master_lock);
  312. mle->inuse--;
  313. __dlm_put_mle(mle);
  314. spin_unlock(&dlm->master_lock);
  315. spin_unlock(&dlm->spinlock);
  316. }
  317. /* remove from list and free */
  318. static void __dlm_put_mle(struct dlm_master_list_entry *mle)
  319. {
  320. struct dlm_ctxt *dlm;
  321. dlm = mle->dlm;
  322. assert_spin_locked(&dlm->spinlock);
  323. assert_spin_locked(&dlm->master_lock);
  324. if (!atomic_read(&mle->mle_refs.refcount)) {
  325. /* this may or may not crash, but who cares.
  326. * it's a BUG. */
  327. mlog(ML_ERROR, "bad mle: %p\n", mle);
  328. dlm_print_one_mle(mle);
  329. BUG();
  330. } else
  331. kref_put(&mle->mle_refs, dlm_mle_release);
  332. }
  333. /* must not have any spinlocks coming in */
  334. static void dlm_put_mle(struct dlm_master_list_entry *mle)
  335. {
  336. struct dlm_ctxt *dlm;
  337. dlm = mle->dlm;
  338. spin_lock(&dlm->spinlock);
  339. spin_lock(&dlm->master_lock);
  340. __dlm_put_mle(mle);
  341. spin_unlock(&dlm->master_lock);
  342. spin_unlock(&dlm->spinlock);
  343. }
  344. static inline void dlm_get_mle(struct dlm_master_list_entry *mle)
  345. {
  346. kref_get(&mle->mle_refs);
  347. }
  348. static void dlm_init_mle(struct dlm_master_list_entry *mle,
  349. enum dlm_mle_type type,
  350. struct dlm_ctxt *dlm,
  351. struct dlm_lock_resource *res,
  352. const char *name,
  353. unsigned int namelen)
  354. {
  355. assert_spin_locked(&dlm->spinlock);
  356. mle->dlm = dlm;
  357. mle->type = type;
  358. INIT_LIST_HEAD(&mle->list);
  359. INIT_LIST_HEAD(&mle->hb_events);
  360. memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
  361. spin_lock_init(&mle->spinlock);
  362. init_waitqueue_head(&mle->wq);
  363. atomic_set(&mle->woken, 0);
  364. kref_init(&mle->mle_refs);
  365. memset(mle->response_map, 0, sizeof(mle->response_map));
  366. mle->master = O2NM_MAX_NODES;
  367. mle->new_master = O2NM_MAX_NODES;
  368. mle->inuse = 0;
  369. if (mle->type == DLM_MLE_MASTER) {
  370. BUG_ON(!res);
  371. mle->u.res = res;
  372. } else if (mle->type == DLM_MLE_BLOCK) {
  373. BUG_ON(!name);
  374. memcpy(mle->u.name.name, name, namelen);
  375. mle->u.name.len = namelen;
  376. } else /* DLM_MLE_MIGRATION */ {
  377. BUG_ON(!name);
  378. memcpy(mle->u.name.name, name, namelen);
  379. mle->u.name.len = namelen;
  380. }
  381. /* copy off the node_map and register hb callbacks on our copy */
  382. memcpy(mle->node_map, dlm->domain_map, sizeof(mle->node_map));
  383. memcpy(mle->vote_map, dlm->domain_map, sizeof(mle->vote_map));
  384. clear_bit(dlm->node_num, mle->vote_map);
  385. clear_bit(dlm->node_num, mle->node_map);
  386. /* attach the mle to the domain node up/down events */
  387. __dlm_mle_attach_hb_events(dlm, mle);
  388. }
  389. /* returns 1 if found, 0 if not */
  390. static int dlm_find_mle(struct dlm_ctxt *dlm,
  391. struct dlm_master_list_entry **mle,
  392. char *name, unsigned int namelen)
  393. {
  394. struct dlm_master_list_entry *tmpmle;
  395. struct list_head *iter;
  396. assert_spin_locked(&dlm->master_lock);
  397. list_for_each(iter, &dlm->master_list) {
  398. tmpmle = list_entry(iter, struct dlm_master_list_entry, list);
  399. if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
  400. continue;
  401. dlm_get_mle(tmpmle);
  402. *mle = tmpmle;
  403. return 1;
  404. }
  405. return 0;
  406. }
  407. void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
  408. {
  409. struct dlm_master_list_entry *mle;
  410. struct list_head *iter;
  411. assert_spin_locked(&dlm->spinlock);
  412. list_for_each(iter, &dlm->mle_hb_events) {
  413. mle = list_entry(iter, struct dlm_master_list_entry,
  414. hb_events);
  415. if (node_up)
  416. dlm_mle_node_up(dlm, mle, NULL, idx);
  417. else
  418. dlm_mle_node_down(dlm, mle, NULL, idx);
  419. }
  420. }
  421. static void dlm_mle_node_down(struct dlm_ctxt *dlm,
  422. struct dlm_master_list_entry *mle,
  423. struct o2nm_node *node, int idx)
  424. {
  425. spin_lock(&mle->spinlock);
  426. if (!test_bit(idx, mle->node_map))
  427. mlog(0, "node %u already removed from nodemap!\n", idx);
  428. else
  429. clear_bit(idx, mle->node_map);
  430. spin_unlock(&mle->spinlock);
  431. }
  432. static void dlm_mle_node_up(struct dlm_ctxt *dlm,
  433. struct dlm_master_list_entry *mle,
  434. struct o2nm_node *node, int idx)
  435. {
  436. spin_lock(&mle->spinlock);
  437. if (test_bit(idx, mle->node_map))
  438. mlog(0, "node %u already in node map!\n", idx);
  439. else
  440. set_bit(idx, mle->node_map);
  441. spin_unlock(&mle->spinlock);
  442. }
  443. int dlm_init_mle_cache(void)
  444. {
  445. dlm_mle_cache = kmem_cache_create("dlm_mle_cache",
  446. sizeof(struct dlm_master_list_entry),
  447. 0, SLAB_HWCACHE_ALIGN,
  448. NULL, NULL);
  449. if (dlm_mle_cache == NULL)
  450. return -ENOMEM;
  451. return 0;
  452. }
  453. void dlm_destroy_mle_cache(void)
  454. {
  455. if (dlm_mle_cache)
  456. kmem_cache_destroy(dlm_mle_cache);
  457. }
  458. static void dlm_mle_release(struct kref *kref)
  459. {
  460. struct dlm_master_list_entry *mle;
  461. struct dlm_ctxt *dlm;
  462. mlog_entry_void();
  463. mle = container_of(kref, struct dlm_master_list_entry, mle_refs);
  464. dlm = mle->dlm;
  465. if (mle->type != DLM_MLE_MASTER) {
  466. mlog(0, "calling mle_release for %.*s, type %d\n",
  467. mle->u.name.len, mle->u.name.name, mle->type);
  468. } else {
  469. mlog(0, "calling mle_release for %.*s, type %d\n",
  470. mle->u.res->lockname.len,
  471. mle->u.res->lockname.name, mle->type);
  472. }
  473. assert_spin_locked(&dlm->spinlock);
  474. assert_spin_locked(&dlm->master_lock);
  475. /* remove from list if not already */
  476. if (!list_empty(&mle->list))
  477. list_del_init(&mle->list);
  478. /* detach the mle from the domain node up/down events */
  479. __dlm_mle_detach_hb_events(dlm, mle);
  480. /* NOTE: kfree under spinlock here.
  481. * if this is bad, we can move this to a freelist. */
  482. kmem_cache_free(dlm_mle_cache, mle);
  483. }
  484. /*
  485. * LOCK RESOURCE FUNCTIONS
  486. */
  487. static void dlm_set_lockres_owner(struct dlm_ctxt *dlm,
  488. struct dlm_lock_resource *res,
  489. u8 owner)
  490. {
  491. assert_spin_locked(&res->spinlock);
  492. mlog_entry("%.*s, %u\n", res->lockname.len, res->lockname.name, owner);
  493. if (owner == dlm->node_num)
  494. atomic_inc(&dlm->local_resources);
  495. else if (owner == DLM_LOCK_RES_OWNER_UNKNOWN)
  496. atomic_inc(&dlm->unknown_resources);
  497. else
  498. atomic_inc(&dlm->remote_resources);
  499. res->owner = owner;
  500. }
  501. void dlm_change_lockres_owner(struct dlm_ctxt *dlm,
  502. struct dlm_lock_resource *res, u8 owner)
  503. {
  504. assert_spin_locked(&res->spinlock);
  505. if (owner == res->owner)
  506. return;
  507. if (res->owner == dlm->node_num)
  508. atomic_dec(&dlm->local_resources);
  509. else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN)
  510. atomic_dec(&dlm->unknown_resources);
  511. else
  512. atomic_dec(&dlm->remote_resources);
  513. dlm_set_lockres_owner(dlm, res, owner);
  514. }
  515. static void dlm_lockres_release(struct kref *kref)
  516. {
  517. struct dlm_lock_resource *res;
  518. res = container_of(kref, struct dlm_lock_resource, refs);
  519. /* This should not happen -- all lockres' have a name
  520. * associated with them at init time. */
  521. BUG_ON(!res->lockname.name);
  522. mlog(0, "destroying lockres %.*s\n", res->lockname.len,
  523. res->lockname.name);
  524. if (!hlist_unhashed(&res->hash_node) ||
  525. !list_empty(&res->granted) ||
  526. !list_empty(&res->converting) ||
  527. !list_empty(&res->blocked) ||
  528. !list_empty(&res->dirty) ||
  529. !list_empty(&res->recovering) ||
  530. !list_empty(&res->purge)) {
  531. mlog(ML_ERROR,
  532. "Going to BUG for resource %.*s."
  533. " We're on a list! [%c%c%c%c%c%c%c]\n",
  534. res->lockname.len, res->lockname.name,
  535. !hlist_unhashed(&res->hash_node) ? 'H' : ' ',
  536. !list_empty(&res->granted) ? 'G' : ' ',
  537. !list_empty(&res->converting) ? 'C' : ' ',
  538. !list_empty(&res->blocked) ? 'B' : ' ',
  539. !list_empty(&res->dirty) ? 'D' : ' ',
  540. !list_empty(&res->recovering) ? 'R' : ' ',
  541. !list_empty(&res->purge) ? 'P' : ' ');
  542. dlm_print_one_lock_resource(res);
  543. }
  544. /* By the time we're ready to blow this guy away, we shouldn't
  545. * be on any lists. */
  546. BUG_ON(!hlist_unhashed(&res->hash_node));
  547. BUG_ON(!list_empty(&res->granted));
  548. BUG_ON(!list_empty(&res->converting));
  549. BUG_ON(!list_empty(&res->blocked));
  550. BUG_ON(!list_empty(&res->dirty));
  551. BUG_ON(!list_empty(&res->recovering));
  552. BUG_ON(!list_empty(&res->purge));
  553. kfree(res->lockname.name);
  554. kfree(res);
  555. }
  556. void dlm_lockres_put(struct dlm_lock_resource *res)
  557. {
  558. kref_put(&res->refs, dlm_lockres_release);
  559. }
  560. static void dlm_init_lockres(struct dlm_ctxt *dlm,
  561. struct dlm_lock_resource *res,
  562. const char *name, unsigned int namelen)
  563. {
  564. char *qname;
  565. /* If we memset here, we lose our reference to the kmalloc'd
  566. * res->lockname.name, so be sure to init every field
  567. * correctly! */
  568. qname = (char *) res->lockname.name;
  569. memcpy(qname, name, namelen);
  570. res->lockname.len = namelen;
  571. res->lockname.hash = dlm_lockid_hash(name, namelen);
  572. init_waitqueue_head(&res->wq);
  573. spin_lock_init(&res->spinlock);
  574. INIT_HLIST_NODE(&res->hash_node);
  575. INIT_LIST_HEAD(&res->granted);
  576. INIT_LIST_HEAD(&res->converting);
  577. INIT_LIST_HEAD(&res->blocked);
  578. INIT_LIST_HEAD(&res->dirty);
  579. INIT_LIST_HEAD(&res->recovering);
  580. INIT_LIST_HEAD(&res->purge);
  581. atomic_set(&res->asts_reserved, 0);
  582. res->migration_pending = 0;
  583. res->inflight_locks = 0;
  584. kref_init(&res->refs);
  585. /* just for consistency */
  586. spin_lock(&res->spinlock);
  587. dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
  588. spin_unlock(&res->spinlock);
  589. res->state = DLM_LOCK_RES_IN_PROGRESS;
  590. res->last_used = 0;
  591. memset(res->lvb, 0, DLM_LVB_LEN);
  592. memset(res->refmap, 0, sizeof(res->refmap));
  593. }
  594. struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
  595. const char *name,
  596. unsigned int namelen)
  597. {
  598. struct dlm_lock_resource *res;
  599. res = kmalloc(sizeof(struct dlm_lock_resource), GFP_NOFS);
  600. if (!res)
  601. return NULL;
  602. res->lockname.name = kmalloc(namelen, GFP_NOFS);
  603. if (!res->lockname.name) {
  604. kfree(res);
  605. return NULL;
  606. }
  607. dlm_init_lockres(dlm, res, name, namelen);
  608. return res;
  609. }
  610. void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
  611. struct dlm_lock_resource *res,
  612. int new_lockres,
  613. const char *file,
  614. int line)
  615. {
  616. if (!new_lockres)
  617. assert_spin_locked(&res->spinlock);
  618. if (!test_bit(dlm->node_num, res->refmap)) {
  619. BUG_ON(res->inflight_locks != 0);
  620. dlm_lockres_set_refmap_bit(dlm->node_num, res);
  621. }
  622. res->inflight_locks++;
  623. mlog(0, "%s:%.*s: inflight++: now %u\n",
  624. dlm->name, res->lockname.len, res->lockname.name,
  625. res->inflight_locks);
  626. }
  627. void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
  628. struct dlm_lock_resource *res,
  629. const char *file,
  630. int line)
  631. {
  632. assert_spin_locked(&res->spinlock);
  633. BUG_ON(res->inflight_locks == 0);
  634. res->inflight_locks--;
  635. mlog(0, "%s:%.*s: inflight--: now %u\n",
  636. dlm->name, res->lockname.len, res->lockname.name,
  637. res->inflight_locks);
  638. if (res->inflight_locks == 0)
  639. dlm_lockres_clear_refmap_bit(dlm->node_num, res);
  640. wake_up(&res->wq);
  641. }
  642. /*
  643. * lookup a lock resource by name.
  644. * may already exist in the hashtable.
  645. * lockid is null terminated
  646. *
  647. * if not, allocate enough for the lockres and for
  648. * the temporary structure used in doing the mastering.
  649. *
  650. * also, do a lookup in the dlm->master_list to see
  651. * if another node has begun mastering the same lock.
  652. * if so, there should be a block entry in there
  653. * for this name, and we should *not* attempt to master
  654. * the lock here. need to wait around for that node
  655. * to assert_master (or die).
  656. *
  657. */
  658. struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
  659. const char *lockid,
  660. int namelen,
  661. int flags)
  662. {
  663. struct dlm_lock_resource *tmpres=NULL, *res=NULL;
  664. struct dlm_master_list_entry *mle = NULL;
  665. struct dlm_master_list_entry *alloc_mle = NULL;
  666. int blocked = 0;
  667. int ret, nodenum;
  668. struct dlm_node_iter iter;
  669. unsigned int hash;
  670. int tries = 0;
  671. int bit, wait_on_recovery = 0;
  672. int drop_inflight_if_nonlocal = 0;
  673. BUG_ON(!lockid);
  674. hash = dlm_lockid_hash(lockid, namelen);
  675. mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
  676. lookup:
  677. spin_lock(&dlm->spinlock);
  678. tmpres = __dlm_lookup_lockres_full(dlm, lockid, namelen, hash);
  679. if (tmpres) {
  680. int dropping_ref = 0;
  681. spin_lock(&tmpres->spinlock);
  682. if (tmpres->owner == dlm->node_num) {
  683. BUG_ON(tmpres->state & DLM_LOCK_RES_DROPPING_REF);
  684. dlm_lockres_grab_inflight_ref(dlm, tmpres);
  685. } else if (tmpres->state & DLM_LOCK_RES_DROPPING_REF)
  686. dropping_ref = 1;
  687. spin_unlock(&tmpres->spinlock);
  688. spin_unlock(&dlm->spinlock);
  689. /* wait until done messaging the master, drop our ref to allow
  690. * the lockres to be purged, start over. */
  691. if (dropping_ref) {
  692. spin_lock(&tmpres->spinlock);
  693. __dlm_wait_on_lockres_flags(tmpres, DLM_LOCK_RES_DROPPING_REF);
  694. spin_unlock(&tmpres->spinlock);
  695. dlm_lockres_put(tmpres);
  696. tmpres = NULL;
  697. goto lookup;
  698. }
  699. mlog(0, "found in hash!\n");
  700. if (res)
  701. dlm_lockres_put(res);
  702. res = tmpres;
  703. goto leave;
  704. }
  705. if (!res) {
  706. spin_unlock(&dlm->spinlock);
  707. mlog(0, "allocating a new resource\n");
  708. /* nothing found and we need to allocate one. */
  709. alloc_mle = (struct dlm_master_list_entry *)
  710. kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
  711. if (!alloc_mle)
  712. goto leave;
  713. res = dlm_new_lockres(dlm, lockid, namelen);
  714. if (!res)
  715. goto leave;
  716. goto lookup;
  717. }
  718. mlog(0, "no lockres found, allocated our own: %p\n", res);
  719. if (flags & LKM_LOCAL) {
  720. /* caller knows it's safe to assume it's not mastered elsewhere
  721. * DONE! return right away */
  722. spin_lock(&res->spinlock);
  723. dlm_change_lockres_owner(dlm, res, dlm->node_num);
  724. __dlm_insert_lockres(dlm, res);
  725. dlm_lockres_grab_inflight_ref(dlm, res);
  726. spin_unlock(&res->spinlock);
  727. spin_unlock(&dlm->spinlock);
  728. /* lockres still marked IN_PROGRESS */
  729. goto wake_waiters;
  730. }
  731. /* check master list to see if another node has started mastering it */
  732. spin_lock(&dlm->master_lock);
  733. /* if we found a block, wait for lock to be mastered by another node */
  734. blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
  735. if (blocked) {
  736. int mig;
  737. if (mle->type == DLM_MLE_MASTER) {
  738. mlog(ML_ERROR, "master entry for nonexistent lock!\n");
  739. BUG();
  740. }
  741. mig = (mle->type == DLM_MLE_MIGRATION);
  742. /* if there is a migration in progress, let the migration
  743. * finish before continuing. we can wait for the absence
  744. * of the MIGRATION mle: either the migrate finished or
  745. * one of the nodes died and the mle was cleaned up.
  746. * if there is a BLOCK here, but it already has a master
  747. * set, we are too late. the master does not have a ref
  748. * for us in the refmap. detach the mle and drop it.
  749. * either way, go back to the top and start over. */
  750. if (mig || mle->master != O2NM_MAX_NODES) {
  751. BUG_ON(mig && mle->master == dlm->node_num);
  752. /* we arrived too late. the master does not
  753. * have a ref for us. retry. */
  754. mlog(0, "%s:%.*s: late on %s\n",
  755. dlm->name, namelen, lockid,
  756. mig ? "MIGRATION" : "BLOCK");
  757. spin_unlock(&dlm->master_lock);
  758. spin_unlock(&dlm->spinlock);
  759. /* master is known, detach */
  760. if (!mig)
  761. dlm_mle_detach_hb_events(dlm, mle);
  762. dlm_put_mle(mle);
  763. mle = NULL;
  764. /* this is lame, but we cant wait on either
  765. * the mle or lockres waitqueue here */
  766. if (mig)
  767. msleep(100);
  768. goto lookup;
  769. }
  770. } else {
  771. /* go ahead and try to master lock on this node */
  772. mle = alloc_mle;
  773. /* make sure this does not get freed below */
  774. alloc_mle = NULL;
  775. dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0);
  776. set_bit(dlm->node_num, mle->maybe_map);
  777. list_add(&mle->list, &dlm->master_list);
  778. /* still holding the dlm spinlock, check the recovery map
  779. * to see if there are any nodes that still need to be
  780. * considered. these will not appear in the mle nodemap
  781. * but they might own this lockres. wait on them. */
  782. bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
  783. if (bit < O2NM_MAX_NODES) {
  784. mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
  785. "recover before lock mastery can begin\n",
  786. dlm->name, namelen, (char *)lockid, bit);
  787. wait_on_recovery = 1;
  788. }
  789. }
  790. /* at this point there is either a DLM_MLE_BLOCK or a
  791. * DLM_MLE_MASTER on the master list, so it's safe to add the
  792. * lockres to the hashtable. anyone who finds the lock will
  793. * still have to wait on the IN_PROGRESS. */
  794. /* finally add the lockres to its hash bucket */
  795. __dlm_insert_lockres(dlm, res);
  796. /* since this lockres is new it doesnt not require the spinlock */
  797. dlm_lockres_grab_inflight_ref_new(dlm, res);
  798. /* if this node does not become the master make sure to drop
  799. * this inflight reference below */
  800. drop_inflight_if_nonlocal = 1;
  801. /* get an extra ref on the mle in case this is a BLOCK
  802. * if so, the creator of the BLOCK may try to put the last
  803. * ref at this time in the assert master handler, so we
  804. * need an extra one to keep from a bad ptr deref. */
  805. dlm_get_mle_inuse(mle);
  806. spin_unlock(&dlm->master_lock);
  807. spin_unlock(&dlm->spinlock);
  808. redo_request:
  809. while (wait_on_recovery) {
  810. /* any cluster changes that occurred after dropping the
  811. * dlm spinlock would be detectable be a change on the mle,
  812. * so we only need to clear out the recovery map once. */
  813. if (dlm_is_recovery_lock(lockid, namelen)) {
  814. mlog(ML_NOTICE, "%s: recovery map is not empty, but "
  815. "must master $RECOVERY lock now\n", dlm->name);
  816. if (!dlm_pre_master_reco_lockres(dlm, res))
  817. wait_on_recovery = 0;
  818. else {
  819. mlog(0, "%s: waiting 500ms for heartbeat state "
  820. "change\n", dlm->name);
  821. msleep(500);
  822. }
  823. continue;
  824. }
  825. dlm_kick_recovery_thread(dlm);
  826. msleep(1000);
  827. dlm_wait_for_recovery(dlm);
  828. spin_lock(&dlm->spinlock);
  829. bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
  830. if (bit < O2NM_MAX_NODES) {
  831. mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to"
  832. "recover before lock mastery can begin\n",
  833. dlm->name, namelen, (char *)lockid, bit);
  834. wait_on_recovery = 1;
  835. } else
  836. wait_on_recovery = 0;
  837. spin_unlock(&dlm->spinlock);
  838. if (wait_on_recovery)
  839. dlm_wait_for_node_recovery(dlm, bit, 10000);
  840. }
  841. /* must wait for lock to be mastered elsewhere */
  842. if (blocked)
  843. goto wait;
  844. ret = -EINVAL;
  845. dlm_node_iter_init(mle->vote_map, &iter);
  846. while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
  847. ret = dlm_do_master_request(res, mle, nodenum);
  848. if (ret < 0)
  849. mlog_errno(ret);
  850. if (mle->master != O2NM_MAX_NODES) {
  851. /* found a master ! */
  852. if (mle->master <= nodenum)
  853. break;
  854. /* if our master request has not reached the master
  855. * yet, keep going until it does. this is how the
  856. * master will know that asserts are needed back to
  857. * the lower nodes. */
  858. mlog(0, "%s:%.*s: requests only up to %u but master "
  859. "is %u, keep going\n", dlm->name, namelen,
  860. lockid, nodenum, mle->master);
  861. }
  862. }
  863. wait:
  864. /* keep going until the response map includes all nodes */
  865. ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
  866. if (ret < 0) {
  867. wait_on_recovery = 1;
  868. mlog(0, "%s:%.*s: node map changed, redo the "
  869. "master request now, blocked=%d\n",
  870. dlm->name, res->lockname.len,
  871. res->lockname.name, blocked);
  872. if (++tries > 20) {
  873. mlog(ML_ERROR, "%s:%.*s: spinning on "
  874. "dlm_wait_for_lock_mastery, blocked=%d\n",
  875. dlm->name, res->lockname.len,
  876. res->lockname.name, blocked);
  877. dlm_print_one_lock_resource(res);
  878. dlm_print_one_mle(mle);
  879. tries = 0;
  880. }
  881. goto redo_request;
  882. }
  883. mlog(0, "lockres mastered by %u\n", res->owner);
  884. /* make sure we never continue without this */
  885. BUG_ON(res->owner == O2NM_MAX_NODES);
  886. /* master is known, detach if not already detached */
  887. dlm_mle_detach_hb_events(dlm, mle);
  888. dlm_put_mle(mle);
  889. /* put the extra ref */
  890. dlm_put_mle_inuse(mle);
  891. wake_waiters:
  892. spin_lock(&res->spinlock);
  893. if (res->owner != dlm->node_num && drop_inflight_if_nonlocal)
  894. dlm_lockres_drop_inflight_ref(dlm, res);
  895. res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
  896. spin_unlock(&res->spinlock);
  897. wake_up(&res->wq);
  898. leave:
  899. /* need to free the unused mle */
  900. if (alloc_mle)
  901. kmem_cache_free(dlm_mle_cache, alloc_mle);
  902. return res;
  903. }
  904. #define DLM_MASTERY_TIMEOUT_MS 5000
  905. static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
  906. struct dlm_lock_resource *res,
  907. struct dlm_master_list_entry *mle,
  908. int *blocked)
  909. {
  910. u8 m;
  911. int ret, bit;
  912. int map_changed, voting_done;
  913. int assert, sleep;
  914. recheck:
  915. ret = 0;
  916. assert = 0;
  917. /* check if another node has already become the owner */
  918. spin_lock(&res->spinlock);
  919. if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
  920. mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name,
  921. res->lockname.len, res->lockname.name, res->owner);
  922. spin_unlock(&res->spinlock);
  923. /* this will cause the master to re-assert across
  924. * the whole cluster, freeing up mles */
  925. if (res->owner != dlm->node_num) {
  926. ret = dlm_do_master_request(res, mle, res->owner);
  927. if (ret < 0) {
  928. /* give recovery a chance to run */
  929. mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
  930. msleep(500);
  931. goto recheck;
  932. }
  933. }
  934. ret = 0;
  935. goto leave;
  936. }
  937. spin_unlock(&res->spinlock);
  938. spin_lock(&mle->spinlock);
  939. m = mle->master;
  940. map_changed = (memcmp(mle->vote_map, mle->node_map,
  941. sizeof(mle->vote_map)) != 0);
  942. voting_done = (memcmp(mle->vote_map, mle->response_map,
  943. sizeof(mle->vote_map)) == 0);
  944. /* restart if we hit any errors */
  945. if (map_changed) {
  946. int b;
  947. mlog(0, "%s: %.*s: node map changed, restarting\n",
  948. dlm->name, res->lockname.len, res->lockname.name);
  949. ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked);
  950. b = (mle->type == DLM_MLE_BLOCK);
  951. if ((*blocked && !b) || (!*blocked && b)) {
  952. mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
  953. dlm->name, res->lockname.len, res->lockname.name,
  954. *blocked, b);
  955. *blocked = b;
  956. }
  957. spin_unlock(&mle->spinlock);
  958. if (ret < 0) {
  959. mlog_errno(ret);
  960. goto leave;
  961. }
  962. mlog(0, "%s:%.*s: restart lock mastery succeeded, "
  963. "rechecking now\n", dlm->name, res->lockname.len,
  964. res->lockname.name);
  965. goto recheck;
  966. } else {
  967. if (!voting_done) {
  968. mlog(0, "map not changed and voting not done "
  969. "for %s:%.*s\n", dlm->name, res->lockname.len,
  970. res->lockname.name);
  971. }
  972. }
  973. if (m != O2NM_MAX_NODES) {
  974. /* another node has done an assert!
  975. * all done! */
  976. sleep = 0;
  977. } else {
  978. sleep = 1;
  979. /* have all nodes responded? */
  980. if (voting_done && !*blocked) {
  981. bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
  982. if (dlm->node_num <= bit) {
  983. /* my node number is lowest.
  984. * now tell other nodes that I am
  985. * mastering this. */
  986. mle->master = dlm->node_num;
  987. /* ref was grabbed in get_lock_resource
  988. * will be dropped in dlmlock_master */
  989. assert = 1;
  990. sleep = 0;
  991. }
  992. /* if voting is done, but we have not received
  993. * an assert master yet, we must sleep */
  994. }
  995. }
  996. spin_unlock(&mle->spinlock);
  997. /* sleep if we haven't finished voting yet */
  998. if (sleep) {
  999. unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS);
  1000. /*
  1001. if (atomic_read(&mle->mle_refs.refcount) < 2)
  1002. mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
  1003. atomic_read(&mle->mle_refs.refcount),
  1004. res->lockname.len, res->lockname.name);
  1005. */
  1006. atomic_set(&mle->woken, 0);
  1007. (void)wait_event_timeout(mle->wq,
  1008. (atomic_read(&mle->woken) == 1),
  1009. timeo);
  1010. if (res->owner == O2NM_MAX_NODES) {
  1011. mlog(0, "%s:%.*s: waiting again\n", dlm->name,
  1012. res->lockname.len, res->lockname.name);
  1013. goto recheck;
  1014. }
  1015. mlog(0, "done waiting, master is %u\n", res->owner);
  1016. ret = 0;
  1017. goto leave;
  1018. }
  1019. ret = 0; /* done */
  1020. if (assert) {
  1021. m = dlm->node_num;
  1022. mlog(0, "about to master %.*s here, this=%u\n",
  1023. res->lockname.len, res->lockname.name, m);
  1024. ret = dlm_do_assert_master(dlm, res, mle->vote_map, 0);
  1025. if (ret) {
  1026. /* This is a failure in the network path,
  1027. * not in the response to the assert_master
  1028. * (any nonzero response is a BUG on this node).
  1029. * Most likely a socket just got disconnected
  1030. * due to node death. */
  1031. mlog_errno(ret);
  1032. }
  1033. /* no longer need to restart lock mastery.
  1034. * all living nodes have been contacted. */
  1035. ret = 0;
  1036. }
  1037. /* set the lockres owner */
  1038. spin_lock(&res->spinlock);
  1039. /* mastery reference obtained either during
  1040. * assert_master_handler or in get_lock_resource */
  1041. dlm_change_lockres_owner(dlm, res, m);
  1042. spin_unlock(&res->spinlock);
  1043. leave:
  1044. return ret;
  1045. }
  1046. struct dlm_bitmap_diff_iter
  1047. {
  1048. int curnode;
  1049. unsigned long *orig_bm;
  1050. unsigned long *cur_bm;
  1051. unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
  1052. };
  1053. enum dlm_node_state_change
  1054. {
  1055. NODE_DOWN = -1,
  1056. NODE_NO_CHANGE = 0,
  1057. NODE_UP
  1058. };
  1059. static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter,
  1060. unsigned long *orig_bm,
  1061. unsigned long *cur_bm)
  1062. {
  1063. unsigned long p1, p2;
  1064. int i;
  1065. iter->curnode = -1;
  1066. iter->orig_bm = orig_bm;
  1067. iter->cur_bm = cur_bm;
  1068. for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) {
  1069. p1 = *(iter->orig_bm + i);
  1070. p2 = *(iter->cur_bm + i);
  1071. iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1);
  1072. }
  1073. }
  1074. static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter,
  1075. enum dlm_node_state_change *state)
  1076. {
  1077. int bit;
  1078. if (iter->curnode >= O2NM_MAX_NODES)
  1079. return -ENOENT;
  1080. bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES,
  1081. iter->curnode+1);
  1082. if (bit >= O2NM_MAX_NODES) {
  1083. iter->curnode = O2NM_MAX_NODES;
  1084. return -ENOENT;
  1085. }
  1086. /* if it was there in the original then this node died */
  1087. if (test_bit(bit, iter->orig_bm))
  1088. *state = NODE_DOWN;
  1089. else
  1090. *state = NODE_UP;
  1091. iter->curnode = bit;
  1092. return bit;
  1093. }
  1094. static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
  1095. struct dlm_lock_resource *res,
  1096. struct dlm_master_list_entry *mle,
  1097. int blocked)
  1098. {
  1099. struct dlm_bitmap_diff_iter bdi;
  1100. enum dlm_node_state_change sc;
  1101. int node;
  1102. int ret = 0;
  1103. mlog(0, "something happened such that the "
  1104. "master process may need to be restarted!\n");
  1105. assert_spin_locked(&mle->spinlock);
  1106. dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map);
  1107. node = dlm_bitmap_diff_iter_next(&bdi, &sc);
  1108. while (node >= 0) {
  1109. if (sc == NODE_UP) {
  1110. /* a node came up. clear any old vote from
  1111. * the response map and set it in the vote map
  1112. * then restart the mastery. */
  1113. mlog(ML_NOTICE, "node %d up while restarting\n", node);
  1114. /* redo the master request, but only for the new node */
  1115. mlog(0, "sending request to new node\n");
  1116. clear_bit(node, mle->response_map);
  1117. set_bit(node, mle->vote_map);
  1118. } else {
  1119. mlog(ML_ERROR, "node down! %d\n", node);
  1120. if (blocked) {
  1121. int lowest = find_next_bit(mle->maybe_map,
  1122. O2NM_MAX_NODES, 0);
  1123. /* act like it was never there */
  1124. clear_bit(node, mle->maybe_map);
  1125. if (node == lowest) {
  1126. mlog(0, "expected master %u died"
  1127. " while this node was blocked "
  1128. "waiting on it!\n", node);
  1129. lowest = find_next_bit(mle->maybe_map,
  1130. O2NM_MAX_NODES,
  1131. lowest+1);
  1132. if (lowest < O2NM_MAX_NODES) {
  1133. mlog(0, "%s:%.*s:still "
  1134. "blocked. waiting on %u "
  1135. "now\n", dlm->name,
  1136. res->lockname.len,
  1137. res->lockname.name,
  1138. lowest);
  1139. } else {
  1140. /* mle is an MLE_BLOCK, but
  1141. * there is now nothing left to
  1142. * block on. we need to return
  1143. * all the way back out and try
  1144. * again with an MLE_MASTER.
  1145. * dlm_do_local_recovery_cleanup
  1146. * has already run, so the mle
  1147. * refcount is ok */
  1148. mlog(0, "%s:%.*s: no "
  1149. "longer blocking. try to "
  1150. "master this here\n",
  1151. dlm->name,
  1152. res->lockname.len,
  1153. res->lockname.name);
  1154. mle->type = DLM_MLE_MASTER;
  1155. mle->u.res = res;
  1156. }
  1157. }
  1158. }
  1159. /* now blank out everything, as if we had never
  1160. * contacted anyone */
  1161. memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
  1162. memset(mle->response_map, 0, sizeof(mle->response_map));
  1163. /* reset the vote_map to the current node_map */
  1164. memcpy(mle->vote_map, mle->node_map,
  1165. sizeof(mle->node_map));
  1166. /* put myself into the maybe map */
  1167. if (mle->type != DLM_MLE_BLOCK)
  1168. set_bit(dlm->node_num, mle->maybe_map);
  1169. }
  1170. ret = -EAGAIN;
  1171. node = dlm_bitmap_diff_iter_next(&bdi, &sc);
  1172. }
  1173. return ret;
  1174. }
  1175. /*
  1176. * DLM_MASTER_REQUEST_MSG
  1177. *
  1178. * returns: 0 on success,
  1179. * -errno on a network error
  1180. *
  1181. * on error, the caller should assume the target node is "dead"
  1182. *
  1183. */
  1184. static int dlm_do_master_request(struct dlm_lock_resource *res,
  1185. struct dlm_master_list_entry *mle, int to)
  1186. {
  1187. struct dlm_ctxt *dlm = mle->dlm;
  1188. struct dlm_master_request request;
  1189. int ret, response=0, resend;
  1190. memset(&request, 0, sizeof(request));
  1191. request.node_idx = dlm->node_num;
  1192. BUG_ON(mle->type == DLM_MLE_MIGRATION);
  1193. if (mle->type != DLM_MLE_MASTER) {
  1194. request.namelen = mle->u.name.len;
  1195. memcpy(request.name, mle->u.name.name, request.namelen);
  1196. } else {
  1197. request.namelen = mle->u.res->lockname.len;
  1198. memcpy(request.name, mle->u.res->lockname.name,
  1199. request.namelen);
  1200. }
  1201. again:
  1202. ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request,
  1203. sizeof(request), to, &response);
  1204. if (ret < 0) {
  1205. if (ret == -ESRCH) {
  1206. /* should never happen */
  1207. mlog(ML_ERROR, "TCP stack not ready!\n");
  1208. BUG();
  1209. } else if (ret == -EINVAL) {
  1210. mlog(ML_ERROR, "bad args passed to o2net!\n");
  1211. BUG();
  1212. } else if (ret == -ENOMEM) {
  1213. mlog(ML_ERROR, "out of memory while trying to send "
  1214. "network message! retrying\n");
  1215. /* this is totally crude */
  1216. msleep(50);
  1217. goto again;
  1218. } else if (!dlm_is_host_down(ret)) {
  1219. /* not a network error. bad. */
  1220. mlog_errno(ret);
  1221. mlog(ML_ERROR, "unhandled error!");
  1222. BUG();
  1223. }
  1224. /* all other errors should be network errors,
  1225. * and likely indicate node death */
  1226. mlog(ML_ERROR, "link to %d went down!\n", to);
  1227. goto out;
  1228. }
  1229. ret = 0;
  1230. resend = 0;
  1231. spin_lock(&mle->spinlock);
  1232. switch (response) {
  1233. case DLM_MASTER_RESP_YES:
  1234. set_bit(to, mle->response_map);
  1235. mlog(0, "node %u is the master, response=YES\n", to);
  1236. mlog(0, "%s:%.*s: master node %u now knows I have a "
  1237. "reference\n", dlm->name, res->lockname.len,
  1238. res->lockname.name, to);
  1239. mle->master = to;
  1240. break;
  1241. case DLM_MASTER_RESP_NO:
  1242. mlog(0, "node %u not master, response=NO\n", to);
  1243. set_bit(to, mle->response_map);
  1244. break;
  1245. case DLM_MASTER_RESP_MAYBE:
  1246. mlog(0, "node %u not master, response=MAYBE\n", to);
  1247. set_bit(to, mle->response_map);
  1248. set_bit(to, mle->maybe_map);
  1249. break;
  1250. case DLM_MASTER_RESP_ERROR:
  1251. mlog(0, "node %u hit an error, resending\n", to);
  1252. resend = 1;
  1253. response = 0;
  1254. break;
  1255. default:
  1256. mlog(ML_ERROR, "bad response! %u\n", response);
  1257. BUG();
  1258. }
  1259. spin_unlock(&mle->spinlock);
  1260. if (resend) {
  1261. /* this is also totally crude */
  1262. msleep(50);
  1263. goto again;
  1264. }
  1265. out:
  1266. return ret;
  1267. }
  1268. /*
  1269. * locks that can be taken here:
  1270. * dlm->spinlock
  1271. * res->spinlock
  1272. * mle->spinlock
  1273. * dlm->master_list
  1274. *
  1275. * if possible, TRIM THIS DOWN!!!
  1276. */
  1277. int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
  1278. void **ret_data)
  1279. {
  1280. u8 response = DLM_MASTER_RESP_MAYBE;
  1281. struct dlm_ctxt *dlm = data;
  1282. struct dlm_lock_resource *res = NULL;
  1283. struct dlm_master_request *request = (struct dlm_master_request *) msg->buf;
  1284. struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL;
  1285. char *name;
  1286. unsigned int namelen, hash;
  1287. int found, ret;
  1288. int set_maybe;
  1289. int dispatch_assert = 0;
  1290. if (!dlm_grab(dlm))
  1291. return DLM_MASTER_RESP_NO;
  1292. if (!dlm_domain_fully_joined(dlm)) {
  1293. response = DLM_MASTER_RESP_NO;
  1294. goto send_response;
  1295. }
  1296. name = request->name;
  1297. namelen = request->namelen;
  1298. hash = dlm_lockid_hash(name, namelen);
  1299. if (namelen > DLM_LOCKID_NAME_MAX) {
  1300. response = DLM_IVBUFLEN;
  1301. goto send_response;
  1302. }
  1303. way_up_top:
  1304. spin_lock(&dlm->spinlock);
  1305. res = __dlm_lookup_lockres(dlm, name, namelen, hash);
  1306. if (res) {
  1307. spin_unlock(&dlm->spinlock);
  1308. /* take care of the easy cases up front */
  1309. spin_lock(&res->spinlock);
  1310. if (res->state & (DLM_LOCK_RES_RECOVERING|
  1311. DLM_LOCK_RES_MIGRATING)) {
  1312. spin_unlock(&res->spinlock);
  1313. mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
  1314. "being recovered/migrated\n");
  1315. response = DLM_MASTER_RESP_ERROR;
  1316. if (mle)
  1317. kmem_cache_free(dlm_mle_cache, mle);
  1318. goto send_response;
  1319. }
  1320. if (res->owner == dlm->node_num) {
  1321. mlog(0, "%s:%.*s: setting bit %u in refmap\n",
  1322. dlm->name, namelen, name, request->node_idx);
  1323. dlm_lockres_set_refmap_bit(request->node_idx, res);
  1324. spin_unlock(&res->spinlock);
  1325. response = DLM_MASTER_RESP_YES;
  1326. if (mle)
  1327. kmem_cache_free(dlm_mle_cache, mle);
  1328. /* this node is the owner.
  1329. * there is some extra work that needs to
  1330. * happen now. the requesting node has
  1331. * caused all nodes up to this one to
  1332. * create mles. this node now needs to
  1333. * go back and clean those up. */
  1334. dispatch_assert = 1;
  1335. goto send_response;
  1336. } else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1337. spin_unlock(&res->spinlock);
  1338. // mlog(0, "node %u is the master\n", res->owner);
  1339. response = DLM_MASTER_RESP_NO;
  1340. if (mle)
  1341. kmem_cache_free(dlm_mle_cache, mle);
  1342. goto send_response;
  1343. }
  1344. /* ok, there is no owner. either this node is
  1345. * being blocked, or it is actively trying to
  1346. * master this lock. */
  1347. if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
  1348. mlog(ML_ERROR, "lock with no owner should be "
  1349. "in-progress!\n");
  1350. BUG();
  1351. }
  1352. // mlog(0, "lockres is in progress...\n");
  1353. spin_lock(&dlm->master_lock);
  1354. found = dlm_find_mle(dlm, &tmpmle, name, namelen);
  1355. if (!found) {
  1356. mlog(ML_ERROR, "no mle found for this lock!\n");
  1357. BUG();
  1358. }
  1359. set_maybe = 1;
  1360. spin_lock(&tmpmle->spinlock);
  1361. if (tmpmle->type == DLM_MLE_BLOCK) {
  1362. // mlog(0, "this node is waiting for "
  1363. // "lockres to be mastered\n");
  1364. response = DLM_MASTER_RESP_NO;
  1365. } else if (tmpmle->type == DLM_MLE_MIGRATION) {
  1366. mlog(0, "node %u is master, but trying to migrate to "
  1367. "node %u.\n", tmpmle->master, tmpmle->new_master);
  1368. if (tmpmle->master == dlm->node_num) {
  1369. mlog(ML_ERROR, "no owner on lockres, but this "
  1370. "node is trying to migrate it to %u?!\n",
  1371. tmpmle->new_master);
  1372. BUG();
  1373. } else {
  1374. /* the real master can respond on its own */
  1375. response = DLM_MASTER_RESP_NO;
  1376. }
  1377. } else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) {
  1378. set_maybe = 0;
  1379. if (tmpmle->master == dlm->node_num) {
  1380. response = DLM_MASTER_RESP_YES;
  1381. /* this node will be the owner.
  1382. * go back and clean the mles on any
  1383. * other nodes */
  1384. dispatch_assert = 1;
  1385. dlm_lockres_set_refmap_bit(request->node_idx, res);
  1386. mlog(0, "%s:%.*s: setting bit %u in refmap\n",
  1387. dlm->name, namelen, name,
  1388. request->node_idx);
  1389. } else
  1390. response = DLM_MASTER_RESP_NO;
  1391. } else {
  1392. // mlog(0, "this node is attempting to "
  1393. // "master lockres\n");
  1394. response = DLM_MASTER_RESP_MAYBE;
  1395. }
  1396. if (set_maybe)
  1397. set_bit(request->node_idx, tmpmle->maybe_map);
  1398. spin_unlock(&tmpmle->spinlock);
  1399. spin_unlock(&dlm->master_lock);
  1400. spin_unlock(&res->spinlock);
  1401. /* keep the mle attached to heartbeat events */
  1402. dlm_put_mle(tmpmle);
  1403. if (mle)
  1404. kmem_cache_free(dlm_mle_cache, mle);
  1405. goto send_response;
  1406. }
  1407. /*
  1408. * lockres doesn't exist on this node
  1409. * if there is an MLE_BLOCK, return NO
  1410. * if there is an MLE_MASTER, return MAYBE
  1411. * otherwise, add an MLE_BLOCK, return NO
  1412. */
  1413. spin_lock(&dlm->master_lock);
  1414. found = dlm_find_mle(dlm, &tmpmle, name, namelen);
  1415. if (!found) {
  1416. /* this lockid has never been seen on this node yet */
  1417. // mlog(0, "no mle found\n");
  1418. if (!mle) {
  1419. spin_unlock(&dlm->master_lock);
  1420. spin_unlock(&dlm->spinlock);
  1421. mle = (struct dlm_master_list_entry *)
  1422. kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
  1423. if (!mle) {
  1424. response = DLM_MASTER_RESP_ERROR;
  1425. mlog_errno(-ENOMEM);
  1426. goto send_response;
  1427. }
  1428. goto way_up_top;
  1429. }
  1430. // mlog(0, "this is second time thru, already allocated, "
  1431. // "add the block.\n");
  1432. dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen);
  1433. set_bit(request->node_idx, mle->maybe_map);
  1434. list_add(&mle->list, &dlm->master_list);
  1435. response = DLM_MASTER_RESP_NO;
  1436. } else {
  1437. // mlog(0, "mle was found\n");
  1438. set_maybe = 1;
  1439. spin_lock(&tmpmle->spinlock);
  1440. if (tmpmle->master == dlm->node_num) {
  1441. mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n");
  1442. BUG();
  1443. }
  1444. if (tmpmle->type == DLM_MLE_BLOCK)
  1445. response = DLM_MASTER_RESP_NO;
  1446. else if (tmpmle->type == DLM_MLE_MIGRATION) {
  1447. mlog(0, "migration mle was found (%u->%u)\n",
  1448. tmpmle->master, tmpmle->new_master);
  1449. /* real master can respond on its own */
  1450. response = DLM_MASTER_RESP_NO;
  1451. } else
  1452. response = DLM_MASTER_RESP_MAYBE;
  1453. if (set_maybe)
  1454. set_bit(request->node_idx, tmpmle->maybe_map);
  1455. spin_unlock(&tmpmle->spinlock);
  1456. }
  1457. spin_unlock(&dlm->master_lock);
  1458. spin_unlock(&dlm->spinlock);
  1459. if (found) {
  1460. /* keep the mle attached to heartbeat events */
  1461. dlm_put_mle(tmpmle);
  1462. }
  1463. send_response:
  1464. if (dispatch_assert) {
  1465. if (response != DLM_MASTER_RESP_YES)
  1466. mlog(ML_ERROR, "invalid response %d\n", response);
  1467. if (!res) {
  1468. mlog(ML_ERROR, "bad lockres while trying to assert!\n");
  1469. BUG();
  1470. }
  1471. mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
  1472. dlm->node_num, res->lockname.len, res->lockname.name);
  1473. ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx,
  1474. DLM_ASSERT_MASTER_MLE_CLEANUP);
  1475. if (ret < 0) {
  1476. mlog(ML_ERROR, "failed to dispatch assert master work\n");
  1477. response = DLM_MASTER_RESP_ERROR;
  1478. }
  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. 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, *iter;
  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(iter, queue) {
  2163. lock = list_entry(iter, struct dlm_lock, list);
  2164. ++count;
  2165. if (lock->ml.node == dlm->node_num) {
  2166. mlog(0, "found a lock owned by this node still "
  2167. "on the %s queue! will not migrate this "
  2168. "lockres\n", (i == 0 ? "granted" :
  2169. (i == 1 ? "converting" :
  2170. "blocked")));
  2171. ret = -ENOTEMPTY;
  2172. goto leave;
  2173. }
  2174. }
  2175. queue++;
  2176. }
  2177. *numlocks = count;
  2178. mlog(0, "migrateable lockres having %d locks\n", *numlocks);
  2179. leave:
  2180. return ret;
  2181. }
  2182. /*
  2183. * DLM_MIGRATE_LOCKRES
  2184. */
  2185. static int dlm_migrate_lockres(struct dlm_ctxt *dlm,
  2186. struct dlm_lock_resource *res,
  2187. u8 target)
  2188. {
  2189. struct dlm_master_list_entry *mle = NULL;
  2190. struct dlm_master_list_entry *oldmle = NULL;
  2191. struct dlm_migratable_lockres *mres = NULL;
  2192. int ret = 0;
  2193. const char *name;
  2194. unsigned int namelen;
  2195. int mle_added = 0;
  2196. int numlocks;
  2197. int wake = 0;
  2198. if (!dlm_grab(dlm))
  2199. return -EINVAL;
  2200. name = res->lockname.name;
  2201. namelen = res->lockname.len;
  2202. mlog(0, "migrating %.*s to %u\n", namelen, name, target);
  2203. /*
  2204. * ensure this lockres is a proper candidate for migration
  2205. */
  2206. spin_lock(&res->spinlock);
  2207. ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
  2208. if (ret < 0) {
  2209. spin_unlock(&res->spinlock);
  2210. goto leave;
  2211. }
  2212. spin_unlock(&res->spinlock);
  2213. /* no work to do */
  2214. if (numlocks == 0) {
  2215. mlog(0, "no locks were found on this lockres! done!\n");
  2216. goto leave;
  2217. }
  2218. /*
  2219. * preallocate up front
  2220. * if this fails, abort
  2221. */
  2222. ret = -ENOMEM;
  2223. mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_NOFS);
  2224. if (!mres) {
  2225. mlog_errno(ret);
  2226. goto leave;
  2227. }
  2228. mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
  2229. GFP_NOFS);
  2230. if (!mle) {
  2231. mlog_errno(ret);
  2232. goto leave;
  2233. }
  2234. ret = 0;
  2235. /*
  2236. * find a node to migrate the lockres to
  2237. */
  2238. mlog(0, "picking a migration node\n");
  2239. spin_lock(&dlm->spinlock);
  2240. /* pick a new node */
  2241. if (!test_bit(target, dlm->domain_map) ||
  2242. target >= O2NM_MAX_NODES) {
  2243. target = dlm_pick_migration_target(dlm, res);
  2244. }
  2245. mlog(0, "node %u chosen for migration\n", target);
  2246. if (target >= O2NM_MAX_NODES ||
  2247. !test_bit(target, dlm->domain_map)) {
  2248. /* target chosen is not alive */
  2249. ret = -EINVAL;
  2250. }
  2251. if (ret) {
  2252. spin_unlock(&dlm->spinlock);
  2253. goto fail;
  2254. }
  2255. mlog(0, "continuing with target = %u\n", target);
  2256. /*
  2257. * clear any existing master requests and
  2258. * add the migration mle to the list
  2259. */
  2260. spin_lock(&dlm->master_lock);
  2261. ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name,
  2262. namelen, target, dlm->node_num);
  2263. spin_unlock(&dlm->master_lock);
  2264. spin_unlock(&dlm->spinlock);
  2265. if (ret == -EEXIST) {
  2266. mlog(0, "another process is already migrating it\n");
  2267. goto fail;
  2268. }
  2269. mle_added = 1;
  2270. /*
  2271. * set the MIGRATING flag and flush asts
  2272. * if we fail after this we need to re-dirty the lockres
  2273. */
  2274. if (dlm_mark_lockres_migrating(dlm, res, target) < 0) {
  2275. mlog(ML_ERROR, "tried to migrate %.*s to %u, but "
  2276. "the target went down.\n", res->lockname.len,
  2277. res->lockname.name, target);
  2278. spin_lock(&res->spinlock);
  2279. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2280. wake = 1;
  2281. spin_unlock(&res->spinlock);
  2282. ret = -EINVAL;
  2283. }
  2284. fail:
  2285. if (oldmle) {
  2286. /* master is known, detach if not already detached */
  2287. dlm_mle_detach_hb_events(dlm, oldmle);
  2288. dlm_put_mle(oldmle);
  2289. }
  2290. if (ret < 0) {
  2291. if (mle_added) {
  2292. dlm_mle_detach_hb_events(dlm, mle);
  2293. dlm_put_mle(mle);
  2294. } else if (mle) {
  2295. kmem_cache_free(dlm_mle_cache, mle);
  2296. }
  2297. goto leave;
  2298. }
  2299. /*
  2300. * at this point, we have a migration target, an mle
  2301. * in the master list, and the MIGRATING flag set on
  2302. * the lockres
  2303. */
  2304. /* now that remote nodes are spinning on the MIGRATING flag,
  2305. * ensure that all assert_master work is flushed. */
  2306. flush_workqueue(dlm->dlm_worker);
  2307. /* get an extra reference on the mle.
  2308. * otherwise the assert_master from the new
  2309. * master will destroy this.
  2310. * also, make sure that all callers of dlm_get_mle
  2311. * take both dlm->spinlock and dlm->master_lock */
  2312. spin_lock(&dlm->spinlock);
  2313. spin_lock(&dlm->master_lock);
  2314. dlm_get_mle_inuse(mle);
  2315. spin_unlock(&dlm->master_lock);
  2316. spin_unlock(&dlm->spinlock);
  2317. /* notify new node and send all lock state */
  2318. /* call send_one_lockres with migration flag.
  2319. * this serves as notice to the target node that a
  2320. * migration is starting. */
  2321. ret = dlm_send_one_lockres(dlm, res, mres, target,
  2322. DLM_MRES_MIGRATION);
  2323. if (ret < 0) {
  2324. mlog(0, "migration to node %u failed with %d\n",
  2325. target, ret);
  2326. /* migration failed, detach and clean up mle */
  2327. dlm_mle_detach_hb_events(dlm, mle);
  2328. dlm_put_mle(mle);
  2329. dlm_put_mle_inuse(mle);
  2330. spin_lock(&res->spinlock);
  2331. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2332. wake = 1;
  2333. spin_unlock(&res->spinlock);
  2334. goto leave;
  2335. }
  2336. /* at this point, the target sends a message to all nodes,
  2337. * (using dlm_do_migrate_request). this node is skipped since
  2338. * we had to put an mle in the list to begin the process. this
  2339. * node now waits for target to do an assert master. this node
  2340. * will be the last one notified, ensuring that the migration
  2341. * is complete everywhere. if the target dies while this is
  2342. * going on, some nodes could potentially see the target as the
  2343. * master, so it is important that my recovery finds the migration
  2344. * mle and sets the master to UNKNONWN. */
  2345. /* wait for new node to assert master */
  2346. while (1) {
  2347. ret = wait_event_interruptible_timeout(mle->wq,
  2348. (atomic_read(&mle->woken) == 1),
  2349. msecs_to_jiffies(5000));
  2350. if (ret >= 0) {
  2351. if (atomic_read(&mle->woken) == 1 ||
  2352. res->owner == target)
  2353. break;
  2354. mlog(0, "%s:%.*s: timed out during migration\n",
  2355. dlm->name, res->lockname.len, res->lockname.name);
  2356. /* avoid hang during shutdown when migrating lockres
  2357. * to a node which also goes down */
  2358. if (dlm_is_node_dead(dlm, target)) {
  2359. mlog(0, "%s:%.*s: expected migration "
  2360. "target %u is no longer up, restarting\n",
  2361. dlm->name, res->lockname.len,
  2362. res->lockname.name, target);
  2363. ret = -EINVAL;
  2364. /* migration failed, detach and clean up mle */
  2365. dlm_mle_detach_hb_events(dlm, mle);
  2366. dlm_put_mle(mle);
  2367. dlm_put_mle_inuse(mle);
  2368. spin_lock(&res->spinlock);
  2369. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2370. wake = 1;
  2371. spin_unlock(&res->spinlock);
  2372. goto leave;
  2373. }
  2374. } else
  2375. mlog(0, "%s:%.*s: caught signal during migration\n",
  2376. dlm->name, res->lockname.len, res->lockname.name);
  2377. }
  2378. /* all done, set the owner, clear the flag */
  2379. spin_lock(&res->spinlock);
  2380. dlm_set_lockres_owner(dlm, res, target);
  2381. res->state &= ~DLM_LOCK_RES_MIGRATING;
  2382. dlm_remove_nonlocal_locks(dlm, res);
  2383. spin_unlock(&res->spinlock);
  2384. wake_up(&res->wq);
  2385. /* master is known, detach if not already detached */
  2386. dlm_mle_detach_hb_events(dlm, mle);
  2387. dlm_put_mle_inuse(mle);
  2388. ret = 0;
  2389. dlm_lockres_calc_usage(dlm, res);
  2390. leave:
  2391. /* re-dirty the lockres if we failed */
  2392. if (ret < 0)
  2393. dlm_kick_thread(dlm, res);
  2394. /* wake up waiters if the MIGRATING flag got set
  2395. * but migration failed */
  2396. if (wake)
  2397. wake_up(&res->wq);
  2398. /* TODO: cleanup */
  2399. if (mres)
  2400. free_page((unsigned long)mres);
  2401. dlm_put(dlm);
  2402. mlog(0, "returning %d\n", ret);
  2403. return ret;
  2404. }
  2405. #define DLM_MIGRATION_RETRY_MS 100
  2406. /* Should be called only after beginning the domain leave process.
  2407. * There should not be any remaining locks on nonlocal lock resources,
  2408. * and there should be no local locks left on locally mastered resources.
  2409. *
  2410. * Called with the dlm spinlock held, may drop it to do migration, but
  2411. * will re-acquire before exit.
  2412. *
  2413. * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped */
  2414. int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
  2415. {
  2416. int ret;
  2417. int lock_dropped = 0;
  2418. int numlocks;
  2419. spin_lock(&res->spinlock);
  2420. if (res->owner != dlm->node_num) {
  2421. if (!__dlm_lockres_unused(res)) {
  2422. mlog(ML_ERROR, "%s:%.*s: this node is not master, "
  2423. "trying to free this but locks remain\n",
  2424. dlm->name, res->lockname.len, res->lockname.name);
  2425. }
  2426. spin_unlock(&res->spinlock);
  2427. goto leave;
  2428. }
  2429. /* No need to migrate a lockres having no locks */
  2430. ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
  2431. if (ret >= 0 && numlocks == 0) {
  2432. spin_unlock(&res->spinlock);
  2433. goto leave;
  2434. }
  2435. spin_unlock(&res->spinlock);
  2436. /* Wheee! Migrate lockres here! Will sleep so drop spinlock. */
  2437. spin_unlock(&dlm->spinlock);
  2438. lock_dropped = 1;
  2439. while (1) {
  2440. ret = dlm_migrate_lockres(dlm, res, O2NM_MAX_NODES);
  2441. if (ret >= 0)
  2442. break;
  2443. if (ret == -ENOTEMPTY) {
  2444. mlog(ML_ERROR, "lockres %.*s still has local locks!\n",
  2445. res->lockname.len, res->lockname.name);
  2446. BUG();
  2447. }
  2448. mlog(0, "lockres %.*s: migrate failed, "
  2449. "retrying\n", res->lockname.len,
  2450. res->lockname.name);
  2451. msleep(DLM_MIGRATION_RETRY_MS);
  2452. }
  2453. spin_lock(&dlm->spinlock);
  2454. leave:
  2455. return lock_dropped;
  2456. }
  2457. int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
  2458. {
  2459. int ret;
  2460. spin_lock(&dlm->ast_lock);
  2461. spin_lock(&lock->spinlock);
  2462. ret = (list_empty(&lock->bast_list) && !lock->bast_pending);
  2463. spin_unlock(&lock->spinlock);
  2464. spin_unlock(&dlm->ast_lock);
  2465. return ret;
  2466. }
  2467. static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
  2468. struct dlm_lock_resource *res,
  2469. u8 mig_target)
  2470. {
  2471. int can_proceed;
  2472. spin_lock(&res->spinlock);
  2473. can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING);
  2474. spin_unlock(&res->spinlock);
  2475. /* target has died, so make the caller break out of the
  2476. * wait_event, but caller must recheck the domain_map */
  2477. spin_lock(&dlm->spinlock);
  2478. if (!test_bit(mig_target, dlm->domain_map))
  2479. can_proceed = 1;
  2480. spin_unlock(&dlm->spinlock);
  2481. return can_proceed;
  2482. }
  2483. static int dlm_lockres_is_dirty(struct dlm_ctxt *dlm,
  2484. struct dlm_lock_resource *res)
  2485. {
  2486. int ret;
  2487. spin_lock(&res->spinlock);
  2488. ret = !!(res->state & DLM_LOCK_RES_DIRTY);
  2489. spin_unlock(&res->spinlock);
  2490. return ret;
  2491. }
  2492. static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
  2493. struct dlm_lock_resource *res,
  2494. u8 target)
  2495. {
  2496. int ret = 0;
  2497. mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
  2498. res->lockname.len, res->lockname.name, dlm->node_num,
  2499. target);
  2500. /* need to set MIGRATING flag on lockres. this is done by
  2501. * ensuring that all asts have been flushed for this lockres. */
  2502. spin_lock(&res->spinlock);
  2503. BUG_ON(res->migration_pending);
  2504. res->migration_pending = 1;
  2505. /* strategy is to reserve an extra ast then release
  2506. * it below, letting the release do all of the work */
  2507. __dlm_lockres_reserve_ast(res);
  2508. spin_unlock(&res->spinlock);
  2509. /* now flush all the pending asts */
  2510. dlm_kick_thread(dlm, res);
  2511. /* before waiting on DIRTY, block processes which may
  2512. * try to dirty the lockres before MIGRATING is set */
  2513. spin_lock(&res->spinlock);
  2514. BUG_ON(res->state & DLM_LOCK_RES_BLOCK_DIRTY);
  2515. res->state |= DLM_LOCK_RES_BLOCK_DIRTY;
  2516. spin_unlock(&res->spinlock);
  2517. /* now wait on any pending asts and the DIRTY state */
  2518. wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
  2519. dlm_lockres_release_ast(dlm, res);
  2520. mlog(0, "about to wait on migration_wq, dirty=%s\n",
  2521. res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
  2522. /* if the extra ref we just put was the final one, this
  2523. * will pass thru immediately. otherwise, we need to wait
  2524. * for the last ast to finish. */
  2525. again:
  2526. ret = wait_event_interruptible_timeout(dlm->migration_wq,
  2527. dlm_migration_can_proceed(dlm, res, target),
  2528. msecs_to_jiffies(1000));
  2529. if (ret < 0) {
  2530. mlog(0, "woken again: migrating? %s, dead? %s\n",
  2531. res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
  2532. test_bit(target, dlm->domain_map) ? "no":"yes");
  2533. } else {
  2534. mlog(0, "all is well: migrating? %s, dead? %s\n",
  2535. res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
  2536. test_bit(target, dlm->domain_map) ? "no":"yes");
  2537. }
  2538. if (!dlm_migration_can_proceed(dlm, res, target)) {
  2539. mlog(0, "trying again...\n");
  2540. goto again;
  2541. }
  2542. /* now that we are sure the MIGRATING state is there, drop
  2543. * the unneded state which blocked threads trying to DIRTY */
  2544. spin_lock(&res->spinlock);
  2545. BUG_ON(!(res->state & DLM_LOCK_RES_BLOCK_DIRTY));
  2546. BUG_ON(!(res->state & DLM_LOCK_RES_MIGRATING));
  2547. res->state &= ~DLM_LOCK_RES_BLOCK_DIRTY;
  2548. spin_unlock(&res->spinlock);
  2549. /* did the target go down or die? */
  2550. spin_lock(&dlm->spinlock);
  2551. if (!test_bit(target, dlm->domain_map)) {
  2552. mlog(ML_ERROR, "aha. migration target %u just went down\n",
  2553. target);
  2554. ret = -EHOSTDOWN;
  2555. }
  2556. spin_unlock(&dlm->spinlock);
  2557. /*
  2558. * at this point:
  2559. *
  2560. * o the DLM_LOCK_RES_MIGRATING flag is set
  2561. * o there are no pending asts on this lockres
  2562. * o all processes trying to reserve an ast on this
  2563. * lockres must wait for the MIGRATING flag to clear
  2564. */
  2565. return ret;
  2566. }
  2567. /* last step in the migration process.
  2568. * original master calls this to free all of the dlm_lock
  2569. * structures that used to be for other nodes. */
  2570. static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
  2571. struct dlm_lock_resource *res)
  2572. {
  2573. struct list_head *iter, *iter2;
  2574. struct list_head *queue = &res->granted;
  2575. int i, bit;
  2576. struct dlm_lock *lock;
  2577. assert_spin_locked(&res->spinlock);
  2578. BUG_ON(res->owner == dlm->node_num);
  2579. for (i=0; i<3; i++) {
  2580. list_for_each_safe(iter, iter2, queue) {
  2581. lock = list_entry (iter, struct dlm_lock, list);
  2582. if (lock->ml.node != dlm->node_num) {
  2583. mlog(0, "putting lock for node %u\n",
  2584. lock->ml.node);
  2585. /* be extra careful */
  2586. BUG_ON(!list_empty(&lock->ast_list));
  2587. BUG_ON(!list_empty(&lock->bast_list));
  2588. BUG_ON(lock->ast_pending);
  2589. BUG_ON(lock->bast_pending);
  2590. dlm_lockres_clear_refmap_bit(lock->ml.node, res);
  2591. list_del_init(&lock->list);
  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 list_head *iter;
  2623. struct dlm_lock *lock;
  2624. int nodenum;
  2625. assert_spin_locked(&dlm->spinlock);
  2626. spin_lock(&res->spinlock);
  2627. for (i=0; i<3; i++) {
  2628. list_for_each(iter, queue) {
  2629. /* up to the caller to make sure this node
  2630. * is alive */
  2631. lock = list_entry (iter, struct dlm_lock, list);
  2632. if (lock->ml.node != dlm->node_num) {
  2633. spin_unlock(&res->spinlock);
  2634. return lock->ml.node;
  2635. }
  2636. }
  2637. queue++;
  2638. }
  2639. spin_unlock(&res->spinlock);
  2640. mlog(0, "have not found a suitable target yet! checking domain map\n");
  2641. /* ok now we're getting desperate. pick anyone alive. */
  2642. nodenum = -1;
  2643. while (1) {
  2644. nodenum = find_next_bit(dlm->domain_map,
  2645. O2NM_MAX_NODES, nodenum+1);
  2646. mlog(0, "found %d in domain map\n", nodenum);
  2647. if (nodenum >= O2NM_MAX_NODES)
  2648. break;
  2649. if (nodenum != dlm->node_num) {
  2650. mlog(0, "picking %d\n", nodenum);
  2651. return nodenum;
  2652. }
  2653. }
  2654. mlog(0, "giving up. no master to migrate to\n");
  2655. return DLM_LOCK_RES_OWNER_UNKNOWN;
  2656. }
  2657. /* this is called by the new master once all lockres
  2658. * data has been received */
  2659. static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
  2660. struct dlm_lock_resource *res,
  2661. u8 master, u8 new_master,
  2662. struct dlm_node_iter *iter)
  2663. {
  2664. struct dlm_migrate_request migrate;
  2665. int ret, status = 0;
  2666. int nodenum;
  2667. memset(&migrate, 0, sizeof(migrate));
  2668. migrate.namelen = res->lockname.len;
  2669. memcpy(migrate.name, res->lockname.name, migrate.namelen);
  2670. migrate.new_master = new_master;
  2671. migrate.master = master;
  2672. ret = 0;
  2673. /* send message to all nodes, except the master and myself */
  2674. while ((nodenum = dlm_node_iter_next(iter)) >= 0) {
  2675. if (nodenum == master ||
  2676. nodenum == new_master)
  2677. continue;
  2678. ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key,
  2679. &migrate, sizeof(migrate), nodenum,
  2680. &status);
  2681. if (ret < 0)
  2682. mlog_errno(ret);
  2683. else if (status < 0) {
  2684. mlog(0, "migrate request (node %u) returned %d!\n",
  2685. nodenum, status);
  2686. ret = status;
  2687. } else if (status == DLM_MIGRATE_RESPONSE_MASTERY_REF) {
  2688. /* during the migration request we short-circuited
  2689. * the mastery of the lockres. make sure we have
  2690. * a mastery ref for nodenum */
  2691. mlog(0, "%s:%.*s: need ref for node %u\n",
  2692. dlm->name, res->lockname.len, res->lockname.name,
  2693. nodenum);
  2694. spin_lock(&res->spinlock);
  2695. dlm_lockres_set_refmap_bit(nodenum, res);
  2696. spin_unlock(&res->spinlock);
  2697. }
  2698. }
  2699. if (ret < 0)
  2700. mlog_errno(ret);
  2701. mlog(0, "returning ret=%d\n", ret);
  2702. return ret;
  2703. }
  2704. /* if there is an existing mle for this lockres, we now know who the master is.
  2705. * (the one who sent us *this* message) we can clear it up right away.
  2706. * since the process that put the mle on the list still has a reference to it,
  2707. * we can unhash it now, set the master and wake the process. as a result,
  2708. * we will have no mle in the list to start with. now we can add an mle for
  2709. * the migration and this should be the only one found for those scanning the
  2710. * list. */
  2711. int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
  2712. void **ret_data)
  2713. {
  2714. struct dlm_ctxt *dlm = data;
  2715. struct dlm_lock_resource *res = NULL;
  2716. struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf;
  2717. struct dlm_master_list_entry *mle = NULL, *oldmle = NULL;
  2718. const char *name;
  2719. unsigned int namelen, hash;
  2720. int ret = 0;
  2721. if (!dlm_grab(dlm))
  2722. return -EINVAL;
  2723. name = migrate->name;
  2724. namelen = migrate->namelen;
  2725. hash = dlm_lockid_hash(name, namelen);
  2726. /* preallocate.. if this fails, abort */
  2727. mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
  2728. GFP_NOFS);
  2729. if (!mle) {
  2730. ret = -ENOMEM;
  2731. goto leave;
  2732. }
  2733. /* check for pre-existing lock */
  2734. spin_lock(&dlm->spinlock);
  2735. res = __dlm_lookup_lockres(dlm, name, namelen, hash);
  2736. spin_lock(&dlm->master_lock);
  2737. if (res) {
  2738. spin_lock(&res->spinlock);
  2739. if (res->state & DLM_LOCK_RES_RECOVERING) {
  2740. /* if all is working ok, this can only mean that we got
  2741. * a migrate request from a node that we now see as
  2742. * dead. what can we do here? drop it to the floor? */
  2743. spin_unlock(&res->spinlock);
  2744. mlog(ML_ERROR, "Got a migrate request, but the "
  2745. "lockres is marked as recovering!");
  2746. kmem_cache_free(dlm_mle_cache, mle);
  2747. ret = -EINVAL; /* need a better solution */
  2748. goto unlock;
  2749. }
  2750. res->state |= DLM_LOCK_RES_MIGRATING;
  2751. spin_unlock(&res->spinlock);
  2752. }
  2753. /* ignore status. only nonzero status would BUG. */
  2754. ret = dlm_add_migration_mle(dlm, res, mle, &oldmle,
  2755. name, namelen,
  2756. migrate->new_master,
  2757. migrate->master);
  2758. unlock:
  2759. spin_unlock(&dlm->master_lock);
  2760. spin_unlock(&dlm->spinlock);
  2761. if (oldmle) {
  2762. /* master is known, detach if not already detached */
  2763. dlm_mle_detach_hb_events(dlm, oldmle);
  2764. dlm_put_mle(oldmle);
  2765. }
  2766. if (res)
  2767. dlm_lockres_put(res);
  2768. leave:
  2769. dlm_put(dlm);
  2770. return ret;
  2771. }
  2772. /* must be holding dlm->spinlock and dlm->master_lock
  2773. * when adding a migration mle, we can clear any other mles
  2774. * in the master list because we know with certainty that
  2775. * the master is "master". so we remove any old mle from
  2776. * the list after setting it's master field, and then add
  2777. * the new migration mle. this way we can hold with the rule
  2778. * of having only one mle for a given lock name at all times. */
  2779. static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
  2780. struct dlm_lock_resource *res,
  2781. struct dlm_master_list_entry *mle,
  2782. struct dlm_master_list_entry **oldmle,
  2783. const char *name, unsigned int namelen,
  2784. u8 new_master, u8 master)
  2785. {
  2786. int found;
  2787. int ret = 0;
  2788. *oldmle = NULL;
  2789. mlog_entry_void();
  2790. assert_spin_locked(&dlm->spinlock);
  2791. assert_spin_locked(&dlm->master_lock);
  2792. /* caller is responsible for any ref taken here on oldmle */
  2793. found = dlm_find_mle(dlm, oldmle, (char *)name, namelen);
  2794. if (found) {
  2795. struct dlm_master_list_entry *tmp = *oldmle;
  2796. spin_lock(&tmp->spinlock);
  2797. if (tmp->type == DLM_MLE_MIGRATION) {
  2798. if (master == dlm->node_num) {
  2799. /* ah another process raced me to it */
  2800. mlog(0, "tried to migrate %.*s, but some "
  2801. "process beat me to it\n",
  2802. namelen, name);
  2803. ret = -EEXIST;
  2804. } else {
  2805. /* bad. 2 NODES are trying to migrate! */
  2806. mlog(ML_ERROR, "migration error mle: "
  2807. "master=%u new_master=%u // request: "
  2808. "master=%u new_master=%u // "
  2809. "lockres=%.*s\n",
  2810. tmp->master, tmp->new_master,
  2811. master, new_master,
  2812. namelen, name);
  2813. BUG();
  2814. }
  2815. } else {
  2816. /* this is essentially what assert_master does */
  2817. tmp->master = master;
  2818. atomic_set(&tmp->woken, 1);
  2819. wake_up(&tmp->wq);
  2820. /* remove it from the list so that only one
  2821. * mle will be found */
  2822. list_del_init(&tmp->list);
  2823. /* this was obviously WRONG. mle is uninited here. should be tmp. */
  2824. __dlm_mle_detach_hb_events(dlm, tmp);
  2825. ret = DLM_MIGRATE_RESPONSE_MASTERY_REF;
  2826. mlog(0, "%s:%.*s: master=%u, newmaster=%u, "
  2827. "telling master to get ref for cleared out mle "
  2828. "during migration\n", dlm->name, namelen, name,
  2829. master, new_master);
  2830. }
  2831. spin_unlock(&tmp->spinlock);
  2832. }
  2833. /* now add a migration mle to the tail of the list */
  2834. dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
  2835. mle->new_master = new_master;
  2836. /* the new master will be sending an assert master for this.
  2837. * at that point we will get the refmap reference */
  2838. mle->master = master;
  2839. /* do this for consistency with other mle types */
  2840. set_bit(new_master, mle->maybe_map);
  2841. list_add(&mle->list, &dlm->master_list);
  2842. return ret;
  2843. }
  2844. void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
  2845. {
  2846. struct list_head *iter, *iter2;
  2847. struct dlm_master_list_entry *mle;
  2848. struct dlm_lock_resource *res;
  2849. unsigned int hash;
  2850. mlog_entry("dlm=%s, dead node=%u\n", dlm->name, dead_node);
  2851. top:
  2852. assert_spin_locked(&dlm->spinlock);
  2853. /* clean the master list */
  2854. spin_lock(&dlm->master_lock);
  2855. list_for_each_safe(iter, iter2, &dlm->master_list) {
  2856. mle = list_entry(iter, struct dlm_master_list_entry, list);
  2857. BUG_ON(mle->type != DLM_MLE_BLOCK &&
  2858. mle->type != DLM_MLE_MASTER &&
  2859. mle->type != DLM_MLE_MIGRATION);
  2860. /* MASTER mles are initiated locally. the waiting
  2861. * process will notice the node map change
  2862. * shortly. let that happen as normal. */
  2863. if (mle->type == DLM_MLE_MASTER)
  2864. continue;
  2865. /* BLOCK mles are initiated by other nodes.
  2866. * need to clean up if the dead node would have
  2867. * been the master. */
  2868. if (mle->type == DLM_MLE_BLOCK) {
  2869. int bit;
  2870. spin_lock(&mle->spinlock);
  2871. bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
  2872. if (bit != dead_node) {
  2873. mlog(0, "mle found, but dead node %u would "
  2874. "not have been master\n", dead_node);
  2875. spin_unlock(&mle->spinlock);
  2876. } else {
  2877. /* must drop the refcount by one since the
  2878. * assert_master will never arrive. this
  2879. * may result in the mle being unlinked and
  2880. * freed, but there may still be a process
  2881. * waiting in the dlmlock path which is fine. */
  2882. mlog(0, "node %u was expected master\n",
  2883. dead_node);
  2884. atomic_set(&mle->woken, 1);
  2885. spin_unlock(&mle->spinlock);
  2886. wake_up(&mle->wq);
  2887. /* do not need events any longer, so detach
  2888. * from heartbeat */
  2889. __dlm_mle_detach_hb_events(dlm, mle);
  2890. __dlm_put_mle(mle);
  2891. }
  2892. continue;
  2893. }
  2894. /* everything else is a MIGRATION mle */
  2895. /* the rule for MIGRATION mles is that the master
  2896. * becomes UNKNOWN if *either* the original or
  2897. * the new master dies. all UNKNOWN lockreses
  2898. * are sent to whichever node becomes the recovery
  2899. * master. the new master is responsible for
  2900. * determining if there is still a master for
  2901. * this lockres, or if he needs to take over
  2902. * mastery. either way, this node should expect
  2903. * another message to resolve this. */
  2904. if (mle->master != dead_node &&
  2905. mle->new_master != dead_node)
  2906. continue;
  2907. /* if we have reached this point, this mle needs to
  2908. * be removed from the list and freed. */
  2909. /* remove from the list early. NOTE: unlinking
  2910. * list_head while in list_for_each_safe */
  2911. __dlm_mle_detach_hb_events(dlm, mle);
  2912. spin_lock(&mle->spinlock);
  2913. list_del_init(&mle->list);
  2914. atomic_set(&mle->woken, 1);
  2915. spin_unlock(&mle->spinlock);
  2916. wake_up(&mle->wq);
  2917. mlog(0, "%s: node %u died during migration from "
  2918. "%u to %u!\n", dlm->name, dead_node,
  2919. mle->master, mle->new_master);
  2920. /* if there is a lockres associated with this
  2921. * mle, find it and set its owner to UNKNOWN */
  2922. hash = dlm_lockid_hash(mle->u.name.name, mle->u.name.len);
  2923. res = __dlm_lookup_lockres(dlm, mle->u.name.name,
  2924. mle->u.name.len, hash);
  2925. if (res) {
  2926. /* unfortunately if we hit this rare case, our
  2927. * lock ordering is messed. we need to drop
  2928. * the master lock so that we can take the
  2929. * lockres lock, meaning that we will have to
  2930. * restart from the head of list. */
  2931. spin_unlock(&dlm->master_lock);
  2932. /* move lockres onto recovery list */
  2933. spin_lock(&res->spinlock);
  2934. dlm_set_lockres_owner(dlm, res,
  2935. DLM_LOCK_RES_OWNER_UNKNOWN);
  2936. dlm_move_lockres_to_recovery_list(dlm, res);
  2937. spin_unlock(&res->spinlock);
  2938. dlm_lockres_put(res);
  2939. /* about to get rid of mle, detach from heartbeat */
  2940. __dlm_mle_detach_hb_events(dlm, mle);
  2941. /* dump the mle */
  2942. spin_lock(&dlm->master_lock);
  2943. __dlm_put_mle(mle);
  2944. spin_unlock(&dlm->master_lock);
  2945. /* restart */
  2946. goto top;
  2947. }
  2948. /* this may be the last reference */
  2949. __dlm_put_mle(mle);
  2950. }
  2951. spin_unlock(&dlm->master_lock);
  2952. }
  2953. int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
  2954. u8 old_master)
  2955. {
  2956. struct dlm_node_iter iter;
  2957. int ret = 0;
  2958. spin_lock(&dlm->spinlock);
  2959. dlm_node_iter_init(dlm->domain_map, &iter);
  2960. clear_bit(old_master, iter.node_map);
  2961. clear_bit(dlm->node_num, iter.node_map);
  2962. spin_unlock(&dlm->spinlock);
  2963. /* ownership of the lockres is changing. account for the
  2964. * mastery reference here since old_master will briefly have
  2965. * a reference after the migration completes */
  2966. spin_lock(&res->spinlock);
  2967. dlm_lockres_set_refmap_bit(old_master, res);
  2968. spin_unlock(&res->spinlock);
  2969. mlog(0, "now time to do a migrate request to other nodes\n");
  2970. ret = dlm_do_migrate_request(dlm, res, old_master,
  2971. dlm->node_num, &iter);
  2972. if (ret < 0) {
  2973. mlog_errno(ret);
  2974. goto leave;
  2975. }
  2976. mlog(0, "doing assert master of %.*s to all except the original node\n",
  2977. res->lockname.len, res->lockname.name);
  2978. /* this call now finishes out the nodemap
  2979. * even if one or more nodes die */
  2980. ret = dlm_do_assert_master(dlm, res, iter.node_map,
  2981. DLM_ASSERT_MASTER_FINISH_MIGRATION);
  2982. if (ret < 0) {
  2983. /* no longer need to retry. all living nodes contacted. */
  2984. mlog_errno(ret);
  2985. ret = 0;
  2986. }
  2987. memset(iter.node_map, 0, sizeof(iter.node_map));
  2988. set_bit(old_master, iter.node_map);
  2989. mlog(0, "doing assert master of %.*s back to %u\n",
  2990. res->lockname.len, res->lockname.name, old_master);
  2991. ret = dlm_do_assert_master(dlm, res, iter.node_map,
  2992. DLM_ASSERT_MASTER_FINISH_MIGRATION);
  2993. if (ret < 0) {
  2994. mlog(0, "assert master to original master failed "
  2995. "with %d.\n", ret);
  2996. /* the only nonzero status here would be because of
  2997. * a dead original node. we're done. */
  2998. ret = 0;
  2999. }
  3000. /* all done, set the owner, clear the flag */
  3001. spin_lock(&res->spinlock);
  3002. dlm_set_lockres_owner(dlm, res, dlm->node_num);
  3003. res->state &= ~DLM_LOCK_RES_MIGRATING;
  3004. spin_unlock(&res->spinlock);
  3005. /* re-dirty it on the new master */
  3006. dlm_kick_thread(dlm, res);
  3007. wake_up(&res->wq);
  3008. leave:
  3009. return ret;
  3010. }
  3011. /*
  3012. * LOCKRES AST REFCOUNT
  3013. * this is integral to migration
  3014. */
  3015. /* for future intent to call an ast, reserve one ahead of time.
  3016. * this should be called only after waiting on the lockres
  3017. * with dlm_wait_on_lockres, and while still holding the
  3018. * spinlock after the call. */
  3019. void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res)
  3020. {
  3021. assert_spin_locked(&res->spinlock);
  3022. if (res->state & DLM_LOCK_RES_MIGRATING) {
  3023. __dlm_print_one_lock_resource(res);
  3024. }
  3025. BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
  3026. atomic_inc(&res->asts_reserved);
  3027. }
  3028. /*
  3029. * used to drop the reserved ast, either because it went unused,
  3030. * or because the ast/bast was actually called.
  3031. *
  3032. * also, if there is a pending migration on this lockres,
  3033. * and this was the last pending ast on the lockres,
  3034. * atomically set the MIGRATING flag before we drop the lock.
  3035. * this is how we ensure that migration can proceed with no
  3036. * asts in progress. note that it is ok if the state of the
  3037. * queues is such that a lock should be granted in the future
  3038. * or that a bast should be fired, because the new master will
  3039. * shuffle the lists on this lockres as soon as it is migrated.
  3040. */
  3041. void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
  3042. struct dlm_lock_resource *res)
  3043. {
  3044. if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock))
  3045. return;
  3046. if (!res->migration_pending) {
  3047. spin_unlock(&res->spinlock);
  3048. return;
  3049. }
  3050. BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
  3051. res->migration_pending = 0;
  3052. res->state |= DLM_LOCK_RES_MIGRATING;
  3053. spin_unlock(&res->spinlock);
  3054. wake_up(&res->wq);
  3055. wake_up(&dlm->migration_wq);
  3056. }