Whamcloud - gitweb
4d17c7de15bcb8e95db077d8939631543cdb4807
[fs/lustre-release.git] / lnet / lnet / lib-msg.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * lib/lib-msg.c
5  * Message decoding, parsing and finalizing routines
6  *
7  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
8  *
9  *   This file is part of Lustre, http://www.lustre.org
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #ifndef __KERNEL__
26 # include <stdio.h>
27 #else
28 # define DEBUG_SUBSYSTEM S_PORTALS
29 # include <linux/kp30.h>
30 #endif
31
32 #include <portals/lib-p30.h>
33
34 void
35 lib_enq_event_locked (lib_nal_t *nal, void *private, 
36                       lib_eq_t *eq, ptl_event_t *ev)
37 {
38         ptl_event_t  *eq_slot;
39
40         /* Allocate the next queue slot */
41         ev->link = ev->sequence = eq->eq_enq_seq++;
42         /* NB we don't support START events yet and we don't create a separate
43          * UNLINK event unless an explicit unlink succeeds, so the link
44          * sequence is pretty useless */
45
46         /* We don't support different uid/jids yet */
47         ev->uid = 0;
48         ev->jid = 0;
49         
50         /* size must be a power of 2 to handle sequence # overflow */
51         LASSERT (eq->eq_size != 0 &&
52                  eq->eq_size == LOWEST_BIT_SET (eq->eq_size));
53         eq_slot = eq->eq_events + (ev->sequence & (eq->eq_size - 1));
54
55         /* There is no race since both event consumers and event producers
56          * take the LIB_LOCK(), so we don't screw around with memory
57          * barriers, setting the sequence number last or wierd structure
58          * layout assertions. */
59         *eq_slot = *ev;
60
61         /* Call the callback handler (if any) */
62         if (eq->eq_callback != NULL)
63                 eq->eq_callback (eq_slot);
64
65         /* Wake anyone sleeping for an event (see lib-eq.c) */
66 #ifdef __KERNEL__
67         if (waitqueue_active(&nal->libnal_ni.ni_waitq))
68                 wake_up_all(&nal->libnal_ni.ni_waitq);
69 #else
70         pthread_cond_broadcast(&nal->libnal_ni.ni_cond);
71 #endif
72 }
73
74 void 
75 lib_finalize (lib_nal_t *nal, void *private, lib_msg_t *msg, ptl_err_t status)
76 {
77         lib_md_t     *md;
78         int           unlink;
79         unsigned long flags;
80         int           rc;
81         ptl_hdr_t     ack;
82
83         if (msg == NULL)
84                 return;
85
86         /* Only send an ACK if the PUT completed successfully */
87         if (status == PTL_OK &&
88             !ptl_is_wire_handle_none(&msg->ack_wmd)) {
89
90                 LASSERT(msg->ev.type == PTL_EVENT_PUT_END);
91
92                 memset (&ack, 0, sizeof (ack));
93                 ack.type     = cpu_to_le32(PTL_MSG_ACK);
94                 ack.dest_nid = cpu_to_le64(msg->ev.initiator.nid);
95                 ack.dest_pid = cpu_to_le32(msg->ev.initiator.pid);
96                 ack.src_nid  = cpu_to_le64(nal->libnal_ni.ni_pid.nid);
97                 ack.src_pid  = cpu_to_le32(nal->libnal_ni.ni_pid.pid);
98                 ack.payload_length = 0;
99
100                 ack.msg.ack.dst_wmd = msg->ack_wmd;
101                 ack.msg.ack.match_bits = msg->ev.match_bits;
102                 ack.msg.ack.mlength = cpu_to_le32(msg->ev.mlength);
103
104                 rc = lib_send (nal, private, NULL, &ack, PTL_MSG_ACK,
105                                msg->ev.initiator.nid, msg->ev.initiator.pid, 
106                                NULL, 0, 0);
107                 if (rc != PTL_OK) {
108                         /* send failed: there's nothing else to clean up. */
109                         CERROR("Error %d sending ACK to "LPX64"\n", 
110                                rc, msg->ev.initiator.nid);
111                 }
112         }
113
114         md = msg->md;
115
116         LIB_LOCK(nal, flags);
117
118         /* Now it's safe to drop my caller's ref */
119         md->pending--;
120         LASSERT (md->pending >= 0);
121
122         /* Should I unlink this MD? */
123         if (md->pending != 0)                   /* other refs */
124                 unlink = 0;
125         else if ((md->md_flags & PTL_MD_FLAG_ZOMBIE) != 0)
126                 unlink = 1;
127         else if ((md->md_flags & PTL_MD_FLAG_AUTO_UNLINK) == 0)
128                 unlink = 0;
129         else
130                 unlink = lib_md_exhausted(md);
131
132         msg->ev.ni_fail_type = status;
133         msg->ev.unlinked = unlink;
134
135         if (md->eq != NULL)
136                 lib_enq_event_locked(nal, private, md->eq, &msg->ev);
137
138         if (unlink)
139                 lib_md_unlink(nal, md);
140
141         list_del (&msg->msg_list);
142         nal->libnal_ni.ni_counters.msgs_alloc--;
143         lib_msg_free(nal, msg);
144
145         LIB_UNLOCK(nal, flags);
146 }