~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ocfs2/dlm/dlmmaster.c

Version: ~ [ linux-5.9-rc5 ] ~ [ linux-5.8.10 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.66 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.146 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.198 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.236 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.236 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp