orangefs: get rid of loop in wait_for_matching_downcall()
[linux-block.git] / fs / orangefs / waitqueue.c
CommitLineData
1182fca3
MM
1/*
2 * (C) 2001 Clemson University and The University of Chicago
3 * (C) 2011 Omnibond Systems
4 *
5 * Changes by Acxiom Corporation to implement generic service_operation()
6 * function, Copyright Acxiom Corporation, 2005.
7 *
8 * See COPYING in top-level directory.
9 */
10
11/*
12 * In-kernel waitqueue operations.
13 */
14
15#include "protocol.h"
575e9461
MM
16#include "orangefs-kernel.h"
17#include "orangefs-bufmap.h"
1182fca3 18
ade3d781 19static int wait_for_matching_downcall(struct orangefs_kernel_op_s *);
d2d87a3b 20static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *);
ade3d781 21
1182fca3
MM
22/*
23 * What we do in this function is to walk the list of operations that are
24 * present in the request queue and mark them as purged.
25 * NOTE: This is called from the device close after client-core has
26 * guaranteed that no new operations could appear on the list since the
27 * client-core is anyway going to exit.
28 */
29void purge_waiting_ops(void)
30{
8bb8aefd 31 struct orangefs_kernel_op_s *op;
1182fca3 32
8bb8aefd
YL
33 spin_lock(&orangefs_request_list_lock);
34 list_for_each_entry(op, &orangefs_request_list, list) {
1182fca3
MM
35 gossip_debug(GOSSIP_WAIT_DEBUG,
36 "pvfs2-client-core: purging op tag %llu %s\n",
37 llu(op->tag),
38 get_opname_string(op));
1182fca3 39 set_op_state_purged(op);
1182fca3 40 }
8bb8aefd 41 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
42}
43
fc916da5 44static inline void
78699e29 45__add_op_to_request_list(struct orangefs_kernel_op_s *op)
fc916da5 46{
fc916da5
AV
47 spin_lock(&op->lock);
48 set_op_state_waiting(op);
49 list_add_tail(&op->list, &orangefs_request_list);
fc916da5
AV
50 spin_unlock(&op->lock);
51 wake_up_interruptible(&orangefs_request_list_waitq);
52}
53
78699e29
AV
54static inline void
55add_op_to_request_list(struct orangefs_kernel_op_s *op)
56{
57 spin_lock(&orangefs_request_list_lock);
58 __add_op_to_request_list(op);
59 spin_unlock(&orangefs_request_list_lock);
60}
61
fc916da5
AV
62static inline
63void add_priority_op_to_request_list(struct orangefs_kernel_op_s *op)
64{
65 spin_lock(&orangefs_request_list_lock);
66 spin_lock(&op->lock);
67 set_op_state_waiting(op);
68
69 list_add(&op->list, &orangefs_request_list);
70 spin_unlock(&orangefs_request_list_lock);
71 spin_unlock(&op->lock);
72 wake_up_interruptible(&orangefs_request_list_waitq);
73}
74
1182fca3 75/*
8bb8aefd 76 * submits a ORANGEFS operation and waits for it to complete
1182fca3
MM
77 *
78 * Note op->downcall.status will contain the status of the operation (in
79 * errno format), whether provided by pvfs2-client or a result of failure to
80 * service the operation. If the caller wishes to distinguish, then
81 * op->state can be checked to see if it was serviced or not.
82 *
83 * Returns contents of op->downcall.status for convenience
84 */
8bb8aefd 85int service_operation(struct orangefs_kernel_op_s *op,
1182fca3
MM
86 const char *op_name,
87 int flags)
88{
89 /* flags to modify behavior */
90 sigset_t orig_sigset;
91 int ret = 0;
92
ce6c414e 93 DEFINE_WAIT(wait_entry);
1182fca3
MM
94
95 op->upcall.tgid = current->tgid;
96 op->upcall.pid = current->pid;
97
98retry_servicing:
99 op->downcall.status = 0;
100 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 101 "orangefs: service_operation: %s %p\n",
1182fca3
MM
102 op_name,
103 op);
104 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 105 "orangefs: operation posted by process: %s, pid: %i\n",
1182fca3
MM
106 current->comm,
107 current->pid);
108
109 /* mask out signals if this operation is not to be interrupted */
8bb8aefd 110 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 111 orangefs_block_signals(&orig_sigset);
1182fca3 112
8bb8aefd 113 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
1182fca3
MM
114 ret = mutex_lock_interruptible(&request_mutex);
115 /*
116 * check to see if we were interrupted while waiting for
117 * semaphore
118 */
119 if (ret < 0) {
8bb8aefd 120 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 121 orangefs_set_signals(&orig_sigset);
1182fca3
MM
122 op->downcall.status = ret;
123 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 124 "orangefs: service_operation interrupted.\n");
1182fca3
MM
125 return ret;
126 }
127 }
128
129 gossip_debug(GOSSIP_WAIT_DEBUG,
130 "%s:About to call is_daemon_in_service().\n",
131 __func__);
132
133 if (is_daemon_in_service() < 0) {
134 /*
135 * By incrementing the per-operation attempt counter, we
136 * directly go into the timeout logic while waiting for
137 * the matching downcall to be read
138 */
139 gossip_debug(GOSSIP_WAIT_DEBUG,
140 "%s:client core is NOT in service(%d).\n",
141 __func__,
142 is_daemon_in_service());
143 op->attempts++;
144 }
145
146 /* queue up the operation */
8bb8aefd 147 if (flags & ORANGEFS_OP_PRIORITY) {
1182fca3
MM
148 add_priority_op_to_request_list(op);
149 } else {
150 gossip_debug(GOSSIP_WAIT_DEBUG,
151 "%s:About to call add_op_to_request_list().\n",
152 __func__);
153 add_op_to_request_list(op);
154 }
155
8bb8aefd 156 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
1182fca3
MM
157 mutex_unlock(&request_mutex);
158
159 /*
160 * If we are asked to service an asynchronous operation from
161 * VFS perspective, we are done.
162 */
8bb8aefd 163 if (flags & ORANGEFS_OP_ASYNC)
1182fca3
MM
164 return 0;
165
78699e29 166 ret = wait_for_matching_downcall(op);
1182fca3
MM
167
168 if (ret < 0) {
169 /* failed to get matching downcall */
170 if (ret == -ETIMEDOUT) {
8bb8aefd 171 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
1182fca3
MM
172 op_name);
173 }
d2d87a3b 174 orangefs_clean_up_interrupted_operation(op);
1182fca3
MM
175 op->downcall.status = ret;
176 } else {
d2d87a3b 177 spin_unlock(&op->lock);
1182fca3
MM
178 /* got matching downcall; make sure status is in errno format */
179 op->downcall.status =
8bb8aefd 180 orangefs_normalize_to_errno(op->downcall.status);
1182fca3
MM
181 ret = op->downcall.status;
182 }
183
8bb8aefd 184 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 185 orangefs_set_signals(&orig_sigset);
1182fca3
MM
186
187 BUG_ON(ret != op->downcall.status);
188 /* retry if operation has not been serviced and if requested */
189 if (!op_state_serviced(op) && op->downcall.status == -EAGAIN) {
190 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 191 "orangefs: tag %llu (%s)"
1182fca3
MM
192 " -- operation to be retried (%d attempt)\n",
193 llu(op->tag),
194 op_name,
195 op->attempts + 1);
196
197 if (!op->uses_shared_memory)
198 /*
199 * this operation doesn't use the shared memory
200 * system
201 */
202 goto retry_servicing;
203
204 /* op uses shared memory */
7d221485 205 if (orangefs_get_bufmap_init() == 0) {
6ebcc3fc 206 WARN_ON(1);
1182fca3
MM
207 /*
208 * This operation uses the shared memory system AND
209 * the system is not yet ready. This situation occurs
210 * when the client-core is restarted AND there were
211 * operations waiting to be processed or were already
212 * in process.
213 */
214 gossip_debug(GOSSIP_WAIT_DEBUG,
215 "uses_shared_memory is true.\n");
216 gossip_debug(GOSSIP_WAIT_DEBUG,
217 "Client core in-service status(%d).\n",
218 is_daemon_in_service());
219 gossip_debug(GOSSIP_WAIT_DEBUG, "bufmap_init:%d.\n",
7d221485 220 orangefs_get_bufmap_init());
1182fca3
MM
221 gossip_debug(GOSSIP_WAIT_DEBUG,
222 "operation's status is 0x%0x.\n",
223 op->op_state);
224
225 /*
226 * let process sleep for a few seconds so shared
227 * memory system can be initialized.
228 */
ce6c414e
MM
229 prepare_to_wait(&orangefs_bufmap_init_waitq,
230 &wait_entry,
231 TASK_INTERRUPTIBLE);
1182fca3 232
1182fca3 233 /*
8bb8aefd 234 * Wait for orangefs_bufmap_initialize() to wake me up
1182fca3
MM
235 * within the allotted time.
236 */
727cbfea
AV
237 ret = schedule_timeout(
238 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ);
1182fca3
MM
239
240 gossip_debug(GOSSIP_WAIT_DEBUG,
241 "Value returned from schedule_timeout:"
242 "%d.\n",
243 ret);
244 gossip_debug(GOSSIP_WAIT_DEBUG,
245 "Is shared memory available? (%d).\n",
7d221485 246 orangefs_get_bufmap_init());
1182fca3 247
ce6c414e 248 finish_wait(&orangefs_bufmap_init_waitq, &wait_entry);
1182fca3 249
7d221485 250 if (orangefs_get_bufmap_init() == 0) {
1182fca3
MM
251 gossip_err("%s:The shared memory system has not started in %d seconds after the client core restarted. Aborting user's request(%s).\n",
252 __func__,
8bb8aefd 253 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS,
1182fca3
MM
254 get_opname_string(op));
255 return -EIO;
256 }
257
258 /*
259 * Return to the calling function and re-populate a
260 * shared memory buffer.
261 */
262 return -EAGAIN;
263 }
264 }
265
266 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 267 "orangefs: service_operation %s returning: %d for %p.\n",
1182fca3
MM
268 op_name,
269 ret,
270 op);
271 return ret;
272}
273
78699e29
AV
274bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
275{
276 u64 tag = op->tag;
277 if (!op_state_in_progress(op))
278 return false;
279
280 op->slot_to_free = op->upcall.req.io.buf_index;
281 memset(&op->upcall, 0, sizeof(op->upcall));
282 memset(&op->downcall, 0, sizeof(op->downcall));
283 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
284 op->upcall.req.cancel.op_tag = tag;
285 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
286 op->downcall.status = -1;
287 orangefs_new_tag(op);
288
289 spin_lock(&orangefs_request_list_lock);
290 /* orangefs_request_list_lock is enough of a barrier here */
291 if (!__is_daemon_in_service()) {
292 spin_unlock(&orangefs_request_list_lock);
293 return false;
294 }
295 __add_op_to_request_list(op);
296 spin_unlock(&orangefs_request_list_lock);
297
298 gossip_debug(GOSSIP_UTILS_DEBUG,
299 "Attempting ORANGEFS operation cancellation of tag %llu\n",
300 llu(tag));
301 return true;
302}
303
e07db0a2 304static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
1182fca3
MM
305{
306 /*
307 * handle interrupted cases depending on what state we were in when
308 * the interruption is detected. there is a coarse grained lock
309 * across the operation.
310 *
eab9b389 311 * Called with op->lock held.
1182fca3 312 */
ed42fe05 313 op->op_state |= OP_VFS_STATE_GIVEN_UP;
1182fca3
MM
314
315 if (op_state_waiting(op)) {
316 /*
317 * upcall hasn't been read; remove op from upcall request
318 * list.
319 */
320 spin_unlock(&op->lock);
ed42fe05
AV
321 spin_lock(&orangefs_request_list_lock);
322 list_del(&op->list);
323 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
324 gossip_debug(GOSSIP_WAIT_DEBUG,
325 "Interrupted: Removed op %p from request_list\n",
326 op);
327 } else if (op_state_in_progress(op)) {
328 /* op must be removed from the in progress htable */
329 spin_unlock(&op->lock);
330 spin_lock(&htable_ops_in_progress_lock);
331 list_del(&op->list);
332 spin_unlock(&htable_ops_in_progress_lock);
333 gossip_debug(GOSSIP_WAIT_DEBUG,
334 "Interrupted: Removed op %p"
335 " from htable_ops_in_progress\n",
336 op);
337 } else if (!op_state_serviced(op)) {
338 spin_unlock(&op->lock);
339 gossip_err("interrupted operation is in a weird state 0x%x\n",
340 op->op_state);
84d02150
MM
341 } else {
342 /*
343 * It is not intended for execution to flow here,
344 * but having this unlock here makes sparse happy.
345 */
346 gossip_err("%s: can't get here.\n", __func__);
347 spin_unlock(&op->lock);
1182fca3 348 }
d2d87a3b 349 reinit_completion(&op->waitq);
1182fca3
MM
350}
351
352/*
353 * sleeps on waitqueue waiting for matching downcall.
354 * if client-core finishes servicing, then we are good to go.
355 * else if client-core exits, we get woken up here, and retry with a timeout
356 *
357 * Post when this call returns to the caller, the specified op will no
358 * longer be on any list or htable.
359 *
360 * Returns 0 on success and -errno on failure
361 * Errors are:
362 * EAGAIN in case we want the caller to requeue and try again..
363 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
364 * operation since client-core seems to be exiting too often
365 * or if we were interrupted.
d2d87a3b
AV
366 *
367 * Returns with op->lock taken.
1182fca3 368 */
b7ae37b0 369static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op)
1182fca3 370{
d2d87a3b 371 long timeout, n;
1182fca3 372
d2d87a3b
AV
373 timeout = op->attempts ? op_timeout_secs * HZ : MAX_SCHEDULE_TIMEOUT;
374 n = wait_for_completion_interruptible_timeout(&op->waitq, timeout);
375 spin_lock(&op->lock);
1182fca3 376
d2d87a3b
AV
377 if (op_state_serviced(op))
378 return 0;
70c6ea26 379
d2d87a3b
AV
380 if (unlikely(n < 0)) {
381 gossip_debug(GOSSIP_WAIT_DEBUG,
382 "*** %s:"
383 " operation interrupted by a signal (tag "
384 "%llu, op %p)\n",
385 __func__,
386 llu(op->tag),
387 op);
388 return -EINTR;
1182fca3 389 }
d2d87a3b
AV
390 op->attempts++;
391 if (op_state_purged(op)) {
392 gossip_debug(GOSSIP_WAIT_DEBUG,
393 "*** %s:"
394 " operation purged (tag "
395 "%llu, %p, att %d)\n",
396 __func__,
397 llu(op->tag),
398 op,
399 op->attempts);
400 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
401 -EAGAIN :
402 -EIO;
403 }
404 /* must have timed out, then... */
405 gossip_debug(GOSSIP_WAIT_DEBUG,
406 "*** %s:"
407 " operation timed out (tag"
408 " %llu, %p, att %d)\n",
409 __func__,
410 llu(op->tag),
411 op,
412 op->attempts);
413 return -ETIMEDOUT;
1182fca3 414}