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, 2013, Intel Corporation.
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>
61 #include <libcfs/list.h>
64 LU_CACHE_PERCENT_MAX = 50,
65 LU_CACHE_PERCENT_DEFAULT = 20
68 #define LU_CACHE_NR_MAX_ADJUST 128
69 #define LU_CACHE_NR_UNLIMITED -1
70 #define LU_CACHE_NR_DEFAULT LU_CACHE_NR_UNLIMITED
71 #define LU_CACHE_NR_LDISKFS_LIMIT LU_CACHE_NR_UNLIMITED
72 /** This is set to roughly (20 * OSS_NTHRS_MAX) to prevent thrashing */
73 #define LU_CACHE_NR_ZFS_LIMIT 10240
75 #define LU_SITE_BITS_MIN 12
76 #define LU_SITE_BITS_MAX 24
78 * total 256 buckets, we don't want too many buckets because:
79 * - consume too much memory
80 * - avoid unbalanced LRU list
82 #define LU_SITE_BKT_BITS 8
85 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
86 CFS_MODULE_PARM(lu_cache_percent, "i", int, 0644,
87 "Percentage of memory to be used as lu_object cache");
89 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
90 CFS_MODULE_PARM(lu_cache_nr, "l", long, 0644,
91 "Maximum number of objects in lu_object cache");
93 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
95 extern spinlock_t obd_types_lock;
98 * Decrease reference counter on object. If last reference is freed, return
99 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
100 * case, free object immediately.
102 void lu_object_put(const struct lu_env *env, struct lu_object *o)
104 struct lu_site_bkt_data *bkt;
105 struct lu_object_header *top;
106 struct lu_site *site;
107 struct lu_object *orig;
109 const struct lu_fid *fid;
112 site = o->lo_dev->ld_site;
116 * till we have full fids-on-OST implemented anonymous objects
117 * are possible in OSP. such an object isn't listed in the site
118 * so we should not remove it from the site.
120 fid = lu_object_fid(o);
121 if (fid_is_zero(fid)) {
122 LASSERT(top->loh_hash.next == NULL
123 && top->loh_hash.pprev == NULL);
124 LASSERT(cfs_list_empty(&top->loh_lru));
125 if (!cfs_atomic_dec_and_test(&top->loh_ref))
127 cfs_list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
128 if (o->lo_ops->loo_object_release != NULL)
129 o->lo_ops->loo_object_release(env, o);
131 lu_object_free(env, orig);
135 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
136 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
138 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
139 if (lu_object_is_dying(top)) {
142 * somebody may be waiting for this, currently only
143 * used for cl_object, see cl_object_put_last().
145 wake_up_all(&bkt->lsb_marche_funebre);
150 LASSERT(bkt->lsb_busy > 0);
153 * When last reference is released, iterate over object
154 * layers, and notify them that object is no longer busy.
156 cfs_list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
157 if (o->lo_ops->loo_object_release != NULL)
158 o->lo_ops->loo_object_release(env, o);
161 if (!lu_object_is_dying(top)) {
162 LASSERT(cfs_list_empty(&top->loh_lru));
163 cfs_list_add_tail(&top->loh_lru, &bkt->lsb_lru);
164 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
169 * If object is dying (will not be cached), removed it
170 * from hash table and LRU.
172 * This is done with hash table and LRU lists locked. As the only
173 * way to acquire first reference to previously unreferenced
174 * object is through hash-table lookup (lu_object_find()),
175 * or LRU scanning (lu_site_purge()), that are done under hash-table
176 * and LRU lock, no race with concurrent object lookup is possible
177 * and we can safely destroy object below.
179 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
180 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
181 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
183 * Object was already removed from hash and lru above, can
186 lu_object_free(env, orig);
188 EXPORT_SYMBOL(lu_object_put);
191 * Put object and don't keep in cache. This is temporary solution for
192 * multi-site objects when its layering is not constant.
194 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
196 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
197 return lu_object_put(env, o);
199 EXPORT_SYMBOL(lu_object_put_nocache);
202 * Kill the object and take it out of LRU cache.
203 * Currently used by client code for layout change.
205 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
207 struct lu_object_header *top;
210 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
211 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
212 cfs_hash_t *obj_hash = o->lo_dev->ld_site->ls_obj_hash;
215 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
216 cfs_list_del_init(&top->loh_lru);
217 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
218 cfs_hash_bd_unlock(obj_hash, &bd, 1);
221 EXPORT_SYMBOL(lu_object_unhash);
224 * Allocate new object.
226 * This follows object creation protocol, described in the comment within
227 * struct lu_device_operations definition.
229 static struct lu_object *lu_object_alloc(const struct lu_env *env,
230 struct lu_device *dev,
231 const struct lu_fid *f,
232 const struct lu_object_conf *conf)
234 struct lu_object *scan;
235 struct lu_object *top;
237 unsigned int init_mask = 0;
238 unsigned int init_flag;
244 * Create top-level object slice. This will also create
247 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
249 RETURN(ERR_PTR(-ENOMEM));
253 * This is the only place where object fid is assigned. It's constant
256 top->lo_header->loh_fid = *f;
257 layers = &top->lo_header->loh_layers;
261 * Call ->loo_object_init() repeatedly, until no more new
262 * object slices are created.
266 cfs_list_for_each_entry(scan, layers, lo_linkage) {
267 if (init_mask & init_flag)
270 scan->lo_header = top->lo_header;
271 result = scan->lo_ops->loo_object_init(env, scan, conf);
273 lu_object_free(env, top);
274 RETURN(ERR_PTR(result));
276 init_mask |= init_flag;
282 cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
283 if (scan->lo_ops->loo_object_start != NULL) {
284 result = scan->lo_ops->loo_object_start(env, scan);
286 lu_object_free(env, top);
287 RETURN(ERR_PTR(result));
292 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
299 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
301 struct lu_site_bkt_data *bkt;
302 struct lu_site *site;
303 struct lu_object *scan;
307 site = o->lo_dev->ld_site;
308 layers = &o->lo_header->loh_layers;
309 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
311 * First call ->loo_object_delete() method to release all resources.
313 cfs_list_for_each_entry_reverse(scan, layers, lo_linkage) {
314 if (scan->lo_ops->loo_object_delete != NULL)
315 scan->lo_ops->loo_object_delete(env, scan);
319 * Then, splice object layers into stand-alone list, and call
320 * ->loo_object_free() on all layers to free memory. Splice is
321 * necessary, because lu_object_header is freed together with the
324 CFS_INIT_LIST_HEAD(&splice);
325 cfs_list_splice_init(layers, &splice);
326 while (!cfs_list_empty(&splice)) {
328 * Free layers in bottom-to-top order, so that object header
329 * lives as long as possible and ->loo_object_free() methods
330 * can look at its contents.
332 o = container_of0(splice.prev, struct lu_object, lo_linkage);
333 cfs_list_del_init(&o->lo_linkage);
334 LASSERT(o->lo_ops->loo_object_free != NULL);
335 o->lo_ops->loo_object_free(env, o);
338 if (waitqueue_active(&bkt->lsb_marche_funebre))
339 wake_up_all(&bkt->lsb_marche_funebre);
343 * Free \a nr objects from the cold end of the site LRU list.
345 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
347 struct lu_object_header *h;
348 struct lu_object_header *temp;
349 struct lu_site_bkt_data *bkt;
359 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
362 CFS_INIT_LIST_HEAD(&dispose);
364 * Under LRU list lock, scan LRU list and move unreferenced objects to
365 * the dispose list, removing them from LRU and hash table.
367 start = s->ls_purge_start;
368 bnr = (nr == ~0) ? -1 : nr / CFS_HASH_NBKT(s->ls_obj_hash) + 1;
371 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
375 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
376 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
378 cfs_list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
379 LASSERT(cfs_atomic_read(&h->loh_ref) == 0);
381 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
382 LASSERT(bd.bd_bucket == bd2.bd_bucket);
384 cfs_hash_bd_del_locked(s->ls_obj_hash,
386 cfs_list_move(&h->loh_lru, &dispose);
390 if (nr != ~0 && --nr == 0)
393 if (count > 0 && --count == 0)
397 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
400 * Free everything on the dispose list. This is safe against
401 * races due to the reasons described in lu_object_put().
403 while (!cfs_list_empty(&dispose)) {
404 h = container_of0(dispose.next,
405 struct lu_object_header, loh_lru);
406 cfs_list_del_init(&h->loh_lru);
407 lu_object_free(env, lu_object_top(h));
408 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
415 if (nr != 0 && did_sth && start != 0) {
416 start = 0; /* restart from the first bucket */
419 /* race on s->ls_purge_start, but nobody cares */
420 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
424 EXPORT_SYMBOL(lu_site_purge);
429 * Code below has to jump through certain loops to output object description
430 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
431 * composes object description from strings that are parts of _lines_ of
432 * output (i.e., strings that are not terminated by newline). This doesn't fit
433 * very well into libcfs_debug_msg() interface that assumes that each message
434 * supplied to it is a self-contained output line.
436 * To work around this, strings are collected in a temporary buffer
437 * (implemented as a value of lu_cdebug_key key), until terminating newline
438 * character is detected.
446 * XXX overflow is not handled correctly.
451 struct lu_cdebug_data {
455 char lck_area[LU_CDEBUG_LINE];
458 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
459 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
462 * Key, holding temporary buffer. This key is registered very early by
465 struct lu_context_key lu_global_key = {
466 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
467 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
468 .lct_init = lu_global_key_init,
469 .lct_fini = lu_global_key_fini
473 * Printer function emitting messages through libcfs_debug_msg().
475 int lu_cdebug_printer(const struct lu_env *env,
476 void *cookie, const char *format, ...)
478 struct libcfs_debug_msg_data *msgdata = cookie;
479 struct lu_cdebug_data *key;
484 va_start(args, format);
486 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
487 LASSERT(key != NULL);
489 used = strlen(key->lck_area);
490 complete = format[strlen(format) - 1] == '\n';
492 * Append new chunk to the buffer.
494 vsnprintf(key->lck_area + used,
495 ARRAY_SIZE(key->lck_area) - used, format, args);
497 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
498 libcfs_debug_msg(msgdata, "%s", key->lck_area);
499 key->lck_area[0] = 0;
504 EXPORT_SYMBOL(lu_cdebug_printer);
507 * Print object header.
509 void lu_object_header_print(const struct lu_env *env, void *cookie,
510 lu_printer_t printer,
511 const struct lu_object_header *hdr)
513 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
514 hdr, hdr->loh_flags, cfs_atomic_read(&hdr->loh_ref),
516 cfs_hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
517 cfs_list_empty((cfs_list_t *)&hdr->loh_lru) ? \
519 hdr->loh_attr & LOHA_EXISTS ? " exist":"");
521 EXPORT_SYMBOL(lu_object_header_print);
524 * Print human readable representation of the \a o to the \a printer.
526 void lu_object_print(const struct lu_env *env, void *cookie,
527 lu_printer_t printer, const struct lu_object *o)
529 static const char ruler[] = "........................................";
530 struct lu_object_header *top;
534 lu_object_header_print(env, cookie, printer, top);
535 (*printer)(env, cookie, "{\n");
537 cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
539 * print `.' \a depth times followed by type name and address
541 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
542 o->lo_dev->ld_type->ldt_name, o);
544 if (o->lo_ops->loo_object_print != NULL)
545 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
547 (*printer)(env, cookie, "\n");
550 (*printer)(env, cookie, "} header@%p\n", top);
552 EXPORT_SYMBOL(lu_object_print);
555 * Check object consistency.
557 int lu_object_invariant(const struct lu_object *o)
559 struct lu_object_header *top;
562 cfs_list_for_each_entry(o, &top->loh_layers, lo_linkage) {
563 if (o->lo_ops->loo_object_invariant != NULL &&
564 !o->lo_ops->loo_object_invariant(o))
569 EXPORT_SYMBOL(lu_object_invariant);
571 static struct lu_object *htable_lookup(struct lu_site *s,
573 const struct lu_fid *f,
574 wait_queue_t *waiter,
577 struct lu_site_bkt_data *bkt;
578 struct lu_object_header *h;
579 cfs_hlist_node_t *hnode;
580 __u64 ver = cfs_hash_bd_version_get(bd);
583 return ERR_PTR(-ENOENT);
586 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
587 /* cfs_hash_bd_peek_locked is a somehow "internal" function
588 * of cfs_hash, it doesn't add refcount on object. */
589 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
591 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
592 return ERR_PTR(-ENOENT);
595 h = container_of0(hnode, struct lu_object_header, loh_hash);
596 if (likely(!lu_object_is_dying(h))) {
597 cfs_hash_get(s->ls_obj_hash, hnode);
598 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
599 cfs_list_del_init(&h->loh_lru);
600 return lu_object_top(h);
604 * Lookup found an object being destroyed this object cannot be
605 * returned (to assure that references to dying objects are eventually
606 * drained), and moreover, lookup has to wait until object is freed.
609 init_waitqueue_entry_current(waiter);
610 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
611 set_current_state(TASK_UNINTERRUPTIBLE);
612 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
613 return ERR_PTR(-EAGAIN);
616 static struct lu_object *htable_lookup_nowait(struct lu_site *s,
618 const struct lu_fid *f)
620 cfs_hlist_node_t *hnode;
621 struct lu_object_header *h;
623 /* cfs_hash_bd_peek_locked is a somehow "internal" function
624 * of cfs_hash, it doesn't add refcount on object. */
625 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
627 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
628 return ERR_PTR(-ENOENT);
631 h = container_of0(hnode, struct lu_object_header, loh_hash);
632 if (unlikely(lu_object_is_dying(h)))
633 return ERR_PTR(-ENOENT);
635 cfs_hash_get(s->ls_obj_hash, hnode);
636 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
637 cfs_list_del_init(&h->loh_lru);
638 return lu_object_top(h);
642 * Search cache for an object with the fid \a f. If such object is found,
643 * return it. Otherwise, create new object, insert it into cache and return
644 * it. In any case, additional reference is acquired on the returned object.
646 struct lu_object *lu_object_find(const struct lu_env *env,
647 struct lu_device *dev, const struct lu_fid *f,
648 const struct lu_object_conf *conf)
650 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
652 EXPORT_SYMBOL(lu_object_find);
655 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
656 * the calculation for the number of objects to reclaim is not covered by
657 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
658 * This ensures that many concurrent threads will not accidentally purge
661 static void lu_object_limit(const struct lu_env *env,
662 struct lu_device *dev)
666 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
669 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
670 nr = (__u64)lu_cache_nr;
672 lu_site_purge(env, dev->ld_site,
673 MIN(size - nr, LU_CACHE_NR_MAX_ADJUST));
678 static struct lu_object *lu_object_new(const struct lu_env *env,
679 struct lu_device *dev,
680 const struct lu_fid *f,
681 const struct lu_object_conf *conf)
686 struct lu_site_bkt_data *bkt;
688 o = lu_object_alloc(env, dev, f, conf);
689 if (unlikely(IS_ERR(o)))
692 hs = dev->ld_site->ls_obj_hash;
693 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
694 bkt = cfs_hash_bd_extra_get(hs, &bd);
695 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
697 cfs_hash_bd_unlock(hs, &bd, 1);
699 lu_object_limit(env, dev);
705 * Core logic of lu_object_find*() functions.
707 static struct lu_object *lu_object_find_try(const struct lu_env *env,
708 struct lu_device *dev,
709 const struct lu_fid *f,
710 const struct lu_object_conf *conf,
711 wait_queue_t *waiter)
714 struct lu_object *shadow;
721 * This uses standard index maintenance protocol:
723 * - search index under lock, and return object if found;
724 * - otherwise, unlock index, allocate new object;
725 * - lock index and search again;
726 * - if nothing is found (usual case), insert newly created
728 * - otherwise (race: other thread inserted object), free
729 * object just allocated.
733 * For "LOC_F_NEW" case, we are sure the object is new established.
734 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
735 * just alloc and insert directly.
737 * If dying object is found during index search, add @waiter to the
738 * site wait-queue and return ERR_PTR(-EAGAIN).
740 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
741 return lu_object_new(env, dev, f, conf);
745 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
746 o = htable_lookup(s, &bd, f, waiter, &version);
747 cfs_hash_bd_unlock(hs, &bd, 1);
748 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
752 * Allocate new object. This may result in rather complicated
753 * operations, including fld queries, inode loading, etc.
755 o = lu_object_alloc(env, dev, f, conf);
756 if (unlikely(IS_ERR(o)))
759 LASSERT(lu_fid_eq(lu_object_fid(o), f));
761 cfs_hash_bd_lock(hs, &bd, 1);
763 shadow = htable_lookup(s, &bd, f, waiter, &version);
764 if (likely(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT)) {
765 struct lu_site_bkt_data *bkt;
767 bkt = cfs_hash_bd_extra_get(hs, &bd);
768 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
770 cfs_hash_bd_unlock(hs, &bd, 1);
772 lu_object_limit(env, dev);
777 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
778 cfs_hash_bd_unlock(hs, &bd, 1);
779 lu_object_free(env, o);
784 * Much like lu_object_find(), but top level device of object is specifically
785 * \a dev rather than top level device of the site. This interface allows
786 * objects of different "stacking" to be created within the same site.
788 struct lu_object *lu_object_find_at(const struct lu_env *env,
789 struct lu_device *dev,
790 const struct lu_fid *f,
791 const struct lu_object_conf *conf)
793 struct lu_site_bkt_data *bkt;
794 struct lu_object *obj;
798 obj = lu_object_find_try(env, dev, f, conf, &wait);
799 if (obj != ERR_PTR(-EAGAIN))
802 * lu_object_find_try() already added waiter into the
805 waitq_wait(&wait, TASK_UNINTERRUPTIBLE);
806 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
807 remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
810 EXPORT_SYMBOL(lu_object_find_at);
813 * Try to find the object in cache without waiting for the dead object
814 * to be released nor allocating object if no cached one was found.
816 * The found object will be set as LU_OBJECT_HEARD_BANSHEE for purging.
818 void lu_object_purge(const struct lu_env *env, struct lu_device *dev,
819 const struct lu_fid *f)
821 struct lu_site *s = dev->ld_site;
822 cfs_hash_t *hs = s->ls_obj_hash;
826 cfs_hash_bd_get_and_lock(hs, f, &bd, 1);
827 o = htable_lookup_nowait(s, &bd, f);
828 cfs_hash_bd_unlock(hs, &bd, 1);
830 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
831 lu_object_put(env, o);
834 EXPORT_SYMBOL(lu_object_purge);
837 * Find object with given fid, and return its slice belonging to given device.
839 struct lu_object *lu_object_find_slice(const struct lu_env *env,
840 struct lu_device *dev,
841 const struct lu_fid *f,
842 const struct lu_object_conf *conf)
844 struct lu_object *top;
845 struct lu_object *obj;
847 top = lu_object_find(env, dev, f, conf);
849 obj = lu_object_locate(top->lo_header, dev->ld_type);
851 lu_object_put(env, top);
856 EXPORT_SYMBOL(lu_object_find_slice);
859 * Global list of all device types.
861 static CFS_LIST_HEAD(lu_device_types);
863 int lu_device_type_init(struct lu_device_type *ldt)
867 atomic_set(&ldt->ldt_device_nr, 0);
868 CFS_INIT_LIST_HEAD(&ldt->ldt_linkage);
869 if (ldt->ldt_ops->ldto_init)
870 result = ldt->ldt_ops->ldto_init(ldt);
873 spin_lock(&obd_types_lock);
874 cfs_list_add(&ldt->ldt_linkage, &lu_device_types);
875 spin_unlock(&obd_types_lock);
879 EXPORT_SYMBOL(lu_device_type_init);
881 void lu_device_type_fini(struct lu_device_type *ldt)
883 spin_lock(&obd_types_lock);
884 cfs_list_del_init(&ldt->ldt_linkage);
885 spin_unlock(&obd_types_lock);
886 if (ldt->ldt_ops->ldto_fini)
887 ldt->ldt_ops->ldto_fini(ldt);
889 EXPORT_SYMBOL(lu_device_type_fini);
892 * Global list of all sites on this node
894 static CFS_LIST_HEAD(lu_sites);
895 static DEFINE_MUTEX(lu_sites_guard);
898 * Global environment used by site shrinker.
900 static struct lu_env lu_shrink_env;
902 struct lu_site_print_arg {
903 struct lu_env *lsp_env;
905 lu_printer_t lsp_printer;
909 lu_site_obj_print(cfs_hash_t *hs, cfs_hash_bd_t *bd,
910 cfs_hlist_node_t *hnode, void *data)
912 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
913 struct lu_object_header *h;
915 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
916 if (!cfs_list_empty(&h->loh_layers)) {
917 const struct lu_object *o;
919 o = lu_object_top(h);
920 lu_object_print(arg->lsp_env, arg->lsp_cookie,
921 arg->lsp_printer, o);
923 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
924 arg->lsp_printer, h);
930 * Print all objects in \a s.
932 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
933 lu_printer_t printer)
935 struct lu_site_print_arg arg = {
936 .lsp_env = (struct lu_env *)env,
937 .lsp_cookie = cookie,
938 .lsp_printer = printer,
941 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
943 EXPORT_SYMBOL(lu_site_print);
946 * Return desired hash table order.
948 static int lu_htable_order(struct lu_device *top)
950 unsigned long cache_size;
954 * For ZFS based OSDs the cache should be disabled by default. This
955 * allows the ZFS ARC maximum flexibility in determining what buffers
956 * to cache. If Lustre has objects or buffer which it wants to ensure
957 * always stay cached it must maintain a hold on them.
959 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
960 lu_cache_percent = 1;
961 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
962 return LU_SITE_BITS_MIN;
966 * Calculate hash table size, assuming that we want reasonable
967 * performance when 20% of total memory is occupied by cache of
970 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
972 cache_size = totalram_pages;
974 #if BITS_PER_LONG == 32
975 /* limit hashtable size for lowmem systems to low RAM */
976 if (cache_size > 1 << (30 - PAGE_CACHE_SHIFT))
977 cache_size = 1 << (30 - PAGE_CACHE_SHIFT) * 3 / 4;
980 /* clear off unreasonable cache setting. */
981 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
982 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
983 " the range of (0, %u]. Will use default value: %u.\n",
984 lu_cache_percent, LU_CACHE_PERCENT_MAX,
985 LU_CACHE_PERCENT_DEFAULT);
987 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
989 cache_size = cache_size / 100 * lu_cache_percent *
990 (PAGE_CACHE_SIZE / 1024);
992 for (bits = 1; (1 << bits) < cache_size; ++bits) {
998 static unsigned lu_obj_hop_hash(cfs_hash_t *hs,
999 const void *key, unsigned mask)
1001 struct lu_fid *fid = (struct lu_fid *)key;
1004 hash = fid_flatten32(fid);
1005 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
1006 hash = cfs_hash_long(hash, hs->hs_bkt_bits);
1008 /* give me another random factor */
1009 hash -= cfs_hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
1011 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
1012 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
1017 static void *lu_obj_hop_object(cfs_hlist_node_t *hnode)
1019 return cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
1022 static void *lu_obj_hop_key(cfs_hlist_node_t *hnode)
1024 struct lu_object_header *h;
1026 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
1030 static int lu_obj_hop_keycmp(const void *key, cfs_hlist_node_t *hnode)
1032 struct lu_object_header *h;
1034 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
1035 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
1038 static void lu_obj_hop_get(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1040 struct lu_object_header *h;
1042 h = cfs_hlist_entry(hnode, struct lu_object_header, loh_hash);
1043 if (cfs_atomic_add_return(1, &h->loh_ref) == 1) {
1044 struct lu_site_bkt_data *bkt;
1047 cfs_hash_bd_get(hs, &h->loh_fid, &bd);
1048 bkt = cfs_hash_bd_extra_get(hs, &bd);
1053 static void lu_obj_hop_put_locked(cfs_hash_t *hs, cfs_hlist_node_t *hnode)
1055 LBUG(); /* we should never called it */
1058 cfs_hash_ops_t lu_site_hash_ops = {
1059 .hs_hash = lu_obj_hop_hash,
1060 .hs_key = lu_obj_hop_key,
1061 .hs_keycmp = lu_obj_hop_keycmp,
1062 .hs_object = lu_obj_hop_object,
1063 .hs_get = lu_obj_hop_get,
1064 .hs_put_locked = lu_obj_hop_put_locked,
1067 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1069 spin_lock(&s->ls_ld_lock);
1070 if (cfs_list_empty(&d->ld_linkage))
1071 cfs_list_add(&d->ld_linkage, &s->ls_ld_linkage);
1072 spin_unlock(&s->ls_ld_lock);
1074 EXPORT_SYMBOL(lu_dev_add_linkage);
1076 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1078 spin_lock(&s->ls_ld_lock);
1079 cfs_list_del_init(&d->ld_linkage);
1080 spin_unlock(&s->ls_ld_lock);
1082 EXPORT_SYMBOL(lu_dev_del_linkage);
1085 * Initialize site \a s, with \a d as the top level device.
1087 int lu_site_init(struct lu_site *s, struct lu_device *top)
1089 struct lu_site_bkt_data *bkt;
1096 memset(s, 0, sizeof *s);
1097 bits = lu_htable_order(top);
1098 snprintf(name, 16, "lu_site_%s", top->ld_type->ldt_name);
1099 for (bits = min(max(LU_SITE_BITS_MIN, bits), LU_SITE_BITS_MAX);
1100 bits >= LU_SITE_BITS_MIN; bits--) {
1101 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1102 bits - LU_SITE_BKT_BITS,
1105 CFS_HASH_SPIN_BKTLOCK |
1106 CFS_HASH_NO_ITEMREF |
1108 CFS_HASH_ASSERT_EMPTY |
1110 if (s->ls_obj_hash != NULL)
1114 if (s->ls_obj_hash == NULL) {
1115 CERROR("failed to create lu_site hash with bits: %d\n", bits);
1119 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1120 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1121 CFS_INIT_LIST_HEAD(&bkt->lsb_lru);
1122 init_waitqueue_head(&bkt->lsb_marche_funebre);
1125 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1126 if (s->ls_stats == NULL) {
1127 cfs_hash_putref(s->ls_obj_hash);
1128 s->ls_obj_hash = NULL;
1132 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1133 0, "created", "created");
1134 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1135 0, "cache_hit", "cache_hit");
1136 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1137 0, "cache_miss", "cache_miss");
1138 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1139 0, "cache_race", "cache_race");
1140 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1141 0, "cache_death_race", "cache_death_race");
1142 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1143 0, "lru_purged", "lru_purged");
1145 CFS_INIT_LIST_HEAD(&s->ls_linkage);
1146 s->ls_top_dev = top;
1149 lu_ref_add(&top->ld_reference, "site-top", s);
1151 CFS_INIT_LIST_HEAD(&s->ls_ld_linkage);
1152 spin_lock_init(&s->ls_ld_lock);
1154 lu_dev_add_linkage(s, top);
1158 EXPORT_SYMBOL(lu_site_init);
1161 * Finalize \a s and release its resources.
1163 void lu_site_fini(struct lu_site *s)
1165 mutex_lock(&lu_sites_guard);
1166 cfs_list_del_init(&s->ls_linkage);
1167 mutex_unlock(&lu_sites_guard);
1169 if (s->ls_obj_hash != NULL) {
1170 cfs_hash_putref(s->ls_obj_hash);
1171 s->ls_obj_hash = NULL;
1174 if (s->ls_top_dev != NULL) {
1175 s->ls_top_dev->ld_site = NULL;
1176 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1177 lu_device_put(s->ls_top_dev);
1178 s->ls_top_dev = NULL;
1181 if (s->ls_stats != NULL)
1182 lprocfs_free_stats(&s->ls_stats);
1184 EXPORT_SYMBOL(lu_site_fini);
1187 * Called when initialization of stack for this site is completed.
1189 int lu_site_init_finish(struct lu_site *s)
1192 mutex_lock(&lu_sites_guard);
1193 result = lu_context_refill(&lu_shrink_env.le_ctx);
1195 cfs_list_add(&s->ls_linkage, &lu_sites);
1196 mutex_unlock(&lu_sites_guard);
1199 EXPORT_SYMBOL(lu_site_init_finish);
1202 * Acquire additional reference on device \a d
1204 void lu_device_get(struct lu_device *d)
1206 cfs_atomic_inc(&d->ld_ref);
1208 EXPORT_SYMBOL(lu_device_get);
1211 * Release reference on device \a d.
1213 void lu_device_put(struct lu_device *d)
1215 LASSERT(cfs_atomic_read(&d->ld_ref) > 0);
1216 cfs_atomic_dec(&d->ld_ref);
1218 EXPORT_SYMBOL(lu_device_put);
1221 * Initialize device \a d of type \a t.
1223 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1225 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1226 t->ldt_ops->ldto_start != NULL)
1227 t->ldt_ops->ldto_start(t);
1229 memset(d, 0, sizeof *d);
1231 lu_ref_init(&d->ld_reference);
1232 CFS_INIT_LIST_HEAD(&d->ld_linkage);
1235 EXPORT_SYMBOL(lu_device_init);
1238 * Finalize device \a d.
1240 void lu_device_fini(struct lu_device *d)
1242 struct lu_device_type *t;
1245 if (d->ld_obd != NULL) {
1246 d->ld_obd->obd_lu_dev = NULL;
1250 lu_ref_fini(&d->ld_reference);
1251 LASSERTF(cfs_atomic_read(&d->ld_ref) == 0,
1252 "Refcount is %u\n", cfs_atomic_read(&d->ld_ref));
1253 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1255 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1256 t->ldt_ops->ldto_stop != NULL)
1257 t->ldt_ops->ldto_stop(t);
1259 EXPORT_SYMBOL(lu_device_fini);
1262 * Initialize object \a o that is part of compound object \a h and was created
1265 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1266 struct lu_device *d)
1268 memset(o, 0, sizeof(*o));
1272 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1273 CFS_INIT_LIST_HEAD(&o->lo_linkage);
1277 EXPORT_SYMBOL(lu_object_init);
1280 * Finalize object and release its resources.
1282 void lu_object_fini(struct lu_object *o)
1284 struct lu_device *dev = o->lo_dev;
1286 LASSERT(cfs_list_empty(&o->lo_linkage));
1289 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1295 EXPORT_SYMBOL(lu_object_fini);
1298 * Add object \a o as first layer of compound object \a h
1300 * This is typically called by the ->ldo_object_alloc() method of top-level
1303 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1305 cfs_list_move(&o->lo_linkage, &h->loh_layers);
1307 EXPORT_SYMBOL(lu_object_add_top);
1310 * Add object \a o as a layer of compound object, going after \a before.
1312 * This is typically called by the ->ldo_object_alloc() method of \a
1315 void lu_object_add(struct lu_object *before, struct lu_object *o)
1317 cfs_list_move(&o->lo_linkage, &before->lo_linkage);
1319 EXPORT_SYMBOL(lu_object_add);
1322 * Initialize compound object.
1324 int lu_object_header_init(struct lu_object_header *h)
1326 memset(h, 0, sizeof *h);
1327 cfs_atomic_set(&h->loh_ref, 1);
1328 CFS_INIT_HLIST_NODE(&h->loh_hash);
1329 CFS_INIT_LIST_HEAD(&h->loh_lru);
1330 CFS_INIT_LIST_HEAD(&h->loh_layers);
1331 lu_ref_init(&h->loh_reference);
1334 EXPORT_SYMBOL(lu_object_header_init);
1337 * Finalize compound object.
1339 void lu_object_header_fini(struct lu_object_header *h)
1341 LASSERT(cfs_list_empty(&h->loh_layers));
1342 LASSERT(cfs_list_empty(&h->loh_lru));
1343 LASSERT(cfs_hlist_unhashed(&h->loh_hash));
1344 lu_ref_fini(&h->loh_reference);
1346 EXPORT_SYMBOL(lu_object_header_fini);
1349 * Given a compound object, find its slice, corresponding to the device type
1352 struct lu_object *lu_object_locate(struct lu_object_header *h,
1353 const struct lu_device_type *dtype)
1355 struct lu_object *o;
1357 cfs_list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1358 if (o->lo_dev->ld_type == dtype)
1363 EXPORT_SYMBOL(lu_object_locate);
1368 * Finalize and free devices in the device stack.
1370 * Finalize device stack by purging object cache, and calling
1371 * lu_device_type_operations::ldto_device_fini() and
1372 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1374 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1376 struct lu_site *site = top->ld_site;
1377 struct lu_device *scan;
1378 struct lu_device *next;
1380 lu_site_purge(env, site, ~0);
1381 for (scan = top; scan != NULL; scan = next) {
1382 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1383 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1384 lu_device_put(scan);
1388 lu_site_purge(env, site, ~0);
1390 for (scan = top; scan != NULL; scan = next) {
1391 const struct lu_device_type *ldt = scan->ld_type;
1392 struct obd_type *type;
1394 next = ldt->ldt_ops->ldto_device_free(env, scan);
1395 type = ldt->ldt_obd_type;
1398 class_put_type(type);
1402 EXPORT_SYMBOL(lu_stack_fini);
1406 * Maximal number of tld slots.
1408 LU_CONTEXT_KEY_NR = 40
1411 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1413 static DEFINE_SPINLOCK(lu_keys_guard);
1416 * Global counter incremented whenever key is registered, unregistered,
1417 * revived or quiesced. This is used to void unnecessary calls to
1418 * lu_context_refill(). No locking is provided, as initialization and shutdown
1419 * are supposed to be externally serialized.
1421 static unsigned key_set_version = 0;
1426 int lu_context_key_register(struct lu_context_key *key)
1431 LASSERT(key->lct_init != NULL);
1432 LASSERT(key->lct_fini != NULL);
1433 LASSERT(key->lct_tags != 0);
1434 LASSERT(key->lct_owner != NULL);
1437 spin_lock(&lu_keys_guard);
1438 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1439 if (lu_keys[i] == NULL) {
1441 cfs_atomic_set(&key->lct_used, 1);
1443 lu_ref_init(&key->lct_reference);
1449 spin_unlock(&lu_keys_guard);
1452 EXPORT_SYMBOL(lu_context_key_register);
1454 static void key_fini(struct lu_context *ctx, int index)
1456 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1457 struct lu_context_key *key;
1459 key = lu_keys[index];
1460 LASSERT(key != NULL);
1461 LASSERT(key->lct_fini != NULL);
1462 LASSERT(cfs_atomic_read(&key->lct_used) > 1);
1464 key->lct_fini(ctx, key, ctx->lc_value[index]);
1465 lu_ref_del(&key->lct_reference, "ctx", ctx);
1466 cfs_atomic_dec(&key->lct_used);
1468 LASSERT(key->lct_owner != NULL);
1469 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1470 LINVRNT(module_refcount(key->lct_owner) > 0);
1471 module_put(key->lct_owner);
1473 ctx->lc_value[index] = NULL;
1480 void lu_context_key_degister(struct lu_context_key *key)
1482 LASSERT(cfs_atomic_read(&key->lct_used) >= 1);
1483 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1485 lu_context_key_quiesce(key);
1488 spin_lock(&lu_keys_guard);
1489 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1490 if (lu_keys[key->lct_index]) {
1491 lu_keys[key->lct_index] = NULL;
1492 lu_ref_fini(&key->lct_reference);
1494 spin_unlock(&lu_keys_guard);
1496 LASSERTF(cfs_atomic_read(&key->lct_used) == 1,
1497 "key has instances: %d\n",
1498 cfs_atomic_read(&key->lct_used));
1500 EXPORT_SYMBOL(lu_context_key_degister);
1503 * Register a number of keys. This has to be called after all keys have been
1504 * initialized by a call to LU_CONTEXT_KEY_INIT().
1506 int lu_context_key_register_many(struct lu_context_key *k, ...)
1508 struct lu_context_key *key = k;
1514 result = lu_context_key_register(key);
1517 key = va_arg(args, struct lu_context_key *);
1518 } while (key != NULL);
1524 lu_context_key_degister(k);
1525 k = va_arg(args, struct lu_context_key *);
1532 EXPORT_SYMBOL(lu_context_key_register_many);
1535 * De-register a number of keys. This is a dual to
1536 * lu_context_key_register_many().
1538 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1544 lu_context_key_degister(k);
1545 k = va_arg(args, struct lu_context_key*);
1546 } while (k != NULL);
1549 EXPORT_SYMBOL(lu_context_key_degister_many);
1552 * Revive a number of keys.
1554 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1560 lu_context_key_revive(k);
1561 k = va_arg(args, struct lu_context_key*);
1562 } while (k != NULL);
1565 EXPORT_SYMBOL(lu_context_key_revive_many);
1568 * Quiescent a number of keys.
1570 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1576 lu_context_key_quiesce(k);
1577 k = va_arg(args, struct lu_context_key*);
1578 } while (k != NULL);
1581 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1584 * Return value associated with key \a key in context \a ctx.
1586 void *lu_context_key_get(const struct lu_context *ctx,
1587 const struct lu_context_key *key)
1589 LINVRNT(ctx->lc_state == LCS_ENTERED);
1590 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1591 LASSERT(lu_keys[key->lct_index] == key);
1592 return ctx->lc_value[key->lct_index];
1594 EXPORT_SYMBOL(lu_context_key_get);
1597 * List of remembered contexts. XXX document me.
1599 static CFS_LIST_HEAD(lu_context_remembered);
1602 * Destroy \a key in all remembered contexts. This is used to destroy key
1603 * values in "shared" contexts (like service threads), when a module owning
1604 * the key is about to be unloaded.
1606 void lu_context_key_quiesce(struct lu_context_key *key)
1608 struct lu_context *ctx;
1610 if (!(key->lct_tags & LCT_QUIESCENT)) {
1612 * XXX layering violation.
1614 key->lct_tags |= LCT_QUIESCENT;
1616 * XXX memory barrier has to go here.
1618 spin_lock(&lu_keys_guard);
1619 cfs_list_for_each_entry(ctx, &lu_context_remembered,
1621 key_fini(ctx, key->lct_index);
1622 spin_unlock(&lu_keys_guard);
1626 EXPORT_SYMBOL(lu_context_key_quiesce);
1628 void lu_context_key_revive(struct lu_context_key *key)
1630 key->lct_tags &= ~LCT_QUIESCENT;
1633 EXPORT_SYMBOL(lu_context_key_revive);
1635 static void keys_fini(struct lu_context *ctx)
1639 if (ctx->lc_value == NULL)
1642 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1645 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1646 ctx->lc_value = NULL;
1649 static int keys_fill(struct lu_context *ctx)
1653 LINVRNT(ctx->lc_value != NULL);
1654 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1655 struct lu_context_key *key;
1658 if (ctx->lc_value[i] == NULL && key != NULL &&
1659 (key->lct_tags & ctx->lc_tags) &&
1661 * Don't create values for a LCT_QUIESCENT key, as this
1662 * will pin module owning a key.
1664 !(key->lct_tags & LCT_QUIESCENT)) {
1667 LINVRNT(key->lct_init != NULL);
1668 LINVRNT(key->lct_index == i);
1670 value = key->lct_init(ctx, key);
1671 if (unlikely(IS_ERR(value)))
1672 return PTR_ERR(value);
1674 LASSERT(key->lct_owner != NULL);
1675 if (!(ctx->lc_tags & LCT_NOREF))
1676 try_module_get(key->lct_owner);
1677 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1678 cfs_atomic_inc(&key->lct_used);
1680 * This is the only place in the code, where an
1681 * element of ctx->lc_value[] array is set to non-NULL
1684 ctx->lc_value[i] = value;
1685 if (key->lct_exit != NULL)
1686 ctx->lc_tags |= LCT_HAS_EXIT;
1688 ctx->lc_version = key_set_version;
1693 static int keys_init(struct lu_context *ctx)
1695 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1696 if (likely(ctx->lc_value != NULL))
1697 return keys_fill(ctx);
1703 * Initialize context data-structure. Create values for all keys.
1705 int lu_context_init(struct lu_context *ctx, __u32 tags)
1709 memset(ctx, 0, sizeof *ctx);
1710 ctx->lc_state = LCS_INITIALIZED;
1711 ctx->lc_tags = tags;
1712 if (tags & LCT_REMEMBER) {
1713 spin_lock(&lu_keys_guard);
1714 cfs_list_add(&ctx->lc_remember, &lu_context_remembered);
1715 spin_unlock(&lu_keys_guard);
1717 CFS_INIT_LIST_HEAD(&ctx->lc_remember);
1720 rc = keys_init(ctx);
1722 lu_context_fini(ctx);
1726 EXPORT_SYMBOL(lu_context_init);
1729 * Finalize context data-structure. Destroy key values.
1731 void lu_context_fini(struct lu_context *ctx)
1733 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1734 ctx->lc_state = LCS_FINALIZED;
1736 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1737 LASSERT(cfs_list_empty(&ctx->lc_remember));
1740 } else { /* could race with key degister */
1741 spin_lock(&lu_keys_guard);
1743 cfs_list_del_init(&ctx->lc_remember);
1744 spin_unlock(&lu_keys_guard);
1747 EXPORT_SYMBOL(lu_context_fini);
1750 * Called before entering context.
1752 void lu_context_enter(struct lu_context *ctx)
1754 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1755 ctx->lc_state = LCS_ENTERED;
1757 EXPORT_SYMBOL(lu_context_enter);
1760 * Called after exiting from \a ctx
1762 void lu_context_exit(struct lu_context *ctx)
1766 LINVRNT(ctx->lc_state == LCS_ENTERED);
1767 ctx->lc_state = LCS_LEFT;
1768 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1769 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1770 if (ctx->lc_value[i] != NULL) {
1771 struct lu_context_key *key;
1774 LASSERT(key != NULL);
1775 if (key->lct_exit != NULL)
1777 key, ctx->lc_value[i]);
1782 EXPORT_SYMBOL(lu_context_exit);
1785 * Allocate for context all missing keys that were registered after context
1786 * creation. key_set_version is only changed in rare cases when modules
1787 * are loaded and removed.
1789 int lu_context_refill(struct lu_context *ctx)
1791 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1793 EXPORT_SYMBOL(lu_context_refill);
1796 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1797 * obd being added. Currently, this is only used on client side, specifically
1798 * for echo device client, for other stack (like ptlrpc threads), context are
1799 * predefined when the lu_device type are registered, during the module probe
1802 __u32 lu_context_tags_default = 0;
1803 __u32 lu_session_tags_default = 0;
1805 void lu_context_tags_update(__u32 tags)
1807 spin_lock(&lu_keys_guard);
1808 lu_context_tags_default |= tags;
1810 spin_unlock(&lu_keys_guard);
1812 EXPORT_SYMBOL(lu_context_tags_update);
1814 void lu_context_tags_clear(__u32 tags)
1816 spin_lock(&lu_keys_guard);
1817 lu_context_tags_default &= ~tags;
1819 spin_unlock(&lu_keys_guard);
1821 EXPORT_SYMBOL(lu_context_tags_clear);
1823 void lu_session_tags_update(__u32 tags)
1825 spin_lock(&lu_keys_guard);
1826 lu_session_tags_default |= tags;
1828 spin_unlock(&lu_keys_guard);
1830 EXPORT_SYMBOL(lu_session_tags_update);
1832 void lu_session_tags_clear(__u32 tags)
1834 spin_lock(&lu_keys_guard);
1835 lu_session_tags_default &= ~tags;
1837 spin_unlock(&lu_keys_guard);
1839 EXPORT_SYMBOL(lu_session_tags_clear);
1841 int lu_env_init(struct lu_env *env, __u32 tags)
1846 result = lu_context_init(&env->le_ctx, tags);
1847 if (likely(result == 0))
1848 lu_context_enter(&env->le_ctx);
1851 EXPORT_SYMBOL(lu_env_init);
1853 void lu_env_fini(struct lu_env *env)
1855 lu_context_exit(&env->le_ctx);
1856 lu_context_fini(&env->le_ctx);
1859 EXPORT_SYMBOL(lu_env_fini);
1861 int lu_env_refill(struct lu_env *env)
1865 result = lu_context_refill(&env->le_ctx);
1866 if (result == 0 && env->le_ses != NULL)
1867 result = lu_context_refill(env->le_ses);
1870 EXPORT_SYMBOL(lu_env_refill);
1873 * Currently, this API will only be used by echo client.
1874 * Because echo client and normal lustre client will share
1875 * same cl_env cache. So echo client needs to refresh
1876 * the env context after it get one from the cache, especially
1877 * when normal client and echo client co-exist in the same client.
1879 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1884 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1885 env->le_ctx.lc_version = 0;
1886 env->le_ctx.lc_tags |= ctags;
1889 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1890 env->le_ses->lc_version = 0;
1891 env->le_ses->lc_tags |= stags;
1894 result = lu_env_refill(env);
1898 EXPORT_SYMBOL(lu_env_refill_by_tags);
1900 static struct shrinker *lu_site_shrinker;
1902 typedef struct lu_site_stats{
1903 unsigned lss_populated;
1904 unsigned lss_max_search;
1909 static void lu_site_stats_get(cfs_hash_t *hs,
1910 lu_site_stats_t *stats, int populated)
1915 cfs_hash_for_each_bucket(hs, &bd, i) {
1916 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1917 cfs_hlist_head_t *hhead;
1919 cfs_hash_bd_lock(hs, &bd, 1);
1920 stats->lss_busy += bkt->lsb_busy;
1921 stats->lss_total += cfs_hash_bd_count_get(&bd);
1922 stats->lss_max_search = max((int)stats->lss_max_search,
1923 cfs_hash_bd_depmax_get(&bd));
1925 cfs_hash_bd_unlock(hs, &bd, 1);
1929 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1930 if (!cfs_hlist_empty(hhead))
1931 stats->lss_populated++;
1933 cfs_hash_bd_unlock(hs, &bd, 1);
1940 * There exists a potential lock inversion deadlock scenario when using
1941 * Lustre on top of ZFS. This occurs between one of ZFS's
1942 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
1943 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
1944 * while thread B will take the ht_lock and sleep on the lu_sites_guard
1945 * lock. Obviously neither thread will wake and drop their respective hold
1948 * To prevent this from happening we must ensure the lu_sites_guard lock is
1949 * not taken while down this code path. ZFS reliably does not set the
1950 * __GFP_FS bit in its code paths, so this can be used to determine if it
1951 * is safe to take the lu_sites_guard lock.
1953 * Ideally we should accurately return the remaining number of cached
1954 * objects without taking the lu_sites_guard lock, but this is not
1955 * possible in the current implementation.
1957 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
1959 lu_site_stats_t stats;
1961 struct lu_site *tmp;
1963 int remain = shrink_param(sc, nr_to_scan);
1964 CFS_LIST_HEAD(splice);
1966 if (!(shrink_param(sc, gfp_mask) & __GFP_FS)) {
1970 /* We must not take the lu_sites_guard lock when
1971 * __GFP_FS is *not* set because of the deadlock
1972 * possibility detailed above. Additionally,
1973 * since we cannot determine the number of
1974 * objects in the cache without taking this
1975 * lock, we're in a particularly tough spot. As
1976 * a result, we'll just lie and say our cache is
1977 * empty. This _should_ be ok, as we can't
1978 * reclaim objects when __GFP_FS is *not* set
1984 CDEBUG(D_INODE, "Shrink %d objects\n", remain);
1986 mutex_lock(&lu_sites_guard);
1987 cfs_list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1988 if (shrink_param(sc, nr_to_scan) != 0) {
1989 remain = lu_site_purge(&lu_shrink_env, s, remain);
1991 * Move just shrunk site to the tail of site list to
1992 * assure shrinking fairness.
1994 cfs_list_move_tail(&s->ls_linkage, &splice);
1997 memset(&stats, 0, sizeof(stats));
1998 lu_site_stats_get(s->ls_obj_hash, &stats, 0);
1999 cached += stats.lss_total - stats.lss_busy;
2000 if (shrink_param(sc, nr_to_scan) && remain <= 0)
2003 cfs_list_splice(&splice, lu_sites.prev);
2004 mutex_unlock(&lu_sites_guard);
2006 cached = (cached / 100) * sysctl_vfs_cache_pressure;
2007 if (shrink_param(sc, nr_to_scan) == 0)
2008 CDEBUG(D_INODE, "%d objects cached\n", cached);
2017 * Environment to be used in debugger, contains all tags.
2019 struct lu_env lu_debugging_env;
2022 * Debugging printer function using printk().
2024 int lu_printk_printer(const struct lu_env *env,
2025 void *unused, const char *format, ...)
2029 va_start(args, format);
2030 vprintk(format, args);
2035 int lu_debugging_setup(void)
2037 return lu_env_init(&lu_debugging_env, ~0);
2040 void lu_context_keys_dump(void)
2044 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2045 struct lu_context_key *key;
2049 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2050 i, key, key->lct_tags,
2051 key->lct_init, key->lct_fini, key->lct_exit,
2052 key->lct_index, cfs_atomic_read(&key->lct_used),
2053 key->lct_owner ? key->lct_owner->name : "",
2055 lu_ref_print(&key->lct_reference);
2059 EXPORT_SYMBOL(lu_context_keys_dump);
2060 #else /* !__KERNEL__ */
2061 static int lu_cache_shrink(int nr, unsigned int gfp_mask)
2065 #endif /* __KERNEL__ */
2068 * Initialization of global lu_* data.
2070 int lu_global_init(void)
2074 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2076 result = lu_ref_global_init();
2080 LU_CONTEXT_KEY_INIT(&lu_global_key);
2081 result = lu_context_key_register(&lu_global_key);
2086 * At this level, we don't know what tags are needed, so allocate them
2087 * conservatively. This should not be too bad, because this
2088 * environment is global.
2090 mutex_lock(&lu_sites_guard);
2091 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2092 mutex_unlock(&lu_sites_guard);
2097 * seeks estimation: 3 seeks to read a record from oi, one to read
2098 * inode, one for ea. Unfortunately setting this high value results in
2099 * lu_object/inode cache consuming all the memory.
2101 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, lu_cache_shrink);
2102 if (lu_site_shrinker == NULL)
2109 * Dual to lu_global_init().
2111 void lu_global_fini(void)
2113 if (lu_site_shrinker != NULL) {
2114 remove_shrinker(lu_site_shrinker);
2115 lu_site_shrinker = NULL;
2118 lu_context_key_degister(&lu_global_key);
2121 * Tear shrinker environment down _after_ de-registering
2122 * lu_global_key, because the latter has a value in the former.
2124 mutex_lock(&lu_sites_guard);
2125 lu_env_fini(&lu_shrink_env);
2126 mutex_unlock(&lu_sites_guard);
2128 lu_ref_global_fini();
2131 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2134 struct lprocfs_counter ret;
2136 lprocfs_stats_collect(stats, idx, &ret);
2137 return (__u32)ret.lc_count;
2144 * Output site statistical counters into a buffer. Suitable for
2145 * lprocfs_rd_*()-style functions.
2147 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
2149 lu_site_stats_t stats;
2151 memset(&stats, 0, sizeof(stats));
2152 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2154 return snprintf(page, count, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2157 stats.lss_populated,
2158 CFS_HASH_NHLIST(s->ls_obj_hash),
2159 stats.lss_max_search,
2160 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2161 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2162 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2163 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2164 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2165 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2167 EXPORT_SYMBOL(lu_site_stats_print);
2170 * Helper function to initialize a number of kmem slab caches at once.
2172 int lu_kmem_init(struct lu_kmem_descr *caches)
2175 struct lu_kmem_descr *iter = caches;
2177 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2178 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2181 if (*iter->ckd_cache == NULL) {
2183 /* free all previously allocated caches */
2184 lu_kmem_fini(caches);
2190 EXPORT_SYMBOL(lu_kmem_init);
2193 * Helper function to finalize a number of kmem slab cached at once. Dual to
2196 void lu_kmem_fini(struct lu_kmem_descr *caches)
2198 for (; caches->ckd_cache != NULL; ++caches) {
2199 if (*caches->ckd_cache != NULL) {
2200 kmem_cache_destroy(*caches->ckd_cache);
2201 *caches->ckd_cache = NULL;
2205 EXPORT_SYMBOL(lu_kmem_fini);
2208 * Temporary solution to be able to assign fid in ->do_create()
2209 * till we have fully-functional OST fids
2211 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2212 const struct lu_fid *fid)
2214 struct lu_site *s = o->lo_dev->ld_site;
2215 struct lu_fid *old = &o->lo_header->loh_fid;
2216 struct lu_site_bkt_data *bkt;
2217 struct lu_object *shadow;
2218 wait_queue_t waiter;
2223 LASSERT(fid_is_zero(old));
2225 hs = s->ls_obj_hash;
2226 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2227 shadow = htable_lookup(s, &bd, fid, &waiter, &version);
2228 /* supposed to be unique */
2229 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2231 bkt = cfs_hash_bd_extra_get(hs, &bd);
2232 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2234 cfs_hash_bd_unlock(hs, &bd, 1);
2236 EXPORT_SYMBOL(lu_object_assign_fid);
2239 * allocates object with 0 (non-assiged) fid
2240 * XXX: temporary solution to be able to assign fid in ->do_create()
2241 * till we have fully-functional OST fids
2243 struct lu_object *lu_object_anon(const struct lu_env *env,
2244 struct lu_device *dev,
2245 const struct lu_object_conf *conf)
2248 struct lu_object *o;
2251 o = lu_object_alloc(env, dev, &fid, conf);
2255 EXPORT_SYMBOL(lu_object_anon);
2257 struct lu_buf LU_BUF_NULL = {
2261 EXPORT_SYMBOL(LU_BUF_NULL);
2263 void lu_buf_free(struct lu_buf *buf)
2267 LASSERT(buf->lb_len > 0);
2268 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2273 EXPORT_SYMBOL(lu_buf_free);
2275 void lu_buf_alloc(struct lu_buf *buf, int size)
2278 LASSERT(buf->lb_buf == NULL);
2279 LASSERT(buf->lb_len == 0);
2280 OBD_ALLOC_LARGE(buf->lb_buf, size);
2281 if (likely(buf->lb_buf))
2284 EXPORT_SYMBOL(lu_buf_alloc);
2286 void lu_buf_realloc(struct lu_buf *buf, int size)
2289 lu_buf_alloc(buf, size);
2291 EXPORT_SYMBOL(lu_buf_realloc);
2293 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, int len)
2295 if (buf->lb_buf == NULL && buf->lb_len == 0)
2296 lu_buf_alloc(buf, len);
2298 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2299 lu_buf_realloc(buf, len);
2303 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2306 * Increase the size of the \a buf.
2307 * preserves old data in buffer
2308 * old buffer remains unchanged on error
2309 * \retval 0 or -ENOMEM
2311 int lu_buf_check_and_grow(struct lu_buf *buf, int len)
2315 if (len <= buf->lb_len)
2318 OBD_ALLOC_LARGE(ptr, len);
2322 /* Free the old buf */
2323 if (buf->lb_buf != NULL) {
2324 memcpy(ptr, buf->lb_buf, buf->lb_len);
2325 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2332 EXPORT_SYMBOL(lu_buf_check_and_grow);