Whamcloud - gitweb
b=20973 Doxygen comments for LNet API
[fs/lustre-release.git] / lnet / lnet / lib-me.c
index e3c46ea..d62c970 100644 (file)
 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
  * vim:expandtab:shiftwidth=8:tabstop=8:
  *
- * lib/lib-me.c
- * Match Entry management routines
+ * GPL HEADER START
+ *
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
- *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
- *  Copyright (c) 2001-2002 Sandia National Laboratories
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 only,
+ * as published by the Free Software Foundation.
  *
- *   This file is part of Lustre, http://www.sf.net/projects/lustre/
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License version 2 for more details (a copy is included
+ * in the LICENSE file that accompanied this code).
  *
- *   Lustre is free software; you can redistribute it and/or
- *   modify it under the terms of version 2 of the GNU General Public
- *   License as published by the Free Software Foundation.
+ * You should have received a copy of the GNU General Public License
+ * version 2 along with this program; If not, see
+ * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  *
- *   Lustre is distributed in the hope that it will be useful,
- *   but WITHOUT ANY WARRANTY; without even the implied warranty of
- *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *   GNU General Public License for more details.
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
  *
- *   You should have received a copy of the GNU General Public License
- *   along with Lustre; if not, write to the Free Software
- *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ * Lustre is a trademark of Sun Microsystems, Inc.
+ *
+ * lnet/lnet/lib-me.c
+ *
+ * Match Entry management routines
  */
 
-#ifndef __KERNEL__
-# include <stdio.h>
-#else
-# define DEBUG_SUBSYSTEM S_PORTALS
-# include <linux/kp30.h>
-#endif
+#define DEBUG_SUBSYSTEM S_LNET
 
-#include <portals/lib-p30.h>
-#include <portals/arg-blocks.h>
+#include <lnet/lib-lnet.h>
 
