]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - fs/orangefs/waitqueue.c
orangefs: sanitize handling of request list
[karo-tx-linux.git] / fs / orangefs / waitqueue.c
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"
16 #include "orangefs-kernel.h"
17 #include "orangefs-bufmap.h"
18
19 static int wait_for_matching_downcall(struct orangefs_kernel_op_s *);
20 static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *);
21
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 {
31         struct orangefs_kernel_op_s *op;
32
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",
37                              llu(op->tag),
38                              get_opname_string(op));
39                 set_op_state_purged(op);
40         }
41         spin_unlock(&orangefs_request_list_lock);
42 }
43
44 /*
45  * submits a ORANGEFS operation and waits for it to complete
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  */
54 int service_operation(struct orangefs_kernel_op_s *op,
55                       const char *op_name,
56                       int flags)
57 {
58         /* flags to modify behavior */
59         sigset_t orig_sigset;
60         int ret = 0;
61
62         DEFINE_WAIT(wait_entry);
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,
70                      "orangefs: service_operation: %s %p\n",
71                      op_name,
72                      op);
73         gossip_debug(GOSSIP_WAIT_DEBUG,
74                      "orangefs: operation posted by process: %s, pid: %i\n",
75                      current->comm,
76                      current->pid);
77
78         /* mask out signals if this operation is not to be interrupted */
79         if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
80                 orangefs_block_signals(&orig_sigset);
81
82         if (!(flags & ORANGEFS_OP_NO_SEMAPHORE)) {
83                 ret = mutex_lock_interruptible(&request_mutex);
84                 /*
85                  * check to see if we were interrupted while waiting for
86                  * semaphore
87                  */
88                 if (ret < 0) {
89                         if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
90                                 orangefs_set_signals(&orig_sigset);
91                         op->downcall.status = ret;
92                         gossip_debug(GOSSIP_WAIT_DEBUG,
93                                      "orangefs: service_operation interrupted.\n");
94                         return ret;
95                 }
96         }
97
98         /* queue up the operation */
99         spin_lock(&orangefs_request_list_lock);
100         spin_lock(&op->lock);
101         set_op_state_waiting(op);
102         if (flags & ORANGEFS_OP_PRIORITY)
103                 list_add(&op->list, &orangefs_request_list);
104         else
105                 list_add_tail(&op->list, &orangefs_request_list);
106         spin_unlock(&op->lock);
107         wake_up_interruptible(&orangefs_request_list_waitq);
108         if (!__is_daemon_in_service()) {
109                 /*
110                  * By incrementing the per-operation attempt counter, we
111                  * directly go into the timeout logic while waiting for
112                  * the matching downcall to be read
113                  */
114                 gossip_debug(GOSSIP_WAIT_DEBUG,
115                              "%s:client core is NOT in service.\n",
116                              __func__);
117                 op->attempts++;
118         }
119         spin_unlock(&orangefs_request_list_lock);
120
121         if (!(flags & ORANGEFS_OP_NO_SEMAPHORE))
122                 mutex_unlock(&request_mutex);
123
124         /*
125          * If we are asked to service an asynchronous operation from
126          * VFS perspective, we are done.
127          */
128         if (flags & ORANGEFS_OP_ASYNC)
129                 return 0;
130
131         ret = wait_for_matching_downcall(op);
132
133         if (ret < 0) {
134                 /* failed to get matching downcall */
135                 if (ret == -ETIMEDOUT) {
136                         gossip_err("orangefs: %s -- wait timed out; aborting attempt.\n",
137                                    op_name);
138                 }
139                 orangefs_clean_up_interrupted_operation(op);
140                 op->downcall.status = ret;
141         } else {
142                 spin_unlock(&op->lock);
143                 /* got matching downcall; make sure status is in errno format */
144                 op->downcall.status =
145                     orangefs_normalize_to_errno(op->downcall.status);
146                 ret = op->downcall.status;
147         }
148
149         if (!(flags & ORANGEFS_OP_INTERRUPTIBLE))
150                 orangefs_set_signals(&orig_sigset);
151
152         BUG_ON(ret != op->downcall.status);
153         /* retry if operation has not been serviced and if requested */
154         if (!op_state_serviced(op) && op->downcall.status == -EAGAIN) {
155                 gossip_debug(GOSSIP_WAIT_DEBUG,
156                              "orangefs: tag %llu (%s)"
157                              " -- operation to be retried (%d attempt)\n",
158                              llu(op->tag),
159                              op_name,
160                              op->attempts + 1);
161
162                 if (!op->uses_shared_memory)
163                         /*
164                          * this operation doesn't use the shared memory
165                          * system
166                          */
167                         goto retry_servicing;
168
169                 /* op uses shared memory */
170                 if (orangefs_get_bufmap_init() == 0) {
171                         WARN_ON(1);
172                         /*
173                          * This operation uses the shared memory system AND
174                          * the system is not yet ready. This situation occurs
175                          * when the client-core is restarted AND there were
176                          * operations waiting to be processed or were already
177                          * in process.
178                          */
179                         gossip_debug(GOSSIP_WAIT_DEBUG,
180                                      "uses_shared_memory is true.\n");
181                         gossip_debug(GOSSIP_WAIT_DEBUG,
182                                      "Client core in-service status(%d).\n",
183                                      is_daemon_in_service());
184                         gossip_debug(GOSSIP_WAIT_DEBUG, "bufmap_init:%d.\n",
185                                      orangefs_get_bufmap_init());
186                         gossip_debug(GOSSIP_WAIT_DEBUG,
187                                      "operation's status is 0x%0x.\n",
188                                      op->op_state);
189
190                         /*
191                          * let process sleep for a few seconds so shared
192                          * memory system can be initialized.
193                          */
194                         prepare_to_wait(&orangefs_bufmap_init_waitq,
195                                         &wait_entry,
196                                         TASK_INTERRUPTIBLE);
197
198                         /*
199                          * Wait for orangefs_bufmap_initialize() to wake me up
200                          * within the allotted time.
201                          */
202                         ret = schedule_timeout(
203                                 ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS * HZ);
204
205                         gossip_debug(GOSSIP_WAIT_DEBUG,
206                                      "Value returned from schedule_timeout:"
207                                      "%d.\n",
208                                      ret);
209                         gossip_debug(GOSSIP_WAIT_DEBUG,
210                                      "Is shared memory available? (%d).\n",
211                                      orangefs_get_bufmap_init());
212
213                         finish_wait(&orangefs_bufmap_init_waitq, &wait_entry);
214
215                         if (orangefs_get_bufmap_init() == 0) {
216                                 gossip_err("%s:The shared memory system has not started in %d seconds after the client core restarted.  Aborting user's request(%s).\n",
217                                            __func__,
218                                            ORANGEFS_BUFMAP_WAIT_TIMEOUT_SECS,
219                                            get_opname_string(op));
220                                 return -EIO;
221                         }
222
223                         /*
224                          * Return to the calling function and re-populate a
225                          * shared memory buffer.
226                          */
227                         return -EAGAIN;
228                 }
229         }
230
231         gossip_debug(GOSSIP_WAIT_DEBUG,
232                      "orangefs: service_operation %s returning: %d for %p.\n",
233                      op_name,
234                      ret,
235                      op);
236         return ret;
237 }
238
239 bool orangefs_cancel_op_in_progress(struct orangefs_kernel_op_s *op)
240 {
241         u64 tag = op->tag;
242         if (!op_state_in_progress(op))
243                 return false;
244
245         op->slot_to_free = op->upcall.req.io.buf_index;
246         memset(&op->upcall, 0, sizeof(op->upcall));
247         memset(&op->downcall, 0, sizeof(op->downcall));
248         op->upcall.type = ORANGEFS_VFS_OP_CANCEL;
249         op->upcall.req.cancel.op_tag = tag;
250         op->downcall.type = ORANGEFS_VFS_OP_INVALID;
251         op->downcall.status = -1;
252         orangefs_new_tag(op);
253
254         spin_lock(&orangefs_request_list_lock);
255         /* orangefs_request_list_lock is enough of a barrier here */
256         if (!__is_daemon_in_service()) {
257                 spin_unlock(&orangefs_request_list_lock);
258                 return false;
259         }
260         spin_lock(&op->lock);
261         set_op_state_waiting(op);
262         list_add(&op->list, &orangefs_request_list);
263         spin_unlock(&op->lock);
264         spin_unlock(&orangefs_request_list_lock);
265
266         gossip_debug(GOSSIP_UTILS_DEBUG,
267                      "Attempting ORANGEFS operation cancellation of tag %llu\n",
268                      llu(tag));
269         return true;
270 }
271
272 static void orangefs_clean_up_interrupted_operation(struct orangefs_kernel_op_s *op)
273 {
274         /*
275          * handle interrupted cases depending on what state we were in when
276          * the interruption is detected.  there is a coarse grained lock
277          * across the operation.
278          *
279          * Called with op->lock held.
280          */
281         op->op_state |= OP_VFS_STATE_GIVEN_UP;
282
283         if (op_state_waiting(op)) {
284                 /*
285                  * upcall hasn't been read; remove op from upcall request
286                  * list.
287                  */
288                 spin_unlock(&op->lock);
289                 spin_lock(&orangefs_request_list_lock);
290                 list_del(&op->list);
291                 spin_unlock(&orangefs_request_list_lock);
292                 gossip_debug(GOSSIP_WAIT_DEBUG,
293                              "Interrupted: Removed op %p from request_list\n",
294                              op);
295         } else if (op_state_in_progress(op)) {
296                 /* op must be removed from the in progress htable */
297                 spin_unlock(&op->lock);
298                 spin_lock(&htable_ops_in_progress_lock);
299                 list_del(&op->list);
300                 spin_unlock(&htable_ops_in_progress_lock);
301                 gossip_debug(GOSSIP_WAIT_DEBUG,
302                              "Interrupted: Removed op %p"
303                              " from htable_ops_in_progress\n",
304                              op);
305         } else if (!op_state_serviced(op)) {
306                 spin_unlock(&op->lock);
307                 gossip_err("interrupted operation is in a weird state 0x%x\n",
308                            op->op_state);
309         } else {
310                 /*
311                  * It is not intended for execution to flow here,
312                  * but having this unlock here makes sparse happy.
313                  */
314                 gossip_err("%s: can't get here.\n", __func__);
315                 spin_unlock(&op->lock);
316         }
317         reinit_completion(&op->waitq);
318 }
319
320 /*
321  * sleeps on waitqueue waiting for matching downcall.
322  * if client-core finishes servicing, then we are good to go.
323  * else if client-core exits, we get woken up here, and retry with a timeout
324  *
325  * Post when this call returns to the caller, the specified op will no
326  * longer be on any list or htable.
327  *
328  * Returns 0 on success and -errno on failure
329  * Errors are:
330  * EAGAIN in case we want the caller to requeue and try again..
331  * EINTR/EIO/ETIMEDOUT indicating we are done trying to service this
332  * operation since client-core seems to be exiting too often
333  * or if we were interrupted.
334  *
335  * Returns with op->lock taken.
336  */
337 static int wait_for_matching_downcall(struct orangefs_kernel_op_s *op)
338 {
339         long timeout, n;
340
341         timeout = op->attempts ? op_timeout_secs * HZ : MAX_SCHEDULE_TIMEOUT;
342         n = wait_for_completion_interruptible_timeout(&op->waitq, timeout);
343         spin_lock(&op->lock);
344
345         if (op_state_serviced(op))
346                 return 0;
347
348         if (unlikely(n < 0)) {
349                 gossip_debug(GOSSIP_WAIT_DEBUG,
350                              "*** %s:"
351                              " operation interrupted by a signal (tag "
352                              "%llu, op %p)\n",
353                              __func__,
354                              llu(op->tag),
355                              op);
356                 return -EINTR;
357         }
358         op->attempts++;
359         if (op_state_purged(op)) {
360                 gossip_debug(GOSSIP_WAIT_DEBUG,
361                              "*** %s:"
362                              " operation purged (tag "
363                              "%llu, %p, att %d)\n",
364                              __func__,
365                              llu(op->tag),
366                              op,
367                              op->attempts);
368                 return (op->attempts < ORANGEFS_PURGE_RETRY_COUNT) ?
369                          -EAGAIN :
370                          -EIO;
371         }
372         /* must have timed out, then... */
373         gossip_debug(GOSSIP_WAIT_DEBUG,
374                      "*** %s:"
375                      " operation timed out (tag"
376                      " %llu, %p, att %d)\n",
377                      __func__,
378                      llu(op->tag),
379                      op,
380                      op->attempts);
381         return -ETIMEDOUT;
382 }