xref: /linux/fs/dlm/requestqueue.c (revision 6d40c4a708e0e996fd9c60d4093aebba5fe1f749)
1e7fd4179SDavid Teigland /******************************************************************************
2e7fd4179SDavid Teigland *******************************************************************************
3e7fd4179SDavid Teigland **
4c36258b5SDavid Teigland **  Copyright (C) 2005-2007 Red Hat, Inc.  All rights reserved.
5e7fd4179SDavid Teigland **
6e7fd4179SDavid Teigland **  This copyrighted material is made available to anyone wishing to use,
7e7fd4179SDavid Teigland **  modify, copy, or redistribute it subject to the terms and conditions
8e7fd4179SDavid Teigland **  of the GNU General Public License v.2.
9e7fd4179SDavid Teigland **
10e7fd4179SDavid Teigland *******************************************************************************
11e7fd4179SDavid Teigland ******************************************************************************/
12e7fd4179SDavid Teigland 
13e7fd4179SDavid Teigland #include "dlm_internal.h"
14e7fd4179SDavid Teigland #include "member.h"
15e7fd4179SDavid Teigland #include "lock.h"
16e7fd4179SDavid Teigland #include "dir.h"
17e7fd4179SDavid Teigland #include "config.h"
18e7fd4179SDavid Teigland #include "requestqueue.h"
19e7fd4179SDavid Teigland 
20e7fd4179SDavid Teigland struct rq_entry {
21e7fd4179SDavid Teigland 	struct list_head list;
22*6d40c4a7SDavid Teigland 	uint32_t recover_seq;
23e7fd4179SDavid Teigland 	int nodeid;
248b0d8e03SAl Viro 	struct dlm_message request;
25e7fd4179SDavid Teigland };
26e7fd4179SDavid Teigland 
27e7fd4179SDavid Teigland /*
28e7fd4179SDavid Teigland  * Requests received while the lockspace is in recovery get added to the
29e7fd4179SDavid Teigland  * request queue and processed when recovery is complete.  This happens when
30e7fd4179SDavid Teigland  * the lockspace is suspended on some nodes before it is on others, or the
31e7fd4179SDavid Teigland  * lockspace is enabled on some while still suspended on others.
32e7fd4179SDavid Teigland  */
33e7fd4179SDavid Teigland 
348b0d8e03SAl Viro void dlm_add_requestqueue(struct dlm_ls *ls, int nodeid, struct dlm_message *ms)
35e7fd4179SDavid Teigland {
36e7fd4179SDavid Teigland 	struct rq_entry *e;
378b0d8e03SAl Viro 	int length = ms->m_header.h_length - sizeof(struct dlm_message);
38e7fd4179SDavid Teigland 
39573c24c4SDavid Teigland 	e = kmalloc(sizeof(struct rq_entry) + length, GFP_NOFS);
40e7fd4179SDavid Teigland 	if (!e) {
41c36258b5SDavid Teigland 		log_print("dlm_add_requestqueue: out of memory len %d", length);
42c36258b5SDavid Teigland 		return;
43e7fd4179SDavid Teigland 	}
44e7fd4179SDavid Teigland 
45*6d40c4a7SDavid Teigland 	e->recover_seq = ls->ls_recover_seq & 0xFFFFFFFF;
46e7fd4179SDavid Teigland 	e->nodeid = nodeid;
478b0d8e03SAl Viro 	memcpy(&e->request, ms, ms->m_header.h_length);
48e7fd4179SDavid Teigland 
4990135925SDavid Teigland 	mutex_lock(&ls->ls_requestqueue_mutex);
50e7fd4179SDavid Teigland 	list_add_tail(&e->list, &ls->ls_requestqueue);
5190135925SDavid Teigland 	mutex_unlock(&ls->ls_requestqueue_mutex);
52e7fd4179SDavid Teigland }
53e7fd4179SDavid Teigland 
54c36258b5SDavid Teigland /*
55c36258b5SDavid Teigland  * Called by dlm_recoverd to process normal messages saved while recovery was
56c36258b5SDavid Teigland  * happening.  Normal locking has been enabled before this is called.  dlm_recv
57c36258b5SDavid Teigland  * upon receiving a message, will wait for all saved messages to be drained
58c36258b5SDavid Teigland  * here before processing the message it got.  If a new dlm_ls_stop() arrives
59c36258b5SDavid Teigland  * while we're processing these saved messages, it may block trying to suspend
60c36258b5SDavid Teigland  * dlm_recv if dlm_recv is waiting for us in dlm_wait_requestqueue.  In that
61c36258b5SDavid Teigland  * case, we don't abort since locking_stopped is still 0.  If dlm_recv is not
62c36258b5SDavid Teigland  * waiting for us, then this processing may be aborted due to locking_stopped.
63c36258b5SDavid Teigland  */
64c36258b5SDavid Teigland 
65e7fd4179SDavid Teigland int dlm_process_requestqueue(struct dlm_ls *ls)
66e7fd4179SDavid Teigland {
67e7fd4179SDavid Teigland 	struct rq_entry *e;
68e7fd4179SDavid Teigland 	int error = 0;
69e7fd4179SDavid Teigland 
7090135925SDavid Teigland 	mutex_lock(&ls->ls_requestqueue_mutex);
71e7fd4179SDavid Teigland 
72e7fd4179SDavid Teigland 	for (;;) {
73e7fd4179SDavid Teigland 		if (list_empty(&ls->ls_requestqueue)) {
7490135925SDavid Teigland 			mutex_unlock(&ls->ls_requestqueue_mutex);
75e7fd4179SDavid Teigland 			error = 0;
76e7fd4179SDavid Teigland 			break;
77e7fd4179SDavid Teigland 		}
78e7fd4179SDavid Teigland 		e = list_entry(ls->ls_requestqueue.next, struct rq_entry, list);
7990135925SDavid Teigland 		mutex_unlock(&ls->ls_requestqueue_mutex);
80e7fd4179SDavid Teigland 
81*6d40c4a7SDavid Teigland 		dlm_receive_message_saved(ls, &e->request, e->recover_seq);
82e7fd4179SDavid Teigland 
8390135925SDavid Teigland 		mutex_lock(&ls->ls_requestqueue_mutex);
84e7fd4179SDavid Teigland 		list_del(&e->list);
85e7fd4179SDavid Teigland 		kfree(e);
86e7fd4179SDavid Teigland 
87e7fd4179SDavid Teigland 		if (dlm_locking_stopped(ls)) {
88e7fd4179SDavid Teigland 			log_debug(ls, "process_requestqueue abort running");
8990135925SDavid Teigland 			mutex_unlock(&ls->ls_requestqueue_mutex);
90e7fd4179SDavid Teigland 			error = -EINTR;
91e7fd4179SDavid Teigland 			break;
92e7fd4179SDavid Teigland 		}
93e7fd4179SDavid Teigland 		schedule();
94e7fd4179SDavid Teigland 	}
95e7fd4179SDavid Teigland 
96e7fd4179SDavid Teigland 	return error;
97e7fd4179SDavid Teigland }
98e7fd4179SDavid Teigland 
99e7fd4179SDavid Teigland /*
100e7fd4179SDavid Teigland  * After recovery is done, locking is resumed and dlm_recoverd takes all the
101c36258b5SDavid Teigland  * saved requests and processes them as they would have been by dlm_recv.  At
102c36258b5SDavid Teigland  * the same time, dlm_recv will start receiving new requests from remote nodes.
103c36258b5SDavid Teigland  * We want to delay dlm_recv processing new requests until dlm_recoverd has
104c36258b5SDavid Teigland  * finished processing the old saved requests.  We don't check for locking
105c36258b5SDavid Teigland  * stopped here because dlm_ls_stop won't stop locking until it's suspended us
106c36258b5SDavid Teigland  * (dlm_recv).
107e7fd4179SDavid Teigland  */
108e7fd4179SDavid Teigland 
109e7fd4179SDavid Teigland void dlm_wait_requestqueue(struct dlm_ls *ls)
110e7fd4179SDavid Teigland {
111e7fd4179SDavid Teigland 	for (;;) {
11290135925SDavid Teigland 		mutex_lock(&ls->ls_requestqueue_mutex);
113e7fd4179SDavid Teigland 		if (list_empty(&ls->ls_requestqueue))
114e7fd4179SDavid Teigland 			break;
11590135925SDavid Teigland 		mutex_unlock(&ls->ls_requestqueue_mutex);
116e7fd4179SDavid Teigland 		schedule();
117e7fd4179SDavid Teigland 	}
11890135925SDavid Teigland 	mutex_unlock(&ls->ls_requestqueue_mutex);
119e7fd4179SDavid Teigland }
120e7fd4179SDavid Teigland 
121e7fd4179SDavid Teigland static int purge_request(struct dlm_ls *ls, struct dlm_message *ms, int nodeid)
122e7fd4179SDavid Teigland {
123e7fd4179SDavid Teigland 	uint32_t type = ms->m_type;
124e7fd4179SDavid Teigland 
1252896ee37SDavid Teigland 	/* the ls is being cleaned up and freed by release_lockspace */
1262896ee37SDavid Teigland 	if (!ls->ls_count)
1272896ee37SDavid Teigland 		return 1;
1282896ee37SDavid Teigland 
129e7fd4179SDavid Teigland 	if (dlm_is_removed(ls, nodeid))
130e7fd4179SDavid Teigland 		return 1;
131e7fd4179SDavid Teigland 
132e7fd4179SDavid Teigland 	/* directory operations are always purged because the directory is
133e7fd4179SDavid Teigland 	   always rebuilt during recovery and the lookups resent */
134e7fd4179SDavid Teigland 
135e7fd4179SDavid Teigland 	if (type == DLM_MSG_REMOVE ||
136e7fd4179SDavid Teigland 	    type == DLM_MSG_LOOKUP ||
137e7fd4179SDavid Teigland 	    type == DLM_MSG_LOOKUP_REPLY)
138e7fd4179SDavid Teigland 		return 1;
139e7fd4179SDavid Teigland 
140e7fd4179SDavid Teigland 	if (!dlm_no_directory(ls))
141e7fd4179SDavid Teigland 		return 0;
142e7fd4179SDavid Teigland 
143e7fd4179SDavid Teigland 	/* with no directory, the master is likely to change as a part of
144e7fd4179SDavid Teigland 	   recovery; requests to/from the defunct master need to be purged */
145e7fd4179SDavid Teigland 
146e7fd4179SDavid Teigland 	switch (type) {
147e7fd4179SDavid Teigland 	case DLM_MSG_REQUEST:
148e7fd4179SDavid Teigland 	case DLM_MSG_CONVERT:
149e7fd4179SDavid Teigland 	case DLM_MSG_UNLOCK:
150e7fd4179SDavid Teigland 	case DLM_MSG_CANCEL:
151e7fd4179SDavid Teigland 		/* we're no longer the master of this resource, the sender
152e7fd4179SDavid Teigland 		   will resend to the new master (see waiter_needs_recovery) */
153e7fd4179SDavid Teigland 
154e7fd4179SDavid Teigland 		if (dlm_hash2nodeid(ls, ms->m_hash) != dlm_our_nodeid())
155e7fd4179SDavid Teigland 			return 1;
156e7fd4179SDavid Teigland 		break;
157e7fd4179SDavid Teigland 
158e7fd4179SDavid Teigland 	case DLM_MSG_REQUEST_REPLY:
159e7fd4179SDavid Teigland 	case DLM_MSG_CONVERT_REPLY:
160e7fd4179SDavid Teigland 	case DLM_MSG_UNLOCK_REPLY:
161e7fd4179SDavid Teigland 	case DLM_MSG_CANCEL_REPLY:
162e7fd4179SDavid Teigland 	case DLM_MSG_GRANT:
163e7fd4179SDavid Teigland 		/* this reply is from the former master of the resource,
164e7fd4179SDavid Teigland 		   we'll resend to the new master if needed */
165e7fd4179SDavid Teigland 
166e7fd4179SDavid Teigland 		if (dlm_hash2nodeid(ls, ms->m_hash) != nodeid)
167e7fd4179SDavid Teigland 			return 1;
168e7fd4179SDavid Teigland 		break;
169e7fd4179SDavid Teigland 	}
170e7fd4179SDavid Teigland 
171e7fd4179SDavid Teigland 	return 0;
172e7fd4179SDavid Teigland }
173e7fd4179SDavid Teigland 
174e7fd4179SDavid Teigland void dlm_purge_requestqueue(struct dlm_ls *ls)
175e7fd4179SDavid Teigland {
176e7fd4179SDavid Teigland 	struct dlm_message *ms;
177e7fd4179SDavid Teigland 	struct rq_entry *e, *safe;
178e7fd4179SDavid Teigland 
17990135925SDavid Teigland 	mutex_lock(&ls->ls_requestqueue_mutex);
180e7fd4179SDavid Teigland 	list_for_each_entry_safe(e, safe, &ls->ls_requestqueue, list) {
1818b0d8e03SAl Viro 		ms =  &e->request;
182e7fd4179SDavid Teigland 
183e7fd4179SDavid Teigland 		if (purge_request(ls, ms, e->nodeid)) {
184e7fd4179SDavid Teigland 			list_del(&e->list);
185e7fd4179SDavid Teigland 			kfree(e);
186e7fd4179SDavid Teigland 		}
187e7fd4179SDavid Teigland 	}
18890135925SDavid Teigland 	mutex_unlock(&ls->ls_requestqueue_mutex);
189e7fd4179SDavid Teigland }
190e7fd4179SDavid Teigland 
191