blob: 0aa4b043f4de8626ac265457bcb9a543de9e22bc [file] [log] [blame]
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001/* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
3 *
4 * dlmmod.c
5 *
6 * standalone DLM module
7 *
8 * Copyright (C) 2004 Oracle. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
24 *
25 */
26
27
28#include <linux/module.h>
29#include <linux/fs.h>
30#include <linux/types.h>
31#include <linux/slab.h>
32#include <linux/highmem.h>
33#include <linux/utsname.h>
34#include <linux/init.h>
35#include <linux/sysctl.h>
36#include <linux/random.h>
37#include <linux/blkdev.h>
38#include <linux/socket.h>
39#include <linux/inet.h>
40#include <linux/spinlock.h>
41#include <linux/delay.h>
42
43
44#include "cluster/heartbeat.h"
45#include "cluster/nodemanager.h"
46#include "cluster/tcp.h"
47
48#include "dlmapi.h"
49#include "dlmcommon.h"
Adrian Bunk82353b52005-12-19 11:16:07 -080050#include "dlmdomain.h"
Sunil Mushrane5a03342008-03-10 15:16:28 -070051#include "dlmdebug.h"
Kurt Hackel6714d8e2005-12-15 14:31:23 -080052
53#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_MASTER)
54#include "cluster/masklog.h"
55
Kurt Hackel6714d8e2005-12-15 14:31:23 -080056static void dlm_mle_node_down(struct dlm_ctxt *dlm,
57 struct dlm_master_list_entry *mle,
58 struct o2nm_node *node,
59 int idx);
60static void dlm_mle_node_up(struct dlm_ctxt *dlm,
61 struct dlm_master_list_entry *mle,
62 struct o2nm_node *node,
63 int idx);
64
65static void dlm_assert_master_worker(struct dlm_work_item *item, void *data);
Kurt Hackelba2bf212006-12-01 14:47:20 -080066static int dlm_do_assert_master(struct dlm_ctxt *dlm,
67 struct dlm_lock_resource *res,
68 void *nodemap, u32 flags);
Sunil Mushranf3f85462007-01-29 15:19:16 -080069static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data);
Kurt Hackel6714d8e2005-12-15 14:31:23 -080070
71static inline int dlm_mle_equal(struct dlm_ctxt *dlm,
72 struct dlm_master_list_entry *mle,
73 const char *name,
74 unsigned int namelen)
75{
76 struct dlm_lock_resource *res;
77
78 if (dlm != mle->dlm)
79 return 0;
80
81 if (mle->type == DLM_MLE_BLOCK ||
82 mle->type == DLM_MLE_MIGRATION) {
83 if (namelen != mle->u.name.len ||
84 memcmp(name, mle->u.name.name, namelen)!=0)
85 return 0;
86 } else {
87 res = mle->u.res;
88 if (namelen != res->lockname.len ||
89 memcmp(res->lockname.name, name, namelen) != 0)
90 return 0;
91 }
92 return 1;
93}
94
Sunil Mushran724bdca2008-03-10 15:16:20 -070095static struct kmem_cache *dlm_lockres_cache = NULL;
96static struct kmem_cache *dlm_lockname_cache = NULL;
Christoph Lametere18b8902006-12-06 20:33:20 -080097static struct kmem_cache *dlm_mle_cache = NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -080098
Kurt Hackel6714d8e2005-12-15 14:31:23 -080099static void dlm_mle_release(struct kref *kref);
100static void dlm_init_mle(struct dlm_master_list_entry *mle,
101 enum dlm_mle_type type,
102 struct dlm_ctxt *dlm,
103 struct dlm_lock_resource *res,
104 const char *name,
105 unsigned int namelen);
106static void dlm_put_mle(struct dlm_master_list_entry *mle);
107static void __dlm_put_mle(struct dlm_master_list_entry *mle);
108static int dlm_find_mle(struct dlm_ctxt *dlm,
109 struct dlm_master_list_entry **mle,
110 char *name, unsigned int namelen);
111
Kurt Hackelba2bf212006-12-01 14:47:20 -0800112static int dlm_do_master_request(struct dlm_lock_resource *res,
113 struct dlm_master_list_entry *mle, int to);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800114
115
116static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
117 struct dlm_lock_resource *res,
118 struct dlm_master_list_entry *mle,
119 int *blocked);
120static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
121 struct dlm_lock_resource *res,
122 struct dlm_master_list_entry *mle,
123 int blocked);
124static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
125 struct dlm_lock_resource *res,
126 struct dlm_master_list_entry *mle,
127 struct dlm_master_list_entry **oldmle,
128 const char *name, unsigned int namelen,
129 u8 new_master, u8 master);
130
131static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
132 struct dlm_lock_resource *res);
133static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
134 struct dlm_lock_resource *res);
135static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
136 struct dlm_lock_resource *res,
137 u8 target);
Kurt Hackelc03872f2006-03-06 14:08:49 -0800138static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
139 struct dlm_lock_resource *res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800140
141
142int dlm_is_host_down(int errno)
143{
144 switch (errno) {
145 case -EBADF:
146 case -ECONNREFUSED:
147 case -ENOTCONN:
148 case -ECONNRESET:
149 case -EPIPE:
150 case -EHOSTDOWN:
151 case -EHOSTUNREACH:
152 case -ETIMEDOUT:
153 case -ECONNABORTED:
154 case -ENETDOWN:
155 case -ENETUNREACH:
156 case -ENETRESET:
157 case -ESHUTDOWN:
158 case -ENOPROTOOPT:
159 case -EINVAL: /* if returned from our tcp code,
160 this means there is no socket */
161 return 1;
162 }
163 return 0;
164}
165
166
167/*
168 * MASTER LIST FUNCTIONS
169 */
170
171
172/*
173 * regarding master list entries and heartbeat callbacks:
174 *
175 * in order to avoid sleeping and allocation that occurs in
176 * heartbeat, master list entries are simply attached to the
177 * dlm's established heartbeat callbacks. the mle is attached
178 * when it is created, and since the dlm->spinlock is held at
179 * that time, any heartbeat event will be properly discovered
180 * by the mle. the mle needs to be detached from the
181 * dlm->mle_hb_events list as soon as heartbeat events are no
182 * longer useful to the mle, and before the mle is freed.
183 *
184 * as a general rule, heartbeat events are no longer needed by
185 * the mle once an "answer" regarding the lock master has been
186 * received.
187 */
188static inline void __dlm_mle_attach_hb_events(struct dlm_ctxt *dlm,
189 struct dlm_master_list_entry *mle)
190{
191 assert_spin_locked(&dlm->spinlock);
192
193 list_add_tail(&mle->hb_events, &dlm->mle_hb_events);
194}
195
196
197static inline void __dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
198 struct dlm_master_list_entry *mle)
199{
200 if (!list_empty(&mle->hb_events))
201 list_del_init(&mle->hb_events);
202}
203
204
205static inline void dlm_mle_detach_hb_events(struct dlm_ctxt *dlm,
206 struct dlm_master_list_entry *mle)
207{
208 spin_lock(&dlm->spinlock);
209 __dlm_mle_detach_hb_events(dlm, mle);
210 spin_unlock(&dlm->spinlock);
211}
212
Kurt Hackela2bf0472006-04-27 18:51:26 -0700213static void dlm_get_mle_inuse(struct dlm_master_list_entry *mle)
214{
215 struct dlm_ctxt *dlm;
216 dlm = mle->dlm;
217
218 assert_spin_locked(&dlm->spinlock);
219 assert_spin_locked(&dlm->master_lock);
220 mle->inuse++;
221 kref_get(&mle->mle_refs);
222}
223
224static void dlm_put_mle_inuse(struct dlm_master_list_entry *mle)
225{
226 struct dlm_ctxt *dlm;
227 dlm = mle->dlm;
228
229 spin_lock(&dlm->spinlock);
230 spin_lock(&dlm->master_lock);
231 mle->inuse--;
232 __dlm_put_mle(mle);
233 spin_unlock(&dlm->master_lock);
234 spin_unlock(&dlm->spinlock);
235
236}
237
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800238/* remove from list and free */
239static void __dlm_put_mle(struct dlm_master_list_entry *mle)
240{
241 struct dlm_ctxt *dlm;
242 dlm = mle->dlm;
243
244 assert_spin_locked(&dlm->spinlock);
245 assert_spin_locked(&dlm->master_lock);
Kurt Hackelaa852352006-04-27 19:04:49 -0700246 if (!atomic_read(&mle->mle_refs.refcount)) {
247 /* this may or may not crash, but who cares.
248 * it's a BUG. */
249 mlog(ML_ERROR, "bad mle: %p\n", mle);
250 dlm_print_one_mle(mle);
251 BUG();
252 } else
253 kref_put(&mle->mle_refs, dlm_mle_release);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800254}
255
256
257/* must not have any spinlocks coming in */
258static void dlm_put_mle(struct dlm_master_list_entry *mle)
259{
260 struct dlm_ctxt *dlm;
261 dlm = mle->dlm;
262
263 spin_lock(&dlm->spinlock);
264 spin_lock(&dlm->master_lock);
265 __dlm_put_mle(mle);
266 spin_unlock(&dlm->master_lock);
267 spin_unlock(&dlm->spinlock);
268}
269
270static inline void dlm_get_mle(struct dlm_master_list_entry *mle)
271{
272 kref_get(&mle->mle_refs);
273}
274
275static void dlm_init_mle(struct dlm_master_list_entry *mle,
276 enum dlm_mle_type type,
277 struct dlm_ctxt *dlm,
278 struct dlm_lock_resource *res,
279 const char *name,
280 unsigned int namelen)
281{
282 assert_spin_locked(&dlm->spinlock);
283
284 mle->dlm = dlm;
285 mle->type = type;
286 INIT_LIST_HEAD(&mle->list);
287 INIT_LIST_HEAD(&mle->hb_events);
288 memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
289 spin_lock_init(&mle->spinlock);
290 init_waitqueue_head(&mle->wq);
291 atomic_set(&mle->woken, 0);
292 kref_init(&mle->mle_refs);
293 memset(mle->response_map, 0, sizeof(mle->response_map));
294 mle->master = O2NM_MAX_NODES;
295 mle->new_master = O2NM_MAX_NODES;
Kurt Hackela2bf0472006-04-27 18:51:26 -0700296 mle->inuse = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800297
298 if (mle->type == DLM_MLE_MASTER) {
299 BUG_ON(!res);
300 mle->u.res = res;
301 } else if (mle->type == DLM_MLE_BLOCK) {
302 BUG_ON(!name);
303 memcpy(mle->u.name.name, name, namelen);
304 mle->u.name.len = namelen;
305 } else /* DLM_MLE_MIGRATION */ {
306 BUG_ON(!name);
307 memcpy(mle->u.name.name, name, namelen);
308 mle->u.name.len = namelen;
309 }
310
311 /* copy off the node_map and register hb callbacks on our copy */
312 memcpy(mle->node_map, dlm->domain_map, sizeof(mle->node_map));
313 memcpy(mle->vote_map, dlm->domain_map, sizeof(mle->vote_map));
314 clear_bit(dlm->node_num, mle->vote_map);
315 clear_bit(dlm->node_num, mle->node_map);
316
317 /* attach the mle to the domain node up/down events */
318 __dlm_mle_attach_hb_events(dlm, mle);
319}
320
Sunil Mushran1c084572009-02-26 15:00:37 -0800321void __dlm_unlink_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle)
322{
323 assert_spin_locked(&dlm->spinlock);
324 assert_spin_locked(&dlm->master_lock);
325
326 if (!list_empty(&mle->list))
327 list_del_init(&mle->list);
328}
329
330void __dlm_insert_mle(struct dlm_ctxt *dlm, struct dlm_master_list_entry *mle)
331{
332 assert_spin_locked(&dlm->master_lock);
333
334 list_add(&mle->list, &dlm->master_list);
335}
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800336
337/* returns 1 if found, 0 if not */
338static int dlm_find_mle(struct dlm_ctxt *dlm,
339 struct dlm_master_list_entry **mle,
340 char *name, unsigned int namelen)
341{
342 struct dlm_master_list_entry *tmpmle;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800343
344 assert_spin_locked(&dlm->master_lock);
345
Christoph Hellwig800deef2007-05-17 16:03:13 +0200346 list_for_each_entry(tmpmle, &dlm->master_list, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800347 if (!dlm_mle_equal(dlm, tmpmle, name, namelen))
348 continue;
349 dlm_get_mle(tmpmle);
350 *mle = tmpmle;
351 return 1;
352 }
353 return 0;
354}
355
356void dlm_hb_event_notify_attached(struct dlm_ctxt *dlm, int idx, int node_up)
357{
358 struct dlm_master_list_entry *mle;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800359
360 assert_spin_locked(&dlm->spinlock);
361
Christoph Hellwig800deef2007-05-17 16:03:13 +0200362 list_for_each_entry(mle, &dlm->mle_hb_events, hb_events) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800363 if (node_up)
364 dlm_mle_node_up(dlm, mle, NULL, idx);
365 else
366 dlm_mle_node_down(dlm, mle, NULL, idx);
367 }
368}
369
370static void dlm_mle_node_down(struct dlm_ctxt *dlm,
371 struct dlm_master_list_entry *mle,
372 struct o2nm_node *node, int idx)
373{
374 spin_lock(&mle->spinlock);
375
376 if (!test_bit(idx, mle->node_map))
377 mlog(0, "node %u already removed from nodemap!\n", idx);
378 else
379 clear_bit(idx, mle->node_map);
380
381 spin_unlock(&mle->spinlock);
382}
383
384static void dlm_mle_node_up(struct dlm_ctxt *dlm,
385 struct dlm_master_list_entry *mle,
386 struct o2nm_node *node, int idx)
387{
388 spin_lock(&mle->spinlock);
389
390 if (test_bit(idx, mle->node_map))
391 mlog(0, "node %u already in node map!\n", idx);
392 else
393 set_bit(idx, mle->node_map);
394
395 spin_unlock(&mle->spinlock);
396}
397
398
399int dlm_init_mle_cache(void)
400{
Sunil Mushran12eb0032008-03-10 15:16:19 -0700401 dlm_mle_cache = kmem_cache_create("o2dlm_mle",
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800402 sizeof(struct dlm_master_list_entry),
403 0, SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +0900404 NULL);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800405 if (dlm_mle_cache == NULL)
406 return -ENOMEM;
407 return 0;
408}
409
410void dlm_destroy_mle_cache(void)
411{
412 if (dlm_mle_cache)
413 kmem_cache_destroy(dlm_mle_cache);
414}
415
416static void dlm_mle_release(struct kref *kref)
417{
418 struct dlm_master_list_entry *mle;
419 struct dlm_ctxt *dlm;
420
421 mlog_entry_void();
422
423 mle = container_of(kref, struct dlm_master_list_entry, mle_refs);
424 dlm = mle->dlm;
425
426 if (mle->type != DLM_MLE_MASTER) {
427 mlog(0, "calling mle_release for %.*s, type %d\n",
428 mle->u.name.len, mle->u.name.name, mle->type);
429 } else {
430 mlog(0, "calling mle_release for %.*s, type %d\n",
431 mle->u.res->lockname.len,
432 mle->u.res->lockname.name, mle->type);
433 }
434 assert_spin_locked(&dlm->spinlock);
435 assert_spin_locked(&dlm->master_lock);
436
437 /* remove from list if not already */
Sunil Mushran1c084572009-02-26 15:00:37 -0800438 __dlm_unlink_mle(dlm, mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800439
440 /* detach the mle from the domain node up/down events */
441 __dlm_mle_detach_hb_events(dlm, mle);
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
Sunil Mushran724bdca2008-03-10 15:16:20 -0700453int 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;
468bail:
469 dlm_destroy_master_caches();
470 return -ENOMEM;
471}
472
473void 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
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800482static void dlm_set_lockres_owner(struct dlm_ctxt *dlm,
483 struct dlm_lock_resource *res,
484 u8 owner)
485{
486 assert_spin_locked(&res->spinlock);
487
488 mlog_entry("%.*s, %u\n", res->lockname.len, res->lockname.name, owner);
489
490 if (owner == dlm->node_num)
491 atomic_inc(&dlm->local_resources);
492 else if (owner == DLM_LOCK_RES_OWNER_UNKNOWN)
493 atomic_inc(&dlm->unknown_resources);
494 else
495 atomic_inc(&dlm->remote_resources);
496
497 res->owner = owner;
498}
499
500void dlm_change_lockres_owner(struct dlm_ctxt *dlm,
501 struct dlm_lock_resource *res, u8 owner)
502{
503 assert_spin_locked(&res->spinlock);
504
505 if (owner == res->owner)
506 return;
507
508 if (res->owner == dlm->node_num)
509 atomic_dec(&dlm->local_resources);
510 else if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN)
511 atomic_dec(&dlm->unknown_resources);
512 else
513 atomic_dec(&dlm->remote_resources);
514
515 dlm_set_lockres_owner(dlm, res, owner);
516}
517
518
519static void dlm_lockres_release(struct kref *kref)
520{
521 struct dlm_lock_resource *res;
Sunil Mushranb0d4f812008-12-16 15:49:22 -0800522 struct dlm_ctxt *dlm;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800523
524 res = container_of(kref, struct dlm_lock_resource, refs);
Sunil Mushranb0d4f812008-12-16 15:49:22 -0800525 dlm = res->dlm;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800526
527 /* This should not happen -- all lockres' have a name
528 * associated with them at init time. */
529 BUG_ON(!res->lockname.name);
530
531 mlog(0, "destroying lockres %.*s\n", res->lockname.len,
532 res->lockname.name);
533
Sunil Mushranb0d4f812008-12-16 15:49:22 -0800534 spin_lock(&dlm->track_lock);
Sunil Mushran29576f82008-03-10 15:16:21 -0700535 if (!list_empty(&res->tracking))
536 list_del_init(&res->tracking);
537 else {
538 mlog(ML_ERROR, "Resource %.*s not on the Tracking list\n",
539 res->lockname.len, res->lockname.name);
540 dlm_print_one_lock_resource(res);
541 }
Sunil Mushranb0d4f812008-12-16 15:49:22 -0800542 spin_unlock(&dlm->track_lock);
543
544 dlm_put(dlm);
Sunil Mushran29576f82008-03-10 15:16:21 -0700545
Kurt Hackela7f90d82006-04-27 19:24:21 -0700546 if (!hlist_unhashed(&res->hash_node) ||
547 !list_empty(&res->granted) ||
548 !list_empty(&res->converting) ||
549 !list_empty(&res->blocked) ||
550 !list_empty(&res->dirty) ||
551 !list_empty(&res->recovering) ||
552 !list_empty(&res->purge)) {
553 mlog(ML_ERROR,
554 "Going to BUG for resource %.*s."
555 " We're on a list! [%c%c%c%c%c%c%c]\n",
556 res->lockname.len, res->lockname.name,
557 !hlist_unhashed(&res->hash_node) ? 'H' : ' ',
558 !list_empty(&res->granted) ? 'G' : ' ',
559 !list_empty(&res->converting) ? 'C' : ' ',
560 !list_empty(&res->blocked) ? 'B' : ' ',
561 !list_empty(&res->dirty) ? 'D' : ' ',
562 !list_empty(&res->recovering) ? 'R' : ' ',
563 !list_empty(&res->purge) ? 'P' : ' ');
564
565 dlm_print_one_lock_resource(res);
566 }
567
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800568 /* By the time we're ready to blow this guy away, we shouldn't
569 * be on any lists. */
Mark Fasheh81f20942006-02-28 17:31:22 -0800570 BUG_ON(!hlist_unhashed(&res->hash_node));
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800571 BUG_ON(!list_empty(&res->granted));
572 BUG_ON(!list_empty(&res->converting));
573 BUG_ON(!list_empty(&res->blocked));
574 BUG_ON(!list_empty(&res->dirty));
575 BUG_ON(!list_empty(&res->recovering));
576 BUG_ON(!list_empty(&res->purge));
577
Sunil Mushran724bdca2008-03-10 15:16:20 -0700578 kmem_cache_free(dlm_lockname_cache, (void *)res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800579
Sunil Mushran724bdca2008-03-10 15:16:20 -0700580 kmem_cache_free(dlm_lockres_cache, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800581}
582
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800583void dlm_lockres_put(struct dlm_lock_resource *res)
584{
585 kref_put(&res->refs, dlm_lockres_release);
586}
587
588static void dlm_init_lockres(struct dlm_ctxt *dlm,
589 struct dlm_lock_resource *res,
590 const char *name, unsigned int namelen)
591{
592 char *qname;
593
594 /* If we memset here, we lose our reference to the kmalloc'd
595 * res->lockname.name, so be sure to init every field
596 * correctly! */
597
598 qname = (char *) res->lockname.name;
599 memcpy(qname, name, namelen);
600
601 res->lockname.len = namelen;
Mark Fasheha3d33292006-03-09 17:55:56 -0800602 res->lockname.hash = dlm_lockid_hash(name, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800603
604 init_waitqueue_head(&res->wq);
605 spin_lock_init(&res->spinlock);
Mark Fasheh81f20942006-02-28 17:31:22 -0800606 INIT_HLIST_NODE(&res->hash_node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800607 INIT_LIST_HEAD(&res->granted);
608 INIT_LIST_HEAD(&res->converting);
609 INIT_LIST_HEAD(&res->blocked);
610 INIT_LIST_HEAD(&res->dirty);
611 INIT_LIST_HEAD(&res->recovering);
612 INIT_LIST_HEAD(&res->purge);
Sunil Mushran29576f82008-03-10 15:16:21 -0700613 INIT_LIST_HEAD(&res->tracking);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800614 atomic_set(&res->asts_reserved, 0);
615 res->migration_pending = 0;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800616 res->inflight_locks = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800617
Sunil Mushranb0d4f812008-12-16 15:49:22 -0800618 /* put in dlm_lockres_release */
619 dlm_grab(dlm);
620 res->dlm = dlm;
621
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800622 kref_init(&res->refs);
623
624 /* just for consistency */
625 spin_lock(&res->spinlock);
626 dlm_set_lockres_owner(dlm, res, DLM_LOCK_RES_OWNER_UNKNOWN);
627 spin_unlock(&res->spinlock);
628
629 res->state = DLM_LOCK_RES_IN_PROGRESS;
630
631 res->last_used = 0;
632
Sunil Mushran18c6ac32008-07-07 10:06:29 -0700633 spin_lock(&dlm->spinlock);
Sunil Mushran29576f82008-03-10 15:16:21 -0700634 list_add_tail(&res->tracking, &dlm->tracking_list);
Sunil Mushran18c6ac32008-07-07 10:06:29 -0700635 spin_unlock(&dlm->spinlock);
Sunil Mushran29576f82008-03-10 15:16:21 -0700636
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800637 memset(res->lvb, 0, DLM_LVB_LEN);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800638 memset(res->refmap, 0, sizeof(res->refmap));
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800639}
640
641struct dlm_lock_resource *dlm_new_lockres(struct dlm_ctxt *dlm,
642 const char *name,
643 unsigned int namelen)
644{
Sunil Mushran724bdca2008-03-10 15:16:20 -0700645 struct dlm_lock_resource *res = NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800646
Sunil Mushran724bdca2008-03-10 15:16:20 -0700647 res = (struct dlm_lock_resource *)
648 kmem_cache_zalloc(dlm_lockres_cache, GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800649 if (!res)
Sunil Mushran724bdca2008-03-10 15:16:20 -0700650 goto error;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800651
Sunil Mushran724bdca2008-03-10 15:16:20 -0700652 res->lockname.name = (char *)
653 kmem_cache_zalloc(dlm_lockname_cache, GFP_NOFS);
654 if (!res->lockname.name)
655 goto error;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800656
657 dlm_init_lockres(dlm, res, name, namelen);
658 return res;
Sunil Mushran724bdca2008-03-10 15:16:20 -0700659
660error:
661 if (res && res->lockname.name)
662 kmem_cache_free(dlm_lockname_cache, (void *)res->lockname.name);
663
664 if (res)
665 kmem_cache_free(dlm_lockres_cache, res);
666 return NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800667}
668
Kurt Hackelba2bf212006-12-01 14:47:20 -0800669void __dlm_lockres_grab_inflight_ref(struct dlm_ctxt *dlm,
670 struct dlm_lock_resource *res,
671 int new_lockres,
672 const char *file,
673 int line)
674{
675 if (!new_lockres)
676 assert_spin_locked(&res->spinlock);
677
678 if (!test_bit(dlm->node_num, res->refmap)) {
679 BUG_ON(res->inflight_locks != 0);
680 dlm_lockres_set_refmap_bit(dlm->node_num, res);
681 }
682 res->inflight_locks++;
683 mlog(0, "%s:%.*s: inflight++: now %u\n",
684 dlm->name, res->lockname.len, res->lockname.name,
685 res->inflight_locks);
686}
687
688void __dlm_lockres_drop_inflight_ref(struct dlm_ctxt *dlm,
689 struct dlm_lock_resource *res,
690 const char *file,
691 int line)
692{
693 assert_spin_locked(&res->spinlock);
694
695 BUG_ON(res->inflight_locks == 0);
696 res->inflight_locks--;
697 mlog(0, "%s:%.*s: inflight--: now %u\n",
698 dlm->name, res->lockname.len, res->lockname.name,
699 res->inflight_locks);
700 if (res->inflight_locks == 0)
701 dlm_lockres_clear_refmap_bit(dlm->node_num, res);
702 wake_up(&res->wq);
703}
704
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800705/*
706 * lookup a lock resource by name.
707 * may already exist in the hashtable.
708 * lockid is null terminated
709 *
710 * if not, allocate enough for the lockres and for
711 * the temporary structure used in doing the mastering.
712 *
713 * also, do a lookup in the dlm->master_list to see
714 * if another node has begun mastering the same lock.
715 * if so, there should be a block entry in there
716 * for this name, and we should *not* attempt to master
717 * the lock here. need to wait around for that node
718 * to assert_master (or die).
719 *
720 */
721struct dlm_lock_resource * dlm_get_lock_resource(struct dlm_ctxt *dlm,
722 const char *lockid,
Mark Fasheh3384f3d2006-09-08 11:38:29 -0700723 int namelen,
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800724 int flags)
725{
726 struct dlm_lock_resource *tmpres=NULL, *res=NULL;
727 struct dlm_master_list_entry *mle = NULL;
728 struct dlm_master_list_entry *alloc_mle = NULL;
729 int blocked = 0;
730 int ret, nodenum;
731 struct dlm_node_iter iter;
Mark Fasheh3384f3d2006-09-08 11:38:29 -0700732 unsigned int hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800733 int tries = 0;
Kurt Hackelc03872f2006-03-06 14:08:49 -0800734 int bit, wait_on_recovery = 0;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800735 int drop_inflight_if_nonlocal = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800736
737 BUG_ON(!lockid);
738
Mark Fasheha3d33292006-03-09 17:55:56 -0800739 hash = dlm_lockid_hash(lockid, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800740
741 mlog(0, "get lockres %s (len %d)\n", lockid, namelen);
742
743lookup:
744 spin_lock(&dlm->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800745 tmpres = __dlm_lookup_lockres_full(dlm, lockid, namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800746 if (tmpres) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800747 int dropping_ref = 0;
748
Sunil Mushran7b791d62008-12-16 15:49:23 -0800749 spin_unlock(&dlm->spinlock);
750
Kurt Hackelba2bf212006-12-01 14:47:20 -0800751 spin_lock(&tmpres->spinlock);
Sunil Mushran7b791d62008-12-16 15:49:23 -0800752 /* We wait for the other thread that is mastering the resource */
753 if (tmpres->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
754 __dlm_wait_on_lockres(tmpres);
755 BUG_ON(tmpres->owner == DLM_LOCK_RES_OWNER_UNKNOWN);
756 }
757
Kurt Hackelba2bf212006-12-01 14:47:20 -0800758 if (tmpres->owner == dlm->node_num) {
759 BUG_ON(tmpres->state & DLM_LOCK_RES_DROPPING_REF);
760 dlm_lockres_grab_inflight_ref(dlm, tmpres);
761 } else if (tmpres->state & DLM_LOCK_RES_DROPPING_REF)
762 dropping_ref = 1;
763 spin_unlock(&tmpres->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800764
765 /* wait until done messaging the master, drop our ref to allow
766 * the lockres to be purged, start over. */
767 if (dropping_ref) {
768 spin_lock(&tmpres->spinlock);
769 __dlm_wait_on_lockres_flags(tmpres, DLM_LOCK_RES_DROPPING_REF);
770 spin_unlock(&tmpres->spinlock);
771 dlm_lockres_put(tmpres);
772 tmpres = NULL;
773 goto lookup;
774 }
775
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800776 mlog(0, "found in hash!\n");
777 if (res)
778 dlm_lockres_put(res);
779 res = tmpres;
780 goto leave;
781 }
782
783 if (!res) {
784 spin_unlock(&dlm->spinlock);
785 mlog(0, "allocating a new resource\n");
786 /* nothing found and we need to allocate one. */
787 alloc_mle = (struct dlm_master_list_entry *)
Kurt Hackelad8100e2006-05-01 14:25:21 -0700788 kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800789 if (!alloc_mle)
790 goto leave;
791 res = dlm_new_lockres(dlm, lockid, namelen);
792 if (!res)
793 goto leave;
794 goto lookup;
795 }
796
797 mlog(0, "no lockres found, allocated our own: %p\n", res);
798
799 if (flags & LKM_LOCAL) {
800 /* caller knows it's safe to assume it's not mastered elsewhere
801 * DONE! return right away */
802 spin_lock(&res->spinlock);
803 dlm_change_lockres_owner(dlm, res, dlm->node_num);
804 __dlm_insert_lockres(dlm, res);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800805 dlm_lockres_grab_inflight_ref(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800806 spin_unlock(&res->spinlock);
807 spin_unlock(&dlm->spinlock);
808 /* lockres still marked IN_PROGRESS */
809 goto wake_waiters;
810 }
811
812 /* check master list to see if another node has started mastering it */
813 spin_lock(&dlm->master_lock);
814
815 /* if we found a block, wait for lock to be mastered by another node */
816 blocked = dlm_find_mle(dlm, &mle, (char *)lockid, namelen);
817 if (blocked) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800818 int mig;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800819 if (mle->type == DLM_MLE_MASTER) {
820 mlog(ML_ERROR, "master entry for nonexistent lock!\n");
821 BUG();
Kurt Hackelba2bf212006-12-01 14:47:20 -0800822 }
823 mig = (mle->type == DLM_MLE_MIGRATION);
824 /* if there is a migration in progress, let the migration
825 * finish before continuing. we can wait for the absence
826 * of the MIGRATION mle: either the migrate finished or
827 * one of the nodes died and the mle was cleaned up.
828 * if there is a BLOCK here, but it already has a master
829 * set, we are too late. the master does not have a ref
830 * for us in the refmap. detach the mle and drop it.
831 * either way, go back to the top and start over. */
832 if (mig || mle->master != O2NM_MAX_NODES) {
833 BUG_ON(mig && mle->master == dlm->node_num);
834 /* we arrived too late. the master does not
835 * have a ref for us. retry. */
836 mlog(0, "%s:%.*s: late on %s\n",
837 dlm->name, namelen, lockid,
838 mig ? "MIGRATION" : "BLOCK");
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800839 spin_unlock(&dlm->master_lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800840 spin_unlock(&dlm->spinlock);
841
842 /* master is known, detach */
Kurt Hackelba2bf212006-12-01 14:47:20 -0800843 if (!mig)
844 dlm_mle_detach_hb_events(dlm, mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800845 dlm_put_mle(mle);
846 mle = NULL;
Kurt Hackelba2bf212006-12-01 14:47:20 -0800847 /* this is lame, but we cant wait on either
848 * the mle or lockres waitqueue here */
849 if (mig)
850 msleep(100);
851 goto lookup;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800852 }
853 } else {
854 /* go ahead and try to master lock on this node */
855 mle = alloc_mle;
856 /* make sure this does not get freed below */
857 alloc_mle = NULL;
858 dlm_init_mle(mle, DLM_MLE_MASTER, dlm, res, NULL, 0);
859 set_bit(dlm->node_num, mle->maybe_map);
Sunil Mushran1c084572009-02-26 15:00:37 -0800860 __dlm_insert_mle(dlm, mle);
Kurt Hackelc03872f2006-03-06 14:08:49 -0800861
862 /* still holding the dlm spinlock, check the recovery map
863 * to see if there are any nodes that still need to be
864 * considered. these will not appear in the mle nodemap
865 * but they might own this lockres. wait on them. */
866 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
867 if (bit < O2NM_MAX_NODES) {
Joe Perches27592362007-11-19 17:53:34 -0800868 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to "
Kurt Hackelc03872f2006-03-06 14:08:49 -0800869 "recover before lock mastery can begin\n",
870 dlm->name, namelen, (char *)lockid, bit);
871 wait_on_recovery = 1;
872 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800873 }
874
875 /* at this point there is either a DLM_MLE_BLOCK or a
876 * DLM_MLE_MASTER on the master list, so it's safe to add the
877 * lockres to the hashtable. anyone who finds the lock will
878 * still have to wait on the IN_PROGRESS. */
879
880 /* finally add the lockres to its hash bucket */
881 __dlm_insert_lockres(dlm, res);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800882 /* since this lockres is new it doesnt not require the spinlock */
883 dlm_lockres_grab_inflight_ref_new(dlm, res);
884
885 /* if this node does not become the master make sure to drop
886 * this inflight reference below */
887 drop_inflight_if_nonlocal = 1;
888
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800889 /* get an extra ref on the mle in case this is a BLOCK
890 * if so, the creator of the BLOCK may try to put the last
891 * ref at this time in the assert master handler, so we
892 * need an extra one to keep from a bad ptr deref. */
Kurt Hackela2bf0472006-04-27 18:51:26 -0700893 dlm_get_mle_inuse(mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800894 spin_unlock(&dlm->master_lock);
895 spin_unlock(&dlm->spinlock);
896
Kurt Hackele7e69eb2006-05-01 11:49:52 -0700897redo_request:
Kurt Hackelc03872f2006-03-06 14:08:49 -0800898 while (wait_on_recovery) {
899 /* any cluster changes that occurred after dropping the
900 * dlm spinlock would be detectable be a change on the mle,
901 * so we only need to clear out the recovery map once. */
902 if (dlm_is_recovery_lock(lockid, namelen)) {
903 mlog(ML_NOTICE, "%s: recovery map is not empty, but "
904 "must master $RECOVERY lock now\n", dlm->name);
905 if (!dlm_pre_master_reco_lockres(dlm, res))
906 wait_on_recovery = 0;
907 else {
908 mlog(0, "%s: waiting 500ms for heartbeat state "
909 "change\n", dlm->name);
910 msleep(500);
911 }
912 continue;
913 }
914
915 dlm_kick_recovery_thread(dlm);
Kurt Hackelaa087b82006-05-01 12:02:07 -0700916 msleep(1000);
Kurt Hackelc03872f2006-03-06 14:08:49 -0800917 dlm_wait_for_recovery(dlm);
918
919 spin_lock(&dlm->spinlock);
920 bit = find_next_bit(dlm->recovery_map, O2NM_MAX_NODES, 0);
921 if (bit < O2NM_MAX_NODES) {
Joe Perches27592362007-11-19 17:53:34 -0800922 mlog(ML_NOTICE, "%s:%.*s: at least one node (%d) to "
Kurt Hackelc03872f2006-03-06 14:08:49 -0800923 "recover before lock mastery can begin\n",
924 dlm->name, namelen, (char *)lockid, bit);
925 wait_on_recovery = 1;
926 } else
927 wait_on_recovery = 0;
928 spin_unlock(&dlm->spinlock);
Kurt Hackelb7084ab2006-05-01 13:54:07 -0700929
930 if (wait_on_recovery)
931 dlm_wait_for_node_recovery(dlm, bit, 10000);
Kurt Hackelc03872f2006-03-06 14:08:49 -0800932 }
933
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800934 /* must wait for lock to be mastered elsewhere */
935 if (blocked)
936 goto wait;
937
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800938 ret = -EINVAL;
939 dlm_node_iter_init(mle->vote_map, &iter);
940 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
Kurt Hackelba2bf212006-12-01 14:47:20 -0800941 ret = dlm_do_master_request(res, mle, nodenum);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800942 if (ret < 0)
943 mlog_errno(ret);
944 if (mle->master != O2NM_MAX_NODES) {
945 /* found a master ! */
Kurt Hackel9c6510a2006-03-02 18:09:26 -0800946 if (mle->master <= nodenum)
947 break;
948 /* if our master request has not reached the master
949 * yet, keep going until it does. this is how the
950 * master will know that asserts are needed back to
951 * the lower nodes. */
952 mlog(0, "%s:%.*s: requests only up to %u but master "
953 "is %u, keep going\n", dlm->name, namelen,
954 lockid, nodenum, mle->master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800955 }
956 }
957
958wait:
959 /* keep going until the response map includes all nodes */
960 ret = dlm_wait_for_lock_mastery(dlm, res, mle, &blocked);
961 if (ret < 0) {
Kurt Hackele7e69eb2006-05-01 11:49:52 -0700962 wait_on_recovery = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800963 mlog(0, "%s:%.*s: node map changed, redo the "
964 "master request now, blocked=%d\n",
965 dlm->name, res->lockname.len,
966 res->lockname.name, blocked);
967 if (++tries > 20) {
968 mlog(ML_ERROR, "%s:%.*s: spinning on "
969 "dlm_wait_for_lock_mastery, blocked=%d\n",
970 dlm->name, res->lockname.len,
971 res->lockname.name, blocked);
972 dlm_print_one_lock_resource(res);
Mark Fasheh8a9343f2006-05-01 14:55:10 -0700973 dlm_print_one_mle(mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800974 tries = 0;
975 }
976 goto redo_request;
977 }
978
979 mlog(0, "lockres mastered by %u\n", res->owner);
980 /* make sure we never continue without this */
981 BUG_ON(res->owner == O2NM_MAX_NODES);
982
983 /* master is known, detach if not already detached */
984 dlm_mle_detach_hb_events(dlm, mle);
985 dlm_put_mle(mle);
986 /* put the extra ref */
Kurt Hackela2bf0472006-04-27 18:51:26 -0700987 dlm_put_mle_inuse(mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800988
989wake_waiters:
990 spin_lock(&res->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -0800991 if (res->owner != dlm->node_num && drop_inflight_if_nonlocal)
992 dlm_lockres_drop_inflight_ref(dlm, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -0800993 res->state &= ~DLM_LOCK_RES_IN_PROGRESS;
994 spin_unlock(&res->spinlock);
995 wake_up(&res->wq);
996
997leave:
998 /* need to free the unused mle */
999 if (alloc_mle)
1000 kmem_cache_free(dlm_mle_cache, alloc_mle);
1001
1002 return res;
1003}
1004
1005
1006#define DLM_MASTERY_TIMEOUT_MS 5000
1007
1008static int dlm_wait_for_lock_mastery(struct dlm_ctxt *dlm,
1009 struct dlm_lock_resource *res,
1010 struct dlm_master_list_entry *mle,
1011 int *blocked)
1012{
1013 u8 m;
1014 int ret, bit;
1015 int map_changed, voting_done;
1016 int assert, sleep;
1017
1018recheck:
1019 ret = 0;
1020 assert = 0;
1021
1022 /* check if another node has already become the owner */
1023 spin_lock(&res->spinlock);
1024 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001025 mlog(0, "%s:%.*s: owner is suddenly %u\n", dlm->name,
1026 res->lockname.len, res->lockname.name, res->owner);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001027 spin_unlock(&res->spinlock);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001028 /* this will cause the master to re-assert across
1029 * the whole cluster, freeing up mles */
Kurt Hackel588e0092006-05-01 11:22:06 -07001030 if (res->owner != dlm->node_num) {
Kurt Hackelba2bf212006-12-01 14:47:20 -08001031 ret = dlm_do_master_request(res, mle, res->owner);
Kurt Hackel588e0092006-05-01 11:22:06 -07001032 if (ret < 0) {
1033 /* give recovery a chance to run */
1034 mlog(ML_ERROR, "link to %u went down?: %d\n", res->owner, ret);
1035 msleep(500);
1036 goto recheck;
1037 }
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001038 }
1039 ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001040 goto leave;
1041 }
1042 spin_unlock(&res->spinlock);
1043
1044 spin_lock(&mle->spinlock);
1045 m = mle->master;
1046 map_changed = (memcmp(mle->vote_map, mle->node_map,
1047 sizeof(mle->vote_map)) != 0);
1048 voting_done = (memcmp(mle->vote_map, mle->response_map,
1049 sizeof(mle->vote_map)) == 0);
1050
1051 /* restart if we hit any errors */
1052 if (map_changed) {
1053 int b;
1054 mlog(0, "%s: %.*s: node map changed, restarting\n",
1055 dlm->name, res->lockname.len, res->lockname.name);
1056 ret = dlm_restart_lock_mastery(dlm, res, mle, *blocked);
1057 b = (mle->type == DLM_MLE_BLOCK);
1058 if ((*blocked && !b) || (!*blocked && b)) {
1059 mlog(0, "%s:%.*s: status change: old=%d new=%d\n",
1060 dlm->name, res->lockname.len, res->lockname.name,
1061 *blocked, b);
1062 *blocked = b;
1063 }
1064 spin_unlock(&mle->spinlock);
1065 if (ret < 0) {
1066 mlog_errno(ret);
1067 goto leave;
1068 }
1069 mlog(0, "%s:%.*s: restart lock mastery succeeded, "
1070 "rechecking now\n", dlm->name, res->lockname.len,
1071 res->lockname.name);
1072 goto recheck;
Kurt Hackelaa852352006-04-27 19:04:49 -07001073 } else {
1074 if (!voting_done) {
1075 mlog(0, "map not changed and voting not done "
1076 "for %s:%.*s\n", dlm->name, res->lockname.len,
1077 res->lockname.name);
1078 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001079 }
1080
1081 if (m != O2NM_MAX_NODES) {
1082 /* another node has done an assert!
1083 * all done! */
1084 sleep = 0;
1085 } else {
1086 sleep = 1;
1087 /* have all nodes responded? */
1088 if (voting_done && !*blocked) {
1089 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
1090 if (dlm->node_num <= bit) {
1091 /* my node number is lowest.
1092 * now tell other nodes that I am
1093 * mastering this. */
1094 mle->master = dlm->node_num;
Kurt Hackelba2bf212006-12-01 14:47:20 -08001095 /* ref was grabbed in get_lock_resource
1096 * will be dropped in dlmlock_master */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001097 assert = 1;
1098 sleep = 0;
1099 }
1100 /* if voting is done, but we have not received
1101 * an assert master yet, we must sleep */
1102 }
1103 }
1104
1105 spin_unlock(&mle->spinlock);
1106
1107 /* sleep if we haven't finished voting yet */
1108 if (sleep) {
1109 unsigned long timeo = msecs_to_jiffies(DLM_MASTERY_TIMEOUT_MS);
1110
1111 /*
1112 if (atomic_read(&mle->mle_refs.refcount) < 2)
1113 mlog(ML_ERROR, "mle (%p) refs=%d, name=%.*s\n", mle,
1114 atomic_read(&mle->mle_refs.refcount),
1115 res->lockname.len, res->lockname.name);
1116 */
1117 atomic_set(&mle->woken, 0);
1118 (void)wait_event_timeout(mle->wq,
1119 (atomic_read(&mle->woken) == 1),
1120 timeo);
1121 if (res->owner == O2NM_MAX_NODES) {
Kurt Hackelba2bf212006-12-01 14:47:20 -08001122 mlog(0, "%s:%.*s: waiting again\n", dlm->name,
1123 res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001124 goto recheck;
1125 }
1126 mlog(0, "done waiting, master is %u\n", res->owner);
1127 ret = 0;
1128 goto leave;
1129 }
1130
1131 ret = 0; /* done */
1132 if (assert) {
1133 m = dlm->node_num;
1134 mlog(0, "about to master %.*s here, this=%u\n",
1135 res->lockname.len, res->lockname.name, m);
Kurt Hackelba2bf212006-12-01 14:47:20 -08001136 ret = dlm_do_assert_master(dlm, res, mle->vote_map, 0);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001137 if (ret) {
1138 /* This is a failure in the network path,
1139 * not in the response to the assert_master
1140 * (any nonzero response is a BUG on this node).
1141 * Most likely a socket just got disconnected
1142 * due to node death. */
1143 mlog_errno(ret);
1144 }
1145 /* no longer need to restart lock mastery.
1146 * all living nodes have been contacted. */
1147 ret = 0;
1148 }
1149
1150 /* set the lockres owner */
1151 spin_lock(&res->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -08001152 /* mastery reference obtained either during
1153 * assert_master_handler or in get_lock_resource */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001154 dlm_change_lockres_owner(dlm, res, m);
1155 spin_unlock(&res->spinlock);
1156
1157leave:
1158 return ret;
1159}
1160
1161struct dlm_bitmap_diff_iter
1162{
1163 int curnode;
1164 unsigned long *orig_bm;
1165 unsigned long *cur_bm;
1166 unsigned long diff_bm[BITS_TO_LONGS(O2NM_MAX_NODES)];
1167};
1168
1169enum dlm_node_state_change
1170{
1171 NODE_DOWN = -1,
1172 NODE_NO_CHANGE = 0,
1173 NODE_UP
1174};
1175
1176static void dlm_bitmap_diff_iter_init(struct dlm_bitmap_diff_iter *iter,
1177 unsigned long *orig_bm,
1178 unsigned long *cur_bm)
1179{
1180 unsigned long p1, p2;
1181 int i;
1182
1183 iter->curnode = -1;
1184 iter->orig_bm = orig_bm;
1185 iter->cur_bm = cur_bm;
1186
1187 for (i = 0; i < BITS_TO_LONGS(O2NM_MAX_NODES); i++) {
1188 p1 = *(iter->orig_bm + i);
1189 p2 = *(iter->cur_bm + i);
1190 iter->diff_bm[i] = (p1 & ~p2) | (p2 & ~p1);
1191 }
1192}
1193
1194static int dlm_bitmap_diff_iter_next(struct dlm_bitmap_diff_iter *iter,
1195 enum dlm_node_state_change *state)
1196{
1197 int bit;
1198
1199 if (iter->curnode >= O2NM_MAX_NODES)
1200 return -ENOENT;
1201
1202 bit = find_next_bit(iter->diff_bm, O2NM_MAX_NODES,
1203 iter->curnode+1);
1204 if (bit >= O2NM_MAX_NODES) {
1205 iter->curnode = O2NM_MAX_NODES;
1206 return -ENOENT;
1207 }
1208
1209 /* if it was there in the original then this node died */
1210 if (test_bit(bit, iter->orig_bm))
1211 *state = NODE_DOWN;
1212 else
1213 *state = NODE_UP;
1214
1215 iter->curnode = bit;
1216 return bit;
1217}
1218
1219
1220static int dlm_restart_lock_mastery(struct dlm_ctxt *dlm,
1221 struct dlm_lock_resource *res,
1222 struct dlm_master_list_entry *mle,
1223 int blocked)
1224{
1225 struct dlm_bitmap_diff_iter bdi;
1226 enum dlm_node_state_change sc;
1227 int node;
1228 int ret = 0;
1229
1230 mlog(0, "something happened such that the "
1231 "master process may need to be restarted!\n");
1232
1233 assert_spin_locked(&mle->spinlock);
1234
1235 dlm_bitmap_diff_iter_init(&bdi, mle->vote_map, mle->node_map);
1236 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1237 while (node >= 0) {
1238 if (sc == NODE_UP) {
Kurt Hackele2faea42006-01-12 14:24:55 -08001239 /* a node came up. clear any old vote from
1240 * the response map and set it in the vote map
1241 * then restart the mastery. */
1242 mlog(ML_NOTICE, "node %d up while restarting\n", node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001243
1244 /* redo the master request, but only for the new node */
1245 mlog(0, "sending request to new node\n");
1246 clear_bit(node, mle->response_map);
1247 set_bit(node, mle->vote_map);
1248 } else {
1249 mlog(ML_ERROR, "node down! %d\n", node);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001250 if (blocked) {
1251 int lowest = find_next_bit(mle->maybe_map,
1252 O2NM_MAX_NODES, 0);
1253
1254 /* act like it was never there */
1255 clear_bit(node, mle->maybe_map);
1256
Kurt Hackele7e69eb2006-05-01 11:49:52 -07001257 if (node == lowest) {
1258 mlog(0, "expected master %u died"
1259 " while this node was blocked "
1260 "waiting on it!\n", node);
1261 lowest = find_next_bit(mle->maybe_map,
1262 O2NM_MAX_NODES,
1263 lowest+1);
1264 if (lowest < O2NM_MAX_NODES) {
1265 mlog(0, "%s:%.*s:still "
1266 "blocked. waiting on %u "
1267 "now\n", dlm->name,
1268 res->lockname.len,
1269 res->lockname.name,
1270 lowest);
1271 } else {
1272 /* mle is an MLE_BLOCK, but
1273 * there is now nothing left to
1274 * block on. we need to return
1275 * all the way back out and try
1276 * again with an MLE_MASTER.
1277 * dlm_do_local_recovery_cleanup
1278 * has already run, so the mle
1279 * refcount is ok */
1280 mlog(0, "%s:%.*s: no "
1281 "longer blocking. try to "
1282 "master this here\n",
1283 dlm->name,
1284 res->lockname.len,
1285 res->lockname.name);
1286 mle->type = DLM_MLE_MASTER;
1287 mle->u.res = res;
1288 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001289 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001290 }
1291
Kurt Hackele7e69eb2006-05-01 11:49:52 -07001292 /* now blank out everything, as if we had never
1293 * contacted anyone */
1294 memset(mle->maybe_map, 0, sizeof(mle->maybe_map));
1295 memset(mle->response_map, 0, sizeof(mle->response_map));
1296 /* reset the vote_map to the current node_map */
1297 memcpy(mle->vote_map, mle->node_map,
1298 sizeof(mle->node_map));
1299 /* put myself into the maybe map */
1300 if (mle->type != DLM_MLE_BLOCK)
1301 set_bit(dlm->node_num, mle->maybe_map);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001302 }
1303 ret = -EAGAIN;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001304 node = dlm_bitmap_diff_iter_next(&bdi, &sc);
1305 }
1306 return ret;
1307}
1308
1309
1310/*
1311 * DLM_MASTER_REQUEST_MSG
1312 *
1313 * returns: 0 on success,
1314 * -errno on a network error
1315 *
1316 * on error, the caller should assume the target node is "dead"
1317 *
1318 */
1319
Kurt Hackelba2bf212006-12-01 14:47:20 -08001320static int dlm_do_master_request(struct dlm_lock_resource *res,
1321 struct dlm_master_list_entry *mle, int to)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001322{
1323 struct dlm_ctxt *dlm = mle->dlm;
1324 struct dlm_master_request request;
1325 int ret, response=0, resend;
1326
1327 memset(&request, 0, sizeof(request));
1328 request.node_idx = dlm->node_num;
1329
1330 BUG_ON(mle->type == DLM_MLE_MIGRATION);
1331
1332 if (mle->type != DLM_MLE_MASTER) {
1333 request.namelen = mle->u.name.len;
1334 memcpy(request.name, mle->u.name.name, request.namelen);
1335 } else {
1336 request.namelen = mle->u.res->lockname.len;
1337 memcpy(request.name, mle->u.res->lockname.name,
1338 request.namelen);
1339 }
1340
1341again:
1342 ret = o2net_send_message(DLM_MASTER_REQUEST_MSG, dlm->key, &request,
1343 sizeof(request), to, &response);
1344 if (ret < 0) {
1345 if (ret == -ESRCH) {
1346 /* should never happen */
1347 mlog(ML_ERROR, "TCP stack not ready!\n");
1348 BUG();
1349 } else if (ret == -EINVAL) {
1350 mlog(ML_ERROR, "bad args passed to o2net!\n");
1351 BUG();
1352 } else if (ret == -ENOMEM) {
1353 mlog(ML_ERROR, "out of memory while trying to send "
1354 "network message! retrying\n");
1355 /* this is totally crude */
1356 msleep(50);
1357 goto again;
1358 } else if (!dlm_is_host_down(ret)) {
1359 /* not a network error. bad. */
1360 mlog_errno(ret);
1361 mlog(ML_ERROR, "unhandled error!");
1362 BUG();
1363 }
1364 /* all other errors should be network errors,
1365 * and likely indicate node death */
1366 mlog(ML_ERROR, "link to %d went down!\n", to);
1367 goto out;
1368 }
1369
1370 ret = 0;
1371 resend = 0;
1372 spin_lock(&mle->spinlock);
1373 switch (response) {
1374 case DLM_MASTER_RESP_YES:
1375 set_bit(to, mle->response_map);
1376 mlog(0, "node %u is the master, response=YES\n", to);
Kurt Hackelba2bf212006-12-01 14:47:20 -08001377 mlog(0, "%s:%.*s: master node %u now knows I have a "
1378 "reference\n", dlm->name, res->lockname.len,
1379 res->lockname.name, to);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001380 mle->master = to;
1381 break;
1382 case DLM_MASTER_RESP_NO:
1383 mlog(0, "node %u not master, response=NO\n", to);
1384 set_bit(to, mle->response_map);
1385 break;
1386 case DLM_MASTER_RESP_MAYBE:
1387 mlog(0, "node %u not master, response=MAYBE\n", to);
1388 set_bit(to, mle->response_map);
1389 set_bit(to, mle->maybe_map);
1390 break;
1391 case DLM_MASTER_RESP_ERROR:
1392 mlog(0, "node %u hit an error, resending\n", to);
1393 resend = 1;
1394 response = 0;
1395 break;
1396 default:
1397 mlog(ML_ERROR, "bad response! %u\n", response);
1398 BUG();
1399 }
1400 spin_unlock(&mle->spinlock);
1401 if (resend) {
1402 /* this is also totally crude */
1403 msleep(50);
1404 goto again;
1405 }
1406
1407out:
1408 return ret;
1409}
1410
1411/*
1412 * locks that can be taken here:
1413 * dlm->spinlock
1414 * res->spinlock
1415 * mle->spinlock
1416 * dlm->master_list
1417 *
1418 * if possible, TRIM THIS DOWN!!!
1419 */
Kurt Hackeld74c9802007-01-17 17:04:25 -08001420int dlm_master_request_handler(struct o2net_msg *msg, u32 len, void *data,
1421 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001422{
1423 u8 response = DLM_MASTER_RESP_MAYBE;
1424 struct dlm_ctxt *dlm = data;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001425 struct dlm_lock_resource *res = NULL;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001426 struct dlm_master_request *request = (struct dlm_master_request *) msg->buf;
1427 struct dlm_master_list_entry *mle = NULL, *tmpmle = NULL;
1428 char *name;
Mark Fasheha3d33292006-03-09 17:55:56 -08001429 unsigned int namelen, hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001430 int found, ret;
1431 int set_maybe;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001432 int dispatch_assert = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001433
1434 if (!dlm_grab(dlm))
1435 return DLM_MASTER_RESP_NO;
1436
1437 if (!dlm_domain_fully_joined(dlm)) {
1438 response = DLM_MASTER_RESP_NO;
1439 goto send_response;
1440 }
1441
1442 name = request->name;
1443 namelen = request->namelen;
Mark Fasheha3d33292006-03-09 17:55:56 -08001444 hash = dlm_lockid_hash(name, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001445
1446 if (namelen > DLM_LOCKID_NAME_MAX) {
1447 response = DLM_IVBUFLEN;
1448 goto send_response;
1449 }
1450
1451way_up_top:
1452 spin_lock(&dlm->spinlock);
Mark Fasheha3d33292006-03-09 17:55:56 -08001453 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001454 if (res) {
1455 spin_unlock(&dlm->spinlock);
1456
1457 /* take care of the easy cases up front */
1458 spin_lock(&res->spinlock);
Kurt Hackel1cd04db2007-01-17 14:53:37 -08001459 if (res->state & (DLM_LOCK_RES_RECOVERING|
1460 DLM_LOCK_RES_MIGRATING)) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001461 spin_unlock(&res->spinlock);
1462 mlog(0, "returning DLM_MASTER_RESP_ERROR since res is "
Kurt Hackel1cd04db2007-01-17 14:53:37 -08001463 "being recovered/migrated\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001464 response = DLM_MASTER_RESP_ERROR;
1465 if (mle)
1466 kmem_cache_free(dlm_mle_cache, mle);
1467 goto send_response;
1468 }
1469
1470 if (res->owner == dlm->node_num) {
Kurt Hackelba2bf212006-12-01 14:47:20 -08001471 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1472 dlm->name, namelen, name, request->node_idx);
1473 dlm_lockres_set_refmap_bit(request->node_idx, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001474 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001475 response = DLM_MASTER_RESP_YES;
1476 if (mle)
1477 kmem_cache_free(dlm_mle_cache, mle);
1478
1479 /* this node is the owner.
1480 * there is some extra work that needs to
1481 * happen now. the requesting node has
1482 * caused all nodes up to this one to
1483 * create mles. this node now needs to
1484 * go back and clean those up. */
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001485 dispatch_assert = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001486 goto send_response;
1487 } else if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1488 spin_unlock(&res->spinlock);
1489 // mlog(0, "node %u is the master\n", res->owner);
1490 response = DLM_MASTER_RESP_NO;
1491 if (mle)
1492 kmem_cache_free(dlm_mle_cache, mle);
1493 goto send_response;
1494 }
1495
1496 /* ok, there is no owner. either this node is
1497 * being blocked, or it is actively trying to
1498 * master this lock. */
1499 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1500 mlog(ML_ERROR, "lock with no owner should be "
1501 "in-progress!\n");
1502 BUG();
1503 }
1504
1505 // mlog(0, "lockres is in progress...\n");
1506 spin_lock(&dlm->master_lock);
1507 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1508 if (!found) {
1509 mlog(ML_ERROR, "no mle found for this lock!\n");
1510 BUG();
1511 }
1512 set_maybe = 1;
1513 spin_lock(&tmpmle->spinlock);
1514 if (tmpmle->type == DLM_MLE_BLOCK) {
1515 // mlog(0, "this node is waiting for "
1516 // "lockres to be mastered\n");
1517 response = DLM_MASTER_RESP_NO;
1518 } else if (tmpmle->type == DLM_MLE_MIGRATION) {
1519 mlog(0, "node %u is master, but trying to migrate to "
1520 "node %u.\n", tmpmle->master, tmpmle->new_master);
1521 if (tmpmle->master == dlm->node_num) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001522 mlog(ML_ERROR, "no owner on lockres, but this "
1523 "node is trying to migrate it to %u?!\n",
1524 tmpmle->new_master);
1525 BUG();
1526 } else {
1527 /* the real master can respond on its own */
1528 response = DLM_MASTER_RESP_NO;
1529 }
1530 } else if (tmpmle->master != DLM_LOCK_RES_OWNER_UNKNOWN) {
1531 set_maybe = 0;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001532 if (tmpmle->master == dlm->node_num) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001533 response = DLM_MASTER_RESP_YES;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001534 /* this node will be the owner.
1535 * go back and clean the mles on any
1536 * other nodes */
1537 dispatch_assert = 1;
Kurt Hackelba2bf212006-12-01 14:47:20 -08001538 dlm_lockres_set_refmap_bit(request->node_idx, res);
1539 mlog(0, "%s:%.*s: setting bit %u in refmap\n",
1540 dlm->name, namelen, name,
1541 request->node_idx);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001542 } else
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001543 response = DLM_MASTER_RESP_NO;
1544 } else {
1545 // mlog(0, "this node is attempting to "
1546 // "master lockres\n");
1547 response = DLM_MASTER_RESP_MAYBE;
1548 }
1549 if (set_maybe)
1550 set_bit(request->node_idx, tmpmle->maybe_map);
1551 spin_unlock(&tmpmle->spinlock);
1552
1553 spin_unlock(&dlm->master_lock);
1554 spin_unlock(&res->spinlock);
1555
1556 /* keep the mle attached to heartbeat events */
1557 dlm_put_mle(tmpmle);
1558 if (mle)
1559 kmem_cache_free(dlm_mle_cache, mle);
1560 goto send_response;
1561 }
1562
1563 /*
1564 * lockres doesn't exist on this node
1565 * if there is an MLE_BLOCK, return NO
1566 * if there is an MLE_MASTER, return MAYBE
1567 * otherwise, add an MLE_BLOCK, return NO
1568 */
1569 spin_lock(&dlm->master_lock);
1570 found = dlm_find_mle(dlm, &tmpmle, name, namelen);
1571 if (!found) {
1572 /* this lockid has never been seen on this node yet */
1573 // mlog(0, "no mle found\n");
1574 if (!mle) {
1575 spin_unlock(&dlm->master_lock);
1576 spin_unlock(&dlm->spinlock);
1577
1578 mle = (struct dlm_master_list_entry *)
Kurt Hackelad8100e2006-05-01 14:25:21 -07001579 kmem_cache_alloc(dlm_mle_cache, GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001580 if (!mle) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001581 response = DLM_MASTER_RESP_ERROR;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001582 mlog_errno(-ENOMEM);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001583 goto send_response;
1584 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001585 goto way_up_top;
1586 }
1587
1588 // mlog(0, "this is second time thru, already allocated, "
1589 // "add the block.\n");
Kurt Hackel41b8c8a2006-04-27 19:00:26 -07001590 dlm_init_mle(mle, DLM_MLE_BLOCK, dlm, NULL, name, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001591 set_bit(request->node_idx, mle->maybe_map);
Sunil Mushran1c084572009-02-26 15:00:37 -08001592 __dlm_insert_mle(dlm, mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001593 response = DLM_MASTER_RESP_NO;
1594 } else {
1595 // mlog(0, "mle was found\n");
1596 set_maybe = 1;
1597 spin_lock(&tmpmle->spinlock);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001598 if (tmpmle->master == dlm->node_num) {
1599 mlog(ML_ERROR, "no lockres, but an mle with this node as master!\n");
1600 BUG();
1601 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001602 if (tmpmle->type == DLM_MLE_BLOCK)
1603 response = DLM_MASTER_RESP_NO;
1604 else if (tmpmle->type == DLM_MLE_MIGRATION) {
1605 mlog(0, "migration mle was found (%u->%u)\n",
1606 tmpmle->master, tmpmle->new_master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001607 /* real master can respond on its own */
1608 response = DLM_MASTER_RESP_NO;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001609 } else
1610 response = DLM_MASTER_RESP_MAYBE;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001611 if (set_maybe)
1612 set_bit(request->node_idx, tmpmle->maybe_map);
1613 spin_unlock(&tmpmle->spinlock);
1614 }
1615 spin_unlock(&dlm->master_lock);
1616 spin_unlock(&dlm->spinlock);
1617
1618 if (found) {
1619 /* keep the mle attached to heartbeat events */
1620 dlm_put_mle(tmpmle);
1621 }
1622send_response:
Sunil Mushranb31cfc02008-03-01 14:04:22 -08001623 /*
1624 * __dlm_lookup_lockres() grabbed a reference to this lockres.
1625 * The reference is released by dlm_assert_master_worker() under
1626 * the call to dlm_dispatch_assert_master(). If
1627 * dlm_assert_master_worker() isn't called, we drop it here.
1628 */
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001629 if (dispatch_assert) {
1630 if (response != DLM_MASTER_RESP_YES)
1631 mlog(ML_ERROR, "invalid response %d\n", response);
1632 if (!res) {
1633 mlog(ML_ERROR, "bad lockres while trying to assert!\n");
1634 BUG();
1635 }
1636 mlog(0, "%u is the owner of %.*s, cleaning everyone else\n",
1637 dlm->node_num, res->lockname.len, res->lockname.name);
1638 ret = dlm_dispatch_assert_master(dlm, res, 0, request->node_idx,
1639 DLM_ASSERT_MASTER_MLE_CLEANUP);
1640 if (ret < 0) {
1641 mlog(ML_ERROR, "failed to dispatch assert master work\n");
1642 response = DLM_MASTER_RESP_ERROR;
Sunil Mushranb31cfc02008-03-01 14:04:22 -08001643 dlm_lockres_put(res);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001644 }
Sunil Mushranb31cfc02008-03-01 14:04:22 -08001645 } else {
1646 if (res)
1647 dlm_lockres_put(res);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001648 }
1649
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001650 dlm_put(dlm);
1651 return response;
1652}
1653
1654/*
1655 * DLM_ASSERT_MASTER_MSG
1656 */
1657
1658
1659/*
1660 * NOTE: this can be used for debugging
1661 * can periodically run all locks owned by this node
1662 * and re-assert across the cluster...
1663 */
Adrian Bunk05488bb2008-02-17 10:20:41 +02001664static int dlm_do_assert_master(struct dlm_ctxt *dlm,
1665 struct dlm_lock_resource *res,
1666 void *nodemap, u32 flags)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001667{
1668 struct dlm_assert_master assert;
1669 int to, tmpret;
1670 struct dlm_node_iter iter;
1671 int ret = 0;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001672 int reassert;
Kurt Hackelba2bf212006-12-01 14:47:20 -08001673 const char *lockname = res->lockname.name;
1674 unsigned int namelen = res->lockname.len;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001675
1676 BUG_ON(namelen > O2NM_MAX_NAME_LEN);
Sunil Mushranf3f85462007-01-29 15:19:16 -08001677
1678 spin_lock(&res->spinlock);
1679 res->state |= DLM_LOCK_RES_SETREF_INPROG;
1680 spin_unlock(&res->spinlock);
1681
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001682again:
1683 reassert = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001684
1685 /* note that if this nodemap is empty, it returns 0 */
1686 dlm_node_iter_init(nodemap, &iter);
1687 while ((to = dlm_node_iter_next(&iter)) >= 0) {
1688 int r = 0;
Kurt Hackela9ee4c82006-04-27 19:26:15 -07001689 struct dlm_master_list_entry *mle = NULL;
1690
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001691 mlog(0, "sending assert master to %d (%.*s)\n", to,
1692 namelen, lockname);
1693 memset(&assert, 0, sizeof(assert));
1694 assert.node_idx = dlm->node_num;
1695 assert.namelen = namelen;
1696 memcpy(assert.name, lockname, namelen);
1697 assert.flags = cpu_to_be32(flags);
1698
1699 tmpret = o2net_send_message(DLM_ASSERT_MASTER_MSG, dlm->key,
1700 &assert, sizeof(assert), to, &r);
1701 if (tmpret < 0) {
Kurt Hackel3b3b84a2006-05-01 14:31:37 -07001702 mlog(0, "assert_master returned %d!\n", tmpret);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001703 if (!dlm_is_host_down(tmpret)) {
Kurt Hackel3b3b84a2006-05-01 14:31:37 -07001704 mlog(ML_ERROR, "unhandled error=%d!\n", tmpret);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001705 BUG();
1706 }
1707 /* a node died. finish out the rest of the nodes. */
Kurt Hackel3b3b84a2006-05-01 14:31:37 -07001708 mlog(0, "link to %d went down!\n", to);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001709 /* any nonzero status return will do */
1710 ret = tmpret;
Kurt Hackelba2bf212006-12-01 14:47:20 -08001711 r = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001712 } else if (r < 0) {
1713 /* ok, something horribly messed. kill thyself. */
1714 mlog(ML_ERROR,"during assert master of %.*s to %u, "
1715 "got %d.\n", namelen, lockname, to, r);
Kurt Hackela9ee4c82006-04-27 19:26:15 -07001716 spin_lock(&dlm->spinlock);
1717 spin_lock(&dlm->master_lock);
1718 if (dlm_find_mle(dlm, &mle, (char *)lockname,
1719 namelen)) {
1720 dlm_print_one_mle(mle);
1721 __dlm_put_mle(mle);
1722 }
1723 spin_unlock(&dlm->master_lock);
1724 spin_unlock(&dlm->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001725 BUG();
Kurt Hackelba2bf212006-12-01 14:47:20 -08001726 }
1727
1728 if (r & DLM_ASSERT_RESPONSE_REASSERT &&
1729 !(r & DLM_ASSERT_RESPONSE_MASTERY_REF)) {
1730 mlog(ML_ERROR, "%.*s: very strange, "
1731 "master MLE but no lockres on %u\n",
1732 namelen, lockname, to);
1733 }
1734
1735 if (r & DLM_ASSERT_RESPONSE_REASSERT) {
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001736 mlog(0, "%.*s: node %u create mles on other "
1737 "nodes and requests a re-assert\n",
1738 namelen, lockname, to);
1739 reassert = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001740 }
Kurt Hackelba2bf212006-12-01 14:47:20 -08001741 if (r & DLM_ASSERT_RESPONSE_MASTERY_REF) {
1742 mlog(0, "%.*s: node %u has a reference to this "
1743 "lockres, set the bit in the refmap\n",
1744 namelen, lockname, to);
1745 spin_lock(&res->spinlock);
1746 dlm_lockres_set_refmap_bit(to, res);
1747 spin_unlock(&res->spinlock);
1748 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001749 }
1750
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001751 if (reassert)
1752 goto again;
1753
Sunil Mushranf3f85462007-01-29 15:19:16 -08001754 spin_lock(&res->spinlock);
1755 res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
1756 spin_unlock(&res->spinlock);
1757 wake_up(&res->wq);
1758
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001759 return ret;
1760}
1761
1762/*
1763 * locks that can be taken here:
1764 * dlm->spinlock
1765 * res->spinlock
1766 * mle->spinlock
1767 * dlm->master_list
1768 *
1769 * if possible, TRIM THIS DOWN!!!
1770 */
Kurt Hackeld74c9802007-01-17 17:04:25 -08001771int dlm_assert_master_handler(struct o2net_msg *msg, u32 len, void *data,
1772 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001773{
1774 struct dlm_ctxt *dlm = data;
1775 struct dlm_master_list_entry *mle = NULL;
1776 struct dlm_assert_master *assert = (struct dlm_assert_master *)msg->buf;
1777 struct dlm_lock_resource *res = NULL;
1778 char *name;
Mark Fasheha3d33292006-03-09 17:55:56 -08001779 unsigned int namelen, hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001780 u32 flags;
Kurt Hackelba2bf212006-12-01 14:47:20 -08001781 int master_request = 0, have_lockres_ref = 0;
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001782 int ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001783
1784 if (!dlm_grab(dlm))
1785 return 0;
1786
1787 name = assert->name;
1788 namelen = assert->namelen;
Mark Fasheha3d33292006-03-09 17:55:56 -08001789 hash = dlm_lockid_hash(name, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001790 flags = be32_to_cpu(assert->flags);
1791
1792 if (namelen > DLM_LOCKID_NAME_MAX) {
1793 mlog(ML_ERROR, "Invalid name length!");
1794 goto done;
1795 }
1796
1797 spin_lock(&dlm->spinlock);
1798
1799 if (flags)
1800 mlog(0, "assert_master with flags: %u\n", flags);
1801
1802 /* find the MLE */
1803 spin_lock(&dlm->master_lock);
1804 if (!dlm_find_mle(dlm, &mle, name, namelen)) {
1805 /* not an error, could be master just re-asserting */
1806 mlog(0, "just got an assert_master from %u, but no "
1807 "MLE for it! (%.*s)\n", assert->node_idx,
1808 namelen, name);
1809 } else {
1810 int bit = find_next_bit (mle->maybe_map, O2NM_MAX_NODES, 0);
1811 if (bit >= O2NM_MAX_NODES) {
1812 /* not necessarily an error, though less likely.
1813 * could be master just re-asserting. */
Kurt Hackelaa852352006-04-27 19:04:49 -07001814 mlog(0, "no bits set in the maybe_map, but %u "
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001815 "is asserting! (%.*s)\n", assert->node_idx,
1816 namelen, name);
1817 } else if (bit != assert->node_idx) {
1818 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1819 mlog(0, "master %u was found, %u should "
1820 "back off\n", assert->node_idx, bit);
1821 } else {
1822 /* with the fix for bug 569, a higher node
1823 * number winning the mastery will respond
1824 * YES to mastery requests, but this node
1825 * had no way of knowing. let it pass. */
Kurt Hackelaa852352006-04-27 19:04:49 -07001826 mlog(0, "%u is the lowest node, "
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001827 "%u is asserting. (%.*s) %u must "
1828 "have begun after %u won.\n", bit,
1829 assert->node_idx, namelen, name, bit,
1830 assert->node_idx);
1831 }
1832 }
Kurt Hackel2d1a8682006-04-27 19:01:35 -07001833 if (mle->type == DLM_MLE_MIGRATION) {
1834 if (flags & DLM_ASSERT_MASTER_MLE_CLEANUP) {
1835 mlog(0, "%s:%.*s: got cleanup assert"
1836 " from %u for migration\n",
1837 dlm->name, namelen, name,
1838 assert->node_idx);
1839 } else if (!(flags & DLM_ASSERT_MASTER_FINISH_MIGRATION)) {
1840 mlog(0, "%s:%.*s: got unrelated assert"
1841 " from %u for migration, ignoring\n",
1842 dlm->name, namelen, name,
1843 assert->node_idx);
1844 __dlm_put_mle(mle);
1845 spin_unlock(&dlm->master_lock);
1846 spin_unlock(&dlm->spinlock);
1847 goto done;
1848 }
1849 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001850 }
1851 spin_unlock(&dlm->master_lock);
1852
1853 /* ok everything checks out with the MLE
1854 * now check to see if there is a lockres */
Mark Fasheha3d33292006-03-09 17:55:56 -08001855 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001856 if (res) {
1857 spin_lock(&res->spinlock);
1858 if (res->state & DLM_LOCK_RES_RECOVERING) {
1859 mlog(ML_ERROR, "%u asserting but %.*s is "
1860 "RECOVERING!\n", assert->node_idx, namelen, name);
1861 goto kill;
1862 }
1863 if (!mle) {
Kurt Hackeldc2ed192006-04-27 19:03:18 -07001864 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN &&
1865 res->owner != assert->node_idx) {
Sunil Mushran53ecd252009-02-03 12:37:16 -08001866 mlog(ML_ERROR, "DIE! Mastery assert from %u, "
1867 "but current owner is %u! (%.*s)\n",
1868 assert->node_idx, res->owner, namelen,
1869 name);
1870 __dlm_print_one_lock_resource(res);
1871 BUG();
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001872 }
1873 } else if (mle->type != DLM_MLE_MIGRATION) {
1874 if (res->owner != DLM_LOCK_RES_OWNER_UNKNOWN) {
1875 /* owner is just re-asserting */
1876 if (res->owner == assert->node_idx) {
1877 mlog(0, "owner %u re-asserting on "
1878 "lock %.*s\n", assert->node_idx,
1879 namelen, name);
1880 goto ok;
1881 }
1882 mlog(ML_ERROR, "got assert_master from "
1883 "node %u, but %u is the owner! "
1884 "(%.*s)\n", assert->node_idx,
1885 res->owner, namelen, name);
1886 goto kill;
1887 }
1888 if (!(res->state & DLM_LOCK_RES_IN_PROGRESS)) {
1889 mlog(ML_ERROR, "got assert from %u, but lock "
1890 "with no owner should be "
1891 "in-progress! (%.*s)\n",
1892 assert->node_idx,
1893 namelen, name);
1894 goto kill;
1895 }
1896 } else /* mle->type == DLM_MLE_MIGRATION */ {
1897 /* should only be getting an assert from new master */
1898 if (assert->node_idx != mle->new_master) {
1899 mlog(ML_ERROR, "got assert from %u, but "
1900 "new master is %u, and old master "
1901 "was %u (%.*s)\n",
1902 assert->node_idx, mle->new_master,
1903 mle->master, namelen, name);
1904 goto kill;
1905 }
1906
1907 }
1908ok:
1909 spin_unlock(&res->spinlock);
1910 }
1911 spin_unlock(&dlm->spinlock);
1912
1913 // mlog(0, "woo! got an assert_master from node %u!\n",
1914 // assert->node_idx);
1915 if (mle) {
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001916 int extra_ref = 0;
1917 int nn = -1;
Kurt Hackela2bf0472006-04-27 18:51:26 -07001918 int rr, err = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001919
1920 spin_lock(&mle->spinlock);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08001921 if (mle->type == DLM_MLE_BLOCK || mle->type == DLM_MLE_MIGRATION)
1922 extra_ref = 1;
1923 else {
1924 /* MASTER mle: if any bits set in the response map
1925 * then the calling node needs to re-assert to clear
1926 * up nodes that this node contacted */
1927 while ((nn = find_next_bit (mle->response_map, O2NM_MAX_NODES,
1928 nn+1)) < O2NM_MAX_NODES) {
1929 if (nn != dlm->node_num && nn != assert->node_idx)
1930 master_request = 1;
1931 }
1932 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001933 mle->master = assert->node_idx;
1934 atomic_set(&mle->woken, 1);
1935 wake_up(&mle->wq);
1936 spin_unlock(&mle->spinlock);
1937
Kurt Hackela2bf0472006-04-27 18:51:26 -07001938 if (res) {
Kurt Hackela6fa3642007-01-17 14:59:12 -08001939 int wake = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001940 spin_lock(&res->spinlock);
Kurt Hackela2bf0472006-04-27 18:51:26 -07001941 if (mle->type == DLM_MLE_MIGRATION) {
1942 mlog(0, "finishing off migration of lockres %.*s, "
1943 "from %u to %u\n",
1944 res->lockname.len, res->lockname.name,
1945 dlm->node_num, mle->new_master);
1946 res->state &= ~DLM_LOCK_RES_MIGRATING;
Kurt Hackela6fa3642007-01-17 14:59:12 -08001947 wake = 1;
Kurt Hackela2bf0472006-04-27 18:51:26 -07001948 dlm_change_lockres_owner(dlm, res, mle->new_master);
1949 BUG_ON(res->state & DLM_LOCK_RES_DIRTY);
1950 } else {
1951 dlm_change_lockres_owner(dlm, res, mle->master);
1952 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001953 spin_unlock(&res->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -08001954 have_lockres_ref = 1;
Kurt Hackela6fa3642007-01-17 14:59:12 -08001955 if (wake)
1956 wake_up(&res->wq);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001957 }
Kurt Hackela2bf0472006-04-27 18:51:26 -07001958
1959 /* master is known, detach if not already detached.
1960 * ensures that only one assert_master call will happen
1961 * on this mle. */
1962 spin_lock(&dlm->spinlock);
1963 spin_lock(&dlm->master_lock);
1964
1965 rr = atomic_read(&mle->mle_refs.refcount);
1966 if (mle->inuse > 0) {
1967 if (extra_ref && rr < 3)
1968 err = 1;
1969 else if (!extra_ref && rr < 2)
1970 err = 1;
1971 } else {
1972 if (extra_ref && rr < 2)
1973 err = 1;
1974 else if (!extra_ref && rr < 1)
1975 err = 1;
1976 }
1977 if (err) {
1978 mlog(ML_ERROR, "%s:%.*s: got assert master from %u "
1979 "that will mess up this node, refs=%d, extra=%d, "
1980 "inuse=%d\n", dlm->name, namelen, name,
1981 assert->node_idx, rr, extra_ref, mle->inuse);
1982 dlm_print_one_mle(mle);
1983 }
Sunil Mushran1c084572009-02-26 15:00:37 -08001984 __dlm_unlink_mle(dlm, mle);
Kurt Hackela2bf0472006-04-27 18:51:26 -07001985 __dlm_mle_detach_hb_events(dlm, mle);
1986 __dlm_put_mle(mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08001987 if (extra_ref) {
1988 /* the assert master message now balances the extra
1989 * ref given by the master / migration request message.
1990 * if this is the last put, it will be removed
1991 * from the list. */
Kurt Hackela2bf0472006-04-27 18:51:26 -07001992 __dlm_put_mle(mle);
1993 }
1994 spin_unlock(&dlm->master_lock);
1995 spin_unlock(&dlm->spinlock);
1996 } else if (res) {
1997 if (res->owner != assert->node_idx) {
1998 mlog(0, "assert_master from %u, but current "
1999 "owner is %u (%.*s), no mle\n", assert->node_idx,
2000 res->owner, namelen, name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002001 }
2002 }
2003
2004done:
Kurt Hackel9c6510a2006-03-02 18:09:26 -08002005 ret = 0;
Kurt Hackel3b8118c2007-01-17 17:05:53 -08002006 if (res) {
2007 spin_lock(&res->spinlock);
2008 res->state |= DLM_LOCK_RES_SETREF_INPROG;
2009 spin_unlock(&res->spinlock);
2010 *ret_data = (void *)res;
2011 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002012 dlm_put(dlm);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08002013 if (master_request) {
2014 mlog(0, "need to tell master to reassert\n");
Kurt Hackelba2bf212006-12-01 14:47:20 -08002015 /* positive. negative would shoot down the node. */
2016 ret |= DLM_ASSERT_RESPONSE_REASSERT;
2017 if (!have_lockres_ref) {
2018 mlog(ML_ERROR, "strange, got assert from %u, MASTER "
2019 "mle present here for %s:%.*s, but no lockres!\n",
2020 assert->node_idx, dlm->name, namelen, name);
2021 }
2022 }
2023 if (have_lockres_ref) {
2024 /* let the master know we have a reference to the lockres */
2025 ret |= DLM_ASSERT_RESPONSE_MASTERY_REF;
2026 mlog(0, "%s:%.*s: got assert from %u, need a ref\n",
2027 dlm->name, namelen, name, assert->node_idx);
Kurt Hackel9c6510a2006-03-02 18:09:26 -08002028 }
2029 return ret;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002030
2031kill:
2032 /* kill the caller! */
Kurt Hackela9ee4c82006-04-27 19:26:15 -07002033 mlog(ML_ERROR, "Bad message received from another node. Dumping state "
2034 "and killing the other node now! This node is OK and can continue.\n");
2035 __dlm_print_one_lock_resource(res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002036 spin_unlock(&res->spinlock);
2037 spin_unlock(&dlm->spinlock);
Kurt Hackel3b8118c2007-01-17 17:05:53 -08002038 *ret_data = (void *)res;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002039 dlm_put(dlm);
2040 return -EINVAL;
2041}
2042
Kurt Hackel3b8118c2007-01-17 17:05:53 -08002043void dlm_assert_master_post_handler(int status, void *data, void *ret_data)
2044{
2045 struct dlm_lock_resource *res = (struct dlm_lock_resource *)ret_data;
2046
2047 if (ret_data) {
2048 spin_lock(&res->spinlock);
2049 res->state &= ~DLM_LOCK_RES_SETREF_INPROG;
2050 spin_unlock(&res->spinlock);
2051 wake_up(&res->wq);
2052 dlm_lockres_put(res);
2053 }
2054 return;
2055}
2056
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002057int dlm_dispatch_assert_master(struct dlm_ctxt *dlm,
2058 struct dlm_lock_resource *res,
2059 int ignore_higher, u8 request_from, u32 flags)
2060{
2061 struct dlm_work_item *item;
Robert P. J. Daycd861282006-12-13 00:34:52 -08002062 item = kzalloc(sizeof(*item), GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002063 if (!item)
2064 return -ENOMEM;
2065
2066
2067 /* queue up work for dlm_assert_master_worker */
2068 dlm_grab(dlm); /* get an extra ref for the work item */
2069 dlm_init_work_item(dlm, item, dlm_assert_master_worker, NULL);
2070 item->u.am.lockres = res; /* already have a ref */
2071 /* can optionally ignore node numbers higher than this node */
2072 item->u.am.ignore_higher = ignore_higher;
2073 item->u.am.request_from = request_from;
2074 item->u.am.flags = flags;
2075
Kurt Hackel9c6510a2006-03-02 18:09:26 -08002076 if (ignore_higher)
2077 mlog(0, "IGNORE HIGHER: %.*s\n", res->lockname.len,
2078 res->lockname.name);
2079
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002080 spin_lock(&dlm->work_lock);
2081 list_add_tail(&item->list, &dlm->work_list);
2082 spin_unlock(&dlm->work_lock);
2083
Kurt Hackel3156d262006-05-01 14:39:29 -07002084 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002085 return 0;
2086}
2087
2088static void dlm_assert_master_worker(struct dlm_work_item *item, void *data)
2089{
2090 struct dlm_ctxt *dlm = data;
2091 int ret = 0;
2092 struct dlm_lock_resource *res;
2093 unsigned long nodemap[BITS_TO_LONGS(O2NM_MAX_NODES)];
2094 int ignore_higher;
2095 int bit;
2096 u8 request_from;
2097 u32 flags;
2098
2099 dlm = item->dlm;
2100 res = item->u.am.lockres;
2101 ignore_higher = item->u.am.ignore_higher;
2102 request_from = item->u.am.request_from;
2103 flags = item->u.am.flags;
2104
2105 spin_lock(&dlm->spinlock);
2106 memcpy(nodemap, dlm->domain_map, sizeof(nodemap));
2107 spin_unlock(&dlm->spinlock);
2108
2109 clear_bit(dlm->node_num, nodemap);
2110 if (ignore_higher) {
2111 /* if is this just to clear up mles for nodes below
2112 * this node, do not send the message to the original
2113 * caller or any node number higher than this */
2114 clear_bit(request_from, nodemap);
2115 bit = dlm->node_num;
2116 while (1) {
2117 bit = find_next_bit(nodemap, O2NM_MAX_NODES,
2118 bit+1);
2119 if (bit >= O2NM_MAX_NODES)
2120 break;
2121 clear_bit(bit, nodemap);
2122 }
2123 }
2124
Kurt Hackel36407482006-05-01 13:32:27 -07002125 /*
2126 * If we're migrating this lock to someone else, we are no
2127 * longer allowed to assert out own mastery. OTOH, we need to
2128 * prevent migration from starting while we're still asserting
2129 * our dominance. The reserved ast delays migration.
2130 */
2131 spin_lock(&res->spinlock);
2132 if (res->state & DLM_LOCK_RES_MIGRATING) {
2133 mlog(0, "Someone asked us to assert mastery, but we're "
2134 "in the middle of migration. Skipping assert, "
2135 "the new master will handle that.\n");
2136 spin_unlock(&res->spinlock);
2137 goto put;
2138 } else
2139 __dlm_lockres_reserve_ast(res);
2140 spin_unlock(&res->spinlock);
2141
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002142 /* this call now finishes out the nodemap
2143 * even if one or more nodes die */
2144 mlog(0, "worker about to master %.*s here, this=%u\n",
2145 res->lockname.len, res->lockname.name, dlm->node_num);
Kurt Hackelba2bf212006-12-01 14:47:20 -08002146 ret = dlm_do_assert_master(dlm, res, nodemap, flags);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002147 if (ret < 0) {
2148 /* no need to restart, we are done */
Kurt Hackel3b3b84a2006-05-01 14:31:37 -07002149 if (!dlm_is_host_down(ret))
2150 mlog_errno(ret);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002151 }
2152
Kurt Hackel36407482006-05-01 13:32:27 -07002153 /* Ok, we've asserted ourselves. Let's let migration start. */
2154 dlm_lockres_release_ast(dlm, res);
2155
2156put:
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002157 dlm_lockres_put(res);
2158
2159 mlog(0, "finished with dlm_assert_master_worker\n");
2160}
2161
Kurt Hackelc03872f2006-03-06 14:08:49 -08002162/* SPECIAL CASE for the $RECOVERY lock used by the recovery thread.
2163 * We cannot wait for node recovery to complete to begin mastering this
2164 * lockres because this lockres is used to kick off recovery! ;-)
2165 * So, do a pre-check on all living nodes to see if any of those nodes
2166 * think that $RECOVERY is currently mastered by a dead node. If so,
2167 * we wait a short time to allow that node to get notified by its own
2168 * heartbeat stack, then check again. All $RECOVERY lock resources
2169 * mastered by dead nodes are purged when the hearbeat callback is
2170 * fired, so we can know for sure that it is safe to continue once
2171 * the node returns a live node or no node. */
2172static int dlm_pre_master_reco_lockres(struct dlm_ctxt *dlm,
2173 struct dlm_lock_resource *res)
2174{
2175 struct dlm_node_iter iter;
2176 int nodenum;
2177 int ret = 0;
2178 u8 master = DLM_LOCK_RES_OWNER_UNKNOWN;
2179
2180 spin_lock(&dlm->spinlock);
2181 dlm_node_iter_init(dlm->domain_map, &iter);
2182 spin_unlock(&dlm->spinlock);
2183
2184 while ((nodenum = dlm_node_iter_next(&iter)) >= 0) {
2185 /* do not send to self */
2186 if (nodenum == dlm->node_num)
2187 continue;
2188 ret = dlm_do_master_requery(dlm, res, nodenum, &master);
2189 if (ret < 0) {
2190 mlog_errno(ret);
2191 if (!dlm_is_host_down(ret))
2192 BUG();
2193 /* host is down, so answer for that node would be
2194 * DLM_LOCK_RES_OWNER_UNKNOWN. continue. */
Kurt Hackelf42a1002006-05-01 11:53:33 -07002195 ret = 0;
Kurt Hackelc03872f2006-03-06 14:08:49 -08002196 }
2197
2198 if (master != DLM_LOCK_RES_OWNER_UNKNOWN) {
2199 /* check to see if this master is in the recovery map */
2200 spin_lock(&dlm->spinlock);
2201 if (test_bit(master, dlm->recovery_map)) {
2202 mlog(ML_NOTICE, "%s: node %u has not seen "
2203 "node %u go down yet, and thinks the "
2204 "dead node is mastering the recovery "
2205 "lock. must wait.\n", dlm->name,
2206 nodenum, master);
2207 ret = -EAGAIN;
2208 }
2209 spin_unlock(&dlm->spinlock);
2210 mlog(0, "%s: reco lock master is %u\n", dlm->name,
2211 master);
2212 break;
2213 }
2214 }
2215 return ret;
2216}
2217
Kurt Hackelba2bf212006-12-01 14:47:20 -08002218/*
2219 * DLM_DEREF_LOCKRES_MSG
2220 */
2221
2222int dlm_drop_lockres_ref(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2223{
2224 struct dlm_deref_lockres deref;
2225 int ret = 0, r;
2226 const char *lockname;
2227 unsigned int namelen;
2228
2229 lockname = res->lockname.name;
2230 namelen = res->lockname.len;
2231 BUG_ON(namelen > O2NM_MAX_NAME_LEN);
2232
2233 mlog(0, "%s:%.*s: sending deref to %d\n",
2234 dlm->name, namelen, lockname, res->owner);
2235 memset(&deref, 0, sizeof(deref));
2236 deref.node_idx = dlm->node_num;
2237 deref.namelen = namelen;
2238 memcpy(deref.name, lockname, namelen);
2239
2240 ret = o2net_send_message(DLM_DEREF_LOCKRES_MSG, dlm->key,
2241 &deref, sizeof(deref), res->owner, &r);
2242 if (ret < 0)
2243 mlog_errno(ret);
2244 else if (r < 0) {
2245 /* BAD. other node says I did not have a ref. */
2246 mlog(ML_ERROR,"while dropping ref on %s:%.*s "
2247 "(master=%u) got %d.\n", dlm->name, namelen,
2248 lockname, res->owner, r);
2249 dlm_print_one_lock_resource(res);
2250 BUG();
2251 }
2252 return ret;
2253}
2254
Kurt Hackeld74c9802007-01-17 17:04:25 -08002255int dlm_deref_lockres_handler(struct o2net_msg *msg, u32 len, void *data,
2256 void **ret_data)
Kurt Hackelba2bf212006-12-01 14:47:20 -08002257{
2258 struct dlm_ctxt *dlm = data;
2259 struct dlm_deref_lockres *deref = (struct dlm_deref_lockres *)msg->buf;
2260 struct dlm_lock_resource *res = NULL;
2261 char *name;
2262 unsigned int namelen;
2263 int ret = -EINVAL;
2264 u8 node;
2265 unsigned int hash;
Sunil Mushranf3f85462007-01-29 15:19:16 -08002266 struct dlm_work_item *item;
2267 int cleared = 0;
2268 int dispatch = 0;
Kurt Hackelba2bf212006-12-01 14:47:20 -08002269
2270 if (!dlm_grab(dlm))
2271 return 0;
2272
2273 name = deref->name;
2274 namelen = deref->namelen;
2275 node = deref->node_idx;
2276
2277 if (namelen > DLM_LOCKID_NAME_MAX) {
2278 mlog(ML_ERROR, "Invalid name length!");
2279 goto done;
2280 }
2281 if (deref->node_idx >= O2NM_MAX_NODES) {
2282 mlog(ML_ERROR, "Invalid node number: %u\n", node);
2283 goto done;
2284 }
2285
2286 hash = dlm_lockid_hash(name, namelen);
2287
2288 spin_lock(&dlm->spinlock);
2289 res = __dlm_lookup_lockres_full(dlm, name, namelen, hash);
2290 if (!res) {
2291 spin_unlock(&dlm->spinlock);
2292 mlog(ML_ERROR, "%s:%.*s: bad lockres name\n",
2293 dlm->name, namelen, name);
2294 goto done;
2295 }
2296 spin_unlock(&dlm->spinlock);
2297
2298 spin_lock(&res->spinlock);
Sunil Mushranf3f85462007-01-29 15:19:16 -08002299 if (res->state & DLM_LOCK_RES_SETREF_INPROG)
2300 dispatch = 1;
2301 else {
2302 BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
2303 if (test_bit(node, res->refmap)) {
2304 dlm_lockres_clear_refmap_bit(node, res);
2305 cleared = 1;
2306 }
Kurt Hackelba2bf212006-12-01 14:47:20 -08002307 }
2308 spin_unlock(&res->spinlock);
2309
Sunil Mushranf3f85462007-01-29 15:19:16 -08002310 if (!dispatch) {
2311 if (cleared)
2312 dlm_lockres_calc_usage(dlm, res);
2313 else {
2314 mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
2315 "but it is already dropped!\n", dlm->name,
2316 res->lockname.len, res->lockname.name, node);
Tao Ma2af37ce2008-02-28 10:41:55 +08002317 dlm_print_one_lock_resource(res);
Sunil Mushranf3f85462007-01-29 15:19:16 -08002318 }
2319 ret = 0;
2320 goto done;
2321 }
2322
2323 item = kzalloc(sizeof(*item), GFP_NOFS);
2324 if (!item) {
2325 ret = -ENOMEM;
2326 mlog_errno(ret);
2327 goto done;
2328 }
2329
2330 dlm_init_work_item(dlm, item, dlm_deref_lockres_worker, NULL);
2331 item->u.dl.deref_res = res;
2332 item->u.dl.deref_node = node;
2333
2334 spin_lock(&dlm->work_lock);
2335 list_add_tail(&item->list, &dlm->work_list);
2336 spin_unlock(&dlm->work_lock);
2337
2338 queue_work(dlm->dlm_worker, &dlm->dispatched_work);
2339 return 0;
2340
Kurt Hackelba2bf212006-12-01 14:47:20 -08002341done:
2342 if (res)
2343 dlm_lockres_put(res);
2344 dlm_put(dlm);
Sunil Mushranf3f85462007-01-29 15:19:16 -08002345
Kurt Hackelba2bf212006-12-01 14:47:20 -08002346 return ret;
2347}
2348
Sunil Mushranf3f85462007-01-29 15:19:16 -08002349static void dlm_deref_lockres_worker(struct dlm_work_item *item, void *data)
2350{
2351 struct dlm_ctxt *dlm;
2352 struct dlm_lock_resource *res;
2353 u8 node;
2354 u8 cleared = 0;
2355
2356 dlm = item->dlm;
2357 res = item->u.dl.deref_res;
2358 node = item->u.dl.deref_node;
2359
2360 spin_lock(&res->spinlock);
2361 BUG_ON(res->state & DLM_LOCK_RES_DROPPING_REF);
2362 if (test_bit(node, res->refmap)) {
2363 __dlm_wait_on_lockres_flags(res, DLM_LOCK_RES_SETREF_INPROG);
2364 dlm_lockres_clear_refmap_bit(node, res);
2365 cleared = 1;
2366 }
2367 spin_unlock(&res->spinlock);
2368
2369 if (cleared) {
2370 mlog(0, "%s:%.*s node %u ref dropped in dispatch\n",
2371 dlm->name, res->lockname.len, res->lockname.name, node);
2372 dlm_lockres_calc_usage(dlm, res);
2373 } else {
2374 mlog(ML_ERROR, "%s:%.*s: node %u trying to drop ref "
2375 "but it is already dropped!\n", dlm->name,
2376 res->lockname.len, res->lockname.name, node);
Tao Ma2af37ce2008-02-28 10:41:55 +08002377 dlm_print_one_lock_resource(res);
Sunil Mushranf3f85462007-01-29 15:19:16 -08002378 }
2379
2380 dlm_lockres_put(res);
2381}
2382
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -07002383/* Checks whether the lockres can be migrated. Returns 0 if yes, < 0
2384 * if not. If 0, numlocks is set to the number of locks in the lockres.
2385 */
2386static int dlm_is_lockres_migrateable(struct dlm_ctxt *dlm,
2387 struct dlm_lock_resource *res,
2388 int *numlocks)
2389{
2390 int ret;
2391 int i;
2392 int count = 0;
Christoph Hellwig800deef2007-05-17 16:03:13 +02002393 struct list_head *queue;
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -07002394 struct dlm_lock *lock;
2395
2396 assert_spin_locked(&res->spinlock);
2397
2398 ret = -EINVAL;
2399 if (res->owner == DLM_LOCK_RES_OWNER_UNKNOWN) {
2400 mlog(0, "cannot migrate lockres with unknown owner!\n");
2401 goto leave;
2402 }
2403
2404 if (res->owner != dlm->node_num) {
2405 mlog(0, "cannot migrate lockres this node doesn't own!\n");
2406 goto leave;
2407 }
2408
2409 ret = 0;
2410 queue = &res->granted;
2411 for (i = 0; i < 3; i++) {
Christoph Hellwig800deef2007-05-17 16:03:13 +02002412 list_for_each_entry(lock, queue, list) {
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -07002413 ++count;
2414 if (lock->ml.node == dlm->node_num) {
2415 mlog(0, "found a lock owned by this node still "
2416 "on the %s queue! will not migrate this "
2417 "lockres\n", (i == 0 ? "granted" :
2418 (i == 1 ? "converting" :
2419 "blocked")));
2420 ret = -ENOTEMPTY;
2421 goto leave;
2422 }
2423 }
2424 queue++;
2425 }
2426
2427 *numlocks = count;
2428 mlog(0, "migrateable lockres having %d locks\n", *numlocks);
2429
2430leave:
2431 return ret;
2432}
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002433
2434/*
2435 * DLM_MIGRATE_LOCKRES
2436 */
2437
2438
Adrian Bunkfaf0ec92006-12-14 00:17:32 +01002439static int dlm_migrate_lockres(struct dlm_ctxt *dlm,
2440 struct dlm_lock_resource *res,
2441 u8 target)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002442{
2443 struct dlm_master_list_entry *mle = NULL;
2444 struct dlm_master_list_entry *oldmle = NULL;
2445 struct dlm_migratable_lockres *mres = NULL;
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -07002446 int ret = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002447 const char *name;
2448 unsigned int namelen;
2449 int mle_added = 0;
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -07002450 int numlocks;
2451 int wake = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002452
2453 if (!dlm_grab(dlm))
2454 return -EINVAL;
2455
2456 name = res->lockname.name;
2457 namelen = res->lockname.len;
2458
2459 mlog(0, "migrating %.*s to %u\n", namelen, name, target);
2460
2461 /*
2462 * ensure this lockres is a proper candidate for migration
2463 */
2464 spin_lock(&res->spinlock);
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -07002465 ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
2466 if (ret < 0) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002467 spin_unlock(&res->spinlock);
2468 goto leave;
2469 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002470 spin_unlock(&res->spinlock);
2471
2472 /* no work to do */
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -07002473 if (numlocks == 0) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002474 mlog(0, "no locks were found on this lockres! done!\n");
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002475 goto leave;
2476 }
2477
2478 /*
2479 * preallocate up front
2480 * if this fails, abort
2481 */
2482
2483 ret = -ENOMEM;
Kurt Hackelad8100e2006-05-01 14:25:21 -07002484 mres = (struct dlm_migratable_lockres *) __get_free_page(GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002485 if (!mres) {
2486 mlog_errno(ret);
2487 goto leave;
2488 }
2489
2490 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
Kurt Hackelad8100e2006-05-01 14:25:21 -07002491 GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002492 if (!mle) {
2493 mlog_errno(ret);
2494 goto leave;
2495 }
2496 ret = 0;
2497
2498 /*
2499 * find a node to migrate the lockres to
2500 */
2501
2502 mlog(0, "picking a migration node\n");
2503 spin_lock(&dlm->spinlock);
2504 /* pick a new node */
2505 if (!test_bit(target, dlm->domain_map) ||
2506 target >= O2NM_MAX_NODES) {
2507 target = dlm_pick_migration_target(dlm, res);
2508 }
2509 mlog(0, "node %u chosen for migration\n", target);
2510
2511 if (target >= O2NM_MAX_NODES ||
2512 !test_bit(target, dlm->domain_map)) {
2513 /* target chosen is not alive */
2514 ret = -EINVAL;
2515 }
2516
2517 if (ret) {
2518 spin_unlock(&dlm->spinlock);
2519 goto fail;
2520 }
2521
2522 mlog(0, "continuing with target = %u\n", target);
2523
2524 /*
2525 * clear any existing master requests and
2526 * add the migration mle to the list
2527 */
2528 spin_lock(&dlm->master_lock);
2529 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle, name,
2530 namelen, target, dlm->node_num);
2531 spin_unlock(&dlm->master_lock);
2532 spin_unlock(&dlm->spinlock);
2533
2534 if (ret == -EEXIST) {
2535 mlog(0, "another process is already migrating it\n");
2536 goto fail;
2537 }
2538 mle_added = 1;
2539
2540 /*
2541 * set the MIGRATING flag and flush asts
2542 * if we fail after this we need to re-dirty the lockres
2543 */
2544 if (dlm_mark_lockres_migrating(dlm, res, target) < 0) {
2545 mlog(ML_ERROR, "tried to migrate %.*s to %u, but "
2546 "the target went down.\n", res->lockname.len,
2547 res->lockname.name, target);
2548 spin_lock(&res->spinlock);
2549 res->state &= ~DLM_LOCK_RES_MIGRATING;
Kurt Hackela6fa3642007-01-17 14:59:12 -08002550 wake = 1;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002551 spin_unlock(&res->spinlock);
2552 ret = -EINVAL;
2553 }
2554
2555fail:
2556 if (oldmle) {
2557 /* master is known, detach if not already detached */
2558 dlm_mle_detach_hb_events(dlm, oldmle);
2559 dlm_put_mle(oldmle);
2560 }
2561
2562 if (ret < 0) {
2563 if (mle_added) {
2564 dlm_mle_detach_hb_events(dlm, mle);
2565 dlm_put_mle(mle);
2566 } else if (mle) {
2567 kmem_cache_free(dlm_mle_cache, mle);
2568 }
2569 goto leave;
2570 }
2571
2572 /*
2573 * at this point, we have a migration target, an mle
2574 * in the master list, and the MIGRATING flag set on
2575 * the lockres
2576 */
2577
Kurt Hackel1cd04db2007-01-17 14:53:37 -08002578 /* now that remote nodes are spinning on the MIGRATING flag,
2579 * ensure that all assert_master work is flushed. */
2580 flush_workqueue(dlm->dlm_worker);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002581
2582 /* get an extra reference on the mle.
2583 * otherwise the assert_master from the new
2584 * master will destroy this.
2585 * also, make sure that all callers of dlm_get_mle
2586 * take both dlm->spinlock and dlm->master_lock */
2587 spin_lock(&dlm->spinlock);
2588 spin_lock(&dlm->master_lock);
Kurt Hackela2bf0472006-04-27 18:51:26 -07002589 dlm_get_mle_inuse(mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002590 spin_unlock(&dlm->master_lock);
2591 spin_unlock(&dlm->spinlock);
2592
2593 /* notify new node and send all lock state */
2594 /* call send_one_lockres with migration flag.
2595 * this serves as notice to the target node that a
2596 * migration is starting. */
2597 ret = dlm_send_one_lockres(dlm, res, mres, target,
2598 DLM_MRES_MIGRATION);
2599
2600 if (ret < 0) {
2601 mlog(0, "migration to node %u failed with %d\n",
2602 target, ret);
2603 /* migration failed, detach and clean up mle */
2604 dlm_mle_detach_hb_events(dlm, mle);
2605 dlm_put_mle(mle);
Kurt Hackela2bf0472006-04-27 18:51:26 -07002606 dlm_put_mle_inuse(mle);
2607 spin_lock(&res->spinlock);
2608 res->state &= ~DLM_LOCK_RES_MIGRATING;
Kurt Hackela6fa3642007-01-17 14:59:12 -08002609 wake = 1;
Kurt Hackela2bf0472006-04-27 18:51:26 -07002610 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002611 goto leave;
2612 }
2613
2614 /* at this point, the target sends a message to all nodes,
2615 * (using dlm_do_migrate_request). this node is skipped since
2616 * we had to put an mle in the list to begin the process. this
2617 * node now waits for target to do an assert master. this node
2618 * will be the last one notified, ensuring that the migration
2619 * is complete everywhere. if the target dies while this is
2620 * going on, some nodes could potentially see the target as the
2621 * master, so it is important that my recovery finds the migration
2622 * mle and sets the master to UNKNONWN. */
2623
2624
2625 /* wait for new node to assert master */
2626 while (1) {
2627 ret = wait_event_interruptible_timeout(mle->wq,
2628 (atomic_read(&mle->woken) == 1),
2629 msecs_to_jiffies(5000));
2630
2631 if (ret >= 0) {
2632 if (atomic_read(&mle->woken) == 1 ||
2633 res->owner == target)
2634 break;
2635
Kurt Hackel1cd04db2007-01-17 14:53:37 -08002636 mlog(0, "%s:%.*s: timed out during migration\n",
2637 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackele2faea42006-01-12 14:24:55 -08002638 /* avoid hang during shutdown when migrating lockres
2639 * to a node which also goes down */
2640 if (dlm_is_node_dead(dlm, target)) {
Kurt Hackelaa852352006-04-27 19:04:49 -07002641 mlog(0, "%s:%.*s: expected migration "
2642 "target %u is no longer up, restarting\n",
Kurt Hackele2faea42006-01-12 14:24:55 -08002643 dlm->name, res->lockname.len,
2644 res->lockname.name, target);
Kurt Hackel1cd04db2007-01-17 14:53:37 -08002645 ret = -EINVAL;
2646 /* migration failed, detach and clean up mle */
2647 dlm_mle_detach_hb_events(dlm, mle);
2648 dlm_put_mle(mle);
2649 dlm_put_mle_inuse(mle);
2650 spin_lock(&res->spinlock);
2651 res->state &= ~DLM_LOCK_RES_MIGRATING;
Kurt Hackela6fa3642007-01-17 14:59:12 -08002652 wake = 1;
Kurt Hackel1cd04db2007-01-17 14:53:37 -08002653 spin_unlock(&res->spinlock);
2654 goto leave;
Kurt Hackele2faea42006-01-12 14:24:55 -08002655 }
Kurt Hackel1cd04db2007-01-17 14:53:37 -08002656 } else
2657 mlog(0, "%s:%.*s: caught signal during migration\n",
2658 dlm->name, res->lockname.len, res->lockname.name);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002659 }
2660
2661 /* all done, set the owner, clear the flag */
2662 spin_lock(&res->spinlock);
2663 dlm_set_lockres_owner(dlm, res, target);
2664 res->state &= ~DLM_LOCK_RES_MIGRATING;
2665 dlm_remove_nonlocal_locks(dlm, res);
2666 spin_unlock(&res->spinlock);
2667 wake_up(&res->wq);
2668
2669 /* master is known, detach if not already detached */
2670 dlm_mle_detach_hb_events(dlm, mle);
Kurt Hackela2bf0472006-04-27 18:51:26 -07002671 dlm_put_mle_inuse(mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002672 ret = 0;
2673
2674 dlm_lockres_calc_usage(dlm, res);
2675
2676leave:
2677 /* re-dirty the lockres if we failed */
2678 if (ret < 0)
2679 dlm_kick_thread(dlm, res);
2680
Kurt Hackela6fa3642007-01-17 14:59:12 -08002681 /* wake up waiters if the MIGRATING flag got set
2682 * but migration failed */
2683 if (wake)
2684 wake_up(&res->wq);
2685
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002686 /* TODO: cleanup */
2687 if (mres)
2688 free_page((unsigned long)mres);
2689
2690 dlm_put(dlm);
2691
2692 mlog(0, "returning %d\n", ret);
2693 return ret;
2694}
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002695
Kurt Hackelba2bf212006-12-01 14:47:20 -08002696#define DLM_MIGRATION_RETRY_MS 100
2697
2698/* Should be called only after beginning the domain leave process.
2699 * There should not be any remaining locks on nonlocal lock resources,
2700 * and there should be no local locks left on locally mastered resources.
2701 *
2702 * Called with the dlm spinlock held, may drop it to do migration, but
2703 * will re-acquire before exit.
2704 *
2705 * Returns: 1 if dlm->spinlock was dropped/retaken, 0 if never dropped */
2706int dlm_empty_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res)
2707{
2708 int ret;
2709 int lock_dropped = 0;
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -07002710 int numlocks;
Kurt Hackelba2bf212006-12-01 14:47:20 -08002711
Sunil Mushranb36c3f82007-03-12 13:25:44 -07002712 spin_lock(&res->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -08002713 if (res->owner != dlm->node_num) {
2714 if (!__dlm_lockres_unused(res)) {
2715 mlog(ML_ERROR, "%s:%.*s: this node is not master, "
2716 "trying to free this but locks remain\n",
2717 dlm->name, res->lockname.len, res->lockname.name);
2718 }
Sunil Mushranb36c3f82007-03-12 13:25:44 -07002719 spin_unlock(&res->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -08002720 goto leave;
2721 }
Sunil Mushran2f5bf1f2007-03-22 17:08:32 -07002722
2723 /* No need to migrate a lockres having no locks */
2724 ret = dlm_is_lockres_migrateable(dlm, res, &numlocks);
2725 if (ret >= 0 && numlocks == 0) {
2726 spin_unlock(&res->spinlock);
2727 goto leave;
2728 }
Sunil Mushranb36c3f82007-03-12 13:25:44 -07002729 spin_unlock(&res->spinlock);
Kurt Hackelba2bf212006-12-01 14:47:20 -08002730
2731 /* Wheee! Migrate lockres here! Will sleep so drop spinlock. */
2732 spin_unlock(&dlm->spinlock);
2733 lock_dropped = 1;
2734 while (1) {
2735 ret = dlm_migrate_lockres(dlm, res, O2NM_MAX_NODES);
2736 if (ret >= 0)
2737 break;
2738 if (ret == -ENOTEMPTY) {
2739 mlog(ML_ERROR, "lockres %.*s still has local locks!\n",
2740 res->lockname.len, res->lockname.name);
2741 BUG();
2742 }
2743
2744 mlog(0, "lockres %.*s: migrate failed, "
2745 "retrying\n", res->lockname.len,
2746 res->lockname.name);
2747 msleep(DLM_MIGRATION_RETRY_MS);
2748 }
2749 spin_lock(&dlm->spinlock);
2750leave:
2751 return lock_dropped;
2752}
2753
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002754int dlm_lock_basts_flushed(struct dlm_ctxt *dlm, struct dlm_lock *lock)
2755{
2756 int ret;
2757 spin_lock(&dlm->ast_lock);
2758 spin_lock(&lock->spinlock);
2759 ret = (list_empty(&lock->bast_list) && !lock->bast_pending);
2760 spin_unlock(&lock->spinlock);
2761 spin_unlock(&dlm->ast_lock);
2762 return ret;
2763}
2764
2765static int dlm_migration_can_proceed(struct dlm_ctxt *dlm,
2766 struct dlm_lock_resource *res,
2767 u8 mig_target)
2768{
2769 int can_proceed;
2770 spin_lock(&res->spinlock);
2771 can_proceed = !!(res->state & DLM_LOCK_RES_MIGRATING);
2772 spin_unlock(&res->spinlock);
2773
2774 /* target has died, so make the caller break out of the
2775 * wait_event, but caller must recheck the domain_map */
2776 spin_lock(&dlm->spinlock);
2777 if (!test_bit(mig_target, dlm->domain_map))
2778 can_proceed = 1;
2779 spin_unlock(&dlm->spinlock);
2780 return can_proceed;
2781}
2782
Adrian Bunkfaf0ec92006-12-14 00:17:32 +01002783static int dlm_lockres_is_dirty(struct dlm_ctxt *dlm,
2784 struct dlm_lock_resource *res)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002785{
2786 int ret;
2787 spin_lock(&res->spinlock);
2788 ret = !!(res->state & DLM_LOCK_RES_DIRTY);
2789 spin_unlock(&res->spinlock);
2790 return ret;
2791}
2792
2793
2794static int dlm_mark_lockres_migrating(struct dlm_ctxt *dlm,
2795 struct dlm_lock_resource *res,
2796 u8 target)
2797{
2798 int ret = 0;
2799
2800 mlog(0, "dlm_mark_lockres_migrating: %.*s, from %u to %u\n",
2801 res->lockname.len, res->lockname.name, dlm->node_num,
2802 target);
2803 /* need to set MIGRATING flag on lockres. this is done by
2804 * ensuring that all asts have been flushed for this lockres. */
2805 spin_lock(&res->spinlock);
2806 BUG_ON(res->migration_pending);
2807 res->migration_pending = 1;
2808 /* strategy is to reserve an extra ast then release
2809 * it below, letting the release do all of the work */
2810 __dlm_lockres_reserve_ast(res);
2811 spin_unlock(&res->spinlock);
2812
Kurt Hackelddc09c82007-01-05 15:00:17 -08002813 /* now flush all the pending asts */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002814 dlm_kick_thread(dlm, res);
Kurt Hackelddc09c82007-01-05 15:00:17 -08002815 /* before waiting on DIRTY, block processes which may
2816 * try to dirty the lockres before MIGRATING is set */
2817 spin_lock(&res->spinlock);
2818 BUG_ON(res->state & DLM_LOCK_RES_BLOCK_DIRTY);
2819 res->state |= DLM_LOCK_RES_BLOCK_DIRTY;
2820 spin_unlock(&res->spinlock);
2821 /* now wait on any pending asts and the DIRTY state */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002822 wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res));
2823 dlm_lockres_release_ast(dlm, res);
2824
2825 mlog(0, "about to wait on migration_wq, dirty=%s\n",
2826 res->state & DLM_LOCK_RES_DIRTY ? "yes" : "no");
2827 /* if the extra ref we just put was the final one, this
2828 * will pass thru immediately. otherwise, we need to wait
2829 * for the last ast to finish. */
2830again:
2831 ret = wait_event_interruptible_timeout(dlm->migration_wq,
2832 dlm_migration_can_proceed(dlm, res, target),
2833 msecs_to_jiffies(1000));
2834 if (ret < 0) {
2835 mlog(0, "woken again: migrating? %s, dead? %s\n",
2836 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2837 test_bit(target, dlm->domain_map) ? "no":"yes");
2838 } else {
2839 mlog(0, "all is well: migrating? %s, dead? %s\n",
2840 res->state & DLM_LOCK_RES_MIGRATING ? "yes":"no",
2841 test_bit(target, dlm->domain_map) ? "no":"yes");
2842 }
2843 if (!dlm_migration_can_proceed(dlm, res, target)) {
2844 mlog(0, "trying again...\n");
2845 goto again;
2846 }
Kurt Hackelddc09c82007-01-05 15:00:17 -08002847 /* now that we are sure the MIGRATING state is there, drop
2848 * the unneded state which blocked threads trying to DIRTY */
2849 spin_lock(&res->spinlock);
2850 BUG_ON(!(res->state & DLM_LOCK_RES_BLOCK_DIRTY));
2851 BUG_ON(!(res->state & DLM_LOCK_RES_MIGRATING));
2852 res->state &= ~DLM_LOCK_RES_BLOCK_DIRTY;
2853 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002854
2855 /* did the target go down or die? */
2856 spin_lock(&dlm->spinlock);
2857 if (!test_bit(target, dlm->domain_map)) {
2858 mlog(ML_ERROR, "aha. migration target %u just went down\n",
2859 target);
2860 ret = -EHOSTDOWN;
2861 }
2862 spin_unlock(&dlm->spinlock);
2863
2864 /*
2865 * at this point:
2866 *
2867 * o the DLM_LOCK_RES_MIGRATING flag is set
2868 * o there are no pending asts on this lockres
2869 * o all processes trying to reserve an ast on this
2870 * lockres must wait for the MIGRATING flag to clear
2871 */
2872 return ret;
2873}
2874
2875/* last step in the migration process.
2876 * original master calls this to free all of the dlm_lock
2877 * structures that used to be for other nodes. */
2878static void dlm_remove_nonlocal_locks(struct dlm_ctxt *dlm,
2879 struct dlm_lock_resource *res)
2880{
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002881 struct list_head *queue = &res->granted;
Kurt Hackelba2bf212006-12-01 14:47:20 -08002882 int i, bit;
Christoph Hellwig800deef2007-05-17 16:03:13 +02002883 struct dlm_lock *lock, *next;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002884
2885 assert_spin_locked(&res->spinlock);
2886
2887 BUG_ON(res->owner == dlm->node_num);
2888
2889 for (i=0; i<3; i++) {
Christoph Hellwig800deef2007-05-17 16:03:13 +02002890 list_for_each_entry_safe(lock, next, queue, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002891 if (lock->ml.node != dlm->node_num) {
2892 mlog(0, "putting lock for node %u\n",
2893 lock->ml.node);
2894 /* be extra careful */
2895 BUG_ON(!list_empty(&lock->ast_list));
2896 BUG_ON(!list_empty(&lock->bast_list));
2897 BUG_ON(lock->ast_pending);
2898 BUG_ON(lock->bast_pending);
Kurt Hackelba2bf212006-12-01 14:47:20 -08002899 dlm_lockres_clear_refmap_bit(lock->ml.node, res);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002900 list_del_init(&lock->list);
2901 dlm_lock_put(lock);
Sunil Mushran2c5c54a2008-03-01 14:04:20 -08002902 /* In a normal unlock, we would have added a
2903 * DLM_UNLOCK_FREE_LOCK action. Force it. */
2904 dlm_lock_put(lock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002905 }
2906 }
2907 queue++;
2908 }
Kurt Hackelba2bf212006-12-01 14:47:20 -08002909 bit = 0;
2910 while (1) {
2911 bit = find_next_bit(res->refmap, O2NM_MAX_NODES, bit);
2912 if (bit >= O2NM_MAX_NODES)
2913 break;
2914 /* do not clear the local node reference, if there is a
2915 * process holding this, let it drop the ref itself */
2916 if (bit != dlm->node_num) {
2917 mlog(0, "%s:%.*s: node %u had a ref to this "
2918 "migrating lockres, clearing\n", dlm->name,
2919 res->lockname.len, res->lockname.name, bit);
2920 dlm_lockres_clear_refmap_bit(bit, res);
2921 }
2922 bit++;
2923 }
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002924}
2925
2926/* for now this is not too intelligent. we will
2927 * need stats to make this do the right thing.
2928 * this just finds the first lock on one of the
2929 * queues and uses that node as the target. */
2930static u8 dlm_pick_migration_target(struct dlm_ctxt *dlm,
2931 struct dlm_lock_resource *res)
2932{
2933 int i;
2934 struct list_head *queue = &res->granted;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002935 struct dlm_lock *lock;
2936 int nodenum;
2937
2938 assert_spin_locked(&dlm->spinlock);
2939
2940 spin_lock(&res->spinlock);
2941 for (i=0; i<3; i++) {
Christoph Hellwig800deef2007-05-17 16:03:13 +02002942 list_for_each_entry(lock, queue, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002943 /* up to the caller to make sure this node
2944 * is alive */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002945 if (lock->ml.node != dlm->node_num) {
2946 spin_unlock(&res->spinlock);
2947 return lock->ml.node;
2948 }
2949 }
2950 queue++;
2951 }
2952 spin_unlock(&res->spinlock);
2953 mlog(0, "have not found a suitable target yet! checking domain map\n");
2954
2955 /* ok now we're getting desperate. pick anyone alive. */
2956 nodenum = -1;
2957 while (1) {
2958 nodenum = find_next_bit(dlm->domain_map,
2959 O2NM_MAX_NODES, nodenum+1);
2960 mlog(0, "found %d in domain map\n", nodenum);
2961 if (nodenum >= O2NM_MAX_NODES)
2962 break;
2963 if (nodenum != dlm->node_num) {
2964 mlog(0, "picking %d\n", nodenum);
2965 return nodenum;
2966 }
2967 }
2968
2969 mlog(0, "giving up. no master to migrate to\n");
2970 return DLM_LOCK_RES_OWNER_UNKNOWN;
2971}
2972
2973
2974
2975/* this is called by the new master once all lockres
2976 * data has been received */
2977static int dlm_do_migrate_request(struct dlm_ctxt *dlm,
2978 struct dlm_lock_resource *res,
2979 u8 master, u8 new_master,
2980 struct dlm_node_iter *iter)
2981{
2982 struct dlm_migrate_request migrate;
Sunil Mushran2b832562008-12-16 15:49:19 -08002983 int ret, skip, status = 0;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08002984 int nodenum;
2985
2986 memset(&migrate, 0, sizeof(migrate));
2987 migrate.namelen = res->lockname.len;
2988 memcpy(migrate.name, res->lockname.name, migrate.namelen);
2989 migrate.new_master = new_master;
2990 migrate.master = master;
2991
2992 ret = 0;
2993
2994 /* send message to all nodes, except the master and myself */
2995 while ((nodenum = dlm_node_iter_next(iter)) >= 0) {
2996 if (nodenum == master ||
2997 nodenum == new_master)
2998 continue;
2999
Sunil Mushran2b832562008-12-16 15:49:19 -08003000 /* We could race exit domain. If exited, skip. */
3001 spin_lock(&dlm->spinlock);
3002 skip = (!test_bit(nodenum, dlm->domain_map));
3003 spin_unlock(&dlm->spinlock);
3004 if (skip) {
3005 clear_bit(nodenum, iter->node_map);
3006 continue;
3007 }
3008
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003009 ret = o2net_send_message(DLM_MIGRATE_REQUEST_MSG, dlm->key,
3010 &migrate, sizeof(migrate), nodenum,
3011 &status);
Sunil Mushran2b832562008-12-16 15:49:19 -08003012 if (ret < 0) {
3013 mlog(0, "migrate_request returned %d!\n", ret);
3014 if (!dlm_is_host_down(ret)) {
3015 mlog(ML_ERROR, "unhandled error=%d!\n", ret);
3016 BUG();
3017 }
3018 clear_bit(nodenum, iter->node_map);
3019 ret = 0;
3020 } else if (status < 0) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003021 mlog(0, "migrate request (node %u) returned %d!\n",
3022 nodenum, status);
3023 ret = status;
Kurt Hackelba2bf212006-12-01 14:47:20 -08003024 } else if (status == DLM_MIGRATE_RESPONSE_MASTERY_REF) {
3025 /* during the migration request we short-circuited
3026 * the mastery of the lockres. make sure we have
3027 * a mastery ref for nodenum */
3028 mlog(0, "%s:%.*s: need ref for node %u\n",
3029 dlm->name, res->lockname.len, res->lockname.name,
3030 nodenum);
3031 spin_lock(&res->spinlock);
3032 dlm_lockres_set_refmap_bit(nodenum, res);
3033 spin_unlock(&res->spinlock);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003034 }
3035 }
3036
3037 if (ret < 0)
3038 mlog_errno(ret);
3039
3040 mlog(0, "returning ret=%d\n", ret);
3041 return ret;
3042}
3043
3044
3045/* if there is an existing mle for this lockres, we now know who the master is.
3046 * (the one who sent us *this* message) we can clear it up right away.
3047 * since the process that put the mle on the list still has a reference to it,
3048 * we can unhash it now, set the master and wake the process. as a result,
3049 * we will have no mle in the list to start with. now we can add an mle for
3050 * the migration and this should be the only one found for those scanning the
3051 * list. */
Kurt Hackeld74c9802007-01-17 17:04:25 -08003052int dlm_migrate_request_handler(struct o2net_msg *msg, u32 len, void *data,
3053 void **ret_data)
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003054{
3055 struct dlm_ctxt *dlm = data;
3056 struct dlm_lock_resource *res = NULL;
3057 struct dlm_migrate_request *migrate = (struct dlm_migrate_request *) msg->buf;
3058 struct dlm_master_list_entry *mle = NULL, *oldmle = NULL;
3059 const char *name;
Mark Fasheha3d33292006-03-09 17:55:56 -08003060 unsigned int namelen, hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003061 int ret = 0;
3062
3063 if (!dlm_grab(dlm))
3064 return -EINVAL;
3065
3066 name = migrate->name;
3067 namelen = migrate->namelen;
Mark Fasheha3d33292006-03-09 17:55:56 -08003068 hash = dlm_lockid_hash(name, namelen);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003069
3070 /* preallocate.. if this fails, abort */
3071 mle = (struct dlm_master_list_entry *) kmem_cache_alloc(dlm_mle_cache,
Kurt Hackelad8100e2006-05-01 14:25:21 -07003072 GFP_NOFS);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003073
3074 if (!mle) {
3075 ret = -ENOMEM;
3076 goto leave;
3077 }
3078
3079 /* check for pre-existing lock */
3080 spin_lock(&dlm->spinlock);
Mark Fasheha3d33292006-03-09 17:55:56 -08003081 res = __dlm_lookup_lockres(dlm, name, namelen, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003082 spin_lock(&dlm->master_lock);
3083
3084 if (res) {
3085 spin_lock(&res->spinlock);
3086 if (res->state & DLM_LOCK_RES_RECOVERING) {
3087 /* if all is working ok, this can only mean that we got
3088 * a migrate request from a node that we now see as
3089 * dead. what can we do here? drop it to the floor? */
3090 spin_unlock(&res->spinlock);
3091 mlog(ML_ERROR, "Got a migrate request, but the "
3092 "lockres is marked as recovering!");
3093 kmem_cache_free(dlm_mle_cache, mle);
3094 ret = -EINVAL; /* need a better solution */
3095 goto unlock;
3096 }
3097 res->state |= DLM_LOCK_RES_MIGRATING;
3098 spin_unlock(&res->spinlock);
3099 }
3100
3101 /* ignore status. only nonzero status would BUG. */
3102 ret = dlm_add_migration_mle(dlm, res, mle, &oldmle,
3103 name, namelen,
3104 migrate->new_master,
3105 migrate->master);
3106
3107unlock:
3108 spin_unlock(&dlm->master_lock);
3109 spin_unlock(&dlm->spinlock);
3110
3111 if (oldmle) {
3112 /* master is known, detach if not already detached */
3113 dlm_mle_detach_hb_events(dlm, oldmle);
3114 dlm_put_mle(oldmle);
3115 }
3116
3117 if (res)
3118 dlm_lockres_put(res);
3119leave:
3120 dlm_put(dlm);
3121 return ret;
3122}
3123
3124/* must be holding dlm->spinlock and dlm->master_lock
3125 * when adding a migration mle, we can clear any other mles
3126 * in the master list because we know with certainty that
3127 * the master is "master". so we remove any old mle from
3128 * the list after setting it's master field, and then add
3129 * the new migration mle. this way we can hold with the rule
3130 * of having only one mle for a given lock name at all times. */
3131static int dlm_add_migration_mle(struct dlm_ctxt *dlm,
3132 struct dlm_lock_resource *res,
3133 struct dlm_master_list_entry *mle,
3134 struct dlm_master_list_entry **oldmle,
3135 const char *name, unsigned int namelen,
3136 u8 new_master, u8 master)
3137{
3138 int found;
3139 int ret = 0;
3140
3141 *oldmle = NULL;
3142
3143 mlog_entry_void();
3144
3145 assert_spin_locked(&dlm->spinlock);
3146 assert_spin_locked(&dlm->master_lock);
3147
3148 /* caller is responsible for any ref taken here on oldmle */
3149 found = dlm_find_mle(dlm, oldmle, (char *)name, namelen);
3150 if (found) {
3151 struct dlm_master_list_entry *tmp = *oldmle;
3152 spin_lock(&tmp->spinlock);
3153 if (tmp->type == DLM_MLE_MIGRATION) {
3154 if (master == dlm->node_num) {
3155 /* ah another process raced me to it */
3156 mlog(0, "tried to migrate %.*s, but some "
3157 "process beat me to it\n",
3158 namelen, name);
3159 ret = -EEXIST;
3160 } else {
3161 /* bad. 2 NODES are trying to migrate! */
3162 mlog(ML_ERROR, "migration error mle: "
3163 "master=%u new_master=%u // request: "
3164 "master=%u new_master=%u // "
3165 "lockres=%.*s\n",
3166 tmp->master, tmp->new_master,
3167 master, new_master,
3168 namelen, name);
3169 BUG();
3170 }
3171 } else {
3172 /* this is essentially what assert_master does */
3173 tmp->master = master;
3174 atomic_set(&tmp->woken, 1);
3175 wake_up(&tmp->wq);
Sunil Mushran1c084572009-02-26 15:00:37 -08003176 /* remove it so that only one mle will be found */
3177 __dlm_unlink_mle(dlm, tmp);
Kurt Hackelba2bf212006-12-01 14:47:20 -08003178 __dlm_mle_detach_hb_events(dlm, tmp);
3179 ret = DLM_MIGRATE_RESPONSE_MASTERY_REF;
3180 mlog(0, "%s:%.*s: master=%u, newmaster=%u, "
3181 "telling master to get ref for cleared out mle "
3182 "during migration\n", dlm->name, namelen, name,
3183 master, new_master);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003184 }
3185 spin_unlock(&tmp->spinlock);
3186 }
3187
3188 /* now add a migration mle to the tail of the list */
3189 dlm_init_mle(mle, DLM_MLE_MIGRATION, dlm, res, name, namelen);
3190 mle->new_master = new_master;
Kurt Hackelba2bf212006-12-01 14:47:20 -08003191 /* the new master will be sending an assert master for this.
3192 * at that point we will get the refmap reference */
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003193 mle->master = master;
3194 /* do this for consistency with other mle types */
3195 set_bit(new_master, mle->maybe_map);
Sunil Mushran1c084572009-02-26 15:00:37 -08003196 __dlm_insert_mle(dlm, mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003197
3198 return ret;
3199}
3200
3201
3202void dlm_clean_master_list(struct dlm_ctxt *dlm, u8 dead_node)
3203{
Christoph Hellwig800deef2007-05-17 16:03:13 +02003204 struct dlm_master_list_entry *mle, *next;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003205 struct dlm_lock_resource *res;
Mark Fasheha3d33292006-03-09 17:55:56 -08003206 unsigned int hash;
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003207
3208 mlog_entry("dlm=%s, dead node=%u\n", dlm->name, dead_node);
3209top:
3210 assert_spin_locked(&dlm->spinlock);
3211
3212 /* clean the master list */
3213 spin_lock(&dlm->master_lock);
Christoph Hellwig800deef2007-05-17 16:03:13 +02003214 list_for_each_entry_safe(mle, next, &dlm->master_list, list) {
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003215 BUG_ON(mle->type != DLM_MLE_BLOCK &&
3216 mle->type != DLM_MLE_MASTER &&
3217 mle->type != DLM_MLE_MIGRATION);
3218
3219 /* MASTER mles are initiated locally. the waiting
3220 * process will notice the node map change
3221 * shortly. let that happen as normal. */
3222 if (mle->type == DLM_MLE_MASTER)
3223 continue;
3224
3225
3226 /* BLOCK mles are initiated by other nodes.
3227 * need to clean up if the dead node would have
3228 * been the master. */
3229 if (mle->type == DLM_MLE_BLOCK) {
3230 int bit;
3231
3232 spin_lock(&mle->spinlock);
3233 bit = find_next_bit(mle->maybe_map, O2NM_MAX_NODES, 0);
3234 if (bit != dead_node) {
3235 mlog(0, "mle found, but dead node %u would "
3236 "not have been master\n", dead_node);
3237 spin_unlock(&mle->spinlock);
3238 } else {
3239 /* must drop the refcount by one since the
3240 * assert_master will never arrive. this
3241 * may result in the mle being unlinked and
3242 * freed, but there may still be a process
3243 * waiting in the dlmlock path which is fine. */
Kurt Hackel3b3b84a2006-05-01 14:31:37 -07003244 mlog(0, "node %u was expected master\n",
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003245 dead_node);
3246 atomic_set(&mle->woken, 1);
3247 spin_unlock(&mle->spinlock);
3248 wake_up(&mle->wq);
Kurt Hackelf671c092006-02-14 11:45:21 -08003249 /* do not need events any longer, so detach
3250 * from heartbeat */
3251 __dlm_mle_detach_hb_events(dlm, mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003252 __dlm_put_mle(mle);
3253 }
3254 continue;
3255 }
3256
3257 /* everything else is a MIGRATION mle */
3258
3259 /* the rule for MIGRATION mles is that the master
3260 * becomes UNKNOWN if *either* the original or
3261 * the new master dies. all UNKNOWN lockreses
3262 * are sent to whichever node becomes the recovery
3263 * master. the new master is responsible for
3264 * determining if there is still a master for
3265 * this lockres, or if he needs to take over
3266 * mastery. either way, this node should expect
3267 * another message to resolve this. */
3268 if (mle->master != dead_node &&
3269 mle->new_master != dead_node)
3270 continue;
3271
3272 /* if we have reached this point, this mle needs to
3273 * be removed from the list and freed. */
3274
3275 /* remove from the list early. NOTE: unlinking
3276 * list_head while in list_for_each_safe */
Kurt Hackelda01ad02006-04-27 18:53:04 -07003277 __dlm_mle_detach_hb_events(dlm, mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003278 spin_lock(&mle->spinlock);
Sunil Mushran1c084572009-02-26 15:00:37 -08003279 __dlm_unlink_mle(dlm, mle);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003280 atomic_set(&mle->woken, 1);
3281 spin_unlock(&mle->spinlock);
3282 wake_up(&mle->wq);
3283
Kurt Hackelaa852352006-04-27 19:04:49 -07003284 mlog(0, "%s: node %u died during migration from "
3285 "%u to %u!\n", dlm->name, dead_node,
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003286 mle->master, mle->new_master);
3287 /* if there is a lockres associated with this
3288 * mle, find it and set its owner to UNKNOWN */
Mark Fasheha3d33292006-03-09 17:55:56 -08003289 hash = dlm_lockid_hash(mle->u.name.name, mle->u.name.len);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003290 res = __dlm_lookup_lockres(dlm, mle->u.name.name,
Mark Fasheha3d33292006-03-09 17:55:56 -08003291 mle->u.name.len, hash);
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003292 if (res) {
3293 /* unfortunately if we hit this rare case, our
3294 * lock ordering is messed. we need to drop
3295 * the master lock so that we can take the
3296 * lockres lock, meaning that we will have to
3297 * restart from the head of list. */
3298 spin_unlock(&dlm->master_lock);
3299
3300 /* move lockres onto recovery list */
3301 spin_lock(&res->spinlock);
3302 dlm_set_lockres_owner(dlm, res,
3303 DLM_LOCK_RES_OWNER_UNKNOWN);
3304 dlm_move_lockres_to_recovery_list(dlm, res);
3305 spin_unlock(&res->spinlock);
3306 dlm_lockres_put(res);
3307
Kurt Hackelf671c092006-02-14 11:45:21 -08003308 /* about to get rid of mle, detach from heartbeat */
3309 __dlm_mle_detach_hb_events(dlm, mle);
3310
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003311 /* dump the mle */
3312 spin_lock(&dlm->master_lock);
3313 __dlm_put_mle(mle);
3314 spin_unlock(&dlm->master_lock);
3315
3316 /* restart */
3317 goto top;
3318 }
3319
3320 /* this may be the last reference */
3321 __dlm_put_mle(mle);
3322 }
3323 spin_unlock(&dlm->master_lock);
3324}
3325
3326
3327int dlm_finish_migration(struct dlm_ctxt *dlm, struct dlm_lock_resource *res,
3328 u8 old_master)
3329{
3330 struct dlm_node_iter iter;
3331 int ret = 0;
3332
3333 spin_lock(&dlm->spinlock);
3334 dlm_node_iter_init(dlm->domain_map, &iter);
3335 clear_bit(old_master, iter.node_map);
3336 clear_bit(dlm->node_num, iter.node_map);
3337 spin_unlock(&dlm->spinlock);
3338
Kurt Hackelba2bf212006-12-01 14:47:20 -08003339 /* ownership of the lockres is changing. account for the
3340 * mastery reference here since old_master will briefly have
3341 * a reference after the migration completes */
3342 spin_lock(&res->spinlock);
3343 dlm_lockres_set_refmap_bit(old_master, res);
3344 spin_unlock(&res->spinlock);
3345
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003346 mlog(0, "now time to do a migrate request to other nodes\n");
3347 ret = dlm_do_migrate_request(dlm, res, old_master,
3348 dlm->node_num, &iter);
3349 if (ret < 0) {
3350 mlog_errno(ret);
3351 goto leave;
3352 }
3353
3354 mlog(0, "doing assert master of %.*s to all except the original node\n",
3355 res->lockname.len, res->lockname.name);
3356 /* this call now finishes out the nodemap
3357 * even if one or more nodes die */
Kurt Hackelba2bf212006-12-01 14:47:20 -08003358 ret = dlm_do_assert_master(dlm, res, iter.node_map,
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003359 DLM_ASSERT_MASTER_FINISH_MIGRATION);
3360 if (ret < 0) {
3361 /* no longer need to retry. all living nodes contacted. */
3362 mlog_errno(ret);
3363 ret = 0;
3364 }
3365
3366 memset(iter.node_map, 0, sizeof(iter.node_map));
3367 set_bit(old_master, iter.node_map);
3368 mlog(0, "doing assert master of %.*s back to %u\n",
3369 res->lockname.len, res->lockname.name, old_master);
Kurt Hackelba2bf212006-12-01 14:47:20 -08003370 ret = dlm_do_assert_master(dlm, res, iter.node_map,
Kurt Hackel6714d8e2005-12-15 14:31:23 -08003371 DLM_ASSERT_MASTER_FINISH_MIGRATION);
3372 if (ret < 0) {
3373 mlog(0, "assert master to original master failed "
3374 "with %d.\n", ret);
3375 /* the only nonzero status here would be because of
3376 * a dead original node. we're done. */
3377 ret = 0;
3378 }
3379
3380 /* all done, set the owner, clear the flag */
3381 spin_lock(&res->spinlock);
3382 dlm_set_lockres_owner(dlm, res, dlm->node_num);
3383 res->state &= ~DLM_LOCK_RES_MIGRATING;
3384 spin_unlock(&res->spinlock);
3385 /* re-dirty it on the new master */
3386 dlm_kick_thread(dlm, res);
3387 wake_up(&res->wq);
3388leave:
3389 return ret;
3390}
3391
3392/*
3393 * LOCKRES AST REFCOUNT
3394 * this is integral to migration
3395 */
3396
3397/* for future intent to call an ast, reserve one ahead of time.
3398 * this should be called only after waiting on the lockres
3399 * with dlm_wait_on_lockres, and while still holding the
3400 * spinlock after the call. */
3401void __dlm_lockres_reserve_ast(struct dlm_lock_resource *res)
3402{
3403 assert_spin_locked(&res->spinlock);
3404 if (res->state & DLM_LOCK_RES_MIGRATING) {
3405 __dlm_print_one_lock_resource(res);
3406 }
3407 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3408
3409 atomic_inc(&res->asts_reserved);
3410}
3411
3412/*
3413 * used to drop the reserved ast, either because it went unused,
3414 * or because the ast/bast was actually called.
3415 *
3416 * also, if there is a pending migration on this lockres,
3417 * and this was the last pending ast on the lockres,
3418 * atomically set the MIGRATING flag before we drop the lock.
3419 * this is how we ensure that migration can proceed with no
3420 * asts in progress. note that it is ok if the state of the
3421 * queues is such that a lock should be granted in the future
3422 * or that a bast should be fired, because the new master will
3423 * shuffle the lists on this lockres as soon as it is migrated.
3424 */
3425void dlm_lockres_release_ast(struct dlm_ctxt *dlm,
3426 struct dlm_lock_resource *res)
3427{
3428 if (!atomic_dec_and_lock(&res->asts_reserved, &res->spinlock))
3429 return;
3430
3431 if (!res->migration_pending) {
3432 spin_unlock(&res->spinlock);
3433 return;
3434 }
3435
3436 BUG_ON(res->state & DLM_LOCK_RES_MIGRATING);
3437 res->migration_pending = 0;
3438 res->state |= DLM_LOCK_RES_MIGRATING;
3439 spin_unlock(&res->spinlock);
3440 wake_up(&res->wq);
3441 wake_up(&dlm->migration_wq);
3442}