1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
25 #include <linux/config.h>
26 #include <linux/module.h>
27 #include <linux/kernel.h>
29 #define DEBUG_SUBSYSTEM S_RPC
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
35 extern int server_request_callback(ptl_event_t *ev, void *data);
36 extern int ptl_handled_rpc(struct ptlrpc_service *service, void *start);
38 static int ptlrpc_check_event(struct ptlrpc_service *svc)
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;
50 if (svc->srv_flags & SVC_STOPPING)
53 if (svc->srv_flags & SVC_EVENT)
58 err = PtlEQGet(svc->srv_eq_h, &svc->srv_ev);
61 svc->srv_flags |= SVC_EVENT;
65 if (err != PTL_EQ_EMPTY) {
66 CDEBUG(D_NET, "BUG: PtlEQGet returned %d\n", rc);
73 if (!list_empty(&svc->srv_reqs)) {
74 svc->srv_flags |= SVC_LIST;
80 spin_unlock(&svc->srv_lock);
84 struct ptlrpc_service *
85 ptlrpc_init_svc(__u32 bufsize, int req_portal, int rep_portal, char *uuid,
86 svc_handler_t handler)
90 struct ptlrpc_service *service;
92 OBD_ALLOC(service, sizeof(*service));
98 memset(service, 0, sizeof(*service));
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);
105 service->srv_thread = NULL;
106 service->srv_flags = 0;
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;
113 err = kportal_uuid_to_peer(uuid, &service->srv_self);
115 CERROR("cannot get peer for uuid %s", uuid);
116 GOTO(err_free, NULL);
119 service->srv_ring_length = RPC_RING_LENGTH;
120 service->srv_id.nid = PTL_ID_ANY;
121 service->srv_id.pid = PTL_ID_ANY;
123 rc = PtlEQAlloc(service->srv_self.peer_ni, 128,
124 server_request_callback,
125 service, &(service->srv_eq_h));
128 CERROR("PtlEQAlloc failed: %d\n", rc);
130 GOTO(err_free, NULL);
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");
138 GOTO(err_ring, NULL);
140 service->srv_ref_count[i] = 0;
141 ptlrpc_link_svc_me(service, i);
144 CDEBUG(D_NET, "Starting service listening on portal %d\n",
145 service->srv_req_portal);
149 service->srv_ring_length = i;
150 rpc_unregister_service(service); // XXX verify this is right
151 PtlEQFree(service->srv_eq_h);
153 OBD_FREE(service, sizeof(*service));
157 static int ptlrpc_main(void *arg)
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;
168 spin_lock_irq(¤t->sigmask_lock);
169 sigfillset(¤t->blocked);
170 recalc_sigpending(current);
171 spin_unlock_irq(¤t->sigmask_lock);
173 sprintf(current->comm, data->name);
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);
180 /* XXX maintain a list of all managed devices: insert here */
182 /* And now, loop forever on requests */
184 wait_event(svc->srv_waitq, ptlrpc_check_event(svc));
186 spin_lock(&svc->srv_lock);
187 if (svc->srv_flags & SVC_SIGNAL) {
188 spin_unlock(&svc->srv_lock);
193 if (svc->srv_flags & SVC_STOPPING) {
194 spin_unlock(&svc->srv_lock);
199 if (svc->srv_flags & SVC_EVENT) {
200 struct ptlrpc_request request;
202 svc->srv_flags = SVC_RUNNING;
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 +
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);
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;
222 spin_unlock(&svc->srv_lock);
223 rc = svc->srv_handler(obddev, svc, &request);
224 ptl_handled_rpc(svc, start);
228 if (svc->srv_flags & SVC_LIST) {
229 struct ptlrpc_request *request;
230 svc->srv_flags = SVC_RUNNING;
232 request = list_entry(svc->srv_reqs.next,
233 struct ptlrpc_request,
235 list_del(&request->rq_list);
236 spin_unlock(&svc->srv_lock);
237 rc = svc->srv_handler(obddev, svc, request);
240 CERROR("unknown break in service");
241 spin_unlock(&svc->srv_lock);
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);
253 void ptlrpc_stop_thread(struct ptlrpc_service *svc)
255 svc->srv_flags = SVC_STOPPING;
257 wake_up(&svc->srv_waitq);
258 wait_event_interruptible(svc->srv_ctl_waitq,
259 (svc->srv_flags & SVC_STOPPED));
262 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
265 struct ptlrpc_svc_data d;
273 init_waitqueue_head(&svc->srv_waitq);
275 init_waitqueue_head(&svc->srv_ctl_waitq);
276 rc = kernel_thread(ptlrpc_main, (void *) &d,
277 CLONE_VM | CLONE_FS | CLONE_FILES);
279 CERROR("cannot start thread\n");
282 wait_event(svc->srv_ctl_waitq, svc->srv_flags & SVC_RUNNING);
287 int rpc_unregister_service(struct ptlrpc_service *service)
291 for (i = 0; i < service->srv_ring_length; i++) {
292 if (service->srv_me_h[i]) {
293 rc = PtlMEUnlink(service->srv_me_h[i]);
295 CERROR("PtlMEUnlink failed: %d\n", rc);
296 service->srv_me_h[i] = 0;
299 if (service->srv_buf[i] != NULL)
300 OBD_FREE(service->srv_buf[i], service->srv_buf_size);
301 service->srv_buf[i] = NULL;
304 rc = PtlEQFree(service->srv_eq_h);
306 CERROR("PtlEQFree failed: %d\n", rc);