Whamcloud - gitweb
d9c2b960bdf3976b77f1a624920aa8e8d7701516
[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
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
28
29 #define DEBUG_SUBSYSTEM S_RPC
30
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
34
35 extern int server_request_callback(ptl_event_t *ev, void *data);
36 extern int ptl_handled_rpc(struct ptlrpc_service *service, void *start);
37
38 static int ptlrpc_check_event(struct ptlrpc_service *svc)
39 {
40         int rc = 0;
41
42         spin_lock(&svc->srv_lock); 
43         if (sigismember(&(current->pending.signal), SIGKILL) ||
44             sigismember(&(current->pending.signal), SIGTERM) ||
45             sigismember(&(current->pending.signal), SIGINT)) { 
46                 svc->srv_flags |= SVC_KILLED;
47                 GOTO(out, rc = 1);
48         }
49
50         if (svc->srv_flags & SVC_STOPPING)
51                 GOTO(out, rc = 1);
52
53         if (svc->srv_flags & SVC_EVENT)
54                 LBUG();
55
56         if (ptl_is_valid_handle(&svc->srv_eq_h)) {
57                 int err;
58                 err = PtlEQGet(svc->srv_eq_h, &svc->srv_ev);
59
60                 if (err == PTL_OK) { 
61                         svc->srv_flags |= SVC_EVENT;
62                         GOTO(out, rc = 1);
63                 }
64
65                 if (err != PTL_EQ_EMPTY) {
66                         CDEBUG(D_NET, "BUG: PtlEQGet returned %d\n", rc);
67                         LBUG();
68                 }
69
70                 GOTO(out, rc = 0);
71         }
72
73         if (!list_empty(&svc->srv_reqs)) {
74                 svc->srv_flags |= SVC_LIST;
75                 GOTO(out, rc = 1);
76         }
77
78         EXIT;
79  out:
80         spin_unlock(&svc->srv_lock); 
81         return rc;
82 }
83
84 struct ptlrpc_service *
85 ptlrpc_init_svc(__u32 bufsize, int req_portal, int rep_portal, char *uuid,
86                 svc_handler_t handler)
87 {
88         int err;
89         int rc, i;
90         struct ptlrpc_service *service;
91
92         OBD_ALLOC(service, sizeof(*service)); 
93         if ( !service ) { 
94                 LBUG();
95                 RETURN(NULL);
96         }
97
98         memset(service, 0, sizeof(*service)); 
99
100         spin_lock_init(&service->srv_lock);
101         INIT_LIST_HEAD(&service->srv_reqs);
102         init_waitqueue_head(&service->srv_ctl_waitq); 
103         init_waitqueue_head(&service->srv_waitq); 
104
105         service->srv_thread = NULL;
106         service->srv_flags = 0;
107
108         service->srv_buf_size = bufsize;
109         service->srv_rep_portal = rep_portal;
110         service->srv_req_portal = req_portal;
111         service->srv_handler = handler;
112
113         err = kportal_uuid_to_peer(uuid, &service->srv_self);
114         if (err) { 
115                 CERROR("cannot get peer for uuid %s", uuid); 
116                 GOTO(err_free, NULL); 
117         }
118
119         service->srv_ring_length = RPC_RING_LENGTH;
120         service->srv_id.nid = PTL_ID_ANY;
121         service->srv_id.pid = PTL_ID_ANY;
122
123         rc = PtlEQAlloc(service->srv_self.peer_ni, 128, 
124                         server_request_callback,
125                         service, &(service->srv_eq_h));
126
127         if (rc != PTL_OK) {
128                 CERROR("PtlEQAlloc failed: %d\n", rc);
129                 LBUG();
130                 GOTO(err_free, NULL);
131         }
132
133         for (i = 0; i < service->srv_ring_length; i++) {
134                 OBD_ALLOC(service->srv_buf[i], service->srv_buf_size);
135                 if (service->srv_buf[i] == NULL) {
136                         CERROR("no memory\n");
137                         LBUG();
138                         GOTO(err_ring, NULL);
139                 }
140                 service->srv_ref_count[i] = 0; 
141                 ptlrpc_link_svc_me(service, i); 
142         }
143
144         CDEBUG(D_NET, "Starting service listening on portal %d\n",
145                service->srv_req_portal);
146
147         return service;
148 err_ring:
149         service->srv_ring_length = i;
150         rpc_unregister_service(service); // XXX verify this is right
151         PtlEQFree(service->srv_eq_h);
152 err_free:
153         OBD_FREE(service, sizeof(*service));
154         return NULL;
155 }
156
157 static int ptlrpc_main(void *arg)
158 {
159         int rc;
160         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
161         struct obd_device *obddev = data->dev;
162         struct ptlrpc_service *svc = data->svc;
163
164         ENTRY;
165
166         lock_kernel();
167         daemonize();
168         spin_lock_irq(&current->sigmask_lock);
169         sigfillset(&current->blocked);
170         recalc_sigpending(current);
171         spin_unlock_irq(&current->sigmask_lock);
172
173         sprintf(current->comm, data->name);
174
175         /* Record that the  thread is running */
176         svc->srv_thread = current;
177         svc->srv_flags = SVC_RUNNING;
178         wake_up(&svc->srv_ctl_waitq); 
179
180         /* XXX maintain a list of all managed devices: insert here */
181
182         /* And now, loop forever on requests */
183         while (1) {
184                 wait_event(svc->srv_waitq, ptlrpc_check_event(svc));
185                 
186                 spin_lock(&svc->srv_lock);
187                 if (svc->srv_flags & SVC_SIGNAL) {
188                         spin_unlock(&svc->srv_lock);
189                         EXIT;
190                         break;
191                 }
192
193                 if (svc->srv_flags & SVC_STOPPING) {
194                         spin_unlock(&svc->srv_lock);
195                         EXIT;
196                         break;
197                 }
198
199                 if (svc->srv_flags & SVC_EVENT) { 
200                         struct ptlrpc_request request;
201                         void *start;
202                         svc->srv_flags = SVC_RUNNING; 
203
204                         /* FIXME: If we move to an event-driven model,
205                          * we should put the request on the stack of
206                          * mds_handle instead. */
207                         start = svc->srv_ev.mem_desc.start;
208                         memset(&request, 0, sizeof(request));
209                         request.rq_obd = obddev;
210                         request.rq_reqbuf = (svc->srv_ev.mem_desc.start +
211                                              svc->srv_ev.offset);
212                         request.rq_reqlen = svc->srv_ev.mem_desc.length;
213                         request.rq_xid = svc->srv_ev.match_bits;
214                         CDEBUG(D_NET, "got req %d\n", request.rq_xid);
215
216                         request.rq_peer.peer_nid = svc->srv_ev.initiator.nid;
217                         /* FIXME: this NI should be the incoming NI.
218                          * We don't know how to find that from here. */
219                         request.rq_peer.peer_ni = svc->srv_self.peer_ni;
220                         svc->srv_flags &= ~SVC_EVENT;
221
222                         spin_unlock(&svc->srv_lock);
223                         rc = svc->srv_handler(obddev, svc, &request);
224                         ptl_handled_rpc(svc, start);
225                         continue;
226                 }
227
228                 if (svc->srv_flags & SVC_LIST) { 
229                         struct ptlrpc_request *request;
230                         svc->srv_flags = SVC_RUNNING; 
231
232                         request = list_entry(svc->srv_reqs.next,
233                                              struct ptlrpc_request,
234                                              rq_list);
235                         list_del(&request->rq_list);
236                         spin_unlock(&svc->srv_lock);
237                         rc = svc->srv_handler(obddev, svc, request);
238                         continue;
239                 }
240                 CERROR("unknown break in service"); 
241                 spin_unlock(&svc->srv_lock);
242                 EXIT;
243                 break; 
244         }
245
246         svc->srv_thread = NULL;
247         svc->srv_flags = SVC_STOPPED;
248         wake_up(&svc->srv_ctl_waitq);
249         CDEBUG(D_NET, "svc exiting process %d\n", current->pid);
250         return 0;
251 }
252
253 void ptlrpc_stop_thread(struct ptlrpc_service *svc)
254 {
255         svc->srv_flags = SVC_STOPPING;
256
257         wake_up(&svc->srv_waitq);
258         wait_event_interruptible(svc->srv_ctl_waitq, 
259                                  (svc->srv_flags & SVC_STOPPED));
260 }
261
262 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
263                                 char *name)
264 {
265         struct ptlrpc_svc_data d;
266         int rc;
267         ENTRY;
268
269         d.dev = dev;
270         d.svc = svc;
271         d.name = name;
272
273         init_waitqueue_head(&svc->srv_waitq);
274
275         init_waitqueue_head(&svc->srv_ctl_waitq);
276         rc = kernel_thread(ptlrpc_main, (void *) &d, 
277                            CLONE_VM | CLONE_FS | CLONE_FILES);
278         if (rc < 0) {
279                 CERROR("cannot start thread\n"); 
280                 RETURN(-EINVAL);
281         }
282         wait_event(svc->srv_ctl_waitq, svc->srv_flags & SVC_RUNNING);
283
284         RETURN(0);
285 }
286
287 int rpc_unregister_service(struct ptlrpc_service *service)
288 {
289         int rc, i;
290
291         for (i = 0; i < service->srv_ring_length; i++) {
292                 if (ptl_is_valid_handle(&(service->srv_me_h[i]))) {
293                         rc = PtlMEUnlink(service->srv_me_h[i]);
294                         if (rc)
295                                 CERROR("PtlMEUnlink failed: %d\n", rc);
296                         ptl_set_inv_handle(&(service->srv_me_h[i]));
297                 }
298
299                 if (service->srv_buf[i] != NULL)
300                         OBD_FREE(service->srv_buf[i], service->srv_buf_size);
301                 service->srv_buf[i] = NULL;
302         }
303
304         rc = PtlEQFree(service->srv_eq_h);
305         if (rc)
306                 CERROR("PtlEQFree failed: %d\n", rc);
307
308         return 0;
309 }