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