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