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 LASSERT ((event->mem_desc.options & PTL_MD_IOV) == 0);
142 start = event->mem_desc.start;
144 memset(&request, 0, sizeof(request));
145 request.rq_svc = svc;
146 request.rq_obd = obddev;
147 request.rq_xid = event->match_bits;
148 request.rq_reqmsg = event->mem_desc.start + event->offset;
149 request.rq_reqlen = event->mem_desc.length;
151 if (request.rq_reqlen < sizeof(struct lustre_msg)) {
152 CERROR("incomplete request (%d): ptl %d from %Lx xid %Ld\n",
153 request.rq_reqlen, svc->srv_req_portal,
154 event->initiator.nid, request.rq_xid);
155 spin_unlock(&svc->srv_lock);
159 if (request.rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
160 CERROR("wrong lustre_msg magic %d: ptl %d from %Lx xid %Ld\n",
161 request.rq_reqmsg->magic, svc->srv_req_portal,
162 event->initiator.nid, request.rq_xid);
163 spin_unlock(&svc->srv_lock);
167 if (request.rq_reqmsg->version != PTLRPC_MSG_VERSION) {
168 CERROR("wrong lustre_msg version %d: ptl %d from %Lx xid %Ld\n",
169 request.rq_reqmsg->version, svc->srv_req_portal,
170 event->initiator.nid, request.rq_xid);
171 spin_unlock(&svc->srv_lock);
175 CDEBUG(D_NET, "got req %Ld\n", request.rq_xid);
177 peer.peer_nid = event->initiator.nid;
178 /* FIXME: this NI should be the incoming NI.
179 * We don't know how to find that from here. */
180 peer.peer_ni = svc->srv_self.peer_ni;
182 request.rq_export = class_conn2export((struct lustre_handle *) request.rq_reqmsg);
184 if (request.rq_export) {
185 request.rq_connection = request.rq_export->exp_connection;
186 ptlrpc_connection_addref(request.rq_connection);
188 request.rq_connection = ptlrpc_get_connection(&peer);
191 spin_unlock(&svc->srv_lock);
192 rc = svc->srv_handler(&request);
193 ptlrpc_put_connection(request.rq_connection);
194 ptl_handled_rpc(svc, start);
198 void ptlrpc_rotate_reqbufs(struct ptlrpc_service *service,
203 LASSERT ((ev->mem_desc.options & PTL_MD_IOV) == 0);
205 for (index = 0; index < service->srv_ring_length; index++)
206 if (service->srv_buf[index] == ev->mem_desc.start)
209 if (index == service->srv_ring_length)
212 service->srv_ref_count[index]++;
214 if (ptl_is_valid_handle(&ev->unlinked_me)) {
217 for (idx = 0; idx < service->srv_ring_length; idx++)
218 if (service->srv_me_h[idx].handle_idx ==
219 ev->unlinked_me.handle_idx)
221 if (idx == service->srv_ring_length)
224 CDEBUG(D_NET, "unlinked %d\n", idx);
225 ptl_set_inv_handle(&(service->srv_me_h[idx]));
227 if (service->srv_ref_count[idx] == 0)
228 ptlrpc_link_svc_me(service, idx);
232 static int ptlrpc_main(void *arg)
235 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
236 struct obd_device *obddev = data->dev;
237 struct ptlrpc_service *svc = data->svc;
238 struct ptlrpc_thread *thread = data->thread;
244 spin_lock_irq(¤t->sigmask_lock);
245 sigfillset(¤t->blocked);
246 recalc_sigpending(current);
247 spin_unlock_irq(¤t->sigmask_lock);
249 sprintf(current->comm, data->name);
251 /* Record that the thread is running */
252 thread->t_flags = SVC_RUNNING;
253 wake_up(&thread->t_ctl_waitq);
255 /* XXX maintain a list of all managed devices: insert here */
257 /* And now, loop forever on requests */
261 wait_event(svc->srv_waitq,
262 ptlrpc_check_event(svc, thread, &event));
264 spin_lock(&svc->srv_lock);
266 if (thread->t_flags & SVC_STOPPING) {
267 thread->t_flags &= ~SVC_STOPPING;
268 spin_unlock(&svc->srv_lock);
273 if (thread->t_flags & SVC_EVENT) {
274 thread->t_flags &= ~SVC_EVENT;
275 ptlrpc_rotate_reqbufs(svc, &event);
277 rc = handle_incoming_request(obddev, svc, &event);
278 thread->t_flags &= ~SVC_EVENT;
282 CERROR("unknown break in service");
283 spin_unlock(&svc->srv_lock);
288 thread->t_flags = SVC_STOPPED;
289 wake_up(&thread->t_ctl_waitq);
290 CDEBUG(D_NET, "service thread exiting, process %d\n", current->pid);
294 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
295 struct ptlrpc_thread *thread)
297 spin_lock(&svc->srv_lock);
298 thread->t_flags = SVC_STOPPING;
299 spin_unlock(&svc->srv_lock);
301 wake_up(&svc->srv_waitq);
302 wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
305 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
307 spin_lock(&svc->srv_lock);
308 while (!list_empty(&svc->srv_threads)) {
309 struct ptlrpc_thread *thread;
310 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
312 spin_unlock(&svc->srv_lock);
313 ptlrpc_stop_thread(svc, thread);
314 spin_lock(&svc->srv_lock);
315 list_del(&thread->t_link);
316 OBD_FREE(thread, sizeof(*thread));
318 spin_unlock(&svc->srv_lock);
321 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
324 struct ptlrpc_svc_data d;
325 struct ptlrpc_thread *thread;
329 OBD_ALLOC(thread, sizeof(*thread));
330 if (thread == NULL) {
334 init_waitqueue_head(&thread->t_ctl_waitq);
341 spin_lock(&svc->srv_lock);
342 list_add(&thread->t_link, &svc->srv_threads);
343 spin_unlock(&svc->srv_lock);
345 rc = kernel_thread(ptlrpc_main, (void *) &d,
346 CLONE_VM | CLONE_FS | CLONE_FILES);
348 CERROR("cannot start thread\n");
351 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
356 int ptlrpc_unregister_service(struct ptlrpc_service *service)
360 for (i = 0; i < service->srv_ring_length; i++) {
361 if (ptl_is_valid_handle(&(service->srv_me_h[i]))) {
362 rc = PtlMEUnlink(service->srv_me_h[i]);
364 CERROR("PtlMEUnlink failed: %d\n", rc);
365 ptl_set_inv_handle(&(service->srv_me_h[i]));
368 if (service->srv_buf[i] != NULL)
369 OBD_FREE(service->srv_buf[i], service->srv_buf_size);
370 service->srv_buf[i] = NULL;
373 rc = PtlEQFree(service->srv_eq_h);
375 CERROR("PtlEQFree failed: %d\n", rc);
377 if (!list_empty(&service->srv_reqs)) {
378 // XXX reply with errors and clean up
379 CERROR("Request list not empty!\n");
382 OBD_FREE(service, sizeof(*service));