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));
81 service->srv_name = name;
82 spin_lock_init(&service->srv_lock);
83 INIT_LIST_HEAD(&service->srv_threads);
84 init_waitqueue_head(&service->srv_waitq);
86 service->srv_max_req_size = max_req_size;
87 service->srv_buf_size = bufsize;
88 INIT_LIST_HEAD(&service->srv_rqbds);
89 service->srv_nrqbds = 0;
90 atomic_set(&service->srv_nrqbds_receiving, 0);
92 service->srv_rep_portal = rep_portal;
93 service->srv_req_portal = req_portal;
94 service->srv_handler = handler;
96 err = kportal_uuid_to_peer(uuid, &service->srv_self);
98 CERROR("%s: cannot get peer for uuid '%s'\n", name, uuid);
99 OBD_FREE(service, sizeof(*service));
103 rc = PtlEQAlloc(service->srv_self.peer_ni, nevents,
104 request_in_callback, &(service->srv_eq_h));
107 CERROR("%s: PtlEQAlloc failed: %d\n", name, rc);
108 OBD_FREE(service, sizeof(*service));
112 for (i = 0; i < nbufs; i++) {
113 struct ptlrpc_request_buffer_desc *rqbd;
115 OBD_ALLOC(rqbd, sizeof(*rqbd));
119 rqbd->rqbd_service = service;
120 ptl_set_inv_handle(&rqbd->rqbd_me_h);
121 atomic_set(&rqbd->rqbd_refcount, 0);
122 OBD_ALLOC(rqbd->rqbd_buffer, service->srv_buf_size);
123 if (rqbd->rqbd_buffer == NULL) {
124 OBD_FREE(rqbd, sizeof(*rqbd));
127 list_add(&rqbd->rqbd_list, &service->srv_rqbds);
128 service->srv_nrqbds++;
130 ptlrpc_link_svc_me(rqbd);
133 CDEBUG(D_NET, "Starting service listening on portal %d\n",
134 service->srv_req_portal);
138 ptlrpc_unregister_service(service);
142 static int handle_incoming_request(struct obd_device *obddev,
143 struct ptlrpc_service *svc,
145 struct ptlrpc_request *request)
147 struct ptlrpc_request_buffer_desc *rqbd = event->mem_desc.user_ptr;
150 /* FIXME: If we move to an event-driven model, we should put the request
151 * on the stack of mds_handle instead. */
153 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
154 LASSERT ((event->mem_desc.options & PTL_MD_IOV) == 0);
155 LASSERT (rqbd->rqbd_service == svc);
156 LASSERT (rqbd->rqbd_buffer == event->mem_desc.start);
157 LASSERT (event->offset + event->mlength <= svc->srv_buf_size);
159 memset(request, 0, sizeof(*request));
160 request->rq_svc = svc;
161 request->rq_obd = obddev;
162 request->rq_xid = event->match_bits;
163 request->rq_reqmsg = event->mem_desc.start + event->offset;
164 request->rq_reqlen = event->mlength;
168 if (request->rq_reqlen < sizeof(struct lustre_msg)) {
169 CERROR("incomplete request (%d): ptl %d from "LPX64" xid "
171 request->rq_reqlen, svc->srv_req_portal,
172 event->initiator.nid, request->rq_xid);
176 if (NTOH__u32(request->rq_reqmsg->type) != PTL_RPC_MSG_REQUEST) {
177 CERROR("wrong packet type received (type=%u)\n",
178 request->rq_reqmsg->type);
182 if (request->rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
183 CERROR("wrong lustre_msg magic %d: ptl %d from "LPX64" xid "
185 request->rq_reqmsg->magic, svc->srv_req_portal,
186 event->initiator.nid, request->rq_xid);
190 if (request->rq_reqmsg->version != PTLRPC_MSG_VERSION) {
191 CERROR("wrong lustre_msg version %d: ptl %d from "LPX64" xid "
193 request->rq_reqmsg->version, svc->srv_req_portal,
194 event->initiator.nid, request->rq_xid);
198 CDEBUG(D_NET, "got req "LPD64"\n", request->rq_xid);
200 request->rq_peer.peer_nid = event->initiator.nid;
201 /* FIXME: this NI should be the incoming NI.
202 * We don't know how to find that from here. */
203 request->rq_peer.peer_ni = svc->srv_self.peer_ni;
205 request->rq_export = class_conn2export((struct lustre_handle *)
208 if (request->rq_export) {
209 request->rq_connection = request->rq_export->exp_connection;
210 ptlrpc_connection_addref(request->rq_connection);
212 /* create a (hopefully temporary) connection that will be used
213 * to send the reply if this call doesn't create an export.
214 * XXX revisit this when we revamp ptlrpc */
215 request->rq_connection =
216 ptlrpc_get_connection(&request->rq_peer, NULL);
219 rc = svc->srv_handler(request);
220 ptlrpc_put_connection(request->rq_connection);
223 if (atomic_dec_and_test (&rqbd->rqbd_refcount)) /* last reference? */
224 ptlrpc_link_svc_me (rqbd);
229 static int ptlrpc_main(void *arg)
231 struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
232 struct obd_device *obddev = data->dev;
233 struct ptlrpc_service *svc = data->svc;
234 struct ptlrpc_thread *thread = data->thread;
235 struct ptlrpc_request *request;
243 spin_lock_irq(¤t->sigmask_lock);
244 sigfillset(¤t->blocked);
245 recalc_sigpending(current);
246 spin_unlock_irq(¤t->sigmask_lock);
248 strcpy(current->comm, data->name);
251 OBD_ALLOC(event, sizeof(*event));
253 GOTO(out, rc = -ENOMEM);
254 OBD_ALLOC(request, sizeof(*request));
256 GOTO(out_event, rc = -ENOMEM);
258 /* Record that the thread is running */
259 thread->t_flags = SVC_RUNNING;
260 wake_up(&thread->t_ctl_waitq);
262 /* XXX maintain a list of all managed devices: insert here */
264 /* And now, loop forever on requests */
266 wait_event(svc->srv_waitq,
267 ptlrpc_check_event(svc, thread, event));
269 if (thread->t_flags & SVC_STOPPING) {
270 spin_lock(&svc->srv_lock);
271 thread->t_flags &= ~SVC_STOPPING;
272 spin_unlock(&svc->srv_lock);
278 if (thread->t_flags & SVC_EVENT) {
279 spin_lock(&svc->srv_lock);
280 thread->t_flags &= ~SVC_EVENT;
281 spin_unlock(&svc->srv_lock);
283 rc = handle_incoming_request(obddev, svc, event,
288 CERROR("unknown break in service");
294 OBD_FREE(request, sizeof(*request));
296 OBD_FREE(event, sizeof(*event));
298 thread->t_flags = SVC_STOPPED;
299 wake_up(&thread->t_ctl_waitq);
301 CDEBUG(D_NET, "service thread exiting, process %d: rc = %d\n",
306 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
307 struct ptlrpc_thread *thread)
309 spin_lock(&svc->srv_lock);
310 thread->t_flags = SVC_STOPPING;
311 spin_unlock(&svc->srv_lock);
313 wake_up(&svc->srv_waitq);
314 wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
317 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
319 spin_lock(&svc->srv_lock);
320 while (!list_empty(&svc->srv_threads)) {
321 struct ptlrpc_thread *thread;
322 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
324 spin_unlock(&svc->srv_lock);
325 ptlrpc_stop_thread(svc, thread);
326 spin_lock(&svc->srv_lock);
327 list_del(&thread->t_link);
328 OBD_FREE(thread, sizeof(*thread));
330 spin_unlock(&svc->srv_lock);
333 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
336 struct ptlrpc_svc_data d;
337 struct ptlrpc_thread *thread;
341 OBD_ALLOC(thread, sizeof(*thread));
342 if (thread == NULL) {
346 init_waitqueue_head(&thread->t_ctl_waitq);
353 spin_lock(&svc->srv_lock);
354 list_add(&thread->t_link, &svc->srv_threads);
355 spin_unlock(&svc->srv_lock);
357 /* XXX should we really be cloning open file handles here? */
358 rc = kernel_thread(ptlrpc_main, (void *) &d,
359 CLONE_VM | CLONE_FS | CLONE_FILES);
361 CERROR("cannot start thread\n");
362 OBD_FREE(thread, sizeof(*thread));
365 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
370 int ptlrpc_unregister_service(struct ptlrpc_service *service)
374 LASSERT (list_empty (&service->srv_threads));
376 /* XXX We could reply (with failure) to all buffered requests
377 * _after_ unlinking _all_ the request buffers, but _before_
381 while (!list_empty (&service->srv_rqbds)) {
382 struct ptlrpc_request_buffer_desc *rqbd =
383 list_entry (service->srv_rqbds.next,
384 struct ptlrpc_request_buffer_desc,
387 list_del (&rqbd->rqbd_list);
389 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
390 /* refcount could be anything; it's possible for the
391 * buffers to continued to get filled after all the server
392 * threads exited. But we know they _have_ exited.
395 (void) PtlMEUnlink(rqbd->rqbd_me_h);
396 /* The callback handler could have unlinked this ME already
397 * (we're racing with her) but it's safe to ensure it _has_
401 OBD_FREE (rqbd->rqbd_buffer, service->srv_buf_size);
402 OBD_FREE (rqbd, sizeof (*rqbd));
403 service->srv_nrqbds--;
406 LASSERT (service->srv_nrqbds == 0);
408 rc = PtlEQFree(service->srv_eq_h);
410 CERROR("PtlEQFree failed: %d\n", rc);
412 OBD_FREE(service, sizeof(*service));