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 DEFINE_WAIT(wait_entry
);
91 op
->upcall
.tgid
= current
->tgid
;
92 op
->upcall
.pid
= current
->pid
;
95 op
->downcall
.status
= 0;
96 gossip_debug(GOSSIP_WAIT_DEBUG
,
97 "orangefs: service_operation: %s %p\n",
100 gossip_debug(GOSSIP_WAIT_DEBUG
,
101 "orangefs: operation posted by process: %s, pid: %i\n",
105 /* mask out signals if this operation is not to be interrupted */
106 if (!(flags
& ORANGEFS_OP_INTERRUPTIBLE
))
107 orangefs_block_signals(&orig_sigset
);
109 if (!(flags
& ORANGEFS_OP_NO_SEMAPHORE
)) {
110 ret
= mutex_lock_interruptible(&request_mutex
);
112 * check to see if we were interrupted while waiting for
116 if (!(flags
& ORANGEFS_OP_INTERRUPTIBLE
))
117 orangefs_set_signals(&orig_sigset
);
118 op
->downcall
.status
= ret
;
119 gossip_debug(GOSSIP_WAIT_DEBUG
,
120 "orangefs: service_operation interrupted.\n");
125 gossip_debug(GOSSIP_WAIT_DEBUG
,
126 "%s:About to call is_daemon_in_service().\n",
129 if (is_daemon_in_service() < 0) {
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
135 gossip_debug(GOSSIP_WAIT_DEBUG
,
136 "%s:client core is NOT in service(%d).\n",
138 is_daemon_in_service());
142 /* queue up the operation */
143 if (flags
& ORANGEFS_OP_PRIORITY
) {
144 add_priority_op_to_request_list(op
);
146 gossip_debug(GOSSIP_WAIT_DEBUG
,
147 "%s:About to call add_op_to_request_list().\n",
149 add_op_to_request_list(op
);
152 if (!(flags
& ORANGEFS_OP_NO_SEMAPHORE
))
153 mutex_unlock(&request_mutex
);
156 * If we are asked to service an asynchronous operation from
157 * VFS perspective, we are done.
159 if (flags
& ORANGEFS_OP_ASYNC
)
162 if (flags
& ORANGEFS_OP_CANCELLATION
) {
163 gossip_debug(GOSSIP_WAIT_DEBUG
,
165 "About to call wait_for_cancellation_downcall.\n",
167 ret
= wait_for_cancellation_downcall(op
);
169 ret
= wait_for_matching_downcall(op
);
173 /* failed to get matching downcall */
174 if (ret
== -ETIMEDOUT
) {
175 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
178 op
->downcall
.status
= ret
;
180 /* got matching downcall; make sure status is in errno format */
181 op
->downcall
.status
=
182 orangefs_normalize_to_errno(op
->downcall
.status
);
183 ret
= op
->downcall
.status
;
186 if (!(flags
& ORANGEFS_OP_INTERRUPTIBLE
))
187 orangefs_set_signals(&orig_sigset
);
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
,
193 "orangefs: tag %llu (%s)"
194 " -- operation to be retried (%d attempt)\n",
199 if (!op
->uses_shared_memory
)
201 * this operation doesn't use the shared memory
204 goto retry_servicing
;
206 /* op uses shared memory */
207 if (orangefs_get_bufmap_init() == 0) {
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
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",
221 orangefs_get_bufmap_init());
222 gossip_debug(GOSSIP_WAIT_DEBUG
,
223 "operation's status is 0x%0x.\n",
227 * let process sleep for a few seconds so shared
228 * memory system can be initialized.
230 prepare_to_wait(&orangefs_bufmap_init_waitq
,
235 * Wait for orangefs_bufmap_initialize() to wake me up
236 * within the allotted time.
238 ret
= schedule_timeout(MSECS_TO_JIFFIES
239 (1000 * ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS
));
241 gossip_debug(GOSSIP_WAIT_DEBUG
,
242 "Value returned from schedule_timeout:"
245 gossip_debug(GOSSIP_WAIT_DEBUG
,
246 "Is shared memory available? (%d).\n",
247 orangefs_get_bufmap_init());
249 finish_wait(&orangefs_bufmap_init_waitq
, &wait_entry
);
251 if (orangefs_get_bufmap_init() == 0) {
252 gossip_err("%s:The shared memory system has not started in %d seconds after the client core restarted. Aborting user's request(%s).\n",
254 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS
,
255 get_opname_string(op
));
260 * Return to the calling function and re-populate a
261 * shared memory buffer.
267 gossip_debug(GOSSIP_WAIT_DEBUG
,
268 "orangefs: service_operation %s returning: %d for %p.\n",
275 static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s
*op
)
278 * handle interrupted cases depending on what state we were in when
279 * the interruption is detected. there is a coarse grained lock
280 * across the operation.
282 * NOTE: be sure not to reverse lock ordering by locking an op lock
283 * while holding the request_list lock. Here, we first lock the op
284 * and then lock the appropriate list.
287 gossip_debug(GOSSIP_WAIT_DEBUG
,
288 "%s: op is null, ignoring\n",
294 * one more sanity check, make sure it's in one of the possible states
295 * or don't try to cancel it
297 if (!(op_state_waiting(op
) ||
298 op_state_in_progress(op
) ||
299 op_state_serviced(op
) ||
300 op_state_purged(op
))) {
301 gossip_debug(GOSSIP_WAIT_DEBUG
,
302 "%s: op %p not in a valid state (%0x), "
310 spin_lock(&op
->lock
);
311 op
->op_state
|= OP_VFS_STATE_GIVEN_UP
;
313 if (op_state_waiting(op
)) {
315 * upcall hasn't been read; remove op from upcall request
318 spin_unlock(&op
->lock
);
319 spin_lock(&orangefs_request_list_lock
);
321 spin_unlock(&orangefs_request_list_lock
);
322 gossip_debug(GOSSIP_WAIT_DEBUG
,
323 "Interrupted: Removed op %p from request_list\n",
325 } else if (op_state_in_progress(op
)) {
326 /* op must be removed from the in progress htable */
327 spin_unlock(&op
->lock
);
328 spin_lock(&htable_ops_in_progress_lock
);
330 spin_unlock(&htable_ops_in_progress_lock
);
331 gossip_debug(GOSSIP_WAIT_DEBUG
,
332 "Interrupted: Removed op %p"
333 " from htable_ops_in_progress\n",
335 } else if (!op_state_serviced(op
)) {
336 spin_unlock(&op
->lock
);
337 gossip_err("interrupted operation is in a weird state 0x%x\n",
341 * It is not intended for execution to flow here,
342 * but having this unlock here makes sparse happy.
344 gossip_err("%s: can't get here.\n", __func__
);
345 spin_unlock(&op
->lock
);
350 * sleeps on waitqueue waiting for matching downcall.
351 * if client-core finishes servicing, then we are good to go.
352 * else if client-core exits, we get woken up here, and retry with a timeout
354 * Post when this call returns to the caller, the specified op will no
355 * longer be on any list or htable.
357 * Returns 0 on success and -errno on failure
359 * EAGAIN in case we want the caller to requeue and try again..
360 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
361 * operation since client-core seems to be exiting too often
362 * or if we were interrupted.
364 static int wait_for_matching_downcall(struct orangefs_kernel_op_s
*op
)
367 DEFINE_WAIT(wait_entry
);
370 spin_lock(&op
->lock
);
371 prepare_to_wait(&op
->waitq
, &wait_entry
, TASK_INTERRUPTIBLE
);
372 if (op_state_serviced(op
)) {
373 spin_unlock(&op
->lock
);
377 spin_unlock(&op
->lock
);
379 if (unlikely(signal_pending(current
))) {
380 gossip_debug(GOSSIP_WAIT_DEBUG
,
382 " operation interrupted by a signal (tag "
387 orangefs_clean_up_interrupted_operation(op
);
393 * if this was our first attempt and client-core
394 * has not purged our operation, we are happy to
397 spin_lock(&op
->lock
);
398 if (op
->attempts
== 0 && !op_state_purged(op
)) {
399 spin_unlock(&op
->lock
);
402 spin_unlock(&op
->lock
);
404 * subsequent attempts, we retry exactly once
407 if (!schedule_timeout(MSECS_TO_JIFFIES
408 (1000 * op_timeout_secs
))) {
409 gossip_debug(GOSSIP_WAIT_DEBUG
,
411 " operation timed out (tag"
412 " %llu, %p, att %d)\n",
418 orangefs_clean_up_interrupted_operation(op
);
422 spin_lock(&op
->lock
);
425 * if the operation was purged in the meantime, it
426 * is better to requeue it afresh but ensure that
427 * we have not been purged repeatedly. This could
428 * happen if client-core crashes when an op
429 * is being serviced, so we requeue the op, client
430 * core crashes again so we requeue the op, client
431 * core starts, and so on...
433 if (op_state_purged(op
)) {
434 ret
= (op
->attempts
< ORANGEFS_PURGE_RETRY_COUNT
) ?
437 spin_unlock(&op
->lock
);
438 gossip_debug(GOSSIP_WAIT_DEBUG
,
440 " operation purged (tag "
441 "%llu, %p, att %d)\n",
446 orangefs_clean_up_interrupted_operation(op
);
449 spin_unlock(&op
->lock
);
452 spin_lock(&op
->lock
);
453 finish_wait(&op
->waitq
, &wait_entry
);
454 spin_unlock(&op
->lock
);
460 * similar to wait_for_matching_downcall(), but used in the special case
461 * of I/O cancellations.
463 * Note we need a special wait function because if this is called we already
464 * know that a signal is pending in current and need to service the
465 * cancellation upcall anyway. the only way to exit this is to either
466 * timeout or have the cancellation be serviced properly.
468 static int wait_for_cancellation_downcall(struct orangefs_kernel_op_s
*op
)
471 DEFINE_WAIT(wait_entry
);
474 spin_lock(&op
->lock
);
475 prepare_to_wait(&op
->waitq
, &wait_entry
, TASK_INTERRUPTIBLE
);
476 if (op_state_serviced(op
)) {
477 gossip_debug(GOSSIP_WAIT_DEBUG
,
478 "%s:op-state is SERVICED.\n",
480 spin_unlock(&op
->lock
);
484 spin_unlock(&op
->lock
);
486 if (signal_pending(current
)) {
487 gossip_debug(GOSSIP_WAIT_DEBUG
,
488 "%s:operation interrupted by a signal (tag"
493 orangefs_clean_up_interrupted_operation(op
);
498 gossip_debug(GOSSIP_WAIT_DEBUG
,
499 "%s:About to call schedule_timeout.\n",
502 schedule_timeout(MSECS_TO_JIFFIES(1000 * op_timeout_secs
));
504 gossip_debug(GOSSIP_WAIT_DEBUG
,
505 "%s:Value returned from schedule_timeout(%d).\n",
509 gossip_debug(GOSSIP_WAIT_DEBUG
,
510 "%s:*** operation timed out: %p\n",
513 orangefs_clean_up_interrupted_operation(op
);
518 gossip_debug(GOSSIP_WAIT_DEBUG
,
519 "%s:Breaking out of loop, regardless of value returned by schedule_timeout.\n",
525 spin_lock(&op
->lock
);
526 finish_wait(&op
->waitq
, &wait_entry
);
527 spin_unlock(&op
->lock
);
529 gossip_debug(GOSSIP_WAIT_DEBUG
,
530 "%s:returning ret(%d)\n",