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);
96 * Decrease reference counter on object. If last reference is freed, return
97 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
98 * case, free object immediately.
100 void lu_object_put(const struct lu_env *env, struct lu_object *o)
102 struct lu_site_bkt_data *bkt;
103 struct lu_object_header *top;
104 struct lu_site *site;
105 struct lu_object *orig;
107 const struct lu_fid *fid;
110 site = o->lo_dev->ld_site;
114 * till we have full fids-on-OST implemented anonymous objects
115 * are possible in OSP. such an object isn't listed in the site
116 * so we should not remove it from the site.
118 fid = lu_object_fid(o);
119 if (fid_is_zero(fid)) {
120 LASSERT(top->loh_hash.next == NULL
121 && top->loh_hash.pprev == NULL);
122 LASSERT(list_empty(&top->loh_lru));
123 if (!atomic_dec_and_test(&top->loh_ref))
125 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
126 if (o->lo_ops->loo_object_release != NULL)
127 o->lo_ops->loo_object_release(env, o);
129 lu_object_free(env, orig);
133 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
134 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
136 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
137 if (lu_object_is_dying(top)) {
140 * somebody may be waiting for this, currently only
141 * used for cl_object, see cl_object_put_last().
143 wake_up_all(&bkt->lsb_marche_funebre);
148 LASSERT(bkt->lsb_busy > 0);
151 * When last reference is released, iterate over object
152 * layers, and notify them that object is no longer busy.
154 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
155 if (o->lo_ops->loo_object_release != NULL)
156 o->lo_ops->loo_object_release(env, o);
159 if (!lu_object_is_dying(top)) {
160 LASSERT(list_empty(&top->loh_lru));
161 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
162 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
167 * If object is dying (will not be cached), removed it
168 * from hash table and LRU.
170 * This is done with hash table and LRU lists locked. As the only
171 * way to acquire first reference to previously unreferenced
172 * object is through hash-table lookup (lu_object_find()),
173 * or LRU scanning (lu_site_purge()), that are done under hash-table
174 * and LRU lock, no race with concurrent object lookup is possible
175 * and we can safely destroy object below.
177 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
178 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
179 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
181 * Object was already removed from hash and lru above, can
184 lu_object_free(env, orig);
186 EXPORT_SYMBOL(lu_object_put);
189 * Put object and don't keep in cache. This is temporary solution for
190 * multi-site objects when its layering is not constant.
192 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
194 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
195 return lu_object_put(env, o);
197 EXPORT_SYMBOL(lu_object_put_nocache);
200 * Kill the object and take it out of LRU cache.
201 * Currently used by client code for layout change.
203 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
205 struct lu_object_header *top;
208 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
209 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
210 cfs_hash_t *obj_hash = o->lo_dev->ld_site->ls_obj_hash;
213 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
214 list_del_init(&top->loh_lru);
215 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
216 cfs_hash_bd_unlock(obj_hash, &bd, 1);
219 EXPORT_SYMBOL(lu_object_unhash);
222 * Allocate new object.
224 * This follows object creation protocol, described in the comment within
225 * struct lu_device_operations definition.
227 static struct lu_object *lu_object_alloc(const struct lu_env *env,
228 struct lu_device *dev,
229 const struct lu_fid *f,
230 const struct lu_object_conf *conf)
232 struct lu_object *scan;
233 struct lu_object *top;
234 struct list_head *layers;
235 unsigned int init_mask = 0;
236 unsigned int init_flag;
242 * Create top-level object slice. This will also create
245 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
247 RETURN(ERR_PTR(-ENOMEM));
251 * This is the only place where object fid is assigned. It's constant
254 top->lo_header->loh_fid = *f;
255 layers = &top->lo_header->loh_layers;
259 * Call ->loo_object_init() repeatedly, until no more new
260 * object slices are created.
264 list_for_each_entry(scan, layers, lo_linkage) {
265 if (init_mask & init_flag)
268 scan->lo_header = top->lo_header;
269 result = scan->lo_ops->loo_object_init(env, scan, conf);
271 lu_object_free(env, top);
272 RETURN(ERR_PTR(result));
274 init_mask |= init_flag;
280 list_for_each_entry_reverse(scan, layers, lo_linkage) {
281 if (scan->lo_ops->loo_object_start != NULL) {
282 result = scan->lo_ops->loo_object_start(env, scan);
284 lu_object_free(env, top);
285 RETURN(ERR_PTR(result));
290 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
297 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
299 struct lu_site_bkt_data *bkt;
300 struct lu_site *site;
301 struct lu_object *scan;
302 struct list_head *layers;
303 struct list_head splice;
305 site = o->lo_dev->ld_site;
306 layers = &o->lo_header->loh_layers;
307 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
309 * First call ->loo_object_delete() method to release all resources.
311 list_for_each_entry_reverse(scan, layers, lo_linkage) {
312 if (scan->lo_ops->loo_object_delete != NULL)
313 scan->lo_ops->loo_object_delete(env, scan);
317 * Then, splice object layers into stand-alone list, and call
318 * ->loo_object_free() on all layers to free memory. Splice is
319 * necessary, because lu_object_header is freed together with the
322 INIT_LIST_HEAD(&splice);
323 list_splice_init(layers, &splice);
324 while (!list_empty(&splice)) {
326 * Free layers in bottom-to-top order, so that object header
327 * lives as long as possible and ->loo_object_free() methods
328 * can look at its contents.
330 o = container_of0(splice.prev, struct lu_object, lo_linkage);
331 list_del_init(&o->lo_linkage);
332 LASSERT(o->lo_ops->loo_object_free != NULL);
333 o->lo_ops->loo_object_free(env, o);
336 if (waitqueue_active(&bkt->lsb_marche_funebre))
337 wake_up_all(&bkt->lsb_marche_funebre);
341 * Free \a nr objects from the cold end of the site LRU list.
343 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
345 struct lu_object_header *h;
346 struct lu_object_header *temp;
347 struct lu_site_bkt_data *bkt;
350 struct list_head dispose;
357 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
360 INIT_LIST_HEAD(&dispose);
362 * Under LRU list lock, scan LRU list and move unreferenced objects to
363 * the dispose list, removing them from LRU and hash table.
365 start = s->ls_purge_start;
366 bnr = (nr == ~0) ? -1 : nr / CFS_HASH_NBKT(s->ls_obj_hash) + 1;
369 * It doesn't make any sense to make purge threads parallel, that can
370 * only bring troubles to us. See LU-5331.
372 mutex_lock(&s->ls_purge_mutex);
374 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
378 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
379 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
381 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
382 LASSERT(atomic_read(&h->loh_ref) == 0);
384 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
385 LASSERT(bd.bd_bucket == bd2.bd_bucket);
387 cfs_hash_bd_del_locked(s->ls_obj_hash,
389 list_move(&h->loh_lru, &dispose);
393 if (nr != ~0 && --nr == 0)
396 if (count > 0 && --count == 0)
400 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
403 * Free everything on the dispose list. This is safe against
404 * races due to the reasons described in lu_object_put().
406 while (!list_empty(&dispose)) {
407 h = container_of0(dispose.next,
408 struct lu_object_header, loh_lru);
409 list_del_init(&h->loh_lru);
410 lu_object_free(env, lu_object_top(h));
411 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
417 mutex_unlock(&s->ls_purge_mutex);
419 if (nr != 0 && did_sth && start != 0) {
420 start = 0; /* restart from the first bucket */
423 /* race on s->ls_purge_start, but nobody cares */
424 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
428 EXPORT_SYMBOL(lu_site_purge);
433 * Code below has to jump through certain loops to output object description
434 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
435 * composes object description from strings that are parts of _lines_ of
436 * output (i.e., strings that are not terminated by newline). This doesn't fit
437 * very well into libcfs_debug_msg() interface that assumes that each message
438 * supplied to it is a self-contained output line.
440 * To work around this, strings are collected in a temporary buffer
441 * (implemented as a value of lu_cdebug_key key), until terminating newline
442 * character is detected.
450 * XXX overflow is not handled correctly.
455 struct lu_cdebug_data {
459 char lck_area[LU_CDEBUG_LINE];
462 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
463 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
466 * Key, holding temporary buffer. This key is registered very early by
469 struct lu_context_key lu_global_key = {
470 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
471 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
472 .lct_init = lu_global_key_init,
473 .lct_fini = lu_global_key_fini
477 * Printer function emitting messages through libcfs_debug_msg().
479 int lu_cdebug_printer(const struct lu_env *env,
480 void *cookie, const char *format, ...)
482 struct libcfs_debug_msg_data *msgdata = cookie;
483 struct lu_cdebug_data *key;
488 va_start(args, format);
490 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
491 LASSERT(key != NULL);
493 used = strlen(key->lck_area);
494 complete = format[strlen(format) - 1] == '\n';
496 * Append new chunk to the buffer.
498 vsnprintf(key->lck_area + used,
499 ARRAY_SIZE(key->lck_area) - used, format, args);
501 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
502 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
503 key->lck_area[0] = 0;
508 EXPORT_SYMBOL(lu_cdebug_printer);
511 * Print object header.
513 void lu_object_header_print(const struct lu_env *env, void *cookie,
514 lu_printer_t printer,
515 const struct lu_object_header *hdr)
517 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
518 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
520 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
521 list_empty((struct list_head *)&hdr->loh_lru) ? \
523 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
525 EXPORT_SYMBOL(lu_object_header_print);
528 * Print human readable representation of the \a o to the \a printer.
530 void lu_object_print(const struct lu_env *env, void *cookie,
531 lu_printer_t printer, const struct lu_object *o)
533 static const char ruler[] = "........................................";
534 struct lu_object_header *top;
538 lu_object_header_print(env, cookie, printer, top);
539 (*printer)(env, cookie, "{\n");
541 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
543 * print `.' \a depth times followed by type name and address
545 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
546 o->lo_dev->ld_type->ldt_name, o);
548 if (o->lo_ops->loo_object_print != NULL)
549 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
551 (*printer)(env, cookie, "\n");
554 (*printer)(env, cookie, "} header@%p\n", top);
556 EXPORT_SYMBOL(lu_object_print);
559 * Check object consistency.
561 int lu_object_invariant(const struct lu_object *o)
563 struct lu_object_header *top;
566 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
567 if (o->lo_ops->loo_object_invariant != NULL &&
568 !o->lo_ops->loo_object_invariant(o))
573 EXPORT_SYMBOL(lu_object_invariant);
575 static struct lu_object *htable_lookup(struct lu_site *s,
577 const struct lu_fid *f,
578 wait_queue_t *waiter,
581 struct lu_site_bkt_data *bkt;
582 struct lu_object_header *h;
583 struct hlist_node *hnode;
584 __u64 ver = cfs_hash_bd_version_get(bd);
587 return ERR_PTR(-ENOENT);
590 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
591 /* cfs_hash_bd_peek_locked is a somehow "internal" function
592 * of cfs_hash, it doesn't add refcount on object. */
593 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
595 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
596 return ERR_PTR(-ENOENT);
599 h = container_of0(hnode, struct lu_object_header, loh_hash);
600 if (likely(!lu_object_is_dying(h))) {
601 cfs_hash_get(s->ls_obj_hash, hnode);
602 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
603 list_del_init(&h->loh_lru);
604 return lu_object_top(h);
608 * Lookup found an object being destroyed this object cannot be
609 * returned (to assure that references to dying objects are eventually
610 * drained), and moreover, lookup has to wait until object is freed.
613 init_waitqueue_entry_current(waiter);
614 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
615 set_current_state(TASK_UNINTERRUPTIBLE);
616 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
617 return ERR_PTR(-EAGAIN);
620 static struct lu_object *htable_lookup_nowait(struct lu_site *s,
622 const struct lu_fid *f)
624 struct hlist_node *hnode;
625 struct lu_object_header *h;
627 /* cfs_hash_bd_peek_locked is a somehow "internal" function
628 * of cfs_hash, it doesn't add refcount on object. */
629 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
631 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
632 return ERR_PTR(-ENOENT);
635 h = container_of0(hnode, struct lu_object_header, loh_hash);
636 if (unlikely(lu_object_is_dying(h)))
637 return ERR_PTR(-ENOENT);
639 cfs_hash_get(s->ls_obj_hash, hnode);
640 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
641 list_del_init(&h->loh_lru);
642 return lu_object_top(h);
646 * Search cache for an object with the fid \a f. If such object is found,
647 * return it. Otherwise, create new object, insert it into cache and return
648 * it. In any case, additional reference is acquired on the returned object.
650 struct lu_object *lu_object_find(const struct lu_env *env,
651 struct lu_device *dev, const struct lu_fid *f,
652 const struct lu_object_conf *conf)
654 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
656 EXPORT_SYMBOL(lu_object_find);
659 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
660 * the calculation for the number of objects to reclaim is not covered by
661 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
662 * This ensures that many concurrent threads will not accidentally purge
665 static void lu_object_limit(const struct lu_env *env,
666 struct lu_device *dev)
670 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
673 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
674 nr = (__u64)lu_cache_nr;
676 lu_site_purge(env, dev->ld_site,
677 MIN(size - nr, LU_CACHE_NR_MAX_ADJUST));
682 static struct lu_object *lu_object_new(const struct lu_env *env,
683 struct lu_device *dev,
684 const struct lu_fid *f,
685 const struct lu_object_conf *conf)
690 struct lu_site_bkt_data *bkt;
692 o = lu_object_alloc(env, dev, f, conf);
693 if (unlikely(IS_ERR(o)))
696 hs = dev->ld_site->ls_obj_hash;
697 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
698 bkt = cfs_hash_bd_extra_get(hs, &bd);
699 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
701 cfs_hash_bd_unlock(hs, &bd, 1);
703 lu_object_limit(env, dev);
709 * Core logic of lu_object_find*() functions.
711 static struct lu_object *lu_object_find_try(const struct lu_env *env,
712 struct lu_device *dev,
713 const struct lu_fid *f,
714 const struct lu_object_conf *conf,
715 wait_queue_t *waiter)
718 struct lu_object *shadow;
725 * This uses standard index maintenance protocol:
727 * - search index under lock, and return object if found;
728 * - otherwise, unlock index, allocate new object;
729 * - lock index and search again;
730 * - if nothing is found (usual case), insert newly created
732 * - otherwise (race: other thread inserted object), free
733 * object just allocated.
737 * For "LOC_F_NEW" case, we are sure the object is new established.
738 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
739 * just alloc and insert directly.
741 * If dying object is found during index search, add @waiter to the
742 * site wait-queue and return ERR_PTR(-EAGAIN).
744 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
745 return lu_object_new(env, dev, f, conf);
749 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
750 o = htable_lookup(s, &bd, f, waiter, &version);
751 cfs_hash_bd_unlock(hs, &bd, 1);
752 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
756 * Allocate new object. This may result in rather complicated
757 * operations, including fld queries, inode loading, etc.
759 o = lu_object_alloc(env, dev, f, conf);
760 if (unlikely(IS_ERR(o)))
763 LASSERT(lu_fid_eq(lu_object_fid(o), f));
765 cfs_hash_bd_lock(hs, &bd, 1);
767 shadow = htable_lookup(s, &bd, f, waiter, &version);
768 if (likely(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT)) {
769 struct lu_site_bkt_data *bkt;
771 bkt = cfs_hash_bd_extra_get(hs, &bd);
772 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
774 cfs_hash_bd_unlock(hs, &bd, 1);
776 lu_object_limit(env, dev);
781 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
782 cfs_hash_bd_unlock(hs, &bd, 1);
783 lu_object_free(env, o);
788 * Much like lu_object_find(), but top level device of object is specifically
789 * \a dev rather than top level device of the site. This interface allows
790 * objects of different "stacking" to be created within the same site.
792 struct lu_object *lu_object_find_at(const struct lu_env *env,
793 struct lu_device *dev,
794 const struct lu_fid *f,
795 const struct lu_object_conf *conf)
797 struct lu_site_bkt_data *bkt;
798 struct lu_object *obj;
802 obj = lu_object_find_try(env, dev, f, conf, &wait);
803 if (obj != ERR_PTR(-EAGAIN))
806 * lu_object_find_try() already added waiter into the
809 waitq_wait(&wait, TASK_UNINTERRUPTIBLE);
810 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
811 remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
814 EXPORT_SYMBOL(lu_object_find_at);
817 * Try to find the object in cache without waiting for the dead object
818 * to be released nor allocating object if no cached one was found.
820 * The found object will be set as LU_OBJECT_HEARD_BANSHEE for purging.
822 void lu_object_purge(const struct lu_env *env, struct lu_device *dev,
823 const struct lu_fid *f)
825 struct lu_site *s = dev->ld_site;
826 cfs_hash_t *hs = s->ls_obj_hash;
830 cfs_hash_bd_get_and_lock(hs, f, &bd, 1);
831 o = htable_lookup_nowait(s, &bd, f);
832 cfs_hash_bd_unlock(hs, &bd, 1);
834 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
835 lu_object_put(env, o);
838 EXPORT_SYMBOL(lu_object_purge);
841 * Find object with given fid, and return its slice belonging to given device.
843 struct lu_object *lu_object_find_slice(const struct lu_env *env,
844 struct lu_device *dev,
845 const struct lu_fid *f,
846 const struct lu_object_conf *conf)
848 struct lu_object *top;
849 struct lu_object *obj;
851 top = lu_object_find(env, dev, f, conf);
853 obj = lu_object_locate(top->lo_header, dev->ld_type);
855 lu_object_put(env, top);
860 EXPORT_SYMBOL(lu_object_find_slice);
863 * Global list of all device types.
865 static struct list_head lu_device_types;
867 int lu_device_type_init(struct lu_device_type *ldt)
871 atomic_set(&ldt->ldt_device_nr, 0);
872 INIT_LIST_HEAD(&ldt->ldt_linkage);
873 if (ldt->ldt_ops->ldto_init)
874 result = ldt->ldt_ops->ldto_init(ldt);
877 spin_lock(&obd_types_lock);
878 list_add(&ldt->ldt_linkage, &lu_device_types);
879 spin_unlock(&obd_types_lock);
884 EXPORT_SYMBOL(lu_device_type_init);
886 void lu_device_type_fini(struct lu_device_type *ldt)
888 spin_lock(&obd_types_lock);
889 list_del_init(&ldt->ldt_linkage);
890 spin_unlock(&obd_types_lock);
891 if (ldt->ldt_ops->ldto_fini)
892 ldt->ldt_ops->ldto_fini(ldt);
894 EXPORT_SYMBOL(lu_device_type_fini);
897 * Global list of all sites on this node
899 static struct list_head lu_sites;
900 static DEFINE_MUTEX(lu_sites_guard);
903 * Global environment used by site shrinker.
905 static struct lu_env lu_shrink_env;
907 struct lu_site_print_arg {
908 struct lu_env *lsp_env;
910 lu_printer_t lsp_printer;
914 lu_site_obj_print(cfs_hash_t *hs, cfs_hash_bd_t *bd,
915 struct hlist_node *hnode, void *data)
917 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
918 struct lu_object_header *h;
920 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
921 if (!list_empty(&h->loh_layers)) {
922 const struct lu_object *o;
924 o = lu_object_top(h);
925 lu_object_print(arg->lsp_env, arg->lsp_cookie,
926 arg->lsp_printer, o);
928 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
929 arg->lsp_printer, h);
935 * Print all objects in \a s.
937 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
938 lu_printer_t printer)
940 struct lu_site_print_arg arg = {
941 .lsp_env = (struct lu_env *)env,
942 .lsp_cookie = cookie,
943 .lsp_printer = printer,
946 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
948 EXPORT_SYMBOL(lu_site_print);
951 * Return desired hash table order.
953 static unsigned int lu_htable_order(struct lu_device *top)
955 unsigned long cache_size;
959 * For ZFS based OSDs the cache should be disabled by default. This
960 * allows the ZFS ARC maximum flexibility in determining what buffers
961 * to cache. If Lustre has objects or buffer which it wants to ensure
962 * always stay cached it must maintain a hold on them.
964 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
965 lu_cache_percent = 1;
966 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
967 return LU_SITE_BITS_MIN;
971 * Calculate hash table size, assuming that we want reasonable
972 * performance when 20% of total memory is occupied by cache of
975 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
977 cache_size = totalram_pages;
979 #if BITS_PER_LONG == 32
980 /* limit hashtable size for lowmem systems to low RAM */
981 if (cache_size > 1 << (30 - PAGE_CACHE_SHIFT))
982 cache_size = 1 << (30 - PAGE_CACHE_SHIFT) * 3 / 4;
985 /* clear off unreasonable cache setting. */
986 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
987 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
988 " the range of (0, %u]. Will use default value: %u.\n",
989 lu_cache_percent, LU_CACHE_PERCENT_MAX,
990 LU_CACHE_PERCENT_DEFAULT);
992 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
994 cache_size = cache_size / 100 * lu_cache_percent *
995 (PAGE_CACHE_SIZE / 1024);
997 for (bits = 1; (1 << bits) < cache_size; ++bits) {
1003 static unsigned lu_obj_hop_hash(cfs_hash_t *hs,
1004 const void *key, unsigned mask)
1006 struct lu_fid *fid = (struct lu_fid *)key;
1009 hash = fid_flatten32(fid);
1010 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
1011 hash = hash_long(hash, hs->hs_bkt_bits);
1013 /* give me another random factor */
1014 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
1016 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
1017 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
1022 static void *lu_obj_hop_object(struct hlist_node *hnode)
1024 return hlist_entry(hnode, struct lu_object_header, loh_hash);
1027 static void *lu_obj_hop_key(struct hlist_node *hnode)
1029 struct lu_object_header *h;
1031 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1035 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
1037 struct lu_object_header *h;
1039 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1040 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
1043 static void lu_obj_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
1045 struct lu_object_header *h;
1047 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1048 if (atomic_add_return(1, &h->loh_ref) == 1) {
1049 struct lu_site_bkt_data *bkt;
1052 cfs_hash_bd_get(hs, &h->loh_fid, &bd);
1053 bkt = cfs_hash_bd_extra_get(hs, &bd);
1058 static void lu_obj_hop_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
1060 LBUG(); /* we should never called it */
1063 cfs_hash_ops_t lu_site_hash_ops = {
1064 .hs_hash = lu_obj_hop_hash,
1065 .hs_key = lu_obj_hop_key,
1066 .hs_keycmp = lu_obj_hop_keycmp,
1067 .hs_object = lu_obj_hop_object,
1068 .hs_get = lu_obj_hop_get,
1069 .hs_put_locked = lu_obj_hop_put_locked,
1072 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1074 spin_lock(&s->ls_ld_lock);
1075 if (list_empty(&d->ld_linkage))
1076 list_add(&d->ld_linkage, &s->ls_ld_linkage);
1077 spin_unlock(&s->ls_ld_lock);
1079 EXPORT_SYMBOL(lu_dev_add_linkage);
1081 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1083 spin_lock(&s->ls_ld_lock);
1084 list_del_init(&d->ld_linkage);
1085 spin_unlock(&s->ls_ld_lock);
1087 EXPORT_SYMBOL(lu_dev_del_linkage);
1090 * Initialize site \a s, with \a d as the top level device.
1092 int lu_site_init(struct lu_site *s, struct lu_device *top)
1094 struct lu_site_bkt_data *bkt;
1101 memset(s, 0, sizeof *s);
1102 mutex_init(&s->ls_purge_mutex);
1103 bits = lu_htable_order(top);
1104 snprintf(name, 16, "lu_site_%s", top->ld_type->ldt_name);
1105 for (bits = clamp_t(typeof(bits), bits,
1106 LU_SITE_BITS_MIN, LU_SITE_BITS_MAX);
1107 bits >= LU_SITE_BITS_MIN; bits--) {
1108 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1109 bits - LU_SITE_BKT_BITS,
1112 CFS_HASH_SPIN_BKTLOCK |
1113 CFS_HASH_NO_ITEMREF |
1115 CFS_HASH_ASSERT_EMPTY |
1117 if (s->ls_obj_hash != NULL)
1121 if (s->ls_obj_hash == NULL) {
1122 CERROR("failed to create lu_site hash with bits: %d\n", bits);
1126 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1127 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1128 INIT_LIST_HEAD(&bkt->lsb_lru);
1129 init_waitqueue_head(&bkt->lsb_marche_funebre);
1132 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1133 if (s->ls_stats == NULL) {
1134 cfs_hash_putref(s->ls_obj_hash);
1135 s->ls_obj_hash = NULL;
1139 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1140 0, "created", "created");
1141 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1142 0, "cache_hit", "cache_hit");
1143 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1144 0, "cache_miss", "cache_miss");
1145 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1146 0, "cache_race", "cache_race");
1147 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1148 0, "cache_death_race", "cache_death_race");
1149 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1150 0, "lru_purged", "lru_purged");
1152 INIT_LIST_HEAD(&s->ls_linkage);
1153 s->ls_top_dev = top;
1156 lu_ref_add(&top->ld_reference, "site-top", s);
1158 INIT_LIST_HEAD(&s->ls_ld_linkage);
1159 spin_lock_init(&s->ls_ld_lock);
1161 lu_dev_add_linkage(s, top);
1165 EXPORT_SYMBOL(lu_site_init);
1168 * Finalize \a s and release its resources.
1170 void lu_site_fini(struct lu_site *s)
1172 mutex_lock(&lu_sites_guard);
1173 list_del_init(&s->ls_linkage);
1174 mutex_unlock(&lu_sites_guard);
1176 if (s->ls_obj_hash != NULL) {
1177 cfs_hash_putref(s->ls_obj_hash);
1178 s->ls_obj_hash = NULL;
1181 if (s->ls_top_dev != NULL) {
1182 s->ls_top_dev->ld_site = NULL;
1183 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1184 lu_device_put(s->ls_top_dev);
1185 s->ls_top_dev = NULL;
1188 if (s->ls_stats != NULL)
1189 lprocfs_free_stats(&s->ls_stats);
1191 EXPORT_SYMBOL(lu_site_fini);
1194 * Called when initialization of stack for this site is completed.
1196 int lu_site_init_finish(struct lu_site *s)
1199 mutex_lock(&lu_sites_guard);
1200 result = lu_context_refill(&lu_shrink_env.le_ctx);
1202 list_add(&s->ls_linkage, &lu_sites);
1203 mutex_unlock(&lu_sites_guard);
1206 EXPORT_SYMBOL(lu_site_init_finish);
1209 * Acquire additional reference on device \a d
1211 void lu_device_get(struct lu_device *d)
1213 atomic_inc(&d->ld_ref);
1215 EXPORT_SYMBOL(lu_device_get);
1218 * Release reference on device \a d.
1220 void lu_device_put(struct lu_device *d)
1222 LASSERT(atomic_read(&d->ld_ref) > 0);
1223 atomic_dec(&d->ld_ref);
1225 EXPORT_SYMBOL(lu_device_put);
1228 * Initialize device \a d of type \a t.
1230 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1232 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1233 t->ldt_ops->ldto_start != NULL)
1234 t->ldt_ops->ldto_start(t);
1236 memset(d, 0, sizeof *d);
1238 lu_ref_init(&d->ld_reference);
1239 INIT_LIST_HEAD(&d->ld_linkage);
1243 EXPORT_SYMBOL(lu_device_init);
1246 * Finalize device \a d.
1248 void lu_device_fini(struct lu_device *d)
1250 struct lu_device_type *t = d->ld_type;
1252 if (d->ld_obd != NULL) {
1253 d->ld_obd->obd_lu_dev = NULL;
1257 lu_ref_fini(&d->ld_reference);
1258 LASSERTF(atomic_read(&d->ld_ref) == 0,
1259 "Refcount is %u\n", atomic_read(&d->ld_ref));
1260 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1262 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1263 t->ldt_ops->ldto_stop != NULL)
1264 t->ldt_ops->ldto_stop(t);
1266 EXPORT_SYMBOL(lu_device_fini);
1269 * Initialize object \a o that is part of compound object \a h and was created
1272 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1273 struct lu_device *d)
1275 memset(o, 0, sizeof(*o));
1279 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1280 INIT_LIST_HEAD(&o->lo_linkage);
1284 EXPORT_SYMBOL(lu_object_init);
1287 * Finalize object and release its resources.
1289 void lu_object_fini(struct lu_object *o)
1291 struct lu_device *dev = o->lo_dev;
1293 LASSERT(list_empty(&o->lo_linkage));
1296 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1302 EXPORT_SYMBOL(lu_object_fini);
1305 * Add object \a o as first layer of compound object \a h
1307 * This is typically called by the ->ldo_object_alloc() method of top-level
1310 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1312 list_move(&o->lo_linkage, &h->loh_layers);
1314 EXPORT_SYMBOL(lu_object_add_top);
1317 * Add object \a o as a layer of compound object, going after \a before.
1319 * This is typically called by the ->ldo_object_alloc() method of \a
1322 void lu_object_add(struct lu_object *before, struct lu_object *o)
1324 list_move(&o->lo_linkage, &before->lo_linkage);
1326 EXPORT_SYMBOL(lu_object_add);
1329 * Initialize compound object.
1331 int lu_object_header_init(struct lu_object_header *h)
1333 memset(h, 0, sizeof *h);
1334 atomic_set(&h->loh_ref, 1);
1335 INIT_HLIST_NODE(&h->loh_hash);
1336 INIT_LIST_HEAD(&h->loh_lru);
1337 INIT_LIST_HEAD(&h->loh_layers);
1338 lu_ref_init(&h->loh_reference);
1341 EXPORT_SYMBOL(lu_object_header_init);
1344 * Finalize compound object.
1346 void lu_object_header_fini(struct lu_object_header *h)
1348 LASSERT(list_empty(&h->loh_layers));
1349 LASSERT(list_empty(&h->loh_lru));
1350 LASSERT(hlist_unhashed(&h->loh_hash));
1351 lu_ref_fini(&h->loh_reference);
1353 EXPORT_SYMBOL(lu_object_header_fini);
1356 * Given a compound object, find its slice, corresponding to the device type
1359 struct lu_object *lu_object_locate(struct lu_object_header *h,
1360 const struct lu_device_type *dtype)
1362 struct lu_object *o;
1364 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1365 if (o->lo_dev->ld_type == dtype)
1370 EXPORT_SYMBOL(lu_object_locate);
1373 * Finalize and free devices in the device stack.
1375 * Finalize device stack by purging object cache, and calling
1376 * lu_device_type_operations::ldto_device_fini() and
1377 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1379 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1381 struct lu_site *site = top->ld_site;
1382 struct lu_device *scan;
1383 struct lu_device *next;
1385 lu_site_purge(env, site, ~0);
1386 for (scan = top; scan != NULL; scan = next) {
1387 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1388 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1389 lu_device_put(scan);
1393 lu_site_purge(env, site, ~0);
1395 for (scan = top; scan != NULL; scan = next) {
1396 const struct lu_device_type *ldt = scan->ld_type;
1397 struct obd_type *type;
1399 next = ldt->ldt_ops->ldto_device_free(env, scan);
1400 type = ldt->ldt_obd_type;
1403 class_put_type(type);
1407 EXPORT_SYMBOL(lu_stack_fini);
1411 * Maximal number of tld slots.
1413 LU_CONTEXT_KEY_NR = 40
1416 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1418 static DEFINE_SPINLOCK(lu_keys_guard);
1421 * Global counter incremented whenever key is registered, unregistered,
1422 * revived or quiesced. This is used to void unnecessary calls to
1423 * lu_context_refill(). No locking is provided, as initialization and shutdown
1424 * are supposed to be externally serialized.
1426 static unsigned key_set_version = 0;
1431 int lu_context_key_register(struct lu_context_key *key)
1436 LASSERT(key->lct_init != NULL);
1437 LASSERT(key->lct_fini != NULL);
1438 LASSERT(key->lct_tags != 0);
1439 LASSERT(key->lct_owner != NULL);
1442 spin_lock(&lu_keys_guard);
1443 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1444 if (lu_keys[i] == NULL) {
1446 atomic_set(&key->lct_used, 1);
1448 lu_ref_init(&key->lct_reference);
1454 spin_unlock(&lu_keys_guard);
1457 EXPORT_SYMBOL(lu_context_key_register);
1459 static void key_fini(struct lu_context *ctx, int index)
1461 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1462 struct lu_context_key *key;
1464 key = lu_keys[index];
1465 LASSERT(key != NULL);
1466 LASSERT(key->lct_fini != NULL);
1467 LASSERT(atomic_read(&key->lct_used) > 1);
1469 key->lct_fini(ctx, key, ctx->lc_value[index]);
1470 lu_ref_del(&key->lct_reference, "ctx", ctx);
1471 atomic_dec(&key->lct_used);
1473 LASSERT(key->lct_owner != NULL);
1474 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1475 LINVRNT(module_refcount(key->lct_owner) > 0);
1476 module_put(key->lct_owner);
1478 ctx->lc_value[index] = NULL;
1485 void lu_context_key_degister(struct lu_context_key *key)
1487 LASSERT(atomic_read(&key->lct_used) >= 1);
1488 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1490 lu_context_key_quiesce(key);
1493 spin_lock(&lu_keys_guard);
1494 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1495 if (lu_keys[key->lct_index]) {
1496 lu_keys[key->lct_index] = NULL;
1497 lu_ref_fini(&key->lct_reference);
1499 spin_unlock(&lu_keys_guard);
1501 LASSERTF(atomic_read(&key->lct_used) == 1,
1502 "key has instances: %d\n",
1503 atomic_read(&key->lct_used));
1505 EXPORT_SYMBOL(lu_context_key_degister);
1508 * Register a number of keys. This has to be called after all keys have been
1509 * initialized by a call to LU_CONTEXT_KEY_INIT().
1511 int lu_context_key_register_many(struct lu_context_key *k, ...)
1513 struct lu_context_key *key = k;
1519 result = lu_context_key_register(key);
1522 key = va_arg(args, struct lu_context_key *);
1523 } while (key != NULL);
1529 lu_context_key_degister(k);
1530 k = va_arg(args, struct lu_context_key *);
1537 EXPORT_SYMBOL(lu_context_key_register_many);
1540 * De-register a number of keys. This is a dual to
1541 * lu_context_key_register_many().
1543 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1549 lu_context_key_degister(k);
1550 k = va_arg(args, struct lu_context_key*);
1551 } while (k != NULL);
1554 EXPORT_SYMBOL(lu_context_key_degister_many);
1557 * Revive a number of keys.
1559 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1565 lu_context_key_revive(k);
1566 k = va_arg(args, struct lu_context_key*);
1567 } while (k != NULL);
1570 EXPORT_SYMBOL(lu_context_key_revive_many);
1573 * Quiescent a number of keys.
1575 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1581 lu_context_key_quiesce(k);
1582 k = va_arg(args, struct lu_context_key*);
1583 } while (k != NULL);
1586 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1589 * Return value associated with key \a key in context \a ctx.
1591 void *lu_context_key_get(const struct lu_context *ctx,
1592 const struct lu_context_key *key)
1594 LINVRNT(ctx->lc_state == LCS_ENTERED);
1595 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1596 LASSERT(lu_keys[key->lct_index] == key);
1597 return ctx->lc_value[key->lct_index];
1599 EXPORT_SYMBOL(lu_context_key_get);
1602 * List of remembered contexts. XXX document me.
1604 static struct list_head lu_context_remembered;
1607 * Destroy \a key in all remembered contexts. This is used to destroy key
1608 * values in "shared" contexts (like service threads), when a module owning
1609 * the key is about to be unloaded.
1611 void lu_context_key_quiesce(struct lu_context_key *key)
1613 struct lu_context *ctx;
1614 extern unsigned cl_env_cache_purge(unsigned nr);
1616 if (!(key->lct_tags & LCT_QUIESCENT)) {
1618 * XXX layering violation.
1620 cl_env_cache_purge(~0);
1621 key->lct_tags |= LCT_QUIESCENT;
1623 * XXX memory barrier has to go here.
1625 spin_lock(&lu_keys_guard);
1626 list_for_each_entry(ctx, &lu_context_remembered,
1628 key_fini(ctx, key->lct_index);
1629 spin_unlock(&lu_keys_guard);
1633 EXPORT_SYMBOL(lu_context_key_quiesce);
1635 void lu_context_key_revive(struct lu_context_key *key)
1637 key->lct_tags &= ~LCT_QUIESCENT;
1640 EXPORT_SYMBOL(lu_context_key_revive);
1642 static void keys_fini(struct lu_context *ctx)
1646 if (ctx->lc_value == NULL)
1649 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1652 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1653 ctx->lc_value = NULL;
1656 static int keys_fill(struct lu_context *ctx)
1660 LINVRNT(ctx->lc_value != NULL);
1661 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1662 struct lu_context_key *key;
1665 if (ctx->lc_value[i] == NULL && key != NULL &&
1666 (key->lct_tags & ctx->lc_tags) &&
1668 * Don't create values for a LCT_QUIESCENT key, as this
1669 * will pin module owning a key.
1671 !(key->lct_tags & LCT_QUIESCENT)) {
1674 LINVRNT(key->lct_init != NULL);
1675 LINVRNT(key->lct_index == i);
1677 value = key->lct_init(ctx, key);
1678 if (unlikely(IS_ERR(value)))
1679 return PTR_ERR(value);
1681 LASSERT(key->lct_owner != NULL);
1682 if (!(ctx->lc_tags & LCT_NOREF))
1683 try_module_get(key->lct_owner);
1684 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1685 atomic_inc(&key->lct_used);
1687 * This is the only place in the code, where an
1688 * element of ctx->lc_value[] array is set to non-NULL
1691 ctx->lc_value[i] = value;
1692 if (key->lct_exit != NULL)
1693 ctx->lc_tags |= LCT_HAS_EXIT;
1695 ctx->lc_version = key_set_version;
1700 static int keys_init(struct lu_context *ctx)
1702 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1703 if (likely(ctx->lc_value != NULL))
1704 return keys_fill(ctx);
1710 * Initialize context data-structure. Create values for all keys.
1712 int lu_context_init(struct lu_context *ctx, __u32 tags)
1716 memset(ctx, 0, sizeof *ctx);
1717 ctx->lc_state = LCS_INITIALIZED;
1718 ctx->lc_tags = tags;
1719 if (tags & LCT_REMEMBER) {
1720 spin_lock(&lu_keys_guard);
1721 list_add(&ctx->lc_remember, &lu_context_remembered);
1722 spin_unlock(&lu_keys_guard);
1724 INIT_LIST_HEAD(&ctx->lc_remember);
1727 rc = keys_init(ctx);
1729 lu_context_fini(ctx);
1733 EXPORT_SYMBOL(lu_context_init);
1736 * Finalize context data-structure. Destroy key values.
1738 void lu_context_fini(struct lu_context *ctx)
1740 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1741 ctx->lc_state = LCS_FINALIZED;
1743 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1744 LASSERT(list_empty(&ctx->lc_remember));
1747 } else { /* could race with key degister */
1748 spin_lock(&lu_keys_guard);
1750 list_del_init(&ctx->lc_remember);
1751 spin_unlock(&lu_keys_guard);
1754 EXPORT_SYMBOL(lu_context_fini);
1757 * Called before entering context.
1759 void lu_context_enter(struct lu_context *ctx)
1761 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1762 ctx->lc_state = LCS_ENTERED;
1764 EXPORT_SYMBOL(lu_context_enter);
1767 * Called after exiting from \a ctx
1769 void lu_context_exit(struct lu_context *ctx)
1773 LINVRNT(ctx->lc_state == LCS_ENTERED);
1774 ctx->lc_state = LCS_LEFT;
1775 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1776 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1777 if (ctx->lc_value[i] != NULL) {
1778 struct lu_context_key *key;
1781 LASSERT(key != NULL);
1782 if (key->lct_exit != NULL)
1784 key, ctx->lc_value[i]);
1789 EXPORT_SYMBOL(lu_context_exit);
1792 * Allocate for context all missing keys that were registered after context
1793 * creation. key_set_version is only changed in rare cases when modules
1794 * are loaded and removed.
1796 int lu_context_refill(struct lu_context *ctx)
1798 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1800 EXPORT_SYMBOL(lu_context_refill);
1803 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1804 * obd being added. Currently, this is only used on client side, specifically
1805 * for echo device client, for other stack (like ptlrpc threads), context are
1806 * predefined when the lu_device type are registered, during the module probe
1809 __u32 lu_context_tags_default = 0;
1810 __u32 lu_session_tags_default = 0;
1812 void lu_context_tags_update(__u32 tags)
1814 spin_lock(&lu_keys_guard);
1815 lu_context_tags_default |= tags;
1817 spin_unlock(&lu_keys_guard);
1819 EXPORT_SYMBOL(lu_context_tags_update);
1821 void lu_context_tags_clear(__u32 tags)
1823 spin_lock(&lu_keys_guard);
1824 lu_context_tags_default &= ~tags;
1826 spin_unlock(&lu_keys_guard);
1828 EXPORT_SYMBOL(lu_context_tags_clear);
1830 void lu_session_tags_update(__u32 tags)
1832 spin_lock(&lu_keys_guard);
1833 lu_session_tags_default |= tags;
1835 spin_unlock(&lu_keys_guard);
1837 EXPORT_SYMBOL(lu_session_tags_update);
1839 void lu_session_tags_clear(__u32 tags)
1841 spin_lock(&lu_keys_guard);
1842 lu_session_tags_default &= ~tags;
1844 spin_unlock(&lu_keys_guard);
1846 EXPORT_SYMBOL(lu_session_tags_clear);
1848 int lu_env_init(struct lu_env *env, __u32 tags)
1853 result = lu_context_init(&env->le_ctx, tags);
1854 if (likely(result == 0))
1855 lu_context_enter(&env->le_ctx);
1858 EXPORT_SYMBOL(lu_env_init);
1860 void lu_env_fini(struct lu_env *env)
1862 lu_context_exit(&env->le_ctx);
1863 lu_context_fini(&env->le_ctx);
1866 EXPORT_SYMBOL(lu_env_fini);
1868 int lu_env_refill(struct lu_env *env)
1872 result = lu_context_refill(&env->le_ctx);
1873 if (result == 0 && env->le_ses != NULL)
1874 result = lu_context_refill(env->le_ses);
1877 EXPORT_SYMBOL(lu_env_refill);
1880 * Currently, this API will only be used by echo client.
1881 * Because echo client and normal lustre client will share
1882 * same cl_env cache. So echo client needs to refresh
1883 * the env context after it get one from the cache, especially
1884 * when normal client and echo client co-exist in the same client.
1886 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1891 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1892 env->le_ctx.lc_version = 0;
1893 env->le_ctx.lc_tags |= ctags;
1896 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1897 env->le_ses->lc_version = 0;
1898 env->le_ses->lc_tags |= stags;
1901 result = lu_env_refill(env);
1905 EXPORT_SYMBOL(lu_env_refill_by_tags);
1907 static struct shrinker *lu_site_shrinker;
1909 typedef struct lu_site_stats{
1910 unsigned lss_populated;
1911 unsigned lss_max_search;
1916 static void lu_site_stats_get(cfs_hash_t *hs,
1917 lu_site_stats_t *stats, int populated)
1922 cfs_hash_for_each_bucket(hs, &bd, i) {
1923 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1924 struct hlist_head *hhead;
1926 cfs_hash_bd_lock(hs, &bd, 1);
1927 stats->lss_busy += bkt->lsb_busy;
1928 stats->lss_total += cfs_hash_bd_count_get(&bd);
1929 stats->lss_max_search = max((int)stats->lss_max_search,
1930 cfs_hash_bd_depmax_get(&bd));
1932 cfs_hash_bd_unlock(hs, &bd, 1);
1936 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1937 if (!hlist_empty(hhead))
1938 stats->lss_populated++;
1940 cfs_hash_bd_unlock(hs, &bd, 1);
1946 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
1947 struct shrink_control *sc)
1949 lu_site_stats_t stats;
1951 struct lu_site *tmp;
1952 unsigned long cached = 0;
1954 if (!(sc->gfp_mask & __GFP_FS))
1957 mutex_lock(&lu_sites_guard);
1958 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1959 memset(&stats, 0, sizeof(stats));
1960 lu_site_stats_get(s->ls_obj_hash, &stats, 0);
1961 cached += stats.lss_total - stats.lss_busy;
1963 mutex_unlock(&lu_sites_guard);
1965 cached = (cached / 100) * sysctl_vfs_cache_pressure;
1966 CDEBUG(D_INODE, "%ld objects cached\n", cached);
1970 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
1971 struct shrink_control *sc)
1974 struct lu_site *tmp;
1975 unsigned long remain = sc->nr_to_scan;
1978 if (!(sc->gfp_mask & __GFP_FS))
1979 /* We must not take the lu_sites_guard lock when
1980 * __GFP_FS is *not* set because of the deadlock
1981 * possibility detailed above. Additionally,
1982 * since we cannot determine the number of
1983 * objects in the cache without taking this
1984 * lock, we're in a particularly tough spot. As
1985 * a result, we'll just lie and say our cache is
1986 * empty. This _should_ be ok, as we can't
1987 * reclaim objects when __GFP_FS is *not* set
1992 mutex_lock(&lu_sites_guard);
1993 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1994 remain = lu_site_purge(&lu_shrink_env, s, remain);
1996 * Move just shrunk site to the tail of site list to
1997 * assure shrinking fairness.
1999 list_move_tail(&s->ls_linkage, &splice);
2001 list_splice(&splice, lu_sites.prev);
2002 mutex_unlock(&lu_sites_guard);
2004 return sc->nr_to_scan - remain;
2007 #ifndef HAVE_SHRINKER_COUNT
2009 * There exists a potential lock inversion deadlock scenario when using
2010 * Lustre on top of ZFS. This occurs between one of ZFS's
2011 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
2012 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
2013 * while thread B will take the ht_lock and sleep on the lu_sites_guard
2014 * lock. Obviously neither thread will wake and drop their respective hold
2017 * To prevent this from happening we must ensure the lu_sites_guard lock is
2018 * not taken while down this code path. ZFS reliably does not set the
2019 * __GFP_FS bit in its code paths, so this can be used to determine if it
2020 * is safe to take the lu_sites_guard lock.
2022 * Ideally we should accurately return the remaining number of cached
2023 * objects without taking the lu_sites_guard lock, but this is not
2024 * possible in the current implementation.
2026 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
2029 struct shrink_control scv = {
2030 .nr_to_scan = shrink_param(sc, nr_to_scan),
2031 .gfp_mask = shrink_param(sc, gfp_mask)
2033 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
2034 struct shrinker* shrinker = NULL;
2038 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
2040 lu_cache_shrink_scan(shrinker, &scv);
2042 cached = lu_cache_shrink_count(shrinker, &scv);
2043 if (scv.nr_to_scan == 0)
2044 CDEBUG(D_INODE, "%d objects cached\n", cached);
2048 #endif /* HAVE_SHRINKER_COUNT */
2056 * Environment to be used in debugger, contains all tags.
2058 struct lu_env lu_debugging_env;
2061 * Debugging printer function using printk().
2063 int lu_printk_printer(const struct lu_env *env,
2064 void *unused, const char *format, ...)
2068 va_start(args, format);
2069 vprintk(format, args);
2074 int lu_debugging_setup(void)
2076 return lu_env_init(&lu_debugging_env, ~0);
2079 void lu_context_keys_dump(void)
2083 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2084 struct lu_context_key *key;
2088 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2089 i, key, key->lct_tags,
2090 key->lct_init, key->lct_fini, key->lct_exit,
2091 key->lct_index, atomic_read(&key->lct_used),
2092 key->lct_owner ? key->lct_owner->name : "",
2094 lu_ref_print(&key->lct_reference);
2098 EXPORT_SYMBOL(lu_context_keys_dump);
2099 #endif /* __KERNEL__ */
2102 * Initialization of global lu_* data.
2104 int lu_global_init(void)
2107 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2108 lu_cache_shrink_count, lu_cache_shrink_scan);
2110 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2112 INIT_LIST_HEAD(&lu_device_types);
2113 INIT_LIST_HEAD(&lu_context_remembered);
2114 INIT_LIST_HEAD(&lu_sites);
2116 result = lu_ref_global_init();
2120 LU_CONTEXT_KEY_INIT(&lu_global_key);
2121 result = lu_context_key_register(&lu_global_key);
2126 * At this level, we don't know what tags are needed, so allocate them
2127 * conservatively. This should not be too bad, because this
2128 * environment is global.
2130 mutex_lock(&lu_sites_guard);
2131 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2132 mutex_unlock(&lu_sites_guard);
2137 * seeks estimation: 3 seeks to read a record from oi, one to read
2138 * inode, one for ea. Unfortunately setting this high value results in
2139 * lu_object/inode cache consuming all the memory.
2141 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2142 if (lu_site_shrinker == NULL)
2149 * Dual to lu_global_init().
2151 void lu_global_fini(void)
2153 if (lu_site_shrinker != NULL) {
2154 remove_shrinker(lu_site_shrinker);
2155 lu_site_shrinker = NULL;
2158 lu_context_key_degister(&lu_global_key);
2161 * Tear shrinker environment down _after_ de-registering
2162 * lu_global_key, because the latter has a value in the former.
2164 mutex_lock(&lu_sites_guard);
2165 lu_env_fini(&lu_shrink_env);
2166 mutex_unlock(&lu_sites_guard);
2168 lu_ref_global_fini();
2171 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2174 struct lprocfs_counter ret;
2176 lprocfs_stats_collect(stats, idx, &ret);
2177 return (__u32)ret.lc_count;
2184 * Output site statistical counters into a buffer. Suitable for
2185 * lprocfs_rd_*()-style functions.
2187 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2189 lu_site_stats_t stats;
2191 memset(&stats, 0, sizeof(stats));
2192 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2194 return seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2197 stats.lss_populated,
2198 CFS_HASH_NHLIST(s->ls_obj_hash),
2199 stats.lss_max_search,
2200 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2201 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2202 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2203 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2204 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2205 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2207 EXPORT_SYMBOL(lu_site_stats_seq_print);
2209 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
2211 lu_site_stats_t stats;
2213 memset(&stats, 0, sizeof(stats));
2214 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2216 return snprintf(page, count, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2219 stats.lss_populated,
2220 CFS_HASH_NHLIST(s->ls_obj_hash),
2221 stats.lss_max_search,
2222 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2223 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2224 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2225 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2226 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2227 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2229 EXPORT_SYMBOL(lu_site_stats_print);
2232 * Helper function to initialize a number of kmem slab caches at once.
2234 int lu_kmem_init(struct lu_kmem_descr *caches)
2237 struct lu_kmem_descr *iter = caches;
2239 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2240 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2243 if (*iter->ckd_cache == NULL) {
2245 /* free all previously allocated caches */
2246 lu_kmem_fini(caches);
2252 EXPORT_SYMBOL(lu_kmem_init);
2255 * Helper function to finalize a number of kmem slab cached at once. Dual to
2258 void lu_kmem_fini(struct lu_kmem_descr *caches)
2260 for (; caches->ckd_cache != NULL; ++caches) {
2261 if (*caches->ckd_cache != NULL) {
2262 kmem_cache_destroy(*caches->ckd_cache);
2263 *caches->ckd_cache = NULL;
2267 EXPORT_SYMBOL(lu_kmem_fini);
2270 * Temporary solution to be able to assign fid in ->do_create()
2271 * till we have fully-functional OST fids
2273 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2274 const struct lu_fid *fid)
2276 struct lu_site *s = o->lo_dev->ld_site;
2277 struct lu_fid *old = &o->lo_header->loh_fid;
2278 struct lu_site_bkt_data *bkt;
2279 struct lu_object *shadow;
2280 wait_queue_t waiter;
2285 LASSERT(fid_is_zero(old));
2287 hs = s->ls_obj_hash;
2288 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2289 shadow = htable_lookup(s, &bd, fid, &waiter, &version);
2290 /* supposed to be unique */
2291 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2293 bkt = cfs_hash_bd_extra_get(hs, &bd);
2294 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2296 cfs_hash_bd_unlock(hs, &bd, 1);
2298 EXPORT_SYMBOL(lu_object_assign_fid);
2301 * allocates object with 0 (non-assiged) fid
2302 * XXX: temporary solution to be able to assign fid in ->do_create()
2303 * till we have fully-functional OST fids
2305 struct lu_object *lu_object_anon(const struct lu_env *env,
2306 struct lu_device *dev,
2307 const struct lu_object_conf *conf)
2310 struct lu_object *o;
2313 o = lu_object_alloc(env, dev, &fid, conf);
2317 EXPORT_SYMBOL(lu_object_anon);
2319 struct lu_buf LU_BUF_NULL = {
2323 EXPORT_SYMBOL(LU_BUF_NULL);
2325 void lu_buf_free(struct lu_buf *buf)
2329 LASSERT(buf->lb_len > 0);
2330 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2335 EXPORT_SYMBOL(lu_buf_free);
2337 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2340 LASSERT(buf->lb_buf == NULL);
2341 LASSERT(buf->lb_len == 0);
2342 OBD_ALLOC_LARGE(buf->lb_buf, size);
2343 if (likely(buf->lb_buf))
2346 EXPORT_SYMBOL(lu_buf_alloc);
2348 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2351 lu_buf_alloc(buf, size);
2353 EXPORT_SYMBOL(lu_buf_realloc);
2355 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2357 if (buf->lb_buf == NULL && buf->lb_len == 0)
2358 lu_buf_alloc(buf, len);
2360 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2361 lu_buf_realloc(buf, len);
2365 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2368 * Increase the size of the \a buf.
2369 * preserves old data in buffer
2370 * old buffer remains unchanged on error
2371 * \retval 0 or -ENOMEM
2373 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2377 if (len <= buf->lb_len)
2380 OBD_ALLOC_LARGE(ptr, len);
2384 /* Free the old buf */
2385 if (buf->lb_buf != NULL) {
2386 memcpy(ptr, buf->lb_buf, buf->lb_len);
2387 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2394 EXPORT_SYMBOL(lu_buf_check_and_grow);