4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * Client Lustre Object.
34 * Author: Nikita Danilov <nikita.danilov@sun.com>
35 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
47 #define DEBUG_SUBSYSTEM S_CLASS
49 #include <linux/list.h>
50 #include <libcfs/libcfs.h>
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre_fid.h>
54 #include <libcfs/libcfs_hash.h> /* for cfs_hash stuff */
55 #include <cl_object.h>
56 #include <lu_object.h>
57 #include "cl_internal.h"
59 static struct kmem_cache *cl_env_kmem;
60 struct kmem_cache *cl_dio_aio_kmem;
61 struct kmem_cache *cl_page_kmem_array[16];
62 unsigned short cl_page_kmem_size_array[16];
64 /** Lock class of cl_object_header::coh_attr_guard */
65 static struct lock_class_key cl_attr_guard_class;
68 * Initialize cl_object_header.
70 int cl_object_header_init(struct cl_object_header *h)
75 result = lu_object_header_init(&h->coh_lu);
77 spin_lock_init(&h->coh_attr_guard);
78 lockdep_set_class(&h->coh_attr_guard, &cl_attr_guard_class);
79 h->coh_page_bufsize = 0;
83 EXPORT_SYMBOL(cl_object_header_init);
86 * Finalize cl_object_header.
88 void cl_object_header_fini(struct cl_object_header *h)
90 lu_object_header_fini(&h->coh_lu);
94 * Returns a cl_object with a given \a fid.
96 * Returns either cached or newly created object. Additional reference on the
97 * returned object is acquired.
99 * \see lu_object_find(), cl_page_find(), cl_lock_find()
101 struct cl_object *cl_object_find(const struct lu_env *env,
102 struct cl_device *cd, const struct lu_fid *fid,
103 const struct cl_object_conf *c)
106 return lu2cl(lu_object_find_slice(env, cl2lu_dev(cd), fid, &c->coc_lu));
108 EXPORT_SYMBOL(cl_object_find);
111 * Releases a reference on \a o.
113 * When last reference is released object is returned to the cache, unless
114 * lu_object_header_flags::LU_OBJECT_HEARD_BANSHEE bit is set in its header.
116 * \see cl_page_put(), cl_lock_put().
118 void cl_object_put(const struct lu_env *env, struct cl_object *o)
120 lu_object_put(env, &o->co_lu);
122 EXPORT_SYMBOL(cl_object_put);
125 * Acquire an additional reference to the object \a o.
127 * This can only be used to acquire _additional_ reference, i.e., caller
128 * already has to possess at least one reference to \a o before calling this.
130 * \see cl_page_get(), cl_lock_get().
132 void cl_object_get(struct cl_object *o)
134 lu_object_get(&o->co_lu);
136 EXPORT_SYMBOL(cl_object_get);
139 * Returns the top-object for a given \a o.
143 struct cl_object *cl_object_top(struct cl_object *o)
145 struct cl_object_header *hdr = cl_object_header(o);
146 struct cl_object *top;
148 while (hdr->coh_parent != NULL)
149 hdr = hdr->coh_parent;
151 top = lu2cl(lu_object_top(&hdr->coh_lu));
152 CDEBUG(D_TRACE, "%p -> %p\n", o, top);
155 EXPORT_SYMBOL(cl_object_top);
158 * Returns pointer to the lock protecting data-attributes for the given object
161 * Data-attributes are protected by the cl_object_header::coh_attr_guard
162 * spin-lock in the top-object.
164 * \see cl_attr, cl_object_attr_lock(), cl_object_operations::coo_attr_get().
166 static spinlock_t *cl_object_attr_guard(struct cl_object *o)
168 return &cl_object_header(cl_object_top(o))->coh_attr_guard;
172 * Locks data-attributes.
174 * Prevents data-attributes from changing, until lock is released by
175 * cl_object_attr_unlock(). This has to be called before calls to
176 * cl_object_attr_get(), cl_object_attr_update().
178 void cl_object_attr_lock(struct cl_object *o)
179 __acquires(cl_object_attr_guard(o))
181 spin_lock(cl_object_attr_guard(o));
183 EXPORT_SYMBOL(cl_object_attr_lock);
186 * Releases data-attributes lock, acquired by cl_object_attr_lock().
188 void cl_object_attr_unlock(struct cl_object *o)
189 __releases(cl_object_attr_guard(o))
191 spin_unlock(cl_object_attr_guard(o));
193 EXPORT_SYMBOL(cl_object_attr_unlock);
196 * Returns data-attributes of an object \a obj.
198 * Every layer is asked (by calling cl_object_operations::coo_attr_get())
199 * top-to-bottom to fill in parts of \a attr that this layer is responsible
202 int cl_object_attr_get(const struct lu_env *env, struct cl_object *obj,
203 struct cl_attr *attr)
205 struct lu_object_header *top;
208 assert_spin_locked(cl_object_attr_guard(obj));
211 top = obj->co_lu.lo_header;
213 list_for_each_entry(obj, &top->loh_layers, co_lu.lo_linkage) {
214 if (obj->co_ops->coo_attr_get != NULL) {
215 result = obj->co_ops->coo_attr_get(env, obj, attr);
225 EXPORT_SYMBOL(cl_object_attr_get);
228 * Updates data-attributes of an object \a obj.
230 * Only attributes, mentioned in a validness bit-mask \a v are
231 * updated. Calls cl_object_operations::coo_upd_attr() on every layer, bottom
234 int cl_object_attr_update(const struct lu_env *env, struct cl_object *obj,
235 const struct cl_attr *attr, unsigned v)
237 struct lu_object_header *top;
240 assert_spin_locked(cl_object_attr_guard(obj));
243 top = obj->co_lu.lo_header;
245 list_for_each_entry_reverse(obj, &top->loh_layers, co_lu.lo_linkage) {
246 if (obj->co_ops->coo_attr_update != NULL) {
247 result = obj->co_ops->coo_attr_update(env, obj, attr,
258 EXPORT_SYMBOL(cl_object_attr_update);
261 * Notifies layers (bottom-to-top) that glimpse AST was received.
263 * Layers have to fill \a lvb fields with information that will be shipped
264 * back to glimpse issuer.
266 * \see cl_lock_operations::clo_glimpse()
268 int cl_object_glimpse(const struct lu_env *env, struct cl_object *obj,
271 struct lu_object_header *top;
275 top = obj->co_lu.lo_header;
277 list_for_each_entry_reverse(obj, &top->loh_layers, co_lu.lo_linkage) {
278 if (obj->co_ops->coo_glimpse != NULL) {
279 result = obj->co_ops->coo_glimpse(env, obj, lvb);
284 LU_OBJECT_HEADER(D_DLMTRACE, env, lu_object_top(top),
285 "size: %llu mtime: %llu atime: %llu "
286 "ctime: %llu blocks: %llu\n",
287 lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
288 lvb->lvb_ctime, lvb->lvb_blocks);
291 EXPORT_SYMBOL(cl_object_glimpse);
294 * Updates a configuration of an object \a obj.
296 int cl_conf_set(const struct lu_env *env, struct cl_object *obj,
297 const struct cl_object_conf *conf)
299 struct lu_object_header *top;
303 top = obj->co_lu.lo_header;
305 list_for_each_entry(obj, &top->loh_layers, co_lu.lo_linkage) {
306 if (obj->co_ops->coo_conf_set != NULL) {
307 result = obj->co_ops->coo_conf_set(env, obj, conf);
314 EXPORT_SYMBOL(cl_conf_set);
317 * Prunes caches of pages and locks for this object.
319 int cl_object_prune(const struct lu_env *env, struct cl_object *obj)
321 struct lu_object_header *top;
326 top = obj->co_lu.lo_header;
328 list_for_each_entry(o, &top->loh_layers, co_lu.lo_linkage) {
329 if (o->co_ops->coo_prune != NULL) {
330 result = o->co_ops->coo_prune(env, o);
338 EXPORT_SYMBOL(cl_object_prune);
341 * Get stripe information of this object.
343 int cl_object_getstripe(const struct lu_env *env, struct cl_object *obj,
344 struct lov_user_md __user *uarg, size_t size)
346 struct lu_object_header *top;
350 top = obj->co_lu.lo_header;
351 list_for_each_entry(obj, &top->loh_layers, co_lu.lo_linkage) {
352 if (obj->co_ops->coo_getstripe != NULL) {
353 result = obj->co_ops->coo_getstripe(env, obj, uarg,
361 EXPORT_SYMBOL(cl_object_getstripe);
364 * Get fiemap extents from file object.
366 * \param env [in] lustre environment
367 * \param obj [in] file object
368 * \param key [in] fiemap request argument
369 * \param fiemap [out] fiemap extents mapping retrived
370 * \param buflen [in] max buffer length of @fiemap
375 int cl_object_fiemap(const struct lu_env *env, struct cl_object *obj,
376 struct ll_fiemap_info_key *key,
377 struct fiemap *fiemap, size_t *buflen)
379 struct lu_object_header *top;
383 top = obj->co_lu.lo_header;
384 list_for_each_entry(obj, &top->loh_layers, co_lu.lo_linkage) {
385 if (obj->co_ops->coo_fiemap != NULL) {
386 result = obj->co_ops->coo_fiemap(env, obj, key, fiemap,
394 EXPORT_SYMBOL(cl_object_fiemap);
396 int cl_object_layout_get(const struct lu_env *env, struct cl_object *obj,
397 struct cl_layout *cl)
399 struct lu_object_header *top = obj->co_lu.lo_header;
402 list_for_each_entry(obj, &top->loh_layers, co_lu.lo_linkage) {
403 if (obj->co_ops->coo_layout_get != NULL)
404 return obj->co_ops->coo_layout_get(env, obj, cl);
409 EXPORT_SYMBOL(cl_object_layout_get);
411 loff_t cl_object_maxbytes(struct cl_object *obj)
413 struct lu_object_header *top = obj->co_lu.lo_header;
414 loff_t maxbytes = LLONG_MAX;
417 list_for_each_entry(obj, &top->loh_layers, co_lu.lo_linkage) {
418 if (obj->co_ops->coo_maxbytes != NULL)
419 maxbytes = min_t(loff_t, obj->co_ops->coo_maxbytes(obj),
425 EXPORT_SYMBOL(cl_object_maxbytes);
427 int cl_object_flush(const struct lu_env *env, struct cl_object *obj,
428 struct ldlm_lock *lock)
430 struct lu_object_header *top = obj->co_lu.lo_header;
434 list_for_each_entry(obj, &top->loh_layers, co_lu.lo_linkage) {
435 if (obj->co_ops->coo_object_flush) {
436 rc = obj->co_ops->coo_object_flush(env, obj, lock);
443 EXPORT_SYMBOL(cl_object_flush);
446 * Helper function removing all object locks, and marking object for
447 * deletion. All object pages must have been deleted at this point.
449 * This is called by cl_inode_fini() and lov_object_delete() to destroy top-
450 * and sub- objects respectively.
452 void cl_object_kill(const struct lu_env *env, struct cl_object *obj)
454 struct cl_object_header *hdr = cl_object_header(obj);
456 set_bit(LU_OBJECT_HEARD_BANSHEE, &hdr->coh_lu.loh_flags);
458 EXPORT_SYMBOL(cl_object_kill);
460 void cache_stats_init(struct cache_stats *cs, const char *name)
465 for (i = 0; i < CS_NR; i++)
466 atomic_set(&cs->cs_stats[i], 0);
469 static int cache_stats_print(const struct cache_stats *cs,
470 struct seq_file *m, int h)
475 * lookup hit total cached create
476 * env: ...... ...... ...... ...... ......
479 const char *names[CS_NR] = CS_NAMES;
481 seq_printf(m, "%6s", " ");
482 for (i = 0; i < CS_NR; i++)
483 seq_printf(m, "%8s", names[i]);
487 seq_printf(m, "%5.5s:", cs->cs_name);
488 for (i = 0; i < CS_NR; i++)
489 seq_printf(m, "%8u", atomic_read(&cs->cs_stats[i]));
493 static void cl_env_percpu_refill(void);
496 * Initialize client site.
498 * Perform common initialization (lu_site_init()), and initialize statistical
499 * counters. Also perform global initializations on the first call.
501 int cl_site_init(struct cl_site *s, struct cl_device *d)
506 result = lu_site_init(&s->cs_lu, &d->cd_lu_dev);
508 cache_stats_init(&s->cs_pages, "pages");
509 for (i = 0; i < ARRAY_SIZE(s->cs_pages_state); ++i)
510 atomic_set(&s->cs_pages_state[0], 0);
511 cl_env_percpu_refill();
515 EXPORT_SYMBOL(cl_site_init);
518 * Finalize client site. Dual to cl_site_init().
520 void cl_site_fini(struct cl_site *s)
522 lu_site_fini(&s->cs_lu);
524 EXPORT_SYMBOL(cl_site_fini);
526 static struct cache_stats cl_env_stats = {
528 .cs_stats = { ATOMIC_INIT(0), }
532 * Outputs client site statistical counters into a buffer. Suitable for
533 * ll_rd_*()-style functions.
535 int cl_site_stats_print(const struct cl_site *site, struct seq_file *m)
537 static const char *pstate[] = {
547 lookup hit total busy create
548 pages: ...... ...... ...... ...... ...... [...... ...... ...... ......]
549 locks: ...... ...... ...... ...... ...... [...... ...... ...... ...... ......]
550 env: ...... ...... ...... ...... ......
552 lu_site_stats_seq_print(&site->cs_lu, m);
553 cache_stats_print(&site->cs_pages, m, 1);
555 for (i = 0; i < ARRAY_SIZE(site->cs_pages_state); ++i)
556 seq_printf(m, "%s: %u ", pstate[i],
557 atomic_read(&site->cs_pages_state[i]));
558 seq_printf(m, "]\n");
559 cache_stats_print(&cl_env_stats, m, 0);
563 EXPORT_SYMBOL(cl_site_stats_print);
565 /*****************************************************************************
567 * lu_env handling on client.
572 * The most efficient way is to store cl_env pointer in task specific
573 * structures. On Linux, it isn't easy to use task_struct->journal_info
574 * because Lustre code may call into other fs during memory reclaim, which
575 * has certain assumptions about journal_info. There are not currently any
576 * fields in task_struct that can be used for this purpose.
577 * \note As long as we use task_struct to store cl_env, we assume that once
578 * called into Lustre, we'll never call into the other part of the kernel
579 * which will use those fields in task_struct without explicitly exiting
582 * Since there's no space in task_struct is available, hash will be used.
586 static unsigned cl_envs_cached_max = 32; /* XXX: prototype: arbitrary limit
588 static struct cl_env_cache {
591 struct list_head cec_envs;
597 struct lu_context ce_ses;
600 * Linkage into global list of all client environments. Used for
601 * garbage collection.
603 struct list_head ce_linkage;
609 * Debugging field: address of the caller who made original
615 static void cl_env_inc(enum cache_stats_item item)
617 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
618 atomic_inc(&cl_env_stats.cs_stats[item]);
622 static void cl_env_dec(enum cache_stats_item item)
624 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
625 LASSERT(atomic_read(&cl_env_stats.cs_stats[item]) > 0);
626 atomic_dec(&cl_env_stats.cs_stats[item]);
630 static void cl_env_init0(struct cl_env *cle, void *debug)
632 LASSERT(cle->ce_ref == 0);
633 LASSERT(cle->ce_magic == &cl_env_init0);
634 LASSERT(cle->ce_debug == NULL);
637 cle->ce_debug = debug;
641 static struct lu_env *cl_env_new(__u32 ctx_tags, __u32 ses_tags, void *debug)
646 OBD_SLAB_ALLOC_PTR_GFP(cle, cl_env_kmem, GFP_NOFS);
650 INIT_LIST_HEAD(&cle->ce_linkage);
651 cle->ce_magic = &cl_env_init0;
653 rc = lu_env_init(env, LCT_CL_THREAD|ctx_tags);
655 rc = lu_context_init(&cle->ce_ses,
656 LCT_SESSION | ses_tags);
658 lu_context_enter(&cle->ce_ses);
659 env->le_ses = &cle->ce_ses;
660 cl_env_init0(cle, debug);
665 OBD_SLAB_FREE_PTR(cle, cl_env_kmem);
668 cl_env_inc(CS_create);
669 cl_env_inc(CS_total);
672 env = ERR_PTR(-ENOMEM);
676 static void cl_env_fini(struct cl_env *cle)
678 cl_env_dec(CS_total);
679 lu_context_fini(&cle->ce_lu.le_ctx);
680 lu_context_fini(&cle->ce_ses);
681 OBD_SLAB_FREE_PTR(cle, cl_env_kmem);
684 static struct lu_env *cl_env_obtain(void *debug)
692 read_lock(&cl_envs[cpu].cec_guard);
693 LASSERT(equi(cl_envs[cpu].cec_count == 0,
694 list_empty(&cl_envs[cpu].cec_envs)));
695 if (cl_envs[cpu].cec_count > 0) {
698 cle = container_of(cl_envs[cpu].cec_envs.next, struct cl_env,
700 list_del_init(&cle->ce_linkage);
701 cl_envs[cpu].cec_count--;
702 read_unlock(&cl_envs[cpu].cec_guard);
706 rc = lu_env_refill(env);
708 cl_env_init0(cle, debug);
709 lu_context_enter(&env->le_ctx);
710 lu_context_enter(&cle->ce_ses);
716 read_unlock(&cl_envs[cpu].cec_guard);
718 env = cl_env_new(lu_context_tags_default,
719 lu_session_tags_default, debug);
724 static inline struct cl_env *cl_env_container(struct lu_env *env)
726 return container_of(env, struct cl_env, ce_lu);
730 * Returns lu_env: if there already is an environment associated with the
731 * current thread, it is returned, otherwise, new environment is allocated.
733 * Allocations are amortized through the global cache of environments.
735 * \param refcheck pointer to a counter used to detect environment leaks. In
736 * the usual case cl_env_get() and cl_env_put() are called in the same lexical
737 * scope and pointer to the same integer is passed as \a refcheck. This is
738 * used to detect missed cl_env_put().
742 struct lu_env *cl_env_get(__u16 *refcheck)
746 env = cl_env_obtain(__builtin_return_address(0));
750 cle = cl_env_container(env);
751 *refcheck = cle->ce_ref;
752 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
756 EXPORT_SYMBOL(cl_env_get);
759 * Forces an allocation of a fresh environment with given tags.
763 struct lu_env *cl_env_alloc(__u16 *refcheck, __u32 tags)
767 env = cl_env_new(tags, tags, __builtin_return_address(0));
771 cle = cl_env_container(env);
772 *refcheck = cle->ce_ref;
773 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
777 EXPORT_SYMBOL(cl_env_alloc);
779 static void cl_env_exit(struct cl_env *cle)
781 lu_context_exit(&cle->ce_lu.le_ctx);
782 lu_context_exit(&cle->ce_ses);
786 * Finalizes and frees a given number of cached environments. This is done to
787 * (1) free some memory (not currently hooked into VM), or (2) release
788 * references to modules.
790 unsigned cl_env_cache_purge(unsigned nr)
796 for_each_possible_cpu(i) {
797 write_lock(&cl_envs[i].cec_guard);
798 for (; !list_empty(&cl_envs[i].cec_envs) && nr > 0; --nr) {
799 cle = container_of(cl_envs[i].cec_envs.next,
800 struct cl_env, ce_linkage);
801 list_del_init(&cle->ce_linkage);
802 LASSERT(cl_envs[i].cec_count > 0);
803 cl_envs[i].cec_count--;
804 write_unlock(&cl_envs[i].cec_guard);
807 write_lock(&cl_envs[i].cec_guard);
809 LASSERT(equi(cl_envs[i].cec_count == 0,
810 list_empty(&cl_envs[i].cec_envs)));
811 write_unlock(&cl_envs[i].cec_guard);
815 EXPORT_SYMBOL(cl_env_cache_purge);
818 * Release an environment.
820 * Decrement \a env reference counter. When counter drops to 0, nothing in
821 * this thread is using environment and it is returned to the allocation
822 * cache, or freed straight away, if cache is large enough.
824 void cl_env_put(struct lu_env *env, __u16 *refcheck)
828 cle = cl_env_container(env);
830 LASSERT(cle->ce_ref > 0);
831 LASSERT(ergo(refcheck != NULL, cle->ce_ref == *refcheck));
833 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
834 if (--cle->ce_ref == 0) {
838 cle->ce_debug = NULL;
841 * Don't bother to take a lock here.
843 * Return environment to the cache only when it was allocated
844 * with the standard tags.
846 if (cl_envs[cpu].cec_count < cl_envs_cached_max &&
847 (env->le_ctx.lc_tags & ~LCT_HAS_EXIT) == lu_context_tags_default &&
848 (env->le_ses->lc_tags & ~LCT_HAS_EXIT) == lu_session_tags_default) {
849 read_lock(&cl_envs[cpu].cec_guard);
850 list_add(&cle->ce_linkage, &cl_envs[cpu].cec_envs);
851 cl_envs[cpu].cec_count++;
852 read_unlock(&cl_envs[cpu].cec_guard);
858 EXPORT_SYMBOL(cl_env_put);
861 * Converts struct cl_attr to struct ost_lvb.
865 void cl_attr2lvb(struct ost_lvb *lvb, const struct cl_attr *attr)
867 lvb->lvb_size = attr->cat_size;
868 lvb->lvb_mtime = attr->cat_mtime;
869 lvb->lvb_atime = attr->cat_atime;
870 lvb->lvb_ctime = attr->cat_ctime;
871 lvb->lvb_blocks = attr->cat_blocks;
875 * Converts struct ost_lvb to struct cl_attr.
879 void cl_lvb2attr(struct cl_attr *attr, const struct ost_lvb *lvb)
881 attr->cat_size = lvb->lvb_size;
882 attr->cat_mtime = lvb->lvb_mtime;
883 attr->cat_atime = lvb->lvb_atime;
884 attr->cat_ctime = lvb->lvb_ctime;
885 attr->cat_blocks = lvb->lvb_blocks;
887 EXPORT_SYMBOL(cl_lvb2attr);
889 static struct cl_env cl_env_percpu[NR_CPUS];
891 static int cl_env_percpu_init(void)
894 int tags = LCT_REMEMBER | LCT_NOREF;
898 for_each_possible_cpu(i) {
901 rwlock_init(&cl_envs[i].cec_guard);
902 INIT_LIST_HEAD(&cl_envs[i].cec_envs);
903 cl_envs[i].cec_count = 0;
905 cle = &cl_env_percpu[i];
908 INIT_LIST_HEAD(&cle->ce_linkage);
909 cle->ce_magic = &cl_env_init0;
910 rc = lu_env_init(env, LCT_CL_THREAD | tags);
912 rc = lu_context_init(&cle->ce_ses, LCT_SESSION | tags);
914 lu_context_enter(&cle->ce_ses);
915 env->le_ses = &cle->ce_ses;
924 /* Indices 0 to i (excluding i) were correctly initialized,
925 * thus we must uninitialize up to i, the rest are undefined. */
926 for (j = 0; j < i; j++) {
927 cle = &cl_env_percpu[j];
928 lu_context_exit(&cle->ce_ses);
929 lu_context_fini(&cle->ce_ses);
930 lu_env_fini(&cle->ce_lu);
937 static void cl_env_percpu_fini(void)
941 for_each_possible_cpu(i) {
942 struct cl_env *cle = &cl_env_percpu[i];
944 lu_context_exit(&cle->ce_ses);
945 lu_context_fini(&cle->ce_ses);
946 lu_env_fini(&cle->ce_lu);
950 static void cl_env_percpu_refill(void)
954 for_each_possible_cpu(i)
955 lu_env_refill(&cl_env_percpu[i].ce_lu);
958 void cl_env_percpu_put(struct lu_env *env)
963 cpu = smp_processor_id();
964 cle = cl_env_container(env);
965 LASSERT(cle == &cl_env_percpu[cpu]);
968 LASSERT(cle->ce_ref == 0);
971 cle->ce_debug = NULL;
975 EXPORT_SYMBOL(cl_env_percpu_put);
977 struct lu_env *cl_env_percpu_get(void)
981 cle = &cl_env_percpu[get_cpu()];
982 cl_env_init0(cle, __builtin_return_address(0));
986 EXPORT_SYMBOL(cl_env_percpu_get);
988 /*****************************************************************************
990 * Temporary prototype thing: mirror obd-devices into cl devices.
994 struct cl_device *cl_type_setup(const struct lu_env *env, struct lu_site *site,
995 struct lu_device_type *ldt,
996 struct lu_device *next)
998 const char *typename;
1001 LASSERT(ldt != NULL);
1003 typename = ldt->ldt_name;
1004 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, NULL);
1010 rc = ldt->ldt_ops->ldto_device_init(env, d, typename, next);
1013 lu_ref_add(&d->ld_reference,
1014 "lu-stack", &lu_site_init);
1016 ldt->ldt_ops->ldto_device_free(env, d);
1017 CERROR("can't init device '%s', %d\n", typename, rc);
1021 CERROR("Cannot allocate device: '%s'\n", typename);
1022 return lu2cl_dev(d);
1024 EXPORT_SYMBOL(cl_type_setup);
1027 * Finalize device stack by calling lu_stack_fini().
1029 void cl_stack_fini(const struct lu_env *env, struct cl_device *cl)
1031 lu_stack_fini(env, cl2lu_dev(cl));
1033 EXPORT_SYMBOL(cl_stack_fini);
1035 static struct lu_context_key cl_key;
1037 struct cl_thread_info *cl_env_info(const struct lu_env *env)
1039 return lu_context_key_get(&env->le_ctx, &cl_key);
1042 /* defines cl_key_{init,fini}() */
1043 LU_KEY_INIT_FINI(cl, struct cl_thread_info);
1045 static struct lu_context_key cl_key = {
1046 .lct_tags = LCT_CL_THREAD,
1047 .lct_init = cl_key_init,
1048 .lct_fini = cl_key_fini,
1051 static struct lu_kmem_descr cl_object_caches[] = {
1053 .ckd_cache = &cl_env_kmem,
1054 .ckd_name = "cl_env_kmem",
1055 .ckd_size = sizeof(struct cl_env)
1058 .ckd_cache = &cl_dio_aio_kmem,
1059 .ckd_name = "cl_dio_aio_kmem",
1060 .ckd_size = sizeof(struct cl_dio_aio)
1068 * Global initialization of cl-data. Create kmem caches, register
1069 * lu_context_key's, etc.
1071 * \see cl_global_fini()
1073 int cl_global_init(void)
1077 OBD_ALLOC(cl_envs, sizeof(*cl_envs) * num_possible_cpus());
1078 if (cl_envs == NULL)
1079 GOTO(out, result = -ENOMEM);
1081 result = lu_kmem_init(cl_object_caches);
1083 GOTO(out_envs, result);
1085 LU_CONTEXT_KEY_INIT(&cl_key);
1086 result = lu_context_key_register(&cl_key);
1088 GOTO(out_kmem, result);
1090 result = cl_env_percpu_init();
1091 if (result) /* no cl_env_percpu_fini on error */
1092 GOTO(out_keys, result);
1097 lu_context_key_degister(&cl_key);
1099 lu_kmem_fini(cl_object_caches);
1101 OBD_FREE(cl_envs, sizeof(*cl_envs) * num_possible_cpus());
1107 * Finalization of global cl-data. Dual to cl_global_init().
1109 void cl_global_fini(void)
1113 for (i = 0; i < ARRAY_SIZE(cl_page_kmem_array); i++) {
1114 if (cl_page_kmem_array[i]) {
1115 kmem_cache_destroy(cl_page_kmem_array[i]);
1116 cl_page_kmem_array[i] = NULL;
1119 cl_env_percpu_fini();
1120 lu_context_key_degister(&cl_key);
1121 lu_kmem_fini(cl_object_caches);
1122 OBD_FREE(cl_envs, sizeof(*cl_envs) * num_possible_cpus());