-static void lib_me_dump(nal_cb_t * nal, lib_me_t * me);
+static int
+lnet_me_match_portal(lnet_portal_t *ptl, lnet_process_id_t id,
+                     __u64 match_bits, __u64 ignore_bits)
+{
+        cfs_list_t       *mhash = NULL;
+        int               unique;
+
+        LASSERT (!(lnet_portal_is_unique(ptl) &&
+                   lnet_portal_is_wildcard(ptl)));
+
+        /* prefer to check w/o any lock */
+        unique = lnet_match_is_unique(id, match_bits, ignore_bits);
+        if (likely(lnet_portal_is_unique(ptl) ||
+                   lnet_portal_is_wildcard(ptl)))
+                goto match;
+
+        /* unset, new portal */
+        if (unique) {
+                mhash = lnet_portal_mhash_alloc();
+                if (mhash == NULL)
+                        return -ENOMEM;
+        }
 
-int do_PtlMEAttach(nal_cb_t * nal, void *private, void *v_args, void *v_ret)
+        LNET_LOCK();
+        if (lnet_portal_is_unique(ptl) ||
+            lnet_portal_is_wildcard(ptl)) {
+                /* someone set it before me */
+                if (mhash != NULL)
+                        lnet_portal_mhash_free(mhash);
+                LNET_UNLOCK();
+                goto match;
+        }
+
+        /* still not set */
+        LASSERT (ptl->ptl_mhash == NULL);
+        if (unique) {
+                ptl->ptl_mhash = mhash;
+                lnet_portal_setopt(ptl, LNET_PTL_MATCH_UNIQUE);
+        } else {
+                lnet_portal_setopt(ptl, LNET_PTL_MATCH_WILDCARD);
+        }
+        LNET_UNLOCK();
+        return 0;
+
+ match:
+        if (lnet_portal_is_unique(ptl) && !unique)
+                return -EPERM;
+
+        if (lnet_portal_is_wildcard(ptl) && unique)
+                return -EPERM;
+
+        return 0;
+}
+
+/**
+ * Create and attach a match entry to the match list of \a portal. The new
+ * ME is empty, i.e. not associated with a memory descriptor. LNetMDAttach()
+ * can be used to attach a MD to an empty ME.
+ *
+ * \param portal The portal table index where the ME should be attached.
+ * \param match_id Specifies the match criteria for the process ID of
+ * the requester. The constants LNET_PID_ANY and LNET_NID_ANY can be
+ * used to wildcard either of the identifiers in the lnet_process_id_t
+ * structure.
+ * \param match_bits,ignore_bits Specify the match criteria to apply
+ * to the match bits in the incoming request. The ignore bits are used
+ * to mask out insignificant bits in the incoming match bits. The resulting
+ * bits are then compared to the ME's match bits to determine if the
+ * incoming request meets the match criteria.
+ * \param unlink Indicates whether the ME should be unlinked when the memory
+ * descriptor associated with it is unlinked (Note that the check for
+ * unlinking a ME only occurs when the memory descriptor is unlinked.).
+ * Valid values are LNET_RETAIN and LNET_UNLINK.
+ * \param pos Indicates whether the new ME should be prepended or
+ * appended to the match list. Allowed constants: LNET_INS_BEFORE,
+ * LNET_INS_AFTER.
+ * \param handle On successful returns, a handle to the newly created ME
+ * object is saved here. This handle can be used later in LNetMEInsert(),
+ * LNetMEUnlink(), or LNetMDAttach() functions.
+ *
+ * \retval 0       On success.
+ * \retval -EINVAL If \a portal is invalid.
+ * \retval -ENOMEM If new ME object cannot be allocated.
+ */
+int
+LNetMEAttach(unsigned int portal,
+             lnet_process_id_t match_id,
+             __u64 match_bits, __u64 ignore_bits,
+             lnet_unlink_t unlink, lnet_ins_pos_t pos,
+             lnet_handle_me_t *handle)
 {
-        PtlMEAttach_in *args = v_args;
-        PtlMEAttach_out *ret = v_ret;
-        lib_ni_t *ni = &nal->ni;
-        lib_ptl_t *tbl = &ni->tbl;
-        unsigned long flags;
-        lib_me_t *me;
+        lnet_me_t        *me;
+        lnet_portal_t    *ptl;
+        cfs_list_t       *head;
+        int               rc;
+
+        LASSERT (the_lnet.ln_init);
+        LASSERT (the_lnet.ln_refcount > 0);
 
-        if (args->index_in >= tbl->size)
-                return ret->rc = PTL_PT_INDEX_INVALID;
+        if ((int)portal >= the_lnet.ln_nportals)
+                return -EINVAL;
 
-        /* Should check for valid matchid, but not yet */
-        if (0)
-                return ret->rc = PTL_PROCESS_INVALID;
+        ptl = &the_lnet.ln_portals[portal];
+        rc = lnet_me_match_portal(ptl, match_id, match_bits, ignore_bits);
+        if (rc != 0)
+                return rc;
 
-        me = lib_me_alloc (nal);
+        me = lnet_me_alloc();
         if (me == NULL)
-                return (ret->rc = PTL_NO_SPACE);
+                return -ENOMEM;
 
-        state_lock(nal, &flags);
+        LNET_LOCK();
 
-        me->match_id = args->match_id_in;
-        me->match_bits = args->match_bits_in;
-        me->ignore_bits = args->ignore_bits_in;
-        me->unlink = args->unlink_in;
-        me->md = NULL;
+        me->me_portal = portal;
+        me->me_match_id = match_id;
+        me->me_match_bits = match_bits;
+        me->me_ignore_bits = ignore_bits;
+        me->me_unlink = unlink;
+        me->me_md = NULL;
 
-        lib_initialise_handle (nal, &me->me_lh, PTL_COOKIE_TYPE_ME);
+        lnet_initialise_handle (&me->me_lh, LNET_COOKIE_TYPE_ME);
+        head = lnet_portal_me_head(portal, match_id, match_bits);
+        LASSERT (head != NULL);
 
-        if (args->position_in == PTL_INS_AFTER)
-                list_add_tail(&me->me_list, &(tbl->tbl[args->index_in]));
+        if (pos == LNET_INS_AFTER)
+                cfs_list_add_tail(&me->me_list, head);
         else
-                list_add(&me->me_list, &(tbl->tbl[args->index_in]));
+                cfs_list_add(&me->me_list, head);
 
-        ptl_me2handle(&ret->handle_out, me);
+        lnet_me2handle(handle, me);
 
-        state_unlock(nal, &flags);
+        LNET_UNLOCK();
 
-        return ret->rc = PTL_OK;
+        return 0;
 }
 
