Whamcloud - gitweb
add conf-sanity.sh to acceptance-small.sh
[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/ctype.h>
47 # include <linux/init.h>
48 #else
49 # include <ctype.h>
50 #endif
51
52 #include <linux/lustre_ha.h>
53 #include <linux/obd_support.h> /* for OBD_FAIL_CHECK */
54 #include <linux/lprocfs_status.h>
55
56 #define LIOD_STOP 0
57 static struct ptlrpcd_ctl {
58         unsigned long             pc_flags;
59         spinlock_t                pc_lock;
60         struct completion         pc_starting;
61         struct completion         pc_finishing;
62         struct list_head          pc_req_list;
63         wait_queue_head_t         pc_waitq;
64         struct ptlrpc_request_set *pc_set;
65 } ptlrpcd_pc;
66
67 static DECLARE_MUTEX(ptlrpcd_sem);
68 static int ptlrpcd_users = 0;
69
70 void ptlrpcd_add_req(struct ptlrpc_request *req)
71 {
72         struct ptlrpcd_ctl *pc = &ptlrpcd_pc;
73
74         ptlrpc_set_add_new_req(pc->pc_set, req);
75         wake_up(&pc->pc_waitq);
76 }
77
78 static int ptlrpcd_check(struct ptlrpcd_ctl *pc)
79 {
80         struct list_head *tmp, *pos;
81         struct ptlrpc_request *req;
82         unsigned long flags;
83         int rc = 0;
84         ENTRY;
85
86         if (test_bit(LIOD_STOP, &pc->pc_flags))
87                 RETURN(1);
88
89         spin_lock_irqsave(&pc->pc_set->set_new_req_lock, flags);
90         list_for_each_safe(pos, tmp, &pc->pc_set->set_new_requests) {
91                 req = list_entry(pos, struct ptlrpc_request, rq_set_chain);
92                 list_del_init(&req->rq_set_chain);
93                 ptlrpc_set_add_req(pc->pc_set, req);
94                 rc = 1; /* need to calculate its timeout */
95         }
96         spin_unlock_irqrestore(&pc->pc_set->set_new_req_lock, flags);
97
98         if (pc->pc_set->set_remaining) {
99                 rc = rc | ptlrpc_check_set(pc->pc_set);
100
101                 /* XXX our set never completes, so we prune the completed
102                  * reqs after each iteration. boy could this be smarter. */
103                 list_for_each_safe(pos, tmp, &pc->pc_set->set_requests) {
104                         req = list_entry(pos, struct ptlrpc_request,
105                                          rq_set_chain);
106                         if (req->rq_phase != RQ_PHASE_COMPLETE)
107                                 continue;
108
109                         list_del_init(&req->rq_set_chain);
110                         req->rq_set = NULL;
111                         ptlrpc_req_finished (req);
112                 }
113         }
114
115         RETURN(rc);
116 }
117
118 #ifdef __KERNEL__
119 /* ptlrpc's code paths like to execute in process context, so we have this
120  * thread which spins on a set which contains the io rpcs.  llite specifies
121  * ptlrpcd's set when it pushes pages down into the oscs */
122 static int ptlrpcd(void *arg)
123 {
124         struct ptlrpcd_ctl *pc = arg;
125         unsigned long flags;
126         ENTRY;
127
128         kportal_daemonize("ptlrpcd");
129
130         SIGNAL_MASK_LOCK(current, flags);
131         sigfillset(&current->blocked);
132         RECALC_SIGPENDING;
133         SIGNAL_MASK_UNLOCK(current, flags);
134
135         complete(&pc->pc_starting);
136
137         /* like kswapd */
138         current->flags |= PF_MEMALLOC;
139
140         /* this mainloop strongly resembles ptlrpc_set_wait except
141          * that our set never completes.  ptlrpcd_check calls ptlrpc_check_set
142          * when there are requests in the set.  new requests come in
143          * on the set's new_req_list and ptlrpcd_check moves them into
144          * the set. */
145         while (1) {
146                 wait_queue_t set_wait;
147                 struct l_wait_info lwi;
148                 int timeout;
149
150                 timeout = ptlrpc_set_next_timeout(pc->pc_set) * HZ;
151                 lwi = LWI_TIMEOUT(timeout, ptlrpc_expired_set, pc->pc_set);
152
153                 /* ala the pinger, wait on pc's waitqueue and the set's */
154                 init_waitqueue_entry(&set_wait, current);
155                 add_wait_queue(&pc->pc_set->set_waitq, &set_wait);
156                 l_wait_event(pc->pc_waitq, ptlrpcd_check(pc), &lwi);
157                 remove_wait_queue(&pc->pc_set->set_waitq, &set_wait);
158
159                 if (test_bit(LIOD_STOP, &pc->pc_flags))
160                         break;
161         }
162         /* XXX should be making sure we don't have anything in flight */
163         complete(&pc->pc_finishing);
164         return 0;
165 }
166 #else
167 static int ptlrpcd_recurred = 0;
168 static void *ptlrpcd_callback;
169
170 int ptlrpcd_check_async_rpcs(void *arg)
171 {
172         struct ptlrpcd_ctl *pc = arg;
173         int                  rc = 0;
174
175         /* single threaded!! */
176         ptlrpcd_recurred++;
177
178         if (ptlrpcd_recurred == 1)
179                 rc = ptlrpcd_check(pc);
180
181         ptlrpcd_recurred--;
182         return rc;
183 }
184 #endif
185
186 int ptlrpcd_addref(void)
187 {
188         struct ptlrpcd_ctl *pc = &ptlrpcd_pc;
189         int rc = 0;
190         ENTRY;
191
192         down(&ptlrpcd_sem);
193         if (++ptlrpcd_users != 1)
194                 GOTO(out, rc);
195
196         memset(pc, 0, sizeof(*pc));
197         init_completion(&pc->pc_starting);
198         init_completion(&pc->pc_finishing);
199         init_waitqueue_head(&pc->pc_waitq);
200         pc->pc_flags = 0;
201         spin_lock_init(&pc->pc_lock);
202         INIT_LIST_HEAD(&pc->pc_req_list);
203
204         pc->pc_set = ptlrpc_prep_set();
205         if (pc->pc_set == NULL)
206                 GOTO(out, rc = -ENOMEM);
207
208 #ifdef __KERNEL__
209         if (kernel_thread(ptlrpcd, pc, 0) < 0)  {
210                 ptlrpc_set_destroy(pc->pc_set);
211                 GOTO(out, rc = -ECHILD);
212         }
213
214         wait_for_completion(&pc->pc_starting);
215 #else
216         ptlrpcd_callback =
217                 liblustre_register_wait_callback(&ptlrpcd_check_async_rpcs, pc);
218 #endif
219 out:
220         up(&ptlrpcd_sem);
221         RETURN(rc);
222 }
223
224 void ptlrpcd_decref(void)
225 {
226         struct ptlrpcd_ctl *pc = &ptlrpcd_pc;
227
228         down(&ptlrpcd_sem);
229         if (--ptlrpcd_users == 0) {
230                 set_bit(LIOD_STOP, &pc->pc_flags);
231                 wake_up(&pc->pc_waitq);
232 #ifdef __KERNEL__
233                 wait_for_completion(&pc->pc_finishing);
234 #else
235                 liblustre_deregister_wait_callback(ptlrpcd_callback);
236 #endif
237                 ptlrpc_set_destroy(pc->pc_set);
238         }
239         up(&ptlrpcd_sem);
240 }