Whamcloud - gitweb
LU-443 LNet: Only squawk when md->start is NULL on non-zero length
[fs/lustre-release.git] / lnet / lnet / lib-md.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lnet/lnet/lib-md.c
37  *
38  * Memory Descriptor management routines
39  */
40
41 #define DEBUG_SUBSYSTEM S_LNET
42
43 #include <lnet/lib-lnet.h>
44
45 /* must be called with LNET_LOCK held */
46 void
47 lnet_md_unlink(lnet_libmd_t *md)
48 {
49         if ((md->md_flags & LNET_MD_FLAG_ZOMBIE) == 0) {
50                 /* first unlink attempt... */
51                 lnet_me_t *me = md->md_me;
52
53                 md->md_flags |= LNET_MD_FLAG_ZOMBIE;
54
55                 /* Disassociate from ME (if any), and unlink it if it was created
56                  * with LNET_UNLINK */
57                 if (me != NULL) {
58                         md->md_me = NULL;
59                         me->me_md = NULL;
60                         if (me->me_unlink == LNET_UNLINK)
61                                 lnet_me_unlink(me);
62                 }
63
64                 /* ensure all future handle lookups fail */
65                 lnet_invalidate_handle(&md->md_lh);
66         }
67
68         if (md->md_refcount != 0) {
69                 CDEBUG(D_NET, "Queueing unlink of md %p\n", md);
70                 return;
71         }
72
73         CDEBUG(D_NET, "Unlinking md %p\n", md);
74
75         if (md->md_eq != NULL) {
76                 md->md_eq->eq_refcount--;
77                 LASSERT (md->md_eq->eq_refcount >= 0);
78         }
79
80         LASSERT (!cfs_list_empty(&md->md_list));
81         cfs_list_del_init (&md->md_list);
82         lnet_md_free(md);
83 }
84
85 /* must be called with LNET_LOCK held */
86 static int
87 lib_md_build(lnet_libmd_t *lmd, lnet_md_t *umd, int unlink)
88 {
89         lnet_eq_t   *eq = NULL;
90         int          i;
91         unsigned int niov;
92         int          total_length = 0;
93
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.
97          */
98
99         if (!LNetHandleIsInvalid (umd->eq_handle)) {
100                 eq = lnet_handle2eq(&umd->eq_handle);
101                 if (eq == NULL)
102                         return -ENOENT;
103         }
104
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);
112          */
113
114         lmd->md_me = NULL;
115         lmd->md_start = umd->start;
116         lmd->md_offset = 0;
117         lmd->md_max_size = umd->max_size;
118         lmd->md_options = umd->options;
119         lmd->md_user_ptr = umd->user_ptr;
120         lmd->md_eq = eq;
121         lmd->md_threshold = umd->threshold;
122         lmd->md_refcount = 0;
123         lmd->md_flags = (unlink == LNET_UNLINK) ? LNET_MD_FLAG_AUTO_UNLINK : 0;
124
125         if ((umd->options & LNET_MD_IOVEC) != 0) {
126
127                 if ((umd->options & LNET_MD_KIOV) != 0) /* Can't specify both */
128                         return -EINVAL;
129
130                 lmd->md_niov = niov = umd->length;
131                 memcpy(lmd->md_iov.iov, umd->start,
132                        niov * sizeof (lmd->md_iov.iov[0]));
133
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 */
137                                 return -EINVAL;
138
139                         total_length += lmd->md_iov.iov[i].iov_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
149         } else if ((umd->options & LNET_MD_KIOV) != 0) {
150 #ifndef __KERNEL__
151                 return -EINVAL;
152 #else
153                 lmd->md_niov = niov = umd->length;
154                 memcpy(lmd->md_iov.kiov, umd->start,
155                        niov * sizeof (lmd->md_iov.kiov[0]));
156
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 */
162
163                         total_length += lmd->md_iov.kiov[i].kiov_len;
164                 }
165
166                 lmd->md_length = total_length;
167
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
171                         return -EINVAL;
172 #endif
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;
178
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
182                         return -EINVAL;
183         }
184
185         if (eq != NULL)
186                 eq->eq_refcount++;
187
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 (cfs_list_empty(&lmd->md_list));
191         cfs_list_add (&lmd->md_list, &the_lnet.ln_active_mds);
192
193         return 0;
194 }
195
196 /* must be called with LNET_LOCK held */
197 void
198 lnet_md_deconstruct(lnet_libmd_t *lmd, lnet_md_t *umd)
199 {
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
203          * and that's all.
204          */
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);
213 }
214
215 int
216 lnet_md_validate(lnet_md_t *umd)
217 {
218         if (umd->start == NULL && umd->length != 0) {
219                 CERROR("MD start pointer can not be NULL with length %u\n",
220                        umd->length);
221                 return -EINVAL;
222         }
223
224         if ((umd->options & (LNET_MD_KIOV | LNET_MD_IOVEC)) != 0 &&
225             umd->length > LNET_MAX_IOV) {
226                 CERROR("Invalid option: too many fragments %u, %d max\n",
227                        umd->length, LNET_MAX_IOV);
228                 return -EINVAL;
229         }
230
231         return 0;
232 }
233
234 /**
235  * Create a memory descriptor and attach it to a ME
236  *
237  * \param meh A handle for a ME to associate the new MD with.
238  * \param umd Provides initial values for the user-visible parts of a MD.
239  * Other than its use for initialization, there is no linkage between this
240  * structure and the MD maintained by the LNet.
241  * \param unlink A flag to indicate whether the MD is automatically unlinked
242  * when it becomes inactive, either because the operation threshold drops to
243  * zero or because the available memory becomes less than \a umd.max_size.
244  * (Note that the check for unlinking a MD only occurs after the completion
245  * of a successful operation on the MD.) The value LNET_UNLINK enables auto
246  * unlinking; the value LNET_RETAIN disables it.
247  * \param handle On successful returns, a handle to the newly created MD is
248  * saved here. This handle can be used later in LNetMDUnlink().
249  *
250  * \retval 0       On success.
251  * \retval -EINVAL If \a umd is not valid.
252  * \retval -ENOMEM If new MD cannot be allocated.
253  * \retval -ENOENT Either \a meh or \a umd.eq_handle does not point to a
254  * valid object. Note that it's OK to supply a NULL \a umd.eq_handle by
255  * calling LNetInvalidateHandle() on it.
256  * \retval -EBUSY  If the ME pointed to by \a meh is already associated with
257  * a MD.
258  */
259 int
260 LNetMDAttach(lnet_handle_me_t meh, lnet_md_t umd,
261              lnet_unlink_t unlink, lnet_handle_md_t *handle)
262 {
263         lnet_me_t     *me;
264         lnet_libmd_t  *md;
265         int            rc;
266
267         LASSERT (the_lnet.ln_init);
268         LASSERT (the_lnet.ln_refcount > 0);
269
270         if (lnet_md_validate(&umd) != 0)
271                 return -EINVAL;
272
273         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) == 0) {
274                 CERROR("Invalid option: no MD_OP set\n");
275                 return -EINVAL;
276         }
277
278         md = lnet_md_alloc(&umd);
279         if (md == NULL)
280                 return -ENOMEM;
281
282         LNET_LOCK();
283
284         me = lnet_handle2me(&meh);
285         if (me == NULL) {
286                 rc = -ENOENT;
287         } else if (me->me_md != NULL) {
288                 rc = -EBUSY;
289         } else {
290                 rc = lib_md_build(md, &umd, unlink);
291                 if (rc == 0) {
292                         the_lnet.ln_portals[me->me_portal].ptl_ml_version++;
293
294                         me->me_md = md;
295                         md->md_me = me;
296
297                         lnet_md2handle(handle, md);
298
299                         /* check if this MD matches any blocked msgs */
300                         lnet_match_blocked_msg(md);   /* expects LNET_LOCK held */
301
302                         LNET_UNLOCK();
303                         return (0);
304                 }
305         }
306
307         lnet_md_free (md);
308
309         LNET_UNLOCK();
310         return (rc);
311 }
312
313 /**
314  * Create a "free floating" memory descriptor - a MD that is not associated
315  * with a ME. Such MDs are usually used in LNetPut() and LNetGet() operations.
316  *
317  * \param umd,unlink See the discussion for LNetMDAttach().
318  * \param handle On successful returns, a handle to the newly created MD is
319  * saved here. This handle can be used later in LNetMDUnlink(), LNetPut(),
320  * and LNetGet() operations.
321  *
322  * \retval 0       On success.
323  * \retval -EINVAL If \a umd is not valid.
324  * \retval -ENOMEM If new MD cannot be allocated.
325  * \retval -ENOENT \a umd.eq_handle does not point to a valid EQ. Note that
326  * it's OK to supply a NULL \a umd.eq_handle by calling
327  * LNetInvalidateHandle() on it.
328  */
329 int
330 LNetMDBind(lnet_md_t umd, lnet_unlink_t unlink, lnet_handle_md_t *handle)
331 {
332         lnet_libmd_t  *md;
333         int            rc;
334
335         LASSERT (the_lnet.ln_init);
336         LASSERT (the_lnet.ln_refcount > 0);
337
338         if (lnet_md_validate(&umd) != 0)
339                 return -EINVAL;
340
341         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) != 0) {
342                 CERROR("Invalid option: GET|PUT illegal on active MDs\n");
343                 return -EINVAL;
344         }
345
346         md = lnet_md_alloc(&umd);
347         if (md == NULL)
348                 return -ENOMEM;
349
350         LNET_LOCK();
351
352         rc = lib_md_build(md, &umd, unlink);
353
354         if (rc == 0) {
355                 lnet_md2handle(handle, md);
356
357                 LNET_UNLOCK();
358                 return (0);
359         }
360
361         lnet_md_free (md);
362
363         LNET_UNLOCK();
364         return (rc);
365 }
366
367 /**
368  * Unlink the memory descriptor from any ME it may be linked to and release
369  * the internal resources associated with it.
370  *
371  * This function does not free the memory region associated with the MD;
372  * i.e., the memory the user allocated for this MD. If the ME associated with
373  * this MD is not NULL and was created with auto unlink enabled, the ME is
374  * unlinked as well (see LNetMEAttach()).
375  *
376  * Explicitly unlinking a MD via this function call has the same behavior as
377  * a MD that has been automatically unlinked, except that no LNET_EVENT_UNLINK
378  * is generated in the latter case.
379  *
380  * An unlinked event can be reported in two ways:
381  * - If there's no pending operations on the MD, it's unlinked immediately
382  *   and an LNET_EVENT_UNLINK event is logged before this function returns.
383  * - Otherwise, the MD is only marked for deletion when this function
384  *   returns, and the unlinked event will be piggybacked on the event of
385  *   the completion of the last operation by setting the unlinked field of
386  *   the event. No dedicated LNET_EVENT_UNLINK event is generated.
387  *
388  * Note that in both cases the unlinked field of the event is always set; no
389  * more event will happen on the MD after such an event is logged.
390  *
391  * \param mdh A handle for the MD to be unlinked.
392  *
393  * \retval 0       On success.
394  * \retval -ENOENT If \a mdh does not point to a valid MD object.
395  */
396 int
397 LNetMDUnlink (lnet_handle_md_t mdh)
398 {
399         lnet_event_t     ev;
400         lnet_libmd_t    *md;
401
402         LASSERT (the_lnet.ln_init);
403         LASSERT (the_lnet.ln_refcount > 0);
404
405         LNET_LOCK();
406
407         md = lnet_handle2md(&mdh);
408         if (md == NULL) {
409                 LNET_UNLOCK();
410                 return -ENOENT;
411         }
412
413         /* If the MD is busy, lnet_md_unlink just marks it for deletion, and
414          * when the NAL is done, the completion event flags that the MD was
415          * unlinked.  Otherwise, we enqueue an event now... */
416
417         if (md->md_eq != NULL &&
418             md->md_refcount == 0) {
419                 lnet_build_unlink_event(md, &ev);
420                 lnet_enq_event_locked(md->md_eq, &ev);
421         }
422
423         lnet_md_unlink(md);
424
425         LNET_UNLOCK();
426         return 0;
427 }