1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Memory Descriptor management routines
41 #define DEBUG_SUBSYSTEM S_LNET
43 #include <lnet/lib-lnet.h>
45 /* must be called with LNET_LOCK held */
47 lnet_md_unlink(lnet_libmd_t *md)
49 if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) == 0) {
50 /* first unlink attempt... */
51 lnet_me_t *me = md->md_me;
53 md->md_flags |= LNET_MD_FLAG_ZOMBIE;
55 /* Disassociate from ME (if any), and unlink it if it was created
60 if (me->me_unlink == LNET_UNLINK)
64 /* ensure all future handle lookups fail */
65 lnet_invalidate_handle(&md->md_lh);
68 if (md->md_refcount != 0) {
69 CDEBUG(D_NET, "Queueing unlink of md %p\n", md);
73 CDEBUG(D_NET, "Unlinking md %p\n", md);
75 if (md->md_eq != NULL) {
76 md->md_eq->eq_refcount--;
77 LASSERT (md->md_eq->eq_refcount >= 0);
80 LASSERT (!list_empty(&md->md_list));
81 list_del_init (&md->md_list);
85 /* must be called with LNET_LOCK held */
87 lib_md_build(lnet_libmd_t *lmd, lnet_md_t *umd, int unlink)
94 /* NB we are passed an allocated, but uninitialised/active md.
95 * if we return success, caller may lnet_md_unlink() it.
96 * otherwise caller may only lnet_md_free() it.
99 if (!LNetHandleIsInvalid (umd->eq_handle)) {
100 eq = lnet_handle2eq(&umd->eq_handle);
105 /* This implementation doesn't know how to create START events or
106 * disable END events. Best to LASSERT our caller is compliant so
107 * we find out quickly... */
108 /* TODO - reevaluate what should be here in light of
109 * the removal of the start and end events
110 * maybe there we shouldn't even allow LNET_EQ_NONE!)
111 LASSERT (eq == NULL);
115 lmd->md_start = umd->start;
117 lmd->md_max_size = umd->max_size;
118 lmd->md_options = umd->options;
119 lmd->md_user_ptr = umd->user_ptr;
121 lmd->md_threshold = umd->threshold;
122 lmd->md_refcount = 0;
123 lmd->md_flags = (unlink == LNET_UNLINK) ? LNET_MD_FLAG_AUTO_UNLINK : 0;
125 if ((umd->options & LNET_MD_IOVEC) != 0) {
127 if ((umd->options & LNET_MD_KIOV) != 0) /* Can't specify both */
130 lmd->md_niov = niov = umd->length;
131 memcpy(lmd->md_iov.iov, umd->start,
132 niov * sizeof (lmd->md_iov.iov[0]));
134 for (i = 0; i < (int)niov; i++) {
135 /* We take the base address on trust */
136 if (lmd->md_iov.iov[i].iov_len <= 0) /* invalid length */
139 total_length += lmd->md_iov.iov[i].iov_len;
142 lmd->md_length = total_length;
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
149 } else if ((umd->options & LNET_MD_KIOV) != 0) {
153 lmd->md_niov = niov = umd->length;
154 memcpy(lmd->md_iov.kiov, umd->start,
155 niov * sizeof (lmd->md_iov.kiov[0]));
157 for (i = 0; i < (int)niov; i++) {
158 /* We take the page pointer on trust */
159 if (lmd->md_iov.kiov[i].kiov_offset +
160 lmd->md_iov.kiov[i].kiov_len > CFS_PAGE_SIZE )
161 return -EINVAL; /* invalid length */
163 total_length += lmd->md_iov.kiov[i].kiov_len;
166 lmd->md_length = total_length;
168 if ((umd->options & LNET_MD_MAX_SIZE) != 0 && /* max size used */
169 (umd->max_size < 0 ||
170 umd->max_size > total_length)) // illegal max_size
173 } else { /* contiguous */
174 lmd->md_length = umd->length;
175 lmd->md_niov = niov = 1;
176 lmd->md_iov.iov[0].iov_base = umd->start;
177 lmd->md_iov.iov[0].iov_len = umd->length;
179 if ((umd->options & LNET_MD_MAX_SIZE) != 0 && /* max size used */
180 (umd->max_size < 0 ||
181 umd->max_size > (int)umd->length)) // illegal max_size
188 /* It's good; let handle2md succeed and add to active mds */
189 lnet_initialise_handle (&lmd->md_lh, LNET_COOKIE_TYPE_MD);
190 LASSERT (list_empty(&lmd->md_list));
191 list_add (&lmd->md_list, &the_lnet.ln_active_mds);
196 /* must be called with LNET_LOCK held */
198 lnet_md_deconstruct(lnet_libmd_t *lmd, lnet_md_t *umd)
200 /* NB this doesn't copy out all the iov entries so when a
201 * discontiguous MD is copied out, the target gets to know the
202 * original iov pointer (in start) and the number of entries it had
205 umd->start = lmd->md_start;
206 umd->length = ((lmd->md_options & (LNET_MD_IOVEC | LNET_MD_KIOV)) == 0) ?
207 lmd->md_length : lmd->md_niov;
208 umd->threshold = lmd->md_threshold;
209 umd->max_size = lmd->md_max_size;
210 umd->options = lmd->md_options;
211 umd->user_ptr = lmd->md_user_ptr;
212 lnet_eq2handle(&umd->eq_handle, lmd->md_eq);
216 lnet_md_validate(lnet_md_t *umd)
218 if (umd->start == NULL) {
219 CERROR("MD start pointer can not be NULL\n");
223 if ((umd->options & (LNET_MD_KIOV | LNET_MD_IOVEC)) != 0 &&
224 umd->length > LNET_MAX_IOV) {
225 CERROR("Invalid option: too many fragments %u, %d max\n",
226 umd->length, LNET_MAX_IOV);
234 LNetMDAttach(lnet_handle_me_t meh, lnet_md_t umd,
235 lnet_unlink_t unlink, lnet_handle_md_t *handle)
241 LASSERT (the_lnet.ln_init);
242 LASSERT (the_lnet.ln_refcount > 0);
244 if (lnet_md_validate(&umd) != 0)
247 if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) == 0) {
248 CERROR("Invalid option: no MD_OP set\n");
252 md = lnet_md_alloc(&umd);
258 me = lnet_handle2me(&meh);
261 } else if (me->me_md != NULL) {
264 rc = lib_md_build(md, &umd, unlink);
266 the_lnet.ln_portals[me->me_portal].ptl_ml_version++;
271 lnet_md2handle(handle, md);
273 /* check if this MD matches any blocked msgs */
274 lnet_match_blocked_msg(md); /* expects LNET_LOCK held */
288 LNetMDBind(lnet_md_t umd, lnet_unlink_t unlink, lnet_handle_md_t *handle)
293 LASSERT (the_lnet.ln_init);
294 LASSERT (the_lnet.ln_refcount > 0);
296 if (lnet_md_validate(&umd) != 0)
299 if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) != 0) {
300 CERROR("Invalid option: GET|PUT illegal on active MDs\n");
304 md = lnet_md_alloc(&umd);
310 rc = lib_md_build(md, &umd, unlink);
313 lnet_md2handle(handle, md);
326 LNetMDUnlink (lnet_handle_md_t mdh)
331 LASSERT (the_lnet.ln_init);
332 LASSERT (the_lnet.ln_refcount > 0);
336 md = lnet_handle2md(&mdh);
342 /* If the MD is busy, lnet_md_unlink just marks it for deletion, and
343 * when the NAL is done, the completion event flags that the MD was
344 * unlinked. Otherwise, we enqueue an event now... */
346 if (md->md_eq != NULL &&
347 md->md_refcount == 0) {
348 lnet_build_unlink_event(md, &ev);
349 lnet_enq_event_locked(md->md_eq, &ev);