2 * (C) 2001 Clemson University and The University of Chicago
3 * (C) 2011 Omnibond Systems
5 * Changes by Acxiom Corporation to implement generic service_operation()
6 * function, Copyright Acxiom Corporation, 2005.
8 * See COPYING in top-level directory.
12 * In-kernel waitqueue operations.
16 #include "orangefs-kernel.h"
17 #include "orangefs-bufmap.h"
19 static int wait_for_matching_downcall(struct orangefs_kernel_op_s
*, long, bool);
20 static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s
*);
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.
29 void purge_waiting_ops(void)
31 struct orangefs_kernel_op_s
*op
;
33 spin_lock(&orangefs_request_list_lock
);
34 list_for_each_entry(op
, &orangefs_request_list
, list
) {
35 gossip_debug(GOSSIP_WAIT_DEBUG
,
36 "pvfs2-client-core: purging op tag %llu %s\n",
38 get_opname_string(op
));
39 set_op_state_purged(op
);
40 gossip_debug(GOSSIP_DEV_DEBUG
,
41 "%s: op:%s: op_state:%d: process:%s:\n",
43 get_opname_string(op
),
47 spin_unlock(&orangefs_request_list_lock
);
51 * submits a ORANGEFS operation and waits for it to complete
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.
58 * Returns contents of op->downcall.status for convenience
60 int service_operation(struct orangefs_kernel_op_s
*op
,
64 long timeout
= MAX_SCHEDULE_TIMEOUT
;
67 DEFINE_WAIT(wait_entry
);
69 op
->upcall
.tgid
= current
->tgid
;
70 op
->upcall
.pid
= current
->pid
;
73 op
->downcall
.status
= 0;
74 gossip_debug(GOSSIP_WAIT_DEBUG
,
75 "%s: %s op:%p: process:%s: pid:%d:\n",
83 * If ORANGEFS_OP_NO_MUTEX was set in flags, we need to avoid
84 * acquiring the request_mutex because we're servicing a
85 * high priority remount operation and the request_mutex is
88 if (!(flags
& ORANGEFS_OP_NO_MUTEX
)) {
89 if (flags
& ORANGEFS_OP_INTERRUPTIBLE
)
90 ret
= mutex_lock_interruptible(&orangefs_request_mutex
);
92 ret
= mutex_lock_killable(&orangefs_request_mutex
);
94 * check to see if we were interrupted while waiting for
98 op
->downcall
.status
= ret
;
99 gossip_debug(GOSSIP_WAIT_DEBUG
,
100 "%s: service_operation interrupted.\n",
106 /* queue up the operation */
107 spin_lock(&orangefs_request_list_lock
);
108 spin_lock(&op
->lock
);
109 set_op_state_waiting(op
);
110 gossip_debug(GOSSIP_DEV_DEBUG
,
111 "%s: op:%s: op_state:%d: process:%s:\n",
113 get_opname_string(op
),
116 /* add high priority remount op to the front of the line. */
117 if (flags
& ORANGEFS_OP_PRIORITY
)
118 list_add(&op
->list
, &orangefs_request_list
);
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()) {
124 gossip_debug(GOSSIP_WAIT_DEBUG
,
125 "%s:client core is NOT in service.\n",
128 * Don't wait for the userspace component to return if
129 * the filesystem is being umounted anyway.
131 if (op
->upcall
.type
== ORANGEFS_VFS_OP_FS_UMOUNT
)
134 timeout
= op_timeout_secs
* HZ
;
136 spin_unlock(&orangefs_request_list_lock
);
138 if (!(flags
& ORANGEFS_OP_NO_MUTEX
))
139 mutex_unlock(&orangefs_request_mutex
);
141 ret
= wait_for_matching_downcall(op
, timeout
,
142 flags
& ORANGEFS_OP_INTERRUPTIBLE
);
144 gossip_debug(GOSSIP_WAIT_DEBUG
,
145 "%s: wait_for_matching_downcall returned %d for %p\n",
150 /* got matching downcall; make sure status is in errno format */
152 spin_unlock(&op
->lock
);
153 op
->downcall
.status
=
154 orangefs_normalize_to_errno(op
->downcall
.status
);
155 ret
= op
->downcall
.status
;
159 /* failed to get matching downcall */
160 if (ret
== -ETIMEDOUT
) {
161 gossip_err("%s: %s -- wait timed out; aborting attempt.\n",
167 * remove a waiting op from the request list or
168 * remove an in-progress op from the in-progress list.
170 orangefs_clean_up_interrupted_operation(op
);
172 op
->downcall
.status
= ret
;
173 /* retry if operation has not been serviced and if requested */
174 if (ret
== -EAGAIN
) {
176 timeout
= op_timeout_secs
* HZ
;
177 gossip_debug(GOSSIP_WAIT_DEBUG
,
178 "orangefs: tag %llu (%s)"
179 " -- operation to be retried (%d attempt)\n",
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.
189 if (!op
->uses_shared_memory
)
190 goto retry_servicing
;
194 gossip_debug(GOSSIP_WAIT_DEBUG
,
195 "%s: %s returning: %d for %p.\n",
203 /* This can get called on an I/O op if it had a bad service_operation. */
204 bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s
*op
)
207 if (!op_state_in_progress(op
))
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
);
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
);
225 spin_lock(&op
->lock
);
226 set_op_state_waiting(op
);
227 gossip_debug(GOSSIP_DEV_DEBUG
,
228 "%s: op:%s: op_state:%d: process:%s:\n",
230 get_opname_string(op
),
233 list_add(&op
->list
, &orangefs_request_list
);
234 spin_unlock(&op
->lock
);
235 spin_unlock(&orangefs_request_list_lock
);
237 gossip_debug(GOSSIP_WAIT_DEBUG
,
238 "Attempting ORANGEFS operation cancellation of tag %llu\n",
244 * Change an op to the "given up" state and remove it from its list.
247 orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s
*op
)
250 * handle interrupted cases depending on what state we were in when
251 * the interruption is detected.
253 * Called with op->lock held.
257 * List manipulation code elsewhere will ignore ops that
258 * have been given up upon.
260 op
->op_state
|= OP_VFS_STATE_GIVEN_UP
;
262 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
)) {
269 * upcall hasn't been read; remove op from upcall request
272 spin_unlock(&op
->lock
);
273 spin_lock(&orangefs_request_list_lock
);
274 list_del_init(&op
->list
);
275 spin_unlock(&orangefs_request_list_lock
);
276 gossip_debug(GOSSIP_WAIT_DEBUG
,
277 "Interrupted: Removed op %p from request_list\n",
279 } else if (op_state_in_progress(op
)) {
280 /* op must be removed from the in progress htable */
281 spin_unlock(&op
->lock
);
282 spin_lock(&orangefs_htable_ops_in_progress_lock
);
283 list_del_init(&op
->list
);
284 spin_unlock(&orangefs_htable_ops_in_progress_lock
);
285 gossip_debug(GOSSIP_WAIT_DEBUG
,
286 "Interrupted: Removed op %p"
287 " from htable_ops_in_progress\n",
290 spin_unlock(&op
->lock
);
291 gossip_err("interrupted operation is in a weird state 0x%x\n",
294 reinit_completion(&op
->waitq
);
298 * Sleeps on waitqueue waiting for matching downcall.
299 * If client-core finishes servicing, then we are good to go.
300 * else if client-core exits, we get woken up here, and retry with a timeout
302 * 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.
305 * Returns 0 on success and -errno on failure
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.
312 * Returns with op->lock taken.
314 static int wait_for_matching_downcall(struct orangefs_kernel_op_s
*op
,
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.
327 n
= wait_for_completion_interruptible_timeout(&op
->waitq
,
330 n
= wait_for_completion_killable_timeout(&op
->waitq
, timeout
);
332 spin_lock(&op
->lock
);
334 if (op_state_serviced(op
))
337 if (unlikely(n
< 0)) {
338 gossip_debug(GOSSIP_WAIT_DEBUG
,
339 "%s: operation interrupted, tag %llu, %p\n",
345 if (op_state_purged(op
)) {
346 gossip_debug(GOSSIP_WAIT_DEBUG
,
347 "%s: operation purged, tag %llu, %p, %d\n",
352 return (op
->attempts
< ORANGEFS_PURGE_RETRY_COUNT
) ?
356 /* must have timed out, then... */
357 gossip_debug(GOSSIP_WAIT_DEBUG
,
358 "%s: operation timed out, tag %llu, %p, %d)\n",