License cleanup: add SPDX GPL-2.0 license identifier to files with no license
[linux-block.git] / fs / orangefs / waitqueue.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
1182fca3
MM
2/*
3 * (C) 2001 Clemson University and The University of Chicago
4 * (C) 2011 Omnibond Systems
5 *
6 * Changes by Acxiom Corporation to implement generic service_operation()
7 * function, Copyright Acxiom Corporation, 2005.
8 *
9 * See COPYING in top-level directory.
10 */
11
12/*
13 * In-kernel waitqueue operations.
14 */
15
16#include "protocol.h"
575e9461
MM
17#include "orangefs-kernel.h"
18#include "orangefs-bufmap.h"
1182fca3 19
05b39a8b 20static int wait_for_matching_downcall(struct orangefs_kernel_op_s *, long, bool);
d2d87a3b 21static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *);
ade3d781 22
1182fca3
MM
23/*
24 * What we do in this function is to walk the list of operations that are
25 * present in the request queue and mark them as purged.
26 * NOTE: This is called from the device close after client-core has
27 * guaranteed that no new operations could appear on the list since the
28 * client-core is anyway going to exit.
29 */
30void purge_waiting_ops(void)
31{
8bb8aefd 32 struct orangefs_kernel_op_s *op;
1182fca3 33
8bb8aefd
YL
34 spin_lock(&orangefs_request_list_lock);
35 list_for_each_entry(op, &orangefs_request_list, list) {
1182fca3
MM
36 gossip_debug(GOSSIP_WAIT_DEBUG,
37 "pvfs2-client-core: purging op tag %llu %s\n",
38 llu(op->tag),
39 get_opname_string(op));
1182fca3 40 set_op_state_purged(op);
9d9e7ba9
MM
41 gossip_debug(GOSSIP_DEV_DEBUG,
42 "%s: op:%s: op_state:%d: process:%s:\n",
43 __func__,
44 get_opname_string(op),
45 op->op_state,
46 current->comm);
1182fca3 47 }
8bb8aefd 48 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
49}
50
51/*
8bb8aefd 52 * submits a ORANGEFS operation and waits for it to complete
1182fca3
MM
53 *
54 * Note op->downcall.status will contain the status of the operation (in
55 * errno format), whether provided by pvfs2-client or a result of failure to
56 * service the operation. If the caller wishes to distinguish, then
57 * op->state can be checked to see if it was serviced or not.
58 *
59 * Returns contents of op->downcall.status for convenience
60 */
8bb8aefd 61int service_operation(struct orangefs_kernel_op_s *op,
1182fca3
MM
62 const char *op_name,
63 int flags)
64{
05b39a8b 65 long timeout = MAX_SCHEDULE_TIMEOUT;
1182fca3
MM
66 int ret = 0;
67
ce6c414e 68 DEFINE_WAIT(wait_entry);
1182fca3
MM
69
70 op->upcall.tgid = current->tgid;
71 op->upcall.pid = current->pid;
72
73retry_servicing:
74 op->downcall.status = 0;
75 gossip_debug(GOSSIP_WAIT_DEBUG,
5253487e
MM
76 "%s: %s op:%p: process:%s: pid:%d:\n",
77 __func__,
1182fca3 78 op_name,
5253487e 79 op,
1182fca3
MM
80 current->comm,
81 current->pid);
82
adcf34a2
MM
83 /*
84 * If ORANGEFS_OP_NO_MUTEX was set in flags, we need to avoid
ca9f518e 85 * acquiring the request_mutex because we're servicing a
adcf34a2
MM
86 * high priority remount operation and the request_mutex is
87 * already taken.
88 */
89 if (!(flags & ORANGEFS_OP_NO_MUTEX)) {
c72f15b7 90 if (flags & ORANGEFS_OP_INTERRUPTIBLE)
1d503617 91 ret = mutex_lock_interruptible(&orangefs_request_mutex);
c72f15b7 92 else
1d503617 93 ret = mutex_lock_killable(&orangefs_request_mutex);
1182fca3
MM
94 /*
95 * check to see if we were interrupted while waiting for
adcf34a2 96 * mutex
1182fca3
MM
97 */
98 if (ret < 0) {
1182fca3
MM
99 op->downcall.status = ret;
100 gossip_debug(GOSSIP_WAIT_DEBUG,
ca9f518e
MM
101 "%s: service_operation interrupted.\n",
102 __func__);
1182fca3
MM
103 return ret;
104 }
105 }
106
98815ade
AV
107 /* queue up the operation */
108 spin_lock(&orangefs_request_list_lock);
109 spin_lock(&op->lock);
110 set_op_state_waiting(op);
9d9e7ba9
MM
111 gossip_debug(GOSSIP_DEV_DEBUG,
112 "%s: op:%s: op_state:%d: process:%s:\n",
113 __func__,
114 get_opname_string(op),
115 op->op_state,
116 current->comm);
adcf34a2 117 /* add high priority remount op to the front of the line. */
98815ade
AV
118 if (flags & ORANGEFS_OP_PRIORITY)
119 list_add(&op->list, &orangefs_request_list);
120 else
121 list_add_tail(&op->list, &orangefs_request_list);
122 spin_unlock(&op->lock);
123 wake_up_interruptible(&orangefs_request_list_waitq);
124 if (!__is_daemon_in_service()) {
1182fca3 125 gossip_debug(GOSSIP_WAIT_DEBUG,
98815ade 126 "%s:client core is NOT in service.\n",
1182fca3 127 __func__);
b5a9d61e
MB
128 /*
129 * Don't wait for the userspace component to return if
130 * the filesystem is being umounted anyway.
131 */
132 if (op->upcall.type == ORANGEFS_VFS_OP_FS_UMOUNT)
133 timeout = 0;
134 else
135 timeout = op_timeout_secs * HZ;
1182fca3 136 }
98815ade 137 spin_unlock(&orangefs_request_list_lock);
1182fca3 138
adcf34a2 139 if (!(flags & ORANGEFS_OP_NO_MUTEX))
1d503617 140 mutex_unlock(&orangefs_request_mutex);
1182fca3 141
05b39a8b
AV
142 ret = wait_for_matching_downcall(op, timeout,
143 flags & ORANGEFS_OP_INTERRUPTIBLE);
5253487e
MM
144
145 gossip_debug(GOSSIP_WAIT_DEBUG,
146 "%s: wait_for_matching_downcall returned %d for %p\n",
147 __func__,
148 ret,
149 op);
150
ca9f518e 151 /* got matching downcall; make sure status is in errno format */
05b39a8b 152 if (!ret) {
d2d87a3b 153 spin_unlock(&op->lock);
1182fca3 154 op->downcall.status =
8bb8aefd 155 orangefs_normalize_to_errno(op->downcall.status);
1182fca3 156 ret = op->downcall.status;
05b39a8b 157 goto out;
1182fca3
MM
158 }
159
05b39a8b
AV
160 /* failed to get matching downcall */
161 if (ret == -ETIMEDOUT) {
adcf34a2
MM
162 gossip_err("%s: %s -- wait timed out; aborting attempt.\n",
163 __func__,
05b39a8b
AV
164 op_name);
165 }
adcf34a2
MM
166
167 /*
ca9f518e
MM
168 * remove a waiting op from the request list or
169 * remove an in-progress op from the in-progress list.
adcf34a2 170 */
05b39a8b 171 orangefs_clean_up_interrupted_operation(op);
adcf34a2 172
05b39a8b 173 op->downcall.status = ret;
1182fca3 174 /* retry if operation has not been serviced and if requested */
05b39a8b
AV
175 if (ret == -EAGAIN) {
176 op->attempts++;
177 timeout = op_timeout_secs * HZ;
1182fca3 178 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 179 "orangefs: tag %llu (%s)"
1182fca3
MM
180 " -- operation to be retried (%d attempt)\n",
181 llu(op->tag),
182 op_name,
05b39a8b 183 op->attempts);
1182fca3 184
adcf34a2
MM
185 /*
186 * io ops (ops that use the shared memory buffer) have
187 * to be returned to their caller for a retry. Other ops
188 * can just be recycled here.
189 */
1182fca3 190 if (!op->uses_shared_memory)
1182fca3 191 goto retry_servicing;
1182fca3
MM
192 }
193
05b39a8b 194out:
1182fca3 195 gossip_debug(GOSSIP_WAIT_DEBUG,
9d9e7ba9
MM
196 "%s: %s returning: %d for %p.\n",
197 __func__,
1182fca3
MM
198 op_name,
199 ret,
200 op);
201 return ret;
202}
203
ca9f518e 204/* This can get called on an I/O op if it had a bad service_operation. */
78699e29
AV
205bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
206{
207 u64 tag = op->tag;
208 if (!op_state_in_progress(op))
209 return false;
210
211 op->slot_to_free = op->upcall.req.io.buf_index;
212 memset(&op->upcall, 0, sizeof(op->upcall));
213 memset(&op->downcall, 0, sizeof(op->downcall));
214 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
215 op->upcall.req.cancel.op_tag = tag;
216 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
217 op->downcall.status = -1;
218 orangefs_new_tag(op);
219
220 spin_lock(&orangefs_request_list_lock);
221 /* orangefs_request_list_lock is enough of a barrier here */
222 if (!__is_daemon_in_service()) {
223 spin_unlock(&orangefs_request_list_lock);
224 return false;
225 }
98815ade
AV
226 spin_lock(&op->lock);
227 set_op_state_waiting(op);
9d9e7ba9
MM
228 gossip_debug(GOSSIP_DEV_DEBUG,
229 "%s: op:%s: op_state:%d: process:%s:\n",
230 __func__,
231 get_opname_string(op),
232 op->op_state,
233 current->comm);
98815ade
AV
234 list_add(&op->list, &orangefs_request_list);
235 spin_unlock(&op->lock);
78699e29
AV
236 spin_unlock(&orangefs_request_list_lock);
237
ca9f518e 238 gossip_debug(GOSSIP_WAIT_DEBUG,
78699e29
AV
239 "Attempting ORANGEFS operation cancellation of tag %llu\n",
240 llu(tag));
241 return true;
242}
243
ca9f518e
MM
244/*
245 * Change an op to the "given up" state and remove it from its list.
246 */
247static void
248 orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
1182fca3
MM
249{
250 /*
251 * handle interrupted cases depending on what state we were in when
ca9f518e 252 * the interruption is detected.
1182fca3 253 *
eab9b389 254 * Called with op->lock held.
1182fca3 255 */
ca9f518e
MM
256
257 /*
258 * List manipulation code elsewhere will ignore ops that
259 * have been given up upon.
260 */
ed42fe05 261 op->op_state |= OP_VFS_STATE_GIVEN_UP;
ca9f518e 262
05a50a5b
AV
263 if (list_empty(&op->list)) {
264 /* caught copying to/from daemon */
265 BUG_ON(op_state_serviced(op));
266 spin_unlock(&op->lock);
267 wait_for_completion(&op->waitq);
268 } else if (op_state_waiting(op)) {
1182fca3
MM
269 /*
270 * upcall hasn't been read; remove op from upcall request
271 * list.
272 */
273 spin_unlock(&op->lock);
ed42fe05 274 spin_lock(&orangefs_request_list_lock);
05a50a5b 275 list_del_init(&op->list);
ed42fe05 276 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
277 gossip_debug(GOSSIP_WAIT_DEBUG,
278 "Interrupted: Removed op %p from request_list\n",
279 op);
280 } else if (op_state_in_progress(op)) {
281 /* op must be removed from the in progress htable */
282 spin_unlock(&op->lock);
1d503617 283 spin_lock(&orangefs_htable_ops_in_progress_lock);
05a50a5b 284 list_del_init(&op->list);
1d503617 285 spin_unlock(&orangefs_htable_ops_in_progress_lock);
1182fca3
MM
286 gossip_debug(GOSSIP_WAIT_DEBUG,
287 "Interrupted: Removed op %p"
288 " from htable_ops_in_progress\n",
289 op);
05a50a5b 290 } else {
1182fca3
MM
291 spin_unlock(&op->lock);
292 gossip_err("interrupted operation is in a weird state 0x%x\n",
293 op->op_state);
294 }
d2d87a3b 295 reinit_completion(&op->waitq);
1182fca3
MM
296}
297
298/*
ca9f518e
MM
299 * Sleeps on waitqueue waiting for matching downcall.
300 * If client-core finishes servicing, then we are good to go.
1182fca3
MM
301 * else if client-core exits, we get woken up here, and retry with a timeout
302 *
ca9f518e
MM
303 * When this call returns to the caller, the specified op will no
304 * longer be in either the in_progress hash table or on the request list.
1182fca3
MM
305 *
306 * Returns 0 on success and -errno on failure
307 * Errors are:
308 * EAGAIN in case we want the caller to requeue and try again..
309 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
310 * operation since client-core seems to be exiting too often
311 * or if we were interrupted.
d2d87a3b
AV
312 *
313 * Returns with op->lock taken.
1182fca3 314 */
c72f15b7 315static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
05b39a8b 316 long timeout,
c72f15b7 317 bool interruptible)
1182fca3 318{
05b39a8b 319 long n;
c72f15b7 320
ca9f518e
MM
321 /*
322 * There's a "schedule_timeout" inside of these wait
323 * primitives, during which the op is out of the hands of the
324 * user process that needs something done and is being
325 * manipulated by the client-core process.
326 */
c72f15b7 327 if (interruptible)
adcf34a2
MM
328 n = wait_for_completion_interruptible_timeout(&op->waitq,
329 timeout);
c72f15b7
AV
330 else
331 n = wait_for_completion_killable_timeout(&op->waitq, timeout);
332
d2d87a3b 333 spin_lock(&op->lock);
1182fca3 334
d2d87a3b
AV
335 if (op_state_serviced(op))
336 return 0;
70c6ea26 337
d2d87a3b
AV
338 if (unlikely(n < 0)) {
339 gossip_debug(GOSSIP_WAIT_DEBUG,
9d9e7ba9 340 "%s: operation interrupted, tag %llu, %p\n",
d2d87a3b
AV
341 __func__,
342 llu(op->tag),
343 op);
344 return -EINTR;
1182fca3 345 }
d2d87a3b
AV
346 if (op_state_purged(op)) {
347 gossip_debug(GOSSIP_WAIT_DEBUG,
9d9e7ba9 348 "%s: operation purged, tag %llu, %p, %d\n",
d2d87a3b
AV
349 __func__,
350 llu(op->tag),
351 op,
352 op->attempts);
353 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
354 -EAGAIN :
355 -EIO;
356 }
357 /* must have timed out, then... */
358 gossip_debug(GOSSIP_WAIT_DEBUG,
9d9e7ba9 359 "%s: operation timed out, tag %llu, %p, %d)\n",
d2d87a3b
AV
360 __func__,
361 llu(op->tag),
362 op,
363 op->attempts);
364 return -ETIMEDOUT;
1182fca3 365}