blob: 1eadf69cc919eb8c346510f485e33d397d4125db [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 int ret = 0;
60
Mike Marshallce6c4142015-12-14 14:54:46 -050061 DEFINE_WAIT(wait_entry);
Mike Marshall1182fca2015-07-17 10:38:15 -040062
63 op->upcall.tgid = current->tgid;
64 op->upcall.pid = current->pid;
65
66retry_servicing:
67 op->downcall.status = 0;
68 gossip_debug(GOSSIP_WAIT_DEBUG,
Mike Marshall52534872016-02-16 17:09:09 -050069 "%s: %s op:%p: process:%s: pid:%d:\n",
70 __func__,
Mike Marshall1182fca2015-07-17 10:38:15 -040071 op_name,
Mike Marshall52534872016-02-16 17:09:09 -050072 op,
Mike Marshall1182fca2015-07-17 10:38:15 -040073 current->comm,
74 current->pid);
75
Mike Marshalladcf34a2016-02-24 16:54:27 -050076 /*
77 * If ORANGEFS_OP_NO_MUTEX was set in flags, we need to avoid
78 * aquiring the request_mutex because we're servicing a
79 * high priority remount operation and the request_mutex is
80 * already taken.
81 */
82 if (!(flags & ORANGEFS_OP_NO_MUTEX)) {
Al Viroc72f15b2016-02-13 10:49:24 -050083 if (flags & ORANGEFS_OP_INTERRUPTIBLE)
84 ret = mutex_lock_interruptible(&request_mutex);
85 else
86 ret = mutex_lock_killable(&request_mutex);
Mike Marshall1182fca2015-07-17 10:38:15 -040087 /*
88 * check to see if we were interrupted while waiting for
Mike Marshalladcf34a2016-02-24 16:54:27 -050089 * mutex
Mike Marshall1182fca2015-07-17 10:38:15 -040090 */
91 if (ret < 0) {
Mike Marshall1182fca2015-07-17 10:38:15 -040092 op->downcall.status = ret;
93 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -050094 "orangefs: service_operation interrupted.\n");
Mike Marshall1182fca2015-07-17 10:38:15 -040095 return ret;
96 }
97 }
98
Al Viro98815ad2016-02-13 10:38:23 -050099 /* queue up the operation */
100 spin_lock(&orangefs_request_list_lock);
101 spin_lock(&op->lock);
102 set_op_state_waiting(op);
Mike Marshalladcf34a2016-02-24 16:54:27 -0500103 /* add high priority remount op to the front of the line. */
Al Viro98815ad2016-02-13 10:38:23 -0500104 if (flags & ORANGEFS_OP_PRIORITY)
105 list_add(&op->list, &orangefs_request_list);
106 else
107 list_add_tail(&op->list, &orangefs_request_list);
108 spin_unlock(&op->lock);
109 wake_up_interruptible(&orangefs_request_list_waitq);
110 if (!__is_daemon_in_service()) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400111 gossip_debug(GOSSIP_WAIT_DEBUG,
Al Viro98815ad2016-02-13 10:38:23 -0500112 "%s:client core is NOT in service.\n",
113 __func__);
Al Viro05b39a82016-02-13 11:04:19 -0500114 timeout = op_timeout_secs * HZ;
Mike Marshall1182fca2015-07-17 10:38:15 -0400115 }
Al Viro98815ad2016-02-13 10:38:23 -0500116 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400117
Mike Marshalladcf34a2016-02-24 16:54:27 -0500118 if (!(flags & ORANGEFS_OP_NO_MUTEX))
Mike Marshall1182fca2015-07-17 10:38:15 -0400119 mutex_unlock(&request_mutex);
120
Al Viro05b39a82016-02-13 11:04:19 -0500121 ret = wait_for_matching_downcall(op, timeout,
122 flags & ORANGEFS_OP_INTERRUPTIBLE);
Mike Marshall52534872016-02-16 17:09:09 -0500123
124 gossip_debug(GOSSIP_WAIT_DEBUG,
125 "%s: wait_for_matching_downcall returned %d for %p\n",
126 __func__,
127 ret,
128 op);
129
Al Viro05b39a82016-02-13 11:04:19 -0500130 if (!ret) {
Al Virod2d87a32016-02-13 10:15:22 -0500131 spin_unlock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400132 /* got matching downcall; make sure status is in errno format */
133 op->downcall.status =
Yi Liu8bb8aef2015-11-24 15:12:14 -0500134 orangefs_normalize_to_errno(op->downcall.status);
Mike Marshall1182fca2015-07-17 10:38:15 -0400135 ret = op->downcall.status;
Al Viro05b39a82016-02-13 11:04:19 -0500136 goto out;
Mike Marshall1182fca2015-07-17 10:38:15 -0400137 }
138
Al Viro05b39a82016-02-13 11:04:19 -0500139 /* failed to get matching downcall */
140 if (ret == -ETIMEDOUT) {
Mike Marshalladcf34a2016-02-24 16:54:27 -0500141 gossip_err("%s: %s -- wait timed out; aborting attempt.\n",
142 __func__,
Al Viro05b39a82016-02-13 11:04:19 -0500143 op_name);
144 }
Mike Marshalladcf34a2016-02-24 16:54:27 -0500145
146 /*
147 * remove waiting ops from the request list or
148 * remove in-progress ops from the in-progress list.
149 */
Al Viro05b39a82016-02-13 11:04:19 -0500150 orangefs_clean_up_interrupted_operation(op);
Mike Marshalladcf34a2016-02-24 16:54:27 -0500151
Al Viro05b39a82016-02-13 11:04:19 -0500152 op->downcall.status = ret;
Mike Marshall1182fca2015-07-17 10:38:15 -0400153 /* retry if operation has not been serviced and if requested */
Al Viro05b39a82016-02-13 11:04:19 -0500154 if (ret == -EAGAIN) {
155 op->attempts++;
156 timeout = op_timeout_secs * HZ;
Mike Marshall1182fca2015-07-17 10:38:15 -0400157 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500158 "orangefs: tag %llu (%s)"
Mike Marshall1182fca2015-07-17 10:38:15 -0400159 " -- operation to be retried (%d attempt)\n",
160 llu(op->tag),
161 op_name,
Al Viro05b39a82016-02-13 11:04:19 -0500162 op->attempts);
Mike Marshall1182fca2015-07-17 10:38:15 -0400163
Mike Marshalladcf34a2016-02-24 16:54:27 -0500164 /*
165 * io ops (ops that use the shared memory buffer) have
166 * to be returned to their caller for a retry. Other ops
167 * can just be recycled here.
168 */
Mike Marshall1182fca2015-07-17 10:38:15 -0400169 if (!op->uses_shared_memory)
Mike Marshall1182fca2015-07-17 10:38:15 -0400170 goto retry_servicing;
Mike Marshall1182fca2015-07-17 10:38:15 -0400171 }
172
Al Viro05b39a82016-02-13 11:04:19 -0500173out:
Mike Marshall1182fca2015-07-17 10:38:15 -0400174 gossip_debug(GOSSIP_WAIT_DEBUG,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500175 "orangefs: service_operation %s returning: %d for %p.\n",
Mike Marshall1182fca2015-07-17 10:38:15 -0400176 op_name,
177 ret,
178 op);
179 return ret;
180}
181
Al Viro78699e22016-02-11 23:07:19 -0500182bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
183{
184 u64 tag = op->tag;
185 if (!op_state_in_progress(op))
186 return false;
187
188 op->slot_to_free = op->upcall.req.io.buf_index;
189 memset(&op->upcall, 0, sizeof(op->upcall));
190 memset(&op->downcall, 0, sizeof(op->downcall));
191 op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
192 op->upcall.req.cancel.op_tag = tag;
193 op->downcall.type = ORANGEFS_VFS_OP_INVALID;
194 op->downcall.status = -1;
195 orangefs_new_tag(op);
196
197 spin_lock(&orangefs_request_list_lock);
198 /* orangefs_request_list_lock is enough of a barrier here */
199 if (!__is_daemon_in_service()) {
200 spin_unlock(&orangefs_request_list_lock);
201 return false;
202 }
Al Viro98815ad2016-02-13 10:38:23 -0500203 spin_lock(&op->lock);
204 set_op_state_waiting(op);
205 list_add(&op->list, &orangefs_request_list);
206 spin_unlock(&op->lock);
Al Viro78699e22016-02-11 23:07:19 -0500207 spin_unlock(&orangefs_request_list_lock);
208
209 gossip_debug(GOSSIP_UTILS_DEBUG,
210 "Attempting ORANGEFS operation cancellation of tag %llu\n",
211 llu(tag));
212 return true;
213}
214
Al Viroe07db0a2016-01-21 22:21:41 -0500215static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
Mike Marshall1182fca2015-07-17 10:38:15 -0400216{
217 /*
218 * handle interrupted cases depending on what state we were in when
219 * the interruption is detected. there is a coarse grained lock
220 * across the operation.
221 *
Al Viroeab9b382016-01-23 13:09:05 -0500222 * Called with op->lock held.
Mike Marshall1182fca2015-07-17 10:38:15 -0400223 */
Al Viroed42fe02016-01-22 19:47:47 -0500224 op->op_state |= OP_VFS_STATE_GIVEN_UP;
Al Viro05a50a52016-02-18 18:59:44 -0500225 /* from that point on it can't be moved by anybody else */
226 if (list_empty(&op->list)) {
227 /* caught copying to/from daemon */
228 BUG_ON(op_state_serviced(op));
229 spin_unlock(&op->lock);
230 wait_for_completion(&op->waitq);
231 } else if (op_state_waiting(op)) {
Mike Marshall1182fca2015-07-17 10:38:15 -0400232 /*
233 * upcall hasn't been read; remove op from upcall request
234 * list.
235 */
236 spin_unlock(&op->lock);
Al Viroed42fe02016-01-22 19:47:47 -0500237 spin_lock(&orangefs_request_list_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500238 list_del_init(&op->list);
Al Viroed42fe02016-01-22 19:47:47 -0500239 spin_unlock(&orangefs_request_list_lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400240 gossip_debug(GOSSIP_WAIT_DEBUG,
241 "Interrupted: Removed op %p from request_list\n",
242 op);
243 } else if (op_state_in_progress(op)) {
244 /* op must be removed from the in progress htable */
245 spin_unlock(&op->lock);
246 spin_lock(&htable_ops_in_progress_lock);
Al Viro05a50a52016-02-18 18:59:44 -0500247 list_del_init(&op->list);
Mike Marshall1182fca2015-07-17 10:38:15 -0400248 spin_unlock(&htable_ops_in_progress_lock);
249 gossip_debug(GOSSIP_WAIT_DEBUG,
250 "Interrupted: Removed op %p"
251 " from htable_ops_in_progress\n",
252 op);
Al Viro05a50a52016-02-18 18:59:44 -0500253 } else {
Mike Marshall1182fca2015-07-17 10:38:15 -0400254 spin_unlock(&op->lock);
255 gossip_err("interrupted operation is in a weird state 0x%x\n",
256 op->op_state);
Mike Marshall1182fca2015-07-17 10:38:15 -0400257 }
Al Virod2d87a32016-02-13 10:15:22 -0500258 reinit_completion(&op->waitq);
Mike Marshall1182fca2015-07-17 10:38:15 -0400259}
260
261/*
262 * sleeps on waitqueue waiting for matching downcall.
263 * if client-core finishes servicing, then we are good to go.
264 * else if client-core exits, we get woken up here, and retry with a timeout
265 *
266 * Post when this call returns to the caller, the specified op will no
267 * longer be on any list or htable.
268 *
269 * Returns 0 on success and -errno on failure
270 * Errors are:
271 * EAGAIN in case we want the caller to requeue and try again..
272 * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
273 * operation since client-core seems to be exiting too often
274 * or if we were interrupted.
Al Virod2d87a32016-02-13 10:15:22 -0500275 *
276 * Returns with op->lock taken.
Mike Marshall1182fca2015-07-17 10:38:15 -0400277 */
Al Viroc72f15b2016-02-13 10:49:24 -0500278static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op,
Al Viro05b39a82016-02-13 11:04:19 -0500279 long timeout,
Al Viroc72f15b2016-02-13 10:49:24 -0500280 bool interruptible)
Mike Marshall1182fca2015-07-17 10:38:15 -0400281{
Al Viro05b39a82016-02-13 11:04:19 -0500282 long n;
Al Viroc72f15b2016-02-13 10:49:24 -0500283
284 if (interruptible)
Mike Marshalladcf34a2016-02-24 16:54:27 -0500285 n = wait_for_completion_interruptible_timeout(&op->waitq,
286 timeout);
Al Viroc72f15b2016-02-13 10:49:24 -0500287 else
288 n = wait_for_completion_killable_timeout(&op->waitq, timeout);
289
Mike Marshall1182fca2015-07-17 10:38:15 -0400290 spin_lock(&op->lock);
Mike Marshall1182fca2015-07-17 10:38:15 -0400291
Al Virod2d87a32016-02-13 10:15:22 -0500292 if (op_state_serviced(op))
293 return 0;
294
295 if (unlikely(n < 0)) {
296 gossip_debug(GOSSIP_WAIT_DEBUG,
297 "*** %s:"
298 " operation interrupted by a signal (tag "
299 "%llu, op %p)\n",
300 __func__,
301 llu(op->tag),
302 op);
303 return -EINTR;
304 }
Al Virod2d87a32016-02-13 10:15:22 -0500305 if (op_state_purged(op)) {
306 gossip_debug(GOSSIP_WAIT_DEBUG,
307 "*** %s:"
308 " operation purged (tag "
309 "%llu, %p, att %d)\n",
310 __func__,
311 llu(op->tag),
312 op,
313 op->attempts);
314 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
315 -EAGAIN :
316 -EIO;
317 }
318 /* must have timed out, then... */
319 gossip_debug(GOSSIP_WAIT_DEBUG,
320 "*** %s:"
321 " operation timed out (tag"
322 " %llu, %p, att %d)\n",
323 __func__,
324 llu(op->tag),
325 op,
326 op->attempts);
327 return -ETIMEDOUT;
Mike Marshall1182fca2015-07-17 10:38:15 -0400328}