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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/lu_object.c
35 * These are the only exported functions, they provide some generic
36 * infrastructure for managing object devices
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 #include <linux/module.h>
44 #include <linux/list.h>
45 #include <libcfs/libcfs.h>
46 #include <libcfs/libcfs_hash.h> /* hash_long() */
47 #include <obd_class.h>
48 #include <obd_support.h>
49 #include <lustre_disk.h>
50 #include <lustre_fid.h>
51 #include <lu_object.h>
55 LU_CACHE_PERCENT_MAX = 50,
56 LU_CACHE_PERCENT_DEFAULT = 20
59 #define LU_CACHE_NR_MAX_ADJUST 512
60 #define LU_CACHE_NR_UNLIMITED -1
61 #define LU_CACHE_NR_DEFAULT LU_CACHE_NR_UNLIMITED
62 #define LU_CACHE_NR_LDISKFS_LIMIT LU_CACHE_NR_UNLIMITED
63 /** This is set to roughly (20 * OSS_NTHRS_MAX) to prevent thrashing */
64 #define LU_CACHE_NR_ZFS_LIMIT 10240
66 #define LU_SITE_BITS_MIN 12
67 #define LU_SITE_BITS_MAX 24
68 #define LU_SITE_BITS_MAX_CL 19
70 * total 256 buckets, we don't want too many buckets because:
71 * - consume too much memory
72 * - avoid unbalanced LRU list
74 #define LU_SITE_BKT_BITS 8
77 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
78 module_param(lu_cache_percent, int, 0644);
79 MODULE_PARM_DESC(lu_cache_percent, "Percentage of memory to be used as lu_object cache");
81 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
82 module_param(lu_cache_nr, long, 0644);
83 MODULE_PARM_DESC(lu_cache_nr, "Maximum number of objects in lu_object cache");
85 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
86 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx);
89 * Decrease reference counter on object. If last reference is freed, return
90 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
91 * case, free object immediately.
93 void lu_object_put(const struct lu_env *env, struct lu_object *o)
95 struct lu_site_bkt_data *bkt;
96 struct lu_object_header *top;
98 struct lu_object *orig;
99 struct cfs_hash_bd bd;
100 const struct lu_fid *fid;
103 site = o->lo_dev->ld_site;
107 * till we have full fids-on-OST implemented anonymous objects
108 * are possible in OSP. such an object isn't listed in the site
109 * so we should not remove it from the site.
111 fid = lu_object_fid(o);
112 if (fid_is_zero(fid)) {
113 LASSERT(top->loh_hash.next == NULL
114 && top->loh_hash.pprev == NULL);
115 LASSERT(list_empty(&top->loh_lru));
116 if (!atomic_dec_and_test(&top->loh_ref))
118 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
119 if (o->lo_ops->loo_object_release != NULL)
120 o->lo_ops->loo_object_release(env, o);
122 lu_object_free(env, orig);
126 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
127 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
129 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
130 if (lu_object_is_dying(top)) {
133 * somebody may be waiting for this, currently only
134 * used for cl_object, see cl_object_put_last().
136 wake_up_all(&bkt->lsb_marche_funebre);
142 * When last reference is released, iterate over object
143 * layers, and notify them that object is no longer busy.
145 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
146 if (o->lo_ops->loo_object_release != NULL)
147 o->lo_ops->loo_object_release(env, o);
150 if (!lu_object_is_dying(top) &&
151 (lu_object_exists(orig) || lu_object_is_cl(orig))) {
152 LASSERT(list_empty(&top->loh_lru));
153 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
155 percpu_counter_inc(&site->ls_lru_len_counter);
156 CDEBUG(D_INODE, "Add %p to site lru. hash: %p, bkt: %p, "
158 o, site->ls_obj_hash, bkt, bkt->lsb_lru_len);
159 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
164 * If object is dying (will not be cached) then remove it
165 * from hash table and LRU.
167 * This is done with hash table and LRU lists locked. As the only
168 * way to acquire first reference to previously unreferenced
169 * object is through hash-table lookup (lu_object_find()),
170 * or LRU scanning (lu_site_purge()), that are done under hash-table
171 * and LRU lock, no race with concurrent object lookup is possible
172 * and we can safely destroy object below.
174 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
175 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
176 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
178 * Object was already removed from hash and lru above, can
181 lu_object_free(env, orig);
183 EXPORT_SYMBOL(lu_object_put);
186 * Put object and don't keep in cache. This is temporary solution for
187 * multi-site objects when its layering is not constant.
189 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
191 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
192 return lu_object_put(env, o);
194 EXPORT_SYMBOL(lu_object_put_nocache);
197 * Kill the object and take it out of LRU cache.
198 * Currently used by client code for layout change.
200 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
202 struct lu_object_header *top;
205 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
206 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
207 struct lu_site *site = o->lo_dev->ld_site;
208 struct cfs_hash *obj_hash = site->ls_obj_hash;
209 struct cfs_hash_bd bd;
211 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
212 if (!list_empty(&top->loh_lru)) {
213 struct lu_site_bkt_data *bkt;
215 list_del_init(&top->loh_lru);
216 bkt = cfs_hash_bd_extra_get(obj_hash, &bd);
218 percpu_counter_dec(&site->ls_lru_len_counter);
220 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
221 cfs_hash_bd_unlock(obj_hash, &bd, 1);
224 EXPORT_SYMBOL(lu_object_unhash);
227 * Allocate new object.
229 * This follows object creation protocol, described in the comment within
230 * struct lu_device_operations definition.
232 static struct lu_object *lu_object_alloc(const struct lu_env *env,
233 struct lu_device *dev,
234 const struct lu_fid *f,
235 const struct lu_object_conf *conf)
237 struct lu_object *scan;
238 struct lu_object *top;
239 struct list_head *layers;
240 unsigned int init_mask = 0;
241 unsigned int init_flag;
247 * Create top-level object slice. This will also create
250 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
252 RETURN(ERR_PTR(-ENOMEM));
256 * This is the only place where object fid is assigned. It's constant
259 top->lo_header->loh_fid = *f;
260 layers = &top->lo_header->loh_layers;
264 * Call ->loo_object_init() repeatedly, until no more new
265 * object slices are created.
269 list_for_each_entry(scan, layers, lo_linkage) {
270 if (init_mask & init_flag)
273 scan->lo_header = top->lo_header;
274 result = scan->lo_ops->loo_object_init(env, scan, conf);
276 lu_object_free(env, top);
277 RETURN(ERR_PTR(result));
279 init_mask |= init_flag;
285 list_for_each_entry_reverse(scan, layers, lo_linkage) {
286 if (scan->lo_ops->loo_object_start != NULL) {
287 result = scan->lo_ops->loo_object_start(env, scan);
289 lu_object_free(env, top);
290 RETURN(ERR_PTR(result));
295 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
302 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
304 struct lu_site_bkt_data *bkt;
305 struct lu_site *site;
306 struct lu_object *scan;
307 struct list_head *layers;
308 struct list_head splice;
310 site = o->lo_dev->ld_site;
311 layers = &o->lo_header->loh_layers;
312 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
314 * First call ->loo_object_delete() method to release all resources.
316 list_for_each_entry_reverse(scan, layers, lo_linkage) {
317 if (scan->lo_ops->loo_object_delete != NULL)
318 scan->lo_ops->loo_object_delete(env, scan);
322 * Then, splice object layers into stand-alone list, and call
323 * ->loo_object_free() on all layers to free memory. Splice is
324 * necessary, because lu_object_header is freed together with the
327 INIT_LIST_HEAD(&splice);
328 list_splice_init(layers, &splice);
329 while (!list_empty(&splice)) {
331 * Free layers in bottom-to-top order, so that object header
332 * lives as long as possible and ->loo_object_free() methods
333 * can look at its contents.
335 o = container_of0(splice.prev, struct lu_object, lo_linkage);
336 list_del_init(&o->lo_linkage);
337 LASSERT(o->lo_ops->loo_object_free != NULL);
338 o->lo_ops->loo_object_free(env, o);
341 if (waitqueue_active(&bkt->lsb_marche_funebre))
342 wake_up_all(&bkt->lsb_marche_funebre);
346 * Free \a nr objects from the cold end of the site LRU list.
347 * if canblock is 0, then don't block awaiting for another
348 * instance of lu_site_purge() to complete
350 int lu_site_purge_objects(const struct lu_env *env, struct lu_site *s,
351 int nr, int canblock)
353 struct lu_object_header *h;
354 struct lu_object_header *temp;
355 struct lu_site_bkt_data *bkt;
356 struct cfs_hash_bd bd;
357 struct cfs_hash_bd bd2;
358 struct list_head dispose;
360 unsigned int start = 0;
365 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
368 INIT_LIST_HEAD(&dispose);
370 * Under LRU list lock, scan LRU list and move unreferenced objects to
371 * the dispose list, removing them from LRU and hash table.
374 start = s->ls_purge_start;
375 bnr = (nr == ~0) ? -1 : nr / (int)CFS_HASH_NBKT(s->ls_obj_hash) + 1;
378 * It doesn't make any sense to make purge threads parallel, that can
379 * only bring troubles to us. See LU-5331.
382 mutex_lock(&s->ls_purge_mutex);
383 else if (mutex_trylock(&s->ls_purge_mutex) == 0)
387 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
391 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
392 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
394 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
395 LASSERT(atomic_read(&h->loh_ref) == 0);
397 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
398 LASSERT(bd.bd_bucket == bd2.bd_bucket);
400 cfs_hash_bd_del_locked(s->ls_obj_hash,
402 list_move(&h->loh_lru, &dispose);
404 percpu_counter_dec(&s->ls_lru_len_counter);
408 if (nr != ~0 && --nr == 0)
411 if (count > 0 && --count == 0)
415 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
418 * Free everything on the dispose list. This is safe against
419 * races due to the reasons described in lu_object_put().
421 while (!list_empty(&dispose)) {
422 h = container_of0(dispose.next,
423 struct lu_object_header, loh_lru);
424 list_del_init(&h->loh_lru);
425 lu_object_free(env, lu_object_top(h));
426 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
432 mutex_unlock(&s->ls_purge_mutex);
434 if (nr != 0 && did_sth && start != 0) {
435 start = 0; /* restart from the first bucket */
438 /* race on s->ls_purge_start, but nobody cares */
439 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
444 EXPORT_SYMBOL(lu_site_purge_objects);
449 * Code below has to jump through certain loops to output object description
450 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
451 * composes object description from strings that are parts of _lines_ of
452 * output (i.e., strings that are not terminated by newline). This doesn't fit
453 * very well into libcfs_debug_msg() interface that assumes that each message
454 * supplied to it is a self-contained output line.
456 * To work around this, strings are collected in a temporary buffer
457 * (implemented as a value of lu_cdebug_key key), until terminating newline
458 * character is detected.
466 * XXX overflow is not handled correctly.
471 struct lu_cdebug_data {
475 char lck_area[LU_CDEBUG_LINE];
478 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
479 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
482 * Key, holding temporary buffer. This key is registered very early by
485 static struct lu_context_key lu_global_key = {
486 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
487 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
488 .lct_init = lu_global_key_init,
489 .lct_fini = lu_global_key_fini
493 * Printer function emitting messages through libcfs_debug_msg().
495 int lu_cdebug_printer(const struct lu_env *env,
496 void *cookie, const char *format, ...)
498 struct libcfs_debug_msg_data *msgdata = cookie;
499 struct lu_cdebug_data *key;
504 va_start(args, format);
506 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
507 LASSERT(key != NULL);
509 used = strlen(key->lck_area);
510 complete = format[strlen(format) - 1] == '\n';
512 * Append new chunk to the buffer.
514 vsnprintf(key->lck_area + used,
515 ARRAY_SIZE(key->lck_area) - used, format, args);
517 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
518 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
519 key->lck_area[0] = 0;
524 EXPORT_SYMBOL(lu_cdebug_printer);
527 * Print object header.
529 void lu_object_header_print(const struct lu_env *env, void *cookie,
530 lu_printer_t printer,
531 const struct lu_object_header *hdr)
533 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
534 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
536 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
537 list_empty((struct list_head *)&hdr->loh_lru) ? \
539 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
541 EXPORT_SYMBOL(lu_object_header_print);
544 * Print human readable representation of the \a o to the \a printer.
546 void lu_object_print(const struct lu_env *env, void *cookie,
547 lu_printer_t printer, const struct lu_object *o)
549 static const char ruler[] = "........................................";
550 struct lu_object_header *top;
554 lu_object_header_print(env, cookie, printer, top);
555 (*printer)(env, cookie, "{\n");
557 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
559 * print `.' \a depth times followed by type name and address
561 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
562 o->lo_dev->ld_type->ldt_name, o);
564 if (o->lo_ops->loo_object_print != NULL)
565 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
567 (*printer)(env, cookie, "\n");
570 (*printer)(env, cookie, "} header@%p\n", top);
572 EXPORT_SYMBOL(lu_object_print);
575 * Check object consistency.
577 int lu_object_invariant(const struct lu_object *o)
579 struct lu_object_header *top;
582 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
583 if (o->lo_ops->loo_object_invariant != NULL &&
584 !o->lo_ops->loo_object_invariant(o))
590 static struct lu_object *htable_lookup(struct lu_site *s,
591 struct cfs_hash_bd *bd,
592 const struct lu_fid *f,
593 wait_queue_t *waiter,
596 struct lu_site_bkt_data *bkt;
597 struct lu_object_header *h;
598 struct hlist_node *hnode;
599 __u64 ver = cfs_hash_bd_version_get(bd);
602 return ERR_PTR(-ENOENT);
605 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
606 /* cfs_hash_bd_peek_locked is a somehow "internal" function
607 * of cfs_hash, it doesn't add refcount on object. */
608 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
610 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
611 return ERR_PTR(-ENOENT);
614 h = container_of0(hnode, struct lu_object_header, loh_hash);
615 if (likely(!lu_object_is_dying(h))) {
616 cfs_hash_get(s->ls_obj_hash, hnode);
617 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
618 if (!list_empty(&h->loh_lru)) {
619 list_del_init(&h->loh_lru);
621 percpu_counter_dec(&s->ls_lru_len_counter);
623 return lu_object_top(h);
627 * Lookup found an object being destroyed this object cannot be
628 * returned (to assure that references to dying objects are eventually
629 * drained), and moreover, lookup has to wait until object is freed.
632 if (likely(waiter != NULL)) {
633 init_waitqueue_entry(waiter, current);
634 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
635 set_current_state(TASK_UNINTERRUPTIBLE);
636 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
639 return ERR_PTR(-EAGAIN);
643 * Search cache for an object with the fid \a f. If such object is found,
644 * return it. Otherwise, create new object, insert it into cache and return
645 * it. In any case, additional reference is acquired on the returned object.
647 struct lu_object *lu_object_find(const struct lu_env *env,
648 struct lu_device *dev, const struct lu_fid *f,
649 const struct lu_object_conf *conf)
651 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
653 EXPORT_SYMBOL(lu_object_find);
656 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
657 * the calculation for the number of objects to reclaim is not covered by
658 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
659 * This ensures that many concurrent threads will not accidentally purge
662 static void lu_object_limit(const struct lu_env *env,
663 struct lu_device *dev)
667 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
670 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
671 nr = (__u64)lu_cache_nr;
675 lu_site_purge_objects(env, dev->ld_site,
676 MIN(size - nr, LU_CACHE_NR_MAX_ADJUST), 0);
679 static struct lu_object *lu_object_new(const struct lu_env *env,
680 struct lu_device *dev,
681 const struct lu_fid *f,
682 const struct lu_object_conf *conf)
686 struct cfs_hash_bd bd;
688 o = lu_object_alloc(env, dev, f, conf);
689 if (unlikely(IS_ERR(o)))
692 hs = dev->ld_site->ls_obj_hash;
693 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
694 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
695 cfs_hash_bd_unlock(hs, &bd, 1);
697 lu_object_limit(env, dev);
703 * Core logic of lu_object_find*() functions.
705 static struct lu_object *lu_object_find_try(const struct lu_env *env,
706 struct lu_device *dev,
707 const struct lu_fid *f,
708 const struct lu_object_conf *conf,
709 wait_queue_t *waiter)
712 struct lu_object *shadow;
715 struct cfs_hash_bd bd;
719 * This uses standard index maintenance protocol:
721 * - search index under lock, and return object if found;
722 * - otherwise, unlock index, allocate new object;
723 * - lock index and search again;
724 * - if nothing is found (usual case), insert newly created
726 * - otherwise (race: other thread inserted object), free
727 * object just allocated.
731 * For "LOC_F_NEW" case, we are sure the object is new established.
732 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
733 * just alloc and insert directly.
735 * If dying object is found during index search, add @waiter to the
736 * site wait-queue and return ERR_PTR(-EAGAIN).
738 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
739 return lu_object_new(env, dev, f, conf);
743 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
744 o = htable_lookup(s, &bd, f, waiter, &version);
745 cfs_hash_bd_unlock(hs, &bd, 1);
746 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
750 * Allocate new object. This may result in rather complicated
751 * operations, including fld queries, inode loading, etc.
753 o = lu_object_alloc(env, dev, f, conf);
754 if (unlikely(IS_ERR(o)))
757 LASSERT(lu_fid_eq(lu_object_fid(o), f));
759 cfs_hash_bd_lock(hs, &bd, 1);
761 shadow = htable_lookup(s, &bd, f, waiter, &version);
762 if (likely(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT)) {
763 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
764 cfs_hash_bd_unlock(hs, &bd, 1);
766 lu_object_limit(env, dev);
771 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
772 cfs_hash_bd_unlock(hs, &bd, 1);
773 lu_object_free(env, o);
778 * Much like lu_object_find(), but top level device of object is specifically
779 * \a dev rather than top level device of the site. This interface allows
780 * objects of different "stacking" to be created within the same site.
782 struct lu_object *lu_object_find_at(const struct lu_env *env,
783 struct lu_device *dev,
784 const struct lu_fid *f,
785 const struct lu_object_conf *conf)
787 struct lu_site_bkt_data *bkt;
788 struct lu_object *obj;
791 if (conf != NULL && conf->loc_flags & LOC_F_NOWAIT)
792 return lu_object_find_try(env, dev, f, conf, NULL);
795 obj = lu_object_find_try(env, dev, f, conf, &wait);
796 if (obj != ERR_PTR(-EAGAIN))
799 * lu_object_find_try() already added waiter into the
803 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
804 remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
807 EXPORT_SYMBOL(lu_object_find_at);
810 * Find object with given fid, and return its slice belonging to given device.
812 struct lu_object *lu_object_find_slice(const struct lu_env *env,
813 struct lu_device *dev,
814 const struct lu_fid *f,
815 const struct lu_object_conf *conf)
817 struct lu_object *top;
818 struct lu_object *obj;
820 top = lu_object_find(env, dev, f, conf);
824 obj = lu_object_locate(top->lo_header, dev->ld_type);
825 if (unlikely(obj == NULL)) {
826 lu_object_put(env, top);
827 obj = ERR_PTR(-ENOENT);
832 EXPORT_SYMBOL(lu_object_find_slice);
834 int lu_device_type_init(struct lu_device_type *ldt)
838 atomic_set(&ldt->ldt_device_nr, 0);
839 if (ldt->ldt_ops->ldto_init)
840 result = ldt->ldt_ops->ldto_init(ldt);
844 EXPORT_SYMBOL(lu_device_type_init);
846 void lu_device_type_fini(struct lu_device_type *ldt)
848 if (ldt->ldt_ops->ldto_fini)
849 ldt->ldt_ops->ldto_fini(ldt);
851 EXPORT_SYMBOL(lu_device_type_fini);
854 * Global list of all sites on this node
856 static LIST_HEAD(lu_sites);
857 static DECLARE_RWSEM(lu_sites_guard);
860 * Global environment used by site shrinker.
862 static struct lu_env lu_shrink_env;
864 struct lu_site_print_arg {
865 struct lu_env *lsp_env;
867 lu_printer_t lsp_printer;
871 lu_site_obj_print(struct cfs_hash *hs, struct cfs_hash_bd *bd,
872 struct hlist_node *hnode, void *data)
874 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
875 struct lu_object_header *h;
877 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
878 if (!list_empty(&h->loh_layers)) {
879 const struct lu_object *o;
881 o = lu_object_top(h);
882 lu_object_print(arg->lsp_env, arg->lsp_cookie,
883 arg->lsp_printer, o);
885 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
886 arg->lsp_printer, h);
892 * Print all objects in \a s.
894 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
895 lu_printer_t printer)
897 struct lu_site_print_arg arg = {
898 .lsp_env = (struct lu_env *)env,
899 .lsp_cookie = cookie,
900 .lsp_printer = printer,
903 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
905 EXPORT_SYMBOL(lu_site_print);
908 * Return desired hash table order.
910 static unsigned long lu_htable_order(struct lu_device *top)
912 unsigned long cache_size;
914 unsigned long bits_max = LU_SITE_BITS_MAX;
917 * For ZFS based OSDs the cache should be disabled by default. This
918 * allows the ZFS ARC maximum flexibility in determining what buffers
919 * to cache. If Lustre has objects or buffer which it wants to ensure
920 * always stay cached it must maintain a hold on them.
922 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
923 lu_cache_percent = 1;
924 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
925 return LU_SITE_BITS_MIN;
928 if (strcmp(top->ld_type->ldt_name, LUSTRE_VVP_NAME) == 0)
929 bits_max = LU_SITE_BITS_MAX_CL;
932 * Calculate hash table size, assuming that we want reasonable
933 * performance when 20% of total memory is occupied by cache of
936 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
938 cache_size = totalram_pages;
940 #if BITS_PER_LONG == 32
941 /* limit hashtable size for lowmem systems to low RAM */
942 if (cache_size > 1 << (30 - PAGE_SHIFT))
943 cache_size = 1 << (30 - PAGE_SHIFT) * 3 / 4;
946 /* clear off unreasonable cache setting. */
947 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
948 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
949 " the range of (0, %u]. Will use default value: %u.\n",
950 lu_cache_percent, LU_CACHE_PERCENT_MAX,
951 LU_CACHE_PERCENT_DEFAULT);
953 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
955 cache_size = cache_size / 100 * lu_cache_percent *
958 for (bits = 1; (1 << bits) < cache_size; ++bits) {
962 return clamp_t(typeof(bits), bits, LU_SITE_BITS_MIN, bits_max);
965 static unsigned lu_obj_hop_hash(struct cfs_hash *hs,
966 const void *key, unsigned mask)
968 struct lu_fid *fid = (struct lu_fid *)key;
971 hash = fid_flatten32(fid);
972 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
973 hash = hash_long(hash, hs->hs_bkt_bits);
975 /* give me another random factor */
976 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
978 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
979 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
984 static void *lu_obj_hop_object(struct hlist_node *hnode)
986 return hlist_entry(hnode, struct lu_object_header, loh_hash);
989 static void *lu_obj_hop_key(struct hlist_node *hnode)
991 struct lu_object_header *h;
993 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
997 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
999 struct lu_object_header *h;
1001 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1002 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
1005 static void lu_obj_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1007 struct lu_object_header *h;
1009 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1010 atomic_inc(&h->loh_ref);
1013 static void lu_obj_hop_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
1015 LBUG(); /* we should never called it */
1018 static struct cfs_hash_ops 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;
1050 struct cfs_hash_bd bd;
1057 memset(s, 0, sizeof *s);
1058 mutex_init(&s->ls_purge_mutex);
1060 #ifdef HAVE_PERCPU_COUNTER_INIT_GFP_FLAG
1061 rc = percpu_counter_init(&s->ls_lru_len_counter, 0, GFP_NOFS);
1063 rc = percpu_counter_init(&s->ls_lru_len_counter, 0);
1068 snprintf(name, sizeof(name), "lu_site_%s", top->ld_type->ldt_name);
1069 for (bits = lu_htable_order(top);
1070 bits >= LU_SITE_BITS_MIN; bits--) {
1071 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1072 bits - LU_SITE_BKT_BITS,
1075 CFS_HASH_SPIN_BKTLOCK |
1076 CFS_HASH_NO_ITEMREF |
1078 CFS_HASH_ASSERT_EMPTY |
1080 if (s->ls_obj_hash != NULL)
1084 if (s->ls_obj_hash == NULL) {
1085 CERROR("failed to create lu_site hash with bits: %lu\n", bits);
1089 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1090 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1091 INIT_LIST_HEAD(&bkt->lsb_lru);
1092 init_waitqueue_head(&bkt->lsb_marche_funebre);
1095 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1096 if (s->ls_stats == NULL) {
1097 cfs_hash_putref(s->ls_obj_hash);
1098 s->ls_obj_hash = NULL;
1102 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1103 0, "created", "created");
1104 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1105 0, "cache_hit", "cache_hit");
1106 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1107 0, "cache_miss", "cache_miss");
1108 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1109 0, "cache_race", "cache_race");
1110 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1111 0, "cache_death_race", "cache_death_race");
1112 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1113 0, "lru_purged", "lru_purged");
1115 INIT_LIST_HEAD(&s->ls_linkage);
1116 s->ls_top_dev = top;
1119 lu_ref_add(&top->ld_reference, "site-top", s);
1121 INIT_LIST_HEAD(&s->ls_ld_linkage);
1122 spin_lock_init(&s->ls_ld_lock);
1124 lu_dev_add_linkage(s, top);
1128 EXPORT_SYMBOL(lu_site_init);
1131 * Finalize \a s and release its resources.
1133 void lu_site_fini(struct lu_site *s)
1135 down_write(&lu_sites_guard);
1136 list_del_init(&s->ls_linkage);
1137 up_write(&lu_sites_guard);
1139 percpu_counter_destroy(&s->ls_lru_len_counter);
1141 if (s->ls_obj_hash != NULL) {
1142 cfs_hash_putref(s->ls_obj_hash);
1143 s->ls_obj_hash = NULL;
1146 if (s->ls_top_dev != NULL) {
1147 s->ls_top_dev->ld_site = NULL;
1148 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1149 lu_device_put(s->ls_top_dev);
1150 s->ls_top_dev = NULL;
1153 if (s->ls_stats != NULL)
1154 lprocfs_free_stats(&s->ls_stats);
1156 EXPORT_SYMBOL(lu_site_fini);
1159 * Called when initialization of stack for this site is completed.
1161 int lu_site_init_finish(struct lu_site *s)
1164 down_write(&lu_sites_guard);
1165 result = lu_context_refill(&lu_shrink_env.le_ctx);
1167 list_add(&s->ls_linkage, &lu_sites);
1168 up_write(&lu_sites_guard);
1171 EXPORT_SYMBOL(lu_site_init_finish);
1174 * Acquire additional reference on device \a d
1176 void lu_device_get(struct lu_device *d)
1178 atomic_inc(&d->ld_ref);
1180 EXPORT_SYMBOL(lu_device_get);
1183 * Release reference on device \a d.
1185 void lu_device_put(struct lu_device *d)
1187 LASSERT(atomic_read(&d->ld_ref) > 0);
1188 atomic_dec(&d->ld_ref);
1190 EXPORT_SYMBOL(lu_device_put);
1193 * Initialize device \a d of type \a t.
1195 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1197 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1198 t->ldt_ops->ldto_start != NULL)
1199 t->ldt_ops->ldto_start(t);
1201 memset(d, 0, sizeof *d);
1203 lu_ref_init(&d->ld_reference);
1204 INIT_LIST_HEAD(&d->ld_linkage);
1208 EXPORT_SYMBOL(lu_device_init);
1211 * Finalize device \a d.
1213 void lu_device_fini(struct lu_device *d)
1215 struct lu_device_type *t = d->ld_type;
1217 if (d->ld_obd != NULL) {
1218 d->ld_obd->obd_lu_dev = NULL;
1222 lu_ref_fini(&d->ld_reference);
1223 LASSERTF(atomic_read(&d->ld_ref) == 0,
1224 "Refcount is %u\n", atomic_read(&d->ld_ref));
1225 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1227 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1228 t->ldt_ops->ldto_stop != NULL)
1229 t->ldt_ops->ldto_stop(t);
1231 EXPORT_SYMBOL(lu_device_fini);
1234 * Initialize object \a o that is part of compound object \a h and was created
1237 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1238 struct lu_device *d)
1240 memset(o, 0, sizeof(*o));
1244 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1245 INIT_LIST_HEAD(&o->lo_linkage);
1249 EXPORT_SYMBOL(lu_object_init);
1252 * Finalize object and release its resources.
1254 void lu_object_fini(struct lu_object *o)
1256 struct lu_device *dev = o->lo_dev;
1258 LASSERT(list_empty(&o->lo_linkage));
1261 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1267 EXPORT_SYMBOL(lu_object_fini);
1270 * Add object \a o as first layer of compound object \a h
1272 * This is typically called by the ->ldo_object_alloc() method of top-level
1275 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1277 list_move(&o->lo_linkage, &h->loh_layers);
1279 EXPORT_SYMBOL(lu_object_add_top);
1282 * Add object \a o as a layer of compound object, going after \a before.
1284 * This is typically called by the ->ldo_object_alloc() method of \a
1287 void lu_object_add(struct lu_object *before, struct lu_object *o)
1289 list_move(&o->lo_linkage, &before->lo_linkage);
1291 EXPORT_SYMBOL(lu_object_add);
1294 * Initialize compound object.
1296 int lu_object_header_init(struct lu_object_header *h)
1298 memset(h, 0, sizeof *h);
1299 atomic_set(&h->loh_ref, 1);
1300 INIT_HLIST_NODE(&h->loh_hash);
1301 INIT_LIST_HEAD(&h->loh_lru);
1302 INIT_LIST_HEAD(&h->loh_layers);
1303 lu_ref_init(&h->loh_reference);
1306 EXPORT_SYMBOL(lu_object_header_init);
1309 * Finalize compound object.
1311 void lu_object_header_fini(struct lu_object_header *h)
1313 LASSERT(list_empty(&h->loh_layers));
1314 LASSERT(list_empty(&h->loh_lru));
1315 LASSERT(hlist_unhashed(&h->loh_hash));
1316 lu_ref_fini(&h->loh_reference);
1318 EXPORT_SYMBOL(lu_object_header_fini);
1321 * Given a compound object, find its slice, corresponding to the device type
1324 struct lu_object *lu_object_locate(struct lu_object_header *h,
1325 const struct lu_device_type *dtype)
1327 struct lu_object *o;
1329 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1330 if (o->lo_dev->ld_type == dtype)
1335 EXPORT_SYMBOL(lu_object_locate);
1338 * Finalize and free devices in the device stack.
1340 * Finalize device stack by purging object cache, and calling
1341 * lu_device_type_operations::ldto_device_fini() and
1342 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1344 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1346 struct lu_site *site = top->ld_site;
1347 struct lu_device *scan;
1348 struct lu_device *next;
1350 lu_site_purge(env, site, ~0);
1351 for (scan = top; scan != NULL; scan = next) {
1352 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1353 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1354 lu_device_put(scan);
1358 lu_site_purge(env, site, ~0);
1360 for (scan = top; scan != NULL; scan = next) {
1361 const struct lu_device_type *ldt = scan->ld_type;
1362 struct obd_type *type;
1364 next = ldt->ldt_ops->ldto_device_free(env, scan);
1365 type = ldt->ldt_obd_type;
1368 class_put_type(type);
1375 * Maximal number of tld slots.
1377 LU_CONTEXT_KEY_NR = 40
1380 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1382 DEFINE_RWLOCK(lu_keys_guard);
1383 static atomic_t lu_key_initing_cnt = ATOMIC_INIT(0);
1386 * Global counter incremented whenever key is registered, unregistered,
1387 * revived or quiesced. This is used to void unnecessary calls to
1388 * lu_context_refill(). No locking is provided, as initialization and shutdown
1389 * are supposed to be externally serialized.
1391 static unsigned key_set_version = 0;
1396 int lu_context_key_register(struct lu_context_key *key)
1401 LASSERT(key->lct_init != NULL);
1402 LASSERT(key->lct_fini != NULL);
1403 LASSERT(key->lct_tags != 0);
1404 LASSERT(key->lct_owner != NULL);
1407 write_lock(&lu_keys_guard);
1408 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1409 if (lu_keys[i] == NULL) {
1411 atomic_set(&key->lct_used, 1);
1413 lu_ref_init(&key->lct_reference);
1419 write_unlock(&lu_keys_guard);
1422 EXPORT_SYMBOL(lu_context_key_register);
1424 static void key_fini(struct lu_context *ctx, int index)
1426 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1427 struct lu_context_key *key;
1429 key = lu_keys[index];
1430 LASSERT(key != NULL);
1431 LASSERT(key->lct_fini != NULL);
1432 LASSERT(atomic_read(&key->lct_used) > 1);
1434 key->lct_fini(ctx, key, ctx->lc_value[index]);
1435 lu_ref_del(&key->lct_reference, "ctx", ctx);
1436 atomic_dec(&key->lct_used);
1438 LASSERT(key->lct_owner != NULL);
1439 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1440 LINVRNT(module_refcount(key->lct_owner) > 0);
1441 module_put(key->lct_owner);
1443 ctx->lc_value[index] = NULL;
1450 void lu_context_key_degister(struct lu_context_key *key)
1452 LASSERT(atomic_read(&key->lct_used) >= 1);
1453 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1455 lu_context_key_quiesce(key);
1458 write_lock(&lu_keys_guard);
1459 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1462 * Wait until all transient contexts referencing this key have
1463 * run lu_context_key::lct_fini() method.
1465 while (atomic_read(&key->lct_used) > 1) {
1466 write_unlock(&lu_keys_guard);
1467 CDEBUG(D_INFO, "lu_context_key_degister: \"%s\" %p, %d\n",
1468 key->lct_owner ? key->lct_owner->name : "", key,
1469 atomic_read(&key->lct_used));
1471 write_lock(&lu_keys_guard);
1473 if (lu_keys[key->lct_index]) {
1474 lu_keys[key->lct_index] = NULL;
1475 lu_ref_fini(&key->lct_reference);
1477 write_unlock(&lu_keys_guard);
1479 LASSERTF(atomic_read(&key->lct_used) == 1,
1480 "key has instances: %d\n",
1481 atomic_read(&key->lct_used));
1483 EXPORT_SYMBOL(lu_context_key_degister);
1486 * Register a number of keys. This has to be called after all keys have been
1487 * initialized by a call to LU_CONTEXT_KEY_INIT().
1489 int lu_context_key_register_many(struct lu_context_key *k, ...)
1491 struct lu_context_key *key = k;
1497 result = lu_context_key_register(key);
1500 key = va_arg(args, struct lu_context_key *);
1501 } while (key != NULL);
1507 lu_context_key_degister(k);
1508 k = va_arg(args, struct lu_context_key *);
1515 EXPORT_SYMBOL(lu_context_key_register_many);
1518 * De-register a number of keys. This is a dual to
1519 * lu_context_key_register_many().
1521 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1527 lu_context_key_degister(k);
1528 k = va_arg(args, struct lu_context_key*);
1529 } while (k != NULL);
1532 EXPORT_SYMBOL(lu_context_key_degister_many);
1535 * Revive a number of keys.
1537 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1543 lu_context_key_revive(k);
1544 k = va_arg(args, struct lu_context_key*);
1545 } while (k != NULL);
1548 EXPORT_SYMBOL(lu_context_key_revive_many);
1551 * Quiescent a number of keys.
1553 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1559 lu_context_key_quiesce(k);
1560 k = va_arg(args, struct lu_context_key*);
1561 } while (k != NULL);
1564 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1567 * Return value associated with key \a key in context \a ctx.
1569 void *lu_context_key_get(const struct lu_context *ctx,
1570 const struct lu_context_key *key)
1572 LINVRNT(ctx->lc_state == LCS_ENTERED);
1573 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1574 LASSERT(lu_keys[key->lct_index] == key);
1575 return ctx->lc_value[key->lct_index];
1577 EXPORT_SYMBOL(lu_context_key_get);
1580 * List of remembered contexts. XXX document me.
1582 static LIST_HEAD(lu_context_remembered);
1585 * Destroy \a key in all remembered contexts. This is used to destroy key
1586 * values in "shared" contexts (like service threads), when a module owning
1587 * the key is about to be unloaded.
1589 void lu_context_key_quiesce(struct lu_context_key *key)
1591 struct lu_context *ctx;
1593 if (!(key->lct_tags & LCT_QUIESCENT)) {
1595 * XXX memory barrier has to go here.
1597 write_lock(&lu_keys_guard);
1598 key->lct_tags |= LCT_QUIESCENT;
1601 * Wait until all lu_context_key::lct_init() methods
1604 while (atomic_read(&lu_key_initing_cnt) > 0) {
1605 write_unlock(&lu_keys_guard);
1606 CDEBUG(D_INFO, "lu_context_key_quiesce: \"%s\""
1608 key->lct_owner ? key->lct_owner->name : "",
1609 key, atomic_read(&key->lct_used),
1610 atomic_read(&lu_key_initing_cnt));
1612 write_lock(&lu_keys_guard);
1615 list_for_each_entry(ctx, &lu_context_remembered,
1617 key_fini(ctx, key->lct_index);
1618 write_unlock(&lu_keys_guard);
1623 void lu_context_key_revive(struct lu_context_key *key)
1625 key->lct_tags &= ~LCT_QUIESCENT;
1629 static void keys_fini(struct lu_context *ctx)
1633 if (ctx->lc_value == NULL)
1636 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1639 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1640 ctx->lc_value = NULL;
1643 static int keys_fill(struct lu_context *ctx)
1648 * A serialisation with lu_context_key_quiesce() is needed, but some
1649 * "key->lct_init()" are calling kernel memory allocation routine and
1650 * can't be called while holding a spin_lock.
1651 * "lu_keys_guard" is held while incrementing "lu_key_initing_cnt"
1652 * to ensure the start of the serialisation.
1653 * An atomic_t variable is still used, in order not to reacquire the
1654 * lock when decrementing the counter.
1656 read_lock(&lu_keys_guard);
1657 atomic_inc(&lu_key_initing_cnt);
1658 read_unlock(&lu_keys_guard);
1660 LINVRNT(ctx->lc_value != NULL);
1661 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1662 struct lu_context_key *key;
1665 if (ctx->lc_value[i] == NULL && key != NULL &&
1666 (key->lct_tags & ctx->lc_tags) &&
1668 * Don't create values for a LCT_QUIESCENT key, as this
1669 * will pin module owning a key.
1671 !(key->lct_tags & LCT_QUIESCENT)) {
1674 LINVRNT(key->lct_init != NULL);
1675 LINVRNT(key->lct_index == i);
1677 LASSERT(key->lct_owner != NULL);
1678 if (!(ctx->lc_tags & LCT_NOREF) &&
1679 try_module_get(key->lct_owner) == 0) {
1680 /* module is unloading, skip this key */
1684 value = key->lct_init(ctx, key);
1685 if (unlikely(IS_ERR(value))) {
1686 atomic_dec(&lu_key_initing_cnt);
1687 return PTR_ERR(value);
1690 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1691 atomic_inc(&key->lct_used);
1693 * This is the only place in the code, where an
1694 * element of ctx->lc_value[] array is set to non-NULL
1697 ctx->lc_value[i] = value;
1698 if (key->lct_exit != NULL)
1699 ctx->lc_tags |= LCT_HAS_EXIT;
1701 ctx->lc_version = key_set_version;
1703 atomic_dec(&lu_key_initing_cnt);
1707 static int keys_init(struct lu_context *ctx)
1709 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1710 if (likely(ctx->lc_value != NULL))
1711 return keys_fill(ctx);
1717 * Initialize context data-structure. Create values for all keys.
1719 int lu_context_init(struct lu_context *ctx, __u32 tags)
1723 memset(ctx, 0, sizeof *ctx);
1724 ctx->lc_state = LCS_INITIALIZED;
1725 ctx->lc_tags = tags;
1726 if (tags & LCT_REMEMBER) {
1727 write_lock(&lu_keys_guard);
1728 list_add(&ctx->lc_remember, &lu_context_remembered);
1729 write_unlock(&lu_keys_guard);
1731 INIT_LIST_HEAD(&ctx->lc_remember);
1734 rc = keys_init(ctx);
1736 lu_context_fini(ctx);
1740 EXPORT_SYMBOL(lu_context_init);
1743 * Finalize context data-structure. Destroy key values.
1745 void lu_context_fini(struct lu_context *ctx)
1747 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1748 ctx->lc_state = LCS_FINALIZED;
1750 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1751 LASSERT(list_empty(&ctx->lc_remember));
1754 } else { /* could race with key degister */
1755 write_lock(&lu_keys_guard);
1757 list_del_init(&ctx->lc_remember);
1758 write_unlock(&lu_keys_guard);
1761 EXPORT_SYMBOL(lu_context_fini);
1764 * Called before entering context.
1766 void lu_context_enter(struct lu_context *ctx)
1768 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1769 ctx->lc_state = LCS_ENTERED;
1771 EXPORT_SYMBOL(lu_context_enter);
1774 * Called after exiting from \a ctx
1776 void lu_context_exit(struct lu_context *ctx)
1780 LINVRNT(ctx->lc_state == LCS_ENTERED);
1781 ctx->lc_state = LCS_LEFT;
1782 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1783 /* could race with key quiescency */
1784 if (ctx->lc_tags & LCT_REMEMBER)
1785 read_lock(&lu_keys_guard);
1787 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1788 if (ctx->lc_value[i] != NULL) {
1789 struct lu_context_key *key;
1792 LASSERT(key != NULL);
1793 if (key->lct_exit != NULL)
1795 key, ctx->lc_value[i]);
1799 if (ctx->lc_tags & LCT_REMEMBER)
1800 read_unlock(&lu_keys_guard);
1803 EXPORT_SYMBOL(lu_context_exit);
1806 * Allocate for context all missing keys that were registered after context
1807 * creation. key_set_version is only changed in rare cases when modules
1808 * are loaded and removed.
1810 int lu_context_refill(struct lu_context *ctx)
1812 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1816 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1817 * obd being added. Currently, this is only used on client side, specifically
1818 * for echo device client, for other stack (like ptlrpc threads), context are
1819 * predefined when the lu_device type are registered, during the module probe
1822 __u32 lu_context_tags_default = 0;
1823 __u32 lu_session_tags_default = 0;
1825 void lu_context_tags_update(__u32 tags)
1827 write_lock(&lu_keys_guard);
1828 lu_context_tags_default |= tags;
1830 write_unlock(&lu_keys_guard);
1832 EXPORT_SYMBOL(lu_context_tags_update);
1834 void lu_context_tags_clear(__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_clear);
1843 void lu_session_tags_update(__u32 tags)
1845 write_lock(&lu_keys_guard);
1846 lu_session_tags_default |= tags;
1848 write_unlock(&lu_keys_guard);
1850 EXPORT_SYMBOL(lu_session_tags_update);
1852 void lu_session_tags_clear(__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_clear);
1861 int lu_env_init(struct lu_env *env, __u32 tags)
1866 result = lu_context_init(&env->le_ctx, tags);
1867 if (likely(result == 0))
1868 lu_context_enter(&env->le_ctx);
1871 EXPORT_SYMBOL(lu_env_init);
1873 void lu_env_fini(struct lu_env *env)
1875 lu_context_exit(&env->le_ctx);
1876 lu_context_fini(&env->le_ctx);
1879 EXPORT_SYMBOL(lu_env_fini);
1881 int lu_env_refill(struct lu_env *env)
1885 result = lu_context_refill(&env->le_ctx);
1886 if (result == 0 && env->le_ses != NULL)
1887 result = lu_context_refill(env->le_ses);
1890 EXPORT_SYMBOL(lu_env_refill);
1893 * Currently, this API will only be used by echo client.
1894 * Because echo client and normal lustre client will share
1895 * same cl_env cache. So echo client needs to refresh
1896 * the env context after it get one from the cache, especially
1897 * when normal client and echo client co-exist in the same client.
1899 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1904 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1905 env->le_ctx.lc_version = 0;
1906 env->le_ctx.lc_tags |= ctags;
1909 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1910 env->le_ses->lc_version = 0;
1911 env->le_ses->lc_tags |= stags;
1914 result = lu_env_refill(env);
1918 EXPORT_SYMBOL(lu_env_refill_by_tags);
1920 static struct shrinker *lu_site_shrinker;
1922 typedef struct lu_site_stats{
1923 unsigned lss_populated;
1924 unsigned lss_max_search;
1929 static void lu_site_stats_get(struct cfs_hash *hs,
1930 lu_site_stats_t *stats, int populated)
1932 struct cfs_hash_bd bd;
1935 cfs_hash_for_each_bucket(hs, &bd, i) {
1936 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1937 struct hlist_head *hhead;
1939 cfs_hash_bd_lock(hs, &bd, 1);
1941 cfs_hash_bd_count_get(&bd) - bkt->lsb_lru_len;
1942 stats->lss_total += cfs_hash_bd_count_get(&bd);
1943 stats->lss_max_search = max((int)stats->lss_max_search,
1944 cfs_hash_bd_depmax_get(&bd));
1946 cfs_hash_bd_unlock(hs, &bd, 1);
1950 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1951 if (!hlist_empty(hhead))
1952 stats->lss_populated++;
1954 cfs_hash_bd_unlock(hs, &bd, 1);
1960 * lu_cache_shrink_count() returns an approximate number of cached objects
1961 * that can be freed by shrink_slab(). A counter, which tracks the
1962 * number of items in the site's lru, is maintained in a percpu_counter
1963 * for each site. The percpu values are incremented and decremented as
1964 * objects are added or removed from the lru. The percpu values are summed
1965 * and saved whenever a percpu value exceeds a threshold. Thus the saved,
1966 * summed value at any given time may not accurately reflect the current
1967 * lru length. But this value is sufficiently accurate for the needs of
1970 * Using a per cpu counter is a compromise solution to concurrent access:
1971 * lu_object_put() can update the counter without locking the site and
1972 * lu_cache_shrink_count can sum the counters without locking each
1973 * ls_obj_hash bucket.
1975 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
1976 struct shrink_control *sc)
1979 struct lu_site *tmp;
1980 unsigned long cached = 0;
1982 if (!(sc->gfp_mask & __GFP_FS))
1985 down_read(&lu_sites_guard);
1986 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage)
1987 cached += percpu_counter_read_positive(&s->ls_lru_len_counter);
1988 up_read(&lu_sites_guard);
1990 cached = (cached / 100) * sysctl_vfs_cache_pressure;
1991 CDEBUG(D_INODE, "%ld objects cached, cache pressure %d\n",
1992 cached, sysctl_vfs_cache_pressure);
1997 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
1998 struct shrink_control *sc)
2001 struct lu_site *tmp;
2002 unsigned long remain = sc->nr_to_scan;
2005 if (!(sc->gfp_mask & __GFP_FS))
2006 /* We must not take the lu_sites_guard lock when
2007 * __GFP_FS is *not* set because of the deadlock
2008 * possibility detailed above. Additionally,
2009 * since we cannot determine the number of
2010 * objects in the cache without taking this
2011 * lock, we're in a particularly tough spot. As
2012 * a result, we'll just lie and say our cache is
2013 * empty. This _should_ be ok, as we can't
2014 * reclaim objects when __GFP_FS is *not* set
2019 down_write(&lu_sites_guard);
2020 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
2021 remain = lu_site_purge(&lu_shrink_env, s, remain);
2023 * Move just shrunk site to the tail of site list to
2024 * assure shrinking fairness.
2026 list_move_tail(&s->ls_linkage, &splice);
2028 list_splice(&splice, lu_sites.prev);
2029 up_write(&lu_sites_guard);
2031 return sc->nr_to_scan - remain;
2034 #ifndef HAVE_SHRINKER_COUNT
2036 * There exists a potential lock inversion deadlock scenario when using
2037 * Lustre on top of ZFS. This occurs between one of ZFS's
2038 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
2039 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
2040 * while thread B will take the ht_lock and sleep on the lu_sites_guard
2041 * lock. Obviously neither thread will wake and drop their respective hold
2044 * To prevent this from happening we must ensure the lu_sites_guard lock is
2045 * not taken while down this code path. ZFS reliably does not set the
2046 * __GFP_FS bit in its code paths, so this can be used to determine if it
2047 * is safe to take the lu_sites_guard lock.
2049 * Ideally we should accurately return the remaining number of cached
2050 * objects without taking the lu_sites_guard lock, but this is not
2051 * possible in the current implementation.
2053 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
2056 struct shrink_control scv = {
2057 .nr_to_scan = shrink_param(sc, nr_to_scan),
2058 .gfp_mask = shrink_param(sc, gfp_mask)
2060 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
2061 struct shrinker* shrinker = NULL;
2065 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
2067 if (scv.nr_to_scan != 0)
2068 lu_cache_shrink_scan(shrinker, &scv);
2070 cached = lu_cache_shrink_count(shrinker, &scv);
2074 #endif /* HAVE_SHRINKER_COUNT */
2082 * Environment to be used in debugger, contains all tags.
2084 static struct lu_env lu_debugging_env;
2087 * Debugging printer function using printk().
2089 int lu_printk_printer(const struct lu_env *env,
2090 void *unused, const char *format, ...)
2094 va_start(args, format);
2095 vprintk(format, args);
2100 int lu_debugging_setup(void)
2102 return lu_env_init(&lu_debugging_env, ~0);
2105 void lu_context_keys_dump(void)
2109 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2110 struct lu_context_key *key;
2114 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2115 i, key, key->lct_tags,
2116 key->lct_init, key->lct_fini, key->lct_exit,
2117 key->lct_index, atomic_read(&key->lct_used),
2118 key->lct_owner ? key->lct_owner->name : "",
2120 lu_ref_print(&key->lct_reference);
2126 * Initialization of global lu_* data.
2128 int lu_global_init(void)
2131 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2132 lu_cache_shrink_count, lu_cache_shrink_scan);
2134 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2136 result = lu_ref_global_init();
2140 LU_CONTEXT_KEY_INIT(&lu_global_key);
2141 result = lu_context_key_register(&lu_global_key);
2146 * At this level, we don't know what tags are needed, so allocate them
2147 * conservatively. This should not be too bad, because this
2148 * environment is global.
2150 down_write(&lu_sites_guard);
2151 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2152 up_write(&lu_sites_guard);
2157 * seeks estimation: 3 seeks to read a record from oi, one to read
2158 * inode, one for ea. Unfortunately setting this high value results in
2159 * lu_object/inode cache consuming all the memory.
2161 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2162 if (lu_site_shrinker == NULL)
2169 * Dual to lu_global_init().
2171 void lu_global_fini(void)
2173 if (lu_site_shrinker != NULL) {
2174 remove_shrinker(lu_site_shrinker);
2175 lu_site_shrinker = NULL;
2178 lu_context_key_degister(&lu_global_key);
2181 * Tear shrinker environment down _after_ de-registering
2182 * lu_global_key, because the latter has a value in the former.
2184 down_write(&lu_sites_guard);
2185 lu_env_fini(&lu_shrink_env);
2186 up_write(&lu_sites_guard);
2188 lu_ref_global_fini();
2191 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2193 #ifdef CONFIG_PROC_FS
2194 struct lprocfs_counter ret;
2196 lprocfs_stats_collect(stats, idx, &ret);
2197 return (__u32)ret.lc_count;
2204 * Output site statistical counters into a buffer. Suitable for
2205 * lprocfs_rd_*()-style functions.
2207 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2209 lu_site_stats_t stats;
2211 memset(&stats, 0, sizeof(stats));
2212 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2214 seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2217 stats.lss_populated,
2218 CFS_HASH_NHLIST(s->ls_obj_hash),
2219 stats.lss_max_search,
2220 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2221 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2222 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2223 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2224 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2225 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2228 EXPORT_SYMBOL(lu_site_stats_seq_print);
2231 * Helper function to initialize a number of kmem slab caches at once.
2233 int lu_kmem_init(struct lu_kmem_descr *caches)
2236 struct lu_kmem_descr *iter = caches;
2238 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2239 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2242 if (*iter->ckd_cache == NULL) {
2244 /* free all previously allocated caches */
2245 lu_kmem_fini(caches);
2251 EXPORT_SYMBOL(lu_kmem_init);
2254 * Helper function to finalize a number of kmem slab cached at once. Dual to
2257 void lu_kmem_fini(struct lu_kmem_descr *caches)
2259 for (; caches->ckd_cache != NULL; ++caches) {
2260 if (*caches->ckd_cache != NULL) {
2261 kmem_cache_destroy(*caches->ckd_cache);
2262 *caches->ckd_cache = NULL;
2266 EXPORT_SYMBOL(lu_kmem_fini);
2269 * Temporary solution to be able to assign fid in ->do_create()
2270 * till we have fully-functional OST fids
2272 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2273 const struct lu_fid *fid)
2275 struct lu_site *s = o->lo_dev->ld_site;
2276 struct lu_fid *old = &o->lo_header->loh_fid;
2277 struct cfs_hash *hs;
2278 struct cfs_hash_bd bd;
2280 LASSERT(fid_is_zero(old));
2282 /* supposed to be unique */
2283 hs = s->ls_obj_hash;
2284 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2285 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
2288 wait_queue_t waiter;
2289 struct lu_object *shadow;
2290 shadow = htable_lookup(s, &bd, fid, &waiter, &version);
2291 /* supposed to be unique */
2292 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2296 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2297 cfs_hash_bd_unlock(hs, &bd, 1);
2299 EXPORT_SYMBOL(lu_object_assign_fid);
2302 * allocates object with 0 (non-assiged) fid
2303 * XXX: temporary solution to be able to assign fid in ->do_create()
2304 * till we have fully-functional OST fids
2306 struct lu_object *lu_object_anon(const struct lu_env *env,
2307 struct lu_device *dev,
2308 const struct lu_object_conf *conf)
2311 struct lu_object *o;
2314 o = lu_object_alloc(env, dev, &fid, conf);
2318 EXPORT_SYMBOL(lu_object_anon);
2320 struct lu_buf LU_BUF_NULL = {
2324 EXPORT_SYMBOL(LU_BUF_NULL);
2326 void lu_buf_free(struct lu_buf *buf)
2330 LASSERT(buf->lb_len > 0);
2331 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2336 EXPORT_SYMBOL(lu_buf_free);
2338 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2341 LASSERT(buf->lb_buf == NULL);
2342 LASSERT(buf->lb_len == 0);
2343 OBD_ALLOC_LARGE(buf->lb_buf, size);
2344 if (likely(buf->lb_buf))
2347 EXPORT_SYMBOL(lu_buf_alloc);
2349 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2352 lu_buf_alloc(buf, size);
2354 EXPORT_SYMBOL(lu_buf_realloc);
2356 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2358 if (buf->lb_buf == NULL && buf->lb_len == 0)
2359 lu_buf_alloc(buf, len);
2361 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2362 lu_buf_realloc(buf, len);
2366 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2369 * Increase the size of the \a buf.
2370 * preserves old data in buffer
2371 * old buffer remains unchanged on error
2372 * \retval 0 or -ENOMEM
2374 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2378 if (len <= buf->lb_len)
2381 OBD_ALLOC_LARGE(ptr, len);
2385 /* Free the old buf */
2386 if (buf->lb_buf != NULL) {
2387 memcpy(ptr, buf->lb_buf, buf->lb_len);
2388 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2395 EXPORT_SYMBOL(lu_buf_check_and_grow);