4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/obdclass/lu_object.c
35 * These are the only exported functions, they provide some generic
36 * infrastructure for managing object devices
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_CLASS
43 #include <linux/module.h>
44 #include <linux/list.h>
45 #ifdef HAVE_PROCESSOR_H
46 #include <linux/processor.h>
48 #include <libcfs/linux/processor.h>
51 #include <libcfs/libcfs.h>
52 #include <libcfs/libcfs_hash.h> /* hash_long() */
53 #include <libcfs/linux/linux-mem.h>
54 #include <obd_class.h>
55 #include <obd_support.h>
56 #include <lustre_disk.h>
57 #include <lustre_fid.h>
58 #include <lu_object.h>
61 struct lu_site_bkt_data {
63 * LRU list, updated on each access to object. Protected by
64 * bucket lock of lu_site::ls_obj_hash.
66 * "Cold" end of LRU is lu_site::ls_lru.next. Accessed object are
67 * moved to the lu_site::ls_lru.prev (this is due to the non-existence
68 * of list_for_each_entry_safe_reverse()).
70 struct list_head lsb_lru;
72 * Wait-queue signaled when an object in this site is ultimately
73 * destroyed (lu_object_free()). It is used by lu_object_find() to
74 * wait before re-trying when object in the process of destruction is
75 * found in the hash table.
77 * \see htable_lookup().
79 wait_queue_head_t lsb_marche_funebre;
83 LU_CACHE_PERCENT_MAX = 50,
84 LU_CACHE_PERCENT_DEFAULT = 20
87 #define LU_CACHE_NR_MAX_ADJUST 512
88 #define LU_CACHE_NR_UNLIMITED -1
89 #define LU_CACHE_NR_DEFAULT LU_CACHE_NR_UNLIMITED
90 #define LU_CACHE_NR_LDISKFS_LIMIT LU_CACHE_NR_UNLIMITED
91 /** This is set to roughly (20 * OSS_NTHRS_MAX) to prevent thrashing */
92 #define LU_CACHE_NR_ZFS_LIMIT 10240
94 #define LU_SITE_BITS_MIN 12
95 #define LU_SITE_BITS_MAX 24
96 #define LU_SITE_BITS_MAX_CL 19
98 * total 256 buckets, we don't want too many buckets because:
99 * - consume too much memory
100 * - avoid unbalanced LRU list
102 #define LU_SITE_BKT_BITS 8
105 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
106 module_param(lu_cache_percent, int, 0644);
107 MODULE_PARM_DESC(lu_cache_percent, "Percentage of memory to be used as lu_object cache");
109 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
110 module_param(lu_cache_nr, long, 0644);
111 MODULE_PARM_DESC(lu_cache_nr, "Maximum number of objects in lu_object cache");
113 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
114 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx);
117 lu_site_wq_from_fid(struct lu_site *site, struct lu_fid *fid)
119 struct cfs_hash_bd bd;
120 struct lu_site_bkt_data *bkt;
122 cfs_hash_bd_get(site->ls_obj_hash, fid, &bd);
123 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
124 return &bkt->lsb_marche_funebre;
126 EXPORT_SYMBOL(lu_site_wq_from_fid);
129 * Decrease reference counter on object. If last reference is freed, return
130 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
131 * case, free object immediately.
133 void lu_object_put(const struct lu_env *env, struct lu_object *o)
135 struct lu_site_bkt_data *bkt;
136 struct lu_object_header *top;
137 struct lu_site *site;
138 struct lu_object *orig;
139 struct cfs_hash_bd bd;
140 const struct lu_fid *fid;
143 site = o->lo_dev->ld_site;
147 * till we have full fids-on-OST implemented anonymous objects
148 * are possible in OSP. such an object isn't listed in the site
149 * so we should not remove it from the site.
151 fid = lu_object_fid(o);
152 if (fid_is_zero(fid)) {
153 LASSERT(top->loh_hash.next == NULL
154 && top->loh_hash.pprev == NULL);
155 LASSERT(list_empty(&top->loh_lru));
156 if (!atomic_dec_and_test(&top->loh_ref))
158 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
159 if (o->lo_ops->loo_object_release != NULL)
160 o->lo_ops->loo_object_release(env, o);
162 lu_object_free(env, orig);
166 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
167 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
169 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
170 if (lu_object_is_dying(top)) {
172 * somebody may be waiting for this, currently only
173 * used for cl_object, see cl_object_put_last().
175 wake_up_all(&bkt->lsb_marche_funebre);
181 * When last reference is released, iterate over object
182 * layers, and notify them that object is no longer busy.
184 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
185 if (o->lo_ops->loo_object_release != NULL)
186 o->lo_ops->loo_object_release(env, o);
189 if (!lu_object_is_dying(top) &&
190 (lu_object_exists(orig) || lu_object_is_cl(orig))) {
191 LASSERT(list_empty(&top->loh_lru));
192 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
193 percpu_counter_inc(&site->ls_lru_len_counter);
194 CDEBUG(D_INODE, "Add %p/%p to site lru. hash: %p, bkt: %p\n",
195 orig, top, site->ls_obj_hash, bkt);
196 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
201 * If object is dying (will not be cached) then remove it
202 * from hash table and LRU.
204 * This is done with hash table and LRU lists locked. As the only
205 * way to acquire first reference to previously unreferenced
206 * object is through hash-table lookup (lu_object_find()),
207 * or LRU scanning (lu_site_purge()), that are done under hash-table
208 * and LRU lock, no race with concurrent object lookup is possible
209 * and we can safely destroy object below.
211 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
212 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
213 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
215 * Object was already removed from hash and lru above, can
218 lu_object_free(env, orig);
220 EXPORT_SYMBOL(lu_object_put);
223 * Put object and don't keep in cache. This is temporary solution for
224 * multi-site objects when its layering is not constant.
226 void lu_object_put_nocache(const struct lu_env *env, struct lu_object *o)
228 set_bit(LU_OBJECT_HEARD_BANSHEE, &o->lo_header->loh_flags);
229 return lu_object_put(env, o);
231 EXPORT_SYMBOL(lu_object_put_nocache);
234 * Kill the object and take it out of LRU cache.
235 * Currently used by client code for layout change.
237 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
239 struct lu_object_header *top;
242 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
243 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
244 struct lu_site *site = o->lo_dev->ld_site;
245 struct cfs_hash *obj_hash = site->ls_obj_hash;
246 struct cfs_hash_bd bd;
248 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
249 if (!list_empty(&top->loh_lru)) {
250 struct lu_site_bkt_data *bkt;
252 list_del_init(&top->loh_lru);
253 bkt = cfs_hash_bd_extra_get(obj_hash, &bd);
254 percpu_counter_dec(&site->ls_lru_len_counter);
256 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
257 cfs_hash_bd_unlock(obj_hash, &bd, 1);
260 EXPORT_SYMBOL(lu_object_unhash);
263 * Allocate new object.
265 * This follows object creation protocol, described in the comment within
266 * struct lu_device_operations definition.
268 static struct lu_object *lu_object_alloc(const struct lu_env *env,
269 struct lu_device *dev,
270 const struct lu_fid *f,
271 const struct lu_object_conf *conf)
273 struct lu_object *scan;
274 struct lu_object *top;
275 struct list_head *layers;
276 unsigned int init_mask = 0;
277 unsigned int init_flag;
283 * Create top-level object slice. This will also create
286 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
288 RETURN(ERR_PTR(-ENOMEM));
292 * This is the only place where object fid is assigned. It's constant
295 top->lo_header->loh_fid = *f;
296 layers = &top->lo_header->loh_layers;
300 * Call ->loo_object_init() repeatedly, until no more new
301 * object slices are created.
305 list_for_each_entry(scan, layers, lo_linkage) {
306 if (init_mask & init_flag)
309 scan->lo_header = top->lo_header;
310 result = scan->lo_ops->loo_object_init(env, scan, conf);
312 lu_object_free(env, top);
313 RETURN(ERR_PTR(result));
315 init_mask |= init_flag;
321 list_for_each_entry_reverse(scan, layers, lo_linkage) {
322 if (scan->lo_ops->loo_object_start != NULL) {
323 result = scan->lo_ops->loo_object_start(env, scan);
325 lu_object_free(env, top);
326 RETURN(ERR_PTR(result));
331 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
338 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
340 wait_queue_head_t *wq;
341 struct lu_site *site;
342 struct lu_object *scan;
343 struct list_head *layers;
344 struct list_head splice;
346 site = o->lo_dev->ld_site;
347 layers = &o->lo_header->loh_layers;
348 wq = lu_site_wq_from_fid(site, &o->lo_header->loh_fid);
350 * First call ->loo_object_delete() method to release all resources.
352 list_for_each_entry_reverse(scan, layers, lo_linkage) {
353 if (scan->lo_ops->loo_object_delete != NULL)
354 scan->lo_ops->loo_object_delete(env, scan);
358 * Then, splice object layers into stand-alone list, and call
359 * ->loo_object_free() on all layers to free memory. Splice is
360 * necessary, because lu_object_header is freed together with the
363 INIT_LIST_HEAD(&splice);
364 list_splice_init(layers, &splice);
365 while (!list_empty(&splice)) {
367 * Free layers in bottom-to-top order, so that object header
368 * lives as long as possible and ->loo_object_free() methods
369 * can look at its contents.
371 o = container_of0(splice.prev, struct lu_object, lo_linkage);
372 list_del_init(&o->lo_linkage);
373 LASSERT(o->lo_ops->loo_object_free != NULL);
374 o->lo_ops->loo_object_free(env, o);
377 if (waitqueue_active(wq))
382 * Free \a nr objects from the cold end of the site LRU list.
383 * if canblock is 0, then don't block awaiting for another
384 * instance of lu_site_purge() to complete
386 int lu_site_purge_objects(const struct lu_env *env, struct lu_site *s,
387 int nr, int canblock)
389 struct lu_object_header *h;
390 struct lu_object_header *temp;
391 struct lu_site_bkt_data *bkt;
392 struct cfs_hash_bd bd;
393 struct cfs_hash_bd bd2;
394 struct list_head dispose;
396 unsigned int start = 0;
401 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
404 INIT_LIST_HEAD(&dispose);
406 * Under LRU list lock, scan LRU list and move unreferenced objects to
407 * the dispose list, removing them from LRU and hash table.
410 start = s->ls_purge_start;
411 bnr = (nr == ~0) ? -1 : nr / (int)CFS_HASH_NBKT(s->ls_obj_hash) + 1;
414 * It doesn't make any sense to make purge threads parallel, that can
415 * only bring troubles to us. See LU-5331.
418 mutex_lock(&s->ls_purge_mutex);
419 else if (mutex_trylock(&s->ls_purge_mutex) == 0)
423 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
427 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
428 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
430 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
431 LASSERT(atomic_read(&h->loh_ref) == 0);
433 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
434 LASSERT(bd.bd_bucket == bd2.bd_bucket);
436 cfs_hash_bd_del_locked(s->ls_obj_hash,
438 list_move(&h->loh_lru, &dispose);
439 percpu_counter_dec(&s->ls_lru_len_counter);
443 if (nr != ~0 && --nr == 0)
446 if (count > 0 && --count == 0)
450 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
453 * Free everything on the dispose list. This is safe against
454 * races due to the reasons described in lu_object_put().
456 while (!list_empty(&dispose)) {
457 h = container_of0(dispose.next,
458 struct lu_object_header, loh_lru);
459 list_del_init(&h->loh_lru);
460 lu_object_free(env, lu_object_top(h));
461 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
467 mutex_unlock(&s->ls_purge_mutex);
469 if (nr != 0 && did_sth && start != 0) {
470 start = 0; /* restart from the first bucket */
473 /* race on s->ls_purge_start, but nobody cares */
474 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
479 EXPORT_SYMBOL(lu_site_purge_objects);
484 * Code below has to jump through certain loops to output object description
485 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
486 * composes object description from strings that are parts of _lines_ of
487 * output (i.e., strings that are not terminated by newline). This doesn't fit
488 * very well into libcfs_debug_msg() interface that assumes that each message
489 * supplied to it is a self-contained output line.
491 * To work around this, strings are collected in a temporary buffer
492 * (implemented as a value of lu_cdebug_key key), until terminating newline
493 * character is detected.
501 * XXX overflow is not handled correctly.
506 struct lu_cdebug_data {
510 char lck_area[LU_CDEBUG_LINE];
513 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
514 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
517 * Key, holding temporary buffer. This key is registered very early by
520 static struct lu_context_key lu_global_key = {
521 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
522 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
523 .lct_init = lu_global_key_init,
524 .lct_fini = lu_global_key_fini
528 * Printer function emitting messages through libcfs_debug_msg().
530 int lu_cdebug_printer(const struct lu_env *env,
531 void *cookie, const char *format, ...)
533 struct libcfs_debug_msg_data *msgdata = cookie;
534 struct lu_cdebug_data *key;
539 va_start(args, format);
541 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
542 LASSERT(key != NULL);
544 used = strlen(key->lck_area);
545 complete = format[strlen(format) - 1] == '\n';
547 * Append new chunk to the buffer.
549 vsnprintf(key->lck_area + used,
550 ARRAY_SIZE(key->lck_area) - used, format, args);
552 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
553 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
554 key->lck_area[0] = 0;
559 EXPORT_SYMBOL(lu_cdebug_printer);
562 * Print object header.
564 void lu_object_header_print(const struct lu_env *env, void *cookie,
565 lu_printer_t printer,
566 const struct lu_object_header *hdr)
568 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
569 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
571 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
572 list_empty((struct list_head *)&hdr->loh_lru) ? \
574 hdr->loh_attr & LOHA_EXISTS ? " exist" : "");
576 EXPORT_SYMBOL(lu_object_header_print);
579 * Print human readable representation of the \a o to the \a printer.
581 void lu_object_print(const struct lu_env *env, void *cookie,
582 lu_printer_t printer, const struct lu_object *o)
584 static const char ruler[] = "........................................";
585 struct lu_object_header *top;
589 lu_object_header_print(env, cookie, printer, top);
590 (*printer)(env, cookie, "{\n");
592 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
594 * print `.' \a depth times followed by type name and address
596 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
597 o->lo_dev->ld_type->ldt_name, o);
599 if (o->lo_ops->loo_object_print != NULL)
600 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
602 (*printer)(env, cookie, "\n");
605 (*printer)(env, cookie, "} header@%p\n", top);
607 EXPORT_SYMBOL(lu_object_print);
610 * Check object consistency.
612 int lu_object_invariant(const struct lu_object *o)
614 struct lu_object_header *top;
617 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
618 if (o->lo_ops->loo_object_invariant != NULL &&
619 !o->lo_ops->loo_object_invariant(o))
625 static struct lu_object *htable_lookup(struct lu_site *s,
626 struct cfs_hash_bd *bd,
627 const struct lu_fid *f,
630 struct lu_site_bkt_data *bkt;
631 struct lu_object_header *h;
632 struct hlist_node *hnode;
633 __u64 ver = cfs_hash_bd_version_get(bd);
636 return ERR_PTR(-ENOENT);
639 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
640 /* cfs_hash_bd_peek_locked is a somehow "internal" function
641 * of cfs_hash, it doesn't add refcount on object. */
642 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
644 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
645 return ERR_PTR(-ENOENT);
648 h = container_of0(hnode, struct lu_object_header, loh_hash);
649 cfs_hash_get(s->ls_obj_hash, hnode);
650 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
651 if (!list_empty(&h->loh_lru)) {
652 list_del_init(&h->loh_lru);
653 percpu_counter_dec(&s->ls_lru_len_counter);
655 return lu_object_top(h);
659 * Search cache for an object with the fid \a f. If such object is found,
660 * return it. Otherwise, create new object, insert it into cache and return
661 * it. In any case, additional reference is acquired on the returned object.
663 struct lu_object *lu_object_find(const struct lu_env *env,
664 struct lu_device *dev, const struct lu_fid *f,
665 const struct lu_object_conf *conf)
667 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
669 EXPORT_SYMBOL(lu_object_find);
672 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
673 * the calculation for the number of objects to reclaim is not covered by
674 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
675 * This ensures that many concurrent threads will not accidentally purge
678 static void lu_object_limit(const struct lu_env *env,
679 struct lu_device *dev)
683 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
686 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
687 nr = (__u64)lu_cache_nr;
691 lu_site_purge_objects(env, dev->ld_site,
692 MIN(size - nr, LU_CACHE_NR_MAX_ADJUST), 0);
696 * Core logic of lu_object_find*() functions.
698 * Much like lu_object_find(), but top level device of object is specifically
699 * \a dev rather than top level device of the site. This interface allows
700 * objects of different "stacking" to be created within the same site.
702 struct lu_object *lu_object_find_at(const struct lu_env *env,
703 struct lu_device *dev,
704 const struct lu_fid *f,
705 const struct lu_object_conf *conf)
708 struct lu_object *shadow;
711 struct cfs_hash_bd bd;
715 * This uses standard index maintenance protocol:
717 * - search index under lock, and return object if found;
718 * - otherwise, unlock index, allocate new object;
719 * - lock index and search again;
720 * - if nothing is found (usual case), insert newly created
722 * - otherwise (race: other thread inserted object), free
723 * object just allocated.
727 * For "LOC_F_NEW" case, we are sure the object is new established.
728 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
729 * just alloc and insert directly.
734 cfs_hash_bd_get(hs, f, &bd);
735 if (!(conf && conf->loc_flags & LOC_F_NEW)) {
736 cfs_hash_bd_lock(hs, &bd, 1);
737 o = htable_lookup(s, &bd, f, &version);
738 cfs_hash_bd_unlock(hs, &bd, 1);
740 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
744 * Allocate new object. This may result in rather complicated
745 * operations, including fld queries, inode loading, etc.
747 o = lu_object_alloc(env, dev, f, conf);
751 LASSERT(lu_fid_eq(lu_object_fid(o), f));
753 cfs_hash_bd_lock(hs, &bd, 1);
755 if (conf && conf->loc_flags & LOC_F_NEW)
756 shadow = ERR_PTR(-ENOENT);
758 shadow = htable_lookup(s, &bd, f, &version);
759 if (likely(PTR_ERR(shadow) == -ENOENT)) {
760 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
761 cfs_hash_bd_unlock(hs, &bd, 1);
763 lu_object_limit(env, dev);
768 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
769 cfs_hash_bd_unlock(hs, &bd, 1);
770 lu_object_free(env, o);
773 EXPORT_SYMBOL(lu_object_find_at);
776 * Find object with given fid, and return its slice belonging to given device.
778 struct lu_object *lu_object_find_slice(const struct lu_env *env,
779 struct lu_device *dev,
780 const struct lu_fid *f,
781 const struct lu_object_conf *conf)
783 struct lu_object *top;
784 struct lu_object *obj;
786 top = lu_object_find(env, dev, f, conf);
790 obj = lu_object_locate(top->lo_header, dev->ld_type);
791 if (unlikely(obj == NULL)) {
792 lu_object_put(env, top);
793 obj = ERR_PTR(-ENOENT);
798 EXPORT_SYMBOL(lu_object_find_slice);
800 int lu_device_type_init(struct lu_device_type *ldt)
804 atomic_set(&ldt->ldt_device_nr, 0);
805 if (ldt->ldt_ops->ldto_init)
806 result = ldt->ldt_ops->ldto_init(ldt);
810 EXPORT_SYMBOL(lu_device_type_init);
812 void lu_device_type_fini(struct lu_device_type *ldt)
814 if (ldt->ldt_ops->ldto_fini)
815 ldt->ldt_ops->ldto_fini(ldt);
817 EXPORT_SYMBOL(lu_device_type_fini);
820 * Global list of all sites on this node
822 static LIST_HEAD(lu_sites);
823 static DECLARE_RWSEM(lu_sites_guard);
826 * Global environment used by site shrinker.
828 static struct lu_env lu_shrink_env;
830 struct lu_site_print_arg {
831 struct lu_env *lsp_env;
833 lu_printer_t lsp_printer;
837 lu_site_obj_print(struct cfs_hash *hs, struct cfs_hash_bd *bd,
838 struct hlist_node *hnode, void *data)
840 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
841 struct lu_object_header *h;
843 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
844 if (!list_empty(&h->loh_layers)) {
845 const struct lu_object *o;
847 o = lu_object_top(h);
848 lu_object_print(arg->lsp_env, arg->lsp_cookie,
849 arg->lsp_printer, o);
851 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
852 arg->lsp_printer, h);
858 * Print all objects in \a s.
860 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
861 lu_printer_t printer)
863 struct lu_site_print_arg arg = {
864 .lsp_env = (struct lu_env *)env,
865 .lsp_cookie = cookie,
866 .lsp_printer = printer,
869 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
871 EXPORT_SYMBOL(lu_site_print);
874 * Return desired hash table order.
876 static unsigned long lu_htable_order(struct lu_device *top)
878 unsigned long cache_size;
880 unsigned long bits_max = LU_SITE_BITS_MAX;
883 * For ZFS based OSDs the cache should be disabled by default. This
884 * allows the ZFS ARC maximum flexibility in determining what buffers
885 * to cache. If Lustre has objects or buffer which it wants to ensure
886 * always stay cached it must maintain a hold on them.
888 if (strcmp(top->ld_type->ldt_name, LUSTRE_OSD_ZFS_NAME) == 0) {
889 lu_cache_percent = 1;
890 lu_cache_nr = LU_CACHE_NR_ZFS_LIMIT;
891 return LU_SITE_BITS_MIN;
894 if (strcmp(top->ld_type->ldt_name, LUSTRE_VVP_NAME) == 0)
895 bits_max = LU_SITE_BITS_MAX_CL;
898 * Calculate hash table size, assuming that we want reasonable
899 * performance when 20% of total memory is occupied by cache of
902 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
904 cache_size = totalram_pages;
906 #if BITS_PER_LONG == 32
907 /* limit hashtable size for lowmem systems to low RAM */
908 if (cache_size > 1 << (30 - PAGE_SHIFT))
909 cache_size = 1 << (30 - PAGE_SHIFT) * 3 / 4;
912 /* clear off unreasonable cache setting. */
913 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
914 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in"
915 " the range of (0, %u]. Will use default value: %u.\n",
916 lu_cache_percent, LU_CACHE_PERCENT_MAX,
917 LU_CACHE_PERCENT_DEFAULT);
919 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
921 cache_size = cache_size / 100 * lu_cache_percent *
924 for (bits = 1; (1 << bits) < cache_size; ++bits) {
928 return clamp_t(typeof(bits), bits, LU_SITE_BITS_MIN, bits_max);
931 static unsigned lu_obj_hop_hash(struct cfs_hash *hs,
932 const void *key, unsigned mask)
934 struct lu_fid *fid = (struct lu_fid *)key;
937 hash = fid_flatten32(fid);
938 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
939 hash = hash_long(hash, hs->hs_bkt_bits);
941 /* give me another random factor */
942 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
944 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
945 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
950 static void *lu_obj_hop_object(struct hlist_node *hnode)
952 return hlist_entry(hnode, struct lu_object_header, loh_hash);
955 static void *lu_obj_hop_key(struct hlist_node *hnode)
957 struct lu_object_header *h;
959 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
963 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
965 struct lu_object_header *h;
967 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
968 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
971 static void lu_obj_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
973 struct lu_object_header *h;
975 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
976 atomic_inc(&h->loh_ref);
979 static void lu_obj_hop_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
981 LBUG(); /* we should never called it */
984 static struct cfs_hash_ops lu_site_hash_ops = {
985 .hs_hash = lu_obj_hop_hash,
986 .hs_key = lu_obj_hop_key,
987 .hs_keycmp = lu_obj_hop_keycmp,
988 .hs_object = lu_obj_hop_object,
989 .hs_get = lu_obj_hop_get,
990 .hs_put_locked = lu_obj_hop_put_locked,
993 void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
995 spin_lock(&s->ls_ld_lock);
996 if (list_empty(&d->ld_linkage))
997 list_add(&d->ld_linkage, &s->ls_ld_linkage);
998 spin_unlock(&s->ls_ld_lock);
1000 EXPORT_SYMBOL(lu_dev_add_linkage);
1002 void lu_dev_del_linkage(struct lu_site *s, struct lu_device *d)
1004 spin_lock(&s->ls_ld_lock);
1005 list_del_init(&d->ld_linkage);
1006 spin_unlock(&s->ls_ld_lock);
1008 EXPORT_SYMBOL(lu_dev_del_linkage);
1011 * Initialize site \a s, with \a d as the top level device.
1013 int lu_site_init(struct lu_site *s, struct lu_device *top)
1015 struct lu_site_bkt_data *bkt;
1016 struct cfs_hash_bd bd;
1023 memset(s, 0, sizeof *s);
1024 mutex_init(&s->ls_purge_mutex);
1026 #ifdef HAVE_PERCPU_COUNTER_INIT_GFP_FLAG
1027 rc = percpu_counter_init(&s->ls_lru_len_counter, 0, GFP_NOFS);
1029 rc = percpu_counter_init(&s->ls_lru_len_counter, 0);
1034 snprintf(name, sizeof(name), "lu_site_%s", top->ld_type->ldt_name);
1035 for (bits = lu_htable_order(top);
1036 bits >= LU_SITE_BITS_MIN; bits--) {
1037 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
1038 bits - LU_SITE_BKT_BITS,
1041 CFS_HASH_SPIN_BKTLOCK |
1042 CFS_HASH_NO_ITEMREF |
1044 CFS_HASH_ASSERT_EMPTY |
1046 if (s->ls_obj_hash != NULL)
1050 if (s->ls_obj_hash == NULL) {
1051 CERROR("failed to create lu_site hash with bits: %lu\n", bits);
1055 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1056 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1057 INIT_LIST_HEAD(&bkt->lsb_lru);
1058 init_waitqueue_head(&bkt->lsb_marche_funebre);
1061 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1062 if (s->ls_stats == NULL) {
1063 cfs_hash_putref(s->ls_obj_hash);
1064 s->ls_obj_hash = NULL;
1068 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1069 0, "created", "created");
1070 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1071 0, "cache_hit", "cache_hit");
1072 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1073 0, "cache_miss", "cache_miss");
1074 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1075 0, "cache_race", "cache_race");
1076 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1077 0, "cache_death_race", "cache_death_race");
1078 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1079 0, "lru_purged", "lru_purged");
1081 INIT_LIST_HEAD(&s->ls_linkage);
1082 s->ls_top_dev = top;
1085 lu_ref_add(&top->ld_reference, "site-top", s);
1087 INIT_LIST_HEAD(&s->ls_ld_linkage);
1088 spin_lock_init(&s->ls_ld_lock);
1090 lu_dev_add_linkage(s, top);
1094 EXPORT_SYMBOL(lu_site_init);
1097 * Finalize \a s and release its resources.
1099 void lu_site_fini(struct lu_site *s)
1101 down_write(&lu_sites_guard);
1102 list_del_init(&s->ls_linkage);
1103 up_write(&lu_sites_guard);
1105 percpu_counter_destroy(&s->ls_lru_len_counter);
1107 if (s->ls_obj_hash != NULL) {
1108 cfs_hash_putref(s->ls_obj_hash);
1109 s->ls_obj_hash = NULL;
1112 if (s->ls_top_dev != NULL) {
1113 s->ls_top_dev->ld_site = NULL;
1114 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1115 lu_device_put(s->ls_top_dev);
1116 s->ls_top_dev = NULL;
1119 if (s->ls_stats != NULL)
1120 lprocfs_free_stats(&s->ls_stats);
1122 EXPORT_SYMBOL(lu_site_fini);
1125 * Called when initialization of stack for this site is completed.
1127 int lu_site_init_finish(struct lu_site *s)
1130 down_write(&lu_sites_guard);
1131 result = lu_context_refill(&lu_shrink_env.le_ctx);
1133 list_add(&s->ls_linkage, &lu_sites);
1134 up_write(&lu_sites_guard);
1137 EXPORT_SYMBOL(lu_site_init_finish);
1140 * Acquire additional reference on device \a d
1142 void lu_device_get(struct lu_device *d)
1144 atomic_inc(&d->ld_ref);
1146 EXPORT_SYMBOL(lu_device_get);
1149 * Release reference on device \a d.
1151 void lu_device_put(struct lu_device *d)
1153 LASSERT(atomic_read(&d->ld_ref) > 0);
1154 atomic_dec(&d->ld_ref);
1156 EXPORT_SYMBOL(lu_device_put);
1159 * Initialize device \a d of type \a t.
1161 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1163 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1164 t->ldt_ops->ldto_start != NULL)
1165 t->ldt_ops->ldto_start(t);
1167 memset(d, 0, sizeof *d);
1169 lu_ref_init(&d->ld_reference);
1170 INIT_LIST_HEAD(&d->ld_linkage);
1174 EXPORT_SYMBOL(lu_device_init);
1177 * Finalize device \a d.
1179 void lu_device_fini(struct lu_device *d)
1181 struct lu_device_type *t = d->ld_type;
1183 if (d->ld_obd != NULL) {
1184 d->ld_obd->obd_lu_dev = NULL;
1188 lu_ref_fini(&d->ld_reference);
1189 LASSERTF(atomic_read(&d->ld_ref) == 0,
1190 "Refcount is %u\n", atomic_read(&d->ld_ref));
1191 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1193 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1194 t->ldt_ops->ldto_stop != NULL)
1195 t->ldt_ops->ldto_stop(t);
1197 EXPORT_SYMBOL(lu_device_fini);
1200 * Initialize object \a o that is part of compound object \a h and was created
1203 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1204 struct lu_device *d)
1206 memset(o, 0, sizeof(*o));
1210 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1211 INIT_LIST_HEAD(&o->lo_linkage);
1215 EXPORT_SYMBOL(lu_object_init);
1218 * Finalize object and release its resources.
1220 void lu_object_fini(struct lu_object *o)
1222 struct lu_device *dev = o->lo_dev;
1224 LASSERT(list_empty(&o->lo_linkage));
1227 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1233 EXPORT_SYMBOL(lu_object_fini);
1236 * Add object \a o as first layer of compound object \a h
1238 * This is typically called by the ->ldo_object_alloc() method of top-level
1241 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1243 list_move(&o->lo_linkage, &h->loh_layers);
1245 EXPORT_SYMBOL(lu_object_add_top);
1248 * Add object \a o as a layer of compound object, going after \a before.
1250 * This is typically called by the ->ldo_object_alloc() method of \a
1253 void lu_object_add(struct lu_object *before, struct lu_object *o)
1255 list_move(&o->lo_linkage, &before->lo_linkage);
1257 EXPORT_SYMBOL(lu_object_add);
1260 * Initialize compound object.
1262 int lu_object_header_init(struct lu_object_header *h)
1264 memset(h, 0, sizeof *h);
1265 atomic_set(&h->loh_ref, 1);
1266 INIT_HLIST_NODE(&h->loh_hash);
1267 INIT_LIST_HEAD(&h->loh_lru);
1268 INIT_LIST_HEAD(&h->loh_layers);
1269 lu_ref_init(&h->loh_reference);
1272 EXPORT_SYMBOL(lu_object_header_init);
1275 * Finalize compound object.
1277 void lu_object_header_fini(struct lu_object_header *h)
1279 LASSERT(list_empty(&h->loh_layers));
1280 LASSERT(list_empty(&h->loh_lru));
1281 LASSERT(hlist_unhashed(&h->loh_hash));
1282 lu_ref_fini(&h->loh_reference);
1284 EXPORT_SYMBOL(lu_object_header_fini);
1287 * Given a compound object, find its slice, corresponding to the device type
1290 struct lu_object *lu_object_locate(struct lu_object_header *h,
1291 const struct lu_device_type *dtype)
1293 struct lu_object *o;
1295 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1296 if (o->lo_dev->ld_type == dtype)
1301 EXPORT_SYMBOL(lu_object_locate);
1304 * Finalize and free devices in the device stack.
1306 * Finalize device stack by purging object cache, and calling
1307 * lu_device_type_operations::ldto_device_fini() and
1308 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1310 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1312 struct lu_site *site = top->ld_site;
1313 struct lu_device *scan;
1314 struct lu_device *next;
1316 lu_site_purge(env, site, ~0);
1317 for (scan = top; scan != NULL; scan = next) {
1318 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1319 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1320 lu_device_put(scan);
1324 lu_site_purge(env, site, ~0);
1326 for (scan = top; scan != NULL; scan = next) {
1327 const struct lu_device_type *ldt = scan->ld_type;
1328 struct obd_type *type;
1330 next = ldt->ldt_ops->ldto_device_free(env, scan);
1331 type = ldt->ldt_obd_type;
1334 class_put_type(type);
1341 * Maximal number of tld slots.
1343 LU_CONTEXT_KEY_NR = 40
1346 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1348 DEFINE_RWLOCK(lu_keys_guard);
1349 static DECLARE_RWSEM(lu_key_initing);
1352 * Global counter incremented whenever key is registered, unregistered,
1353 * revived or quiesced. This is used to void unnecessary calls to
1354 * lu_context_refill(). No locking is provided, as initialization and shutdown
1355 * are supposed to be externally serialized.
1357 static atomic_t key_set_version = ATOMIC_INIT(0);
1362 int lu_context_key_register(struct lu_context_key *key)
1367 LASSERT(key->lct_init != NULL);
1368 LASSERT(key->lct_fini != NULL);
1369 LASSERT(key->lct_tags != 0);
1370 LASSERT(key->lct_owner != NULL);
1373 atomic_set(&key->lct_used, 1);
1374 lu_ref_init(&key->lct_reference);
1375 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1379 if (cmpxchg(&lu_keys[i], NULL, key) != NULL)
1383 atomic_inc(&key_set_version);
1387 lu_ref_fini(&key->lct_reference);
1388 atomic_set(&key->lct_used, 0);
1392 EXPORT_SYMBOL(lu_context_key_register);
1394 static void key_fini(struct lu_context *ctx, int index)
1396 if (ctx->lc_value != NULL && ctx->lc_value[index] != NULL) {
1397 struct lu_context_key *key;
1399 key = lu_keys[index];
1400 LASSERT(key != NULL);
1401 LASSERT(key->lct_fini != NULL);
1402 LASSERT(atomic_read(&key->lct_used) > 1);
1404 key->lct_fini(ctx, key, ctx->lc_value[index]);
1405 lu_ref_del(&key->lct_reference, "ctx", ctx);
1406 if (atomic_dec_and_test(&key->lct_used))
1407 wake_up_var(&key->lct_used);
1409 LASSERT(key->lct_owner != NULL);
1410 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1411 LINVRNT(module_refcount(key->lct_owner) > 0);
1412 module_put(key->lct_owner);
1414 ctx->lc_value[index] = NULL;
1421 void lu_context_key_degister(struct lu_context_key *key)
1423 LASSERT(atomic_read(&key->lct_used) >= 1);
1424 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1426 lu_context_key_quiesce(key);
1428 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1431 * Wait until all transient contexts referencing this key have
1432 * run lu_context_key::lct_fini() method.
1434 atomic_dec(&key->lct_used);
1435 wait_var_event(&key->lct_used, atomic_read(&key->lct_used) == 0);
1437 if (!WARN_ON(lu_keys[key->lct_index] == NULL))
1438 lu_ref_fini(&key->lct_reference);
1440 smp_store_release(&lu_keys[key->lct_index], NULL);
1442 EXPORT_SYMBOL(lu_context_key_degister);
1445 * Register a number of keys. This has to be called after all keys have been
1446 * initialized by a call to LU_CONTEXT_KEY_INIT().
1448 int lu_context_key_register_many(struct lu_context_key *k, ...)
1450 struct lu_context_key *key = k;
1456 result = lu_context_key_register(key);
1459 key = va_arg(args, struct lu_context_key *);
1460 } while (key != NULL);
1466 lu_context_key_degister(k);
1467 k = va_arg(args, struct lu_context_key *);
1474 EXPORT_SYMBOL(lu_context_key_register_many);
1477 * De-register a number of keys. This is a dual to
1478 * lu_context_key_register_many().
1480 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1486 lu_context_key_degister(k);
1487 k = va_arg(args, struct lu_context_key*);
1488 } while (k != NULL);
1491 EXPORT_SYMBOL(lu_context_key_degister_many);
1494 * Revive a number of keys.
1496 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1502 lu_context_key_revive(k);
1503 k = va_arg(args, struct lu_context_key*);
1504 } while (k != NULL);
1507 EXPORT_SYMBOL(lu_context_key_revive_many);
1510 * Quiescent a number of keys.
1512 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1518 lu_context_key_quiesce(k);
1519 k = va_arg(args, struct lu_context_key*);
1520 } while (k != NULL);
1523 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1526 * Return value associated with key \a key in context \a ctx.
1528 void *lu_context_key_get(const struct lu_context *ctx,
1529 const struct lu_context_key *key)
1531 LINVRNT(ctx->lc_state == LCS_ENTERED);
1532 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1533 LASSERT(lu_keys[key->lct_index] == key);
1534 return ctx->lc_value[key->lct_index];
1536 EXPORT_SYMBOL(lu_context_key_get);
1539 * List of remembered contexts. XXX document me.
1541 static LIST_HEAD(lu_context_remembered);
1544 * Destroy \a key in all remembered contexts. This is used to destroy key
1545 * values in "shared" contexts (like service threads), when a module owning
1546 * the key is about to be unloaded.
1548 void lu_context_key_quiesce(struct lu_context_key *key)
1550 struct lu_context *ctx;
1552 if (!(key->lct_tags & LCT_QUIESCENT)) {
1554 * The write-lock on lu_key_initing will ensure that any
1555 * keys_fill() which didn't see LCT_QUIESCENT will have
1556 * finished before we call key_fini().
1558 down_write(&lu_key_initing);
1559 key->lct_tags |= LCT_QUIESCENT;
1560 up_write(&lu_key_initing);
1562 write_lock(&lu_keys_guard);
1563 list_for_each_entry(ctx, &lu_context_remembered, lc_remember) {
1564 spin_until_cond(READ_ONCE(ctx->lc_state) != LCS_LEAVING);
1565 key_fini(ctx, key->lct_index);
1568 write_unlock(&lu_keys_guard);
1572 void lu_context_key_revive(struct lu_context_key *key)
1574 key->lct_tags &= ~LCT_QUIESCENT;
1575 atomic_inc(&key_set_version);
1578 static void keys_fini(struct lu_context *ctx)
1582 if (ctx->lc_value == NULL)
1585 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1588 OBD_FREE(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1589 ctx->lc_value = NULL;
1592 static int keys_fill(struct lu_context *ctx)
1598 * A serialisation with lu_context_key_quiesce() is needed, to
1599 * ensure we see LCT_QUIESCENT and don't allocate a new value
1600 * after it freed one. The rwsem provides this. As down_read()
1601 * does optimistic spinning while the writer is active, this is
1602 * unlikely to ever sleep.
1604 down_read(&lu_key_initing);
1605 ctx->lc_version = atomic_read(&key_set_version);
1607 LINVRNT(ctx->lc_value);
1608 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1609 struct lu_context_key *key;
1612 if (!ctx->lc_value[i] && key &&
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 LASSERT(key->lct_owner != NULL);
1625 if (!(ctx->lc_tags & LCT_NOREF) &&
1626 try_module_get(key->lct_owner) == 0) {
1627 /* module is unloading, skip this key */
1631 value = key->lct_init(ctx, key);
1632 if (unlikely(IS_ERR(value))) {
1633 rc = PTR_ERR(value);
1637 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1638 atomic_inc(&key->lct_used);
1640 * This is the only place in the code, where an
1641 * element of ctx->lc_value[] array is set to non-NULL
1644 ctx->lc_value[i] = value;
1645 if (key->lct_exit != NULL)
1646 ctx->lc_tags |= LCT_HAS_EXIT;
1650 up_read(&lu_key_initing);
1654 static int keys_init(struct lu_context *ctx)
1656 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1657 if (likely(ctx->lc_value != NULL))
1658 return keys_fill(ctx);
1664 * Initialize context data-structure. Create values for all keys.
1666 int lu_context_init(struct lu_context *ctx, __u32 tags)
1670 memset(ctx, 0, sizeof *ctx);
1671 ctx->lc_state = LCS_INITIALIZED;
1672 ctx->lc_tags = tags;
1673 if (tags & LCT_REMEMBER) {
1674 write_lock(&lu_keys_guard);
1675 list_add(&ctx->lc_remember, &lu_context_remembered);
1676 write_unlock(&lu_keys_guard);
1678 INIT_LIST_HEAD(&ctx->lc_remember);
1681 rc = keys_init(ctx);
1683 lu_context_fini(ctx);
1687 EXPORT_SYMBOL(lu_context_init);
1690 * Finalize context data-structure. Destroy key values.
1692 void lu_context_fini(struct lu_context *ctx)
1694 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1695 ctx->lc_state = LCS_FINALIZED;
1697 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1698 LASSERT(list_empty(&ctx->lc_remember));
1701 } else { /* could race with key degister */
1702 write_lock(&lu_keys_guard);
1704 list_del_init(&ctx->lc_remember);
1705 write_unlock(&lu_keys_guard);
1708 EXPORT_SYMBOL(lu_context_fini);
1711 * Called before entering context.
1713 void lu_context_enter(struct lu_context *ctx)
1715 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1716 ctx->lc_state = LCS_ENTERED;
1718 EXPORT_SYMBOL(lu_context_enter);
1721 * Called after exiting from \a ctx
1723 void lu_context_exit(struct lu_context *ctx)
1727 LINVRNT(ctx->lc_state == LCS_ENTERED);
1729 * Disable preempt to ensure we get a warning if
1730 * any lct_exit ever tries to sleep. That would hurt
1731 * lu_context_key_quiesce() which spins waiting for us.
1732 * This also ensure we aren't preempted while the state
1733 * is LCS_LEAVING, as that too would cause problems for
1734 * lu_context_key_quiesce().
1738 * Ensure lu_context_key_quiesce() sees LCS_LEAVING
1739 * or we see LCT_QUIESCENT
1741 smp_store_mb(ctx->lc_state, LCS_LEAVING);
1742 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value) {
1743 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1744 struct lu_context_key *key;
1747 if (ctx->lc_value[i] &&
1748 !(key->lct_tags & LCT_QUIESCENT) &&
1750 key->lct_exit(ctx, key, ctx->lc_value[i]);
1754 smp_store_release(&ctx->lc_state, LCS_LEFT);
1757 EXPORT_SYMBOL(lu_context_exit);
1760 * Allocate for context all missing keys that were registered after context
1761 * creation. key_set_version is only changed in rare cases when modules
1762 * are loaded and removed.
1764 int lu_context_refill(struct lu_context *ctx)
1766 if (likely(ctx->lc_version == atomic_read(&key_set_version)))
1769 return keys_fill(ctx);
1773 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1774 * obd being added. Currently, this is only used on client side, specifically
1775 * for echo device client, for other stack (like ptlrpc threads), context are
1776 * predefined when the lu_device type are registered, during the module probe
1779 u32 lu_context_tags_default;
1780 u32 lu_session_tags_default;
1782 #ifdef HAVE_SERVER_SUPPORT
1783 void lu_context_tags_update(__u32 tags)
1785 write_lock(&lu_keys_guard);
1786 lu_context_tags_default |= tags;
1787 atomic_inc(&key_set_version);
1788 write_unlock(&lu_keys_guard);
1790 EXPORT_SYMBOL(lu_context_tags_update);
1792 void lu_context_tags_clear(__u32 tags)
1794 write_lock(&lu_keys_guard);
1795 lu_context_tags_default &= ~tags;
1796 atomic_inc(&key_set_version);
1797 write_unlock(&lu_keys_guard);
1799 EXPORT_SYMBOL(lu_context_tags_clear);
1801 void lu_session_tags_update(__u32 tags)
1803 write_lock(&lu_keys_guard);
1804 lu_session_tags_default |= tags;
1805 atomic_inc(&key_set_version);
1806 write_unlock(&lu_keys_guard);
1808 EXPORT_SYMBOL(lu_session_tags_update);
1810 void lu_session_tags_clear(__u32 tags)
1812 write_lock(&lu_keys_guard);
1813 lu_session_tags_default &= ~tags;
1814 atomic_inc(&key_set_version);
1815 write_unlock(&lu_keys_guard);
1817 EXPORT_SYMBOL(lu_session_tags_clear);
1818 #endif /* HAVE_SERVER_SUPPORT */
1820 int lu_env_init(struct lu_env *env, __u32 tags)
1825 result = lu_context_init(&env->le_ctx, tags);
1826 if (likely(result == 0))
1827 lu_context_enter(&env->le_ctx);
1830 EXPORT_SYMBOL(lu_env_init);
1832 void lu_env_fini(struct lu_env *env)
1834 lu_context_exit(&env->le_ctx);
1835 lu_context_fini(&env->le_ctx);
1838 EXPORT_SYMBOL(lu_env_fini);
1840 int lu_env_refill(struct lu_env *env)
1844 result = lu_context_refill(&env->le_ctx);
1845 if (result == 0 && env->le_ses != NULL)
1846 result = lu_context_refill(env->le_ses);
1849 EXPORT_SYMBOL(lu_env_refill);
1852 * Currently, this API will only be used by echo client.
1853 * Because echo client and normal lustre client will share
1854 * same cl_env cache. So echo client needs to refresh
1855 * the env context after it get one from the cache, especially
1856 * when normal client and echo client co-exist in the same client.
1858 int lu_env_refill_by_tags(struct lu_env *env, __u32 ctags,
1863 if ((env->le_ctx.lc_tags & ctags) != ctags) {
1864 env->le_ctx.lc_version = 0;
1865 env->le_ctx.lc_tags |= ctags;
1868 if (env->le_ses && (env->le_ses->lc_tags & stags) != stags) {
1869 env->le_ses->lc_version = 0;
1870 env->le_ses->lc_tags |= stags;
1873 result = lu_env_refill(env);
1877 EXPORT_SYMBOL(lu_env_refill_by_tags);
1880 struct lu_env_item {
1881 struct task_struct *lei_task; /* rhashtable key */
1882 struct rhash_head lei_linkage;
1883 struct lu_env *lei_env;
1886 static const struct rhashtable_params lu_env_rhash_params = {
1887 .key_len = sizeof(struct task_struct *),
1888 .key_offset = offsetof(struct lu_env_item, lei_task),
1889 .head_offset = offsetof(struct lu_env_item, lei_linkage),
1892 struct rhashtable lu_env_rhash;
1894 struct lu_env_percpu {
1895 struct task_struct *lep_task;
1896 struct lu_env *lep_env ____cacheline_aligned_in_smp;
1899 static struct lu_env_percpu lu_env_percpu[NR_CPUS];
1901 int lu_env_add(struct lu_env *env)
1903 struct lu_env_item *lei, *old;
1911 lei->lei_task = current;
1914 old = rhashtable_lookup_get_insert_fast(&lu_env_rhash,
1916 lu_env_rhash_params);
1921 EXPORT_SYMBOL(lu_env_add);
1923 void lu_env_remove(struct lu_env *env)
1925 struct lu_env_item *lei;
1926 const void *task = current;
1929 for_each_possible_cpu(i) {
1930 if (lu_env_percpu[i].lep_env == env) {
1931 LASSERT(lu_env_percpu[i].lep_task == task);
1932 lu_env_percpu[i].lep_task = NULL;
1933 lu_env_percpu[i].lep_env = NULL;
1938 lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
1939 lu_env_rhash_params);
1940 if (lei && rhashtable_remove_fast(&lu_env_rhash, &lei->lei_linkage,
1941 lu_env_rhash_params) == 0)
1945 EXPORT_SYMBOL(lu_env_remove);
1947 struct lu_env *lu_env_find(void)
1949 struct lu_env *env = NULL;
1950 struct lu_env_item *lei;
1951 const void *task = current;
1954 if (lu_env_percpu[i].lep_task == current) {
1955 env = lu_env_percpu[i].lep_env;
1961 lei = rhashtable_lookup_fast(&lu_env_rhash, &task,
1962 lu_env_rhash_params);
1965 lu_env_percpu[i].lep_task = current;
1966 lu_env_percpu[i].lep_env = env;
1972 EXPORT_SYMBOL(lu_env_find);
1974 static struct shrinker *lu_site_shrinker;
1976 typedef struct lu_site_stats{
1977 unsigned lss_populated;
1978 unsigned lss_max_search;
1983 static void lu_site_stats_get(const struct lu_site *s,
1984 lu_site_stats_t *stats, int populated)
1986 struct cfs_hash *hs = s->ls_obj_hash;
1987 struct cfs_hash_bd bd;
1990 * percpu_counter_sum_positive() won't accept a const pointer
1991 * as it does modify the struct by taking a spinlock
1993 struct lu_site *s2 = (struct lu_site *)s;
1995 stats->lss_busy += cfs_hash_size_get(hs) -
1996 percpu_counter_sum_positive(&s2->ls_lru_len_counter);
1997 cfs_hash_for_each_bucket(hs, &bd, i) {
1998 struct hlist_head *hhead;
2000 cfs_hash_bd_lock(hs, &bd, 1);
2001 stats->lss_total += cfs_hash_bd_count_get(&bd);
2002 stats->lss_max_search = max((int)stats->lss_max_search,
2003 cfs_hash_bd_depmax_get(&bd));
2005 cfs_hash_bd_unlock(hs, &bd, 1);
2009 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
2010 if (!hlist_empty(hhead))
2011 stats->lss_populated++;
2013 cfs_hash_bd_unlock(hs, &bd, 1);
2019 * lu_cache_shrink_count() returns an approximate number of cached objects
2020 * that can be freed by shrink_slab(). A counter, which tracks the
2021 * number of items in the site's lru, is maintained in a percpu_counter
2022 * for each site. The percpu values are incremented and decremented as
2023 * objects are added or removed from the lru. The percpu values are summed
2024 * and saved whenever a percpu value exceeds a threshold. Thus the saved,
2025 * summed value at any given time may not accurately reflect the current
2026 * lru length. But this value is sufficiently accurate for the needs of
2029 * Using a per cpu counter is a compromise solution to concurrent access:
2030 * lu_object_put() can update the counter without locking the site and
2031 * lu_cache_shrink_count can sum the counters without locking each
2032 * ls_obj_hash bucket.
2034 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
2035 struct shrink_control *sc)
2038 struct lu_site *tmp;
2039 unsigned long cached = 0;
2041 if (!(sc->gfp_mask & __GFP_FS))
2044 down_read(&lu_sites_guard);
2045 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage)
2046 cached += percpu_counter_read_positive(&s->ls_lru_len_counter);
2047 up_read(&lu_sites_guard);
2049 cached = (cached / 100) * sysctl_vfs_cache_pressure;
2050 CDEBUG(D_INODE, "%ld objects cached, cache pressure %d\n",
2051 cached, sysctl_vfs_cache_pressure);
2056 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
2057 struct shrink_control *sc)
2060 struct lu_site *tmp;
2061 unsigned long remain = sc->nr_to_scan;
2064 if (!(sc->gfp_mask & __GFP_FS))
2065 /* We must not take the lu_sites_guard lock when
2066 * __GFP_FS is *not* set because of the deadlock
2067 * possibility detailed above. Additionally,
2068 * since we cannot determine the number of
2069 * objects in the cache without taking this
2070 * lock, we're in a particularly tough spot. As
2071 * a result, we'll just lie and say our cache is
2072 * empty. This _should_ be ok, as we can't
2073 * reclaim objects when __GFP_FS is *not* set
2078 down_write(&lu_sites_guard);
2079 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
2080 remain = lu_site_purge(&lu_shrink_env, s, remain);
2082 * Move just shrunk site to the tail of site list to
2083 * assure shrinking fairness.
2085 list_move_tail(&s->ls_linkage, &splice);
2087 list_splice(&splice, lu_sites.prev);
2088 up_write(&lu_sites_guard);
2090 return sc->nr_to_scan - remain;
2093 #ifndef HAVE_SHRINKER_COUNT
2095 * There exists a potential lock inversion deadlock scenario when using
2096 * Lustre on top of ZFS. This occurs between one of ZFS's
2097 * buf_hash_table.ht_lock's, and Lustre's lu_sites_guard lock. Essentially,
2098 * thread A will take the lu_sites_guard lock and sleep on the ht_lock,
2099 * while thread B will take the ht_lock and sleep on the lu_sites_guard
2100 * lock. Obviously neither thread will wake and drop their respective hold
2103 * To prevent this from happening we must ensure the lu_sites_guard lock is
2104 * not taken while down this code path. ZFS reliably does not set the
2105 * __GFP_FS bit in its code paths, so this can be used to determine if it
2106 * is safe to take the lu_sites_guard lock.
2108 * Ideally we should accurately return the remaining number of cached
2109 * objects without taking the lu_sites_guard lock, but this is not
2110 * possible in the current implementation.
2112 static int lu_cache_shrink(SHRINKER_ARGS(sc, nr_to_scan, gfp_mask))
2115 struct shrink_control scv = {
2116 .nr_to_scan = shrink_param(sc, nr_to_scan),
2117 .gfp_mask = shrink_param(sc, gfp_mask)
2119 #if !defined(HAVE_SHRINKER_WANT_SHRINK_PTR) && !defined(HAVE_SHRINK_CONTROL)
2120 struct shrinker* shrinker = NULL;
2124 CDEBUG(D_INODE, "Shrink %lu objects\n", scv.nr_to_scan);
2126 if (scv.nr_to_scan != 0)
2127 lu_cache_shrink_scan(shrinker, &scv);
2129 cached = lu_cache_shrink_count(shrinker, &scv);
2133 #endif /* HAVE_SHRINKER_COUNT */
2141 * Environment to be used in debugger, contains all tags.
2143 static struct lu_env lu_debugging_env;
2146 * Debugging printer function using printk().
2148 int lu_printk_printer(const struct lu_env *env,
2149 void *unused, const char *format, ...)
2153 va_start(args, format);
2154 vprintk(format, args);
2159 int lu_debugging_setup(void)
2161 return lu_env_init(&lu_debugging_env, ~0);
2164 void lu_context_keys_dump(void)
2168 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
2169 struct lu_context_key *key;
2173 CERROR("[%d]: %p %x (%p,%p,%p) %d %d \"%s\"@%p\n",
2174 i, key, key->lct_tags,
2175 key->lct_init, key->lct_fini, key->lct_exit,
2176 key->lct_index, atomic_read(&key->lct_used),
2177 key->lct_owner ? key->lct_owner->name : "",
2179 lu_ref_print(&key->lct_reference);
2185 * Initialization of global lu_* data.
2187 int lu_global_init(void)
2190 DEF_SHRINKER_VAR(shvar, lu_cache_shrink,
2191 lu_cache_shrink_count, lu_cache_shrink_scan);
2193 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
2195 result = lu_ref_global_init();
2199 LU_CONTEXT_KEY_INIT(&lu_global_key);
2200 result = lu_context_key_register(&lu_global_key);
2205 * At this level, we don't know what tags are needed, so allocate them
2206 * conservatively. This should not be too bad, because this
2207 * environment is global.
2209 down_write(&lu_sites_guard);
2210 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
2211 up_write(&lu_sites_guard);
2216 * seeks estimation: 3 seeks to read a record from oi, one to read
2217 * inode, one for ea. Unfortunately setting this high value results in
2218 * lu_object/inode cache consuming all the memory.
2220 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, &shvar);
2221 if (lu_site_shrinker == NULL)
2224 result = rhashtable_init(&lu_env_rhash, &lu_env_rhash_params);
2230 * Dual to lu_global_init().
2232 void lu_global_fini(void)
2234 if (lu_site_shrinker != NULL) {
2235 remove_shrinker(lu_site_shrinker);
2236 lu_site_shrinker = NULL;
2239 lu_context_key_degister(&lu_global_key);
2242 * Tear shrinker environment down _after_ de-registering
2243 * lu_global_key, because the latter has a value in the former.
2245 down_write(&lu_sites_guard);
2246 lu_env_fini(&lu_shrink_env);
2247 up_write(&lu_sites_guard);
2249 rhashtable_destroy(&lu_env_rhash);
2251 lu_ref_global_fini();
2254 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
2256 #ifdef CONFIG_PROC_FS
2257 struct lprocfs_counter ret;
2259 lprocfs_stats_collect(stats, idx, &ret);
2260 return (__u32)ret.lc_count;
2267 * Output site statistical counters into a buffer. Suitable for
2268 * lprocfs_rd_*()-style functions.
2270 int lu_site_stats_seq_print(const struct lu_site *s, struct seq_file *m)
2272 lu_site_stats_t stats;
2274 memset(&stats, 0, sizeof(stats));
2275 lu_site_stats_get(s, &stats, 1);
2277 seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d\n",
2280 stats.lss_populated,
2281 CFS_HASH_NHLIST(s->ls_obj_hash),
2282 stats.lss_max_search,
2283 ls_stats_read(s->ls_stats, LU_SS_CREATED),
2284 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
2285 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
2286 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
2287 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
2288 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED));
2291 EXPORT_SYMBOL(lu_site_stats_seq_print);
2294 * Helper function to initialize a number of kmem slab caches at once.
2296 int lu_kmem_init(struct lu_kmem_descr *caches)
2299 struct lu_kmem_descr *iter = caches;
2301 for (result = 0; iter->ckd_cache != NULL; ++iter) {
2302 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
2305 if (*iter->ckd_cache == NULL) {
2307 /* free all previously allocated caches */
2308 lu_kmem_fini(caches);
2314 EXPORT_SYMBOL(lu_kmem_init);
2317 * Helper function to finalize a number of kmem slab cached at once. Dual to
2320 void lu_kmem_fini(struct lu_kmem_descr *caches)
2322 for (; caches->ckd_cache != NULL; ++caches) {
2323 if (*caches->ckd_cache != NULL) {
2324 kmem_cache_destroy(*caches->ckd_cache);
2325 *caches->ckd_cache = NULL;
2329 EXPORT_SYMBOL(lu_kmem_fini);
2332 * Temporary solution to be able to assign fid in ->do_create()
2333 * till we have fully-functional OST fids
2335 void lu_object_assign_fid(const struct lu_env *env, struct lu_object *o,
2336 const struct lu_fid *fid)
2338 struct lu_site *s = o->lo_dev->ld_site;
2339 struct lu_fid *old = &o->lo_header->loh_fid;
2340 struct cfs_hash *hs;
2341 struct cfs_hash_bd bd;
2343 LASSERT(fid_is_zero(old));
2345 /* supposed to be unique */
2346 hs = s->ls_obj_hash;
2347 cfs_hash_bd_get_and_lock(hs, (void *)fid, &bd, 1);
2348 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
2351 struct lu_object *shadow;
2353 shadow = htable_lookup(s, &bd, fid, &version);
2354 /* supposed to be unique */
2355 LASSERT(IS_ERR(shadow) && PTR_ERR(shadow) == -ENOENT);
2359 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
2360 cfs_hash_bd_unlock(hs, &bd, 1);
2362 EXPORT_SYMBOL(lu_object_assign_fid);
2365 * allocates object with 0 (non-assiged) fid
2366 * XXX: temporary solution to be able to assign fid in ->do_create()
2367 * till we have fully-functional OST fids
2369 struct lu_object *lu_object_anon(const struct lu_env *env,
2370 struct lu_device *dev,
2371 const struct lu_object_conf *conf)
2374 struct lu_object *o;
2377 o = lu_object_alloc(env, dev, &fid, conf);
2381 EXPORT_SYMBOL(lu_object_anon);
2383 struct lu_buf LU_BUF_NULL = {
2387 EXPORT_SYMBOL(LU_BUF_NULL);
2389 void lu_buf_free(struct lu_buf *buf)
2393 LASSERT(buf->lb_len > 0);
2394 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2399 EXPORT_SYMBOL(lu_buf_free);
2401 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2404 LASSERT(buf->lb_buf == NULL);
2405 LASSERT(buf->lb_len == 0);
2406 OBD_ALLOC_LARGE(buf->lb_buf, size);
2407 if (likely(buf->lb_buf))
2410 EXPORT_SYMBOL(lu_buf_alloc);
2412 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2415 lu_buf_alloc(buf, size);
2417 EXPORT_SYMBOL(lu_buf_realloc);
2419 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2421 if (buf->lb_buf == NULL && buf->lb_len == 0)
2422 lu_buf_alloc(buf, len);
2424 if ((len > buf->lb_len) && (buf->lb_buf != NULL))
2425 lu_buf_realloc(buf, len);
2429 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2432 * Increase the size of the \a buf.
2433 * preserves old data in buffer
2434 * old buffer remains unchanged on error
2435 * \retval 0 or -ENOMEM
2437 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2441 if (len <= buf->lb_len)
2444 OBD_ALLOC_LARGE(ptr, len);
2448 /* Free the old buf */
2449 if (buf->lb_buf != NULL) {
2450 memcpy(ptr, buf->lb_buf, buf->lb_len);
2451 OBD_FREE_LARGE(buf->lb_buf, buf->lb_len);
2458 EXPORT_SYMBOL(lu_buf_check_and_grow);