Whamcloud - gitweb
file lustre_types.h was added on branch b1_4_mountconf on 2006-04-26 18:45:44 +0000
[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 <linux/version.h>
31 # include <linux/module.h>
32 # include <linux/mm.h>
33 # include <linux/highmem.h>
34 # include <linux/lustre_dlm.h>
35 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
36 #  include <linux/workqueue.h>
37 #  include <linux/smp_lock.h>
38 # else
39 #  include <linux/locks.h>
40 # endif
41 # include <linux/ctype.h>
42 # include <linux/init.h>
43 #else /* __KERNEL__ */
44 # include <liblustre.h>
45 # include <ctype.h>
46 #endif
47
48 #include <libcfs/kp30.h>
49 #include <linux/lustre_net.h>
50
51 #include <linux/lustre_ha.h>
52 #include <linux/obd_support.h> /* for OBD_FAIL_CHECK */
53 #include <linux/lprocfs_status.h>
54
55 #define LIOD_STOP 0
56 struct ptlrpcd_ctl {
57         unsigned long             pc_flags;
58         spinlock_t                pc_lock;
59         struct completion         pc_starting;
60         struct completion         pc_finishing;
61         struct list_head          pc_req_list;
62         wait_queue_head_t         pc_waitq;
63         struct ptlrpc_request_set *pc_set;
64         char                      pc_name[16];
65 #ifndef __KERNEL__
66         int                       pc_recurred;
67         void                     *pc_callback;
68 #endif
69 };
70
71 static struct ptlrpcd_ctl ptlrpcd_pc;
72 static struct ptlrpcd_ctl ptlrpcd_recovery_pc;
73
74 static DECLARE_MUTEX(ptlrpcd_sem);
75 static int ptlrpcd_users = 0;
76
77 void ptlrpcd_wake(struct ptlrpc_request *req)
78 {
79         struct ptlrpcd_ctl *pc = req->rq_ptlrpcd_data;
80
81         LASSERT(pc != NULL);
82
83         wake_up(&pc->pc_waitq);
84 }
85
86 /* requests that are added to the ptlrpcd queue are sent via
87  * ptlrpcd_check->ptlrpc_check_set() */
88 void ptlrpcd_add_req(struct ptlrpc_request *req)
89 {
90         struct ptlrpcd_ctl *pc;
91
92         if (req->rq_send_state == LUSTRE_IMP_FULL)
93                 pc = &ptlrpcd_pc;
94         else
95                 pc = &ptlrpcd_recovery_pc;
96
97         ptlrpc_set_add_new_req(pc->pc_set, req);
98         req->rq_ptlrpcd_data = pc;
99
100         ptlrpcd_wake(req);
101 }
102
103 static int ptlrpcd_check(struct ptlrpcd_ctl *pc)
104 {
105         struct list_head *tmp, *pos;
106         struct ptlrpc_request *req;
107         unsigned long flags;
108         int rc = 0;
109         ENTRY;
110
111         if (test_bit(LIOD_STOP, &pc->pc_flags))
112                 RETURN(1);
113
114         spin_lock_irqsave(&pc->pc_set->set_new_req_lock, flags);
115         list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
116                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
117                 list_del_init(&req->rq_set_chain);
118                 ptlrpc_set_add_req(pc->pc_set, req);
119                 rc = 1; /* need to calculate its timeout */
120         }
121         spin_unlock_irqrestore(&pc->pc_set->set_new_req_lock, flags);
122
123         if (pc->pc_set->set_remaining) {
124                 rc = rc | ptlrpc_check_set(pc->pc_set);
125
126                 /* XXX our set never completes, so we prune the completed
127                  * reqs after each iteration. boy could this be smarter. */
128                 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
129                         req = list_entry(pos, struct ptlrpc_request,
130                                          rq_set_chain);
131                         if (req->rq_phase != RQ_PHASE_COMPLETE)
132                                 continue;
133
134                         list_del_init(&req->rq_set_chain);
135                         req->rq_set = NULL;
136                         ptlrpc_req_finished (req);
137                 }
138         }
139
140         if (rc == 0) {
141                 /* If new requests have been added, make sure to wake up */
142                 spin_lock_irqsave(&pc->pc_set->set_new_req_lock, flags);
143                 rc = !list_empty(&pc->pc_set->set_new_requests);
144                 spin_unlock_irqrestore(&pc->pc_set->set_new_req_lock, flags);
145         }
146
147         RETURN(rc);
148 }
149
150 #ifdef __KERNEL__
151 /* ptlrpc's code paths like to execute in process context, so we have this
152  * thread which spins on a set which contains the io rpcs.  llite specifies
153  * ptlrpcd's set when it pushes pages down into the oscs */
154 static int ptlrpcd(void *arg)
155 {
156         struct ptlrpcd_ctl *pc = arg;
157         unsigned long flags;
158         ENTRY;
159
160         libcfs_daemonize(pc->pc_name);
161
162         SIGNAL_MASK_LOCK(current, flags);
163         sigfillset(&current->blocked);
164         RECALC_SIGPENDING;
165         SIGNAL_MASK_UNLOCK(current, flags);
166
167         complete(&pc->pc_starting);
168
169         /* this mainloop strongly resembles ptlrpc_set_wait except
170          * that our set never completes.  ptlrpcd_check calls ptlrpc_check_set
171          * when there are requests in the set.  new requests come in
172          * on the set's new_req_list and ptlrpcd_check moves them into
173          * the set. */
174         while (1) {
175                 wait_queue_t set_wait;
176                 struct l_wait_info lwi;
177                 int timeout;
178
179                 timeout = ptlrpc_set_next_timeout(pc->pc_set) * HZ;
180                 lwi = LWI_TIMEOUT(timeout, ptlrpc_expired_set, pc->pc_set);
181
182                 /* ala the pinger, wait on pc's waitqueue and the set's */
183                 init_waitqueue_entry(&set_wait, current);
184                 add_wait_queue(&pc->pc_set->set_waitq, &set_wait);
185                 l_wait_event(pc->pc_waitq, ptlrpcd_check(pc), &lwi);
186                 remove_wait_queue(&pc->pc_set->set_waitq, &set_wait);
187
188                 if (test_bit(LIOD_STOP, &pc->pc_flags))
189                         break;
190         }
191         /* wait for inflight requests to drain */
192         if (!list_empty(&pc->pc_set->set_requests))
193                 ptlrpc_set_wait(pc->pc_set);
194         complete(&pc->pc_finishing);
195         return 0;
196 }
197 #else
198
199 int ptlrpcd_check_async_rpcs(void *arg)
200 {
201         struct ptlrpcd_ctl *pc = arg;
202         int                  rc = 0;
203
204         /* single threaded!! */
205         pc->pc_recurred++;
206
207         if (pc->pc_recurred == 1) {
208                 rc = ptlrpcd_check(pc);
209                 if (!rc)
210                         ptlrpc_expired_set(pc->pc_set);
211         }
212
213         pc->pc_recurred--;
214         return rc;
215 }
216 #endif
217
218 static int ptlrpcd_start(char *name, struct ptlrpcd_ctl *pc)
219 {
220         int rc;
221
222         memset(pc, 0, sizeof(*pc));
223         init_completion(&pc->pc_starting);
224         init_completion(&pc->pc_finishing);
225         init_waitqueue_head(&pc->pc_waitq);
226         pc->pc_flags = 0;
227         spin_lock_init(&pc->pc_lock);
228         INIT_LIST_HEAD(&pc->pc_req_list);
229         snprintf (pc->pc_name, sizeof (pc->pc_name), name);
230
231         pc->pc_set = ptlrpc_prep_set();
232         if (pc->pc_set == NULL)
233                 RETURN(-ENOMEM);
234
235 #ifdef __KERNEL__
236         rc = kernel_thread(ptlrpcd, pc, 0);
237         if (rc < 0)  {
238                 ptlrpc_set_destroy(pc->pc_set);
239                 RETURN(rc);
240         }
241
242         wait_for_completion(&pc->pc_starting);
243 #else
244         pc->pc_callback =
245                 liblustre_register_wait_callback(&ptlrpcd_check_async_rpcs, pc);
246         (void)rc;
247 #endif
248         RETURN(0);
249 }
250
251 static void ptlrpcd_stop(struct ptlrpcd_ctl *pc)
252 {
253         set_bit(LIOD_STOP, &pc->pc_flags);
254         wake_up(&pc->pc_waitq);
255 #ifdef __KERNEL__
256         wait_for_completion(&pc->pc_finishing);
257 #else
258         liblustre_deregister_wait_callback(pc->pc_callback);
259 #endif
260         ptlrpc_set_destroy(pc->pc_set);
261 }
262
263 int ptlrpcd_addref(void)
264 {
265         int rc = 0;
266         ENTRY;
267
268         down(&ptlrpcd_sem);
269         if (++ptlrpcd_users != 1)
270                 GOTO(out, rc);
271
272         rc = ptlrpcd_start("ptlrpcd", &ptlrpcd_pc);
273         if (rc) {
274                 --ptlrpcd_users;
275                 GOTO(out, rc);
276         }
277
278         rc = ptlrpcd_start("ptlrpcd-recov", &ptlrpcd_recovery_pc);
279         if (rc) {
280                 ptlrpcd_stop(&ptlrpcd_pc);
281                 --ptlrpcd_users;
282                 GOTO(out, rc);
283         }
284 out:
285         up(&ptlrpcd_sem);
286         RETURN(rc);
287 }
288
289 void ptlrpcd_decref(void)
290 {
291         down(&ptlrpcd_sem);
292         if (--ptlrpcd_users == 0) {
293                 ptlrpcd_stop(&ptlrpcd_pc);
294                 ptlrpcd_stop(&ptlrpcd_recovery_pc);
295         }
296         up(&ptlrpcd_sem);
297 }