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