Whamcloud - gitweb
b=16098
[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 <lustre_net.h>
49 # include <lustre_lib.h>
50
51 #include <lustre_ha.h>
52 #include <obd_class.h>   /* for obd_zombie */
53 #include <obd_support.h> /* for OBD_FAIL_CHECK */
54 #include <lprocfs_status.h>
55
56 static struct ptlrpcd_ctl ptlrpcd_pc;
57 static struct ptlrpcd_ctl ptlrpcd_recovery_pc;
58
59 struct semaphore ptlrpcd_sem;
60 static int ptlrpcd_users = 0;
61
62 void ptlrpcd_wake(struct ptlrpc_request *req)
63 {
64         struct ptlrpc_request_set *rq_set = req->rq_set;
65
66         LASSERT(rq_set != NULL);
67
68         cfs_waitq_signal(&rq_set->set_waitq);
69 }
70
71 /* 
72  * Requests that are added to the ptlrpcd queue are sent via
73  * ptlrpcd_check->ptlrpc_check_set().
74  */
75 void ptlrpcd_add_req(struct ptlrpc_request *req)
76 {
77         struct ptlrpcd_ctl *pc;
78         int rc;
79
80         if (req->rq_send_state == LUSTRE_IMP_FULL)
81                 pc = &ptlrpcd_pc;
82         else
83                 pc = &ptlrpcd_recovery_pc;
84
85         rc = ptlrpc_set_add_new_req(pc, req);
86         if (rc) {
87                 int (*interpreter)(struct ptlrpc_request *,
88                                    void *, int);
89                                    
90                 interpreter = req->rq_interpret_reply;
91
92                 /*
93                  * Thread is probably in stop now so we need to
94                  * kill this rpc as it was not added. Let's call
95                  * interpret for it to let know we're killing it
96                  * so that higher levels might free assosiated
97                  * resources.
98                  */
99                 req->rq_status = -EBADR;
100                 interpreter(req, &req->rq_async_args,
101                             req->rq_status);
102                 req->rq_set = NULL;
103                 ptlrpc_req_finished(req);
104         }
105 }
106
107 static int ptlrpcd_check(struct ptlrpcd_ctl *pc)
108 {
109         struct list_head *tmp, *pos;
110         struct ptlrpc_request *req;
111         int rc = 0;
112         ENTRY;
113
114         if (test_bit(LIOD_STOP, &pc->pc_flags))
115                 RETURN(1);
116
117         spin_lock(&pc->pc_set->set_new_req_lock);
118         list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
119                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
120                 list_del_init(&req->rq_set_chain);
121                 ptlrpc_set_add_req(pc->pc_set, req);
122                 /* 
123                  * Need to calculate its timeout. 
124                  */
125                 rc = 1;
126         }
127         spin_unlock(&pc->pc_set->set_new_req_lock);
128
129         if (pc->pc_set->set_remaining) {
130                 rc = rc | ptlrpc_check_set(pc->pc_set);
131
132                 /* 
133                  * XXX: our set never completes, so we prune the completed
134                  * reqs after each iteration. boy could this be smarter. 
135                  */
136                 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
137                         req = list_entry(pos, struct ptlrpc_request,
138                                          rq_set_chain);
139                         if (req->rq_phase != RQ_PHASE_COMPLETE)
140                                 continue;
141
142                         list_del_init(&req->rq_set_chain);
143                         req->rq_set = NULL;
144                         ptlrpc_req_finished (req);
145                 }
146         }
147
148         if (rc == 0) {
149                 /* 
150                  * If new requests have been added, make sure to wake up. 
151                  */
152                 spin_lock(&pc->pc_set->set_new_req_lock);
153                 rc = !list_empty(&pc->pc_set->set_new_requests);
154                 spin_unlock(&pc->pc_set->set_new_req_lock);
155         }
156
157         RETURN(rc);
158 }
159
160 #ifdef __KERNEL__
161 /* 
162  * ptlrpc's code paths like to execute in process context, so we have this
163  * thread which spins on a set which contains the io rpcs. llite specifies
164  * ptlrpcd's set when it pushes pages down into the oscs.
165  */
166 static int ptlrpcd(void *arg)
167 {
168         struct ptlrpcd_ctl *pc = arg;
169         int rc;
170         ENTRY;
171
172         if ((rc = cfs_daemonize_ctxt(pc->pc_name))) {
173                 complete(&pc->pc_starting);
174                 goto out;
175         }
176
177         complete(&pc->pc_starting);
178
179         /* 
180          * This mainloop strongly resembles ptlrpc_set_wait() except that our
181          * set never completes.  ptlrpcd_check() calls ptlrpc_check_set() when
182          * there are requests in the set. New requests come in on the set's 
183          * new_req_list and ptlrpcd_check() moves them into the set. 
184          */
185         while (1) {
186                 struct l_wait_info lwi;
187                 cfs_duration_t timeout;
188
189                 timeout = cfs_time_seconds(ptlrpc_set_next_timeout(pc->pc_set));
190                 lwi = LWI_TIMEOUT(timeout, ptlrpc_expired_set, pc->pc_set);
191
192                 l_wait_event(pc->pc_set->set_waitq, ptlrpcd_check(pc), &lwi);
193
194                 /*
195                  * Abort inflight rpcs for forced stop case.
196                  */
197                 if (test_bit(LIOD_STOP_FORCE, &pc->pc_flags))
198                         ptlrpc_abort_set(pc->pc_set);
199
200                 if (test_bit(LIOD_STOP, &pc->pc_flags))
201                         break;
202         }
203
204         /* 
205          * Wait for inflight requests to drain. 
206          */
207         if (!list_empty(&pc->pc_set->set_requests))
208                 ptlrpc_set_wait(pc->pc_set);
209
210         complete(&pc->pc_finishing);
211 out:
212         clear_bit(LIOD_START, &pc->pc_flags);
213         clear_bit(LIOD_STOP, &pc->pc_flags);
214         return 0;
215 }
216
217 #else
218
219 int ptlrpcd_check_async_rpcs(void *arg)
220 {
221         struct ptlrpcd_ctl *pc = arg;
222         int                  rc = 0;
223
224         /* 
225          * Single threaded!! 
226          */
227         pc->pc_recurred++;
228
229         if (pc->pc_recurred == 1) {
230                 rc = ptlrpcd_check(pc);
231                 if (!rc)
232                         ptlrpc_expired_set(pc->pc_set);
233                 /* 
234                  * XXX: send replay requests. 
235                  */
236                 if (pc == &ptlrpcd_recovery_pc)
237                         rc = ptlrpcd_check(pc);
238         }
239
240         pc->pc_recurred--;
241         return rc;
242 }
243
244 int ptlrpcd_idle(void *arg)
245 {
246         struct ptlrpcd_ctl *pc = arg;
247
248         return (list_empty(&pc->pc_set->set_new_requests) &&
249                 pc->pc_set->set_remaining == 0);
250 }
251
252 #endif
253
254 int ptlrpcd_start(char *name, struct ptlrpcd_ctl *pc)
255 {
256         int rc = 0;
257         ENTRY;
258  
259         /* 
260          * Do not allow start second thread for one pc. 
261          */
262         if (test_and_set_bit(LIOD_START, &pc->pc_flags)) {
263                 CERROR("Starting second thread (%s) for same pc %p\n",
264                        name, pc);
265                 RETURN(-EALREADY);
266         }
267
268         init_completion(&pc->pc_starting);
269         init_completion(&pc->pc_finishing);
270         spin_lock_init(&pc->pc_lock);
271         snprintf (pc->pc_name, sizeof (pc->pc_name), name);
272
273         pc->pc_set = ptlrpc_prep_set();
274         if (pc->pc_set == NULL)
275                 GOTO(out, rc = -ENOMEM);
276
277 #ifdef __KERNEL__
278         rc = cfs_kernel_thread(ptlrpcd, pc, 0);
279         if (rc < 0)  {
280                 ptlrpc_set_destroy(pc->pc_set);
281                 GOTO(out, rc);
282         }
283         rc = 0;
284         wait_for_completion(&pc->pc_starting);
285 #else
286         pc->pc_wait_callback =
287                 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
288                                                  &ptlrpcd_check_async_rpcs, pc);
289         pc->pc_idle_callback =
290                 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
291                                                  &ptlrpcd_idle, pc);
292 #endif
293 out:
294         if (rc)
295                 clear_bit(LIOD_START, &pc->pc_flags);
296         RETURN(rc);
297 }
298
299 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
300 {
301         if (!test_bit(LIOD_START, &pc->pc_flags)) {
302                 CERROR("Thread for pc %p was not started\n", pc);
303                 return;
304         }
305
306         set_bit(LIOD_STOP, &pc->pc_flags);
307         if (force)
308                 set_bit(LIOD_STOP_FORCE, &pc->pc_flags);
309         cfs_waitq_signal(&pc->pc_set->set_waitq);
310 #ifdef __KERNEL__
311         wait_for_completion(&pc->pc_finishing);
312 #else
313         liblustre_deregister_wait_callback(pc->pc_wait_callback);
314         liblustre_deregister_idle_callback(pc->pc_idle_callback);
315 #endif
316         ptlrpc_set_destroy(pc->pc_set);
317 }
318
319 int ptlrpcd_addref(void)
320 {
321         int rc = 0;
322         ENTRY;
323
324         mutex_down(&ptlrpcd_sem);
325         if (++ptlrpcd_users != 1)
326                 GOTO(out, rc);
327
328         rc = ptlrpcd_start("ptlrpcd", &ptlrpcd_pc);
329         if (rc) {
330                 --ptlrpcd_users;
331                 GOTO(out, rc);
332         }
333
334         rc = ptlrpcd_start("ptlrpcd-recov", &ptlrpcd_recovery_pc);
335         if (rc) {
336                 ptlrpcd_stop(&ptlrpcd_pc, 0);
337                 --ptlrpcd_users;
338                 GOTO(out, rc);
339         }
340 out:
341         mutex_up(&ptlrpcd_sem);
342         RETURN(rc);
343 }
344
345 void ptlrpcd_decref(void)
346 {
347         mutex_down(&ptlrpcd_sem);
348         if (--ptlrpcd_users == 0) {
349                 ptlrpcd_stop(&ptlrpcd_pc, 0);
350                 ptlrpcd_stop(&ptlrpcd_recovery_pc, 0);
351         }
352         mutex_up(&ptlrpcd_sem);
353 }