Whamcloud - gitweb
- rename ha_mgr to recovd
[fs/lustre-release.git] / lustre / ptlrpc / service.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #define EXPORT_SYMTAB
24 #define DEBUG_SUBSYSTEM S_RPC
25
26 #include <linux/lustre_net.h>
27
28 extern int request_in_callback(ptl_event_t *ev, void *data);
29 extern int ptl_handled_rpc(struct ptlrpc_service *service, void *start);
30
31 static int ptlrpc_check_event(struct ptlrpc_service *svc)
32 {
33         int rc = 0;
34         ENTRY;
35
36         spin_lock(&svc->srv_lock);
37         if (sigismember(&(current->pending.signal), SIGKILL) ||
38             sigismember(&(current->pending.signal), SIGTERM) ||
39             sigismember(&(current->pending.signal), SIGINT)) {
40                 svc->srv_flags |= SVC_KILLED;
41                 GOTO(out, rc = 1);
42         }
43
44         if (svc->srv_flags & SVC_STOPPING)
45                 GOTO(out, rc = 1);
46
47         if (ptl_is_valid_handle(&svc->srv_eq_h)) {
48                 int err;
49                 err = PtlEQGet(svc->srv_eq_h, &svc->srv_ev);
50
51                 if (err == PTL_OK) {
52                         svc->srv_flags |= SVC_EVENT;
53                         GOTO(out, rc = 1);
54                 }
55
56                 if (err != PTL_EQ_EMPTY) {
57                         CDEBUG(D_NET, "BUG: PtlEQGet returned %d\n", rc);
58                         LBUG();
59                 }
60
61                 GOTO(out, rc = 0);
62         }
63
64         EXIT;
65  out:
66         spin_unlock(&svc->srv_lock);
67         return rc;
68 }
69
70 struct ptlrpc_service *
71 ptlrpc_init_svc(__u32 bufsize, int req_portal, int rep_portal, char *uuid,
72                 svc_handler_t handler)
73 {
74         int err;
75         int rc, i;
76         struct ptlrpc_service *service;
77         ENTRY;
78
79         OBD_ALLOC(service, sizeof(*service));
80         if (!service) {
81                 LBUG();
82                 RETURN(NULL);
83         }
84
85         spin_lock_init(&service->srv_lock);
86         INIT_LIST_HEAD(&service->srv_reqs);
87         init_waitqueue_head(&service->srv_ctl_waitq);
88         init_waitqueue_head(&service->srv_waitq);
89
90         service->srv_thread = NULL;
91         service->srv_flags = 0;
92
93         service->srv_buf_size = bufsize;
94         service->srv_rep_portal = rep_portal;
95         service->srv_req_portal = req_portal;
96         service->srv_handler = handler;
97
98         err = kportal_uuid_to_peer(uuid, &service->srv_self);
99         if (err) {
100                 CERROR("cannot get peer for uuid %s", uuid);
101                 GOTO(err_free, NULL);
102         }
103
104         service->srv_ring_length = RPC_RING_LENGTH;
105         service->srv_id.nid = PTL_ID_ANY;
106         service->srv_id.pid = PTL_ID_ANY;
107
108         rc = PtlEQAlloc(service->srv_self.peer_ni, 128, request_in_callback,
109                         service, &(service->srv_eq_h));
110
111         if (rc != PTL_OK) {
112                 CERROR("PtlEQAlloc failed: %d\n", rc);
113                 LBUG();
114                 GOTO(err_free, NULL);
115         }
116
117         for (i = 0; i < service->srv_ring_length; i++) {
118                 OBD_ALLOC(service->srv_buf[i], service->srv_buf_size);
119                 if (service->srv_buf[i] == NULL) {
120                         CERROR("no memory\n");
121                         LBUG();
122                         GOTO(err_ring, NULL);
123                 }
124                 service->srv_ref_count[i] = 0;
125                 ptlrpc_link_svc_me(service, i);
126         }
127
128         CDEBUG(D_NET, "Starting service listening on portal %d\n",
129                service->srv_req_portal);
130
131         RETURN(service);
132 err_ring:
133         service->srv_ring_length = i;
134         rpc_unregister_service(service); // XXX verify this is right
135         PtlEQFree(service->srv_eq_h);
136 err_free:
137         OBD_FREE(service, sizeof(*service));
138         return NULL;
139 }
140
141 static int handle_incoming_request(struct obd_device *obddev,
142                                    struct ptlrpc_service *svc)
143 {
144         struct ptlrpc_request request;
145         struct lustre_peer peer;
146         void *start;
147         int rc;
148
149         /* FIXME: If we move to an event-driven model, we should put the request
150          * on the stack of mds_handle instead. */
151         start = svc->srv_ev.mem_desc.start;
152         memset(&request, 0, sizeof(request));
153         request.rq_obd = obddev;
154         request.rq_reqmsg = (svc->srv_ev.mem_desc.start +
155                              svc->srv_ev.offset);
156         request.rq_reqlen = svc->srv_ev.mem_desc.length;
157
158         if (request.rq_reqmsg->xid != svc->srv_ev.match_bits)
159                 LBUG();
160
161         CDEBUG(D_NET, "got req %d\n", request.rq_reqmsg->xid);
162
163         peer.peer_nid = svc->srv_ev.initiator.nid;
164         /* FIXME: this NI should be the incoming NI.
165          * We don't know how to find that from here. */
166         peer.peer_ni = svc->srv_self.peer_ni;
167
168         if (request.rq_reqmsg->conn) {
169                 request.rq_connection =
170                         (void *)(unsigned long)request.rq_reqmsg->conn;
171                 if (request.rq_reqmsg->token !=
172                     request.rq_connection->c_token) {
173                         struct ptlrpc_connection *tmp;
174                         tmp = ptlrpc_get_connection(&peer);
175                         CERROR("rq_reqmsg->conn: %p\n", request.rq_connection);
176                         CERROR("real connection: %p\n", tmp);
177                         CERROR("rq_reqmsg->token: %Lu\n",
178                                request.rq_reqmsg->token);
179                         CERROR("real token      : %Lu\n", tmp->c_token);
180                         LBUG();
181                 }
182                 ptlrpc_connection_addref(request.rq_connection);
183         } else {
184                 /*
185                   PHIL? should we perhaps only do this when 
186                    we get an incoming connmgr_connect request? 
187                 */
188                 request.rq_connection = ptlrpc_get_connection(&peer);
189                 if (!request.rq_connection)
190                         LBUG();
191         }
192
193         svc->srv_flags &= ~SVC_EVENT;
194
195         spin_unlock(&svc->srv_lock);
196         rc = svc->srv_handler(obddev, svc, &request);
197         ptlrpc_put_connection(request.rq_connection);
198         ptl_handled_rpc(svc, start);
199         return rc;
200 }
201
202 static int ptlrpc_main(void *arg)
203 {
204         int rc;
205         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
206         struct obd_device *obddev = data->dev;
207         struct ptlrpc_service *svc = data->svc;
208
209         ENTRY;
210
211         lock_kernel();
212         daemonize();
213         spin_lock_irq(&current->sigmask_lock);
214         sigfillset(&current->blocked);
215         recalc_sigpending(current);
216         spin_unlock_irq(&current->sigmask_lock);
217
218         sprintf(current->comm, data->name);
219
220         /* Record that the  thread is running */
221         svc->srv_thread = current;
222         svc->srv_flags = SVC_RUNNING;
223         wake_up(&svc->srv_ctl_waitq);
224
225         /* XXX maintain a list of all managed devices: insert here */
226
227         /* And now, loop forever on requests */
228         while (1) {
229                 wait_event(svc->srv_waitq, ptlrpc_check_event(svc));
230
231                 spin_lock(&svc->srv_lock);
232                 if (svc->srv_flags & SVC_SIGNAL) {
233                         svc->srv_flags &= ~SVC_SIGNAL;
234                         spin_unlock(&svc->srv_lock);
235                         EXIT;
236                         break;
237                 }
238
239                 if (svc->srv_flags & SVC_STOPPING) {
240                         svc->srv_flags &= ~SVC_STOPPING;
241                         spin_unlock(&svc->srv_lock);
242                         EXIT;
243                         break;
244                 }
245                 
246                 if (svc->srv_flags & SVC_EVENT) { 
247                         svc->srv_flags &= ~SVC_EVENT;
248                         rc = handle_incoming_request(obddev, svc);
249                         continue;
250                 }
251
252                 CERROR("unknown break in service");
253                 spin_unlock(&svc->srv_lock);
254                 EXIT;
255                 break;
256         }
257
258         svc->srv_thread = NULL;
259         svc->srv_flags = SVC_STOPPED;
260         wake_up(&svc->srv_ctl_waitq);
261         CDEBUG(D_NET, "svc exiting process %d\n", current->pid);
262         return 0;
263 }
264
265 void ptlrpc_stop_thread(struct ptlrpc_service *svc)
266 {
267         svc->srv_flags = SVC_STOPPING;
268
269         wake_up(&svc->srv_waitq);
270         wait_event_interruptible(svc->srv_ctl_waitq,
271                                  (svc->srv_flags & SVC_STOPPED));
272 }
273
274 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
275                                 char *name)
276 {
277         struct ptlrpc_svc_data d;
278         int rc;
279         ENTRY;
280
281         d.dev = dev;
282         d.svc = svc;
283         d.name = name;
284
285         init_waitqueue_head(&svc->srv_waitq);
286
287         init_waitqueue_head(&svc->srv_ctl_waitq);
288         rc = kernel_thread(ptlrpc_main, (void *) &d,
289                            CLONE_VM | CLONE_FS | CLONE_FILES);
290         if (rc < 0) {
291                 CERROR("cannot start thread\n");
292                 RETURN(-EINVAL);
293         }
294         wait_event(svc->srv_ctl_waitq, svc->srv_flags & SVC_RUNNING);
295
296         RETURN(0);
297 }
298
299 int rpc_unregister_service(struct ptlrpc_service *service)
300 {
301         int rc, i;
302
303         for (i = 0; i < service->srv_ring_length; i++) {
304                 if (ptl_is_valid_handle(&(service->srv_me_h[i]))) {
305                         rc = PtlMEUnlink(service->srv_me_h[i]);
306                         if (rc)
307                                 CERROR("PtlMEUnlink failed: %d\n", rc);
308                         ptl_set_inv_handle(&(service->srv_me_h[i]));
309                 }
310
311                 if (service->srv_buf[i] != NULL)
312                         OBD_FREE(service->srv_buf[i], service->srv_buf_size);
313                 service->srv_buf[i] = NULL;
314         }
315
316         rc = PtlEQFree(service->srv_eq_h);
317         if (rc)
318                 CERROR("PtlEQFree failed: %d\n", rc);
319
320         return 0;
321 }