Whamcloud - gitweb
8df99176397af9ae44048852dc1f9ab1a509c108
[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         struct lnet_match_table *mtable;
81         struct lnet_me          *me;
82         cfs_list_t              *head;
83
84         LASSERT(the_lnet.ln_init);
85         LASSERT(the_lnet.ln_refcount > 0);
86
87         if ((int)portal >= the_lnet.ln_nportals)
88                 return -EINVAL;
89
90         mtable = lnet_mt_of_attach(portal, match_id,
91                                    match_bits, ignore_bits, pos);
92         if (mtable == NULL) /* can't match portal type */
93                 return -EPERM;
94
95         me = lnet_me_alloc();
96         if (me == NULL)
97                 return -ENOMEM;
98
99         lnet_res_lock(mtable->mt_cpt);
100
101         me->me_portal = portal;
102         me->me_match_id = match_id;
103         me->me_match_bits = match_bits;
104         me->me_ignore_bits = ignore_bits;
105         me->me_unlink = unlink;
106         me->me_md = NULL;
107
108         lnet_res_lh_initialize(the_lnet.ln_me_containers[mtable->mt_cpt],
109                                &me->me_lh);
110         if (ignore_bits != 0)
111                 head = &mtable->mt_mhash[LNET_MT_HASH_IGNORE];
112         else
113                 head = lnet_mt_match_head(mtable, match_id, match_bits);
114
115         me->me_pos = head - &mtable->mt_mhash[0];
116         if (pos == LNET_INS_AFTER || pos == LNET_INS_LOCAL)
117                 cfs_list_add_tail(&me->me_list, head);
118         else
119                 cfs_list_add(&me->me_list, head);
120
121         lnet_me2handle(handle, me);
122
123         lnet_res_unlock(mtable->mt_cpt);
124         return 0;
125 }
126 EXPORT_SYMBOL(LNetMEAttach);
127
128 /**
129  * Create and a match entry and insert it before or after the ME pointed to by
130  * \a current_meh. The new ME is empty, i.e. not associated with a memory
131  * descriptor. LNetMDAttach() can be used to attach a MD to an empty ME.
132  *
133  * This function is identical to LNetMEAttach() except for the position
134  * where the new ME is inserted.
135  *
136  * \param current_meh A handle for a ME. The new ME will be inserted
137  * immediately before or immediately after this ME.
138  * \param match_id,match_bits,ignore_bits,unlink,pos,handle See the discussion
139  * for LNetMEAttach().
140  *
141  * \retval 0       On success.
142  * \retval -ENOMEM If new ME object cannot be allocated.
143  * \retval -ENOENT If \a current_meh does not point to a valid match entry.
144  */
145 int
146 LNetMEInsert(lnet_handle_me_t current_meh,
147              lnet_process_id_t match_id,
148              __u64 match_bits, __u64 ignore_bits,
149              lnet_unlink_t unlink, lnet_ins_pos_t pos,
150              lnet_handle_me_t *handle)
151 {
152         struct lnet_me          *current_me;
153         struct lnet_me          *new_me;
154         struct lnet_portal      *ptl;
155         int                     cpt;
156
157         LASSERT(the_lnet.ln_init);
158         LASSERT(the_lnet.ln_refcount > 0);
159
160         if (pos == LNET_INS_LOCAL)
161                 return -EPERM;
162
163         new_me = lnet_me_alloc();
164         if (new_me == NULL)
165                 return -ENOMEM;
166
167         cpt = lnet_cpt_of_cookie(current_meh.cookie);
168
169         lnet_res_lock(cpt);
170
171         current_me = lnet_handle2me(&current_meh);
172         if (current_me == NULL) {
173                 lnet_me_free_locked(new_me);
174
175                 lnet_res_unlock(cpt);
176                 return -ENOENT;
177         }
178
179         LASSERT(current_me->me_portal < the_lnet.ln_nportals);
180
181         ptl = the_lnet.ln_portals[current_me->me_portal];
182         if (lnet_ptl_is_unique(ptl)) {
183                 /* nosense to insertion on unique portal */
184                 lnet_me_free_locked(new_me);
185                 lnet_res_unlock(cpt);
186                 return -EPERM;
187         }
188
189         new_me->me_pos = current_me->me_pos;
190         new_me->me_portal = current_me->me_portal;
191         new_me->me_match_id = match_id;
192         new_me->me_match_bits = match_bits;
193         new_me->me_ignore_bits = ignore_bits;
194         new_me->me_unlink = unlink;
195         new_me->me_md = NULL;
196
197         lnet_res_lh_initialize(the_lnet.ln_me_containers[cpt], &new_me->me_lh);
198
199         if (pos == LNET_INS_AFTER)
200                 cfs_list_add(&new_me->me_list, &current_me->me_list);
201         else
202                 cfs_list_add_tail(&new_me->me_list, &current_me->me_list);
203
204         lnet_me2handle(handle, new_me);
205
206         lnet_res_unlock(cpt);
207
208         return 0;
209 }
210 EXPORT_SYMBOL(LNetMEInsert);
211
212 /**
213  * Unlink a match entry from its match list.
214  *
215  * This operation also releases any resources associated with the ME. If a
216  * memory descriptor is attached to the ME, then it will be unlinked as well
217  * and an unlink event will be generated. It is an error to use the ME handle
218  * after calling LNetMEUnlink().
219  *
220  * \param meh A handle for the ME to be unlinked.
221  *
222  * \retval 0       On success.
223  * \retval -ENOENT If \a meh does not point to a valid ME.
224  * \see LNetMDUnlink() for the discussion on delivering unlink event.
225  */
226 int
227 LNetMEUnlink(lnet_handle_me_t meh)
228 {
229         lnet_me_t       *me;
230         lnet_libmd_t    *md;
231         lnet_event_t    ev;
232         int             cpt;
233
234         LASSERT(the_lnet.ln_init);
235         LASSERT(the_lnet.ln_refcount > 0);
236
237         cpt = lnet_cpt_of_cookie(meh.cookie);
238         lnet_res_lock(cpt);
239
240         me = lnet_handle2me(&meh);
241         if (me == NULL) {
242                 lnet_res_unlock(cpt);
243                 return -ENOENT;
244         }
245
246         md = me->me_md;
247         if (md != NULL &&
248             md->md_eq != NULL &&
249             md->md_refcount == 0) {
250                 lnet_build_unlink_event(md, &ev);
251                 lnet_eq_enqueue_event(md->md_eq, &ev);
252         }
253
254         lnet_me_unlink(me);
255
256         lnet_res_unlock(cpt);
257         return 0;
258 }
259 EXPORT_SYMBOL(LNetMEUnlink);
260
261 /* call with lnet_res_lock please */
262 void
263 lnet_me_unlink(lnet_me_t *me)
264 {
265         cfs_list_del(&me->me_list);
266
267         if (me->me_md != NULL) {
268                 lnet_libmd_t *md = me->me_md;
269
270                 /* detach MD from portal of this ME */
271                 lnet_ptl_detach_md(me, md);
272                 lnet_md_unlink(md);
273         }
274
275         lnet_res_lh_invalidate(&me->me_lh);
276         lnet_me_free_locked(me);
277 }
278
279 #if 0
280 static void
281 lib_me_dump(lnet_me_t *me)
282 {
283         CWARN("Match Entry %p ("LPX64")\n", me,
284               me->me_lh.lh_cookie);
285
286         CWARN("\tMatch/Ignore\t= %016lx / %016lx\n",
287               me->me_match_bits, me->me_ignore_bits);
288
289         CWARN("\tMD\t= %p\n", me->md);
290         CWARN("\tprev\t= %p\n",
291               cfs_list_entry(me->me_list.prev, lnet_me_t, me_list));
292         CWARN("\tnext\t= %p\n",
293               cfs_list_entry(me->me_list.next, lnet_me_t, me_list));
294 }
295 #endif