orangefs: get rid of loop in wait_for_matching_downcall()
[deliverable/linux.git] / fs / orangefs / waitqueue.c
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"
16 #include "orangefs-kernel.h"
17 #include "orangefs-bufmap.h"
18
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 *);
21
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 */
29 void purge_waiting_ops(void)
30 {
31 struct orangefs_kernel_op_s *op;
32
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",
37 llu(op->tag),
38 get_opname_string(op));
39 set_op_state_purged(op);
40 }
41 spin_unlock(&orangefs_request_list_lock);
42 }
43
44 static inline void
45 __add_op_to_request_list(struct orangefs_kernel_op_s *op)
46 {
47 spin_lock(&op->lock);
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);
52 }
53
54 static inline void
55 add_op_to_request_list(struct orangefs_kernel_op_s *op)
56 {
57 spin_lock(&orangefs_request_list_lock);
58 __add_op_to_request_list(op);
59 spin_unlock(&orangefs_request_list_lock);
60 }
61
62 static inline
63 void add_priority_op_to_request_list(struct orangefs_kernel_op_s *op)
64 {
65 spin_lock(&orangefs_request_list_lock);
66 spin_lock(&op->lock);
67 set_op_state_waiting(op);
68
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);
73 }
74
75 /*
76 * submits a ORANGEFS operation and waits for it to complete
77 *
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.
82 *
83 * Returns contents of op->downcall.status for convenience
84 */
85 int service_operation(struct orangefs_kernel_op_s *op,
86 const char *op_name,
87 int flags)
88 {
89 /* flags to modify behavior */
90 sigset_t orig_sigset;
91 int ret = 0;
92
93 DEFINE_WAIT(wait_entry);
94
95 op->upcall.tgid = current->tgid;
96 op->upcall.pid = current->pid;
97
98 retry_servicing:
99 op->downcall.status = 0;
100 gossip_debug(GOSSIP_WAIT_DEBUG,
101 "orangefs: service_operation: %s %p\n",
102 op_name,
103 op);
104 gossip_debug(GOSSIP_WAIT_DEBUG,
105 "orangefs: operation posted by process: %s, pid: %i\n",
106 current->comm,
107 current->pid);
108
109 /* mask out signals if this operation is not to be interrupted */
110 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
111 orangefs_block_signals(&orig_sigset);
112
113 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
114 ret = mutex_lock_interruptible(&request_mutex);
115 /*
116 * check to see if we were interrupted while waiting for
117 * semaphore
118 */
119 if (ret < 0) {
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");
125 return ret;
126 }
127 }
128
129 gossip_debug(GOSSIP_WAIT_DEBUG,
130 "%s:About to call is_daemon_in_service().\n",
131 __func__);
132
133 if (is_daemon_in_service() < 0) {
134 /*
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
138 */
139 gossip_debug(GOSSIP_WAIT_DEBUG,
140 "%s:client core is NOT in service(%d).\n",
141 __func__,
142 is_daemon_in_service());
143 op->attempts++;
144 }
145
146 /* queue up the operation */
147 if (flags & ORANGEFS_OP_PRIORITY) {
148 add_priority_op_to_request_list(op);
149 } else {
150 gossip_debug(GOSSIP_WAIT_DEBUG,
151 "%s:About to call add_op_to_request_list().\n",
152 __func__);
153 add_op_to_request_list(op);
154 }
155
156 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
157 mutex_unlock(&request_mutex);
158
159 /*
160 * If we are asked to service an asynchronous operation from
161 * VFS perspective, we are done.
162 */
163 if (flags & ORANGEFS_OP_ASYNC)
164 return 0;
165
166 ret = wait_for_matching_downcall(op);
167
168 if (ret < 0) {
169 /* failed to get matching downcall */
170 if (ret == -ETIMEDOUT) {
171 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
172 op_name);
173 }
174 orangefs_clean_up_interrupted_operation(op);
175 op->downcall.status = ret;
176 } else {
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;
182 }
183
184 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
185 orangefs_set_signals(&orig_sigset);
186
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",
193 llu(op->tag),
194 op_name,
195 op->attempts + 1);
196
197 if (!op->uses_shared_memory)
198 /*
199 * this operation doesn't use the shared memory
200 * system
201 */
202 goto retry_servicing;
203
204 /* op uses shared memory */
205 if (orangefs_get_bufmap_init() == 0) {
206 WARN_ON(1);
207 /*
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
212 * in process.
213 */
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",
223 op->op_state);
224
225 /*
226 * let process sleep for a few seconds so shared
227 * memory system can be initialized.
228 */
229 prepare_to_wait(&orangefs_bufmap_init_waitq,
230 &wait_entry,
231 TASK_INTERRUPTIBLE);
232
233 /*
234 * Wait for orangefs_bufmap_initialize() to wake me up
235 * within the allotted time.
236 */
237 ret = schedule_timeout(
238 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ);
239
240 gossip_debug(GOSSIP_WAIT_DEBUG,
241 "Value returned from schedule_timeout:"
242 "%d.\n",
243 ret);
244 gossip_debug(GOSSIP_WAIT_DEBUG,
245 "Is shared memory available? (%d).\n",
246 orangefs_get_bufmap_init());
247
248 finish_wait(&orangefs_bufmap_init_waitq, &wait_entry);
249
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",
252 __func__,
253 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS,
254 get_opname_string(op));
255 return -EIO;
256 }
257
258 /*
259 * Return to the calling function and re-populate a
260 * shared memory buffer.
261 */
262 return -EAGAIN;
263 }
264 }
265
266 gossip_debug(GOSSIP_WAIT_DEBUG,
267 "orangefs: service_operation %s returning: %d for %p.\n",
268 op_name,
269 ret,
270 op);
271 return ret;
272 }
273
274 bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
275 {
276 u64 tag = op->tag;
277 if (!op_state_in_progress(op))
278 return false;
279
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);
288
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);
293 return false;
294 }
295 __add_op_to_request_list(op);
296 spin_unlock(&orangefs_request_list_lock);
297
298 gossip_debug(GOSSIP_UTILS_DEBUG,
299 "Attempting ORANGEFS operation cancellation of tag %llu\n",
300 llu(tag));
301 return true;
302 }
303
304 static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
305 {
306 /*
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.
310 *
311 * Called with op->lock held.
312 */
313 op->op_state |= OP_VFS_STATE_GIVEN_UP;
314
315 if (op_state_waiting(op)) {
316 /*
317 * upcall hasn't been read; remove op from upcall request
318 * list.
319 */
320 spin_unlock(&op->lock);
321 spin_lock(&orangefs_request_list_lock);
322 list_del(&op->list);
323 spin_unlock(&orangefs_request_list_lock);
324 gossip_debug(GOSSIP_WAIT_DEBUG,
325 "Interrupted: Removed op %p from request_list\n",
326 op);
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);
331 list_del(&op->list);
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",
336 op);
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",
340 op->op_state);
341 } else {
342 /*
343 * It is not intended for execution to flow here,
344 * but having this unlock here makes sparse happy.
345 */
346 gossip_err("%s: can't get here.\n", __func__);
347 spin_unlock(&op->lock);
348 }
349 reinit_completion(&op->waitq);
350 }
351
352 /*
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
356 *
357 * Post when this call returns to the caller, the specified op will no
358 * longer be on any list or htable.
359 *
360 * Returns 0 on success and -errno on failure
361 * Errors are:
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.
366 *
367 * Returns with op->lock taken.
368 */
369 static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op)
370 {
371 long timeout, n;
372
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);
376
377 if (op_state_serviced(op))
378 return 0;
379
380 if (unlikely(n < 0)) {
381 gossip_debug(GOSSIP_WAIT_DEBUG,
382 "*** %s:"
383 " operation interrupted by a signal (tag "
384 "%llu, op %p)\n",
385 __func__,
386 llu(op->tag),
387 op);
388 return -EINTR;
389 }
390 op->attempts++;
391 if (op_state_purged(op)) {
392 gossip_debug(GOSSIP_WAIT_DEBUG,
393 "*** %s:"
394 " operation purged (tag "
395 "%llu, %p, att %d)\n",
396 __func__,
397 llu(op->tag),
398 op,
399 op->attempts);
400 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
401 -EAGAIN :
402 -EIO;
403 }
404 /* must have timed out, then... */
405 gossip_debug(GOSSIP_WAIT_DEBUG,
406 "*** %s:"
407 " operation timed out (tag"
408 " %llu, %p, att %d)\n",
409 __func__,
410 llu(op->tag),
411 op,
412 op->attempts);
413 return -ETIMEDOUT;
414 }
This page took 0.045183 seconds and 6 git commands to generate.