Whamcloud - gitweb
* fixed copy/paste error in previous 11684 commit
[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  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author Peter Braam <braam@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  *
25  */
26
27 #define DEBUG_SUBSYSTEM S_RPC
28
29 #ifdef __KERNEL__
30 # include <libcfs/libcfs.h>
31 #else /* __KERNEL__ */
32 # include <liblustre.h>
33 # include <ctype.h>
34 #endif
35
36 #include <libcfs/kp30.h>
37 #include <lustre_net.h>
38 # include <lustre_lib.h>
39
40 #include <lustre_ha.h>
41 #include <obd_class.h>   /* for obd_zombie */
42 #include <obd_support.h> /* for OBD_FAIL_CHECK */
43 #include <lprocfs_status.h>
44
45 #define LIOD_STOP 0
46 struct ptlrpcd_ctl {
47         unsigned long             pc_flags;
48         spinlock_t                pc_lock;
49         struct completion         pc_starting;
50         struct completion         pc_finishing;
51         struct list_head          pc_req_list;
52         cfs_waitq_t               pc_waitq;
53         struct ptlrpc_request_set *pc_set;
54         char                      pc_name[16];
55 #ifndef __KERNEL__
56         int                       pc_recurred;
57         void                     *pc_wait_callback;
58         void                     *pc_idle_callback;
59 #endif
60 };
61
62 static struct ptlrpcd_ctl ptlrpcd_pc;
63 static struct ptlrpcd_ctl ptlrpcd_recovery_pc;
64
65 struct semaphore ptlrpcd_sem;
66 static int ptlrpcd_users = 0;
67
68 void ptlrpcd_wake(struct ptlrpc_request *req)
69 {
70         struct ptlrpcd_ctl *pc = req->rq_ptlrpcd_data;
71
72         LASSERT(pc != NULL);
73
74         cfs_waitq_signal(&pc->pc_waitq);
75 }
76
77 /* requests that are added to the ptlrpcd queue are sent via
78  * ptlrpcd_check->ptlrpc_check_set() */
79 void ptlrpcd_add_req(struct ptlrpc_request *req)
80 {
81         struct ptlrpcd_ctl *pc;
82
83         if (req->rq_send_state == LUSTRE_IMP_FULL)
84                 pc = &ptlrpcd_pc;
85         else
86                 pc = &ptlrpcd_recovery_pc;
87
88         req->rq_ptlrpcd_data = pc;
89         ptlrpc_set_add_new_req(pc->pc_set, req);
90         wake_up(&pc->pc_waitq);
91 }
92
93 static int ptlrpcd_check(struct ptlrpcd_ctl *pc)
94 {
95         struct list_head *tmp, *pos;
96         struct ptlrpc_request *req;
97         int rc = 0;
98         ENTRY;
99
100         if (test_bit(LIOD_STOP, &pc->pc_flags))
101                 RETURN(1);
102
103         obd_zombie_impexp_cull();
104
105         spin_lock(&pc->pc_set->set_new_req_lock);
106         list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
107                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
108                 list_del_init(&req->rq_set_chain);
109                 ptlrpc_set_add_req(pc->pc_set, req);
110                 rc = 1; /* need to calculate its timeout */
111         }
112         spin_unlock(&pc->pc_set->set_new_req_lock);
113
114         if (pc->pc_set->set_remaining) {
115                 rc = rc | ptlrpc_check_set(pc->pc_set);
116
117                 /* XXX our set never completes, so we prune the completed
118                  * reqs after each iteration. boy could this be smarter. */
119                 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
120                         req = list_entry(pos, struct ptlrpc_request,
121                                          rq_set_chain);
122                         if (req->rq_phase != RQ_PHASE_COMPLETE)
123                                 continue;
124
125                         list_del_init(&req->rq_set_chain);
126                         req->rq_set = NULL;
127                         ptlrpc_req_finished (req);
128                 }
129         }
130
131         if (rc == 0) {
132                 /* If new requests have been added, make sure to wake up */
133                 spin_lock(&pc->pc_set->set_new_req_lock);
134                 rc = !list_empty(&pc->pc_set->set_new_requests);
135                 spin_unlock(&pc->pc_set->set_new_req_lock);
136         }
137
138         RETURN(rc);
139 }
140
141 #ifdef __KERNEL__
142 /* ptlrpc's code paths like to execute in process context, so we have this
143  * thread which spins on a set which contains the io rpcs.  llite specifies
144  * ptlrpcd's set when it pushes pages down into the oscs */
145 static int ptlrpcd(void *arg)
146 {
147         struct ptlrpcd_ctl *pc = arg;
148         int rc;
149         ENTRY;
150
151         if ((rc = cfs_daemonize_ctxt(pc->pc_name))) {
152                 complete(&pc->pc_starting);
153                 return rc;
154         }
155
156         complete(&pc->pc_starting);
157
158         /* this mainloop strongly resembles ptlrpc_set_wait except
159          * that our set never completes.  ptlrpcd_check calls ptlrpc_check_set
160          * when there are requests in the set.  new requests come in
161          * on the set's new_req_list and ptlrpcd_check moves them into
162          * the set. */
163         while (1) {
164                 cfs_waitlink_t set_wait;
165                 struct l_wait_info lwi;
166                 cfs_duration_t timeout;
167
168                 timeout = cfs_time_seconds(ptlrpc_set_next_timeout(pc->pc_set));
169                 lwi = LWI_TIMEOUT(timeout, ptlrpc_expired_set, pc->pc_set);
170
171                 /* ala the pinger, wait on pc's waitqueue and the set's */
172                 cfs_waitlink_init(&set_wait);
173                 cfs_waitq_add(&pc->pc_set->set_waitq, &set_wait);
174                 cfs_waitq_forward(&set_wait, &pc->pc_waitq);
175                 l_wait_event(pc->pc_waitq, ptlrpcd_check(pc), &lwi);
176                 cfs_waitq_del(&pc->pc_set->set_waitq, &set_wait);
177
178                 if (test_bit(LIOD_STOP, &pc->pc_flags))
179                         break;
180         }
181         /* wait for inflight requests to drain */
182         if (!list_empty(&pc->pc_set->set_requests))
183                 ptlrpc_set_wait(pc->pc_set);
184         complete(&pc->pc_finishing);
185         return 0;
186 }
187
188 static void ptlrpcd_zombie_impexp_notify(void)
189 {
190         cfs_waitq_signal(&ptlrpcd_pc.pc_waitq);
191 }
192 #else
193
194 int ptlrpcd_check_async_rpcs(void *arg)
195 {
196         struct ptlrpcd_ctl *pc = arg;
197         int                  rc = 0;
198
199         /* single threaded!! */
200         pc->pc_recurred++;
201
202         if (pc->pc_recurred == 1) {
203                 rc = ptlrpcd_check(pc);
204                 if (!rc)
205                         ptlrpc_expired_set(pc->pc_set);
206                 /*XXX send replay requests */
207                 if (pc == &ptlrpcd_recovery_pc)
208                         rc = ptlrpcd_check(pc);
209         }
210
211         pc->pc_recurred--;
212         return rc;
213 }
214
215 int ptlrpcd_idle(void *arg)
216 {
217         struct ptlrpcd_ctl *pc = arg;
218
219         return (list_empty(&pc->pc_set->set_new_requests) &&
220                 pc->pc_set->set_remaining == 0);
221 }
222
223 #endif
224
225 static int ptlrpcd_start(char *name, struct ptlrpcd_ctl *pc)
226 {
227         int rc;
228
229         ENTRY;
230         memset(pc, 0, sizeof(*pc));
231         init_completion(&pc->pc_starting);
232         init_completion(&pc->pc_finishing);
233         cfs_waitq_init(&pc->pc_waitq);
234         pc->pc_flags = 0;
235         spin_lock_init(&pc->pc_lock);
236         CFS_INIT_LIST_HEAD(&pc->pc_req_list);
237         snprintf (pc->pc_name, sizeof (pc->pc_name), name);
238
239         pc->pc_set = ptlrpc_prep_set();
240         if (pc->pc_set == NULL)
241                 RETURN(-ENOMEM);
242
243 #ifdef __KERNEL__
244         /* wake ptlrpcd when zombie imports or exports exist */
245         obd_zombie_impexp_notify = ptlrpcd_zombie_impexp_notify;
246         
247         rc = cfs_kernel_thread(ptlrpcd, pc, 0);
248         if (rc < 0)  {
249                 ptlrpc_set_destroy(pc->pc_set);
250                 RETURN(rc);
251         }
252
253         wait_for_completion(&pc->pc_starting);
254 #else
255         pc->pc_wait_callback =
256                 liblustre_register_wait_callback("ptlrpcd_check_async_rpcs",
257                                                  &ptlrpcd_check_async_rpcs, pc);
258         pc->pc_idle_callback =
259                 liblustre_register_idle_callback("ptlrpcd_check_idle_rpcs",
260                                                  &ptlrpcd_idle, pc);
261         (void)rc;
262 #endif
263         RETURN(0);
264 }
265
266 static void ptlrpcd_stop(struct ptlrpcd_ctl *pc)
267 {
268         set_bit(LIOD_STOP, &pc->pc_flags);
269         cfs_waitq_signal(&pc->pc_waitq);
270 #ifdef __KERNEL__
271         obd_zombie_impexp_notify = NULL;
272         wait_for_completion(&pc->pc_finishing);
273 #else
274         liblustre_deregister_wait_callback(pc->pc_wait_callback);
275         liblustre_deregister_idle_callback(pc->pc_idle_callback);
276 #endif
277         ptlrpc_set_destroy(pc->pc_set);
278 }
279
280 int ptlrpcd_addref(void)
281 {
282         int rc = 0;
283         ENTRY;
284
285         mutex_down(&ptlrpcd_sem);
286         if (++ptlrpcd_users != 1)
287                 GOTO(out, rc);
288
289         rc = ptlrpcd_start("ptlrpcd", &ptlrpcd_pc);
290         if (rc) {
291                 --ptlrpcd_users;
292                 GOTO(out, rc);
293         }
294
295         rc = ptlrpcd_start("ptlrpcd-recov", &ptlrpcd_recovery_pc);
296         if (rc) {
297                 ptlrpcd_stop(&ptlrpcd_pc);
298                 --ptlrpcd_users;
299                 GOTO(out, rc);
300         }
301 out:
302         mutex_up(&ptlrpcd_sem);
303         RETURN(rc);
304 }
305
306 void ptlrpcd_decref(void)
307 {
308         mutex_down(&ptlrpcd_sem);
309         if (--ptlrpcd_users == 0) {
310                 ptlrpcd_stop(&ptlrpcd_pc);
311                 ptlrpcd_stop(&ptlrpcd_recovery_pc);
312         }
313         mutex_up(&ptlrpcd_sem);
314 }