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_matching_downcall(struct orangefs_kernel_op_s
*);
20 static void orangefs_clean_up_interrupted_operation(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
));
39 set_op_state_purged(op
);
41 spin_unlock(&orangefs_request_list_lock
);
45 __add_op_to_request_list(struct orangefs_kernel_op_s
*op
)
48 set_op_state_waiting(op
);
49 list_add_tail(&op
->list
, &orangefs_request_list
);
50 spin_unlock(&op
->lock
);
51 wake_up_interruptible(&orangefs_request_list_waitq
);
55 add_op_to_request_list(struct orangefs_kernel_op_s
*op
)
57 spin_lock(&orangefs_request_list_lock
);
58 __add_op_to_request_list(op
);
59 spin_unlock(&orangefs_request_list_lock
);
63 void add_priority_op_to_request_list(struct orangefs_kernel_op_s
*op
)
65 spin_lock(&orangefs_request_list_lock
);
67 set_op_state_waiting(op
);
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
);
76 * submits a ORANGEFS operation and waits for it to complete
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.
83 * Returns contents of op->downcall.status for convenience
85 int service_operation(struct orangefs_kernel_op_s
*op
,
89 /* flags to modify behavior */
93 DEFINE_WAIT(wait_entry
);
95 op
->upcall
.tgid
= current
->tgid
;
96 op
->upcall
.pid
= current
->pid
;
99 op
->downcall
.status
= 0;
100 gossip_debug(GOSSIP_WAIT_DEBUG
,
101 "orangefs: service_operation: %s %p\n",
104 gossip_debug(GOSSIP_WAIT_DEBUG
,
105 "orangefs: operation posted by process: %s, pid: %i\n",
109 /* mask out signals if this operation is not to be interrupted */
110 if (!(flags
& ORANGEFS_OP_INTERRUPTIBLE
))
111 orangefs_block_signals(&orig_sigset
);
113 if (!(flags
& ORANGEFS_OP_NO_SEMAPHORE
)) {
114 ret
= mutex_lock_interruptible(&request_mutex
);
116 * check to see if we were interrupted while waiting for
120 if (!(flags
& ORANGEFS_OP_INTERRUPTIBLE
))
121 orangefs_set_signals(&orig_sigset
);
122 op
->downcall
.status
= ret
;
123 gossip_debug(GOSSIP_WAIT_DEBUG
,
124 "orangefs: service_operation interrupted.\n");
129 gossip_debug(GOSSIP_WAIT_DEBUG
,
130 "%s:About to call is_daemon_in_service().\n",
133 if (is_daemon_in_service() < 0) {
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
139 gossip_debug(GOSSIP_WAIT_DEBUG
,
140 "%s:client core is NOT in service(%d).\n",
142 is_daemon_in_service());
146 /* queue up the operation */
147 if (flags
& ORANGEFS_OP_PRIORITY
) {
148 add_priority_op_to_request_list(op
);
150 gossip_debug(GOSSIP_WAIT_DEBUG
,
151 "%s:About to call add_op_to_request_list().\n",
153 add_op_to_request_list(op
);
156 if (!(flags
& ORANGEFS_OP_NO_SEMAPHORE
))
157 mutex_unlock(&request_mutex
);
160 * If we are asked to service an asynchronous operation from
161 * VFS perspective, we are done.
163 if (flags
& ORANGEFS_OP_ASYNC
)
166 ret
= wait_for_matching_downcall(op
);
169 /* failed to get matching downcall */
170 if (ret
== -ETIMEDOUT
) {
171 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
174 orangefs_clean_up_interrupted_operation(op
);
175 op
->downcall
.status
= ret
;
177 spin_unlock(&op
->lock
);
178 /* got matching downcall; make sure status is in errno format */
179 op
->downcall
.status
=
180 orangefs_normalize_to_errno(op
->downcall
.status
);
181 ret
= op
->downcall
.status
;
184 if (!(flags
& ORANGEFS_OP_INTERRUPTIBLE
))
185 orangefs_set_signals(&orig_sigset
);
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
,
191 "orangefs: tag %llu (%s)"
192 " -- operation to be retried (%d attempt)\n",
197 if (!op
->uses_shared_memory
)
199 * this operation doesn't use the shared memory
202 goto retry_servicing
;
204 /* op uses shared memory */
205 if (orangefs_get_bufmap_init() == 0) {
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
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",
220 orangefs_get_bufmap_init());
221 gossip_debug(GOSSIP_WAIT_DEBUG
,
222 "operation's status is 0x%0x.\n",
226 * let process sleep for a few seconds so shared
227 * memory system can be initialized.
229 prepare_to_wait(&orangefs_bufmap_init_waitq
,
234 * Wait for orangefs_bufmap_initialize() to wake me up
235 * within the allotted time.
237 ret
= schedule_timeout(
238 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS
* HZ
);
240 gossip_debug(GOSSIP_WAIT_DEBUG
,
241 "Value returned from schedule_timeout:"
244 gossip_debug(GOSSIP_WAIT_DEBUG
,
245 "Is shared memory available? (%d).\n",
246 orangefs_get_bufmap_init());
248 finish_wait(&orangefs_bufmap_init_waitq
, &wait_entry
);
250 if (orangefs_get_bufmap_init() == 0) {
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",
253 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS
,
254 get_opname_string(op
));
259 * Return to the calling function and re-populate a
260 * shared memory buffer.
266 gossip_debug(GOSSIP_WAIT_DEBUG
,
267 "orangefs: service_operation %s returning: %d for %p.\n",
274 bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s
*op
)
277 if (!op_state_in_progress(op
))
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
);
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
);
295 __add_op_to_request_list(op
);
296 spin_unlock(&orangefs_request_list_lock
);
298 gossip_debug(GOSSIP_UTILS_DEBUG
,
299 "Attempting ORANGEFS operation cancellation of tag %llu\n",
304 static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s
*op
)
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.
311 * Called with op->lock held.
313 op
->op_state
|= OP_VFS_STATE_GIVEN_UP
;
315 if (op_state_waiting(op
)) {
317 * upcall hasn't been read; remove op from upcall request
320 spin_unlock(&op
->lock
);
321 spin_lock(&orangefs_request_list_lock
);
323 spin_unlock(&orangefs_request_list_lock
);
324 gossip_debug(GOSSIP_WAIT_DEBUG
,
325 "Interrupted: Removed op %p from request_list\n",
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
);
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",
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",
343 * It is not intended for execution to flow here,
344 * but having this unlock here makes sparse happy.
346 gossip_err("%s: can't get here.\n", __func__
);
347 spin_unlock(&op
->lock
);
349 reinit_completion(&op
->waitq
);
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
357 * Post when this call returns to the caller, the specified op will no
358 * longer be on any list or htable.
360 * Returns 0 on success and -errno on failure
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.
367 * Returns with op->lock taken.
369 static int wait_for_matching_downcall(struct orangefs_kernel_op_s
*op
)
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
);
377 if (op_state_serviced(op
))
380 if (unlikely(n
< 0)) {
381 gossip_debug(GOSSIP_WAIT_DEBUG
,
383 " operation interrupted by a signal (tag "
391 if (op_state_purged(op
)) {
392 gossip_debug(GOSSIP_WAIT_DEBUG
,
394 " operation purged (tag "
395 "%llu, %p, att %d)\n",
400 return (op
->attempts
< ORANGEFS_PURGE_RETRY_COUNT
) ?
404 /* must have timed out, then... */
405 gossip_debug(GOSSIP_WAIT_DEBUG
,
407 " operation timed out (tag"
408 " %llu, %p, att %d)\n",