Whamcloud - gitweb
LU-56 lnet: router-checker (RC) cleanup
[fs/lustre-release.git] / lnet / lnet / lib-me.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lnet/lnet/lib-me.c
35  *
36  * Match Entry management routines
37  */
38
39 #define DEBUG_SUBSYSTEM S_LNET
40
41 #include <lnet/lib-lnet.h>
42
43 /**
44  * Create and attach a match entry to the match list of \a portal. The new
45  * ME is empty, i.e. not associated with a memory descriptor. LNetMDAttach()
46  * can be used to attach a MD to an empty ME.
47  *
48  * \param portal The portal table index where the ME should be attached.
49  * \param match_id Specifies the match criteria for the process ID of
50  * the requester. The constants LNET_PID_ANY and LNET_NID_ANY can be
51  * used to wildcard either of the identifiers in the lnet_process_id_t
52  * structure.
53  * \param match_bits,ignore_bits Specify the match criteria to apply
54  * to the match bits in the incoming request. The ignore bits are used
55  * to mask out insignificant bits in the incoming match bits. The resulting
56  * bits are then compared to the ME's match bits to determine if the
57  * incoming request meets the match criteria.
58  * \param unlink Indicates whether the ME should be unlinked when the memory
59  * descriptor associated with it is unlinked (Note that the check for
60  * unlinking a ME only occurs when the memory descriptor is unlinked.).
61  * Valid values are LNET_RETAIN and LNET_UNLINK.
62  * \param pos Indicates whether the new ME should be prepended or
63  * appended to the match list. Allowed constants: LNET_INS_BEFORE,
64  * LNET_INS_AFTER.
65  * \param handle On successful returns, a handle to the newly created ME
66  * object is saved here. This handle can be used later in LNetMEInsert(),
67  * LNetMEUnlink(), or LNetMDAttach() functions.
68  *
69  * \retval 0       On success.
70  * \retval -EINVAL If \a portal is invalid.
71  * \retval -ENOMEM If new ME object cannot be allocated.
72  */
73 int
74 LNetMEAttach(unsigned int portal,
75              lnet_process_id_t match_id,
76              __u64 match_bits, __u64 ignore_bits,
77              lnet_unlink_t unlink, lnet_ins_pos_t pos,
78              lnet_handle_me_t *handle)
79 {
80         lnet_me_t        *me;
81         lnet_portal_t    *ptl;
82         cfs_list_t       *head;
83         int               rc;
84
85         LASSERT (the_lnet.ln_init);
86         LASSERT (the_lnet.ln_refcount > 0);
87
88         if ((int)portal >= the_lnet.ln_nportals)
89                 return -EINVAL;
90
91         ptl = the_lnet.ln_portals[portal];
92         rc = lnet_ptl_type_match(ptl, match_id, match_bits, ignore_bits);
93         if (!rc)
94                 return -EPERM;
95
96         me = lnet_me_alloc();
97         if (me == NULL)
98                 return -ENOMEM;
99
100         LNET_LOCK();
101
102         me->me_portal = portal;
103         me->me_match_id = match_id;
104         me->me_match_bits = match_bits;
105         me->me_ignore_bits = ignore_bits;
106         me->me_unlink = unlink;
107         me->me_md = NULL;
108
109         lnet_res_lh_initialize(&the_lnet.ln_me_container, &me->me_lh);
110         head = lnet_ptl_me_head(portal, match_id, match_bits);
111         LASSERT (head != NULL);
112
113         if (pos == LNET_INS_AFTER)
114                 cfs_list_add_tail(&me->me_list, head);
115         else
116                 cfs_list_add(&me->me_list, head);
117
118         lnet_me2handle(handle, me);
119
120         LNET_UNLOCK();
121
122         return 0;
123 }
124
125 /**
126  * Create and a match entry and insert it before or after the ME pointed to by
127  * \a current_meh. The new ME is empty, i.e. not associated with a memory
128  * descriptor. LNetMDAttach() can be used to attach a MD to an empty ME.
129  *
130  * This function is identical to LNetMEAttach() except for the position
131  * where the new ME is inserted.
132  *
133  * \param current_meh A handle for a ME. The new ME will be inserted
134  * immediately before or immediately after this ME.
135  * \param match_id,match_bits,ignore_bits,unlink,pos,handle See the discussion
136  * for LNetMEAttach().
137  *
138  * \retval 0       On success.
139  * \retval -ENOMEM If new ME object cannot be allocated.
140  * \retval -ENOENT If \a current_meh does not point to a valid match entry.
141  */
142 int
143 LNetMEInsert(lnet_handle_me_t current_meh,
144              lnet_process_id_t match_id,
145              __u64 match_bits, __u64 ignore_bits,
146              lnet_unlink_t unlink, lnet_ins_pos_t pos,
147              lnet_handle_me_t *handle)
148 {
149         lnet_me_t     *current_me;
150         lnet_me_t     *new_me;
151         lnet_portal_t *ptl;
152
153         LASSERT (the_lnet.ln_init);
154         LASSERT (the_lnet.ln_refcount > 0);
155
156         new_me = lnet_me_alloc();
157         if (new_me == NULL)
158                 return -ENOMEM;
159
160         LNET_LOCK();
161
162         current_me = lnet_handle2me(&current_meh);
163         if (current_me == NULL) {
164                 lnet_me_free_locked(new_me);
165
166                 LNET_UNLOCK();
167                 return -ENOENT;
168         }
169
170         LASSERT (current_me->me_portal < the_lnet.ln_nportals);
171
172         ptl = the_lnet.ln_portals[current_me->me_portal];
173         if (lnet_ptl_is_unique(ptl)) {
174                 /* nosense to insertion on unique portal */
175                 lnet_me_free_locked(new_me);
176                 LNET_UNLOCK();
177                 return -EPERM;
178         }
179
180         new_me->me_portal = current_me->me_portal;
181         new_me->me_match_id = match_id;
182         new_me->me_match_bits = match_bits;
183         new_me->me_ignore_bits = ignore_bits;
184         new_me->me_unlink = unlink;
185         new_me->me_md = NULL;
186
187         lnet_res_lh_initialize(&the_lnet.ln_me_container, &new_me->me_lh);
188
189         if (pos == LNET_INS_AFTER)
190                 cfs_list_add(&new_me->me_list, &current_me->me_list);
191         else
192                 cfs_list_add_tail(&new_me->me_list, &current_me->me_list);
193
194         lnet_me2handle(handle, new_me);
195
196         LNET_UNLOCK();
197
198         return 0;
199 }
200
201 /**
202  * Unlink a match entry from its match list.
203  *
204  * This operation also releases any resources associated with the ME. If a
205  * memory descriptor is attached to the ME, then it will be unlinked as well
206  * and an unlink event will be generated. It is an error to use the ME handle
207  * after calling LNetMEUnlink().
208  *
209  * \param meh A handle for the ME to be unlinked.
210  *
211  * \retval 0       On success.
212  * \retval -ENOENT If \a meh does not point to a valid ME.
213  * \see LNetMDUnlink() for the discussion on delivering unlink event.
214  */
215 int
216 LNetMEUnlink(lnet_handle_me_t meh)
217 {
218         lnet_me_t    *me;
219         lnet_libmd_t *md;
220         lnet_event_t  ev;
221
222         LASSERT (the_lnet.ln_init);
223         LASSERT (the_lnet.ln_refcount > 0);
224
225         LNET_LOCK();
226
227         me = lnet_handle2me(&meh);
228         if (me == NULL) {
229                 LNET_UNLOCK();
230                 return -ENOENT;
231         }
232
233         md = me->me_md;
234         if (md != NULL &&
235             md->md_eq != NULL &&
236             md->md_refcount == 0) {
237                 lnet_build_unlink_event(md, &ev);
238                 lnet_eq_enqueue_event(md->md_eq, &ev);
239         }
240
241         lnet_me_unlink(me);
242
243         LNET_UNLOCK();
244         return 0;
245 }
246
247 /* call with LNET_LOCK please */
248 void
249 lnet_me_unlink(lnet_me_t *me)
250 {
251         cfs_list_del (&me->me_list);
252
253         if (me->me_md != NULL) {
254                 me->me_md->md_me = NULL;
255                 lnet_md_unlink(me->me_md);
256         }
257
258         lnet_res_lh_invalidate(&me->me_lh);
259         lnet_me_free_locked(me);
260 }
261
262 #if 0
263 static void
264 lib_me_dump(lnet_me_t *me)
265 {
266         CWARN("Match Entry %p ("LPX64")\n", me,
267               me->me_lh.lh_cookie);
268
269         CWARN("\tMatch/Ignore\t= %016lx / %016lx\n",
270               me->me_match_bits, me->me_ignore_bits);
271
272         CWARN("\tMD\t= %p\n", me->md);
273         CWARN("\tprev\t= %p\n",
274               cfs_list_entry(me->me_list.prev, lnet_me_t, me_list));
275         CWARN("\tnext\t= %p\n",
276               cfs_list_entry(me->me_list.next, lnet_me_t, me_list));
277 }
278 #endif