Whamcloud - gitweb
merge b_md onto HEAD. as best as I can remember:
[fs/lustre-release.git] / lustre / ptlrpc / service.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  */
22
23 #define DEBUG_SUBSYSTEM S_RPC
24
25 #include <linux/obd_support.h>
26 #include <linux/obd_class.h>
27 #include <linux/lustre_net.h>
28
29 extern int request_in_callback(ptl_event_t *ev);
30
31 static int ptlrpc_check_event(struct ptlrpc_service *svc,
32                               struct ptlrpc_thread *thread, ptl_event_t *event)
33 {
34         int rc;
35         ENTRY;
36
37         spin_lock(&svc->srv_lock);
38
39         if (thread->t_flags & SVC_STOPPING)
40                 GOTO(out, rc = 1);
41
42         LASSERT ((thread->t_flags & SVC_EVENT) == 0);
43         LASSERT (ptl_is_valid_handle (&svc->srv_eq_h));
44
45         rc = PtlEQGet(svc->srv_eq_h, event);
46         switch (rc)
47         {
48         case PTL_OK:
49                 thread->t_flags |= SVC_EVENT;
50                 GOTO(out, rc = 1);
51                 
52         case PTL_EQ_EMPTY:
53                 GOTO(out, rc = 0);
54                 
55         default:
56                 CERROR("BUG: PtlEQGet returned %d\n", rc);
57                 LBUG();
58         }
59  out:
60         spin_unlock(&svc->srv_lock);
61         return rc;
62 }
63
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)
69 {
70         int err;
71         int rc, i;
72         struct ptlrpc_service *service;
73         ENTRY;
74
75         OBD_ALLOC(service, sizeof(*service));
76         if (!service)
77                 RETURN(NULL);
78
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);
83
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);
89
90         service->srv_rep_portal = rep_portal;
91         service->srv_req_portal = req_portal;
92         service->srv_handler = handler;
93
94         err = kportal_uuid_to_peer(uuid, &service->srv_self);
95         if (err) {
96                 CERROR("%s: cannot get peer for uuid '%s'\n", name, uuid);
97                 OBD_FREE(service, sizeof(*service));
98                 RETURN(NULL);
99         }
100
101         rc = PtlEQAlloc(service->srv_self.peer_ni, nevents,
102                         request_in_callback, &(service->srv_eq_h));
103
104         if (rc != PTL_OK) {
105                 CERROR("%s: PtlEQAlloc failed: %d\n", name, rc);
106                 OBD_FREE(service, sizeof(*service));
107                 RETURN(NULL);
108         }
109
110         for (i = 0; i < nbufs; i++) {
111                 struct ptlrpc_request_buffer_desc *rqbd;
112
113                 OBD_ALLOC(rqbd, sizeof(*rqbd));
114                 if (rqbd == NULL)
115                         GOTO(failed, NULL);
116
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));
123                         GOTO(failed, NULL);
124                 }
125                 list_add(&rqbd->rqbd_list, &service->srv_rqbds);
126                 service->srv_nrqbds++;
127
128                 ptlrpc_link_svc_me(rqbd);
129         }
130
131         CDEBUG(D_NET, "Starting service listening on portal %d (eq: %p)\n",
132                service->srv_req_portal, service->srv_eq_h.handle_idx);
133
134         RETURN(service);
135 failed:
136         ptlrpc_unregister_service(service);
137         return NULL;
138 }
139
140 static int handle_incoming_request(struct obd_device *obddev,
141                                    struct ptlrpc_service *svc,
142                                    ptl_event_t *event,
143                                    struct ptlrpc_request *request)
144 {
145         struct ptlrpc_request_buffer_desc *rqbd = event->mem_desc.user_ptr;
146         int rc;
147
148         /* FIXME: If we move to an event-driven model, we should put the request
149          * on the stack of mds_handle instead. */
150
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);
156
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;
163
164         rc = -EINVAL;
165
166         if (request->rq_reqlen < sizeof(struct lustre_msg)) {
167                 CERROR("incomplete request (%d): ptl %d from "LPX64" xid "
168                        LPD64"\n",
169                        request->rq_reqlen, svc->srv_req_portal,
170                        event->initiator.nid, request->rq_xid);
171                 goto out;
172         }
173
174         CDEBUG(D_RPCTRACE, "Handling RPC pid:xid:nid:opc %d:"
175                LPX64":%x:%d\n", 
176                NTOH__u32(request->rq_reqmsg->status), 
177                request->rq_xid,
178                event->initiator.nid,
179                NTOH__u32(request->rq_reqmsg->opc));
180
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);
184                 goto out;
185         }
186
187         if (request->rq_reqmsg->magic != PTLRPC_MSG_MAGIC) {
188                 CERROR("wrong lustre_msg magic %d: ptl %d from "LPX64" xid "
189                        LPD64"\n",
190                        request->rq_reqmsg->magic, svc->srv_req_portal,
191                        event->initiator.nid, request->rq_xid);
192                 goto out;
193         }
194
195         if (request->rq_reqmsg->version != PTLRPC_MSG_VERSION) {
196                 CERROR("wrong lustre_msg version %d: ptl %d from "LPX64" xid "
197                        LPD64"\n",
198                        request->rq_reqmsg->version, svc->srv_req_portal,
199                        event->initiator.nid, request->rq_xid);
200                 goto out;
201         }
202
203         CDEBUG(D_NET, "got req "LPD64" (md: %p + %d)\n", request->rq_xid,
204                event->mem_desc.start, event->offset);
205
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;
210
211         request->rq_export = class_conn2export((struct lustre_handle *)
212                                                request->rq_reqmsg);
213
214         if (request->rq_export) {
215                 request->rq_connection = request->rq_export->exp_connection;
216                 ptlrpc_connection_addref(request->rq_connection);
217         } else {
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);
223         }
224
225         rc = svc->srv_handler(request);
226         ptlrpc_put_connection(request->rq_connection);
227
228  out:
229         if (atomic_dec_and_test (&rqbd->rqbd_refcount)) /* last reference? */
230                 ptlrpc_link_svc_me (rqbd);
231
232         return rc;
233 }
234
235 static int ptlrpc_main(void *arg)
236 {
237         struct ptlrpc_svc_data *data = (struct ptlrpc_svc_data *)arg;
238         struct obd_device *obddev = data->dev;
239         struct ptlrpc_service *svc = data->svc;
240         struct ptlrpc_thread *thread = data->thread;
241         struct ptlrpc_request *request;
242         ptl_event_t *event;
243         int rc = 0;
244
245         ENTRY;
246
247         lock_kernel();
248         daemonize();
249 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0)
250         sigfillset(&current->blocked);
251         recalc_sigpending();
252 #else
253         spin_lock_irq(&current->sigmask_lock);
254         sigfillset(&current->blocked);
255         recalc_sigpending(current);
256         spin_unlock_irq(&current->sigmask_lock);
257 #endif
258
259 #ifdef __arch_um__
260         sprintf(current->comm, "%s|%d", 
261                 data->name, current->thread.extern_pid);
262 #else
263         strcpy(current->comm, data->name);
264 #endif
265         unlock_kernel();
266
267         OBD_ALLOC(event, sizeof(*event));
268         if (!event)
269                 GOTO(out, rc = -ENOMEM);
270         OBD_ALLOC(request, sizeof(*request));
271         if (!request)
272                 GOTO(out_event, rc = -ENOMEM);
273
274         /* Record that the thread is running */
275         thread->t_flags = SVC_RUNNING;
276         wake_up(&thread->t_ctl_waitq);
277
278         /* XXX maintain a list of all managed devices: insert here */
279
280         /* And now, loop forever on requests */
281         while (1) {
282                 wait_event(svc->srv_waitq,
283                            ptlrpc_check_event(svc, thread, event));
284
285                 if (thread->t_flags & SVC_STOPPING) {
286                         spin_lock(&svc->srv_lock);
287                         thread->t_flags &= ~SVC_STOPPING;
288                         spin_unlock(&svc->srv_lock);
289
290                         EXIT;
291                         break;
292                 }
293
294                 if (thread->t_flags & SVC_EVENT) {
295                         spin_lock(&svc->srv_lock);
296                         thread->t_flags &= ~SVC_EVENT;
297                         spin_unlock(&svc->srv_lock);
298
299                         rc = handle_incoming_request(obddev, svc, event,
300                                                      request);
301                         continue;
302                 }
303
304                 CERROR("unknown break in service");
305                 LBUG();
306                 EXIT;
307                 break;
308         }
309
310         OBD_FREE(request, sizeof(*request));
311 out_event:
312         OBD_FREE(event, sizeof(*event));
313 out:
314         thread->t_flags = SVC_STOPPED;
315         wake_up(&thread->t_ctl_waitq);
316
317         CDEBUG(D_NET, "service thread exiting, process %d: rc = %d\n",
318                current->pid, rc);
319         return rc;
320 }
321
322 static void ptlrpc_stop_thread(struct ptlrpc_service *svc,
323                                struct ptlrpc_thread *thread)
324 {
325         spin_lock(&svc->srv_lock);
326         thread->t_flags = SVC_STOPPING;
327         spin_unlock(&svc->srv_lock);
328
329         wake_up(&svc->srv_waitq);
330         wait_event(thread->t_ctl_waitq, (thread->t_flags & SVC_STOPPED));
331 }
332
333 void ptlrpc_stop_all_threads(struct ptlrpc_service *svc)
334 {
335         spin_lock(&svc->srv_lock);
336         while (!list_empty(&svc->srv_threads)) {
337                 struct ptlrpc_thread *thread;
338                 thread = list_entry(svc->srv_threads.next, struct ptlrpc_thread,
339                                     t_link);
340                 spin_unlock(&svc->srv_lock);
341                 ptlrpc_stop_thread(svc, thread);
342                 spin_lock(&svc->srv_lock);
343                 list_del(&thread->t_link);
344                 OBD_FREE(thread, sizeof(*thread));
345         }
346         spin_unlock(&svc->srv_lock);
347 }
348
349 int ptlrpc_start_thread(struct obd_device *dev, struct ptlrpc_service *svc,
350                         char *name)
351 {
352         struct ptlrpc_svc_data d;
353         struct ptlrpc_thread *thread;
354         int rc;
355         ENTRY;
356
357         OBD_ALLOC(thread, sizeof(*thread));
358         if (thread == NULL) {
359                 LBUG();
360                 RETURN(-ENOMEM);
361         }
362         init_waitqueue_head(&thread->t_ctl_waitq);
363
364         d.dev = dev;
365         d.svc = svc;
366         d.name = name;
367         d.thread = thread;
368
369         spin_lock(&svc->srv_lock);
370         list_add(&thread->t_link, &svc->srv_threads);
371         spin_unlock(&svc->srv_lock);
372
373         /* XXX should we really be cloning open file handles here? */
374         rc = kernel_thread(ptlrpc_main, (void *) &d,
375                            CLONE_VM | CLONE_FS | CLONE_FILES);
376         if (rc < 0) {
377                 CERROR("cannot start thread\n");
378                 OBD_FREE(thread, sizeof(*thread));
379                 RETURN(rc);
380         }
381         wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING);
382
383         RETURN(0);
384 }
385
386 int ptlrpc_unregister_service(struct ptlrpc_service *service)
387 {
388         int rc;
389
390         LASSERT (list_empty (&service->srv_threads));
391
392         /* XXX We could reply (with failure) to all buffered requests
393          * _after_ unlinking _all_ the request buffers, but _before_
394          * freeing them.
395          */
396
397         while (!list_empty (&service->srv_rqbds)) {
398                 struct ptlrpc_request_buffer_desc *rqbd =
399                         list_entry (service->srv_rqbds.next,
400                                     struct ptlrpc_request_buffer_desc,
401                                     rqbd_list);
402
403                 list_del (&rqbd->rqbd_list);
404
405                 LASSERT (atomic_read (&rqbd->rqbd_refcount) > 0);
406                 /* refcount could be anything; it's possible for the
407                  * buffers to continued to get filled after all the server
408                  * threads exited.  But we know they _have_ exited.
409                  */
410
411                 (void) PtlMEUnlink(rqbd->rqbd_me_h);
412                 /* The callback handler could have unlinked this ME already
413                  * (we're racing with her) but it's safe to ensure it _has_
414                  * been unlinked.
415                  */
416
417                 OBD_FREE (rqbd->rqbd_buffer, service->srv_buf_size);
418                 OBD_FREE (rqbd, sizeof (*rqbd));
419                 service->srv_nrqbds--;
420         }
421
422         LASSERT (service->srv_nrqbds == 0);
423
424         rc = PtlEQFree(service->srv_eq_h);
425         if (rc)
426                 CERROR("PtlEQFree failed: %d\n", rc);
427
428         OBD_FREE(service, sizeof(*service));
429         if (rc)
430                 LBUG();
431         return rc;
432 }