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, 2015, 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 "../../include/linux/libcfs/libcfs.h"
45 # include <linux/module.h>
48 #include "../../include/linux/libcfs/libcfs_hash.h"
49 #include "../include/obd_class.h"
50 #include "../include/obd_support.h"
51 #include "../include/lustre_disk.h"
52 #include "../include/lustre_fid.h"
53 #include "../include/lu_object.h"
54 #include "../include/cl_object.h"
55 #include "../include/lu_ref.h"
56 #include <linux/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 #define LU_CACHE_NR_ZFS_LIMIT 256
69 #define LU_SITE_BITS_MIN 12
70 #define LU_SITE_BITS_MAX 24
72 * total 256 buckets, we don't want too many buckets because:
73 * - consume too much memory
74 * - avoid unbalanced LRU list
76 #define LU_SITE_BKT_BITS 8
78 static unsigned int lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
79 module_param(lu_cache_percent, int, 0644);
80 MODULE_PARM_DESC(lu_cache_percent, "Percentage of memory to be used as lu_object cache");
82 static long lu_cache_nr = LU_CACHE_NR_DEFAULT;
83 module_param(lu_cache_nr, long, 0644);
84 MODULE_PARM_DESC(lu_cache_nr, "Maximum number of objects in lu_object cache");
86 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
87 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx);
90 * Decrease reference counter on object. If last reference is freed, return
91 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
92 * case, free object immediately.
94 void lu_object_put(const struct lu_env *env, struct lu_object *o)
96 struct lu_site_bkt_data *bkt;
97 struct lu_object_header *top;
99 struct lu_object *orig;
100 struct cfs_hash_bd bd;
101 const struct lu_fid *fid;
104 site = o->lo_dev->ld_site;
108 * till we have full fids-on-OST implemented anonymous objects
109 * are possible in OSP. such an object isn't listed in the site
110 * so we should not remove it from the site.
112 fid = lu_object_fid(o);
113 if (fid_is_zero(fid)) {
114 LASSERT(!top->loh_hash.next && !top->loh_hash.pprev);
115 LASSERT(list_empty(&top->loh_lru));
116 if (!atomic_dec_and_test(&top->loh_ref))
118 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
119 if (o->lo_ops->loo_object_release)
120 o->lo_ops->loo_object_release(env, o);
122 lu_object_free(env, orig);
126 cfs_hash_bd_get(site->ls_obj_hash, &top->loh_fid, &bd);
127 bkt = cfs_hash_bd_extra_get(site->ls_obj_hash, &bd);
129 if (!cfs_hash_bd_dec_and_lock(site->ls_obj_hash, &bd, &top->loh_ref)) {
130 if (lu_object_is_dying(top)) {
132 * somebody may be waiting for this, currently only
133 * used for cl_object, see cl_object_put_last().
135 wake_up_all(&bkt->lsb_marche_funebre);
141 * When last reference is released, iterate over object
142 * layers, and notify them that object is no longer busy.
144 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
145 if (o->lo_ops->loo_object_release)
146 o->lo_ops->loo_object_release(env, o);
149 if (!lu_object_is_dying(top)) {
150 LASSERT(list_empty(&top->loh_lru));
151 list_add_tail(&top->loh_lru, &bkt->lsb_lru);
153 lprocfs_counter_incr(site->ls_stats, LU_SS_LRU_LEN);
154 CDEBUG(D_INODE, "Add %p to site lru. hash: %p, bkt: %p, lru_len: %ld\n",
155 o, site->ls_obj_hash, bkt, bkt->lsb_lru_len);
156 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
161 * If object is dying (will not be cached), then removed it
162 * from hash table and LRU.
164 * This is done with hash table and LRU lists locked. As the only
165 * way to acquire first reference to previously unreferenced
166 * object is through hash-table lookup (lu_object_find()),
167 * or LRU scanning (lu_site_purge()), that are done under hash-table
168 * and LRU lock, no race with concurrent object lookup is possible
169 * and we can safely destroy object below.
171 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags))
172 cfs_hash_bd_del_locked(site->ls_obj_hash, &bd, &top->loh_hash);
173 cfs_hash_bd_unlock(site->ls_obj_hash, &bd, 1);
175 * Object was already removed from hash and lru above, can
178 lu_object_free(env, orig);
180 EXPORT_SYMBOL(lu_object_put);
183 * Kill the object and take it out of LRU cache.
184 * Currently used by client code for layout change.
186 void lu_object_unhash(const struct lu_env *env, struct lu_object *o)
188 struct lu_object_header *top;
191 set_bit(LU_OBJECT_HEARD_BANSHEE, &top->loh_flags);
192 if (!test_and_set_bit(LU_OBJECT_UNHASHED, &top->loh_flags)) {
193 struct lu_site *site = o->lo_dev->ld_site;
194 struct cfs_hash *obj_hash = site->ls_obj_hash;
195 struct cfs_hash_bd bd;
197 cfs_hash_bd_get_and_lock(obj_hash, &top->loh_fid, &bd, 1);
198 if (!list_empty(&top->loh_lru)) {
199 struct lu_site_bkt_data *bkt;
201 list_del_init(&top->loh_lru);
202 bkt = cfs_hash_bd_extra_get(obj_hash, &bd);
204 lprocfs_counter_decr(site->ls_stats, LU_SS_LRU_LEN);
206 cfs_hash_bd_del_locked(obj_hash, &bd, &top->loh_hash);
207 cfs_hash_bd_unlock(obj_hash, &bd, 1);
210 EXPORT_SYMBOL(lu_object_unhash);
213 * Allocate new object.
215 * This follows object creation protocol, described in the comment within
216 * struct lu_device_operations definition.
218 static struct lu_object *lu_object_alloc(const struct lu_env *env,
219 struct lu_device *dev,
220 const struct lu_fid *f,
221 const struct lu_object_conf *conf)
223 struct lu_object *scan;
224 struct lu_object *top;
225 struct list_head *layers;
226 unsigned int init_mask = 0;
227 unsigned int init_flag;
232 * Create top-level object slice. This will also create
235 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
237 return ERR_PTR(-ENOMEM);
241 * This is the only place where object fid is assigned. It's constant
244 top->lo_header->loh_fid = *f;
245 layers = &top->lo_header->loh_layers;
249 * Call ->loo_object_init() repeatedly, until no more new
250 * object slices are created.
254 list_for_each_entry(scan, layers, lo_linkage) {
255 if (init_mask & init_flag)
258 scan->lo_header = top->lo_header;
259 result = scan->lo_ops->loo_object_init(env, scan, conf);
261 lu_object_free(env, top);
262 return ERR_PTR(result);
264 init_mask |= init_flag;
270 list_for_each_entry_reverse(scan, layers, lo_linkage) {
271 if (scan->lo_ops->loo_object_start) {
272 result = scan->lo_ops->loo_object_start(env, scan);
274 lu_object_free(env, top);
275 return ERR_PTR(result);
280 lprocfs_counter_incr(dev->ld_site->ls_stats, LU_SS_CREATED);
287 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
289 struct lu_site_bkt_data *bkt;
290 struct lu_site *site;
291 struct lu_object *scan;
292 struct list_head *layers;
293 struct list_head splice;
295 site = o->lo_dev->ld_site;
296 layers = &o->lo_header->loh_layers;
297 bkt = lu_site_bkt_from_fid(site, &o->lo_header->loh_fid);
299 * First call ->loo_object_delete() method to release all resources.
301 list_for_each_entry_reverse(scan, layers, lo_linkage) {
302 if (scan->lo_ops->loo_object_delete)
303 scan->lo_ops->loo_object_delete(env, scan);
307 * Then, splice object layers into stand-alone list, and call
308 * ->loo_object_free() on all layers to free memory. Splice is
309 * necessary, because lu_object_header is freed together with the
312 INIT_LIST_HEAD(&splice);
313 list_splice_init(layers, &splice);
314 while (!list_empty(&splice)) {
316 * Free layers in bottom-to-top order, so that object header
317 * lives as long as possible and ->loo_object_free() methods
318 * can look at its contents.
320 o = container_of0(splice.prev, struct lu_object, lo_linkage);
321 list_del_init(&o->lo_linkage);
322 o->lo_ops->loo_object_free(env, o);
325 if (waitqueue_active(&bkt->lsb_marche_funebre))
326 wake_up_all(&bkt->lsb_marche_funebre);
330 * Free \a nr objects from the cold end of the site LRU list.
332 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
334 struct lu_object_header *h;
335 struct lu_object_header *temp;
336 struct lu_site_bkt_data *bkt;
337 struct cfs_hash_bd bd;
338 struct cfs_hash_bd bd2;
339 struct list_head dispose;
346 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_NO_LRU))
349 INIT_LIST_HEAD(&dispose);
351 * Under LRU list lock, scan LRU list and move unreferenced objects to
352 * the dispose list, removing them from LRU and hash table.
354 start = s->ls_purge_start;
355 bnr = (nr == ~0) ? -1 : nr / (int)CFS_HASH_NBKT(s->ls_obj_hash) + 1;
358 * It doesn't make any sense to make purge threads parallel, that can
359 * only bring troubles to us. See LU-5331.
361 mutex_lock(&s->ls_purge_mutex);
363 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
367 cfs_hash_bd_lock(s->ls_obj_hash, &bd, 1);
368 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
370 list_for_each_entry_safe(h, temp, &bkt->lsb_lru, loh_lru) {
371 LASSERT(atomic_read(&h->loh_ref) == 0);
373 cfs_hash_bd_get(s->ls_obj_hash, &h->loh_fid, &bd2);
374 LASSERT(bd.bd_bucket == bd2.bd_bucket);
376 cfs_hash_bd_del_locked(s->ls_obj_hash,
378 list_move(&h->loh_lru, &dispose);
380 lprocfs_counter_decr(s->ls_stats, LU_SS_LRU_LEN);
384 if (nr != ~0 && --nr == 0)
387 if (count > 0 && --count == 0)
390 cfs_hash_bd_unlock(s->ls_obj_hash, &bd, 1);
393 * Free everything on the dispose list. This is safe against
394 * races due to the reasons described in lu_object_put().
396 while (!list_empty(&dispose)) {
397 h = container_of0(dispose.next,
398 struct lu_object_header, loh_lru);
399 list_del_init(&h->loh_lru);
400 lu_object_free(env, lu_object_top(h));
401 lprocfs_counter_incr(s->ls_stats, LU_SS_LRU_PURGED);
407 mutex_unlock(&s->ls_purge_mutex);
409 if (nr != 0 && did_sth && start != 0) {
410 start = 0; /* restart from the first bucket */
413 /* race on s->ls_purge_start, but nobody cares */
414 s->ls_purge_start = i % CFS_HASH_NBKT(s->ls_obj_hash);
418 EXPORT_SYMBOL(lu_site_purge);
423 * Code below has to jump through certain loops to output object description
424 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
425 * composes object description from strings that are parts of _lines_ of
426 * output (i.e., strings that are not terminated by newline). This doesn't fit
427 * very well into libcfs_debug_msg() interface that assumes that each message
428 * supplied to it is a self-contained output line.
430 * To work around this, strings are collected in a temporary buffer
431 * (implemented as a value of lu_cdebug_key key), until terminating newline
432 * character is detected.
440 * XXX overflow is not handled correctly.
445 struct lu_cdebug_data {
449 char lck_area[LU_CDEBUG_LINE];
452 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
453 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
456 * Key, holding temporary buffer. This key is registered very early by
459 static struct lu_context_key lu_global_key = {
460 .lct_tags = LCT_MD_THREAD | LCT_DT_THREAD |
461 LCT_MG_THREAD | LCT_CL_THREAD | LCT_LOCAL,
462 .lct_init = lu_global_key_init,
463 .lct_fini = lu_global_key_fini
467 * Printer function emitting messages through libcfs_debug_msg().
469 int lu_cdebug_printer(const struct lu_env *env,
470 void *cookie, const char *format, ...)
472 struct libcfs_debug_msg_data *msgdata = cookie;
473 struct lu_cdebug_data *key;
478 va_start(args, format);
480 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
482 used = strlen(key->lck_area);
483 complete = format[strlen(format) - 1] == '\n';
485 * Append new chunk to the buffer.
487 vsnprintf(key->lck_area + used,
488 ARRAY_SIZE(key->lck_area) - used, format, args);
490 if (cfs_cdebug_show(msgdata->msg_mask, msgdata->msg_subsys))
491 libcfs_debug_msg(msgdata, "%s\n", key->lck_area);
492 key->lck_area[0] = 0;
497 EXPORT_SYMBOL(lu_cdebug_printer);
500 * Print object header.
502 void lu_object_header_print(const struct lu_env *env, void *cookie,
503 lu_printer_t printer,
504 const struct lu_object_header *hdr)
506 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
507 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
509 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
510 list_empty((struct list_head *)&hdr->loh_lru) ? \
512 hdr->loh_attr & LOHA_EXISTS ? " exist":"");
514 EXPORT_SYMBOL(lu_object_header_print);
517 * Print human readable representation of the \a o to the \a printer.
519 void lu_object_print(const struct lu_env *env, void *cookie,
520 lu_printer_t printer, const struct lu_object *o)
522 static const char ruler[] = "........................................";
523 struct lu_object_header *top;
527 lu_object_header_print(env, cookie, printer, top);
528 (*printer)(env, cookie, "{\n");
530 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
532 * print `.' \a depth times followed by type name and address
534 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
535 o->lo_dev->ld_type->ldt_name, o);
537 if (o->lo_ops->loo_object_print)
538 (*o->lo_ops->loo_object_print)(env, cookie, printer, o);
540 (*printer)(env, cookie, "\n");
543 (*printer)(env, cookie, "} header@%p\n", top);
545 EXPORT_SYMBOL(lu_object_print);
547 static struct lu_object *htable_lookup(struct lu_site *s,
548 struct cfs_hash_bd *bd,
549 const struct lu_fid *f,
550 wait_queue_t *waiter,
553 struct lu_site_bkt_data *bkt;
554 struct lu_object_header *h;
555 struct hlist_node *hnode;
556 __u64 ver = cfs_hash_bd_version_get(bd);
559 return ERR_PTR(-ENOENT);
562 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, bd);
563 /* cfs_hash_bd_peek_locked is a somehow "internal" function
564 * of cfs_hash, it doesn't add refcount on object.
566 hnode = cfs_hash_bd_peek_locked(s->ls_obj_hash, bd, (void *)f);
568 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_MISS);
569 return ERR_PTR(-ENOENT);
572 h = container_of0(hnode, struct lu_object_header, loh_hash);
573 if (likely(!lu_object_is_dying(h))) {
574 cfs_hash_get(s->ls_obj_hash, hnode);
575 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_HIT);
576 if (!list_empty(&h->loh_lru)) {
577 list_del_init(&h->loh_lru);
579 lprocfs_counter_decr(s->ls_stats, LU_SS_LRU_LEN);
581 return lu_object_top(h);
585 * Lookup found an object being destroyed this object cannot be
586 * returned (to assure that references to dying objects are eventually
587 * drained), and moreover, lookup has to wait until object is freed.
590 init_waitqueue_entry(waiter, current);
591 add_wait_queue(&bkt->lsb_marche_funebre, waiter);
592 set_current_state(TASK_UNINTERRUPTIBLE);
593 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_DEATH_RACE);
594 return ERR_PTR(-EAGAIN);
598 * Search cache for an object with the fid \a f. If such object is found,
599 * return it. Otherwise, create new object, insert it into cache and return
600 * it. In any case, additional reference is acquired on the returned object.
602 static struct lu_object *lu_object_find(const struct lu_env *env,
603 struct lu_device *dev,
604 const struct lu_fid *f,
605 const struct lu_object_conf *conf)
607 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
611 * Limit the lu_object cache to a maximum of lu_cache_nr objects. Because
612 * the calculation for the number of objects to reclaim is not covered by
613 * a lock the maximum number of objects is capped by LU_CACHE_MAX_ADJUST.
614 * This ensures that many concurrent threads will not accidentally purge
617 static void lu_object_limit(const struct lu_env *env, struct lu_device *dev)
621 if (lu_cache_nr == LU_CACHE_NR_UNLIMITED)
624 size = cfs_hash_size_get(dev->ld_site->ls_obj_hash);
625 nr = (__u64)lu_cache_nr;
627 lu_site_purge(env, dev->ld_site,
628 min_t(__u64, size - nr, LU_CACHE_NR_MAX_ADJUST));
631 static struct lu_object *lu_object_new(const struct lu_env *env,
632 struct lu_device *dev,
633 const struct lu_fid *f,
634 const struct lu_object_conf *conf)
638 struct cfs_hash_bd bd;
640 o = lu_object_alloc(env, dev, f, conf);
644 hs = dev->ld_site->ls_obj_hash;
645 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
646 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
647 cfs_hash_bd_unlock(hs, &bd, 1);
649 lu_object_limit(env, dev);
655 * Core logic of lu_object_find*() functions.
657 static struct lu_object *lu_object_find_try(const struct lu_env *env,
658 struct lu_device *dev,
659 const struct lu_fid *f,
660 const struct lu_object_conf *conf,
661 wait_queue_t *waiter)
664 struct lu_object *shadow;
667 struct cfs_hash_bd bd;
671 * This uses standard index maintenance protocol:
673 * - search index under lock, and return object if found;
674 * - otherwise, unlock index, allocate new object;
675 * - lock index and search again;
676 * - if nothing is found (usual case), insert newly created
678 * - otherwise (race: other thread inserted object), free
679 * object just allocated.
683 * For "LOC_F_NEW" case, we are sure the object is new established.
684 * It is unnecessary to perform lookup-alloc-lookup-insert, instead,
685 * just alloc and insert directly.
687 * If dying object is found during index search, add @waiter to the
688 * site wait-queue and return ERR_PTR(-EAGAIN).
690 if (conf && conf->loc_flags & LOC_F_NEW)
691 return lu_object_new(env, dev, f, conf);
695 cfs_hash_bd_get_and_lock(hs, (void *)f, &bd, 1);
696 o = htable_lookup(s, &bd, f, waiter, &version);
697 cfs_hash_bd_unlock(hs, &bd, 1);
698 if (!IS_ERR(o) || PTR_ERR(o) != -ENOENT)
702 * Allocate new object. This may result in rather complicated
703 * operations, including fld queries, inode loading, etc.
705 o = lu_object_alloc(env, dev, f, conf);
709 LASSERT(lu_fid_eq(lu_object_fid(o), f));
711 cfs_hash_bd_lock(hs, &bd, 1);
713 shadow = htable_lookup(s, &bd, f, waiter, &version);
714 if (likely(PTR_ERR(shadow) == -ENOENT)) {
715 cfs_hash_bd_add_locked(hs, &bd, &o->lo_header->loh_hash);
716 cfs_hash_bd_unlock(hs, &bd, 1);
718 lu_object_limit(env, dev);
723 lprocfs_counter_incr(s->ls_stats, LU_SS_CACHE_RACE);
724 cfs_hash_bd_unlock(hs, &bd, 1);
725 lu_object_free(env, o);
730 * Much like lu_object_find(), but top level device of object is specifically
731 * \a dev rather than top level device of the site. This interface allows
732 * objects of different "stacking" to be created within the same site.
734 struct lu_object *lu_object_find_at(const struct lu_env *env,
735 struct lu_device *dev,
736 const struct lu_fid *f,
737 const struct lu_object_conf *conf)
739 struct lu_site_bkt_data *bkt;
740 struct lu_object *obj;
744 obj = lu_object_find_try(env, dev, f, conf, &wait);
745 if (obj != ERR_PTR(-EAGAIN))
748 * lu_object_find_try() already added waiter into the
752 bkt = lu_site_bkt_from_fid(dev->ld_site, (void *)f);
753 remove_wait_queue(&bkt->lsb_marche_funebre, &wait);
756 EXPORT_SYMBOL(lu_object_find_at);
759 * Find object with given fid, and return its slice belonging to given device.
761 struct lu_object *lu_object_find_slice(const struct lu_env *env,
762 struct lu_device *dev,
763 const struct lu_fid *f,
764 const struct lu_object_conf *conf)
766 struct lu_object *top;
767 struct lu_object *obj;
769 top = lu_object_find(env, dev, f, conf);
773 obj = lu_object_locate(top->lo_header, dev->ld_type);
774 if (unlikely(!obj)) {
775 lu_object_put(env, top);
776 obj = ERR_PTR(-ENOENT);
781 EXPORT_SYMBOL(lu_object_find_slice);
784 * Global list of all device types.
786 static LIST_HEAD(lu_device_types);
788 int lu_device_type_init(struct lu_device_type *ldt)
792 atomic_set(&ldt->ldt_device_nr, 0);
793 INIT_LIST_HEAD(&ldt->ldt_linkage);
794 if (ldt->ldt_ops->ldto_init)
795 result = ldt->ldt_ops->ldto_init(ldt);
798 spin_lock(&obd_types_lock);
799 list_add(&ldt->ldt_linkage, &lu_device_types);
800 spin_unlock(&obd_types_lock);
805 EXPORT_SYMBOL(lu_device_type_init);
807 void lu_device_type_fini(struct lu_device_type *ldt)
809 spin_lock(&obd_types_lock);
810 list_del_init(&ldt->ldt_linkage);
811 spin_unlock(&obd_types_lock);
812 if (ldt->ldt_ops->ldto_fini)
813 ldt->ldt_ops->ldto_fini(ldt);
815 EXPORT_SYMBOL(lu_device_type_fini);
818 * Global list of all sites on this node
820 static LIST_HEAD(lu_sites);
821 static DEFINE_MUTEX(lu_sites_guard);
824 * Global environment used by site shrinker.
826 static struct lu_env lu_shrink_env;
828 struct lu_site_print_arg {
829 struct lu_env *lsp_env;
831 lu_printer_t lsp_printer;
835 lu_site_obj_print(struct cfs_hash *hs, struct cfs_hash_bd *bd,
836 struct hlist_node *hnode, void *data)
838 struct lu_site_print_arg *arg = (struct lu_site_print_arg *)data;
839 struct lu_object_header *h;
841 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
842 if (!list_empty(&h->loh_layers)) {
843 const struct lu_object *o;
845 o = lu_object_top(h);
846 lu_object_print(arg->lsp_env, arg->lsp_cookie,
847 arg->lsp_printer, o);
849 lu_object_header_print(arg->lsp_env, arg->lsp_cookie,
850 arg->lsp_printer, h);
856 * Print all objects in \a s.
858 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
859 lu_printer_t printer)
861 struct lu_site_print_arg arg = {
862 .lsp_env = (struct lu_env *)env,
863 .lsp_cookie = cookie,
864 .lsp_printer = printer,
867 cfs_hash_for_each(s->ls_obj_hash, lu_site_obj_print, &arg);
869 EXPORT_SYMBOL(lu_site_print);
872 * Return desired hash table order.
874 static unsigned long lu_htable_order(struct lu_device *top)
876 unsigned long bits_max = LU_SITE_BITS_MAX;
877 unsigned long cache_size;
881 * Calculate hash table size, assuming that we want reasonable
882 * performance when 20% of total memory is occupied by cache of
885 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
887 cache_size = totalram_pages;
889 #if BITS_PER_LONG == 32
890 /* limit hashtable size for lowmem systems to low RAM */
891 if (cache_size > 1 << (30 - PAGE_SHIFT))
892 cache_size = 1 << (30 - PAGE_SHIFT) * 3 / 4;
895 /* clear off unreasonable cache setting. */
896 if (lu_cache_percent == 0 || lu_cache_percent > LU_CACHE_PERCENT_MAX) {
897 CWARN("obdclass: invalid lu_cache_percent: %u, it must be in the range of (0, %u]. Will use default value: %u.\n",
898 lu_cache_percent, LU_CACHE_PERCENT_MAX,
899 LU_CACHE_PERCENT_DEFAULT);
901 lu_cache_percent = LU_CACHE_PERCENT_DEFAULT;
903 cache_size = cache_size / 100 * lu_cache_percent *
906 for (bits = 1; (1 << bits) < cache_size; ++bits) {
909 return clamp_t(typeof(bits), bits, LU_SITE_BITS_MIN, bits_max);
912 static unsigned lu_obj_hop_hash(struct cfs_hash *hs,
913 const void *key, unsigned mask)
915 struct lu_fid *fid = (struct lu_fid *)key;
918 hash = fid_flatten32(fid);
919 hash += (hash >> 4) + (hash << 12); /* mixing oid and seq */
920 hash = hash_long(hash, hs->hs_bkt_bits);
922 /* give me another random factor */
923 hash -= hash_long((unsigned long)hs, fid_oid(fid) % 11 + 3);
925 hash <<= hs->hs_cur_bits - hs->hs_bkt_bits;
926 hash |= (fid_seq(fid) + fid_oid(fid)) & (CFS_HASH_NBKT(hs) - 1);
931 static void *lu_obj_hop_object(struct hlist_node *hnode)
933 return hlist_entry(hnode, struct lu_object_header, loh_hash);
936 static void *lu_obj_hop_key(struct hlist_node *hnode)
938 struct lu_object_header *h;
940 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
944 static int lu_obj_hop_keycmp(const void *key, struct hlist_node *hnode)
946 struct lu_object_header *h;
948 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
949 return lu_fid_eq(&h->loh_fid, (struct lu_fid *)key);
952 static void lu_obj_hop_get(struct cfs_hash *hs, struct hlist_node *hnode)
954 struct lu_object_header *h;
956 h = hlist_entry(hnode, struct lu_object_header, loh_hash);
957 atomic_inc(&h->loh_ref);
960 static void lu_obj_hop_put_locked(struct cfs_hash *hs, struct hlist_node *hnode)
962 LBUG(); /* we should never called it */
965 static struct cfs_hash_ops lu_site_hash_ops = {
966 .hs_hash = lu_obj_hop_hash,
967 .hs_key = lu_obj_hop_key,
968 .hs_keycmp = lu_obj_hop_keycmp,
969 .hs_object = lu_obj_hop_object,
970 .hs_get = lu_obj_hop_get,
971 .hs_put_locked = lu_obj_hop_put_locked,
974 static void lu_dev_add_linkage(struct lu_site *s, struct lu_device *d)
976 spin_lock(&s->ls_ld_lock);
977 if (list_empty(&d->ld_linkage))
978 list_add(&d->ld_linkage, &s->ls_ld_linkage);
979 spin_unlock(&s->ls_ld_lock);
983 * Initialize site \a s, with \a d as the top level device.
985 int lu_site_init(struct lu_site *s, struct lu_device *top)
987 struct lu_site_bkt_data *bkt;
988 struct cfs_hash_bd bd;
993 memset(s, 0, sizeof(*s));
994 mutex_init(&s->ls_purge_mutex);
995 snprintf(name, sizeof(name), "lu_site_%s", top->ld_type->ldt_name);
996 for (bits = lu_htable_order(top); bits >= LU_SITE_BITS_MIN; bits--) {
997 s->ls_obj_hash = cfs_hash_create(name, bits, bits,
998 bits - LU_SITE_BKT_BITS,
1001 CFS_HASH_SPIN_BKTLOCK |
1002 CFS_HASH_NO_ITEMREF |
1004 CFS_HASH_ASSERT_EMPTY |
1010 if (!s->ls_obj_hash) {
1011 CERROR("failed to create lu_site hash with bits: %lu\n", bits);
1015 cfs_hash_for_each_bucket(s->ls_obj_hash, &bd, i) {
1016 bkt = cfs_hash_bd_extra_get(s->ls_obj_hash, &bd);
1017 INIT_LIST_HEAD(&bkt->lsb_lru);
1018 init_waitqueue_head(&bkt->lsb_marche_funebre);
1021 s->ls_stats = lprocfs_alloc_stats(LU_SS_LAST_STAT, 0);
1023 cfs_hash_putref(s->ls_obj_hash);
1024 s->ls_obj_hash = NULL;
1028 lprocfs_counter_init(s->ls_stats, LU_SS_CREATED,
1029 0, "created", "created");
1030 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_HIT,
1031 0, "cache_hit", "cache_hit");
1032 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_MISS,
1033 0, "cache_miss", "cache_miss");
1034 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_RACE,
1035 0, "cache_race", "cache_race");
1036 lprocfs_counter_init(s->ls_stats, LU_SS_CACHE_DEATH_RACE,
1037 0, "cache_death_race", "cache_death_race");
1038 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_PURGED,
1039 0, "lru_purged", "lru_purged");
1041 * Unlike other counters, lru_len can be decremented so
1042 * need lc_sum instead of just lc_count
1044 lprocfs_counter_init(s->ls_stats, LU_SS_LRU_LEN,
1045 LPROCFS_CNTR_AVGMINMAX, "lru_len", "lru_len");
1047 INIT_LIST_HEAD(&s->ls_linkage);
1048 s->ls_top_dev = top;
1051 lu_ref_add(&top->ld_reference, "site-top", s);
1053 INIT_LIST_HEAD(&s->ls_ld_linkage);
1054 spin_lock_init(&s->ls_ld_lock);
1056 lu_dev_add_linkage(s, top);
1060 EXPORT_SYMBOL(lu_site_init);
1063 * Finalize \a s and release its resources.
1065 void lu_site_fini(struct lu_site *s)
1067 mutex_lock(&lu_sites_guard);
1068 list_del_init(&s->ls_linkage);
1069 mutex_unlock(&lu_sites_guard);
1071 if (s->ls_obj_hash) {
1072 cfs_hash_putref(s->ls_obj_hash);
1073 s->ls_obj_hash = NULL;
1076 if (s->ls_top_dev) {
1077 s->ls_top_dev->ld_site = NULL;
1078 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
1079 lu_device_put(s->ls_top_dev);
1080 s->ls_top_dev = NULL;
1084 lprocfs_free_stats(&s->ls_stats);
1086 EXPORT_SYMBOL(lu_site_fini);
1089 * Called when initialization of stack for this site is completed.
1091 int lu_site_init_finish(struct lu_site *s)
1095 mutex_lock(&lu_sites_guard);
1096 result = lu_context_refill(&lu_shrink_env.le_ctx);
1098 list_add(&s->ls_linkage, &lu_sites);
1099 mutex_unlock(&lu_sites_guard);
1102 EXPORT_SYMBOL(lu_site_init_finish);
1105 * Acquire additional reference on device \a d
1107 void lu_device_get(struct lu_device *d)
1109 atomic_inc(&d->ld_ref);
1111 EXPORT_SYMBOL(lu_device_get);
1114 * Release reference on device \a d.
1116 void lu_device_put(struct lu_device *d)
1118 LASSERT(atomic_read(&d->ld_ref) > 0);
1119 atomic_dec(&d->ld_ref);
1121 EXPORT_SYMBOL(lu_device_put);
1124 * Initialize device \a d of type \a t.
1126 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
1128 if (atomic_inc_return(&t->ldt_device_nr) == 1 &&
1129 t->ldt_ops->ldto_start)
1130 t->ldt_ops->ldto_start(t);
1132 memset(d, 0, sizeof(*d));
1133 atomic_set(&d->ld_ref, 0);
1135 lu_ref_init(&d->ld_reference);
1136 INIT_LIST_HEAD(&d->ld_linkage);
1139 EXPORT_SYMBOL(lu_device_init);
1142 * Finalize device \a d.
1144 void lu_device_fini(struct lu_device *d)
1146 struct lu_device_type *t = d->ld_type;
1149 d->ld_obd->obd_lu_dev = NULL;
1153 lu_ref_fini(&d->ld_reference);
1154 LASSERTF(atomic_read(&d->ld_ref) == 0,
1155 "Refcount is %u\n", atomic_read(&d->ld_ref));
1156 LASSERT(atomic_read(&t->ldt_device_nr) > 0);
1158 if (atomic_dec_and_test(&t->ldt_device_nr) &&
1159 t->ldt_ops->ldto_stop)
1160 t->ldt_ops->ldto_stop(t);
1162 EXPORT_SYMBOL(lu_device_fini);
1165 * Initialize object \a o that is part of compound object \a h and was created
1168 int lu_object_init(struct lu_object *o, struct lu_object_header *h,
1169 struct lu_device *d)
1171 memset(o, 0, sizeof(*o));
1175 lu_ref_add_at(&d->ld_reference, &o->lo_dev_ref, "lu_object", o);
1176 INIT_LIST_HEAD(&o->lo_linkage);
1180 EXPORT_SYMBOL(lu_object_init);
1183 * Finalize object and release its resources.
1185 void lu_object_fini(struct lu_object *o)
1187 struct lu_device *dev = o->lo_dev;
1189 LASSERT(list_empty(&o->lo_linkage));
1192 lu_ref_del_at(&dev->ld_reference, &o->lo_dev_ref,
1198 EXPORT_SYMBOL(lu_object_fini);
1201 * Add object \a o as first layer of compound object \a h
1203 * This is typically called by the ->ldo_object_alloc() method of top-level
1206 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
1208 list_move(&o->lo_linkage, &h->loh_layers);
1210 EXPORT_SYMBOL(lu_object_add_top);
1213 * Add object \a o as a layer of compound object, going after \a before.
1215 * This is typically called by the ->ldo_object_alloc() method of \a
1218 void lu_object_add(struct lu_object *before, struct lu_object *o)
1220 list_move(&o->lo_linkage, &before->lo_linkage);
1222 EXPORT_SYMBOL(lu_object_add);
1225 * Initialize compound object.
1227 int lu_object_header_init(struct lu_object_header *h)
1229 memset(h, 0, sizeof(*h));
1230 atomic_set(&h->loh_ref, 1);
1231 INIT_HLIST_NODE(&h->loh_hash);
1232 INIT_LIST_HEAD(&h->loh_lru);
1233 INIT_LIST_HEAD(&h->loh_layers);
1234 lu_ref_init(&h->loh_reference);
1237 EXPORT_SYMBOL(lu_object_header_init);
1240 * Finalize compound object.
1242 void lu_object_header_fini(struct lu_object_header *h)
1244 LASSERT(list_empty(&h->loh_layers));
1245 LASSERT(list_empty(&h->loh_lru));
1246 LASSERT(hlist_unhashed(&h->loh_hash));
1247 lu_ref_fini(&h->loh_reference);
1249 EXPORT_SYMBOL(lu_object_header_fini);
1252 * Given a compound object, find its slice, corresponding to the device type
1255 struct lu_object *lu_object_locate(struct lu_object_header *h,
1256 const struct lu_device_type *dtype)
1258 struct lu_object *o;
1260 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
1261 if (o->lo_dev->ld_type == dtype)
1266 EXPORT_SYMBOL(lu_object_locate);
1269 * Finalize and free devices in the device stack.
1271 * Finalize device stack by purging object cache, and calling
1272 * lu_device_type_operations::ldto_device_fini() and
1273 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
1275 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
1277 struct lu_site *site = top->ld_site;
1278 struct lu_device *scan;
1279 struct lu_device *next;
1281 lu_site_purge(env, site, ~0);
1282 for (scan = top; scan; scan = next) {
1283 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
1284 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
1285 lu_device_put(scan);
1289 lu_site_purge(env, site, ~0);
1291 for (scan = top; scan; scan = next) {
1292 const struct lu_device_type *ldt = scan->ld_type;
1293 struct obd_type *type;
1295 next = ldt->ldt_ops->ldto_device_free(env, scan);
1296 type = ldt->ldt_obd_type;
1299 class_put_type(type);
1306 * Maximal number of tld slots.
1308 LU_CONTEXT_KEY_NR = 40
1311 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1313 static DEFINE_SPINLOCK(lu_keys_guard);
1316 * Global counter incremented whenever key is registered, unregistered,
1317 * revived or quiesced. This is used to void unnecessary calls to
1318 * lu_context_refill(). No locking is provided, as initialization and shutdown
1319 * are supposed to be externally serialized.
1321 static unsigned key_set_version;
1326 int lu_context_key_register(struct lu_context_key *key)
1331 LASSERT(key->lct_init);
1332 LASSERT(key->lct_fini);
1333 LASSERT(key->lct_tags != 0);
1336 spin_lock(&lu_keys_guard);
1337 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1340 atomic_set(&key->lct_used, 1);
1342 lu_ref_init(&key->lct_reference);
1348 spin_unlock(&lu_keys_guard);
1351 EXPORT_SYMBOL(lu_context_key_register);
1353 static void key_fini(struct lu_context *ctx, int index)
1355 if (ctx->lc_value && ctx->lc_value[index]) {
1356 struct lu_context_key *key;
1358 key = lu_keys[index];
1359 LASSERT(atomic_read(&key->lct_used) > 1);
1361 key->lct_fini(ctx, key, ctx->lc_value[index]);
1362 lu_ref_del(&key->lct_reference, "ctx", ctx);
1363 atomic_dec(&key->lct_used);
1365 if ((ctx->lc_tags & LCT_NOREF) == 0) {
1366 #ifdef CONFIG_MODULE_UNLOAD
1367 LINVRNT(module_refcount(key->lct_owner) > 0);
1369 module_put(key->lct_owner);
1371 ctx->lc_value[index] = NULL;
1378 void lu_context_key_degister(struct lu_context_key *key)
1380 LASSERT(atomic_read(&key->lct_used) >= 1);
1381 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1383 lu_context_key_quiesce(key);
1386 spin_lock(&lu_keys_guard);
1387 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1388 if (lu_keys[key->lct_index]) {
1389 lu_keys[key->lct_index] = NULL;
1390 lu_ref_fini(&key->lct_reference);
1392 spin_unlock(&lu_keys_guard);
1394 LASSERTF(atomic_read(&key->lct_used) == 1,
1395 "key has instances: %d\n",
1396 atomic_read(&key->lct_used));
1398 EXPORT_SYMBOL(lu_context_key_degister);
1401 * Register a number of keys. This has to be called after all keys have been
1402 * initialized by a call to LU_CONTEXT_KEY_INIT().
1404 int lu_context_key_register_many(struct lu_context_key *k, ...)
1406 struct lu_context_key *key = k;
1412 result = lu_context_key_register(key);
1415 key = va_arg(args, struct lu_context_key *);
1422 lu_context_key_degister(k);
1423 k = va_arg(args, struct lu_context_key *);
1430 EXPORT_SYMBOL(lu_context_key_register_many);
1433 * De-register a number of keys. This is a dual to
1434 * lu_context_key_register_many().
1436 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1442 lu_context_key_degister(k);
1443 k = va_arg(args, struct lu_context_key*);
1447 EXPORT_SYMBOL(lu_context_key_degister_many);
1450 * Revive a number of keys.
1452 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1458 lu_context_key_revive(k);
1459 k = va_arg(args, struct lu_context_key*);
1463 EXPORT_SYMBOL(lu_context_key_revive_many);
1466 * Quiescent a number of keys.
1468 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1474 lu_context_key_quiesce(k);
1475 k = va_arg(args, struct lu_context_key*);
1479 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1482 * Return value associated with key \a key in context \a ctx.
1484 void *lu_context_key_get(const struct lu_context *ctx,
1485 const struct lu_context_key *key)
1487 LINVRNT(ctx->lc_state == LCS_ENTERED);
1488 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1489 LASSERT(lu_keys[key->lct_index] == key);
1490 return ctx->lc_value[key->lct_index];
1492 EXPORT_SYMBOL(lu_context_key_get);
1495 * List of remembered contexts. XXX document me.
1497 static LIST_HEAD(lu_context_remembered);
1500 * Destroy \a key in all remembered contexts. This is used to destroy key
1501 * values in "shared" contexts (like service threads), when a module owning
1502 * the key is about to be unloaded.
1504 void lu_context_key_quiesce(struct lu_context_key *key)
1506 struct lu_context *ctx;
1508 if (!(key->lct_tags & LCT_QUIESCENT)) {
1510 * XXX layering violation.
1512 cl_env_cache_purge(~0);
1513 key->lct_tags |= LCT_QUIESCENT;
1515 * XXX memory barrier has to go here.
1517 spin_lock(&lu_keys_guard);
1518 list_for_each_entry(ctx, &lu_context_remembered, lc_remember)
1519 key_fini(ctx, key->lct_index);
1520 spin_unlock(&lu_keys_guard);
1525 void lu_context_key_revive(struct lu_context_key *key)
1527 key->lct_tags &= ~LCT_QUIESCENT;
1531 static void keys_fini(struct lu_context *ctx)
1538 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1541 kfree(ctx->lc_value);
1542 ctx->lc_value = NULL;
1545 static int keys_fill(struct lu_context *ctx)
1549 LINVRNT(ctx->lc_value);
1550 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1551 struct lu_context_key *key;
1554 if (!ctx->lc_value[i] && key &&
1555 (key->lct_tags & ctx->lc_tags) &&
1557 * Don't create values for a LCT_QUIESCENT key, as this
1558 * will pin module owning a key.
1560 !(key->lct_tags & LCT_QUIESCENT)) {
1563 LINVRNT(key->lct_init);
1564 LINVRNT(key->lct_index == i);
1566 value = key->lct_init(ctx, key);
1568 return PTR_ERR(value);
1570 if (!(ctx->lc_tags & LCT_NOREF))
1571 try_module_get(key->lct_owner);
1572 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1573 atomic_inc(&key->lct_used);
1575 * This is the only place in the code, where an
1576 * element of ctx->lc_value[] array is set to non-NULL
1579 ctx->lc_value[i] = value;
1581 ctx->lc_tags |= LCT_HAS_EXIT;
1583 ctx->lc_version = key_set_version;
1588 static int keys_init(struct lu_context *ctx)
1590 ctx->lc_value = kcalloc(ARRAY_SIZE(lu_keys), sizeof(ctx->lc_value[0]),
1592 if (likely(ctx->lc_value))
1593 return keys_fill(ctx);
1599 * Initialize context data-structure. Create values for all keys.
1601 int lu_context_init(struct lu_context *ctx, __u32 tags)
1605 memset(ctx, 0, sizeof(*ctx));
1606 ctx->lc_state = LCS_INITIALIZED;
1607 ctx->lc_tags = tags;
1608 if (tags & LCT_REMEMBER) {
1609 spin_lock(&lu_keys_guard);
1610 list_add(&ctx->lc_remember, &lu_context_remembered);
1611 spin_unlock(&lu_keys_guard);
1613 INIT_LIST_HEAD(&ctx->lc_remember);
1616 rc = keys_init(ctx);
1618 lu_context_fini(ctx);
1622 EXPORT_SYMBOL(lu_context_init);
1625 * Finalize context data-structure. Destroy key values.
1627 void lu_context_fini(struct lu_context *ctx)
1629 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1630 ctx->lc_state = LCS_FINALIZED;
1632 if ((ctx->lc_tags & LCT_REMEMBER) == 0) {
1633 LASSERT(list_empty(&ctx->lc_remember));
1636 } else { /* could race with key degister */
1637 spin_lock(&lu_keys_guard);
1639 list_del_init(&ctx->lc_remember);
1640 spin_unlock(&lu_keys_guard);
1643 EXPORT_SYMBOL(lu_context_fini);
1646 * Called before entering context.
1648 void lu_context_enter(struct lu_context *ctx)
1650 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1651 ctx->lc_state = LCS_ENTERED;
1653 EXPORT_SYMBOL(lu_context_enter);
1656 * Called after exiting from \a ctx
1658 void lu_context_exit(struct lu_context *ctx)
1662 LINVRNT(ctx->lc_state == LCS_ENTERED);
1663 ctx->lc_state = LCS_LEFT;
1664 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value) {
1665 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1666 if (ctx->lc_value[i]) {
1667 struct lu_context_key *key;
1672 key, ctx->lc_value[i]);
1677 EXPORT_SYMBOL(lu_context_exit);
1680 * Allocate for context all missing keys that were registered after context
1681 * creation. key_set_version is only changed in rare cases when modules
1682 * are loaded and removed.
1684 int lu_context_refill(struct lu_context *ctx)
1686 return likely(ctx->lc_version == key_set_version) ? 0 : keys_fill(ctx);
1690 * lu_ctx_tags/lu_ses_tags will be updated if there are new types of
1691 * obd being added. Currently, this is only used on client side, specifically
1692 * for echo device client, for other stack (like ptlrpc threads), context are
1693 * predefined when the lu_device type are registered, during the module probe
1696 __u32 lu_context_tags_default;
1697 __u32 lu_session_tags_default;
1699 int lu_env_init(struct lu_env *env, __u32 tags)
1704 result = lu_context_init(&env->le_ctx, tags);
1705 if (likely(result == 0))
1706 lu_context_enter(&env->le_ctx);
1709 EXPORT_SYMBOL(lu_env_init);
1711 void lu_env_fini(struct lu_env *env)
1713 lu_context_exit(&env->le_ctx);
1714 lu_context_fini(&env->le_ctx);
1717 EXPORT_SYMBOL(lu_env_fini);
1719 int lu_env_refill(struct lu_env *env)
1723 result = lu_context_refill(&env->le_ctx);
1724 if (result == 0 && env->le_ses)
1725 result = lu_context_refill(env->le_ses);
1728 EXPORT_SYMBOL(lu_env_refill);
1730 struct lu_site_stats {
1731 unsigned lss_populated;
1732 unsigned lss_max_search;
1737 static void lu_site_stats_get(struct cfs_hash *hs,
1738 struct lu_site_stats *stats, int populated)
1740 struct cfs_hash_bd bd;
1743 cfs_hash_for_each_bucket(hs, &bd, i) {
1744 struct lu_site_bkt_data *bkt = cfs_hash_bd_extra_get(hs, &bd);
1745 struct hlist_head *hhead;
1747 cfs_hash_bd_lock(hs, &bd, 1);
1749 cfs_hash_bd_count_get(&bd) - bkt->lsb_lru_len;
1750 stats->lss_total += cfs_hash_bd_count_get(&bd);
1751 stats->lss_max_search = max((int)stats->lss_max_search,
1752 cfs_hash_bd_depmax_get(&bd));
1754 cfs_hash_bd_unlock(hs, &bd, 1);
1758 cfs_hash_bd_for_each_hlist(hs, &bd, hhead) {
1759 if (!hlist_empty(hhead))
1760 stats->lss_populated++;
1762 cfs_hash_bd_unlock(hs, &bd, 1);
1767 * lu_cache_shrink_count returns the number of cached objects that are
1768 * candidates to be freed by shrink_slab(). A counter, which tracks
1769 * the number of items in the site's lru, is maintained in the per cpu
1770 * stats of each site. The counter is incremented when an object is added
1771 * to a site's lru and decremented when one is removed. The number of
1772 * free-able objects is the sum of all per cpu counters for all sites.
1774 * Using a per cpu counter is a compromise solution to concurrent access:
1775 * lu_object_put() can update the counter without locking the site and
1776 * lu_cache_shrink_count can sum the counters without locking each
1777 * ls_obj_hash bucket.
1779 static unsigned long lu_cache_shrink_count(struct shrinker *sk,
1780 struct shrink_control *sc)
1783 struct lu_site *tmp;
1784 unsigned long cached = 0;
1786 if (!(sc->gfp_mask & __GFP_FS))
1789 mutex_lock(&lu_sites_guard);
1790 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1791 cached += ls_stats_read(s->ls_stats, LU_SS_LRU_LEN);
1793 mutex_unlock(&lu_sites_guard);
1795 cached = (cached / 100) * sysctl_vfs_cache_pressure;
1796 CDEBUG(D_INODE, "%ld objects cached, cache pressure %d\n",
1797 cached, sysctl_vfs_cache_pressure);
1802 static unsigned long lu_cache_shrink_scan(struct shrinker *sk,
1803 struct shrink_control *sc)
1806 struct lu_site *tmp;
1807 unsigned long remain = sc->nr_to_scan, freed = 0;
1810 if (!(sc->gfp_mask & __GFP_FS))
1811 /* We must not take the lu_sites_guard lock when
1812 * __GFP_FS is *not* set because of the deadlock
1813 * possibility detailed above. Additionally,
1814 * since we cannot determine the number of
1815 * objects in the cache without taking this
1816 * lock, we're in a particularly tough spot. As
1817 * a result, we'll just lie and say our cache is
1818 * empty. This _should_ be ok, as we can't
1819 * reclaim objects when __GFP_FS is *not* set
1824 mutex_lock(&lu_sites_guard);
1825 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1826 freed = lu_site_purge(&lu_shrink_env, s, remain);
1829 * Move just shrunk site to the tail of site list to
1830 * assure shrinking fairness.
1832 list_move_tail(&s->ls_linkage, &splice);
1834 list_splice(&splice, lu_sites.prev);
1835 mutex_unlock(&lu_sites_guard);
1837 return sc->nr_to_scan - remain;
1841 * Debugging printer function using printk().
1843 static struct shrinker lu_site_shrinker = {
1844 .count_objects = lu_cache_shrink_count,
1845 .scan_objects = lu_cache_shrink_scan,
1846 .seeks = DEFAULT_SEEKS,
1850 * Initialization of global lu_* data.
1852 int lu_global_init(void)
1856 CDEBUG(D_INFO, "Lustre LU module (%p).\n", &lu_keys);
1858 result = lu_ref_global_init();
1862 LU_CONTEXT_KEY_INIT(&lu_global_key);
1863 result = lu_context_key_register(&lu_global_key);
1868 * At this level, we don't know what tags are needed, so allocate them
1869 * conservatively. This should not be too bad, because this
1870 * environment is global.
1872 mutex_lock(&lu_sites_guard);
1873 result = lu_env_init(&lu_shrink_env, LCT_SHRINKER);
1874 mutex_unlock(&lu_sites_guard);
1879 * seeks estimation: 3 seeks to read a record from oi, one to read
1880 * inode, one for ea. Unfortunately setting this high value results in
1881 * lu_object/inode cache consuming all the memory.
1883 register_shrinker(&lu_site_shrinker);
1889 * Dual to lu_global_init().
1891 void lu_global_fini(void)
1893 unregister_shrinker(&lu_site_shrinker);
1894 lu_context_key_degister(&lu_global_key);
1897 * Tear shrinker environment down _after_ de-registering
1898 * lu_global_key, because the latter has a value in the former.
1900 mutex_lock(&lu_sites_guard);
1901 lu_env_fini(&lu_shrink_env);
1902 mutex_unlock(&lu_sites_guard);
1904 lu_ref_global_fini();
1907 static __u32 ls_stats_read(struct lprocfs_stats *stats, int idx)
1909 struct lprocfs_counter ret;
1911 lprocfs_stats_collect(stats, idx, &ret);
1912 if (idx == LU_SS_LRU_LEN)
1914 * protect against counter on cpu A being decremented
1915 * before counter is incremented on cpu B; unlikely
1917 return (__u32)((ret.lc_sum > 0) ? ret.lc_sum : 0);
1919 return (__u32)ret.lc_count;
1923 * Output site statistical counters into a buffer. Suitable for
1924 * lprocfs_rd_*()-style functions.
1926 int lu_site_stats_print(const struct lu_site *s, struct seq_file *m)
1928 struct lu_site_stats stats;
1930 memset(&stats, 0, sizeof(stats));
1931 lu_site_stats_get(s->ls_obj_hash, &stats, 1);
1933 seq_printf(m, "%d/%d %d/%d %d %d %d %d %d %d %d %d\n",
1936 stats.lss_populated,
1937 CFS_HASH_NHLIST(s->ls_obj_hash),
1938 stats.lss_max_search,
1939 ls_stats_read(s->ls_stats, LU_SS_CREATED),
1940 ls_stats_read(s->ls_stats, LU_SS_CACHE_HIT),
1941 ls_stats_read(s->ls_stats, LU_SS_CACHE_MISS),
1942 ls_stats_read(s->ls_stats, LU_SS_CACHE_RACE),
1943 ls_stats_read(s->ls_stats, LU_SS_CACHE_DEATH_RACE),
1944 ls_stats_read(s->ls_stats, LU_SS_LRU_PURGED),
1945 ls_stats_read(s->ls_stats, LU_SS_LRU_LEN));
1948 EXPORT_SYMBOL(lu_site_stats_print);
1951 * Helper function to initialize a number of kmem slab caches at once.
1953 int lu_kmem_init(struct lu_kmem_descr *caches)
1956 struct lu_kmem_descr *iter = caches;
1958 for (result = 0; iter->ckd_cache; ++iter) {
1959 *iter->ckd_cache = kmem_cache_create(iter->ckd_name,
1962 if (!*iter->ckd_cache) {
1964 /* free all previously allocated caches */
1965 lu_kmem_fini(caches);
1971 EXPORT_SYMBOL(lu_kmem_init);
1974 * Helper function to finalize a number of kmem slab cached at once. Dual to
1977 void lu_kmem_fini(struct lu_kmem_descr *caches)
1979 for (; caches->ckd_cache; ++caches) {
1980 kmem_cache_destroy(*caches->ckd_cache);
1981 *caches->ckd_cache = NULL;
1984 EXPORT_SYMBOL(lu_kmem_fini);
1986 void lu_buf_free(struct lu_buf *buf)
1990 LASSERT(buf->lb_len > 0);
1991 kvfree(buf->lb_buf);
1996 EXPORT_SYMBOL(lu_buf_free);
1998 void lu_buf_alloc(struct lu_buf *buf, size_t size)
2001 LASSERT(!buf->lb_buf);
2002 LASSERT(!buf->lb_len);
2003 buf->lb_buf = libcfs_kvzalloc(size, GFP_NOFS);
2004 if (likely(buf->lb_buf))
2007 EXPORT_SYMBOL(lu_buf_alloc);
2009 void lu_buf_realloc(struct lu_buf *buf, size_t size)
2012 lu_buf_alloc(buf, size);
2014 EXPORT_SYMBOL(lu_buf_realloc);
2016 struct lu_buf *lu_buf_check_and_alloc(struct lu_buf *buf, size_t len)
2018 if (!buf->lb_buf && !buf->lb_len)
2019 lu_buf_alloc(buf, len);
2021 if ((len > buf->lb_len) && buf->lb_buf)
2022 lu_buf_realloc(buf, len);
2026 EXPORT_SYMBOL(lu_buf_check_and_alloc);
2029 * Increase the size of the \a buf.
2030 * preserves old data in buffer
2031 * old buffer remains unchanged on error
2032 * \retval 0 or -ENOMEM
2034 int lu_buf_check_and_grow(struct lu_buf *buf, size_t len)
2038 if (len <= buf->lb_len)
2041 ptr = libcfs_kvzalloc(len, GFP_NOFS);
2045 /* Free the old buf */
2047 memcpy(ptr, buf->lb_buf, buf->lb_len);
2048 kvfree(buf->lb_buf);