Whamcloud - gitweb
LU-10391 lnet: switch to large lnet_processid for matching
[fs/lustre-release.git] / lnet / lnet / lib-me.c
index bd1af5b..8d7c9ee 100644 (file)
-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
+/*
+ * GPL HEADER START
  *
- * lib/lib-me.c
- * Match Entry management routines
+ * 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.gnu.org/licenses/gpl-2.0.html
  *
- *   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.
+ * GPL HEADER END
+ */
+/*
+ * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
+ * Use is subject to license terms.
  *
- *   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.
+ * Copyright (c) 2012, 2013, Intel Corporation.
+ */
+/*
+ * This file is part of Lustre, http://www.lustre.org/
+ *
+ * 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
-
-#include <portals/lib-p30.h>
-#include <portals/arg-blocks.h>
-
-static void lib_me_dump(nal_cb_t * nal, lib_me_t * me);
-
-int do_PtlMEAttach(nal_cb_t * nal, void *private, void *v_args, void *v_ret)
-{
-        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;
-
-        if (args->index_in < 0 || args->index_in >= tbl->size)
-                return ret->rc = PTL_INV_PTINDEX;
-
-        /* Should check for valid matchid, but not yet */
-        if (0)
-                return ret->rc = PTL_INV_PROC;
-
-        me = lib_me_alloc (nal);
-        if (me == NULL)
-                return (ret->rc = PTL_NOSPACE);
-
-        state_lock(nal, &flags);
-
-        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;
-
-        lib_initialise_handle (nal, &me->me_lh, PTL_COOKIE_TYPE_ME);
-
-        if (args->position_in == PTL_INS_AFTER)
-                list_add_tail(&me->me_list, &(tbl->tbl[args->index_in]));
-        else
-                list_add(&me->me_list, &(tbl->tbl[args->index_in]));
-
-        ptl_me2handle(&ret->handle_out, me);
-
-        state_unlock(nal, &flags);
-
-        return ret->rc = PTL_OK;
-}
-
-int do_PtlMEInsert(nal_cb_t * nal, void *private, void *v_args, void *v_ret)
-{
-        PtlMEInsert_in *args = v_args;
-        PtlMEInsert_out *ret = v_ret;
-        unsigned long flags;
-        lib_me_t *me;
-        lib_me_t *new;
-
-        new = lib_me_alloc (nal);
-        if (new == NULL)
-                return (ret->rc = PTL_NOSPACE);
-
-        /* Should check for valid matchid, but not yet */
-
-        state_lock(nal, &flags);
-
-        me = ptl_handle2me(&args->current_in, nal);
-        if (me == NULL) {
-                lib_me_free (nal, new);
-
-                state_unlock (nal, &flags);
-                return (ret->rc = PTL_INV_ME);
-        }
-
-        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);
-
-        ptl_me2handle(&ret->handle_out, new);
-
-        state_unlock(nal, &flags);
-
-        return ret->rc = PTL_OK;
-}
-
-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;
-
-        state_lock(nal, &flags);
-
-        me = ptl_handle2me(&args->current_in, nal);
-        if (me == NULL) {
-                ret->rc = PTL_INV_ME;
-        } else {
-                lib_me_unlink(nal, me);
-                ret->rc = PTL_OK;
-        }
-
-        state_unlock(nal, &flags);
-
-        return (ret->rc);
-}
-
-/* call with state_lock please */
-void lib_me_unlink(nal_cb_t *nal, lib_me_t *me)
-{
-        lib_ni_t *ni = &nal->ni;
-
-        if (ni->debug & PTL_DEBUG_UNLINK) {
-                ptl_handle_any_t handle;
-                ptl_me2handle(&handle, me);
-        }
-
-        list_del (&me->me_list);
+#define DEBUG_SUBSYSTEM S_LNET
 
-        if (me->md) {
-                me->md->me = NULL;
-                lib_md_unlink(nal, me->md);
-        }
+#include <lnet/lib-lnet.h>
 
-        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)
+/**
+ * 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 struct lnet_process_id
+ * 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.
+ *
+ * \retval A handle to the newly created ME is returned on success
+ * \retval ERR_PTR(-EINVAL) If \a portal is invalid.
+ * \retval ERR_PTR(-ENOMEM) If new ME object cannot be allocated.
+ */
+struct lnet_me *
+LNetMEAttach(unsigned int portal,
+            struct lnet_processid *match_id,
+            __u64 match_bits, __u64 ignore_bits,
+            enum lnet_unlink unlink, enum lnet_ins_pos pos)
 {
-        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_INV_PTINDEX;
-
-        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);
-        }
-        state_unlock(nal, &flags);
-
-        return ret->rc = PTL_OK;
+       struct lnet_match_table *mtable;
+       struct lnet_me          *me;
+       struct list_head        *head;
+
+       LASSERT(the_lnet.ln_refcount > 0);
+
+       if ((int)portal >= the_lnet.ln_nportals)
+               return ERR_PTR(-EINVAL);
+
+       mtable = lnet_mt_of_attach(portal, match_id,
+                                  match_bits, ignore_bits, pos);
+       if (mtable == NULL) /* can't match portal type */
+               return ERR_PTR(-EPERM);
+
+       me = kmem_cache_zalloc(lnet_mes_cachep, GFP_NOFS);
+       if (me == NULL) {
+               CDEBUG(D_MALLOC, "failed to allocate 'me'\n");
+               return ERR_PTR(-ENOMEM);
+       }
+       CDEBUG(D_MALLOC, "slab-alloced 'me' at %p.\n", me);
+
+       lnet_res_lock(mtable->mt_cpt);
+
+       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;
+
+       me->me_cpt = mtable->mt_cpt;
+
+       if (ignore_bits != 0)
+               head = &mtable->mt_mhash[LNET_MT_HASH_IGNORE];
+       else
+               head = lnet_mt_match_head(mtable, match_id, match_bits);
+
+       me->me_pos = head - &mtable->mt_mhash[0];
+       if (pos == LNET_INS_AFTER || pos == LNET_INS_LOCAL)
+               list_add_tail(&me->me_list, head);
+       else
+               list_add(&me->me_list, head);
+
+       lnet_res_unlock(mtable->mt_cpt);
+       return me;
 }
