1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
39 * lustre/obdclass/lu_object.c
42 * These are the only exported functions, they provide some generic
43 * infrastructure for managing object devices
45 * Author: Nikita Danilov <nikita.danilov@sun.com>
48 #define DEBUG_SUBSYSTEM S_CLASS
50 # define EXPORT_SYMTAB
53 #include <libcfs/libcfs.h>
56 # include <linux/module.h>
60 #include <libcfs/libcfs_hash.h>
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <lustre_disk.h>
64 #include <lustre_fid.h>
65 #include <lu_object.h>
66 #include <libcfs/list.h>
67 /* lu_time_global_{init,fini}() */
70 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
73 * Decrease reference counter on object. If last reference is freed, return
74 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
75 * case, free object immediately.
77 void lu_object_put(const struct lu_env *env, struct lu_object *o)
79 struct lu_site_bkt_data *bkt;
80 struct lu_object_header *top;
82 struct lu_object *orig;
86 site = o->lo_dev->ld_site;
89 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
90 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
92 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
93 if (lu_object_is_dying(top)) {
96 * somebody may be waiting for this, currently only
97 * used for cl_object, see cl_object_put_last().
99 cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
104 LASSERT(bkt->lsb_busy > 0);
107 * When last reference is released, iterate over object
108 * layers, and notify them that object is no longer busy.
110 cfs_list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
111 if (o->lo_ops->loo_object_release != NULL)
112 o->lo_ops->loo_object_release(env, o);
115 if (!lu_object_is_dying(top)) {
116 LASSERT(cfs_list_empty(&top->loh_lru));
117 cfs_list_add_tail(&top->loh_lru, &bkt->lsb_lru);
118 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
123 * If object is dying (will not be cached), removed it
124 * from hash table and LRU.
126 * This is done with hash table and LRU lists locked. As the only
127 * way to acquire first reference to previously unreferenced
128 * object is through hash-table lookup (lu_object_find()),
129 * or LRU scanning (lu_site_purge()), that are done under hash-table
130 * and LRU lock, no race with concurrent object lookup is possible
131 * and we can safely destroy object below.
133 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
134 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
136 * Object was already removed from hash and lru above, can
139 lu_object_free(env, orig);
141 EXPORT_SYMBOL(lu_object_put);
144 * Allocate new object.
146 * This follows object creation protocol, described in the comment within
147 * struct lu_device_operations definition.
149 static struct lu_object *lu_object_alloc(const struct lu_env *env,
150 struct lu_device *dev,
151 const struct lu_fid *f,
152 const struct lu_object_conf *conf)
154 struct lu_object *scan;
155 struct lu_object *top;
162 * Create top-level object slice. This will also create
165 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
167 RETURN(ERR_PTR(-ENOMEM));
169 * This is the only place where object fid is assigned. It's constant
172 LASSERT(fid_is_igif(f) || fid_ver(f) == 0);
173 top->lo_header->loh_fid = *f;
174 layers = &top->lo_header->loh_layers;
177 * Call ->loo_object_init() repeatedly, until no more new
178 * object slices are created.
181 cfs_list_for_each_entry(scan, layers, lo_linkage) {
182 if (scan->lo_flags & LU_OBJECT_ALLOCATED)
185 scan->lo_header = top->lo_header;
186 result = scan->lo_ops->loo_object_init(env, scan, conf);
188 lu_object_free(env, top);
189 RETURN(ERR_PTR(result));
191 scan->lo_flags |= LU_OBJECT_ALLOCATED;
195 cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
196 if (scan->lo_ops->loo_object_start != NULL) {
197 result = scan->lo_ops->loo_object_start(env, scan);
199 lu_object_free(env, top);
200 RETURN(ERR_PTR(result));
205 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
212 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
214 struct lu_site_bkt_data *bkt;
215 struct lu_site *site;
216 struct lu_object *scan;
220 site = o->lo_dev->ld_site;
221 layers = &o->lo_header->loh_layers;
222 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
224 * First call ->loo_object_delete() method to release all resources.
226 cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
227 if (scan->lo_ops->loo_object_delete != NULL)
228 scan->lo_ops->loo_object_delete(env, scan);
232 * Then, splice object layers into stand-alone list, and call
233 * ->loo_object_free() on all layers to free memory. Splice is
234 * necessary, because lu_object_header is freed together with the
237 CFS_INIT_LIST_HEAD(&splice);
238 cfs_list_splice_init(layers, &splice);
239 while (!cfs_list_empty(&splice)) {
241 * Free layers in bottom-to-top order, so that object header
242 * lives as long as possible and ->loo_object_free() methods
243 * can look at its contents.
245 o = container_of0(splice.prev, struct lu_object, lo_linkage);
246 cfs_list_del_init(&o->lo_linkage);
247 LASSERT(o->lo_ops->loo_object_free != NULL);
248 o->lo_ops->loo_object_free(env, o);
251 if (cfs_waitq_active(&bkt->lsb_marche_funebre))
252 cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
256 * Free \a nr objects from the cold end of the site LRU list.
258 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
260 struct lu_object_header *h;
261 struct lu_object_header *temp;
262 struct lu_site_bkt_data *bkt;
272 CFS_INIT_LIST_HEAD(&dispose);
274 * Under LRU list lock, scan LRU list and move unreferenced objects to
275 * the dispose list, removing them from LRU and hash table.
277 start = s->ls_purge_start;
278 bnr = (nr == ~0) ? -1 : nr / CFS_HASH_NBKT(s->ls_obj_hash) + 1;
281 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
285 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
286 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
288 cfs_list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
289 LASSERT(cfs_atomic_read(&h->loh_ref) == 0);
291 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
292 LASSERT(bd.bd_bucket == bd2.bd_bucket);
294 cfs_hash_bd_del_locked(s->ls_obj_hash,
296 cfs_list_move(&h->loh_lru, &dispose);
300 if (nr != ~0 && --nr == 0)
303 if (count > 0 && --count == 0)
307 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
310 * Free everything on the dispose list. This is safe against
311 * races due to the reasons described in lu_object_put().
313 while (!cfs_list_empty(&dispose)) {
314 h = container_of0(dispose.next,
315 struct lu_object_header, loh_lru);
316 cfs_list_del_init(&h->loh_lru);
317 lu_object_free(env, lu_object_top(h));
318 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
325 if (nr != 0 && did_sth && start != 0) {
326 start = 0; /* restart from the first bucket */
329 /* race on s->ls_purge_start, but nobody cares */
330 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
334 EXPORT_SYMBOL(lu_site_purge);
339 * Code below has to jump through certain loops to output object description
340 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
341 * composes object description from strings that are parts of _lines_ of
342 * output (i.e., strings that are not terminated by newline). This doesn't fit
343 * very well into libcfs_debug_msg() interface that assumes that each message
344 * supplied to it is a self-contained output line.
346 * To work around this, strings are collected in a temporary buffer
347 * (implemented as a value of lu_cdebug_key key), until terminating newline
348 * character is detected.
356 * XXX overflow is not handled correctly.
361 struct lu_cdebug_data {
365 char lck_area[LU_CDEBUG_LINE];
368 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
369 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
372 * Key, holding temporary buffer. This key is registered very early by
375 struct lu_context_key lu_global_key = {
376 .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
377 .lct_init = lu_global_key_init,
378 .lct_fini = lu_global_key_fini
382 * Printer function emitting messages through libcfs_debug_msg().
384 int lu_cdebug_printer(const struct lu_env *env,
385 void *cookie, const char *format, ...)
387 struct lu_cdebug_print_info *info = cookie;
388 struct lu_cdebug_data *key;
393 va_start(args, format);
395 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
396 LASSERT(key != NULL);
398 used = strlen(key->lck_area);
399 complete = format[strlen(format) - 1] == '\n';
401 * Append new chunk to the buffer.
403 vsnprintf(key->lck_area + used,
404 ARRAY_SIZE(key->lck_area) - used, format, args);
406 if (cfs_cdebug_show(info->lpi_mask, info->lpi_subsys))
407 libcfs_debug_msg(NULL, info->lpi_subsys, info->lpi_mask,
408 (char *)info->lpi_file, info->lpi_fn,
409 info->lpi_line, "%s", key->lck_area);
410 key->lck_area[0] = 0;
415 EXPORT_SYMBOL(lu_cdebug_printer);
418 * Print object header.
420 void lu_object_header_print(const struct lu_env *env, void *cookie,
421 lu_printer_t printer,
422 const struct lu_object_header *hdr)
424 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
425 hdr, hdr->loh_flags, cfs_atomic_read(&hdr->loh_ref),
427 cfs_hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
428 cfs_list_empty((cfs_list_t *)&hdr->loh_lru) ? \
430 hdr->loh_attr & LOHA_EXISTS ? " exist":"");
432 EXPORT_SYMBOL(lu_object_header_print);
435 * Print human readable representation of the \a o to the \a printer.
437 void lu_object_print(const struct lu_env *env, void *cookie,
438 lu_printer_t printer, const struct lu_object *o)
440 static const char ruler[] = "........................................";
441 struct lu_object_header *top;
445 lu_object_header_print(env, cookie, printer, top);
446 (*printer)(env, cookie, "{ \n");
447 cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
448 depth = o->lo_depth + 4;
451 * print `.' \a depth times followed by type name and address
453 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
454 o->lo_dev->ld_type->ldt_name, o);
455 if (o->lo_ops->loo_object_print != NULL)
456 o->lo_ops->loo_object_print(env, cookie, printer, o);
457 (*printer)(env, cookie, "\n");
459 (*printer)(env, cookie, "} header@%p\n", top);
461 EXPORT_SYMBOL(lu_object_print);
464 * Check object consistency.
466 int lu_object_invariant(const struct lu_object *o)
468 struct lu_object_header *top;
471 cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
472 if (o->lo_ops->loo_object_invariant != NULL &&
473 !o->lo_ops->loo_object_invariant(o))
478 EXPORT_SYMBOL(lu_object_invariant);
480 static struct lu_object *htable_lookup(struct lu_site *s,
482 const struct lu_fid *f,
483 cfs_waitlink_t *waiter,
486 struct lu_site_bkt_data *bkt;
487 struct lu_object_header *h;
488 cfs_hlist_node_t *hnode;
489 __u64 ver = cfs_hash_bd_version_get(bd);
495 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
496 /* cfs_hash_bd_lookup_intent is a somehow "internal" function
497 * of cfs_hash, but we don't want refcount on object right now */
498 hnode = cfs_hash_bd_lookup_locked(s->ls_obj_hash, bd, (void *)f);
500 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
504 h = container_of0(hnode, struct lu_object_header, loh_hash);
505 if (likely(!lu_object_is_dying(h))) {
506 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
507 return lu_object_top(h);
511 * Lookup found an object being destroyed this object cannot be
512 * returned (to assure that references to dying objects are eventually
513 * drained), and moreover, lookup has to wait until object is freed.
515 cfs_atomic_dec(&h->loh_ref);
517 cfs_waitlink_init(waiter);
518 cfs_waitq_add(&bkt->lsb_marche_funebre, waiter);
519 cfs_set_current_state(CFS_TASK_UNINT);
520 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
521 return ERR_PTR(-EAGAIN);
525 * Search cache for an object with the fid \a f. If such object is found,
526 * return it. Otherwise, create new object, insert it into cache and return
527 * it. In any case, additional reference is acquired on the returned object.
529 struct lu_object *lu_object_find(const struct lu_env *env,
530 struct lu_device *dev, const struct lu_fid *f,
531 const struct lu_object_conf *conf)
533 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
535 EXPORT_SYMBOL(lu_object_find);
537 static struct lu_object *lu_object_new(const struct lu_env *env,
538 struct lu_device *dev,
539 const struct lu_fid *f,
540 const struct lu_object_conf *conf)
545 struct lu_site_bkt_data *bkt;
547 o = lu_object_alloc(env, dev, f, conf);
548 if (unlikely(IS_ERR(o)))
551 hs = dev->ld_site->ls_obj_hash;
552 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
553 bkt = cfs_hash_bd_extra_get(hs, &bd);
554 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
556 cfs_hash_bd_unlock(hs, &bd, 1);
561 * Core logic of lu_object_find*() functions.
563 static struct lu_object *lu_object_find_try(const struct lu_env *env,
564 struct lu_device *dev,
565 const struct lu_fid *f,
566 const struct lu_object_conf *conf,
567 cfs_waitlink_t *waiter)
570 struct lu_object *shadow;
577 * This uses standard index maintenance protocol:
579 * - search index under lock, and return object if found;
580 * - otherwise, unlock index, allocate new object;
581 * - lock index and search again;
582 * - if nothing is found (usual case), insert newly created
584 * - otherwise (race: other thread inserted object), free
585 * object just allocated.
589 * For "LOC_F_NEW" case, we are sure the object is new established.
590 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
591 * just alloc and insert directly.
593 * If dying object is found during index search, add @waiter to the
594 * site wait-queue and return ERR_PTR(-EAGAIN).
596 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
597 return lu_object_new(env, dev, f, conf);
601 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
602 o = htable_lookup(s, &bd, f, waiter, &version);
603 if (o != NULL && !cfs_list_empty(&o->lo_header->loh_lru))
604 cfs_list_del_init(&o->lo_header->loh_lru);
605 cfs_hash_bd_unlock(hs, &bd, 1);
610 * Allocate new object. This may result in rather complicated
611 * operations, including fld queries, inode loading, etc.
613 o = lu_object_alloc(env, dev, f, conf);
614 if (unlikely(IS_ERR(o)))
617 LASSERT(lu_fid_eq(lu_object_fid(o), f));
619 cfs_hash_bd_lock(hs, &bd, 1);
621 shadow = htable_lookup(s, &bd, f, waiter, &version);
622 if (likely(shadow == NULL)) {
623 struct lu_site_bkt_data *bkt;
625 bkt = cfs_hash_bd_extra_get(hs, &bd);
626 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
628 cfs_hash_bd_unlock(hs, &bd, 1);
632 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
633 cfs_hash_bd_unlock(hs, &bd, 1);
634 lu_object_free(env, o);
639 * Much like lu_object_find(), but top level device of object is specifically
640 * \a dev rather than top level device of the site. This interface allows
641 * objects of different "stacking" to be created within the same site.
643 struct lu_object *lu_object_find_at(const struct lu_env *env,
644 struct lu_device *dev,
645 const struct lu_fid *f,
646 const struct lu_object_conf *conf)
648 struct lu_site_bkt_data *bkt;
649 struct lu_object *obj;
653 obj = lu_object_find_try(env, dev, f, conf, &wait);
654 if (obj != ERR_PTR(-EAGAIN))
657 * lu_object_find_try() already added waiter into the
660 cfs_waitq_wait(&wait, CFS_TASK_UNINT);
661 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
662 cfs_waitq_del(&bkt->lsb_marche_funebre, &wait);
665 EXPORT_SYMBOL(lu_object_find_at);
668 * Find object with given fid, and return its slice belonging to given device.
670 struct lu_object *lu_object_find_slice(const struct lu_env *env,
671 struct lu_device *dev,
672 const struct lu_fid *f,
673 const struct lu_object_conf *conf)
675 struct lu_object *top;
676 struct lu_object *obj;
678 top = lu_object_find(env, dev, f, conf);
680 obj = lu_object_locate(top->lo_header, dev->ld_type);
682 lu_object_put(env, top);
687 EXPORT_SYMBOL(lu_object_find_slice);
690 * Global list of all device types.
692 static CFS_LIST_HEAD(lu_device_types);
694 int lu_device_type_init(struct lu_device_type *ldt)
698 CFS_INIT_LIST_HEAD(&ldt->ldt_linkage);
699 result = ldt->ldt_ops->ldto_init(ldt);
701 cfs_list_add(&ldt->ldt_linkage, &lu_device_types);
704 EXPORT_SYMBOL(lu_device_type_init);
706 void lu_device_type_fini(struct lu_device_type *ldt)
708 cfs_list_del_init(&ldt->ldt_linkage);
709 ldt->ldt_ops->ldto_fini(ldt);
711 EXPORT_SYMBOL(lu_device_type_fini);
713 void lu_types_stop(void)
715 struct lu_device_type *ldt;
717 cfs_list_for_each_entry(ldt, &lu_device_types, ldt_linkage) {
718 if (ldt->ldt_device_nr == 0)
719 ldt->ldt_ops->ldto_stop(ldt);
722 EXPORT_SYMBOL(lu_types_stop);
725 * Global list of all sites on this node
727 static CFS_LIST_HEAD(lu_sites);
728 static CFS_DECLARE_MUTEX(lu_sites_guard);
731 * Global environment used by site shrinker.
733 static struct lu_env lu_shrink_env;
735 struct lu_site_print_arg {
736 struct lu_env *lsp_env;
738 lu_printer_t lsp_printer;
742 lu_site_obj_print(cfs_hash_t *hs, cfs_hash_bd_t *bd,
743 cfs_hlist_node_t *hnode, void *data)
745 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
746 struct lu_object_header *h;
748 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
749 if (!cfs_list_empty(&h->loh_layers)) {
750 const struct lu_object *o;
752 o = lu_object_top(h);
753 lu_object_print(arg->lsp_env, arg->lsp_cookie,
754 arg->lsp_printer, o);
756 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
757 arg->lsp_printer, h);
763 * Print all objects in \a s.
765 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
766 lu_printer_t printer)
768 struct lu_site_print_arg arg = {
769 .lsp_env = (struct lu_env *)env,
770 .lsp_cookie = cookie,
771 .lsp_printer = printer,
774 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
776 EXPORT_SYMBOL(lu_site_print);
779 LU_CACHE_PERCENT_MAX = 50,
780 LU_CACHE_PERCENT_DEFAULT = 20
783 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
784 CFS_MODULE_PARM(lu_cache_percent, "i", int, 0644,
785 "Percentage of memory to be used as lu_object cache");
788 * Return desired hash table order.
790 static int lu_htable_order(void)
792 unsigned long cache_size;
796 * Calculate hash table size, assuming that we want reasonable
797 * performance when 20% of total memory is occupied by cache of
800 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
802 cache_size = cfs_num_physpages;
804 #if BITS_PER_LONG == 32
805 /* limit hashtable size for lowmem systems to low RAM */
806 if (cache_size > 1 << (30 - CFS_PAGE_SHIFT))
807 cache_size = 1 << (30 - CFS_PAGE_SHIFT) * 3 / 4;
810 /* clear off unreasonable cache setting. */
811 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
812 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
813 " the range of (0, %u]. Will use default value: %u.\n",
814 lu_cache_percent, LU_CACHE_PERCENT_MAX,
815 LU_CACHE_PERCENT_DEFAULT);
817 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
819 cache_size = cache_size / 100 * lu_cache_percent *
820 (CFS_PAGE_SIZE / 1024);
822 for (bits = 1; (1 << bits) < cache_size; ++bits) {
828 static unsigned lu_obj_hop_hash(cfs_hash_t *hs,
829 const void *key, unsigned mask)
831 struct lu_fid *fid = (struct lu_fid *)key;
834 hash = fid_flatten32(fid);
835 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
836 hash = cfs_hash_long(hash, hs->hs_bkt_bits);
838 /* give me another random factor */
839 hash -= cfs_hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
841 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
842 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
847 static void *lu_obj_hop_object(cfs_hlist_node_t *hnode)
849 return cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
852 static void *lu_obj_hop_key(cfs_hlist_node_t *hnode)
854 struct lu_object_header *h;
856 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
860 static int lu_obj_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
862 struct lu_object_header *h;
864 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
865 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
868 static void lu_obj_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
870 struct lu_object_header *h;
872 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
873 if (cfs_atomic_add_return(1, &h->loh_ref) == 1) {
874 struct lu_site_bkt_data *bkt;
877 cfs_hash_bd_get(hs, &h->loh_fid, &bd);
878 bkt = cfs_hash_bd_extra_get(hs, &bd);
883 static void lu_obj_hop_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
885 LBUG(); /* we should never called it */
888 cfs_hash_ops_t lu_site_hash_ops = {
889 .hs_hash = lu_obj_hop_hash,
890 .hs_key = lu_obj_hop_key,
891 .hs_keycmp = lu_obj_hop_keycmp,
892 .hs_object = lu_obj_hop_object,
893 .hs_get = lu_obj_hop_get,
894 .hs_put_locked = lu_obj_hop_put_locked,
898 * Initialize site \a s, with \a d as the top level device.
900 #define LU_SITE_BITS_MIN 12
901 #define LU_SITE_BITS_MAX 24
903 * total 256 buckets, we don't want too many buckets because:
904 * - consume too much memory
905 * - avoid unbalanced LRU list
907 #define LU_SITE_BKT_BITS 8
909 int lu_site_init(struct lu_site *s, struct lu_device *top)
911 struct lu_site_bkt_data *bkt;
918 memset(s, 0, sizeof *s);
919 bits = lu_htable_order();
920 snprintf(name, 16, "lu_site_%s", top->ld_type->ldt_name);
921 for (bits = min(max(LU_SITE_BITS_MIN, bits), LU_SITE_BITS_MAX);
922 bits >= LU_SITE_BITS_MIN; bits--) {
923 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
924 bits - LU_SITE_BKT_BITS,
927 CFS_HASH_SPIN_BKTLOCK |
928 CFS_HASH_NO_ITEMREF |
930 CFS_HASH_ASSERT_EMPTY);
931 if (s->ls_obj_hash != NULL)
935 if (s->ls_obj_hash == NULL) {
936 CERROR("failed to create lu_site hash with bits: %d\n", bits);
940 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
941 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
942 CFS_INIT_LIST_HEAD(&bkt->lsb_lru);
943 cfs_waitq_init(&bkt->lsb_marche_funebre);
946 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
947 if (s->ls_stats == NULL) {
948 cfs_hash_putref(s->ls_obj_hash);
949 s->ls_obj_hash = NULL;
953 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
954 0, "created", "created");
955 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
956 0, "cache_hit", "cache_hit");
957 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
958 0, "cache_miss", "cache_miss");
959 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
960 0, "cache_race", "cache_race");
961 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
962 0, "cache_death_race", "cache_death_race");
963 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
964 0, "lru_purged", "lru_purged");
966 CFS_INIT_LIST_HEAD(&s->ls_linkage);
970 lu_ref_add(&top->ld_reference, "site-top", s);
972 CFS_INIT_LIST_HEAD(&s->ls_ld_linkage);
973 cfs_spin_lock_init(&s->ls_ld_lock);
975 cfs_spin_lock(&s->ls_ld_lock);
976 cfs_list_add(&top->ld_linkage, &s->ls_ld_linkage);
977 cfs_spin_unlock(&s->ls_ld_lock);
981 EXPORT_SYMBOL(lu_site_init);
984 * Finalize \a s and release its resources.
986 void lu_site_fini(struct lu_site *s)
988 cfs_down(&lu_sites_guard);
989 cfs_list_del_init(&s->ls_linkage);
990 cfs_up(&lu_sites_guard);
992 if (s->ls_obj_hash != NULL) {
993 cfs_hash_putref(s->ls_obj_hash);
994 s->ls_obj_hash = NULL;
997 if (s->ls_top_dev != NULL) {
998 s->ls_top_dev->ld_site = NULL;
999 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1000 lu_device_put(s->ls_top_dev);
1001 s->ls_top_dev = NULL;
1004 if (s->ls_stats != NULL)
1005 lprocfs_free_stats(&s->ls_stats);
1007 EXPORT_SYMBOL(lu_site_fini);
1010 * Called when initialization of stack for this site is completed.
1012 int lu_site_init_finish(struct lu_site *s)
1015 cfs_down(&lu_sites_guard);
1016 result = lu_context_refill(&lu_shrink_env.le_ctx);
1018 cfs_list_add(&s->ls_linkage, &lu_sites);
1019 cfs_up(&lu_sites_guard);
1022 EXPORT_SYMBOL(lu_site_init_finish);
1025 * Acquire additional reference on device \a d
1027 void lu_device_get(struct lu_device *d)
1029 cfs_atomic_inc(&d->ld_ref);
1031 EXPORT_SYMBOL(lu_device_get);
1034 * Release reference on device \a d.
1036 void lu_device_put(struct lu_device *d)
1038 LASSERT(cfs_atomic_read(&d->ld_ref) > 0);
1039 cfs_atomic_dec(&d->ld_ref);
1041 EXPORT_SYMBOL(lu_device_put);
1044 * Initialize device \a d of type \a t.
1046 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1048 if (t->ldt_device_nr++ == 0 && t->ldt_ops->ldto_start != NULL)
1049 t->ldt_ops->ldto_start(t);
1050 memset(d, 0, sizeof *d);
1051 cfs_atomic_set(&d->ld_ref, 0);
1053 lu_ref_init(&d->ld_reference);
1054 CFS_INIT_LIST_HEAD(&d->ld_linkage);
1057 EXPORT_SYMBOL(lu_device_init);
1060 * Finalize device \a d.
1062 void lu_device_fini(struct lu_device *d)
1064 struct lu_device_type *t;
1067 if (d->ld_obd != NULL) {
1068 d->ld_obd->obd_lu_dev = NULL;
1072 lu_ref_fini(&d->ld_reference);
1073 LASSERTF(cfs_atomic_read(&d->ld_ref) == 0,
1074 "Refcount is %u\n", cfs_atomic_read(&d->ld_ref));
1075 LASSERT(t->ldt_device_nr > 0);
1076 if (--t->ldt_device_nr == 0 && t->ldt_ops->ldto_stop != NULL)
1077 t->ldt_ops->ldto_stop(t);
1079 EXPORT_SYMBOL(lu_device_fini);
1082 * Initialize object \a o that is part of compound object \a h and was created
1085 int lu_object_init(struct lu_object *o,
1086 struct lu_object_header *h, struct lu_device *d)
1088 memset(o, 0, sizeof *o);
1092 o->lo_dev_ref = lu_ref_add(&d->ld_reference, "lu_object", o);
1093 CFS_INIT_LIST_HEAD(&o->lo_linkage);
1096 EXPORT_SYMBOL(lu_object_init);
1099 * Finalize object and release its resources.
1101 void lu_object_fini(struct lu_object *o)
1103 struct lu_device *dev = o->lo_dev;
1105 LASSERT(cfs_list_empty(&o->lo_linkage));
1108 lu_ref_del_at(&dev->ld_reference,
1109 o->lo_dev_ref , "lu_object", o);
1114 EXPORT_SYMBOL(lu_object_fini);
1117 * Add object \a o as first layer of compound object \a h
1119 * This is typically called by the ->ldo_object_alloc() method of top-level
1122 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1124 cfs_list_move(&o->lo_linkage, &h->loh_layers);
1126 EXPORT_SYMBOL(lu_object_add_top);
1129 * Add object \a o as a layer of compound object, going after \a before.
1131 * This is typically called by the ->ldo_object_alloc() method of \a
1134 void lu_object_add(struct lu_object *before, struct lu_object *o)
1136 cfs_list_move(&o->lo_linkage, &before->lo_linkage);
1138 EXPORT_SYMBOL(lu_object_add);
1141 * Initialize compound object.
1143 int lu_object_header_init(struct lu_object_header *h)
1145 memset(h, 0, sizeof *h);
1146 cfs_atomic_set(&h->loh_ref, 1);
1147 CFS_INIT_HLIST_NODE(&h->loh_hash);
1148 CFS_INIT_LIST_HEAD(&h->loh_lru);
1149 CFS_INIT_LIST_HEAD(&h->loh_layers);
1150 lu_ref_init(&h->loh_reference);
1153 EXPORT_SYMBOL(lu_object_header_init);
1156 * Finalize compound object.
1158 void lu_object_header_fini(struct lu_object_header *h)
1160 LASSERT(cfs_list_empty(&h->loh_layers));
1161 LASSERT(cfs_list_empty(&h->loh_lru));
1162 LASSERT(cfs_hlist_unhashed(&h->loh_hash));
1163 lu_ref_fini(&h->loh_reference);
1165 EXPORT_SYMBOL(lu_object_header_fini);
1168 * Given a compound object, find its slice, corresponding to the device type
1171 struct lu_object *lu_object_locate(struct lu_object_header *h,
1172 const struct lu_device_type *dtype)
1174 struct lu_object *o;
1176 cfs_list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1177 if (o->lo_dev->ld_type == dtype)
1182 EXPORT_SYMBOL(lu_object_locate);
1187 * Finalize and free devices in the device stack.
1189 * Finalize device stack by purging object cache, and calling
1190 * lu_device_type_operations::ldto_device_fini() and
1191 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1193 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1195 struct lu_site *site = top->ld_site;
1196 struct lu_device *scan;
1197 struct lu_device *next;
1199 lu_site_purge(env, site, ~0);
1200 for (scan = top; scan != NULL; scan = next) {
1201 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1202 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1203 lu_device_put(scan);
1207 lu_site_purge(env, site, ~0);
1209 if (!cfs_hash_is_empty(site->ls_obj_hash)) {
1211 * Uh-oh, objects still exist.
1213 static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
1215 lu_site_print(env, site, &cookie, lu_cdebug_printer);
1218 for (scan = top; scan != NULL; scan = next) {
1219 const struct lu_device_type *ldt = scan->ld_type;
1220 struct obd_type *type;
1222 next = ldt->ldt_ops->ldto_device_free(env, scan);
1223 type = ldt->ldt_obd_type;
1226 class_put_type(type);
1230 EXPORT_SYMBOL(lu_stack_fini);
1234 * Maximal number of tld slots.
1236 LU_CONTEXT_KEY_NR = 32
1239 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1241 static cfs_spinlock_t lu_keys_guard = CFS_SPIN_LOCK_UNLOCKED;
1244 * Global counter incremented whenever key is registered, unregistered,
1245 * revived or quiesced. This is used to void unnecessary calls to
1246 * lu_context_refill(). No locking is provided, as initialization and shutdown
1247 * are supposed to be externally serialized.
1249 static unsigned key_set_version = 0;
1254 int lu_context_key_register(struct lu_context_key *key)
1259 LASSERT(key->lct_init != NULL);
1260 LASSERT(key->lct_fini != NULL);
1261 LASSERT(key->lct_tags != 0);
1262 LASSERT(key->lct_owner != NULL);
1265 cfs_spin_lock(&lu_keys_guard);
1266 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1267 if (lu_keys[i] == NULL) {
1269 cfs_atomic_set(&key->lct_used, 1);
1271 lu_ref_init(&key->lct_reference);
1277 cfs_spin_unlock(&lu_keys_guard);
1280 EXPORT_SYMBOL(lu_context_key_register);
1282 static void key_fini(struct lu_context *ctx, int index)
1284 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1285 struct lu_context_key *key;
1287 key = lu_keys[index];
1288 LASSERT(key != NULL);
1289 LASSERT(key->lct_fini != NULL);
1290 LASSERT(cfs_atomic_read(&key->lct_used) > 1);
1292 key->lct_fini(ctx, key, ctx->lc_value[index]);
1293 lu_ref_del(&key->lct_reference, "ctx", ctx);
1294 cfs_atomic_dec(&key->lct_used);
1295 LASSERT(key->lct_owner != NULL);
1296 if (!(ctx->lc_tags & LCT_NOREF)) {
1297 LASSERT(cfs_module_refcount(key->lct_owner) > 0);
1298 cfs_module_put(key->lct_owner);
1300 ctx->lc_value[index] = NULL;
1307 void lu_context_key_degister(struct lu_context_key *key)
1309 LASSERT(cfs_atomic_read(&key->lct_used) >= 1);
1310 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1312 lu_context_key_quiesce(key);
1315 cfs_spin_lock(&lu_keys_guard);
1316 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1317 if (lu_keys[key->lct_index]) {
1318 lu_keys[key->lct_index] = NULL;
1319 lu_ref_fini(&key->lct_reference);
1321 cfs_spin_unlock(&lu_keys_guard);
1323 LASSERTF(cfs_atomic_read(&key->lct_used) == 1,
1324 "key has instances: %d\n",
1325 cfs_atomic_read(&key->lct_used));
1327 EXPORT_SYMBOL(lu_context_key_degister);
1330 * Register a number of keys. This has to be called after all keys have been
1331 * initialized by a call to LU_CONTEXT_KEY_INIT().
1333 int lu_context_key_register_many(struct lu_context_key *k, ...)
1335 struct lu_context_key *key = k;
1341 result = lu_context_key_register(key);
1344 key = va_arg(args, struct lu_context_key *);
1345 } while (key != NULL);
1351 lu_context_key_degister(k);
1352 k = va_arg(args, struct lu_context_key *);
1359 EXPORT_SYMBOL(lu_context_key_register_many);
1362 * De-register a number of keys. This is a dual to
1363 * lu_context_key_register_many().
1365 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1371 lu_context_key_degister(k);
1372 k = va_arg(args, struct lu_context_key*);
1373 } while (k != NULL);
1376 EXPORT_SYMBOL(lu_context_key_degister_many);
1379 * Revive a number of keys.
1381 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1387 lu_context_key_revive(k);
1388 k = va_arg(args, struct lu_context_key*);
1389 } while (k != NULL);
1392 EXPORT_SYMBOL(lu_context_key_revive_many);
1395 * Quiescent a number of keys.
1397 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1403 lu_context_key_quiesce(k);
1404 k = va_arg(args, struct lu_context_key*);
1405 } while (k != NULL);
1408 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1411 * Return value associated with key \a key in context \a ctx.
1413 void *lu_context_key_get(const struct lu_context *ctx,
1414 const struct lu_context_key *key)
1416 LINVRNT(ctx->lc_state == LCS_ENTERED);
1417 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1418 LASSERT(lu_keys[key->lct_index] == key);
1419 return ctx->lc_value[key->lct_index];
1421 EXPORT_SYMBOL(lu_context_key_get);
1424 * List of remembered contexts. XXX document me.
1426 static CFS_LIST_HEAD(lu_context_remembered);
1429 * Destroy \a key in all remembered contexts. This is used to destroy key
1430 * values in "shared" contexts (like service threads), when a module owning
1431 * the key is about to be unloaded.
1433 void lu_context_key_quiesce(struct lu_context_key *key)
1435 struct lu_context *ctx;
1436 extern unsigned cl_env_cache_purge(unsigned nr);
1438 if (!(key->lct_tags & LCT_QUIESCENT)) {
1440 * XXX layering violation.
1442 cl_env_cache_purge(~0);
1443 key->lct_tags |= LCT_QUIESCENT;
1445 * XXX memory barrier has to go here.
1447 cfs_spin_lock(&lu_keys_guard);
1448 cfs_list_for_each_entry(ctx, &lu_context_remembered,
1450 key_fini(ctx, key->lct_index);
1451 cfs_spin_unlock(&lu_keys_guard);
1455 EXPORT_SYMBOL(lu_context_key_quiesce);
1457 void lu_context_key_revive(struct lu_context_key *key)
1459 key->lct_tags &= ~LCT_QUIESCENT;
1462 EXPORT_SYMBOL(lu_context_key_revive);
1464 static void keys_fini(struct lu_context *ctx)
1468 cfs_spin_lock(&lu_keys_guard);
1469 if (ctx->lc_value != NULL) {
1470 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1472 OBD_FREE(ctx->lc_value,
1473 ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1474 ctx->lc_value = NULL;
1476 cfs_spin_unlock(&lu_keys_guard);
1479 static int keys_fill(struct lu_context *ctx)
1483 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1484 struct lu_context_key *key;
1487 if (ctx->lc_value[i] == NULL && key != NULL &&
1488 (key->lct_tags & ctx->lc_tags) &&
1490 * Don't create values for a LCT_QUIESCENT key, as this
1491 * will pin module owning a key.
1493 !(key->lct_tags & LCT_QUIESCENT)) {
1496 LINVRNT(key->lct_init != NULL);
1497 LINVRNT(key->lct_index == i);
1499 value = key->lct_init(ctx, key);
1500 if (unlikely(IS_ERR(value)))
1501 return PTR_ERR(value);
1503 LASSERT(key->lct_owner != NULL);
1504 if (!(ctx->lc_tags & LCT_NOREF))
1505 cfs_try_module_get(key->lct_owner);
1506 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1507 cfs_atomic_inc(&key->lct_used);
1509 * This is the only place in the code, where an
1510 * element of ctx->lc_value[] array is set to non-NULL
1513 ctx->lc_value[i] = value;
1514 if (key->lct_exit != NULL)
1515 ctx->lc_tags |= LCT_HAS_EXIT;
1517 ctx->lc_version = key_set_version;
1522 static int keys_init(struct lu_context *ctx)
1526 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1527 if (likely(ctx->lc_value != NULL))
1528 result = keys_fill(ctx);
1538 * Initialize context data-structure. Create values for all keys.
1540 int lu_context_init(struct lu_context *ctx, __u32 tags)
1542 memset(ctx, 0, sizeof *ctx);
1543 ctx->lc_state = LCS_INITIALIZED;
1544 ctx->lc_tags = tags;
1545 if (tags & LCT_REMEMBER) {
1546 cfs_spin_lock(&lu_keys_guard);
1547 cfs_list_add(&ctx->lc_remember, &lu_context_remembered);
1548 cfs_spin_unlock(&lu_keys_guard);
1550 CFS_INIT_LIST_HEAD(&ctx->lc_remember);
1551 return keys_init(ctx);
1553 EXPORT_SYMBOL(lu_context_init);
1556 * Finalize context data-structure. Destroy key values.
1558 void lu_context_fini(struct lu_context *ctx)
1560 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1561 ctx->lc_state = LCS_FINALIZED;
1563 cfs_spin_lock(&lu_keys_guard);
1564 cfs_list_del_init(&ctx->lc_remember);
1565 cfs_spin_unlock(&lu_keys_guard);
1567 EXPORT_SYMBOL(lu_context_fini);
1570 * Called before entering context.
1572 void lu_context_enter(struct lu_context *ctx)
1574 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1575 ctx->lc_state = LCS_ENTERED;
1577 EXPORT_SYMBOL(lu_context_enter);
1580 * Called after exiting from \a ctx
1582 void lu_context_exit(struct lu_context *ctx)
1586 LINVRNT(ctx->lc_state == LCS_ENTERED);
1587 ctx->lc_state = LCS_LEFT;
1588 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1589 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1590 if (ctx->lc_value[i] != NULL) {
1591 struct lu_context_key *key;
1594 LASSERT(key != NULL);
1595 if (key->lct_exit != NULL)
1597 key, ctx->lc_value[i]);
1602 EXPORT_SYMBOL(lu_context_exit);
1605 * Allocate for context all missing keys that were registered after context
1608 int lu_context_refill(struct lu_context *ctx)
1610 LINVRNT(ctx->lc_value != NULL);
1611 return ctx->lc_version == key_set_version ? 0 : keys_fill(ctx);
1613 EXPORT_SYMBOL(lu_context_refill);
1615 int lu_env_init(struct lu_env *env, __u32 tags)
1620 result = lu_context_init(&env->le_ctx, tags);
1621 if (likely(result == 0))
1622 lu_context_enter(&env->le_ctx);
1625 EXPORT_SYMBOL(lu_env_init);
1627 void lu_env_fini(struct lu_env *env)
1629 lu_context_exit(&env->le_ctx);
1630 lu_context_fini(&env->le_ctx);
1633 EXPORT_SYMBOL(lu_env_fini);
1635 int lu_env_refill(struct lu_env *env)
1639 result = lu_context_refill(&env->le_ctx);
1640 if (result == 0 && env->le_ses != NULL)
1641 result = lu_context_refill(env->le_ses);
1644 EXPORT_SYMBOL(lu_env_refill);
1646 static struct cfs_shrinker *lu_site_shrinker = NULL;
1648 typedef struct lu_site_stats{
1649 unsigned lss_populated;
1650 unsigned lss_max_search;
1655 static void lu_site_stats_get(cfs_hash_t *hs,
1656 lu_site_stats_t *stats, int populated)
1661 cfs_hash_for_each_bucket(hs, &bd, i) {
1662 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1663 cfs_hlist_head_t *hhead;
1665 cfs_hash_bd_lock(hs, &bd, 1);
1666 stats->lss_busy += bkt->lsb_busy;
1667 stats->lss_total += cfs_hash_bd_count_get(&bd);
1668 stats->lss_max_search = max((int)stats->lss_max_search,
1669 cfs_hash_bd_depmax_get(&bd));
1671 cfs_hash_bd_unlock(hs, &bd, 1);
1675 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1676 if (!cfs_hlist_empty(hhead))
1677 stats->lss_populated++;
1679 cfs_hash_bd_unlock(hs, &bd, 1);
1685 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
1687 lu_site_stats_t stats;
1689 struct lu_site *tmp;
1691 int remain = shrink_param(sc, nr_to_scan);
1692 CFS_LIST_HEAD(splice);
1695 if (!(shrink_param(sc, gfp_mask) & __GFP_FS))
1697 CDEBUG(D_INODE, "Shrink %d objects\n", remain);
1700 cfs_down(&lu_sites_guard);
1701 cfs_list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1702 if (shrink_param(sc, nr_to_scan) != 0) {
1703 remain = lu_site_purge(&lu_shrink_env, s, remain);
1705 * Move just shrunk site to the tail of site list to
1706 * assure shrinking fairness.
1708 cfs_list_move_tail(&s->ls_linkage, &splice);
1711 memset(&stats, 0, sizeof(stats));
1712 lu_site_stats_get(s->ls_obj_hash, &stats, 0);
1713 cached += stats.lss_total - stats.lss_busy;
1714 if (shrink_param(sc, nr_to_scan) && remain <= 0)
1717 cfs_list_splice(&splice, lu_sites.prev);
1718 cfs_up(&lu_sites_guard);
1720 cached = (cached / 100) * sysctl_vfs_cache_pressure;
1721 if (shrink_param(sc, nr_to_scan) == 0)
1722 CDEBUG(D_INODE, "%d objects cached\n", cached);
1731 * Environment to be used in debugger, contains all tags.
1733 struct lu_env lu_debugging_env;
1736 * Debugging printer function using printk().
1738 int lu_printk_printer(const struct lu_env *env,
1739 void *unused, const char *format, ...)
1743 va_start(args, format);
1744 vprintk(format, args);
1749 void lu_debugging_setup(void)
1751 lu_env_init(&lu_debugging_env, ~0);
1754 void lu_context_keys_dump(void)
1758 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1759 struct lu_context_key *key;
1763 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
1764 i, key, key->lct_tags,
1765 key->lct_init, key->lct_fini, key->lct_exit,
1766 key->lct_index, cfs_atomic_read(&key->lct_used),
1767 key->lct_owner ? key->lct_owner->name : "",
1769 lu_ref_print(&key->lct_reference);
1773 EXPORT_SYMBOL(lu_context_keys_dump);
1774 #else /* !__KERNEL__ */
1775 static int lu_cache_shrink(int nr, unsigned int gfp_mask)
1779 #endif /* __KERNEL__ */
1781 int cl_global_init(void);
1782 void cl_global_fini(void);
1783 int lu_ref_global_init(void);
1784 void lu_ref_global_fini(void);
1786 int dt_global_init(void);
1787 void dt_global_fini(void);
1789 int llo_global_init(void);
1790 void llo_global_fini(void);
1793 * Initialization of global lu_* data.
1795 int lu_global_init(void)
1799 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
1801 result = lu_ref_global_init();
1805 LU_CONTEXT_KEY_INIT(&lu_global_key);
1806 result = lu_context_key_register(&lu_global_key);
1810 * At this level, we don't know what tags are needed, so allocate them
1811 * conservatively. This should not be too bad, because this
1812 * environment is global.
1814 cfs_down(&lu_sites_guard);
1815 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
1816 cfs_up(&lu_sites_guard);
1821 * seeks estimation: 3 seeks to read a record from oi, one to read
1822 * inode, one for ea. Unfortunately setting this high value results in
1823 * lu_object/inode cache consuming all the memory.
1825 lu_site_shrinker = cfs_set_shrinker(CFS_DEFAULT_SEEKS, lu_cache_shrink);
1826 if (lu_site_shrinker == NULL)
1829 result = lu_time_global_init();
1834 result = dt_global_init();
1838 result = llo_global_init();
1842 result = cl_global_init();
1849 * Dual to lu_global_init().
1851 void lu_global_fini(void)
1858 lu_time_global_fini();
1859 if (lu_site_shrinker != NULL) {
1860 cfs_remove_shrinker(lu_site_shrinker);
1861 lu_site_shrinker = NULL;
1864 lu_context_key_degister(&lu_global_key);
1867 * Tear shrinker environment down _after_ de-registering
1868 * lu_global_key, because the latter has a value in the former.
1870 cfs_down(&lu_sites_guard);
1871 lu_env_fini(&lu_shrink_env);
1872 cfs_up(&lu_sites_guard);
1874 lu_ref_global_fini();
1877 struct lu_buf LU_BUF_NULL = {
1881 EXPORT_SYMBOL(LU_BUF_NULL);
1883 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
1886 struct lprocfs_counter ret;
1888 lprocfs_stats_collect(stats, idx, &ret);
1889 return (__u32)ret.lc_count;
1896 * Output site statistical counters into a buffer. Suitable for
1897 * lprocfs_rd_*()-style functions.
1899 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
1901 lu_site_stats_t stats;
1903 memset(&stats, 0, sizeof(stats));
1904 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
1906 return snprintf(page, count, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
1909 stats.lss_populated,
1910 CFS_HASH_NHLIST(s->ls_obj_hash),
1911 stats.lss_max_search,
1912 ls_stats_read(s->ls_stats, LU_SS_CREATED),
1913 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
1914 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
1915 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
1916 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
1917 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
1919 EXPORT_SYMBOL(lu_site_stats_print);
1921 const char *lu_time_names[LU_TIME_NR] = {
1922 [LU_TIME_FIND_LOOKUP] = "find_lookup",
1923 [LU_TIME_FIND_ALLOC] = "find_alloc",
1924 [LU_TIME_FIND_INSERT] = "find_insert"
1926 EXPORT_SYMBOL(lu_time_names);
1929 * Helper function to initialize a number of kmem slab caches at once.
1931 int lu_kmem_init(struct lu_kmem_descr *caches)
1934 struct lu_kmem_descr *iter = caches;
1936 for (result = 0; iter->ckd_cache != NULL; ++iter) {
1937 *iter->ckd_cache = cfs_mem_cache_create(iter->ckd_name,
1940 if (*iter->ckd_cache == NULL) {
1942 /* free all previously allocated caches */
1943 lu_kmem_fini(caches);
1949 EXPORT_SYMBOL(lu_kmem_init);
1952 * Helper function to finalize a number of kmem slab cached at once. Dual to
1955 void lu_kmem_fini(struct lu_kmem_descr *caches)
1959 for (; caches->ckd_cache != NULL; ++caches) {
1960 if (*caches->ckd_cache != NULL) {
1961 rc = cfs_mem_cache_destroy(*caches->ckd_cache);
1962 LASSERTF(rc == 0, "couldn't destroy %s slab\n",
1964 *caches->ckd_cache = NULL;
1968 EXPORT_SYMBOL(lu_kmem_fini);