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