orangefs: move wakeups into set_op_state_{serviced,purged}()
[deliverable/linux.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
MM
18
19/*
20 * What we do in this function is to walk the list of operations that are
21 * present in the request queue and mark them as purged.
22 * NOTE: This is called from the device close after client-core has
23 * guaranteed that no new operations could appear on the list since the
24 * client-core is anyway going to exit.
25 */
26void purge_waiting_ops(void)
27{
8bb8aefd 28 struct orangefs_kernel_op_s *op;
1182fca3 29
8bb8aefd
YL
30 spin_lock(&orangefs_request_list_lock);
31 list_for_each_entry(op, &orangefs_request_list, list) {
1182fca3
MM
32 gossip_debug(GOSSIP_WAIT_DEBUG,
33 "pvfs2-client-core: purging op tag %llu %s\n",
34 llu(op->tag),
35 get_opname_string(op));
36 spin_lock(&op->lock);
37 set_op_state_purged(op);
38 spin_unlock(&op->lock);
1182fca3 39 }
8bb8aefd 40 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
41}
42
fc916da5
AV
43static inline void
44add_op_to_request_list(struct orangefs_kernel_op_s *op)
45{
46 spin_lock(&orangefs_request_list_lock);
47 spin_lock(&op->lock);
48 set_op_state_waiting(op);
49 list_add_tail(&op->list, &orangefs_request_list);
50 spin_unlock(&orangefs_request_list_lock);
51 spin_unlock(&op->lock);
52 wake_up_interruptible(&orangefs_request_list_waitq);
53}
54
55static inline
56void add_priority_op_to_request_list(struct orangefs_kernel_op_s *op)
57{
58 spin_lock(&orangefs_request_list_lock);
59 spin_lock(&op->lock);
60 set_op_state_waiting(op);
61
62 list_add(&op->list, &orangefs_request_list);
63 spin_unlock(&orangefs_request_list_lock);
64 spin_unlock(&op->lock);
65 wake_up_interruptible(&orangefs_request_list_waitq);
66}
67
1182fca3 68/*
8bb8aefd 69 * submits a ORANGEFS operation and waits for it to complete
1182fca3
MM
70 *
71 * Note op->downcall.status will contain the status of the operation (in
72 * errno format), whether provided by pvfs2-client or a result of failure to
73 * service the operation. If the caller wishes to distinguish, then
74 * op->state can be checked to see if it was serviced or not.
75 *
76 * Returns contents of op->downcall.status for convenience
77 */
8bb8aefd 78int service_operation(struct orangefs_kernel_op_s *op,
1182fca3
MM
79 const char *op_name,
80 int flags)
81{
82 /* flags to modify behavior */
83 sigset_t orig_sigset;
84 int ret = 0;
85
86 /* irqflags and wait_entry are only used IF the client-core aborts */
87 unsigned long irqflags;
88
ce6c414e 89 DEFINE_WAIT(wait_entry);
1182fca3
MM
90
91 op->upcall.tgid = current->tgid;
92 op->upcall.pid = current->pid;
93
94retry_servicing:
95 op->downcall.status = 0;
96 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 97 "orangefs: service_operation: %s %p\n",
1182fca3
MM
98 op_name,
99 op);
100 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 101 "orangefs: operation posted by process: %s, pid: %i\n",
1182fca3
MM
102 current->comm,
103 current->pid);
104
105 /* mask out signals if this operation is not to be interrupted */
8bb8aefd 106 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 107 orangefs_block_signals(&orig_sigset);
1182fca3 108
8bb8aefd 109 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
1182fca3
MM
110 ret = mutex_lock_interruptible(&request_mutex);
111 /*
112 * check to see if we were interrupted while waiting for
113 * semaphore
114 */
115 if (ret < 0) {
8bb8aefd 116 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 117 orangefs_set_signals(&orig_sigset);
1182fca3
MM
118 op->downcall.status = ret;
119 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 120 "orangefs: service_operation interrupted.\n");
1182fca3
MM
121 return ret;
122 }
123 }
124
125 gossip_debug(GOSSIP_WAIT_DEBUG,
126 "%s:About to call is_daemon_in_service().\n",
127 __func__);
128
129 if (is_daemon_in_service() < 0) {
130 /*
131 * By incrementing the per-operation attempt counter, we
132 * directly go into the timeout logic while waiting for
133 * the matching downcall to be read
134 */
135 gossip_debug(GOSSIP_WAIT_DEBUG,
136 "%s:client core is NOT in service(%d).\n",
137 __func__,
138 is_daemon_in_service());
139 op->attempts++;
140 }
141
142 /* queue up the operation */
8bb8aefd 143 if (flags & ORANGEFS_OP_PRIORITY) {
1182fca3
MM
144 add_priority_op_to_request_list(op);
145 } else {
146 gossip_debug(GOSSIP_WAIT_DEBUG,
147 "%s:About to call add_op_to_request_list().\n",
148 __func__);
149 add_op_to_request_list(op);
150 }
151
8bb8aefd 152 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
1182fca3
MM
153 mutex_unlock(&request_mutex);
154
155 /*
156 * If we are asked to service an asynchronous operation from
157 * VFS perspective, we are done.
158 */
8bb8aefd 159 if (flags & ORANGEFS_OP_ASYNC)
1182fca3
MM
160 return 0;
161
8bb8aefd 162 if (flags & ORANGEFS_OP_CANCELLATION) {
1182fca3
MM
163 gossip_debug(GOSSIP_WAIT_DEBUG,
164 "%s:"
165 "About to call wait_for_cancellation_downcall.\n",
166 __func__);
167 ret = wait_for_cancellation_downcall(op);
168 } else {
169 ret = wait_for_matching_downcall(op);
170 }
171
172 if (ret < 0) {
173 /* failed to get matching downcall */
174 if (ret == -ETIMEDOUT) {
8bb8aefd 175 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
1182fca3
MM
176 op_name);
177 }
178 op->downcall.status = ret;
179 } else {
180 /* got matching downcall; make sure status is in errno format */
181 op->downcall.status =
8bb8aefd 182 orangefs_normalize_to_errno(op->downcall.status);
1182fca3
MM
183 ret = op->downcall.status;
184 }
185
8bb8aefd 186 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 187 orangefs_set_signals(&orig_sigset);
1182fca3
MM
188
189 BUG_ON(ret != op->downcall.status);
190 /* retry if operation has not been serviced and if requested */
191 if (!op_state_serviced(op) && op->downcall.status == -EAGAIN) {
192 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 193 "orangefs: tag %llu (%s)"
1182fca3
MM
194 " -- operation to be retried (%d attempt)\n",
195 llu(op->tag),
196 op_name,
197 op->attempts + 1);
198
199 if (!op->uses_shared_memory)
200 /*
201 * this operation doesn't use the shared memory
202 * system
203 */
204 goto retry_servicing;
205
206 /* op uses shared memory */
7d221485 207 if (orangefs_get_bufmap_init() == 0) {
1182fca3
MM
208 /*
209 * This operation uses the shared memory system AND
210 * the system is not yet ready. This situation occurs
211 * when the client-core is restarted AND there were
212 * operations waiting to be processed or were already
213 * in process.
214 */
215 gossip_debug(GOSSIP_WAIT_DEBUG,
216 "uses_shared_memory is true.\n");
217 gossip_debug(GOSSIP_WAIT_DEBUG,
218 "Client core in-service status(%d).\n",
219 is_daemon_in_service());
220 gossip_debug(GOSSIP_WAIT_DEBUG, "bufmap_init:%d.\n",
7d221485 221 orangefs_get_bufmap_init());
1182fca3
MM
222 gossip_debug(GOSSIP_WAIT_DEBUG,
223 "operation's status is 0x%0x.\n",
224 op->op_state);
225
226 /*
227 * let process sleep for a few seconds so shared
228 * memory system can be initialized.
229 */
230 spin_lock_irqsave(&op->lock, irqflags);
ce6c414e
MM
231 prepare_to_wait(&orangefs_bufmap_init_waitq,
232 &wait_entry,
233 TASK_INTERRUPTIBLE);
1182fca3
MM
234 spin_unlock_irqrestore(&op->lock, irqflags);
235
1182fca3 236 /*
8bb8aefd 237 * Wait for orangefs_bufmap_initialize() to wake me up
1182fca3
MM
238 * within the allotted time.
239 */
240 ret = schedule_timeout(MSECS_TO_JIFFIES
8bb8aefd 241 (1000 * ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS));
1182fca3
MM
242
243 gossip_debug(GOSSIP_WAIT_DEBUG,
244 "Value returned from schedule_timeout:"
245 "%d.\n",
246 ret);
247 gossip_debug(GOSSIP_WAIT_DEBUG,
248 "Is shared memory available? (%d).\n",
7d221485 249 orangefs_get_bufmap_init());
1182fca3
MM
250
251 spin_lock_irqsave(&op->lock, irqflags);
ce6c414e 252 finish_wait(&orangefs_bufmap_init_waitq, &wait_entry);
1182fca3
MM
253 spin_unlock_irqrestore(&op->lock, irqflags);
254
7d221485 255 if (orangefs_get_bufmap_init() == 0) {
1182fca3
MM
256 gossip_err("%s:The shared memory system has not started in %d seconds after the client core restarted. Aborting user's request(%s).\n",
257 __func__,
8bb8aefd 258 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS,
1182fca3
MM
259 get_opname_string(op));
260 return -EIO;
261 }
262
263 /*
264 * Return to the calling function and re-populate a
265 * shared memory buffer.
266 */
267 return -EAGAIN;
268 }
269 }
270
271 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 272 "orangefs: service_operation %s returning: %d for %p.\n",
1182fca3
MM
273 op_name,
274 ret,
275 op);
276 return ret;
277}
278
fc916da5
AV
279static inline void remove_op_from_request_list(struct orangefs_kernel_op_s *op)
280{
281 struct list_head *tmp = NULL;
282 struct list_head *tmp_safe = NULL;
283 struct orangefs_kernel_op_s *tmp_op = NULL;
284
285 spin_lock(&orangefs_request_list_lock);
286 list_for_each_safe(tmp, tmp_safe, &orangefs_request_list) {
287 tmp_op = list_entry(tmp,
288 struct orangefs_kernel_op_s,
289 list);
290 if (tmp_op && (tmp_op == op)) {
291 list_del(&tmp_op->list);
292 break;
293 }
294 }
295 spin_unlock(&orangefs_request_list_lock);
296}
297
e07db0a2 298static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
1182fca3
MM
299{
300 /*
301 * handle interrupted cases depending on what state we were in when
302 * the interruption is detected. there is a coarse grained lock
303 * across the operation.
304 *
305 * NOTE: be sure not to reverse lock ordering by locking an op lock
306 * while holding the request_list lock. Here, we first lock the op
307 * and then lock the appropriate list.
308 */
309 if (!op) {
310 gossip_debug(GOSSIP_WAIT_DEBUG,
311 "%s: op is null, ignoring\n",
312 __func__);
313 return;
314 }
315
316 /*
317 * one more sanity check, make sure it's in one of the possible states
318 * or don't try to cancel it
319 */
320 if (!(op_state_waiting(op) ||
321 op_state_in_progress(op) ||
322 op_state_serviced(op) ||
323 op_state_purged(op))) {
324 gossip_debug(GOSSIP_WAIT_DEBUG,
325 "%s: op %p not in a valid state (%0x), "
326 "ignoring\n",
327 __func__,
328 op,
329 op->op_state);
330 return;
331 }
332
333 spin_lock(&op->lock);
334
335 if (op_state_waiting(op)) {
336 /*
337 * upcall hasn't been read; remove op from upcall request
338 * list.
339 */
340 spin_unlock(&op->lock);
341 remove_op_from_request_list(op);
342 gossip_debug(GOSSIP_WAIT_DEBUG,
343 "Interrupted: Removed op %p from request_list\n",
344 op);
345 } else if (op_state_in_progress(op)) {
346 /* op must be removed from the in progress htable */
347 spin_unlock(&op->lock);
348 spin_lock(&htable_ops_in_progress_lock);
349 list_del(&op->list);
350 spin_unlock(&htable_ops_in_progress_lock);
351 gossip_debug(GOSSIP_WAIT_DEBUG,
352 "Interrupted: Removed op %p"
353 " from htable_ops_in_progress\n",
354 op);
355 } else if (!op_state_serviced(op)) {
356 spin_unlock(&op->lock);
357 gossip_err("interrupted operation is in a weird state 0x%x\n",
358 op->op_state);
84d02150
MM
359 } else {
360 /*
361 * It is not intended for execution to flow here,
362 * but having this unlock here makes sparse happy.
363 */
364 gossip_err("%s: can't get here.\n", __func__);
365 spin_unlock(&op->lock);
1182fca3
MM
366 }
367}
368
369/*
370 * sleeps on waitqueue waiting for matching downcall.
371 * if client-core finishes servicing, then we are good to go.
372 * else if client-core exits, we get woken up here, and retry with a timeout
373 *
374 * Post when this call returns to the caller, the specified op will no
375 * longer be on any list or htable.
376 *
377 * Returns 0 on success and -errno on failure
378 * Errors are:
379 * EAGAIN in case we want the caller to requeue and try again..
380 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
381 * operation since client-core seems to be exiting too often
382 * or if we were interrupted.
383 */
b7ae37b0 384static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op)
1182fca3
MM
385{
386 int ret = -EINVAL;
ce6c414e 387 DEFINE_WAIT(wait_entry);
1182fca3
MM
388
389 while (1) {
1182fca3 390 spin_lock(&op->lock);
ce6c414e 391 prepare_to_wait(&op->waitq, &wait_entry, TASK_INTERRUPTIBLE);
1182fca3
MM
392 if (op_state_serviced(op)) {
393 spin_unlock(&op->lock);
394 ret = 0;
395 break;
396 }
397 spin_unlock(&op->lock);
398
399 if (!signal_pending(current)) {
400 /*
401 * if this was our first attempt and client-core
402 * has not purged our operation, we are happy to
403 * simply wait
404 */
405 spin_lock(&op->lock);
406 if (op->attempts == 0 && !op_state_purged(op)) {
407 spin_unlock(&op->lock);
408 schedule();
409 } else {
410 spin_unlock(&op->lock);
411 /*
412 * subsequent attempts, we retry exactly once
413 * with timeouts
414 */
415 if (!schedule_timeout(MSECS_TO_JIFFIES
416 (1000 * op_timeout_secs))) {
417 gossip_debug(GOSSIP_WAIT_DEBUG,
418 "*** %s:"
419 " operation timed out (tag"
420 " %llu, %p, att %d)\n",
421 __func__,
422 llu(op->tag),
423 op,
424 op->attempts);
425 ret = -ETIMEDOUT;
8bb8aefd 426 orangefs_clean_up_interrupted_operation
1182fca3
MM
427 (op);
428 break;
429 }
430 }
431 spin_lock(&op->lock);
432 op->attempts++;
433 /*
434 * if the operation was purged in the meantime, it
435 * is better to requeue it afresh but ensure that
436 * we have not been purged repeatedly. This could
437 * happen if client-core crashes when an op
438 * is being serviced, so we requeue the op, client
439 * core crashes again so we requeue the op, client
440 * core starts, and so on...
441 */
442 if (op_state_purged(op)) {
8bb8aefd 443 ret = (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
1182fca3
MM
444 -EAGAIN :
445 -EIO;
446 spin_unlock(&op->lock);
447 gossip_debug(GOSSIP_WAIT_DEBUG,
448 "*** %s:"
449 " operation purged (tag "
450 "%llu, %p, att %d)\n",
451 __func__,
452 llu(op->tag),
453 op,
454 op->attempts);
8bb8aefd 455 orangefs_clean_up_interrupted_operation(op);
1182fca3
MM
456 break;
457 }
458 spin_unlock(&op->lock);
459 continue;
460 }
461
462 gossip_debug(GOSSIP_WAIT_DEBUG,
463 "*** %s:"
464 " operation interrupted by a signal (tag "
465 "%llu, op %p)\n",
466 __func__,
467 llu(op->tag),
468 op);
8bb8aefd 469 orangefs_clean_up_interrupted_operation(op);
1182fca3
MM
470 ret = -EINTR;
471 break;
472 }
473
1182fca3 474 spin_lock(&op->lock);
ce6c414e 475 finish_wait(&op->waitq, &wait_entry);
1182fca3
MM
476 spin_unlock(&op->lock);
477
478 return ret;
479}
480
481/*
482 * similar to wait_for_matching_downcall(), but used in the special case
483 * of I/O cancellations.
484 *
485 * Note we need a special wait function because if this is called we already
486 * know that a signal is pending in current and need to service the
487 * cancellation upcall anyway. the only way to exit this is to either
488 * timeout or have the cancellation be serviced properly.
489 */
b7ae37b0 490static int wait_for_cancellation_downcall(struct orangefs_kernel_op_s *op)
1182fca3
MM
491{
492 int ret = -EINVAL;
ce6c414e 493 DEFINE_WAIT(wait_entry);
1182fca3
MM
494
495 while (1) {
1182fca3 496 spin_lock(&op->lock);
ce6c414e 497 prepare_to_wait(&op->waitq, &wait_entry, TASK_INTERRUPTIBLE);
1182fca3
MM
498 if (op_state_serviced(op)) {
499 gossip_debug(GOSSIP_WAIT_DEBUG,
500 "%s:op-state is SERVICED.\n",
501 __func__);
502 spin_unlock(&op->lock);
503 ret = 0;
504 break;
505 }
506 spin_unlock(&op->lock);
507
508 if (signal_pending(current)) {
509 gossip_debug(GOSSIP_WAIT_DEBUG,
510 "%s:operation interrupted by a signal (tag"
511 " %llu, op %p)\n",
512 __func__,
513 llu(op->tag),
514 op);
8bb8aefd 515 orangefs_clean_up_interrupted_operation(op);
1182fca3
MM
516 ret = -EINTR;
517 break;
518 }
519
520 gossip_debug(GOSSIP_WAIT_DEBUG,
521 "%s:About to call schedule_timeout.\n",
522 __func__);
523 ret =
524 schedule_timeout(MSECS_TO_JIFFIES(1000 * op_timeout_secs));
525
526 gossip_debug(GOSSIP_WAIT_DEBUG,
527 "%s:Value returned from schedule_timeout(%d).\n",
528 __func__,
529 ret);
530 if (!ret) {
531 gossip_debug(GOSSIP_WAIT_DEBUG,
532 "%s:*** operation timed out: %p\n",
533 __func__,
534 op);
8bb8aefd 535 orangefs_clean_up_interrupted_operation(op);
1182fca3
MM
536 ret = -ETIMEDOUT;
537 break;
538 }
539
540 gossip_debug(GOSSIP_WAIT_DEBUG,
541 "%s:Breaking out of loop, regardless of value returned by schedule_timeout.\n",
542 __func__);
543 ret = -ETIMEDOUT;
544 break;
545 }
546
1182fca3 547 spin_lock(&op->lock);
ce6c414e 548 finish_wait(&op->waitq, &wait_entry);
1182fca3
MM
549 spin_unlock(&op->lock);
550
551 gossip_debug(GOSSIP_WAIT_DEBUG,
552 "%s:returning ret(%d)\n",
553 __func__,
554 ret);
555
556 return ret;
557}
This page took 0.052653 seconds and 5 git commands to generate.