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 * 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 # define EXPORT_SYMTAB
50 #include <libcfs/libcfs.h>
53 # include <linux/module.h>
57 #include <libcfs/libcfs_hash.h>
58 #include <obd_class.h>
59 #include <obd_support.h>
60 #include <lustre_disk.h>
61 #include <lustre_fid.h>
62 #include <lu_object.h>
63 #include <libcfs/list.h>
64 /* lu_time_global_{init,fini}() */
67 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
70 * Decrease reference counter on object. If last reference is freed, return
71 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
72 * case, free object immediately.
74 void lu_object_put(const struct lu_env *env, struct lu_object *o)
76 struct lu_object_header *top;
78 struct lu_object *orig;
82 site = o->lo_dev->ld_site;
85 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
86 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
87 if (lu_object_is_dying(top)) {
88 struct lu_site_bkt_data *bkt;
91 * somebody may be waiting for this, currently only
92 * used for cl_object, see cl_object_put_last().
94 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
95 cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
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 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
115 * If object is dying (will not be cached), removed it
116 * from hash table and LRU.
118 * This is done with hash table and LRU lists locked. As the only
119 * way to acquire first reference to previously unreferenced
120 * object is through hash-table lookup (lu_object_find()),
121 * or LRU scanning (lu_site_purge()), that are done under hash-table
122 * and LRU lock, no race with concurrent object lookup is possible
123 * and we can safely destroy object below.
125 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
126 cfs_list_del_init(&top->loh_lru);
127 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
129 * Object was already removed from hash and lru above, can
132 lu_object_free(env, orig);
134 EXPORT_SYMBOL(lu_object_put);
137 * Allocate new object.
139 * This follows object creation protocol, described in the comment within
140 * struct lu_device_operations definition.
142 static struct lu_object *lu_object_alloc(const struct lu_env *env,
143 struct lu_device *dev,
144 const struct lu_fid *f,
145 const struct lu_object_conf *conf)
147 struct lu_object *scan;
148 struct lu_object *top;
155 * Create top-level object slice. This will also create
158 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
160 RETURN(ERR_PTR(-ENOMEM));
162 * This is the only place where object fid is assigned. It's constant
165 LASSERT(fid_is_igif(f) || fid_ver(f) == 0);
166 top->lo_header->loh_fid = *f;
167 layers = &top->lo_header->loh_layers;
170 * Call ->loo_object_init() repeatedly, until no more new
171 * object slices are created.
174 cfs_list_for_each_entry(scan, layers, lo_linkage) {
175 if (scan->lo_flags & LU_OBJECT_ALLOCATED)
178 scan->lo_header = top->lo_header;
179 result = scan->lo_ops->loo_object_init(env, scan, conf);
181 lu_object_free(env, top);
182 RETURN(ERR_PTR(result));
184 scan->lo_flags |= LU_OBJECT_ALLOCATED;
188 cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
189 if (scan->lo_ops->loo_object_start != NULL) {
190 result = scan->lo_ops->loo_object_start(env, scan);
192 lu_object_free(env, top);
193 RETURN(ERR_PTR(result));
198 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
205 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
207 struct lu_site_bkt_data *bkt;
208 struct lu_site *site;
209 struct lu_object *scan;
213 site = o->lo_dev->ld_site;
214 layers = &o->lo_header->loh_layers;
215 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
217 * First call ->loo_object_delete() method to release all resources.
219 cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
220 if (scan->lo_ops->loo_object_delete != NULL)
221 scan->lo_ops->loo_object_delete(env, scan);
225 * Then, splice object layers into stand-alone list, and call
226 * ->loo_object_free() on all layers to free memory. Splice is
227 * necessary, because lu_object_header is freed together with the
230 CFS_INIT_LIST_HEAD(&splice);
231 cfs_list_splice_init(layers, &splice);
232 while (!cfs_list_empty(&splice)) {
234 * Free layers in bottom-to-top order, so that object header
235 * lives as long as possible and ->loo_object_free() methods
236 * can look at its contents.
238 o = container_of0(splice.prev, struct lu_object, lo_linkage);
239 cfs_list_del_init(&o->lo_linkage);
240 LASSERT(o->lo_ops->loo_object_free != NULL);
241 o->lo_ops->loo_object_free(env, o);
244 if (cfs_waitq_active(&bkt->lsb_marche_funebre))
245 cfs_waitq_broadcast(&bkt->lsb_marche_funebre);
249 * Free \a nr objects from the cold end of the site LRU list.
251 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
253 struct lu_object_header *h;
254 struct lu_object_header *temp;
255 struct lu_site_bkt_data *bkt;
265 CFS_INIT_LIST_HEAD(&dispose);
267 * Under LRU list lock, scan LRU list and move unreferenced objects to
268 * the dispose list, removing them from LRU and hash table.
270 start = s->ls_purge_start;
271 bnr = (nr == ~0) ? -1 : nr / CFS_HASH_NBKT(s->ls_obj_hash) + 1;
274 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
278 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
279 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
281 cfs_list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
283 * Objects are sorted in lru order, and "busy"
284 * objects (ones with h->loh_ref > 0) naturally tend to
285 * live near hot end that we scan last. Unfortunately,
286 * sites usually have small (less then ten) number of
287 * busy yet rarely accessed objects (some global
288 * objects, accessed directly through pointers,
289 * bypassing hash table).
290 * Currently algorithm scans them over and over again.
291 * Probably we should move busy objects out of LRU,
292 * or we can live with that.
294 if (cfs_atomic_read(&h->loh_ref) > 0)
297 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
298 LASSERT(bd.bd_bucket == bd2.bd_bucket);
300 cfs_hash_bd_del_locked(s->ls_obj_hash,
302 cfs_list_move(&h->loh_lru, &dispose);
306 if (nr != ~0 && --nr == 0)
309 if (count > 0 && --count == 0)
313 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
316 * Free everything on the dispose list. This is safe against
317 * races due to the reasons described in lu_object_put().
319 while (!cfs_list_empty(&dispose)) {
320 h = container_of0(dispose.next,
321 struct lu_object_header, loh_lru);
322 cfs_list_del_init(&h->loh_lru);
323 lu_object_free(env, lu_object_top(h));
324 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
331 if (nr != 0 && did_sth && start != 0) {
332 start = 0; /* restart from the first bucket */
335 /* race on s->ls_purge_start, but nobody cares */
336 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
340 EXPORT_SYMBOL(lu_site_purge);
345 * Code below has to jump through certain loops to output object description
346 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
347 * composes object description from strings that are parts of _lines_ of
348 * output (i.e., strings that are not terminated by newline). This doesn't fit
349 * very well into libcfs_debug_msg() interface that assumes that each message
350 * supplied to it is a self-contained output line.
352 * To work around this, strings are collected in a temporary buffer
353 * (implemented as a value of lu_cdebug_key key), until terminating newline
354 * character is detected.
362 * XXX overflow is not handled correctly.
367 struct lu_cdebug_data {
371 char lck_area[LU_CDEBUG_LINE];
374 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
375 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
378 * Key, holding temporary buffer. This key is registered very early by
381 struct lu_context_key lu_global_key = {
382 .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
383 .lct_init = lu_global_key_init,
384 .lct_fini = lu_global_key_fini
388 * Printer function emitting messages through libcfs_debug_msg().
390 int lu_cdebug_printer(const struct lu_env *env,
391 void *cookie, const char *format, ...)
393 struct lu_cdebug_print_info *info = cookie;
394 struct lu_cdebug_data *key;
399 va_start(args, format);
401 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
402 LASSERT(key != NULL);
404 used = strlen(key->lck_area);
405 complete = format[strlen(format) - 1] == '\n';
407 * Append new chunk to the buffer.
409 vsnprintf(key->lck_area + used,
410 ARRAY_SIZE(key->lck_area) - used, format, args);
412 if (cfs_cdebug_show(info->lpi_mask, info->lpi_subsys))
413 libcfs_debug_msg(NULL, info->lpi_subsys, info->lpi_mask,
414 (char *)info->lpi_file, info->lpi_fn,
415 info->lpi_line, "%s", key->lck_area);
416 key->lck_area[0] = 0;
421 EXPORT_SYMBOL(lu_cdebug_printer);
424 * Print object header.
426 void lu_object_header_print(const struct lu_env *env, void *cookie,
427 lu_printer_t printer,
428 const struct lu_object_header *hdr)
430 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
431 hdr, hdr->loh_flags, cfs_atomic_read(&hdr->loh_ref),
433 cfs_hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
434 cfs_list_empty((cfs_list_t *)&hdr->loh_lru) ? \
436 hdr->loh_attr & LOHA_EXISTS ? " exist":"");
438 EXPORT_SYMBOL(lu_object_header_print);
441 * Print human readable representation of the \a o to the \a printer.
443 void lu_object_print(const struct lu_env *env, void *cookie,
444 lu_printer_t printer, const struct lu_object *o)
446 static const char ruler[] = "........................................";
447 struct lu_object_header *top;
451 lu_object_header_print(env, cookie, printer, top);
452 (*printer)(env, cookie, "{ \n");
453 cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
454 depth = o->lo_depth + 4;
457 * print `.' \a depth times followed by type name and address
459 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
460 o->lo_dev->ld_type->ldt_name, o);
461 if (o->lo_ops->loo_object_print != NULL)
462 o->lo_ops->loo_object_print(env, cookie, printer, o);
463 (*printer)(env, cookie, "\n");
465 (*printer)(env, cookie, "} header@%p\n", top);
467 EXPORT_SYMBOL(lu_object_print);
470 * Check object consistency.
472 int lu_object_invariant(const struct lu_object *o)
474 struct lu_object_header *top;
477 cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
478 if (o->lo_ops->loo_object_invariant != NULL &&
479 !o->lo_ops->loo_object_invariant(o))
484 EXPORT_SYMBOL(lu_object_invariant);
486 static struct lu_object *htable_lookup(struct lu_site *s,
488 const struct lu_fid *f,
489 cfs_waitlink_t *waiter,
492 struct lu_site_bkt_data *bkt;
493 struct lu_object_header *h;
494 cfs_hlist_node_t *hnode;
495 __u64 ver = cfs_hash_bd_version_get(bd);
501 /* cfs_hash_bd_lookup_intent is a somehow "internal" function
502 * of cfs_hash, but we don't want refcount on object right now */
503 hnode = cfs_hash_bd_lookup_locked(s->ls_obj_hash, bd, (void *)f);
505 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
509 h = container_of0(hnode, struct lu_object_header, loh_hash);
510 if (likely(!lu_object_is_dying(h))) {
511 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
512 return lu_object_top(h);
516 * Lookup found an object being destroyed this object cannot be
517 * returned (to assure that references to dying objects are eventually
518 * drained), and moreover, lookup has to wait until object is freed.
520 cfs_atomic_dec(&h->loh_ref);
522 cfs_waitlink_init(waiter);
523 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
524 cfs_waitq_add(&bkt->lsb_marche_funebre, waiter);
525 cfs_set_current_state(CFS_TASK_UNINT);
526 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
527 return ERR_PTR(-EAGAIN);
531 * Search cache for an object with the fid \a f. If such object is found,
532 * return it. Otherwise, create new object, insert it into cache and return
533 * it. In any case, additional reference is acquired on the returned object.
535 struct lu_object *lu_object_find(const struct lu_env *env,
536 struct lu_device *dev, const struct lu_fid *f,
537 const struct lu_object_conf *conf)
539 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
541 EXPORT_SYMBOL(lu_object_find);
544 * Core logic of lu_object_find*() functions.
546 static struct lu_object *lu_object_find_try(const struct lu_env *env,
547 struct lu_device *dev,
548 const struct lu_fid *f,
549 const struct lu_object_conf *conf,
550 cfs_waitlink_t *waiter)
553 struct lu_object *shadow;
560 * This uses standard index maintenance protocol:
562 * - search index under lock, and return object if found;
563 * - otherwise, unlock index, allocate new object;
564 * - lock index and search again;
565 * - if nothing is found (usual case), insert newly created
567 * - otherwise (race: other thread inserted object), free
568 * object just allocated.
572 * If dying object is found during index search, add @waiter to the
573 * site wait-queue and return ERR_PTR(-EAGAIN).
577 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
578 o = htable_lookup(s, &bd, f, waiter, &version);
579 cfs_hash_bd_unlock(hs, &bd, 1);
584 * Allocate new object. This may result in rather complicated
585 * operations, including fld queries, inode loading, etc.
587 o = lu_object_alloc(env, dev, f, conf);
588 if (unlikely(IS_ERR(o)))
591 LASSERT(lu_fid_eq(lu_object_fid(o), f));
593 cfs_hash_bd_lock(hs, &bd, 1);
595 shadow = htable_lookup(s, &bd, f, waiter, &version);
596 if (likely(shadow == NULL)) {
597 struct lu_site_bkt_data *bkt;
599 bkt = cfs_hash_bd_extra_get(hs, &bd);
600 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
601 cfs_list_add_tail(&o->lo_header->loh_lru, &bkt->lsb_lru);
602 cfs_hash_bd_unlock(hs, &bd, 1);
606 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
607 cfs_hash_bd_unlock(hs, &bd, 1);
608 lu_object_free(env, o);
613 * Much like lu_object_find(), but top level device of object is specifically
614 * \a dev rather than top level device of the site. This interface allows
615 * objects of different "stacking" to be created within the same site.
617 struct lu_object *lu_object_find_at(const struct lu_env *env,
618 struct lu_device *dev,
619 const struct lu_fid *f,
620 const struct lu_object_conf *conf)
622 struct lu_site_bkt_data *bkt;
623 struct lu_object *obj;
627 obj = lu_object_find_try(env, dev, f, conf, &wait);
628 if (obj != ERR_PTR(-EAGAIN))
631 * lu_object_find_try() already added waiter into the
634 cfs_waitq_wait(&wait, CFS_TASK_UNINT);
635 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
636 cfs_waitq_del(&bkt->lsb_marche_funebre, &wait);
639 EXPORT_SYMBOL(lu_object_find_at);
642 * Find object with given fid, and return its slice belonging to given device.
644 struct lu_object *lu_object_find_slice(const struct lu_env *env,
645 struct lu_device *dev,
646 const struct lu_fid *f,
647 const struct lu_object_conf *conf)
649 struct lu_object *top;
650 struct lu_object *obj;
652 top = lu_object_find(env, dev, f, conf);
654 obj = lu_object_locate(top->lo_header, dev->ld_type);
656 lu_object_put(env, top);
661 EXPORT_SYMBOL(lu_object_find_slice);
664 * Global list of all device types.
666 static CFS_LIST_HEAD(lu_device_types);
668 int lu_device_type_init(struct lu_device_type *ldt)
672 CFS_INIT_LIST_HEAD(&ldt->ldt_linkage);
673 result = ldt->ldt_ops->ldto_init(ldt);
675 cfs_list_add(&ldt->ldt_linkage, &lu_device_types);
678 EXPORT_SYMBOL(lu_device_type_init);
680 void lu_device_type_fini(struct lu_device_type *ldt)
682 cfs_list_del_init(&ldt->ldt_linkage);
683 ldt->ldt_ops->ldto_fini(ldt);
685 EXPORT_SYMBOL(lu_device_type_fini);
687 void lu_types_stop(void)
689 struct lu_device_type *ldt;
691 cfs_list_for_each_entry(ldt, &lu_device_types, ldt_linkage) {
692 if (ldt->ldt_device_nr == 0)
693 ldt->ldt_ops->ldto_stop(ldt);
696 EXPORT_SYMBOL(lu_types_stop);
699 * Global list of all sites on this node
701 static CFS_LIST_HEAD(lu_sites);
702 static CFS_DECLARE_MUTEX(lu_sites_guard);
705 * Global environment used by site shrinker.
707 static struct lu_env lu_shrink_env;
709 struct lu_site_print_arg {
710 struct lu_env *lsp_env;
712 lu_printer_t lsp_printer;
716 lu_site_obj_print(cfs_hash_t *hs, cfs_hash_bd_t *bd,
717 cfs_hlist_node_t *hnode, void *data)
719 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
720 struct lu_object_header *h;
722 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
723 if (!cfs_list_empty(&h->loh_layers)) {
724 const struct lu_object *o;
726 o = lu_object_top(h);
727 lu_object_print(arg->lsp_env, arg->lsp_cookie,
728 arg->lsp_printer, o);
730 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
731 arg->lsp_printer, h);
737 * Print all objects in \a s.
739 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
740 lu_printer_t printer)
742 struct lu_site_print_arg arg = {
743 .lsp_env = (struct lu_env *)env,
744 .lsp_cookie = cookie,
745 .lsp_printer = printer,
748 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
750 EXPORT_SYMBOL(lu_site_print);
753 LU_CACHE_PERCENT = 20,
757 * Return desired hash table order.
759 static int lu_htable_order(void)
761 unsigned long cache_size;
765 * Calculate hash table size, assuming that we want reasonable
766 * performance when 20% of total memory is occupied by cache of
769 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
771 cache_size = cfs_num_physpages;
773 #if BITS_PER_LONG == 32
774 /* limit hashtable size for lowmem systems to low RAM */
775 if (cache_size > 1 << (30 - CFS_PAGE_SHIFT))
776 cache_size = 1 << (30 - CFS_PAGE_SHIFT) * 3 / 4;
779 cache_size = cache_size / 100 * LU_CACHE_PERCENT *
780 (CFS_PAGE_SIZE / 1024);
782 for (bits = 1; (1 << bits) < cache_size; ++bits) {
788 static unsigned lu_obj_hop_hash(cfs_hash_t *hs, void *key, unsigned mask)
790 struct lu_fid *fid = (struct lu_fid *)key;
793 hash = (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
794 hash += fid_hash(fid, hs->hs_bkt_bits) << hs->hs_bkt_bits;
798 static void *lu_obj_hop_object(cfs_hlist_node_t *hnode)
800 return cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
803 static void *lu_obj_hop_key(cfs_hlist_node_t *hnode)
805 struct lu_object_header *h;
807 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
811 static int lu_obj_hop_keycmp(void *key, cfs_hlist_node_t *hnode)
813 struct lu_object_header *h;
815 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
816 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
819 static void *lu_obj_hop_get(cfs_hlist_node_t *hnode)
821 struct lu_object_header *h;
823 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
824 cfs_atomic_inc(&h->loh_ref);
828 static void *lu_obj_hop_put_locked(cfs_hlist_node_t *hnode)
830 LBUG(); /* we should never called it */
834 cfs_hash_ops_t lu_site_hash_ops = {
835 .hs_hash = lu_obj_hop_hash,
836 .hs_key = lu_obj_hop_key,
837 .hs_keycmp = lu_obj_hop_keycmp,
838 .hs_object = lu_obj_hop_object,
839 .hs_get = lu_obj_hop_get,
840 .hs_put_locked = lu_obj_hop_put_locked,
844 * Initialize site \a s, with \a d as the top level device.
846 #define LU_SITE_BITS_MIN 10
847 #define LU_SITE_BITS_MAX 23
849 int lu_site_init(struct lu_site *s, struct lu_device *top)
851 struct lu_site_bkt_data *bkt;
857 memset(s, 0, sizeof *s);
858 bits = lu_htable_order();
859 for (bits = min(max(LU_SITE_BITS_MIN, bits), LU_SITE_BITS_MAX);
860 bits >= LU_SITE_BITS_MIN; bits--) {
861 s->ls_obj_hash = cfs_hash_create("lu_site", bits,
862 bits, bits - LU_SITE_BITS_MIN,
865 CFS_HASH_SPIN_BKTLOCK |
866 CFS_HASH_NO_ITEMREF |
868 CFS_HASH_ASSERT_EMPTY);
869 if (s->ls_obj_hash != NULL)
873 if (s->ls_obj_hash == NULL) {
874 CERROR("failed to create lu_site hash with bits: %d\n", bits);
878 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
879 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
880 CFS_INIT_LIST_HEAD(&bkt->lsb_lru);
881 cfs_waitq_init(&bkt->lsb_marche_funebre);
884 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
885 if (s->ls_stats == NULL) {
886 cfs_hash_putref(s->ls_obj_hash);
887 s->ls_obj_hash = NULL;
891 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
892 0, "created", "created");
893 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
894 0, "cache_hit", "cache_hit");
895 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
896 0, "cache_miss", "cache_miss");
897 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
898 0, "cache_race", "cache_race");
899 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
900 0, "cache_death_race", "cache_death_race");
901 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
902 0, "lru_purged", "lru_purged");
904 CFS_INIT_LIST_HEAD(&s->ls_linkage);
908 lu_ref_add(&top->ld_reference, "site-top", s);
912 EXPORT_SYMBOL(lu_site_init);
915 * Finalize \a s and release its resources.
917 void lu_site_fini(struct lu_site *s)
919 cfs_down(&lu_sites_guard);
920 cfs_list_del_init(&s->ls_linkage);
921 cfs_up(&lu_sites_guard);
923 if (s->ls_obj_hash != NULL) {
924 cfs_hash_putref(s->ls_obj_hash);
925 s->ls_obj_hash = NULL;
928 if (s->ls_top_dev != NULL) {
929 s->ls_top_dev->ld_site = NULL;
930 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
931 lu_device_put(s->ls_top_dev);
932 s->ls_top_dev = NULL;
935 if (s->ls_stats != NULL)
936 lprocfs_free_stats(&s->ls_stats);
938 EXPORT_SYMBOL(lu_site_fini);
941 * Called when initialization of stack for this site is completed.
943 int lu_site_init_finish(struct lu_site *s)
946 cfs_down(&lu_sites_guard);
947 result = lu_context_refill(&lu_shrink_env.le_ctx);
949 cfs_list_add(&s->ls_linkage, &lu_sites);
950 cfs_up(&lu_sites_guard);
953 EXPORT_SYMBOL(lu_site_init_finish);
956 * Acquire additional reference on device \a d
958 void lu_device_get(struct lu_device *d)
960 cfs_atomic_inc(&d->ld_ref);
962 EXPORT_SYMBOL(lu_device_get);
965 * Release reference on device \a d.
967 void lu_device_put(struct lu_device *d)
969 LASSERT(cfs_atomic_read(&d->ld_ref) > 0);
970 cfs_atomic_dec(&d->ld_ref);
972 EXPORT_SYMBOL(lu_device_put);
975 * Initialize device \a d of type \a t.
977 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
979 if (t->ldt_device_nr++ == 0 && t->ldt_ops->ldto_start != NULL)
980 t->ldt_ops->ldto_start(t);
981 memset(d, 0, sizeof *d);
982 cfs_atomic_set(&d->ld_ref, 0);
984 lu_ref_init(&d->ld_reference);
987 EXPORT_SYMBOL(lu_device_init);
990 * Finalize device \a d.
992 void lu_device_fini(struct lu_device *d)
994 struct lu_device_type *t;
997 if (d->ld_obd != NULL) {
998 d->ld_obd->obd_lu_dev = NULL;
1002 lu_ref_fini(&d->ld_reference);
1003 LASSERTF(cfs_atomic_read(&d->ld_ref) == 0,
1004 "Refcount is %u\n", cfs_atomic_read(&d->ld_ref));
1005 LASSERT(t->ldt_device_nr > 0);
1006 if (--t->ldt_device_nr == 0 && t->ldt_ops->ldto_stop != NULL)
1007 t->ldt_ops->ldto_stop(t);
1009 EXPORT_SYMBOL(lu_device_fini);
1012 * Initialize object \a o that is part of compound object \a h and was created
1015 int lu_object_init(struct lu_object *o,
1016 struct lu_object_header *h, struct lu_device *d)
1018 memset(o, 0, sizeof *o);
1022 o->lo_dev_ref = lu_ref_add(&d->ld_reference, "lu_object", o);
1023 CFS_INIT_LIST_HEAD(&o->lo_linkage);
1026 EXPORT_SYMBOL(lu_object_init);
1029 * Finalize object and release its resources.
1031 void lu_object_fini(struct lu_object *o)
1033 struct lu_device *dev = o->lo_dev;
1035 LASSERT(cfs_list_empty(&o->lo_linkage));
1038 lu_ref_del_at(&dev->ld_reference,
1039 o->lo_dev_ref , "lu_object", o);
1044 EXPORT_SYMBOL(lu_object_fini);
1047 * Add object \a o as first layer of compound object \a h
1049 * This is typically called by the ->ldo_object_alloc() method of top-level
1052 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1054 cfs_list_move(&o->lo_linkage, &h->loh_layers);
1056 EXPORT_SYMBOL(lu_object_add_top);
1059 * Add object \a o as a layer of compound object, going after \a before.
1061 * This is typically called by the ->ldo_object_alloc() method of \a
1064 void lu_object_add(struct lu_object *before, struct lu_object *o)
1066 cfs_list_move(&o->lo_linkage, &before->lo_linkage);
1068 EXPORT_SYMBOL(lu_object_add);
1071 * Initialize compound object.
1073 int lu_object_header_init(struct lu_object_header *h)
1075 memset(h, 0, sizeof *h);
1076 cfs_atomic_set(&h->loh_ref, 1);
1077 CFS_INIT_HLIST_NODE(&h->loh_hash);
1078 CFS_INIT_LIST_HEAD(&h->loh_lru);
1079 CFS_INIT_LIST_HEAD(&h->loh_layers);
1080 lu_ref_init(&h->loh_reference);
1083 EXPORT_SYMBOL(lu_object_header_init);
1086 * Finalize compound object.
1088 void lu_object_header_fini(struct lu_object_header *h)
1090 LASSERT(cfs_list_empty(&h->loh_layers));
1091 LASSERT(cfs_list_empty(&h->loh_lru));
1092 LASSERT(cfs_hlist_unhashed(&h->loh_hash));
1093 lu_ref_fini(&h->loh_reference);
1095 EXPORT_SYMBOL(lu_object_header_fini);
1098 * Given a compound object, find its slice, corresponding to the device type
1101 struct lu_object *lu_object_locate(struct lu_object_header *h,
1102 const struct lu_device_type *dtype)
1104 struct lu_object *o;
1106 cfs_list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1107 if (o->lo_dev->ld_type == dtype)
1112 EXPORT_SYMBOL(lu_object_locate);
1117 * Finalize and free devices in the device stack.
1119 * Finalize device stack by purging object cache, and calling
1120 * lu_device_type_operations::ldto_device_fini() and
1121 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1123 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1125 struct lu_site *site = top->ld_site;
1126 struct lu_device *scan;
1127 struct lu_device *next;
1129 lu_site_purge(env, site, ~0);
1130 for (scan = top; scan != NULL; scan = next) {
1131 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1132 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1133 lu_device_put(scan);
1137 lu_site_purge(env, site, ~0);
1139 if (!cfs_hash_is_empty(site->ls_obj_hash)) {
1141 * Uh-oh, objects still exist.
1143 static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
1145 lu_site_print(env, site, &cookie, lu_cdebug_printer);
1148 for (scan = top; scan != NULL; scan = next) {
1149 const struct lu_device_type *ldt = scan->ld_type;
1150 struct obd_type *type;
1152 next = ldt->ldt_ops->ldto_device_free(env, scan);
1153 type = ldt->ldt_obd_type;
1156 class_put_type(type);
1160 EXPORT_SYMBOL(lu_stack_fini);
1164 * Maximal number of tld slots.
1166 LU_CONTEXT_KEY_NR = 32
1169 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1171 static cfs_spinlock_t lu_keys_guard = CFS_SPIN_LOCK_UNLOCKED;
1174 * Global counter incremented whenever key is registered, unregistered,
1175 * revived or quiesced. This is used to void unnecessary calls to
1176 * lu_context_refill(). No locking is provided, as initialization and shutdown
1177 * are supposed to be externally serialized.
1179 static unsigned key_set_version = 0;
1184 int lu_context_key_register(struct lu_context_key *key)
1189 LASSERT(key->lct_init != NULL);
1190 LASSERT(key->lct_fini != NULL);
1191 LASSERT(key->lct_tags != 0);
1192 LASSERT(key->lct_owner != NULL);
1195 cfs_spin_lock(&lu_keys_guard);
1196 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1197 if (lu_keys[i] == NULL) {
1199 cfs_atomic_set(&key->lct_used, 1);
1201 lu_ref_init(&key->lct_reference);
1207 cfs_spin_unlock(&lu_keys_guard);
1210 EXPORT_SYMBOL(lu_context_key_register);
1212 static void key_fini(struct lu_context *ctx, int index)
1214 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1215 struct lu_context_key *key;
1217 key = lu_keys[index];
1218 LASSERT(key != NULL);
1219 LASSERT(key->lct_fini != NULL);
1220 LASSERT(cfs_atomic_read(&key->lct_used) > 1);
1222 key->lct_fini(ctx, key, ctx->lc_value[index]);
1223 lu_ref_del(&key->lct_reference, "ctx", ctx);
1224 cfs_atomic_dec(&key->lct_used);
1225 LASSERT(key->lct_owner != NULL);
1226 if (!(ctx->lc_tags & LCT_NOREF)) {
1227 LASSERT(cfs_module_refcount(key->lct_owner) > 0);
1228 cfs_module_put(key->lct_owner);
1230 ctx->lc_value[index] = NULL;
1237 void lu_context_key_degister(struct lu_context_key *key)
1239 LASSERT(cfs_atomic_read(&key->lct_used) >= 1);
1240 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1242 lu_context_key_quiesce(key);
1245 cfs_spin_lock(&lu_keys_guard);
1246 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1247 if (lu_keys[key->lct_index]) {
1248 lu_keys[key->lct_index] = NULL;
1249 lu_ref_fini(&key->lct_reference);
1251 cfs_spin_unlock(&lu_keys_guard);
1253 LASSERTF(cfs_atomic_read(&key->lct_used) == 1,
1254 "key has instances: %d\n",
1255 cfs_atomic_read(&key->lct_used));
1257 EXPORT_SYMBOL(lu_context_key_degister);
1260 * Register a number of keys. This has to be called after all keys have been
1261 * initialized by a call to LU_CONTEXT_KEY_INIT().
1263 int lu_context_key_register_many(struct lu_context_key *k, ...)
1265 struct lu_context_key *key = k;
1271 result = lu_context_key_register(key);
1274 key = va_arg(args, struct lu_context_key *);
1275 } while (key != NULL);
1281 lu_context_key_degister(k);
1282 k = va_arg(args, struct lu_context_key *);
1289 EXPORT_SYMBOL(lu_context_key_register_many);
1292 * De-register a number of keys. This is a dual to
1293 * lu_context_key_register_many().
1295 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1301 lu_context_key_degister(k);
1302 k = va_arg(args, struct lu_context_key*);
1303 } while (k != NULL);
1306 EXPORT_SYMBOL(lu_context_key_degister_many);
1309 * Revive a number of keys.
1311 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1317 lu_context_key_revive(k);
1318 k = va_arg(args, struct lu_context_key*);
1319 } while (k != NULL);
1322 EXPORT_SYMBOL(lu_context_key_revive_many);
1325 * Quiescent a number of keys.
1327 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1333 lu_context_key_quiesce(k);
1334 k = va_arg(args, struct lu_context_key*);
1335 } while (k != NULL);
1338 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1341 * Return value associated with key \a key in context \a ctx.
1343 void *lu_context_key_get(const struct lu_context *ctx,
1344 const struct lu_context_key *key)
1346 LINVRNT(ctx->lc_state == LCS_ENTERED);
1347 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1348 LASSERT(lu_keys[key->lct_index] == key);
1349 return ctx->lc_value[key->lct_index];
1351 EXPORT_SYMBOL(lu_context_key_get);
1354 * List of remembered contexts. XXX document me.
1356 static CFS_LIST_HEAD(lu_context_remembered);
1359 * Destroy \a key in all remembered contexts. This is used to destroy key
1360 * values in "shared" contexts (like service threads), when a module owning
1361 * the key is about to be unloaded.
1363 void lu_context_key_quiesce(struct lu_context_key *key)
1365 struct lu_context *ctx;
1366 extern unsigned cl_env_cache_purge(unsigned nr);
1368 if (!(key->lct_tags & LCT_QUIESCENT)) {
1370 * XXX layering violation.
1372 cl_env_cache_purge(~0);
1373 key->lct_tags |= LCT_QUIESCENT;
1375 * XXX memory barrier has to go here.
1377 cfs_spin_lock(&lu_keys_guard);
1378 cfs_list_for_each_entry(ctx, &lu_context_remembered,
1380 key_fini(ctx, key->lct_index);
1381 cfs_spin_unlock(&lu_keys_guard);
1385 EXPORT_SYMBOL(lu_context_key_quiesce);
1387 void lu_context_key_revive(struct lu_context_key *key)
1389 key->lct_tags &= ~LCT_QUIESCENT;
1392 EXPORT_SYMBOL(lu_context_key_revive);
1394 static void keys_fini(struct lu_context *ctx)
1398 cfs_spin_lock(&lu_keys_guard);
1399 if (ctx->lc_value != NULL) {
1400 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1402 OBD_FREE(ctx->lc_value,
1403 ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1404 ctx->lc_value = NULL;
1406 cfs_spin_unlock(&lu_keys_guard);
1409 static int keys_fill(struct lu_context *ctx)
1413 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1414 struct lu_context_key *key;
1417 if (ctx->lc_value[i] == NULL && key != NULL &&
1418 (key->lct_tags & ctx->lc_tags) &&
1420 * Don't create values for a LCT_QUIESCENT key, as this
1421 * will pin module owning a key.
1423 !(key->lct_tags & LCT_QUIESCENT)) {
1426 LINVRNT(key->lct_init != NULL);
1427 LINVRNT(key->lct_index == i);
1429 value = key->lct_init(ctx, key);
1430 if (unlikely(IS_ERR(value)))
1431 return PTR_ERR(value);
1433 LASSERT(key->lct_owner != NULL);
1434 if (!(ctx->lc_tags & LCT_NOREF))
1435 cfs_try_module_get(key->lct_owner);
1436 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1437 cfs_atomic_inc(&key->lct_used);
1439 * This is the only place in the code, where an
1440 * element of ctx->lc_value[] array is set to non-NULL
1443 ctx->lc_value[i] = value;
1444 if (key->lct_exit != NULL)
1445 ctx->lc_tags |= LCT_HAS_EXIT;
1447 ctx->lc_version = key_set_version;
1452 static int keys_init(struct lu_context *ctx)
1456 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1457 if (likely(ctx->lc_value != NULL))
1458 result = keys_fill(ctx);
1468 * Initialize context data-structure. Create values for all keys.
1470 int lu_context_init(struct lu_context *ctx, __u32 tags)
1472 memset(ctx, 0, sizeof *ctx);
1473 ctx->lc_state = LCS_INITIALIZED;
1474 ctx->lc_tags = tags;
1475 if (tags & LCT_REMEMBER) {
1476 cfs_spin_lock(&lu_keys_guard);
1477 cfs_list_add(&ctx->lc_remember, &lu_context_remembered);
1478 cfs_spin_unlock(&lu_keys_guard);
1480 CFS_INIT_LIST_HEAD(&ctx->lc_remember);
1481 return keys_init(ctx);
1483 EXPORT_SYMBOL(lu_context_init);
1486 * Finalize context data-structure. Destroy key values.
1488 void lu_context_fini(struct lu_context *ctx)
1490 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1491 ctx->lc_state = LCS_FINALIZED;
1493 cfs_spin_lock(&lu_keys_guard);
1494 cfs_list_del_init(&ctx->lc_remember);
1495 cfs_spin_unlock(&lu_keys_guard);
1497 EXPORT_SYMBOL(lu_context_fini);
1500 * Called before entering context.
1502 void lu_context_enter(struct lu_context *ctx)
1504 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1505 ctx->lc_state = LCS_ENTERED;
1507 EXPORT_SYMBOL(lu_context_enter);
1510 * Called after exiting from \a ctx
1512 void lu_context_exit(struct lu_context *ctx)
1516 LINVRNT(ctx->lc_state == LCS_ENTERED);
1517 ctx->lc_state = LCS_LEFT;
1518 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1519 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1520 if (ctx->lc_value[i] != NULL) {
1521 struct lu_context_key *key;
1524 LASSERT(key != NULL);
1525 if (key->lct_exit != NULL)
1527 key, ctx->lc_value[i]);
1532 EXPORT_SYMBOL(lu_context_exit);
1535 * Allocate for context all missing keys that were registered after context
1538 int lu_context_refill(struct lu_context *ctx)
1540 LINVRNT(ctx->lc_value != NULL);
1541 return ctx->lc_version == key_set_version ? 0 : keys_fill(ctx);
1543 EXPORT_SYMBOL(lu_context_refill);
1545 int lu_env_init(struct lu_env *env, __u32 tags)
1550 result = lu_context_init(&env->le_ctx, tags);
1551 if (likely(result == 0))
1552 lu_context_enter(&env->le_ctx);
1555 EXPORT_SYMBOL(lu_env_init);
1557 void lu_env_fini(struct lu_env *env)
1559 lu_context_exit(&env->le_ctx);
1560 lu_context_fini(&env->le_ctx);
1563 EXPORT_SYMBOL(lu_env_fini);
1565 int lu_env_refill(struct lu_env *env)
1569 result = lu_context_refill(&env->le_ctx);
1570 if (result == 0 && env->le_ses != NULL)
1571 result = lu_context_refill(env->le_ses);
1574 EXPORT_SYMBOL(lu_env_refill);
1576 static struct cfs_shrinker *lu_site_shrinker = NULL;
1578 struct lu_site_stats_result {
1579 unsigned lss_populated;
1580 unsigned lss_max_search;
1583 cfs_hash_bd_t lss_bd;
1586 static int lu_site_stats_get(cfs_hash_t *hs, cfs_hash_bd_t *bd,
1587 cfs_hlist_node_t *hnode, void *data)
1589 struct lu_site_stats_result *sa = data;
1590 struct lu_object_header *h;
1593 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
1594 if (cfs_atomic_read(&h->loh_ref) > 0)
1597 if (sa->lss_bd.bd_bucket == NULL ||
1598 cfs_hash_bd_compare(&sa->lss_bd, bd) != 0) {
1599 if (sa->lss_max_search < cfs_hash_bd_depmax_get(bd))
1600 sa->lss_max_search = cfs_hash_bd_depmax_get(bd);
1601 sa->lss_populated++;
1608 static int lu_cache_shrink(int nr, unsigned int gfp_mask)
1610 struct lu_site_stats_result stats;
1612 struct lu_site *tmp;
1615 CFS_LIST_HEAD(splice);
1618 if (!(gfp_mask & __GFP_FS))
1620 CDEBUG(D_INODE, "Shrink %d objects\n", nr);
1623 cfs_down(&lu_sites_guard);
1624 cfs_list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1626 remain = lu_site_purge(&lu_shrink_env, s, remain);
1628 * Move just shrunk site to the tail of site list to
1629 * assure shrinking fairness.
1631 cfs_list_move_tail(&s->ls_linkage, &splice);
1634 memset(&stats, 0, sizeof(stats));
1635 cfs_hash_for_each(s->ls_obj_hash, lu_site_stats_get, &stats);
1636 cached += stats.lss_total - stats.lss_busy;
1637 if (nr && remain <= 0)
1640 cfs_list_splice(&splice, lu_sites.prev);
1641 cfs_up(&lu_sites_guard);
1643 cached = (cached / 100) * sysctl_vfs_cache_pressure;
1645 CDEBUG(D_INODE, "%d objects cached\n", cached);
1654 * Environment to be used in debugger, contains all tags.
1656 struct lu_env lu_debugging_env;
1659 * Debugging printer function using printk().
1661 int lu_printk_printer(const struct lu_env *env,
1662 void *unused, const char *format, ...)
1666 va_start(args, format);
1667 vprintk(format, args);
1672 void lu_debugging_setup(void)
1674 lu_env_init(&lu_debugging_env, ~0);
1677 void lu_context_keys_dump(void)
1681 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1682 struct lu_context_key *key;
1686 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
1687 i, key, key->lct_tags,
1688 key->lct_init, key->lct_fini, key->lct_exit,
1689 key->lct_index, cfs_atomic_read(&key->lct_used),
1690 key->lct_owner ? key->lct_owner->name : "",
1692 lu_ref_print(&key->lct_reference);
1696 EXPORT_SYMBOL(lu_context_keys_dump);
1697 #else /* !__KERNEL__ */
1698 static int lu_cache_shrink(int nr, unsigned int gfp_mask)
1702 #endif /* __KERNEL__ */
1704 int cl_global_init(void);
1705 void cl_global_fini(void);
1706 int lu_ref_global_init(void);
1707 void lu_ref_global_fini(void);
1709 int dt_global_init(void);
1710 void dt_global_fini(void);
1712 int llo_global_init(void);
1713 void llo_global_fini(void);
1716 * Initialization of global lu_* data.
1718 int lu_global_init(void)
1722 CDEBUG(D_CONSOLE, "Lustre LU module (%p).\n", &lu_keys);
1724 result = lu_ref_global_init();
1728 LU_CONTEXT_KEY_INIT(&lu_global_key);
1729 result = lu_context_key_register(&lu_global_key);
1733 * At this level, we don't know what tags are needed, so allocate them
1734 * conservatively. This should not be too bad, because this
1735 * environment is global.
1737 cfs_down(&lu_sites_guard);
1738 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
1739 cfs_up(&lu_sites_guard);
1744 * seeks estimation: 3 seeks to read a record from oi, one to read
1745 * inode, one for ea. Unfortunately setting this high value results in
1746 * lu_object/inode cache consuming all the memory.
1748 lu_site_shrinker = cfs_set_shrinker(CFS_DEFAULT_SEEKS, lu_cache_shrink);
1749 if (lu_site_shrinker == NULL)
1752 result = lu_time_global_init();
1757 result = dt_global_init();
1761 result = llo_global_init();
1765 result = cl_global_init();
1772 * Dual to lu_global_init().
1774 void lu_global_fini(void)
1781 lu_time_global_fini();
1782 if (lu_site_shrinker != NULL) {
1783 cfs_remove_shrinker(lu_site_shrinker);
1784 lu_site_shrinker = NULL;
1787 lu_context_key_degister(&lu_global_key);
1790 * Tear shrinker environment down _after_ de-registering
1791 * lu_global_key, because the latter has a value in the former.
1793 cfs_down(&lu_sites_guard);
1794 lu_env_fini(&lu_shrink_env);
1795 cfs_up(&lu_sites_guard);
1797 lu_ref_global_fini();
1800 struct lu_buf LU_BUF_NULL = {
1804 EXPORT_SYMBOL(LU_BUF_NULL);
1806 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
1809 struct lprocfs_counter ret;
1811 lprocfs_stats_collect(stats, idx, &ret);
1812 return (__u32)ret.lc_count;
1819 * Output site statistical counters into a buffer. Suitable for
1820 * lprocfs_rd_*()-style functions.
1822 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
1824 struct lu_site_stats_result stats;
1826 memset(&stats, 0, sizeof(stats));
1827 cfs_hash_for_each(s->ls_obj_hash, lu_site_stats_get, &stats);
1829 return snprintf(page, count, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
1832 stats.lss_populated,
1833 CFS_HASH_NHLIST(s->ls_obj_hash),
1834 stats.lss_max_search,
1835 ls_stats_read(s->ls_stats, LU_SS_CREATED),
1836 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
1837 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
1838 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
1839 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
1840 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
1842 EXPORT_SYMBOL(lu_site_stats_print);
1844 const char *lu_time_names[LU_TIME_NR] = {
1845 [LU_TIME_FIND_LOOKUP] = "find_lookup",
1846 [LU_TIME_FIND_ALLOC] = "find_alloc",
1847 [LU_TIME_FIND_INSERT] = "find_insert"
1849 EXPORT_SYMBOL(lu_time_names);
1852 * Helper function to initialize a number of kmem slab caches at once.
1854 int lu_kmem_init(struct lu_kmem_descr *caches)
1858 for (result = 0; caches->ckd_cache != NULL; ++caches) {
1859 *caches->ckd_cache = cfs_mem_cache_create(caches->ckd_name,
1862 if (*caches->ckd_cache == NULL) {
1869 EXPORT_SYMBOL(lu_kmem_init);
1872 * Helper function to finalize a number of kmem slab cached at once. Dual to
1875 void lu_kmem_fini(struct lu_kmem_descr *caches)
1879 for (; caches->ckd_cache != NULL; ++caches) {
1880 if (*caches->ckd_cache != NULL) {
1881 rc = cfs_mem_cache_destroy(*caches->ckd_cache);
1882 LASSERTF(rc == 0, "couldn't destroy %s slab\n",
1884 *caches->ckd_cache = NULL;
1888 EXPORT_SYMBOL(lu_kmem_fini);