Whamcloud - gitweb
LU-56 lnet: match-table for Portals
[fs/lustre-release.git] / lnet / lnet / lib-md.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-md.c
35  *
36  * Memory Descriptor management routines
37  */
38
39 #define DEBUG_SUBSYSTEM S_LNET
40
41 #include <lnet/lib-lnet.h>
42
43 /* must be called with LNET_LOCK held */
44 void
45 lnet_md_unlink(lnet_libmd_t *md)
46 {
47         if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) == 0) {
48                 /* first unlink attempt... */
49                 lnet_me_t *me = md->md_me;
50
51                 md->md_flags |= LNET_MD_FLAG_ZOMBIE;
52
53                 /* Disassociate from ME (if any), and unlink it if it was created
54                  * with LNET_UNLINK */
55                 if (me != NULL) {
56                         /* detach MD from portal */
57                         lnet_ptl_detach_md(me, md);
58                         if (me->me_unlink == LNET_UNLINK)
59                                 lnet_me_unlink(me);
60                 }
61
62                 /* ensure all future handle lookups fail */
63                 lnet_res_lh_invalidate(&md->md_lh);
64         }
65
66         if (md->md_refcount != 0) {
67                 CDEBUG(D_NET, "Queueing unlink of md %p\n", md);
68                 return;
69         }
70
71         CDEBUG(D_NET, "Unlinking md %p\n", md);
72
73         if (md->md_eq != NULL) {
74                 md->md_eq->eq_refcount--;
75                 LASSERT (md->md_eq->eq_refcount >= 0);
76         }
77
78         LASSERT (!cfs_list_empty(&md->md_list));
79         cfs_list_del_init (&md->md_list);
80         lnet_md_free_locked(md);
81 }
82
83 static int
84 lnet_md_build(lnet_libmd_t *lmd, lnet_md_t *umd, int unlink)
85 {
86         int          i;
87         unsigned int niov;
88         int          total_length = 0;
89
90         lmd->md_me = NULL;
91         lmd->md_start = umd->start;
92         lmd->md_offset = 0;
93         lmd->md_max_size = umd->max_size;
94         lmd->md_options = umd->options;
95         lmd->md_user_ptr = umd->user_ptr;
96         lmd->md_eq = NULL;
97         lmd->md_threshold = umd->threshold;
98         lmd->md_refcount = 0;
99         lmd->md_flags = (unlink == LNET_UNLINK) ? LNET_MD_FLAG_AUTO_UNLINK : 0;
100
101         if ((umd->options & LNET_MD_IOVEC) != 0) {
102
103                 if ((umd->options & LNET_MD_KIOV) != 0) /* Can't specify both */
104                         return -EINVAL;
105
106                 lmd->md_niov = niov = umd->length;
107                 memcpy(lmd->md_iov.iov, umd->start,
108                        niov * sizeof (lmd->md_iov.iov[0]));
109
110                 for (i = 0; i < (int)niov; i++) {
111                         /* We take the base address on trust */
112                         if (lmd->md_iov.iov[i].iov_len <= 0) /* invalid length */
113                                 return -EINVAL;
114
115                         total_length += lmd->md_iov.iov[i].iov_len;
116                 }
117
118                 lmd->md_length = total_length;
119
120                 if ((umd->options & LNET_MD_MAX_SIZE) != 0 && /* max size used */
121                     (umd->max_size < 0 ||
122                      umd->max_size > total_length)) // illegal max_size
123                         return -EINVAL;
124
125         } else if ((umd->options & LNET_MD_KIOV) != 0) {
126 #ifndef __KERNEL__
127                 return -EINVAL;
128 #else
129                 lmd->md_niov = niov = umd->length;
130                 memcpy(lmd->md_iov.kiov, umd->start,
131                        niov * sizeof (lmd->md_iov.kiov[0]));
132
133                 for (i = 0; i < (int)niov; i++) {
134                         /* We take the page pointer on trust */
135                         if (lmd->md_iov.kiov[i].kiov_offset +
136                             lmd->md_iov.kiov[i].kiov_len > CFS_PAGE_SIZE )
137                                 return -EINVAL; /* invalid length */
138
139                         total_length += lmd->md_iov.kiov[i].kiov_len;
140                 }
141
142                 lmd->md_length = total_length;
143
144                 if ((umd->options & LNET_MD_MAX_SIZE) != 0 && /* max size used */
145                     (umd->max_size < 0 ||
146                      umd->max_size > total_length)) // illegal max_size
147                         return -EINVAL;
148 #endif
149         } else {   /* contiguous */
150                 lmd->md_length = umd->length;
151                 lmd->md_niov = niov = 1;
152                 lmd->md_iov.iov[0].iov_base = umd->start;
153                 lmd->md_iov.iov[0].iov_len = umd->length;
154
155                 if ((umd->options & LNET_MD_MAX_SIZE) != 0 && /* max size used */
156                     (umd->max_size < 0 ||
157                      umd->max_size > (int)umd->length)) // illegal max_size
158                         return -EINVAL;
159         }
160
161         return 0;
162 }
163
164 /* must be called with resource lock held */
165 static int
166 lnet_md_link(lnet_libmd_t *md, lnet_handle_eq_t eq_handle)
167 {
168         struct lnet_res_container *container = &the_lnet.ln_md_container;
169
170         /* NB we are passed an allocated, but inactive md.
171          * if we return success, caller may lnet_md_unlink() it.
172          * otherwise caller may only lnet_md_free() it.
173          */
174         /* This implementation doesn't know how to create START events or
175          * disable END events.  Best to LASSERT our caller is compliant so
176          * we find out quickly...  */
177         /*  TODO - reevaluate what should be here in light of
178          * the removal of the start and end events
179          * maybe there we shouldn't even allow LNET_EQ_NONE!)
180          * LASSERT (eq == NULL);
181          */
182         if (!LNetHandleIsInvalid(eq_handle)) {
183                 md->md_eq = lnet_handle2eq(&eq_handle);
184
185                 if (md->md_eq == NULL)
186                         return -ENOENT;
187
188                 md->md_eq->eq_refcount++;
189         }
190
191         lnet_res_lh_initialize(container, &md->md_lh);
192
193         LASSERT(cfs_list_empty(&md->md_list));
194         cfs_list_add(&md->md_list, &container->rec_active);
195
196         return 0;
197 }
198
199 /* must be called with LNET_LOCK held */
200 void
201 lnet_md_deconstruct(lnet_libmd_t *lmd, lnet_md_t *umd)
202 {
203         /* NB this doesn't copy out all the iov entries so when a
204          * discontiguous MD is copied out, the target gets to know the
205          * original iov pointer (in start) and the number of entries it had
206          * and that's all.
207          */
208         umd->start = lmd->md_start;
209         umd->length = ((lmd->md_options & (LNET_MD_IOVEC | LNET_MD_KIOV)) == 0) ?
210                       lmd->md_length : lmd->md_niov;
211         umd->threshold = lmd->md_threshold;
212         umd->max_size = lmd->md_max_size;
213         umd->options = lmd->md_options;
214         umd->user_ptr = lmd->md_user_ptr;
215         lnet_eq2handle(&umd->eq_handle, lmd->md_eq);
216 }
217
218 int
219 lnet_md_validate(lnet_md_t *umd)
220 {
221         if (umd->start == NULL && umd->length != 0) {
222                 CERROR("MD start pointer can not be NULL with length %u\n",
223                        umd->length);
224                 return -EINVAL;
225         }
226
227         if ((umd->options & (LNET_MD_KIOV | LNET_MD_IOVEC)) != 0 &&
228             umd->length > LNET_MAX_IOV) {
229                 CERROR("Invalid option: too many fragments %u, %d max\n",
230                        umd->length, LNET_MAX_IOV);
231                 return -EINVAL;
232         }
233
234         return 0;
235 }
236
237 /**
238  * Create a memory descriptor and attach it to a ME
239  *
240  * \param meh A handle for a ME to associate the new MD with.
241  * \param umd Provides initial values for the user-visible parts of a MD.
242  * Other than its use for initialization, there is no linkage between this
243  * structure and the MD maintained by the LNet.
244  * \param unlink A flag to indicate whether the MD is automatically unlinked
245  * when it becomes inactive, either because the operation threshold drops to
246  * zero or because the available memory becomes less than \a umd.max_size.
247  * (Note that the check for unlinking a MD only occurs after the completion
248  * of a successful operation on the MD.) The value LNET_UNLINK enables auto
249  * unlinking; the value LNET_RETAIN disables it.
250  * \param handle On successful returns, a handle to the newly created MD is
251  * saved here. This handle can be used later in LNetMDUnlink().
252  *
253  * \retval 0       On success.
254  * \retval -EINVAL If \a umd is not valid.
255  * \retval -ENOMEM If new MD cannot be allocated.
256  * \retval -ENOENT Either \a meh or \a umd.eq_handle does not point to a
257  * valid object. Note that it's OK to supply a NULL \a umd.eq_handle by
258  * calling LNetInvalidateHandle() on it.
259  * \retval -EBUSY  If the ME pointed to by \a meh is already associated with
260  * a MD.
261  */
262 int
263 LNetMDAttach(lnet_handle_me_t meh, lnet_md_t umd,
264              lnet_unlink_t unlink, lnet_handle_md_t *handle)
265 {
266         CFS_LIST_HEAD   (matches);
267         CFS_LIST_HEAD   (drops);
268         lnet_me_t     *me;
269         lnet_libmd_t  *md;
270         int            rc;
271
272         LASSERT (the_lnet.ln_init);
273         LASSERT (the_lnet.ln_refcount > 0);
274
275         if (lnet_md_validate(&umd) != 0)
276                 return -EINVAL;
277
278         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) == 0) {
279                 CERROR("Invalid option: no MD_OP set\n");
280                 return -EINVAL;
281         }
282
283         md = lnet_md_alloc(&umd);
284         if (md == NULL)
285                 return -ENOMEM;
286
287         rc = lnet_md_build(md, &umd, unlink);
288
289         LNET_LOCK();
290         if (rc != 0)
291                 goto failed;
292
293         me = lnet_handle2me(&meh);
294         if (me == NULL)
295                 rc = -ENOENT;
296         else if (me->me_md != NULL)
297                 rc = -EBUSY;
298         else
299                 rc = lnet_md_link(md, umd.eq_handle);
300
301         if (rc != 0)
302                 goto failed;
303
304         /* attach this MD to portal of ME and check if it matches any
305          * blocked msgs on this portal */
306         lnet_ptl_attach_md(me, md, &matches, &drops);
307
308         lnet_md2handle(handle, md);
309
310         LNET_UNLOCK();
311
312         lnet_drop_delayed_msg_list(&drops, "Bad match");
313         lnet_recv_delayed_msg_list(&matches);
314
315         return 0;
316
317  failed:
318         lnet_md_free_locked(md);
319
320         LNET_UNLOCK();
321         return rc;
322 }
323
324 /**
325  * Create a "free floating" memory descriptor - a MD that is not associated
326  * with a ME. Such MDs are usually used in LNetPut() and LNetGet() operations.
327  *
328  * \param umd,unlink See the discussion for LNetMDAttach().
329  * \param handle On successful returns, a handle to the newly created MD is
330  * saved here. This handle can be used later in LNetMDUnlink(), LNetPut(),
331  * and LNetGet() operations.
332  *
333  * \retval 0       On success.
334  * \retval -EINVAL If \a umd is not valid.
335  * \retval -ENOMEM If new MD cannot be allocated.
336  * \retval -ENOENT \a umd.eq_handle does not point to a valid EQ. Note that
337  * it's OK to supply a NULL \a umd.eq_handle by calling
338  * LNetInvalidateHandle() on it.
339  */
340 int
341 LNetMDBind(lnet_md_t umd, lnet_unlink_t unlink, lnet_handle_md_t *handle)
342 {
343         lnet_libmd_t  *md;
344         int            rc;
345
346         LASSERT (the_lnet.ln_init);
347         LASSERT (the_lnet.ln_refcount > 0);
348
349         if (lnet_md_validate(&umd) != 0)
350                 return -EINVAL;
351
352         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) != 0) {
353                 CERROR("Invalid option: GET|PUT illegal on active MDs\n");
354                 return -EINVAL;
355         }
356
357         md = lnet_md_alloc(&umd);
358         if (md == NULL)
359                 return -ENOMEM;
360
361         rc = lnet_md_build(md, &umd, unlink);
362
363         LNET_LOCK();
364         if (rc != 0)
365                 goto failed;
366
367         rc = lnet_md_link(md, umd.eq_handle);
368         if (rc != 0)
369                 goto failed;
370
371         lnet_md2handle(handle, md);
372
373         LNET_UNLOCK();
374         return 0;
375
376  failed:
377         lnet_md_free_locked(md);
378
379         LNET_UNLOCK();
380         return rc;
381 }
382
383 /**
384  * Unlink the memory descriptor from any ME it may be linked to and release
385  * the internal resources associated with it.
386  *
387  * This function does not free the memory region associated with the MD;
388  * i.e., the memory the user allocated for this MD. If the ME associated with
389  * this MD is not NULL and was created with auto unlink enabled, the ME is
390  * unlinked as well (see LNetMEAttach()).
391  *
392  * Explicitly unlinking a MD via this function call has the same behavior as
393  * a MD that has been automatically unlinked, except that no LNET_EVENT_UNLINK
394  * is generated in the latter case.
395  *
396  * An unlinked event can be reported in two ways:
397  * - If there's no pending operations on the MD, it's unlinked immediately
398  *   and an LNET_EVENT_UNLINK event is logged before this function returns.
399  * - Otherwise, the MD is only marked for deletion when this function
400  *   returns, and the unlinked event will be piggybacked on the event of
401  *   the completion of the last operation by setting the unlinked field of
402  *   the event. No dedicated LNET_EVENT_UNLINK event is generated.
403  *
404  * Note that in both cases the unlinked field of the event is always set; no
405  * more event will happen on the MD after such an event is logged.
406  *
407  * \param mdh A handle for the MD to be unlinked.
408  *
409  * \retval 0       On success.
410  * \retval -ENOENT If \a mdh does not point to a valid MD object.
411  */
412 int
413 LNetMDUnlink (lnet_handle_md_t mdh)
414 {
415         lnet_event_t     ev;
416         lnet_libmd_t    *md;
417
418         LASSERT (the_lnet.ln_init);
419         LASSERT (the_lnet.ln_refcount > 0);
420
421         LNET_LOCK();
422
423         md = lnet_handle2md(&mdh);
424         if (md == NULL) {
425                 LNET_UNLOCK();
426                 return -ENOENT;
427         }
428
429         /* If the MD is busy, lnet_md_unlink just marks it for deletion, and
430          * when the NAL is done, the completion event flags that the MD was
431          * unlinked.  Otherwise, we enqueue an event now... */
432
433         if (md->md_eq != NULL &&
434             md->md_refcount == 0) {
435                 lnet_build_unlink_event(md, &ev);
436                 lnet_eq_enqueue_event(md->md_eq, &ev);
437         }
438
439         lnet_md_unlink(md);
440
441         LNET_UNLOCK();
442         return 0;
443 }