orangefs: use S_ISREG(mode) and friends instead of mode & S_IFREG.
[deliverable/linux.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
ade3d781
AV
19static int wait_for_matching_downcall(struct orangefs_kernel_op_s *);
20
1182fca3
MM
21/*
22 * What we do in this function is to walk the list of operations that are
23 * present in the request queue and mark them as purged.
24 * NOTE: This is called from the device close after client-core has
25 * guaranteed that no new operations could appear on the list since the
26 * client-core is anyway going to exit.
27 */
28void purge_waiting_ops(void)
29{
8bb8aefd 30 struct orangefs_kernel_op_s *op;
1182fca3 31
8bb8aefd
YL
32 spin_lock(&orangefs_request_list_lock);
33 list_for_each_entry(op, &orangefs_request_list, list) {
1182fca3
MM
34 gossip_debug(GOSSIP_WAIT_DEBUG,
35 "pvfs2-client-core: purging op tag %llu %s\n",
36 llu(op->tag),
37 get_opname_string(op));
1182fca3 38 set_op_state_purged(op);
1182fca3 39 }
8bb8aefd 40 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
41}
42
fc916da5 43static inline void
78699e29 44__add_op_to_request_list(struct orangefs_kernel_op_s *op)
fc916da5 45{
fc916da5
AV
46 spin_lock(&op->lock);
47 set_op_state_waiting(op);
48 list_add_tail(&op->list, &orangefs_request_list);
fc916da5
AV
49 spin_unlock(&op->lock);
50 wake_up_interruptible(&orangefs_request_list_waitq);
51}
52
78699e29
AV
53static inline void
54add_op_to_request_list(struct orangefs_kernel_op_s *op)
55{
56 spin_lock(&orangefs_request_list_lock);
57 __add_op_to_request_list(op);
58 spin_unlock(&orangefs_request_list_lock);
59}
60
fc916da5
AV
61static inline
62void add_priority_op_to_request_list(struct orangefs_kernel_op_s *op)
63{
64 spin_lock(&orangefs_request_list_lock);
65 spin_lock(&op->lock);
66 set_op_state_waiting(op);
67
68 list_add(&op->list, &orangefs_request_list);
69 spin_unlock(&orangefs_request_list_lock);
70 spin_unlock(&op->lock);
71 wake_up_interruptible(&orangefs_request_list_waitq);
72}
73
1182fca3 74/*
8bb8aefd 75 * submits a ORANGEFS operation and waits for it to complete
1182fca3
MM
76 *
77 * Note op->downcall.status will contain the status of the operation (in
78 * errno format), whether provided by pvfs2-client or a result of failure to
79 * service the operation. If the caller wishes to distinguish, then
80 * op->state can be checked to see if it was serviced or not.
81 *
82 * Returns contents of op->downcall.status for convenience
83 */
8bb8aefd 84int service_operation(struct orangefs_kernel_op_s *op,
1182fca3
MM
85 const char *op_name,
86 int flags)
87{
88 /* flags to modify behavior */
89 sigset_t orig_sigset;
90 int ret = 0;
91
ce6c414e 92 DEFINE_WAIT(wait_entry);
1182fca3
MM
93
94 op->upcall.tgid = current->tgid;
95 op->upcall.pid = current->pid;
96
97retry_servicing:
98 op->downcall.status = 0;
99 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 100 "orangefs: service_operation: %s %p\n",
1182fca3
MM
101 op_name,
102 op);
103 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 104 "orangefs: operation posted by process: %s, pid: %i\n",
1182fca3
MM
105 current->comm,
106 current->pid);
107
108 /* mask out signals if this operation is not to be interrupted */
8bb8aefd 109 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 110 orangefs_block_signals(&orig_sigset);
1182fca3 111
8bb8aefd 112 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
1182fca3
MM
113 ret = mutex_lock_interruptible(&request_mutex);
114 /*
115 * check to see if we were interrupted while waiting for
116 * semaphore
117 */
118 if (ret < 0) {
8bb8aefd 119 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 120 orangefs_set_signals(&orig_sigset);
1182fca3
MM
121 op->downcall.status = ret;
122 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 123 "orangefs: service_operation interrupted.\n");
1182fca3
MM
124 return ret;
125 }
126 }
127
128 gossip_debug(GOSSIP_WAIT_DEBUG,
129 "%s:About to call is_daemon_in_service().\n",
130 __func__);
131
132 if (is_daemon_in_service() < 0) {
133 /*
134 * By incrementing the per-operation attempt counter, we
135 * directly go into the timeout logic while waiting for
136 * the matching downcall to be read
137 */
138 gossip_debug(GOSSIP_WAIT_DEBUG,
139 "%s:client core is NOT in service(%d).\n",
140 __func__,
141 is_daemon_in_service());
142 op->attempts++;
143 }
144
145 /* queue up the operation */
8bb8aefd 146 if (flags & ORANGEFS_OP_PRIORITY) {
1182fca3
MM
147 add_priority_op_to_request_list(op);
148 } else {
149 gossip_debug(GOSSIP_WAIT_DEBUG,
150 "%s:About to call add_op_to_request_list().\n",
151 __func__);
152 add_op_to_request_list(op);
153 }
154
8bb8aefd 155 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
1182fca3
MM
156 mutex_unlock(&request_mutex);
157
158 /*
159 * If we are asked to service an asynchronous operation from
160 * VFS perspective, we are done.
161 */
8bb8aefd 162 if (flags & ORANGEFS_OP_ASYNC)
1182fca3
MM
163 return 0;
164
78699e29 165 ret = wait_for_matching_downcall(op);
1182fca3
MM
166
167 if (ret < 0) {
168 /* failed to get matching downcall */
169 if (ret == -ETIMEDOUT) {
8bb8aefd 170 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
1182fca3
MM
171 op_name);
172 }
173 op->downcall.status = ret;
174 } else {
175 /* got matching downcall; make sure status is in errno format */
176 op->downcall.status =
8bb8aefd 177 orangefs_normalize_to_errno(op->downcall.status);
1182fca3
MM
178 ret = op->downcall.status;
179 }
180
8bb8aefd 181 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 182 orangefs_set_signals(&orig_sigset);
1182fca3
MM
183
184 BUG_ON(ret != op->downcall.status);
185 /* retry if operation has not been serviced and if requested */
186 if (!op_state_serviced(op) && op->downcall.status == -EAGAIN) {
187 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 188 "orangefs: tag %llu (%s)"
1182fca3
MM
189 " -- operation to be retried (%d attempt)\n",
190 llu(op->tag),
191 op_name,
192 op->attempts + 1);
193
194 if (!op->uses_shared_memory)
195 /*
196 * this operation doesn't use the shared memory
197 * system
198 */
199 goto retry_servicing;
200
201 /* op uses shared memory */
7d221485 202 if (orangefs_get_bufmap_init() == 0) {
6ebcc3fc 203 WARN_ON(1);
1182fca3
MM
204 /*
205 * This operation uses the shared memory system AND
206 * the system is not yet ready. This situation occurs
207 * when the client-core is restarted AND there were
208 * operations waiting to be processed or were already
209 * in process.
210 */
211 gossip_debug(GOSSIP_WAIT_DEBUG,
212 "uses_shared_memory is true.\n");
213 gossip_debug(GOSSIP_WAIT_DEBUG,
214 "Client core in-service status(%d).\n",
215 is_daemon_in_service());
216 gossip_debug(GOSSIP_WAIT_DEBUG, "bufmap_init:%d.\n",
7d221485 217 orangefs_get_bufmap_init());
1182fca3
MM
218 gossip_debug(GOSSIP_WAIT_DEBUG,
219 "operation's status is 0x%0x.\n",
220 op->op_state);
221
222 /*
223 * let process sleep for a few seconds so shared
224 * memory system can be initialized.
225 */
ce6c414e
MM
226 prepare_to_wait(&orangefs_bufmap_init_waitq,
227 &wait_entry,
228 TASK_INTERRUPTIBLE);
1182fca3 229
1182fca3 230 /*
8bb8aefd 231 * Wait for orangefs_bufmap_initialize() to wake me up
1182fca3
MM
232 * within the allotted time.
233 */
727cbfea
AV
234 ret = schedule_timeout(
235 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ);
1182fca3
MM
236
237 gossip_debug(GOSSIP_WAIT_DEBUG,
238 "Value returned from schedule_timeout:"
239 "%d.\n",
240 ret);
241 gossip_debug(GOSSIP_WAIT_DEBUG,
242 "Is shared memory available? (%d).\n",
7d221485 243 orangefs_get_bufmap_init());
1182fca3 244
ce6c414e 245 finish_wait(&orangefs_bufmap_init_waitq, &wait_entry);
1182fca3 246
7d221485 247 if (orangefs_get_bufmap_init() == 0) {
1182fca3
MM
248 gossip_err("%s:The shared memory system has not started in %d seconds after the client core restarted. Aborting user's request(%s).\n",
249 __func__,
8bb8aefd 250 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS,
1182fca3
MM
251 get_opname_string(op));
252 return -EIO;
253 }
254
255 /*
256 * Return to the calling function and re-populate a
257 * shared memory buffer.
258 */
259 return -EAGAIN;
260 }
261 }
262
263 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 264 "orangefs: service_operation %s returning: %d for %p.\n",
1182fca3
MM
265 op_name,
266 ret,
267 op);
268 return ret;
269}
270
78699e29
AV
271bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
272{
273 u64 tag = op->tag;
274 if (!op_state_in_progress(op))
275 return false;
276
277 op->slot_to_free = op->upcall.req.io.buf_index;
278 memset(&op->upcall, 0, sizeof(op->upcall));
279 memset(&op->downcall, 0, sizeof(op->downcall));
280 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
281 op->upcall.req.cancel.op_tag = tag;
282 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
283 op->downcall.status = -1;
284 orangefs_new_tag(op);
285
286 spin_lock(&orangefs_request_list_lock);
287 /* orangefs_request_list_lock is enough of a barrier here */
288 if (!__is_daemon_in_service()) {
289 spin_unlock(&orangefs_request_list_lock);
290 return false;
291 }
292 __add_op_to_request_list(op);
293 spin_unlock(&orangefs_request_list_lock);
294
295 gossip_debug(GOSSIP_UTILS_DEBUG,
296 "Attempting ORANGEFS operation cancellation of tag %llu\n",
297 llu(tag));
298 return true;
299}
300
e07db0a2 301static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
1182fca3
MM
302{
303 /*
304 * handle interrupted cases depending on what state we were in when
305 * the interruption is detected. there is a coarse grained lock
306 * across the operation.
307 *
eab9b389 308 * Called with op->lock held.
1182fca3 309 */
ed42fe05 310 op->op_state |= OP_VFS_STATE_GIVEN_UP;
1182fca3
MM
311
312 if (op_state_waiting(op)) {
313 /*
314 * upcall hasn't been read; remove op from upcall request
315 * list.
316 */
317 spin_unlock(&op->lock);
ed42fe05
AV
318 spin_lock(&orangefs_request_list_lock);
319 list_del(&op->list);
320 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
321 gossip_debug(GOSSIP_WAIT_DEBUG,
322 "Interrupted: Removed op %p from request_list\n",
323 op);
324 } else if (op_state_in_progress(op)) {
325 /* op must be removed from the in progress htable */
326 spin_unlock(&op->lock);
327 spin_lock(&htable_ops_in_progress_lock);
328 list_del(&op->list);
329 spin_unlock(&htable_ops_in_progress_lock);
330 gossip_debug(GOSSIP_WAIT_DEBUG,
331 "Interrupted: Removed op %p"
332 " from htable_ops_in_progress\n",
333 op);
334 } else if (!op_state_serviced(op)) {
335 spin_unlock(&op->lock);
336 gossip_err("interrupted operation is in a weird state 0x%x\n",
337 op->op_state);
84d02150
MM
338 } else {
339 /*
340 * It is not intended for execution to flow here,
341 * but having this unlock here makes sparse happy.
342 */
343 gossip_err("%s: can't get here.\n", __func__);
344 spin_unlock(&op->lock);
1182fca3
MM
345 }
346}
347
348/*
349 * sleeps on waitqueue waiting for matching downcall.
350 * if client-core finishes servicing, then we are good to go.
351 * else if client-core exits, we get woken up here, and retry with a timeout
352 *
353 * Post when this call returns to the caller, the specified op will no
354 * longer be on any list or htable.
355 *
356 * Returns 0 on success and -errno on failure
357 * Errors are:
358 * EAGAIN in case we want the caller to requeue and try again..
359 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
360 * operation since client-core seems to be exiting too often
361 * or if we were interrupted.
362 */
b7ae37b0 363static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op)
1182fca3
MM
364{
365 int ret = -EINVAL;
ce6c414e 366 DEFINE_WAIT(wait_entry);
1182fca3
MM
367
368 while (1) {
1182fca3 369 spin_lock(&op->lock);
ce6c414e 370 prepare_to_wait(&op->waitq, &wait_entry, TASK_INTERRUPTIBLE);
1182fca3
MM
371 if (op_state_serviced(op)) {
372 spin_unlock(&op->lock);
373 ret = 0;
374 break;
375 }
1182fca3 376
70c6ea26
AV
377 if (unlikely(signal_pending(current))) {
378 gossip_debug(GOSSIP_WAIT_DEBUG,
379 "*** %s:"
380 " operation interrupted by a signal (tag "
381 "%llu, op %p)\n",
382 __func__,
383 llu(op->tag),
384 op);
385 orangefs_clean_up_interrupted_operation(op);
386 ret = -EINTR;
387 break;
388 }
389
390 /*
391 * if this was our first attempt and client-core
392 * has not purged our operation, we are happy to
393 * simply wait
394 */
70c6ea26
AV
395 if (op->attempts == 0 && !op_state_purged(op)) {
396 spin_unlock(&op->lock);
397 schedule();
398 } else {
399 spin_unlock(&op->lock);
1182fca3 400 /*
70c6ea26
AV
401 * subsequent attempts, we retry exactly once
402 * with timeouts
1182fca3 403 */
727cbfea 404 if (!schedule_timeout(op_timeout_secs * HZ)) {
1182fca3
MM
405 gossip_debug(GOSSIP_WAIT_DEBUG,
406 "*** %s:"
70c6ea26
AV
407 " operation timed out (tag"
408 " %llu, %p, att %d)\n",
1182fca3
MM
409 __func__,
410 llu(op->tag),
411 op,
412 op->attempts);
70c6ea26 413 ret = -ETIMEDOUT;
eab9b389 414 spin_lock(&op->lock);
8bb8aefd 415 orangefs_clean_up_interrupted_operation(op);
1182fca3
MM
416 break;
417 }
70c6ea26
AV
418 }
419 spin_lock(&op->lock);
420 op->attempts++;
421 /*
422 * if the operation was purged in the meantime, it
423 * is better to requeue it afresh but ensure that
424 * we have not been purged repeatedly. This could
425 * happen if client-core crashes when an op
426 * is being serviced, so we requeue the op, client
427 * core crashes again so we requeue the op, client
428 * core starts, and so on...
429 */
430 if (op_state_purged(op)) {
431 ret = (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
432 -EAGAIN :
433 -EIO;
70c6ea26
AV
434 gossip_debug(GOSSIP_WAIT_DEBUG,
435 "*** %s:"
436 " operation purged (tag "
437 "%llu, %p, att %d)\n",
438 __func__,
439 llu(op->tag),
440 op,
441 op->attempts);
442 orangefs_clean_up_interrupted_operation(op);
443 break;
1182fca3 444 }
70c6ea26 445 spin_unlock(&op->lock);
1182fca3
MM
446 }
447
1182fca3 448 spin_lock(&op->lock);
ce6c414e 449 finish_wait(&op->waitq, &wait_entry);
1182fca3
MM
450 spin_unlock(&op->lock);
451
452 return ret;
453}
This page took 0.05373 seconds and 5 git commands to generate.