📄 dlmdomain.c
字号:
/* -*- mode: c; c-basic-offset: 8; -*- * vim: noexpandtab sw=8 ts=8 sts=0: * * dlmdomain.c * * defines domain join / leave apis * * Copyright (C) 2004 Oracle. All rights reserved. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 021110-1307, USA. * */#include <linux/module.h>#include <linux/types.h>#include <linux/slab.h>#include <linux/highmem.h>#include <linux/utsname.h>#include <linux/init.h>#include <linux/spinlock.h>#include <linux/delay.h>#include <linux/err.h>#include "cluster/heartbeat.h"#include "cluster/nodemanager.h"#include "cluster/tcp.h"#include "dlmapi.h"#include "dlmcommon.h"#include "dlmdebug.h"#include "dlmdomain.h"#include "dlmver.h"#define MLOG_MASK_PREFIX (ML_DLM|ML_DLM_DOMAIN)#include "cluster/masklog.h"/* * * spinlock lock ordering: if multiple locks are needed, obey this ordering: * dlm_domain_lock * struct dlm_ctxt->spinlock * struct dlm_lock_resource->spinlock * struct dlm_ctxt->master_lock * struct dlm_ctxt->ast_lock * dlm_master_list_entry->spinlock * dlm_lock->spinlock * */spinlock_t dlm_domain_lock = SPIN_LOCK_UNLOCKED;LIST_HEAD(dlm_domains);static DECLARE_WAIT_QUEUE_HEAD(dlm_domain_events);#define DLM_DOMAIN_BACKOFF_MS 200static int dlm_query_join_handler(struct o2net_msg *msg, u32 len, void *data);static int dlm_assert_joined_handler(struct o2net_msg *msg, u32 len, void *data);static int dlm_cancel_join_handler(struct o2net_msg *msg, u32 len, void *data);static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data);static void dlm_unregister_domain_handlers(struct dlm_ctxt *dlm);void __dlm_unhash_lockres(struct dlm_lock_resource *lockres){ hlist_del_init(&lockres->hash_node); dlm_lockres_put(lockres);}void __dlm_insert_lockres(struct dlm_ctxt *dlm, struct dlm_lock_resource *res){ struct hlist_head *bucket; struct qstr *q; assert_spin_locked(&dlm->spinlock); q = &res->lockname; q->hash = full_name_hash(q->name, q->len); bucket = &(dlm->lockres_hash[q->hash % DLM_HASH_BUCKETS]); /* get a reference for our hashtable */ dlm_lockres_get(res); hlist_add_head(&res->hash_node, bucket);}struct dlm_lock_resource * __dlm_lookup_lockres(struct dlm_ctxt *dlm, const char *name, unsigned int len){ unsigned int hash; struct hlist_node *iter; struct dlm_lock_resource *tmpres=NULL; struct hlist_head *bucket; mlog_entry("%.*s\n", len, name); assert_spin_locked(&dlm->spinlock); hash = full_name_hash(name, len); bucket = &(dlm->lockres_hash[hash % DLM_HASH_BUCKETS]); /* check for pre-existing lock */ hlist_for_each(iter, bucket) { tmpres = hlist_entry(iter, struct dlm_lock_resource, hash_node); if (tmpres->lockname.len == len && memcmp(tmpres->lockname.name, name, len) == 0) { dlm_lockres_get(tmpres); break; } tmpres = NULL; } return tmpres;}struct dlm_lock_resource * dlm_lookup_lockres(struct dlm_ctxt *dlm, const char *name, unsigned int len){ struct dlm_lock_resource *res; spin_lock(&dlm->spinlock); res = __dlm_lookup_lockres(dlm, name, len); spin_unlock(&dlm->spinlock); return res;}static struct dlm_ctxt * __dlm_lookup_domain_full(const char *domain, int len){ struct dlm_ctxt *tmp = NULL; struct list_head *iter; assert_spin_locked(&dlm_domain_lock); /* tmp->name here is always NULL terminated, * but domain may not be! */ list_for_each(iter, &dlm_domains) { tmp = list_entry (iter, struct dlm_ctxt, list); if (strlen(tmp->name) == len && memcmp(tmp->name, domain, len)==0) break; tmp = NULL; } return tmp;}/* For null terminated domain strings ONLY */static struct dlm_ctxt * __dlm_lookup_domain(const char *domain){ assert_spin_locked(&dlm_domain_lock); return __dlm_lookup_domain_full(domain, strlen(domain));}/* returns true on one of two conditions: * 1) the domain does not exist * 2) the domain exists and it's state is "joined" */static int dlm_wait_on_domain_helper(const char *domain){ int ret = 0; struct dlm_ctxt *tmp = NULL; spin_lock(&dlm_domain_lock); tmp = __dlm_lookup_domain(domain); if (!tmp) ret = 1; else if (tmp->dlm_state == DLM_CTXT_JOINED) ret = 1; spin_unlock(&dlm_domain_lock); return ret;}static void dlm_free_ctxt_mem(struct dlm_ctxt *dlm){ if (dlm->lockres_hash) free_page((unsigned long) dlm->lockres_hash); if (dlm->name) kfree(dlm->name); kfree(dlm);}/* A little strange - this function will be called while holding * dlm_domain_lock and is expected to be holding it on the way out. We * will however drop and reacquire it multiple times */static void dlm_ctxt_release(struct kref *kref){ struct dlm_ctxt *dlm; dlm = container_of(kref, struct dlm_ctxt, dlm_refs); BUG_ON(dlm->num_joins); BUG_ON(dlm->dlm_state == DLM_CTXT_JOINED); /* we may still be in the list if we hit an error during join. */ list_del_init(&dlm->list); spin_unlock(&dlm_domain_lock); mlog(0, "freeing memory from domain %s\n", dlm->name); wake_up(&dlm_domain_events); dlm_free_ctxt_mem(dlm); spin_lock(&dlm_domain_lock);}void dlm_put(struct dlm_ctxt *dlm){ spin_lock(&dlm_domain_lock); kref_put(&dlm->dlm_refs, dlm_ctxt_release); spin_unlock(&dlm_domain_lock);}static void __dlm_get(struct dlm_ctxt *dlm){ kref_get(&dlm->dlm_refs);}/* given a questionable reference to a dlm object, gets a reference if * it can find it in the list, otherwise returns NULL in which case * you shouldn't trust your pointer. */struct dlm_ctxt *dlm_grab(struct dlm_ctxt *dlm){ struct list_head *iter; struct dlm_ctxt *target = NULL; spin_lock(&dlm_domain_lock); list_for_each(iter, &dlm_domains) { target = list_entry (iter, struct dlm_ctxt, list); if (target == dlm) { __dlm_get(target); break; } target = NULL; } spin_unlock(&dlm_domain_lock); return target;}int dlm_domain_fully_joined(struct dlm_ctxt *dlm){ int ret; spin_lock(&dlm_domain_lock); ret = (dlm->dlm_state == DLM_CTXT_JOINED) || (dlm->dlm_state == DLM_CTXT_IN_SHUTDOWN); spin_unlock(&dlm_domain_lock); return ret;}static void dlm_complete_dlm_shutdown(struct dlm_ctxt *dlm){ dlm_unregister_domain_handlers(dlm); dlm_complete_thread(dlm); dlm_complete_recovery_thread(dlm); /* We've left the domain. Now we can take ourselves out of the * list and allow the kref stuff to help us free the * memory. */ spin_lock(&dlm_domain_lock); list_del_init(&dlm->list); spin_unlock(&dlm_domain_lock); /* Wake up anyone waiting for us to remove this domain */ wake_up(&dlm_domain_events);}static void dlm_migrate_all_locks(struct dlm_ctxt *dlm){ int i; struct dlm_lock_resource *res; mlog(0, "Migrating locks from domain %s\n", dlm->name);restart: spin_lock(&dlm->spinlock); for (i = 0; i < DLM_HASH_BUCKETS; i++) { while (!hlist_empty(&dlm->lockres_hash[i])) { res = hlist_entry(dlm->lockres_hash[i].first, struct dlm_lock_resource, hash_node); /* need reference when manually grabbing lockres */ dlm_lockres_get(res); /* this should unhash the lockres * and exit with dlm->spinlock */ mlog(0, "purging res=%p\n", res); if (dlm_lockres_is_dirty(dlm, res)) { /* HACK! this should absolutely go. * need to figure out why some empty * lockreses are still marked dirty */ mlog(ML_ERROR, "lockres %.*s dirty!\n", res->lockname.len, res->lockname.name); spin_unlock(&dlm->spinlock); dlm_kick_thread(dlm, res); wait_event(dlm->ast_wq, !dlm_lockres_is_dirty(dlm, res)); dlm_lockres_put(res); goto restart; } dlm_purge_lockres(dlm, res); dlm_lockres_put(res); } } spin_unlock(&dlm->spinlock); mlog(0, "DONE Migrating locks from domain %s\n", dlm->name);}static int dlm_no_joining_node(struct dlm_ctxt *dlm){ int ret; spin_lock(&dlm->spinlock); ret = dlm->joining_node == DLM_LOCK_RES_OWNER_UNKNOWN; spin_unlock(&dlm->spinlock); return ret;}static void dlm_mark_domain_leaving(struct dlm_ctxt *dlm){ /* Yikes, a double spinlock! I need domain_lock for the dlm * state and the dlm spinlock for join state... Sorry! */again: spin_lock(&dlm_domain_lock); spin_lock(&dlm->spinlock); if (dlm->joining_node != DLM_LOCK_RES_OWNER_UNKNOWN) { mlog(0, "Node %d is joining, we wait on it.\n", dlm->joining_node); spin_unlock(&dlm->spinlock); spin_unlock(&dlm_domain_lock); wait_event(dlm->dlm_join_events, dlm_no_joining_node(dlm)); goto again; } dlm->dlm_state = DLM_CTXT_LEAVING; spin_unlock(&dlm->spinlock); spin_unlock(&dlm_domain_lock);}static void __dlm_print_nodes(struct dlm_ctxt *dlm){ int node = -1; assert_spin_locked(&dlm->spinlock); mlog(ML_NOTICE, "Nodes in my domain (\"%s\"):\n", dlm->name); while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, node + 1)) < O2NM_MAX_NODES) { mlog(ML_NOTICE, " node %d\n", node); }}static int dlm_exit_domain_handler(struct o2net_msg *msg, u32 len, void *data){ struct dlm_ctxt *dlm = data; unsigned int node; struct dlm_exit_domain *exit_msg = (struct dlm_exit_domain *) msg->buf; mlog_entry("%p %u %p", msg, len, data); if (!dlm_grab(dlm)) return 0; node = exit_msg->node_idx; mlog(0, "Node %u leaves domain %s\n", node, dlm->name); spin_lock(&dlm->spinlock); clear_bit(node, dlm->domain_map); __dlm_print_nodes(dlm); /* notify anything attached to the heartbeat events */ dlm_hb_event_notify_attached(dlm, node, 0); spin_unlock(&dlm->spinlock); dlm_put(dlm); return 0;}static int dlm_send_one_domain_exit(struct dlm_ctxt *dlm, unsigned int node){ int status; struct dlm_exit_domain leave_msg; mlog(0, "Asking node %u if we can leave the domain %s me = %u\n", node, dlm->name, dlm->node_num); memset(&leave_msg, 0, sizeof(leave_msg)); leave_msg.node_idx = dlm->node_num; status = o2net_send_message(DLM_EXIT_DOMAIN_MSG, dlm->key, &leave_msg, sizeof(leave_msg), node, NULL); mlog(0, "status return %d from o2net_send_message\n", status); return status;}static void dlm_leave_domain(struct dlm_ctxt *dlm){ int node, clear_node, status; /* At this point we've migrated away all our locks and won't * accept mastership of new ones. The dlm is responsible for * almost nothing now. We make sure not to confuse any joining * nodes and then commence shutdown procedure. */ spin_lock(&dlm->spinlock); /* Clear ourselves from the domain map */ clear_bit(dlm->node_num, dlm->domain_map); while ((node = find_next_bit(dlm->domain_map, O2NM_MAX_NODES, 0)) < O2NM_MAX_NODES) { /* Drop the dlm spinlock. This is safe wrt the domain_map. * -nodes cannot be added now as the * query_join_handlers knows to respond with OK_NO_MAP * -we catch the right network errors if a node is * removed from the map while we're sending him the * exit message. */ spin_unlock(&dlm->spinlock); clear_node = 1; status = dlm_send_one_domain_exit(dlm, node); if (status < 0 && status != -ENOPROTOOPT && status != -ENOTCONN) { mlog(ML_NOTICE, "Error %d sending domain exit message " "to node %d\n", status, node); /* Not sure what to do here but lets sleep for * a bit in case this was a transient * error... */ msleep(DLM_DOMAIN_BACKOFF_MS); clear_node = 0; } spin_lock(&dlm->spinlock); /* If we're not clearing the node bit then we intend * to loop back around to try again. */ if (clear_node) clear_bit(node, dlm->domain_map); } spin_unlock(&dlm->spinlock);}int dlm_joined(struct dlm_ctxt *dlm){ int ret = 0; spin_lock(&dlm_domain_lock); if (dlm->dlm_state == DLM_CTXT_JOINED) ret = 1; spin_unlock(&dlm_domain_lock); return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -