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