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 struct obd_uuid *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->uuid, &service->srv_self);
96 CERROR("%s: cannot get peer for uuid '%s'\n", name,
98 OBD_FREE(service, sizeof(*service));
102 rc = PtlEQAlloc(service->srv_self.peer_ni, nevents,
103 request_in_callback, &(service->srv_eq_h));
106 CERROR("%s: PtlEQAlloc failed: %d\n", name, rc);
107 OBD_FREE(service, sizeof(*service));
111 for (i = 0; i < nbufs; i++) {
112 struct ptlrpc_request_buffer_desc *rqbd;
114 OBD_ALLOC(rqbd, sizeof(*rqbd));
118 rqbd->rqbd_service = service;
119 ptl_set_inv_handle(&rqbd->rqbd_me_h);
120 atomic_set(&rqbd->rqbd_refcount, 0);
121 OBD_ALLOC(rqbd->rqbd_buffer, service->srv_buf_size);
122 if (rqbd->rqbd_buffer == NULL) {
123 OBD_FREE(rqbd, sizeof(*rqbd));
126 list_add(&rqbd->rqbd_list, &service->srv_rqbds);
127 service->srv_nrqbds++;
129 ptlrpc_link_svc_me(rqbd);
132 CDEBUG(D_NET, "Starting service listening on portal %d (eq: %lu)\n",
133 service->srv_req_portal, service->srv_eq_h.handle_idx);
137 ptlrpc_unregister_service(service);
141 static int handle_incoming_request(struct obd_device *obddev,
142 struct ptlrpc_service *svc,
144 struct ptlrpc_request *request)
146 struct ptlrpc_request_buffer_desc *rqbd = event->mem_desc.user_ptr;
149 /* FIXME: If we move to an event-driven model, we should put the request
150 * on the stack of mds_handle instead. */
152 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
153 LASSERT ((event->mem_desc.options & PTL_MD_IOV) == 0);
154 LASSERT (rqbd->rqbd_service == svc);
155 LASSERT (rqbd->rqbd_buffer == event->mem_desc.start);
156 LASSERT (event->offset + event->mlength <= svc->srv_buf_size);
158 memset(request, 0, sizeof(*request));
159 request->rq_svc = svc;
160 request->rq_obd = obddev;
161 request->rq_xid = event->match_bits;
162 request->rq_reqmsg = event->mem_desc.start + event->offset;
163 request->rq_reqlen = event->mlength;
167 if (request->rq_reqlen < sizeof(struct lustre_msg)) {
168 CERROR("incomplete request (%d): ptl %d from "LPX64" xid "
170 request->rq_reqlen, svc->srv_req_portal,
171 event->initiator.nid, request->rq_xid);
175 CDEBUG(D_RPCTRACE, "Handling RPC pid:xid:nid:opc %d:"LPU64":"LPX64":%d\n",
176 NTOH__u32(request->rq_reqmsg->status),
178 event->initiator.nid,
179 NTOH__u32(request->rq_reqmsg->opc));
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" (md: %p + %d)\n", request->rq_xid,
204 event->mem_desc.start, event->offset);
206 request->rq_peer.peer_nid = event->initiator.nid;
207 /* FIXME: this NI should be the incoming NI.
208 * We don't know how to find that from here. */
209 request->rq_peer.peer_ni = svc->srv_self.peer_ni;
211 request->rq_export = class_conn2export((struct lustre_handle *)
214 if (request->rq_export) {
215 request->rq_connection = request->rq_export->exp_connection;
216 ptlrpc_connection_addref(request->rq_connection);
218 /* create a (hopefully temporary) connection that will be used
219 * to send the reply if this call doesn't create an export.
220 * XXX revisit this when we revamp ptlrpc */
221 request->rq_connection =
222 ptlrpc_get_connection(&request->rq_peer, NULL);
225 rc = svc->srv_handler(request);
226 ptlrpc_put_connection(request->rq_connection);
229 if (atomic_dec_and_test (&rqbd->rqbd_refcount)) /* last reference? */
230 ptlrpc_link_svc_me (rqbd);
235 /* Don't use daemonize, it removes fs struct from new thread (bug 418) */
236 static void ptlrpc_daemonize(void)
240 current->session = 1;
248 static int ptlrpc_main(void *arg)
250 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
251 struct obd_device *obddev = data->dev;
252 struct ptlrpc_service *svc = data->svc;
253 struct ptlrpc_thread *thread = data->thread;
254 struct ptlrpc_request *request;
263 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
264 sigfillset(¤t->blocked);
267 spin_lock_irqsave(¤t->sigmask_lock, flags);
268 sigfillset(¤t->blocked);
269 recalc_sigpending(current);
270 spin_unlock_irqrestore(¤t->sigmask_lock, flags);
274 sprintf(current->comm, "%s|%d", data->name,current->thread.extern_pid);
276 strcpy(current->comm, data->name);
280 OBD_ALLOC(event, sizeof(*event));
282 GOTO(out, rc = -ENOMEM);
283 OBD_ALLOC(request, sizeof(*request));
285 GOTO(out_event, rc = -ENOMEM);
287 /* Record that the thread is running */
288 thread->t_flags = SVC_RUNNING;
289 wake_up(&thread->t_ctl_waitq);
291 /* XXX maintain a list of all managed devices: insert here */
293 /* And now, loop forever on requests */
295 wait_event(svc->srv_waitq,
296 ptlrpc_check_event(svc, thread, event));
298 if (thread->t_flags & SVC_STOPPING) {
299 spin_lock(&svc->srv_lock);
300 thread->t_flags &= ~SVC_STOPPING;
301 spin_unlock(&svc->srv_lock);
307 if (thread->t_flags & SVC_EVENT) {
308 spin_lock(&svc->srv_lock);
309 thread->t_flags &= ~SVC_EVENT;
310 spin_unlock(&svc->srv_lock);
312 rc = handle_incoming_request(obddev, svc, event,
317 CERROR("unknown break in service");
323 OBD_FREE(request, sizeof(*request));
325 OBD_FREE(event, sizeof(*event));
327 thread->t_flags = SVC_STOPPED;
328 wake_up(&thread->t_ctl_waitq);
330 CDEBUG(D_NET, "service thread exiting, process %d: rc = %d\n",
335 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
336 struct ptlrpc_thread *thread)
338 spin_lock(&svc->srv_lock);
339 thread->t_flags = SVC_STOPPING;
340 spin_unlock(&svc->srv_lock);
342 wake_up(&svc->srv_waitq);
343 wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
346 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
348 spin_lock(&svc->srv_lock);
349 while (!list_empty(&svc->srv_threads)) {
350 struct ptlrpc_thread *thread;
351 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
353 spin_unlock(&svc->srv_lock);
354 ptlrpc_stop_thread(svc, thread);
355 spin_lock(&svc->srv_lock);
356 list_del(&thread->t_link);
357 OBD_FREE(thread, sizeof(*thread));
359 spin_unlock(&svc->srv_lock);
362 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
365 struct ptlrpc_svc_data d;
366 struct ptlrpc_thread *thread;
370 OBD_ALLOC(thread, sizeof(*thread));
371 if (thread == NULL) {
375 init_waitqueue_head(&thread->t_ctl_waitq);
382 spin_lock(&svc->srv_lock);
383 list_add(&thread->t_link, &svc->srv_threads);
384 spin_unlock(&svc->srv_lock);
386 /* CLONE_VM and CLONE_FILES just avoid a needless copy, because we
387 * just drop the VM and FILES in ptlrpc_daemonize() right away.
389 rc = kernel_thread(ptlrpc_main, (void *) &d, CLONE_VM | CLONE_FILES);
391 CERROR("cannot start thread\n");
392 OBD_FREE(thread, sizeof(*thread));
395 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
400 int ptlrpc_unregister_service(struct ptlrpc_service *service)
404 LASSERT (list_empty (&service->srv_threads));
406 /* XXX We could reply (with failure) to all buffered requests
407 * _after_ unlinking _all_ the request buffers, but _before_
411 while (!list_empty (&service->srv_rqbds)) {
412 struct ptlrpc_request_buffer_desc *rqbd =
413 list_entry (service->srv_rqbds.next,
414 struct ptlrpc_request_buffer_desc,
417 list_del (&rqbd->rqbd_list);
419 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
420 /* refcount could be anything; it's possible for the
421 * buffers to continued to get filled after all the server
422 * threads exited. But we know they _have_ exited.
425 (void) PtlMEUnlink(rqbd->rqbd_me_h);
426 /* The callback handler could have unlinked this ME already
427 * (we're racing with her) but it's safe to ensure it _has_
431 OBD_FREE (rqbd->rqbd_buffer, service->srv_buf_size);
432 OBD_FREE (rqbd, sizeof (*rqbd));
433 service->srv_nrqbds--;
436 LASSERT (service->srv_nrqbds == 0);
438 rc = PtlEQFree(service->srv_eq_h);
440 CERROR("PtlEQFree failed: %d\n", rc);
442 OBD_FREE(service, sizeof(*service));