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>
48 #include <linux/module.h>
49 #include <libcfs/libcfs_hash.h> /* hash_long() */
50 #include <obd_class.h>
51 #include <obd_support.h>
52 #include <lustre_disk.h>
53 #include <lustre_fid.h>
54 #include <lu_object.h>
56 #include <libcfs/list.h>
59 LU_CACHE_PERCENT_MAX = 50,
60 LU_CACHE_PERCENT_DEFAULT = 20
63 #define LU_CACHE_NR_MAX_ADJUST 128
64 #define LU_CACHE_NR_UNLIMITED -1
65 #define LU_CACHE_NR_DEFAULT LU_CACHE_NR_UNLIMITED
66 #define LU_CACHE_NR_LDISKFS_LIMIT LU_CACHE_NR_UNLIMITED
67 /** This is set to roughly (20 * OSS_NTHRS_MAX) to prevent thrashing */
68 #define LU_CACHE_NR_ZFS_LIMIT 10240
70 #define LU_SITE_BITS_MIN 12
71 #define LU_SITE_BITS_MAX 24
73 * total 256 buckets, we don't want too many buckets because:
74 * - consume too much memory
75 * - avoid unbalanced LRU list
77 #define LU_SITE_BKT_BITS 8
80 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
81 CFS_MODULE_PARM(lu_cache_percent, "i", int, 0644,
82 "Percentage of memory to be used as lu_object cache");
84 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
85 CFS_MODULE_PARM(lu_cache_nr, "l", long, 0644,
86 "Maximum number of objects in lu_object cache");
88 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
91 * Decrease reference counter on object. If last reference is freed, return
92 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
93 * case, free object immediately.
95 void lu_object_put(const struct lu_env *env, struct lu_object *o)
97 struct lu_site_bkt_data *bkt;
98 struct lu_object_header *top;
100 struct lu_object *orig;
102 const struct lu_fid *fid;
105 site = o->lo_dev->ld_site;
109 * till we have full fids-on-OST implemented anonymous objects
110 * are possible in OSP. such an object isn't listed in the site
111 * so we should not remove it from the site.
113 fid = lu_object_fid(o);
114 if (fid_is_zero(fid)) {
115 LASSERT(top->loh_hash.next == NULL
116 && top->loh_hash.pprev == NULL);
117 LASSERT(list_empty(&top->loh_lru));
118 if (!atomic_dec_and_test(&top->loh_ref))
120 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
121 if (o->lo_ops->loo_object_release != NULL)
122 o->lo_ops->loo_object_release(env, o);
124 lu_object_free(env, orig);
128 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
129 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
131 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
132 if (lu_object_is_dying(top)) {
135 * somebody may be waiting for this, currently only
136 * used for cl_object, see cl_object_put_last().
138 wake_up_all(&bkt->lsb_marche_funebre);
143 LASSERT(bkt->lsb_busy > 0);
146 * When last reference is released, iterate over object
147 * layers, and notify them that object is no longer busy.
149 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
150 if (o->lo_ops->loo_object_release != NULL)
151 o->lo_ops->loo_object_release(env, o);
154 if (!lu_object_is_dying(top)) {
155 LASSERT(list_empty(&top->loh_lru));
156 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
157 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
162 * If object is dying (will not be cached), removed it
163 * from hash table and LRU.
165 * This is done with hash table and LRU lists locked. As the only
166 * way to acquire first reference to previously unreferenced
167 * object is through hash-table lookup (lu_object_find()),
168 * or LRU scanning (lu_site_purge()), that are done under hash-table
169 * and LRU lock, no race with concurrent object lookup is possible
170 * and we can safely destroy object below.
172 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
173 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
174 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
176 * Object was already removed from hash and lru above, can
179 lu_object_free(env, orig);
181 EXPORT_SYMBOL(lu_object_put);
184 * Put object and don't keep in cache. This is temporary solution for
185 * multi-site objects when its layering is not constant.
187 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
189 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
190 return lu_object_put(env, o);
192 EXPORT_SYMBOL(lu_object_put_nocache);
195 * Kill the object and take it out of LRU cache.
196 * Currently used by client code for layout change.
198 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
200 struct lu_object_header *top;
203 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
204 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
205 cfs_hash_t *obj_hash = o->lo_dev->ld_site->ls_obj_hash;
208 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
209 list_del_init(&top->loh_lru);
210 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
211 cfs_hash_bd_unlock(obj_hash, &bd, 1);
214 EXPORT_SYMBOL(lu_object_unhash);
217 * Allocate new object.
219 * This follows object creation protocol, described in the comment within
220 * struct lu_device_operations definition.
222 static struct lu_object *lu_object_alloc(const struct lu_env *env,
223 struct lu_device *dev,
224 const struct lu_fid *f,
225 const struct lu_object_conf *conf)
227 struct lu_object *scan;
228 struct lu_object *top;
229 struct list_head *layers;
230 unsigned int init_mask = 0;
231 unsigned int init_flag;
237 * Create top-level object slice. This will also create
240 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
242 RETURN(ERR_PTR(-ENOMEM));
246 * This is the only place where object fid is assigned. It's constant
249 top->lo_header->loh_fid = *f;
250 layers = &top->lo_header->loh_layers;
254 * Call ->loo_object_init() repeatedly, until no more new
255 * object slices are created.
259 list_for_each_entry(scan, layers, lo_linkage) {
260 if (init_mask & init_flag)
263 scan->lo_header = top->lo_header;
264 result = scan->lo_ops->loo_object_init(env, scan, conf);
266 lu_object_free(env, top);
267 RETURN(ERR_PTR(result));
269 init_mask |= init_flag;
275 list_for_each_entry_reverse(scan, layers, lo_linkage) {
276 if (scan->lo_ops->loo_object_start != NULL) {
277 result = scan->lo_ops->loo_object_start(env, scan);
279 lu_object_free(env, top);
280 RETURN(ERR_PTR(result));
285 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
292 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
294 struct lu_site_bkt_data *bkt;
295 struct lu_site *site;
296 struct lu_object *scan;
297 struct list_head *layers;
298 struct list_head splice;
300 site = o->lo_dev->ld_site;
301 layers = &o->lo_header->loh_layers;
302 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
304 * First call ->loo_object_delete() method to release all resources.
306 list_for_each_entry_reverse(scan, layers, lo_linkage) {
307 if (scan->lo_ops->loo_object_delete != NULL)
308 scan->lo_ops->loo_object_delete(env, scan);
312 * Then, splice object layers into stand-alone list, and call
313 * ->loo_object_free() on all layers to free memory. Splice is
314 * necessary, because lu_object_header is freed together with the
317 INIT_LIST_HEAD(&splice);
318 list_splice_init(layers, &splice);
319 while (!list_empty(&splice)) {
321 * Free layers in bottom-to-top order, so that object header
322 * lives as long as possible and ->loo_object_free() methods
323 * can look at its contents.
325 o = container_of0(splice.prev, struct lu_object, lo_linkage);
326 list_del_init(&o->lo_linkage);
327 LASSERT(o->lo_ops->loo_object_free != NULL);
328 o->lo_ops->loo_object_free(env, o);
331 if (waitqueue_active(&bkt->lsb_marche_funebre))
332 wake_up_all(&bkt->lsb_marche_funebre);
336 * Free \a nr objects from the cold end of the site LRU list.
338 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
340 struct lu_object_header *h;
341 struct lu_object_header *temp;
342 struct lu_site_bkt_data *bkt;
345 struct list_head dispose;
352 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
355 INIT_LIST_HEAD(&dispose);
357 * Under LRU list lock, scan LRU list and move unreferenced objects to
358 * the dispose list, removing them from LRU and hash table.
360 start = s->ls_purge_start;
361 bnr = (nr == ~0) ? -1 : nr / CFS_HASH_NBKT(s->ls_obj_hash) + 1;
364 * It doesn't make any sense to make purge threads parallel, that can
365 * only bring troubles to us. See LU-5331.
367 mutex_lock(&s->ls_purge_mutex);
369 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
373 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
374 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
376 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
377 LASSERT(atomic_read(&h->loh_ref) == 0);
379 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
380 LASSERT(bd.bd_bucket == bd2.bd_bucket);
382 cfs_hash_bd_del_locked(s->ls_obj_hash,
384 list_move(&h->loh_lru, &dispose);
388 if (nr != ~0 && --nr == 0)
391 if (count > 0 && --count == 0)
395 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
398 * Free everything on the dispose list. This is safe against
399 * races due to the reasons described in lu_object_put().
401 while (!list_empty(&dispose)) {
402 h = container_of0(dispose.next,
403 struct lu_object_header, loh_lru);
404 list_del_init(&h->loh_lru);
405 lu_object_free(env, lu_object_top(h));
406 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
412 mutex_unlock(&s->ls_purge_mutex);
414 if (nr != 0 && did_sth && start != 0) {
415 start = 0; /* restart from the first bucket */
418 /* race on s->ls_purge_start, but nobody cares */
419 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
423 EXPORT_SYMBOL(lu_site_purge);
428 * Code below has to jump through certain loops to output object description
429 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
430 * composes object description from strings that are parts of _lines_ of
431 * output (i.e., strings that are not terminated by newline). This doesn't fit
432 * very well into libcfs_debug_msg() interface that assumes that each message
433 * supplied to it is a self-contained output line.
435 * To work around this, strings are collected in a temporary buffer
436 * (implemented as a value of lu_cdebug_key key), until terminating newline
437 * character is detected.
445 * XXX overflow is not handled correctly.
450 struct lu_cdebug_data {
454 char lck_area[LU_CDEBUG_LINE];
457 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
458 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
461 * Key, holding temporary buffer. This key is registered very early by
464 struct lu_context_key lu_global_key = {
465 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
466 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
467 .lct_init = lu_global_key_init,
468 .lct_fini = lu_global_key_fini
472 * Printer function emitting messages through libcfs_debug_msg().
474 int lu_cdebug_printer(const struct lu_env *env,
475 void *cookie, const char *format, ...)
477 struct libcfs_debug_msg_data *msgdata = cookie;
478 struct lu_cdebug_data *key;
483 va_start(args, format);
485 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
486 LASSERT(key != NULL);
488 used = strlen(key->lck_area);
489 complete = format[strlen(format) - 1] == '\n';
491 * Append new chunk to the buffer.
493 vsnprintf(key->lck_area + used,
494 ARRAY_SIZE(key->lck_area) - used, format, args);
496 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
497 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
498 key->lck_area[0] = 0;
503 EXPORT_SYMBOL(lu_cdebug_printer);
506 * Print object header.
508 void lu_object_header_print(const struct lu_env *env, void *cookie,
509 lu_printer_t printer,
510 const struct lu_object_header *hdr)
512 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
513 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
515 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
516 list_empty((struct list_head *)&hdr->loh_lru) ? \
518 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
520 EXPORT_SYMBOL(lu_object_header_print);
523 * Print human readable representation of the \a o to the \a printer.
525 void lu_object_print(const struct lu_env *env, void *cookie,
526 lu_printer_t printer, const struct lu_object *o)
528 static const char ruler[] = "........................................";
529 struct lu_object_header *top;
533 lu_object_header_print(env, cookie, printer, top);
534 (*printer)(env, cookie, "{\n");
536 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
538 * print `.' \a depth times followed by type name and address
540 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
541 o->lo_dev->ld_type->ldt_name, o);
543 if (o->lo_ops->loo_object_print != NULL)
544 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
546 (*printer)(env, cookie, "\n");
549 (*printer)(env, cookie, "} header@%p\n", top);
551 EXPORT_SYMBOL(lu_object_print);
554 * Check object consistency.
556 int lu_object_invariant(const struct lu_object *o)
558 struct lu_object_header *top;
561 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
562 if (o->lo_ops->loo_object_invariant != NULL &&
563 !o->lo_ops->loo_object_invariant(o))
568 EXPORT_SYMBOL(lu_object_invariant);
570 static struct lu_object *htable_lookup(struct lu_site *s,
572 const struct lu_fid *f,
573 wait_queue_t *waiter,
576 struct lu_site_bkt_data *bkt;
577 struct lu_object_header *h;
578 struct hlist_node *hnode;
579 __u64 ver = cfs_hash_bd_version_get(bd);
582 return ERR_PTR(-ENOENT);
585 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
586 /* cfs_hash_bd_peek_locked is a somehow "internal" function
587 * of cfs_hash, it doesn't add refcount on object. */
588 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
590 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
591 return ERR_PTR(-ENOENT);
594 h = container_of0(hnode, struct lu_object_header, loh_hash);
595 if (likely(!lu_object_is_dying(h))) {
596 cfs_hash_get(s->ls_obj_hash, hnode);
597 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
598 list_del_init(&h->loh_lru);
599 return lu_object_top(h);
603 * Lookup found an object being destroyed this object cannot be
604 * returned (to assure that references to dying objects are eventually
605 * drained), and moreover, lookup has to wait until object is freed.
608 if (likely(waiter != NULL)) {
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);
615 return ERR_PTR(-EAGAIN);
618 static struct lu_object *htable_lookup_nowait(struct lu_site *s,
620 const struct lu_fid *f)
622 struct hlist_node *hnode;
623 struct lu_object_header *h;
625 /* cfs_hash_bd_peek_locked is a somehow "internal" function
626 * of cfs_hash, it doesn't add refcount on object. */
627 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
629 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
630 return ERR_PTR(-ENOENT);
633 h = container_of0(hnode, struct lu_object_header, loh_hash);
634 if (unlikely(lu_object_is_dying(h)))
635 return ERR_PTR(-ENOENT);
637 cfs_hash_get(s->ls_obj_hash, hnode);
638 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
639 list_del_init(&h->loh_lru);
640 return lu_object_top(h);
644 * Search cache for an object with the fid \a f. If such object is found,
645 * return it. Otherwise, create new object, insert it into cache and return
646 * it. In any case, additional reference is acquired on the returned object.
648 struct lu_object *lu_object_find(const struct lu_env *env,
649 struct lu_device *dev, const struct lu_fid *f,
650 const struct lu_object_conf *conf)
652 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
654 EXPORT_SYMBOL(lu_object_find);
657 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
658 * the calculation for the number of objects to reclaim is not covered by
659 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
660 * This ensures that many concurrent threads will not accidentally purge
663 static void lu_object_limit(const struct lu_env *env,
664 struct lu_device *dev)
668 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
671 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
672 nr = (__u64)lu_cache_nr;
674 lu_site_purge(env, dev->ld_site,
675 MIN(size - nr, LU_CACHE_NR_MAX_ADJUST));
680 static struct lu_object *lu_object_new(const struct lu_env *env,
681 struct lu_device *dev,
682 const struct lu_fid *f,
683 const struct lu_object_conf *conf)
688 struct lu_site_bkt_data *bkt;
690 o = lu_object_alloc(env, dev, f, conf);
691 if (unlikely(IS_ERR(o)))
694 hs = dev->ld_site->ls_obj_hash;
695 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
696 bkt = cfs_hash_bd_extra_get(hs, &bd);
697 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
699 cfs_hash_bd_unlock(hs, &bd, 1);
701 lu_object_limit(env, dev);
707 * Core logic of lu_object_find*() functions.
709 static struct lu_object *lu_object_find_try(const struct lu_env *env,
710 struct lu_device *dev,
711 const struct lu_fid *f,
712 const struct lu_object_conf *conf,
713 wait_queue_t *waiter)
716 struct lu_object *shadow;
723 * This uses standard index maintenance protocol:
725 * - search index under lock, and return object if found;
726 * - otherwise, unlock index, allocate new object;
727 * - lock index and search again;
728 * - if nothing is found (usual case), insert newly created
730 * - otherwise (race: other thread inserted object), free
731 * object just allocated.
735 * For "LOC_F_NEW" case, we are sure the object is new established.
736 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
737 * just alloc and insert directly.
739 * If dying object is found during index search, add @waiter to the
740 * site wait-queue and return ERR_PTR(-EAGAIN).
742 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
743 return lu_object_new(env, dev, f, conf);
747 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
748 o = htable_lookup(s, &bd, f, waiter, &version);
749 cfs_hash_bd_unlock(hs, &bd, 1);
750 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
754 * Allocate new object. This may result in rather complicated
755 * operations, including fld queries, inode loading, etc.
757 o = lu_object_alloc(env, dev, f, conf);
758 if (unlikely(IS_ERR(o)))
761 LASSERT(lu_fid_eq(lu_object_fid(o), f));
763 cfs_hash_bd_lock(hs, &bd, 1);
765 shadow = htable_lookup(s, &bd, f, waiter, &version);
766 if (likely(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT)) {
767 struct lu_site_bkt_data *bkt;
769 bkt = cfs_hash_bd_extra_get(hs, &bd);
770 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
772 cfs_hash_bd_unlock(hs, &bd, 1);
774 lu_object_limit(env, dev);
779 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
780 cfs_hash_bd_unlock(hs, &bd, 1);
781 lu_object_free(env, o);
786 * Much like lu_object_find(), but top level device of object is specifically
787 * \a dev rather than top level device of the site. This interface allows
788 * objects of different "stacking" to be created within the same site.
790 struct lu_object *lu_object_find_at(const struct lu_env *env,
791 struct lu_device *dev,
792 const struct lu_fid *f,
793 const struct lu_object_conf *conf)
795 struct lu_site_bkt_data *bkt;
796 struct lu_object *obj;
800 if (conf != NULL && conf->loc_flags & LOC_F_NOWAIT) {
801 obj = lu_object_find_try(env, dev, f, conf, NULL);
806 obj = lu_object_find_try(env, dev, f, conf, &wait);
807 if (obj != ERR_PTR(-EAGAIN))
810 * lu_object_find_try() already added waiter into the
813 waitq_wait(&wait, TASK_UNINTERRUPTIBLE);
814 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
815 remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
818 EXPORT_SYMBOL(lu_object_find_at);
821 * Try to find the object in cache without waiting for the dead object
822 * to be released nor allocating object if no cached one was found.
824 * The found object will be set as LU_OBJECT_HEARD_BANSHEE for purging.
826 void lu_object_purge(const struct lu_env *env, struct lu_device *dev,
827 const struct lu_fid *f)
829 struct lu_site *s = dev->ld_site;
830 cfs_hash_t *hs = s->ls_obj_hash;
834 cfs_hash_bd_get_and_lock(hs, f, &bd, 1);
835 o = htable_lookup_nowait(s, &bd, f);
836 cfs_hash_bd_unlock(hs, &bd, 1);
838 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
839 lu_object_put(env, o);
842 EXPORT_SYMBOL(lu_object_purge);
845 * Find object with given fid, and return its slice belonging to given device.
847 struct lu_object *lu_object_find_slice(const struct lu_env *env,
848 struct lu_device *dev,
849 const struct lu_fid *f,
850 const struct lu_object_conf *conf)
852 struct lu_object *top;
853 struct lu_object *obj;
855 top = lu_object_find(env, dev, f, conf);
857 obj = lu_object_locate(top->lo_header, dev->ld_type);
859 lu_object_put(env, top);
864 EXPORT_SYMBOL(lu_object_find_slice);
867 * Global list of all device types.
869 static struct list_head lu_device_types;
871 int lu_device_type_init(struct lu_device_type *ldt)
875 atomic_set(&ldt->ldt_device_nr, 0);
876 INIT_LIST_HEAD(&ldt->ldt_linkage);
877 if (ldt->ldt_ops->ldto_init)
878 result = ldt->ldt_ops->ldto_init(ldt);
881 spin_lock(&obd_types_lock);
882 list_add(&ldt->ldt_linkage, &lu_device_types);
883 spin_unlock(&obd_types_lock);
888 EXPORT_SYMBOL(lu_device_type_init);
890 void lu_device_type_fini(struct lu_device_type *ldt)
892 spin_lock(&obd_types_lock);
893 list_del_init(&ldt->ldt_linkage);
894 spin_unlock(&obd_types_lock);
895 if (ldt->ldt_ops->ldto_fini)
896 ldt->ldt_ops->ldto_fini(ldt);
898 EXPORT_SYMBOL(lu_device_type_fini);
901 * Global list of all sites on this node
903 static struct list_head lu_sites;
904 static DEFINE_MUTEX(lu_sites_guard);
907 * Global environment used by site shrinker.
909 static struct lu_env lu_shrink_env;
911 struct lu_site_print_arg {
912 struct lu_env *lsp_env;
914 lu_printer_t lsp_printer;
918 lu_site_obj_print(cfs_hash_t *hs, cfs_hash_bd_t *bd,
919 struct hlist_node *hnode, void *data)
921 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
922 struct lu_object_header *h;
924 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
925 if (!list_empty(&h->loh_layers)) {
926 const struct lu_object *o;
928 o = lu_object_top(h);
929 lu_object_print(arg->lsp_env, arg->lsp_cookie,
930 arg->lsp_printer, o);
932 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
933 arg->lsp_printer, h);
939 * Print all objects in \a s.
941 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
942 lu_printer_t printer)
944 struct lu_site_print_arg arg = {
945 .lsp_env = (struct lu_env *)env,
946 .lsp_cookie = cookie,
947 .lsp_printer = printer,
950 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
952 EXPORT_SYMBOL(lu_site_print);
955 * Return desired hash table order.
957 static unsigned int lu_htable_order(struct lu_device *top)
959 unsigned long cache_size;
963 * For ZFS based OSDs the cache should be disabled by default. This
964 * allows the ZFS ARC maximum flexibility in determining what buffers
965 * to cache. If Lustre has objects or buffer which it wants to ensure
966 * always stay cached it must maintain a hold on them.
968 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
969 lu_cache_percent = 1;
970 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
971 return LU_SITE_BITS_MIN;
975 * Calculate hash table size, assuming that we want reasonable
976 * performance when 20% of total memory is occupied by cache of
979 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
981 cache_size = totalram_pages;
983 #if BITS_PER_LONG == 32
984 /* limit hashtable size for lowmem systems to low RAM */
985 if (cache_size > 1 << (30 - PAGE_CACHE_SHIFT))
986 cache_size = 1 << (30 - PAGE_CACHE_SHIFT) * 3 / 4;
989 /* clear off unreasonable cache setting. */
990 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
991 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
992 " the range of (0, %u]. Will use default value: %u.\n",
993 lu_cache_percent, LU_CACHE_PERCENT_MAX,
994 LU_CACHE_PERCENT_DEFAULT);
996 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
998 cache_size = cache_size / 100 * lu_cache_percent *
999 (PAGE_CACHE_SIZE / 1024);
1001 for (bits = 1; (1 << bits) < cache_size; ++bits) {
1007 static unsigned lu_obj_hop_hash(cfs_hash_t *hs,
1008 const void *key, unsigned mask)
1010 struct lu_fid *fid = (struct lu_fid *)key;
1013 hash = fid_flatten32(fid);
1014 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
1015 hash = hash_long(hash, hs->hs_bkt_bits);
1017 /* give me another random factor */
1018 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
1020 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
1021 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
1026 static void *lu_obj_hop_object(struct hlist_node *hnode)
1028 return hlist_entry(hnode, struct lu_object_header, loh_hash);
1031 static void *lu_obj_hop_key(struct hlist_node *hnode)
1033 struct lu_object_header *h;
1035 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1039 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
1041 struct lu_object_header *h;
1043 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1044 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
1047 static void lu_obj_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
1049 struct lu_object_header *h;
1051 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1052 if (atomic_add_return(1, &h->loh_ref) == 1) {
1053 struct lu_site_bkt_data *bkt;
1056 cfs_hash_bd_get(hs, &h->loh_fid, &bd);
1057 bkt = cfs_hash_bd_extra_get(hs, &bd);
1062 static void lu_obj_hop_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
1064 LBUG(); /* we should never called it */
1067 cfs_hash_ops_t lu_site_hash_ops = {
1068 .hs_hash = lu_obj_hop_hash,
1069 .hs_key = lu_obj_hop_key,
1070 .hs_keycmp = lu_obj_hop_keycmp,
1071 .hs_object = lu_obj_hop_object,
1072 .hs_get = lu_obj_hop_get,
1073 .hs_put_locked = lu_obj_hop_put_locked,
1076 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1078 spin_lock(&s->ls_ld_lock);
1079 if (list_empty(&d->ld_linkage))
1080 list_add(&d->ld_linkage, &s->ls_ld_linkage);
1081 spin_unlock(&s->ls_ld_lock);
1083 EXPORT_SYMBOL(lu_dev_add_linkage);
1085 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1087 spin_lock(&s->ls_ld_lock);
1088 list_del_init(&d->ld_linkage);
1089 spin_unlock(&s->ls_ld_lock);
1091 EXPORT_SYMBOL(lu_dev_del_linkage);
1094 * Initialize site \a s, with \a d as the top level device.
1096 int lu_site_init(struct lu_site *s, struct lu_device *top)
1098 struct lu_site_bkt_data *bkt;
1105 memset(s, 0, sizeof *s);
1106 mutex_init(&s->ls_purge_mutex);
1107 bits = lu_htable_order(top);
1108 snprintf(name, 16, "lu_site_%s", top->ld_type->ldt_name);
1109 for (bits = clamp_t(typeof(bits), bits,
1110 LU_SITE_BITS_MIN, LU_SITE_BITS_MAX);
1111 bits >= LU_SITE_BITS_MIN; bits--) {
1112 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1113 bits - LU_SITE_BKT_BITS,
1116 CFS_HASH_SPIN_BKTLOCK |
1117 CFS_HASH_NO_ITEMREF |
1119 CFS_HASH_ASSERT_EMPTY |
1121 if (s->ls_obj_hash != NULL)
1125 if (s->ls_obj_hash == NULL) {
1126 CERROR("failed to create lu_site hash with bits: %d\n", bits);
1130 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1131 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1132 INIT_LIST_HEAD(&bkt->lsb_lru);
1133 init_waitqueue_head(&bkt->lsb_marche_funebre);
1136 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1137 if (s->ls_stats == NULL) {
1138 cfs_hash_putref(s->ls_obj_hash);
1139 s->ls_obj_hash = NULL;
1143 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1144 0, "created", "created");
1145 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1146 0, "cache_hit", "cache_hit");
1147 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1148 0, "cache_miss", "cache_miss");
1149 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1150 0, "cache_race", "cache_race");
1151 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1152 0, "cache_death_race", "cache_death_race");
1153 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1154 0, "lru_purged", "lru_purged");
1156 INIT_LIST_HEAD(&s->ls_linkage);
1157 s->ls_top_dev = top;
1160 lu_ref_add(&top->ld_reference, "site-top", s);
1162 INIT_LIST_HEAD(&s->ls_ld_linkage);
1163 spin_lock_init(&s->ls_ld_lock);
1165 lu_dev_add_linkage(s, top);
1169 EXPORT_SYMBOL(lu_site_init);
1172 * Finalize \a s and release its resources.
1174 void lu_site_fini(struct lu_site *s)
1176 mutex_lock(&lu_sites_guard);
1177 list_del_init(&s->ls_linkage);
1178 mutex_unlock(&lu_sites_guard);
1180 if (s->ls_obj_hash != NULL) {
1181 cfs_hash_putref(s->ls_obj_hash);
1182 s->ls_obj_hash = NULL;
1185 if (s->ls_top_dev != NULL) {
1186 s->ls_top_dev->ld_site = NULL;
1187 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1188 lu_device_put(s->ls_top_dev);
1189 s->ls_top_dev = NULL;
1192 if (s->ls_stats != NULL)
1193 lprocfs_free_stats(&s->ls_stats);
1195 EXPORT_SYMBOL(lu_site_fini);
1198 * Called when initialization of stack for this site is completed.
1200 int lu_site_init_finish(struct lu_site *s)
1203 mutex_lock(&lu_sites_guard);
1204 result = lu_context_refill(&lu_shrink_env.le_ctx);
1206 list_add(&s->ls_linkage, &lu_sites);
1207 mutex_unlock(&lu_sites_guard);
1210 EXPORT_SYMBOL(lu_site_init_finish);
1213 * Acquire additional reference on device \a d
1215 void lu_device_get(struct lu_device *d)
1217 atomic_inc(&d->ld_ref);
1219 EXPORT_SYMBOL(lu_device_get);
1222 * Release reference on device \a d.
1224 void lu_device_put(struct lu_device *d)
1226 LASSERT(atomic_read(&d->ld_ref) > 0);
1227 atomic_dec(&d->ld_ref);
1229 EXPORT_SYMBOL(lu_device_put);
1232 * Initialize device \a d of type \a t.
1234 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1236 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1237 t->ldt_ops->ldto_start != NULL)
1238 t->ldt_ops->ldto_start(t);
1240 memset(d, 0, sizeof *d);
1242 lu_ref_init(&d->ld_reference);
1243 INIT_LIST_HEAD(&d->ld_linkage);
1247 EXPORT_SYMBOL(lu_device_init);
1250 * Finalize device \a d.
1252 void lu_device_fini(struct lu_device *d)
1254 struct lu_device_type *t = d->ld_type;
1256 if (d->ld_obd != NULL) {
1257 d->ld_obd->obd_lu_dev = NULL;
1261 lu_ref_fini(&d->ld_reference);
1262 LASSERTF(atomic_read(&d->ld_ref) == 0,
1263 "Refcount is %u\n", atomic_read(&d->ld_ref));
1264 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1266 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1267 t->ldt_ops->ldto_stop != NULL)
1268 t->ldt_ops->ldto_stop(t);
1270 EXPORT_SYMBOL(lu_device_fini);
1273 * Initialize object \a o that is part of compound object \a h and was created
1276 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1277 struct lu_device *d)
1279 memset(o, 0, sizeof(*o));
1283 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1284 INIT_LIST_HEAD(&o->lo_linkage);
1288 EXPORT_SYMBOL(lu_object_init);
1291 * Finalize object and release its resources.
1293 void lu_object_fini(struct lu_object *o)
1295 struct lu_device *dev = o->lo_dev;
1297 LASSERT(list_empty(&o->lo_linkage));
1300 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1306 EXPORT_SYMBOL(lu_object_fini);
1309 * Add object \a o as first layer of compound object \a h
1311 * This is typically called by the ->ldo_object_alloc() method of top-level
1314 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1316 list_move(&o->lo_linkage, &h->loh_layers);
1318 EXPORT_SYMBOL(lu_object_add_top);
1321 * Add object \a o as a layer of compound object, going after \a before.
1323 * This is typically called by the ->ldo_object_alloc() method of \a
1326 void lu_object_add(struct lu_object *before, struct lu_object *o)
1328 list_move(&o->lo_linkage, &before->lo_linkage);
1330 EXPORT_SYMBOL(lu_object_add);
1333 * Initialize compound object.
1335 int lu_object_header_init(struct lu_object_header *h)
1337 memset(h, 0, sizeof *h);
1338 atomic_set(&h->loh_ref, 1);
1339 INIT_HLIST_NODE(&h->loh_hash);
1340 INIT_LIST_HEAD(&h->loh_lru);
1341 INIT_LIST_HEAD(&h->loh_layers);
1342 lu_ref_init(&h->loh_reference);
1345 EXPORT_SYMBOL(lu_object_header_init);
1348 * Finalize compound object.
1350 void lu_object_header_fini(struct lu_object_header *h)
1352 LASSERT(list_empty(&h->loh_layers));
1353 LASSERT(list_empty(&h->loh_lru));
1354 LASSERT(hlist_unhashed(&h->loh_hash));
1355 lu_ref_fini(&h->loh_reference);
1357 EXPORT_SYMBOL(lu_object_header_fini);
1360 * Given a compound object, find its slice, corresponding to the device type
1363 struct lu_object *lu_object_locate(struct lu_object_header *h,
1364 const struct lu_device_type *dtype)
1366 struct lu_object *o;
1368 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1369 if (o->lo_dev->ld_type == dtype)
1374 EXPORT_SYMBOL(lu_object_locate);
1377 * Finalize and free devices in the device stack.
1379 * Finalize device stack by purging object cache, and calling
1380 * lu_device_type_operations::ldto_device_fini() and
1381 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1383 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1385 struct lu_site *site = top->ld_site;
1386 struct lu_device *scan;
1387 struct lu_device *next;
1389 lu_site_purge(env, site, ~0);
1390 for (scan = top; scan != NULL; scan = next) {
1391 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1392 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1393 lu_device_put(scan);
1397 lu_site_purge(env, site, ~0);
1399 for (scan = top; scan != NULL; scan = next) {
1400 const struct lu_device_type *ldt = scan->ld_type;
1401 struct obd_type *type;
1403 next = ldt->ldt_ops->ldto_device_free(env, scan);
1404 type = ldt->ldt_obd_type;
1407 class_put_type(type);
1411 EXPORT_SYMBOL(lu_stack_fini);
1415 * Maximal number of tld slots.
1417 LU_CONTEXT_KEY_NR = 40
1420 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1422 static DEFINE_SPINLOCK(lu_keys_guard);
1425 * Global counter incremented whenever key is registered, unregistered,
1426 * revived or quiesced. This is used to void unnecessary calls to
1427 * lu_context_refill(). No locking is provided, as initialization and shutdown
1428 * are supposed to be externally serialized.
1430 static unsigned key_set_version = 0;
1435 int lu_context_key_register(struct lu_context_key *key)
1440 LASSERT(key->lct_init != NULL);
1441 LASSERT(key->lct_fini != NULL);
1442 LASSERT(key->lct_tags != 0);
1443 LASSERT(key->lct_owner != NULL);
1446 spin_lock(&lu_keys_guard);
1447 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1448 if (lu_keys[i] == NULL) {
1450 atomic_set(&key->lct_used, 1);
1452 lu_ref_init(&key->lct_reference);
1458 spin_unlock(&lu_keys_guard);
1461 EXPORT_SYMBOL(lu_context_key_register);
1463 static void key_fini(struct lu_context *ctx, int index)
1465 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1466 struct lu_context_key *key;
1468 key = lu_keys[index];
1469 LASSERT(key != NULL);
1470 LASSERT(key->lct_fini != NULL);
1471 LASSERT(atomic_read(&key->lct_used) > 1);
1473 key->lct_fini(ctx, key, ctx->lc_value[index]);
1474 lu_ref_del(&key->lct_reference, "ctx", ctx);
1475 atomic_dec(&key->lct_used);
1477 LASSERT(key->lct_owner != NULL);
1478 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1479 LINVRNT(module_refcount(key->lct_owner) > 0);
1480 module_put(key->lct_owner);
1482 ctx->lc_value[index] = NULL;
1489 void lu_context_key_degister(struct lu_context_key *key)
1491 LASSERT(atomic_read(&key->lct_used) >= 1);
1492 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1494 lu_context_key_quiesce(key);
1497 spin_lock(&lu_keys_guard);
1498 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1499 if (lu_keys[key->lct_index]) {
1500 lu_keys[key->lct_index] = NULL;
1501 lu_ref_fini(&key->lct_reference);
1503 spin_unlock(&lu_keys_guard);
1505 LASSERTF(atomic_read(&key->lct_used) == 1,
1506 "key has instances: %d\n",
1507 atomic_read(&key->lct_used));
1509 EXPORT_SYMBOL(lu_context_key_degister);
1512 * Register a number of keys. This has to be called after all keys have been
1513 * initialized by a call to LU_CONTEXT_KEY_INIT().
1515 int lu_context_key_register_many(struct lu_context_key *k, ...)
1517 struct lu_context_key *key = k;
1523 result = lu_context_key_register(key);
1526 key = va_arg(args, struct lu_context_key *);
1527 } while (key != NULL);
1533 lu_context_key_degister(k);
1534 k = va_arg(args, struct lu_context_key *);
1541 EXPORT_SYMBOL(lu_context_key_register_many);
1544 * De-register a number of keys. This is a dual to
1545 * lu_context_key_register_many().
1547 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1553 lu_context_key_degister(k);
1554 k = va_arg(args, struct lu_context_key*);
1555 } while (k != NULL);
1558 EXPORT_SYMBOL(lu_context_key_degister_many);
1561 * Revive a number of keys.
1563 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1569 lu_context_key_revive(k);
1570 k = va_arg(args, struct lu_context_key*);
1571 } while (k != NULL);
1574 EXPORT_SYMBOL(lu_context_key_revive_many);
1577 * Quiescent a number of keys.
1579 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1585 lu_context_key_quiesce(k);
1586 k = va_arg(args, struct lu_context_key*);
1587 } while (k != NULL);
1590 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1593 * Return value associated with key \a key in context \a ctx.
1595 void *lu_context_key_get(const struct lu_context *ctx,
1596 const struct lu_context_key *key)
1598 LINVRNT(ctx->lc_state == LCS_ENTERED);
1599 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1600 LASSERT(lu_keys[key->lct_index] == key);
1601 return ctx->lc_value[key->lct_index];
1603 EXPORT_SYMBOL(lu_context_key_get);
1606 * List of remembered contexts. XXX document me.
1608 static struct list_head lu_context_remembered;
1611 * Destroy \a key in all remembered contexts. This is used to destroy key
1612 * values in "shared" contexts (like service threads), when a module owning
1613 * the key is about to be unloaded.
1615 void lu_context_key_quiesce(struct lu_context_key *key)
1617 struct lu_context *ctx;
1618 extern unsigned cl_env_cache_purge(unsigned nr);
1620 if (!(key->lct_tags & LCT_QUIESCENT)) {
1622 * XXX layering violation.
1624 cl_env_cache_purge(~0);
1625 key->lct_tags |= LCT_QUIESCENT;
1627 * XXX memory barrier has to go here.
1629 spin_lock(&lu_keys_guard);
1630 list_for_each_entry(ctx, &lu_context_remembered,
1632 key_fini(ctx, key->lct_index);
1633 spin_unlock(&lu_keys_guard);
1637 EXPORT_SYMBOL(lu_context_key_quiesce);
1639 void lu_context_key_revive(struct lu_context_key *key)
1641 key->lct_tags &= ~LCT_QUIESCENT;
1644 EXPORT_SYMBOL(lu_context_key_revive);
1646 static void keys_fini(struct lu_context *ctx)
1650 if (ctx->lc_value == NULL)
1653 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1656 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1657 ctx->lc_value = NULL;
1660 static int keys_fill(struct lu_context *ctx)
1664 LINVRNT(ctx->lc_value != NULL);
1665 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1666 struct lu_context_key *key;
1669 if (ctx->lc_value[i] == NULL && key != NULL &&
1670 (key->lct_tags & ctx->lc_tags) &&
1672 * Don't create values for a LCT_QUIESCENT key, as this
1673 * will pin module owning a key.
1675 !(key->lct_tags & LCT_QUIESCENT)) {
1678 LINVRNT(key->lct_init != NULL);
1679 LINVRNT(key->lct_index == i);
1681 value = key->lct_init(ctx, key);
1682 if (unlikely(IS_ERR(value)))
1683 return PTR_ERR(value);
1685 LASSERT(key->lct_owner != NULL);
1686 if (!(ctx->lc_tags & LCT_NOREF))
1687 try_module_get(key->lct_owner);
1688 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1689 atomic_inc(&key->lct_used);
1691 * This is the only place in the code, where an
1692 * element of ctx->lc_value[] array is set to non-NULL
1695 ctx->lc_value[i] = value;
1696 if (key->lct_exit != NULL)
1697 ctx->lc_tags |= LCT_HAS_EXIT;
1699 ctx->lc_version = key_set_version;
1704 static int keys_init(struct lu_context *ctx)
1706 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1707 if (likely(ctx->lc_value != NULL))
1708 return keys_fill(ctx);
1714 * Initialize context data-structure. Create values for all keys.
1716 int lu_context_init(struct lu_context *ctx, __u32 tags)
1720 memset(ctx, 0, sizeof *ctx);
1721 ctx->lc_state = LCS_INITIALIZED;
1722 ctx->lc_tags = tags;
1723 if (tags & LCT_REMEMBER) {
1724 spin_lock(&lu_keys_guard);
1725 list_add(&ctx->lc_remember, &lu_context_remembered);
1726 spin_unlock(&lu_keys_guard);
1728 INIT_LIST_HEAD(&ctx->lc_remember);
1731 rc = keys_init(ctx);
1733 lu_context_fini(ctx);
1737 EXPORT_SYMBOL(lu_context_init);
1740 * Finalize context data-structure. Destroy key values.
1742 void lu_context_fini(struct lu_context *ctx)
1744 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1745 ctx->lc_state = LCS_FINALIZED;
1747 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1748 LASSERT(list_empty(&ctx->lc_remember));
1751 } else { /* could race with key degister */
1752 spin_lock(&lu_keys_guard);
1754 list_del_init(&ctx->lc_remember);
1755 spin_unlock(&lu_keys_guard);
1758 EXPORT_SYMBOL(lu_context_fini);
1761 * Called before entering context.
1763 void lu_context_enter(struct lu_context *ctx)
1765 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1766 ctx->lc_state = LCS_ENTERED;
1768 EXPORT_SYMBOL(lu_context_enter);
1771 * Called after exiting from \a ctx
1773 void lu_context_exit(struct lu_context *ctx)
1777 LINVRNT(ctx->lc_state == LCS_ENTERED);
1778 ctx->lc_state = LCS_LEFT;
1779 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1780 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1781 if (ctx->lc_value[i] != NULL) {
1782 struct lu_context_key *key;
1785 LASSERT(key != NULL);
1786 if (key->lct_exit != NULL)
1788 key, ctx->lc_value[i]);
1793 EXPORT_SYMBOL(lu_context_exit);
1796 * Allocate for context all missing keys that were registered after context
1797 * creation. key_set_version is only changed in rare cases when modules
1798 * are loaded and removed.
1800 int lu_context_refill(struct lu_context *ctx)
1802 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1804 EXPORT_SYMBOL(lu_context_refill);
1807 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1808 * obd being added. Currently, this is only used on client side, specifically
1809 * for echo device client, for other stack (like ptlrpc threads), context are
1810 * predefined when the lu_device type are registered, during the module probe
1813 __u32 lu_context_tags_default = 0;
1814 __u32 lu_session_tags_default = 0;
1816 void lu_context_tags_update(__u32 tags)
1818 spin_lock(&lu_keys_guard);
1819 lu_context_tags_default |= tags;
1821 spin_unlock(&lu_keys_guard);
1823 EXPORT_SYMBOL(lu_context_tags_update);
1825 void lu_context_tags_clear(__u32 tags)
1827 spin_lock(&lu_keys_guard);
1828 lu_context_tags_default &= ~tags;
1830 spin_unlock(&lu_keys_guard);
1832 EXPORT_SYMBOL(lu_context_tags_clear);
1834 void lu_session_tags_update(__u32 tags)
1836 spin_lock(&lu_keys_guard);
1837 lu_session_tags_default |= tags;
1839 spin_unlock(&lu_keys_guard);
1841 EXPORT_SYMBOL(lu_session_tags_update);
1843 void lu_session_tags_clear(__u32 tags)
1845 spin_lock(&lu_keys_guard);
1846 lu_session_tags_default &= ~tags;
1848 spin_unlock(&lu_keys_guard);
1850 EXPORT_SYMBOL(lu_session_tags_clear);
1852 int lu_env_init(struct lu_env *env, __u32 tags)
1857 result = lu_context_init(&env->le_ctx, tags);
1858 if (likely(result == 0))
1859 lu_context_enter(&env->le_ctx);
1862 EXPORT_SYMBOL(lu_env_init);
1864 void lu_env_fini(struct lu_env *env)
1866 lu_context_exit(&env->le_ctx);
1867 lu_context_fini(&env->le_ctx);
1870 EXPORT_SYMBOL(lu_env_fini);
1872 int lu_env_refill(struct lu_env *env)
1876 result = lu_context_refill(&env->le_ctx);
1877 if (result == 0 && env->le_ses != NULL)
1878 result = lu_context_refill(env->le_ses);
1881 EXPORT_SYMBOL(lu_env_refill);
1884 * Currently, this API will only be used by echo client.
1885 * Because echo client and normal lustre client will share
1886 * same cl_env cache. So echo client needs to refresh
1887 * the env context after it get one from the cache, especially
1888 * when normal client and echo client co-exist in the same client.
1890 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1895 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1896 env->le_ctx.lc_version = 0;
1897 env->le_ctx.lc_tags |= ctags;
1900 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1901 env->le_ses->lc_version = 0;
1902 env->le_ses->lc_tags |= stags;
1905 result = lu_env_refill(env);
1909 EXPORT_SYMBOL(lu_env_refill_by_tags);
1911 static struct shrinker *lu_site_shrinker;
1913 typedef struct lu_site_stats{
1914 unsigned lss_populated;
1915 unsigned lss_max_search;
1920 static void lu_site_stats_get(cfs_hash_t *hs,
1921 lu_site_stats_t *stats, int populated)
1926 cfs_hash_for_each_bucket(hs, &bd, i) {
1927 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1928 struct hlist_head *hhead;
1930 cfs_hash_bd_lock(hs, &bd, 1);
1931 stats->lss_busy += bkt->lsb_busy;
1932 stats->lss_total += cfs_hash_bd_count_get(&bd);
1933 stats->lss_max_search = max((int)stats->lss_max_search,
1934 cfs_hash_bd_depmax_get(&bd));
1936 cfs_hash_bd_unlock(hs, &bd, 1);
1940 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1941 if (!hlist_empty(hhead))
1942 stats->lss_populated++;
1944 cfs_hash_bd_unlock(hs, &bd, 1);
1949 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
1950 struct shrink_control *sc)
1952 lu_site_stats_t stats;
1954 struct lu_site *tmp;
1955 unsigned long cached = 0;
1957 if (!(sc->gfp_mask & __GFP_FS))
1960 mutex_lock(&lu_sites_guard);
1961 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1962 memset(&stats, 0, sizeof(stats));
1963 lu_site_stats_get(s->ls_obj_hash, &stats, 0);
1964 cached += stats.lss_total - stats.lss_busy;
1966 mutex_unlock(&lu_sites_guard);
1968 cached = (cached / 100) * sysctl_vfs_cache_pressure;
1969 CDEBUG(D_INODE, "%ld objects cached\n", cached);
1973 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
1974 struct shrink_control *sc)
1977 struct lu_site *tmp;
1978 unsigned long remain = sc->nr_to_scan;
1981 if (!(sc->gfp_mask & __GFP_FS))
1982 /* We must not take the lu_sites_guard lock when
1983 * __GFP_FS is *not* set because of the deadlock
1984 * possibility detailed above. Additionally,
1985 * since we cannot determine the number of
1986 * objects in the cache without taking this
1987 * lock, we're in a particularly tough spot. As
1988 * a result, we'll just lie and say our cache is
1989 * empty. This _should_ be ok, as we can't
1990 * reclaim objects when __GFP_FS is *not* set
1995 mutex_lock(&lu_sites_guard);
1996 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1997 remain = lu_site_purge(&lu_shrink_env, s, remain);
1999 * Move just shrunk site to the tail of site list to
2000 * assure shrinking fairness.
2002 list_move_tail(&s->ls_linkage, &splice);
2004 list_splice(&splice, lu_sites.prev);
2005 mutex_unlock(&lu_sites_guard);
2007 return sc->nr_to_scan - remain;
2010 #ifndef HAVE_SHRINKER_COUNT
2012 * There exists a potential lock inversion deadlock scenario when using
2013 * Lustre on top of ZFS. This occurs between one of ZFS's
2014 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
2015 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
2016 * while thread B will take the ht_lock and sleep on the lu_sites_guard
2017 * lock. Obviously neither thread will wake and drop their respective hold
2020 * To prevent this from happening we must ensure the lu_sites_guard lock is
2021 * not taken while down this code path. ZFS reliably does not set the
2022 * __GFP_FS bit in its code paths, so this can be used to determine if it
2023 * is safe to take the lu_sites_guard lock.
2025 * Ideally we should accurately return the remaining number of cached
2026 * objects without taking the lu_sites_guard lock, but this is not
2027 * possible in the current implementation.
2029 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
2032 struct shrink_control scv = {
2033 .nr_to_scan = shrink_param(sc, nr_to_scan),
2034 .gfp_mask = shrink_param(sc, gfp_mask)
2036 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
2037 struct shrinker* shrinker = NULL;
2041 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
2043 lu_cache_shrink_scan(shrinker, &scv);
2045 cached = lu_cache_shrink_count(shrinker, &scv);
2046 if (scv.nr_to_scan == 0)
2047 CDEBUG(D_INODE, "%d objects cached\n", cached);
2051 #endif /* HAVE_SHRINKER_COUNT */
2059 * Environment to be used in debugger, contains all tags.
2061 struct lu_env lu_debugging_env;
2064 * Debugging printer function using printk().
2066 int lu_printk_printer(const struct lu_env *env,
2067 void *unused, const char *format, ...)
2071 va_start(args, format);
2072 vprintk(format, args);
2077 int lu_debugging_setup(void)
2079 return lu_env_init(&lu_debugging_env, ~0);
2082 void lu_context_keys_dump(void)
2086 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2087 struct lu_context_key *key;
2091 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2092 i, key, key->lct_tags,
2093 key->lct_init, key->lct_fini, key->lct_exit,
2094 key->lct_index, atomic_read(&key->lct_used),
2095 key->lct_owner ? key->lct_owner->name : "",
2097 lu_ref_print(&key->lct_reference);
2101 EXPORT_SYMBOL(lu_context_keys_dump);
2104 * Initialization of global lu_* data.
2106 int lu_global_init(void)
2109 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2110 lu_cache_shrink_count, lu_cache_shrink_scan);
2112 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2114 INIT_LIST_HEAD(&lu_device_types);
2115 INIT_LIST_HEAD(&lu_context_remembered);
2116 INIT_LIST_HEAD(&lu_sites);
2118 result = lu_ref_global_init();
2122 LU_CONTEXT_KEY_INIT(&lu_global_key);
2123 result = lu_context_key_register(&lu_global_key);
2128 * At this level, we don't know what tags are needed, so allocate them
2129 * conservatively. This should not be too bad, because this
2130 * environment is global.
2132 mutex_lock(&lu_sites_guard);
2133 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2134 mutex_unlock(&lu_sites_guard);
2139 * seeks estimation: 3 seeks to read a record from oi, one to read
2140 * inode, one for ea. Unfortunately setting this high value results in
2141 * lu_object/inode cache consuming all the memory.
2143 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2144 if (lu_site_shrinker == NULL)
2151 * Dual to lu_global_init().
2153 void lu_global_fini(void)
2155 if (lu_site_shrinker != NULL) {
2156 remove_shrinker(lu_site_shrinker);
2157 lu_site_shrinker = NULL;
2160 lu_context_key_degister(&lu_global_key);
2163 * Tear shrinker environment down _after_ de-registering
2164 * lu_global_key, because the latter has a value in the former.
2166 mutex_lock(&lu_sites_guard);
2167 lu_env_fini(&lu_shrink_env);
2168 mutex_unlock(&lu_sites_guard);
2170 lu_ref_global_fini();
2173 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2176 struct lprocfs_counter ret;
2178 lprocfs_stats_collect(stats, idx, &ret);
2179 return (__u32)ret.lc_count;
2186 * Output site statistical counters into a buffer. Suitable for
2187 * lprocfs_rd_*()-style functions.
2189 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2191 lu_site_stats_t stats;
2193 memset(&stats, 0, sizeof(stats));
2194 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2196 return seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2199 stats.lss_populated,
2200 CFS_HASH_NHLIST(s->ls_obj_hash),
2201 stats.lss_max_search,
2202 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2203 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2204 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2205 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2206 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2207 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2209 EXPORT_SYMBOL(lu_site_stats_seq_print);
2211 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
2213 lu_site_stats_t stats;
2215 memset(&stats, 0, sizeof(stats));
2216 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2218 return snprintf(page, count, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2221 stats.lss_populated,
2222 CFS_HASH_NHLIST(s->ls_obj_hash),
2223 stats.lss_max_search,
2224 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2225 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2226 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2227 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2228 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2229 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2231 EXPORT_SYMBOL(lu_site_stats_print);
2234 * Helper function to initialize a number of kmem slab caches at once.
2236 int lu_kmem_init(struct lu_kmem_descr *caches)
2239 struct lu_kmem_descr *iter = caches;
2241 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2242 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2245 if (*iter->ckd_cache == NULL) {
2247 /* free all previously allocated caches */
2248 lu_kmem_fini(caches);
2254 EXPORT_SYMBOL(lu_kmem_init);
2257 * Helper function to finalize a number of kmem slab cached at once. Dual to
2260 void lu_kmem_fini(struct lu_kmem_descr *caches)
2262 for (; caches->ckd_cache != NULL; ++caches) {
2263 if (*caches->ckd_cache != NULL) {
2264 kmem_cache_destroy(*caches->ckd_cache);
2265 *caches->ckd_cache = NULL;
2269 EXPORT_SYMBOL(lu_kmem_fini);
2272 * Temporary solution to be able to assign fid in ->do_create()
2273 * till we have fully-functional OST fids
2275 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2276 const struct lu_fid *fid)
2278 struct lu_site *s = o->lo_dev->ld_site;
2279 struct lu_fid *old = &o->lo_header->loh_fid;
2280 struct lu_site_bkt_data *bkt;
2281 struct lu_object *shadow;
2282 wait_queue_t waiter;
2287 LASSERT(fid_is_zero(old));
2289 hs = s->ls_obj_hash;
2290 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2291 shadow = htable_lookup(s, &bd, fid, &waiter, &version);
2292 /* supposed to be unique */
2293 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2295 bkt = cfs_hash_bd_extra_get(hs, &bd);
2296 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2298 cfs_hash_bd_unlock(hs, &bd, 1);
2300 EXPORT_SYMBOL(lu_object_assign_fid);
2303 * allocates object with 0 (non-assiged) fid
2304 * XXX: temporary solution to be able to assign fid in ->do_create()
2305 * till we have fully-functional OST fids
2307 struct lu_object *lu_object_anon(const struct lu_env *env,
2308 struct lu_device *dev,
2309 const struct lu_object_conf *conf)
2312 struct lu_object *o;
2315 o = lu_object_alloc(env, dev, &fid, conf);
2319 EXPORT_SYMBOL(lu_object_anon);
2321 struct lu_buf LU_BUF_NULL = {
2325 EXPORT_SYMBOL(LU_BUF_NULL);
2327 void lu_buf_free(struct lu_buf *buf)
2331 LASSERT(buf->lb_len > 0);
2332 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2337 EXPORT_SYMBOL(lu_buf_free);
2339 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2342 LASSERT(buf->lb_buf == NULL);
2343 LASSERT(buf->lb_len == 0);
2344 OBD_ALLOC_LARGE(buf->lb_buf, size);
2345 if (likely(buf->lb_buf))
2348 EXPORT_SYMBOL(lu_buf_alloc);
2350 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2353 lu_buf_alloc(buf, size);
2355 EXPORT_SYMBOL(lu_buf_realloc);
2357 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2359 if (buf->lb_buf == NULL && buf->lb_len == 0)
2360 lu_buf_alloc(buf, len);
2362 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2363 lu_buf_realloc(buf, len);
2367 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2370 * Increase the size of the \a buf.
2371 * preserves old data in buffer
2372 * old buffer remains unchanged on error
2373 * \retval 0 or -ENOMEM
2375 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2379 if (len <= buf->lb_len)
2382 OBD_ALLOC_LARGE(ptr, len);
2386 /* Free the old buf */
2387 if (buf->lb_buf != NULL) {
2388 memcpy(ptr, buf->lb_buf, buf->lb_len);
2389 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2396 EXPORT_SYMBOL(lu_buf_check_and_grow);