2 * (C) 2001 Clemson University and The University of Chicago
3 * (C) 2011 Omnibond Systems
5 * Changes by Acxiom Corporation to implement generic service_operation()
6 * function, Copyright Acxiom Corporation, 2005.
8 * See COPYING in top-level directory.
12 * In-kernel waitqueue operations.
16 #include "orangefs-kernel.h"
17 #include "orangefs-bufmap.h"
19 static int wait_for_cancellation_downcall(struct orangefs_kernel_op_s
*);
20 static int wait_for_matching_downcall(struct orangefs_kernel_op_s
*);
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.
29 void purge_waiting_ops(void)
31 struct orangefs_kernel_op_s
*op
;
33 spin_lock(&orangefs_request_list_lock
);
34 list_for_each_entry(op
, &orangefs_request_list
, list
) {
35 gossip_debug(GOSSIP_WAIT_DEBUG
,
36 "pvfs2-client-core: purging op tag %llu %s\n",
38 get_opname_string(op
));
40 set_op_state_purged(op
);
41 spin_unlock(&op
->lock
);
43 spin_unlock(&orangefs_request_list_lock
);
47 add_op_to_request_list(struct orangefs_kernel_op_s
*op
)
49 spin_lock(&orangefs_request_list_lock
);
51 set_op_state_waiting(op
);
52 list_add_tail(&op
->list
, &orangefs_request_list
);
53 spin_unlock(&orangefs_request_list_lock
);
54 spin_unlock(&op
->lock
);
55 wake_up_interruptible(&orangefs_request_list_waitq
);
59 void add_priority_op_to_request_list(struct orangefs_kernel_op_s
*op
)
61 spin_lock(&orangefs_request_list_lock
);
63 set_op_state_waiting(op
);
65 list_add(&op
->list
, &orangefs_request_list
);
66 spin_unlock(&orangefs_request_list_lock
);
67 spin_unlock(&op
->lock
);
68 wake_up_interruptible(&orangefs_request_list_waitq
);
72 * submits a ORANGEFS operation and waits for it to complete
74 * Note op->downcall.status will contain the status of the operation (in
75 * errno format), whether provided by pvfs2-client or a result of failure to
76 * service the operation. If the caller wishes to distinguish, then
77 * op->state can be checked to see if it was serviced or not.
79 * Returns contents of op->downcall.status for convenience
81 int service_operation(struct orangefs_kernel_op_s
*op
,
85 /* flags to modify behavior */
89 /* irqflags and wait_entry are only used IF the client-core aborts */
90 unsigned long irqflags
;
92 DEFINE_WAIT(wait_entry
);
94 op
->upcall
.tgid
= current
->tgid
;
95 op
->upcall
.pid
= current
->pid
;
98 op
->downcall
.status
= 0;
99 gossip_debug(GOSSIP_WAIT_DEBUG
,
100 "orangefs: service_operation: %s %p\n",
103 gossip_debug(GOSSIP_WAIT_DEBUG
,
104 "orangefs: operation posted by process: %s, pid: %i\n",
108 /* mask out signals if this operation is not to be interrupted */
109 if (!(flags
& ORANGEFS_OP_INTERRUPTIBLE
))
110 orangefs_block_signals(&orig_sigset
);
112 if (!(flags
& ORANGEFS_OP_NO_SEMAPHORE
)) {
113 ret
= mutex_lock_interruptible(&request_mutex
);
115 * check to see if we were interrupted while waiting for
119 if (!(flags
& ORANGEFS_OP_INTERRUPTIBLE
))
120 orangefs_set_signals(&orig_sigset
);
121 op
->downcall
.status
= ret
;
122 gossip_debug(GOSSIP_WAIT_DEBUG
,
123 "orangefs: service_operation interrupted.\n");
128 gossip_debug(GOSSIP_WAIT_DEBUG
,
129 "%s:About to call is_daemon_in_service().\n",
132 if (is_daemon_in_service() < 0) {
134 * By incrementing the per-operation attempt counter, we
135 * directly go into the timeout logic while waiting for
136 * the matching downcall to be read
138 gossip_debug(GOSSIP_WAIT_DEBUG
,
139 "%s:client core is NOT in service(%d).\n",
141 is_daemon_in_service());
145 /* queue up the operation */
146 if (flags
& ORANGEFS_OP_PRIORITY
) {
147 add_priority_op_to_request_list(op
);
149 gossip_debug(GOSSIP_WAIT_DEBUG
,
150 "%s:About to call add_op_to_request_list().\n",
152 add_op_to_request_list(op
);
155 if (!(flags
& ORANGEFS_OP_NO_SEMAPHORE
))
156 mutex_unlock(&request_mutex
);
159 * If we are asked to service an asynchronous operation from
160 * VFS perspective, we are done.
162 if (flags
& ORANGEFS_OP_ASYNC
)
165 if (flags
& ORANGEFS_OP_CANCELLATION
) {
166 gossip_debug(GOSSIP_WAIT_DEBUG
,
168 "About to call wait_for_cancellation_downcall.\n",
170 ret
= wait_for_cancellation_downcall(op
);
172 ret
= wait_for_matching_downcall(op
);
176 /* failed to get matching downcall */
177 if (ret
== -ETIMEDOUT
) {
178 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
181 op
->downcall
.status
= ret
;
183 /* got matching downcall; make sure status is in errno format */
184 op
->downcall
.status
=
185 orangefs_normalize_to_errno(op
->downcall
.status
);
186 ret
= op
->downcall
.status
;
189 if (!(flags
& ORANGEFS_OP_INTERRUPTIBLE
))
190 orangefs_set_signals(&orig_sigset
);
192 BUG_ON(ret
!= op
->downcall
.status
);
193 /* retry if operation has not been serviced and if requested */
194 if (!op_state_serviced(op
) && op
->downcall
.status
== -EAGAIN
) {
195 gossip_debug(GOSSIP_WAIT_DEBUG
,
196 "orangefs: tag %llu (%s)"
197 " -- operation to be retried (%d attempt)\n",
202 if (!op
->uses_shared_memory
)
204 * this operation doesn't use the shared memory
207 goto retry_servicing
;
209 /* op uses shared memory */
210 if (orangefs_get_bufmap_init() == 0) {
212 * This operation uses the shared memory system AND
213 * the system is not yet ready. This situation occurs
214 * when the client-core is restarted AND there were
215 * operations waiting to be processed or were already
218 gossip_debug(GOSSIP_WAIT_DEBUG
,
219 "uses_shared_memory is true.\n");
220 gossip_debug(GOSSIP_WAIT_DEBUG
,
221 "Client core in-service status(%d).\n",
222 is_daemon_in_service());
223 gossip_debug(GOSSIP_WAIT_DEBUG
, "bufmap_init:%d.\n",
224 orangefs_get_bufmap_init());
225 gossip_debug(GOSSIP_WAIT_DEBUG
,
226 "operation's status is 0x%0x.\n",
230 * let process sleep for a few seconds so shared
231 * memory system can be initialized.
233 spin_lock_irqsave(&op
->lock
, irqflags
);
234 prepare_to_wait(&orangefs_bufmap_init_waitq
,
237 spin_unlock_irqrestore(&op
->lock
, irqflags
);
240 * Wait for orangefs_bufmap_initialize() to wake me up
241 * within the allotted time.
243 ret
= schedule_timeout(MSECS_TO_JIFFIES
244 (1000 * ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS
));
246 gossip_debug(GOSSIP_WAIT_DEBUG
,
247 "Value returned from schedule_timeout:"
250 gossip_debug(GOSSIP_WAIT_DEBUG
,
251 "Is shared memory available? (%d).\n",
252 orangefs_get_bufmap_init());
254 spin_lock_irqsave(&op
->lock
, irqflags
);
255 finish_wait(&orangefs_bufmap_init_waitq
, &wait_entry
);
256 spin_unlock_irqrestore(&op
->lock
, irqflags
);
258 if (orangefs_get_bufmap_init() == 0) {
259 gossip_err("%s:The shared memory system has not started in %d seconds after the client core restarted. Aborting user's request(%s).\n",
261 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS
,
262 get_opname_string(op
));
267 * Return to the calling function and re-populate a
268 * shared memory buffer.
274 gossip_debug(GOSSIP_WAIT_DEBUG
,
275 "orangefs: service_operation %s returning: %d for %p.\n",
282 static inline void remove_op_from_request_list(struct orangefs_kernel_op_s
*op
)
284 struct list_head
*tmp
= NULL
;
285 struct list_head
*tmp_safe
= NULL
;
286 struct orangefs_kernel_op_s
*tmp_op
= NULL
;
288 spin_lock(&orangefs_request_list_lock
);
289 list_for_each_safe(tmp
, tmp_safe
, &orangefs_request_list
) {
290 tmp_op
= list_entry(tmp
,
291 struct orangefs_kernel_op_s
,
293 if (tmp_op
&& (tmp_op
== op
)) {
294 list_del(&tmp_op
->list
);
298 spin_unlock(&orangefs_request_list_lock
);
301 static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s
*op
)
304 * handle interrupted cases depending on what state we were in when
305 * the interruption is detected. there is a coarse grained lock
306 * across the operation.
308 * NOTE: be sure not to reverse lock ordering by locking an op lock
309 * while holding the request_list lock. Here, we first lock the op
310 * and then lock the appropriate list.
313 gossip_debug(GOSSIP_WAIT_DEBUG
,
314 "%s: op is null, ignoring\n",
320 * one more sanity check, make sure it's in one of the possible states
321 * or don't try to cancel it
323 if (!(op_state_waiting(op
) ||
324 op_state_in_progress(op
) ||
325 op_state_serviced(op
) ||
326 op_state_purged(op
))) {
327 gossip_debug(GOSSIP_WAIT_DEBUG
,
328 "%s: op %p not in a valid state (%0x), "
336 spin_lock(&op
->lock
);
338 if (op_state_waiting(op
)) {
340 * upcall hasn't been read; remove op from upcall request
343 spin_unlock(&op
->lock
);
344 remove_op_from_request_list(op
);
345 gossip_debug(GOSSIP_WAIT_DEBUG
,
346 "Interrupted: Removed op %p from request_list\n",
348 } else if (op_state_in_progress(op
)) {
349 /* op must be removed from the in progress htable */
350 spin_unlock(&op
->lock
);
351 spin_lock(&htable_ops_in_progress_lock
);
353 spin_unlock(&htable_ops_in_progress_lock
);
354 gossip_debug(GOSSIP_WAIT_DEBUG
,
355 "Interrupted: Removed op %p"
356 " from htable_ops_in_progress\n",
358 } else if (!op_state_serviced(op
)) {
359 spin_unlock(&op
->lock
);
360 gossip_err("interrupted operation is in a weird state 0x%x\n",
364 * It is not intended for execution to flow here,
365 * but having this unlock here makes sparse happy.
367 gossip_err("%s: can't get here.\n", __func__
);
368 spin_unlock(&op
->lock
);
373 * sleeps on waitqueue waiting for matching downcall.
374 * if client-core finishes servicing, then we are good to go.
375 * else if client-core exits, we get woken up here, and retry with a timeout
377 * Post when this call returns to the caller, the specified op will no
378 * longer be on any list or htable.
380 * Returns 0 on success and -errno on failure
382 * EAGAIN in case we want the caller to requeue and try again..
383 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
384 * operation since client-core seems to be exiting too often
385 * or if we were interrupted.
387 static int wait_for_matching_downcall(struct orangefs_kernel_op_s
*op
)
390 DEFINE_WAIT(wait_entry
);
393 spin_lock(&op
->lock
);
394 prepare_to_wait(&op
->waitq
, &wait_entry
, TASK_INTERRUPTIBLE
);
395 if (op_state_serviced(op
)) {
396 spin_unlock(&op
->lock
);
400 spin_unlock(&op
->lock
);
402 if (!signal_pending(current
)) {
404 * if this was our first attempt and client-core
405 * has not purged our operation, we are happy to
408 spin_lock(&op
->lock
);
409 if (op
->attempts
== 0 && !op_state_purged(op
)) {
410 spin_unlock(&op
->lock
);
413 spin_unlock(&op
->lock
);
415 * subsequent attempts, we retry exactly once
418 if (!schedule_timeout(MSECS_TO_JIFFIES
419 (1000 * op_timeout_secs
))) {
420 gossip_debug(GOSSIP_WAIT_DEBUG
,
422 " operation timed out (tag"
423 " %llu, %p, att %d)\n",
429 orangefs_clean_up_interrupted_operation
434 spin_lock(&op
->lock
);
437 * if the operation was purged in the meantime, it
438 * is better to requeue it afresh but ensure that
439 * we have not been purged repeatedly. This could
440 * happen if client-core crashes when an op
441 * is being serviced, so we requeue the op, client
442 * core crashes again so we requeue the op, client
443 * core starts, and so on...
445 if (op_state_purged(op
)) {
446 ret
= (op
->attempts
< ORANGEFS_PURGE_RETRY_COUNT
) ?
449 spin_unlock(&op
->lock
);
450 gossip_debug(GOSSIP_WAIT_DEBUG
,
452 " operation purged (tag "
453 "%llu, %p, att %d)\n",
458 orangefs_clean_up_interrupted_operation(op
);
461 spin_unlock(&op
->lock
);
465 gossip_debug(GOSSIP_WAIT_DEBUG
,
467 " operation interrupted by a signal (tag "
472 orangefs_clean_up_interrupted_operation(op
);
477 spin_lock(&op
->lock
);
478 finish_wait(&op
->waitq
, &wait_entry
);
479 spin_unlock(&op
->lock
);
485 * similar to wait_for_matching_downcall(), but used in the special case
486 * of I/O cancellations.
488 * Note we need a special wait function because if this is called we already
489 * know that a signal is pending in current and need to service the
490 * cancellation upcall anyway. the only way to exit this is to either
491 * timeout or have the cancellation be serviced properly.
493 static int wait_for_cancellation_downcall(struct orangefs_kernel_op_s
*op
)
496 DEFINE_WAIT(wait_entry
);
499 spin_lock(&op
->lock
);
500 prepare_to_wait(&op
->waitq
, &wait_entry
, TASK_INTERRUPTIBLE
);
501 if (op_state_serviced(op
)) {
502 gossip_debug(GOSSIP_WAIT_DEBUG
,
503 "%s:op-state is SERVICED.\n",
505 spin_unlock(&op
->lock
);
509 spin_unlock(&op
->lock
);
511 if (signal_pending(current
)) {
512 gossip_debug(GOSSIP_WAIT_DEBUG
,
513 "%s:operation interrupted by a signal (tag"
518 orangefs_clean_up_interrupted_operation(op
);
523 gossip_debug(GOSSIP_WAIT_DEBUG
,
524 "%s:About to call schedule_timeout.\n",
527 schedule_timeout(MSECS_TO_JIFFIES(1000 * op_timeout_secs
));
529 gossip_debug(GOSSIP_WAIT_DEBUG
,
530 "%s:Value returned from schedule_timeout(%d).\n",
534 gossip_debug(GOSSIP_WAIT_DEBUG
,
535 "%s:*** operation timed out: %p\n",
538 orangefs_clean_up_interrupted_operation(op
);
543 gossip_debug(GOSSIP_WAIT_DEBUG
,
544 "%s:Breaking out of loop, regardless of value returned by schedule_timeout.\n",
550 spin_lock(&op
->lock
);
551 finish_wait(&op
->waitq
, &wait_entry
);
552 spin_unlock(&op
->lock
);
554 gossip_debug(GOSSIP_WAIT_DEBUG
,
555 "%s:returning ret(%d)\n",