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/
31 * Client Lustre Object.
33 * Author: Nikita Danilov <nikita.danilov@sun.com>
34 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
46 #define DEBUG_SUBSYSTEM S_CLASS
48 #include <linux/list.h>
49 #include <libcfs/libcfs.h>
50 #include <obd_class.h>
51 #include <obd_support.h>
52 #include <lustre_fid.h>
53 #include <libcfs/libcfs_hash.h> /* for cfs_hash stuff */
54 #include <cl_object.h>
55 #include <lu_object.h>
56 #include "cl_internal.h"
58 static struct kmem_cache *cl_env_kmem;
59 struct kmem_cache *cl_dio_aio_kmem;
60 struct kmem_cache *cl_sub_dio_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 *top,
203 struct cl_attr *attr)
205 struct cl_object *obj;
208 assert_spin_locked(cl_object_attr_guard(top));
211 cl_object_for_each(obj, top) {
212 if (obj->co_ops->coo_attr_get != NULL) {
213 result = obj->co_ops->coo_attr_get(env, obj, attr);
223 EXPORT_SYMBOL(cl_object_attr_get);
226 * Updates data-attributes of an object \a obj.
228 * Only attributes, mentioned in a validness bit-mask \a v are
229 * updated. Calls cl_object_operations::coo_upd_attr() on every layer, bottom
232 int cl_object_attr_update(const struct lu_env *env, struct cl_object *top,
233 const struct cl_attr *attr, unsigned v)
235 struct cl_object *obj;
238 assert_spin_locked(cl_object_attr_guard(top));
241 cl_object_for_each_reverse(obj, top) {
242 if (obj->co_ops->coo_attr_update != NULL) {
243 result = obj->co_ops->coo_attr_update(env, obj, attr,
254 EXPORT_SYMBOL(cl_object_attr_update);
257 * Notifies layers (bottom-to-top) that glimpse AST was received.
259 * Layers have to fill \a lvb fields with information that will be shipped
260 * back to glimpse issuer.
262 * \see cl_lock_operations::clo_glimpse()
264 int cl_object_glimpse(const struct lu_env *env, struct cl_object *top,
267 struct cl_object *obj;
271 cl_object_for_each_reverse(obj, top) {
272 if (obj->co_ops->coo_glimpse != NULL) {
273 result = obj->co_ops->coo_glimpse(env, obj, lvb);
278 LU_OBJECT_HEADER(D_DLMTRACE, env, lu_object_top(top->co_lu.lo_header),
279 "size: %llu mtime: %llu atime: %llu "
280 "ctime: %llu blocks: %llu\n",
281 lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
282 lvb->lvb_ctime, lvb->lvb_blocks);
285 EXPORT_SYMBOL(cl_object_glimpse);
288 * Updates a configuration of an object \a obj.
290 int cl_conf_set(const struct lu_env *env, struct cl_object *top,
291 const struct cl_object_conf *conf)
293 struct cl_object *obj;
297 cl_object_for_each(obj, top) {
298 if (obj->co_ops->coo_conf_set != NULL) {
299 result = obj->co_ops->coo_conf_set(env, obj, conf);
306 EXPORT_SYMBOL(cl_conf_set);
309 * Prunes caches of pages and locks for this object.
311 int cl_object_prune(const struct lu_env *env, struct cl_object *top)
313 struct cl_object *obj;
317 cl_object_for_each(obj, top) {
318 if (obj->co_ops->coo_prune != NULL) {
319 result = obj->co_ops->coo_prune(env, obj);
327 EXPORT_SYMBOL(cl_object_prune);
330 * Get stripe information of this object.
332 int cl_object_getstripe(const struct lu_env *env, struct cl_object *top,
333 struct lov_user_md __user *uarg, size_t size)
335 struct cl_object *obj;
339 cl_object_for_each(obj, top) {
340 if (obj->co_ops->coo_getstripe) {
341 result = obj->co_ops->coo_getstripe(env, obj, uarg,
349 EXPORT_SYMBOL(cl_object_getstripe);
352 * Get fiemap extents from file object.
354 * \param env [in] lustre environment
355 * \param obj [in] file object
356 * \param key [in] fiemap request argument
357 * \param fiemap [out] fiemap extents mapping retrived
358 * \param buflen [in] max buffer length of @fiemap
363 int cl_object_fiemap(const struct lu_env *env, struct cl_object *top,
364 struct ll_fiemap_info_key *key,
365 struct fiemap *fiemap, size_t *buflen)
367 struct cl_object *obj;
371 cl_object_for_each(obj, top) {
372 if (obj->co_ops->coo_fiemap) {
373 result = obj->co_ops->coo_fiemap(env, obj, key, fiemap,
381 EXPORT_SYMBOL(cl_object_fiemap);
383 int cl_object_layout_get(const struct lu_env *env, struct cl_object *top,
384 struct cl_layout *cl)
386 struct cl_object *obj;
389 cl_object_for_each(obj, top) {
390 if (obj->co_ops->coo_layout_get)
391 return obj->co_ops->coo_layout_get(env, obj, cl);
396 EXPORT_SYMBOL(cl_object_layout_get);
398 loff_t cl_object_maxbytes(struct cl_object *top)
400 struct cl_object *obj;
401 loff_t maxbytes = LLONG_MAX;
404 cl_object_for_each(obj, top) {
405 if (obj->co_ops->coo_maxbytes)
406 maxbytes = min_t(loff_t, obj->co_ops->coo_maxbytes(obj),
412 EXPORT_SYMBOL(cl_object_maxbytes);
414 int cl_object_flush(const struct lu_env *env, struct cl_object *top,
415 struct ldlm_lock *lock)
417 struct cl_object *obj;
421 cl_object_for_each(obj, top) {
422 if (obj->co_ops->coo_object_flush) {
423 rc = obj->co_ops->coo_object_flush(env, obj, lock);
430 EXPORT_SYMBOL(cl_object_flush);
433 * Helper function removing all object locks, and marking object for
434 * deletion. All object pages must have been deleted at this point.
436 * This is called by cl_inode_fini() and lov_object_delete() to destroy top-
437 * and sub- objects respectively.
439 void cl_object_kill(const struct lu_env *env, struct cl_object *obj)
441 struct cl_object_header *hdr = cl_object_header(obj);
443 set_bit(LU_OBJECT_HEARD_BANSHEE, &hdr->coh_lu.loh_flags);
445 EXPORT_SYMBOL(cl_object_kill);
447 void cache_stats_init(struct cache_stats *cs, const char *name)
452 for (i = 0; i < CS_NR; i++)
453 atomic_set(&cs->cs_stats[i], 0);
456 static int cache_stats_print(const struct cache_stats *cs,
457 struct seq_file *m, int h)
462 * lookup hit total cached create
463 * env: ...... ...... ...... ...... ......
466 const char *names[CS_NR] = CS_NAMES;
468 seq_printf(m, "%6s", " ");
469 for (i = 0; i < CS_NR; i++)
470 seq_printf(m, "%8s", names[i]);
474 seq_printf(m, "%5.5s:", cs->cs_name);
475 for (i = 0; i < CS_NR; i++)
476 seq_printf(m, "%8u", atomic_read(&cs->cs_stats[i]));
480 static void cl_env_percpu_refill(void);
483 * Initialize client site.
485 * Perform common initialization (lu_site_init()), and initialize statistical
486 * counters. Also perform global initializations on the first call.
488 int cl_site_init(struct cl_site *s, struct cl_device *d)
493 result = lu_site_init(&s->cs_lu, &d->cd_lu_dev);
495 cache_stats_init(&s->cs_pages, "pages");
496 for (i = 0; i < ARRAY_SIZE(s->cs_pages_state); ++i)
497 atomic_set(&s->cs_pages_state[0], 0);
498 cl_env_percpu_refill();
502 EXPORT_SYMBOL(cl_site_init);
505 * Finalize client site. Dual to cl_site_init().
507 void cl_site_fini(struct cl_site *s)
509 lu_site_fini(&s->cs_lu);
511 EXPORT_SYMBOL(cl_site_fini);
513 static struct cache_stats cl_env_stats = {
515 .cs_stats = { ATOMIC_INIT(0), }
519 * Outputs client site statistical counters into a buffer. Suitable for
520 * ll_rd_*()-style functions.
522 int cl_site_stats_print(const struct cl_site *site, struct seq_file *m)
524 static const char *const pstate[] = {
534 lookup hit total busy create
535 pages: ...... ...... ...... ...... ...... [...... ...... ...... ......]
536 locks: ...... ...... ...... ...... ...... [...... ...... ...... ...... ......]
537 env: ...... ...... ...... ...... ......
539 lu_site_stats_seq_print(&site->cs_lu, m);
540 cache_stats_print(&site->cs_pages, m, 1);
542 for (i = 0; i < ARRAY_SIZE(site->cs_pages_state); ++i)
543 seq_printf(m, "%s: %u ", pstate[i],
544 atomic_read(&site->cs_pages_state[i]));
545 seq_printf(m, "]\n");
546 cache_stats_print(&cl_env_stats, m, 0);
550 EXPORT_SYMBOL(cl_site_stats_print);
552 /*****************************************************************************
554 * lu_env handling on client.
559 * The most efficient way is to store cl_env pointer in task specific
560 * structures. On Linux, it isn't easy to use task_struct->journal_info
561 * because Lustre code may call into other fs during memory reclaim, which
562 * has certain assumptions about journal_info. There are not currently any
563 * fields in task_struct that can be used for this purpose.
564 * \note As long as we use task_struct to store cl_env, we assume that once
565 * called into Lustre, we'll never call into the other part of the kernel
566 * which will use those fields in task_struct without explicitly exiting
569 * Since there's no space in task_struct is available, hash will be used.
573 static unsigned cl_envs_cached_max = 32; /* XXX: prototype: arbitrary limit
575 static struct cl_env_cache {
578 struct list_head cec_envs;
584 struct lu_context ce_ses;
587 * Linkage into global list of all client environments. Used for
588 * garbage collection.
590 struct list_head ce_linkage;
596 * Debugging field: address of the caller who made original
602 static void cl_env_inc(enum cache_stats_item item)
604 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
605 atomic_inc(&cl_env_stats.cs_stats[item]);
609 static void cl_env_dec(enum cache_stats_item item)
611 #ifdef CONFIG_DEBUG_PAGESTATE_TRACKING
612 LASSERT(atomic_read(&cl_env_stats.cs_stats[item]) > 0);
613 atomic_dec(&cl_env_stats.cs_stats[item]);
617 static void cl_env_init0(struct cl_env *cle, void *debug)
619 LASSERT(cle->ce_ref == 0);
620 LASSERT(cle->ce_magic == &cl_env_init0);
621 LASSERT(cle->ce_debug == NULL);
624 cle->ce_debug = debug;
628 static struct lu_env *cl_env_new(__u32 ctx_tags, __u32 ses_tags, void *debug)
633 OBD_SLAB_ALLOC_PTR_GFP(cle, cl_env_kmem, GFP_NOFS);
637 INIT_LIST_HEAD(&cle->ce_linkage);
638 cle->ce_magic = &cl_env_init0;
640 rc = lu_env_init(env, LCT_CL_THREAD|ctx_tags);
642 rc = lu_context_init(&cle->ce_ses,
643 LCT_SESSION | ses_tags);
645 lu_context_enter(&cle->ce_ses);
646 env->le_ses = &cle->ce_ses;
647 cl_env_init0(cle, debug);
652 OBD_SLAB_FREE_PTR(cle, cl_env_kmem);
655 cl_env_inc(CS_create);
656 cl_env_inc(CS_total);
659 env = ERR_PTR(-ENOMEM);
663 static void cl_env_fini(struct cl_env *cle)
665 cl_env_dec(CS_total);
666 lu_context_fini(&cle->ce_lu.le_ctx);
667 lu_context_fini(&cle->ce_ses);
668 OBD_SLAB_FREE_PTR(cle, cl_env_kmem);
671 static struct lu_env *cl_env_obtain(void *debug)
679 read_lock(&cl_envs[cpu].cec_guard);
680 LASSERT(equi(cl_envs[cpu].cec_count == 0,
681 list_empty(&cl_envs[cpu].cec_envs)));
682 if (cl_envs[cpu].cec_count > 0) {
685 cle = container_of(cl_envs[cpu].cec_envs.next, struct cl_env,
687 list_del_init(&cle->ce_linkage);
688 cl_envs[cpu].cec_count--;
689 read_unlock(&cl_envs[cpu].cec_guard);
693 rc = lu_env_refill(env);
695 cl_env_init0(cle, debug);
696 lu_context_enter(&env->le_ctx);
697 lu_context_enter(&cle->ce_ses);
703 read_unlock(&cl_envs[cpu].cec_guard);
705 env = cl_env_new(lu_context_tags_default,
706 lu_session_tags_default, debug);
711 static inline struct cl_env *cl_env_container(struct lu_env *env)
713 return container_of(env, struct cl_env, ce_lu);
717 * Returns lu_env: if there already is an environment associated with the
718 * current thread, it is returned, otherwise, new environment is allocated.
720 * Allocations are amortized through the global cache of environments.
722 * \param refcheck pointer to a counter used to detect environment leaks. In
723 * the usual case cl_env_get() and cl_env_put() are called in the same lexical
724 * scope and pointer to the same integer is passed as \a refcheck. This is
725 * used to detect missed cl_env_put().
729 struct lu_env *cl_env_get(__u16 *refcheck)
733 env = cl_env_obtain(__builtin_return_address(0));
737 cle = cl_env_container(env);
738 *refcheck = cle->ce_ref;
739 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
743 EXPORT_SYMBOL(cl_env_get);
746 * Forces an allocation of a fresh environment with given tags.
750 struct lu_env *cl_env_alloc(__u16 *refcheck, __u32 tags)
754 env = cl_env_new(tags, tags, __builtin_return_address(0));
758 cle = cl_env_container(env);
759 *refcheck = cle->ce_ref;
760 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
764 EXPORT_SYMBOL(cl_env_alloc);
766 static void cl_env_exit(struct cl_env *cle)
768 lu_context_exit(&cle->ce_lu.le_ctx);
769 lu_context_exit(&cle->ce_ses);
773 * Finalizes and frees a given number of cached environments. This is done to
774 * (1) free some memory (not currently hooked into VM), or (2) release
775 * references to modules.
777 unsigned cl_env_cache_purge(unsigned nr)
783 for_each_possible_cpu(i) {
784 write_lock(&cl_envs[i].cec_guard);
785 for (; !list_empty(&cl_envs[i].cec_envs) && nr > 0; --nr) {
786 cle = container_of(cl_envs[i].cec_envs.next,
787 struct cl_env, ce_linkage);
788 list_del_init(&cle->ce_linkage);
789 LASSERT(cl_envs[i].cec_count > 0);
790 cl_envs[i].cec_count--;
791 write_unlock(&cl_envs[i].cec_guard);
794 write_lock(&cl_envs[i].cec_guard);
796 LASSERT(equi(cl_envs[i].cec_count == 0,
797 list_empty(&cl_envs[i].cec_envs)));
798 write_unlock(&cl_envs[i].cec_guard);
802 EXPORT_SYMBOL(cl_env_cache_purge);
805 * Release an environment.
807 * Decrement \a env reference counter. When counter drops to 0, nothing in
808 * this thread is using environment and it is returned to the allocation
809 * cache, or freed straight away, if cache is large enough.
811 void cl_env_put(struct lu_env *env, __u16 *refcheck)
815 cle = cl_env_container(env);
817 LASSERT(cle->ce_ref > 0);
818 LASSERT(ergo(refcheck != NULL, cle->ce_ref == *refcheck));
820 CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
821 if (--cle->ce_ref == 0) {
825 cle->ce_debug = NULL;
828 * Don't bother to take a lock here.
830 * Return environment to the cache only when it was allocated
831 * with the standard tags.
833 if (cl_envs[cpu].cec_count < cl_envs_cached_max &&
834 (env->le_ctx.lc_tags & ~LCT_HAS_EXIT) == lu_context_tags_default &&
835 (env->le_ses->lc_tags & ~LCT_HAS_EXIT) == lu_session_tags_default) {
836 read_lock(&cl_envs[cpu].cec_guard);
837 list_add(&cle->ce_linkage, &cl_envs[cpu].cec_envs);
838 cl_envs[cpu].cec_count++;
839 read_unlock(&cl_envs[cpu].cec_guard);
845 EXPORT_SYMBOL(cl_env_put);
848 * Converts struct cl_attr to struct ost_lvb.
852 void cl_attr2lvb(struct ost_lvb *lvb, const struct cl_attr *attr)
854 lvb->lvb_size = attr->cat_size;
855 lvb->lvb_mtime = attr->cat_mtime;
856 lvb->lvb_atime = attr->cat_atime;
857 lvb->lvb_ctime = attr->cat_ctime;
858 lvb->lvb_blocks = attr->cat_blocks;
862 * Converts struct ost_lvb to struct cl_attr.
866 void cl_lvb2attr(struct cl_attr *attr, const struct ost_lvb *lvb)
868 attr->cat_size = lvb->lvb_size;
869 attr->cat_mtime = lvb->lvb_mtime;
870 attr->cat_atime = lvb->lvb_atime;
871 attr->cat_ctime = lvb->lvb_ctime;
872 attr->cat_blocks = lvb->lvb_blocks;
874 EXPORT_SYMBOL(cl_lvb2attr);
876 static struct cl_env cl_env_percpu[NR_CPUS];
877 static DEFINE_MUTEX(cl_env_percpu_mutex);
879 static int cl_env_percpu_init(void)
882 int tags = LCT_REMEMBER | LCT_NOREF;
886 for_each_possible_cpu(i) {
889 rwlock_init(&cl_envs[i].cec_guard);
890 INIT_LIST_HEAD(&cl_envs[i].cec_envs);
891 cl_envs[i].cec_count = 0;
893 cle = &cl_env_percpu[i];
896 INIT_LIST_HEAD(&cle->ce_linkage);
897 cle->ce_magic = &cl_env_init0;
898 rc = lu_env_init(env, LCT_CL_THREAD | tags);
900 rc = lu_context_init(&cle->ce_ses, LCT_SESSION | tags);
902 lu_context_enter(&cle->ce_ses);
903 env->le_ses = &cle->ce_ses;
912 /* Indices 0 to i (excluding i) were correctly initialized,
913 * thus we must uninitialize up to i, the rest are undefined. */
914 for (j = 0; j < i; j++) {
915 cle = &cl_env_percpu[j];
916 lu_context_exit(&cle->ce_ses);
917 lu_context_fini(&cle->ce_ses);
918 lu_env_fini(&cle->ce_lu);
925 static void cl_env_percpu_fini(void)
929 for_each_possible_cpu(i) {
930 struct cl_env *cle = &cl_env_percpu[i];
932 lu_context_exit(&cle->ce_ses);
933 lu_context_fini(&cle->ce_ses);
934 lu_env_fini(&cle->ce_lu);
938 static void cl_env_percpu_refill(void)
942 mutex_lock(&cl_env_percpu_mutex);
943 for_each_possible_cpu(i)
944 lu_env_refill(&cl_env_percpu[i].ce_lu);
945 mutex_unlock(&cl_env_percpu_mutex);
948 void cl_env_percpu_put(struct lu_env *env)
953 cpu = smp_processor_id();
954 cle = cl_env_container(env);
955 LASSERT(cle == &cl_env_percpu[cpu]);
958 LASSERT(cle->ce_ref == 0);
961 cle->ce_debug = NULL;
965 EXPORT_SYMBOL(cl_env_percpu_put);
967 struct lu_env *cl_env_percpu_get(void)
971 cle = &cl_env_percpu[get_cpu()];
972 cl_env_init0(cle, __builtin_return_address(0));
976 EXPORT_SYMBOL(cl_env_percpu_get);
978 /*****************************************************************************
980 * Temporary prototype thing: mirror obd-devices into cl devices.
984 struct cl_device *cl_type_setup(const struct lu_env *env, struct lu_site *site,
985 struct lu_device_type *ldt,
986 struct lu_device *next)
988 const char *typename;
991 LASSERT(ldt != NULL);
993 typename = ldt->ldt_name;
994 d = ldt->ldt_ops->ldto_device_alloc(env, ldt, NULL);
1000 rc = ldt->ldt_ops->ldto_device_init(env, d, typename, next);
1003 lu_ref_add(&d->ld_reference,
1004 "lu-stack", &lu_site_init);
1006 ldt->ldt_ops->ldto_device_free(env, d);
1007 CERROR("can't init device '%s', %d\n", typename, rc);
1011 CERROR("Cannot allocate device: '%s'\n", typename);
1012 return lu2cl_dev(d);
1014 EXPORT_SYMBOL(cl_type_setup);
1017 * Finalize device stack by calling lu_stack_fini().
1019 void cl_stack_fini(const struct lu_env *env, struct cl_device *cl)
1021 lu_stack_fini(env, cl2lu_dev(cl));
1023 EXPORT_SYMBOL(cl_stack_fini);
1025 static struct lu_context_key cl_key;
1027 struct cl_thread_info *cl_env_info(const struct lu_env *env)
1029 return lu_context_key_get(&env->le_ctx, &cl_key);
1032 /* defines cl_key_{init,fini}() */
1033 LU_KEY_INIT_FINI(cl, struct cl_thread_info);
1035 static struct lu_context_key cl_key = {
1036 .lct_tags = LCT_CL_THREAD,
1037 .lct_init = cl_key_init,
1038 .lct_fini = cl_key_fini,
1041 static struct lu_kmem_descr cl_object_caches[] = {
1043 .ckd_cache = &cl_env_kmem,
1044 .ckd_name = "cl_env_kmem",
1045 .ckd_size = sizeof(struct cl_env)
1048 .ckd_cache = &cl_dio_aio_kmem,
1049 .ckd_name = "cl_dio_aio_kmem",
1050 .ckd_size = sizeof(struct cl_dio_aio)
1053 .ckd_cache = &cl_sub_dio_kmem,
1054 .ckd_name = "cl_sub_dio_kmem",
1055 .ckd_size = sizeof(struct cl_sub_dio)
1063 * Global initialization of cl-data. Create kmem caches, register
1064 * lu_context_key's, etc.
1066 * \see cl_global_fini()
1068 int cl_global_init(void)
1072 OBD_ALLOC_PTR_ARRAY(cl_envs, num_possible_cpus());
1073 if (cl_envs == NULL)
1074 GOTO(out, result = -ENOMEM);
1076 result = lu_kmem_init(cl_object_caches);
1078 GOTO(out_envs, result);
1080 LU_CONTEXT_KEY_INIT(&cl_key);
1081 result = lu_context_key_register(&cl_key);
1083 GOTO(out_kmem, result);
1085 result = cl_env_percpu_init();
1086 if (result) /* no cl_env_percpu_fini on error */
1087 GOTO(out_keys, result);
1092 lu_context_key_degister(&cl_key);
1094 lu_kmem_fini(cl_object_caches);
1096 OBD_FREE_PTR_ARRAY(cl_envs, num_possible_cpus());
1102 * Finalization of global cl-data. Dual to cl_global_init().
1104 void cl_global_fini(void)
1108 for (i = 0; i < ARRAY_SIZE(cl_page_kmem_array); i++) {
1109 if (cl_page_kmem_array[i]) {
1110 kmem_cache_destroy(cl_page_kmem_array[i]);
1111 cl_page_kmem_array[i] = NULL;
1114 cl_env_percpu_fini();
1115 lu_context_key_degister(&cl_key);
1116 lu_kmem_fini(cl_object_caches);
1117 OBD_FREE_PTR_ARRAY(cl_envs, num_possible_cpus());