Whamcloud - gitweb
b=15272
[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         lnet_libmd_t *md;
124         lnet_event_t  ev;
125
126         LASSERT (the_lnet.ln_init);
127         LASSERT (the_lnet.ln_refcount > 0);
128
129         LNET_LOCK();
130
131         me = lnet_handle2me(&meh);
132         if (me == NULL) {
133                 LNET_UNLOCK();
134                 return -ENOENT;
135         }
136
137         md = me->me_md;
138         if (md != NULL &&
139             md->md_eq != NULL &&
140             md->md_refcount == 0) {
141                 lnet_build_unlink_event(md, &ev);
142                 lnet_enq_event_locked(md->md_eq, &ev);
143         }
144
145         lnet_me_unlink(me);
146
147         LNET_UNLOCK();
148         return 0;
149 }
150
151 /* call with LNET_LOCK please */
152 void
153 lnet_me_unlink(lnet_me_t *me)
154 {
155         list_del (&me->me_list);
156
157         if (me->me_md != NULL) {
158                 me->me_md->md_me = NULL;
159                 lnet_md_unlink(me->me_md);
160         }
161
162         lnet_invalidate_handle (&me->me_lh);
163         lnet_me_free(me);
164 }
165
166 #if 0
167 static void
168 lib_me_dump(lnet_me_t *me)
169 {
170         CWARN("Match Entry %p ("LPX64")\n", me,
171               me->me_lh.lh_cookie);
172
173         CWARN("\tMatch/Ignore\t= %016lx / %016lx\n",
174               me->me_match_bits, me->me_ignore_bits);
175
176         CWARN("\tMD\t= %p\n", me->md);
177         CWARN("\tprev\t= %p\n",
178               list_entry(me->me_list.prev, lnet_me_t, me_list));
179         CWARN("\tnext\t= %p\n",
180               list_entry(me->me_list.next, lnet_me_t, me_list));
181 }
182 #endif