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 * submits a ORANGEFS operation and waits for it to complete
47 * Note op->downcall.status will contain the status of the operation (in
48 * errno format), whether provided by pvfs2-client or a result of failure to
49 * service the operation. If the caller wishes to distinguish, then
50 * op->state can be checked to see if it was serviced or not.
52 * Returns contents of op->downcall.status for convenience
54 int service_operation(struct orangefs_kernel_op_s *op,
58 /* flags to modify behavior */
62 DEFINE_WAIT(wait_entry);
64 op->upcall.tgid = current->tgid;
65 op->upcall.pid = current->pid;
68 op->downcall.status = 0;
69 gossip_debug(GOSSIP_WAIT_DEBUG,
70 "orangefs: service_operation: %s %p\n",
73 gossip_debug(GOSSIP_WAIT_DEBUG,
74 "orangefs: operation posted by process: %s, pid: %i\n",
78 /* mask out signals if this operation is not to be interrupted */
79 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
80 orangefs_block_signals(&orig_sigset);
82 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
83 ret = mutex_lock_interruptible(&request_mutex);
85 * check to see if we were interrupted while waiting for
89 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
90 orangefs_set_signals(&orig_sigset);
91 op->downcall.status = ret;
92 gossip_debug(GOSSIP_WAIT_DEBUG,
93 "orangefs: service_operation interrupted.\n");
98 /* queue up the operation */
99 spin_lock(&orangefs_request_list_lock);
100 spin_lock(&op->lock);
101 set_op_state_waiting(op);
102 if (flags & ORANGEFS_OP_PRIORITY)
103 list_add(&op->list, &orangefs_request_list);
105 list_add_tail(&op->list, &orangefs_request_list);
106 spin_unlock(&op->lock);
107 wake_up_interruptible(&orangefs_request_list_waitq);
108 if (!__is_daemon_in_service()) {
110 * By incrementing the per-operation attempt counter, we
111 * directly go into the timeout logic while waiting for
112 * the matching downcall to be read
114 gossip_debug(GOSSIP_WAIT_DEBUG,
115 "%s:client core is NOT in service.\n",
119 spin_unlock(&orangefs_request_list_lock);
121 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
122 mutex_unlock(&request_mutex);
125 * If we are asked to service an asynchronous operation from
126 * VFS perspective, we are done.
128 if (flags & ORANGEFS_OP_ASYNC)
131 ret = wait_for_matching_downcall(op);
134 /* failed to get matching downcall */
135 if (ret == -ETIMEDOUT) {
136 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
139 orangefs_clean_up_interrupted_operation(op);
140 op->downcall.status = ret;
142 spin_unlock(&op->lock);
143 /* got matching downcall; make sure status is in errno format */
144 op->downcall.status =
145 orangefs_normalize_to_errno(op->downcall.status);
146 ret = op->downcall.status;
149 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
150 orangefs_set_signals(&orig_sigset);
152 BUG_ON(ret != op->downcall.status);
153 /* retry if operation has not been serviced and if requested */
154 if (!op_state_serviced(op) && op->downcall.status == -EAGAIN) {
155 gossip_debug(GOSSIP_WAIT_DEBUG,
156 "orangefs: tag %llu (%s)"
157 " -- operation to be retried (%d attempt)\n",
162 if (!op->uses_shared_memory)
164 * this operation doesn't use the shared memory
167 goto retry_servicing;
169 /* op uses shared memory */
170 if (orangefs_get_bufmap_init() == 0) {
173 * This operation uses the shared memory system AND
174 * the system is not yet ready. This situation occurs
175 * when the client-core is restarted AND there were
176 * operations waiting to be processed or were already
179 gossip_debug(GOSSIP_WAIT_DEBUG,
180 "uses_shared_memory is true.\n");
181 gossip_debug(GOSSIP_WAIT_DEBUG,
182 "Client core in-service status(%d).\n",
183 is_daemon_in_service());
184 gossip_debug(GOSSIP_WAIT_DEBUG, "bufmap_init:%d.\n",
185 orangefs_get_bufmap_init());
186 gossip_debug(GOSSIP_WAIT_DEBUG,
187 "operation's status is 0x%0x.\n",
191 * let process sleep for a few seconds so shared
192 * memory system can be initialized.
194 prepare_to_wait(&orangefs_bufmap_init_waitq,
199 * Wait for orangefs_bufmap_initialize() to wake me up
200 * within the allotted time.
202 ret = schedule_timeout(
203 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ);
205 gossip_debug(GOSSIP_WAIT_DEBUG,
206 "Value returned from schedule_timeout:"
209 gossip_debug(GOSSIP_WAIT_DEBUG,
210 "Is shared memory available? (%d).\n",
211 orangefs_get_bufmap_init());
213 finish_wait(&orangefs_bufmap_init_waitq, &wait_entry);
215 if (orangefs_get_bufmap_init() == 0) {
216 gossip_err("%s:The shared memory system has not started in %d seconds after the client core restarted. Aborting user's request(%s).\n",
218 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS,
219 get_opname_string(op));
224 * Return to the calling function and re-populate a
225 * shared memory buffer.
231 gossip_debug(GOSSIP_WAIT_DEBUG,
232 "orangefs: service_operation %s returning: %d for %p.\n",
239 bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
242 if (!op_state_in_progress(op))
245 op->slot_to_free = op->upcall.req.io.buf_index;
246 memset(&op->upcall, 0, sizeof(op->upcall));
247 memset(&op->downcall, 0, sizeof(op->downcall));
248 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
249 op->upcall.req.cancel.op_tag = tag;
250 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
251 op->downcall.status = -1;
252 orangefs_new_tag(op);
254 spin_lock(&orangefs_request_list_lock);
255 /* orangefs_request_list_lock is enough of a barrier here */
256 if (!__is_daemon_in_service()) {
257 spin_unlock(&orangefs_request_list_lock);
260 spin_lock(&op->lock);
261 set_op_state_waiting(op);
262 list_add(&op->list, &orangefs_request_list);
263 spin_unlock(&op->lock);
264 spin_unlock(&orangefs_request_list_lock);
266 gossip_debug(GOSSIP_UTILS_DEBUG,
267 "Attempting ORANGEFS operation cancellation of tag %llu\n",
272 static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
275 * handle interrupted cases depending on what state we were in when
276 * the interruption is detected. there is a coarse grained lock
277 * across the operation.
279 * Called with op->lock held.
281 op->op_state |= OP_VFS_STATE_GIVEN_UP;
283 if (op_state_waiting(op)) {
285 * upcall hasn't been read; remove op from upcall request
288 spin_unlock(&op->lock);
289 spin_lock(&orangefs_request_list_lock);
291 spin_unlock(&orangefs_request_list_lock);
292 gossip_debug(GOSSIP_WAIT_DEBUG,
293 "Interrupted: Removed op %p from request_list\n",
295 } else if (op_state_in_progress(op)) {
296 /* op must be removed from the in progress htable */
297 spin_unlock(&op->lock);
298 spin_lock(&htable_ops_in_progress_lock);
300 spin_unlock(&htable_ops_in_progress_lock);
301 gossip_debug(GOSSIP_WAIT_DEBUG,
302 "Interrupted: Removed op %p"
303 " from htable_ops_in_progress\n",
305 } else if (!op_state_serviced(op)) {
306 spin_unlock(&op->lock);
307 gossip_err("interrupted operation is in a weird state 0x%x\n",
311 * It is not intended for execution to flow here,
312 * but having this unlock here makes sparse happy.
314 gossip_err("%s: can't get here.\n", __func__);
315 spin_unlock(&op->lock);
317 reinit_completion(&op->waitq);
321 * sleeps on waitqueue waiting for matching downcall.
322 * if client-core finishes servicing, then we are good to go.
323 * else if client-core exits, we get woken up here, and retry with a timeout
325 * Post when this call returns to the caller, the specified op will no
326 * longer be on any list or htable.
328 * Returns 0 on success and -errno on failure
330 * EAGAIN in case we want the caller to requeue and try again..
331 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
332 * operation since client-core seems to be exiting too often
333 * or if we were interrupted.
335 * Returns with op->lock taken.
337 static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op)
341 timeout = op->attempts ? op_timeout_secs * HZ : MAX_SCHEDULE_TIMEOUT;
342 n = wait_for_completion_interruptible_timeout(&op->waitq, timeout);
343 spin_lock(&op->lock);
345 if (op_state_serviced(op))
348 if (unlikely(n < 0)) {
349 gossip_debug(GOSSIP_WAIT_DEBUG,
351 " operation interrupted by a signal (tag "
359 if (op_state_purged(op)) {
360 gossip_debug(GOSSIP_WAIT_DEBUG,
362 " operation purged (tag "
363 "%llu, %p, att %d)\n",
368 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
372 /* must have timed out, then... */
373 gossip_debug(GOSSIP_WAIT_DEBUG,
375 " operation timed out (tag"
376 " %llu, %p, att %d)\n",