Whamcloud - gitweb
LU-12034 obdclass: put all service's env on the list
[fs/lustre-release.git] / lustre / obdclass / lu_object.c
index 9f62693..ab578a3 100644 (file)
@@ -1882,6 +1882,101 @@ int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
 }
 EXPORT_SYMBOL(lu_env_refill_by_tags);
 
+
+struct lu_env_item {
+       struct task_struct *lei_task;   /* rhashtable key */
+       struct rhash_head lei_linkage;
+       struct lu_env *lei_env;
+};
+
+static const struct rhashtable_params lu_env_rhash_params = {
+       .key_len     = sizeof(struct task_struct *),
+       .key_offset  = offsetof(struct lu_env_item, lei_task),
+       .head_offset = offsetof(struct lu_env_item, lei_linkage),
+    };
+
+struct rhashtable lu_env_rhash;
+
+struct lu_env_percpu {
+       struct task_struct *lep_task;
+       struct lu_env *lep_env ____cacheline_aligned_in_smp;
+};
+
+static struct lu_env_percpu lu_env_percpu[NR_CPUS];
+
+int lu_env_add(struct lu_env *env)
+{
+       struct lu_env_item *lei, *old;
+
+       LASSERT(env);
+
+       OBD_ALLOC_PTR(lei);
+       if (!lei)
+               return -ENOMEM;
+
+       lei->lei_task = current;
+       lei->lei_env = env;
+
+       old = rhashtable_lookup_get_insert_fast(&lu_env_rhash,
+                                               &lei->lei_linkage,
+                                               lu_env_rhash_params);
+       LASSERT(!old);
+
+       return 0;
+}
+EXPORT_SYMBOL(lu_env_add);
+
+void lu_env_remove(struct lu_env *env)
+{
+       struct lu_env_item *lei;
+       const void *task = current;
+       int i;
+
+       for_each_possible_cpu(i) {
+               if (lu_env_percpu[i].lep_env == env) {
+                       LASSERT(lu_env_percpu[i].lep_task == task);
+                       lu_env_percpu[i].lep_task = NULL;
+                       lu_env_percpu[i].lep_env = NULL;
+               }
+       }
+
+       rcu_read_lock();
+       lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
+                                    lu_env_rhash_params);
+       if (lei && rhashtable_remove_fast(&lu_env_rhash, &lei->lei_linkage,
+                                         lu_env_rhash_params) == 0)
+               OBD_FREE_PTR(lei);
+       rcu_read_unlock();
+}
+EXPORT_SYMBOL(lu_env_remove);
+
+struct lu_env *lu_env_find(void)
+{
+       struct lu_env *env = NULL;
+       struct lu_env_item *lei;
+       const void *task = current;
+       int i = get_cpu();
+
+       if (lu_env_percpu[i].lep_task == current) {
+               env = lu_env_percpu[i].lep_env;
+               put_cpu();
+               LASSERT(env);
+               return env;
+       }
+
+       lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
+                                    lu_env_rhash_params);
+       if (lei) {
+               env = lei->lei_env;
+               lu_env_percpu[i].lep_task = current;
+               lu_env_percpu[i].lep_env = env;
+       }
+       put_cpu();
+
+       return env;
+}
+EXPORT_SYMBOL(lu_env_find);
+
 static struct shrinker *lu_site_shrinker;
 
 typedef struct lu_site_stats{
@@ -2097,7 +2192,7 @@ void lu_context_keys_dump(void)
  */
 int lu_global_init(void)
 {
-        int result;
+       int result;
        DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
                         lu_cache_shrink_count, lu_cache_shrink_scan);
 
@@ -2132,6 +2227,8 @@ int lu_global_init(void)
         if (lu_site_shrinker == NULL)
                 return -ENOMEM;
 
+       result = rhashtable_init(&lu_env_rhash, &lu_env_rhash_params);
+
         return result;
 }
 
@@ -2155,6 +2252,8 @@ void lu_global_fini(void)
         lu_env_fini(&lu_shrink_env);
        up_write(&lu_sites_guard);
 
+       rhashtable_destroy(&lu_env_rhash);
+
         lu_ref_global_fini();
 }