Whamcloud - gitweb
b=3887
[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 Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  */
23
24 #define DEBUG_SUBSYSTEM S_RPC
25
26 #ifdef __KERNEL__
27 # include <linux/version.h>
28 # include <linux/module.h>
29 # include <linux/mm.h>
30 # include <linux/highmem.h>
31 # include <linux/lustre_dlm.h>
32 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
33 #  include <linux/workqueue.h>
34 #  include <linux/smp_lock.h>
35 # else
36 #  include <linux/locks.h>
37 # endif
38 #else /* __KERNEL__ */
39 # include <liblustre.h>
40 #endif
41
42 #include <linux/kp30.h>
43 #include <linux/lustre_net.h>
44
45 #ifndef  __CYGWIN__
46 # include <linux/init.h>
47 #else
48 # include <ctype.h>
49 #endif
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 void ptlrpcd_add_req(struct ptlrpc_request *req)
87 {
88         struct ptlrpcd_ctl *pc;
89
90         if (req->rq_send_state == LUSTRE_IMP_FULL)
91                 pc = &ptlrpcd_pc;
92         else 
93                 pc = &ptlrpcd_recovery_pc;
94
95         ptlrpc_set_add_new_req(pc->pc_set, req);
96         req->rq_ptlrpcd_data = pc;
97                 
98         ptlrpcd_wake(req);
99 }
100
101 static int ptlrpcd_check(struct ptlrpcd_ctl *pc)
102 {
103         struct list_head *tmp, *pos;
104         struct ptlrpc_request *req;
105         unsigned long flags;
106         int rc = 0;
107         ENTRY;
108
109         if (test_bit(LIOD_STOP, &pc->pc_flags))
110                 RETURN(1);
111
112         spin_lock_irqsave(&pc->pc_set->set_new_req_lock, flags);
113         list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
114                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
115                 list_del_init(&req->rq_set_chain);
116                 ptlrpc_set_add_req(pc->pc_set, req);
117                 rc = 1; /* need to calculate its timeout */
118         }
119         spin_unlock_irqrestore(&pc->pc_set->set_new_req_lock, flags);
120
121         if (pc->pc_set->set_remaining) {
122                 rc = rc | ptlrpc_check_set(pc->pc_set);
123
124                 /* XXX our set never completes, so we prune the completed
125                  * reqs after each iteration. boy could this be smarter. */
126                 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
127                         req = list_entry(pos, struct ptlrpc_request,
128                                          rq_set_chain);
129                         if (req->rq_phase != RQ_PHASE_COMPLETE)
130                                 continue;
131
132                         list_del_init(&req->rq_set_chain);
133                         req->rq_set = NULL;
134                         ptlrpc_req_finished (req);
135                 }
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         unsigned long flags;
149         ENTRY;
150
151         kportal_daemonize(pc->pc_name);
152
153         SIGNAL_MASK_LOCK(current, flags);
154         sigfillset(&current->blocked);
155         RECALC_SIGPENDING;
156         SIGNAL_MASK_UNLOCK(current, flags);
157
158         complete(&pc->pc_starting);
159
160         /* this mainloop strongly resembles ptlrpc_set_wait except
161          * that our set never completes.  ptlrpcd_check calls ptlrpc_check_set
162          * when there are requests in the set.  new requests come in
163          * on the set's new_req_list and ptlrpcd_check moves them into
164          * the set. */
165         while (1) {
166                 wait_queue_t set_wait;
167                 struct l_wait_info lwi;
168                 int timeout;
169
170                 timeout = ptlrpc_set_next_timeout(pc->pc_set) * HZ;
171                 lwi = LWI_TIMEOUT(timeout, ptlrpc_expired_set, pc->pc_set);
172
173                 /* ala the pinger, wait on pc's waitqueue and the set's */
174                 init_waitqueue_entry(&set_wait, current);
175                 add_wait_queue(&pc->pc_set->set_waitq, &set_wait);
176                 l_wait_event(pc->pc_waitq, ptlrpcd_check(pc), &lwi);
177                 remove_wait_queue(&pc->pc_set->set_waitq, &set_wait);
178
179                 if (test_bit(LIOD_STOP, &pc->pc_flags))
180                         break;
181         }
182         /* wait for inflight requests to drain */
183         if (!list_empty(&pc->pc_set->set_requests))
184                 ptlrpc_set_wait(pc->pc_set);
185         complete(&pc->pc_finishing);
186         return 0;
187 }
188 #else
189
190 int ptlrpcd_check_async_rpcs(void *arg)
191 {
192         struct ptlrpcd_ctl *pc = arg;
193         int                  rc = 0;
194
195         /* single threaded!! */
196         pc->pc_recurred++;
197
198         if (pc->pc_recurred == 1)
199                 rc = ptlrpcd_check(pc);
200
201         pc->pc_recurred--;
202         return rc;
203 }
204 #endif
205
206 static int ptlrpcd_start(char *name, struct ptlrpcd_ctl *pc)
207 {
208         int rc = 0;
209
210         memset(pc, 0, sizeof(*pc));
211         init_completion(&pc->pc_starting);
212         init_completion(&pc->pc_finishing);
213         init_waitqueue_head(&pc->pc_waitq);
214         pc->pc_flags = 0;
215         spin_lock_init(&pc->pc_lock);
216         INIT_LIST_HEAD(&pc->pc_req_list);
217         snprintf (pc->pc_name, sizeof (pc->pc_name), name);
218
219         pc->pc_set = ptlrpc_prep_set();
220         if (pc->pc_set == NULL)
221                 GOTO(out, rc = -ENOMEM);
222
223 #ifdef __KERNEL__
224         if (kernel_thread(ptlrpcd, pc, 0) < 0)  {
225                 ptlrpc_set_destroy(pc->pc_set);
226                 GOTO(out, rc = -ECHILD);
227         }
228
229         wait_for_completion(&pc->pc_starting);
230 #else
231         pc->pc_callback =
232                 liblustre_register_wait_callback(&ptlrpcd_check_async_rpcs, pc);
233 #endif
234 out:
235         RETURN(rc);
236 }
237
238 static void ptlrpcd_stop(struct ptlrpcd_ctl *pc)
239 {
240         set_bit(LIOD_STOP, &pc->pc_flags);
241         wake_up(&pc->pc_waitq);
242 #ifdef __KERNEL__
243         wait_for_completion(&pc->pc_finishing);
244 #else
245         liblustre_deregister_wait_callback(pc->pc_callback);
246 #endif
247         ptlrpc_set_destroy(pc->pc_set);
248 }
249
250 int ptlrpcd_addref(void)
251 {
252         int rc = 0;
253         ENTRY;
254
255         down(&ptlrpcd_sem);
256         if (++ptlrpcd_users != 1)
257                 GOTO(out, rc);
258
259         rc = ptlrpcd_start("ptlrpcd", &ptlrpcd_pc);
260         if (rc) {
261                 --ptlrpcd_users;
262                 GOTO(out, rc);
263         }
264
265         rc = ptlrpcd_start("ptlrpcd-recov", &ptlrpcd_recovery_pc);
266         if (rc) {
267                 ptlrpcd_stop(&ptlrpcd_pc);
268                 --ptlrpcd_users;
269                 GOTO(out, rc);
270         }
271 out:
272         up(&ptlrpcd_sem);
273         RETURN(rc);
274 }
275
276 void ptlrpcd_decref(void)
277 {
278         down(&ptlrpcd_sem);
279         if (--ptlrpcd_users == 0) {
280                 ptlrpcd_stop(&ptlrpcd_pc);
281                 ptlrpcd_stop(&ptlrpcd_recovery_pc);
282         }
283         up(&ptlrpcd_sem);
284 }