blob: cbca58ba008a37424afc1d3e5963aad2ff03999a [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 Viro05b39a82016-02-13 11:04:19 -050019static int wait_for_matching_downcall(struct orangefs_kernel_op_s *, long, bool);
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{
Martin Brandenburgfb393452018-01-22 15:44:51 -050031 struct orangefs_kernel_op_s *op, *tmp;
Mike Marshall1182fca2015-07-17 10:38:15 -040032
Yi Liu8bb8aef2015-11-24 15:12:14 -050033 spin_lock(&orangefs_request_list_lock);
Martin Brandenburgfb393452018-01-22 15:44:51 -050034 list_for_each_entry_safe(op, tmp, &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 Marshall9d9e7ba2016-03-03 13:46:48 -050040 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);
Mike Marshall1182fca2015-07-17 10:38:15 -040046 }
Yi Liu8bb8aef2015-11-24 15:12:14 -050047 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -040048}
49
50/*
Yi Liu8bb8aef2015-11-24 15:12:14 -050051 * submits a ORANGEFS operation and waits for it to complete
Mike Marshall1182fca2015-07-17 10:38:15 -040052 *
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 */
Yi Liu8bb8aef2015-11-24 15:12:14 -050060int service_operation(struct orangefs_kernel_op_s *op,
Mike Marshall1182fca2015-07-17 10:38:15 -040061 const char *op_name,
62 int flags)
63{
Al Viro05b39a82016-02-13 11:04:19 -050064 long timeout = MAX_SCHEDULE_TIMEOUT;
Mike Marshall1182fca2015-07-17 10:38:15 -040065 int ret = 0;
66
Mike Marshallce6c4142015-12-14 14:54:46 -050067 DEFINE_WAIT(wait_entry);
Mike Marshall1182fca2015-07-17 10:38:15 -040068
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,
Mike Marshall52534872016-02-16 17:09:09 -050075 "%s: %s op:%p: process:%s: pid:%d:\n",
76 __func__,
Mike Marshall1182fca2015-07-17 10:38:15 -040077 op_name,
Mike Marshall52534872016-02-16 17:09:09 -050078 op,
Mike Marshall1182fca2015-07-17 10:38:15 -040079 current->comm,
80 current->pid);
81
Mike Marshalladcf34a2016-02-24 16:54:27 -050082 /*
83 * If ORANGEFS_OP_NO_MUTEX was set in flags, we need to avoid
Mike Marshallca9f5182016-02-26 10:21:12 -050084 * acquiring the request_mutex because we're servicing a
Mike Marshalladcf34a2016-02-24 16:54:27 -050085 * high priority remount operation and the request_mutex is
86 * already taken.
87 */
88 if (!(flags & ORANGEFS_OP_NO_MUTEX)) {
Al Viroc72f15b2016-02-13 10:49:24 -050089 if (flags & ORANGEFS_OP_INTERRUPTIBLE)
Martin Brandenburg1d503612016-08-16 11:38:14 -040090 ret = mutex_lock_interruptible(&orangefs_request_mutex);
Al Viroc72f15b2016-02-13 10:49:24 -050091 else
Martin Brandenburg1d503612016-08-16 11:38:14 -040092 ret = mutex_lock_killable(&orangefs_request_mutex);
Mike Marshall1182fca2015-07-17 10:38:15 -040093 /*
94 * check to see if we were interrupted while waiting for
Mike Marshalladcf34a2016-02-24 16:54:27 -050095 * mutex
Mike Marshall1182fca2015-07-17 10:38:15 -040096 */
97 if (ret < 0) {
Mike Marshall1182fca2015-07-17 10:38:15 -040098 op->downcall.status = ret;
99 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshallca9f5182016-02-26 10:21:12 -0500100 "%s: service_operation interrupted.\n",
101 __func__);
Mike Marshall1182fca2015-07-17 10:38:15 -0400102 return ret;
103 }
104 }
105
Al Viro98815ad2016-02-13 10:38:23 -0500106 /* queue up the operation */
107 spin_lock(&orangefs_request_list_lock);
108 spin_lock(&op->lock);
109 set_op_state_waiting(op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500110 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);
Mike Marshalladcf34a2016-02-24 16:54:27 -0500116 /* add high priority remount op to the front of the line. */
Al Viro98815ad2016-02-13 10:38:23 -0500117 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()) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400124 gossip_debug(GOSSIP_WAIT_DEBUG,
Al Viro98815ad2016-02-13 10:38:23 -0500125 "%s:client core is NOT in service.\n",
126 __func__);
Martin Brandenburg688b8452017-04-25 15:38:07 -0400127 /*
128 * Don't wait for the userspace component to return if
129 * the filesystem is being umounted anyway.
130 */
131 if (op->upcall.type == ORANGEFS_VFS_OP_FS_UMOUNT)
132 timeout = 0;
133 else
134 timeout = op_timeout_secs * HZ;
Mike Marshall1182fca2015-07-17 10:38:15 -0400135 }
Al Viro98815ad2016-02-13 10:38:23 -0500136 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400137
Mike Marshalladcf34a2016-02-24 16:54:27 -0500138 if (!(flags & ORANGEFS_OP_NO_MUTEX))
Martin Brandenburg1d503612016-08-16 11:38:14 -0400139 mutex_unlock(&orangefs_request_mutex);
Mike Marshall1182fca2015-07-17 10:38:15 -0400140
Al Viro05b39a82016-02-13 11:04:19 -0500141 ret = wait_for_matching_downcall(op, timeout,
142 flags & ORANGEFS_OP_INTERRUPTIBLE);
Mike Marshall52534872016-02-16 17:09:09 -0500143
144 gossip_debug(GOSSIP_WAIT_DEBUG,
145 "%s: wait_for_matching_downcall returned %d for %p\n",
146 __func__,
147 ret,
148 op);
149
Mike Marshallca9f5182016-02-26 10:21:12 -0500150 /* got matching downcall; make sure status is in errno format */
Al Viro05b39a82016-02-13 11:04:19 -0500151 if (!ret) {
Al Virod2d87a32016-02-13 10:15:22 -0500152 spin_unlock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400153 op->downcall.status =
Yi Liu8bb8aef2015-11-24 15:12:14 -0500154 orangefs_normalize_to_errno(op->downcall.status);
Mike Marshall1182fca2015-07-17 10:38:15 -0400155 ret = op->downcall.status;
Al Viro05b39a82016-02-13 11:04:19 -0500156 goto out;
Mike Marshall1182fca2015-07-17 10:38:15 -0400157 }
158
Al Viro05b39a82016-02-13 11:04:19 -0500159 /* failed to get matching downcall */
160 if (ret == -ETIMEDOUT) {
Mike Marshalladcf34a2016-02-24 16:54:27 -0500161 gossip_err("%s: %s -- wait timed out; aborting attempt.\n",
162 __func__,
Al Viro05b39a82016-02-13 11:04:19 -0500163 op_name);
164 }
Mike Marshalladcf34a2016-02-24 16:54:27 -0500165
166 /*
Mike Marshallca9f5182016-02-26 10:21:12 -0500167 * remove a waiting op from the request list or
168 * remove an in-progress op from the in-progress list.
Mike Marshalladcf34a2016-02-24 16:54:27 -0500169 */
Al Viro05b39a82016-02-13 11:04:19 -0500170 orangefs_clean_up_interrupted_operation(op);
Mike Marshalladcf34a2016-02-24 16:54:27 -0500171
Al Viro05b39a82016-02-13 11:04:19 -0500172 op->downcall.status = ret;
Mike Marshall1182fca2015-07-17 10:38:15 -0400173 /* retry if operation has not been serviced and if requested */
Al Viro05b39a82016-02-13 11:04:19 -0500174 if (ret == -EAGAIN) {
175 op->attempts++;
176 timeout = op_timeout_secs * HZ;
Mike Marshall1182fca2015-07-17 10:38:15 -0400177 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500178 "orangefs: tag %llu (%s)"
Mike Marshall1182fca2015-07-17 10:38:15 -0400179 " -- operation to be retried (%d attempt)\n",
180 llu(op->tag),
181 op_name,
Al Viro05b39a82016-02-13 11:04:19 -0500182 op->attempts);
Mike Marshall1182fca2015-07-17 10:38:15 -0400183
Mike Marshalladcf34a2016-02-24 16:54:27 -0500184 /*
185 * io ops (ops that use the shared memory buffer) have
186 * to be returned to their caller for a retry. Other ops
187 * can just be recycled here.
188 */
Mike Marshall1182fca2015-07-17 10:38:15 -0400189 if (!op->uses_shared_memory)
Mike Marshall1182fca2015-07-17 10:38:15 -0400190 goto retry_servicing;
Mike Marshall1182fca2015-07-17 10:38:15 -0400191 }
192
Al Viro05b39a82016-02-13 11:04:19 -0500193out:
Mike Marshall1182fca2015-07-17 10:38:15 -0400194 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500195 "%s: %s returning: %d for %p.\n",
196 __func__,
Mike Marshall1182fca2015-07-17 10:38:15 -0400197 op_name,
198 ret,
199 op);
200 return ret;
201}
202
Mike Marshallca9f5182016-02-26 10:21:12 -0500203/* This can get called on an I/O op if it had a bad service_operation. */
Al Viro78699e22016-02-11 23:07:19 -0500204bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
205{
206 u64 tag = op->tag;
207 if (!op_state_in_progress(op))
208 return false;
209
210 op->slot_to_free = op->upcall.req.io.buf_index;
211 memset(&op->upcall, 0, sizeof(op->upcall));
212 memset(&op->downcall, 0, sizeof(op->downcall));
213 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
214 op->upcall.req.cancel.op_tag = tag;
215 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
216 op->downcall.status = -1;
217 orangefs_new_tag(op);
218
219 spin_lock(&orangefs_request_list_lock);
220 /* orangefs_request_list_lock is enough of a barrier here */
221 if (!__is_daemon_in_service()) {
222 spin_unlock(&orangefs_request_list_lock);
223 return false;
224 }
Al Viro98815ad2016-02-13 10:38:23 -0500225 spin_lock(&op->lock);
226 set_op_state_waiting(op);
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500227 gossip_debug(GOSSIP_DEV_DEBUG,
228 "%s: op:%s: op_state:%d: process:%s:\n",
229 __func__,
230 get_opname_string(op),
231 op->op_state,
232 current->comm);
Al Viro98815ad2016-02-13 10:38:23 -0500233 list_add(&op->list, &orangefs_request_list);
234 spin_unlock(&op->lock);
Al Viro78699e22016-02-11 23:07:19 -0500235 spin_unlock(&orangefs_request_list_lock);
236
Mike Marshallca9f5182016-02-26 10:21:12 -0500237 gossip_debug(GOSSIP_WAIT_DEBUG,
Al Viro78699e22016-02-11 23:07:19 -0500238 "Attempting ORANGEFS operation cancellation of tag %llu\n",
239 llu(tag));
240 return true;
241}
242
Mike Marshallca9f5182016-02-26 10:21:12 -0500243/*
244 * Change an op to the "given up" state and remove it from its list.
245 */
246static void
247 orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
Mike Marshall1182fca2015-07-17 10:38:15 -0400248{
249 /*
250 * handle interrupted cases depending on what state we were in when
Mike Marshallca9f5182016-02-26 10:21:12 -0500251 * the interruption is detected.
Mike Marshall1182fca2015-07-17 10:38:15 -0400252 *
Al Viroeab9b382016-01-23 13:09:05 -0500253 * Called with op->lock held.
Mike Marshall1182fca2015-07-17 10:38:15 -0400254 */
Mike Marshallca9f5182016-02-26 10:21:12 -0500255
256 /*
257 * List manipulation code elsewhere will ignore ops that
258 * have been given up upon.
259 */
Al Viroed42fe02016-01-22 19:47:47 -0500260 op->op_state |= OP_VFS_STATE_GIVEN_UP;
Mike Marshallca9f5182016-02-26 10:21:12 -0500261
Al Viro05a50a52016-02-18 18:59:44 -0500262 if (list_empty(&op->list)) {
263 /* caught copying to/from daemon */
264 BUG_ON(op_state_serviced(op));
265 spin_unlock(&op->lock);
266 wait_for_completion(&op->waitq);
267 } else if (op_state_waiting(op)) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400268 /*
269 * upcall hasn't been read; remove op from upcall request
270 * list.
271 */
272 spin_unlock(&op->lock);
Al Viroed42fe02016-01-22 19:47:47 -0500273 spin_lock(&orangefs_request_list_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500274 list_del_init(&op->list);
Al Viroed42fe02016-01-22 19:47:47 -0500275 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400276 gossip_debug(GOSSIP_WAIT_DEBUG,
277 "Interrupted: Removed op %p from request_list\n",
278 op);
279 } else if (op_state_in_progress(op)) {
280 /* op must be removed from the in progress htable */
281 spin_unlock(&op->lock);
Martin Brandenburg1d503612016-08-16 11:38:14 -0400282 spin_lock(&orangefs_htable_ops_in_progress_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500283 list_del_init(&op->list);
Martin Brandenburg1d503612016-08-16 11:38:14 -0400284 spin_unlock(&orangefs_htable_ops_in_progress_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400285 gossip_debug(GOSSIP_WAIT_DEBUG,
286 "Interrupted: Removed op %p"
287 " from htable_ops_in_progress\n",
288 op);
Al Viro05a50a52016-02-18 18:59:44 -0500289 } else {
Mike Marshall1182fca2015-07-17 10:38:15 -0400290 spin_unlock(&op->lock);
291 gossip_err("interrupted operation is in a weird state 0x%x\n",
292 op->op_state);
Mike Marshall1182fca2015-07-17 10:38:15 -0400293 }
Al Virod2d87a32016-02-13 10:15:22 -0500294 reinit_completion(&op->waitq);
Mike Marshall1182fca2015-07-17 10:38:15 -0400295}
296
297/*
Mike Marshallca9f5182016-02-26 10:21:12 -0500298 * Sleeps on waitqueue waiting for matching downcall.
299 * If client-core finishes servicing, then we are good to go.
Mike Marshall1182fca2015-07-17 10:38:15 -0400300 * else if client-core exits, we get woken up here, and retry with a timeout
301 *
Mike Marshallca9f5182016-02-26 10:21:12 -0500302 * When this call returns to the caller, the specified op will no
303 * longer be in either the in_progress hash table or on the request list.
Mike Marshall1182fca2015-07-17 10:38:15 -0400304 *
305 * Returns 0 on success and -errno on failure
306 * Errors are:
307 * EAGAIN in case we want the caller to requeue and try again..
308 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
309 * operation since client-core seems to be exiting too often
310 * or if we were interrupted.
Al Virod2d87a32016-02-13 10:15:22 -0500311 *
312 * Returns with op->lock taken.
Mike Marshall1182fca2015-07-17 10:38:15 -0400313 */
Al Viroc72f15b2016-02-13 10:49:24 -0500314static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
Al Viro05b39a82016-02-13 11:04:19 -0500315 long timeout,
Al Viroc72f15b2016-02-13 10:49:24 -0500316 bool interruptible)
Mike Marshall1182fca2015-07-17 10:38:15 -0400317{
Al Viro05b39a82016-02-13 11:04:19 -0500318 long n;
Al Viroc72f15b2016-02-13 10:49:24 -0500319
Mike Marshallca9f5182016-02-26 10:21:12 -0500320 /*
321 * There's a "schedule_timeout" inside of these wait
322 * primitives, during which the op is out of the hands of the
323 * user process that needs something done and is being
324 * manipulated by the client-core process.
325 */
Al Viroc72f15b2016-02-13 10:49:24 -0500326 if (interruptible)
Mike Marshalladcf34a2016-02-24 16:54:27 -0500327 n = wait_for_completion_interruptible_timeout(&op->waitq,
328 timeout);
Al Viroc72f15b2016-02-13 10:49:24 -0500329 else
330 n = wait_for_completion_killable_timeout(&op->waitq, timeout);
331
Mike Marshall1182fca2015-07-17 10:38:15 -0400332 spin_lock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400333
Al Virod2d87a32016-02-13 10:15:22 -0500334 if (op_state_serviced(op))
335 return 0;
336
337 if (unlikely(n < 0)) {
338 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500339 "%s: operation interrupted, tag %llu, %p\n",
Al Virod2d87a32016-02-13 10:15:22 -0500340 __func__,
341 llu(op->tag),
342 op);
343 return -EINTR;
344 }
Al Virod2d87a32016-02-13 10:15:22 -0500345 if (op_state_purged(op)) {
346 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500347 "%s: operation purged, tag %llu, %p, %d\n",
Al Virod2d87a32016-02-13 10:15:22 -0500348 __func__,
349 llu(op->tag),
350 op,
351 op->attempts);
352 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
353 -EAGAIN :
354 -EIO;
355 }
356 /* must have timed out, then... */
357 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall9d9e7ba2016-03-03 13:46:48 -0500358 "%s: operation timed out, tag %llu, %p, %d)\n",
Al Virod2d87a32016-02-13 10:15:22 -0500359 __func__,
360 llu(op->tag),
361 op,
362 op->attempts);
363 return -ETIMEDOUT;
Mike Marshall1182fca2015-07-17 10:38:15 -0400364}