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