Whamcloud - gitweb
b=21448 send recovery rpc ASAP
[fs/lustre-release.git] / lustre / ptlrpc / ptlrpcd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/ptlrpcd.c
37  */
38
39 #define DEBUG_SUBSYSTEM S_RPC
40
41 #ifdef __KERNEL__
42 # include <libcfs/libcfs.h>
43 #else /* __KERNEL__ */
44 # include <liblustre.h>
45 # include <ctype.h>
46 #endif
47
48 #include <libcfs/kp30.h>
49 #include <lustre_net.h>
50 # include <lustre_lib.h>
51
52 #include <lustre_ha.h>
53 #include <obd_class.h>   /* for obd_zombie */
54 #include <obd_support.h> /* for OBD_FAIL_CHECK */
55 #include <lprocfs_status.h>
56
57 static struct ptlrpcd_ctl ptlrpcd_pc;
58 static struct ptlrpcd_ctl ptlrpcd_recovery_pc;
59
60 struct semaphore ptlrpcd_sem;
61 static int ptlrpcd_users = 0;
62
63 void ptlrpcd_wake(struct ptlrpc_request *req)
64 {
65         struct ptlrpc_request_set *rq_set = req->rq_set;
66
67         LASSERT(rq_set != NULL);
68
69         cfs_waitq_signal(&rq_set->set_waitq);
70 }
71
72 /*
73  * Move all request from an existing request set to the ptlrpcd queue.
74  * All requests from the set must be in phase RQ_PHASE_NEW.
75  */
76 void ptlrpcd_add_rqset(struct ptlrpc_request_set *set)
77 {
78         struct list_head *tmp, *pos;
79
80         list_for_each_safe(pos, tmp, &set->set_requests) {
81                 struct ptlrpc_request *req =
82                         list_entry(pos, struct ptlrpc_request, rq_set_chain);
83
84                 LASSERT(req->rq_phase == RQ_PHASE_NEW);
85                 list_del_init(&req->rq_set_chain);
86                 req->rq_set = NULL;
87                 ptlrpcd_add_req(req);
88                 set->set_remaining--;
89         }
90         LASSERT(set->set_remaining == 0);
91 }
92 EXPORT_SYMBOL(ptlrpcd_add_rqset);
93
94 /*
95  * Requests that are added to the ptlrpcd queue are sent via
96  * ptlrpcd_check->ptlrpc_check_set().
97  */
98 int ptlrpcd_add_req(struct ptlrpc_request *req)
99 {
100         struct ptlrpcd_ctl *pc;
101         int rc;
102
103         if (req->rq_send_state == LUSTRE_IMP_FULL)
104                 pc = &ptlrpcd_pc;
105         else
106                 pc = &ptlrpcd_recovery_pc;
107         rc = ptlrpc_set_add_new_req(pc, req);
108         if (rc) {
109                 /*
110                  * Thread is probably in stop now so we need to
111                  * kill this rpc as it was not added. Let's call
112                  * interpret for it to let know we're killing it
113                  * so that higher levels might free assosiated
114                  * resources.
115                 */
116
117                 ptlrpc_req_interpret(req, -EBADR);
118                 req->rq_set = NULL;
119                 ptlrpc_req_finished(req);
120         } else if (req->rq_send_state == LUSTRE_IMP_CONNECTING) {
121                /*
122                 * The request is for recovery, should be sent ASAP.
123                 */
124                cfs_waitq_signal(&pc->pc_set->set_waitq);
125         }
126
127         return rc;
128 }
129
130 static int ptlrpcd_check(struct ptlrpcd_ctl *pc)
131 {
132         struct list_head *tmp, *pos;
133         struct ptlrpc_request *req;
134         int rc = 0;
135         ENTRY;
136
137         spin_lock(&pc->pc_set->set_new_req_lock);
138         list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
139                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
140                 list_del_init(&req->rq_set_chain);
141                 ptlrpc_set_add_req(pc->pc_set, req);
142                 /*
143                  * Need to calculate its timeout.
144                  */
145                 rc = 1;
146         }
147         spin_unlock(&pc->pc_set->set_new_req_lock);
148
149         if (pc->pc_set->set_remaining) {
150                 rc = rc | ptlrpc_check_set(pc->pc_set);
151
152                 /*
153                  * XXX: our set never completes, so we prune the completed
154                  * reqs after each iteration. boy could this be smarter.
155                  */
156                 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
157                         req = list_entry(pos, struct ptlrpc_request,
158                                          rq_set_chain);
159                         if (req->rq_phase != RQ_PHASE_COMPLETE)
160                                 continue;
161
162                         list_del_init(&req->rq_set_chain);
163                         req->rq_set = NULL;
164                         ptlrpc_req_finished (req);
165                 }
166         }
167
168         if (rc == 0) {
169                 /*
170                  * If new requests have been added, make sure to wake up.
171                  */
172                 spin_lock(&pc->pc_set->set_new_req_lock);
173                 rc = !list_empty(&pc->pc_set->set_new_requests);
174                 spin_unlock(&pc->pc_set->set_new_req_lock);
175         }
176
177         RETURN(rc);
178 }
179
180 #ifdef __KERNEL__
181 /*
182  * ptlrpc's code paths like to execute in process context, so we have this
183  * thread which spins on a set which contains the io rpcs. llite specifies
184  * ptlrpcd's set when it pushes pages down into the oscs.
185  */
186 static int ptlrpcd(void *arg)
187 {
188         struct ptlrpcd_ctl *pc = arg;
189         int rc, exit = 0;
190         ENTRY;
191
192         if ((rc = cfs_daemonize_ctxt(pc->pc_name))) {
193                 complete(&pc->pc_starting);
194                 goto out;
195         }
196
197         complete(&pc->pc_starting);
198
199         /*
200          * This mainloop strongly resembles ptlrpc_set_wait() except that our
201          * set never completes.  ptlrpcd_check() calls ptlrpc_check_set() when
202          * there are requests in the set. New requests come in on the set's
203          * new_req_list and ptlrpcd_check() moves them into the set.
204          */
205         do {
206                 struct l_wait_info lwi;
207                 int timeout;
208
209                 timeout = ptlrpc_set_next_timeout(pc->pc_set);
210                 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
211                                   ptlrpc_expired_set, pc->pc_set);
212
213                 l_wait_event(pc->pc_set->set_waitq, ptlrpcd_check(pc), &lwi);
214
215                 /*
216                  * Abort inflight rpcs for forced stop case.
217                  */
218                 if (test_bit(LIOD_STOP, &pc->pc_flags)) {
219                         if (test_bit(LIOD_FORCE, &pc->pc_flags))
220                                 ptlrpc_abort_set(pc->pc_set);
221                         exit++;
222                 }
223
224                 /*
225                  * Let's make one more loop to make sure that ptlrpcd_check()
226                  * copied all raced new rpcs into the set so we can kill them.
227                  */
228         } while (exit < 2);
229
230         /*
231          * Wait for inflight requests to drain.
232          */
233         if (!list_empty(&pc->pc_set->set_requests))
234                 ptlrpc_set_wait(pc->pc_set);
235
236         complete(&pc->pc_finishing);
237 out:
238         clear_bit(LIOD_START, &pc->pc_flags);
239         clear_bit(LIOD_STOP, &pc->pc_flags);
240         clear_bit(LIOD_FORCE, &pc->pc_flags);
241         return 0;
242 }
243
244 #else
245
246 int ptlrpcd_check_async_rpcs(void *arg)
247 {
248         struct ptlrpcd_ctl *pc = arg;
249         int                  rc = 0;
250
251         /*
252          * Single threaded!!
253          */
254         pc->pc_recurred++;
255
256         if (pc->pc_recurred == 1) {
257                 rc = ptlrpcd_check(pc);
258                 if (!rc)
259                         ptlrpc_expired_set(pc->pc_set);
260                 /*
261                  * XXX: send replay requests.
262                  */
263                 if (pc == &ptlrpcd_recovery_pc)
264                         rc = ptlrpcd_check(pc);
265         }
266
267         pc->pc_recurred--;
268         return rc;
269 }
270
271 int ptlrpcd_idle(void *arg)
272 {
273         struct ptlrpcd_ctl *pc = arg;
274
275         return (list_empty(&pc->pc_set->set_new_requests) &&
276                 pc->pc_set->set_remaining == 0);
277 }
278
279 #endif
280
281 int ptlrpcd_start(char *name, struct ptlrpcd_ctl *pc)
282 {
283         int rc = 0;
284         ENTRY;
285
286         /*
287          * Do not allow start second thread for one pc.
288          */
289         if (test_bit(LIOD_START, &pc->pc_flags)) {
290                 CERROR("Starting second thread (%s) for same pc %p\n",
291                        name, pc);
292                 RETURN(-EALREADY);
293         }
294
295         set_bit(LIOD_START, &pc->pc_flags);
296         init_completion(&pc->pc_starting);
297         init_completion(&pc->pc_finishing);
298         spin_lock_init(&pc->pc_lock);
299         strncpy(pc->pc_name, name, sizeof(pc->pc_name) - 1);
300
301         pc->pc_set = ptlrpc_prep_set();
302         if (pc->pc_set == NULL)
303                 GOTO(out, rc = -ENOMEM);
304
305 #ifdef __KERNEL__
306         rc = cfs_kernel_thread(ptlrpcd, pc, 0);
307         if (rc < 0)  {
308                 ptlrpc_set_destroy(pc->pc_set);
309                 GOTO(out, rc);
310         }
311         rc = 0;
312         wait_for_completion(&pc->pc_starting);
313 #else
314         pc->pc_wait_callback =
315                 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
316                                                  &ptlrpcd_check_async_rpcs, pc);
317         pc->pc_idle_callback =
318                 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
319                                                  &ptlrpcd_idle, pc);
320 #endif
321 out:
322         if (rc)
323                 clear_bit(LIOD_START, &pc->pc_flags);
324         RETURN(rc);
325 }
326
327 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
328 {
329         if (!test_bit(LIOD_START, &pc->pc_flags)) {
330                 CERROR("Thread for pc %p was not started\n", pc);
331                 return;
332         }
333
334         set_bit(LIOD_STOP, &pc->pc_flags);
335         if (force)
336                 set_bit(LIOD_FORCE, &pc->pc_flags);
337         cfs_waitq_signal(&pc->pc_set->set_waitq);
338 #ifdef __KERNEL__
339         wait_for_completion(&pc->pc_finishing);
340 #else
341         liblustre_deregister_wait_callback(pc->pc_wait_callback);
342         liblustre_deregister_idle_callback(pc->pc_idle_callback);
343 #endif
344         ptlrpc_set_destroy(pc->pc_set);
345 }
346
347 int ptlrpcd_addref(void)
348 {
349         int rc = 0;
350         ENTRY;
351
352         mutex_down(&ptlrpcd_sem);
353         if (++ptlrpcd_users != 1)
354                 GOTO(out, rc);
355
356         rc = ptlrpcd_start("ptlrpcd", &ptlrpcd_pc);
357         if (rc) {
358                 --ptlrpcd_users;
359                 GOTO(out, rc);
360         }
361
362         rc = ptlrpcd_start("ptlrpcd-recov", &ptlrpcd_recovery_pc);
363         if (rc) {
364                 ptlrpcd_stop(&ptlrpcd_pc, 0);
365                 --ptlrpcd_users;
366                 GOTO(out, rc);
367         }
368 out:
369         mutex_up(&ptlrpcd_sem);
370         RETURN(rc);
371 }
372
373 void ptlrpcd_decref(void)
374 {
375         mutex_down(&ptlrpcd_sem);
376         if (--ptlrpcd_users == 0) {
377                 ptlrpcd_stop(&ptlrpcd_pc, 0);
378                 ptlrpcd_stop(&ptlrpcd_recovery_pc, 0);
379         }
380         mutex_up(&ptlrpcd_sem);
381 }