service_operation(): don't block signals, just use ..._killable
[linux-block.git] / fs / orangefs / waitqueue.c
CommitLineData
1182fca3
MM
1/*
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"
575e9461
MM
16#include "orangefs-kernel.h"
17#include "orangefs-bufmap.h"
1182fca3 18
c72f15b7 19static int wait_for_matching_downcall(struct orangefs_kernel_op_s *, bool);
d2d87a3b 20static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *);
ade3d781 21
1182fca3
MM
22/*
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.
28 */
29void purge_waiting_ops(void)
30{
8bb8aefd 31 struct orangefs_kernel_op_s *op;
1182fca3 32
8bb8aefd
YL
33 spin_lock(&orangefs_request_list_lock);
34 list_for_each_entry(op, &orangefs_request_list, list) {
1182fca3
MM
35 gossip_debug(GOSSIP_WAIT_DEBUG,
36 "pvfs2-client-core: purging op tag %llu %s\n",
37 llu(op->tag),
38 get_opname_string(op));
1182fca3 39 set_op_state_purged(op);
1182fca3 40 }
8bb8aefd 41 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
42}
43
44/*
8bb8aefd 45 * submits a ORANGEFS operation and waits for it to complete
1182fca3
MM
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 */
8bb8aefd 54int service_operation(struct orangefs_kernel_op_s *op,
1182fca3
MM
55 const char *op_name,
56 int flags)
57{
58 /* flags to modify behavior */
1182fca3
MM
59 int ret = 0;
60
ce6c414e 61 DEFINE_WAIT(wait_entry);
1182fca3
MM
62
63 op->upcall.tgid = current->tgid;
64 op->upcall.pid = current->pid;
65
66retry_servicing:
67 op->downcall.status = 0;
68 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 69 "orangefs: service_operation: %s %p\n",
1182fca3
MM
70 op_name,
71 op);
72 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 73 "orangefs: operation posted by process: %s, pid: %i\n",
1182fca3
MM
74 current->comm,
75 current->pid);
76
8bb8aefd 77 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
c72f15b7
AV
78 if (flags & ORANGEFS_OP_INTERRUPTIBLE)
79 ret = mutex_lock_interruptible(&request_mutex);
80 else
81 ret = mutex_lock_killable(&request_mutex);
1182fca3
MM
82 /*
83 * check to see if we were interrupted while waiting for
84 * semaphore
85 */
86 if (ret < 0) {
1182fca3
MM
87 op->downcall.status = ret;
88 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 89 "orangefs: service_operation interrupted.\n");
1182fca3
MM
90 return ret;
91 }
92 }
93
98815ade
AV
94 /* queue up the operation */
95 spin_lock(&orangefs_request_list_lock);
96 spin_lock(&op->lock);
97 set_op_state_waiting(op);
98 if (flags & ORANGEFS_OP_PRIORITY)
99 list_add(&op->list, &orangefs_request_list);
100 else
101 list_add_tail(&op->list, &orangefs_request_list);
102 spin_unlock(&op->lock);
103 wake_up_interruptible(&orangefs_request_list_waitq);
104 if (!__is_daemon_in_service()) {
1182fca3
MM
105 /*
106 * By incrementing the per-operation attempt counter, we
107 * directly go into the timeout logic while waiting for
108 * the matching downcall to be read
109 */
110 gossip_debug(GOSSIP_WAIT_DEBUG,
98815ade 111 "%s:client core is NOT in service.\n",
1182fca3 112 __func__);
98815ade 113 op->attempts++;
1182fca3 114 }
98815ade 115 spin_unlock(&orangefs_request_list_lock);
1182fca3 116
8bb8aefd 117 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
1182fca3
MM
118 mutex_unlock(&request_mutex);
119
120 /*
121 * If we are asked to service an asynchronous operation from
122 * VFS perspective, we are done.
123 */
8bb8aefd 124 if (flags & ORANGEFS_OP_ASYNC)
1182fca3
MM
125 return 0;
126
c72f15b7 127 ret = wait_for_matching_downcall(op, flags & ORANGEFS_OP_INTERRUPTIBLE);
1182fca3
MM
128
129 if (ret < 0) {
130 /* failed to get matching downcall */
131 if (ret == -ETIMEDOUT) {
8bb8aefd 132 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
1182fca3
MM
133 op_name);
134 }
d2d87a3b 135 orangefs_clean_up_interrupted_operation(op);
1182fca3
MM
136 op->downcall.status = ret;
137 } else {
d2d87a3b 138 spin_unlock(&op->lock);
1182fca3
MM
139 /* got matching downcall; make sure status is in errno format */
140 op->downcall.status =
8bb8aefd 141 orangefs_normalize_to_errno(op->downcall.status);
1182fca3
MM
142 ret = op->downcall.status;
143 }
144
1182fca3
MM
145 BUG_ON(ret != op->downcall.status);
146 /* retry if operation has not been serviced and if requested */
147 if (!op_state_serviced(op) && op->downcall.status == -EAGAIN) {
148 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 149 "orangefs: tag %llu (%s)"
1182fca3
MM
150 " -- operation to be retried (%d attempt)\n",
151 llu(op->tag),
152 op_name,
153 op->attempts + 1);
154
155 if (!op->uses_shared_memory)
156 /*
157 * this operation doesn't use the shared memory
158 * system
159 */
160 goto retry_servicing;
161
162 /* op uses shared memory */
7d221485 163 if (orangefs_get_bufmap_init() == 0) {
6ebcc3fc 164 WARN_ON(1);
1182fca3
MM
165 /*
166 * This operation uses the shared memory system AND
167 * the system is not yet ready. This situation occurs
168 * when the client-core is restarted AND there were
169 * operations waiting to be processed or were already
170 * in process.
171 */
172 gossip_debug(GOSSIP_WAIT_DEBUG,
173 "uses_shared_memory is true.\n");
174 gossip_debug(GOSSIP_WAIT_DEBUG,
175 "Client core in-service status(%d).\n",
176 is_daemon_in_service());
177 gossip_debug(GOSSIP_WAIT_DEBUG, "bufmap_init:%d.\n",
7d221485 178 orangefs_get_bufmap_init());
1182fca3
MM
179 gossip_debug(GOSSIP_WAIT_DEBUG,
180 "operation's status is 0x%0x.\n",
181 op->op_state);
182
183 /*
184 * let process sleep for a few seconds so shared
185 * memory system can be initialized.
186 */
ce6c414e
MM
187 prepare_to_wait(&orangefs_bufmap_init_waitq,
188 &wait_entry,
189 TASK_INTERRUPTIBLE);
1182fca3 190
1182fca3 191 /*
8bb8aefd 192 * Wait for orangefs_bufmap_initialize() to wake me up
1182fca3
MM
193 * within the allotted time.
194 */
727cbfea
AV
195 ret = schedule_timeout(
196 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ);
1182fca3
MM
197
198 gossip_debug(GOSSIP_WAIT_DEBUG,
199 "Value returned from schedule_timeout:"
200 "%d.\n",
201 ret);
202 gossip_debug(GOSSIP_WAIT_DEBUG,
203 "Is shared memory available? (%d).\n",
7d221485 204 orangefs_get_bufmap_init());
1182fca3 205
ce6c414e 206 finish_wait(&orangefs_bufmap_init_waitq, &wait_entry);
1182fca3 207
7d221485 208 if (orangefs_get_bufmap_init() == 0) {
1182fca3
MM
209 gossip_err("%s:The shared memory system has not started in %d seconds after the client core restarted. Aborting user's request(%s).\n",
210 __func__,
8bb8aefd 211 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS,
1182fca3
MM
212 get_opname_string(op));
213 return -EIO;
214 }
215
216 /*
217 * Return to the calling function and re-populate a
218 * shared memory buffer.
219 */
220 return -EAGAIN;
221 }
222 }
223
224 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 225 "orangefs: service_operation %s returning: %d for %p.\n",
1182fca3
MM
226 op_name,
227 ret,
228 op);
229 return ret;
230}
231
78699e29
AV
232bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
233{
234 u64 tag = op->tag;
235 if (!op_state_in_progress(op))
236 return false;
237
238 op->slot_to_free = op->upcall.req.io.buf_index;
239 memset(&op->upcall, 0, sizeof(op->upcall));
240 memset(&op->downcall, 0, sizeof(op->downcall));
241 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
242 op->upcall.req.cancel.op_tag = tag;
243 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
244 op->downcall.status = -1;
245 orangefs_new_tag(op);
246
247 spin_lock(&orangefs_request_list_lock);
248 /* orangefs_request_list_lock is enough of a barrier here */
249 if (!__is_daemon_in_service()) {
250 spin_unlock(&orangefs_request_list_lock);
251 return false;
252 }
98815ade
AV
253 spin_lock(&op->lock);
254 set_op_state_waiting(op);
255 list_add(&op->list, &orangefs_request_list);
256 spin_unlock(&op->lock);
78699e29
AV
257 spin_unlock(&orangefs_request_list_lock);
258
259 gossip_debug(GOSSIP_UTILS_DEBUG,
260 "Attempting ORANGEFS operation cancellation of tag %llu\n",
261 llu(tag));
262 return true;
263}
264
e07db0a2 265static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
1182fca3
MM
266{
267 /*
268 * handle interrupted cases depending on what state we were in when
269 * the interruption is detected. there is a coarse grained lock
270 * across the operation.
271 *
eab9b389 272 * Called with op->lock held.
1182fca3 273 */
ed42fe05 274 op->op_state |= OP_VFS_STATE_GIVEN_UP;
1182fca3
MM
275
276 if (op_state_waiting(op)) {
277 /*
278 * upcall hasn't been read; remove op from upcall request
279 * list.
280 */
281 spin_unlock(&op->lock);
ed42fe05
AV
282 spin_lock(&orangefs_request_list_lock);
283 list_del(&op->list);
284 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
285 gossip_debug(GOSSIP_WAIT_DEBUG,
286 "Interrupted: Removed op %p from request_list\n",
287 op);
288 } else if (op_state_in_progress(op)) {
289 /* op must be removed from the in progress htable */
290 spin_unlock(&op->lock);
291 spin_lock(&htable_ops_in_progress_lock);
292 list_del(&op->list);
293 spin_unlock(&htable_ops_in_progress_lock);
294 gossip_debug(GOSSIP_WAIT_DEBUG,
295 "Interrupted: Removed op %p"
296 " from htable_ops_in_progress\n",
297 op);
298 } else if (!op_state_serviced(op)) {
299 spin_unlock(&op->lock);
300 gossip_err("interrupted operation is in a weird state 0x%x\n",
301 op->op_state);
84d02150
MM
302 } else {
303 /*
304 * It is not intended for execution to flow here,
305 * but having this unlock here makes sparse happy.
306 */
307 gossip_err("%s: can't get here.\n", __func__);
308 spin_unlock(&op->lock);
1182fca3 309 }
d2d87a3b 310 reinit_completion(&op->waitq);
1182fca3
MM
311}
312
313/*
314 * sleeps on waitqueue waiting for matching downcall.
315 * if client-core finishes servicing, then we are good to go.
316 * else if client-core exits, we get woken up here, and retry with a timeout
317 *
318 * Post when this call returns to the caller, the specified op will no
319 * longer be on any list or htable.
320 *
321 * Returns 0 on success and -errno on failure
322 * Errors are:
323 * EAGAIN in case we want the caller to requeue and try again..
324 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
325 * operation since client-core seems to be exiting too often
326 * or if we were interrupted.
d2d87a3b
AV
327 *
328 * Returns with op->lock taken.
1182fca3 329 */
c72f15b7
AV
330static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
331 bool interruptible)
1182fca3 332{
d2d87a3b 333 long timeout, n;
1182fca3 334
d2d87a3b 335 timeout = op->attempts ? op_timeout_secs * HZ : MAX_SCHEDULE_TIMEOUT;
c72f15b7
AV
336
337 if (interruptible)
338 n = wait_for_completion_interruptible_timeout(&op->waitq, timeout);
339 else
340 n = wait_for_completion_killable_timeout(&op->waitq, timeout);
341
d2d87a3b 342 spin_lock(&op->lock);
1182fca3 343
d2d87a3b
AV
344 if (op_state_serviced(op))
345 return 0;
70c6ea26 346
d2d87a3b
AV
347 if (unlikely(n < 0)) {
348 gossip_debug(GOSSIP_WAIT_DEBUG,
349 "*** %s:"
350 " operation interrupted by a signal (tag "
351 "%llu, op %p)\n",
352 __func__,
353 llu(op->tag),
354 op);
355 return -EINTR;
1182fca3 356 }
d2d87a3b
AV
357 op->attempts++;
358 if (op_state_purged(op)) {
359 gossip_debug(GOSSIP_WAIT_DEBUG,
360 "*** %s:"
361 " operation purged (tag "
362 "%llu, %p, att %d)\n",
363 __func__,
364 llu(op->tag),
365 op,
366 op->attempts);
367 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
368 -EAGAIN :
369 -EIO;
370 }
371 /* must have timed out, then... */
372 gossip_debug(GOSSIP_WAIT_DEBUG,
373 "*** %s:"
374 " operation timed out (tag"
375 " %llu, %p, att %d)\n",
376 __func__,
377 llu(op->tag),
378 op,
379 op->attempts);
380 return -ETIMEDOUT;
1182fca3 381}