Whamcloud - gitweb
Land b_smallfix onto HEAD (20040423_1603)
[fs/lustre-release.git] / lustre / ptlrpc / events.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 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 #ifdef __KERNEL__
26 #include <linux/module.h>
27 #else
28 #include <liblustre.h>
29 #endif
30 #include <linux/obd_class.h>
31 #include <linux/lustre_net.h>
32 #include "ptlrpc_internal.h"
33
34 #if !defined(__KERNEL__) && defined(CRAY_PORTALS)
35 /* forward ref in events.c */
36 static void cray_portals_callback(ptl_event_t *ev);
37 #endif
38
39
40 struct ptlrpc_ni  ptlrpc_interfaces[NAL_MAX_NR];
41 int               ptlrpc_ninterfaces;
42
43 /*  
44  *  Client's outgoing request callback
45  */
46 void request_out_callback(ptl_event_t *ev)
47 {
48         struct ptlrpc_cb_id   *cbid = ev->mem_desc.user_ptr;
49         struct ptlrpc_request *req = cbid->cbid_arg;
50         unsigned long          flags;
51         ENTRY;
52
53         LASSERT (ev->type == PTL_EVENT_SEND_END ||
54                  ev->type == PTL_EVENT_UNLINK);
55         LASSERT (ev->unlinked);
56
57         DEBUG_REQ((ev->ni_fail_type == PTL_NI_OK) ? D_NET : D_ERROR, req,
58                   "type %d, status %d", ev->type, ev->ni_fail_type);
59
60         if (ev->type == PTL_EVENT_UNLINK ||
61             ev->ni_fail_type != PTL_NI_OK) {
62
63                 /* Failed send: make it seem like the reply timed out, just
64                  * like failing sends in client.c does currently...  */
65
66                 spin_lock_irqsave(&req->rq_lock, flags);
67                 req->rq_net_err = 1;
68                 spin_unlock_irqrestore(&req->rq_lock, flags);
69                 
70                 ptlrpc_wake_client_req(req);
71         }
72
73         /* this balances the atomic_inc in ptl_send_rpc() */
74         ptlrpc_req_finished(req);
75         EXIT;
76 }
77
78 /*
79  * Client's incoming reply callback
80  */
81 void reply_in_callback(ptl_event_t *ev)
82 {
83         struct ptlrpc_cb_id   *cbid = ev->mem_desc.user_ptr;
84         struct ptlrpc_request *req = cbid->cbid_arg;
85         unsigned long flags;
86         ENTRY;
87
88         LASSERT (ev->type == PTL_EVENT_PUT_END ||
89                  ev->type == PTL_EVENT_UNLINK);
90         LASSERT (ev->unlinked);
91         LASSERT (ev->mem_desc.start == req->rq_repmsg);
92         LASSERT (ev->offset == 0);
93         LASSERT (ev->mlength <= req->rq_replen);
94         
95         DEBUG_REQ((ev->ni_fail_type == PTL_NI_OK) ? D_NET : D_ERROR, req,
96                   "type %d, status %d", ev->type, ev->ni_fail_type);
97
98         spin_lock_irqsave (&req->rq_lock, flags);
99
100         LASSERT (req->rq_receiving_reply);
101         req->rq_receiving_reply = 0;
102
103         if (ev->type == PTL_EVENT_PUT_END &&
104             ev->ni_fail_type == PTL_NI_OK) {
105                 req->rq_replied = 1;
106                 req->rq_nob_received = ev->mlength;
107         }
108
109         /* NB don't unlock till after wakeup; req can disappear under us
110          * since we don't have our own ref */
111         ptlrpc_wake_client_req(req);
112
113         spin_unlock_irqrestore (&req->rq_lock, flags);
114         EXIT;
115 }
116
117 /* 
118  * Client's bulk has been written/read
119  */
120 void client_bulk_callback (ptl_event_t *ev)
121 {
122         struct ptlrpc_cb_id     *cbid = ev->mem_desc.user_ptr;
123         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
124         unsigned long            flags;
125         ENTRY;
126
127         LASSERT ((desc->bd_type == BULK_PUT_SINK && 
128                   ev->type == PTL_EVENT_PUT_END) ||
129                  (desc->bd_type == BULK_GET_SOURCE &&
130                   ev->type == PTL_EVENT_GET_END) ||
131                  ev->type == PTL_EVENT_UNLINK);
132         LASSERT (ev->unlinked);
133
134         CDEBUG((ev->ni_fail_type == PTL_NI_OK) ? D_NET : D_ERROR,
135                "event type %d, status %d, desc %p\n", 
136                ev->type, ev->ni_fail_type, desc);
137
138         spin_lock_irqsave (&desc->bd_lock, flags);
139
140         LASSERT(desc->bd_network_rw);
141         desc->bd_network_rw = 0;
142
143         if (ev->type != PTL_EVENT_UNLINK &&
144             ev->ni_fail_type == PTL_NI_OK) {
145                 desc->bd_success = 1;
146                 desc->bd_nob_transferred = ev->mlength;
147         }
148
149         /* NB don't unlock till after wakeup; desc can disappear under us
150          * otherwise */
151         ptlrpc_wake_client_req(desc->bd_req);
152
153         spin_unlock_irqrestore (&desc->bd_lock, flags);
154         EXIT;
155 }
156
157 /* 
158  * Server's incoming request callback
159  */
160 void request_in_callback(ptl_event_t *ev)
161 {
162         struct ptlrpc_cb_id               *cbid = ev->mem_desc.user_ptr;
163         struct ptlrpc_request_buffer_desc *rqbd = cbid->cbid_arg;
164         struct ptlrpc_srv_ni              *srv_ni = rqbd->rqbd_srv_ni;
165         struct ptlrpc_service             *service = srv_ni->sni_service;
166         struct ptlrpc_request             *req;
167         char                              str[PTL_NALFMT_SIZE];
168         unsigned long                     flags;
169         ENTRY;
170
171         LASSERT (ev->type == PTL_EVENT_PUT_END ||
172                  ev->type == PTL_EVENT_UNLINK);
173         LASSERT ((char *)ev->mem_desc.start >= rqbd->rqbd_buffer);
174         LASSERT ((char *)ev->mem_desc.start + ev->offset + ev->mlength <=
175                  rqbd->rqbd_buffer + service->srv_buf_size);
176
177         CDEBUG((ev->ni_fail_type == PTL_OK) ? D_NET : D_ERROR,
178                "event type %d, status %d, service %s\n", 
179                ev->type, ev->ni_fail_type, service->srv_name);
180
181         if (ev->unlinked) {
182                 /* If this is the last request message to fit in the
183                  * request buffer we can use the request object embedded in
184                  * rqbd.  Note that if we failed to allocate a request,
185                  * we'd have to re-post the rqbd, which we can't do in this
186                  * context. */
187                 req = &rqbd->rqbd_req;
188                 memset(req, 0, sizeof (*req));
189         } else {
190                 LASSERT (ev->type == PTL_EVENT_PUT_END);
191                 if (ev->ni_fail_type != PTL_NI_OK) {
192                         /* We moaned above already... */
193                         return;
194                 }
195                 OBD_ALLOC_GFP(req, sizeof(*req), GFP_ATOMIC);
196                 if (req == NULL) {
197                         CERROR("Can't allocate incoming request descriptor: "
198                                "Dropping %s RPC from %s\n",
199                                service->srv_name, 
200                                portals_nid2str(srv_ni->sni_ni->pni_number,
201                                                ev->initiator.nid, str));
202                         return;
203                 }
204         }
205
206         /* NB we ABSOLUTELY RELY on req being zeroed, so pointers are NULL,
207          * flags are reset and scalars are zero.  We only set the message
208          * size to non-zero if this was a successful receive. */
209         req->rq_xid = ev->match_bits;
210         req->rq_reqmsg = ev->mem_desc.start + ev->offset;
211         if (ev->type == PTL_EVENT_PUT_END &&
212             ev->ni_fail_type == PTL_NI_OK)
213                 req->rq_reqlen = ev->mlength;
214         do_gettimeofday(&req->rq_arrival_time);
215         req->rq_peer.peer_nid = ev->initiator.nid;
216         req->rq_peer.peer_ni = rqbd->rqbd_srv_ni->sni_ni;
217         req->rq_rqbd = rqbd;
218
219         spin_lock_irqsave (&service->srv_lock, flags);
220
221         if (ev->unlinked) {
222                 srv_ni->sni_nrqbd_receiving--;
223                 if (ev->type != PTL_EVENT_UNLINK &&
224                     srv_ni->sni_nrqbd_receiving == 0) {
225                         /* This service is off-air on this interface because
226                          * all its request buffers are busy.  Portals will
227                          * start dropping incoming requests until more buffers
228                          * get posted.  NB don't moan if it's because we're
229                          * tearing down the service. */
230                         CWARN("All %s %s request buffers busy\n",
231                               service->srv_name, srv_ni->sni_ni->pni_name);
232                 }
233                 /* req takes over the network's ref on rqbd */
234         } else {
235                 /* req takes a ref on rqbd */
236                 rqbd->rqbd_refcount++;
237         }
238
239         list_add_tail(&req->rq_list, &service->srv_request_queue);
240         service->srv_n_queued_reqs++;
241
242         /* NB everything can disappear under us once the request
243          * has been queued and we unlock, so do the wake now... */
244         wake_up(&service->srv_waitq);
245
246         spin_unlock_irqrestore(&service->srv_lock, flags);
247         EXIT;
248 }
249
250 /*  
251  *  Server's outgoing reply callback
252  */
253 void reply_out_callback(ptl_event_t *ev)
254 {
255         struct ptlrpc_cb_id       *cbid = ev->mem_desc.user_ptr;
256         struct ptlrpc_reply_state *rs = cbid->cbid_arg;
257         struct ptlrpc_srv_ni      *sni = rs->rs_srv_ni;
258         struct ptlrpc_service     *svc = sni->sni_service;
259         unsigned long              flags;
260         ENTRY;
261
262         LASSERT (ev->type == PTL_EVENT_SEND_END ||
263                  ev->type == PTL_EVENT_ACK ||
264                  ev->type == PTL_EVENT_UNLINK);
265
266         if (!rs->rs_difficult) {
267                 /* I'm totally responsible for freeing "easy" replies */
268                 LASSERT (ev->unlinked);
269                 lustre_free_reply_state (rs);
270                 atomic_dec (&svc->srv_outstanding_replies);
271                 EXIT;
272                 return;
273         }
274
275         LASSERT (rs->rs_on_net);
276
277         if (ev->unlinked) {
278                 /* Last network callback */
279                 spin_lock_irqsave (&svc->srv_lock, flags);
280                 rs->rs_on_net = 0;
281                 ptlrpc_schedule_difficult_reply (rs);
282                 spin_unlock_irqrestore (&svc->srv_lock, flags);
283         }
284
285         EXIT;
286 }
287
288 /*
289  * Server's bulk completion callback
290  */
291 void server_bulk_callback (ptl_event_t *ev)
292 {
293         struct ptlrpc_cb_id     *cbid = ev->mem_desc.user_ptr;
294         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
295         unsigned long            flags;
296         ENTRY;
297
298         LASSERT (ev->type == PTL_EVENT_SEND_END ||
299                  ev->type == PTL_EVENT_UNLINK ||
300                  (desc->bd_type == BULK_PUT_SOURCE &&
301                   ev->type == PTL_EVENT_ACK) ||
302                  (desc->bd_type == BULK_GET_SINK &&
303                   ev->type == PTL_EVENT_REPLY_END));
304
305         CDEBUG((ev->ni_fail_type == PTL_NI_OK) ? D_NET : D_ERROR,
306                "event type %d, status %d, desc %p\n", 
307                ev->type, ev->ni_fail_type, desc);
308
309         spin_lock_irqsave (&desc->bd_lock, flags);
310         
311         if ((ev->type == PTL_EVENT_ACK ||
312              ev->type == PTL_EVENT_REPLY_END) &&
313             ev->ni_fail_type == PTL_NI_OK) {
314                 /* We heard back from the peer, so even if we get this
315                  * before the SENT event (oh yes we can), we know we
316                  * read/wrote the peer buffer and how much... */
317                 desc->bd_success = 1;
318                 desc->bd_nob_transferred = ev->mlength;
319         }
320
321         if (ev->unlinked) {
322                 /* This is the last callback no matter what... */
323                 desc->bd_network_rw = 0;
324                 wake_up(&desc->bd_waitq);
325         }
326
327         spin_unlock_irqrestore (&desc->bd_lock, flags);
328         EXIT;
329 }
330
331 static void ptlrpc_master_callback(ptl_event_t *ev)
332 {
333         struct ptlrpc_cb_id *cbid = ev->mem_desc.user_ptr;
334         void (*callback)(ptl_event_t *ev) = cbid->cbid_fn;
335
336         /* Honestly, it's best to find out early. */
337         LASSERT (cbid->cbid_arg != LP_POISON);
338         LASSERT (callback == request_out_callback ||
339                  callback == reply_in_callback ||
340                  callback == client_bulk_callback ||
341                  callback == request_in_callback ||
342                  callback == reply_out_callback ||
343                  callback == server_bulk_callback);
344         
345         callback (ev);
346 }
347
348 int ptlrpc_uuid_to_peer (struct obd_uuid *uuid, struct ptlrpc_peer *peer)
349 {
350         struct ptlrpc_ni   *pni;
351         __u32               peer_nal;
352         ptl_nid_t           peer_nid;
353         int                 i;
354         char                str[PTL_NALFMT_SIZE];
355         int                 rc = lustre_uuid_to_peer(uuid->uuid, 
356                                                      &peer_nal, &peer_nid);
357         if (rc != 0)
358                 RETURN (rc);
359
360         for (i = 0; i < ptlrpc_ninterfaces; i++) {
361                 pni = &ptlrpc_interfaces[i];
362
363                 if (pni->pni_number == peer_nal) {
364                         peer->peer_nid = peer_nid;
365                         peer->peer_ni = pni;
366                         return (0);
367                 }
368         }
369
370         CERROR("Can't find ptlrpc interface for NAL %d, NID %s\n",
371                peer_nal, portals_nid2str(peer_nal, peer_nid, str));
372         return (-ENOENT);
373 }
374
375 void ptlrpc_ni_fini(struct ptlrpc_ni *pni)
376 {
377         wait_queue_head_t   waitq;
378         struct l_wait_info  lwi;
379         int                 rc;
380         int                 retries;
381         
382         /* Wait for the event queue to become idle since there may still be
383          * messages in flight with pending events (i.e. the fire-and-forget
384          * messages == client requests and "non-difficult" server
385          * replies */
386
387         for (retries = 0;; retries++) {
388                 rc = PtlEQFree(pni->pni_eq_h);
389                 switch (rc) {
390                 default:
391                         LBUG();
392
393                 case PTL_OK:
394                         PtlNIFini(pni->pni_ni_h);
395                         return;
396                         
397                 case PTL_EQ_IN_USE:
398                         if (retries != 0)
399                                 CWARN("Event queue for %s still busy\n",
400                                       pni->pni_name);
401                         
402                         /* Wait for a bit */
403                         init_waitqueue_head(&waitq);
404                         lwi = LWI_TIMEOUT(2*HZ, NULL, NULL);
405                         l_wait_event(waitq, 0, &lwi);
406                         break;
407                 }
408         }
409         /* notreached */
410 }
411
412 int ptlrpc_ni_init(int number, char *name, struct ptlrpc_ni *pni)
413 {
414         int              rc;
415         char             str[20];
416         ptl_handle_ni_t  nih;
417
418         /* We're not passing any limits yet... */
419         rc = PtlNIInit(number, 0, NULL, NULL, &nih);
420         if (rc != PTL_OK && rc != PTL_IFACE_DUP) {
421                 CDEBUG (D_NET, "Can't init network interface %s: %d\n", 
422                         name, rc);
423                 return (-ENOENT);
424         }
425
426         PtlSnprintHandle(str, sizeof(str), nih);
427         CDEBUG (D_NET, "init %d %s: %s\n", number, name, str);
428
429         pni->pni_name = name;
430         pni->pni_number = number;
431         pni->pni_ni_h = nih;
432
433         pni->pni_eq_h = PTL_INVALID_HANDLE;
434
435         /* CAVEAT EMPTOR: how we process portals events is _radically_
436          * different depending on... */
437 #ifdef __KERNEL__
438         /* kernel portals calls our master callback when events are added to
439          * the event queue.  In fact lustre never pulls events off this queue,
440          * so it's only sized for some debug history. */
441         rc = PtlEQAlloc(pni->pni_ni_h, 1024, ptlrpc_master_callback,
442                         &pni->pni_eq_h);
443 #else
444         /* liblustre calls the master callback when it removes events from the
445          * event queue.  The event queue has to be big enough not to drop
446          * anything */
447 # if CRAY_PORTALS
448         /* cray portals implements a non-standard callback to notify us there
449          * are buffered events even when the app is not doing a filesystem
450          * call. */
451         rc = PtlEQAlloc(pni->pni_ni_h, 10240, cray_portals_callback,
452                         &pni->pni_eq_h);
453 # else
454         rc = PtlEQAlloc(pni->pni_ni_h, 10240, PTL_EQ_HANDLER_NONE,
455                         &pni->pni_eq_h);
456 # endif
457 #endif
458         if (rc != PTL_OK)
459                 GOTO (fail, rc = -ENOMEM);
460
461         return (0);
462  fail:
463         CERROR ("Failed to initialise network interface %s: %d\n",
464                 name, rc);
465
466         /* OK to do complete teardown since we invalidated the handles above */
467         ptlrpc_ni_fini (pni);
468         return (rc);
469 }
470
471 #ifndef __KERNEL__
472 LIST_HEAD(liblustre_wait_callbacks);
473 void *liblustre_services_callback;
474
475 void *
476 liblustre_register_wait_callback (int (*fn)(void *arg), void *arg)
477 {
478         struct liblustre_wait_callback *llwc;
479         
480         OBD_ALLOC(llwc, sizeof(*llwc));
481         LASSERT (llwc != NULL);
482         
483         llwc->llwc_fn = fn;
484         llwc->llwc_arg = arg;
485         list_add_tail(&llwc->llwc_list, &liblustre_wait_callbacks);
486         
487         return (llwc);
488 }
489
490 void
491 liblustre_deregister_wait_callback (void *opaque)
492 {
493         struct liblustre_wait_callback *llwc = opaque;
494         
495         list_del(&llwc->llwc_list);
496         OBD_FREE(llwc, sizeof(*llwc));
497 }
498
499 int
500 liblustre_check_events (int timeout)
501 {
502         ptl_event_t ev;
503         int         rc;
504         int         i;
505         ENTRY;
506
507         rc = PtlEQPoll(&ptlrpc_interfaces[0].pni_eq_h, 1, timeout * 1000,
508                        &ev, &i);
509         if (rc == PTL_EQ_EMPTY)
510                 RETURN(0);
511         
512         LASSERT (rc == PTL_EQ_DROPPED || rc == PTL_OK);
513         
514         /* liblustre: no asynch callback so we can't affort to miss any
515          * events... */
516         if (rc == PTL_EQ_DROPPED) {
517                 CERROR ("Dropped an event!!!\n");
518                 abort();
519         }
520         
521         ptlrpc_master_callback (&ev);
522         RETURN(1);
523 }
524
525 int liblustre_waiting = 0;
526
527 int
528 liblustre_wait_event (int timeout)
529 {
530         struct list_head               *tmp;
531         struct liblustre_wait_callback *llwc;
532         int                             found_something = 0;
533
534         /* single threaded recursion check... */
535         liblustre_waiting = 1;
536
537         for (;;) {
538                 /* Deal with all pending events */
539                 while (liblustre_check_events(0))
540                         found_something = 1;
541
542                 /* Give all registered callbacks a bite at the cherry */
543                 list_for_each(tmp, &liblustre_wait_callbacks) {
544                         llwc = list_entry(tmp, struct liblustre_wait_callback, 
545                                           llwc_list);
546                 
547                         if (llwc->llwc_fn(llwc->llwc_arg))
548                                 found_something = 1;
549                 }
550
551                 if (found_something || timeout == 0)
552                         break;
553
554                 /* Nothing so far, but I'm allowed to block... */
555                 found_something = liblustre_check_events(timeout);
556                 if (!found_something)           /* still nothing */
557                         break;                  /* I timed out */
558         }
559
560         liblustre_waiting = 0;
561
562         return found_something;
563 }
564
565 #ifdef CRAY_PORTALS
566 static void cray_portals_callback(ptl_event_t *ev)
567 {
568         /* We get a callback from the client Cray portals implementation
569          * whenever anyone calls PtlEQPoll(), and an event queue with a
570          * callback handler has outstanding events.
571          *
572          * If it's not liblustre calling PtlEQPoll(), this lets us know we
573          * have outstanding events which we handle with
574          * liblustre_wait_event().
575          *
576          * Otherwise, we're already eagerly consuming events and we'd
577          * handle events out of order if we recursed. */
578         if (!liblustre_waiting)
579                 liblustre_wait_event(0);
580 }
581 #endif
582 #endif /* __KERNEL__ */
583
584 int ptlrpc_default_nal(void)
585 {
586         if (ptlrpc_ninterfaces == 0)
587                 return (-ENOENT);
588
589         return (ptlrpc_interfaces[0].pni_number);
590 }
591
592 int ptlrpc_init_portals(void)
593 {
594         /* Add new portals network interfaces here.
595          * Order is irrelevent! */
596         static struct {
597                 int   number;
598                 char *name;
599         } ptl_nis[] = {
600                 {QSWNAL,  "qswnal"},
601                 {SOCKNAL, "socknal"},
602                 {GMNAL,   "gmnal"},
603                 {IBNAL,   "ibnal"},
604                 {TCPNAL,  "tcpnal"},
605                 {SCIMACNAL, "scimacnal"},
606                 {CRAY_KB_ERNAL, "cray_kb_ernal"}};
607         int   rc;
608         int   i;
609
610         LASSERT(ptlrpc_ninterfaces == 0);
611
612         for (i = 0; i < sizeof (ptl_nis) / sizeof (ptl_nis[0]); i++) {
613                 LASSERT(ptlrpc_ninterfaces < (sizeof(ptlrpc_interfaces) /
614                                               sizeof(ptlrpc_interfaces[0])));
615
616                 rc = ptlrpc_ni_init(ptl_nis[i].number, ptl_nis[i].name,
617                                     &ptlrpc_interfaces[ptlrpc_ninterfaces]);
618                 if (rc == 0)
619                         ptlrpc_ninterfaces++;
620         }
621
622         if (ptlrpc_ninterfaces == 0) {
623                 CERROR("network initialisation failed: is a NAL module "
624                        "loaded?\n");
625                 return -EIO;
626         }
627 #ifndef __KERNEL__
628         liblustre_services_callback = 
629                 liblustre_register_wait_callback(&liblustre_check_services, NULL);
630 #endif
631         return 0;
632 }
633
634 void ptlrpc_exit_portals(void)
635 {
636 #ifndef __KERNEL__
637         liblustre_deregister_wait_callback(liblustre_services_callback);
638 #endif
639         while (ptlrpc_ninterfaces > 0)
640                 ptlrpc_ni_fini (&ptlrpc_interfaces[--ptlrpc_ninterfaces]);
641 }