Whamcloud - gitweb
- fixed some warnings in the DLM
[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                 request.rq_connection = ptlrpc_get_connection(&peer);
185                 if (!request.rq_connection)
186                         LBUG();
187         }
188
189         svc->srv_flags &= ~SVC_EVENT;
190
191         spin_unlock(&svc->srv_lock);
192         rc = svc->srv_handler(obddev, svc, &request);
193         ptlrpc_put_connection(request.rq_connection);
194         ptl_handled_rpc(svc, start);
195         return rc;
196 }
197
198 static int ptlrpc_main(void *arg)
199 {
200         int rc;
201         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
202         struct obd_device *obddev = data->dev;
203         struct ptlrpc_service *svc = data->svc;
204
205         ENTRY;
206
207         lock_kernel();
208         daemonize();
209         spin_lock_irq(&current->sigmask_lock);
210         sigfillset(&current->blocked);
211         recalc_sigpending(current);
212         spin_unlock_irq(&current->sigmask_lock);
213
214         sprintf(current->comm, data->name);
215
216         /* Record that the  thread is running */
217         svc->srv_thread = current;
218         svc->srv_flags = SVC_RUNNING;
219         wake_up(&svc->srv_ctl_waitq);
220
221         /* XXX maintain a list of all managed devices: insert here */
222
223         /* And now, loop forever on requests */
224         while (1) {
225                 wait_event(svc->srv_waitq, ptlrpc_check_event(svc));
226
227                 spin_lock(&svc->srv_lock);
228                 if (svc->srv_flags & SVC_SIGNAL) {
229                         svc->srv_flags &= ~SVC_SIGNAL;
230                         spin_unlock(&svc->srv_lock);
231                         EXIT;
232                         break;
233                 }
234
235                 if (svc->srv_flags & SVC_STOPPING) {
236                         svc->srv_flags &= ~SVC_STOPPING;
237                         spin_unlock(&svc->srv_lock);
238                         EXIT;
239                         break;
240                 }
241                 
242                 if (svc->srv_flags & SVC_EVENT) { 
243                         svc->srv_flags &= ~SVC_EVENT;
244                         rc = handle_incoming_request(obddev, svc);
245                         continue;
246                 }
247
248                 CERROR("unknown break in service");
249                 spin_unlock(&svc->srv_lock);
250                 EXIT;
251                 break;
252         }
253
254         svc->srv_thread = NULL;
255         svc->srv_flags = SVC_STOPPED;
256         wake_up(&svc->srv_ctl_waitq);
257         CDEBUG(D_NET, "svc exiting process %d\n", current->pid);
258         return 0;
259 }
260
261 void ptlrpc_stop_thread(struct ptlrpc_service *svc)
262 {
263         svc->srv_flags = SVC_STOPPING;
264
265         wake_up(&svc->srv_waitq);
266         wait_event_interruptible(svc->srv_ctl_waitq,
267                                  (svc->srv_flags & SVC_STOPPED));
268 }
269
270 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
271                                 char *name)
272 {
273         struct ptlrpc_svc_data d;
274         int rc;
275         ENTRY;
276
277         d.dev = dev;
278         d.svc = svc;
279         d.name = name;
280
281         init_waitqueue_head(&svc->srv_waitq);
282
283         init_waitqueue_head(&svc->srv_ctl_waitq);
284         rc = kernel_thread(ptlrpc_main, (void *) &d,
285                            CLONE_VM | CLONE_FS | CLONE_FILES);
286         if (rc < 0) {
287                 CERROR("cannot start thread\n");
288                 RETURN(-EINVAL);
289         }
290         wait_event(svc->srv_ctl_waitq, svc->srv_flags & SVC_RUNNING);
291
292         RETURN(0);
293 }
294
295 int rpc_unregister_service(struct ptlrpc_service *service)
296 {
297         int rc, i;
298
299         for (i = 0; i < service->srv_ring_length; i++) {
300                 if (ptl_is_valid_handle(&(service->srv_me_h[i]))) {
301                         rc = PtlMEUnlink(service->srv_me_h[i]);
302                         if (rc)
303                                 CERROR("PtlMEUnlink failed: %d\n", rc);
304                         ptl_set_inv_handle(&(service->srv_me_h[i]));
305                 }
306
307                 if (service->srv_buf[i] != NULL)
308                         OBD_FREE(service->srv_buf[i], service->srv_buf_size);
309                 service->srv_buf[i] = NULL;
310         }
311
312         rc = PtlEQFree(service->srv_eq_h);
313         if (rc)
314                 CERROR("PtlEQFree failed: %d\n", rc);
315
316         return 0;
317 }