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 <linux/module.h>
48 #include <linux/list.h>
49 #include <libcfs/libcfs.h>
50 #include <libcfs/libcfs_hash.h> /* hash_long() */
51 #include <obd_class.h>
52 #include <obd_support.h>
53 #include <lustre_disk.h>
54 #include <lustre_fid.h>
55 #include <lu_object.h>
59 LU_CACHE_PERCENT_MAX = 50,
60 LU_CACHE_PERCENT_DEFAULT = 20
63 #define LU_CACHE_NR_MAX_ADJUST 512
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.
351 * if canblock is 0, then don't block awaiting for another
352 * instance of lu_site_purge() to complete
354 int lu_site_purge_objects(const struct lu_env *env, struct lu_site *s,
355 int nr, int canblock)
357 struct lu_object_header *h;
358 struct lu_object_header *temp;
359 struct lu_site_bkt_data *bkt;
360 struct cfs_hash_bd bd;
361 struct cfs_hash_bd bd2;
362 struct list_head dispose;
364 unsigned int start = 0;
369 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
372 INIT_LIST_HEAD(&dispose);
374 * Under LRU list lock, scan LRU list and move unreferenced objects to
375 * the dispose list, removing them from LRU and hash table.
378 start = s->ls_purge_start;
379 bnr = (nr == ~0) ? -1 : nr / (int)CFS_HASH_NBKT(s->ls_obj_hash) + 1;
382 * It doesn't make any sense to make purge threads parallel, that can
383 * only bring troubles to us. See LU-5331.
386 mutex_lock(&s->ls_purge_mutex);
387 else if (mutex_trylock(&s->ls_purge_mutex) == 0)
391 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
395 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
396 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
398 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
399 LASSERT(atomic_read(&h->loh_ref) == 0);
401 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
402 LASSERT(bd.bd_bucket == bd2.bd_bucket);
404 cfs_hash_bd_del_locked(s->ls_obj_hash,
406 list_move(&h->loh_lru, &dispose);
408 percpu_counter_dec(&s->ls_lru_len_counter);
412 if (nr != ~0 && --nr == 0)
415 if (count > 0 && --count == 0)
419 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
422 * Free everything on the dispose list. This is safe against
423 * races due to the reasons described in lu_object_put().
425 while (!list_empty(&dispose)) {
426 h = container_of0(dispose.next,
427 struct lu_object_header, loh_lru);
428 list_del_init(&h->loh_lru);
429 lu_object_free(env, lu_object_top(h));
430 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
436 mutex_unlock(&s->ls_purge_mutex);
438 if (nr != 0 && did_sth && start != 0) {
439 start = 0; /* restart from the first bucket */
442 /* race on s->ls_purge_start, but nobody cares */
443 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
448 EXPORT_SYMBOL(lu_site_purge_objects);
453 * Code below has to jump through certain loops to output object description
454 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
455 * composes object description from strings that are parts of _lines_ of
456 * output (i.e., strings that are not terminated by newline). This doesn't fit
457 * very well into libcfs_debug_msg() interface that assumes that each message
458 * supplied to it is a self-contained output line.
460 * To work around this, strings are collected in a temporary buffer
461 * (implemented as a value of lu_cdebug_key key), until terminating newline
462 * character is detected.
470 * XXX overflow is not handled correctly.
475 struct lu_cdebug_data {
479 char lck_area[LU_CDEBUG_LINE];
482 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
483 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
486 * Key, holding temporary buffer. This key is registered very early by
489 static struct lu_context_key lu_global_key = {
490 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
491 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
492 .lct_init = lu_global_key_init,
493 .lct_fini = lu_global_key_fini
497 * Printer function emitting messages through libcfs_debug_msg().
499 int lu_cdebug_printer(const struct lu_env *env,
500 void *cookie, const char *format, ...)
502 struct libcfs_debug_msg_data *msgdata = cookie;
503 struct lu_cdebug_data *key;
508 va_start(args, format);
510 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
511 LASSERT(key != NULL);
513 used = strlen(key->lck_area);
514 complete = format[strlen(format) - 1] == '\n';
516 * Append new chunk to the buffer.
518 vsnprintf(key->lck_area + used,
519 ARRAY_SIZE(key->lck_area) - used, format, args);
521 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
522 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
523 key->lck_area[0] = 0;
528 EXPORT_SYMBOL(lu_cdebug_printer);
531 * Print object header.
533 void lu_object_header_print(const struct lu_env *env, void *cookie,
534 lu_printer_t printer,
535 const struct lu_object_header *hdr)
537 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
538 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
540 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
541 list_empty((struct list_head *)&hdr->loh_lru) ? \
543 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
545 EXPORT_SYMBOL(lu_object_header_print);
548 * Print human readable representation of the \a o to the \a printer.
550 void lu_object_print(const struct lu_env *env, void *cookie,
551 lu_printer_t printer, const struct lu_object *o)
553 static const char ruler[] = "........................................";
554 struct lu_object_header *top;
558 lu_object_header_print(env, cookie, printer, top);
559 (*printer)(env, cookie, "{\n");
561 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
563 * print `.' \a depth times followed by type name and address
565 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
566 o->lo_dev->ld_type->ldt_name, o);
568 if (o->lo_ops->loo_object_print != NULL)
569 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
571 (*printer)(env, cookie, "\n");
574 (*printer)(env, cookie, "} header@%p\n", top);
576 EXPORT_SYMBOL(lu_object_print);
579 * Check object consistency.
581 int lu_object_invariant(const struct lu_object *o)
583 struct lu_object_header *top;
586 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
587 if (o->lo_ops->loo_object_invariant != NULL &&
588 !o->lo_ops->loo_object_invariant(o))
594 static struct lu_object *htable_lookup(struct lu_site *s,
595 struct cfs_hash_bd *bd,
596 const struct lu_fid *f,
597 wait_queue_t *waiter,
600 struct lu_site_bkt_data *bkt;
601 struct lu_object_header *h;
602 struct hlist_node *hnode;
603 __u64 ver = cfs_hash_bd_version_get(bd);
606 return ERR_PTR(-ENOENT);
609 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
610 /* cfs_hash_bd_peek_locked is a somehow "internal" function
611 * of cfs_hash, it doesn't add refcount on object. */
612 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
614 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
615 return ERR_PTR(-ENOENT);
618 h = container_of0(hnode, struct lu_object_header, loh_hash);
619 if (likely(!lu_object_is_dying(h))) {
620 cfs_hash_get(s->ls_obj_hash, hnode);
621 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
622 if (!list_empty(&h->loh_lru)) {
623 list_del_init(&h->loh_lru);
625 percpu_counter_dec(&s->ls_lru_len_counter);
627 return lu_object_top(h);
631 * Lookup found an object being destroyed this object cannot be
632 * returned (to assure that references to dying objects are eventually
633 * drained), and moreover, lookup has to wait until object is freed.
636 if (likely(waiter != NULL)) {
637 init_waitqueue_entry(waiter, current);
638 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
639 set_current_state(TASK_UNINTERRUPTIBLE);
640 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
643 return ERR_PTR(-EAGAIN);
647 * Search cache for an object with the fid \a f. If such object is found,
648 * return it. Otherwise, create new object, insert it into cache and return
649 * it. In any case, additional reference is acquired on the returned object.
651 struct lu_object *lu_object_find(const struct lu_env *env,
652 struct lu_device *dev, const struct lu_fid *f,
653 const struct lu_object_conf *conf)
655 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
657 EXPORT_SYMBOL(lu_object_find);
660 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
661 * the calculation for the number of objects to reclaim is not covered by
662 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
663 * This ensures that many concurrent threads will not accidentally purge
666 static void lu_object_limit(const struct lu_env *env,
667 struct lu_device *dev)
671 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
674 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
675 nr = (__u64)lu_cache_nr;
679 lu_site_purge_objects(env, dev->ld_site,
680 MIN(size - nr, LU_CACHE_NR_MAX_ADJUST), 0);
683 static struct lu_object *lu_object_new(const struct lu_env *env,
684 struct lu_device *dev,
685 const struct lu_fid *f,
686 const struct lu_object_conf *conf)
690 struct cfs_hash_bd bd;
692 o = lu_object_alloc(env, dev, f, conf);
693 if (unlikely(IS_ERR(o)))
696 hs = dev->ld_site->ls_obj_hash;
697 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
698 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
699 cfs_hash_bd_unlock(hs, &bd, 1);
701 lu_object_limit(env, dev);
707 * Core logic of lu_object_find*() functions.
709 static struct lu_object *lu_object_find_try(const struct lu_env *env,
710 struct lu_device *dev,
711 const struct lu_fid *f,
712 const struct lu_object_conf *conf,
713 wait_queue_t *waiter)
716 struct lu_object *shadow;
719 struct cfs_hash_bd bd;
723 * This uses standard index maintenance protocol:
725 * - search index under lock, and return object if found;
726 * - otherwise, unlock index, allocate new object;
727 * - lock index and search again;
728 * - if nothing is found (usual case), insert newly created
730 * - otherwise (race: other thread inserted object), free
731 * object just allocated.
735 * For "LOC_F_NEW" case, we are sure the object is new established.
736 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
737 * just alloc and insert directly.
739 * If dying object is found during index search, add @waiter to the
740 * site wait-queue and return ERR_PTR(-EAGAIN).
742 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
743 return lu_object_new(env, dev, f, conf);
747 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
748 o = htable_lookup(s, &bd, f, waiter, &version);
749 cfs_hash_bd_unlock(hs, &bd, 1);
750 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
754 * Allocate new object. This may result in rather complicated
755 * operations, including fld queries, inode loading, etc.
757 o = lu_object_alloc(env, dev, f, conf);
758 if (unlikely(IS_ERR(o)))
761 LASSERT(lu_fid_eq(lu_object_fid(o), f));
763 cfs_hash_bd_lock(hs, &bd, 1);
765 shadow = htable_lookup(s, &bd, f, waiter, &version);
766 if (likely(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT)) {
767 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
768 cfs_hash_bd_unlock(hs, &bd, 1);
770 lu_object_limit(env, dev);
775 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
776 cfs_hash_bd_unlock(hs, &bd, 1);
777 lu_object_free(env, o);
782 * Much like lu_object_find(), but top level device of object is specifically
783 * \a dev rather than top level device of the site. This interface allows
784 * objects of different "stacking" to be created within the same site.
786 struct lu_object *lu_object_find_at(const struct lu_env *env,
787 struct lu_device *dev,
788 const struct lu_fid *f,
789 const struct lu_object_conf *conf)
791 struct lu_site_bkt_data *bkt;
792 struct lu_object *obj;
795 if (conf != NULL && conf->loc_flags & LOC_F_NOWAIT)
796 return lu_object_find_try(env, dev, f, conf, NULL);
799 obj = lu_object_find_try(env, dev, f, conf, &wait);
800 if (obj != ERR_PTR(-EAGAIN))
803 * lu_object_find_try() already added waiter into the
807 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
808 remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
811 EXPORT_SYMBOL(lu_object_find_at);
814 * Find object with given fid, and return its slice belonging to given device.
816 struct lu_object *lu_object_find_slice(const struct lu_env *env,
817 struct lu_device *dev,
818 const struct lu_fid *f,
819 const struct lu_object_conf *conf)
821 struct lu_object *top;
822 struct lu_object *obj;
824 top = lu_object_find(env, dev, f, conf);
828 obj = lu_object_locate(top->lo_header, dev->ld_type);
829 if (unlikely(obj == NULL)) {
830 lu_object_put(env, top);
831 obj = ERR_PTR(-ENOENT);
836 EXPORT_SYMBOL(lu_object_find_slice);
839 * Global list of all device types.
841 static struct list_head lu_device_types;
843 int lu_device_type_init(struct lu_device_type *ldt)
847 atomic_set(&ldt->ldt_device_nr, 0);
848 INIT_LIST_HEAD(&ldt->ldt_linkage);
849 if (ldt->ldt_ops->ldto_init)
850 result = ldt->ldt_ops->ldto_init(ldt);
853 spin_lock(&obd_types_lock);
854 list_add(&ldt->ldt_linkage, &lu_device_types);
855 spin_unlock(&obd_types_lock);
860 EXPORT_SYMBOL(lu_device_type_init);
862 void lu_device_type_fini(struct lu_device_type *ldt)
864 spin_lock(&obd_types_lock);
865 list_del_init(&ldt->ldt_linkage);
866 spin_unlock(&obd_types_lock);
867 if (ldt->ldt_ops->ldto_fini)
868 ldt->ldt_ops->ldto_fini(ldt);
870 EXPORT_SYMBOL(lu_device_type_fini);
873 * Global list of all sites on this node
875 static struct list_head lu_sites;
876 static struct rw_semaphore lu_sites_guard;
879 * Global environment used by site shrinker.
881 static struct lu_env lu_shrink_env;
883 struct lu_site_print_arg {
884 struct lu_env *lsp_env;
886 lu_printer_t lsp_printer;
890 lu_site_obj_print(struct cfs_hash *hs, struct cfs_hash_bd *bd,
891 struct hlist_node *hnode, void *data)
893 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
894 struct lu_object_header *h;
896 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
897 if (!list_empty(&h->loh_layers)) {
898 const struct lu_object *o;
900 o = lu_object_top(h);
901 lu_object_print(arg->lsp_env, arg->lsp_cookie,
902 arg->lsp_printer, o);
904 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
905 arg->lsp_printer, h);
911 * Print all objects in \a s.
913 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
914 lu_printer_t printer)
916 struct lu_site_print_arg arg = {
917 .lsp_env = (struct lu_env *)env,
918 .lsp_cookie = cookie,
919 .lsp_printer = printer,
922 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
924 EXPORT_SYMBOL(lu_site_print);
927 * Return desired hash table order.
929 static unsigned long lu_htable_order(struct lu_device *top)
931 unsigned long cache_size;
933 unsigned long bits_max = LU_SITE_BITS_MAX;
936 * For ZFS based OSDs the cache should be disabled by default. This
937 * allows the ZFS ARC maximum flexibility in determining what buffers
938 * to cache. If Lustre has objects or buffer which it wants to ensure
939 * always stay cached it must maintain a hold on them.
941 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
942 lu_cache_percent = 1;
943 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
944 return LU_SITE_BITS_MIN;
947 if (strcmp(top->ld_type->ldt_name, LUSTRE_VVP_NAME) == 0)
948 bits_max = LU_SITE_BITS_MAX_CL;
951 * Calculate hash table size, assuming that we want reasonable
952 * performance when 20% of total memory is occupied by cache of
955 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
957 cache_size = totalram_pages;
959 #if BITS_PER_LONG == 32
960 /* limit hashtable size for lowmem systems to low RAM */
961 if (cache_size > 1 << (30 - PAGE_CACHE_SHIFT))
962 cache_size = 1 << (30 - PAGE_CACHE_SHIFT) * 3 / 4;
965 /* clear off unreasonable cache setting. */
966 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
967 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
968 " the range of (0, %u]. Will use default value: %u.\n",
969 lu_cache_percent, LU_CACHE_PERCENT_MAX,
970 LU_CACHE_PERCENT_DEFAULT);
972 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
974 cache_size = cache_size / 100 * lu_cache_percent *
975 (PAGE_CACHE_SIZE / 1024);
977 for (bits = 1; (1 << bits) < cache_size; ++bits) {
981 return clamp_t(typeof(bits), bits, LU_SITE_BITS_MIN, bits_max);
984 static unsigned lu_obj_hop_hash(struct cfs_hash *hs,
985 const void *key, unsigned mask)
987 struct lu_fid *fid = (struct lu_fid *)key;
990 hash = fid_flatten32(fid);
991 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
992 hash = hash_long(hash, hs->hs_bkt_bits);
994 /* give me another random factor */
995 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
997 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
998 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
1003 static void *lu_obj_hop_object(struct hlist_node *hnode)
1005 return hlist_entry(hnode, struct lu_object_header, loh_hash);
1008 static void *lu_obj_hop_key(struct hlist_node *hnode)
1010 struct lu_object_header *h;
1012 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1016 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
1018 struct lu_object_header *h;
1020 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1021 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
1024 static void lu_obj_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
1026 struct lu_object_header *h;
1028 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1029 atomic_inc(&h->loh_ref);
1032 static void lu_obj_hop_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
1034 LBUG(); /* we should never called it */
1037 static struct cfs_hash_ops lu_site_hash_ops = {
1038 .hs_hash = lu_obj_hop_hash,
1039 .hs_key = lu_obj_hop_key,
1040 .hs_keycmp = lu_obj_hop_keycmp,
1041 .hs_object = lu_obj_hop_object,
1042 .hs_get = lu_obj_hop_get,
1043 .hs_put_locked = lu_obj_hop_put_locked,
1046 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1048 spin_lock(&s->ls_ld_lock);
1049 if (list_empty(&d->ld_linkage))
1050 list_add(&d->ld_linkage, &s->ls_ld_linkage);
1051 spin_unlock(&s->ls_ld_lock);
1053 EXPORT_SYMBOL(lu_dev_add_linkage);
1055 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1057 spin_lock(&s->ls_ld_lock);
1058 list_del_init(&d->ld_linkage);
1059 spin_unlock(&s->ls_ld_lock);
1061 EXPORT_SYMBOL(lu_dev_del_linkage);
1064 * Initialize site \a s, with \a d as the top level device.
1066 int lu_site_init(struct lu_site *s, struct lu_device *top)
1068 struct lu_site_bkt_data *bkt;
1069 struct cfs_hash_bd bd;
1076 memset(s, 0, sizeof *s);
1077 mutex_init(&s->ls_purge_mutex);
1079 #ifdef HAVE_PERCPU_COUNTER_INIT_GFP_FLAG
1080 rc = percpu_counter_init(&s->ls_lru_len_counter, 0, GFP_NOFS);
1082 rc = percpu_counter_init(&s->ls_lru_len_counter, 0);
1087 snprintf(name, sizeof(name), "lu_site_%s", top->ld_type->ldt_name);
1088 for (bits = lu_htable_order(top);
1089 bits >= LU_SITE_BITS_MIN; bits--) {
1090 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1091 bits - LU_SITE_BKT_BITS,
1094 CFS_HASH_SPIN_BKTLOCK |
1095 CFS_HASH_NO_ITEMREF |
1097 CFS_HASH_ASSERT_EMPTY |
1099 if (s->ls_obj_hash != NULL)
1103 if (s->ls_obj_hash == NULL) {
1104 CERROR("failed to create lu_site hash with bits: %lu\n", bits);
1108 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1109 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1110 INIT_LIST_HEAD(&bkt->lsb_lru);
1111 init_waitqueue_head(&bkt->lsb_marche_funebre);
1114 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1115 if (s->ls_stats == NULL) {
1116 cfs_hash_putref(s->ls_obj_hash);
1117 s->ls_obj_hash = NULL;
1121 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1122 0, "created", "created");
1123 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1124 0, "cache_hit", "cache_hit");
1125 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1126 0, "cache_miss", "cache_miss");
1127 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1128 0, "cache_race", "cache_race");
1129 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1130 0, "cache_death_race", "cache_death_race");
1131 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1132 0, "lru_purged", "lru_purged");
1134 INIT_LIST_HEAD(&s->ls_linkage);
1135 s->ls_top_dev = top;
1138 lu_ref_add(&top->ld_reference, "site-top", s);
1140 INIT_LIST_HEAD(&s->ls_ld_linkage);
1141 spin_lock_init(&s->ls_ld_lock);
1143 lu_dev_add_linkage(s, top);
1147 EXPORT_SYMBOL(lu_site_init);
1150 * Finalize \a s and release its resources.
1152 void lu_site_fini(struct lu_site *s)
1154 down_write(&lu_sites_guard);
1155 list_del_init(&s->ls_linkage);
1156 up_write(&lu_sites_guard);
1158 percpu_counter_destroy(&s->ls_lru_len_counter);
1160 if (s->ls_obj_hash != NULL) {
1161 cfs_hash_putref(s->ls_obj_hash);
1162 s->ls_obj_hash = NULL;
1165 if (s->ls_top_dev != NULL) {
1166 s->ls_top_dev->ld_site = NULL;
1167 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1168 lu_device_put(s->ls_top_dev);
1169 s->ls_top_dev = NULL;
1172 if (s->ls_stats != NULL)
1173 lprocfs_free_stats(&s->ls_stats);
1175 EXPORT_SYMBOL(lu_site_fini);
1178 * Called when initialization of stack for this site is completed.
1180 int lu_site_init_finish(struct lu_site *s)
1183 down_write(&lu_sites_guard);
1184 result = lu_context_refill(&lu_shrink_env.le_ctx);
1186 list_add(&s->ls_linkage, &lu_sites);
1187 up_write(&lu_sites_guard);
1190 EXPORT_SYMBOL(lu_site_init_finish);
1193 * Acquire additional reference on device \a d
1195 void lu_device_get(struct lu_device *d)
1197 atomic_inc(&d->ld_ref);
1199 EXPORT_SYMBOL(lu_device_get);
1202 * Release reference on device \a d.
1204 void lu_device_put(struct lu_device *d)
1206 LASSERT(atomic_read(&d->ld_ref) > 0);
1207 atomic_dec(&d->ld_ref);
1209 EXPORT_SYMBOL(lu_device_put);
1212 * Initialize device \a d of type \a t.
1214 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1216 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1217 t->ldt_ops->ldto_start != NULL)
1218 t->ldt_ops->ldto_start(t);
1220 memset(d, 0, sizeof *d);
1222 lu_ref_init(&d->ld_reference);
1223 INIT_LIST_HEAD(&d->ld_linkage);
1227 EXPORT_SYMBOL(lu_device_init);
1230 * Finalize device \a d.
1232 void lu_device_fini(struct lu_device *d)
1234 struct lu_device_type *t = d->ld_type;
1236 if (d->ld_obd != NULL) {
1237 d->ld_obd->obd_lu_dev = NULL;
1241 lu_ref_fini(&d->ld_reference);
1242 LASSERTF(atomic_read(&d->ld_ref) == 0,
1243 "Refcount is %u\n", atomic_read(&d->ld_ref));
1244 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1246 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1247 t->ldt_ops->ldto_stop != NULL)
1248 t->ldt_ops->ldto_stop(t);
1250 EXPORT_SYMBOL(lu_device_fini);
1253 * Initialize object \a o that is part of compound object \a h and was created
1256 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1257 struct lu_device *d)
1259 memset(o, 0, sizeof(*o));
1263 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1264 INIT_LIST_HEAD(&o->lo_linkage);
1268 EXPORT_SYMBOL(lu_object_init);
1271 * Finalize object and release its resources.
1273 void lu_object_fini(struct lu_object *o)
1275 struct lu_device *dev = o->lo_dev;
1277 LASSERT(list_empty(&o->lo_linkage));
1280 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1286 EXPORT_SYMBOL(lu_object_fini);
1289 * Add object \a o as first layer of compound object \a h
1291 * This is typically called by the ->ldo_object_alloc() method of top-level
1294 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1296 list_move(&o->lo_linkage, &h->loh_layers);
1298 EXPORT_SYMBOL(lu_object_add_top);
1301 * Add object \a o as a layer of compound object, going after \a before.
1303 * This is typically called by the ->ldo_object_alloc() method of \a
1306 void lu_object_add(struct lu_object *before, struct lu_object *o)
1308 list_move(&o->lo_linkage, &before->lo_linkage);
1310 EXPORT_SYMBOL(lu_object_add);
1313 * Initialize compound object.
1315 int lu_object_header_init(struct lu_object_header *h)
1317 memset(h, 0, sizeof *h);
1318 atomic_set(&h->loh_ref, 1);
1319 INIT_HLIST_NODE(&h->loh_hash);
1320 INIT_LIST_HEAD(&h->loh_lru);
1321 INIT_LIST_HEAD(&h->loh_layers);
1322 lu_ref_init(&h->loh_reference);
1325 EXPORT_SYMBOL(lu_object_header_init);
1328 * Finalize compound object.
1330 void lu_object_header_fini(struct lu_object_header *h)
1332 LASSERT(list_empty(&h->loh_layers));
1333 LASSERT(list_empty(&h->loh_lru));
1334 LASSERT(hlist_unhashed(&h->loh_hash));
1335 lu_ref_fini(&h->loh_reference);
1337 EXPORT_SYMBOL(lu_object_header_fini);
1340 * Given a compound object, find its slice, corresponding to the device type
1343 struct lu_object *lu_object_locate(struct lu_object_header *h,
1344 const struct lu_device_type *dtype)
1346 struct lu_object *o;
1348 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1349 if (o->lo_dev->ld_type == dtype)
1354 EXPORT_SYMBOL(lu_object_locate);
1357 * Finalize and free devices in the device stack.
1359 * Finalize device stack by purging object cache, and calling
1360 * lu_device_type_operations::ldto_device_fini() and
1361 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1363 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1365 struct lu_site *site = top->ld_site;
1366 struct lu_device *scan;
1367 struct lu_device *next;
1369 lu_site_purge(env, site, ~0);
1370 for (scan = top; scan != NULL; scan = next) {
1371 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1372 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1373 lu_device_put(scan);
1377 lu_site_purge(env, site, ~0);
1379 for (scan = top; scan != NULL; scan = next) {
1380 const struct lu_device_type *ldt = scan->ld_type;
1381 struct obd_type *type;
1383 next = ldt->ldt_ops->ldto_device_free(env, scan);
1384 type = ldt->ldt_obd_type;
1387 class_put_type(type);
1394 * Maximal number of tld slots.
1396 LU_CONTEXT_KEY_NR = 40
1399 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1401 DEFINE_RWLOCK(lu_keys_guard);
1402 static atomic_t lu_key_initing_cnt = ATOMIC_INIT(0);
1405 * Global counter incremented whenever key is registered, unregistered,
1406 * revived or quiesced. This is used to void unnecessary calls to
1407 * lu_context_refill(). No locking is provided, as initialization and shutdown
1408 * are supposed to be externally serialized.
1410 static unsigned key_set_version = 0;
1415 int lu_context_key_register(struct lu_context_key *key)
1420 LASSERT(key->lct_init != NULL);
1421 LASSERT(key->lct_fini != NULL);
1422 LASSERT(key->lct_tags != 0);
1423 LASSERT(key->lct_owner != NULL);
1426 write_lock(&lu_keys_guard);
1427 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1428 if (lu_keys[i] == NULL) {
1430 atomic_set(&key->lct_used, 1);
1432 lu_ref_init(&key->lct_reference);
1438 write_unlock(&lu_keys_guard);
1441 EXPORT_SYMBOL(lu_context_key_register);
1443 static void key_fini(struct lu_context *ctx, int index)
1445 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1446 struct lu_context_key *key;
1448 key = lu_keys[index];
1449 LASSERT(key != NULL);
1450 LASSERT(key->lct_fini != NULL);
1451 LASSERT(atomic_read(&key->lct_used) > 1);
1453 key->lct_fini(ctx, key, ctx->lc_value[index]);
1454 lu_ref_del(&key->lct_reference, "ctx", ctx);
1455 atomic_dec(&key->lct_used);
1457 LASSERT(key->lct_owner != NULL);
1458 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1459 LINVRNT(module_refcount(key->lct_owner) > 0);
1460 module_put(key->lct_owner);
1462 ctx->lc_value[index] = NULL;
1469 void lu_context_key_degister(struct lu_context_key *key)
1471 LASSERT(atomic_read(&key->lct_used) >= 1);
1472 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1474 lu_context_key_quiesce(key);
1477 write_lock(&lu_keys_guard);
1478 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1481 * Wait until all transient contexts referencing this key have
1482 * run lu_context_key::lct_fini() method.
1484 while (atomic_read(&key->lct_used) > 1) {
1485 write_unlock(&lu_keys_guard);
1486 CDEBUG(D_INFO, "lu_context_key_degister: \"%s\" %p, %d\n",
1487 key->lct_owner ? key->lct_owner->name : "", key,
1488 atomic_read(&key->lct_used));
1490 write_lock(&lu_keys_guard);
1492 if (lu_keys[key->lct_index]) {
1493 lu_keys[key->lct_index] = NULL;
1494 lu_ref_fini(&key->lct_reference);
1496 write_unlock(&lu_keys_guard);
1498 LASSERTF(atomic_read(&key->lct_used) == 1,
1499 "key has instances: %d\n",
1500 atomic_read(&key->lct_used));
1502 EXPORT_SYMBOL(lu_context_key_degister);
1505 * Register a number of keys. This has to be called after all keys have been
1506 * initialized by a call to LU_CONTEXT_KEY_INIT().
1508 int lu_context_key_register_many(struct lu_context_key *k, ...)
1510 struct lu_context_key *key = k;
1516 result = lu_context_key_register(key);
1519 key = va_arg(args, struct lu_context_key *);
1520 } while (key != NULL);
1526 lu_context_key_degister(k);
1527 k = va_arg(args, struct lu_context_key *);
1534 EXPORT_SYMBOL(lu_context_key_register_many);
1537 * De-register a number of keys. This is a dual to
1538 * lu_context_key_register_many().
1540 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1546 lu_context_key_degister(k);
1547 k = va_arg(args, struct lu_context_key*);
1548 } while (k != NULL);
1551 EXPORT_SYMBOL(lu_context_key_degister_many);
1554 * Revive a number of keys.
1556 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1562 lu_context_key_revive(k);
1563 k = va_arg(args, struct lu_context_key*);
1564 } while (k != NULL);
1567 EXPORT_SYMBOL(lu_context_key_revive_many);
1570 * Quiescent a number of keys.
1572 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1578 lu_context_key_quiesce(k);
1579 k = va_arg(args, struct lu_context_key*);
1580 } while (k != NULL);
1583 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1586 * Return value associated with key \a key in context \a ctx.
1588 void *lu_context_key_get(const struct lu_context *ctx,
1589 const struct lu_context_key *key)
1591 LINVRNT(ctx->lc_state == LCS_ENTERED);
1592 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1593 LASSERT(lu_keys[key->lct_index] == key);
1594 return ctx->lc_value[key->lct_index];
1596 EXPORT_SYMBOL(lu_context_key_get);
1599 * List of remembered contexts. XXX document me.
1601 static struct list_head lu_context_remembered;
1604 * Destroy \a key in all remembered contexts. This is used to destroy key
1605 * values in "shared" contexts (like service threads), when a module owning
1606 * the key is about to be unloaded.
1608 void lu_context_key_quiesce(struct lu_context_key *key)
1610 struct lu_context *ctx;
1612 if (!(key->lct_tags & LCT_QUIESCENT)) {
1614 * XXX memory barrier has to go here.
1616 write_lock(&lu_keys_guard);
1617 key->lct_tags |= LCT_QUIESCENT;
1620 * Wait until all lu_context_key::lct_init() methods
1623 while (atomic_read(&lu_key_initing_cnt) > 0) {
1624 write_unlock(&lu_keys_guard);
1625 CDEBUG(D_INFO, "lu_context_key_quiesce: \"%s\""
1627 key->lct_owner ? key->lct_owner->name : "",
1628 key, atomic_read(&key->lct_used),
1629 atomic_read(&lu_key_initing_cnt));
1631 write_lock(&lu_keys_guard);
1634 list_for_each_entry(ctx, &lu_context_remembered,
1636 key_fini(ctx, key->lct_index);
1637 write_unlock(&lu_keys_guard);
1642 void lu_context_key_revive(struct lu_context_key *key)
1644 key->lct_tags &= ~LCT_QUIESCENT;
1648 static void keys_fini(struct lu_context *ctx)
1652 if (ctx->lc_value == NULL)
1655 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1658 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1659 ctx->lc_value = NULL;
1662 static int keys_fill(struct lu_context *ctx)
1667 * A serialisation with lu_context_key_quiesce() is needed, but some
1668 * "key->lct_init()" are calling kernel memory allocation routine and
1669 * can't be called while holding a spin_lock.
1670 * "lu_keys_guard" is held while incrementing "lu_key_initing_cnt"
1671 * to ensure the start of the serialisation.
1672 * An atomic_t variable is still used, in order not to reacquire the
1673 * lock when decrementing the counter.
1675 read_lock(&lu_keys_guard);
1676 atomic_inc(&lu_key_initing_cnt);
1677 read_unlock(&lu_keys_guard);
1679 LINVRNT(ctx->lc_value != NULL);
1680 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1681 struct lu_context_key *key;
1684 if (ctx->lc_value[i] == NULL && key != NULL &&
1685 (key->lct_tags & ctx->lc_tags) &&
1687 * Don't create values for a LCT_QUIESCENT key, as this
1688 * will pin module owning a key.
1690 !(key->lct_tags & LCT_QUIESCENT)) {
1693 LINVRNT(key->lct_init != NULL);
1694 LINVRNT(key->lct_index == i);
1696 LASSERT(key->lct_owner != NULL);
1697 if (!(ctx->lc_tags & LCT_NOREF) &&
1698 try_module_get(key->lct_owner) == 0) {
1699 /* module is unloading, skip this key */
1703 value = key->lct_init(ctx, key);
1704 if (unlikely(IS_ERR(value))) {
1705 atomic_dec(&lu_key_initing_cnt);
1706 return PTR_ERR(value);
1709 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1710 atomic_inc(&key->lct_used);
1712 * This is the only place in the code, where an
1713 * element of ctx->lc_value[] array is set to non-NULL
1716 ctx->lc_value[i] = value;
1717 if (key->lct_exit != NULL)
1718 ctx->lc_tags |= LCT_HAS_EXIT;
1720 ctx->lc_version = key_set_version;
1722 atomic_dec(&lu_key_initing_cnt);
1726 static int keys_init(struct lu_context *ctx)
1728 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1729 if (likely(ctx->lc_value != NULL))
1730 return keys_fill(ctx);
1736 * Initialize context data-structure. Create values for all keys.
1738 int lu_context_init(struct lu_context *ctx, __u32 tags)
1742 memset(ctx, 0, sizeof *ctx);
1743 ctx->lc_state = LCS_INITIALIZED;
1744 ctx->lc_tags = tags;
1745 if (tags & LCT_REMEMBER) {
1746 write_lock(&lu_keys_guard);
1747 list_add(&ctx->lc_remember, &lu_context_remembered);
1748 write_unlock(&lu_keys_guard);
1750 INIT_LIST_HEAD(&ctx->lc_remember);
1753 rc = keys_init(ctx);
1755 lu_context_fini(ctx);
1759 EXPORT_SYMBOL(lu_context_init);
1762 * Finalize context data-structure. Destroy key values.
1764 void lu_context_fini(struct lu_context *ctx)
1766 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1767 ctx->lc_state = LCS_FINALIZED;
1769 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1770 LASSERT(list_empty(&ctx->lc_remember));
1773 } else { /* could race with key degister */
1774 write_lock(&lu_keys_guard);
1776 list_del_init(&ctx->lc_remember);
1777 write_unlock(&lu_keys_guard);
1780 EXPORT_SYMBOL(lu_context_fini);
1783 * Called before entering context.
1785 void lu_context_enter(struct lu_context *ctx)
1787 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1788 ctx->lc_state = LCS_ENTERED;
1790 EXPORT_SYMBOL(lu_context_enter);
1793 * Called after exiting from \a ctx
1795 void lu_context_exit(struct lu_context *ctx)
1799 LINVRNT(ctx->lc_state == LCS_ENTERED);
1800 ctx->lc_state = LCS_LEFT;
1801 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1802 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1803 /* could race with key quiescency */
1804 if (ctx->lc_tags & LCT_REMEMBER)
1805 read_lock(&lu_keys_guard);
1806 if (ctx->lc_value[i] != NULL) {
1807 struct lu_context_key *key;
1810 LASSERT(key != NULL);
1811 if (key->lct_exit != NULL)
1813 key, ctx->lc_value[i]);
1815 if (ctx->lc_tags & LCT_REMEMBER)
1816 read_unlock(&lu_keys_guard);
1820 EXPORT_SYMBOL(lu_context_exit);
1823 * Allocate for context all missing keys that were registered after context
1824 * creation. key_set_version is only changed in rare cases when modules
1825 * are loaded and removed.
1827 int lu_context_refill(struct lu_context *ctx)
1829 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1833 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1834 * obd being added. Currently, this is only used on client side, specifically
1835 * for echo device client, for other stack (like ptlrpc threads), context are
1836 * predefined when the lu_device type are registered, during the module probe
1839 __u32 lu_context_tags_default = 0;
1840 __u32 lu_session_tags_default = 0;
1842 void lu_context_tags_update(__u32 tags)
1844 write_lock(&lu_keys_guard);
1845 lu_context_tags_default |= tags;
1847 write_unlock(&lu_keys_guard);
1849 EXPORT_SYMBOL(lu_context_tags_update);
1851 void lu_context_tags_clear(__u32 tags)
1853 write_lock(&lu_keys_guard);
1854 lu_context_tags_default &= ~tags;
1856 write_unlock(&lu_keys_guard);
1858 EXPORT_SYMBOL(lu_context_tags_clear);
1860 void lu_session_tags_update(__u32 tags)
1862 write_lock(&lu_keys_guard);
1863 lu_session_tags_default |= tags;
1865 write_unlock(&lu_keys_guard);
1867 EXPORT_SYMBOL(lu_session_tags_update);
1869 void lu_session_tags_clear(__u32 tags)
1871 write_lock(&lu_keys_guard);
1872 lu_session_tags_default &= ~tags;
1874 write_unlock(&lu_keys_guard);
1876 EXPORT_SYMBOL(lu_session_tags_clear);
1878 int lu_env_init(struct lu_env *env, __u32 tags)
1883 result = lu_context_init(&env->le_ctx, tags);
1884 if (likely(result == 0))
1885 lu_context_enter(&env->le_ctx);
1888 EXPORT_SYMBOL(lu_env_init);
1890 void lu_env_fini(struct lu_env *env)
1892 lu_context_exit(&env->le_ctx);
1893 lu_context_fini(&env->le_ctx);
1896 EXPORT_SYMBOL(lu_env_fini);
1898 int lu_env_refill(struct lu_env *env)
1902 result = lu_context_refill(&env->le_ctx);
1903 if (result == 0 && env->le_ses != NULL)
1904 result = lu_context_refill(env->le_ses);
1907 EXPORT_SYMBOL(lu_env_refill);
1910 * Currently, this API will only be used by echo client.
1911 * Because echo client and normal lustre client will share
1912 * same cl_env cache. So echo client needs to refresh
1913 * the env context after it get one from the cache, especially
1914 * when normal client and echo client co-exist in the same client.
1916 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1921 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1922 env->le_ctx.lc_version = 0;
1923 env->le_ctx.lc_tags |= ctags;
1926 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1927 env->le_ses->lc_version = 0;
1928 env->le_ses->lc_tags |= stags;
1931 result = lu_env_refill(env);
1935 EXPORT_SYMBOL(lu_env_refill_by_tags);
1937 static struct shrinker *lu_site_shrinker;
1939 typedef struct lu_site_stats{
1940 unsigned lss_populated;
1941 unsigned lss_max_search;
1946 static void lu_site_stats_get(struct cfs_hash *hs,
1947 lu_site_stats_t *stats, int populated)
1949 struct cfs_hash_bd bd;
1952 cfs_hash_for_each_bucket(hs, &bd, i) {
1953 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1954 struct hlist_head *hhead;
1956 cfs_hash_bd_lock(hs, &bd, 1);
1958 cfs_hash_bd_count_get(&bd) - bkt->lsb_lru_len;
1959 stats->lss_total += cfs_hash_bd_count_get(&bd);
1960 stats->lss_max_search = max((int)stats->lss_max_search,
1961 cfs_hash_bd_depmax_get(&bd));
1963 cfs_hash_bd_unlock(hs, &bd, 1);
1967 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1968 if (!hlist_empty(hhead))
1969 stats->lss_populated++;
1971 cfs_hash_bd_unlock(hs, &bd, 1);
1977 * lu_cache_shrink_count() returns an approximate number of cached objects
1978 * that can be freed by shrink_slab(). A counter, which tracks the
1979 * number of items in the site's lru, is maintained in a percpu_counter
1980 * for each site. The percpu values are incremented and decremented as
1981 * objects are added or removed from the lru. The percpu values are summed
1982 * and saved whenever a percpu value exceeds a threshold. Thus the saved,
1983 * summed value at any given time may not accurately reflect the current
1984 * lru length. But this value is sufficiently accurate for the needs of
1987 * Using a per cpu counter is a compromise solution to concurrent access:
1988 * lu_object_put() can update the counter without locking the site and
1989 * lu_cache_shrink_count can sum the counters without locking each
1990 * ls_obj_hash bucket.
1992 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
1993 struct shrink_control *sc)
1996 struct lu_site *tmp;
1997 unsigned long cached = 0;
1999 if (!(sc->gfp_mask & __GFP_FS))
2002 down_read(&lu_sites_guard);
2003 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage)
2004 cached += percpu_counter_read_positive(&s->ls_lru_len_counter);
2005 up_read(&lu_sites_guard);
2007 cached = (cached / 100) * sysctl_vfs_cache_pressure;
2008 CDEBUG(D_INODE, "%ld objects cached, cache pressure %d\n",
2009 cached, sysctl_vfs_cache_pressure);
2014 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
2015 struct shrink_control *sc)
2018 struct lu_site *tmp;
2019 unsigned long remain = sc->nr_to_scan;
2022 if (!(sc->gfp_mask & __GFP_FS))
2023 /* We must not take the lu_sites_guard lock when
2024 * __GFP_FS is *not* set because of the deadlock
2025 * possibility detailed above. Additionally,
2026 * since we cannot determine the number of
2027 * objects in the cache without taking this
2028 * lock, we're in a particularly tough spot. As
2029 * a result, we'll just lie and say our cache is
2030 * empty. This _should_ be ok, as we can't
2031 * reclaim objects when __GFP_FS is *not* set
2036 down_write(&lu_sites_guard);
2037 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
2038 remain = lu_site_purge(&lu_shrink_env, s, remain);
2040 * Move just shrunk site to the tail of site list to
2041 * assure shrinking fairness.
2043 list_move_tail(&s->ls_linkage, &splice);
2045 list_splice(&splice, lu_sites.prev);
2046 up_write(&lu_sites_guard);
2048 return sc->nr_to_scan - remain;
2051 #ifndef HAVE_SHRINKER_COUNT
2053 * There exists a potential lock inversion deadlock scenario when using
2054 * Lustre on top of ZFS. This occurs between one of ZFS's
2055 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
2056 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
2057 * while thread B will take the ht_lock and sleep on the lu_sites_guard
2058 * lock. Obviously neither thread will wake and drop their respective hold
2061 * To prevent this from happening we must ensure the lu_sites_guard lock is
2062 * not taken while down this code path. ZFS reliably does not set the
2063 * __GFP_FS bit in its code paths, so this can be used to determine if it
2064 * is safe to take the lu_sites_guard lock.
2066 * Ideally we should accurately return the remaining number of cached
2067 * objects without taking the lu_sites_guard lock, but this is not
2068 * possible in the current implementation.
2070 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
2073 struct shrink_control scv = {
2074 .nr_to_scan = shrink_param(sc, nr_to_scan),
2075 .gfp_mask = shrink_param(sc, gfp_mask)
2077 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
2078 struct shrinker* shrinker = NULL;
2082 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
2084 if (scv.nr_to_scan != 0)
2085 lu_cache_shrink_scan(shrinker, &scv);
2087 cached = lu_cache_shrink_count(shrinker, &scv);
2091 #endif /* HAVE_SHRINKER_COUNT */
2099 * Environment to be used in debugger, contains all tags.
2101 static struct lu_env lu_debugging_env;
2104 * Debugging printer function using printk().
2106 int lu_printk_printer(const struct lu_env *env,
2107 void *unused, const char *format, ...)
2111 va_start(args, format);
2112 vprintk(format, args);
2117 int lu_debugging_setup(void)
2119 return lu_env_init(&lu_debugging_env, ~0);
2122 void lu_context_keys_dump(void)
2126 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2127 struct lu_context_key *key;
2131 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2132 i, key, key->lct_tags,
2133 key->lct_init, key->lct_fini, key->lct_exit,
2134 key->lct_index, atomic_read(&key->lct_used),
2135 key->lct_owner ? key->lct_owner->name : "",
2137 lu_ref_print(&key->lct_reference);
2143 * Initialization of global lu_* data.
2145 int lu_global_init(void)
2148 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2149 lu_cache_shrink_count, lu_cache_shrink_scan);
2151 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2153 INIT_LIST_HEAD(&lu_device_types);
2154 INIT_LIST_HEAD(&lu_context_remembered);
2155 INIT_LIST_HEAD(&lu_sites);
2156 init_rwsem(&lu_sites_guard);
2158 result = lu_ref_global_init();
2162 LU_CONTEXT_KEY_INIT(&lu_global_key);
2163 result = lu_context_key_register(&lu_global_key);
2168 * At this level, we don't know what tags are needed, so allocate them
2169 * conservatively. This should not be too bad, because this
2170 * environment is global.
2172 down_write(&lu_sites_guard);
2173 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2174 up_write(&lu_sites_guard);
2179 * seeks estimation: 3 seeks to read a record from oi, one to read
2180 * inode, one for ea. Unfortunately setting this high value results in
2181 * lu_object/inode cache consuming all the memory.
2183 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2184 if (lu_site_shrinker == NULL)
2191 * Dual to lu_global_init().
2193 void lu_global_fini(void)
2195 if (lu_site_shrinker != NULL) {
2196 remove_shrinker(lu_site_shrinker);
2197 lu_site_shrinker = NULL;
2200 lu_context_key_degister(&lu_global_key);
2203 * Tear shrinker environment down _after_ de-registering
2204 * lu_global_key, because the latter has a value in the former.
2206 down_write(&lu_sites_guard);
2207 lu_env_fini(&lu_shrink_env);
2208 up_write(&lu_sites_guard);
2210 lu_ref_global_fini();
2213 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2215 #ifdef CONFIG_PROC_FS
2216 struct lprocfs_counter ret;
2218 lprocfs_stats_collect(stats, idx, &ret);
2219 return (__u32)ret.lc_count;
2226 * Output site statistical counters into a buffer. Suitable for
2227 * lprocfs_rd_*()-style functions.
2229 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2231 lu_site_stats_t stats;
2233 memset(&stats, 0, sizeof(stats));
2234 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2236 seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2239 stats.lss_populated,
2240 CFS_HASH_NHLIST(s->ls_obj_hash),
2241 stats.lss_max_search,
2242 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2243 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2244 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2245 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2246 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2247 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2250 EXPORT_SYMBOL(lu_site_stats_seq_print);
2253 * Helper function to initialize a number of kmem slab caches at once.
2255 int lu_kmem_init(struct lu_kmem_descr *caches)
2258 struct lu_kmem_descr *iter = caches;
2260 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2261 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2264 if (*iter->ckd_cache == NULL) {
2266 /* free all previously allocated caches */
2267 lu_kmem_fini(caches);
2273 EXPORT_SYMBOL(lu_kmem_init);
2276 * Helper function to finalize a number of kmem slab cached at once. Dual to
2279 void lu_kmem_fini(struct lu_kmem_descr *caches)
2281 for (; caches->ckd_cache != NULL; ++caches) {
2282 if (*caches->ckd_cache != NULL) {
2283 kmem_cache_destroy(*caches->ckd_cache);
2284 *caches->ckd_cache = NULL;
2288 EXPORT_SYMBOL(lu_kmem_fini);
2291 * Temporary solution to be able to assign fid in ->do_create()
2292 * till we have fully-functional OST fids
2294 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2295 const struct lu_fid *fid)
2297 struct lu_site *s = o->lo_dev->ld_site;
2298 struct lu_fid *old = &o->lo_header->loh_fid;
2299 struct cfs_hash *hs;
2300 struct cfs_hash_bd bd;
2302 LASSERT(fid_is_zero(old));
2304 /* supposed to be unique */
2305 hs = s->ls_obj_hash;
2306 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2307 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
2310 wait_queue_t waiter;
2311 struct lu_object *shadow;
2312 shadow = htable_lookup(s, &bd, fid, &waiter, &version);
2313 /* supposed to be unique */
2314 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2318 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2319 cfs_hash_bd_unlock(hs, &bd, 1);
2321 EXPORT_SYMBOL(lu_object_assign_fid);
2324 * allocates object with 0 (non-assiged) fid
2325 * XXX: temporary solution to be able to assign fid in ->do_create()
2326 * till we have fully-functional OST fids
2328 struct lu_object *lu_object_anon(const struct lu_env *env,
2329 struct lu_device *dev,
2330 const struct lu_object_conf *conf)
2333 struct lu_object *o;
2336 o = lu_object_alloc(env, dev, &fid, conf);
2340 EXPORT_SYMBOL(lu_object_anon);
2342 struct lu_buf LU_BUF_NULL = {
2346 EXPORT_SYMBOL(LU_BUF_NULL);
2348 void lu_buf_free(struct lu_buf *buf)
2352 LASSERT(buf->lb_len > 0);
2353 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2358 EXPORT_SYMBOL(lu_buf_free);
2360 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2363 LASSERT(buf->lb_buf == NULL);
2364 LASSERT(buf->lb_len == 0);
2365 OBD_ALLOC_LARGE(buf->lb_buf, size);
2366 if (likely(buf->lb_buf))
2369 EXPORT_SYMBOL(lu_buf_alloc);
2371 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2374 lu_buf_alloc(buf, size);
2376 EXPORT_SYMBOL(lu_buf_realloc);
2378 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2380 if (buf->lb_buf == NULL && buf->lb_len == 0)
2381 lu_buf_alloc(buf, len);
2383 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2384 lu_buf_realloc(buf, len);
2388 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2391 * Increase the size of the \a buf.
2392 * preserves old data in buffer
2393 * old buffer remains unchanged on error
2394 * \retval 0 or -ENOMEM
2396 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2400 if (len <= buf->lb_len)
2403 OBD_ALLOC_LARGE(ptr, len);
2407 /* Free the old buf */
2408 if (buf->lb_buf != NULL) {
2409 memcpy(ptr, buf->lb_buf, buf->lb_len);
2410 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);