Motr  M0
rm.c File Reference
#include "lib/memory.h"
#include "lib/misc.h"
#include "lib/errno.h"
#include "lib/arith.h"
#include "lib/mutex.h"
#include "lib/trace.h"
#include "lib/bob.h"
#include "lib/finject.h"
#include "fid/fid.h"
#include "addb2/addb2.h"
#include "motr/magic.h"
#include "sm/sm.h"
#include "conf/obj.h"
#include "rm/rm.h"
#include "rm/rm_internal.h"
#include "rpc/service.h"
Include dependency graph for rm.c:

Go to the source code of this file.

Data Structures

struct  owner_invariant_state
 

Macros

#define M0_TRACE_SUBSYSTEM   M0_TRACE_SUBSYS_RM
 
#define INCOMING_CREDIT(in)   in->rin_want.cr_datum
 

Enumerations

enum  { CNT_NR, CNT_TIME, CNT_LAST }
 

Functions

static void resource_get (struct m0_rm_resource *res)
 
static void resource_put (struct m0_rm_resource *res)
 
static struct m0_rm_incomingcr2in (const struct m0_rm_credit *cr)
 
static void windup_incoming_complete (struct m0_rm_incoming *in, int32_t rc)
 
static void windup_incoming_conflict (struct m0_rm_incoming *in)
 
static void cached_credits_clear (struct m0_rm_owner *owner)
 
static bool owner_is_idle (const struct m0_rm_owner *o)
 
static bool owner_is_liquidated (const struct m0_rm_owner *o)
 
static int remnant_loan_get (const struct m0_rm_loan *loan, const struct m0_rm_credit *credit, struct m0_rm_loan **remnant_loan)
 
static int loan_dup (const struct m0_rm_loan *src_loan, struct m0_rm_loan **dest_loan)
 
static void owner_liquidate (struct m0_rm_owner *src_owner)
 
static void owner_cleanup (struct m0_rm_owner *owner)
 
static void credit_processor (struct m0_rm_resource_type *rt)
 
static bool owner_smgrp_is_locked (const struct m0_rm_owner *owner)
 
 M0_TL_DESCR_DEFINE (res, "resources",, struct m0_rm_resource, r_linkage, r_magix, M0_RM_RESOURCE_MAGIC, M0_RM_RESOURCE_HEAD_MAGIC)
 
 M0_TL_DEFINE (res, M0_INTERNAL, struct m0_rm_resource)
 
 M0_BOB_DEFINE (M0_INTERNAL, &resource_bob, m0_rm_resource)
 
 M0_TL_DESCR_DEFINE (m0_rm_ur, "usage credits",, struct m0_rm_credit, cr_linkage, cr_magix, M0_RM_CREDIT_MAGIC, M0_RM_USAGE_CREDIT_HEAD_MAGIC)
 
 M0_TL_DEFINE (m0_rm_ur, M0_INTERNAL, struct m0_rm_credit)
 
 M0_TL_DESCR_DEFINE (m0_remotes, "remote owners",, struct m0_rm_remote, rem_res_linkage, rem_magix, M0_RM_REMOTE_MAGIC, M0_RM_REMOTE_OWNER_HEAD_MAGIC)
 
 M0_TL_DEFINE (m0_remotes, M0_INTERNAL, struct m0_rm_remote)
 
 M0_TL_DESCR_DEFINE (m0_owners, "local owners",, struct m0_rm_owner, ro_owner_linkage, ro_magix, M0_RM_OWNER_LIST_MAGIC, M0_RM_OWNER_LIST_HEAD_MAGIC)
 
 M0_TL_DEFINE (m0_owners, M0_INTERNAL, struct m0_rm_owner)
 
 M0_BOB_DEFINE (M0_INTERNAL, &credit_bob, m0_rm_credit)
 
 M0_TL_DESCR_DEFINE (pr, "pins-of-credit",, struct m0_rm_pin, rp_credit_linkage, rp_magix, M0_RM_PIN_MAGIC, M0_RM_CREDIT_PIN_HEAD_MAGIC)
 
 M0_TL_DEFINE (pr, M0_INTERNAL, struct m0_rm_pin)
 
 M0_TL_DESCR_DEFINE (pi, "pins-of-incoming",, struct m0_rm_pin, rp_incoming_linkage, rp_magix, M0_RM_PIN_MAGIC, M0_RM_INCOMING_PIN_HEAD_MAGIC)
 
 M0_TL_DEFINE (pi, M0_INTERNAL, struct m0_rm_pin)
 
 M0_BOB_DEFINE (static, &pin_bob, m0_rm_pin)
 
 M0_BOB_DEFINE (, &loan_bob, m0_rm_loan)
 
 M0_BOB_DEFINE (static, &incoming_bob, m0_rm_incoming)
 
 M0_BOB_DEFINE (M0_INTERNAL, &outgoing_bob, m0_rm_outgoing)
 
 M0_BOB_DEFINE (M0_INTERNAL, &rem_bob, m0_rm_remote)
 
