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, 2014, 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 (lu_object_exists(orig) || lu_object_is_cl(orig))) {
156 LASSERT(list_empty(&top->loh_lru));
157 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
158 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
163 * If object is dying (will not be cached), removed it
164 * from hash table and LRU.
166 * This is done with hash table and LRU lists locked. As the only
167 * way to acquire first reference to previously unreferenced
168 * object is through hash-table lookup (lu_object_find()),
169 * or LRU scanning (lu_site_purge()), that are done under hash-table
170 * and LRU lock, no race with concurrent object lookup is possible
171 * and we can safely destroy object below.
173 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
174 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
175 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
177 * Object was already removed from hash and lru above, can
180 lu_object_free(env, orig);
182 EXPORT_SYMBOL(lu_object_put);
185 * Put object and don't keep in cache. This is temporary solution for
186 * multi-site objects when its layering is not constant.
188 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
190 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
191 return lu_object_put(env, o);
193 EXPORT_SYMBOL(lu_object_put_nocache);
196 * Kill the object and take it out of LRU cache.
197 * Currently used by client code for layout change.
199 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
201 struct lu_object_header *top;
204 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
205 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
206 cfs_hash_t *obj_hash = o->lo_dev->ld_site->ls_obj_hash;
209 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
210 list_del_init(&top->loh_lru);
211 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
212 cfs_hash_bd_unlock(obj_hash, &bd, 1);
215 EXPORT_SYMBOL(lu_object_unhash);
218 * Allocate new object.
220 * This follows object creation protocol, described in the comment within
221 * struct lu_device_operations definition.
223 static struct lu_object *lu_object_alloc(const struct lu_env *env,
224 struct lu_device *dev,
225 const struct lu_fid *f,
226 const struct lu_object_conf *conf)
228 struct lu_object *scan;
229 struct lu_object *top;
230 struct list_head *layers;
231 unsigned int init_mask = 0;
232 unsigned int init_flag;
238 * Create top-level object slice. This will also create
241 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
243 RETURN(ERR_PTR(-ENOMEM));
247 * This is the only place where object fid is assigned. It's constant
250 top->lo_header->loh_fid = *f;
251 layers = &top->lo_header->loh_layers;
255 * Call ->loo_object_init() repeatedly, until no more new
256 * object slices are created.
260 list_for_each_entry(scan, layers, lo_linkage) {
261 if (init_mask & init_flag)
264 scan->lo_header = top->lo_header;
265 result = scan->lo_ops->loo_object_init(env, scan, conf);
267 lu_object_free(env, top);
268 RETURN(ERR_PTR(result));
270 init_mask |= init_flag;
276 list_for_each_entry_reverse(scan, layers, lo_linkage) {
277 if (scan->lo_ops->loo_object_start != NULL) {
278 result = scan->lo_ops->loo_object_start(env, scan);
280 lu_object_free(env, top);
281 RETURN(ERR_PTR(result));
286 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
293 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
295 struct lu_site_bkt_data *bkt;
296 struct lu_site *site;
297 struct lu_object *scan;
298 struct list_head *layers;
299 struct list_head splice;
301 site = o->lo_dev->ld_site;
302 layers = &o->lo_header->loh_layers;
303 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
305 * First call ->loo_object_delete() method to release all resources.
307 list_for_each_entry_reverse(scan, layers, lo_linkage) {
308 if (scan->lo_ops->loo_object_delete != NULL)
309 scan->lo_ops->loo_object_delete(env, scan);
313 * Then, splice object layers into stand-alone list, and call
314 * ->loo_object_free() on all layers to free memory. Splice is
315 * necessary, because lu_object_header is freed together with the
318 INIT_LIST_HEAD(&splice);
319 list_splice_init(layers, &splice);
320 while (!list_empty(&splice)) {
322 * Free layers in bottom-to-top order, so that object header
323 * lives as long as possible and ->loo_object_free() methods
324 * can look at its contents.
326 o = container_of0(splice.prev, struct lu_object, lo_linkage);
327 list_del_init(&o->lo_linkage);
328 LASSERT(o->lo_ops->loo_object_free != NULL);
329 o->lo_ops->loo_object_free(env, o);
332 if (waitqueue_active(&bkt->lsb_marche_funebre))
333 wake_up_all(&bkt->lsb_marche_funebre);
337 * Free \a nr objects from the cold end of the site LRU list.
339 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
341 struct lu_object_header *h;
342 struct lu_object_header *temp;
343 struct lu_site_bkt_data *bkt;
346 struct list_head dispose;
353 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
356 INIT_LIST_HEAD(&dispose);
358 * Under LRU list lock, scan LRU list and move unreferenced objects to
359 * the dispose list, removing them from LRU and hash table.
361 start = s->ls_purge_start;
362 bnr = (nr == ~0) ? -1 : nr / (int)CFS_HASH_NBKT(s->ls_obj_hash) + 1;
365 * It doesn't make any sense to make purge threads parallel, that can
366 * only bring troubles to us. See LU-5331.
368 mutex_lock(&s->ls_purge_mutex);
370 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
374 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
375 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
377 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
378 LASSERT(atomic_read(&h->loh_ref) == 0);
380 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
381 LASSERT(bd.bd_bucket == bd2.bd_bucket);
383 cfs_hash_bd_del_locked(s->ls_obj_hash,
385 list_move(&h->loh_lru, &dispose);
389 if (nr != ~0 && --nr == 0)
392 if (count > 0 && --count == 0)
396 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
399 * Free everything on the dispose list. This is safe against
400 * races due to the reasons described in lu_object_put().
402 while (!list_empty(&dispose)) {
403 h = container_of0(dispose.next,
404 struct lu_object_header, loh_lru);
405 list_del_init(&h->loh_lru);
406 lu_object_free(env, lu_object_top(h));
407 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
413 mutex_unlock(&s->ls_purge_mutex);
415 if (nr != 0 && did_sth && start != 0) {
416 start = 0; /* restart from the first bucket */
419 /* race on s->ls_purge_start, but nobody cares */
420 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
424 EXPORT_SYMBOL(lu_site_purge);
429 * Code below has to jump through certain loops to output object description
430 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
431 * composes object description from strings that are parts of _lines_ of
432 * output (i.e., strings that are not terminated by newline). This doesn't fit
433 * very well into libcfs_debug_msg() interface that assumes that each message
434 * supplied to it is a self-contained output line.
436 * To work around this, strings are collected in a temporary buffer
437 * (implemented as a value of lu_cdebug_key key), until terminating newline
438 * character is detected.
446 * XXX overflow is not handled correctly.
451 struct lu_cdebug_data {
455 char lck_area[LU_CDEBUG_LINE];
458 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
459 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
462 * Key, holding temporary buffer. This key is registered very early by
465 static struct lu_context_key lu_global_key = {
466 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
467 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
468 .lct_init = lu_global_key_init,
469 .lct_fini = lu_global_key_fini
473 * Printer function emitting messages through libcfs_debug_msg().
475 int lu_cdebug_printer(const struct lu_env *env,
476 void *cookie, const char *format, ...)
478 struct libcfs_debug_msg_data *msgdata = cookie;
479 struct lu_cdebug_data *key;
484 va_start(args, format);
486 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
487 LASSERT(key != NULL);
489 used = strlen(key->lck_area);
490 complete = format[strlen(format) - 1] == '\n';
492 * Append new chunk to the buffer.
494 vsnprintf(key->lck_area + used,
495 ARRAY_SIZE(key->lck_area) - used, format, args);
497 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
498 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
499 key->lck_area[0] = 0;
504 EXPORT_SYMBOL(lu_cdebug_printer);
507 * Print object header.
509 void lu_object_header_print(const struct lu_env *env, void *cookie,
510 lu_printer_t printer,
511 const struct lu_object_header *hdr)
513 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
514 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
516 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
517 list_empty((struct list_head *)&hdr->loh_lru) ? \
519 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
521 EXPORT_SYMBOL(lu_object_header_print);
524 * Print human readable representation of the \a o to the \a printer.
526 void lu_object_print(const struct lu_env *env, void *cookie,
527 lu_printer_t printer, const struct lu_object *o)
529 static const char ruler[] = "........................................";
530 struct lu_object_header *top;
534 lu_object_header_print(env, cookie, printer, top);
535 (*printer)(env, cookie, "{\n");
537 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
539 * print `.' \a depth times followed by type name and address
541 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
542 o->lo_dev->ld_type->ldt_name, o);
544 if (o->lo_ops->loo_object_print != NULL)
545 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
547 (*printer)(env, cookie, "\n");
550 (*printer)(env, cookie, "} header@%p\n", top);
552 EXPORT_SYMBOL(lu_object_print);
555 * Check object consistency.
557 int lu_object_invariant(const struct lu_object *o)
559 struct lu_object_header *top;
562 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
563 if (o->lo_ops->loo_object_invariant != NULL &&
564 !o->lo_ops->loo_object_invariant(o))
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);
619 * Search cache for an object with the fid \a f. If such object is found,
620 * return it. Otherwise, create new object, insert it into cache and return
621 * it. In any case, additional reference is acquired on the returned object.
623 struct lu_object *lu_object_find(const struct lu_env *env,
624 struct lu_device *dev, const struct lu_fid *f,
625 const struct lu_object_conf *conf)
627 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
629 EXPORT_SYMBOL(lu_object_find);
632 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
633 * the calculation for the number of objects to reclaim is not covered by
634 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
635 * This ensures that many concurrent threads will not accidentally purge
638 static void lu_object_limit(const struct lu_env *env,
639 struct lu_device *dev)
643 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
646 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
647 nr = (__u64)lu_cache_nr;
649 lu_site_purge(env, dev->ld_site,
650 MIN(size - nr, LU_CACHE_NR_MAX_ADJUST));
655 static struct lu_object *lu_object_new(const struct lu_env *env,
656 struct lu_device *dev,
657 const struct lu_fid *f,
658 const struct lu_object_conf *conf)
663 struct lu_site_bkt_data *bkt;
665 o = lu_object_alloc(env, dev, f, conf);
666 if (unlikely(IS_ERR(o)))
669 hs = dev->ld_site->ls_obj_hash;
670 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
671 bkt = cfs_hash_bd_extra_get(hs, &bd);
672 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
674 cfs_hash_bd_unlock(hs, &bd, 1);
676 lu_object_limit(env, dev);
682 * Core logic of lu_object_find*() functions.
684 static struct lu_object *lu_object_find_try(const struct lu_env *env,
685 struct lu_device *dev,
686 const struct lu_fid *f,
687 const struct lu_object_conf *conf,
688 wait_queue_t *waiter)
691 struct lu_object *shadow;
698 * This uses standard index maintenance protocol:
700 * - search index under lock, and return object if found;
701 * - otherwise, unlock index, allocate new object;
702 * - lock index and search again;
703 * - if nothing is found (usual case), insert newly created
705 * - otherwise (race: other thread inserted object), free
706 * object just allocated.
710 * For "LOC_F_NEW" case, we are sure the object is new established.
711 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
712 * just alloc and insert directly.
714 * If dying object is found during index search, add @waiter to the
715 * site wait-queue and return ERR_PTR(-EAGAIN).
717 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
718 return lu_object_new(env, dev, f, conf);
722 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
723 o = htable_lookup(s, &bd, f, waiter, &version);
724 cfs_hash_bd_unlock(hs, &bd, 1);
725 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
729 * Allocate new object. This may result in rather complicated
730 * operations, including fld queries, inode loading, etc.
732 o = lu_object_alloc(env, dev, f, conf);
733 if (unlikely(IS_ERR(o)))
736 LASSERT(lu_fid_eq(lu_object_fid(o), f));
738 cfs_hash_bd_lock(hs, &bd, 1);
740 shadow = htable_lookup(s, &bd, f, waiter, &version);
741 if (likely(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT)) {
742 struct lu_site_bkt_data *bkt;
744 bkt = cfs_hash_bd_extra_get(hs, &bd);
745 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
747 cfs_hash_bd_unlock(hs, &bd, 1);
749 lu_object_limit(env, dev);
754 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
755 cfs_hash_bd_unlock(hs, &bd, 1);
756 lu_object_free(env, o);
761 * Much like lu_object_find(), but top level device of object is specifically
762 * \a dev rather than top level device of the site. This interface allows
763 * objects of different "stacking" to be created within the same site.
765 struct lu_object *lu_object_find_at(const struct lu_env *env,
766 struct lu_device *dev,
767 const struct lu_fid *f,
768 const struct lu_object_conf *conf)
770 struct lu_site_bkt_data *bkt;
771 struct lu_object *obj;
774 if (conf != NULL && conf->loc_flags & LOC_F_NOWAIT)
775 return lu_object_find_try(env, dev, f, conf, NULL);
778 obj = lu_object_find_try(env, dev, f, conf, &wait);
779 if (obj != ERR_PTR(-EAGAIN))
782 * lu_object_find_try() already added waiter into the
785 waitq_wait(&wait, TASK_UNINTERRUPTIBLE);
786 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
787 remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
790 EXPORT_SYMBOL(lu_object_find_at);
793 * Find object with given fid, and return its slice belonging to given device.
795 struct lu_object *lu_object_find_slice(const struct lu_env *env,
796 struct lu_device *dev,
797 const struct lu_fid *f,
798 const struct lu_object_conf *conf)
800 struct lu_object *top;
801 struct lu_object *obj;
803 top = lu_object_find(env, dev, f, conf);
807 obj = lu_object_locate(top->lo_header, dev->ld_type);
808 if (unlikely(obj == NULL)) {
809 lu_object_put(env, top);
810 obj = ERR_PTR(-ENOENT);
815 EXPORT_SYMBOL(lu_object_find_slice);
818 * Global list of all device types.
820 static struct list_head lu_device_types;
822 int lu_device_type_init(struct lu_device_type *ldt)
826 atomic_set(&ldt->ldt_device_nr, 0);
827 INIT_LIST_HEAD(&ldt->ldt_linkage);
828 if (ldt->ldt_ops->ldto_init)
829 result = ldt->ldt_ops->ldto_init(ldt);
832 spin_lock(&obd_types_lock);
833 list_add(&ldt->ldt_linkage, &lu_device_types);
834 spin_unlock(&obd_types_lock);
839 EXPORT_SYMBOL(lu_device_type_init);
841 void lu_device_type_fini(struct lu_device_type *ldt)
843 spin_lock(&obd_types_lock);
844 list_del_init(&ldt->ldt_linkage);
845 spin_unlock(&obd_types_lock);
846 if (ldt->ldt_ops->ldto_fini)
847 ldt->ldt_ops->ldto_fini(ldt);
849 EXPORT_SYMBOL(lu_device_type_fini);
852 * Global list of all sites on this node
854 static struct list_head lu_sites;
855 static DEFINE_MUTEX(lu_sites_guard);
858 * Global environment used by site shrinker.
860 static struct lu_env lu_shrink_env;
862 struct lu_site_print_arg {
863 struct lu_env *lsp_env;
865 lu_printer_t lsp_printer;
869 lu_site_obj_print(cfs_hash_t *hs, cfs_hash_bd_t *bd,
870 struct hlist_node *hnode, void *data)
872 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
873 struct lu_object_header *h;
875 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
876 if (!list_empty(&h->loh_layers)) {
877 const struct lu_object *o;
879 o = lu_object_top(h);
880 lu_object_print(arg->lsp_env, arg->lsp_cookie,
881 arg->lsp_printer, o);
883 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
884 arg->lsp_printer, h);
890 * Print all objects in \a s.
892 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
893 lu_printer_t printer)
895 struct lu_site_print_arg arg = {
896 .lsp_env = (struct lu_env *)env,
897 .lsp_cookie = cookie,
898 .lsp_printer = printer,
901 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
903 EXPORT_SYMBOL(lu_site_print);
906 * Return desired hash table order.
908 static unsigned long lu_htable_order(struct lu_device *top)
910 unsigned long cache_size;
914 * For ZFS based OSDs the cache should be disabled by default. This
915 * allows the ZFS ARC maximum flexibility in determining what buffers
916 * to cache. If Lustre has objects or buffer which it wants to ensure
917 * always stay cached it must maintain a hold on them.
919 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
920 lu_cache_percent = 1;
921 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
922 return LU_SITE_BITS_MIN;
926 * Calculate hash table size, assuming that we want reasonable
927 * performance when 20% of total memory is occupied by cache of
930 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
932 cache_size = totalram_pages;
934 #if BITS_PER_LONG == 32
935 /* limit hashtable size for lowmem systems to low RAM */
936 if (cache_size > 1 << (30 - PAGE_CACHE_SHIFT))
937 cache_size = 1 << (30 - PAGE_CACHE_SHIFT) * 3 / 4;
940 /* clear off unreasonable cache setting. */
941 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
942 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
943 " the range of (0, %u]. Will use default value: %u.\n",
944 lu_cache_percent, LU_CACHE_PERCENT_MAX,
945 LU_CACHE_PERCENT_DEFAULT);
947 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
949 cache_size = cache_size / 100 * lu_cache_percent *
950 (PAGE_CACHE_SIZE / 1024);
952 for (bits = 1; (1 << bits) < cache_size; ++bits) {
958 static unsigned lu_obj_hop_hash(cfs_hash_t *hs,
959 const void *key, unsigned mask)
961 struct lu_fid *fid = (struct lu_fid *)key;
964 hash = fid_flatten32(fid);
965 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
966 hash = hash_long(hash, hs->hs_bkt_bits);
968 /* give me another random factor */
969 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
971 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
972 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
977 static void *lu_obj_hop_object(struct hlist_node *hnode)
979 return hlist_entry(hnode, struct lu_object_header, loh_hash);
982 static void *lu_obj_hop_key(struct hlist_node *hnode)
984 struct lu_object_header *h;
986 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
990 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
992 struct lu_object_header *h;
994 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
995 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
998 static void lu_obj_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
1000 struct lu_object_header *h;
1002 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1003 if (atomic_add_return(1, &h->loh_ref) == 1) {
1004 struct lu_site_bkt_data *bkt;
1007 cfs_hash_bd_get(hs, &h->loh_fid, &bd);
1008 bkt = cfs_hash_bd_extra_get(hs, &bd);
1013 static void lu_obj_hop_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
1015 LBUG(); /* we should never called it */
1018 static cfs_hash_ops_t lu_site_hash_ops = {
1019 .hs_hash = lu_obj_hop_hash,
1020 .hs_key = lu_obj_hop_key,
1021 .hs_keycmp = lu_obj_hop_keycmp,
1022 .hs_object = lu_obj_hop_object,
1023 .hs_get = lu_obj_hop_get,
1024 .hs_put_locked = lu_obj_hop_put_locked,
1027 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1029 spin_lock(&s->ls_ld_lock);
1030 if (list_empty(&d->ld_linkage))
1031 list_add(&d->ld_linkage, &s->ls_ld_linkage);
1032 spin_unlock(&s->ls_ld_lock);
1034 EXPORT_SYMBOL(lu_dev_add_linkage);
1036 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1038 spin_lock(&s->ls_ld_lock);
1039 list_del_init(&d->ld_linkage);
1040 spin_unlock(&s->ls_ld_lock);
1042 EXPORT_SYMBOL(lu_dev_del_linkage);
1045 * Initialize site \a s, with \a d as the top level device.
1047 int lu_site_init(struct lu_site *s, struct lu_device *top)
1049 struct lu_site_bkt_data *bkt;
1056 memset(s, 0, sizeof *s);
1057 mutex_init(&s->ls_purge_mutex);
1058 bits = lu_htable_order(top);
1059 snprintf(name, sizeof(name), "lu_site_%s", top->ld_type->ldt_name);
1060 for (bits = clamp_t(typeof(bits), bits,
1061 LU_SITE_BITS_MIN, LU_SITE_BITS_MAX);
1062 bits >= LU_SITE_BITS_MIN; bits--) {
1063 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1064 bits - LU_SITE_BKT_BITS,
1067 CFS_HASH_SPIN_BKTLOCK |
1068 CFS_HASH_NO_ITEMREF |
1070 CFS_HASH_ASSERT_EMPTY |
1072 if (s->ls_obj_hash != NULL)
1076 if (s->ls_obj_hash == NULL) {
1077 CERROR("failed to create lu_site hash with bits: %lu\n", bits);
1081 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1082 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1083 INIT_LIST_HEAD(&bkt->lsb_lru);
1084 init_waitqueue_head(&bkt->lsb_marche_funebre);
1087 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1088 if (s->ls_stats == NULL) {
1089 cfs_hash_putref(s->ls_obj_hash);
1090 s->ls_obj_hash = NULL;
1094 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1095 0, "created", "created");
1096 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1097 0, "cache_hit", "cache_hit");
1098 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1099 0, "cache_miss", "cache_miss");
1100 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1101 0, "cache_race", "cache_race");
1102 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1103 0, "cache_death_race", "cache_death_race");
1104 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1105 0, "lru_purged", "lru_purged");
1107 INIT_LIST_HEAD(&s->ls_linkage);
1108 s->ls_top_dev = top;
1111 lu_ref_add(&top->ld_reference, "site-top", s);
1113 INIT_LIST_HEAD(&s->ls_ld_linkage);
1114 spin_lock_init(&s->ls_ld_lock);
1116 lu_dev_add_linkage(s, top);
1120 EXPORT_SYMBOL(lu_site_init);
1123 * Finalize \a s and release its resources.
1125 void lu_site_fini(struct lu_site *s)
1127 mutex_lock(&lu_sites_guard);
1128 list_del_init(&s->ls_linkage);
1129 mutex_unlock(&lu_sites_guard);
1131 if (s->ls_obj_hash != NULL) {
1132 cfs_hash_putref(s->ls_obj_hash);
1133 s->ls_obj_hash = NULL;
1136 if (s->ls_top_dev != NULL) {
1137 s->ls_top_dev->ld_site = NULL;
1138 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1139 lu_device_put(s->ls_top_dev);
1140 s->ls_top_dev = NULL;
1143 if (s->ls_stats != NULL)
1144 lprocfs_free_stats(&s->ls_stats);
1146 EXPORT_SYMBOL(lu_site_fini);
1149 * Called when initialization of stack for this site is completed.
1151 int lu_site_init_finish(struct lu_site *s)
1154 mutex_lock(&lu_sites_guard);
1155 result = lu_context_refill(&lu_shrink_env.le_ctx);
1157 list_add(&s->ls_linkage, &lu_sites);
1158 mutex_unlock(&lu_sites_guard);
1161 EXPORT_SYMBOL(lu_site_init_finish);
1164 * Acquire additional reference on device \a d
1166 void lu_device_get(struct lu_device *d)
1168 atomic_inc(&d->ld_ref);
1170 EXPORT_SYMBOL(lu_device_get);
1173 * Release reference on device \a d.
1175 void lu_device_put(struct lu_device *d)
1177 LASSERT(atomic_read(&d->ld_ref) > 0);
1178 atomic_dec(&d->ld_ref);
1180 EXPORT_SYMBOL(lu_device_put);
1183 * Initialize device \a d of type \a t.
1185 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1187 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1188 t->ldt_ops->ldto_start != NULL)
1189 t->ldt_ops->ldto_start(t);
1191 memset(d, 0, sizeof *d);
1193 lu_ref_init(&d->ld_reference);
1194 INIT_LIST_HEAD(&d->ld_linkage);
1198 EXPORT_SYMBOL(lu_device_init);
1201 * Finalize device \a d.
1203 void lu_device_fini(struct lu_device *d)
1205 struct lu_device_type *t = d->ld_type;
1207 if (d->ld_obd != NULL) {
1208 d->ld_obd->obd_lu_dev = NULL;
1212 lu_ref_fini(&d->ld_reference);
1213 LASSERTF(atomic_read(&d->ld_ref) == 0,
1214 "Refcount is %u\n", atomic_read(&d->ld_ref));
1215 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1217 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1218 t->ldt_ops->ldto_stop != NULL)
1219 t->ldt_ops->ldto_stop(t);
1221 EXPORT_SYMBOL(lu_device_fini);
1224 * Initialize object \a o that is part of compound object \a h and was created
1227 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1228 struct lu_device *d)
1230 memset(o, 0, sizeof(*o));
1234 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1235 INIT_LIST_HEAD(&o->lo_linkage);
1239 EXPORT_SYMBOL(lu_object_init);
1242 * Finalize object and release its resources.
1244 void lu_object_fini(struct lu_object *o)
1246 struct lu_device *dev = o->lo_dev;
1248 LASSERT(list_empty(&o->lo_linkage));
1251 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1257 EXPORT_SYMBOL(lu_object_fini);
1260 * Add object \a o as first layer of compound object \a h
1262 * This is typically called by the ->ldo_object_alloc() method of top-level
1265 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1267 list_move(&o->lo_linkage, &h->loh_layers);
1269 EXPORT_SYMBOL(lu_object_add_top);
1272 * Add object \a o as a layer of compound object, going after \a before.
1274 * This is typically called by the ->ldo_object_alloc() method of \a
1277 void lu_object_add(struct lu_object *before, struct lu_object *o)
1279 list_move(&o->lo_linkage, &before->lo_linkage);
1281 EXPORT_SYMBOL(lu_object_add);
1284 * Initialize compound object.
1286 int lu_object_header_init(struct lu_object_header *h)
1288 memset(h, 0, sizeof *h);
1289 atomic_set(&h->loh_ref, 1);
1290 INIT_HLIST_NODE(&h->loh_hash);
1291 INIT_LIST_HEAD(&h->loh_lru);
1292 INIT_LIST_HEAD(&h->loh_layers);
1293 lu_ref_init(&h->loh_reference);
1296 EXPORT_SYMBOL(lu_object_header_init);
1299 * Finalize compound object.
1301 void lu_object_header_fini(struct lu_object_header *h)
1303 LASSERT(list_empty(&h->loh_layers));
1304 LASSERT(list_empty(&h->loh_lru));
1305 LASSERT(hlist_unhashed(&h->loh_hash));
1306 lu_ref_fini(&h->loh_reference);
1308 EXPORT_SYMBOL(lu_object_header_fini);
1311 * Given a compound object, find its slice, corresponding to the device type
1314 struct lu_object *lu_object_locate(struct lu_object_header *h,
1315 const struct lu_device_type *dtype)
1317 struct lu_object *o;
1319 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1320 if (o->lo_dev->ld_type == dtype)
1325 EXPORT_SYMBOL(lu_object_locate);
1328 * Finalize and free devices in the device stack.
1330 * Finalize device stack by purging object cache, and calling
1331 * lu_device_type_operations::ldto_device_fini() and
1332 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1334 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1336 struct lu_site *site = top->ld_site;
1337 struct lu_device *scan;
1338 struct lu_device *next;
1340 lu_site_purge(env, site, ~0);
1341 for (scan = top; scan != NULL; scan = next) {
1342 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1343 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1344 lu_device_put(scan);
1348 lu_site_purge(env, site, ~0);
1350 for (scan = top; scan != NULL; scan = next) {
1351 const struct lu_device_type *ldt = scan->ld_type;
1352 struct obd_type *type;
1354 next = ldt->ldt_ops->ldto_device_free(env, scan);
1355 type = ldt->ldt_obd_type;
1358 class_put_type(type);
1365 * Maximal number of tld slots.
1367 LU_CONTEXT_KEY_NR = 40
1370 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1372 static DEFINE_SPINLOCK(lu_keys_guard);
1375 * Global counter incremented whenever key is registered, unregistered,
1376 * revived or quiesced. This is used to void unnecessary calls to
1377 * lu_context_refill(). No locking is provided, as initialization and shutdown
1378 * are supposed to be externally serialized.
1380 static unsigned key_set_version = 0;
1385 int lu_context_key_register(struct lu_context_key *key)
1390 LASSERT(key->lct_init != NULL);
1391 LASSERT(key->lct_fini != NULL);
1392 LASSERT(key->lct_tags != 0);
1393 LASSERT(key->lct_owner != NULL);
1396 spin_lock(&lu_keys_guard);
1397 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1398 if (lu_keys[i] == NULL) {
1400 atomic_set(&key->lct_used, 1);
1402 lu_ref_init(&key->lct_reference);
1408 spin_unlock(&lu_keys_guard);
1411 EXPORT_SYMBOL(lu_context_key_register);
1413 static void key_fini(struct lu_context *ctx, int index)
1415 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1416 struct lu_context_key *key;
1418 key = lu_keys[index];
1419 LASSERT(key != NULL);
1420 LASSERT(key->lct_fini != NULL);
1421 LASSERT(atomic_read(&key->lct_used) > 1);
1423 key->lct_fini(ctx, key, ctx->lc_value[index]);
1424 lu_ref_del(&key->lct_reference, "ctx", ctx);
1425 atomic_dec(&key->lct_used);
1427 LASSERT(key->lct_owner != NULL);
1428 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1429 LINVRNT(module_refcount(key->lct_owner) > 0);
1430 module_put(key->lct_owner);
1432 ctx->lc_value[index] = NULL;
1439 void lu_context_key_degister(struct lu_context_key *key)
1441 LASSERT(atomic_read(&key->lct_used) >= 1);
1442 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1444 lu_context_key_quiesce(key);
1447 spin_lock(&lu_keys_guard);
1448 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1449 if (lu_keys[key->lct_index]) {
1450 lu_keys[key->lct_index] = NULL;
1451 lu_ref_fini(&key->lct_reference);
1453 spin_unlock(&lu_keys_guard);
1455 LASSERTF(atomic_read(&key->lct_used) == 1,
1456 "key has instances: %d\n",
1457 atomic_read(&key->lct_used));
1459 EXPORT_SYMBOL(lu_context_key_degister);
1462 * Register a number of keys. This has to be called after all keys have been
1463 * initialized by a call to LU_CONTEXT_KEY_INIT().
1465 int lu_context_key_register_many(struct lu_context_key *k, ...)
1467 struct lu_context_key *key = k;
1473 result = lu_context_key_register(key);
1476 key = va_arg(args, struct lu_context_key *);
1477 } while (key != NULL);
1483 lu_context_key_degister(k);
1484 k = va_arg(args, struct lu_context_key *);
1491 EXPORT_SYMBOL(lu_context_key_register_many);
1494 * De-register a number of keys. This is a dual to
1495 * lu_context_key_register_many().
1497 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1503 lu_context_key_degister(k);
1504 k = va_arg(args, struct lu_context_key*);
1505 } while (k != NULL);
1508 EXPORT_SYMBOL(lu_context_key_degister_many);
1511 * Revive a number of keys.
1513 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1519 lu_context_key_revive(k);
1520 k = va_arg(args, struct lu_context_key*);
1521 } while (k != NULL);
1524 EXPORT_SYMBOL(lu_context_key_revive_many);
1527 * Quiescent a number of keys.
1529 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1535 lu_context_key_quiesce(k);
1536 k = va_arg(args, struct lu_context_key*);
1537 } while (k != NULL);
1540 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1543 * Return value associated with key \a key in context \a ctx.
1545 void *lu_context_key_get(const struct lu_context *ctx,
1546 const struct lu_context_key *key)
1548 LINVRNT(ctx->lc_state == LCS_ENTERED);
1549 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1550 LASSERT(lu_keys[key->lct_index] == key);
1551 return ctx->lc_value[key->lct_index];
1553 EXPORT_SYMBOL(lu_context_key_get);
1556 * List of remembered contexts. XXX document me.
1558 static struct list_head lu_context_remembered;
1561 * Destroy \a key in all remembered contexts. This is used to destroy key
1562 * values in "shared" contexts (like service threads), when a module owning
1563 * the key is about to be unloaded.
1565 void lu_context_key_quiesce(struct lu_context_key *key)
1567 struct lu_context *ctx;
1568 extern unsigned cl_env_cache_purge(unsigned nr);
1570 if (!(key->lct_tags & LCT_QUIESCENT)) {
1572 * XXX layering violation.
1574 cl_env_cache_purge(~0);
1575 key->lct_tags |= LCT_QUIESCENT;
1577 * XXX memory barrier has to go here.
1579 spin_lock(&lu_keys_guard);
1580 list_for_each_entry(ctx, &lu_context_remembered,
1582 key_fini(ctx, key->lct_index);
1583 spin_unlock(&lu_keys_guard);
1588 void lu_context_key_revive(struct lu_context_key *key)
1590 key->lct_tags &= ~LCT_QUIESCENT;
1594 static void keys_fini(struct lu_context *ctx)
1598 if (ctx->lc_value == NULL)
1601 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1604 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1605 ctx->lc_value = NULL;
1608 static int keys_fill(struct lu_context *ctx)
1612 LINVRNT(ctx->lc_value != NULL);
1613 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1614 struct lu_context_key *key;
1617 if (ctx->lc_value[i] == NULL && key != NULL &&
1618 (key->lct_tags & ctx->lc_tags) &&
1620 * Don't create values for a LCT_QUIESCENT key, as this
1621 * will pin module owning a key.
1623 !(key->lct_tags & LCT_QUIESCENT)) {
1626 LINVRNT(key->lct_init != NULL);
1627 LINVRNT(key->lct_index == i);
1629 value = key->lct_init(ctx, key);
1630 if (unlikely(IS_ERR(value)))
1631 return PTR_ERR(value);
1633 LASSERT(key->lct_owner != NULL);
1634 if (!(ctx->lc_tags & LCT_NOREF))
1635 try_module_get(key->lct_owner);
1636 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1637 atomic_inc(&key->lct_used);
1639 * This is the only place in the code, where an
1640 * element of ctx->lc_value[] array is set to non-NULL
1643 ctx->lc_value[i] = value;
1644 if (key->lct_exit != NULL)
1645 ctx->lc_tags |= LCT_HAS_EXIT;
1647 ctx->lc_version = key_set_version;
1652 static int keys_init(struct lu_context *ctx)
1654 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1655 if (likely(ctx->lc_value != NULL))
1656 return keys_fill(ctx);
1662 * Initialize context data-structure. Create values for all keys.
1664 int lu_context_init(struct lu_context *ctx, __u32 tags)
1668 memset(ctx, 0, sizeof *ctx);
1669 ctx->lc_state = LCS_INITIALIZED;
1670 ctx->lc_tags = tags;
1671 if (tags & LCT_REMEMBER) {
1672 spin_lock(&lu_keys_guard);
1673 list_add(&ctx->lc_remember, &lu_context_remembered);
1674 spin_unlock(&lu_keys_guard);
1676 INIT_LIST_HEAD(&ctx->lc_remember);
1679 rc = keys_init(ctx);
1681 lu_context_fini(ctx);
1685 EXPORT_SYMBOL(lu_context_init);
1688 * Finalize context data-structure. Destroy key values.
1690 void lu_context_fini(struct lu_context *ctx)
1692 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1693 ctx->lc_state = LCS_FINALIZED;
1695 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1696 LASSERT(list_empty(&ctx->lc_remember));
1699 } else { /* could race with key degister */
1700 spin_lock(&lu_keys_guard);
1702 list_del_init(&ctx->lc_remember);
1703 spin_unlock(&lu_keys_guard);
1706 EXPORT_SYMBOL(lu_context_fini);
1709 * Called before entering context.
1711 void lu_context_enter(struct lu_context *ctx)
1713 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1714 ctx->lc_state = LCS_ENTERED;
1716 EXPORT_SYMBOL(lu_context_enter);
1719 * Called after exiting from \a ctx
1721 void lu_context_exit(struct lu_context *ctx)
1725 LINVRNT(ctx->lc_state == LCS_ENTERED);
1726 ctx->lc_state = LCS_LEFT;
1727 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1728 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1729 if (ctx->lc_value[i] != NULL) {
1730 struct lu_context_key *key;
1733 LASSERT(key != NULL);
1734 if (key->lct_exit != NULL)
1736 key, ctx->lc_value[i]);
1741 EXPORT_SYMBOL(lu_context_exit);
1744 * Allocate for context all missing keys that were registered after context
1745 * creation. key_set_version is only changed in rare cases when modules
1746 * are loaded and removed.
1748 int lu_context_refill(struct lu_context *ctx)
1750 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1754 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1755 * obd being added. Currently, this is only used on client side, specifically
1756 * for echo device client, for other stack (like ptlrpc threads), context are
1757 * predefined when the lu_device type are registered, during the module probe
1760 __u32 lu_context_tags_default = 0;
1761 __u32 lu_session_tags_default = 0;
1763 void lu_context_tags_update(__u32 tags)
1765 spin_lock(&lu_keys_guard);
1766 lu_context_tags_default |= tags;
1768 spin_unlock(&lu_keys_guard);
1770 EXPORT_SYMBOL(lu_context_tags_update);
1772 void lu_context_tags_clear(__u32 tags)
1774 spin_lock(&lu_keys_guard);
1775 lu_context_tags_default &= ~tags;
1777 spin_unlock(&lu_keys_guard);
1779 EXPORT_SYMBOL(lu_context_tags_clear);
1781 void lu_session_tags_update(__u32 tags)
1783 spin_lock(&lu_keys_guard);
1784 lu_session_tags_default |= tags;
1786 spin_unlock(&lu_keys_guard);
1788 EXPORT_SYMBOL(lu_session_tags_update);
1790 void lu_session_tags_clear(__u32 tags)
1792 spin_lock(&lu_keys_guard);
1793 lu_session_tags_default &= ~tags;
1795 spin_unlock(&lu_keys_guard);
1797 EXPORT_SYMBOL(lu_session_tags_clear);
1799 int lu_env_init(struct lu_env *env, __u32 tags)
1804 result = lu_context_init(&env->le_ctx, tags);
1805 if (likely(result == 0))
1806 lu_context_enter(&env->le_ctx);
1809 EXPORT_SYMBOL(lu_env_init);
1811 void lu_env_fini(struct lu_env *env)
1813 lu_context_exit(&env->le_ctx);
1814 lu_context_fini(&env->le_ctx);
1817 EXPORT_SYMBOL(lu_env_fini);
1819 int lu_env_refill(struct lu_env *env)
1823 result = lu_context_refill(&env->le_ctx);
1824 if (result == 0 && env->le_ses != NULL)
1825 result = lu_context_refill(env->le_ses);
1828 EXPORT_SYMBOL(lu_env_refill);
1831 * Currently, this API will only be used by echo client.
1832 * Because echo client and normal lustre client will share
1833 * same cl_env cache. So echo client needs to refresh
1834 * the env context after it get one from the cache, especially
1835 * when normal client and echo client co-exist in the same client.
1837 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1842 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1843 env->le_ctx.lc_version = 0;
1844 env->le_ctx.lc_tags |= ctags;
1847 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1848 env->le_ses->lc_version = 0;
1849 env->le_ses->lc_tags |= stags;
1852 result = lu_env_refill(env);
1856 EXPORT_SYMBOL(lu_env_refill_by_tags);
1858 static struct shrinker *lu_site_shrinker;
1860 typedef struct lu_site_stats{
1861 unsigned lss_populated;
1862 unsigned lss_max_search;
1867 static void lu_site_stats_get(cfs_hash_t *hs,
1868 lu_site_stats_t *stats, int populated)
1873 cfs_hash_for_each_bucket(hs, &bd, i) {
1874 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1875 struct hlist_head *hhead;
1877 cfs_hash_bd_lock(hs, &bd, 1);
1878 stats->lss_busy += bkt->lsb_busy;
1879 stats->lss_total += cfs_hash_bd_count_get(&bd);
1880 stats->lss_max_search = max((int)stats->lss_max_search,
1881 cfs_hash_bd_depmax_get(&bd));
1883 cfs_hash_bd_unlock(hs, &bd, 1);
1887 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1888 if (!hlist_empty(hhead))
1889 stats->lss_populated++;
1891 cfs_hash_bd_unlock(hs, &bd, 1);
1896 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
1897 struct shrink_control *sc)
1899 lu_site_stats_t stats;
1901 struct lu_site *tmp;
1902 unsigned long cached = 0;
1904 if (!(sc->gfp_mask & __GFP_FS))
1907 mutex_lock(&lu_sites_guard);
1908 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1909 memset(&stats, 0, sizeof(stats));
1910 lu_site_stats_get(s->ls_obj_hash, &stats, 0);
1911 cached += stats.lss_total - stats.lss_busy;
1913 mutex_unlock(&lu_sites_guard);
1915 cached = (cached / 100) * sysctl_vfs_cache_pressure;
1916 CDEBUG(D_INODE, "%ld objects cached\n", cached);
1920 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
1921 struct shrink_control *sc)
1924 struct lu_site *tmp;
1925 unsigned long remain = sc->nr_to_scan;
1928 if (!(sc->gfp_mask & __GFP_FS))
1929 /* We must not take the lu_sites_guard lock when
1930 * __GFP_FS is *not* set because of the deadlock
1931 * possibility detailed above. Additionally,
1932 * since we cannot determine the number of
1933 * objects in the cache without taking this
1934 * lock, we're in a particularly tough spot. As
1935 * a result, we'll just lie and say our cache is
1936 * empty. This _should_ be ok, as we can't
1937 * reclaim objects when __GFP_FS is *not* set
1942 mutex_lock(&lu_sites_guard);
1943 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1944 remain = lu_site_purge(&lu_shrink_env, s, remain);
1946 * Move just shrunk site to the tail of site list to
1947 * assure shrinking fairness.
1949 list_move_tail(&s->ls_linkage, &splice);
1951 list_splice(&splice, lu_sites.prev);
1952 mutex_unlock(&lu_sites_guard);
1954 return sc->nr_to_scan - remain;
1957 #ifndef HAVE_SHRINKER_COUNT
1959 * There exists a potential lock inversion deadlock scenario when using
1960 * Lustre on top of ZFS. This occurs between one of ZFS's
1961 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
1962 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
1963 * while thread B will take the ht_lock and sleep on the lu_sites_guard
1964 * lock. Obviously neither thread will wake and drop their respective hold
1967 * To prevent this from happening we must ensure the lu_sites_guard lock is
1968 * not taken while down this code path. ZFS reliably does not set the
1969 * __GFP_FS bit in its code paths, so this can be used to determine if it
1970 * is safe to take the lu_sites_guard lock.
1972 * Ideally we should accurately return the remaining number of cached
1973 * objects without taking the lu_sites_guard lock, but this is not
1974 * possible in the current implementation.
1976 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
1979 struct shrink_control scv = {
1980 .nr_to_scan = shrink_param(sc, nr_to_scan),
1981 .gfp_mask = shrink_param(sc, gfp_mask)
1983 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
1984 struct shrinker* shrinker = NULL;
1988 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
1990 lu_cache_shrink_scan(shrinker, &scv);
1992 cached = lu_cache_shrink_count(shrinker, &scv);
1993 if (scv.nr_to_scan == 0)
1994 CDEBUG(D_INODE, "%d objects cached\n", cached);
1998 #endif /* HAVE_SHRINKER_COUNT */
2006 * Environment to be used in debugger, contains all tags.
2008 static struct lu_env lu_debugging_env;
2011 * Debugging printer function using printk().
2013 int lu_printk_printer(const struct lu_env *env,
2014 void *unused, const char *format, ...)
2018 va_start(args, format);
2019 vprintk(format, args);
2024 int lu_debugging_setup(void)
2026 return lu_env_init(&lu_debugging_env, ~0);
2029 void lu_context_keys_dump(void)
2033 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2034 struct lu_context_key *key;
2038 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2039 i, key, key->lct_tags,
2040 key->lct_init, key->lct_fini, key->lct_exit,
2041 key->lct_index, atomic_read(&key->lct_used),
2042 key->lct_owner ? key->lct_owner->name : "",
2044 lu_ref_print(&key->lct_reference);
2050 * Initialization of global lu_* data.
2052 int lu_global_init(void)
2055 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2056 lu_cache_shrink_count, lu_cache_shrink_scan);
2058 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2060 INIT_LIST_HEAD(&lu_device_types);
2061 INIT_LIST_HEAD(&lu_context_remembered);
2062 INIT_LIST_HEAD(&lu_sites);
2064 result = lu_ref_global_init();
2068 LU_CONTEXT_KEY_INIT(&lu_global_key);
2069 result = lu_context_key_register(&lu_global_key);
2074 * At this level, we don't know what tags are needed, so allocate them
2075 * conservatively. This should not be too bad, because this
2076 * environment is global.
2078 mutex_lock(&lu_sites_guard);
2079 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2080 mutex_unlock(&lu_sites_guard);
2085 * seeks estimation: 3 seeks to read a record from oi, one to read
2086 * inode, one for ea. Unfortunately setting this high value results in
2087 * lu_object/inode cache consuming all the memory.
2089 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2090 if (lu_site_shrinker == NULL)
2097 * Dual to lu_global_init().
2099 void lu_global_fini(void)
2101 if (lu_site_shrinker != NULL) {
2102 remove_shrinker(lu_site_shrinker);
2103 lu_site_shrinker = NULL;
2106 lu_context_key_degister(&lu_global_key);
2109 * Tear shrinker environment down _after_ de-registering
2110 * lu_global_key, because the latter has a value in the former.
2112 mutex_lock(&lu_sites_guard);
2113 lu_env_fini(&lu_shrink_env);
2114 mutex_unlock(&lu_sites_guard);
2116 lu_ref_global_fini();
2119 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2121 #ifdef CONFIG_PROC_FS
2122 struct lprocfs_counter ret;
2124 lprocfs_stats_collect(stats, idx, &ret);
2125 return (__u32)ret.lc_count;
2132 * Output site statistical counters into a buffer. Suitable for
2133 * lprocfs_rd_*()-style functions.
2135 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2137 lu_site_stats_t stats;
2139 memset(&stats, 0, sizeof(stats));
2140 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2142 return seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2145 stats.lss_populated,
2146 CFS_HASH_NHLIST(s->ls_obj_hash),
2147 stats.lss_max_search,
2148 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2149 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2150 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2151 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2152 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2153 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2155 EXPORT_SYMBOL(lu_site_stats_seq_print);
2157 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
2159 lu_site_stats_t stats;
2161 memset(&stats, 0, sizeof(stats));
2162 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2164 return snprintf(page, count, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2167 stats.lss_populated,
2168 CFS_HASH_NHLIST(s->ls_obj_hash),
2169 stats.lss_max_search,
2170 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2171 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2172 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2173 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2174 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2175 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2179 * Helper function to initialize a number of kmem slab caches at once.
2181 int lu_kmem_init(struct lu_kmem_descr *caches)
2184 struct lu_kmem_descr *iter = caches;
2186 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2187 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2190 if (*iter->ckd_cache == NULL) {
2192 /* free all previously allocated caches */
2193 lu_kmem_fini(caches);
2199 EXPORT_SYMBOL(lu_kmem_init);
2202 * Helper function to finalize a number of kmem slab cached at once. Dual to
2205 void lu_kmem_fini(struct lu_kmem_descr *caches)
2207 for (; caches->ckd_cache != NULL; ++caches) {
2208 if (*caches->ckd_cache != NULL) {
2209 kmem_cache_destroy(*caches->ckd_cache);
2210 *caches->ckd_cache = NULL;
2214 EXPORT_SYMBOL(lu_kmem_fini);
2217 * Temporary solution to be able to assign fid in ->do_create()
2218 * till we have fully-functional OST fids
2220 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2221 const struct lu_fid *fid)
2223 struct lu_site *s = o->lo_dev->ld_site;
2224 struct lu_fid *old = &o->lo_header->loh_fid;
2225 struct lu_site_bkt_data *bkt;
2226 struct lu_object *shadow;
2227 wait_queue_t waiter;
2232 LASSERT(fid_is_zero(old));
2234 hs = s->ls_obj_hash;
2235 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2236 shadow = htable_lookup(s, &bd, fid, &waiter, &version);
2237 /* supposed to be unique */
2238 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2240 bkt = cfs_hash_bd_extra_get(hs, &bd);
2241 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2243 cfs_hash_bd_unlock(hs, &bd, 1);
2245 EXPORT_SYMBOL(lu_object_assign_fid);
2248 * allocates object with 0 (non-assiged) fid
2249 * XXX: temporary solution to be able to assign fid in ->do_create()
2250 * till we have fully-functional OST fids
2252 struct lu_object *lu_object_anon(const struct lu_env *env,
2253 struct lu_device *dev,
2254 const struct lu_object_conf *conf)
2257 struct lu_object *o;
2260 o = lu_object_alloc(env, dev, &fid, conf);
2264 EXPORT_SYMBOL(lu_object_anon);
2266 struct lu_buf LU_BUF_NULL = {
2270 EXPORT_SYMBOL(LU_BUF_NULL);
2272 void lu_buf_free(struct lu_buf *buf)
2276 LASSERT(buf->lb_len > 0);
2277 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2282 EXPORT_SYMBOL(lu_buf_free);
2284 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2287 LASSERT(buf->lb_buf == NULL);
2288 LASSERT(buf->lb_len == 0);
2289 OBD_ALLOC_LARGE(buf->lb_buf, size);
2290 if (likely(buf->lb_buf))
2293 EXPORT_SYMBOL(lu_buf_alloc);
2295 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2298 lu_buf_alloc(buf, size);
2300 EXPORT_SYMBOL(lu_buf_realloc);
2302 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2304 if (buf->lb_buf == NULL && buf->lb_len == 0)
2305 lu_buf_alloc(buf, len);
2307 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2308 lu_buf_realloc(buf, len);
2312 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2315 * Increase the size of the \a buf.
2316 * preserves old data in buffer
2317 * old buffer remains unchanged on error
2318 * \retval 0 or -ENOMEM
2320 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2324 if (len <= buf->lb_len)
2327 OBD_ALLOC_LARGE(ptr, len);
2331 /* Free the old buf */
2332 if (buf->lb_buf != NULL) {
2333 memcpy(ptr, buf->lb_buf, buf->lb_len);
2334 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);