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