M0_INTERNAL void m0_rm_domain_init (struct m0_rm_domain *dom)
 
M0_INTERNAL void m0_rm_domain_fini (struct m0_rm_domain *dom)
 
M0_INTERNAL struct m0_rm_resourcem0_rm_resource_find (const struct m0_rm_resource_type *rt, const struct m0_rm_resource *res)
 
M0_INTERNAL int m0_rm_type_register (struct m0_rm_domain *dom, struct m0_rm_resource_type *rt)
 
M0_INTERNAL void m0_rm_type_deregister (struct m0_rm_resource_type *rt)
 
M0_INTERNAL struct m0_rm_resource_typem0_rm_resource_type_lookup (const struct m0_rm_domain *dom, const uint64_t rtype_id)
 
M0_INTERNAL void m0_rm_resource_add (struct m0_rm_resource_type *rtype, struct m0_rm_resource *res)
 
M0_INTERNAL void m0_rm_resource_del (struct m0_rm_resource *res)
 
M0_INTERNAL void m0_rm_resource_free (struct m0_rm_resource *res)
 
M0_INTERNAL int m0_rm_resource_encode (struct m0_rm_resource *res, struct m0_buf *buf)
 
static void owner_state_set (struct m0_rm_owner *owner, enum m0_rm_owner_state state)
 
static void owner_fail (struct m0_rm_owner *owner, enum m0_rm_owner_state state, int rc)
 
static bool owner_has_loans (struct m0_rm_owner *owner)
 
static void owner_finalisation_check (struct m0_rm_owner *owner)
 
M0_INTERNAL void m0_rm_owner_lock (struct m0_rm_owner *owner)
 
static int m0_rm_owner_trylock (struct m0_rm_owner *owner)
 
M0_INTERNAL void m0_rm_owner_unlock (struct m0_rm_owner *owner)
 
M0_INTERNAL void m0_rm_owner_init (struct m0_rm_owner *owner, struct m0_fid *fid, const struct m0_uint128 *group, struct m0_rm_resource *res, struct m0_rm_remote *creditor)
 
M0_INTERNAL void m0_rm_owner_init_rfid (struct m0_rm_owner *owner, const struct m0_uint128 *group, struct m0_rm_resource *res, struct m0_rm_remote *creditor)
 
static void ha_events_handle (struct m0_rm_resource_type *rt)
 
static void reserve_prio_set (struct m0_rm_reserve_prio *prio, m0_time_t timestamp, struct m0_rm_owner *owner)
 
static bool reserve_prio_is_set (struct m0_rm_reserve_prio *prio)
 
M0_INTERNAL int m0_rm_owner_selfadd (struct m0_rm_owner *owner, struct m0_rm_credit *r)
 
