xref: /linux/fs/dlm/requestqueue.c (revision 41e0d49104dbff888ef6446ea46842fde66c0a76)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /******************************************************************************
3 *******************************************************************************
4 **
5 **  Copyright (C) 2005-2007 Red Hat, Inc.  All rights reserved.
6 **
7 **
8 *******************************************************************************
9 ******************************************************************************/
10 
11 #include "dlm_internal.h"
12 #include "member.h"
13 #include "lock.h"
14 #include "dir.h"
15 #include "config.h"
16 #include "requestqueue.h"
17 #include "util.h"
18 
19 struct rq_entry {
20 	struct list_head list;
21 	uint32_t recover_seq;
22 	int nodeid;
23 	struct dlm_message request;
24 };
25 
26 /*
27  * Requests received while the lockspace is in recovery get added to the
28  * request queue and processed when recovery is complete.  This happens when
29  * the lockspace is suspended on some nodes before it is on others, or the
30  * lockspace is enabled on some while still suspended on others.
31  */
32 
33 void dlm_add_requestqueue(struct dlm_ls *ls, int nodeid, struct dlm_message *ms)
34 {
35 	struct rq_entry *e;
36 	int length = le16_to_cpu(ms->m_header.h_length) -
37 		sizeof(struct dlm_message);
38 
39 	e = kmalloc(sizeof(struct rq_entry) + length, GFP_NOFS);
40 	if (!e) {
41 		log_print("dlm_add_requestqueue: out of memory len %d", length);
42 		return;
43 	}
44 
45 	e->recover_seq = ls->ls_recover_seq & 0xFFFFFFFF;
46 	e->nodeid = nodeid;
47 	memcpy(&e->request, ms, sizeof(*ms));
48 	memcpy(&e->request.m_extra, ms->m_extra, length);
49 
50 	atomic_inc(&ls->ls_requestqueue_cnt);
51 	mutex_lock(&ls->ls_requestqueue_mutex);
52 	list_add_tail(&e->list, &ls->ls_requestqueue);
53 	mutex_unlock(&ls->ls_requestqueue_mutex);
54 }
55 
56 /*
57  * Called by dlm_recoverd to process normal messages saved while recovery was
58  * happening.  Normal locking has been enabled before this is called.  dlm_recv
59  * upon receiving a message, will wait for all saved messages to be drained
60  * here before processing the message it got.  If a new dlm_ls_stop() arrives
61  * while we're processing these saved messages, it may block trying to suspend
62  * dlm_recv if dlm_recv is waiting for us in dlm_wait_requestqueue.  In that
63  * case, we don't abort since locking_stopped is still 0.  If dlm_recv is not
64  * waiting for us, then this processing may be aborted due to locking_stopped.
65  */
66 
67 int dlm_process_requestqueue(struct dlm_ls *ls)
68 {
69 	struct rq_entry *e;
70 	struct dlm_message *ms;
71 	int error = 0;
72 
73 	mutex_lock(&ls->ls_requestqueue_mutex);
74 
75 	for (;;) {
76 		if (list_empty(&ls->ls_requestqueue)) {
77 			mutex_unlock(&ls->ls_requestqueue_mutex);
78 			error = 0;
79 			break;
80 		}
81 		e = list_entry(ls->ls_requestqueue.next, struct rq_entry, list);
82 		mutex_unlock(&ls->ls_requestqueue_mutex);
83 
84 		ms = &e->request;
85 
86 		log_limit(ls, "dlm_process_requestqueue msg %d from %d "
87 			  "lkid %x remid %x result %d seq %u",
88 			  le32_to_cpu(ms->m_type),
89 			  le32_to_cpu(ms->m_header.h_nodeid),
90 			  le32_to_cpu(ms->m_lkid), le32_to_cpu(ms->m_remid),
91 			  from_dlm_errno(le32_to_cpu(ms->m_result)),
92 			  e->recover_seq);
93 
94 		dlm_receive_message_saved(ls, &e->request, e->recover_seq);
95 
96 		mutex_lock(&ls->ls_requestqueue_mutex);
97 		list_del(&e->list);
98 		if (atomic_dec_and_test(&ls->ls_requestqueue_cnt))
99 			wake_up(&ls->ls_requestqueue_wait);
100 		kfree(e);
101 
102 		if (dlm_locking_stopped(ls)) {
103 			log_debug(ls, "process_requestqueue abort running");
104 			mutex_unlock(&ls->ls_requestqueue_mutex);
105 			error = -EINTR;
106 			break;
107 		}
108 		schedule();
109 	}
110 
111 	return error;
112 }
113 
114 /*
115  * After recovery is done, locking is resumed and dlm_recoverd takes all the
116  * saved requests and processes them as they would have been by dlm_recv.  At
117  * the same time, dlm_recv will start receiving new requests from remote nodes.
118  * We want to delay dlm_recv processing new requests until dlm_recoverd has
119  * finished processing the old saved requests.  We don't check for locking
120  * stopped here because dlm_ls_stop won't stop locking until it's suspended us
121  * (dlm_recv).
122  */
123 
124 void dlm_wait_requestqueue(struct dlm_ls *ls)
125 {
126 	wait_event(ls->ls_requestqueue_wait,
127 		   atomic_read(&ls->ls_requestqueue_cnt) == 0);
128 }
129 
130 static int purge_request(struct dlm_ls *ls, struct dlm_message *ms, int nodeid)
131 {
132 	__le32 type = ms->m_type;
133 
134 	/* the ls is being cleaned up and freed by release_lockspace */
135 	if (!atomic_read(&ls->ls_count))
136 		return 1;
137 
138 	if (dlm_is_removed(ls, nodeid))
139 		return 1;
140 
141 	/* directory operations are always purged because the directory is
142 	   always rebuilt during recovery and the lookups resent */
143 
144 	if (type == cpu_to_le32(DLM_MSG_REMOVE) ||
145 	    type == cpu_to_le32(DLM_MSG_LOOKUP) ||
146 	    type == cpu_to_le32(DLM_MSG_LOOKUP_REPLY))
147 		return 1;
148 
149 	if (!dlm_no_directory(ls))
150 		return 0;
151 
152 	return 1;
153 }
154 
155 void dlm_purge_requestqueue(struct dlm_ls *ls)
156 {
157 	struct dlm_message *ms;
158 	struct rq_entry *e, *safe;
159 
160 	mutex_lock(&ls->ls_requestqueue_mutex);
161 	list_for_each_entry_safe(e, safe, &ls->ls_requestqueue, list) {
162 		ms =  &e->request;
163 
164 		if (purge_request(ls, ms, e->nodeid)) {
165 			list_del(&e->list);
166 			if (atomic_dec_and_test(&ls->ls_requestqueue_cnt))
167 				wake_up(&ls->ls_requestqueue_wait);
168 			kfree(e);
169 		}
170 	}
171 	mutex_unlock(&ls->ls_requestqueue_mutex);
172 }
173 
174