+EXPORT_SYMBOL(LNetMEAttach);
 
-int do_PtlMEDump(nal_cb_t * nal, void *private, void *v_args, void *v_ret)
+/* call with lnet_res_lock please */
+void
+lnet_me_unlink(struct lnet_me *me)
 {
-        PtlMEDump_in *args = v_args;
-        PtlMEDump_out *ret = v_ret;
-        lib_me_t *me;
-        unsigned long flags;
-
-        state_lock(nal, &flags);
+       list_del(&me->me_list);
 
-        me = ptl_handle2me(&args->current_in, nal);
-        if (me == NULL) {
-                ret->rc = PTL_INV_ME;
-        } else {
-                lib_me_dump(nal, me);
-                ret->rc = PTL_OK;
-        }
+       if (me->me_md != NULL) {
+               struct lnet_libmd *md = me->me_md;
 
-        state_unlock(nal, &flags);
+               /* detach MD from portal of this ME */
+               lnet_ptl_detach_md(me, md);
+               lnet_md_unlink(md);
+       }
 
-        return ret->rc;
+       CDEBUG(D_MALLOC, "slab-freed 'me' at %p.\n", me);
+       kmem_cache_free(lnet_mes_cachep, me);
 }
 
-static void lib_me_dump(nal_cb_t * nal, lib_me_t * me)
+#if 0
+static void
+lib_me_dump(struct lnet_me *me)
 {
-        nal->cb_printf(nal, "Match Entry %p ("LPX64")\n", me, 
-                       me->me_lh.lh_cookie);
+       CWARN("Match Entry %p (%#llx)\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",
+             list_entry(me->me_list.prev, struct lnet_me, me_list));
+       CWARN("\tnext\t= %p\n",
+             list_entry(me->me_list.next, struct lnet_me, me_list));
 }
+#endif