M0_INTERNAL int m0_rm_owner_timedwait (struct m0_rm_owner *owner, uint64_t state, const m0_time_t abs_timeout)
 
M0_INTERNAL void m0_rm_owner_creditor_reset (struct m0_rm_owner *owner, struct m0_rm_remote *creditor)
 
static void owner_windup_locked (struct m0_rm_owner *owner)
 
M0_INTERNAL void m0_rm_owner_windup (struct m0_rm_owner *owner)
 
M0_INTERNAL void m0_rm_owner_fini (struct m0_rm_owner *owner)
 
M0_INTERNAL void m0_rm_credit_init (struct m0_rm_credit *credit, struct m0_rm_owner *owner)
 
M0_INTERNAL void m0_rm_credit_fini (struct m0_rm_credit *credit)
 
static void incoming_state_set (struct m0_rm_incoming *in, enum m0_rm_incoming_state state)
 
M0_INTERNAL void m0_rm_incoming_init (struct m0_rm_incoming *in, struct m0_rm_owner *owner, enum m0_rm_incoming_type type, enum m0_rm_incoming_policy policy, uint64_t flags)
 
M0_INTERNAL void incoming_surrender (struct m0_rm_incoming *in)
 
M0_INTERNAL void internal_incoming_fini (struct m0_rm_incoming *in)
 
M0_INTERNAL void m0_rm_incoming_fini (struct m0_rm_incoming *in)
 
M0_INTERNAL int m0_rm_outgoing_init (struct m0_rm_outgoing *out, enum m0_rm_outgoing_type req_type, struct m0_rm_remote *other, struct m0_rm_credit *credit)
 
M0_INTERNAL void m0_rm_outgoing_fini (struct m0_rm_outgoing *out)
 
M0_INTERNAL int m0_rm_loan_alloc (struct m0_rm_loan **loan, const struct m0_rm_credit *credit, struct m0_rm_remote *creditor)
 
M0_INTERNAL int m0_rm_loan_init (struct m0_rm_loan *loan, const struct m0_rm_credit *credit, struct m0_rm_remote *creditor)
 
M0_INTERNAL void m0_rm_loan_fini (struct m0_rm_loan *loan)
 
static void pending_outgoing_send (struct m0_rm_owner *owner, struct m0_clink *link)
 
static bool rev_session_clink_cb (struct m0_clink *link)
 
M0_INTERNAL struct m0_rm_remotem0_rm_remote_find (struct m0_rm_remote_incoming *rem_in)
 
static void rm_remote_free (struct m0_ref *ref)
 
M0_INTERNAL void m0_rm_remote_init (struct m0_rm_remote *rem, struct m0_rm_resource *res)
 
M0_INTERNAL void m0_rm_remote_fini (struct m0_rm_remote *rem)
 
static int cached_credits_remove (struct m0_rm_incoming *in)
 
static M0_UNUSED struct m0_rm_resource_typecredit_to_resource_type (struct m0_rm_credit *credit)
 
static M0_UNUSED struct m0_rm_resource_typerem_incoming_to_resource_type (struct m0_rm_remote_incoming *rem_in)
 
M0_INTERNAL int m0_rm_borrow_commit (struct m0_rm_remote_incoming *rem_in)
 
M0_INTERNAL int m0_rm_revoke_commit (struct m0_rm_remote_incoming *rem_in)
 
Owner state machine

m0_rm_owner and m0_rm_incoming together form a state machine where basic resource management functionality is implemented.

This state machine reacts to the following external events:

  • an incoming request from a local user;
  • an incoming loan request from another domain;
  • an incoming revocation request from another domain;
  • local user releases a pin on a credit (as a by-product of destroying an incoming request);
  • completion of an outgoing request to another domain (including a timeout or a failure).

Any event is processed in a uniform manner:

  1. m0_rm_owner::ro_sm_grp Group lock is taken;
  2. m0_rm_owner lists are updated to reflect the event, see details below. This temporarily violates the owner_invariant();
  3. owner_balance() is called to restore the invariant, this might create new imbalances and go through several iterations;
  4. m0_rm_owner::ro_sm_grp Group lock is released.

