Merge tag 'hwmon-for-linus-v4.9-rc2' of git://git.kernel.org/pub/scm/linux/kernel...
[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
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);
9d9e7ba9
MM
40 gossip_debug(GOSSIP_DEV_DEBUG,
41 "%s: op:%s: op_state:%d: process:%s:\n",
42 __func__,
43 get_opname_string(op),
44 op->op_state,
45 current->comm);
1182fca3 46 }
8bb8aefd 47 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
48}
49
50/*
8bb8aefd 51 * submits a ORANGEFS operation and waits for it to complete
1182fca3
MM
52 *
53 * Note op->downcall.status will contain the status of the operation (in
54 * errno format), whether provided by pvfs2-client or a result of failure to
55 * service the operation. If the caller wishes to distinguish, then
56 * op->state can be checked to see if it was serviced or not.
57 *
58 * Returns contents of op->downcall.status for convenience
59 */
8bb8aefd 60int service_operation(struct orangefs_kernel_op_s *op,
1182fca3
MM
61 const char *op_name,
62 int flags)
63{
05b39a8b 64 long timeout = MAX_SCHEDULE_TIMEOUT;
1182fca3
MM
65 int ret = 0;
66
ce6c414e 67 DEFINE_WAIT(wait_entry);
1182fca3
MM
68
69 op->upcall.tgid = current->tgid;
70 op->upcall.pid = current->pid;
71
72retry_servicing:
73 op->downcall.status = 0;
74 gossip_debug(GOSSIP_WAIT_DEBUG,
5253487e
MM
75 "%s: %s op:%p: process:%s: pid:%d:\n",
76 __func__,
1182fca3 77 op_name,
5253487e 78 op,
1182fca3
MM
79 current->comm,
80 current->pid);
81
adcf34a2
MM
82 /*
83 * If ORANGEFS_OP_NO_MUTEX was set in flags, we need to avoid
ca9f518e 84 * acquiring the request_mutex because we're servicing a
adcf34a2
MM
85 * high priority remount operation and the request_mutex is
86 * already taken.
87 */
88 if (!(flags & ORANGEFS_OP_NO_MUTEX)) {
c72f15b7 89 if (flags & ORANGEFS_OP_INTERRUPTIBLE)
1d503617 90 ret = mutex_lock_interruptible(&orangefs_request_mutex);
c72f15b7 91 else
1d503617 92 ret = mutex_lock_killable(&orangefs_request_mutex);
1182fca3
MM
93 /*
94 * check to see if we were interrupted while waiting for
adcf34a2 95 * mutex
1182fca3
MM
96 */
97 if (ret < 0) {
1182fca3
MM
98 op->downcall.status = ret;
99 gossip_debug(GOSSIP_WAIT_DEBUG,
ca9f518e
MM
100 "%s: service_operation interrupted.\n",
101 __func__);
1182fca3
MM
102 return ret;
103 }
104 }
105
98815ade
AV
106 /* queue up the operation */
107 spin_lock(&orangefs_request_list_lock);
108 spin_lock(&op->lock);
109 set_op_state_waiting(op);
9d9e7ba9
MM
110 gossip_debug(GOSSIP_DEV_DEBUG,
111 "%s: op:%s: op_state:%d: process:%s:\n",
112 __func__,
113 get_opname_string(op),
114 op->op_state,
115 current->comm);
adcf34a2 116 /* add high priority remount op to the front of the line. */
98815ade
AV
117 if (flags & ORANGEFS_OP_PRIORITY)
118 list_add(&op->list, &orangefs_request_list);
119 else
120 list_add_tail(&op->list, &orangefs_request_list);
121 spin_unlock(&op->lock);
122 wake_up_interruptible(&orangefs_request_list_waitq);
123 if (!__is_daemon_in_service()) {
1182fca3 124 gossip_debug(GOSSIP_WAIT_DEBUG,
98815ade 125 "%s:client core is NOT in service.\n",
1182fca3 126 __func__);
05b39a8b 127 timeout = op_timeout_secs * HZ;
1182fca3 128 }
98815ade 129 spin_unlock(&orangefs_request_list_lock);
1182fca3 130
adcf34a2 131 if (!(flags & ORANGEFS_OP_NO_MUTEX))
1d503617 132 mutex_unlock(&orangefs_request_mutex);
1182fca3 133
05b39a8b
AV
134 ret = wait_for_matching_downcall(op, timeout,
135 flags & ORANGEFS_OP_INTERRUPTIBLE);
5253487e
MM
136
137 gossip_debug(GOSSIP_WAIT_DEBUG,
138 "%s: wait_for_matching_downcall returned %d for %p\n",
139 __func__,
140 ret,
141 op);
142
ca9f518e 143 /* got matching downcall; make sure status is in errno format */
05b39a8b 144 if (!ret) {
d2d87a3b 145 spin_unlock(&op->lock);
1182fca3 146 op->downcall.status =
8bb8aefd 147 orangefs_normalize_to_errno(op->downcall.status);
1182fca3 148 ret = op->downcall.status;
05b39a8b 149 goto out;
1182fca3
MM
150 }
151
05b39a8b
AV
152 /* failed to get matching downcall */
153 if (ret == -ETIMEDOUT) {
adcf34a2
MM
154 gossip_err("%s: %s -- wait timed out; aborting attempt.\n",
155 __func__,
05b39a8b
AV
156 op_name);
157 }
adcf34a2
MM
158
159 /*
ca9f518e
MM
160 * remove a waiting op from the request list or
161 * remove an in-progress op from the in-progress list.
adcf34a2 162 */
05b39a8b 163 orangefs_clean_up_interrupted_operation(op);
adcf34a2 164
05b39a8b 165 op->downcall.status = ret;
1182fca3 166 /* retry if operation has not been serviced and if requested */
05b39a8b
AV
167 if (ret == -EAGAIN) {
168 op->attempts++;
169 timeout = op_timeout_secs * HZ;
1182fca3 170 gossip_debug(GOSSIP_WAIT_DEBUG,
8bb8aefd 171 "orangefs: tag %llu (%s)"
1182fca3
MM
172 " -- operation to be retried (%d attempt)\n",
173 llu(op->tag),
174 op_name,
05b39a8b 175 op->attempts);
1182fca3 176
adcf34a2
MM
177 /*
178 * io ops (ops that use the shared memory buffer) have
179 * to be returned to their caller for a retry. Other ops
180 * can just be recycled here.
181 */
1182fca3 182 if (!op->uses_shared_memory)
1182fca3 183 goto retry_servicing;
1182fca3
MM
184 }
185
05b39a8b 186out:
1182fca3 187 gossip_debug(GOSSIP_WAIT_DEBUG,
9d9e7ba9
MM
188 "%s: %s returning: %d for %p.\n",
189 __func__,
1182fca3
MM
190 op_name,
191 ret,
192 op);
193 return ret;
194}
195
ca9f518e 196/* This can get called on an I/O op if it had a bad service_operation. */
78699e29
AV
197bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
198{
199 u64 tag = op->tag;
200 if (!op_state_in_progress(op))
201 return false;
202
203 op->slot_to_free = op->upcall.req.io.buf_index;
204 memset(&op->upcall, 0, sizeof(op->upcall));
205 memset(&op->downcall, 0, sizeof(op->downcall));
206 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
207 op->upcall.req.cancel.op_tag = tag;
208 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
209 op->downcall.status = -1;
210 orangefs_new_tag(op);
211
212 spin_lock(&orangefs_request_list_lock);
213 /* orangefs_request_list_lock is enough of a barrier here */
214 if (!__is_daemon_in_service()) {
215 spin_unlock(&orangefs_request_list_lock);
216 return false;
217 }
98815ade
AV
218 spin_lock(&op->lock);
219 set_op_state_waiting(op);
9d9e7ba9
MM
220 gossip_debug(GOSSIP_DEV_DEBUG,
221 "%s: op:%s: op_state:%d: process:%s:\n",
222 __func__,
223 get_opname_string(op),
224 op->op_state,
225 current->comm);
98815ade
AV
226 list_add(&op->list, &orangefs_request_list);
227 spin_unlock(&op->lock);
78699e29
AV
228 spin_unlock(&orangefs_request_list_lock);
229
ca9f518e 230 gossip_debug(GOSSIP_WAIT_DEBUG,
78699e29
AV
231 "Attempting ORANGEFS operation cancellation of tag %llu\n",
232 llu(tag));
233 return true;
234}
235
ca9f518e
MM
236/*
237 * Change an op to the "given up" state and remove it from its list.
238 */
239static void
240 orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
1182fca3
MM
241{
242 /*
243 * handle interrupted cases depending on what state we were in when
ca9f518e 244 * the interruption is detected.
1182fca3 245 *
eab9b389 246 * Called with op->lock held.
1182fca3 247 */
ca9f518e
MM
248
249 /*
250 * List manipulation code elsewhere will ignore ops that
251 * have been given up upon.
252 */
ed42fe05 253 op->op_state |= OP_VFS_STATE_GIVEN_UP;
ca9f518e 254
05a50a5b
AV
255 if (list_empty(&op->list)) {
256 /* caught copying to/from daemon */
257 BUG_ON(op_state_serviced(op));
258 spin_unlock(&op->lock);
259 wait_for_completion(&op->waitq);
260 } else if (op_state_waiting(op)) {
1182fca3
MM
261 /*
262 * upcall hasn't been read; remove op from upcall request
263 * list.
264 */
265 spin_unlock(&op->lock);
ed42fe05 266 spin_lock(&orangefs_request_list_lock);
05a50a5b 267 list_del_init(&op->list);
ed42fe05 268 spin_unlock(&orangefs_request_list_lock);
1182fca3
MM
269 gossip_debug(GOSSIP_WAIT_DEBUG,
270 "Interrupted: Removed op %p from request_list\n",
271 op);
272 } else if (op_state_in_progress(op)) {
273 /* op must be removed from the in progress htable */
274 spin_unlock(&op->lock);
1d503617 275 spin_lock(&orangefs_htable_ops_in_progress_lock);
05a50a5b 276 list_del_init(&op->list);
1d503617 277 spin_unlock(&orangefs_htable_ops_in_progress_lock);
1182fca3
MM
278 gossip_debug(GOSSIP_WAIT_DEBUG,
279 "Interrupted: Removed op %p"
280 " from htable_ops_in_progress\n",
281 op);
05a50a5b 282 } else {
1182fca3
MM
283 spin_unlock(&op->lock);
284 gossip_err("interrupted operation is in a weird state 0x%x\n",
285 op->op_state);
286 }
d2d87a3b 287 reinit_completion(&op->waitq);
1182fca3
MM
288}
289
290/*
ca9f518e
MM
291 * Sleeps on waitqueue waiting for matching downcall.
292 * If client-core finishes servicing, then we are good to go.
1182fca3
MM
293 * else if client-core exits, we get woken up here, and retry with a timeout
294 *
ca9f518e
MM
295 * When this call returns to the caller, the specified op will no
296 * longer be in either the in_progress hash table or on the request list.
1182fca3
MM
297 *
298 * Returns 0 on success and -errno on failure
299 * Errors are:
300 * EAGAIN in case we want the caller to requeue and try again..
301 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
302 * operation since client-core seems to be exiting too often
303 * or if we were interrupted.
d2d87a3b
AV
304 *
305 * Returns with op->lock taken.
1182fca3 306 */
c72f15b7 307static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
05b39a8b 308 long timeout,
c72f15b7 309 bool interruptible)
1182fca3 310{
05b39a8b 311 long n;
c72f15b7 312
ca9f518e
MM
313 /*
314 * There's a "schedule_timeout" inside of these wait
315 * primitives, during which the op is out of the hands of the
316 * user process that needs something done and is being
317 * manipulated by the client-core process.
318 */
c72f15b7 319 if (interruptible)
adcf34a2
MM
320 n = wait_for_completion_interruptible_timeout(&op->waitq,
321 timeout);
c72f15b7
AV
322 else
323 n = wait_for_completion_killable_timeout(&op->waitq, timeout);
324
d2d87a3b 325 spin_lock(&op->lock);
1182fca3 326
d2d87a3b
AV
327 if (op_state_serviced(op))
328 return 0;
70c6ea26 329
d2d87a3b
AV
330 if (unlikely(n < 0)) {
331 gossip_debug(GOSSIP_WAIT_DEBUG,
9d9e7ba9 332 "%s: operation interrupted, tag %llu, %p\n",
d2d87a3b
AV
333 __func__,
334 llu(op->tag),
335 op);
336 return -EINTR;
1182fca3 337 }
d2d87a3b
AV
338 if (op_state_purged(op)) {
339 gossip_debug(GOSSIP_WAIT_DEBUG,
9d9e7ba9 340 "%s: operation purged, tag %llu, %p, %d\n",
d2d87a3b
AV
341 __func__,
342 llu(op->tag),
343 op,
344 op->attempts);
345 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
346 -EAGAIN :
347 -EIO;
348 }
349 /* must have timed out, then... */
350 gossip_debug(GOSSIP_WAIT_DEBUG,
9d9e7ba9 351 "%s: operation timed out, tag %llu, %p, %d)\n",
d2d87a3b
AV
352 __func__,
353 llu(op->tag),
354 op,
355 op->attempts);
356 return -ETIMEDOUT;
1182fca3 357}