/******************************************************************************1*******************************************************************************2**3** Copyright (C) 2005-2007 Red Hat, Inc. All rights reserved.4**5** This copyrighted material is made available to anyone wishing to use,6** modify, copy, or redistribute it subject to the terms and conditions7** of the GNU General Public License v.2.8**9*******************************************************************************10******************************************************************************/1112#include "dlm_internal.h"13#include "member.h"14#include "lock.h"15#include "dir.h"16#include "config.h"17#include "requestqueue.h"1819struct rq_entry {20struct list_head list;21int nodeid;22struct dlm_message request;23};2425/*26* Requests received while the lockspace is in recovery get added to the27* request queue and processed when recovery is complete. This happens when28* the lockspace is suspended on some nodes before it is on others, or the29* lockspace is enabled on some while still suspended on others.30*/3132void dlm_add_requestqueue(struct dlm_ls *ls, int nodeid, struct dlm_message *ms)33{34struct rq_entry *e;35int length = ms->m_header.h_length - sizeof(struct dlm_message);3637e = kmalloc(sizeof(struct rq_entry) + length, GFP_NOFS);38if (!e) {39log_print("dlm_add_requestqueue: out of memory len %d", length);40return;41}4243e->nodeid = nodeid;44memcpy(&e->request, ms, ms->m_header.h_length);4546mutex_lock(&ls->ls_requestqueue_mutex);47list_add_tail(&e->list, &ls->ls_requestqueue);48mutex_unlock(&ls->ls_requestqueue_mutex);49}5051/*52* Called by dlm_recoverd to process normal messages saved while recovery was53* happening. Normal locking has been enabled before this is called. dlm_recv54* upon receiving a message, will wait for all saved messages to be drained55* here before processing the message it got. If a new dlm_ls_stop() arrives56* while we're processing these saved messages, it may block trying to suspend57* dlm_recv if dlm_recv is waiting for us in dlm_wait_requestqueue. In that58* case, we don't abort since locking_stopped is still 0. If dlm_recv is not59* waiting for us, then this processing may be aborted due to locking_stopped.60*/6162int dlm_process_requestqueue(struct dlm_ls *ls)63{64struct rq_entry *e;65int error = 0;6667mutex_lock(&ls->ls_requestqueue_mutex);6869for (;;) {70if (list_empty(&ls->ls_requestqueue)) {71mutex_unlock(&ls->ls_requestqueue_mutex);72error = 0;73break;74}75e = list_entry(ls->ls_requestqueue.next, struct rq_entry, list);76mutex_unlock(&ls->ls_requestqueue_mutex);7778dlm_receive_message_saved(ls, &e->request);7980mutex_lock(&ls->ls_requestqueue_mutex);81list_del(&e->list);82kfree(e);8384if (dlm_locking_stopped(ls)) {85log_debug(ls, "process_requestqueue abort running");86mutex_unlock(&ls->ls_requestqueue_mutex);87error = -EINTR;88break;89}90schedule();91}9293return error;94}9596/*97* After recovery is done, locking is resumed and dlm_recoverd takes all the98* saved requests and processes them as they would have been by dlm_recv. At99* the same time, dlm_recv will start receiving new requests from remote nodes.100* We want to delay dlm_recv processing new requests until dlm_recoverd has101* finished processing the old saved requests. We don't check for locking102* stopped here because dlm_ls_stop won't stop locking until it's suspended us103* (dlm_recv).104*/105106void dlm_wait_requestqueue(struct dlm_ls *ls)107{108for (;;) {109mutex_lock(&ls->ls_requestqueue_mutex);110if (list_empty(&ls->ls_requestqueue))111break;112mutex_unlock(&ls->ls_requestqueue_mutex);113schedule();114}115mutex_unlock(&ls->ls_requestqueue_mutex);116}117118static int purge_request(struct dlm_ls *ls, struct dlm_message *ms, int nodeid)119{120uint32_t type = ms->m_type;121122/* the ls is being cleaned up and freed by release_lockspace */123if (!ls->ls_count)124return 1;125126if (dlm_is_removed(ls, nodeid))127return 1;128129/* directory operations are always purged because the directory is130always rebuilt during recovery and the lookups resent */131132if (type == DLM_MSG_REMOVE ||133type == DLM_MSG_LOOKUP ||134type == DLM_MSG_LOOKUP_REPLY)135return 1;136137if (!dlm_no_directory(ls))138return 0;139140/* with no directory, the master is likely to change as a part of141recovery; requests to/from the defunct master need to be purged */142143switch (type) {144case DLM_MSG_REQUEST:145case DLM_MSG_CONVERT:146case DLM_MSG_UNLOCK:147case DLM_MSG_CANCEL:148/* we're no longer the master of this resource, the sender149will resend to the new master (see waiter_needs_recovery) */150151if (dlm_hash2nodeid(ls, ms->m_hash) != dlm_our_nodeid())152return 1;153break;154155case DLM_MSG_REQUEST_REPLY:156case DLM_MSG_CONVERT_REPLY:157case DLM_MSG_UNLOCK_REPLY:158case DLM_MSG_CANCEL_REPLY:159case DLM_MSG_GRANT:160/* this reply is from the former master of the resource,161we'll resend to the new master if needed */162163if (dlm_hash2nodeid(ls, ms->m_hash) != nodeid)164return 1;165break;166}167168return 0;169}170171void dlm_purge_requestqueue(struct dlm_ls *ls)172{173struct dlm_message *ms;174struct rq_entry *e, *safe;175176mutex_lock(&ls->ls_requestqueue_mutex);177list_for_each_entry_safe(e, safe, &ls->ls_requestqueue, list) {178ms = &e->request;179180if (purge_request(ls, ms, e->nodeid)) {181list_del(&e->list);182kfree(e);183}184}185mutex_unlock(&ls->ls_requestqueue_mutex);186}187188189190