Commit | Line | Data |
---|---|---|
1182fca3 MM |
1 | /* |
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" | |
575e9461 MM |
16 | #include "orangefs-kernel.h" |
17 | #include "orangefs-bufmap.h" | |
1182fca3 | 18 | |
05b39a8b | 19 | static int wait_for_matching_downcall(struct orangefs_kernel_op_s *, long, bool); |
d2d87a3b | 20 | static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *); |
ade3d781 | 21 | |
1182fca3 MM |
22 | /* |
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 | */ | |
29 | void purge_waiting_ops(void) | |
30 | { | |
8bb8aefd | 31 | struct orangefs_kernel_op_s *op; |
1182fca3 | 32 | |
8bb8aefd YL |
33 | spin_lock(&orangefs_request_list_lock); |
34 | list_for_each_entry(op, &orangefs_request_list, list) { | |
1182fca3 MM |
35 | gossip_debug(GOSSIP_WAIT_DEBUG, |
36 | "pvfs2-client-core: purging op tag %llu %s\n", | |
37 | llu(op->tag), | |
38 | get_opname_string(op)); | |
1182fca3 | 39 | set_op_state_purged(op); |
1182fca3 | 40 | } |
8bb8aefd | 41 | spin_unlock(&orangefs_request_list_lock); |
1182fca3 MM |
42 | } |
43 | ||
44 | /* | |
8bb8aefd | 45 | * submits a ORANGEFS operation and waits for it to complete |
1182fca3 MM |
46 | * |
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 | */ | |
8bb8aefd | 54 | int service_operation(struct orangefs_kernel_op_s *op, |
1182fca3 MM |
55 | const char *op_name, |
56 | int flags) | |
57 | { | |
05b39a8b | 58 | long timeout = MAX_SCHEDULE_TIMEOUT; |
1182fca3 | 59 | /* flags to modify behavior */ |
1182fca3 MM |
60 | int ret = 0; |
61 | ||
ce6c414e | 62 | DEFINE_WAIT(wait_entry); |
1182fca3 MM |
63 | |
64 | op->upcall.tgid = current->tgid; | |
65 | op->upcall.pid = current->pid; | |
66 | ||
67 | retry_servicing: | |
68 | op->downcall.status = 0; | |
69 | gossip_debug(GOSSIP_WAIT_DEBUG, | |
5253487e MM |
70 | "%s: %s op:%p: process:%s: pid:%d:\n", |
71 | __func__, | |
1182fca3 | 72 | op_name, |
5253487e | 73 | op, |
1182fca3 MM |
74 | current->comm, |
75 | current->pid); | |
76 | ||
8bb8aefd | 77 | if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) { |
c72f15b7 AV |
78 | if (flags & ORANGEFS_OP_INTERRUPTIBLE) |
79 | ret = mutex_lock_interruptible(&request_mutex); | |
80 | else | |
81 | ret = mutex_lock_killable(&request_mutex); | |
1182fca3 MM |
82 | /* |
83 | * check to see if we were interrupted while waiting for | |
84 | * semaphore | |
85 | */ | |
86 | if (ret < 0) { | |
1182fca3 MM |
87 | op->downcall.status = ret; |
88 | gossip_debug(GOSSIP_WAIT_DEBUG, | |
8bb8aefd | 89 | "orangefs: service_operation interrupted.\n"); |
1182fca3 MM |
90 | return ret; |
91 | } | |
92 | } | |
93 | ||
98815ade AV |
94 | /* 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()) { | |
1182fca3 | 105 | gossip_debug(GOSSIP_WAIT_DEBUG, |
98815ade | 106 | "%s:client core is NOT in service.\n", |
1182fca3 | 107 | __func__); |
05b39a8b | 108 | timeout = op_timeout_secs * HZ; |
1182fca3 | 109 | } |
98815ade | 110 | spin_unlock(&orangefs_request_list_lock); |
1182fca3 | 111 | |
8bb8aefd | 112 | if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) |
1182fca3 MM |
113 | mutex_unlock(&request_mutex); |
114 | ||
115 | /* | |
116 | * If we are asked to service an asynchronous operation from | |
117 | * VFS perspective, we are done. | |
118 | */ | |
8bb8aefd | 119 | if (flags & ORANGEFS_OP_ASYNC) |
1182fca3 MM |
120 | return 0; |
121 | ||
05b39a8b AV |
122 | ret = wait_for_matching_downcall(op, timeout, |
123 | flags & ORANGEFS_OP_INTERRUPTIBLE); | |
5253487e MM |
124 | |
125 | gossip_debug(GOSSIP_WAIT_DEBUG, | |
126 | "%s: wait_for_matching_downcall returned %d for %p\n", | |
127 | __func__, | |
128 | ret, | |
129 | op); | |
130 | ||
05b39a8b | 131 | if (!ret) { |
d2d87a3b | 132 | spin_unlock(&op->lock); |
1182fca3 MM |
133 | /* got matching downcall; make sure status is in errno format */ |
134 | op->downcall.status = | |
8bb8aefd | 135 | orangefs_normalize_to_errno(op->downcall.status); |
1182fca3 | 136 | ret = op->downcall.status; |
05b39a8b | 137 | goto out; |
1182fca3 MM |
138 | } |
139 | ||
05b39a8b AV |
140 | /* failed to get matching downcall */ |
141 | if (ret == -ETIMEDOUT) { | |
142 | gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n", | |
143 | op_name); | |
144 | } | |
145 | orangefs_clean_up_interrupted_operation(op); | |
146 | op->downcall.status = ret; | |
1182fca3 | 147 | /* retry if operation has not been serviced and if requested */ |
05b39a8b AV |
148 | if (ret == -EAGAIN) { |
149 | op->attempts++; | |
150 | timeout = op_timeout_secs * HZ; | |
1182fca3 | 151 | gossip_debug(GOSSIP_WAIT_DEBUG, |
8bb8aefd | 152 | "orangefs: tag %llu (%s)" |
1182fca3 MM |
153 | " -- operation to be retried (%d attempt)\n", |
154 | llu(op->tag), | |
155 | op_name, | |
05b39a8b | 156 | op->attempts); |
1182fca3 MM |
157 | |
158 | if (!op->uses_shared_memory) | |
159 | /* | |
160 | * this operation doesn't use the shared memory | |
161 | * system | |
162 | */ | |
163 | goto retry_servicing; | |
1182fca3 MM |
164 | } |
165 | ||
05b39a8b | 166 | out: |
1182fca3 | 167 | gossip_debug(GOSSIP_WAIT_DEBUG, |
8bb8aefd | 168 | "orangefs: service_operation %s returning: %d for %p.\n", |
1182fca3 MM |
169 | op_name, |
170 | ret, | |
171 | op); | |
172 | return ret; | |
173 | } | |
174 | ||
78699e29 AV |
175 | bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op) |
176 | { | |
177 | u64 tag = op->tag; | |
178 | if (!op_state_in_progress(op)) | |
179 | return false; | |
180 | ||
181 | op->slot_to_free = op->upcall.req.io.buf_index; | |
182 | memset(&op->upcall, 0, sizeof(op->upcall)); | |
183 | memset(&op->downcall, 0, sizeof(op->downcall)); | |
184 | op->upcall.type = ORANGEFS_VFS_OP_CANCEL; | |
185 | op->upcall.req.cancel.op_tag = tag; | |
186 | op->downcall.type = ORANGEFS_VFS_OP_INVALID; | |
187 | op->downcall.status = -1; | |
188 | orangefs_new_tag(op); | |
189 | ||
190 | spin_lock(&orangefs_request_list_lock); | |
191 | /* orangefs_request_list_lock is enough of a barrier here */ | |
192 | if (!__is_daemon_in_service()) { | |
193 | spin_unlock(&orangefs_request_list_lock); | |
194 | return false; | |
195 | } | |
98815ade AV |
196 | spin_lock(&op->lock); |
197 | set_op_state_waiting(op); | |
198 | list_add(&op->list, &orangefs_request_list); | |
199 | spin_unlock(&op->lock); | |
78699e29 AV |
200 | spin_unlock(&orangefs_request_list_lock); |
201 | ||
202 | gossip_debug(GOSSIP_UTILS_DEBUG, | |
203 | "Attempting ORANGEFS operation cancellation of tag %llu\n", | |
204 | llu(tag)); | |
205 | return true; | |
206 | } | |
207 | ||
e07db0a2 | 208 | static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op) |
1182fca3 MM |
209 | { |
210 | /* | |
211 | * handle interrupted cases depending on what state we were in when | |
212 | * the interruption is detected. there is a coarse grained lock | |
213 | * across the operation. | |
214 | * | |
eab9b389 | 215 | * Called with op->lock held. |
1182fca3 | 216 | */ |
ed42fe05 | 217 | op->op_state |= OP_VFS_STATE_GIVEN_UP; |
1182fca3 MM |
218 | |
219 | if (op_state_waiting(op)) { | |
220 | /* | |
221 | * upcall hasn't been read; remove op from upcall request | |
222 | * list. | |
223 | */ | |
224 | spin_unlock(&op->lock); | |
ed42fe05 AV |
225 | spin_lock(&orangefs_request_list_lock); |
226 | list_del(&op->list); | |
227 | spin_unlock(&orangefs_request_list_lock); | |
1182fca3 MM |
228 | gossip_debug(GOSSIP_WAIT_DEBUG, |
229 | "Interrupted: Removed op %p from request_list\n", | |
230 | op); | |
231 | } else if (op_state_in_progress(op)) { | |
232 | /* op must be removed from the in progress htable */ | |
233 | spin_unlock(&op->lock); | |
234 | spin_lock(&htable_ops_in_progress_lock); | |
235 | list_del(&op->list); | |
236 | spin_unlock(&htable_ops_in_progress_lock); | |
237 | gossip_debug(GOSSIP_WAIT_DEBUG, | |
238 | "Interrupted: Removed op %p" | |
239 | " from htable_ops_in_progress\n", | |
240 | op); | |
241 | } else if (!op_state_serviced(op)) { | |
242 | spin_unlock(&op->lock); | |
243 | gossip_err("interrupted operation is in a weird state 0x%x\n", | |
244 | op->op_state); | |
84d02150 MM |
245 | } else { |
246 | /* | |
247 | * It is not intended for execution to flow here, | |
248 | * but having this unlock here makes sparse happy. | |
249 | */ | |
250 | gossip_err("%s: can't get here.\n", __func__); | |
251 | spin_unlock(&op->lock); | |
1182fca3 | 252 | } |
d2d87a3b | 253 | reinit_completion(&op->waitq); |
1182fca3 MM |
254 | } |
255 | ||
256 | /* | |
257 | * sleeps on waitqueue waiting for matching downcall. | |
258 | * if client-core finishes servicing, then we are good to go. | |
259 | * else if client-core exits, we get woken up here, and retry with a timeout | |
260 | * | |
261 | * Post when this call returns to the caller, the specified op will no | |
262 | * longer be on any list or htable. | |
263 | * | |
264 | * Returns 0 on success and -errno on failure | |
265 | * Errors are: | |
266 | * EAGAIN in case we want the caller to requeue and try again.. | |
267 | * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this | |
268 | * operation since client-core seems to be exiting too often | |
269 | * or if we were interrupted. | |
d2d87a3b AV |
270 | * |
271 | * Returns with op->lock taken. | |
1182fca3 | 272 | */ |
c72f15b7 | 273 | static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op, |
05b39a8b | 274 | long timeout, |
c72f15b7 | 275 | bool interruptible) |
1182fca3 | 276 | { |
05b39a8b | 277 | long n; |
c72f15b7 AV |
278 | |
279 | if (interruptible) | |
280 | n = wait_for_completion_interruptible_timeout(&op->waitq, timeout); | |
281 | else | |
282 | n = wait_for_completion_killable_timeout(&op->waitq, timeout); | |
283 | ||
d2d87a3b | 284 | spin_lock(&op->lock); |
1182fca3 | 285 | |
d2d87a3b AV |
286 | if (op_state_serviced(op)) |
287 | return 0; | |
70c6ea26 | 288 | |
d2d87a3b AV |
289 | if (unlikely(n < 0)) { |
290 | gossip_debug(GOSSIP_WAIT_DEBUG, | |
291 | "*** %s:" | |
292 | " operation interrupted by a signal (tag " | |
293 | "%llu, op %p)\n", | |
294 | __func__, | |
295 | llu(op->tag), | |
296 | op); | |
297 | return -EINTR; | |
1182fca3 | 298 | } |
d2d87a3b AV |
299 | if (op_state_purged(op)) { |
300 | gossip_debug(GOSSIP_WAIT_DEBUG, | |
301 | "*** %s:" | |
302 | " operation purged (tag " | |
303 | "%llu, %p, att %d)\n", | |
304 | __func__, | |
305 | llu(op->tag), | |
306 | op, | |
307 | op->attempts); | |
308 | return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ? | |
309 | -EAGAIN : | |
310 | -EIO; | |
311 | } | |
312 | /* must have timed out, then... */ | |
313 | gossip_debug(GOSSIP_WAIT_DEBUG, | |
314 | "*** %s:" | |
315 | " operation timed out (tag" | |
316 | " %llu, %p, att %d)\n", | |
317 | __func__, | |
318 | llu(op->tag), | |
319 | op, | |
320 | op->attempts); | |
321 | return -ETIMEDOUT; | |
1182fca3 | 322 | } |