orangefs: get rid of op->done
[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
05b39a8b 19static int wait_for_matching_downcall(struct orangefs_kernel_op_s *, long, 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{
05b39a8b 58 long timeout = MAX_SCHEDULE_TIMEOUT;
1182fca3 59 /* flags to modify behavior */
1182fca3
MM
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
8bb8aefd 78 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
c72f15b7
AV
79 if (flags & ORANGEFS_OP_INTERRUPTIBLE)
80 ret = mutex_lock_interruptible(&request_mutex);
81 else
82 ret = mutex_lock_killable(&request_mutex);
1182fca3
MM
83 /*
84 * check to see if we were interrupted while waiting for
85 * semaphore
86 */
87 if (ret < 0) {
1182fca3
MM
88 op->downcall.status = ret;
89 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 90 "orangefs: service_operation interrupted.\n");
1182fca3
MM
91 return ret;
92 }
93 }
94
98815ade
AV
95 /* queue up the operation */
96 spin_lock(&orangefs_request_list_lock);
97 spin_lock(&op->lock);
98 set_op_state_waiting(op);
99 if (flags & ORANGEFS_OP_PRIORITY)
100 list_add(&op->list, &orangefs_request_list);
101 else
102 list_add_tail(&op->list, &orangefs_request_list);
103 spin_unlock(&op->lock);
104 wake_up_interruptible(&orangefs_request_list_waitq);
105 if (!__is_daemon_in_service()) {
1182fca3 106 gossip_debug(GOSSIP_WAIT_DEBUG,
98815ade 107 "%s:client core is NOT in service.\n",
1182fca3 108 __func__);
05b39a8b 109 timeout = op_timeout_secs * HZ;
1182fca3 110 }
98815ade 111 spin_unlock(&orangefs_request_list_lock);
1182fca3 112
8bb8aefd 113 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
1182fca3
MM
114 mutex_unlock(&request_mutex);
115
116 /*
117 * If we are asked to service an asynchronous operation from
118 * VFS perspective, we are done.
119 */
8bb8aefd 120 if (flags & ORANGEFS_OP_ASYNC)
1182fca3
MM
121 return 0;
122
05b39a8b
AV
123 ret = wait_for_matching_downcall(op, timeout,
124 flags & ORANGEFS_OP_INTERRUPTIBLE);
125 if (!ret) {
d2d87a3b 126 spin_unlock(&op->lock);
1182fca3
MM
127 /* got matching downcall; make sure status is in errno format */
128 op->downcall.status =
8bb8aefd 129 orangefs_normalize_to_errno(op->downcall.status);
1182fca3 130 ret = op->downcall.status;
05b39a8b 131 goto out;
1182fca3
MM
132 }
133
05b39a8b
AV
134 /* failed to get matching downcall */
135 if (ret == -ETIMEDOUT) {
136 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
137 op_name);
138 }
139 orangefs_clean_up_interrupted_operation(op);
140 op->downcall.status = ret;
1182fca3 141 /* retry if operation has not been serviced and if requested */
05b39a8b
AV
142 if (ret == -EAGAIN) {
143 op->attempts++;
144 timeout = op_timeout_secs * HZ;
1182fca3 145 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 146 "orangefs: tag %llu (%s)"
1182fca3
MM
147 " -- operation to be retried (%d attempt)\n",
148 llu(op->tag),
149 op_name,
05b39a8b 150 op->attempts);
1182fca3
MM
151
152 if (!op->uses_shared_memory)
153 /*
154 * this operation doesn't use the shared memory
155 * system
156 */
157 goto retry_servicing;
1182fca3
MM
158 }
159
05b39a8b 160out:
1182fca3 161 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 162 "orangefs: service_operation %s returning: %d for %p.\n",
1182fca3
MM
163 op_name,
164 ret,
165 op);
166 return ret;
167}
168
78699e29
AV
169bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
170{
171 u64 tag = op->tag;
172 if (!op_state_in_progress(op))
173 return false;
174
175 op->slot_to_free = op->upcall.req.io.buf_index;
176 memset(&op->upcall, 0, sizeof(op->upcall));
177 memset(&op->downcall, 0, sizeof(op->downcall));
178 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
179 op->upcall.req.cancel.op_tag = tag;
180 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
181 op->downcall.status = -1;
182 orangefs_new_tag(op);
183
184 spin_lock(&orangefs_request_list_lock);
185 /* orangefs_request_list_lock is enough of a barrier here */
186 if (!__is_daemon_in_service()) {
187 spin_unlock(&orangefs_request_list_lock);
188 return false;
189 }
98815ade
AV
190 spin_lock(&op->lock);
191 set_op_state_waiting(op);
192 list_add(&op->list, &orangefs_request_list);
193 spin_unlock(&op->lock);
78699e29
AV
194 spin_unlock(&orangefs_request_list_lock);
195
196 gossip_debug(GOSSIP_UTILS_DEBUG,
197 "Attempting ORANGEFS operation cancellation of tag %llu\n",
198 llu(tag));
199 return true;
200}
201
e07db0a2 202static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
1182fca3
MM
203{
204 /*
205 * handle interrupted cases depending on what state we were in when
206 * the interruption is detected. there is a coarse grained lock
207 * across the operation.
208 *
eab9b389 209 * Called with op->lock held.
1182fca3 210 */
ed42fe05 211 op->op_state |= OP_VFS_STATE_GIVEN_UP;
1182fca3
MM
212
213 if (op_state_waiting(op)) {
214 /*
215 * upcall hasn't been read; remove op from upcall request
216 * list.
217 */
218 spin_unlock(&op->lock);
ed42fe05
AV
219 spin_lock(&orangefs_request_list_lock);
220 list_del(&op->list);
221 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
222 gossip_debug(GOSSIP_WAIT_DEBUG,
223 "Interrupted: Removed op %p from request_list\n",
224 op);
225 } else if (op_state_in_progress(op)) {
226 /* op must be removed from the in progress htable */
227 spin_unlock(&op->lock);
228 spin_lock(&htable_ops_in_progress_lock);
229 list_del(&op->list);
230 spin_unlock(&htable_ops_in_progress_lock);
231 gossip_debug(GOSSIP_WAIT_DEBUG,
232 "Interrupted: Removed op %p"
233 " from htable_ops_in_progress\n",
234 op);
235 } else if (!op_state_serviced(op)) {
236 spin_unlock(&op->lock);
237 gossip_err("interrupted operation is in a weird state 0x%x\n",
238 op->op_state);
84d02150
MM
239 } else {
240 /*
241 * It is not intended for execution to flow here,
242 * but having this unlock here makes sparse happy.
243 */
244 gossip_err("%s: can't get here.\n", __func__);
245 spin_unlock(&op->lock);
1182fca3 246 }
d2d87a3b 247 reinit_completion(&op->waitq);
1182fca3
MM
248}
249
250/*
251 * sleeps on waitqueue waiting for matching downcall.
252 * if client-core finishes servicing, then we are good to go.
253 * else if client-core exits, we get woken up here, and retry with a timeout
254 *
255 * Post when this call returns to the caller, the specified op will no
256 * longer be on any list or htable.
257 *
258 * Returns 0 on success and -errno on failure
259 * Errors are:
260 * EAGAIN in case we want the caller to requeue and try again..
261 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
262 * operation since client-core seems to be exiting too often
263 * or if we were interrupted.
d2d87a3b
AV
264 *
265 * Returns with op->lock taken.
1182fca3 266 */
c72f15b7 267static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
05b39a8b 268 long timeout,
c72f15b7 269 bool interruptible)
1182fca3 270{
05b39a8b 271 long n;
c72f15b7
AV
272
273 if (interruptible)
274 n = wait_for_completion_interruptible_timeout(&op->waitq, timeout);
275 else
276 n = wait_for_completion_killable_timeout(&op->waitq, timeout);
277
d2d87a3b 278 spin_lock(&op->lock);
1182fca3 279
d2d87a3b
AV
280 if (op_state_serviced(op))
281 return 0;
70c6ea26 282
d2d87a3b
AV
283 if (unlikely(n < 0)) {
284 gossip_debug(GOSSIP_WAIT_DEBUG,
285 "*** %s:"
286 " operation interrupted by a signal (tag "
287 "%llu, op %p)\n",
288 __func__,
289 llu(op->tag),
290 op);
291 return -EINTR;
1182fca3 292 }
d2d87a3b
AV
293 if (op_state_purged(op)) {
294 gossip_debug(GOSSIP_WAIT_DEBUG,
295 "*** %s:"
296 " operation purged (tag "
297 "%llu, %p, att %d)\n",
298 __func__,
299 llu(op->tag),
300 op,
301 op->attempts);
302 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
303 -EAGAIN :
304 -EIO;
305 }
306 /* must have timed out, then... */
307 gossip_debug(GOSSIP_WAIT_DEBUG,
308 "*** %s:"
309 " operation timed out (tag"
310 " %llu, %p, att %d)\n",
311 __func__,
312 llu(op->tag),
313 op,
314 op->attempts);
315 return -ETIMEDOUT;
1182fca3 316}