blob: 6cae77400a5b57a18f70e01ed2a9522332c46dea [file] [log] [blame]
Mike Marshall1182fca2015-07-17 10:38:15 -04001/*
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"
Mike Marshall575e9462015-12-04 12:56:14 -050016#include "orangefs-kernel.h"
17#include "orangefs-bufmap.h"
Mike Marshall1182fca2015-07-17 10:38:15 -040018
Al Viroade3d782016-01-21 22:58:58 -050019static int wait_for_matching_downcall(struct orangefs_kernel_op_s *);
Al Virod2d87a32016-02-13 10:15:22 -050020static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *);
Al Viroade3d782016-01-21 22:58:58 -050021
Mike Marshall1182fca2015-07-17 10:38:15 -040022/*
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{
Yi Liu8bb8aef2015-11-24 15:12:14 -050031 struct orangefs_kernel_op_s *op;
Mike Marshall1182fca2015-07-17 10:38:15 -040032
Yi Liu8bb8aef2015-11-24 15:12:14 -050033 spin_lock(&orangefs_request_list_lock);
34 list_for_each_entry(op, &orangefs_request_list, list) {
Mike Marshall1182fca2015-07-17 10:38:15 -040035 gossip_debug(GOSSIP_WAIT_DEBUG,
36 "pvfs2-client-core: purging op tag %llu %s\n",
37 llu(op->tag),
38 get_opname_string(op));
Mike Marshall1182fca2015-07-17 10:38:15 -040039 set_op_state_purged(op);
Mike Marshall1182fca2015-07-17 10:38:15 -040040 }
Yi Liu8bb8aef2015-11-24 15:12:14 -050041 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -040042}
43
44/*
Yi Liu8bb8aef2015-11-24 15:12:14 -050045 * submits a ORANGEFS operation and waits for it to complete
Mike Marshall1182fca2015-07-17 10:38:15 -040046 *
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 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050054int service_operation(struct orangefs_kernel_op_s *op,
Mike Marshall1182fca2015-07-17 10:38:15 -040055 const char *op_name,
56 int flags)
57{
58 /* flags to modify behavior */
59 sigset_t orig_sigset;
60 int ret = 0;
61
Mike Marshallce6c4142015-12-14 14:54:46 -050062 DEFINE_WAIT(wait_entry);
Mike Marshall1182fca2015-07-17 10:38:15 -040063
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,
Yi Liu8bb8aef2015-11-24 15:12:14 -050070 "orangefs: service_operation: %s %p\n",
Mike Marshall1182fca2015-07-17 10:38:15 -040071 op_name,
72 op);
73 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -050074 "orangefs: operation posted by process: %s, pid: %i\n",
Mike Marshall1182fca2015-07-17 10:38:15 -040075 current->comm,
76 current->pid);
77
78 /* mask out signals if this operation is not to be interrupted */
Yi Liu8bb8aef2015-11-24 15:12:14 -050079 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
Richard Weinbergerc146c0b2016-01-02 23:04:47 +010080 orangefs_block_signals(&orig_sigset);
Mike Marshall1182fca2015-07-17 10:38:15 -040081
Yi Liu8bb8aef2015-11-24 15:12:14 -050082 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
Mike Marshall1182fca2015-07-17 10:38:15 -040083 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) {
Yi Liu8bb8aef2015-11-24 15:12:14 -050089 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
Richard Weinbergerc146c0b2016-01-02 23:04:47 +010090 orangefs_set_signals(&orig_sigset);
Mike Marshall1182fca2015-07-17 10:38:15 -040091 op->downcall.status = ret;
92 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -050093 "orangefs: service_operation interrupted.\n");
Mike Marshall1182fca2015-07-17 10:38:15 -040094 return ret;
95 }
96 }
97
Al Viro98815ad2016-02-13 10:38:23 -050098 /* 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()) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400109 /*
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,
Al Viro98815ad2016-02-13 10:38:23 -0500115 "%s:client core is NOT in service.\n",
116 __func__);
Mike Marshall1182fca2015-07-17 10:38:15 -0400117 op->attempts++;
118 }
Al Viro98815ad2016-02-13 10:38:23 -0500119 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400120
Yi Liu8bb8aef2015-11-24 15:12:14 -0500121 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
Mike Marshall1182fca2015-07-17 10:38:15 -0400122 mutex_unlock(&request_mutex);
123
124 /*
125 * If we are asked to service an asynchronous operation from
126 * VFS perspective, we are done.
127 */
Yi Liu8bb8aef2015-11-24 15:12:14 -0500128 if (flags & ORANGEFS_OP_ASYNC)
Mike Marshall1182fca2015-07-17 10:38:15 -0400129 return 0;
130
Al Viro78699e22016-02-11 23:07:19 -0500131 ret = wait_for_matching_downcall(op);
Mike Marshall1182fca2015-07-17 10:38:15 -0400132
133 if (ret < 0) {
134 /* failed to get matching downcall */
135 if (ret == -ETIMEDOUT) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500136 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
Mike Marshall1182fca2015-07-17 10:38:15 -0400137 op_name);
138 }
Al Virod2d87a32016-02-13 10:15:22 -0500139 orangefs_clean_up_interrupted_operation(op);
Mike Marshall1182fca2015-07-17 10:38:15 -0400140 op->downcall.status = ret;
141 } else {
Al Virod2d87a32016-02-13 10:15:22 -0500142 spin_unlock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400143 /* got matching downcall; make sure status is in errno format */
144 op->downcall.status =
Yi Liu8bb8aef2015-11-24 15:12:14 -0500145 orangefs_normalize_to_errno(op->downcall.status);
Mike Marshall1182fca2015-07-17 10:38:15 -0400146 ret = op->downcall.status;
147 }
148
Yi Liu8bb8aef2015-11-24 15:12:14 -0500149 if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
Richard Weinbergerc146c0b2016-01-02 23:04:47 +0100150 orangefs_set_signals(&orig_sigset);
Mike Marshall1182fca2015-07-17 10:38:15 -0400151
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,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500156 "orangefs: tag %llu (%s)"
Mike Marshall1182fca2015-07-17 10:38:15 -0400157 " -- 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 */
Martin Brandenburg7d221482016-01-04 15:05:28 -0500170 if (orangefs_get_bufmap_init() == 0) {
Mike Marshall6ebcc3f2016-02-04 16:28:31 -0500171 WARN_ON(1);
Mike Marshall1182fca2015-07-17 10:38:15 -0400172 /*
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",
Martin Brandenburg7d221482016-01-04 15:05:28 -0500185 orangefs_get_bufmap_init());
Mike Marshall1182fca2015-07-17 10:38:15 -0400186 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 */
Mike Marshallce6c4142015-12-14 14:54:46 -0500194 prepare_to_wait(&orangefs_bufmap_init_waitq,
195 &wait_entry,
196 TASK_INTERRUPTIBLE);
Mike Marshall1182fca2015-07-17 10:38:15 -0400197
Mike Marshall1182fca2015-07-17 10:38:15 -0400198 /*
Yi Liu8bb8aef2015-11-24 15:12:14 -0500199 * Wait for orangefs_bufmap_initialize() to wake me up
Mike Marshall1182fca2015-07-17 10:38:15 -0400200 * within the allotted time.
201 */
Al Viro727cbfe2016-01-23 13:17:55 -0500202 ret = schedule_timeout(
203 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ);
Mike Marshall1182fca2015-07-17 10:38:15 -0400204
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",
Martin Brandenburg7d221482016-01-04 15:05:28 -0500211 orangefs_get_bufmap_init());
Mike Marshall1182fca2015-07-17 10:38:15 -0400212
Mike Marshallce6c4142015-12-14 14:54:46 -0500213 finish_wait(&orangefs_bufmap_init_waitq, &wait_entry);
Mike Marshall1182fca2015-07-17 10:38:15 -0400214
Martin Brandenburg7d221482016-01-04 15:05:28 -0500215 if (orangefs_get_bufmap_init() == 0) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400216 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__,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500218 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS,
Mike Marshall1182fca2015-07-17 10:38:15 -0400219 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,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500232 "orangefs: service_operation %s returning: %d for %p.\n",
Mike Marshall1182fca2015-07-17 10:38:15 -0400233 op_name,
234 ret,
235 op);
236 return ret;
237}
238
Al Viro78699e22016-02-11 23:07:19 -0500239bool 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 }
Al Viro98815ad2016-02-13 10:38:23 -0500260 spin_lock(&op->lock);
261 set_op_state_waiting(op);
262 list_add(&op->list, &orangefs_request_list);
263 spin_unlock(&op->lock);
Al Viro78699e22016-02-11 23:07:19 -0500264 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
Al Viroe07db0a2016-01-21 22:21:41 -0500272static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
Mike Marshall1182fca2015-07-17 10:38:15 -0400273{
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 *
Al Viroeab9b382016-01-23 13:09:05 -0500279 * Called with op->lock held.
Mike Marshall1182fca2015-07-17 10:38:15 -0400280 */
Al Viroed42fe02016-01-22 19:47:47 -0500281 op->op_state |= OP_VFS_STATE_GIVEN_UP;
Mike Marshall1182fca2015-07-17 10:38:15 -0400282
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);
Al Viroed42fe02016-01-22 19:47:47 -0500289 spin_lock(&orangefs_request_list_lock);
290 list_del(&op->list);
291 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400292 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);
Mike Marshall84d02152015-07-28 13:27:51 -0400309 } 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);
Mike Marshall1182fca2015-07-17 10:38:15 -0400316 }
Al Virod2d87a32016-02-13 10:15:22 -0500317 reinit_completion(&op->waitq);
Mike Marshall1182fca2015-07-17 10:38:15 -0400318}
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.
Al Virod2d87a32016-02-13 10:15:22 -0500334 *
335 * Returns with op->lock taken.
Mike Marshall1182fca2015-07-17 10:38:15 -0400336 */
Al Virob7ae37b2016-01-21 22:58:58 -0500337static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op)
Mike Marshall1182fca2015-07-17 10:38:15 -0400338{
Al Virod2d87a32016-02-13 10:15:22 -0500339 long timeout, n;
Mike Marshall1182fca2015-07-17 10:38:15 -0400340
Al Virod2d87a32016-02-13 10:15:22 -0500341 timeout = op->attempts ? op_timeout_secs * HZ : MAX_SCHEDULE_TIMEOUT;
342 n = wait_for_completion_interruptible_timeout(&op->waitq, timeout);
Mike Marshall1182fca2015-07-17 10:38:15 -0400343 spin_lock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400344
Al Virod2d87a32016-02-13 10:15:22 -0500345 if (op_state_serviced(op))
346 return 0;
347
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;
357 }
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;
Mike Marshall1182fca2015-07-17 10:38:15 -0400382}