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);
37 static int ptlrpc_check_event(struct ptlrpc_service *svc)
39 if (sigismember(&(current->pending.signal), SIGKILL) ||
40 sigismember(&(current->pending.signal), SIGINT)) {
41 svc->srv_flags |= SVC_KILLED;
46 if ( svc->srv_flags & SVC_STOPPING ) {
51 if (svc->srv_flags & SVC_EVENT)
54 if ( svc->srv_eq_h ) {
56 rc = PtlEQGet(svc->srv_eq_h, &svc->srv_ev);
59 svc->srv_flags |= SVC_EVENT;
64 if (rc != PTL_EQ_EMPTY) {
65 CDEBUG(D_NET, "BUG: PtlEQGet returned %d\n", rc);
73 if (!list_empty(&svc->srv_reqs)) {
74 svc->srv_flags |= SVC_LIST;
83 struct ptlrpc_service *
84 ptlrpc_init_svc(__u32 bufsize, int req_portal, int rep_portal, char *uuid,
85 req_unpack_t unpack, rep_pack_t pack, svc_handler_t handler)
88 struct ptlrpc_service *svc;
90 OBD_ALLOC(svc, sizeof(*svc));
92 CERROR("no memory\n");
96 memset(svc, 0, sizeof(*svc));
98 spin_lock_init(&svc->srv_lock);
99 INIT_LIST_HEAD(&svc->srv_reqs);
100 init_waitqueue_head(&svc->srv_ctl_waitq);
101 init_waitqueue_head(&svc->srv_waitq);
103 svc->srv_thread = NULL;
106 svc->srv_buf_size = bufsize;
107 svc->srv_rep_portal = rep_portal;
108 svc->srv_req_portal = req_portal;
109 svc->srv_req_unpack = unpack;
110 svc->srv_rep_pack = pack;
111 svc->srv_handler = handler;
112 err = kportal_uuid_to_peer(uuid, &svc->srv_self);
114 CERROR("cannot get peer for uuid %s", uuid);
115 OBD_FREE(svc, sizeof(*svc));
121 static int ptlrpc_main(void *arg)
124 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
125 struct obd_device *obddev = data->dev;
126 struct ptlrpc_service *svc = data->svc;
132 spin_lock_irq(¤t->sigmask_lock);
133 sigfillset(¤t->blocked);
134 recalc_sigpending(current);
135 spin_unlock_irq(¤t->sigmask_lock);
137 sprintf(current->comm, data->name);
139 /* Record that the thread is running */
140 svc->srv_thread = current;
141 svc->srv_flags = SVC_RUNNING;
142 wake_up(&svc->srv_ctl_waitq);
144 /* XXX maintain a list of all managed devices: insert here */
146 /* And now, loop forever on requests */
148 wait_event(svc->srv_waitq, ptlrpc_check_event(svc));
150 if (svc->srv_flags & SVC_SIGNAL) {
155 if (svc->srv_flags & SVC_STOPPING) {
160 if (svc->srv_flags & SVC_EVENT) {
161 struct ptlrpc_request request;
162 svc->srv_flags = SVC_RUNNING;
164 /* FIXME: If we move to an event-driven model,
165 * we should put the request on the stack of
166 * mds_handle instead. */
167 memset(&request, 0, sizeof(request));
168 request.rq_obd = obddev;
169 request.rq_reqbuf = (svc->srv_ev.mem_desc.start +
171 request.rq_reqlen = svc->srv_ev.mem_desc.length;
172 request.rq_xid = svc->srv_ev.match_bits;
173 CDEBUG(D_NET, "got req %d\n", request.rq_xid);
175 request.rq_peer.peer_nid = svc->srv_ev.initiator.nid;
176 /* FIXME: this NI should be the incoming NI.
177 * We don't know how to find that from here. */
178 request.rq_peer.peer_ni = svc->srv_self.peer_ni;
179 rc = svc->srv_handler(obddev, svc, &request);
180 ptl_received_rpc(svc);
181 svc->srv_flags &= ~SVC_EVENT;
185 if (svc->srv_flags & SVC_LIST) {
186 struct ptlrpc_request *request;
187 svc->srv_flags = SVC_RUNNING;
189 spin_lock(&svc->srv_lock);
190 request = list_entry(svc->srv_reqs.next,
191 struct ptlrpc_request,
193 list_del(&request->rq_list);
194 spin_unlock(&svc->srv_lock);
195 rc = svc->srv_handler(obddev, svc, request);
198 CERROR("unknown break in service");
202 svc->srv_thread = NULL;
203 svc->srv_flags = SVC_STOPPED;
204 wake_up(&svc->srv_ctl_waitq);
205 CDEBUG(D_NET, "svc exiting process %d\n", current->pid);
209 void ptlrpc_stop_thread(struct ptlrpc_service *svc)
211 svc->srv_flags = SVC_STOPPING;
213 wake_up(&svc->srv_waitq);
214 wait_event_interruptible(svc->srv_ctl_waitq,
215 (svc->srv_flags & SVC_STOPPED));
218 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
221 struct ptlrpc_svc_data d;
229 init_waitqueue_head(&svc->srv_waitq);
231 init_waitqueue_head(&svc->srv_ctl_waitq);
232 rc = kernel_thread(ptlrpc_main, (void *) &d,
233 CLONE_VM | CLONE_FS | CLONE_FILES);
235 CERROR("cannot start thread\n");
238 wait_event(svc->srv_ctl_waitq, svc->srv_flags & SVC_RUNNING);
245 int rpc_register_service(struct ptlrpc_service *service, char *uuid)
247 struct lustre_peer peer;
250 rc = kportal_uuid_to_peer(uuid, &peer);
252 CERROR("Invalid uuid \"%s\"\n", uuid);
256 service->srv_ring_length = RPC_RING_LENGTH;
257 service->srv_me_active = 0;
258 service->srv_md_active = 0;
260 service->srv_id.addr_kind = PTL_ADDR_GID;
261 service->srv_id.gid = PTL_ID_ANY;
262 service->srv_id.rid = PTL_ID_ANY;
264 rc = PtlEQAlloc(peer.peer_ni, 128, server_request_callback,
265 service, &(service->srv_eq_h));
268 CERROR("PtlEQAlloc failed: %d\n", rc);
272 CDEBUG(D_NET, "Starting service listening on portal %d\n",
273 service->srv_req_portal);
275 /* Attach the leading ME on which we build the ring */
276 rc = PtlMEAttach(peer.peer_ni, service->srv_req_portal,
277 service->srv_id, 0, ~0, PTL_RETAIN,
278 &(service->srv_me_h[0]));
281 CERROR("PtlMEAttach failed: %d\n", rc);
285 for (i = 0; i < service->srv_ring_length; i++) {
286 OBD_ALLOC(service->srv_buf[i], service->srv_buf_size);
288 if (service->srv_buf[i] == NULL) {
289 CERROR("no memory\n");
293 /* Insert additional ME's to the ring */
295 rc = PtlMEInsert(service->srv_me_h[i-1],
296 service->srv_id, 0, ~0, PTL_RETAIN,
297 PTL_INS_AFTER,&(service->srv_me_h[i]));
298 service->srv_me_tail = i;
301 CERROR("PtlMEInsert failed: %d\n", rc);
306 service->srv_ref_count[i] = 0;
307 service->srv_md[i].start = service->srv_buf[i];
308 service->srv_md[i].length = service->srv_buf_size;
309 service->srv_md[i].max_offset = service->srv_buf_size;
310 service->srv_md[i].threshold = PTL_MD_THRESH_INF;
311 service->srv_md[i].options = PTL_MD_OP_PUT;
312 service->srv_md[i].user_ptr = service;
313 service->srv_md[i].eventq = service->srv_eq_h;
314 service->srv_md[i].max_offset = service->srv_buf_size;
316 rc = PtlMDAttach(service->srv_me_h[i], service->srv_md[i],
317 PTL_RETAIN, &(service->srv_md_h[i]));
321 CERROR("PtlMDAttach failed: %d\n", rc);
329 int rpc_unregister_service(struct ptlrpc_service *service)
333 for (i = 0; i < service->srv_ring_length; i++) {
334 rc = PtlMDUnlink(service->srv_md_h[i]);
336 CERROR("PtlMDUnlink failed: %d\n", rc);
338 rc = PtlMEUnlink(service->srv_me_h[i]);
340 CERROR("PtlMEUnlink failed: %d\n", rc);
342 if (service->srv_buf[i] != NULL)
343 OBD_FREE(service->srv_buf[i], service->srv_buf_size);
344 service->srv_buf[i] = NULL;
347 rc = PtlEQFree(service->srv_eq_h);
349 CERROR("PtlEQFree failed: %d\n", rc);