dlmmaster.c 94 KB

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