Whamcloud - gitweb
b=21411 Improvement for AT.
[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         }
121         return rc;
122 }
123
124 static int ptlrpcd_check(struct ptlrpcd_ctl *pc)
125 {
126         struct list_head *tmp, *pos;
127         struct ptlrpc_request *req;
128         int rc = 0;
129         ENTRY;
130
131         spin_lock(&pc->pc_set->set_new_req_lock);
132         list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
133                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
134                 list_del_init(&req->rq_set_chain);
135                 ptlrpc_set_add_req(pc->pc_set, req);
136                 /*
137                  * Need to calculate its timeout.
138                  */
139                 rc = 1;
140         }
141         spin_unlock(&pc->pc_set->set_new_req_lock);
142
143         if (pc->pc_set->set_remaining) {
144                 rc = rc | ptlrpc_check_set(pc->pc_set);
145
146                 /*
147                  * XXX: our set never completes, so we prune the completed
148                  * reqs after each iteration. boy could this be smarter.
149                  */
150                 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
151                         req = list_entry(pos, struct ptlrpc_request,
152                                          rq_set_chain);
153                         if (req->rq_phase != RQ_PHASE_COMPLETE)
154                                 continue;
155
156                         list_del_init(&req->rq_set_chain);
157                         req->rq_set = NULL;
158                         ptlrpc_req_finished (req);
159                 }
160         }
161
162         if (rc == 0) {
163                 /*
164                  * If new requests have been added, make sure to wake up.
165                  */
166                 spin_lock(&pc->pc_set->set_new_req_lock);
167                 rc = !list_empty(&pc->pc_set->set_new_requests);
168                 spin_unlock(&pc->pc_set->set_new_req_lock);
169         }
170
171         RETURN(rc);
172 }
173
174 #ifdef __KERNEL__
175 /*
176  * ptlrpc's code paths like to execute in process context, so we have this
177  * thread which spins on a set which contains the io rpcs. llite specifies
178  * ptlrpcd's set when it pushes pages down into the oscs.
179  */
180 static int ptlrpcd(void *arg)
181 {
182         struct ptlrpcd_ctl *pc = arg;
183         int rc, exit = 0;
184         ENTRY;
185
186         if ((rc = cfs_daemonize_ctxt(pc->pc_name))) {
187                 complete(&pc->pc_starting);
188                 goto out;
189         }
190
191         complete(&pc->pc_starting);
192
193         /*
194          * This mainloop strongly resembles ptlrpc_set_wait() except that our
195          * set never completes.  ptlrpcd_check() calls ptlrpc_check_set() when
196          * there are requests in the set. New requests come in on the set's
197          * new_req_list and ptlrpcd_check() moves them into the set.
198          */
199         do {
200                 struct l_wait_info lwi;
201                 int timeout;
202
203                 timeout = ptlrpc_set_next_timeout(pc->pc_set);
204                 lwi = LWI_TIMEOUT(cfs_time_seconds(timeout ? timeout : 1),
205                                   ptlrpc_expired_set, pc->pc_set);
206
207                 l_wait_event(pc->pc_set->set_waitq, ptlrpcd_check(pc), &lwi);
208
209                 /*
210                  * Abort inflight rpcs for forced stop case.
211                  */
212                 if (test_bit(LIOD_STOP, &pc->pc_flags)) {
213                         if (test_bit(LIOD_FORCE, &pc->pc_flags))
214                                 ptlrpc_abort_set(pc->pc_set);
215                         exit++;
216                 }
217
218                 /*
219                  * Let's make one more loop to make sure that ptlrpcd_check()
220                  * copied all raced new rpcs into the set so we can kill them.
221                  */
222         } while (exit < 2);
223
224         /*
225          * Wait for inflight requests to drain.
226          */
227         if (!list_empty(&pc->pc_set->set_requests))
228                 ptlrpc_set_wait(pc->pc_set);
229
230         complete(&pc->pc_finishing);
231 out:
232         clear_bit(LIOD_START, &pc->pc_flags);
233         clear_bit(LIOD_STOP, &pc->pc_flags);
234         clear_bit(LIOD_FORCE, &pc->pc_flags);
235         return 0;
236 }
237
238 #else
239
240 int ptlrpcd_check_async_rpcs(void *arg)
241 {
242         struct ptlrpcd_ctl *pc = arg;
243         int                  rc = 0;
244
245         /*
246          * Single threaded!!
247          */
248         pc->pc_recurred++;
249
250         if (pc->pc_recurred == 1) {
251                 rc = ptlrpcd_check(pc);
252                 if (!rc)
253                         ptlrpc_expired_set(pc->pc_set);
254                 /*
255                  * XXX: send replay requests.
256                  */
257                 if (pc == &ptlrpcd_recovery_pc)
258                         rc = ptlrpcd_check(pc);
259         }
260
261         pc->pc_recurred--;
262         return rc;
263 }
264
265 int ptlrpcd_idle(void *arg)
266 {
267         struct ptlrpcd_ctl *pc = arg;
268
269         return (list_empty(&pc->pc_set->set_new_requests) &&
270                 pc->pc_set->set_remaining == 0);
271 }
272
273 #endif
274
275 int ptlrpcd_start(char *name, struct ptlrpcd_ctl *pc)
276 {
277         int rc = 0;
278         ENTRY;
279
280         /*
281          * Do not allow start second thread for one pc.
282          */
283         if (test_bit(LIOD_START, &pc->pc_flags)) {
284                 CERROR("Starting second thread (%s) for same pc %p\n",
285                        name, pc);
286                 RETURN(-EALREADY);
287         }
288
289         set_bit(LIOD_START, &pc->pc_flags);
290         init_completion(&pc->pc_starting);
291         init_completion(&pc->pc_finishing);
292         spin_lock_init(&pc->pc_lock);
293         strncpy(pc->pc_name, name, sizeof(pc->pc_name) - 1);
294
295         pc->pc_set = ptlrpc_prep_set();
296         if (pc->pc_set == NULL)
297                 GOTO(out, rc = -ENOMEM);
298
299 #ifdef __KERNEL__
300         rc = cfs_kernel_thread(ptlrpcd, pc, 0);
301         if (rc < 0)  {
302                 ptlrpc_set_destroy(pc->pc_set);
303                 GOTO(out, rc);
304         }
305         rc = 0;
306         wait_for_completion(&pc->pc_starting);
307 #else
308         pc->pc_wait_callback =
309                 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
310                                                  &ptlrpcd_check_async_rpcs, pc);
311         pc->pc_idle_callback =
312                 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
313                                                  &ptlrpcd_idle, pc);
314 #endif
315 out:
316         if (rc)
317                 clear_bit(LIOD_START, &pc->pc_flags);
318         RETURN(rc);
319 }
320
321 void ptlrpcd_stop(struct ptlrpcd_ctl *pc, int force)
322 {
323         if (!test_bit(LIOD_START, &pc->pc_flags)) {
324                 CERROR("Thread for pc %p was not started\n", pc);
325                 return;
326         }
327
328         set_bit(LIOD_STOP, &pc->pc_flags);
329         if (force)
330                 set_bit(LIOD_FORCE, &pc->pc_flags);
331         cfs_waitq_signal(&pc->pc_set->set_waitq);
332 #ifdef __KERNEL__
333         wait_for_completion(&pc->pc_finishing);
334 #else
335         liblustre_deregister_wait_callback(pc->pc_wait_callback);
336         liblustre_deregister_idle_callback(pc->pc_idle_callback);
337 #endif
338         ptlrpc_set_destroy(pc->pc_set);
339 }
340
341 int ptlrpcd_addref(void)
342 {
343         int rc = 0;
344         ENTRY;
345
346         mutex_down(&ptlrpcd_sem);
347         if (++ptlrpcd_users != 1)
348                 GOTO(out, rc);
349
350         rc = ptlrpcd_start("ptlrpcd", &ptlrpcd_pc);
351         if (rc) {
352                 --ptlrpcd_users;
353                 GOTO(out, rc);
354         }
355
356         rc = ptlrpcd_start("ptlrpcd-recov", &ptlrpcd_recovery_pc);
357         if (rc) {
358                 ptlrpcd_stop(&ptlrpcd_pc, 0);
359                 --ptlrpcd_users;
360                 GOTO(out, rc);
361         }
362 out:
363         mutex_up(&ptlrpcd_sem);
364         RETURN(rc);
365 }
366
367 void ptlrpcd_decref(void)
368 {
369         mutex_down(&ptlrpcd_sem);
370         if (--ptlrpcd_users == 0) {
371                 ptlrpcd_stop(&ptlrpcd_pc, 0);
372                 ptlrpcd_stop(&ptlrpcd_recovery_pc, 0);
373         }
374         mutex_up(&ptlrpcd_sem);
375 }