Whamcloud - gitweb
LU-56 lnet: add lnet_*_free_locked for LNet
[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 static int
44 lnet_me_match_portal(lnet_portal_t *ptl, lnet_process_id_t id,
45                      __u64 match_bits, __u64 ignore_bits)
46 {
47         cfs_list_t       *mhash = NULL;
48         int               unique;
49
50         LASSERT (!(lnet_portal_is_unique(ptl) &&
51                    lnet_portal_is_wildcard(ptl)));
52
53         /* prefer to check w/o any lock */
54         unique = lnet_match_is_unique(id, match_bits, ignore_bits);
55         if (likely(lnet_portal_is_unique(ptl) ||
56                    lnet_portal_is_wildcard(ptl)))
57                 goto match;
58
59         /* unset, new portal */
60         if (unique) {
61                 mhash = lnet_portal_mhash_alloc();
62                 if (mhash == NULL)
63                         return -ENOMEM;
64         }
65
66         LNET_LOCK();
67         if (lnet_portal_is_unique(ptl) ||
68             lnet_portal_is_wildcard(ptl)) {
69                 /* someone set it before me */
70                 if (mhash != NULL)
71                         lnet_portal_mhash_free(mhash);
72                 LNET_UNLOCK();
73                 goto match;
74         }
75
76         /* still not set */
77         LASSERT (ptl->ptl_mhash == NULL);
78         if (unique) {
79                 ptl->ptl_mhash = mhash;
80                 lnet_portal_setopt(ptl, LNET_PTL_MATCH_UNIQUE);
81         } else {
82                 lnet_portal_setopt(ptl, LNET_PTL_MATCH_WILDCARD);
83         }
84         LNET_UNLOCK();
85         return 0;
86
87  match:
88         if (lnet_portal_is_unique(ptl) && !unique)
89                 return -EPERM;
90
91         if (lnet_portal_is_wildcard(ptl) && unique)
92                 return -EPERM;
93
94         return 0;
95 }
96
97 /**
98  * Create and attach a match entry to the match list of \a portal. The new
99  * ME is empty, i.e. not associated with a memory descriptor. LNetMDAttach()
100  * can be used to attach a MD to an empty ME.
101  *
102  * \param portal The portal table index where the ME should be attached.
103  * \param match_id Specifies the match criteria for the process ID of
104  * the requester. The constants LNET_PID_ANY and LNET_NID_ANY can be
105  * used to wildcard either of the identifiers in the lnet_process_id_t
106  * structure.
107  * \param match_bits,ignore_bits Specify the match criteria to apply
108  * to the match bits in the incoming request. The ignore bits are used
109  * to mask out insignificant bits in the incoming match bits. The resulting
110  * bits are then compared to the ME's match bits to determine if the
111  * incoming request meets the match criteria.
112  * \param unlink Indicates whether the ME should be unlinked when the memory
113  * descriptor associated with it is unlinked (Note that the check for
114  * unlinking a ME only occurs when the memory descriptor is unlinked.).
115  * Valid values are LNET_RETAIN and LNET_UNLINK.
116  * \param pos Indicates whether the new ME should be prepended or
117  * appended to the match list. Allowed constants: LNET_INS_BEFORE,
118  * LNET_INS_AFTER.
119  * \param handle On successful returns, a handle to the newly created ME
120  * object is saved here. This handle can be used later in LNetMEInsert(),
121  * LNetMEUnlink(), or LNetMDAttach() functions.
122  *
123  * \retval 0       On success.
124  * \retval -EINVAL If \a portal is invalid.
125  * \retval -ENOMEM If new ME object cannot be allocated.
126  */
127 int
128 LNetMEAttach(unsigned int portal,
129              lnet_process_id_t match_id,
130              __u64 match_bits, __u64 ignore_bits,
131              lnet_unlink_t unlink, lnet_ins_pos_t pos,
132              lnet_handle_me_t *handle)
133 {
134         lnet_me_t        *me;
135         lnet_portal_t    *ptl;
136         cfs_list_t       *head;
137         int               rc;
138
139         LASSERT (the_lnet.ln_init);
140         LASSERT (the_lnet.ln_refcount > 0);
141
142         if ((int)portal >= the_lnet.ln_nportals)
143                 return -EINVAL;
144
145         ptl = &the_lnet.ln_portals[portal];
146         rc = lnet_me_match_portal(ptl, match_id, match_bits, ignore_bits);
147         if (rc != 0)
148                 return rc;
149
150         me = lnet_me_alloc();
151         if (me == NULL)
152                 return -ENOMEM;
153
154         LNET_LOCK();
155
156         me->me_portal = portal;
157         me->me_match_id = match_id;
158         me->me_match_bits = match_bits;
159         me->me_ignore_bits = ignore_bits;
160         me->me_unlink = unlink;
161         me->me_md = NULL;
162
163         lnet_initialise_handle (&me->me_lh, LNET_COOKIE_TYPE_ME);
164         head = lnet_portal_me_head(portal, match_id, match_bits);
165         LASSERT (head != NULL);
166
167         if (pos == LNET_INS_AFTER)
168                 cfs_list_add_tail(&me->me_list, head);
169         else
170                 cfs_list_add(&me->me_list, head);
171
172         lnet_me2handle(handle, me);
173
174         LNET_UNLOCK();
175
176         return 0;
177 }
178
179 /**
180  * Create and a match entry and insert it before or after the ME pointed to by
181  * \a current_meh. The new ME is empty, i.e. not associated with a memory
182  * descriptor. LNetMDAttach() can be used to attach a MD to an empty ME.
183  *
184  * This function is identical to LNetMEAttach() except for the position
185  * where the new ME is inserted.
186  *
187  * \param current_meh A handle for a ME. The new ME will be inserted
188  * immediately before or immediately after this ME.
189  * \param match_id,match_bits,ignore_bits,unlink,pos,handle See the discussion
190  * for LNetMEAttach().
191  *
192  * \retval 0       On success.
193  * \retval -ENOMEM If new ME object cannot be allocated.
194  * \retval -ENOENT If \a current_meh does not point to a valid match entry.
195  */
196 int
197 LNetMEInsert(lnet_handle_me_t current_meh,
198              lnet_process_id_t match_id,
199              __u64 match_bits, __u64 ignore_bits,
200              lnet_unlink_t unlink, lnet_ins_pos_t pos,
201              lnet_handle_me_t *handle)
202 {
203         lnet_me_t     *current_me;
204         lnet_me_t     *new_me;
205         lnet_portal_t *ptl;
206
207         LASSERT (the_lnet.ln_init);
208         LASSERT (the_lnet.ln_refcount > 0);
209
210         new_me = lnet_me_alloc();
211         if (new_me == NULL)
212                 return -ENOMEM;
213
214         LNET_LOCK();
215
216         current_me = lnet_handle2me(&current_meh);
217         if (current_me == NULL) {
218                 lnet_me_free_locked(new_me);
219
220                 LNET_UNLOCK();
221                 return -ENOENT;
222         }
223
224         LASSERT (current_me->me_portal < the_lnet.ln_nportals);
225
226         ptl = &the_lnet.ln_portals[current_me->me_portal];
227         if (lnet_portal_is_unique(ptl)) {
228                 /* nosense to insertion on unique portal */
229                 lnet_me_free_locked(new_me);
230                 LNET_UNLOCK();
231                 return -EPERM;
232         }
233
234         new_me->me_portal = current_me->me_portal;
235         new_me->me_match_id = match_id;
236         new_me->me_match_bits = match_bits;
237         new_me->me_ignore_bits = ignore_bits;
238         new_me->me_unlink = unlink;
239         new_me->me_md = NULL;
240
241         lnet_initialise_handle (&new_me->me_lh, LNET_COOKIE_TYPE_ME);
242
243         if (pos == LNET_INS_AFTER)
244                 cfs_list_add(&new_me->me_list, &current_me->me_list);
245         else
246                 cfs_list_add_tail(&new_me->me_list, &current_me->me_list);
247
248         lnet_me2handle(handle, new_me);
249
250         LNET_UNLOCK();
251
252         return 0;
253 }
254
255 /**
256  * Unlink a match entry from its match list.
257  *
258  * This operation also releases any resources associated with the ME. If a
259  * memory descriptor is attached to the ME, then it will be unlinked as well
260  * and an unlink event will be generated. It is an error to use the ME handle
261  * after calling LNetMEUnlink().
262  *
263  * \param meh A handle for the ME to be unlinked.
264  *
265  * \retval 0       On success.
266  * \retval -ENOENT If \a meh does not point to a valid ME.
267  * \see LNetMDUnlink() for the discussion on delivering unlink event.
268  */
269 int
270 LNetMEUnlink(lnet_handle_me_t meh)
271 {
272         lnet_me_t    *me;
273         lnet_libmd_t *md;
274         lnet_event_t  ev;
275
276         LASSERT (the_lnet.ln_init);
277         LASSERT (the_lnet.ln_refcount > 0);
278
279         LNET_LOCK();
280
281         me = lnet_handle2me(&meh);
282         if (me == NULL) {
283                 LNET_UNLOCK();
284                 return -ENOENT;
285         }
286
287         md = me->me_md;
288         if (md != NULL &&
289             md->md_eq != NULL &&
290             md->md_refcount == 0) {
291                 lnet_build_unlink_event(md, &ev);
292                 lnet_enq_event_locked(md->md_eq, &ev);
293         }
294
295         lnet_me_unlink(me);
296
297         LNET_UNLOCK();
298         return 0;
299 }
300
301 /* call with LNET_LOCK please */
302 void
303 lnet_me_unlink(lnet_me_t *me)
304 {
305         cfs_list_del (&me->me_list);
306
307         if (me->me_md != NULL) {
308                 me->me_md->md_me = NULL;
309                 lnet_md_unlink(me->me_md);
310         }
311
312         lnet_invalidate_handle (&me->me_lh);
313         lnet_me_free_locked(me);
314 }
315
316 #if 0
317 static void
318 lib_me_dump(lnet_me_t *me)
319 {
320         CWARN("Match Entry %p ("LPX64")\n", me,
321               me->me_lh.lh_cookie);
322
323         CWARN("\tMatch/Ignore\t= %016lx / %016lx\n",
324               me->me_match_bits, me->me_ignore_bits);
325
326         CWARN("\tMD\t= %p\n", me->md);
327         CWARN("\tprev\t= %p\n",
328               cfs_list_entry(me->me_list.prev, lnet_me_t, me_list));
329         CWARN("\tnext\t= %p\n",
330               cfs_list_entry(me->me_list.next, lnet_me_t, me_list));
331 }
332 #endif