Event handling is serialised by the owner lock. It is not legal to wait for networking or IO events under this lock.

static void owner_balance (struct m0_rm_owner *o)
 
static void incoming_check (struct m0_rm_incoming *in)
 
static int incoming_check_with (struct m0_rm_incoming *in, struct m0_rm_credit *credit)
 
static void incoming_complete (struct m0_rm_incoming *in, int32_t rc)
 
static void incoming_policy_apply (struct m0_rm_incoming *in)
 
static void incoming_policy_none (struct m0_rm_incoming *in)
 
static int outgoing_check (struct m0_rm_incoming *in, enum m0_rm_outgoing_type, struct m0_rm_credit *credit, struct m0_rm_remote *other)
 
static int revoke_send (struct m0_rm_incoming *in, struct m0_rm_loan *loan, struct m0_rm_credit *credit)
 
static int cancel_send (struct m0_rm_loan *loan)
 
static int borrow_send (struct m0_rm_incoming *in, struct m0_rm_credit *credit)
 
static void conflict_notify (struct m0_rm_credit *credit)
 
static int cached_credits_hold (struct m0_rm_incoming *in)
 
static bool incoming_is_complete (const struct m0_rm_incoming *in)
 
static bool credit_group_conflict (const struct m0_uint128 *g1, const struct m0_uint128 *g2)
 
static void incoming_queue (struct m0_rm_owner *owner, struct m0_rm_incoming *in)
 
M0_INTERNAL void m0_rm_credit_get (struct m0_rm_incoming *in)
 
M0_INTERNAL void m0_rm_credit_put (struct m0_rm_incoming *in)
 
static void incoming_pins_del (struct m0_rm_incoming *in, uint32_t flags)
 
static void barrier_pins_del (struct m0_rm_incoming *in)
 
static bool credit_is_reserved (const struct m0_rm_credit *cr)
 
static int credit_maybe_track (struct m0_rm_incoming *in, struct m0_rm_credit *cr, bool notify, int *wait)
 
static int incoming_check_held (struct m0_rm_incoming *in, struct m0_rm_credit *rest, struct m0_rm_credit *held, int *wait, bool *cr_used)
 
static bool has_reserve_priority (struct m0_rm_incoming *in1, struct m0_rm_incoming *in2)
 
static int credit_reservation_check (struct m0_rm_incoming *in, struct m0_rm_credit *cr, int *wait)
 
M0_INTERNAL void m0_rm_outgoing_complete (struct m0_rm_outgoing *og)
 
M0_INTERNAL int m0_rm_owner_loan_debit (struct m0_rm_owner *owner, struct m0_rm_loan *paid_loan, struct m0_tl *list)
 
M0_INTERNAL int granted_maybe_reserve (struct m0_rm_credit *granted, struct m0_rm_credit *to_cache)
 
static int loan_check (struct m0_rm_owner *owner, struct m0_tl *list, struct m0_rm_credit *rest)
 
M0_INTERNAL int m0_rm_loan_settle (struct m0_rm_owner *owner, struct m0_rm_loan *loan)
 
Pin helpers
static void pin_del (struct m0_rm_pin *pin)
 
static int incoming_pin_nr (const struct m0_rm_incoming *in, uint32_t flags)
 
static void incoming_release (struct m0_rm_incoming *in)
 
static int credit_pin_nr (const struct m0_rm_credit *credit, uint32_t flags)
 
M0_INTERNAL int m0_rm_pin_add (struct m0_rm_incoming *in, struct m0_rm_credit *credit, uint32_t flags)
 
remote Code to deal with remote owners
static int service_locate (struct m0_rm_resource_type *rtype, struct m0_rm_remote *rem)
 
