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.
23 #define DEBUG_SUBSYSTEM S_RPC
25 #include <linux/obd_support.h>
26 #include <linux/obd_class.h>
27 #include <linux/lustre_net.h>
29 extern int request_in_callback(ptl_event_t *ev);
30 extern int ptl_handled_rpc(struct ptlrpc_service *service, void *start);
32 static int ptlrpc_check_event(struct ptlrpc_service *svc,
33 struct ptlrpc_thread *thread, ptl_event_t *event)
38 spin_lock(&svc->srv_lock);
39 if (thread->t_flags & SVC_STOPPING)
42 if (ptl_is_valid_handle(&svc->srv_eq_h)) {
44 err = PtlEQGet(svc->srv_eq_h, event);
47 thread->t_flags |= SVC_EVENT;
51 if (err != PTL_EQ_EMPTY) {
52 CERROR("BUG: PtlEQGet returned %d\n", rc);
61 spin_unlock(&svc->srv_lock);
65 struct ptlrpc_service *
66 ptlrpc_init_svc(__u32 bufsize, int req_portal, int rep_portal, char *uuid,
67 svc_handler_t handler)
71 struct ptlrpc_service *service;
74 OBD_ALLOC(service, sizeof(*service));
80 spin_lock_init(&service->srv_lock);
81 INIT_LIST_HEAD(&service->srv_reqs);
82 INIT_LIST_HEAD(&service->srv_threads);
83 init_waitqueue_head(&service->srv_waitq);
85 service->srv_buf_size = bufsize;
86 service->srv_rep_portal = rep_portal;
87 service->srv_req_portal = req_portal;
88 service->srv_handler = handler;
90 err = kportal_uuid_to_peer(uuid, &service->srv_self);
92 CERROR("cannot get peer for uuid '%s'\n", uuid);
93 OBD_FREE(service, sizeof(*service));
97 service->srv_ring_length = RPC_RING_LENGTH;
99 rc = PtlEQAlloc(service->srv_self.peer_ni, 1024, request_in_callback,
100 &(service->srv_eq_h));
103 CERROR("PtlEQAlloc failed: %d\n", rc);
105 OBD_FREE(service, sizeof(*service));
109 for (i = 0; i < service->srv_ring_length; i++) {
110 OBD_ALLOC(service->srv_buf[i], service->srv_buf_size);
111 if (service->srv_buf[i] == NULL) {
112 CERROR("no memory\n");
114 GOTO(err_ring, NULL);
116 service->srv_ref_count[i] = 0;
117 ptlrpc_link_svc_me(service, i);
120 CDEBUG(D_NET, "Starting service listening on portal %d\n",
121 service->srv_req_portal);
125 service->srv_ring_length = i;
126 ptlrpc_unregister_service(service);
130 static int handle_incoming_request(struct obd_device *obddev,
131 struct ptlrpc_service *svc,
134 struct ptlrpc_request request;
135 struct lustre_peer peer;
139 /* FIXME: If we move to an event-driven model, we should put the request
140 * on the stack of mds_handle instead. */
141 start = event->mem_desc.start;
143 memset(&request, 0, sizeof(request));
144 request.rq_svc = svc;
145 request.rq_obd = obddev;
146 request.rq_xid = event->match_bits;
147 request.rq_reqmsg = event->mem_desc.start + event->offset;
148 request.rq_reqlen = event->mem_desc.length;
150 if (request.rq_reqlen < sizeof(struct lustre_msg)) {
151 CERROR("incomplete request: ptl %d from %Lx xid %Ld\n",
152 svc->srv_req_portal, event->initiator.nid,
154 spin_unlock(&svc->srv_lock);
158 if (request.rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
159 CERROR("wrong lustre_msg magic: ptl %d from %Lx xid %Ld\n",
160 svc->srv_req_portal, event->initiator.nid,
162 spin_unlock(&svc->srv_lock);
166 if (request.rq_reqmsg->version != PTLRPC_MSG_VERSION) {
167 CERROR("wrong lustre_msg version: ptl %d from %Lx xid %Ld\n",
168 svc->srv_req_portal, event->initiator.nid,
170 spin_unlock(&svc->srv_lock);
174 CDEBUG(D_NET, "got req %Ld\n", request.rq_xid);
176 peer.peer_nid = event->initiator.nid;
177 /* FIXME: this NI should be the incoming NI.
178 * We don't know how to find that from here. */
179 peer.peer_ni = svc->srv_self.peer_ni;
181 request.rq_export = class_conn2export((struct lustre_handle *) request.rq_reqmsg);
183 if (request.rq_export) {
184 request.rq_connection = request.rq_export->exp_connection;
185 ptlrpc_connection_addref(request.rq_connection);
187 request.rq_connection = ptlrpc_get_connection(&peer);
190 spin_unlock(&svc->srv_lock);
191 rc = svc->srv_handler(&request);
192 ptlrpc_put_connection(request.rq_connection);
193 ptl_handled_rpc(svc, start);
197 void ptlrpc_rotate_reqbufs(struct ptlrpc_service *service,
202 for (index = 0; index < service->srv_ring_length; index++)
203 if (service->srv_buf[index] == ev->mem_desc.start)
206 if (index == service->srv_ring_length)
209 service->srv_ref_count[index]++;
211 if (ptl_is_valid_handle(&ev->unlinked_me)) {
214 for (idx = 0; idx < service->srv_ring_length; idx++)
215 if (service->srv_me_h[idx].handle_idx ==
216 ev->unlinked_me.handle_idx)
218 if (idx == service->srv_ring_length)
221 CDEBUG(D_NET, "unlinked %d\n", idx);
222 ptl_set_inv_handle(&(service->srv_me_h[idx]));
224 if (service->srv_ref_count[idx] == 0)
225 ptlrpc_link_svc_me(service, idx);
229 static int ptlrpc_main(void *arg)
232 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
233 struct obd_device *obddev = data->dev;
234 struct ptlrpc_service *svc = data->svc;
235 struct ptlrpc_thread *thread = data->thread;
241 spin_lock_irq(¤t->sigmask_lock);
242 sigfillset(¤t->blocked);
243 recalc_sigpending(current);
244 spin_unlock_irq(¤t->sigmask_lock);
246 sprintf(current->comm, data->name);
248 /* Record that the thread is running */
249 thread->t_flags = SVC_RUNNING;
250 wake_up(&thread->t_ctl_waitq);
252 /* XXX maintain a list of all managed devices: insert here */
254 /* And now, loop forever on requests */
258 wait_event(svc->srv_waitq,
259 ptlrpc_check_event(svc, thread, &event));
261 spin_lock(&svc->srv_lock);
263 if (thread->t_flags & SVC_STOPPING) {
264 thread->t_flags &= ~SVC_STOPPING;
265 spin_unlock(&svc->srv_lock);
270 if (thread->t_flags & SVC_EVENT) {
271 thread->t_flags &= ~SVC_EVENT;
272 ptlrpc_rotate_reqbufs(svc, &event);
274 rc = handle_incoming_request(obddev, svc, &event);
275 thread->t_flags &= ~SVC_EVENT;
279 CERROR("unknown break in service");
280 spin_unlock(&svc->srv_lock);
285 thread->t_flags = SVC_STOPPED;
286 wake_up(&thread->t_ctl_waitq);
287 CDEBUG(D_NET, "service thread exiting, process %d\n", current->pid);
291 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
292 struct ptlrpc_thread *thread)
294 spin_lock(&svc->srv_lock);
295 thread->t_flags = SVC_STOPPING;
296 spin_unlock(&svc->srv_lock);
298 wake_up(&svc->srv_waitq);
299 wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
302 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
304 spin_lock(&svc->srv_lock);
305 while (!list_empty(&svc->srv_threads)) {
306 struct ptlrpc_thread *thread;
307 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
309 spin_unlock(&svc->srv_lock);
310 ptlrpc_stop_thread(svc, thread);
311 spin_lock(&svc->srv_lock);
312 list_del(&thread->t_link);
313 OBD_FREE(thread, sizeof(*thread));
315 spin_unlock(&svc->srv_lock);
318 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
321 struct ptlrpc_svc_data d;
322 struct ptlrpc_thread *thread;
326 OBD_ALLOC(thread, sizeof(*thread));
327 if (thread == NULL) {
331 init_waitqueue_head(&thread->t_ctl_waitq);
338 spin_lock(&svc->srv_lock);
339 list_add(&thread->t_link, &svc->srv_threads);
340 spin_unlock(&svc->srv_lock);
342 rc = kernel_thread(ptlrpc_main, (void *) &d,
343 CLONE_VM | CLONE_FS | CLONE_FILES);
345 CERROR("cannot start thread\n");
348 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
353 int ptlrpc_unregister_service(struct ptlrpc_service *service)
357 for (i = 0; i < service->srv_ring_length; i++) {
358 if (ptl_is_valid_handle(&(service->srv_me_h[i]))) {
359 rc = PtlMEUnlink(service->srv_me_h[i]);
361 CERROR("PtlMEUnlink failed: %d\n", rc);
362 ptl_set_inv_handle(&(service->srv_me_h[i]));
365 if (service->srv_buf[i] != NULL)
366 OBD_FREE(service->srv_buf[i], service->srv_buf_size);
367 service->srv_buf[i] = NULL;
370 rc = PtlEQFree(service->srv_eq_h);
372 CERROR("PtlEQFree failed: %d\n", rc);
374 if (!list_empty(&service->srv_reqs)) {
375 // XXX reply with errors and clean up
376 CERROR("Request list not empty!\n");
379 OBD_FREE(service, sizeof(*service));