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