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