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