dlmmaster.c 81 KB

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