blob: 0729d2645d6af6ffef902546b67257ea8aa2b97d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Mike Marshall1182fca2015-07-17 10:38:15 -04002/*
3 * (C) 2001 Clemson University and The University of Chicago
4 * (C) 2011 Omnibond Systems
5 *
6 * Changes by Acxiom Corporation to implement generic service_operation()
7 * function, Copyright Acxiom Corporation, 2005.
8 *
9 * See COPYING in top-level directory.
10 */
11
12/*
13 * In-kernel waitqueue operations.
14 */
15
16#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -050017#include "orangefs-kernel.h"
18#include "orangefs-bufmap.h"
Mike Marshall1182fca2015-07-17 10:38:15 -040019
Mike Marshallb1116bc2018-06-01 14:17:23 -040020static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
21 long timeout,
22 bool interruptible)
23 __acquires(op->lock);
24static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
25 __releases(op->lock);
Al Viroade3d782016-01-21 22:58:58 -050026
Mike Marshall1182fca2015-07-17 10:38:15 -040027/*
28 * What we do in this function is to walk the list of operations that are
29 * present in the request queue and mark them as purged.
30 * NOTE: This is called from the device close after client-core has
31 * guaranteed that no new operations could appear on the list since the
32 * client-core is anyway going to exit.
33 */
34void purge_waiting_ops(void)
35{
Martin Brandenburg0afc0de2018-01-22 15:44:51 -050036 struct orangefs_kernel_op_s *op, *tmp;
Mike Marshall1182fca2015-07-17 10:38:15 -040037
Yi Liu8bb8aef2015-11-24 15:12:14 -050038 spin_lock(&orangefs_request_list_lock);
Martin Brandenburg0afc0de2018-01-22 15:44:51 -050039 list_for_each_entry_safe(op, tmp, &orangefs_request_list, list) {
Mike Marshall1182fca2015-07-17 10:38:15 -040040 gossip_debug(GOSSIP_WAIT_DEBUG,
41 "pvfs2-client-core: purging op tag %llu %s\n",
42 llu(op->tag),
43 get_opname_string(op));
Mike Marshall1182fca2015-07-17 10:38:15 -040044 set_op_state_purged(op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -050045 gossip_debug(GOSSIP_DEV_DEBUG,
46 "%s: op:%s: op_state:%d: process:%s:\n",
47 __func__,
48 get_opname_string(op),
49 op->op_state,
50 current->comm);
Mike Marshall1182fca2015-07-17 10:38:15 -040051 }
Yi Liu8bb8aef2015-11-24 15:12:14 -050052 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -040053}
54
55/*
Yi Liu8bb8aef2015-11-24 15:12:14 -050056 * submits a ORANGEFS operation and waits for it to complete
Mike Marshall1182fca2015-07-17 10:38:15 -040057 *
58 * Note op->downcall.status will contain the status of the operation (in
59 * errno format), whether provided by pvfs2-client or a result of failure to
60 * service the operation. If the caller wishes to distinguish, then
61 * op->state can be checked to see if it was serviced or not.
62 *
63 * Returns contents of op->downcall.status for convenience
64 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050065int service_operation(struct orangefs_kernel_op_s *op,
Mike Marshall1182fca2015-07-17 10:38:15 -040066 const char *op_name,
67 int flags)
68{
Al Viro05b39a82016-02-13 11:04:19 -050069 long timeout = MAX_SCHEDULE_TIMEOUT;
Mike Marshall1182fca2015-07-17 10:38:15 -040070 int ret = 0;
71
Mike Marshallce6c4142015-12-14 14:54:46 -050072 DEFINE_WAIT(wait_entry);
Mike Marshall1182fca2015-07-17 10:38:15 -040073
74 op->upcall.tgid = current->tgid;
75 op->upcall.pid = current->pid;
76
77retry_servicing:
78 op->downcall.status = 0;
79 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall52534872016-02-16 17:09:09 -050080 "%s: %s op:%p: process:%s: pid:%d:\n",
81 __func__,
Mike Marshall1182fca2015-07-17 10:38:15 -040082 op_name,
Mike Marshall52534872016-02-16 17:09:09 -050083 op,
Mike Marshall1182fca2015-07-17 10:38:15 -040084 current->comm,
85 current->pid);
86
Mike Marshalladcf34a2016-02-24 16:54:27 -050087 /*
88 * If ORANGEFS_OP_NO_MUTEX was set in flags, we need to avoid
Mike Marshallca9f5182016-02-26 10:21:12 -050089 * acquiring the request_mutex because we're servicing a
Mike Marshalladcf34a2016-02-24 16:54:27 -050090 * high priority remount operation and the request_mutex is
91 * already taken.
92 */
93 if (!(flags & ORANGEFS_OP_NO_MUTEX)) {
Al Viroc72f15b2016-02-13 10:49:24 -050094 if (flags & ORANGEFS_OP_INTERRUPTIBLE)
Martin Brandenburg1d503612016-08-16 11:38:14 -040095 ret = mutex_lock_interruptible(&orangefs_request_mutex);
Al Viroc72f15b2016-02-13 10:49:24 -050096 else
Martin Brandenburg1d503612016-08-16 11:38:14 -040097 ret = mutex_lock_killable(&orangefs_request_mutex);
Mike Marshall1182fca2015-07-17 10:38:15 -040098 /*
99 * check to see if we were interrupted while waiting for
Mike Marshalladcf34a2016-02-24 16:54:27 -0500100 * mutex
Mike Marshall1182fca2015-07-17 10:38:15 -0400101 */
102 if (ret < 0) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400103 op->downcall.status = ret;
104 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshallca9f5182016-02-26 10:21:12 -0500105 "%s: service_operation interrupted.\n",
106 __func__);
Mike Marshall1182fca2015-07-17 10:38:15 -0400107 return ret;
108 }
109 }
110
Al Viro98815ad2016-02-13 10:38:23 -0500111 /* queue up the operation */
112 spin_lock(&orangefs_request_list_lock);
113 spin_lock(&op->lock);
114 set_op_state_waiting(op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500115 gossip_debug(GOSSIP_DEV_DEBUG,
116 "%s: op:%s: op_state:%d: process:%s:\n",
117 __func__,
118 get_opname_string(op),
119 op->op_state,
120 current->comm);
Mike Marshalladcf34a2016-02-24 16:54:27 -0500121 /* add high priority remount op to the front of the line. */
Al Viro98815ad2016-02-13 10:38:23 -0500122 if (flags & ORANGEFS_OP_PRIORITY)
123 list_add(&op->list, &orangefs_request_list);
124 else
125 list_add_tail(&op->list, &orangefs_request_list);
126 spin_unlock(&op->lock);
127 wake_up_interruptible(&orangefs_request_list_waitq);
128 if (!__is_daemon_in_service()) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400129 gossip_debug(GOSSIP_WAIT_DEBUG,
Al Viro98815ad2016-02-13 10:38:23 -0500130 "%s:client core is NOT in service.\n",
131 __func__);
Martin Brandenburgb5a9d612017-04-25 15:38:07 -0400132 /*
133 * Don't wait for the userspace component to return if
134 * the filesystem is being umounted anyway.
135 */
136 if (op->upcall.type == ORANGEFS_VFS_OP_FS_UMOUNT)
137 timeout = 0;
138 else
139 timeout = op_timeout_secs * HZ;
Mike Marshall1182fca2015-07-17 10:38:15 -0400140 }
Al Viro98815ad2016-02-13 10:38:23 -0500141 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400142
Mike Marshalladcf34a2016-02-24 16:54:27 -0500143 if (!(flags & ORANGEFS_OP_NO_MUTEX))
Martin Brandenburg1d503612016-08-16 11:38:14 -0400144 mutex_unlock(&orangefs_request_mutex);
Mike Marshall1182fca2015-07-17 10:38:15 -0400145
Al Viro05b39a82016-02-13 11:04:19 -0500146 ret = wait_for_matching_downcall(op, timeout,
147 flags & ORANGEFS_OP_INTERRUPTIBLE);
Mike Marshall52534872016-02-16 17:09:09 -0500148
149 gossip_debug(GOSSIP_WAIT_DEBUG,
150 "%s: wait_for_matching_downcall returned %d for %p\n",
151 __func__,
152 ret,
153 op);
154
Mike Marshallca9f5182016-02-26 10:21:12 -0500155 /* got matching downcall; make sure status is in errno format */
Al Viro05b39a82016-02-13 11:04:19 -0500156 if (!ret) {
Al Virod2d87a32016-02-13 10:15:22 -0500157 spin_unlock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400158 op->downcall.status =
Yi Liu8bb8aef2015-11-24 15:12:14 -0500159 orangefs_normalize_to_errno(op->downcall.status);
Mike Marshall1182fca2015-07-17 10:38:15 -0400160 ret = op->downcall.status;
Al Viro05b39a82016-02-13 11:04:19 -0500161 goto out;
Mike Marshall1182fca2015-07-17 10:38:15 -0400162 }
163
Al Viro05b39a82016-02-13 11:04:19 -0500164 /* failed to get matching downcall */
165 if (ret == -ETIMEDOUT) {
Mike Marshalladcf34a2016-02-24 16:54:27 -0500166 gossip_err("%s: %s -- wait timed out; aborting attempt.\n",
167 __func__,
Al Viro05b39a82016-02-13 11:04:19 -0500168 op_name);
169 }
Mike Marshalladcf34a2016-02-24 16:54:27 -0500170
171 /*
Mike Marshallca9f5182016-02-26 10:21:12 -0500172 * remove a waiting op from the request list or
173 * remove an in-progress op from the in-progress list.
Mike Marshalladcf34a2016-02-24 16:54:27 -0500174 */
Al Viro05b39a82016-02-13 11:04:19 -0500175 orangefs_clean_up_interrupted_operation(op);
Mike Marshalladcf34a2016-02-24 16:54:27 -0500176
Al Viro05b39a82016-02-13 11:04:19 -0500177 op->downcall.status = ret;
Mike Marshall1182fca2015-07-17 10:38:15 -0400178 /* retry if operation has not been serviced and if requested */
Al Viro05b39a82016-02-13 11:04:19 -0500179 if (ret == -EAGAIN) {
180 op->attempts++;
181 timeout = op_timeout_secs * HZ;
Mike Marshall1182fca2015-07-17 10:38:15 -0400182 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500183 "orangefs: tag %llu (%s)"
Mike Marshall1182fca2015-07-17 10:38:15 -0400184 " -- operation to be retried (%d attempt)\n",
185 llu(op->tag),
186 op_name,
Al Viro05b39a82016-02-13 11:04:19 -0500187 op->attempts);
Mike Marshall1182fca2015-07-17 10:38:15 -0400188
Mike Marshalladcf34a2016-02-24 16:54:27 -0500189 /*
190 * io ops (ops that use the shared memory buffer) have
191 * to be returned to their caller for a retry. Other ops
192 * can just be recycled here.
193 */
Mike Marshall1182fca2015-07-17 10:38:15 -0400194 if (!op->uses_shared_memory)
Mike Marshall1182fca2015-07-17 10:38:15 -0400195 goto retry_servicing;
Mike Marshall1182fca2015-07-17 10:38:15 -0400196 }
197
Al Viro05b39a82016-02-13 11:04:19 -0500198out:
Mike Marshall1182fca2015-07-17 10:38:15 -0400199 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500200 "%s: %s returning: %d for %p.\n",
201 __func__,
Mike Marshall1182fca2015-07-17 10:38:15 -0400202 op_name,
203 ret,
204 op);
205 return ret;
206}
207
Mike Marshallca9f5182016-02-26 10:21:12 -0500208/* This can get called on an I/O op if it had a bad service_operation. */
Al Viro78699e22016-02-11 23:07:19 -0500209bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
210{
211 u64 tag = op->tag;
212 if (!op_state_in_progress(op))
213 return false;
214
215 op->slot_to_free = op->upcall.req.io.buf_index;
216 memset(&op->upcall, 0, sizeof(op->upcall));
217 memset(&op->downcall, 0, sizeof(op->downcall));
218 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
219 op->upcall.req.cancel.op_tag = tag;
220 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
221 op->downcall.status = -1;
222 orangefs_new_tag(op);
223
224 spin_lock(&orangefs_request_list_lock);
225 /* orangefs_request_list_lock is enough of a barrier here */
226 if (!__is_daemon_in_service()) {
227 spin_unlock(&orangefs_request_list_lock);
228 return false;
229 }
Al Viro98815ad2016-02-13 10:38:23 -0500230 spin_lock(&op->lock);
231 set_op_state_waiting(op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500232 gossip_debug(GOSSIP_DEV_DEBUG,
233 "%s: op:%s: op_state:%d: process:%s:\n",
234 __func__,
235 get_opname_string(op),
236 op->op_state,
237 current->comm);
Al Viro98815ad2016-02-13 10:38:23 -0500238 list_add(&op->list, &orangefs_request_list);
239 spin_unlock(&op->lock);
Al Viro78699e22016-02-11 23:07:19 -0500240 spin_unlock(&orangefs_request_list_lock);
241
Mike Marshallca9f5182016-02-26 10:21:12 -0500242 gossip_debug(GOSSIP_WAIT_DEBUG,
Al Viro78699e22016-02-11 23:07:19 -0500243 "Attempting ORANGEFS operation cancellation of tag %llu\n",
244 llu(tag));
245 return true;
246}
247
Mike Marshallca9f5182016-02-26 10:21:12 -0500248/*
249 * Change an op to the "given up" state and remove it from its list.
250 */
251static void
252 orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
Mike Marshallb1116bc2018-06-01 14:17:23 -0400253 __releases(op->lock)
Mike Marshall1182fca2015-07-17 10:38:15 -0400254{
255 /*
256 * handle interrupted cases depending on what state we were in when
Mike Marshallca9f5182016-02-26 10:21:12 -0500257 * the interruption is detected.
Mike Marshall1182fca2015-07-17 10:38:15 -0400258 *
Al Viroeab9b382016-01-23 13:09:05 -0500259 * Called with op->lock held.
Mike Marshall1182fca2015-07-17 10:38:15 -0400260 */
Mike Marshallca9f5182016-02-26 10:21:12 -0500261
262 /*
263 * List manipulation code elsewhere will ignore ops that
264 * have been given up upon.
265 */
Al Viroed42fe02016-01-22 19:47:47 -0500266 op->op_state |= OP_VFS_STATE_GIVEN_UP;
Mike Marshallca9f5182016-02-26 10:21:12 -0500267
Al Viro05a50a52016-02-18 18:59:44 -0500268 if (list_empty(&op->list)) {
269 /* caught copying to/from daemon */
270 BUG_ON(op_state_serviced(op));
271 spin_unlock(&op->lock);
272 wait_for_completion(&op->waitq);
273 } else if (op_state_waiting(op)) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400274 /*
275 * upcall hasn't been read; remove op from upcall request
276 * list.
277 */
278 spin_unlock(&op->lock);
Al Viroed42fe02016-01-22 19:47:47 -0500279 spin_lock(&orangefs_request_list_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500280 list_del_init(&op->list);
Al Viroed42fe02016-01-22 19:47:47 -0500281 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400282 gossip_debug(GOSSIP_WAIT_DEBUG,
283 "Interrupted: Removed op %p from request_list\n",
284 op);
285 } else if (op_state_in_progress(op)) {
286 /* op must be removed from the in progress htable */
287 spin_unlock(&op->lock);
Martin Brandenburg1d503612016-08-16 11:38:14 -0400288 spin_lock(&orangefs_htable_ops_in_progress_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500289 list_del_init(&op->list);
Martin Brandenburg1d503612016-08-16 11:38:14 -0400290 spin_unlock(&orangefs_htable_ops_in_progress_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400291 gossip_debug(GOSSIP_WAIT_DEBUG,
292 "Interrupted: Removed op %p"
293 " from htable_ops_in_progress\n",
294 op);
Al Viro05a50a52016-02-18 18:59:44 -0500295 } else {
Mike Marshall1182fca2015-07-17 10:38:15 -0400296 spin_unlock(&op->lock);
297 gossip_err("interrupted operation is in a weird state 0x%x\n",
298 op->op_state);
Mike Marshall1182fca2015-07-17 10:38:15 -0400299 }
Al Virod2d87a32016-02-13 10:15:22 -0500300 reinit_completion(&op->waitq);
Mike Marshall1182fca2015-07-17 10:38:15 -0400301}
302
303/*
Mike Marshallca9f5182016-02-26 10:21:12 -0500304 * Sleeps on waitqueue waiting for matching downcall.
305 * If client-core finishes servicing, then we are good to go.
Mike Marshall1182fca2015-07-17 10:38:15 -0400306 * else if client-core exits, we get woken up here, and retry with a timeout
307 *
Mike Marshallca9f5182016-02-26 10:21:12 -0500308 * When this call returns to the caller, the specified op will no
309 * longer be in either the in_progress hash table or on the request list.
Mike Marshall1182fca2015-07-17 10:38:15 -0400310 *
311 * Returns 0 on success and -errno on failure
312 * Errors are:
313 * EAGAIN in case we want the caller to requeue and try again..
314 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
315 * operation since client-core seems to be exiting too often
316 * or if we were interrupted.
Al Virod2d87a32016-02-13 10:15:22 -0500317 *
318 * Returns with op->lock taken.
Mike Marshall1182fca2015-07-17 10:38:15 -0400319 */
Al Viroc72f15b2016-02-13 10:49:24 -0500320static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
Mike Marshallb1116bc2018-06-01 14:17:23 -0400321 long timeout,
322 bool interruptible)
323 __acquires(op->lock)
Mike Marshall1182fca2015-07-17 10:38:15 -0400324{
Al Viro05b39a82016-02-13 11:04:19 -0500325 long n;
Al Viroc72f15b2016-02-13 10:49:24 -0500326
Mike Marshallca9f5182016-02-26 10:21:12 -0500327 /*
328 * There's a "schedule_timeout" inside of these wait
329 * primitives, during which the op is out of the hands of the
330 * user process that needs something done and is being
331 * manipulated by the client-core process.
332 */
Al Viroc72f15b2016-02-13 10:49:24 -0500333 if (interruptible)
Mike Marshalladcf34a2016-02-24 16:54:27 -0500334 n = wait_for_completion_interruptible_timeout(&op->waitq,
335 timeout);
Al Viroc72f15b2016-02-13 10:49:24 -0500336 else
337 n = wait_for_completion_killable_timeout(&op->waitq, timeout);
338
Mike Marshall1182fca2015-07-17 10:38:15 -0400339 spin_lock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400340
Al Virod2d87a32016-02-13 10:15:22 -0500341 if (op_state_serviced(op))
342 return 0;
343
344 if (unlikely(n < 0)) {
345 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500346 "%s: operation interrupted, tag %llu, %p\n",
Al Virod2d87a32016-02-13 10:15:22 -0500347 __func__,
348 llu(op->tag),
349 op);
350 return -EINTR;
351 }
Al Virod2d87a32016-02-13 10:15:22 -0500352 if (op_state_purged(op)) {
353 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500354 "%s: operation purged, tag %llu, %p, %d\n",
Al Virod2d87a32016-02-13 10:15:22 -0500355 __func__,
356 llu(op->tag),
357 op,
358 op->attempts);
359 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
360 -EAGAIN :
361 -EIO;
362 }
363 /* must have timed out, then... */
364 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500365 "%s: operation timed out, tag %llu, %p, %d)\n",
Al Virod2d87a32016-02-13 10:15:22 -0500366 __func__,
367 llu(op->tag),
368 op,
369 op->attempts);
370 return -ETIMEDOUT;
Mike Marshall1182fca2015-07-17 10:38:15 -0400371}