1 // SPDX-License-Identifier: GPL-2.0
3 /* Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
4 * Use is subject to license terms.
6 * Copyright (c) 2012, 2013, Intel Corporation.
9 /* This file is part of Lustre, http://www.lustre.org/
11 * Match Entry management routines
14 #define DEBUG_SUBSYSTEM S_LNET
16 #include <lnet/lib-lnet.h>
19 * Create and attach a match entry to the match list of \a portal. The new
20 * ME is empty, i.e. not associated with a memory descriptor. LNetMDAttach()
21 * can be used to attach a MD to an empty ME.
23 * \param portal The portal table index where the ME should be attached.
24 * \param match_id Specifies the match criteria for the process ID of
25 * the requester. The constants LNET_PID_ANY and LNET_NID_ANY can be
26 * used to wildcard either of the identifiers in the struct lnet_process_id
28 * \param match_bits,ignore_bits Specify the match criteria to apply
29 * to the match bits in the incoming request. The ignore bits are used
30 * to mask out insignificant bits in the incoming match bits. The resulting
31 * bits are then compared to the ME's match bits to determine if the
32 * incoming request meets the match criteria.
33 * \param unlink Indicates whether the ME should be unlinked when the memory
34 * descriptor associated with it is unlinked (Note that the check for
35 * unlinking a ME only occurs when the memory descriptor is unlinked.).
36 * Valid values are LNET_RETAIN and LNET_UNLINK.
37 * \param pos Indicates whether the new ME should be prepended or
38 * appended to the match list. Allowed constants: LNET_INS_BEFORE,
41 * \retval A handle to the newly created ME is returned on success
42 * \retval ERR_PTR(-EINVAL) If \a portal is invalid.
43 * \retval ERR_PTR(-ENOMEM) If new ME object cannot be allocated.
46 LNetMEAttach(unsigned int portal,
47 struct lnet_processid *match_id,
48 __u64 match_bits, __u64 ignore_bits,
49 enum lnet_unlink unlink, enum lnet_ins_pos pos)
51 struct lnet_match_table *mtable;
53 struct list_head *head;
55 LASSERT(the_lnet.ln_refcount > 0);
57 if ((int)portal >= the_lnet.ln_nportals)
58 return ERR_PTR(-EINVAL);
60 mtable = lnet_mt_of_attach(portal, match_id,
61 match_bits, ignore_bits, pos);
62 if (mtable == NULL) /* can't match portal type */
63 return ERR_PTR(-EPERM);
65 me = kmem_cache_zalloc(lnet_mes_cachep, GFP_NOFS);
67 CDEBUG(D_MALLOC, "failed to allocate 'me'\n");
68 return ERR_PTR(-ENOMEM);
70 LIBCFS_ALLOC_POST(me, sizeof(*me), "slab-alloced");
72 lnet_res_lock(mtable->mt_cpt);
74 me->me_portal = portal;
75 me->me_match_id = *match_id;
76 me->me_match_bits = match_bits;
77 me->me_ignore_bits = ignore_bits;
78 me->me_unlink = unlink;
81 me->me_cpt = mtable->mt_cpt;
84 head = &mtable->mt_mhash[LNET_MT_HASH_IGNORE];
86 head = lnet_mt_match_head(mtable, match_id, match_bits);
88 me->me_pos = head - &mtable->mt_mhash[0];
89 if (pos == LNET_INS_AFTER || pos == LNET_INS_LOCAL)
90 list_add_tail(&me->me_list, head);
92 list_add(&me->me_list, head);
94 lnet_res_unlock(mtable->mt_cpt);
97 EXPORT_SYMBOL(LNetMEAttach);
99 /* call with lnet_res_lock please */
101 lnet_me_unlink(struct lnet_me *me)
103 list_del(&me->me_list);
105 if (me->me_md != NULL) {
106 struct lnet_libmd *md = me->me_md;
108 /* detach MD from portal of this ME */
109 lnet_ptl_detach_md(me, md);
113 LIBCFS_FREE_PRE(me, sizeof(*me), "slab-freed");
114 kmem_cache_free(lnet_mes_cachep, me);
119 lib_me_dump(struct lnet_me *me)
121 CWARN("Match Entry %p (%#llx)\n", me,
122 me->me_lh.lh_cookie);
124 CWARN("\tMatch/Ignore\t= %016lx / %016lx\n",
125 me->me_match_bits, me->me_ignore_bits);
127 CWARN("\tMD\t= %p\n", me->md);
128 CWARN("\tprev\t= %p\n",
129 list_entry(me->me_list.prev, struct lnet_me, me_list));
130 CWARN("\tnext\t= %p\n",
131 list_entry(me->me_list.next, struct lnet_me, me_list));