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