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