blob: 9b32286a7dc48dc50b7b20492db0ca06398953b9 [file] [log] [blame]
Mike Marshall1182fca2015-07-17 10:38:15 -04001/*
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"
16#include "pvfs2-kernel.h"
17#include "pvfs2-bufmap.h"
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{
28 struct pvfs2_kernel_op_s *op;
29
30 spin_lock(&pvfs2_request_list_lock);
31 list_for_each_entry(op, &pvfs2_request_list, list) {
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);
39 wake_up_interruptible(&op->waitq);
40 }
41 spin_unlock(&pvfs2_request_list_lock);
42}
43
44/*
45 * submits a PVFS2 operation and waits for it to complete
46 *
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.
51 *
52 * Returns contents of op->downcall.status for convenience
53 */
54int service_operation(struct pvfs2_kernel_op_s *op,
55 const char *op_name,
56 int flags)
57{
58 /* flags to modify behavior */
59 sigset_t orig_sigset;
60 int ret = 0;
61
62 /* irqflags and wait_entry are only used IF the client-core aborts */
63 unsigned long irqflags;
64
65 DECLARE_WAITQUEUE(wait_entry, current);
66
67 op->upcall.tgid = current->tgid;
68 op->upcall.pid = current->pid;
69
70retry_servicing:
71 op->downcall.status = 0;
72 gossip_debug(GOSSIP_WAIT_DEBUG,
73 "pvfs2: service_operation: %s %p\n",
74 op_name,
75 op);
76 gossip_debug(GOSSIP_WAIT_DEBUG,
77 "pvfs2: operation posted by process: %s, pid: %i\n",
78 current->comm,
79 current->pid);
80
81 /* mask out signals if this operation is not to be interrupted */
82 if (!(flags & PVFS2_OP_INTERRUPTIBLE))
83 mask_blocked_signals(&orig_sigset);
84
85 if (!(flags & PVFS2_OP_NO_SEMAPHORE)) {
86 ret = mutex_lock_interruptible(&request_mutex);
87 /*
88 * check to see if we were interrupted while waiting for
89 * semaphore
90 */
91 if (ret < 0) {
92 if (!(flags & PVFS2_OP_INTERRUPTIBLE))
93 unmask_blocked_signals(&orig_sigset);
94 op->downcall.status = ret;
95 gossip_debug(GOSSIP_WAIT_DEBUG,
96 "pvfs2: service_operation interrupted.\n");
97 return ret;
98 }
99 }
100
101 gossip_debug(GOSSIP_WAIT_DEBUG,
102 "%s:About to call is_daemon_in_service().\n",
103 __func__);
104
105 if (is_daemon_in_service() < 0) {
106 /*
107 * By incrementing the per-operation attempt counter, we
108 * directly go into the timeout logic while waiting for
109 * the matching downcall to be read
110 */
111 gossip_debug(GOSSIP_WAIT_DEBUG,
112 "%s:client core is NOT in service(%d).\n",
113 __func__,
114 is_daemon_in_service());
115 op->attempts++;
116 }
117
118 /* queue up the operation */
119 if (flags & PVFS2_OP_PRIORITY) {
120 add_priority_op_to_request_list(op);
121 } else {
122 gossip_debug(GOSSIP_WAIT_DEBUG,
123 "%s:About to call add_op_to_request_list().\n",
124 __func__);
125 add_op_to_request_list(op);
126 }
127
128 if (!(flags & PVFS2_OP_NO_SEMAPHORE))
129 mutex_unlock(&request_mutex);
130
131 /*
132 * If we are asked to service an asynchronous operation from
133 * VFS perspective, we are done.
134 */
135 if (flags & PVFS2_OP_ASYNC)
136 return 0;
137
138 if (flags & PVFS2_OP_CANCELLATION) {
139 gossip_debug(GOSSIP_WAIT_DEBUG,
140 "%s:"
141 "About to call wait_for_cancellation_downcall.\n",
142 __func__);
143 ret = wait_for_cancellation_downcall(op);
144 } else {
145 ret = wait_for_matching_downcall(op);
146 }
147
148 if (ret < 0) {
149 /* failed to get matching downcall */
150 if (ret == -ETIMEDOUT) {
151 gossip_err("pvfs2: %s -- wait timed out; aborting attempt.\n",
152 op_name);
153 }
154 op->downcall.status = ret;
155 } else {
156 /* got matching downcall; make sure status is in errno format */
157 op->downcall.status =
158 pvfs2_normalize_to_errno(op->downcall.status);
159 ret = op->downcall.status;
160 }
161
162 if (!(flags & PVFS2_OP_INTERRUPTIBLE))
163 unmask_blocked_signals(&orig_sigset);
164
165 BUG_ON(ret != op->downcall.status);
166 /* retry if operation has not been serviced and if requested */
167 if (!op_state_serviced(op) && op->downcall.status == -EAGAIN) {
168 gossip_debug(GOSSIP_WAIT_DEBUG,
169 "pvfs2: tag %llu (%s)"
170 " -- operation to be retried (%d attempt)\n",
171 llu(op->tag),
172 op_name,
173 op->attempts + 1);
174
175 if (!op->uses_shared_memory)
176 /*
177 * this operation doesn't use the shared memory
178 * system
179 */
180 goto retry_servicing;
181
182 /* op uses shared memory */
183 if (get_bufmap_init() == 0) {
184 /*
185 * This operation uses the shared memory system AND
186 * the system is not yet ready. This situation occurs
187 * when the client-core is restarted AND there were
188 * operations waiting to be processed or were already
189 * in process.
190 */
191 gossip_debug(GOSSIP_WAIT_DEBUG,
192 "uses_shared_memory is true.\n");
193 gossip_debug(GOSSIP_WAIT_DEBUG,
194 "Client core in-service status(%d).\n",
195 is_daemon_in_service());
196 gossip_debug(GOSSIP_WAIT_DEBUG, "bufmap_init:%d.\n",
197 get_bufmap_init());
198 gossip_debug(GOSSIP_WAIT_DEBUG,
199 "operation's status is 0x%0x.\n",
200 op->op_state);
201
202 /*
203 * let process sleep for a few seconds so shared
204 * memory system can be initialized.
205 */
206 spin_lock_irqsave(&op->lock, irqflags);
207 add_wait_queue(&pvfs2_bufmap_init_waitq, &wait_entry);
208 spin_unlock_irqrestore(&op->lock, irqflags);
209
210 set_current_state(TASK_INTERRUPTIBLE);
211
212 /*
213 * Wait for pvfs_bufmap_initialize() to wake me up
214 * within the allotted time.
215 */
216 ret = schedule_timeout(MSECS_TO_JIFFIES
217 (1000 * PVFS2_BUFMAP_WAIT_TIMEOUT_SECS));
218
219 gossip_debug(GOSSIP_WAIT_DEBUG,
220 "Value returned from schedule_timeout:"
221 "%d.\n",
222 ret);
223 gossip_debug(GOSSIP_WAIT_DEBUG,
224 "Is shared memory available? (%d).\n",
225 get_bufmap_init());
226
227 spin_lock_irqsave(&op->lock, irqflags);
228 remove_wait_queue(&pvfs2_bufmap_init_waitq,
229 &wait_entry);
230 spin_unlock_irqrestore(&op->lock, irqflags);
231
232 if (get_bufmap_init() == 0) {
233 gossip_err("%s:The shared memory system has not started in %d seconds after the client core restarted. Aborting user's request(%s).\n",
234 __func__,
235 PVFS2_BUFMAP_WAIT_TIMEOUT_SECS,
236 get_opname_string(op));
237 return -EIO;
238 }
239
240 /*
241 * Return to the calling function and re-populate a
242 * shared memory buffer.
243 */
244 return -EAGAIN;
245 }
246 }
247
248 gossip_debug(GOSSIP_WAIT_DEBUG,
249 "pvfs2: service_operation %s returning: %d for %p.\n",
250 op_name,
251 ret,
252 op);
253 return ret;
254}
255
256void pvfs2_clean_up_interrupted_operation(struct pvfs2_kernel_op_s *op)
257{
258 /*
259 * handle interrupted cases depending on what state we were in when
260 * the interruption is detected. there is a coarse grained lock
261 * across the operation.
262 *
263 * NOTE: be sure not to reverse lock ordering by locking an op lock
264 * while holding the request_list lock. Here, we first lock the op
265 * and then lock the appropriate list.
266 */
267 if (!op) {
268 gossip_debug(GOSSIP_WAIT_DEBUG,
269 "%s: op is null, ignoring\n",
270 __func__);
271 return;
272 }
273
274 /*
275 * one more sanity check, make sure it's in one of the possible states
276 * or don't try to cancel it
277 */
278 if (!(op_state_waiting(op) ||
279 op_state_in_progress(op) ||
280 op_state_serviced(op) ||
281 op_state_purged(op))) {
282 gossip_debug(GOSSIP_WAIT_DEBUG,
283 "%s: op %p not in a valid state (%0x), "
284 "ignoring\n",
285 __func__,
286 op,
287 op->op_state);
288 return;
289 }
290
291 spin_lock(&op->lock);
292
293 if (op_state_waiting(op)) {
294 /*
295 * upcall hasn't been read; remove op from upcall request
296 * list.
297 */
298 spin_unlock(&op->lock);
299 remove_op_from_request_list(op);
300 gossip_debug(GOSSIP_WAIT_DEBUG,
301 "Interrupted: Removed op %p from request_list\n",
302 op);
303 } else if (op_state_in_progress(op)) {
304 /* op must be removed from the in progress htable */
305 spin_unlock(&op->lock);
306 spin_lock(&htable_ops_in_progress_lock);
307 list_del(&op->list);
308 spin_unlock(&htable_ops_in_progress_lock);
309 gossip_debug(GOSSIP_WAIT_DEBUG,
310 "Interrupted: Removed op %p"
311 " from htable_ops_in_progress\n",
312 op);
313 } else if (!op_state_serviced(op)) {
314 spin_unlock(&op->lock);
315 gossip_err("interrupted operation is in a weird state 0x%x\n",
316 op->op_state);
317 }
318}
319
320/*
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
324 *
325 * Post when this call returns to the caller, the specified op will no
326 * longer be on any list or htable.
327 *
328 * Returns 0 on success and -errno on failure
329 * Errors are:
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.
334 */
335int wait_for_matching_downcall(struct pvfs2_kernel_op_s *op)
336{
337 int ret = -EINVAL;
338 DECLARE_WAITQUEUE(wait_entry, current);
339
340 spin_lock(&op->lock);
341 add_wait_queue(&op->waitq, &wait_entry);
342 spin_unlock(&op->lock);
343
344 while (1) {
345 set_current_state(TASK_INTERRUPTIBLE);
346
347 spin_lock(&op->lock);
348 if (op_state_serviced(op)) {
349 spin_unlock(&op->lock);
350 ret = 0;
351 break;
352 }
353 spin_unlock(&op->lock);
354
355 if (!signal_pending(current)) {
356 /*
357 * if this was our first attempt and client-core
358 * has not purged our operation, we are happy to
359 * simply wait
360 */
361 spin_lock(&op->lock);
362 if (op->attempts == 0 && !op_state_purged(op)) {
363 spin_unlock(&op->lock);
364 schedule();
365 } else {
366 spin_unlock(&op->lock);
367 /*
368 * subsequent attempts, we retry exactly once
369 * with timeouts
370 */
371 if (!schedule_timeout(MSECS_TO_JIFFIES
372 (1000 * op_timeout_secs))) {
373 gossip_debug(GOSSIP_WAIT_DEBUG,
374 "*** %s:"
375 " operation timed out (tag"
376 " %llu, %p, att %d)\n",
377 __func__,
378 llu(op->tag),
379 op,
380 op->attempts);
381 ret = -ETIMEDOUT;
382 pvfs2_clean_up_interrupted_operation
383 (op);
384 break;
385 }
386 }
387 spin_lock(&op->lock);
388 op->attempts++;
389 /*
390 * if the operation was purged in the meantime, it
391 * is better to requeue it afresh but ensure that
392 * we have not been purged repeatedly. This could
393 * happen if client-core crashes when an op
394 * is being serviced, so we requeue the op, client
395 * core crashes again so we requeue the op, client
396 * core starts, and so on...
397 */
398 if (op_state_purged(op)) {
399 ret = (op->attempts < PVFS2_PURGE_RETRY_COUNT) ?
400 -EAGAIN :
401 -EIO;
402 spin_unlock(&op->lock);
403 gossip_debug(GOSSIP_WAIT_DEBUG,
404 "*** %s:"
405 " operation purged (tag "
406 "%llu, %p, att %d)\n",
407 __func__,
408 llu(op->tag),
409 op,
410 op->attempts);
411 pvfs2_clean_up_interrupted_operation(op);
412 break;
413 }
414 spin_unlock(&op->lock);
415 continue;
416 }
417
418 gossip_debug(GOSSIP_WAIT_DEBUG,
419 "*** %s:"
420 " operation interrupted by a signal (tag "
421 "%llu, op %p)\n",
422 __func__,
423 llu(op->tag),
424 op);
425 pvfs2_clean_up_interrupted_operation(op);
426 ret = -EINTR;
427 break;
428 }
429
430 set_current_state(TASK_RUNNING);
431
432 spin_lock(&op->lock);
433 remove_wait_queue(&op->waitq, &wait_entry);
434 spin_unlock(&op->lock);
435
436 return ret;
437}
438
439/*
440 * similar to wait_for_matching_downcall(), but used in the special case
441 * of I/O cancellations.
442 *
443 * Note we need a special wait function because if this is called we already
444 * know that a signal is pending in current and need to service the
445 * cancellation upcall anyway. the only way to exit this is to either
446 * timeout or have the cancellation be serviced properly.
447 */
448int wait_for_cancellation_downcall(struct pvfs2_kernel_op_s *op)
449{
450 int ret = -EINVAL;
451 DECLARE_WAITQUEUE(wait_entry, current);
452
453 spin_lock(&op->lock);
454 add_wait_queue(&op->waitq, &wait_entry);
455 spin_unlock(&op->lock);
456
457 while (1) {
458 set_current_state(TASK_INTERRUPTIBLE);
459
460 spin_lock(&op->lock);
461 if (op_state_serviced(op)) {
462 gossip_debug(GOSSIP_WAIT_DEBUG,
463 "%s:op-state is SERVICED.\n",
464 __func__);
465 spin_unlock(&op->lock);
466 ret = 0;
467 break;
468 }
469 spin_unlock(&op->lock);
470
471 if (signal_pending(current)) {
472 gossip_debug(GOSSIP_WAIT_DEBUG,
473 "%s:operation interrupted by a signal (tag"
474 " %llu, op %p)\n",
475 __func__,
476 llu(op->tag),
477 op);
478 pvfs2_clean_up_interrupted_operation(op);
479 ret = -EINTR;
480 break;
481 }
482
483 gossip_debug(GOSSIP_WAIT_DEBUG,
484 "%s:About to call schedule_timeout.\n",
485 __func__);
486 ret =
487 schedule_timeout(MSECS_TO_JIFFIES(1000 * op_timeout_secs));
488
489 gossip_debug(GOSSIP_WAIT_DEBUG,
490 "%s:Value returned from schedule_timeout(%d).\n",
491 __func__,
492 ret);
493 if (!ret) {
494 gossip_debug(GOSSIP_WAIT_DEBUG,
495 "%s:*** operation timed out: %p\n",
496 __func__,
497 op);
498 pvfs2_clean_up_interrupted_operation(op);
499 ret = -ETIMEDOUT;
500 break;
501 }
502
503 gossip_debug(GOSSIP_WAIT_DEBUG,
504 "%s:Breaking out of loop, regardless of value returned by schedule_timeout.\n",
505 __func__);
506 ret = -ETIMEDOUT;
507 break;
508 }
509
510 set_current_state(TASK_RUNNING);
511
512 spin_lock(&op->lock);
513 remove_wait_queue(&op->waitq, &wait_entry);
514 spin_unlock(&op->lock);
515
516 gossip_debug(GOSSIP_WAIT_DEBUG,
517 "%s:returning ret(%d)\n",
518 __func__,
519 ret);
520
521 return ret;
522}