Whamcloud - gitweb
- merge 2 weeks of b1_4 fixes onto HEAD
[fs/lustre-release.git] / lnet / lnet / lib-me.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * lib/lib-me.c
5  * Match Entry management 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 int
36 lib_api_me_attach(nal_t *apinal,
37                   ptl_pt_index_t portal,
38                   ptl_process_id_t match_id, 
39                   ptl_match_bits_t match_bits, 
40                   ptl_match_bits_t ignore_bits,
41                   ptl_unlink_t unlink, ptl_ins_pos_t pos,
42                   ptl_handle_me_t *handle)
43 {
44         lib_nal_t    *nal = apinal->nal_data;
45         lib_ni_t     *ni = &nal->libnal_ni;
46         lib_ptl_t    *tbl = &ni->ni_portals;
47         lib_me_t     *me;
48         unsigned long flags;
49
50         if (portal >= tbl->size)
51                 return PTL_PT_INDEX_INVALID;
52
53         /* Should check for valid matchid, but not yet */
54
55         me = lib_me_alloc (nal);
56         if (me == NULL)
57                 return PTL_NO_SPACE;
58
59         LIB_LOCK(nal, flags);
60
61         me->match_id = match_id;
62         me->match_bits = match_bits;
63         me->ignore_bits = ignore_bits;
64         me->unlink = unlink;
65         me->md = NULL;
66
67         lib_initialise_handle (nal, &me->me_lh, PTL_COOKIE_TYPE_ME);
68
69         if (pos == PTL_INS_AFTER)
70                 list_add_tail(&me->me_list, &(tbl->tbl[portal]));
71         else
72                 list_add(&me->me_list, &(tbl->tbl[portal]));
73
74         ptl_me2handle(handle, nal, me);
75
76         LIB_UNLOCK(nal, flags);
77
78         return PTL_OK;
79 }
80
81 int
82 lib_api_me_insert(nal_t *apinal,
83                   ptl_handle_me_t *current_meh,
84                   ptl_process_id_t match_id, 
85                   ptl_match_bits_t match_bits, 
86                   ptl_match_bits_t ignore_bits,
87                   ptl_unlink_t unlink, ptl_ins_pos_t pos,
88                   ptl_handle_me_t *handle)
89 {
90         lib_nal_t    *nal = apinal->nal_data;
91         lib_me_t     *current_me;
92         lib_me_t     *new_me;
93         unsigned long flags;
94
95         new_me = lib_me_alloc (nal);
96         if (new_me == NULL)
97                 return PTL_NO_SPACE;
98
99         /* Should check for valid matchid, but not yet */
100
101         LIB_LOCK(nal, flags);
102
103         current_me = ptl_handle2me(current_meh, nal);
104         if (current_me == NULL) {
105                 lib_me_free (nal, new_me);
106
107                 LIB_UNLOCK(nal, flags);
108                 return PTL_ME_INVALID;
109         }
110
111         new_me->match_id = match_id;
112         new_me->match_bits = match_bits;
113         new_me->ignore_bits = ignore_bits;
114         new_me->unlink = unlink;
115         new_me->md = NULL;
116
117         lib_initialise_handle (nal, &new_me->me_lh, PTL_COOKIE_TYPE_ME);
118
119         if (pos == PTL_INS_AFTER)
120                 list_add_tail(&new_me->me_list, &current_me->me_list);
121         else
122                 list_add(&new_me->me_list, &current_me->me_list);
123
124         ptl_me2handle(handle, nal, new_me);
125
126         LIB_UNLOCK(nal, flags);
127
128         return PTL_OK;
129 }
130
131 int
132 lib_api_me_unlink (nal_t *apinal, ptl_handle_me_t *meh)
133 {
134         lib_nal_t    *nal = apinal->nal_data;
135         unsigned long flags;
136         lib_me_t     *me;
137         int           rc;
138
139         LIB_LOCK(nal, flags);
140
141         me = ptl_handle2me(meh, nal);
142         if (me == NULL) {
143                 rc = PTL_ME_INVALID;
144         } else {
145                 lib_me_unlink(nal, me);
146                 rc = PTL_OK;
147         }
148
149         LIB_UNLOCK(nal, flags);
150
151         return (rc);
152 }
153
154 /* call with state_lock please */
155 void 
156 lib_me_unlink(lib_nal_t *nal, lib_me_t *me)
157 {
158         list_del (&me->me_list);
159
160         if (me->md) {
161                 me->md->me = NULL;
162                 lib_md_unlink(nal, me->md);
163         }
164
165         lib_invalidate_handle (nal, &me->me_lh);
166         lib_me_free(nal, me);
167 }
168
169 #if 0
170 static void 
171 lib_me_dump(lib_nal_t *nal, lib_me_t * me)
172 {
173         CWARN("Match Entry %p ("LPX64")\n", me, 
174               me->me_lh.lh_cookie);
175
176         CWARN("\tMatch/Ignore\t= %016lx / %016lx\n",
177               me->match_bits, me->ignore_bits);
178
179         CWARN("\tMD\t= %p\n", me->md);
180         CWARN("\tprev\t= %p\n",
181               list_entry(me->me_list.prev, lib_me_t, me_list));
182         CWARN("\tnext\t= %p\n",
183               list_entry(me->me_list.next, lib_me_t, me_list));
184 }
185 #endif