static int resource_locate (struct m0_rm_resource_type *rtype, struct m0_rm_remote *rem)
 
M0_INTERNAL int m0_rm_db_service_query (const char *name, struct m0_rm_remote *rem)
 
M0_INTERNAL int m0_rm_remote_resource_locate (struct m0_rm_remote *rem)
 
M0_INTERNAL int m0_rm_net_locate (struct m0_rm_credit *credit, struct m0_rm_remote *other)
 
Credit helpers
static bool credit_eq (const struct m0_rm_credit *c0, const struct m0_rm_credit *c1)
 
static bool credit_is_empty (const struct m0_rm_credit *credit)
 
static bool credit_intersects (const struct m0_rm_credit *A, const struct m0_rm_credit *B)
 
static bool credit_conflicts (const struct m0_rm_credit *A, const struct m0_rm_credit *B)
 
static int credit_diff (struct m0_rm_credit *c0, const struct m0_rm_credit *c1)
 
static int remnant_credit_get (const struct m0_rm_credit *src, const struct m0_rm_credit *diff, struct m0_rm_credit **remnant_credit)
 
M0_INTERNAL int m0_rm_credit_dup (const struct m0_rm_credit *src_credit, struct m0_rm_credit **dest_credit)
 
M0_INTERNAL int m0_rm_credit_copy (struct m0_rm_credit *dst, const struct m0_rm_credit *src)
 
M0_INTERNAL int m0_rm_credit_encode (struct m0_rm_credit *credit, struct m0_buf *buf)
 
M0_INTERNAL int m0_rm_credit_decode (struct m0_rm_credit *credit, struct m0_buf *buf)
 
HA-notification Code to deal with HA notifications on creditors/debtors

death

static bool rm_on_remote_death_cb (struct m0_clink *link)
 
static void rm_remote_online_handler (struct m0_rm_remote *remote)
 
static void rm_remote_death_handler (struct m0_rm_remote *remote)
 
static struct m0_queueremote_to_queue (struct m0_rm_remote *remote)
 

Variables

static struct m0_bob_type resource_bob
 
static const struct m0_bob_type credit_bob
 
static const struct m0_bob_type pin_bob
 
M0_INTERNAL const struct m0_bob_type loan_bob
 
static const struct m0_bob_type incoming_bob
 
static const struct m0_bob_type outgoing_bob
 
static const struct m0_bob_type rem_bob
 
const struct m0_uint128 m0_rm_no_group = M0_UINT128(0, 0)
 
static const struct m0_rm_incoming_ops windup_incoming_ops
 
static struct m0_sm_state_descr owner_states []
 
static const struct m0_sm_conf owner_conf
 
static struct m0_sm_state_descr inc_states []
 
static const struct m0_sm_conf inc_conf
 

Invariants group

Resource manager maintains a number of interrelated data-structures in memory. Invariant checking functions, defined in this section assert internal consistency of these structures.

enum  credit_queue {
  OIS_BORROWED = 0, OIS_SUBLET, OIS_OUTGOING, OIS_OWNED,
  OIS_INCOMING, OIS_NR
}
 
static bool resource_list_check (const struct m0_rm_resource *res, void *datum)
 
static bool resource_type_invariant (const struct m0_rm_resource_type *rt)
 
static bool owner_invariant (struct m0_rm_owner *owner)
 
static bool owner_invariant_state (const struct m0_rm_owner *owner, struct owner_invariant_state *is)
 
static bool incoming_invariant (const struct m0_rm_incoming *in)
 
static bool pin_check (const void *bob)
 
static bool credit_invariant (const struct m0_rm_credit *credit, void *data)
 
static bool conflict_exists (const struct m0_rm_credit *cr, const struct m0_rm_owner *owner)
 

Macro Definition Documentation

◆ M0_TRACE_SUBSYSTEM

#define M0_TRACE_SUBSYSTEM   M0_TRACE_SUBSYS_RM

Definition at line 25 of file rm.c.