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);
31 static int ptlrpc_check_event(struct ptlrpc_service *svc,
32 struct ptlrpc_thread *thread, ptl_event_t *event)
37 spin_lock(&svc->srv_lock);
38 if (thread->t_flags & SVC_STOPPING)
41 LASSERT ((thread->t_flags & SVC_EVENT) == 0);
43 if (ptl_is_valid_handle(&svc->srv_eq_h)) {
45 err = PtlEQGet(svc->srv_eq_h, event);
48 thread->t_flags |= SVC_EVENT;
52 if (err != PTL_EQ_EMPTY) {
53 CERROR("BUG: PtlEQGet returned %d\n", err);
62 spin_unlock(&svc->srv_lock);
66 struct ptlrpc_service *
67 ptlrpc_init_svc(__u32 nevents, __u32 nbufs,
68 __u32 bufsize, __u32 max_req_size,
69 int req_portal, int rep_portal,
70 obd_uuid_t uuid, svc_handler_t handler, char *name)
74 struct ptlrpc_service *service;
77 OBD_ALLOC(service, sizeof(*service));
83 service->srv_name = name;
84 spin_lock_init(&service->srv_lock);
85 INIT_LIST_HEAD(&service->srv_threads);
86 init_waitqueue_head(&service->srv_waitq);
88 service->srv_max_req_size = max_req_size;
89 service->srv_buf_size = bufsize;
90 INIT_LIST_HEAD (&service->srv_rqbds);
91 service->srv_nrqbds = 0;
92 atomic_set (&service->srv_nrqbds_receiving, 0);
94 service->srv_rep_portal = rep_portal;
95 service->srv_req_portal = req_portal;
96 service->srv_handler = handler;
98 err = kportal_uuid_to_peer(uuid, &service->srv_self);
100 CERROR("cannot get peer for uuid '%s'\n", uuid);
101 OBD_FREE(service, sizeof(*service));
105 rc = PtlEQAlloc(service->srv_self.peer_ni, nevents,
106 request_in_callback, &(service->srv_eq_h));
109 CERROR("PtlEQAlloc failed: %d\n", rc);
110 OBD_FREE(service, sizeof(*service));
114 for (i = 0; i < nbufs; i++) {
115 struct ptlrpc_request_buffer_desc *rqbd;
117 OBD_ALLOC (rqbd, sizeof (*rqbd));
119 CERROR ("no memory\n");
123 rqbd->rqbd_service = service;
124 ptl_set_inv_handle (&rqbd->rqbd_me_h);
125 atomic_set (&rqbd->rqbd_refcount, 0);
126 OBD_ALLOC(rqbd->rqbd_buffer, service->srv_buf_size);
127 if (rqbd->rqbd_buffer == NULL) {
128 OBD_FREE (rqbd, sizeof (*rqbd));
129 CERROR("no memory\n");
132 list_add (&rqbd->rqbd_list, &service->srv_rqbds);
133 service->srv_nrqbds++;
135 ptlrpc_link_svc_me(rqbd);
138 CDEBUG(D_NET, "Starting service listening on portal %d\n",
139 service->srv_req_portal);
143 ptlrpc_unregister_service(service);
147 static int handle_incoming_request(struct obd_device *obddev,
148 struct ptlrpc_service *svc,
150 struct ptlrpc_request *request)
152 struct ptlrpc_request_buffer_desc *rqbd = event->mem_desc.user_ptr;
155 /* FIXME: If we move to an event-driven model, we should put the request
156 * on the stack of mds_handle instead. */
158 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
159 LASSERT ((event->mem_desc.options & PTL_MD_IOV) == 0);
160 LASSERT (rqbd->rqbd_service == svc);
161 LASSERT (rqbd->rqbd_buffer == event->mem_desc.start);
162 LASSERT (event->offset + event->mlength <= svc->srv_buf_size);
164 memset(request, 0, sizeof(*request));
165 request->rq_svc = svc;
166 request->rq_obd = obddev;
167 request->rq_xid = event->match_bits;
168 request->rq_reqmsg = event->mem_desc.start + event->offset;
169 request->rq_reqlen = event->mlength;
173 if (request->rq_reqlen < sizeof(struct lustre_msg)) {
174 CERROR("incomplete request (%d): ptl %d from "LPX64" xid "
176 request->rq_reqlen, svc->srv_req_portal,
177 event->initiator.nid, request->rq_xid);
181 if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
182 CERROR("wrong packet type received (type=%u)\n",
183 request->rq_reqmsg->type);
187 if (request->rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
188 CERROR("wrong lustre_msg magic %d: ptl %d from "LPX64" xid "
190 request->rq_reqmsg->magic, svc->srv_req_portal,
191 event->initiator.nid, request->rq_xid);
195 if (request->rq_reqmsg->version != PTLRPC_MSG_VERSION) {
196 CERROR("wrong lustre_msg version %d: ptl %d from "LPX64" xid "
198 request->rq_reqmsg->version, svc->srv_req_portal,
199 event->initiator.nid, request->rq_xid);
203 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
205 request->rq_peer.peer_nid = event->initiator.nid;
206 /* FIXME: this NI should be the incoming NI.
207 * We don't know how to find that from here. */
208 request->rq_peer.peer_ni = svc->srv_self.peer_ni;
210 request->rq_export = class_conn2export((struct lustre_handle *)
213 if (request->rq_export) {
214 request->rq_connection = request->rq_export->exp_connection;
215 ptlrpc_connection_addref(request->rq_connection);
217 /* create a (hopefully temporary) connection that will be used
218 * to send the reply if this call doesn't create an export.
219 * XXX revisit this when we revamp ptlrpc */
220 request->rq_connection =
221 ptlrpc_get_connection(&request->rq_peer, NULL);
224 rc = svc->srv_handler(request);
225 ptlrpc_put_connection(request->rq_connection);
228 if (atomic_dec_and_test (&rqbd->rqbd_refcount)) /* last reference? */
229 ptlrpc_link_svc_me (rqbd);
234 static int ptlrpc_main(void *arg)
236 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
237 struct obd_device *obddev = data->dev;
238 struct ptlrpc_service *svc = data->svc;
239 struct ptlrpc_thread *thread = data->thread;
240 struct ptlrpc_request *request;
248 spin_lock_irq(¤t->sigmask_lock);
249 sigfillset(¤t->blocked);
250 recalc_sigpending(current);
251 spin_unlock_irq(¤t->sigmask_lock);
253 strcpy(current->comm, data->name);
256 OBD_ALLOC(event, sizeof(*event));
258 GOTO(out, rc = -ENOMEM);
259 OBD_ALLOC(request, sizeof(*request));
261 GOTO(out_event, rc = -ENOMEM);
263 /* Record that the thread is running */
264 thread->t_flags = SVC_RUNNING;
265 wake_up(&thread->t_ctl_waitq);
267 /* XXX maintain a list of all managed devices: insert here */
269 /* And now, loop forever on requests */
271 wait_event(svc->srv_waitq,
272 ptlrpc_check_event(svc, thread, event));
274 if (thread->t_flags & SVC_STOPPING) {
275 spin_lock(&svc->srv_lock);
276 thread->t_flags &= ~SVC_STOPPING;
277 spin_unlock(&svc->srv_lock);
283 if (thread->t_flags & SVC_EVENT) {
284 spin_lock(&svc->srv_lock);
285 thread->t_flags &= ~SVC_EVENT;
286 spin_unlock(&svc->srv_lock);
288 rc = handle_incoming_request(obddev, svc, event,
293 CERROR("unknown break in service");
299 OBD_FREE(request, sizeof(*request));
301 OBD_FREE(event, sizeof(*event));
303 thread->t_flags = SVC_STOPPED;
304 wake_up(&thread->t_ctl_waitq);
306 CDEBUG(D_NET, "service thread exiting, process %d: rc = %d\n",
311 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
312 struct ptlrpc_thread *thread)
314 spin_lock(&svc->srv_lock);
315 thread->t_flags = SVC_STOPPING;
316 spin_unlock(&svc->srv_lock);
318 wake_up(&svc->srv_waitq);
319 wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
322 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
324 spin_lock(&svc->srv_lock);
325 while (!list_empty(&svc->srv_threads)) {
326 struct ptlrpc_thread *thread;
327 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
329 spin_unlock(&svc->srv_lock);
330 ptlrpc_stop_thread(svc, thread);
331 spin_lock(&svc->srv_lock);
332 list_del(&thread->t_link);
333 OBD_FREE(thread, sizeof(*thread));
335 spin_unlock(&svc->srv_lock);
338 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
341 struct ptlrpc_svc_data d;
342 struct ptlrpc_thread *thread;
346 OBD_ALLOC(thread, sizeof(*thread));
347 if (thread == NULL) {
351 init_waitqueue_head(&thread->t_ctl_waitq);
358 spin_lock(&svc->srv_lock);
359 list_add(&thread->t_link, &svc->srv_threads);
360 spin_unlock(&svc->srv_lock);
362 /* XXX should we really be cloning open file handles here? */
363 rc = kernel_thread(ptlrpc_main, (void *) &d,
364 CLONE_VM | CLONE_FS | CLONE_FILES);
366 CERROR("cannot start thread\n");
367 OBD_FREE(thread, sizeof(*thread));
370 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
375 int ptlrpc_unregister_service(struct ptlrpc_service *service)
379 LASSERT (list_empty (&service->srv_threads));
381 /* XXX We could reply (with failure) to all buffered requests
382 * _after_ unlinking _all_ the request buffers, but _before_
386 while (!list_empty (&service->srv_rqbds)) {
387 struct ptlrpc_request_buffer_desc *rqbd =
388 list_entry (service->srv_rqbds.next,
389 struct ptlrpc_request_buffer_desc,
392 list_del (&rqbd->rqbd_list);
394 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
395 /* refcount could be anything; it's possible for the
396 * buffers to continued to get filled after all the server
397 * threads exited. But we know they _have_ exited.
400 (void) PtlMEUnlink(rqbd->rqbd_me_h);
401 /* The callback handler could have unlinked this ME already
402 * (we're racing with her) but it's safe to ensure it _has_
406 OBD_FREE (rqbd->rqbd_buffer, service->srv_buf_size);
407 OBD_FREE (rqbd, sizeof (*rqbd));
408 service->srv_nrqbds--;
411 LASSERT (service->srv_nrqbds == 0);
413 rc = PtlEQFree(service->srv_eq_h);
415 CERROR("PtlEQFree failed: %d\n", rc);
417 OBD_FREE(service, sizeof(*service));