4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2014, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/lu_object.c
39 * These are the only exported functions, they provide some generic
40 * infrastructure for managing object devices
42 * Author: Nikita Danilov <nikita.danilov@sun.com>
45 #define DEBUG_SUBSYSTEM S_CLASS
47 #include <libcfs/libcfs.h>
48 #include <linux/module.h>
49 #include <libcfs/libcfs_hash.h> /* hash_long() */
50 #include <obd_class.h>
51 #include <obd_support.h>
52 #include <lustre_disk.h>
53 #include <lustre_fid.h>
54 #include <lu_object.h>
56 #include <libcfs/list.h>
59 LU_CACHE_PERCENT_MAX = 50,
60 LU_CACHE_PERCENT_DEFAULT = 20
63 #define LU_CACHE_NR_MAX_ADJUST 128
64 #define LU_CACHE_NR_UNLIMITED -1
65 #define LU_CACHE_NR_DEFAULT LU_CACHE_NR_UNLIMITED
66 #define LU_CACHE_NR_LDISKFS_LIMIT LU_CACHE_NR_UNLIMITED
67 /** This is set to roughly (20 * OSS_NTHRS_MAX) to prevent thrashing */
68 #define LU_CACHE_NR_ZFS_LIMIT 10240
70 #define LU_SITE_BITS_MIN 12
71 #define LU_SITE_BITS_MAX 24
73 * total 256 buckets, we don't want too many buckets because:
74 * - consume too much memory
75 * - avoid unbalanced LRU list
77 #define LU_SITE_BKT_BITS 8
80 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
81 CFS_MODULE_PARM(lu_cache_percent, "i", int, 0644,
82 "Percentage of memory to be used as lu_object cache");
84 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
85 CFS_MODULE_PARM(lu_cache_nr, "l", long, 0644,
86 "Maximum number of objects in lu_object cache");
88 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
91 * Decrease reference counter on object. If last reference is freed, return
92 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
93 * case, free object immediately.
95 void lu_object_put(const struct lu_env *env, struct lu_object *o)
97 struct lu_site_bkt_data *bkt;
98 struct lu_object_header *top;
100 struct lu_object *orig;
102 const struct lu_fid *fid;
105 site = o->lo_dev->ld_site;
109 * till we have full fids-on-OST implemented anonymous objects
110 * are possible in OSP. such an object isn't listed in the site
111 * so we should not remove it from the site.
113 fid = lu_object_fid(o);
114 if (fid_is_zero(fid)) {
115 LASSERT(top->loh_hash.next == NULL
116 && top->loh_hash.pprev == NULL);
117 LASSERT(list_empty(&top->loh_lru));
118 if (!atomic_dec_and_test(&top->loh_ref))
120 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
121 if (o->lo_ops->loo_object_release != NULL)
122 o->lo_ops->loo_object_release(env, o);
124 lu_object_free(env, orig);
128 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
129 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
131 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
132 if (lu_object_is_dying(top)) {
135 * somebody may be waiting for this, currently only
136 * used for cl_object, see cl_object_put_last().
138 wake_up_all(&bkt->lsb_marche_funebre);
144 * When last reference is released, iterate over object
145 * layers, and notify them that object is no longer busy.
147 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
148 if (o->lo_ops->loo_object_release != NULL)
149 o->lo_ops->loo_object_release(env, o);
152 if (!lu_object_is_dying(top) &&
153 (lu_object_exists(orig) || lu_object_is_cl(orig))) {
154 LASSERT(list_empty(&top->loh_lru));
155 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
157 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
162 * If object is dying (will not be cached), removed it
163 * from hash table and LRU.
165 * This is done with hash table and LRU lists locked. As the only
166 * way to acquire first reference to previously unreferenced
167 * object is through hash-table lookup (lu_object_find()),
168 * or LRU scanning (lu_site_purge()), that are done under hash-table
169 * and LRU lock, no race with concurrent object lookup is possible
170 * and we can safely destroy object below.
172 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
173 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
174 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
176 * Object was already removed from hash and lru above, can
179 lu_object_free(env, orig);
181 EXPORT_SYMBOL(lu_object_put);
184 * Put object and don't keep in cache. This is temporary solution for
185 * multi-site objects when its layering is not constant.
187 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
189 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
190 return lu_object_put(env, o);
192 EXPORT_SYMBOL(lu_object_put_nocache);
195 * Kill the object and take it out of LRU cache.
196 * Currently used by client code for layout change.
198 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
200 struct lu_object_header *top;
203 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
204 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
205 cfs_hash_t *obj_hash = o->lo_dev->ld_site->ls_obj_hash;
208 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
209 if (!list_empty(&top->loh_lru)) {
210 struct lu_site_bkt_data *bkt;
212 list_del_init(&top->loh_lru);
213 bkt = cfs_hash_bd_extra_get(obj_hash, &bd);
216 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
217 cfs_hash_bd_unlock(obj_hash, &bd, 1);
220 EXPORT_SYMBOL(lu_object_unhash);
223 * Allocate new object.
225 * This follows object creation protocol, described in the comment within
226 * struct lu_device_operations definition.
228 static struct lu_object *lu_object_alloc(const struct lu_env *env,
229 struct lu_device *dev,
230 const struct lu_fid *f,
231 const struct lu_object_conf *conf)
233 struct lu_object *scan;
234 struct lu_object *top;
235 struct list_head *layers;
236 unsigned int init_mask = 0;
237 unsigned int init_flag;
243 * Create top-level object slice. This will also create
246 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
248 RETURN(ERR_PTR(-ENOMEM));
252 * This is the only place where object fid is assigned. It's constant
255 top->lo_header->loh_fid = *f;
256 layers = &top->lo_header->loh_layers;
260 * Call ->loo_object_init() repeatedly, until no more new
261 * object slices are created.
265 list_for_each_entry(scan, layers, lo_linkage) {
266 if (init_mask & init_flag)
269 scan->lo_header = top->lo_header;
270 result = scan->lo_ops->loo_object_init(env, scan, conf);
272 lu_object_free(env, top);
273 RETURN(ERR_PTR(result));
275 init_mask |= init_flag;
281 list_for_each_entry_reverse(scan, layers, lo_linkage) {
282 if (scan->lo_ops->loo_object_start != NULL) {
283 result = scan->lo_ops->loo_object_start(env, scan);
285 lu_object_free(env, top);
286 RETURN(ERR_PTR(result));
291 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
298 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
300 struct lu_site_bkt_data *bkt;
301 struct lu_site *site;
302 struct lu_object *scan;
303 struct list_head *layers;
304 struct list_head splice;
306 site = o->lo_dev->ld_site;
307 layers = &o->lo_header->loh_layers;
308 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
310 * First call ->loo_object_delete() method to release all resources.
312 list_for_each_entry_reverse(scan, layers, lo_linkage) {
313 if (scan->lo_ops->loo_object_delete != NULL)
314 scan->lo_ops->loo_object_delete(env, scan);
318 * Then, splice object layers into stand-alone list, and call
319 * ->loo_object_free() on all layers to free memory. Splice is
320 * necessary, because lu_object_header is freed together with the
323 INIT_LIST_HEAD(&splice);
324 list_splice_init(layers, &splice);
325 while (!list_empty(&splice)) {
327 * Free layers in bottom-to-top order, so that object header
328 * lives as long as possible and ->loo_object_free() methods
329 * can look at its contents.
331 o = container_of0(splice.prev, struct lu_object, lo_linkage);
332 list_del_init(&o->lo_linkage);
333 LASSERT(o->lo_ops->loo_object_free != NULL);
334 o->lo_ops->loo_object_free(env, o);
337 if (waitqueue_active(&bkt->lsb_marche_funebre))
338 wake_up_all(&bkt->lsb_marche_funebre);
342 * Free \a nr objects from the cold end of the site LRU list.
344 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
346 struct lu_object_header *h;
347 struct lu_object_header *temp;
348 struct lu_site_bkt_data *bkt;
351 struct list_head dispose;
358 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
361 INIT_LIST_HEAD(&dispose);
363 * Under LRU list lock, scan LRU list and move unreferenced objects to
364 * the dispose list, removing them from LRU and hash table.
366 start = s->ls_purge_start;
367 bnr = (nr == ~0) ? -1 : nr / (int)CFS_HASH_NBKT(s->ls_obj_hash) + 1;
370 * It doesn't make any sense to make purge threads parallel, that can
371 * only bring troubles to us. See LU-5331.
373 mutex_lock(&s->ls_purge_mutex);
375 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
379 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
380 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
382 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
383 LASSERT(atomic_read(&h->loh_ref) == 0);
385 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
386 LASSERT(bd.bd_bucket == bd2.bd_bucket);
388 cfs_hash_bd_del_locked(s->ls_obj_hash,
390 list_move(&h->loh_lru, &dispose);
395 if (nr != ~0 && --nr == 0)
398 if (count > 0 && --count == 0)
402 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
405 * Free everything on the dispose list. This is safe against
406 * races due to the reasons described in lu_object_put().
408 while (!list_empty(&dispose)) {
409 h = container_of0(dispose.next,
410 struct lu_object_header, loh_lru);
411 list_del_init(&h->loh_lru);
412 lu_object_free(env, lu_object_top(h));
413 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
419 mutex_unlock(&s->ls_purge_mutex);
421 if (nr != 0 && did_sth && start != 0) {
422 start = 0; /* restart from the first bucket */
425 /* race on s->ls_purge_start, but nobody cares */
426 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
430 EXPORT_SYMBOL(lu_site_purge);
435 * Code below has to jump through certain loops to output object description
436 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
437 * composes object description from strings that are parts of _lines_ of
438 * output (i.e., strings that are not terminated by newline). This doesn't fit
439 * very well into libcfs_debug_msg() interface that assumes that each message
440 * supplied to it is a self-contained output line.
442 * To work around this, strings are collected in a temporary buffer
443 * (implemented as a value of lu_cdebug_key key), until terminating newline
444 * character is detected.
452 * XXX overflow is not handled correctly.
457 struct lu_cdebug_data {
461 char lck_area[LU_CDEBUG_LINE];
464 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
465 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
468 * Key, holding temporary buffer. This key is registered very early by
471 static struct lu_context_key lu_global_key = {
472 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
473 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
474 .lct_init = lu_global_key_init,
475 .lct_fini = lu_global_key_fini
479 * Printer function emitting messages through libcfs_debug_msg().
481 int lu_cdebug_printer(const struct lu_env *env,
482 void *cookie, const char *format, ...)
484 struct libcfs_debug_msg_data *msgdata = cookie;
485 struct lu_cdebug_data *key;
490 va_start(args, format);
492 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
493 LASSERT(key != NULL);
495 used = strlen(key->lck_area);
496 complete = format[strlen(format) - 1] == '\n';
498 * Append new chunk to the buffer.
500 vsnprintf(key->lck_area + used,
501 ARRAY_SIZE(key->lck_area) - used, format, args);
503 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
504 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
505 key->lck_area[0] = 0;
510 EXPORT_SYMBOL(lu_cdebug_printer);
513 * Print object header.
515 void lu_object_header_print(const struct lu_env *env, void *cookie,
516 lu_printer_t printer,
517 const struct lu_object_header *hdr)
519 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
520 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
522 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
523 list_empty((struct list_head *)&hdr->loh_lru) ? \
525 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
527 EXPORT_SYMBOL(lu_object_header_print);
530 * Print human readable representation of the \a o to the \a printer.
532 void lu_object_print(const struct lu_env *env, void *cookie,
533 lu_printer_t printer, const struct lu_object *o)
535 static const char ruler[] = "........................................";
536 struct lu_object_header *top;
540 lu_object_header_print(env, cookie, printer, top);
541 (*printer)(env, cookie, "{\n");
543 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
545 * print `.' \a depth times followed by type name and address
547 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
548 o->lo_dev->ld_type->ldt_name, o);
550 if (o->lo_ops->loo_object_print != NULL)
551 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
553 (*printer)(env, cookie, "\n");
556 (*printer)(env, cookie, "} header@%p\n", top);
558 EXPORT_SYMBOL(lu_object_print);
561 * Check object consistency.
563 int lu_object_invariant(const struct lu_object *o)
565 struct lu_object_header *top;
568 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
569 if (o->lo_ops->loo_object_invariant != NULL &&
570 !o->lo_ops->loo_object_invariant(o))
576 static struct lu_object *htable_lookup(struct lu_site *s,
578 const struct lu_fid *f,
579 wait_queue_t *waiter,
582 struct lu_site_bkt_data *bkt;
583 struct lu_object_header *h;
584 struct hlist_node *hnode;
585 __u64 ver = cfs_hash_bd_version_get(bd);
588 return ERR_PTR(-ENOENT);
591 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
592 /* cfs_hash_bd_peek_locked is a somehow "internal" function
593 * of cfs_hash, it doesn't add refcount on object. */
594 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
596 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
597 return ERR_PTR(-ENOENT);
600 h = container_of0(hnode, struct lu_object_header, loh_hash);
601 if (likely(!lu_object_is_dying(h))) {
602 cfs_hash_get(s->ls_obj_hash, hnode);
603 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
604 if (!list_empty(&h->loh_lru)) {
605 list_del_init(&h->loh_lru);
608 return lu_object_top(h);
612 * Lookup found an object being destroyed this object cannot be
613 * returned (to assure that references to dying objects are eventually
614 * drained), and moreover, lookup has to wait until object is freed.
617 if (likely(waiter != NULL)) {
618 init_waitqueue_entry_current(waiter);
619 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
620 set_current_state(TASK_UNINTERRUPTIBLE);
621 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
624 return ERR_PTR(-EAGAIN);
628 * Search cache for an object with the fid \a f. If such object is found,
629 * return it. Otherwise, create new object, insert it into cache and return
630 * it. In any case, additional reference is acquired on the returned object.
632 struct lu_object *lu_object_find(const struct lu_env *env,
633 struct lu_device *dev, const struct lu_fid *f,
634 const struct lu_object_conf *conf)
636 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
638 EXPORT_SYMBOL(lu_object_find);
641 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
642 * the calculation for the number of objects to reclaim is not covered by
643 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
644 * This ensures that many concurrent threads will not accidentally purge
647 static void lu_object_limit(const struct lu_env *env,
648 struct lu_device *dev)
652 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
655 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
656 nr = (__u64)lu_cache_nr;
658 lu_site_purge(env, dev->ld_site,
659 MIN(size - nr, LU_CACHE_NR_MAX_ADJUST));
664 static struct lu_object *lu_object_new(const struct lu_env *env,
665 struct lu_device *dev,
666 const struct lu_fid *f,
667 const struct lu_object_conf *conf)
673 o = lu_object_alloc(env, dev, f, conf);
674 if (unlikely(IS_ERR(o)))
677 hs = dev->ld_site->ls_obj_hash;
678 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
679 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
680 cfs_hash_bd_unlock(hs, &bd, 1);
682 lu_object_limit(env, dev);
688 * Core logic of lu_object_find*() functions.
690 static struct lu_object *lu_object_find_try(const struct lu_env *env,
691 struct lu_device *dev,
692 const struct lu_fid *f,
693 const struct lu_object_conf *conf,
694 wait_queue_t *waiter)
697 struct lu_object *shadow;
704 * This uses standard index maintenance protocol:
706 * - search index under lock, and return object if found;
707 * - otherwise, unlock index, allocate new object;
708 * - lock index and search again;
709 * - if nothing is found (usual case), insert newly created
711 * - otherwise (race: other thread inserted object), free
712 * object just allocated.
716 * For "LOC_F_NEW" case, we are sure the object is new established.
717 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
718 * just alloc and insert directly.
720 * If dying object is found during index search, add @waiter to the
721 * site wait-queue and return ERR_PTR(-EAGAIN).
723 if (conf != NULL && conf->loc_flags & LOC_F_NEW)
724 return lu_object_new(env, dev, f, conf);
728 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
729 o = htable_lookup(s, &bd, f, waiter, &version);
730 cfs_hash_bd_unlock(hs, &bd, 1);
731 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
735 * Allocate new object. This may result in rather complicated
736 * operations, including fld queries, inode loading, etc.
738 o = lu_object_alloc(env, dev, f, conf);
739 if (unlikely(IS_ERR(o)))
742 LASSERT(lu_fid_eq(lu_object_fid(o), f));
744 cfs_hash_bd_lock(hs, &bd, 1);
746 shadow = htable_lookup(s, &bd, f, waiter, &version);
747 if (likely(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT)) {
748 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
749 cfs_hash_bd_unlock(hs, &bd, 1);
751 lu_object_limit(env, dev);
756 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
757 cfs_hash_bd_unlock(hs, &bd, 1);
758 lu_object_free(env, o);
763 * Much like lu_object_find(), but top level device of object is specifically
764 * \a dev rather than top level device of the site. This interface allows
765 * objects of different "stacking" to be created within the same site.
767 struct lu_object *lu_object_find_at(const struct lu_env *env,
768 struct lu_device *dev,
769 const struct lu_fid *f,
770 const struct lu_object_conf *conf)
772 struct lu_site_bkt_data *bkt;
773 struct lu_object *obj;
776 if (conf != NULL && conf->loc_flags & LOC_F_NOWAIT)
777 return lu_object_find_try(env, dev, f, conf, NULL);
780 obj = lu_object_find_try(env, dev, f, conf, &wait);
781 if (obj != ERR_PTR(-EAGAIN))
784 * lu_object_find_try() already added waiter into the
787 waitq_wait(&wait, TASK_UNINTERRUPTIBLE);
788 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
789 remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
792 EXPORT_SYMBOL(lu_object_find_at);
795 * Find object with given fid, and return its slice belonging to given device.
797 struct lu_object *lu_object_find_slice(const struct lu_env *env,
798 struct lu_device *dev,
799 const struct lu_fid *f,
800 const struct lu_object_conf *conf)
802 struct lu_object *top;
803 struct lu_object *obj;
805 top = lu_object_find(env, dev, f, conf);
809 obj = lu_object_locate(top->lo_header, dev->ld_type);
810 if (unlikely(obj == NULL)) {
811 lu_object_put(env, top);
812 obj = ERR_PTR(-ENOENT);
817 EXPORT_SYMBOL(lu_object_find_slice);
820 * Global list of all device types.
822 static struct list_head lu_device_types;
824 int lu_device_type_init(struct lu_device_type *ldt)
828 atomic_set(&ldt->ldt_device_nr, 0);
829 INIT_LIST_HEAD(&ldt->ldt_linkage);
830 if (ldt->ldt_ops->ldto_init)
831 result = ldt->ldt_ops->ldto_init(ldt);
834 spin_lock(&obd_types_lock);
835 list_add(&ldt->ldt_linkage, &lu_device_types);
836 spin_unlock(&obd_types_lock);
841 EXPORT_SYMBOL(lu_device_type_init);
843 void lu_device_type_fini(struct lu_device_type *ldt)
845 spin_lock(&obd_types_lock);
846 list_del_init(&ldt->ldt_linkage);
847 spin_unlock(&obd_types_lock);
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 struct list_head lu_sites;
857 static DEFINE_MUTEX(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(cfs_hash_t *hs, cfs_hash_bd_t *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;
916 * For ZFS based OSDs the cache should be disabled by default. This
917 * allows the ZFS ARC maximum flexibility in determining what buffers
918 * to cache. If Lustre has objects or buffer which it wants to ensure
919 * always stay cached it must maintain a hold on them.
921 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
922 lu_cache_percent = 1;
923 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
924 return LU_SITE_BITS_MIN;
928 * Calculate hash table size, assuming that we want reasonable
929 * performance when 20% of total memory is occupied by cache of
932 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
934 cache_size = totalram_pages;
936 #if BITS_PER_LONG == 32
937 /* limit hashtable size for lowmem systems to low RAM */
938 if (cache_size > 1 << (30 - PAGE_CACHE_SHIFT))
939 cache_size = 1 << (30 - PAGE_CACHE_SHIFT) * 3 / 4;
942 /* clear off unreasonable cache setting. */
943 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
944 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
945 " the range of (0, %u]. Will use default value: %u.\n",
946 lu_cache_percent, LU_CACHE_PERCENT_MAX,
947 LU_CACHE_PERCENT_DEFAULT);
949 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
951 cache_size = cache_size / 100 * lu_cache_percent *
952 (PAGE_CACHE_SIZE / 1024);
954 for (bits = 1; (1 << bits) < cache_size; ++bits) {
960 static unsigned lu_obj_hop_hash(cfs_hash_t *hs,
961 const void *key, unsigned mask)
963 struct lu_fid *fid = (struct lu_fid *)key;
966 hash = fid_flatten32(fid);
967 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
968 hash = hash_long(hash, hs->hs_bkt_bits);
970 /* give me another random factor */
971 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
973 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
974 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
979 static void *lu_obj_hop_object(struct hlist_node *hnode)
981 return hlist_entry(hnode, struct lu_object_header, loh_hash);
984 static void *lu_obj_hop_key(struct hlist_node *hnode)
986 struct lu_object_header *h;
988 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
992 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
994 struct lu_object_header *h;
996 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
997 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
1000 static void lu_obj_hop_get(cfs_hash_t *hs, struct hlist_node *hnode)
1002 struct lu_object_header *h;
1004 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
1005 atomic_inc(&h->loh_ref);
1008 static void lu_obj_hop_put_locked(cfs_hash_t *hs, struct hlist_node *hnode)
1010 LBUG(); /* we should never called it */
1013 static cfs_hash_ops_t lu_site_hash_ops = {
1014 .hs_hash = lu_obj_hop_hash,
1015 .hs_key = lu_obj_hop_key,
1016 .hs_keycmp = lu_obj_hop_keycmp,
1017 .hs_object = lu_obj_hop_object,
1018 .hs_get = lu_obj_hop_get,
1019 .hs_put_locked = lu_obj_hop_put_locked,
1022 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
1024 spin_lock(&s->ls_ld_lock);
1025 if (list_empty(&d->ld_linkage))
1026 list_add(&d->ld_linkage, &s->ls_ld_linkage);
1027 spin_unlock(&s->ls_ld_lock);
1029 EXPORT_SYMBOL(lu_dev_add_linkage);
1031 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1033 spin_lock(&s->ls_ld_lock);
1034 list_del_init(&d->ld_linkage);
1035 spin_unlock(&s->ls_ld_lock);
1037 EXPORT_SYMBOL(lu_dev_del_linkage);
1040 * Initialize site \a s, with \a d as the top level device.
1042 int lu_site_init(struct lu_site *s, struct lu_device *top)
1044 struct lu_site_bkt_data *bkt;
1051 memset(s, 0, sizeof *s);
1052 mutex_init(&s->ls_purge_mutex);
1053 bits = lu_htable_order(top);
1054 snprintf(name, sizeof(name), "lu_site_%s", top->ld_type->ldt_name);
1055 for (bits = clamp_t(typeof(bits), bits,
1056 LU_SITE_BITS_MIN, LU_SITE_BITS_MAX);
1057 bits >= LU_SITE_BITS_MIN; bits--) {
1058 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1059 bits - LU_SITE_BKT_BITS,
1062 CFS_HASH_SPIN_BKTLOCK |
1063 CFS_HASH_NO_ITEMREF |
1065 CFS_HASH_ASSERT_EMPTY |
1067 if (s->ls_obj_hash != NULL)
1071 if (s->ls_obj_hash == NULL) {
1072 CERROR("failed to create lu_site hash with bits: %lu\n", bits);
1076 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1077 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1078 INIT_LIST_HEAD(&bkt->lsb_lru);
1079 init_waitqueue_head(&bkt->lsb_marche_funebre);
1082 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1083 if (s->ls_stats == NULL) {
1084 cfs_hash_putref(s->ls_obj_hash);
1085 s->ls_obj_hash = NULL;
1089 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1090 0, "created", "created");
1091 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1092 0, "cache_hit", "cache_hit");
1093 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1094 0, "cache_miss", "cache_miss");
1095 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1096 0, "cache_race", "cache_race");
1097 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1098 0, "cache_death_race", "cache_death_race");
1099 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1100 0, "lru_purged", "lru_purged");
1102 INIT_LIST_HEAD(&s->ls_linkage);
1103 s->ls_top_dev = top;
1106 lu_ref_add(&top->ld_reference, "site-top", s);
1108 INIT_LIST_HEAD(&s->ls_ld_linkage);
1109 spin_lock_init(&s->ls_ld_lock);
1111 lu_dev_add_linkage(s, top);
1115 EXPORT_SYMBOL(lu_site_init);
1118 * Finalize \a s and release its resources.
1120 void lu_site_fini(struct lu_site *s)
1122 mutex_lock(&lu_sites_guard);
1123 list_del_init(&s->ls_linkage);
1124 mutex_unlock(&lu_sites_guard);
1126 if (s->ls_obj_hash != NULL) {
1127 cfs_hash_putref(s->ls_obj_hash);
1128 s->ls_obj_hash = NULL;
1131 if (s->ls_top_dev != NULL) {
1132 s->ls_top_dev->ld_site = NULL;
1133 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1134 lu_device_put(s->ls_top_dev);
1135 s->ls_top_dev = NULL;
1138 if (s->ls_stats != NULL)
1139 lprocfs_free_stats(&s->ls_stats);
1141 EXPORT_SYMBOL(lu_site_fini);
1144 * Called when initialization of stack for this site is completed.
1146 int lu_site_init_finish(struct lu_site *s)
1149 mutex_lock(&lu_sites_guard);
1150 result = lu_context_refill(&lu_shrink_env.le_ctx);
1152 list_add(&s->ls_linkage, &lu_sites);
1153 mutex_unlock(&lu_sites_guard);
1156 EXPORT_SYMBOL(lu_site_init_finish);
1159 * Acquire additional reference on device \a d
1161 void lu_device_get(struct lu_device *d)
1163 atomic_inc(&d->ld_ref);
1165 EXPORT_SYMBOL(lu_device_get);
1168 * Release reference on device \a d.
1170 void lu_device_put(struct lu_device *d)
1172 LASSERT(atomic_read(&d->ld_ref) > 0);
1173 atomic_dec(&d->ld_ref);
1175 EXPORT_SYMBOL(lu_device_put);
1178 * Initialize device \a d of type \a t.
1180 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1182 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1183 t->ldt_ops->ldto_start != NULL)
1184 t->ldt_ops->ldto_start(t);
1186 memset(d, 0, sizeof *d);
1188 lu_ref_init(&d->ld_reference);
1189 INIT_LIST_HEAD(&d->ld_linkage);
1193 EXPORT_SYMBOL(lu_device_init);
1196 * Finalize device \a d.
1198 void lu_device_fini(struct lu_device *d)
1200 struct lu_device_type *t = d->ld_type;
1202 if (d->ld_obd != NULL) {
1203 d->ld_obd->obd_lu_dev = NULL;
1207 lu_ref_fini(&d->ld_reference);
1208 LASSERTF(atomic_read(&d->ld_ref) == 0,
1209 "Refcount is %u\n", atomic_read(&d->ld_ref));
1210 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1212 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1213 t->ldt_ops->ldto_stop != NULL)
1214 t->ldt_ops->ldto_stop(t);
1216 EXPORT_SYMBOL(lu_device_fini);
1219 * Initialize object \a o that is part of compound object \a h and was created
1222 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1223 struct lu_device *d)
1225 memset(o, 0, sizeof(*o));
1229 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1230 INIT_LIST_HEAD(&o->lo_linkage);
1234 EXPORT_SYMBOL(lu_object_init);
1237 * Finalize object and release its resources.
1239 void lu_object_fini(struct lu_object *o)
1241 struct lu_device *dev = o->lo_dev;
1243 LASSERT(list_empty(&o->lo_linkage));
1246 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1252 EXPORT_SYMBOL(lu_object_fini);
1255 * Add object \a o as first layer of compound object \a h
1257 * This is typically called by the ->ldo_object_alloc() method of top-level
1260 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1262 list_move(&o->lo_linkage, &h->loh_layers);
1264 EXPORT_SYMBOL(lu_object_add_top);
1267 * Add object \a o as a layer of compound object, going after \a before.
1269 * This is typically called by the ->ldo_object_alloc() method of \a
1272 void lu_object_add(struct lu_object *before, struct lu_object *o)
1274 list_move(&o->lo_linkage, &before->lo_linkage);
1276 EXPORT_SYMBOL(lu_object_add);
1279 * Initialize compound object.
1281 int lu_object_header_init(struct lu_object_header *h)
1283 memset(h, 0, sizeof *h);
1284 atomic_set(&h->loh_ref, 1);
1285 INIT_HLIST_NODE(&h->loh_hash);
1286 INIT_LIST_HEAD(&h->loh_lru);
1287 INIT_LIST_HEAD(&h->loh_layers);
1288 lu_ref_init(&h->loh_reference);
1291 EXPORT_SYMBOL(lu_object_header_init);
1294 * Finalize compound object.
1296 void lu_object_header_fini(struct lu_object_header *h)
1298 LASSERT(list_empty(&h->loh_layers));
1299 LASSERT(list_empty(&h->loh_lru));
1300 LASSERT(hlist_unhashed(&h->loh_hash));
1301 lu_ref_fini(&h->loh_reference);
1303 EXPORT_SYMBOL(lu_object_header_fini);
1306 * Given a compound object, find its slice, corresponding to the device type
1309 struct lu_object *lu_object_locate(struct lu_object_header *h,
1310 const struct lu_device_type *dtype)
1312 struct lu_object *o;
1314 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1315 if (o->lo_dev->ld_type == dtype)
1320 EXPORT_SYMBOL(lu_object_locate);
1323 * Finalize and free devices in the device stack.
1325 * Finalize device stack by purging object cache, and calling
1326 * lu_device_type_operations::ldto_device_fini() and
1327 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1329 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1331 struct lu_site *site = top->ld_site;
1332 struct lu_device *scan;
1333 struct lu_device *next;
1335 lu_site_purge(env, site, ~0);
1336 for (scan = top; scan != NULL; scan = next) {
1337 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1338 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1339 lu_device_put(scan);
1343 lu_site_purge(env, site, ~0);
1345 for (scan = top; scan != NULL; scan = next) {
1346 const struct lu_device_type *ldt = scan->ld_type;
1347 struct obd_type *type;
1349 next = ldt->ldt_ops->ldto_device_free(env, scan);
1350 type = ldt->ldt_obd_type;
1353 class_put_type(type);
1360 * Maximal number of tld slots.
1362 LU_CONTEXT_KEY_NR = 40
1365 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1367 static DEFINE_SPINLOCK(lu_keys_guard);
1370 * Global counter incremented whenever key is registered, unregistered,
1371 * revived or quiesced. This is used to void unnecessary calls to
1372 * lu_context_refill(). No locking is provided, as initialization and shutdown
1373 * are supposed to be externally serialized.
1375 static unsigned key_set_version = 0;
1380 int lu_context_key_register(struct lu_context_key *key)
1385 LASSERT(key->lct_init != NULL);
1386 LASSERT(key->lct_fini != NULL);
1387 LASSERT(key->lct_tags != 0);
1388 LASSERT(key->lct_owner != NULL);
1391 spin_lock(&lu_keys_guard);
1392 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1393 if (lu_keys[i] == NULL) {
1395 atomic_set(&key->lct_used, 1);
1397 lu_ref_init(&key->lct_reference);
1403 spin_unlock(&lu_keys_guard);
1406 EXPORT_SYMBOL(lu_context_key_register);
1408 static void key_fini(struct lu_context *ctx, int index)
1410 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1411 struct lu_context_key *key;
1413 key = lu_keys[index];
1414 LASSERT(key != NULL);
1415 LASSERT(key->lct_fini != NULL);
1416 LASSERT(atomic_read(&key->lct_used) > 1);
1418 key->lct_fini(ctx, key, ctx->lc_value[index]);
1419 lu_ref_del(&key->lct_reference, "ctx", ctx);
1420 atomic_dec(&key->lct_used);
1422 LASSERT(key->lct_owner != NULL);
1423 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1424 LINVRNT(module_refcount(key->lct_owner) > 0);
1425 module_put(key->lct_owner);
1427 ctx->lc_value[index] = NULL;
1434 void lu_context_key_degister(struct lu_context_key *key)
1436 LASSERT(atomic_read(&key->lct_used) >= 1);
1437 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1439 lu_context_key_quiesce(key);
1442 spin_lock(&lu_keys_guard);
1443 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1444 if (lu_keys[key->lct_index]) {
1445 lu_keys[key->lct_index] = NULL;
1446 lu_ref_fini(&key->lct_reference);
1448 spin_unlock(&lu_keys_guard);
1450 LASSERTF(atomic_read(&key->lct_used) == 1,
1451 "key has instances: %d\n",
1452 atomic_read(&key->lct_used));
1454 EXPORT_SYMBOL(lu_context_key_degister);
1457 * Register a number of keys. This has to be called after all keys have been
1458 * initialized by a call to LU_CONTEXT_KEY_INIT().
1460 int lu_context_key_register_many(struct lu_context_key *k, ...)
1462 struct lu_context_key *key = k;
1468 result = lu_context_key_register(key);
1471 key = va_arg(args, struct lu_context_key *);
1472 } while (key != NULL);
1478 lu_context_key_degister(k);
1479 k = va_arg(args, struct lu_context_key *);
1486 EXPORT_SYMBOL(lu_context_key_register_many);
1489 * De-register a number of keys. This is a dual to
1490 * lu_context_key_register_many().
1492 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1498 lu_context_key_degister(k);
1499 k = va_arg(args, struct lu_context_key*);
1500 } while (k != NULL);
1503 EXPORT_SYMBOL(lu_context_key_degister_many);
1506 * Revive a number of keys.
1508 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1514 lu_context_key_revive(k);
1515 k = va_arg(args, struct lu_context_key*);
1516 } while (k != NULL);
1519 EXPORT_SYMBOL(lu_context_key_revive_many);
1522 * Quiescent a number of keys.
1524 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1530 lu_context_key_quiesce(k);
1531 k = va_arg(args, struct lu_context_key*);
1532 } while (k != NULL);
1535 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1538 * Return value associated with key \a key in context \a ctx.
1540 void *lu_context_key_get(const struct lu_context *ctx,
1541 const struct lu_context_key *key)
1543 LINVRNT(ctx->lc_state == LCS_ENTERED);
1544 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1545 LASSERT(lu_keys[key->lct_index] == key);
1546 return ctx->lc_value[key->lct_index];
1548 EXPORT_SYMBOL(lu_context_key_get);
1551 * List of remembered contexts. XXX document me.
1553 static struct list_head lu_context_remembered;
1556 * Destroy \a key in all remembered contexts. This is used to destroy key
1557 * values in "shared" contexts (like service threads), when a module owning
1558 * the key is about to be unloaded.
1560 void lu_context_key_quiesce(struct lu_context_key *key)
1562 struct lu_context *ctx;
1563 extern unsigned cl_env_cache_purge(unsigned nr);
1565 if (!(key->lct_tags & LCT_QUIESCENT)) {
1567 * XXX layering violation.
1569 cl_env_cache_purge(~0);
1570 key->lct_tags |= LCT_QUIESCENT;
1572 * XXX memory barrier has to go here.
1574 spin_lock(&lu_keys_guard);
1575 list_for_each_entry(ctx, &lu_context_remembered,
1577 key_fini(ctx, key->lct_index);
1578 spin_unlock(&lu_keys_guard);
1583 void lu_context_key_revive(struct lu_context_key *key)
1585 key->lct_tags &= ~LCT_QUIESCENT;
1589 static void keys_fini(struct lu_context *ctx)
1593 if (ctx->lc_value == NULL)
1596 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1599 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1600 ctx->lc_value = NULL;
1603 static int keys_fill(struct lu_context *ctx)
1607 LINVRNT(ctx->lc_value != NULL);
1608 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1609 struct lu_context_key *key;
1612 if (ctx->lc_value[i] == NULL && key != NULL &&
1613 (key->lct_tags & ctx->lc_tags) &&
1615 * Don't create values for a LCT_QUIESCENT key, as this
1616 * will pin module owning a key.
1618 !(key->lct_tags & LCT_QUIESCENT)) {
1621 LINVRNT(key->lct_init != NULL);
1622 LINVRNT(key->lct_index == i);
1624 value = key->lct_init(ctx, key);
1625 if (unlikely(IS_ERR(value)))
1626 return PTR_ERR(value);
1628 LASSERT(key->lct_owner != NULL);
1629 if (!(ctx->lc_tags & LCT_NOREF))
1630 try_module_get(key->lct_owner);
1631 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1632 atomic_inc(&key->lct_used);
1634 * This is the only place in the code, where an
1635 * element of ctx->lc_value[] array is set to non-NULL
1638 ctx->lc_value[i] = value;
1639 if (key->lct_exit != NULL)
1640 ctx->lc_tags |= LCT_HAS_EXIT;
1642 ctx->lc_version = key_set_version;
1647 static int keys_init(struct lu_context *ctx)
1649 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1650 if (likely(ctx->lc_value != NULL))
1651 return keys_fill(ctx);
1657 * Initialize context data-structure. Create values for all keys.
1659 int lu_context_init(struct lu_context *ctx, __u32 tags)
1663 memset(ctx, 0, sizeof *ctx);
1664 ctx->lc_state = LCS_INITIALIZED;
1665 ctx->lc_tags = tags;
1666 if (tags & LCT_REMEMBER) {
1667 spin_lock(&lu_keys_guard);
1668 list_add(&ctx->lc_remember, &lu_context_remembered);
1669 spin_unlock(&lu_keys_guard);
1671 INIT_LIST_HEAD(&ctx->lc_remember);
1674 rc = keys_init(ctx);
1676 lu_context_fini(ctx);
1680 EXPORT_SYMBOL(lu_context_init);
1683 * Finalize context data-structure. Destroy key values.
1685 void lu_context_fini(struct lu_context *ctx)
1687 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1688 ctx->lc_state = LCS_FINALIZED;
1690 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1691 LASSERT(list_empty(&ctx->lc_remember));
1694 } else { /* could race with key degister */
1695 spin_lock(&lu_keys_guard);
1697 list_del_init(&ctx->lc_remember);
1698 spin_unlock(&lu_keys_guard);
1701 EXPORT_SYMBOL(lu_context_fini);
1704 * Called before entering context.
1706 void lu_context_enter(struct lu_context *ctx)
1708 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1709 ctx->lc_state = LCS_ENTERED;
1711 EXPORT_SYMBOL(lu_context_enter);
1714 * Called after exiting from \a ctx
1716 void lu_context_exit(struct lu_context *ctx)
1720 LINVRNT(ctx->lc_state == LCS_ENTERED);
1721 ctx->lc_state = LCS_LEFT;
1722 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1723 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1724 if (ctx->lc_value[i] != NULL) {
1725 struct lu_context_key *key;
1728 LASSERT(key != NULL);
1729 if (key->lct_exit != NULL)
1731 key, ctx->lc_value[i]);
1736 EXPORT_SYMBOL(lu_context_exit);
1739 * Allocate for context all missing keys that were registered after context
1740 * creation. key_set_version is only changed in rare cases when modules
1741 * are loaded and removed.
1743 int lu_context_refill(struct lu_context *ctx)
1745 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1749 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1750 * obd being added. Currently, this is only used on client side, specifically
1751 * for echo device client, for other stack (like ptlrpc threads), context are
1752 * predefined when the lu_device type are registered, during the module probe
1755 __u32 lu_context_tags_default = 0;
1756 __u32 lu_session_tags_default = 0;
1758 void lu_context_tags_update(__u32 tags)
1760 spin_lock(&lu_keys_guard);
1761 lu_context_tags_default |= tags;
1763 spin_unlock(&lu_keys_guard);
1765 EXPORT_SYMBOL(lu_context_tags_update);
1767 void lu_context_tags_clear(__u32 tags)
1769 spin_lock(&lu_keys_guard);
1770 lu_context_tags_default &= ~tags;
1772 spin_unlock(&lu_keys_guard);
1774 EXPORT_SYMBOL(lu_context_tags_clear);
1776 void lu_session_tags_update(__u32 tags)
1778 spin_lock(&lu_keys_guard);
1779 lu_session_tags_default |= tags;
1781 spin_unlock(&lu_keys_guard);
1783 EXPORT_SYMBOL(lu_session_tags_update);
1785 void lu_session_tags_clear(__u32 tags)
1787 spin_lock(&lu_keys_guard);
1788 lu_session_tags_default &= ~tags;
1790 spin_unlock(&lu_keys_guard);
1792 EXPORT_SYMBOL(lu_session_tags_clear);
1794 int lu_env_init(struct lu_env *env, __u32 tags)
1799 result = lu_context_init(&env->le_ctx, tags);
1800 if (likely(result == 0))
1801 lu_context_enter(&env->le_ctx);
1804 EXPORT_SYMBOL(lu_env_init);
1806 void lu_env_fini(struct lu_env *env)
1808 lu_context_exit(&env->le_ctx);
1809 lu_context_fini(&env->le_ctx);
1812 EXPORT_SYMBOL(lu_env_fini);
1814 int lu_env_refill(struct lu_env *env)
1818 result = lu_context_refill(&env->le_ctx);
1819 if (result == 0 && env->le_ses != NULL)
1820 result = lu_context_refill(env->le_ses);
1823 EXPORT_SYMBOL(lu_env_refill);
1826 * Currently, this API will only be used by echo client.
1827 * Because echo client and normal lustre client will share
1828 * same cl_env cache. So echo client needs to refresh
1829 * the env context after it get one from the cache, especially
1830 * when normal client and echo client co-exist in the same client.
1832 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1837 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1838 env->le_ctx.lc_version = 0;
1839 env->le_ctx.lc_tags |= ctags;
1842 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1843 env->le_ses->lc_version = 0;
1844 env->le_ses->lc_tags |= stags;
1847 result = lu_env_refill(env);
1851 EXPORT_SYMBOL(lu_env_refill_by_tags);
1853 static struct shrinker *lu_site_shrinker;
1855 typedef struct lu_site_stats{
1856 unsigned lss_populated;
1857 unsigned lss_max_search;
1862 static void lu_site_stats_get(cfs_hash_t *hs,
1863 lu_site_stats_t *stats, int populated)
1868 cfs_hash_for_each_bucket(hs, &bd, i) {
1869 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1870 struct hlist_head *hhead;
1872 cfs_hash_bd_lock(hs, &bd, 1);
1874 cfs_hash_bd_count_get(&bd) - bkt->lsb_lru_len;
1875 stats->lss_total += cfs_hash_bd_count_get(&bd);
1876 stats->lss_max_search = max((int)stats->lss_max_search,
1877 cfs_hash_bd_depmax_get(&bd));
1879 cfs_hash_bd_unlock(hs, &bd, 1);
1883 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1884 if (!hlist_empty(hhead))
1885 stats->lss_populated++;
1887 cfs_hash_bd_unlock(hs, &bd, 1);
1892 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
1893 struct shrink_control *sc)
1895 lu_site_stats_t stats;
1897 struct lu_site *tmp;
1898 unsigned long cached = 0;
1900 if (!(sc->gfp_mask & __GFP_FS))
1903 mutex_lock(&lu_sites_guard);
1904 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1905 memset(&stats, 0, sizeof(stats));
1906 lu_site_stats_get(s->ls_obj_hash, &stats, 0);
1907 cached += stats.lss_total - stats.lss_busy;
1909 mutex_unlock(&lu_sites_guard);
1911 cached = (cached / 100) * sysctl_vfs_cache_pressure;
1912 CDEBUG(D_INODE, "%ld objects cached\n", cached);
1916 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
1917 struct shrink_control *sc)
1920 struct lu_site *tmp;
1921 unsigned long remain = sc->nr_to_scan;
1924 if (!(sc->gfp_mask & __GFP_FS))
1925 /* We must not take the lu_sites_guard lock when
1926 * __GFP_FS is *not* set because of the deadlock
1927 * possibility detailed above. Additionally,
1928 * since we cannot determine the number of
1929 * objects in the cache without taking this
1930 * lock, we're in a particularly tough spot. As
1931 * a result, we'll just lie and say our cache is
1932 * empty. This _should_ be ok, as we can't
1933 * reclaim objects when __GFP_FS is *not* set
1938 mutex_lock(&lu_sites_guard);
1939 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1940 remain = lu_site_purge(&lu_shrink_env, s, remain);
1942 * Move just shrunk site to the tail of site list to
1943 * assure shrinking fairness.
1945 list_move_tail(&s->ls_linkage, &splice);
1947 list_splice(&splice, lu_sites.prev);
1948 mutex_unlock(&lu_sites_guard);
1950 return sc->nr_to_scan - remain;
1953 #ifndef HAVE_SHRINKER_COUNT
1955 * There exists a potential lock inversion deadlock scenario when using
1956 * Lustre on top of ZFS. This occurs between one of ZFS's
1957 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
1958 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
1959 * while thread B will take the ht_lock and sleep on the lu_sites_guard
1960 * lock. Obviously neither thread will wake and drop their respective hold
1963 * To prevent this from happening we must ensure the lu_sites_guard lock is
1964 * not taken while down this code path. ZFS reliably does not set the
1965 * __GFP_FS bit in its code paths, so this can be used to determine if it
1966 * is safe to take the lu_sites_guard lock.
1968 * Ideally we should accurately return the remaining number of cached
1969 * objects without taking the lu_sites_guard lock, but this is not
1970 * possible in the current implementation.
1972 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
1975 struct shrink_control scv = {
1976 .nr_to_scan = shrink_param(sc, nr_to_scan),
1977 .gfp_mask = shrink_param(sc, gfp_mask)
1979 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
1980 struct shrinker* shrinker = NULL;
1984 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
1986 lu_cache_shrink_scan(shrinker, &scv);
1988 cached = lu_cache_shrink_count(shrinker, &scv);
1989 if (scv.nr_to_scan == 0)
1990 CDEBUG(D_INODE, "%d objects cached\n", cached);
1994 #endif /* HAVE_SHRINKER_COUNT */
2002 * Environment to be used in debugger, contains all tags.
2004 static struct lu_env lu_debugging_env;
2007 * Debugging printer function using printk().
2009 int lu_printk_printer(const struct lu_env *env,
2010 void *unused, const char *format, ...)
2014 va_start(args, format);
2015 vprintk(format, args);
2020 int lu_debugging_setup(void)
2022 return lu_env_init(&lu_debugging_env, ~0);
2025 void lu_context_keys_dump(void)
2029 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2030 struct lu_context_key *key;
2034 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2035 i, key, key->lct_tags,
2036 key->lct_init, key->lct_fini, key->lct_exit,
2037 key->lct_index, atomic_read(&key->lct_used),
2038 key->lct_owner ? key->lct_owner->name : "",
2040 lu_ref_print(&key->lct_reference);
2046 * Initialization of global lu_* data.
2048 int lu_global_init(void)
2051 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2052 lu_cache_shrink_count, lu_cache_shrink_scan);
2054 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2056 INIT_LIST_HEAD(&lu_device_types);
2057 INIT_LIST_HEAD(&lu_context_remembered);
2058 INIT_LIST_HEAD(&lu_sites);
2060 result = lu_ref_global_init();
2064 LU_CONTEXT_KEY_INIT(&lu_global_key);
2065 result = lu_context_key_register(&lu_global_key);
2070 * At this level, we don't know what tags are needed, so allocate them
2071 * conservatively. This should not be too bad, because this
2072 * environment is global.
2074 mutex_lock(&lu_sites_guard);
2075 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2076 mutex_unlock(&lu_sites_guard);
2081 * seeks estimation: 3 seeks to read a record from oi, one to read
2082 * inode, one for ea. Unfortunately setting this high value results in
2083 * lu_object/inode cache consuming all the memory.
2085 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2086 if (lu_site_shrinker == NULL)
2093 * Dual to lu_global_init().
2095 void lu_global_fini(void)
2097 if (lu_site_shrinker != NULL) {
2098 remove_shrinker(lu_site_shrinker);
2099 lu_site_shrinker = NULL;
2102 lu_context_key_degister(&lu_global_key);
2105 * Tear shrinker environment down _after_ de-registering
2106 * lu_global_key, because the latter has a value in the former.
2108 mutex_lock(&lu_sites_guard);
2109 lu_env_fini(&lu_shrink_env);
2110 mutex_unlock(&lu_sites_guard);
2112 lu_ref_global_fini();
2115 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2117 #ifdef CONFIG_PROC_FS
2118 struct lprocfs_counter ret;
2120 lprocfs_stats_collect(stats, idx, &ret);
2121 return (__u32)ret.lc_count;
2128 * Output site statistical counters into a buffer. Suitable for
2129 * lprocfs_rd_*()-style functions.
2131 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2133 lu_site_stats_t stats;
2135 memset(&stats, 0, sizeof(stats));
2136 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2138 return seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2141 stats.lss_populated,
2142 CFS_HASH_NHLIST(s->ls_obj_hash),
2143 stats.lss_max_search,
2144 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2145 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2146 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2147 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2148 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2149 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2151 EXPORT_SYMBOL(lu_site_stats_seq_print);
2153 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
2155 lu_site_stats_t stats;
2157 memset(&stats, 0, sizeof(stats));
2158 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
2160 return snprintf(page, count, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2163 stats.lss_populated,
2164 CFS_HASH_NHLIST(s->ls_obj_hash),
2165 stats.lss_max_search,
2166 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2167 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2168 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2169 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2170 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2171 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2175 * Helper function to initialize a number of kmem slab caches at once.
2177 int lu_kmem_init(struct lu_kmem_descr *caches)
2180 struct lu_kmem_descr *iter = caches;
2182 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2183 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2186 if (*iter->ckd_cache == NULL) {
2188 /* free all previously allocated caches */
2189 lu_kmem_fini(caches);
2195 EXPORT_SYMBOL(lu_kmem_init);
2198 * Helper function to finalize a number of kmem slab cached at once. Dual to
2201 void lu_kmem_fini(struct lu_kmem_descr *caches)
2203 for (; caches->ckd_cache != NULL; ++caches) {
2204 if (*caches->ckd_cache != NULL) {
2205 kmem_cache_destroy(*caches->ckd_cache);
2206 *caches->ckd_cache = NULL;
2210 EXPORT_SYMBOL(lu_kmem_fini);
2213 * Temporary solution to be able to assign fid in ->do_create()
2214 * till we have fully-functional OST fids
2216 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2217 const struct lu_fid *fid)
2219 struct lu_site *s = o->lo_dev->ld_site;
2220 struct lu_fid *old = &o->lo_header->loh_fid;
2221 struct lu_object *shadow;
2222 wait_queue_t waiter;
2227 LASSERT(fid_is_zero(old));
2229 hs = s->ls_obj_hash;
2230 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2231 shadow = htable_lookup(s, &bd, fid, &waiter, &version);
2232 /* supposed to be unique */
2233 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2235 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2236 cfs_hash_bd_unlock(hs, &bd, 1);
2238 EXPORT_SYMBOL(lu_object_assign_fid);
2241 * allocates object with 0 (non-assiged) fid
2242 * XXX: temporary solution to be able to assign fid in ->do_create()
2243 * till we have fully-functional OST fids
2245 struct lu_object *lu_object_anon(const struct lu_env *env,
2246 struct lu_device *dev,
2247 const struct lu_object_conf *conf)
2250 struct lu_object *o;
2253 o = lu_object_alloc(env, dev, &fid, conf);
2257 EXPORT_SYMBOL(lu_object_anon);
2259 struct lu_buf LU_BUF_NULL = {
2263 EXPORT_SYMBOL(LU_BUF_NULL);
2265 void lu_buf_free(struct lu_buf *buf)
2269 LASSERT(buf->lb_len > 0);
2270 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2275 EXPORT_SYMBOL(lu_buf_free);
2277 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2280 LASSERT(buf->lb_buf == NULL);
2281 LASSERT(buf->lb_len == 0);
2282 OBD_ALLOC_LARGE(buf->lb_buf, size);
2283 if (likely(buf->lb_buf))
2286 EXPORT_SYMBOL(lu_buf_alloc);
2288 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2291 lu_buf_alloc(buf, size);
2293 EXPORT_SYMBOL(lu_buf_realloc);
2295 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2297 if (buf->lb_buf == NULL && buf->lb_len == 0)
2298 lu_buf_alloc(buf, len);
2300 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2301 lu_buf_realloc(buf, len);
2305 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2308 * Increase the size of the \a buf.
2309 * preserves old data in buffer
2310 * old buffer remains unchanged on error
2311 * \retval 0 or -ENOMEM
2313 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2317 if (len <= buf->lb_len)
2320 OBD_ALLOC_LARGE(ptr, len);
2324 /* Free the old buf */
2325 if (buf->lb_buf != NULL) {
2326 memcpy(ptr, buf->lb_buf, buf->lb_len);
2327 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);