Whamcloud - gitweb
use generic LIST_HEAD macros instead of linux specific.
[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 #define DEBUG_SUBSYSTEM S_LNET
26
27 #include <lnet/lib-lnet.h>
28
29 int
30 LNetMEAttach(unsigned int portal,
31              lnet_process_id_t match_id, 
32              __u64 match_bits, __u64 ignore_bits,
33              lnet_unlink_t unlink, lnet_ins_pos_t pos, 
34              lnet_handle_me_t *handle)
35 {
36         lnet_me_t     *me;
37
38         LASSERT (the_lnet.ln_init);
39         LASSERT (the_lnet.ln_refcount > 0);
40         
41         if (portal >= the_lnet.ln_nportals)
42                 return -EINVAL;
43
44         me = lnet_me_alloc();
45         if (me == NULL)
46                 return -ENOMEM;
47
48         LNET_LOCK();
49
50         me->me_portal = portal;
51         me->me_match_id = match_id;
52         me->me_match_bits = match_bits;
53         me->me_ignore_bits = ignore_bits;
54         me->me_unlink = unlink;
55         me->me_md = NULL;
56
57         lnet_initialise_handle (&me->me_lh, LNET_COOKIE_TYPE_ME);
58
59         if (pos == LNET_INS_AFTER)
60                 list_add_tail(&me->me_list, &(the_lnet.ln_portals[portal].ptl_ml));
61         else
62                 list_add(&me->me_list, &(the_lnet.ln_portals[portal].ptl_ml));
63
64         lnet_me2handle(handle, me);
65
66         LNET_UNLOCK();
67
68         return 0;
69 }
70
71 int 
72 LNetMEInsert(lnet_handle_me_t current_meh, 
73              lnet_process_id_t match_id, 
74              __u64 match_bits, __u64 ignore_bits,
75              lnet_unlink_t unlink, lnet_ins_pos_t pos,
76              lnet_handle_me_t *handle)
77 {
78         lnet_me_t     *current_me;
79         lnet_me_t     *new_me;
80
81         LASSERT (the_lnet.ln_init);        
82         LASSERT (the_lnet.ln_refcount > 0);
83         
84         new_me = lnet_me_alloc();
85         if (new_me == NULL)
86                 return -ENOMEM;
87
88         LNET_LOCK();
89
90         current_me = lnet_handle2me(&current_meh);
91         if (current_me == NULL) {
92                 lnet_me_free (new_me);
93
94                 LNET_UNLOCK();
95                 return -ENOENT;
96         }
97
98         new_me->me_portal = current_me->me_portal;
99         new_me->me_match_id = match_id;
100         new_me->me_match_bits = match_bits;
101         new_me->me_ignore_bits = ignore_bits;
102         new_me->me_unlink = unlink;
103         new_me->me_md = NULL;
104
105         lnet_initialise_handle (&new_me->me_lh, LNET_COOKIE_TYPE_ME);
106
107         if (pos == LNET_INS_AFTER)
108                 list_add_tail(&new_me->me_list, &current_me->me_list);
109         else
110                 list_add(&new_me->me_list, &current_me->me_list);
111
112         lnet_me2handle(handle, new_me);
113
114         LNET_UNLOCK();
115
116         return 0;
117 }
118
119 int
120 LNetMEUnlink(lnet_handle_me_t meh)
121 {
122         lnet_me_t     *me;
123         int           rc;
124
125         LASSERT (the_lnet.ln_init);        
126         LASSERT (the_lnet.ln_refcount > 0);
127         
128         LNET_LOCK();
129
130         me = lnet_handle2me(&meh);
131         if (me == NULL) {
132                 rc = -ENOENT;
133         } else {
134                 lnet_me_unlink(me);
135                 rc = 0;
136         }
137
138         LNET_UNLOCK();
139
140         return (rc);
141 }
142
143 /* call with LNET_LOCK please */
144 void
145 lnet_me_unlink(lnet_me_t *me)
146 {
147         list_del (&me->me_list);
148
149         if (me->me_md != NULL) {
150                 me->me_md->md_me = NULL;
151                 lnet_md_unlink(me->me_md);
152         }
153
154         lnet_invalidate_handle (&me->me_lh);
155         lnet_me_free(me);
156 }
157
158 #if 0
159 static void
160 lib_me_dump(lnet_me_t *me)
161 {
162         CWARN("Match Entry %p ("LPX64")\n", me,
163               me->me_lh.lh_cookie);
164
165         CWARN("\tMatch/Ignore\t= %016lx / %016lx\n",
166               me->me_match_bits, me->me_ignore_bits);
167
168         CWARN("\tMD\t= %p\n", me->md);
169         CWARN("\tprev\t= %p\n",
170               list_entry(me->me_list.prev, lnet_me_t, me_list));
171         CWARN("\tnext\t= %p\n",
172               list_entry(me->me_list.next, lnet_me_t, me_list));
173 }
174 #endif