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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/lu_object.c
39 * These are the only exported functions, they provide some generic
40 * infrastructure for managing object devices
42 * Author: Nikita Danilov <nikita.danilov@sun.com>
45 #define DEBUG_SUBSYSTEM S_CLASS
47 #include <libcfs/libcfs.h>
50 # include <linux/module.h>
54 #include <libcfs/libcfs_hash.h>
55 #include <obd_class.h>
56 #include <obd_support.h>
57 #include <lustre_disk.h>
58 #include <lustre_fid.h>
59 #include <lu_object.h>
60 #include <libcfs/list.h>
61 /* lu_time_global_{init,fini}() */
64 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
67 * Decrease reference counter on object. If last reference is freed, return
68 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
69 * case, free object immediately.
71 void lu_object_put(const struct lu_env *env, struct lu_object *o)
73 struct lu_site_bkt_data *bkt;
74 struct lu_object_header *top;
76 struct lu_object *orig;
80 site = o->lo_dev->ld_site;
83 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
84 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
86 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
87 if (lu_object_is_dying(top)) {
90 * somebody may be waiting for this, currently only
91 * used for cl_object, see cl_object_put_last().
93 cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
98 LASSERT(bkt->lsb_busy > 0);
101 * When last reference is released, iterate over object
102 * layers, and notify them that object is no longer busy.
104 cfs_list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
105 if (o->lo_ops->loo_object_release != NULL)
106 o->lo_ops->loo_object_release(env, o);
109 if (!lu_object_is_dying(top)) {
110 LASSERT(cfs_list_empty(&top->loh_lru));
111 cfs_list_add_tail(&top->loh_lru, &bkt->lsb_lru);
112 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
117 * If object is dying (will not be cached), removed it
118 * from hash table and LRU.
120 * This is done with hash table and LRU lists locked. As the only
121 * way to acquire first reference to previously unreferenced
122 * object is through hash-table lookup (lu_object_find()),
123 * or LRU scanning (lu_site_purge()), that are done under hash-table
124 * and LRU lock, no race with concurrent object lookup is possible
125 * and we can safely destroy object below.
127 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
128 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
130 * Object was already removed from hash and lru above, can
133 lu_object_free(env, orig);
135 EXPORT_SYMBOL(lu_object_put);
138 * Allocate new object.
140 * This follows object creation protocol, described in the comment within
141 * struct lu_device_operations definition.
143 static struct lu_object *lu_object_alloc(const struct lu_env *env,
144 struct lu_device *dev,
145 const struct lu_fid *f,
146 const struct lu_object_conf *conf)
148 struct lu_object *scan;
149 struct lu_object *top;
156 * Create top-level object slice. This will also create
159 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
161 RETURN(ERR_PTR(-ENOMEM));
163 * This is the only place where object fid is assigned. It's constant
166 LASSERT(fid_is_igif(f) || fid_ver(f) == 0);
167 top->lo_header->loh_fid = *f;
168 layers = &top->lo_header->loh_layers;
171 * Call ->loo_object_init() repeatedly, until no more new
172 * object slices are created.
175 cfs_list_for_each_entry(scan, layers, lo_linkage) {
176 if (scan->lo_flags & LU_OBJECT_ALLOCATED)
179 scan->lo_header = top->lo_header;
180 result = scan->lo_ops->loo_object_init(env, scan, conf);
182 lu_object_free(env, top);
183 RETURN(ERR_PTR(result));
185 scan->lo_flags |= LU_OBJECT_ALLOCATED;
189 cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
190 if (scan->lo_ops->loo_object_start != NULL) {
191 result = scan->lo_ops->loo_object_start(env, scan);
193 lu_object_free(env, top);
194 RETURN(ERR_PTR(result));
199 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
206 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
208 struct lu_site_bkt_data *bkt;
209 struct lu_site *site;
210 struct lu_object *scan;
214 site = o->lo_dev->ld_site;
215 layers = &o->lo_header->loh_layers;
216 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
218 * First call ->loo_object_delete() method to release all resources.
220 cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
221 if (scan->lo_ops->loo_object_delete != NULL)
222 scan->lo_ops->loo_object_delete(env, scan);
226 * Then, splice object layers into stand-alone list, and call
227 * ->loo_object_free() on all layers to free memory. Splice is
228 * necessary, because lu_object_header is freed together with the
231 CFS_INIT_LIST_HEAD(&splice);
232 cfs_list_splice_init(layers, &splice);
233 while (!cfs_list_empty(&splice)) {
235 * Free layers in bottom-to-top order, so that object header
236 * lives as long as possible and ->loo_object_free() methods
237 * can look at its contents.
239 o = container_of0(splice.prev, struct lu_object, lo_linkage);
240 cfs_list_del_init(&o->lo_linkage);
241 LASSERT(o->lo_ops->loo_object_free != NULL);
242 o->lo_ops->loo_object_free(env, o);
245 if (cfs_waitq_active(&bkt->lsb_marche_funebre))
246 cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
250 * Free \a nr objects from the cold end of the site LRU list.
252 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
254 struct lu_object_header *h;
255 struct lu_object_header *temp;
256 struct lu_site_bkt_data *bkt;
266 CFS_INIT_LIST_HEAD(&dispose);
268 * Under LRU list lock, scan LRU list and move unreferenced objects to
269 * the dispose list, removing them from LRU and hash table.
271 start = s->ls_purge_start;
272 bnr = (nr == ~0) ? -1 : nr / CFS_HASH_NBKT(s->ls_obj_hash) + 1;
275 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
279 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
280 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
282 cfs_list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
283 LASSERT(cfs_atomic_read(&h->loh_ref) == 0);
285 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
286 LASSERT(bd.bd_bucket == bd2.bd_bucket);
288 cfs_hash_bd_del_locked(s->ls_obj_hash,
290 cfs_list_move(&h->loh_lru, &dispose);
294 if (nr != ~0 && --nr == 0)
297 if (count > 0 && --count == 0)
301 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
304 * Free everything on the dispose list. This is safe against
305 * races due to the reasons described in lu_object_put().
307 while (!cfs_list_empty(&dispose)) {
308 h = container_of0(dispose.next,
309 struct lu_object_header, loh_lru);
310 cfs_list_del_init(&h->loh_lru);
311 lu_object_free(env, lu_object_top(h));
312 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
319 if (nr != 0 && did_sth && start != 0) {
320 start = 0; /* restart from the first bucket */
323 /* race on s->ls_purge_start, but nobody cares */
324 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
328 EXPORT_SYMBOL(lu_site_purge);
333 * Code below has to jump through certain loops to output object description
334 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
335 * composes object description from strings that are parts of _lines_ of
336 * output (i.e., strings that are not terminated by newline). This doesn't fit
337 * very well into libcfs_debug_msg() interface that assumes that each message
338 * supplied to it is a self-contained output line.
340 * To work around this, strings are collected in a temporary buffer
341 * (implemented as a value of lu_cdebug_key key), until terminating newline
342 * character is detected.
350 * XXX overflow is not handled correctly.
355 struct lu_cdebug_data {
359 char lck_area[LU_CDEBUG_LINE];
362 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
363 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
366 * Key, holding temporary buffer. This key is registered very early by
369 struct lu_context_key lu_global_key = {
370 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
371 LCT_MG_THREAD | LCT_CL_THREAD,
372 .lct_init = lu_global_key_init,
373 .lct_fini = lu_global_key_fini
377 * Printer function emitting messages through libcfs_debug_msg().
379 int lu_cdebug_printer(const struct lu_env *env,
380 void *cookie, const char *format, ...)
382 struct libcfs_debug_msg_data *msgdata = cookie;
383 struct lu_cdebug_data *key;
388 va_start(args, format);
390 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
391 LASSERT(key != NULL);
393 used = strlen(key->lck_area);
394 complete = format[strlen(format) - 1] == '\n';
396 * Append new chunk to the buffer.
398 vsnprintf(key->lck_area + used,
399 ARRAY_SIZE(key->lck_area) - used, format, args);
401 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
402 libcfs_debug_msg(msgdata, "%s", key->lck_area);
403 key->lck_area[0] = 0;
408 EXPORT_SYMBOL(lu_cdebug_printer);
411 * Print object header.
413 void lu_object_header_print(const struct lu_env *env, void *cookie,
414 lu_printer_t printer,
415 const struct lu_object_header *hdr)
417 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
418 hdr, hdr->loh_flags, cfs_atomic_read(&hdr->loh_ref),
420 cfs_hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
421 cfs_list_empty((cfs_list_t *)&hdr->loh_lru) ? \
423 hdr->loh_attr & LOHA_EXISTS ? " exist":"");
425 EXPORT_SYMBOL(lu_object_header_print);
428 * Print human readable representation of the \a o to the \a printer.
430 void lu_object_print(const struct lu_env *env, void *cookie,
431 lu_printer_t printer, const struct lu_object *o)
433 static const char ruler[] = "........................................";
434 struct lu_object_header *top;
438 lu_object_header_print(env, cookie, printer, top);
439 (*printer)(env, cookie, "{ \n");
440 cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
441 depth = o->lo_depth + 4;
444 * print `.' \a depth times followed by type name and address
446 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
447 o->lo_dev->ld_type->ldt_name, o);
448 if (o->lo_ops->loo_object_print != NULL)
449 o->lo_ops->loo_object_print(env, cookie, printer, o);
450 (*printer)(env, cookie, "\n");
452 (*printer)(env, cookie, "} header@%p\n", top);
454 EXPORT_SYMBOL(lu_object_print);
457 * Check object consistency.
459 int lu_object_invariant(const struct lu_object *o)
461 struct lu_object_header *top;
464 cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
465 if (o->lo_ops->loo_object_invariant != NULL &&
466 !o->lo_ops->loo_object_invariant(o))
471 EXPORT_SYMBOL(lu_object_invariant);
473 static struct lu_object *htable_lookup(struct lu_site *s,
475 const struct lu_fid *f,
476 cfs_waitlink_t *waiter,
479 struct lu_site_bkt_data *bkt;
480 struct lu_object_header *h;
481 cfs_hlist_node_t *hnode;
482 __u64 ver = cfs_hash_bd_version_get(bd);
488 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
489 /* cfs_hash_bd_lookup_intent is a somehow "internal" function
490 * of cfs_hash, but we don't want refcount on object right now */
491 hnode = cfs_hash_bd_lookup_locked(s->ls_obj_hash, bd, (void *)f);
493 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
497 h = container_of0(hnode, struct lu_object_header, loh_hash);
498 if (likely(!lu_object_is_dying(h))) {
499 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
500 cfs_list_del_init(&h->loh_lru);
501 return lu_object_top(h);
505 * Lookup found an object being destroyed this object cannot be
506 * returned (to assure that references to dying objects are eventually
507 * drained), and moreover, lookup has to wait until object is freed.
509 cfs_atomic_dec(&h->loh_ref);
511 cfs_waitlink_init(waiter);
512 cfs_waitq_add(&bkt->lsb_marche_funebre, waiter);
513 cfs_set_current_state(CFS_TASK_UNINT);
514 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
515 return ERR_PTR(-EAGAIN);
519 * Search cache for an object with the fid \a f. If such object is found,
520 * return it. Otherwise, create new object, insert it into cache and return
521 * it. In any case, additional reference is acquired on the returned object.
523 struct lu_object *lu_object_find(const struct lu_env *env,
524 struct lu_device *dev, const struct lu_fid *f,
525 const struct lu_object_conf *conf)
527 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
529 EXPORT_SYMBOL(lu_object_find);
531 static struct lu_object *lu_object_new(const struct lu_env *env,
532 struct lu_device *dev,
533 const struct lu_fid *f,
534 const struct lu_object_conf *conf)
539 struct lu_site_bkt_data *bkt;
541 o = lu_object_alloc(env, dev, f, conf);
542 if (unlikely(IS_ERR(o)))
545 hs = dev->ld_site->ls_obj_hash;
546 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
547 bkt = cfs_hash_bd_extra_get(hs, &bd);
548 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
550 cfs_hash_bd_unlock(hs, &bd, 1);
555 * Core logic of lu_object_find*() functions.
557 static struct lu_object *lu_object_find_try(const struct lu_env *env,
558 struct lu_device *dev,
559 const struct lu_fid *f,
560 const struct lu_object_conf *conf,
561 cfs_waitlink_t *waiter)
564 struct lu_object *shadow;
571 * This uses standard index maintenance protocol:
573 * - search index under lock, and return object if found;
574 * - otherwise, unlock index, allocate new object;
575 * - lock index and search again;
576 * - if nothing is found (usual case), insert newly created
578 * - otherwise (race: other thread inserted object), free
579 * object just allocated.
583 * For "LOC_F_NEW" case, we are sure the object is new established.
584 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
585 * just alloc and insert directly.
587 * If dying object is found during index search, add @waiter to the
588 * site wait-queue and return ERR_PTR(-EAGAIN).
590 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
591 return lu_object_new(env, dev, f, conf);
595 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
596 o = htable_lookup(s, &bd, f, waiter, &version);
597 cfs_hash_bd_unlock(hs, &bd, 1);
602 * Allocate new object. This may result in rather complicated
603 * operations, including fld queries, inode loading, etc.
605 o = lu_object_alloc(env, dev, f, conf);
606 if (unlikely(IS_ERR(o)))
609 LASSERT(lu_fid_eq(lu_object_fid(o), f));
611 cfs_hash_bd_lock(hs, &bd, 1);
613 shadow = htable_lookup(s, &bd, f, waiter, &version);
614 if (likely(shadow == NULL)) {
615 struct lu_site_bkt_data *bkt;
617 bkt = cfs_hash_bd_extra_get(hs, &bd);
618 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
620 cfs_hash_bd_unlock(hs, &bd, 1);
624 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
625 cfs_hash_bd_unlock(hs, &bd, 1);
626 lu_object_free(env, o);
631 * Much like lu_object_find(), but top level device of object is specifically
632 * \a dev rather than top level device of the site. This interface allows
633 * objects of different "stacking" to be created within the same site.
635 struct lu_object *lu_object_find_at(const struct lu_env *env,
636 struct lu_device *dev,
637 const struct lu_fid *f,
638 const struct lu_object_conf *conf)
640 struct lu_site_bkt_data *bkt;
641 struct lu_object *obj;
645 obj = lu_object_find_try(env, dev, f, conf, &wait);
646 if (obj != ERR_PTR(-EAGAIN))
649 * lu_object_find_try() already added waiter into the
652 cfs_waitq_wait(&wait, CFS_TASK_UNINT);
653 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
654 cfs_waitq_del(&bkt->lsb_marche_funebre, &wait);
657 EXPORT_SYMBOL(lu_object_find_at);
660 * Find object with given fid, and return its slice belonging to given device.
662 struct lu_object *lu_object_find_slice(const struct lu_env *env,
663 struct lu_device *dev,
664 const struct lu_fid *f,
665 const struct lu_object_conf *conf)
667 struct lu_object *top;
668 struct lu_object *obj;
670 top = lu_object_find(env, dev, f, conf);
672 obj = lu_object_locate(top->lo_header, dev->ld_type);
674 lu_object_put(env, top);
679 EXPORT_SYMBOL(lu_object_find_slice);
682 * Global list of all device types.
684 static CFS_LIST_HEAD(lu_device_types);
686 int lu_device_type_init(struct lu_device_type *ldt)
690 CFS_INIT_LIST_HEAD(&ldt->ldt_linkage);
691 result = ldt->ldt_ops->ldto_init(ldt);
693 cfs_list_add(&ldt->ldt_linkage, &lu_device_types);
696 EXPORT_SYMBOL(lu_device_type_init);
698 void lu_device_type_fini(struct lu_device_type *ldt)
700 cfs_list_del_init(&ldt->ldt_linkage);
701 ldt->ldt_ops->ldto_fini(ldt);
703 EXPORT_SYMBOL(lu_device_type_fini);
705 void lu_types_stop(void)
707 struct lu_device_type *ldt;
709 cfs_list_for_each_entry(ldt, &lu_device_types, ldt_linkage) {
710 if (ldt->ldt_device_nr == 0)
711 ldt->ldt_ops->ldto_stop(ldt);
714 EXPORT_SYMBOL(lu_types_stop);
717 * Global list of all sites on this node
719 static CFS_LIST_HEAD(lu_sites);
720 static CFS_DEFINE_MUTEX(lu_sites_guard);
723 * Global environment used by site shrinker.
725 static struct lu_env lu_shrink_env;
727 struct lu_site_print_arg {
728 struct lu_env *lsp_env;
730 lu_printer_t lsp_printer;
734 lu_site_obj_print(cfs_hash_t *hs, cfs_hash_bd_t *bd,
735 cfs_hlist_node_t *hnode, void *data)
737 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
738 struct lu_object_header *h;
740 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
741 if (!cfs_list_empty(&h->loh_layers)) {
742 const struct lu_object *o;
744 o = lu_object_top(h);
745 lu_object_print(arg->lsp_env, arg->lsp_cookie,
746 arg->lsp_printer, o);
748 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
749 arg->lsp_printer, h);
755 * Print all objects in \a s.
757 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
758 lu_printer_t printer)
760 struct lu_site_print_arg arg = {
761 .lsp_env = (struct lu_env *)env,
762 .lsp_cookie = cookie,
763 .lsp_printer = printer,
766 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
768 EXPORT_SYMBOL(lu_site_print);
771 LU_CACHE_PERCENT_MAX = 50,
772 LU_CACHE_PERCENT_DEFAULT = 20
775 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
776 CFS_MODULE_PARM(lu_cache_percent, "i", int, 0644,
777 "Percentage of memory to be used as lu_object cache");
780 * Return desired hash table order.
782 static int lu_htable_order(void)
784 unsigned long cache_size;
788 * Calculate hash table size, assuming that we want reasonable
789 * performance when 20% of total memory is occupied by cache of
792 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
794 cache_size = cfs_num_physpages;
796 #if BITS_PER_LONG == 32
797 /* limit hashtable size for lowmem systems to low RAM */
798 if (cache_size > 1 << (30 - CFS_PAGE_SHIFT))
799 cache_size = 1 << (30 - CFS_PAGE_SHIFT) * 3 / 4;
802 /* clear off unreasonable cache setting. */
803 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
804 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
805 " the range of (0, %u]. Will use default value: %u.\n",
806 lu_cache_percent, LU_CACHE_PERCENT_MAX,
807 LU_CACHE_PERCENT_DEFAULT);
809 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
811 cache_size = cache_size / 100 * lu_cache_percent *
812 (CFS_PAGE_SIZE / 1024);
814 for (bits = 1; (1 << bits) < cache_size; ++bits) {
820 static unsigned lu_obj_hop_hash(cfs_hash_t *hs,
821 const void *key, unsigned mask)
823 struct lu_fid *fid = (struct lu_fid *)key;
826 hash = fid_flatten32(fid);
827 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
828 hash = cfs_hash_long(hash, hs->hs_bkt_bits);
830 /* give me another random factor */
831 hash -= cfs_hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
833 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
834 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
839 static void *lu_obj_hop_object(cfs_hlist_node_t *hnode)
841 return cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
844 static void *lu_obj_hop_key(cfs_hlist_node_t *hnode)
846 struct lu_object_header *h;
848 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
852 static int lu_obj_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
854 struct lu_object_header *h;
856 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
857 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
860 static void lu_obj_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
862 struct lu_object_header *h;
864 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
865 if (cfs_atomic_add_return(1, &h->loh_ref) == 1) {
866 struct lu_site_bkt_data *bkt;
869 cfs_hash_bd_get(hs, &h->loh_fid, &bd);
870 bkt = cfs_hash_bd_extra_get(hs, &bd);
875 static void lu_obj_hop_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
877 LBUG(); /* we should never called it */
880 cfs_hash_ops_t lu_site_hash_ops = {
881 .hs_hash = lu_obj_hop_hash,
882 .hs_key = lu_obj_hop_key,
883 .hs_keycmp = lu_obj_hop_keycmp,
884 .hs_object = lu_obj_hop_object,
885 .hs_get = lu_obj_hop_get,
886 .hs_put_locked = lu_obj_hop_put_locked,
890 * Initialize site \a s, with \a d as the top level device.
892 #define LU_SITE_BITS_MIN 12
893 #define LU_SITE_BITS_MAX 24
895 * total 256 buckets, we don't want too many buckets because:
896 * - consume too much memory
897 * - avoid unbalanced LRU list
899 #define LU_SITE_BKT_BITS 8
901 int lu_site_init(struct lu_site *s, struct lu_device *top)
903 struct lu_site_bkt_data *bkt;
910 memset(s, 0, sizeof *s);
911 bits = lu_htable_order();
912 snprintf(name, 16, "lu_site_%s", top->ld_type->ldt_name);
913 for (bits = min(max(LU_SITE_BITS_MIN, bits), LU_SITE_BITS_MAX);
914 bits >= LU_SITE_BITS_MIN; bits--) {
915 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
916 bits - LU_SITE_BKT_BITS,
919 CFS_HASH_SPIN_BKTLOCK |
920 CFS_HASH_NO_ITEMREF |
922 CFS_HASH_ASSERT_EMPTY);
923 if (s->ls_obj_hash != NULL)
927 if (s->ls_obj_hash == NULL) {
928 CERROR("failed to create lu_site hash with bits: %d\n", bits);
932 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
933 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
934 CFS_INIT_LIST_HEAD(&bkt->lsb_lru);
935 cfs_waitq_init(&bkt->lsb_marche_funebre);
938 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
939 if (s->ls_stats == NULL) {
940 cfs_hash_putref(s->ls_obj_hash);
941 s->ls_obj_hash = NULL;
945 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
946 0, "created", "created");
947 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
948 0, "cache_hit", "cache_hit");
949 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
950 0, "cache_miss", "cache_miss");
951 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
952 0, "cache_race", "cache_race");
953 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
954 0, "cache_death_race", "cache_death_race");
955 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
956 0, "lru_purged", "lru_purged");
958 CFS_INIT_LIST_HEAD(&s->ls_linkage);
962 lu_ref_add(&top->ld_reference, "site-top", s);
964 CFS_INIT_LIST_HEAD(&s->ls_ld_linkage);
965 cfs_spin_lock_init(&s->ls_ld_lock);
967 cfs_spin_lock(&s->ls_ld_lock);
968 cfs_list_add(&top->ld_linkage, &s->ls_ld_linkage);
969 cfs_spin_unlock(&s->ls_ld_lock);
973 EXPORT_SYMBOL(lu_site_init);
976 * Finalize \a s and release its resources.
978 void lu_site_fini(struct lu_site *s)
980 cfs_mutex_lock(&lu_sites_guard);
981 cfs_list_del_init(&s->ls_linkage);
982 cfs_mutex_unlock(&lu_sites_guard);
984 if (s->ls_obj_hash != NULL) {
985 cfs_hash_putref(s->ls_obj_hash);
986 s->ls_obj_hash = NULL;
989 if (s->ls_top_dev != NULL) {
990 s->ls_top_dev->ld_site = NULL;
991 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
992 lu_device_put(s->ls_top_dev);
993 s->ls_top_dev = NULL;
996 if (s->ls_stats != NULL)
997 lprocfs_free_stats(&s->ls_stats);
999 EXPORT_SYMBOL(lu_site_fini);
1002 * Called when initialization of stack for this site is completed.
1004 int lu_site_init_finish(struct lu_site *s)
1007 cfs_mutex_lock(&lu_sites_guard);
1008 result = lu_context_refill(&lu_shrink_env.le_ctx);
1010 cfs_list_add(&s->ls_linkage, &lu_sites);
1011 cfs_mutex_unlock(&lu_sites_guard);
1014 EXPORT_SYMBOL(lu_site_init_finish);
1017 * Acquire additional reference on device \a d
1019 void lu_device_get(struct lu_device *d)
1021 cfs_atomic_inc(&d->ld_ref);
1023 EXPORT_SYMBOL(lu_device_get);
1026 * Release reference on device \a d.
1028 void lu_device_put(struct lu_device *d)
1030 LASSERT(cfs_atomic_read(&d->ld_ref) > 0);
1031 cfs_atomic_dec(&d->ld_ref);
1033 EXPORT_SYMBOL(lu_device_put);
1036 * Initialize device \a d of type \a t.
1038 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1040 if (t->ldt_device_nr++ == 0 && t->ldt_ops->ldto_start != NULL)
1041 t->ldt_ops->ldto_start(t);
1042 memset(d, 0, sizeof *d);
1043 cfs_atomic_set(&d->ld_ref, 0);
1045 lu_ref_init(&d->ld_reference);
1046 CFS_INIT_LIST_HEAD(&d->ld_linkage);
1049 EXPORT_SYMBOL(lu_device_init);
1052 * Finalize device \a d.
1054 void lu_device_fini(struct lu_device *d)
1056 struct lu_device_type *t;
1059 if (d->ld_obd != NULL) {
1060 d->ld_obd->obd_lu_dev = NULL;
1064 lu_ref_fini(&d->ld_reference);
1065 LASSERTF(cfs_atomic_read(&d->ld_ref) == 0,
1066 "Refcount is %u\n", cfs_atomic_read(&d->ld_ref));
1067 LASSERT(t->ldt_device_nr > 0);
1068 if (--t->ldt_device_nr == 0 && t->ldt_ops->ldto_stop != NULL)
1069 t->ldt_ops->ldto_stop(t);
1071 EXPORT_SYMBOL(lu_device_fini);
1074 * Initialize object \a o that is part of compound object \a h and was created
1077 int lu_object_init(struct lu_object *o,
1078 struct lu_object_header *h, struct lu_device *d)
1080 memset(o, 0, sizeof *o);
1084 o->lo_dev_ref = lu_ref_add(&d->ld_reference, "lu_object", o);
1085 CFS_INIT_LIST_HEAD(&o->lo_linkage);
1088 EXPORT_SYMBOL(lu_object_init);
1091 * Finalize object and release its resources.
1093 void lu_object_fini(struct lu_object *o)
1095 struct lu_device *dev = o->lo_dev;
1097 LASSERT(cfs_list_empty(&o->lo_linkage));
1100 lu_ref_del_at(&dev->ld_reference,
1101 o->lo_dev_ref , "lu_object", o);
1106 EXPORT_SYMBOL(lu_object_fini);
1109 * Add object \a o as first layer of compound object \a h
1111 * This is typically called by the ->ldo_object_alloc() method of top-level
1114 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1116 cfs_list_move(&o->lo_linkage, &h->loh_layers);
1118 EXPORT_SYMBOL(lu_object_add_top);
1121 * Add object \a o as a layer of compound object, going after \a before.
1123 * This is typically called by the ->ldo_object_alloc() method of \a
1126 void lu_object_add(struct lu_object *before, struct lu_object *o)
1128 cfs_list_move(&o->lo_linkage, &before->lo_linkage);
1130 EXPORT_SYMBOL(lu_object_add);
1133 * Initialize compound object.
1135 int lu_object_header_init(struct lu_object_header *h)
1137 memset(h, 0, sizeof *h);
1138 cfs_atomic_set(&h->loh_ref, 1);
1139 CFS_INIT_HLIST_NODE(&h->loh_hash);
1140 CFS_INIT_LIST_HEAD(&h->loh_lru);
1141 CFS_INIT_LIST_HEAD(&h->loh_layers);
1142 lu_ref_init(&h->loh_reference);
1145 EXPORT_SYMBOL(lu_object_header_init);
1148 * Finalize compound object.
1150 void lu_object_header_fini(struct lu_object_header *h)
1152 LASSERT(cfs_list_empty(&h->loh_layers));
1153 LASSERT(cfs_list_empty(&h->loh_lru));
1154 LASSERT(cfs_hlist_unhashed(&h->loh_hash));
1155 lu_ref_fini(&h->loh_reference);
1157 EXPORT_SYMBOL(lu_object_header_fini);
1160 * Given a compound object, find its slice, corresponding to the device type
1163 struct lu_object *lu_object_locate(struct lu_object_header *h,
1164 const struct lu_device_type *dtype)
1166 struct lu_object *o;
1168 cfs_list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1169 if (o->lo_dev->ld_type == dtype)
1174 EXPORT_SYMBOL(lu_object_locate);
1179 * Finalize and free devices in the device stack.
1181 * Finalize device stack by purging object cache, and calling
1182 * lu_device_type_operations::ldto_device_fini() and
1183 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1185 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1187 struct lu_site *site = top->ld_site;
1188 struct lu_device *scan;
1189 struct lu_device *next;
1191 lu_site_purge(env, site, ~0);
1192 for (scan = top; scan != NULL; scan = next) {
1193 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1194 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1195 lu_device_put(scan);
1199 lu_site_purge(env, site, ~0);
1201 if (!cfs_hash_is_empty(site->ls_obj_hash)) {
1203 * Uh-oh, objects still exist.
1205 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1207 lu_site_print(env, site, &msgdata, lu_cdebug_printer);
1210 for (scan = top; scan != NULL; scan = next) {
1211 const struct lu_device_type *ldt = scan->ld_type;
1212 struct obd_type *type;
1214 next = ldt->ldt_ops->ldto_device_free(env, scan);
1215 type = ldt->ldt_obd_type;
1218 class_put_type(type);
1222 EXPORT_SYMBOL(lu_stack_fini);
1226 * Maximal number of tld slots.
1228 LU_CONTEXT_KEY_NR = 32
1231 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1233 static cfs_spinlock_t lu_keys_guard = CFS_SPIN_LOCK_UNLOCKED;
1236 * Global counter incremented whenever key is registered, unregistered,
1237 * revived or quiesced. This is used to void unnecessary calls to
1238 * lu_context_refill(). No locking is provided, as initialization and shutdown
1239 * are supposed to be externally serialized.
1241 static unsigned key_set_version = 0;
1246 int lu_context_key_register(struct lu_context_key *key)
1251 LASSERT(key->lct_init != NULL);
1252 LASSERT(key->lct_fini != NULL);
1253 LASSERT(key->lct_tags != 0);
1254 LASSERT(key->lct_owner != NULL);
1257 cfs_spin_lock(&lu_keys_guard);
1258 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1259 if (lu_keys[i] == NULL) {
1261 cfs_atomic_set(&key->lct_used, 1);
1263 lu_ref_init(&key->lct_reference);
1269 cfs_spin_unlock(&lu_keys_guard);
1272 EXPORT_SYMBOL(lu_context_key_register);
1274 static void key_fini(struct lu_context *ctx, int index)
1276 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1277 struct lu_context_key *key;
1279 key = lu_keys[index];
1280 LASSERT(key != NULL);
1281 LASSERT(key->lct_fini != NULL);
1282 LASSERT(cfs_atomic_read(&key->lct_used) > 1);
1284 key->lct_fini(ctx, key, ctx->lc_value[index]);
1285 lu_ref_del(&key->lct_reference, "ctx", ctx);
1286 cfs_atomic_dec(&key->lct_used);
1288 LASSERT(key->lct_owner != NULL);
1289 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1290 LINVRNT(cfs_module_refcount(key->lct_owner) > 0);
1291 cfs_module_put(key->lct_owner);
1293 ctx->lc_value[index] = NULL;
1300 void lu_context_key_degister(struct lu_context_key *key)
1302 LASSERT(cfs_atomic_read(&key->lct_used) >= 1);
1303 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1305 lu_context_key_quiesce(key);
1308 cfs_spin_lock(&lu_keys_guard);
1309 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1310 if (lu_keys[key->lct_index]) {
1311 lu_keys[key->lct_index] = NULL;
1312 lu_ref_fini(&key->lct_reference);
1314 cfs_spin_unlock(&lu_keys_guard);
1316 LASSERTF(cfs_atomic_read(&key->lct_used) == 1,
1317 "key has instances: %d\n",
1318 cfs_atomic_read(&key->lct_used));
1320 EXPORT_SYMBOL(lu_context_key_degister);
1323 * Register a number of keys. This has to be called after all keys have been
1324 * initialized by a call to LU_CONTEXT_KEY_INIT().
1326 int lu_context_key_register_many(struct lu_context_key *k, ...)
1328 struct lu_context_key *key = k;
1334 result = lu_context_key_register(key);
1337 key = va_arg(args, struct lu_context_key *);
1338 } while (key != NULL);
1344 lu_context_key_degister(k);
1345 k = va_arg(args, struct lu_context_key *);
1352 EXPORT_SYMBOL(lu_context_key_register_many);
1355 * De-register a number of keys. This is a dual to
1356 * lu_context_key_register_many().
1358 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1364 lu_context_key_degister(k);
1365 k = va_arg(args, struct lu_context_key*);
1366 } while (k != NULL);
1369 EXPORT_SYMBOL(lu_context_key_degister_many);
1372 * Revive a number of keys.
1374 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1380 lu_context_key_revive(k);
1381 k = va_arg(args, struct lu_context_key*);
1382 } while (k != NULL);
1385 EXPORT_SYMBOL(lu_context_key_revive_many);
1388 * Quiescent a number of keys.
1390 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1396 lu_context_key_quiesce(k);
1397 k = va_arg(args, struct lu_context_key*);
1398 } while (k != NULL);
1401 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1404 * Return value associated with key \a key in context \a ctx.
1406 void *lu_context_key_get(const struct lu_context *ctx,
1407 const struct lu_context_key *key)
1409 LINVRNT(ctx->lc_state == LCS_ENTERED);
1410 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1411 LASSERT(lu_keys[key->lct_index] == key);
1412 return ctx->lc_value[key->lct_index];
1414 EXPORT_SYMBOL(lu_context_key_get);
1417 * List of remembered contexts. XXX document me.
1419 static CFS_LIST_HEAD(lu_context_remembered);
1422 * Destroy \a key in all remembered contexts. This is used to destroy key
1423 * values in "shared" contexts (like service threads), when a module owning
1424 * the key is about to be unloaded.
1426 void lu_context_key_quiesce(struct lu_context_key *key)
1428 struct lu_context *ctx;
1429 extern unsigned cl_env_cache_purge(unsigned nr);
1431 if (!(key->lct_tags & LCT_QUIESCENT)) {
1433 * XXX layering violation.
1435 cl_env_cache_purge(~0);
1436 key->lct_tags |= LCT_QUIESCENT;
1438 * XXX memory barrier has to go here.
1440 cfs_spin_lock(&lu_keys_guard);
1441 cfs_list_for_each_entry(ctx, &lu_context_remembered,
1443 key_fini(ctx, key->lct_index);
1444 cfs_spin_unlock(&lu_keys_guard);
1448 EXPORT_SYMBOL(lu_context_key_quiesce);
1450 void lu_context_key_revive(struct lu_context_key *key)
1452 key->lct_tags &= ~LCT_QUIESCENT;
1455 EXPORT_SYMBOL(lu_context_key_revive);
1457 static void keys_fini(struct lu_context *ctx)
1461 if (ctx->lc_value == NULL)
1464 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1467 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1468 ctx->lc_value = NULL;
1471 static int keys_fill(struct lu_context *ctx)
1475 LINVRNT(ctx->lc_value != NULL);
1476 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1477 struct lu_context_key *key;
1480 if (ctx->lc_value[i] == NULL && key != NULL &&
1481 (key->lct_tags & ctx->lc_tags) &&
1483 * Don't create values for a LCT_QUIESCENT key, as this
1484 * will pin module owning a key.
1486 !(key->lct_tags & LCT_QUIESCENT)) {
1489 LINVRNT(key->lct_init != NULL);
1490 LINVRNT(key->lct_index == i);
1492 value = key->lct_init(ctx, key);
1493 if (unlikely(IS_ERR(value)))
1494 return PTR_ERR(value);
1496 LASSERT(key->lct_owner != NULL);
1497 if (!(ctx->lc_tags & LCT_NOREF))
1498 cfs_try_module_get(key->lct_owner);
1499 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1500 cfs_atomic_inc(&key->lct_used);
1502 * This is the only place in the code, where an
1503 * element of ctx->lc_value[] array is set to non-NULL
1506 ctx->lc_value[i] = value;
1507 if (key->lct_exit != NULL)
1508 ctx->lc_tags |= LCT_HAS_EXIT;
1510 ctx->lc_version = key_set_version;
1515 static int keys_init(struct lu_context *ctx)
1517 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1518 if (likely(ctx->lc_value != NULL))
1519 return keys_fill(ctx);
1525 * Initialize context data-structure. Create values for all keys.
1527 int lu_context_init(struct lu_context *ctx, __u32 tags)
1531 memset(ctx, 0, sizeof *ctx);
1532 ctx->lc_state = LCS_INITIALIZED;
1533 ctx->lc_tags = tags;
1534 if (tags & LCT_REMEMBER) {
1535 cfs_spin_lock(&lu_keys_guard);
1536 cfs_list_add(&ctx->lc_remember, &lu_context_remembered);
1537 cfs_spin_unlock(&lu_keys_guard);
1539 CFS_INIT_LIST_HEAD(&ctx->lc_remember);
1542 rc = keys_init(ctx);
1544 lu_context_fini(ctx);
1548 EXPORT_SYMBOL(lu_context_init);
1551 * Finalize context data-structure. Destroy key values.
1553 void lu_context_fini(struct lu_context *ctx)
1555 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1556 ctx->lc_state = LCS_FINALIZED;
1558 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1559 LASSERT(cfs_list_empty(&ctx->lc_remember));
1562 } else { /* could race with key degister */
1563 cfs_spin_lock(&lu_keys_guard);
1565 cfs_list_del_init(&ctx->lc_remember);
1566 cfs_spin_unlock(&lu_keys_guard);
1569 EXPORT_SYMBOL(lu_context_fini);
1572 * Called before entering context.
1574 void lu_context_enter(struct lu_context *ctx)
1576 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1577 ctx->lc_state = LCS_ENTERED;
1579 EXPORT_SYMBOL(lu_context_enter);
1582 * Called after exiting from \a ctx
1584 void lu_context_exit(struct lu_context *ctx)
1588 LINVRNT(ctx->lc_state == LCS_ENTERED);
1589 ctx->lc_state = LCS_LEFT;
1590 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1591 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1592 if (ctx->lc_value[i] != NULL) {
1593 struct lu_context_key *key;
1596 LASSERT(key != NULL);
1597 if (key->lct_exit != NULL)
1599 key, ctx->lc_value[i]);
1604 EXPORT_SYMBOL(lu_context_exit);
1607 * Allocate for context all missing keys that were registered after context
1608 * creation. key_set_version is only changed in rare cases when modules
1609 * are loaded and removed.
1611 int lu_context_refill(struct lu_context *ctx)
1613 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1615 EXPORT_SYMBOL(lu_context_refill);
1618 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1619 * obd being added. Currently, this is only used on client side, specifically
1620 * for echo device client, for other stack (like ptlrpc threads), context are
1621 * predefined when the lu_device type are registered, during the module probe
1624 __u32 lu_context_tags_default = 0;
1625 __u32 lu_session_tags_default = 0;
1627 void lu_context_tags_update(__u32 tags)
1629 cfs_spin_lock(&lu_keys_guard);
1630 lu_context_tags_default |= tags;
1632 cfs_spin_unlock(&lu_keys_guard);
1634 EXPORT_SYMBOL(lu_context_tags_update);
1636 void lu_context_tags_clear(__u32 tags)
1638 cfs_spin_lock(&lu_keys_guard);
1639 lu_context_tags_default &= ~tags;
1641 cfs_spin_unlock(&lu_keys_guard);
1643 EXPORT_SYMBOL(lu_context_tags_clear);
1645 void lu_session_tags_update(__u32 tags)
1647 cfs_spin_lock(&lu_keys_guard);
1648 lu_session_tags_default |= tags;
1650 cfs_spin_unlock(&lu_keys_guard);
1652 EXPORT_SYMBOL(lu_session_tags_update);
1654 void lu_session_tags_clear(__u32 tags)
1656 cfs_spin_lock(&lu_keys_guard);
1657 lu_session_tags_default &= ~tags;
1659 cfs_spin_unlock(&lu_keys_guard);
1661 EXPORT_SYMBOL(lu_session_tags_clear);
1663 int lu_env_init(struct lu_env *env, __u32 tags)
1668 result = lu_context_init(&env->le_ctx, tags);
1669 if (likely(result == 0))
1670 lu_context_enter(&env->le_ctx);
1673 EXPORT_SYMBOL(lu_env_init);
1675 void lu_env_fini(struct lu_env *env)
1677 lu_context_exit(&env->le_ctx);
1678 lu_context_fini(&env->le_ctx);
1681 EXPORT_SYMBOL(lu_env_fini);
1683 int lu_env_refill(struct lu_env *env)
1687 result = lu_context_refill(&env->le_ctx);
1688 if (result == 0 && env->le_ses != NULL)
1689 result = lu_context_refill(env->le_ses);
1692 EXPORT_SYMBOL(lu_env_refill);
1695 * Currently, this API will only be used by echo client.
1696 * Because echo client and normal lustre client will share
1697 * same cl_env cache. So echo client needs to refresh
1698 * the env context after it get one from the cache, especially
1699 * when normal client and echo client co-exist in the same client.
1701 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1706 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1707 env->le_ctx.lc_version = 0;
1708 env->le_ctx.lc_tags |= ctags;
1711 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1712 env->le_ses->lc_version = 0;
1713 env->le_ses->lc_tags |= stags;
1716 result = lu_env_refill(env);
1720 EXPORT_SYMBOL(lu_env_refill_by_tags);
1722 static struct cfs_shrinker *lu_site_shrinker = NULL;
1724 typedef struct lu_site_stats{
1725 unsigned lss_populated;
1726 unsigned lss_max_search;
1731 static void lu_site_stats_get(cfs_hash_t *hs,
1732 lu_site_stats_t *stats, int populated)
1737 cfs_hash_for_each_bucket(hs, &bd, i) {
1738 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1739 cfs_hlist_head_t *hhead;
1741 cfs_hash_bd_lock(hs, &bd, 1);
1742 stats->lss_busy += bkt->lsb_busy;
1743 stats->lss_total += cfs_hash_bd_count_get(&bd);
1744 stats->lss_max_search = max((int)stats->lss_max_search,
1745 cfs_hash_bd_depmax_get(&bd));
1747 cfs_hash_bd_unlock(hs, &bd, 1);
1751 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1752 if (!cfs_hlist_empty(hhead))
1753 stats->lss_populated++;
1755 cfs_hash_bd_unlock(hs, &bd, 1);
1761 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
1763 lu_site_stats_t stats;
1765 struct lu_site *tmp;
1767 int remain = shrink_param(sc, nr_to_scan);
1768 CFS_LIST_HEAD(splice);
1771 if (!(shrink_param(sc, gfp_mask) & __GFP_FS))
1773 CDEBUG(D_INODE, "Shrink %d objects\n", remain);
1776 cfs_mutex_lock(&lu_sites_guard);
1777 cfs_list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1778 if (shrink_param(sc, nr_to_scan) != 0) {
1779 remain = lu_site_purge(&lu_shrink_env, s, remain);
1781 * Move just shrunk site to the tail of site list to
1782 * assure shrinking fairness.
1784 cfs_list_move_tail(&s->ls_linkage, &splice);
1787 memset(&stats, 0, sizeof(stats));
1788 lu_site_stats_get(s->ls_obj_hash, &stats, 0);
1789 cached += stats.lss_total - stats.lss_busy;
1790 if (shrink_param(sc, nr_to_scan) && remain <= 0)
1793 cfs_list_splice(&splice, lu_sites.prev);
1794 cfs_mutex_unlock(&lu_sites_guard);
1796 cached = (cached / 100) * sysctl_vfs_cache_pressure;
1797 if (shrink_param(sc, nr_to_scan) == 0)
1798 CDEBUG(D_INODE, "%d objects cached\n", cached);
1807 * Environment to be used in debugger, contains all tags.
1809 struct lu_env lu_debugging_env;
1812 * Debugging printer function using printk().
1814 int lu_printk_printer(const struct lu_env *env,
1815 void *unused, const char *format, ...)
1819 va_start(args, format);
1820 vprintk(format, args);
1825 void lu_debugging_setup(void)
1827 lu_env_init(&lu_debugging_env, ~0);
1830 void lu_context_keys_dump(void)
1834 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1835 struct lu_context_key *key;
1839 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
1840 i, key, key->lct_tags,
1841 key->lct_init, key->lct_fini, key->lct_exit,
1842 key->lct_index, cfs_atomic_read(&key->lct_used),
1843 key->lct_owner ? key->lct_owner->name : "",
1845 lu_ref_print(&key->lct_reference);
1849 EXPORT_SYMBOL(lu_context_keys_dump);
1850 #else /* !__KERNEL__ */
1851 static int lu_cache_shrink(int nr, unsigned int gfp_mask)
1855 #endif /* __KERNEL__ */
1857 int cl_global_init(void);
1858 void cl_global_fini(void);
1859 int lu_ref_global_init(void);
1860 void lu_ref_global_fini(void);
1862 int dt_global_init(void);
1863 void dt_global_fini(void);
1865 int llo_global_init(void);
1866 void llo_global_fini(void);
1869 * Initialization of global lu_* data.
1871 int lu_global_init(void)
1875 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
1877 result = lu_ref_global_init();
1881 LU_CONTEXT_KEY_INIT(&lu_global_key);
1882 result = lu_context_key_register(&lu_global_key);
1886 * At this level, we don't know what tags are needed, so allocate them
1887 * conservatively. This should not be too bad, because this
1888 * environment is global.
1890 cfs_mutex_lock(&lu_sites_guard);
1891 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
1892 cfs_mutex_unlock(&lu_sites_guard);
1897 * seeks estimation: 3 seeks to read a record from oi, one to read
1898 * inode, one for ea. Unfortunately setting this high value results in
1899 * lu_object/inode cache consuming all the memory.
1901 lu_site_shrinker = cfs_set_shrinker(CFS_DEFAULT_SEEKS, lu_cache_shrink);
1902 if (lu_site_shrinker == NULL)
1905 result = lu_time_global_init();
1910 result = dt_global_init();
1914 result = llo_global_init();
1918 result = cl_global_init();
1925 * Dual to lu_global_init().
1927 void lu_global_fini(void)
1934 lu_time_global_fini();
1935 if (lu_site_shrinker != NULL) {
1936 cfs_remove_shrinker(lu_site_shrinker);
1937 lu_site_shrinker = NULL;
1940 lu_context_key_degister(&lu_global_key);
1943 * Tear shrinker environment down _after_ de-registering
1944 * lu_global_key, because the latter has a value in the former.
1946 cfs_mutex_lock(&lu_sites_guard);
1947 lu_env_fini(&lu_shrink_env);
1948 cfs_mutex_unlock(&lu_sites_guard);
1950 lu_ref_global_fini();
1953 struct lu_buf LU_BUF_NULL = {
1957 EXPORT_SYMBOL(LU_BUF_NULL);
1959 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
1962 struct lprocfs_counter ret;
1964 lprocfs_stats_collect(stats, idx, &ret);
1965 return (__u32)ret.lc_count;
1972 * Output site statistical counters into a buffer. Suitable for
1973 * lprocfs_rd_*()-style functions.
1975 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
1977 lu_site_stats_t stats;
1979 memset(&stats, 0, sizeof(stats));
1980 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
1982 return snprintf(page, count, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
1985 stats.lss_populated,
1986 CFS_HASH_NHLIST(s->ls_obj_hash),
1987 stats.lss_max_search,
1988 ls_stats_read(s->ls_stats, LU_SS_CREATED),
1989 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
1990 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
1991 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
1992 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
1993 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
1995 EXPORT_SYMBOL(lu_site_stats_print);
1997 const char *lu_time_names[LU_TIME_NR] = {
1998 [LU_TIME_FIND_LOOKUP] = "find_lookup",
1999 [LU_TIME_FIND_ALLOC] = "find_alloc",
2000 [LU_TIME_FIND_INSERT] = "find_insert"
2002 EXPORT_SYMBOL(lu_time_names);
2005 * Helper function to initialize a number of kmem slab caches at once.
2007 int lu_kmem_init(struct lu_kmem_descr *caches)
2010 struct lu_kmem_descr *iter = caches;
2012 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2013 *iter->ckd_cache = cfs_mem_cache_create(iter->ckd_name,
2016 if (*iter->ckd_cache == NULL) {
2018 /* free all previously allocated caches */
2019 lu_kmem_fini(caches);
2025 EXPORT_SYMBOL(lu_kmem_init);
2028 * Helper function to finalize a number of kmem slab cached at once. Dual to
2031 void lu_kmem_fini(struct lu_kmem_descr *caches)
2035 for (; caches->ckd_cache != NULL; ++caches) {
2036 if (*caches->ckd_cache != NULL) {
2037 rc = cfs_mem_cache_destroy(*caches->ckd_cache);
2038 LASSERTF(rc == 0, "couldn't destroy %s slab\n",
2040 *caches->ckd_cache = NULL;
2044 EXPORT_SYMBOL(lu_kmem_fini);