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, 2015, 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
72 #define LU_SITE_BITS_MAX_CL 19
74 * total 256 buckets, we don't want too many buckets because:
75 * - consume too much memory
76 * - avoid unbalanced LRU list
78 #define LU_SITE_BKT_BITS 8
81 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
82 module_param(lu_cache_percent, int, 0644);
83 MODULE_PARM_DESC(lu_cache_percent, "Percentage of memory to be used as lu_object cache");
85 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
86 module_param(lu_cache_nr, long, 0644);
87 MODULE_PARM_DESC(lu_cache_nr, "Maximum number of objects in lu_object cache");
89 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
90 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx);
93 * Decrease reference counter on object. If last reference is freed, return
94 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
95 * case, free object immediately.
97 void lu_object_put(const struct lu_env *env, struct lu_object *o)
99 struct lu_site_bkt_data *bkt;
100 struct lu_object_header *top;
101 struct lu_site *site;
102 struct lu_object *orig;
103 struct cfs_hash_bd bd;
104 const struct lu_fid *fid;
107 site = o->lo_dev->ld_site;
111 * till we have full fids-on-OST implemented anonymous objects
112 * are possible in OSP. such an object isn't listed in the site
113 * so we should not remove it from the site.
115 fid = lu_object_fid(o);
116 if (fid_is_zero(fid)) {
117 LASSERT(top->loh_hash.next == NULL
118 && top->loh_hash.pprev == NULL);
119 LASSERT(list_empty(&top->loh_lru));
120 if (!atomic_dec_and_test(&top->loh_ref))
122 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
123 if (o->lo_ops->loo_object_release != NULL)
124 o->lo_ops->loo_object_release(env, o);
126 lu_object_free(env, orig);
130 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
131 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
133 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
134 if (lu_object_is_dying(top)) {
137 * somebody may be waiting for this, currently only
138 * used for cl_object, see cl_object_put_last().
140 wake_up_all(&bkt->lsb_marche_funebre);
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);
159 percpu_counter_inc(&site->ls_lru_len_counter);
160 CDEBUG(D_INODE, "Add %p to site lru. hash: %p, bkt: %p, "
162 o, site->ls_obj_hash, bkt, bkt->lsb_lru_len);
163 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
168 * If object is dying (will not be cached) then remove it
169 * from hash table and LRU.
171 * This is done with hash table and LRU lists locked. As the only
172 * way to acquire first reference to previously unreferenced
173 * object is through hash-table lookup (lu_object_find()),
174 * or LRU scanning (lu_site_purge()), that are done under hash-table
175 * and LRU lock, no race with concurrent object lookup is possible
176 * and we can safely destroy object below.
178 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
179 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
180 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
182 * Object was already removed from hash and lru above, can
185 lu_object_free(env, orig);
187 EXPORT_SYMBOL(lu_object_put);
190 * Put object and don't keep in cache. This is temporary solution for
191 * multi-site objects when its layering is not constant.
193 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
195 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
196 return lu_object_put(env, o);
198 EXPORT_SYMBOL(lu_object_put_nocache);
201 * Kill the object and take it out of LRU cache.
202 * Currently used by client code for layout change.
204 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
206 struct lu_object_header *top;
209 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
210 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
211 struct lu_site *site = o->lo_dev->ld_site;
212 struct cfs_hash *obj_hash = site->ls_obj_hash;
213 struct cfs_hash_bd bd;
215 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
216 if (!list_empty(&top->loh_lru)) {
217 struct lu_site_bkt_data *bkt;
219 list_del_init(&top->loh_lru);
220 bkt = cfs_hash_bd_extra_get(obj_hash, &bd);
222 percpu_counter_dec(&site->ls_lru_len_counter);
224 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
225 cfs_hash_bd_unlock(obj_hash, &bd, 1);
228 EXPORT_SYMBOL(lu_object_unhash);
231 * Allocate new object.
233 * This follows object creation protocol, described in the comment within
234 * struct lu_device_operations definition.
236 static struct lu_object *lu_object_alloc(const struct lu_env *env,
237 struct lu_device *dev,
238 const struct lu_fid *f,
239 const struct lu_object_conf *conf)
241 struct lu_object *scan;
242 struct lu_object *top;
243 struct list_head *layers;
244 unsigned int init_mask = 0;
245 unsigned int init_flag;
251 * Create top-level object slice. This will also create
254 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
256 RETURN(ERR_PTR(-ENOMEM));
260 * This is the only place where object fid is assigned. It's constant
263 top->lo_header->loh_fid = *f;
264 layers = &top->lo_header->loh_layers;
268 * Call ->loo_object_init() repeatedly, until no more new
269 * object slices are created.
273 list_for_each_entry(scan, layers, lo_linkage) {
274 if (init_mask & init_flag)
277 scan->lo_header = top->lo_header;
278 result = scan->lo_ops->loo_object_init(env, scan, conf);
280 lu_object_free(env, top);
281 RETURN(ERR_PTR(result));
283 init_mask |= init_flag;
289 list_for_each_entry_reverse(scan, layers, lo_linkage) {
290 if (scan->lo_ops->loo_object_start != NULL) {
291 result = scan->lo_ops->loo_object_start(env, scan);
293 lu_object_free(env, top);
294 RETURN(ERR_PTR(result));
299 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
306 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
308 struct lu_site_bkt_data *bkt;
309 struct lu_site *site;
310 struct lu_object *scan;
311 struct list_head *layers;
312 struct list_head splice;
314 site = o->lo_dev->ld_site;
315 layers = &o->lo_header->loh_layers;
316 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
318 * First call ->loo_object_delete() method to release all resources.
320 list_for_each_entry_reverse(scan, layers, lo_linkage) {
321 if (scan->lo_ops->loo_object_delete != NULL)
322 scan->lo_ops->loo_object_delete(env, scan);
326 * Then, splice object layers into stand-alone list, and call
327 * ->loo_object_free() on all layers to free memory. Splice is
328 * necessary, because lu_object_header is freed together with the
331 INIT_LIST_HEAD(&splice);
332 list_splice_init(layers, &splice);
333 while (!list_empty(&splice)) {
335 * Free layers in bottom-to-top order, so that object header
336 * lives as long as possible and ->loo_object_free() methods
337 * can look at its contents.
339 o = container_of0(splice.prev, struct lu_object, lo_linkage);
340 list_del_init(&o->lo_linkage);
341 LASSERT(o->lo_ops->loo_object_free != NULL);
342 o->lo_ops->loo_object_free(env, o);
345 if (waitqueue_active(&bkt->lsb_marche_funebre))
346 wake_up_all(&bkt->lsb_marche_funebre);
350 * Free \a nr objects from the cold end of the site LRU list.
352 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
354 struct lu_object_header *h;
355 struct lu_object_header *temp;
356 struct lu_site_bkt_data *bkt;
357 struct cfs_hash_bd bd;
358 struct cfs_hash_bd bd2;
359 struct list_head dispose;
361 unsigned int start = 0;
366 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
369 INIT_LIST_HEAD(&dispose);
371 * Under LRU list lock, scan LRU list and move unreferenced objects to
372 * the dispose list, removing them from LRU and hash table.
375 start = s->ls_purge_start;
376 bnr = (nr == ~0) ? -1 : nr / (int)CFS_HASH_NBKT(s->ls_obj_hash) + 1;
379 * It doesn't make any sense to make purge threads parallel, that can
380 * only bring troubles to us. See LU-5331.
382 mutex_lock(&s->ls_purge_mutex);
384 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
388 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
389 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
391 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
392 LASSERT(atomic_read(&h->loh_ref) == 0);
394 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
395 LASSERT(bd.bd_bucket == bd2.bd_bucket);
397 cfs_hash_bd_del_locked(s->ls_obj_hash,
399 list_move(&h->loh_lru, &dispose);
401 percpu_counter_dec(&s->ls_lru_len_counter);
405 if (nr != ~0 && --nr == 0)
408 if (count > 0 && --count == 0)
412 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
415 * Free everything on the dispose list. This is safe against
416 * races due to the reasons described in lu_object_put().
418 while (!list_empty(&dispose)) {
419 h = container_of0(dispose.next,
420 struct lu_object_header, loh_lru);
421 list_del_init(&h->loh_lru);
422 lu_object_free(env, lu_object_top(h));
423 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
429 mutex_unlock(&s->ls_purge_mutex);
431 if (nr != 0 && did_sth && start != 0) {
432 start = 0; /* restart from the first bucket */
435 /* race on s->ls_purge_start, but nobody cares */
436 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
440 EXPORT_SYMBOL(lu_site_purge);
445 * Code below has to jump through certain loops to output object description
446 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
447 * composes object description from strings that are parts of _lines_ of
448 * output (i.e., strings that are not terminated by newline). This doesn't fit
449 * very well into libcfs_debug_msg() interface that assumes that each message
450 * supplied to it is a self-contained output line.
452 * To work around this, strings are collected in a temporary buffer
453 * (implemented as a value of lu_cdebug_key key), until terminating newline
454 * character is detected.
462 * XXX overflow is not handled correctly.
467 struct lu_cdebug_data {
471 char lck_area[LU_CDEBUG_LINE];
474 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
475 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
478 * Key, holding temporary buffer. This key is registered very early by
481 static struct lu_context_key lu_global_key = {
482 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
483 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
484 .lct_init = lu_global_key_init,
485 .lct_fini = lu_global_key_fini
489 * Printer function emitting messages through libcfs_debug_msg().
491 int lu_cdebug_printer(const struct lu_env *env,
492 void *cookie, const char *format, ...)
494 struct libcfs_debug_msg_data *msgdata = cookie;
495 struct lu_cdebug_data *key;
500 va_start(args, format);
502 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
503 LASSERT(key != NULL);
505 used = strlen(key->lck_area);
506 complete = format[strlen(format) - 1] == '\n';
508 * Append new chunk to the buffer.
510 vsnprintf(key->lck_area + used,
511 ARRAY_SIZE(key->lck_area) - used, format, args);
513 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
514 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
515 key->lck_area[0] = 0;
520 EXPORT_SYMBOL(lu_cdebug_printer);
523 * Print object header.
525 void lu_object_header_print(const struct lu_env *env, void *cookie,
526 lu_printer_t printer,
527 const struct lu_object_header *hdr)
529 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
530 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
532 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
533 list_empty((struct list_head *)&hdr->loh_lru) ? \
535 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
537 EXPORT_SYMBOL(lu_object_header_print);
540 * Print human readable representation of the \a o to the \a printer.
542 void lu_object_print(const struct lu_env *env, void *cookie,
543 lu_printer_t printer, const struct lu_object *o)
545 static const char ruler[] = "........................................";
546 struct lu_object_header *top;
550 lu_object_header_print(env, cookie, printer, top);
551 (*printer)(env, cookie, "{\n");
553 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
555 * print `.' \a depth times followed by type name and address
557 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
558 o->lo_dev->ld_type->ldt_name, o);
560 if (o->lo_ops->loo_object_print != NULL)
561 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
563 (*printer)(env, cookie, "\n");
566 (*printer)(env, cookie, "} header@%p\n", top);
568 EXPORT_SYMBOL(lu_object_print);
571 * Check object consistency.
573 int lu_object_invariant(const struct lu_object *o)
575 struct lu_object_header *top;
578 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
579 if (o->lo_ops->loo_object_invariant != NULL &&
580 !o->lo_ops->loo_object_invariant(o))
586 static struct lu_object *htable_lookup(struct lu_site *s,
587 struct cfs_hash_bd *bd,
588 const struct lu_fid *f,
589 wait_queue_t *waiter,
592 struct lu_site_bkt_data *bkt;
593 struct lu_object_header *h;
594 struct hlist_node *hnode;
595 __u64 ver = cfs_hash_bd_version_get(bd);
598 return ERR_PTR(-ENOENT);
601 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
602 /* cfs_hash_bd_peek_locked is a somehow "internal" function
603 * of cfs_hash, it doesn't add refcount on object. */
604 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
606 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
607 return ERR_PTR(-ENOENT);
610 h = container_of0(hnode, struct lu_object_header, loh_hash);
611 if (likely(!lu_object_is_dying(h))) {
612 cfs_hash_get(s->ls_obj_hash, hnode);
613 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
614 if (!list_empty(&h->loh_lru)) {
615 list_del_init(&h->loh_lru);
617 percpu_counter_dec(&s->ls_lru_len_counter);
619 return lu_object_top(h);
623 * Lookup found an object being destroyed this object cannot be
624 * returned (to assure that references to dying objects are eventually
625 * drained), and moreover, lookup has to wait until object is freed.
628 if (likely(waiter != NULL)) {
629 init_waitqueue_entry(waiter, current);
630 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
631 set_current_state(TASK_UNINTERRUPTIBLE);
632 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
635 return ERR_PTR(-EAGAIN);
639 * Search cache for an object with the fid \a f. If such object is found,
640 * return it. Otherwise, create new object, insert it into cache and return
641 * it. In any case, additional reference is acquired on the returned object.
643 struct lu_object *lu_object_find(const struct lu_env *env,
644 struct lu_device *dev, const struct lu_fid *f,
645 const struct lu_object_conf *conf)
647 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
649 EXPORT_SYMBOL(lu_object_find);
652 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
653 * the calculation for the number of objects to reclaim is not covered by
654 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
655 * This ensures that many concurrent threads will not accidentally purge
658 static void lu_object_limit(const struct lu_env *env,
659 struct lu_device *dev)
663 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
666 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
667 nr = (__u64)lu_cache_nr;
669 lu_site_purge(env, dev->ld_site,
670 MIN(size - nr, LU_CACHE_NR_MAX_ADJUST));
675 static struct lu_object *lu_object_new(const struct lu_env *env,
676 struct lu_device *dev,
677 const struct lu_fid *f,
678 const struct lu_object_conf *conf)
682 struct cfs_hash_bd bd;
684 o = lu_object_alloc(env, dev, f, conf);
685 if (unlikely(IS_ERR(o)))
688 hs = dev->ld_site->ls_obj_hash;
689 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
690 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
691 cfs_hash_bd_unlock(hs, &bd, 1);
693 lu_object_limit(env, dev);
699 * Core logic of lu_object_find*() functions.
701 static struct lu_object *lu_object_find_try(const struct lu_env *env,
702 struct lu_device *dev,
703 const struct lu_fid *f,
704 const struct lu_object_conf *conf,
705 wait_queue_t *waiter)
708 struct lu_object *shadow;
711 struct cfs_hash_bd bd;
715 * This uses standard index maintenance protocol:
717 * - search index under lock, and return object if found;
718 * - otherwise, unlock index, allocate new object;
719 * - lock index and search again;
720 * - if nothing is found (usual case), insert newly created
722 * - otherwise (race: other thread inserted object), free
723 * object just allocated.
727 * For "LOC_F_NEW" case, we are sure the object is new established.
728 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
729 * just alloc and insert directly.
731 * If dying object is found during index search, add @waiter to the
732 * site wait-queue and return ERR_PTR(-EAGAIN).
734 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
735 return lu_object_new(env, dev, f, conf);
739 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
740 o = htable_lookup(s, &bd, f, waiter, &version);
741 cfs_hash_bd_unlock(hs, &bd, 1);
742 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
746 * Allocate new object. This may result in rather complicated
747 * operations, including fld queries, inode loading, etc.
749 o = lu_object_alloc(env, dev, f, conf);
750 if (unlikely(IS_ERR(o)))
753 LASSERT(lu_fid_eq(lu_object_fid(o), f));
755 cfs_hash_bd_lock(hs, &bd, 1);
757 shadow = htable_lookup(s, &bd, f, waiter, &version);
758 if (likely(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT)) {
759 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
760 cfs_hash_bd_unlock(hs, &bd, 1);
762 lu_object_limit(env, dev);
767 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
768 cfs_hash_bd_unlock(hs, &bd, 1);
769 lu_object_free(env, o);
774 * Much like lu_object_find(), but top level device of object is specifically
775 * \a dev rather than top level device of the site. This interface allows
776 * objects of different "stacking" to be created within the same site.
778 struct lu_object *lu_object_find_at(const struct lu_env *env,
779 struct lu_device *dev,
780 const struct lu_fid *f,
781 const struct lu_object_conf *conf)
783 struct lu_site_bkt_data *bkt;
784 struct lu_object *obj;
787 if (conf != NULL && conf->loc_flags & LOC_F_NOWAIT)
788 return lu_object_find_try(env, dev, f, conf, NULL);
791 obj = lu_object_find_try(env, dev, f, conf, &wait);
792 if (obj != ERR_PTR(-EAGAIN))
795 * lu_object_find_try() already added waiter into the
799 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
800 remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
803 EXPORT_SYMBOL(lu_object_find_at);
806 * Find object with given fid, and return its slice belonging to given device.
808 struct lu_object *lu_object_find_slice(const struct lu_env *env,
809 struct lu_device *dev,
810 const struct lu_fid *f,
811 const struct lu_object_conf *conf)
813 struct lu_object *top;
814 struct lu_object *obj;
816 top = lu_object_find(env, dev, f, conf);
820 obj = lu_object_locate(top->lo_header, dev->ld_type);
821 if (unlikely(obj == NULL)) {
822 lu_object_put(env, top);
823 obj = ERR_PTR(-ENOENT);
828 EXPORT_SYMBOL(lu_object_find_slice);
831 * Global list of all device types.
833 static struct list_head lu_device_types;
835 int lu_device_type_init(struct lu_device_type *ldt)
839 atomic_set(&ldt->ldt_device_nr, 0);
840 INIT_LIST_HEAD(&ldt->ldt_linkage);
841 if (ldt->ldt_ops->ldto_init)
842 result = ldt->ldt_ops->ldto_init(ldt);
845 spin_lock(&obd_types_lock);
846 list_add(&ldt->ldt_linkage, &lu_device_types);
847 spin_unlock(&obd_types_lock);
852 EXPORT_SYMBOL(lu_device_type_init);
854 void lu_device_type_fini(struct lu_device_type *ldt)
856 spin_lock(&obd_types_lock);
857 list_del_init(&ldt->ldt_linkage);
858 spin_unlock(&obd_types_lock);
859 if (ldt->ldt_ops->ldto_fini)
860 ldt->ldt_ops->ldto_fini(ldt);
862 EXPORT_SYMBOL(lu_device_type_fini);
865 * Global list of all sites on this node
867 static struct list_head lu_sites;
868 static struct rw_semaphore lu_sites_guard;
871 * Global environment used by site shrinker.
873 static struct lu_env lu_shrink_env;
875 struct lu_site_print_arg {
876 struct lu_env *lsp_env;
878 lu_printer_t lsp_printer;
882 lu_site_obj_print(struct cfs_hash *hs, struct cfs_hash_bd *bd,
883 struct hlist_node *hnode, void *data)
885 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
886 struct lu_object_header *h;
888 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
889 if (!list_empty(&h->loh_layers)) {
890 const struct lu_object *o;
892 o = lu_object_top(h);
893 lu_object_print(arg->lsp_env, arg->lsp_cookie,
894 arg->lsp_printer, o);
896 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
897 arg->lsp_printer, h);
903 * Print all objects in \a s.
905 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
906 lu_printer_t printer)
908 struct lu_site_print_arg arg = {
909 .lsp_env = (struct lu_env *)env,
910 .lsp_cookie = cookie,
911 .lsp_printer = printer,
914 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
916 EXPORT_SYMBOL(lu_site_print);
919 * Return desired hash table order.
921 static unsigned long lu_htable_order(struct lu_device *top)
923 unsigned long cache_size;
925 unsigned long bits_max = LU_SITE_BITS_MAX;
928 * For ZFS based OSDs the cache should be disabled by default. This
929 * allows the ZFS ARC maximum flexibility in determining what buffers
930 * to cache. If Lustre has objects or buffer which it wants to ensure
931 * always stay cached it must maintain a hold on them.
933 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
934 lu_cache_percent = 1;
935 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
936 return LU_SITE_BITS_MIN;
939 if (strcmp(top->ld_type->ldt_name, LUSTRE_VVP_NAME) == 0)
940 bits_max = LU_SITE_BITS_MAX_CL;
943 * Calculate hash table size, assuming that we want reasonable
944 * performance when 20% of total memory is occupied by cache of
947 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
949 cache_size = totalram_pages;
951 #if BITS_PER_LONG == 32
952 /* limit hashtable size for lowmem systems to low RAM */
953 if (cache_size > 1 << (30 - PAGE_CACHE_SHIFT))
954 cache_size = 1 << (30 - PAGE_CACHE_SHIFT) * 3 / 4;
957 /* clear off unreasonable cache setting. */
958 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
959 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
960 " the range of (0, %u]. Will use default value: %u.\n",
961 lu_cache_percent, LU_CACHE_PERCENT_MAX,
962 LU_CACHE_PERCENT_DEFAULT);
964 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
966 cache_size = cache_size / 100 * lu_cache_percent *
967 (PAGE_CACHE_SIZE / 1024);
969 for (bits = 1; (1 << bits) < cache_size; ++bits) {
973 return clamp_t(typeof(bits), bits, LU_SITE_BITS_MIN, bits_max);
976 static unsigned lu_obj_hop_hash(struct cfs_hash *hs,
977 const void *key, unsigned mask)
979 struct lu_fid *fid = (struct lu_fid *)key;
982 hash = fid_flatten32(fid);
983 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
984 hash = hash_long(hash, hs->hs_bkt_bits);
986 /* give me another random factor */
987 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
989 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
990 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
995 static void *lu_obj_hop_object(struct hlist_node *hnode)
997 return hlist_entry(hnode, struct lu_object_header, loh_hash);
1000 static void *lu_obj_hop_key(struct hlist_node *hnode)
1002 struct lu_object_header *h;
1004 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1008 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
1010 struct lu_object_header *h;
1012 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1013 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
1016 static void lu_obj_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1018 struct lu_object_header *h;
1020 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1021 atomic_inc(&h->loh_ref);
1024 static void lu_obj_hop_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
1026 LBUG(); /* we should never called it */
1029 static struct cfs_hash_ops lu_site_hash_ops = {
1030 .hs_hash = lu_obj_hop_hash,
1031 .hs_key = lu_obj_hop_key,
1032 .hs_keycmp = lu_obj_hop_keycmp,
1033 .hs_object = lu_obj_hop_object,
1034 .hs_get = lu_obj_hop_get,
1035 .hs_put_locked = lu_obj_hop_put_locked,
1038 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1040 spin_lock(&s->ls_ld_lock);
1041 if (list_empty(&d->ld_linkage))
1042 list_add(&d->ld_linkage, &s->ls_ld_linkage);
1043 spin_unlock(&s->ls_ld_lock);
1045 EXPORT_SYMBOL(lu_dev_add_linkage);
1047 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1049 spin_lock(&s->ls_ld_lock);
1050 list_del_init(&d->ld_linkage);
1051 spin_unlock(&s->ls_ld_lock);
1053 EXPORT_SYMBOL(lu_dev_del_linkage);
1056 * Initialize site \a s, with \a d as the top level device.
1058 int lu_site_init(struct lu_site *s, struct lu_device *top)
1060 struct lu_site_bkt_data *bkt;
1061 struct cfs_hash_bd bd;
1068 memset(s, 0, sizeof *s);
1069 mutex_init(&s->ls_purge_mutex);
1071 #ifdef HAVE_PERCPU_COUNTER_INIT_GFP_FLAG
1072 rc = percpu_counter_init(&s->ls_lru_len_counter, 0, GFP_NOFS);
1074 rc = percpu_counter_init(&s->ls_lru_len_counter, 0);
1079 snprintf(name, sizeof(name), "lu_site_%s", top->ld_type->ldt_name);
1080 for (bits = lu_htable_order(top);
1081 bits >= LU_SITE_BITS_MIN; bits--) {
1082 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1083 bits - LU_SITE_BKT_BITS,
1086 CFS_HASH_SPIN_BKTLOCK |
1087 CFS_HASH_NO_ITEMREF |
1089 CFS_HASH_ASSERT_EMPTY |
1091 if (s->ls_obj_hash != NULL)
1095 if (s->ls_obj_hash == NULL) {
1096 CERROR("failed to create lu_site hash with bits: %lu\n", bits);
1100 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1101 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1102 INIT_LIST_HEAD(&bkt->lsb_lru);
1103 init_waitqueue_head(&bkt->lsb_marche_funebre);
1106 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1107 if (s->ls_stats == NULL) {
1108 cfs_hash_putref(s->ls_obj_hash);
1109 s->ls_obj_hash = NULL;
1113 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1114 0, "created", "created");
1115 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1116 0, "cache_hit", "cache_hit");
1117 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1118 0, "cache_miss", "cache_miss");
1119 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1120 0, "cache_race", "cache_race");
1121 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1122 0, "cache_death_race", "cache_death_race");
1123 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1124 0, "lru_purged", "lru_purged");
1126 INIT_LIST_HEAD(&s->ls_linkage);
1127 s->ls_top_dev = top;
1130 lu_ref_add(&top->ld_reference, "site-top", s);
1132 INIT_LIST_HEAD(&s->ls_ld_linkage);
1133 spin_lock_init(&s->ls_ld_lock);
1135 lu_dev_add_linkage(s, top);
1139 EXPORT_SYMBOL(lu_site_init);
1142 * Finalize \a s and release its resources.
1144 void lu_site_fini(struct lu_site *s)
1146 down_write(&lu_sites_guard);
1147 list_del_init(&s->ls_linkage);
1148 up_write(&lu_sites_guard);
1150 percpu_counter_destroy(&s->ls_lru_len_counter);
1152 if (s->ls_obj_hash != NULL) {
1153 cfs_hash_putref(s->ls_obj_hash);
1154 s->ls_obj_hash = NULL;
1157 if (s->ls_top_dev != NULL) {
1158 s->ls_top_dev->ld_site = NULL;
1159 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1160 lu_device_put(s->ls_top_dev);
1161 s->ls_top_dev = NULL;
1164 if (s->ls_stats != NULL)
1165 lprocfs_free_stats(&s->ls_stats);
1167 EXPORT_SYMBOL(lu_site_fini);
1170 * Called when initialization of stack for this site is completed.
1172 int lu_site_init_finish(struct lu_site *s)
1175 down_write(&lu_sites_guard);
1176 result = lu_context_refill(&lu_shrink_env.le_ctx);
1178 list_add(&s->ls_linkage, &lu_sites);
1179 up_write(&lu_sites_guard);
1182 EXPORT_SYMBOL(lu_site_init_finish);
1185 * Acquire additional reference on device \a d
1187 void lu_device_get(struct lu_device *d)
1189 atomic_inc(&d->ld_ref);
1191 EXPORT_SYMBOL(lu_device_get);
1194 * Release reference on device \a d.
1196 void lu_device_put(struct lu_device *d)
1198 LASSERT(atomic_read(&d->ld_ref) > 0);
1199 atomic_dec(&d->ld_ref);
1201 EXPORT_SYMBOL(lu_device_put);
1204 * Initialize device \a d of type \a t.
1206 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1208 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1209 t->ldt_ops->ldto_start != NULL)
1210 t->ldt_ops->ldto_start(t);
1212 memset(d, 0, sizeof *d);
1214 lu_ref_init(&d->ld_reference);
1215 INIT_LIST_HEAD(&d->ld_linkage);
1219 EXPORT_SYMBOL(lu_device_init);
1222 * Finalize device \a d.
1224 void lu_device_fini(struct lu_device *d)
1226 struct lu_device_type *t = d->ld_type;
1228 if (d->ld_obd != NULL) {
1229 d->ld_obd->obd_lu_dev = NULL;
1233 lu_ref_fini(&d->ld_reference);
1234 LASSERTF(atomic_read(&d->ld_ref) == 0,
1235 "Refcount is %u\n", atomic_read(&d->ld_ref));
1236 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1238 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1239 t->ldt_ops->ldto_stop != NULL)
1240 t->ldt_ops->ldto_stop(t);
1242 EXPORT_SYMBOL(lu_device_fini);
1245 * Initialize object \a o that is part of compound object \a h and was created
1248 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1249 struct lu_device *d)
1251 memset(o, 0, sizeof(*o));
1255 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1256 INIT_LIST_HEAD(&o->lo_linkage);
1260 EXPORT_SYMBOL(lu_object_init);
1263 * Finalize object and release its resources.
1265 void lu_object_fini(struct lu_object *o)
1267 struct lu_device *dev = o->lo_dev;
1269 LASSERT(list_empty(&o->lo_linkage));
1272 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1278 EXPORT_SYMBOL(lu_object_fini);
1281 * Add object \a o as first layer of compound object \a h
1283 * This is typically called by the ->ldo_object_alloc() method of top-level
1286 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1288 list_move(&o->lo_linkage, &h->loh_layers);
1290 EXPORT_SYMBOL(lu_object_add_top);
1293 * Add object \a o as a layer of compound object, going after \a before.
1295 * This is typically called by the ->ldo_object_alloc() method of \a
1298 void lu_object_add(struct lu_object *before, struct lu_object *o)
1300 list_move(&o->lo_linkage, &before->lo_linkage);
1302 EXPORT_SYMBOL(lu_object_add);
1305 * Initialize compound object.
1307 int lu_object_header_init(struct lu_object_header *h)
1309 memset(h, 0, sizeof *h);
1310 atomic_set(&h->loh_ref, 1);
1311 INIT_HLIST_NODE(&h->loh_hash);
1312 INIT_LIST_HEAD(&h->loh_lru);
1313 INIT_LIST_HEAD(&h->loh_layers);
1314 lu_ref_init(&h->loh_reference);
1317 EXPORT_SYMBOL(lu_object_header_init);
1320 * Finalize compound object.
1322 void lu_object_header_fini(struct lu_object_header *h)
1324 LASSERT(list_empty(&h->loh_layers));
1325 LASSERT(list_empty(&h->loh_lru));
1326 LASSERT(hlist_unhashed(&h->loh_hash));
1327 lu_ref_fini(&h->loh_reference);
1329 EXPORT_SYMBOL(lu_object_header_fini);
1332 * Given a compound object, find its slice, corresponding to the device type
1335 struct lu_object *lu_object_locate(struct lu_object_header *h,
1336 const struct lu_device_type *dtype)
1338 struct lu_object *o;
1340 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1341 if (o->lo_dev->ld_type == dtype)
1346 EXPORT_SYMBOL(lu_object_locate);
1349 * Finalize and free devices in the device stack.
1351 * Finalize device stack by purging object cache, and calling
1352 * lu_device_type_operations::ldto_device_fini() and
1353 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1355 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1357 struct lu_site *site = top->ld_site;
1358 struct lu_device *scan;
1359 struct lu_device *next;
1361 lu_site_purge(env, site, ~0);
1362 for (scan = top; scan != NULL; scan = next) {
1363 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1364 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1365 lu_device_put(scan);
1369 lu_site_purge(env, site, ~0);
1371 for (scan = top; scan != NULL; scan = next) {
1372 const struct lu_device_type *ldt = scan->ld_type;
1373 struct obd_type *type;
1375 next = ldt->ldt_ops->ldto_device_free(env, scan);
1376 type = ldt->ldt_obd_type;
1379 class_put_type(type);
1386 * Maximal number of tld slots.
1388 LU_CONTEXT_KEY_NR = 40
1391 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1393 DEFINE_RWLOCK(lu_keys_guard);
1394 static atomic_t lu_key_initing_cnt = ATOMIC_INIT(0);
1397 * Global counter incremented whenever key is registered, unregistered,
1398 * revived or quiesced. This is used to void unnecessary calls to
1399 * lu_context_refill(). No locking is provided, as initialization and shutdown
1400 * are supposed to be externally serialized.
1402 static unsigned key_set_version = 0;
1407 int lu_context_key_register(struct lu_context_key *key)
1412 LASSERT(key->lct_init != NULL);
1413 LASSERT(key->lct_fini != NULL);
1414 LASSERT(key->lct_tags != 0);
1415 LASSERT(key->lct_owner != NULL);
1418 write_lock(&lu_keys_guard);
1419 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1420 if (lu_keys[i] == NULL) {
1422 atomic_set(&key->lct_used, 1);
1424 lu_ref_init(&key->lct_reference);
1430 write_unlock(&lu_keys_guard);
1433 EXPORT_SYMBOL(lu_context_key_register);
1435 static void key_fini(struct lu_context *ctx, int index)
1437 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1438 struct lu_context_key *key;
1440 key = lu_keys[index];
1441 LASSERT(key != NULL);
1442 LASSERT(key->lct_fini != NULL);
1443 LASSERT(atomic_read(&key->lct_used) > 1);
1445 key->lct_fini(ctx, key, ctx->lc_value[index]);
1446 lu_ref_del(&key->lct_reference, "ctx", ctx);
1447 atomic_dec(&key->lct_used);
1449 LASSERT(key->lct_owner != NULL);
1450 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1451 LINVRNT(module_refcount(key->lct_owner) > 0);
1452 module_put(key->lct_owner);
1454 ctx->lc_value[index] = NULL;
1461 void lu_context_key_degister(struct lu_context_key *key)
1463 LASSERT(atomic_read(&key->lct_used) >= 1);
1464 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1466 lu_context_key_quiesce(key);
1469 write_lock(&lu_keys_guard);
1470 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1473 * Wait until all transient contexts referencing this key have
1474 * run lu_context_key::lct_fini() method.
1476 while (atomic_read(&key->lct_used) > 1) {
1477 write_unlock(&lu_keys_guard);
1478 CDEBUG(D_INFO, "lu_context_key_degister: \"%s\" %p, %d\n",
1479 key->lct_owner ? key->lct_owner->name : "", key,
1480 atomic_read(&key->lct_used));
1482 write_lock(&lu_keys_guard);
1484 if (lu_keys[key->lct_index]) {
1485 lu_keys[key->lct_index] = NULL;
1486 lu_ref_fini(&key->lct_reference);
1488 write_unlock(&lu_keys_guard);
1490 LASSERTF(atomic_read(&key->lct_used) == 1,
1491 "key has instances: %d\n",
1492 atomic_read(&key->lct_used));
1494 EXPORT_SYMBOL(lu_context_key_degister);
1497 * Register a number of keys. This has to be called after all keys have been
1498 * initialized by a call to LU_CONTEXT_KEY_INIT().
1500 int lu_context_key_register_many(struct lu_context_key *k, ...)
1502 struct lu_context_key *key = k;
1508 result = lu_context_key_register(key);
1511 key = va_arg(args, struct lu_context_key *);
1512 } while (key != NULL);
1518 lu_context_key_degister(k);
1519 k = va_arg(args, struct lu_context_key *);
1526 EXPORT_SYMBOL(lu_context_key_register_many);
1529 * De-register a number of keys. This is a dual to
1530 * lu_context_key_register_many().
1532 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1538 lu_context_key_degister(k);
1539 k = va_arg(args, struct lu_context_key*);
1540 } while (k != NULL);
1543 EXPORT_SYMBOL(lu_context_key_degister_many);
1546 * Revive a number of keys.
1548 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1554 lu_context_key_revive(k);
1555 k = va_arg(args, struct lu_context_key*);
1556 } while (k != NULL);
1559 EXPORT_SYMBOL(lu_context_key_revive_many);
1562 * Quiescent a number of keys.
1564 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1570 lu_context_key_quiesce(k);
1571 k = va_arg(args, struct lu_context_key*);
1572 } while (k != NULL);
1575 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1578 * Return value associated with key \a key in context \a ctx.
1580 void *lu_context_key_get(const struct lu_context *ctx,
1581 const struct lu_context_key *key)
1583 LINVRNT(ctx->lc_state == LCS_ENTERED);
1584 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1585 LASSERT(lu_keys[key->lct_index] == key);
1586 return ctx->lc_value[key->lct_index];
1588 EXPORT_SYMBOL(lu_context_key_get);
1591 * List of remembered contexts. XXX document me.
1593 static struct list_head lu_context_remembered;
1596 * Destroy \a key in all remembered contexts. This is used to destroy key
1597 * values in "shared" contexts (like service threads), when a module owning
1598 * the key is about to be unloaded.
1600 void lu_context_key_quiesce(struct lu_context_key *key)
1602 struct lu_context *ctx;
1604 if (!(key->lct_tags & LCT_QUIESCENT)) {
1606 * XXX memory barrier has to go here.
1608 write_lock(&lu_keys_guard);
1609 key->lct_tags |= LCT_QUIESCENT;
1612 * Wait until all lu_context_key::lct_init() methods
1615 while (atomic_read(&lu_key_initing_cnt) > 0) {
1616 write_unlock(&lu_keys_guard);
1617 CDEBUG(D_INFO, "lu_context_key_quiesce: \"%s\""
1619 key->lct_owner ? key->lct_owner->name : "",
1620 key, atomic_read(&key->lct_used),
1621 atomic_read(&lu_key_initing_cnt));
1623 write_lock(&lu_keys_guard);
1626 list_for_each_entry(ctx, &lu_context_remembered,
1628 key_fini(ctx, key->lct_index);
1629 write_unlock(&lu_keys_guard);
1634 void lu_context_key_revive(struct lu_context_key *key)
1636 key->lct_tags &= ~LCT_QUIESCENT;
1640 static void keys_fini(struct lu_context *ctx)
1644 if (ctx->lc_value == NULL)
1647 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1650 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1651 ctx->lc_value = NULL;
1654 static int keys_fill(struct lu_context *ctx)
1659 * A serialisation with lu_context_key_quiesce() is needed, but some
1660 * "key->lct_init()" are calling kernel memory allocation routine and
1661 * can't be called while holding a spin_lock.
1662 * "lu_keys_guard" is held while incrementing "lu_key_initing_cnt"
1663 * to ensure the start of the serialisation.
1664 * An atomic_t variable is still used, in order not to reacquire the
1665 * lock when decrementing the counter.
1667 read_lock(&lu_keys_guard);
1668 atomic_inc(&lu_key_initing_cnt);
1669 read_unlock(&lu_keys_guard);
1671 LINVRNT(ctx->lc_value != NULL);
1672 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1673 struct lu_context_key *key;
1676 if (ctx->lc_value[i] == NULL && key != NULL &&
1677 (key->lct_tags & ctx->lc_tags) &&
1679 * Don't create values for a LCT_QUIESCENT key, as this
1680 * will pin module owning a key.
1682 !(key->lct_tags & LCT_QUIESCENT)) {
1685 LINVRNT(key->lct_init != NULL);
1686 LINVRNT(key->lct_index == i);
1688 LASSERT(key->lct_owner != NULL);
1689 if (!(ctx->lc_tags & LCT_NOREF) &&
1690 try_module_get(key->lct_owner) == 0) {
1691 /* module is unloading, skip this key */
1695 value = key->lct_init(ctx, key);
1696 if (unlikely(IS_ERR(value))) {
1697 atomic_dec(&lu_key_initing_cnt);
1698 return PTR_ERR(value);
1701 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1702 atomic_inc(&key->lct_used);
1704 * This is the only place in the code, where an
1705 * element of ctx->lc_value[] array is set to non-NULL
1708 ctx->lc_value[i] = value;
1709 if (key->lct_exit != NULL)
1710 ctx->lc_tags |= LCT_HAS_EXIT;
1712 ctx->lc_version = key_set_version;
1714 atomic_dec(&lu_key_initing_cnt);
1718 static int keys_init(struct lu_context *ctx)
1720 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1721 if (likely(ctx->lc_value != NULL))
1722 return keys_fill(ctx);
1728 * Initialize context data-structure. Create values for all keys.
1730 int lu_context_init(struct lu_context *ctx, __u32 tags)
1734 memset(ctx, 0, sizeof *ctx);
1735 ctx->lc_state = LCS_INITIALIZED;
1736 ctx->lc_tags = tags;
1737 if (tags & LCT_REMEMBER) {
1738 write_lock(&lu_keys_guard);
1739 list_add(&ctx->lc_remember, &lu_context_remembered);
1740 write_unlock(&lu_keys_guard);
1742 INIT_LIST_HEAD(&ctx->lc_remember);
1745 rc = keys_init(ctx);
1747 lu_context_fini(ctx);
1751 EXPORT_SYMBOL(lu_context_init);
1754 * Finalize context data-structure. Destroy key values.
1756 void lu_context_fini(struct lu_context *ctx)
1758 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1759 ctx->lc_state = LCS_FINALIZED;
1761 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1762 LASSERT(list_empty(&ctx->lc_remember));
1765 } else { /* could race with key degister */
1766 write_lock(&lu_keys_guard);
1768 list_del_init(&ctx->lc_remember);
1769 write_unlock(&lu_keys_guard);
1772 EXPORT_SYMBOL(lu_context_fini);
1775 * Called before entering context.
1777 void lu_context_enter(struct lu_context *ctx)
1779 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1780 ctx->lc_state = LCS_ENTERED;
1782 EXPORT_SYMBOL(lu_context_enter);
1785 * Called after exiting from \a ctx
1787 void lu_context_exit(struct lu_context *ctx)
1791 LINVRNT(ctx->lc_state == LCS_ENTERED);
1792 ctx->lc_state = LCS_LEFT;
1793 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1794 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1795 /* could race with key quiescency */
1796 if (ctx->lc_tags & LCT_REMEMBER)
1797 read_lock(&lu_keys_guard);
1798 if (ctx->lc_value[i] != NULL) {
1799 struct lu_context_key *key;
1802 LASSERT(key != NULL);
1803 if (key->lct_exit != NULL)
1805 key, ctx->lc_value[i]);
1807 if (ctx->lc_tags & LCT_REMEMBER)
1808 read_unlock(&lu_keys_guard);
1812 EXPORT_SYMBOL(lu_context_exit);
1815 * Allocate for context all missing keys that were registered after context
1816 * creation. key_set_version is only changed in rare cases when modules
1817 * are loaded and removed.
1819 int lu_context_refill(struct lu_context *ctx)
1821 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1825 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1826 * obd being added. Currently, this is only used on client side, specifically
1827 * for echo device client, for other stack (like ptlrpc threads), context are
1828 * predefined when the lu_device type are registered, during the module probe
1831 __u32 lu_context_tags_default = 0;
1832 __u32 lu_session_tags_default = 0;
1834 void lu_context_tags_update(__u32 tags)
1836 write_lock(&lu_keys_guard);
1837 lu_context_tags_default |= tags;
1839 write_unlock(&lu_keys_guard);
1841 EXPORT_SYMBOL(lu_context_tags_update);
1843 void lu_context_tags_clear(__u32 tags)
1845 write_lock(&lu_keys_guard);
1846 lu_context_tags_default &= ~tags;
1848 write_unlock(&lu_keys_guard);
1850 EXPORT_SYMBOL(lu_context_tags_clear);
1852 void lu_session_tags_update(__u32 tags)
1854 write_lock(&lu_keys_guard);
1855 lu_session_tags_default |= tags;
1857 write_unlock(&lu_keys_guard);
1859 EXPORT_SYMBOL(lu_session_tags_update);
1861 void lu_session_tags_clear(__u32 tags)
1863 write_lock(&lu_keys_guard);
1864 lu_session_tags_default &= ~tags;
1866 write_unlock(&lu_keys_guard);
1868 EXPORT_SYMBOL(lu_session_tags_clear);
1870 int lu_env_init(struct lu_env *env, __u32 tags)
1875 result = lu_context_init(&env->le_ctx, tags);
1876 if (likely(result == 0))
1877 lu_context_enter(&env->le_ctx);
1880 EXPORT_SYMBOL(lu_env_init);
1882 void lu_env_fini(struct lu_env *env)
1884 lu_context_exit(&env->le_ctx);
1885 lu_context_fini(&env->le_ctx);
1888 EXPORT_SYMBOL(lu_env_fini);
1890 int lu_env_refill(struct lu_env *env)
1894 result = lu_context_refill(&env->le_ctx);
1895 if (result == 0 && env->le_ses != NULL)
1896 result = lu_context_refill(env->le_ses);
1899 EXPORT_SYMBOL(lu_env_refill);
1902 * Currently, this API will only be used by echo client.
1903 * Because echo client and normal lustre client will share
1904 * same cl_env cache. So echo client needs to refresh
1905 * the env context after it get one from the cache, especially
1906 * when normal client and echo client co-exist in the same client.
1908 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1913 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1914 env->le_ctx.lc_version = 0;
1915 env->le_ctx.lc_tags |= ctags;
1918 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1919 env->le_ses->lc_version = 0;
1920 env->le_ses->lc_tags |= stags;
1923 result = lu_env_refill(env);
1927 EXPORT_SYMBOL(lu_env_refill_by_tags);
1929 static struct shrinker *lu_site_shrinker;
1931 typedef struct lu_site_stats{
1932 unsigned lss_populated;
1933 unsigned lss_max_search;
1938 static void lu_site_stats_get(struct cfs_hash *hs,
1939 lu_site_stats_t *stats, int populated)
1941 struct cfs_hash_bd bd;
1944 cfs_hash_for_each_bucket(hs, &bd, i) {
1945 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1946 struct hlist_head *hhead;
1948 cfs_hash_bd_lock(hs, &bd, 1);
1950 cfs_hash_bd_count_get(&bd) - bkt->lsb_lru_len;
1951 stats->lss_total += cfs_hash_bd_count_get(&bd);
1952 stats->lss_max_search = max((int)stats->lss_max_search,
1953 cfs_hash_bd_depmax_get(&bd));
1955 cfs_hash_bd_unlock(hs, &bd, 1);
1959 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1960 if (!hlist_empty(hhead))
1961 stats->lss_populated++;
1963 cfs_hash_bd_unlock(hs, &bd, 1);
1969 * lu_cache_shrink_count() returns an approximate number of cached objects
1970 * that can be freed by shrink_slab(). A counter, which tracks the
1971 * number of items in the site's lru, is maintained in a percpu_counter
1972 * for each site. The percpu values are incremented and decremented as
1973 * objects are added or removed from the lru. The percpu values are summed
1974 * and saved whenever a percpu value exceeds a threshold. Thus the saved,
1975 * summed value at any given time may not accurately reflect the current
1976 * lru length. But this value is sufficiently accurate for the needs of
1979 * Using a per cpu counter is a compromise solution to concurrent access:
1980 * lu_object_put() can update the counter without locking the site and
1981 * lu_cache_shrink_count can sum the counters without locking each
1982 * ls_obj_hash bucket.
1984 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
1985 struct shrink_control *sc)
1988 struct lu_site *tmp;
1989 unsigned long cached = 0;
1991 if (!(sc->gfp_mask & __GFP_FS))
1994 down_read(&lu_sites_guard);
1995 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage)
1996 cached += percpu_counter_read_positive(&s->ls_lru_len_counter);
1997 up_read(&lu_sites_guard);
1999 cached = (cached / 100) * sysctl_vfs_cache_pressure;
2000 CDEBUG(D_INODE, "%ld objects cached, cache pressure %d\n",
2001 cached, sysctl_vfs_cache_pressure);
2006 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
2007 struct shrink_control *sc)
2010 struct lu_site *tmp;
2011 unsigned long remain = sc->nr_to_scan;
2014 if (!(sc->gfp_mask & __GFP_FS))
2015 /* We must not take the lu_sites_guard lock when
2016 * __GFP_FS is *not* set because of the deadlock
2017 * possibility detailed above. Additionally,
2018 * since we cannot determine the number of
2019 * objects in the cache without taking this
2020 * lock, we're in a particularly tough spot. As
2021 * a result, we'll just lie and say our cache is
2022 * empty. This _should_ be ok, as we can't
2023 * reclaim objects when __GFP_FS is *not* set
2028 down_write(&lu_sites_guard);
2029 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
2030 remain = lu_site_purge(&lu_shrink_env, s, remain);
2032 * Move just shrunk site to the tail of site list to
2033 * assure shrinking fairness.
2035 list_move_tail(&s->ls_linkage, &splice);
2037 list_splice(&splice, lu_sites.prev);
2038 up_write(&lu_sites_guard);
2040 return sc->nr_to_scan - remain;
2043 #ifndef HAVE_SHRINKER_COUNT
2045 * There exists a potential lock inversion deadlock scenario when using
2046 * Lustre on top of ZFS. This occurs between one of ZFS's
2047 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
2048 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
2049 * while thread B will take the ht_lock and sleep on the lu_sites_guard
2050 * lock. Obviously neither thread will wake and drop their respective hold
2053 * To prevent this from happening we must ensure the lu_sites_guard lock is
2054 * not taken while down this code path. ZFS reliably does not set the
2055 * __GFP_FS bit in its code paths, so this can be used to determine if it
2056 * is safe to take the lu_sites_guard lock.
2058 * Ideally we should accurately return the remaining number of cached
2059 * objects without taking the lu_sites_guard lock, but this is not
2060 * possible in the current implementation.
2062 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
2065 struct shrink_control scv = {
2066 .nr_to_scan = shrink_param(sc, nr_to_scan),
2067 .gfp_mask = shrink_param(sc, gfp_mask)
2069 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
2070 struct shrinker* shrinker = NULL;
2074 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
2076 if (scv.nr_to_scan != 0)
2077 lu_cache_shrink_scan(shrinker, &scv);
2079 cached = lu_cache_shrink_count(shrinker, &scv);
2083 #endif /* HAVE_SHRINKER_COUNT */
2091 * Environment to be used in debugger, contains all tags.
2093 static struct lu_env lu_debugging_env;
2096 * Debugging printer function using printk().
2098 int lu_printk_printer(const struct lu_env *env,
2099 void *unused, const char *format, ...)
2103 va_start(args, format);
2104 vprintk(format, args);
2109 int lu_debugging_setup(void)
2111 return lu_env_init(&lu_debugging_env, ~0);
2114 void lu_context_keys_dump(void)
2118 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2119 struct lu_context_key *key;
2123 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2124 i, key, key->lct_tags,
2125 key->lct_init, key->lct_fini, key->lct_exit,
2126 key->lct_index, atomic_read(&key->lct_used),
2127 key->lct_owner ? key->lct_owner->name : "",
2129 lu_ref_print(&key->lct_reference);
2135 * Initialization of global lu_* data.
2137 int lu_global_init(void)
2140 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2141 lu_cache_shrink_count, lu_cache_shrink_scan);
2143 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2145 INIT_LIST_HEAD(&lu_device_types);
2146 INIT_LIST_HEAD(&lu_context_remembered);
2147 INIT_LIST_HEAD(&lu_sites);
2148 init_rwsem(&lu_sites_guard);
2150 result = lu_ref_global_init();
2154 LU_CONTEXT_KEY_INIT(&lu_global_key);
2155 result = lu_context_key_register(&lu_global_key);
2160 * At this level, we don't know what tags are needed, so allocate them
2161 * conservatively. This should not be too bad, because this
2162 * environment is global.
2164 down_write(&lu_sites_guard);
2165 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2166 up_write(&lu_sites_guard);
2171 * seeks estimation: 3 seeks to read a record from oi, one to read
2172 * inode, one for ea. Unfortunately setting this high value results in
2173 * lu_object/inode cache consuming all the memory.
2175 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2176 if (lu_site_shrinker == NULL)
2183 * Dual to lu_global_init().
2185 void lu_global_fini(void)
2187 if (lu_site_shrinker != NULL) {
2188 remove_shrinker(lu_site_shrinker);
2189 lu_site_shrinker = NULL;
2192 lu_context_key_degister(&lu_global_key);
2195 * Tear shrinker environment down _after_ de-registering
2196 * lu_global_key, because the latter has a value in the former.
2198 down_write(&lu_sites_guard);
2199 lu_env_fini(&lu_shrink_env);
2200 up_write(&lu_sites_guard);
2202 lu_ref_global_fini();
2205 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2207 #ifdef CONFIG_PROC_FS
2208 struct lprocfs_counter ret;
2210 lprocfs_stats_collect(stats, idx, &ret);
2211 return (__u32)ret.lc_count;
2218 * Output site statistical counters into a buffer. Suitable for
2219 * lprocfs_rd_*()-style functions.
2221 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2223 lu_site_stats_t stats;
2225 memset(&stats, 0, sizeof(stats));
2226 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2228 seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2231 stats.lss_populated,
2232 CFS_HASH_NHLIST(s->ls_obj_hash),
2233 stats.lss_max_search,
2234 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2235 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2236 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2237 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2238 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2239 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2242 EXPORT_SYMBOL(lu_site_stats_seq_print);
2245 * Helper function to initialize a number of kmem slab caches at once.
2247 int lu_kmem_init(struct lu_kmem_descr *caches)
2250 struct lu_kmem_descr *iter = caches;
2252 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2253 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2256 if (*iter->ckd_cache == NULL) {
2258 /* free all previously allocated caches */
2259 lu_kmem_fini(caches);
2265 EXPORT_SYMBOL(lu_kmem_init);
2268 * Helper function to finalize a number of kmem slab cached at once. Dual to
2271 void lu_kmem_fini(struct lu_kmem_descr *caches)
2273 for (; caches->ckd_cache != NULL; ++caches) {
2274 if (*caches->ckd_cache != NULL) {
2275 kmem_cache_destroy(*caches->ckd_cache);
2276 *caches->ckd_cache = NULL;
2280 EXPORT_SYMBOL(lu_kmem_fini);
2283 * Temporary solution to be able to assign fid in ->do_create()
2284 * till we have fully-functional OST fids
2286 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2287 const struct lu_fid *fid)
2289 struct lu_site *s = o->lo_dev->ld_site;
2290 struct lu_fid *old = &o->lo_header->loh_fid;
2291 struct lu_object *shadow;
2292 wait_queue_t waiter;
2293 struct cfs_hash *hs;
2294 struct cfs_hash_bd bd;
2297 LASSERT(fid_is_zero(old));
2299 hs = s->ls_obj_hash;
2300 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2301 shadow = htable_lookup(s, &bd, fid, &waiter, &version);
2302 /* supposed to be unique */
2303 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2305 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2306 cfs_hash_bd_unlock(hs, &bd, 1);
2308 EXPORT_SYMBOL(lu_object_assign_fid);
2311 * allocates object with 0 (non-assiged) fid
2312 * XXX: temporary solution to be able to assign fid in ->do_create()
2313 * till we have fully-functional OST fids
2315 struct lu_object *lu_object_anon(const struct lu_env *env,
2316 struct lu_device *dev,
2317 const struct lu_object_conf *conf)
2320 struct lu_object *o;
2323 o = lu_object_alloc(env, dev, &fid, conf);
2327 EXPORT_SYMBOL(lu_object_anon);
2329 struct lu_buf LU_BUF_NULL = {
2333 EXPORT_SYMBOL(LU_BUF_NULL);
2335 void lu_buf_free(struct lu_buf *buf)
2339 LASSERT(buf->lb_len > 0);
2340 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2345 EXPORT_SYMBOL(lu_buf_free);
2347 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2350 LASSERT(buf->lb_buf == NULL);
2351 LASSERT(buf->lb_len == 0);
2352 OBD_ALLOC_LARGE(buf->lb_buf, size);
2353 if (likely(buf->lb_buf))
2356 EXPORT_SYMBOL(lu_buf_alloc);
2358 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2361 lu_buf_alloc(buf, size);
2363 EXPORT_SYMBOL(lu_buf_realloc);
2365 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2367 if (buf->lb_buf == NULL && buf->lb_len == 0)
2368 lu_buf_alloc(buf, len);
2370 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2371 lu_buf_realloc(buf, len);
2375 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2378 * Increase the size of the \a buf.
2379 * preserves old data in buffer
2380 * old buffer remains unchanged on error
2381 * \retval 0 or -ENOMEM
2383 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2387 if (len <= buf->lb_len)
2390 OBD_ALLOC_LARGE(ptr, len);
2394 /* Free the old buf */
2395 if (buf->lb_buf != NULL) {
2396 memcpy(ptr, buf->lb_buf, buf->lb_len);
2397 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);