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: %p)\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 if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
175 CERROR("wrong packet type received (type=%u)\n",
176 request->rq_reqmsg->type);
180 if (request->rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
181 CERROR("wrong lustre_msg magic %d: ptl %d from "LPX64" xid "
183 request->rq_reqmsg->magic, svc->srv_req_portal,
184 event->initiator.nid, request->rq_xid);
188 if (request->rq_reqmsg->version != PTLRPC_MSG_VERSION) {
189 CERROR("wrong lustre_msg version %d: ptl %d from "LPX64" xid "
191 request->rq_reqmsg->version, svc->srv_req_portal,
192 event->initiator.nid, request->rq_xid);
196 CDEBUG(D_NET, "got req "LPD64" (md: %p + %d)\n", request->rq_xid,
197 event->mem_desc.start, event->offset);
199 request->rq_peer.peer_nid = event->initiator.nid;
200 /* FIXME: this NI should be the incoming NI.
201 * We don't know how to find that from here. */
202 request->rq_peer.peer_ni = svc->srv_self.peer_ni;
204 request->rq_export = class_conn2export((struct lustre_handle *)
207 if (request->rq_export) {
208 request->rq_connection = request->rq_export->exp_connection;
209 ptlrpc_connection_addref(request->rq_connection);
211 /* create a (hopefully temporary) connection that will be used
212 * to send the reply if this call doesn't create an export.
213 * XXX revisit this when we revamp ptlrpc */
214 request->rq_connection =
215 ptlrpc_get_connection(&request->rq_peer, NULL);
218 rc = svc->srv_handler(request);
219 ptlrpc_put_connection(request->rq_connection);
222 if (atomic_dec_and_test (&rqbd->rqbd_refcount)) /* last reference? */
223 ptlrpc_link_svc_me (rqbd);
228 static int ptlrpc_main(void *arg)
230 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
231 struct obd_device *obddev = data->dev;
232 struct ptlrpc_service *svc = data->svc;
233 struct ptlrpc_thread *thread = data->thread;
234 struct ptlrpc_request *request;
242 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
243 sigfillset(¤t->blocked);
246 spin_lock_irq(¤t->sigmask_lock);
247 sigfillset(¤t->blocked);
248 recalc_sigpending(current);
249 spin_unlock_irq(¤t->sigmask_lock);
252 strcpy(current->comm, data->name);
255 OBD_ALLOC(event, sizeof(*event));
257 GOTO(out, rc = -ENOMEM);
258 OBD_ALLOC(request, sizeof(*request));
260 GOTO(out_event, rc = -ENOMEM);
262 /* Record that the thread is running */
263 thread->t_flags = SVC_RUNNING;
264 wake_up(&thread->t_ctl_waitq);
266 /* XXX maintain a list of all managed devices: insert here */
268 /* And now, loop forever on requests */
270 wait_event(svc->srv_waitq,
271 ptlrpc_check_event(svc, thread, event));
273 if (thread->t_flags & SVC_STOPPING) {
274 spin_lock(&svc->srv_lock);
275 thread->t_flags &= ~SVC_STOPPING;
276 spin_unlock(&svc->srv_lock);
282 if (thread->t_flags & SVC_EVENT) {
283 spin_lock(&svc->srv_lock);
284 thread->t_flags &= ~SVC_EVENT;
285 spin_unlock(&svc->srv_lock);
287 rc = handle_incoming_request(obddev, svc, event,
292 CERROR("unknown break in service");
298 OBD_FREE(request, sizeof(*request));
300 OBD_FREE(event, sizeof(*event));
302 thread->t_flags = SVC_STOPPED;
303 wake_up(&thread->t_ctl_waitq);
305 CDEBUG(D_NET, "service thread exiting, process %d: rc = %d\n",
310 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
311 struct ptlrpc_thread *thread)
313 spin_lock(&svc->srv_lock);
314 thread->t_flags = SVC_STOPPING;
315 spin_unlock(&svc->srv_lock);
317 wake_up(&svc->srv_waitq);
318 wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
321 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
323 spin_lock(&svc->srv_lock);
324 while (!list_empty(&svc->srv_threads)) {
325 struct ptlrpc_thread *thread;
326 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
328 spin_unlock(&svc->srv_lock);
329 ptlrpc_stop_thread(svc, thread);
330 spin_lock(&svc->srv_lock);
331 list_del(&thread->t_link);
332 OBD_FREE(thread, sizeof(*thread));
334 spin_unlock(&svc->srv_lock);
337 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
340 struct ptlrpc_svc_data d;
341 struct ptlrpc_thread *thread;
345 OBD_ALLOC(thread, sizeof(*thread));
346 if (thread == NULL) {
350 init_waitqueue_head(&thread->t_ctl_waitq);
357 spin_lock(&svc->srv_lock);
358 list_add(&thread->t_link, &svc->srv_threads);
359 spin_unlock(&svc->srv_lock);
361 /* XXX should we really be cloning open file handles here? */
362 rc = kernel_thread(ptlrpc_main, (void *) &d,
363 CLONE_VM | CLONE_FS | CLONE_FILES);
365 CERROR("cannot start thread\n");
366 OBD_FREE(thread, sizeof(*thread));
369 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
374 int ptlrpc_unregister_service(struct ptlrpc_service *service)
378 LASSERT (list_empty (&service->srv_threads));
380 /* XXX We could reply (with failure) to all buffered requests
381 * _after_ unlinking _all_ the request buffers, but _before_
385 while (!list_empty (&service->srv_rqbds)) {
386 struct ptlrpc_request_buffer_desc *rqbd =
387 list_entry (service->srv_rqbds.next,
388 struct ptlrpc_request_buffer_desc,
391 list_del (&rqbd->rqbd_list);
393 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
394 /* refcount could be anything; it's possible for the
395 * buffers to continued to get filled after all the server
396 * threads exited. But we know they _have_ exited.
399 (void) PtlMEUnlink(rqbd->rqbd_me_h);
400 /* The callback handler could have unlinked this ME already
401 * (we're racing with her) but it's safe to ensure it _has_
405 OBD_FREE (rqbd->rqbd_buffer, service->srv_buf_size);
406 OBD_FREE (rqbd, sizeof (*rqbd));
407 service->srv_nrqbds--;
410 LASSERT (service->srv_nrqbds == 0);
412 rc = PtlEQFree(service->srv_eq_h);
414 CERROR("PtlEQFree failed: %d\n", rc);
416 OBD_FREE(service, sizeof(*service));