-/* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
- * vim:expandtab:shiftwidth=8:tabstop=8:
- *
+/*
* GPL HEADER START
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* GPL HEADER END
*/
/*
- * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
+ * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
+ *
+ * Copyright (c) 2011, 2012, Whamcloud, Inc.
*/
/*
* This file is part of Lustre, http://www.lustre.org/
*/
#define DEBUG_SUBSYSTEM S_CLASS
-#ifndef EXPORT_SYMTAB
-# define EXPORT_SYMTAB
-#endif
#include <libcfs/libcfs.h>
/* class_put_type() */
/** Lock class of cl_object_header::coh_attr_guard */
static cfs_lock_class_key_t cl_attr_guard_class;
+extern __u32 lu_context_tags_default;
+extern __u32 lu_session_tags_default;
/**
* Initialize cl_object_header.
*/
cfs_spin_lock_init(&h->coh_page_guard);
cfs_spin_lock_init(&h->coh_lock_guard);
cfs_spin_lock_init(&h->coh_attr_guard);
- cfs_lockdep_set_class(&h->coh_attr_guard, &cl_page_guard_class);
- cfs_lockdep_set_class(&h->coh_attr_guard, &cl_lock_guard_class);
+ cfs_lockdep_set_class(&h->coh_page_guard, &cl_page_guard_class);
+ cfs_lockdep_set_class(&h->coh_lock_guard, &cl_lock_guard_class);
cfs_lockdep_set_class(&h->coh_attr_guard, &cl_attr_guard_class);
h->coh_pages = 0;
/* XXX hard coded GFP_* mask. */
*
*/
+/**
+ * The most efficient way is to store cl_env pointer in task specific
+ * structures. On Linux, it wont' be easy to use task_struct->journal_info
+ * because Lustre code may call into other fs which has certain assumptions
+ * about journal_info. Currently following fields in task_struct are identified
+ * can be used for this purpose:
+ * - cl_env: for liblustre.
+ * - tux_info: ony on RedHat kernel.
+ * - ...
+ * \note As long as we use task_struct to store cl_env, we assume that once
+ * called into Lustre, we'll never call into the other part of the kernel
+ * which will use those fields in task_struct without explicitly exiting
+ * Lustre.
+ *
+ * If there's no space in task_struct is available, hash will be used.
+ * bz20044, bz22683.
+ */
+
static CFS_LIST_HEAD(cl_envs);
static unsigned cl_envs_cached_nr = 0;
static unsigned cl_envs_cached_max = 128; /* XXX: prototype: arbitrary limit
* for now. */
-static cfs_spinlock_t cl_envs_guard = CFS_SPIN_LOCK_UNLOCKED;
+static DEFINE_SPINLOCK(cl_envs_guard);
struct cl_env {
void *ce_magic;
struct lu_env ce_lu;
struct lu_context ce_ses;
+
+#ifdef LL_TASK_CL_ENV
+ void *ce_prev;
+#else
/**
* This allows cl_env to be entered into cl_env_hash which implements
* the current thread -> client environment lookup.
*/
cfs_hlist_node_t ce_node;
+#endif
/**
- * Owner for the current cl_env, the key for cfs_hash.
- * Now current thread pointer is stored.
+ * Owner for the current cl_env.
+ *
+ * If LL_TASK_CL_ENV is defined, this point to the owning cfs_current(),
+ * only for debugging purpose ;
+ * Otherwise hash is used, and this is the key for cfs_hash.
+ * Now current thread pid is stored. Note using thread pointer would
+ * lead to unbalanced hash because of its specific allocation locality
+ * and could be varied for different platforms and OSes, even different
+ * OS versions.
*/
void *ce_owner;
+
/*
* Linkage into global list of all client environments. Used for
* garbage collection.
cfs_atomic_dec(&cl_env_stats.counter); \
} while (0)
-/*****************************************************************************
- * Routins to use cfs_hash functionality to bind the current thread
- * to cl_env
+static void cl_env_init0(struct cl_env *cle, void *debug)
+{
+ LASSERT(cle->ce_ref == 0);
+ LASSERT(cle->ce_magic == &cl_env_init0);
+ LASSERT(cle->ce_debug == NULL && cle->ce_owner == NULL);
+
+ cle->ce_ref = 1;
+ cle->ce_debug = debug;
+ CL_ENV_INC(cs_busy);
+}
+
+
+#ifndef LL_TASK_CL_ENV
+/*
+ * The implementation of using hash table to connect cl_env and thread
*/
-/** lustre hash to manage the cl_env for current thread */
static cfs_hash_t *cl_env_hash;
-static void cl_env_init0(struct cl_env *cle, void *debug);
-static unsigned cl_env_hops_hash(cfs_hash_t *lh, void *key, unsigned mask)
+static unsigned cl_env_hops_hash(cfs_hash_t *lh,
+ const void *key, unsigned mask)
{
#if BITS_PER_LONG == 64
return cfs_hash_u64_hash((__u64)key, mask);
return (void *)cle;
}
-static int cl_env_hops_compare(void *key, cfs_hlist_node_t *hn)
+static int cl_env_hops_keycmp(const void *key, cfs_hlist_node_t *hn)
{
struct cl_env *cle = cl_env_hops_obj(hn);
return (key == cle->ce_owner);
}
+static void cl_env_hops_noop(cfs_hash_t *hs, cfs_hlist_node_t *hn)
+{
+ struct cl_env *cle = cfs_hlist_entry(hn, struct cl_env, ce_node);
+ LASSERT(cle->ce_magic == &cl_env_init0);
+}
+
static cfs_hash_ops_t cl_env_hops = {
- .hs_hash = cl_env_hops_hash,
- .hs_compare = cl_env_hops_compare,
- .hs_key = cl_env_hops_obj,
- .hs_get = cl_env_hops_obj,
- .hs_put = cl_env_hops_obj,
+ .hs_hash = cl_env_hops_hash,
+ .hs_key = cl_env_hops_obj,
+ .hs_keycmp = cl_env_hops_keycmp,
+ .hs_object = cl_env_hops_obj,
+ .hs_get = cl_env_hops_noop,
+ .hs_put_locked = cl_env_hops_noop,
};
static inline struct cl_env *cl_env_fetch(void)
{
struct cl_env *cle;
- cle = cfs_hash_lookup(cl_env_hash, cfs_current());
+
+ cle = cfs_hash_lookup(cl_env_hash, (void *) (long) cfs_current()->pid);
LASSERT(ergo(cle, cle->ce_magic == &cl_env_init0));
return cle;
}
{
if (cle) {
int rc;
+
LASSERT(cle->ce_owner == NULL);
- cle->ce_owner = cfs_current();
+ cle->ce_owner = (void *) (long) cfs_current()->pid;
rc = cfs_hash_add_unique(cl_env_hash, cle->ce_owner,
- &cle->ce_node);
+ &cle->ce_node);
LASSERT(rc == 0);
}
}
-static inline struct cl_env *cl_env_detach(struct cl_env *cle)
+static inline void cl_env_do_detach(struct cl_env *cle)
{
- if (cle == NULL)
- cle = cl_env_fetch();
- if (cle && cle->ce_owner) {
- void *cookie;
- LASSERT(cle->ce_owner == cfs_current());
- cookie = cfs_hash_del(cl_env_hash, cle->ce_owner,
- &cle->ce_node);
- cle->ce_owner = NULL;
- LASSERT(cookie == cle);
- }
+ void *cookie;
+
+ LASSERT(cle->ce_owner == (void *) (long) cfs_current()->pid);
+ cookie = cfs_hash_del(cl_env_hash, cle->ce_owner,
+ &cle->ce_node);
+ LASSERT(cookie == cle);
+ cle->ce_owner = NULL;
+}
+
+static int cl_env_store_init(void) {
+ cl_env_hash = cfs_hash_create("cl_env",
+ HASH_CL_ENV_BITS, HASH_CL_ENV_BITS,
+ HASH_CL_ENV_BKT_BITS, 0,
+ CFS_HASH_MIN_THETA,
+ CFS_HASH_MAX_THETA,
+ &cl_env_hops,
+ CFS_HASH_RW_BKTLOCK);
+ return cl_env_hash != NULL ? 0 :-ENOMEM;
+}
+
+static void cl_env_store_fini(void) {
+ cfs_hash_putref(cl_env_hash);
+}
+
+#else /* LL_TASK_CL_ENV */
+/*
+ * The implementation of store cl_env directly in thread structure.
+ */
+
+static inline struct cl_env *cl_env_fetch(void)
+{
+ struct cl_env *cle;
+
+ cle = cfs_current()->LL_TASK_CL_ENV;
+ if (cle && cle->ce_magic != &cl_env_init0)
+ cle = NULL;
return cle;
}
-/* ----------------------- hash routines end ---------------------------- */
-static void cl_env_init0(struct cl_env *cle, void *debug)
+static inline void cl_env_attach(struct cl_env *cle)
{
- LASSERT(cle->ce_ref == 0);
- LASSERT(cle->ce_magic == &cl_env_init0);
- LASSERT(cle->ce_debug == NULL && cle->ce_owner == NULL);
+ if (cle) {
+ LASSERT(cle->ce_owner == NULL);
+ cle->ce_owner = cfs_current();
+ cle->ce_prev = cfs_current()->LL_TASK_CL_ENV;
+ cfs_current()->LL_TASK_CL_ENV = cle;
+ }
+}
- cle->ce_ref = 1;
- cle->ce_debug = debug;
- CL_ENV_INC(cs_busy);
+static inline void cl_env_do_detach(struct cl_env *cle)
+{
+ LASSERT(cle->ce_owner == cfs_current());
+ LASSERT(cfs_current()->LL_TASK_CL_ENV == cle);
+ cfs_current()->LL_TASK_CL_ENV = cle->ce_prev;
+ cle->ce_owner = NULL;
+}
+
+static int cl_env_store_init(void) { return 0; }
+static void cl_env_store_fini(void) { }
+
+#endif /* LL_TASK_CL_ENV */
+
+static inline struct cl_env *cl_env_detach(struct cl_env *cle)
+{
+ if (cle == NULL)
+ cle = cl_env_fetch();
+
+ if (cle && cle->ce_owner)
+ cl_env_do_detach(cle);
+
+ return cle;
}
-static struct lu_env *cl_env_new(__u32 tags, void *debug)
+static struct lu_env *cl_env_new(__u32 ctx_tags, __u32 ses_tags, void *debug)
{
struct lu_env *env;
struct cl_env *cle;
CFS_INIT_LIST_HEAD(&cle->ce_linkage);
cle->ce_magic = &cl_env_init0;
env = &cle->ce_lu;
- rc = lu_env_init(env, LCT_CL_THREAD|tags);
+ rc = lu_env_init(env, LCT_CL_THREAD|ctx_tags);
if (rc == 0) {
- rc = lu_context_init(&cle->ce_ses, LCT_SESSION|tags);
+ rc = lu_context_init(&cle->ce_ses,
+ LCT_SESSION | ses_tags);
if (rc == 0) {
lu_context_enter(&cle->ce_ses);
env->le_ses = &cle->ce_ses;
}
} else {
cfs_spin_unlock(&cl_envs_guard);
- env = cl_env_new(0, debug);
+ env = cl_env_new(lu_context_tags_default,
+ lu_session_tags_default, debug);
}
RETURN(env);
}
env = &cle->ce_lu;
*refcheck = ++cle->ce_ref;
}
- CDEBUG(D_OTHER, "%i@%p\n", cle ? cle->ce_ref : 0, cle);
+ CDEBUG(D_OTHER, "%d@%p\n", cle ? cle->ce_ref : 0, cle);
return env;
}
EXPORT_SYMBOL(cl_env_peek);
cle = cl_env_container(env);
cl_env_attach(cle);
*refcheck = cle->ce_ref;
- CDEBUG(D_OTHER, "%i@%p\n", cle->ce_ref, cle);
+ CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
}
}
return env;
struct lu_env *env;
LASSERT(cl_env_peek(refcheck) == NULL);
- env = cl_env_new(tags, __builtin_return_address(0));
+ env = cl_env_new(tags, tags, __builtin_return_address(0));
if (!IS_ERR(env)) {
struct cl_env *cle;
cle = cl_env_container(env);
*refcheck = cle->ce_ref;
- CDEBUG(D_OTHER, "%i@%p\n", cle->ce_ref, cle);
+ CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
}
return env;
}
LASSERT(cle->ce_ref > 0);
LASSERT(ergo(refcheck != NULL, cle->ce_ref == *refcheck));
- CDEBUG(D_OTHER, "%i@%p\n", cle->ce_ref, cle);
+ CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
if (--cle->ce_ref == 0) {
CL_ENV_DEC(cs_busy);
cl_env_detach(cle);
cl_env_attach(cle);
cl_env_get(refcheck);
- CDEBUG(D_OTHER, "%i@%p\n", cle->ce_ref, cle);
+ CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
}
EXPORT_SYMBOL(cl_env_implant);
LASSERT(cle->ce_ref > 1);
- CDEBUG(D_OTHER, "%i@%p\n", cle->ce_ref, cle);
+ CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
cl_env_detach(cle);
cl_env_put(env, refcheck);
{
int result;
- cl_env_hash = cfs_hash_create("cl_env", 8, 10, &cl_env_hops,
- CFS_HASH_REHASH);
- if (cl_env_hash == NULL)
- return -ENOMEM;
+ result = cl_env_store_init();
+ if (result)
+ return result;
result = lu_kmem_init(cl_object_caches);
- if (result == 0) {
- LU_CONTEXT_KEY_INIT(&cl_key);
- result = lu_context_key_register(&cl_key);
- if (result == 0) {
- result = cl_lock_init();
- if (result == 0)
- result = cl_page_init();
- }
- }
if (result)
- cfs_hash_putref(cl_env_hash);
+ goto out_store;
+
+ LU_CONTEXT_KEY_INIT(&cl_key);
+ result = lu_context_key_register(&cl_key);
+ if (result)
+ goto out_kmem;
+
+ result = cl_lock_init();
+ if (result)
+ goto out_context;
+
+ result = cl_page_init();
+ if (result)
+ goto out_lock;
+
+ return 0;
+out_lock:
+ cl_lock_fini();
+out_context:
+ lu_context_key_degister(&cl_key);
+out_kmem:
+ lu_kmem_fini(cl_object_caches);
+out_store:
+ cl_env_store_fini();
return result;
}
cl_page_fini();
lu_context_key_degister(&cl_key);
lu_kmem_fini(cl_object_caches);
- cfs_hash_putref(cl_env_hash);
+ cl_env_store_fini();
}