orangefs: sanitize handling of request list
[linux-2.6-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
ade3d781 19static int wait_for_matching_downcall(struct orangefs_kernel_op_s *);
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 */
59 sigset_t orig_sigset;
60 int ret = 0;
61
ce6c414e 62 DEFINE_WAIT(wait_entry);
1182fca3
MM
63
64 op->upcall.tgid = current->tgid;
65 op->upcall.pid = current->pid;
66
67retry_servicing:
68 op->downcall.status = 0;
69 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 70 "orangefs: service_operation: %s %p\n",
1182fca3
MM
71 op_name,
72 op);
73 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 74 "orangefs: operation posted by process: %s, pid: %i\n",
1182fca3
MM
75 current->comm,
76 current->pid);
77
78 /* mask out signals if this operation is not to be interrupted */
8bb8aefd 79 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 80 orangefs_block_signals(&orig_sigset);
1182fca3 81
8bb8aefd 82 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
1182fca3
MM
83 ret = mutex_lock_interruptible(&request_mutex);
84 /*
85 * check to see if we were interrupted while waiting for
86 * semaphore
87 */
88 if (ret < 0) {
8bb8aefd 89 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 90 orangefs_set_signals(&orig_sigset);
1182fca3
MM
91 op->downcall.status = ret;
92 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 93 "orangefs: service_operation interrupted.\n");
1182fca3
MM
94 return ret;
95 }
96 }
97
98815ade
AV
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);
104 else
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()) {
1182fca3
MM
109 /*
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
113 */
114 gossip_debug(GOSSIP_WAIT_DEBUG,
98815ade 115 "%s:client core is NOT in service.\n",
1182fca3 116 __func__);
98815ade 117 op->attempts++;
1182fca3 118 }
98815ade 119 spin_unlock(&orangefs_request_list_lock);
1182fca3 120
8bb8aefd 121 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
1182fca3
MM
122 mutex_unlock(&request_mutex);
123
124 /*
125 * If we are asked to service an asynchronous operation from
126 * VFS perspective, we are done.
127 */
8bb8aefd 128 if (flags & ORANGEFS_OP_ASYNC)
1182fca3
MM
129 return 0;
130
78699e29 131 ret = wait_for_matching_downcall(op);
1182fca3
MM
132
133 if (ret < 0) {
134 /* failed to get matching downcall */
135 if (ret == -ETIMEDOUT) {
8bb8aefd 136 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
1182fca3
MM
137 op_name);
138 }
d2d87a3b 139 orangefs_clean_up_interrupted_operation(op);
1182fca3
MM
140 op->downcall.status = ret;
141 } else {
d2d87a3b 142 spin_unlock(&op->lock);
1182fca3
MM
143 /* got matching downcall; make sure status is in errno format */
144 op->downcall.status =
8bb8aefd 145 orangefs_normalize_to_errno(op->downcall.status);
1182fca3
MM
146 ret = op->downcall.status;
147 }
148
8bb8aefd 149 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
c146c0b8 150 orangefs_set_signals(&orig_sigset);
1182fca3
MM
151
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,
8bb8aefd 156 "orangefs: tag %llu (%s)"
1182fca3
MM
157 " -- operation to be retried (%d attempt)\n",
158 llu(op->tag),
159 op_name,
160 op->attempts + 1);
161
162 if (!op->uses_shared_memory)
163 /*
164 * this operation doesn't use the shared memory
165 * system
166 */
167 goto retry_servicing;
168
169 /* op uses shared memory */
7d221485 170 if (orangefs_get_bufmap_init() == 0) {
6ebcc3fc 171 WARN_ON(1);
1182fca3
MM
172 /*
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
177 * in process.
178 */
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",
7d221485 185 orangefs_get_bufmap_init());
1182fca3
MM
186 gossip_debug(GOSSIP_WAIT_DEBUG,
187 "operation's status is 0x%0x.\n",
188 op->op_state);
189
190 /*
191 * let process sleep for a few seconds so shared
192 * memory system can be initialized.
193 */
ce6c414e
MM
194 prepare_to_wait(&orangefs_bufmap_init_waitq,
195 &wait_entry,
196 TASK_INTERRUPTIBLE);
1182fca3 197
1182fca3 198 /*
8bb8aefd 199 * Wait for orangefs_bufmap_initialize() to wake me up
1182fca3
MM
200 * within the allotted time.
201 */
727cbfea
AV
202 ret = schedule_timeout(
203 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ);
1182fca3
MM
204
205 gossip_debug(GOSSIP_WAIT_DEBUG,
206 "Value returned from schedule_timeout:"
207 "%d.\n",
208 ret);
209 gossip_debug(GOSSIP_WAIT_DEBUG,
210 "Is shared memory available? (%d).\n",
7d221485 211 orangefs_get_bufmap_init());
1182fca3 212
ce6c414e 213 finish_wait(&orangefs_bufmap_init_waitq, &wait_entry);
1182fca3 214
7d221485 215 if (orangefs_get_bufmap_init() == 0) {
1182fca3
MM
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",
217 __func__,
8bb8aefd 218 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS,
1182fca3
MM
219 get_opname_string(op));
220 return -EIO;
221 }
222
223 /*
224 * Return to the calling function and re-populate a
225 * shared memory buffer.
226 */
227 return -EAGAIN;
228 }
229 }
230
231 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 232 "orangefs: service_operation %s returning: %d for %p.\n",
1182fca3
MM
233 op_name,
234 ret,
235 op);
236 return ret;
237}
238
78699e29
AV
239bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
240{
241 u64 tag = op->tag;
242 if (!op_state_in_progress(op))
243 return false;
244
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);
253
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);
258 return false;
259 }
98815ade
AV
260 spin_lock(&op->lock);
261 set_op_state_waiting(op);
262 list_add(&op->list, &orangefs_request_list);
263 spin_unlock(&op->lock);
78699e29
AV
264 spin_unlock(&orangefs_request_list_lock);
265
266 gossip_debug(GOSSIP_UTILS_DEBUG,
267 "Attempting ORANGEFS operation cancellation of tag %llu\n",
268 llu(tag));
269 return true;
270}
271
e07db0a2 272static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
1182fca3
MM
273{
274 /*
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.
278 *
eab9b389 279 * Called with op->lock held.
1182fca3 280 */
ed42fe05 281 op->op_state |= OP_VFS_STATE_GIVEN_UP;
1182fca3
MM
282
283 if (op_state_waiting(op)) {
284 /*
285 * upcall hasn't been read; remove op from upcall request
286 * list.
287 */
288 spin_unlock(&op->lock);
ed42fe05
AV
289 spin_lock(&orangefs_request_list_lock);
290 list_del(&op->list);
291 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
292 gossip_debug(GOSSIP_WAIT_DEBUG,
293 "Interrupted: Removed op %p from request_list\n",
294 op);
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);
299 list_del(&op->list);
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",
304 op);
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",
308 op->op_state);
84d02150
MM
309 } else {
310 /*
311 * It is not intended for execution to flow here,
312 * but having this unlock here makes sparse happy.
313 */
314 gossip_err("%s: can't get here.\n", __func__);
315 spin_unlock(&op->lock);
1182fca3 316 }
d2d87a3b 317 reinit_completion(&op->waitq);
1182fca3
MM
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.
d2d87a3b
AV
334 *
335 * Returns with op->lock taken.
1182fca3 336 */
b7ae37b0 337static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op)
1182fca3 338{
d2d87a3b 339 long timeout, n;
1182fca3 340
d2d87a3b
AV
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);
1182fca3 344
d2d87a3b
AV
345 if (op_state_serviced(op))
346 return 0;
70c6ea26 347
d2d87a3b
AV
348 if (unlikely(n < 0)) {
349 gossip_debug(GOSSIP_WAIT_DEBUG,
350 "*** %s:"
351 " operation interrupted by a signal (tag "
352 "%llu, op %p)\n",
353 __func__,
354 llu(op->tag),
355 op);
356 return -EINTR;
1182fca3 357 }
d2d87a3b
AV
358 op->attempts++;
359 if (op_state_purged(op)) {
360 gossip_debug(GOSSIP_WAIT_DEBUG,
361 "*** %s:"
362 " operation purged (tag "
363 "%llu, %p, att %d)\n",
364 __func__,
365 llu(op->tag),
366 op,
367 op->attempts);
368 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
369 -EAGAIN :
370 -EIO;
371 }
372 /* must have timed out, then... */
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 return -ETIMEDOUT;
1182fca3 382}