Whamcloud - gitweb
file lustre_types.h was added on branch b1_4_mountconf on 2006-04-26 18:45:44 +0000
[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 #ifdef __KERNEL__
29 #include <linux/module.h>
30 #else
31 #include <liblustre.h>
32 #endif
33 #include <linux/obd_class.h>
34 #include <linux/lustre_net.h>
35 #include "ptlrpc_internal.h"
36
37 lnet_handle_eq_t   ptlrpc_eq_h;
38
39 /*  
40  *  Client's outgoing request callback
41  */
42 void request_out_callback(lnet_event_t *ev)
43 {
44         struct ptlrpc_cb_id   *cbid = ev->md.user_ptr;
45         struct ptlrpc_request *req = cbid->cbid_arg;
46         unsigned long          flags;
47         ENTRY;
48
49         LASSERT (ev->type == LNET_EVENT_SEND ||
50                  ev->type == LNET_EVENT_UNLINK);
51         LASSERT (ev->unlinked);
52
53         DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
54                   "type %d, status %d", ev->type, ev->status);
55
56         if (ev->type == LNET_EVENT_UNLINK || ev->status != 0) {
57
58                 /* Failed send: make it seem like the reply timed out, just
59                  * like failing sends in client.c does currently...  */
60
61                 spin_lock_irqsave(&req->rq_lock, flags);
62                 req->rq_net_err = 1;
63                 spin_unlock_irqrestore(&req->rq_lock, flags);
64                 
65                 ptlrpc_wake_client_req(req);
66         }
67
68         /* this balances the atomic_inc in ptl_send_rpc() */
69         ptlrpc_req_finished(req);
70         EXIT;
71 }
72
73 /*
74  * Client's incoming reply callback
75  */
76 void reply_in_callback(lnet_event_t *ev)
77 {
78         struct ptlrpc_cb_id   *cbid = ev->md.user_ptr;
79         struct ptlrpc_request *req = cbid->cbid_arg;
80         unsigned long flags;
81         ENTRY;
82
83         LASSERT (ev->type == LNET_EVENT_PUT ||
84                  ev->type == LNET_EVENT_UNLINK);
85         LASSERT (ev->unlinked);
86         LASSERT (ev->md.start == req->rq_repmsg);
87         LASSERT (ev->offset == 0);
88         LASSERT (ev->mlength <= req->rq_replen);
89         
90         DEBUG_REQ((ev->status == 0) ? D_NET : D_ERROR, req,
91                   "type %d, status %d", ev->type, ev->status);
92
93         spin_lock_irqsave (&req->rq_lock, flags);
94
95         LASSERT (req->rq_receiving_reply);
96         req->rq_receiving_reply = 0;
97
98         if (ev->type == LNET_EVENT_PUT && ev->status == 0) {
99                 req->rq_replied = 1;
100                 req->rq_nob_received = ev->mlength;
101         }
102
103         /* NB don't unlock till after wakeup; req can disappear under us
104          * since we don't have our own ref */
105         ptlrpc_wake_client_req(req);
106
107         spin_unlock_irqrestore (&req->rq_lock, flags);
108         EXIT;
109 }
110
111 /* 
112  * Client's bulk has been written/read
113  */
114 void client_bulk_callback (lnet_event_t *ev)
115 {
116         struct ptlrpc_cb_id     *cbid = ev->md.user_ptr;
117         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
118         unsigned long            flags;
119         ENTRY;
120
121         LASSERT ((desc->bd_type == BULK_PUT_SINK && 
122                   ev->type == LNET_EVENT_PUT) ||
123                  (desc->bd_type == BULK_GET_SOURCE &&
124                   ev->type == LNET_EVENT_GET) ||
125                  ev->type == LNET_EVENT_UNLINK);
126         LASSERT (ev->unlinked);
127
128         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
129                "event type %d, status %d, desc %p\n", 
130                ev->type, ev->status, desc);
131
132         spin_lock_irqsave (&desc->bd_lock, flags);
133
134         LASSERT(desc->bd_network_rw);
135         desc->bd_network_rw = 0;
136
137         if (ev->type != LNET_EVENT_UNLINK && ev->status == 0) {
138                 desc->bd_success = 1;
139                 desc->bd_nob_transferred = ev->mlength;
140         }
141
142         /* NB don't unlock till after wakeup; desc can disappear under us
143          * otherwise */
144         ptlrpc_wake_client_req(desc->bd_req);
145
146         spin_unlock_irqrestore (&desc->bd_lock, flags);
147         EXIT;
148 }
149
150 /* 
151  * Server's incoming request callback
152  */
153 void request_in_callback(lnet_event_t *ev)
154 {
155         struct ptlrpc_cb_id               *cbid = ev->md.user_ptr;
156         struct ptlrpc_request_buffer_desc *rqbd = cbid->cbid_arg;
157         struct ptlrpc_service             *service = rqbd->rqbd_service;
158         struct ptlrpc_request             *req;
159         unsigned long                     flags;
160         ENTRY;
161
162         LASSERT (ev->type == LNET_EVENT_PUT ||
163                  ev->type == LNET_EVENT_UNLINK);
164         LASSERT ((char *)ev->md.start >= rqbd->rqbd_buffer);
165         LASSERT ((char *)ev->md.start + ev->offset + ev->mlength <=
166                  rqbd->rqbd_buffer + service->srv_buf_size);
167
168         CDEBUG((ev->status == 0) ? D_NET : D_ERROR,
169                "event type %d, status %d, service %s\n", 
170                ev->type, ev->status, service->srv_name);
171
172         if (ev->unlinked) {
173                 /* If this is the last request message to fit in the
174                  * request buffer we can use the request object embedded in
175                  * rqbd.  Note that if we failed to allocate a request,
176                  * we'd have to re-post the rqbd, which we can't do in this
177                  * context. */
178                 req = &rqbd->rqbd_req;
179                 memset(req, 0, sizeof (*req));
180         } else {
181                 LASSERT (ev->type == LNET_EVENT_PUT);
182                 if (ev->status != 0) {
183                         /* We moaned above already... */
184                         return;
185                 }
186                 OBD_ALLOC_GFP(req, sizeof(*req), GFP_ATOMIC);
187                 if (req == NULL) {
188                         CERROR("Can't allocate incoming request descriptor: "
189                                "Dropping %s RPC from %s\n",
190                                service->srv_name, 
191                                libcfs_id2str(ev->initiator));
192                         return;
193                 }
194         }
195
196         /* NB we ABSOLUTELY RELY on req being zeroed, so pointers are NULL,
197          * flags are reset and scalars are zero.  We only set the message
198          * size to non-zero if this was a successful receive. */
199         req->rq_xid = ev->match_bits;
200         req->rq_reqmsg = ev->md.start + ev->offset;
201         if (ev->type == LNET_EVENT_PUT && ev->status == 0)
202                 req->rq_reqlen = ev->mlength;
203         do_gettimeofday(&req->rq_arrival_time);
204         req->rq_peer = ev->initiator;
205         req->rq_self = ev->target.nid;
206         req->rq_rqbd = rqbd;
207         req->rq_phase = RQ_PHASE_NEW;
208 #if CRAY_XT3
209         req->rq_uid = ev->uid;
210 #endif
211
212         spin_lock_irqsave (&service->srv_lock, flags);
213
214         req->rq_history_seq = service->srv_request_seq++;
215         list_add_tail(&req->rq_history_list, &service->srv_request_history);
216
217         if (ev->unlinked) {
218                 service->srv_nrqbd_receiving--;
219                 if (ev->type != LNET_EVENT_UNLINK &&
220                     service->srv_nrqbd_receiving == 0) {
221                         /* This service is off-air because all its request
222                          * buffers are busy.  Portals will start dropping
223                          * incoming requests until more buffers get posted.  
224                          * NB don't moan if it's because we're tearing down the
225                          * service. */
226                         CERROR("All %s request buffers busy\n",
227                               service->srv_name);
228                 }
229                 /* req takes over the network's ref on rqbd */
230         } else {
231                 /* req takes a ref on rqbd */
232                 rqbd->rqbd_refcount++;
233         }
234
235         list_add_tail(&req->rq_list, &service->srv_request_queue);
236         service->srv_n_queued_reqs++;
237
238         /* NB everything can disappear under us once the request
239          * has been queued and we unlock, so do the wake now... */
240         wake_up(&service->srv_waitq);
241
242         spin_unlock_irqrestore(&service->srv_lock, flags);
243         EXIT;
244 }
245
246 /*
247  *  Server's outgoing reply callback
248  */
249 void reply_out_callback(lnet_event_t *ev)
250 {
251         struct ptlrpc_cb_id       *cbid = ev->md.user_ptr;
252         struct ptlrpc_reply_state *rs = cbid->cbid_arg;
253         struct ptlrpc_service     *svc = rs->rs_service;
254         unsigned long              flags;
255         ENTRY;
256
257         LASSERT (ev->type == LNET_EVENT_SEND ||
258                  ev->type == LNET_EVENT_ACK ||
259                  ev->type == LNET_EVENT_UNLINK);
260
261         if (!rs->rs_difficult) {
262                 /* 'Easy' replies have no further processing so I drop the
263                  * net's ref on 'rs' */
264                 LASSERT (ev->unlinked);
265                 ptlrpc_rs_decref(rs);
266                 atomic_dec (&svc->srv_outstanding_replies);
267                 EXIT;
268                 return;
269         }
270
271         LASSERT (rs->rs_on_net);
272
273         if (ev->unlinked) {
274                 /* Last network callback.  The net's ref on 'rs' stays put
275                  * until ptlrpc_server_handle_reply() is done with it */
276                 spin_lock_irqsave (&svc->srv_lock, flags);
277                 rs->rs_on_net = 0;
278                 ptlrpc_schedule_difficult_reply (rs);
279                 spin_unlock_irqrestore (&svc->srv_lock, flags);
280         }
281
282         EXIT;
283 }
284
285 /*
286  * Server's bulk completion callback
287  */
288 void server_bulk_callback (lnet_event_t *ev)
289 {
290         struct ptlrpc_cb_id     *cbid = ev->md.user_ptr;
291         struct ptlrpc_bulk_desc *desc = cbid->cbid_arg;
292         unsigned long            flags;
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_irqsave (&desc->bd_lock, flags);
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         }
317
318         if (ev->unlinked) {
319                 /* This is the last callback no matter what... */
320                 desc->bd_network_rw = 0;
321                 wake_up(&desc->bd_waitq);
322         }
323
324         spin_unlock_irqrestore (&desc->bd_lock, flags);
325         EXIT;
326 }
327
328 static void ptlrpc_master_callback(lnet_event_t *ev)
329 {
330         struct ptlrpc_cb_id *cbid = ev->md.user_ptr;
331         void (*callback)(lnet_event_t *ev) = cbid->cbid_fn;
332
333         /* Honestly, it's best to find out early. */
334         LASSERT (cbid->cbid_arg != LP_POISON);
335         LASSERT (callback == request_out_callback ||
336                  callback == reply_in_callback ||
337                  callback == client_bulk_callback ||
338                  callback == request_in_callback ||
339                  callback == reply_out_callback ||
340                  callback == server_bulk_callback);
341         
342         callback (ev);
343 }
344
345 int ptlrpc_uuid_to_peer (struct obd_uuid *uuid, 
346                          lnet_process_id_t *peer, lnet_nid_t *self)
347 {
348         int               best_dist = 0;
349         int               best_order = 0;
350         int               count = 0;
351         int               rc = -ENOENT;
352         int               portals_compatibility;
353         int               dist;
354         int               order;
355         lnet_nid_t        dst_nid;
356         lnet_nid_t        src_nid;
357
358         portals_compatibility = LNetCtl(IOC_LIBCFS_PORTALS_COMPATIBILITY, NULL);
359
360         peer->pid = LUSTRE_SRV_LNET_PID;
361
362         /* Choose the matching UUID that's closest */
363         while (lustre_uuid_to_peer(uuid->uuid, &dst_nid, count++) == 0) {
364                 dist = LNetDist(dst_nid, &src_nid, &order);
365                 if (dist < 0)
366                         continue;
367
368                 if (dist == 0) {                /* local! use loopback LND */
369                         peer->nid = *self = LNET_MKNID(LNET_MKNET(LOLND, 0), 0);
370                         rc = 0;
371                         break;
372                 }
373                 
374                 LASSERT (order >= 0);
375                 if (rc < 0 ||
376                     dist < best_dist ||
377                     (dist == best_dist && order < best_order)) {
378                         best_dist = dist;
379                         best_order = order;
380
381                         if (portals_compatibility > 1) {
382                                 /* Strong portals compatibility: Zero the nid's
383                                  * NET, so if I'm reading new config logs, or
384                                  * getting configured by (new) lconf I can
385                                  * still talk to old servers. */
386                                 dst_nid = LNET_MKNID(0, LNET_NIDADDR(dst_nid));
387                                 src_nid = LNET_MKNID(0, LNET_NIDADDR(src_nid));
388                         }
389                         peer->nid = dst_nid;
390                         *self = src_nid;
391                         rc = 0;
392                 }
393         }
394
395         CDEBUG(D_NET,"%s->%s\n", uuid->uuid, libcfs_id2str(*peer));
396         if (rc != 0) 
397                 CERROR("No NID found for %s\n", uuid->uuid);
398         return rc;
399 }
400
401 void ptlrpc_ni_fini(void)
402 {
403         wait_queue_head_t   waitq;
404         struct l_wait_info  lwi;
405         int                 rc;
406         int                 retries;
407         
408         /* Wait for the event queue to become idle since there may still be
409          * messages in flight with pending events (i.e. the fire-and-forget
410          * messages == client requests and "non-difficult" server
411          * replies */
412
413         for (retries = 0;; retries++) {
414                 rc = LNetEQFree(ptlrpc_eq_h);
415                 switch (rc) {
416                 default:
417                         LBUG();
418
419                 case 0:
420                         LNetNIFini();
421                         return;
422                         
423                 case -EBUSY:
424                         if (retries != 0)
425                                 CWARN("Event queue still busy\n");
426                         
427                         /* Wait for a bit */
428                         init_waitqueue_head(&waitq);
429                         lwi = LWI_TIMEOUT(2*HZ, NULL, NULL);
430                         l_wait_event(waitq, 0, &lwi);
431                         break;
432                 }
433         }
434         /* notreached */
435 }
436
437 lnet_pid_t ptl_get_pid(void)
438 {
439         lnet_pid_t        pid;
440
441 #ifndef  __KERNEL__
442         pid = getpid();
443 #else
444         pid = LUSTRE_SRV_LNET_PID;
445 #endif
446         return pid;
447 }
448         
449 int ptlrpc_ni_init(void)
450 {
451         int              rc;
452         lnet_pid_t       pid;
453
454         pid = ptl_get_pid();
455         CDEBUG(D_NET, "My pid is: %x\n", pid);
456
457         /* We're not passing any limits yet... */
458         rc = LNetNIInit(pid);
459         if (rc < 0) {
460                 CDEBUG (D_NET, "Can't init network interface: %d\n", rc);
461                 return (-ENOENT);
462         }
463
464         /* CAVEAT EMPTOR: how we process portals events is _radically_
465          * different depending on... */
466 #ifdef __KERNEL__
467         /* kernel portals calls our master callback when events are added to
468          * the event queue.  In fact lustre never pulls events off this queue,
469          * so it's only sized for some debug history. */
470         rc = LNetEQAlloc(1024, ptlrpc_master_callback, &ptlrpc_eq_h);
471 #else
472         /* liblustre calls the master callback when it removes events from the
473          * event queue.  The event queue has to be big enough not to drop
474          * anything */
475         rc = LNetEQAlloc(10240, LNET_EQ_HANDLER_NONE, &ptlrpc_eq_h);
476 #endif
477         if (rc == 0)
478                 return 0;
479
480         CERROR ("Failed to allocate event queue: %d\n", rc);
481         LNetNIFini();
482
483         return (-ENOMEM);
484 }
485
486 #ifndef __KERNEL__
487 LIST_HEAD(liblustre_wait_callbacks);
488 void *liblustre_services_callback;
489
490 void *
491 liblustre_register_wait_callback (int (*fn)(void *arg), void *arg)
492 {
493         struct liblustre_wait_callback *llwc;
494         
495         OBD_ALLOC(llwc, sizeof(*llwc));
496         LASSERT (llwc != NULL);
497         
498         llwc->llwc_fn = fn;
499         llwc->llwc_arg = arg;
500         list_add_tail(&llwc->llwc_list, &liblustre_wait_callbacks);
501         
502         return (llwc);
503 }
504
505 void
506 liblustre_deregister_wait_callback (void *opaque)
507 {
508         struct liblustre_wait_callback *llwc = opaque;
509         
510         list_del(&llwc->llwc_list);
511         OBD_FREE(llwc, sizeof(*llwc));
512 }
513
514 int
515 liblustre_check_events (int timeout)
516 {
517         lnet_event_t ev;
518         int         rc;
519         int         i;
520         ENTRY;
521
522         rc = LNetEQPoll(&ptlrpc_eq_h, 1, timeout * 1000, &ev, &i);
523         if (rc == 0)
524                 RETURN(0);
525         
526         LASSERT (rc == -EOVERFLOW || rc == 1);
527         
528         /* liblustre: no asynch callback so we can't affort to miss any
529          * events... */
530         if (rc == -EOVERFLOW) {
531                 CERROR ("Dropped an event!!!\n");
532                 abort();
533         }
534         
535         ptlrpc_master_callback (&ev);
536         RETURN(1);
537 }
538
539 int liblustre_waiting = 0;
540
541 int
542 liblustre_wait_event (int timeout)
543 {
544         struct list_head               *tmp;
545         struct liblustre_wait_callback *llwc;
546         int                             found_something = 0;
547
548         /* single threaded recursion check... */
549         liblustre_waiting = 1;
550
551         for (;;) {
552                 /* Deal with all pending events */
553                 while (liblustre_check_events(0))
554                         found_something = 1;
555
556                 /* Give all registered callbacks a bite at the cherry */
557                 list_for_each(tmp, &liblustre_wait_callbacks) {
558                         llwc = list_entry(tmp, struct liblustre_wait_callback, 
559                                           llwc_list);
560                 
561                         if (llwc->llwc_fn(llwc->llwc_arg))
562                                 found_something = 1;
563                 }
564
565                 if (found_something || timeout == 0)
566                         break;
567
568                 /* Nothing so far, but I'm allowed to block... */
569                 found_something = liblustre_check_events(timeout);
570                 if (!found_something)           /* still nothing */
571                         break;                  /* I timed out */
572         }
573
574         liblustre_waiting = 0;
575
576         return found_something;
577 }
578
579 #endif /* __KERNEL__ */
580
581 int ptlrpc_init_portals(void)
582 {
583         int   rc = ptlrpc_ni_init();
584
585         if (rc != 0) {
586                 CERROR("network initialisation failed\n");
587                 return -EIO;
588         }
589 #ifndef __KERNEL__
590         liblustre_services_callback = 
591                 liblustre_register_wait_callback(&liblustre_check_services, NULL);
592 #endif
593         return 0;
594 }
595
596 void ptlrpc_exit_portals(void)
597 {
598 #ifndef __KERNEL__
599         liblustre_deregister_wait_callback(liblustre_services_callback);
600 #endif
601         ptlrpc_ni_fini();
602 }