blob: 3f9e43066444a02646acdd1dc4a362fa11c41a5a [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{
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{
Al Viro05b39a82016-02-13 11:04:19 -050058 long timeout = MAX_SCHEDULE_TIMEOUT;
Mike Marshall1182fca2015-07-17 10:38:15 -040059 /* flags to modify behavior */
Mike Marshall1182fca2015-07-17 10:38:15 -040060 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,
Mike Marshall52534872016-02-16 17:09:09 -050070 "%s: %s op:%p: process:%s: pid:%d:\n",
71 __func__,
Mike Marshall1182fca2015-07-17 10:38:15 -040072 op_name,
Mike Marshall52534872016-02-16 17:09:09 -050073 op,
Mike Marshall1182fca2015-07-17 10:38:15 -040074 current->comm,
75 current->pid);
76
Yi Liu8bb8aef2015-11-24 15:12:14 -050077 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
Al Viroc72f15b2016-02-13 10:49:24 -050078 if (flags & ORANGEFS_OP_INTERRUPTIBLE)
79 ret = mutex_lock_interruptible(&request_mutex);
80 else
81 ret = mutex_lock_killable(&request_mutex);
Mike Marshall1182fca2015-07-17 10:38:15 -040082 /*
83 * check to see if we were interrupted while waiting for
84 * semaphore
85 */
86 if (ret < 0) {
Mike Marshall1182fca2015-07-17 10:38:15 -040087 op->downcall.status = ret;
88 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -050089 "orangefs: service_operation interrupted.\n");
Mike Marshall1182fca2015-07-17 10:38:15 -040090 return ret;
91 }
92 }
93
Al Viro98815ad2016-02-13 10:38:23 -050094 /* queue up the operation */
95 spin_lock(&orangefs_request_list_lock);
96 spin_lock(&op->lock);
97 set_op_state_waiting(op);
98 if (flags & ORANGEFS_OP_PRIORITY)
99 list_add(&op->list, &orangefs_request_list);
100 else
101 list_add_tail(&op->list, &orangefs_request_list);
102 spin_unlock(&op->lock);
103 wake_up_interruptible(&orangefs_request_list_waitq);
104 if (!__is_daemon_in_service()) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400105 gossip_debug(GOSSIP_WAIT_DEBUG,
Al Viro98815ad2016-02-13 10:38:23 -0500106 "%s:client core is NOT in service.\n",
107 __func__);
Al Viro05b39a82016-02-13 11:04:19 -0500108 timeout = op_timeout_secs * HZ;
Mike Marshall1182fca2015-07-17 10:38:15 -0400109 }
Al Viro98815ad2016-02-13 10:38:23 -0500110 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400111
Yi Liu8bb8aef2015-11-24 15:12:14 -0500112 if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
Mike Marshall1182fca2015-07-17 10:38:15 -0400113 mutex_unlock(&request_mutex);
114
Al Viro05b39a82016-02-13 11:04:19 -0500115 ret = wait_for_matching_downcall(op, timeout,
116 flags & ORANGEFS_OP_INTERRUPTIBLE);
Mike Marshall52534872016-02-16 17:09:09 -0500117
118 gossip_debug(GOSSIP_WAIT_DEBUG,
119 "%s: wait_for_matching_downcall returned %d for %p\n",
120 __func__,
121 ret,
122 op);
123
Al Viro05b39a82016-02-13 11:04:19 -0500124 if (!ret) {
Al Virod2d87a32016-02-13 10:15:22 -0500125 spin_unlock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400126 /* got matching downcall; make sure status is in errno format */
127 op->downcall.status =
Yi Liu8bb8aef2015-11-24 15:12:14 -0500128 orangefs_normalize_to_errno(op->downcall.status);
Mike Marshall1182fca2015-07-17 10:38:15 -0400129 ret = op->downcall.status;
Al Viro05b39a82016-02-13 11:04:19 -0500130 goto out;
Mike Marshall1182fca2015-07-17 10:38:15 -0400131 }
132
Al Viro05b39a82016-02-13 11:04:19 -0500133 /* failed to get matching downcall */
134 if (ret == -ETIMEDOUT) {
135 gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
136 op_name);
137 }
138 orangefs_clean_up_interrupted_operation(op);
139 op->downcall.status = ret;
Mike Marshall1182fca2015-07-17 10:38:15 -0400140 /* retry if operation has not been serviced and if requested */
Al Viro05b39a82016-02-13 11:04:19 -0500141 if (ret == -EAGAIN) {
142 op->attempts++;
143 timeout = op_timeout_secs * HZ;
Mike Marshall1182fca2015-07-17 10:38:15 -0400144 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500145 "orangefs: tag %llu (%s)"
Mike Marshall1182fca2015-07-17 10:38:15 -0400146 " -- operation to be retried (%d attempt)\n",
147 llu(op->tag),
148 op_name,
Al Viro05b39a82016-02-13 11:04:19 -0500149 op->attempts);
Mike Marshall1182fca2015-07-17 10:38:15 -0400150
151 if (!op->uses_shared_memory)
152 /*
153 * this operation doesn't use the shared memory
154 * system
155 */
156 goto retry_servicing;
Mike Marshall1182fca2015-07-17 10:38:15 -0400157 }
158
Al Viro05b39a82016-02-13 11:04:19 -0500159out:
Mike Marshall1182fca2015-07-17 10:38:15 -0400160 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500161 "orangefs: service_operation %s returning: %d for %p.\n",
Mike Marshall1182fca2015-07-17 10:38:15 -0400162 op_name,
163 ret,
164 op);
165 return ret;
166}
167
Al Viro78699e22016-02-11 23:07:19 -0500168bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
169{
170 u64 tag = op->tag;
171 if (!op_state_in_progress(op))
172 return false;
173
174 op->slot_to_free = op->upcall.req.io.buf_index;
175 memset(&op->upcall, 0, sizeof(op->upcall));
176 memset(&op->downcall, 0, sizeof(op->downcall));
177 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
178 op->upcall.req.cancel.op_tag = tag;
179 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
180 op->downcall.status = -1;
181 orangefs_new_tag(op);
182
183 spin_lock(&orangefs_request_list_lock);
184 /* orangefs_request_list_lock is enough of a barrier here */
185 if (!__is_daemon_in_service()) {
186 spin_unlock(&orangefs_request_list_lock);
187 return false;
188 }
Al Viro98815ad2016-02-13 10:38:23 -0500189 spin_lock(&op->lock);
190 set_op_state_waiting(op);
191 list_add(&op->list, &orangefs_request_list);
192 spin_unlock(&op->lock);
Al Viro78699e22016-02-11 23:07:19 -0500193 spin_unlock(&orangefs_request_list_lock);
194
195 gossip_debug(GOSSIP_UTILS_DEBUG,
196 "Attempting ORANGEFS operation cancellation of tag %llu\n",
197 llu(tag));
198 return true;
199}
200
Al Viroe07db0a2016-01-21 22:21:41 -0500201static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
Mike Marshall1182fca2015-07-17 10:38:15 -0400202{
203 /*
204 * handle interrupted cases depending on what state we were in when
205 * the interruption is detected. there is a coarse grained lock
206 * across the operation.
207 *
Al Viroeab9b382016-01-23 13:09:05 -0500208 * Called with op->lock held.
Mike Marshall1182fca2015-07-17 10:38:15 -0400209 */
Al Viroed42fe02016-01-22 19:47:47 -0500210 op->op_state |= OP_VFS_STATE_GIVEN_UP;
Al Viro05a50a52016-02-18 18:59:44 -0500211 /* from that point on it can't be moved by anybody else */
212 if (list_empty(&op->list)) {
213 /* caught copying to/from daemon */
214 BUG_ON(op_state_serviced(op));
215 spin_unlock(&op->lock);
216 wait_for_completion(&op->waitq);
217 } else if (op_state_waiting(op)) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400218 /*
219 * upcall hasn't been read; remove op from upcall request
220 * list.
221 */
222 spin_unlock(&op->lock);
Al Viroed42fe02016-01-22 19:47:47 -0500223 spin_lock(&orangefs_request_list_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500224 list_del_init(&op->list);
Al Viroed42fe02016-01-22 19:47:47 -0500225 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400226 gossip_debug(GOSSIP_WAIT_DEBUG,
227 "Interrupted: Removed op %p from request_list\n",
228 op);
229 } else if (op_state_in_progress(op)) {
230 /* op must be removed from the in progress htable */
231 spin_unlock(&op->lock);
232 spin_lock(&htable_ops_in_progress_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500233 list_del_init(&op->list);
Mike Marshall1182fca2015-07-17 10:38:15 -0400234 spin_unlock(&htable_ops_in_progress_lock);
235 gossip_debug(GOSSIP_WAIT_DEBUG,
236 "Interrupted: Removed op %p"
237 " from htable_ops_in_progress\n",
238 op);
Al Viro05a50a52016-02-18 18:59:44 -0500239 } else {
Mike Marshall1182fca2015-07-17 10:38:15 -0400240 spin_unlock(&op->lock);
241 gossip_err("interrupted operation is in a weird state 0x%x\n",
242 op->op_state);
Mike Marshall1182fca2015-07-17 10:38:15 -0400243 }
Al Virod2d87a32016-02-13 10:15:22 -0500244 reinit_completion(&op->waitq);
Mike Marshall1182fca2015-07-17 10:38:15 -0400245}
246
247/*
248 * sleeps on waitqueue waiting for matching downcall.
249 * if client-core finishes servicing, then we are good to go.
250 * else if client-core exits, we get woken up here, and retry with a timeout
251 *
252 * Post when this call returns to the caller, the specified op will no
253 * longer be on any list or htable.
254 *
255 * Returns 0 on success and -errno on failure
256 * Errors are:
257 * EAGAIN in case we want the caller to requeue and try again..
258 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
259 * operation since client-core seems to be exiting too often
260 * or if we were interrupted.
Al Virod2d87a32016-02-13 10:15:22 -0500261 *
262 * Returns with op->lock taken.
Mike Marshall1182fca2015-07-17 10:38:15 -0400263 */
Al Viroc72f15b2016-02-13 10:49:24 -0500264static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
Al Viro05b39a82016-02-13 11:04:19 -0500265 long timeout,
Al Viroc72f15b2016-02-13 10:49:24 -0500266 bool interruptible)
Mike Marshall1182fca2015-07-17 10:38:15 -0400267{
Al Viro05b39a82016-02-13 11:04:19 -0500268 long n;
Al Viroc72f15b2016-02-13 10:49:24 -0500269
270 if (interruptible)
271 n = wait_for_completion_interruptible_timeout(&op->waitq, timeout);
272 else
273 n = wait_for_completion_killable_timeout(&op->waitq, timeout);
274
Mike Marshall1182fca2015-07-17 10:38:15 -0400275 spin_lock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400276
Al Virod2d87a32016-02-13 10:15:22 -0500277 if (op_state_serviced(op))
278 return 0;
279
280 if (unlikely(n < 0)) {
281 gossip_debug(GOSSIP_WAIT_DEBUG,
282 "*** %s:"
283 " operation interrupted by a signal (tag "
284 "%llu, op %p)\n",
285 __func__,
286 llu(op->tag),
287 op);
288 return -EINTR;
289 }
Al Virod2d87a32016-02-13 10:15:22 -0500290 if (op_state_purged(op)) {
291 gossip_debug(GOSSIP_WAIT_DEBUG,
292 "*** %s:"
293 " operation purged (tag "
294 "%llu, %p, att %d)\n",
295 __func__,
296 llu(op->tag),
297 op,
298 op->attempts);
299 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
300 -EAGAIN :
301 -EIO;
302 }
303 /* must have timed out, then... */
304 gossip_debug(GOSSIP_WAIT_DEBUG,
305 "*** %s:"
306 " operation timed out (tag"
307 " %llu, %p, att %d)\n",
308 __func__,
309 llu(op->tag),
310 op,
311 op->attempts);
312 return -ETIMEDOUT;
Mike Marshall1182fca2015-07-17 10:38:15 -0400313}