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