Whamcloud - gitweb
update patchless client for support 2.6.20, 2.6.21 and RHEL5 kernels.
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  */
25
26 #define DEBUG_SUBSYSTEM S_RPC
27
28 #ifndef __KERNEL__
29 # include <liblustre.h>
30 #else
31 # ifdef __mips64__
32 #  include <linux/kernel.h>
33 # endif
34 #endif
35 #include <obd_class.h>
36 #include <lustre_net.h>
37 #include "ptlrpc_internal.h"
38
39 lnet_handle_eq_t   ptlrpc_eq_h;
40
41 /*  
42  *  Client's outgoing request callback
43  */
44 void request_out_callback(lnet_event_t *ev)
45 {
46         struct ptlrpc_cb_id   *cbid = ev->md.user_ptr;
47         struct ptlrpc_request *req = cbid->cbid_arg;
48         ENTRY;
49
50         LASSERT (ev->type == LNET_EVENT_SEND ||
51                  ev->type == LNET_EVENT_UNLINK);
52         LASSERT (ev->unlinked);
53
54         DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
55                   "type %d, status %d", ev->type, ev->status);
56
57         if (ev->type == LNET_EVENT_UNLINK || ev->status != 0) {
58
59                 /* Failed send: make it seem like the reply timed out, just
60                  * like failing sends in client.c does currently...  */
61
62                 spin_lock(&req->rq_lock);
63                 req->rq_net_err = 1;
64                 spin_unlock(&req->rq_lock);
65
66                 ptlrpc_wake_client_req(req);
67         }
68
69         /* these balance the references in ptl_send_rpc() */
70         atomic_dec(&req->rq_import->imp_inflight);
71         ptlrpc_req_finished(req);
72
73         EXIT;
74 }
75
76 /*
77  * Client's incoming reply callback
78  */
79 void reply_in_callback(lnet_event_t *ev)
80 {
81         struct ptlrpc_cb_id   *cbid = ev->md.user_ptr;
82         struct ptlrpc_request *req = cbid->cbid_arg;
83         ENTRY;
84
85         LASSERT (ev->type == LNET_EVENT_PUT ||
86                  ev->type == LNET_EVENT_UNLINK);
87         LASSERT (ev->unlinked);
88         LASSERT (ev->md.start == req->rq_repmsg);
89         LASSERT (ev->offset == 0);
90         LASSERT (ev->mlength <= req->rq_replen);
91         
92         DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
93                   "type %d, status %d", ev->type, ev->status);
94
95         spin_lock(&req->rq_lock);
96
97         LASSERT (req->rq_receiving_reply);
98         req->rq_receiving_reply = 0;
99
100         if (ev->type == LNET_EVENT_PUT && ev->status == 0) {
101                 req->rq_replied = 1;
102                 req->rq_nob_received = ev->mlength;
103         }
104
105         /* NB don't unlock till after wakeup; req can disappear under us
106          * since we don't have our own ref */
107         ptlrpc_wake_client_req(req);
108
109         spin_unlock(&req->rq_lock);
110         EXIT;
111 }
112
113 /* 
114  * Client's bulk has been written/read
115  */
116 void client_bulk_callback (lnet_event_t *ev)
117 {
118         struct ptlrpc_cb_id     *cbid = ev->md.user_ptr;
119         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
120         ENTRY;
121
122         LASSERT ((desc->bd_type == BULK_PUT_SINK && 
123                   ev->type == LNET_EVENT_PUT) ||
124                  (desc->bd_type == BULK_GET_SOURCE &&
125                   ev->type == LNET_EVENT_GET) ||
126                  ev->type == LNET_EVENT_UNLINK);
127         LASSERT (ev->unlinked);
128
129         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
130                "event type %d, status %d, desc %p\n", 
131                ev->type, ev->status, desc);
132
133         spin_lock(&desc->bd_lock);
134
135         LASSERT(desc->bd_network_rw);
136         desc->bd_network_rw = 0;
137
138         if (ev->type != LNET_EVENT_UNLINK && ev->status == 0) {
139                 desc->bd_success = 1;
140                 desc->bd_nob_transferred = ev->mlength;
141                 desc->bd_sender = ev->sender;
142         }
143
144         /* NB don't unlock till after wakeup; desc can disappear under us
145          * otherwise */
146         ptlrpc_wake_client_req(desc->bd_req);
147
148         spin_unlock(&desc->bd_lock);
149         EXIT;
150 }
151
152 /* 
153  * Server's incoming request callback
154  */
155 void request_in_callback(lnet_event_t *ev)
156 {
157         struct ptlrpc_cb_id               *cbid = ev->md.user_ptr;
158         struct ptlrpc_request_buffer_desc *rqbd = cbid->cbid_arg;
159         struct ptlrpc_service             *service = rqbd->rqbd_service;
160         struct ptlrpc_request             *req;
161         ENTRY;
162
163         LASSERT (ev->type == LNET_EVENT_PUT ||
164                  ev->type == LNET_EVENT_UNLINK);
165         LASSERT ((char *)ev->md.start >= rqbd->rqbd_buffer);
166         LASSERT ((char *)ev->md.start + ev->offset + ev->mlength <=
167                  rqbd->rqbd_buffer + service->srv_buf_size);
168
169         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
170                "event type %d, status %d, service %s\n", 
171                ev->type, ev->status, service->srv_name);
172
173         if (ev->unlinked) {
174                 /* If this is the last request message to fit in the
175                  * request buffer we can use the request object embedded in
176                  * rqbd.  Note that if we failed to allocate a request,
177                  * we'd have to re-post the rqbd, which we can't do in this
178                  * context. */
179                 req = &rqbd->rqbd_req;
180                 memset(req, 0, sizeof (*req));
181         } else {
182                 LASSERT (ev->type == LNET_EVENT_PUT);
183                 if (ev->status != 0) {
184                         /* We moaned above already... */
185                         return;
186                 }
187                 OBD_ALLOC_GFP(req, sizeof(*req), CFS_ALLOC_ATOMIC_TRY);
188                 if (req == NULL) {
189                         CERROR("Can't allocate incoming request descriptor: "
190                                "Dropping %s RPC from %s\n",
191                                service->srv_name, 
192                                libcfs_id2str(ev->initiator));
193                         return;
194                 }
195         }
196
197         /* NB we ABSOLUTELY RELY on req being zeroed, so pointers are NULL,
198          * flags are reset and scalars are zero.  We only set the message
199          * size to non-zero if this was a successful receive. */
200         req->rq_xid = ev->match_bits;
201         req->rq_reqmsg = ev->md.start + ev->offset;
202         if (ev->type == LNET_EVENT_PUT && ev->status == 0)
203                 req->rq_reqlen = ev->mlength;
204         do_gettimeofday(&req->rq_arrival_time);
205         req->rq_peer = ev->initiator;
206         req->rq_self = ev->target.nid;
207         req->rq_rqbd = rqbd;
208         req->rq_phase = RQ_PHASE_NEW;
209 #ifdef CRAY_XT3
210         req->rq_uid = ev->uid;
211 #endif
212
213         spin_lock(&service->srv_lock);
214
215         req->rq_history_seq = service->srv_request_seq++;
216         list_add_tail(&req->rq_history_list, &service->srv_request_history);
217
218         if (ev->unlinked) {
219                 service->srv_nrqbd_receiving--;
220                 CDEBUG(D_RPCTRACE,"Buffer complete: %d buffers still posted\n",
221                        service->srv_nrqbd_receiving);
222
223                 /* Normally, don't complain about 0 buffers posted; LNET won't
224                  * drop incoming reqs since we set the portal lazy */
225                 if (test_req_buffer_pressure &&
226                     ev->type != LNET_EVENT_UNLINK &&
227                     service->srv_nrqbd_receiving == 0)
228                         CWARN("All %s request buffers busy\n",
229                               service->srv_name);
230
231                 /* req takes over the network's ref on rqbd */
232         } else {
233                 /* req takes a ref on rqbd */
234                 rqbd->rqbd_refcount++;
235         }
236
237         list_add_tail(&req->rq_list, &service->srv_request_queue);
238         service->srv_n_queued_reqs++;
239
240         /* NB everything can disappear under us once the request
241          * has been queued and we unlock, so do the wake now... */
242         cfs_waitq_signal(&service->srv_waitq);
243
244         spin_unlock(&service->srv_lock);
245         EXIT;
246 }
247
248 /*
249  *  Server's outgoing reply callback
250  */
251 void reply_out_callback(lnet_event_t *ev)
252 {
253         struct ptlrpc_cb_id       *cbid = ev->md.user_ptr;
254         struct ptlrpc_reply_state *rs = cbid->cbid_arg;
255         struct ptlrpc_service     *svc = rs->rs_service;
256         ENTRY;
257
258         LASSERT (ev->type == LNET_EVENT_SEND ||
259                  ev->type == LNET_EVENT_ACK ||
260                  ev->type == LNET_EVENT_UNLINK);
261
262         if (!rs->rs_difficult) {
263                 /* 'Easy' replies have no further processing so I drop the
264                  * net's ref on 'rs' */
265                 LASSERT (ev->unlinked);
266                 ptlrpc_rs_decref(rs);
267                 atomic_dec (&svc->srv_outstanding_replies);
268                 EXIT;
269                 return;
270         }
271
272         LASSERT (rs->rs_on_net);
273
274         if (ev->unlinked) {
275                 /* Last network callback.  The net's ref on 'rs' stays put
276                  * until ptlrpc_server_handle_reply() is done with it */
277                 spin_lock(&svc->srv_lock);
278                 rs->rs_on_net = 0;
279                 ptlrpc_schedule_difficult_reply (rs);
280                 spin_unlock(&svc->srv_lock);
281         }
282
283         EXIT;
284 }
285
286 /*
287  * Server's bulk completion callback
288  */
289 void server_bulk_callback (lnet_event_t *ev)
290 {
291         struct ptlrpc_cb_id     *cbid = ev->md.user_ptr;
292         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
293         ENTRY;
294
295         LASSERT (ev->type == LNET_EVENT_SEND ||
296                  ev->type == LNET_EVENT_UNLINK ||
297                  (desc->bd_type == BULK_PUT_SOURCE &&
298                   ev->type == LNET_EVENT_ACK) ||
299                  (desc->bd_type == BULK_GET_SINK &&
300                   ev->type == LNET_EVENT_REPLY));
301
302         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
303                "event type %d, status %d, desc %p\n", 
304                ev->type, ev->status, desc);
305
306         spin_lock(&desc->bd_lock);
307         
308         if ((ev->type == LNET_EVENT_ACK ||
309              ev->type == LNET_EVENT_REPLY) &&
310             ev->status == 0) {
311                 /* We heard back from the peer, so even if we get this
312                  * before the SENT event (oh yes we can), we know we
313                  * read/wrote the peer buffer and how much... */
314                 desc->bd_success = 1;
315                 desc->bd_nob_transferred = ev->mlength;
316                 desc->bd_sender = ev->sender;
317         }
318
319         if (ev->unlinked) {
320                 /* This is the last callback no matter what... */
321                 desc->bd_network_rw = 0;
322                 cfs_waitq_signal(&desc->bd_waitq);
323         }
324
325         spin_unlock(&desc->bd_lock);
326         EXIT;
327 }
328
329 static void ptlrpc_master_callback(lnet_event_t *ev)
330 {
331         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
332         void (*callback)(lnet_event_t *ev) = cbid->cbid_fn;
333
334         /* Honestly, it's best to find out early. */
335         LASSERT (cbid->cbid_arg != LP_POISON);
336         LASSERT (callback == request_out_callback ||
337                  callback == reply_in_callback ||
338                  callback == client_bulk_callback ||
339                  callback == request_in_callback ||
340                  callback == reply_out_callback ||
341                  callback == server_bulk_callback);
342         
343         callback (ev);
344 }
345
346 int ptlrpc_uuid_to_peer (struct obd_uuid *uuid, 
347                          lnet_process_id_t *peer, lnet_nid_t *self)
348 {
349         int               best_dist = 0;
350         int               best_order = 0;
351         int               count = 0;
352         int               rc = -ENOENT;
353         int               portals_compatibility;
354         int               dist;
355         int               order;
356         lnet_nid_t        dst_nid;
357         lnet_nid_t        src_nid;
358
359         portals_compatibility = LNetCtl(IOC_LIBCFS_PORTALS_COMPATIBILITY, NULL);
360
361         peer->pid = LUSTRE_SRV_LNET_PID;
362
363         /* Choose the matching UUID that's closest */
364         while (lustre_uuid_to_peer(uuid->uuid, &dst_nid, count++) == 0) {
365                 dist = LNetDist(dst_nid, &src_nid, &order);
366                 if (dist < 0)
367                         continue;
368
369                 if (dist == 0) {                /* local! use loopback LND */
370                         peer->nid = *self = LNET_MKNID(LNET_MKNET(LOLND, 0), 0);
371                         rc = 0;
372                         break;
373                 }
374                 
375                 LASSERT (order >= 0);
376                 if (rc < 0 ||
377                     dist < best_dist ||
378                     (dist == best_dist && order < best_order)) {
379                         best_dist = dist;
380                         best_order = order;
381
382                         if (portals_compatibility > 1) {
383                                 /* Strong portals compatibility: Zero the nid's
384                                  * NET, so if I'm reading new config logs, or
385                                  * getting configured by (new) lconf I can
386                                  * still talk to old servers. */
387                                 dst_nid = LNET_MKNID(0, LNET_NIDADDR(dst_nid));
388                                 src_nid = LNET_MKNID(0, LNET_NIDADDR(src_nid));
389                         }
390                         peer->nid = dst_nid;
391                         *self = src_nid;
392                         rc = 0;
393                 }
394         }
395
396         CDEBUG(D_NET,"%s->%s\n", uuid->uuid, libcfs_id2str(*peer));
397         if (rc != 0) 
398                 CERROR("No NID found for %s\n", uuid->uuid);
399         return rc;
400 }
401
402 void ptlrpc_ni_fini(void)
403 {
404         cfs_waitq_t         waitq;
405         struct l_wait_info  lwi;
406         int                 rc;
407         int                 retries;
408         
409         /* Wait for the event queue to become idle since there may still be
410          * messages in flight with pending events (i.e. the fire-and-forget
411          * messages == client requests and "non-difficult" server
412          * replies */
413
414         for (retries = 0;; retries++) {
415                 rc = LNetEQFree(ptlrpc_eq_h);
416                 switch (rc) {
417                 default:
418                         LBUG();
419
420                 case 0:
421                         LNetNIFini();
422                         return;
423                         
424                 case -EBUSY:
425                         if (retries != 0)
426                                 CWARN("Event queue still busy\n");
427                         
428                         /* Wait for a bit */
429                         cfs_waitq_init(&waitq);
430                         lwi = LWI_TIMEOUT(cfs_time_seconds(2), NULL, NULL);
431                         l_wait_event(waitq, 0, &lwi);
432                         break;
433                 }
434         }
435         /* notreached */
436 }
437
438 lnet_pid_t ptl_get_pid(void)
439 {
440         lnet_pid_t        pid;
441
442 #ifndef  __KERNEL__
443         pid = getpid();
444 #else
445         pid = LUSTRE_SRV_LNET_PID;
446 #endif
447         return pid;
448 }
449         
450 int ptlrpc_ni_init(void)
451 {
452         int              rc;
453         lnet_pid_t       pid;
454
455         pid = ptl_get_pid();
456         CDEBUG(D_NET, "My pid is: %x\n", pid);
457
458         /* We're not passing any limits yet... */
459         rc = LNetNIInit(pid);
460         if (rc < 0) {
461                 CDEBUG (D_NET, "Can't init network interface: %d\n", rc);
462                 return (-ENOENT);
463         }
464
465         /* CAVEAT EMPTOR: how we process portals events is _radically_
466          * different depending on... */
467 #ifdef __KERNEL__
468         /* kernel portals calls our master callback when events are added to
469          * the event queue.  In fact lustre never pulls events off this queue,
470          * so it's only sized for some debug history. */
471         rc = LNetEQAlloc(1024, ptlrpc_master_callback, &ptlrpc_eq_h);
472 #else
473         /* liblustre calls the master callback when it removes events from the
474          * event queue.  The event queue has to be big enough not to drop
475          * anything */
476         rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &ptlrpc_eq_h);
477 #endif
478         if (rc == 0)
479                 return 0;
480
481         CERROR ("Failed to allocate event queue: %d\n", rc);
482         LNetNIFini();
483
484         return (-ENOMEM);
485 }
486
487 #ifndef __KERNEL__
488 CFS_LIST_HEAD(liblustre_wait_callbacks);
489 CFS_LIST_HEAD(liblustre_idle_callbacks);
490 void *liblustre_services_callback;
491
492 void *
493 liblustre_register_waitidle_callback (struct list_head *callback_list,
494                                       const char *name,
495                                       int (*fn)(void *arg), void *arg)
496 {
497         struct liblustre_wait_callback *llwc;
498         
499         OBD_ALLOC(llwc, sizeof(*llwc));
500         LASSERT (llwc != NULL);
501         
502         llwc->llwc_name = name;
503         llwc->llwc_fn = fn;
504         llwc->llwc_arg = arg;
505         list_add_tail(&llwc->llwc_list, callback_list);
506         
507         return (llwc);
508 }
509
510 void
511 liblustre_deregister_waitidle_callback (void *opaque)
512 {
513         struct liblustre_wait_callback *llwc = opaque;
514         
515         list_del(&llwc->llwc_list);
516         OBD_FREE(llwc, sizeof(*llwc));
517 }
518
519 void *
520 liblustre_register_wait_callback (const char *name,
521                                   int (*fn)(void *arg), void *arg)
522 {
523         return liblustre_register_waitidle_callback(&liblustre_wait_callbacks,
524                                                     name, fn, arg);
525 }
526
527 void
528 liblustre_deregister_wait_callback (void *opaque)
529 {
530         liblustre_deregister_waitidle_callback(opaque);
531 }
532
533 void *
534 liblustre_register_idle_callback (const char *name,
535                                   int (*fn)(void *arg), void *arg)
536 {
537         return liblustre_register_waitidle_callback(&liblustre_idle_callbacks,
538                                                     name, fn, arg);
539 }
540
541 void
542 liblustre_deregister_idle_callback (void *opaque)
543 {
544         liblustre_deregister_waitidle_callback(opaque);
545 }
546
547 int
548 liblustre_check_events (int timeout)
549 {
550         lnet_event_t ev;
551         int         rc;
552         int         i;
553         ENTRY;
554
555         rc = LNetEQPoll(&ptlrpc_eq_h, 1, timeout * 1000, &ev, &i);
556         if (rc == 0)
557                 RETURN(0);
558         
559         LASSERT (rc == -EOVERFLOW || rc == 1);
560         
561         /* liblustre: no asynch callback so we can't affort to miss any
562          * events... */
563         if (rc == -EOVERFLOW) {
564                 CERROR ("Dropped an event!!!\n");
565                 abort();
566         }
567         
568         ptlrpc_master_callback (&ev);
569         RETURN(1);
570 }
571
572 int liblustre_waiting = 0;
573
574 int
575 liblustre_wait_event (int timeout)
576 {
577         struct list_head               *tmp;
578         struct liblustre_wait_callback *llwc;
579         int                             found_something = 0;
580
581         /* single threaded recursion check... */
582         liblustre_waiting = 1;
583
584         for (;;) {
585                 /* Deal with all pending events */
586                 while (liblustre_check_events(0))
587                         found_something = 1;
588
589                 /* Give all registered callbacks a bite at the cherry */
590                 list_for_each(tmp, &liblustre_wait_callbacks) {
591                         llwc = list_entry(tmp, struct liblustre_wait_callback, 
592                                           llwc_list);
593                 
594                         if (llwc->llwc_fn(llwc->llwc_arg))
595                                 found_something = 1;
596                 }
597
598                 if (found_something || timeout == 0)
599                         break;
600
601                 /* Nothing so far, but I'm allowed to block... */
602                 found_something = liblustre_check_events(timeout);
603                 if (!found_something)           /* still nothing */
604                         break;                  /* I timed out */
605         }
606
607         liblustre_waiting = 0;
608
609         return found_something;
610 }
611
612 void
613 liblustre_wait_idle(void)
614 {
615         static int recursed = 0;
616         
617         struct list_head               *tmp;
618         struct liblustre_wait_callback *llwc;
619         int                             idle = 0;
620
621         LASSERT(!recursed);
622         recursed = 1;
623         
624         do {
625                 liblustre_wait_event(0);
626
627                 idle = 1;
628
629                 list_for_each(tmp, &liblustre_idle_callbacks) {
630                         llwc = list_entry(tmp, struct liblustre_wait_callback,
631                                           llwc_list);
632                         
633                         if (!llwc->llwc_fn(llwc->llwc_arg)) {
634                                 idle = 0;
635                                 break;
636                         }
637                 }
638                         
639         } while (!idle);
640
641         recursed = 0;
642 }
643
644 #endif /* __KERNEL__ */
645
646 int ptlrpc_init_portals(void)
647 {
648         int   rc = ptlrpc_ni_init();
649
650         if (rc != 0) {
651                 CERROR("network initialisation failed\n");
652                 return -EIO;
653         }
654 #ifndef __KERNEL__
655         liblustre_services_callback = 
656                 liblustre_register_wait_callback("liblustre_check_services",
657                                                  &liblustre_check_services, NULL);
658 #endif
659         rc = ptlrpcd_addref();
660         if (rc == 0)
661                 return 0;
662         
663         CERROR("rpcd initialisation failed\n");
664 #ifndef __KERNEL__
665         liblustre_deregister_wait_callback(liblustre_services_callback);
666 #endif
667         ptlrpc_ni_fini();
668         return rc;
669 }
670
671 void ptlrpc_exit_portals(void)
672 {
673 #ifndef __KERNEL__
674         liblustre_deregister_wait_callback(liblustre_services_callback);
675 #endif
676         ptlrpcd_decref();
677         ptlrpc_ni_fini();
678 }