Whamcloud - gitweb
i=liangzhen,i=maxim,b=16022:
[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 [sun.com URL with a
20  * copy of GPLv2].
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  2008 Sun Microsystems, Inc. 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         list_del (&md->md_list);
81         lnet_md_free(md);
82 }
83
84 /* must be called with LNET_LOCK held */
85 static int
86 lib_md_build(lnet_libmd_t *lmd, lnet_md_t *umd, int unlink)
87 {
88         lnet_eq_t   *eq = NULL;
89         int          i;
90         unsigned int niov;
91         int          total_length = 0;
92
93         /* NB we are passed an allocated, but uninitialised/active md.
94          * if we return success, caller may lnet_md_unlink() it.
95          * otherwise caller may only lnet_md_free() it.
96          */
97
98         if (!LNetHandleIsEqual (umd->eq_handle, LNET_EQ_NONE)) {
99                 eq = lnet_handle2eq(&umd->eq_handle);
100                 if (eq == NULL)
101                         return -ENOENT;
102         }
103
104         /* This implementation doesn't know how to create START events or
105          * disable END events.  Best to LASSERT our caller is compliant so
106          * we find out quickly...  */
107         /*  TODO - reevaluate what should be here in light of 
108          * the removal of the start and end events
109          * maybe there we shouldn't even allow LNET_EQ_NONE!)
110         LASSERT (eq == NULL);
111          */
112
113         lmd->md_me = NULL;
114         lmd->md_start = umd->start;
115         lmd->md_offset = 0;
116         lmd->md_max_size = umd->max_size;
117         lmd->md_options = umd->options;
118         lmd->md_user_ptr = umd->user_ptr;
119         lmd->md_eq = eq;
120         lmd->md_threshold = umd->threshold;
121         lmd->md_refcount = 0;
122         lmd->md_flags = (unlink == LNET_UNLINK) ? LNET_MD_FLAG_AUTO_UNLINK : 0;
123
124         if ((umd->options & LNET_MD_IOVEC) != 0) {
125
126                 if ((umd->options & LNET_MD_KIOV) != 0) /* Can't specify both */
127                         return -EINVAL;
128
129                 lmd->md_niov = niov = umd->length;
130                 memcpy(lmd->md_iov.iov, umd->start,
131                        niov * sizeof (lmd->md_iov.iov[0]));
132
133                 for (i = 0; i < niov; i++) {
134                         /* We take the base address on trust */
135                         if (lmd->md_iov.iov[i].iov_len <= 0) /* invalid length */
136                                 return -EINVAL;
137
138                         total_length += lmd->md_iov.iov[i].iov_len;
139                 }
140
141                 lmd->md_length = total_length;
142
143                 if ((umd->options & LNET_MD_MAX_SIZE) != 0 && /* max size used */
144                     (umd->max_size < 0 ||
145                      umd->max_size > total_length)) // illegal max_size
146                         return -EINVAL;
147
148         } else if ((umd->options & LNET_MD_KIOV) != 0) {
149 #ifndef __KERNEL__
150                 return -EINVAL;
151 #else
152                 lmd->md_niov = niov = umd->length;
153                 memcpy(lmd->md_iov.kiov, umd->start,
154                        niov * sizeof (lmd->md_iov.kiov[0]));
155
156                 for (i = 0; i < niov; i++) {
157                         /* We take the page pointer on trust */
158                         if (lmd->md_iov.kiov[i].kiov_offset +
159                             lmd->md_iov.kiov[i].kiov_len > CFS_PAGE_SIZE )
160                                 return -EINVAL; /* invalid length */
161
162                         total_length += lmd->md_iov.kiov[i].kiov_len;
163                 }
164
165                 lmd->md_length = total_length;
166
167                 if ((umd->options & LNET_MD_MAX_SIZE) != 0 && /* max size used */
168                     (umd->max_size < 0 ||
169                      umd->max_size > total_length)) // illegal max_size
170                         return -EINVAL;
171 #endif
172         } else {   /* contiguous */
173                 lmd->md_length = umd->length;
174                 lmd->md_niov = niov = 1;
175                 lmd->md_iov.iov[0].iov_base = umd->start;
176                 lmd->md_iov.iov[0].iov_len = umd->length;
177
178                 if ((umd->options & LNET_MD_MAX_SIZE) != 0 && /* max size used */
179                     (umd->max_size < 0 ||
180                      umd->max_size > umd->length)) // illegal max_size
181                         return -EINVAL;
182         }
183
184         if (eq != NULL)
185                 eq->eq_refcount++;
186
187         /* It's good; let handle2md succeed and add to active mds */
188         lnet_initialise_handle (&lmd->md_lh, LNET_COOKIE_TYPE_MD);
189         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 LNetMDAttach(lnet_handle_me_t meh, lnet_md_t umd,
215              lnet_unlink_t unlink, lnet_handle_md_t *handle)
216 {
217         lnet_me_t     *me;
218         lnet_libmd_t  *md;
219         int            rc;
220
221         LASSERT (the_lnet.ln_init);
222         LASSERT (the_lnet.ln_refcount > 0);
223
224         if ((umd.options & (LNET_MD_KIOV | LNET_MD_IOVEC)) != 0 &&
225             umd.length > LNET_MAX_IOV) /* too many fragments */
226                 return -EINVAL;
227
228         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) == 0)
229                 return -EINVAL;
230
231         md = lnet_md_alloc(&umd);
232         if (md == NULL)
233                 return -ENOMEM;
234
235         LNET_LOCK();
236
237         me = lnet_handle2me(&meh);
238         if (me == NULL) {
239                 rc = -ENOENT;
240         } else if (me->me_md != NULL) {
241                 rc = -EBUSY;
242         } else {
243                 rc = lib_md_build(md, &umd, unlink);
244                 if (rc == 0) {
245                         me->me_md = md;
246                         md->md_me = me;
247
248                         lnet_md2handle(handle, md);
249
250                         /* check if this MD matches any blocked msgs */
251                         lnet_match_blocked_msg(md);   /* expects LNET_LOCK held */
252
253                         LNET_UNLOCK();
254                         return (0);
255                 }
256         }
257
258         lnet_md_free (md);
259
260         LNET_UNLOCK();
261         return (rc);
262 }
263
264 int
265 LNetMDBind(lnet_md_t umd, lnet_unlink_t unlink, lnet_handle_md_t *handle)
266 {
267         lnet_libmd_t  *md;
268         int            rc;
269
270         LASSERT (the_lnet.ln_init);
271         LASSERT (the_lnet.ln_refcount > 0);
272
273         if ((umd.options & (LNET_MD_KIOV | LNET_MD_IOVEC)) != 0 &&
274             umd.length > LNET_MAX_IOV) /* too many fragments */
275                 return -EINVAL;
276
277         if ((umd.options & (LNET_MD_OP_GET | LNET_MD_OP_PUT)) != 0)
278                 return -EINVAL;
279
280         md = lnet_md_alloc(&umd);
281         if (md == NULL)
282                 return -ENOMEM;
283
284         LNET_LOCK();
285
286         rc = lib_md_build(md, &umd, unlink);
287
288         if (rc == 0) {
289                 lnet_md2handle(handle, md);
290
291                 LNET_UNLOCK();
292                 return (0);
293         }
294
295         lnet_md_free (md);
296
297         LNET_UNLOCK();
298         return (rc);
299 }
300
301 int
302 LNetMDUnlink (lnet_handle_md_t mdh)
303 {
304         lnet_event_t     ev;
305         lnet_libmd_t    *md;
306
307         LASSERT (the_lnet.ln_init);
308         LASSERT (the_lnet.ln_refcount > 0);
309
310         LNET_LOCK();
311
312         md = lnet_handle2md(&mdh);
313         if (md == NULL) {
314                 LNET_UNLOCK();
315                 return -ENOENT;
316         }
317
318         /* If the MD is busy, lnet_md_unlink just marks it for deletion, and
319          * when the NAL is done, the completion event flags that the MD was
320          * unlinked.  Otherwise, we enqueue an event now... */
321
322         if (md->md_eq != NULL &&
323             md->md_refcount == 0) {
324                 lnet_build_unlink_event(md, &ev);
325                 lnet_enq_event_locked(md->md_eq, &ev);
326         }
327
328         lnet_md_unlink(md);
329
330         LNET_UNLOCK();
331         return 0;
332 }