-int do_PtlMEInsert(nal_cb_t * nal, void *private, void *v_args, void *v_ret)
+/**
+ * Create and a match entry and insert it before or after the ME pointed to by
+ * \a current_meh. The new ME is empty, i.e. not associated with a memory
+ * descriptor. LNetMDAttach() can be used to attach a MD to an empty ME.
+ *
+ * This function is identical to LNetMEAttach() except for the position
+ * where the new ME is inserted.
+ *
+ * \param current_meh A handle for a ME. The new ME will be inserted
+ * immediately before or immediately after this ME.
+ * \param match_id,match_bits,ignore_bits,unlink,pos,handle See the discussion
+ * for LNetMEAttach().
+ *
+ * \retval 0       On success.
+ * \retval -ENOMEM If new ME object cannot be allocated.
+ * \retval -ENOENT If \a current_meh does not point to a valid match entry.
+ */
+int
+LNetMEInsert(lnet_handle_me_t current_meh,
+             lnet_process_id_t match_id,
+             __u64 match_bits, __u64 ignore_bits,
+             lnet_unlink_t unlink, lnet_ins_pos_t pos,
+             lnet_handle_me_t *handle)
 {
-        PtlMEInsert_in *args = v_args;
-        PtlMEInsert_out *ret = v_ret;
-        unsigned long flags;
-        lib_me_t *me;
-        lib_me_t *new;
+        lnet_me_t     *current_me;
+        lnet_me_t     *new_me;
+        lnet_portal_t *ptl;
 
-        new = lib_me_alloc (nal);
-        if (new == NULL)
-                return (ret->rc = PTL_NO_SPACE);
+        LASSERT (the_lnet.ln_init);
+        LASSERT (the_lnet.ln_refcount > 0);
 
-        /* Should check for valid matchid, but not yet */
+        new_me = lnet_me_alloc();
+        if (new_me == NULL)
+                return -ENOMEM;
 
-        state_lock(nal, &flags);
+        LNET_LOCK();
 
-        me = ptl_handle2me(&args->current_in, nal);
-        if (me == NULL) {
-                lib_me_free (nal, new);
+        current_me = lnet_handle2me(&current_meh);
+        if (current_me == NULL) {
+                lnet_me_free (new_me);
 
-                state_unlock (nal, &flags);
-                return (ret->rc = PTL_ME_INVALID);
+                LNET_UNLOCK();
+                return -ENOENT;
         }
 
-        new->match_id = args->match_id_in;
-        new->match_bits = args->match_bits_in;
-        new->ignore_bits = args->ignore_bits_in;
-        new->unlink = args->unlink_in;
-        new->md = NULL;
-
-        lib_initialise_handle (nal, &new->me_lh, PTL_COOKIE_TYPE_ME);
-
-        if (args->position_in == PTL_INS_AFTER)
-                list_add_tail(&new->me_list, &me->me_list);
-        else
-                list_add(&new->me_list, &me->me_list);
+        LASSERT (current_me->me_portal < the_lnet.ln_nportals);
 
-        ptl_me2handle(&ret->handle_out, new);
-
-        state_unlock(nal, &flags);
+        ptl = &the_lnet.ln_portals[current_me->me_portal];
+        if (lnet_portal_is_unique(ptl)) {
+                /* nosense to insertion on unique portal */
+                lnet_me_free (new_me);
+                LNET_UNLOCK();
+                return -EPERM;
+        }
 
-        return ret->rc = PTL_OK;
-}
+        new_me->me_portal = current_me->me_portal;
+        new_me->me_match_id = match_id;
+        new_me->me_match_bits = match_bits;
+        new_me->me_ignore_bits = ignore_bits;
+        new_me->me_unlink = unlink;
+        new_me->me_md = NULL;
 
-int do_PtlMEUnlink(nal_cb_t * nal, void *private, void *v_args, void *v_ret)
-{
-        PtlMEUnlink_in *args = v_args;
-        PtlMEUnlink_out *ret = v_ret;
-        unsigned long flags;
-        lib_me_t *me;
+        lnet_initialise_handle (&new_me->me_lh, LNET_COOKIE_TYPE_ME);
 
-        state_lock(nal, &flags);
+        if (pos == LNET_INS_AFTER)
+                cfs_list_add(&new_me->me_list, &current_me->me_list);
+        else
+                cfs_list_add_tail(&new_me->me_list, &current_me->me_list);
 
-        me = ptl_handle2me(&args->current_in, nal);
-        if (me == NULL) {
-                ret->rc = PTL_ME_INVALID;
-        } else {
-                lib_me_unlink(nal, me);
-                ret->rc = PTL_OK;
-        }
+        lnet_me2handle(handle, new_me);
 
-        state_unlock(nal, &flags);
+        LNET_UNLOCK();
 
-        return (ret->rc);
+        return 0;
 }
 
-/* call with state_lock please */
-void lib_me_unlink(nal_cb_t *nal, lib_me_t *me)
+/**
+ * Unlink a match entry from its match list.
+ *
+ * This operation also releases any resources associated with the ME. If a
+ * memory descriptor is attached to the ME, then it will be unlinked as well
+ * and an unlink event will be generated. It is an error to use the ME handle
+ * after calling LNetMEUnlink().
+ *
+ * \param meh A handle for the ME to be unlinked.
+ *
+ * \retval 0       On success.
+ * \retval -ENOENT If \a meh does not point to a valid ME.
+ * \see LNetMDUnlink() for the discussion on delivering unlink event.
+ */
+int
+LNetMEUnlink(lnet_handle_me_t meh)
 {
-        lib_ni_t *ni = &nal->ni;
+        lnet_me_t    *me;
+        lnet_libmd_t *md;
+        lnet_event_t  ev;
 
-        if (ni->debug & PTL_DEBUG_UNLINK) {
-                ptl_handle_any_t handle;
-                ptl_me2handle(&handle, me);
-        }
+        LASSERT (the_lnet.ln_init);
+        LASSERT (the_lnet.ln_refcount > 0);
 
-        list_del (&me->me_list);
+        LNET_LOCK();
 
-        if (me->md) {
-                me->md->me = NULL;
-                lib_md_unlink(nal, me->md);
+        me = lnet_handle2me(&meh);
+        if (me == NULL) {
+                LNET_UNLOCK();
+                return -ENOENT;
         }
 
-        lib_invalidate_handle (nal, &me->me_lh);
-        lib_me_free(nal, me);
-}
-
-int do_PtlTblDump(nal_cb_t * nal, void *private, void *v_args, void *v_ret)
-{
-        PtlTblDump_in *args = v_args;
-        PtlTblDump_out *ret = v_ret;
-        lib_ptl_t *tbl = &nal->ni.tbl;
-        ptl_handle_any_t handle;
-        struct list_head *tmp;
-        unsigned long flags;
-
-        if (args->index_in < 0 || args->index_in >= tbl->size)
-                return ret->rc = PTL_PT_INDEX_INVALID;
-
-        nal->cb_printf(nal, "Portal table index %d\n", args->index_in);
-
-        state_lock(nal, &flags);
-        list_for_each(tmp, &(tbl->tbl[args->index_in])) {
-                lib_me_t *me = list_entry(tmp, lib_me_t, me_list);
-                ptl_me2handle(&handle, me);
-                lib_me_dump(nal, me);
+        md = me->me_md;
+        if (md != NULL &&
+            md->md_eq != NULL &&
+            md->md_refcount == 0) {
+                lnet_build_unlink_event(md, &ev);
+                lnet_enq_event_locked(md->md_eq, &ev);
         }
-        state_unlock(nal, &flags);
 
-        return ret->rc = PTL_OK;
+        lnet_me_unlink(me);
+
+        LNET_UNLOCK();
+        return 0;
 }
 
-int do_PtlMEDump(nal_cb_t * nal, void *private, void *v_args, void *v_ret)
+/* call with LNET_LOCK please */
+void
+lnet_me_unlink(lnet_me_t *me)
 {
-        PtlMEDump_in *args = v_args;
-        PtlMEDump_out *ret = v_ret;
-        lib_me_t *me;
-        unsigned long flags;
-
-        state_lock(nal, &flags);
+        cfs_list_del (&me->me_list);
 
-        me = ptl_handle2me(&args->current_in, nal);
-        if (me == NULL) {
-                ret->rc = PTL_ME_INVALID;
-        } else {
-                lib_me_dump(nal, me);
-                ret->rc = PTL_OK;
+        if (me->me_md != NULL) {
+                me->me_md->md_me = NULL;
+                lnet_md_unlink(me->me_md);
         }
 
-        state_unlock(nal, &flags);
-
-        return ret->rc;
+        lnet_invalidate_handle (&me->me_lh);
+        lnet_me_free(me);
 }
 
-static void lib_me_dump(nal_cb_t * nal, lib_me_t * me)
+#if 0
+static void
+lib_me_dump(lnet_me_t *me)
 {
-        nal->cb_printf(nal, "Match Entry %p ("LPX64")\n", me, 
-                       me->me_lh.lh_cookie);
+        CWARN("Match Entry %p ("LPX64")\n", me,
+              me->me_lh.lh_cookie);
 
-        nal->cb_printf(nal, "\tMatch/Ignore\t= %016lx / %016lx\n",
-                       me->match_bits, me->ignore_bits);
+        CWARN("\tMatch/Ignore\t= %016lx / %016lx\n",
+              me->me_match_bits, me->me_ignore_bits);
 
-        nal->cb_printf(nal, "\tMD\t= %p\n", me->md);
-        nal->cb_printf(nal, "\tprev\t= %p\n",
-                       list_entry(me->me_list.prev, lib_me_t, me_list));
-        nal->cb_printf(nal, "\tnext\t= %p\n",
-                       list_entry(me->me_list.next, lib_me_t, me_list));
+        CWARN("\tMD\t= %p\n", me->md);
+        CWARN("\tprev\t= %p\n",
+              cfs_list_entry(me->me_list.prev, lnet_me_t, me_list));
+        CWARN("\tnext\t= %p\n",
+              cfs_list_entry(me->me_list.next, lnet_me_t, me_list));
 }
+#endif