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);
39 if (thread->t_flags & SVC_STOPPING)
42 LASSERT ((thread->t_flags & SVC_EVENT) == 0);
43 LASSERT (ptl_is_valid_handle (&svc->srv_eq_h));
45 rc = PtlEQGet(svc->srv_eq_h, event);
49 thread->t_flags |= SVC_EVENT;
56 CERROR("BUG: PtlEQGet returned %d\n", rc);
60 spin_unlock(&svc->srv_lock);
64 struct ptlrpc_service *
65 ptlrpc_init_svc(__u32 nevents, __u32 nbufs,
66 __u32 bufsize, __u32 max_req_size,
67 int req_portal, int rep_portal,
68 obd_uuid_t uuid, svc_handler_t handler, char *name)
72 struct ptlrpc_service *service;
75 OBD_ALLOC(service, sizeof(*service));
79 service->srv_name = name;
80 spin_lock_init(&service->srv_lock);
81 INIT_LIST_HEAD(&service->srv_threads);
82 init_waitqueue_head(&service->srv_waitq);
84 service->srv_max_req_size = max_req_size;
85 service->srv_buf_size = bufsize;
86 INIT_LIST_HEAD(&service->srv_rqbds);
87 service->srv_nrqbds = 0;
88 atomic_set(&service->srv_nrqbds_receiving, 0);
90 service->srv_rep_portal = rep_portal;
91 service->srv_req_portal = req_portal;
92 service->srv_handler = handler;
94 err = kportal_uuid_to_peer(uuid, &service->srv_self);
96 CERROR("%s: cannot get peer for uuid '%s'\n", name, uuid);
97 OBD_FREE(service, sizeof(*service));
101 rc = PtlEQAlloc(service->srv_self.peer_ni, nevents,
102 request_in_callback, &(service->srv_eq_h));
105 CERROR("%s: PtlEQAlloc failed: %d\n", name, rc);
106 OBD_FREE(service, sizeof(*service));
110 for (i = 0; i < nbufs; i++) {
111 struct ptlrpc_request_buffer_desc *rqbd;
113 OBD_ALLOC(rqbd, sizeof(*rqbd));
117 rqbd->rqbd_service = service;
118 ptl_set_inv_handle(&rqbd->rqbd_me_h);
119 atomic_set(&rqbd->rqbd_refcount, 0);
120 OBD_ALLOC(rqbd->rqbd_buffer, service->srv_buf_size);
121 if (rqbd->rqbd_buffer == NULL) {
122 OBD_FREE(rqbd, sizeof(*rqbd));
125 list_add(&rqbd->rqbd_list, &service->srv_rqbds);
126 service->srv_nrqbds++;
128 ptlrpc_link_svc_me(rqbd);
131 CDEBUG(D_NET, "Starting service listening on portal %d (eq: %lu)\n",
132 service->srv_req_portal, service->srv_eq_h.handle_idx);
136 ptlrpc_unregister_service(service);
140 static int handle_incoming_request(struct obd_device *obddev,
141 struct ptlrpc_service *svc,
143 struct ptlrpc_request *request)
145 struct ptlrpc_request_buffer_desc *rqbd = event->mem_desc.user_ptr;
148 /* FIXME: If we move to an event-driven model, we should put the request
149 * on the stack of mds_handle instead. */
151 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
152 LASSERT ((event->mem_desc.options & PTL_MD_IOV) == 0);
153 LASSERT (rqbd->rqbd_service == svc);
154 LASSERT (rqbd->rqbd_buffer == event->mem_desc.start);
155 LASSERT (event->offset + event->mlength <= svc->srv_buf_size);
157 memset(request, 0, sizeof(*request));
158 request->rq_svc = svc;
159 request->rq_obd = obddev;
160 request->rq_xid = event->match_bits;
161 request->rq_reqmsg = event->mem_desc.start + event->offset;
162 request->rq_reqlen = event->mlength;
166 if (request->rq_reqlen < sizeof(struct lustre_msg)) {
167 CERROR("incomplete request (%d): ptl %d from "LPX64" xid "
169 request->rq_reqlen, svc->srv_req_portal,
170 event->initiator.nid, request->rq_xid);
174 CDEBUG(D_RPCTRACE, "Handling RPC pid:xid:nid:opc %d:"LPX64":"LPX64":%d\n",
175 NTOH__u32(request->rq_reqmsg->status),
177 event->initiator.nid,
178 NTOH__u32(request->rq_reqmsg->opc));
180 if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
181 CERROR("wrong packet type received (type=%u)\n",
182 request->rq_reqmsg->type);
186 if (request->rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
187 CERROR("wrong lustre_msg magic %d: ptl %d from "LPX64" xid "
189 request->rq_reqmsg->magic, svc->srv_req_portal,
190 event->initiator.nid, request->rq_xid);
194 if (request->rq_reqmsg->version != PTLRPC_MSG_VERSION) {
195 CERROR("wrong lustre_msg version %d: ptl %d from "LPX64" xid "
197 request->rq_reqmsg->version, svc->srv_req_portal,
198 event->initiator.nid, request->rq_xid);
202 CDEBUG(D_NET, "got req "LPD64" (md: %p + %d)\n", request->rq_xid,
203 event->mem_desc.start, event->offset);
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 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
235 static void ptlrpc_daemonize(void)
239 current->session = 1;
247 static int ptlrpc_main(void *arg)
249 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
250 struct obd_device *obddev = data->dev;
251 struct ptlrpc_service *svc = data->svc;
252 struct ptlrpc_thread *thread = data->thread;
253 struct ptlrpc_request *request;
262 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
263 sigfillset(¤t->blocked);
266 spin_lock_irqsave(¤t->sigmask_lock, flags);
267 sigfillset(¤t->blocked);
268 recalc_sigpending(current);
269 spin_unlock_irqrestore(¤t->sigmask_lock, flags);
273 sprintf(current->comm, "%s|%d", data->name,current->thread.extern_pid);
275 strcpy(current->comm, data->name);
279 OBD_ALLOC(event, sizeof(*event));
281 GOTO(out, rc = -ENOMEM);
282 OBD_ALLOC(request, sizeof(*request));
284 GOTO(out_event, rc = -ENOMEM);
286 /* Record that the thread is running */
287 thread->t_flags = SVC_RUNNING;
288 wake_up(&thread->t_ctl_waitq);
290 /* XXX maintain a list of all managed devices: insert here */
292 /* And now, loop forever on requests */
294 wait_event(svc->srv_waitq,
295 ptlrpc_check_event(svc, thread, event));
297 if (thread->t_flags & SVC_STOPPING) {
298 spin_lock(&svc->srv_lock);
299 thread->t_flags &= ~SVC_STOPPING;
300 spin_unlock(&svc->srv_lock);
306 if (thread->t_flags & SVC_EVENT) {
307 spin_lock(&svc->srv_lock);
308 thread->t_flags &= ~SVC_EVENT;
309 spin_unlock(&svc->srv_lock);
311 rc = handle_incoming_request(obddev, svc, event,
316 CERROR("unknown break in service");
322 OBD_FREE(request, sizeof(*request));
324 OBD_FREE(event, sizeof(*event));
326 thread->t_flags = SVC_STOPPED;
327 wake_up(&thread->t_ctl_waitq);
329 CDEBUG(D_NET, "service thread exiting, process %d: rc = %d\n",
334 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
335 struct ptlrpc_thread *thread)
337 spin_lock(&svc->srv_lock);
338 thread->t_flags = SVC_STOPPING;
339 spin_unlock(&svc->srv_lock);
341 wake_up(&svc->srv_waitq);
342 wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
345 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
347 spin_lock(&svc->srv_lock);
348 while (!list_empty(&svc->srv_threads)) {
349 struct ptlrpc_thread *thread;
350 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
352 spin_unlock(&svc->srv_lock);
353 ptlrpc_stop_thread(svc, thread);
354 spin_lock(&svc->srv_lock);
355 list_del(&thread->t_link);
356 OBD_FREE(thread, sizeof(*thread));
358 spin_unlock(&svc->srv_lock);
361 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
364 struct ptlrpc_svc_data d;
365 struct ptlrpc_thread *thread;
369 OBD_ALLOC(thread, sizeof(*thread));
370 if (thread == NULL) {
374 init_waitqueue_head(&thread->t_ctl_waitq);
381 spin_lock(&svc->srv_lock);
382 list_add(&thread->t_link, &svc->srv_threads);
383 spin_unlock(&svc->srv_lock);
385 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
386 * just drop the VM and FILES in ptlrpc_daemonize() right away.
388 rc = kernel_thread(ptlrpc_main, (void *) &d, CLONE_VM | CLONE_FILES);
390 CERROR("cannot start thread\n");
391 OBD_FREE(thread, sizeof(*thread));
394 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
399 int ptlrpc_unregister_service(struct ptlrpc_service *service)
403 LASSERT (list_empty (&service->srv_threads));
405 /* XXX We could reply (with failure) to all buffered requests
406 * _after_ unlinking _all_ the request buffers, but _before_
410 while (!list_empty (&service->srv_rqbds)) {
411 struct ptlrpc_request_buffer_desc *rqbd =
412 list_entry (service->srv_rqbds.next,
413 struct ptlrpc_request_buffer_desc,
416 list_del (&rqbd->rqbd_list);
418 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
419 /* refcount could be anything; it's possible for the
420 * buffers to continued to get filled after all the server
421 * threads exited. But we know they _have_ exited.
424 (void) PtlMEUnlink(rqbd->rqbd_me_h);
425 /* The callback handler could have unlinked this ME already
426 * (we're racing with her) but it's safe to ensure it _has_
430 OBD_FREE (rqbd->rqbd_buffer, service->srv_buf_size);
431 OBD_FREE (rqbd, sizeof (*rqbd));
432 service->srv_nrqbds--;
435 LASSERT (service->srv_nrqbds == 0);
437 rc = PtlEQFree(service->srv_eq_h);
439 CERROR("PtlEQFree failed: %d\n", rc);
441 OBD_FREE(service, sizeof(*service));