1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/lu_object.c
39 * These are the only exported functions, they provide some generic
40 * infrastructure for managing object devices
42 * Author: Nikita Danilov <nikita@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_CLASS
47 # define EXPORT_SYMTAB
50 #include <linux/seq_file.h>
51 #include <linux/module.h>
53 #include <linux/swap.h>
55 #include <libcfs/libcfs_hash.h>
56 #include <obd_support.h>
57 #include <lustre_disk.h>
58 #include <lustre_fid.h>
59 #include <lu_object.h>
60 #include <libcfs/list.h>
61 /* lu_time_global_{init,fini}() */
64 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
67 * Decrease reference counter on object. If last reference is freed, return
68 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
69 * case, free object immediately.
71 void lu_object_put(const struct lu_env *env, struct lu_object *o)
73 struct lu_object_header *top;
75 struct lu_object *orig;
79 site = o->lo_dev->ld_site;
82 write_lock(&site->ls_guard);
83 if (atomic_dec_and_test(&top->loh_ref)) {
85 * When last reference is released, iterate over object
86 * layers, and notify them that object is no longer busy.
88 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
89 if (o->lo_ops->loo_object_release != NULL)
90 o->lo_ops->loo_object_release(env, o);
93 if (lu_object_is_dying(top)) {
95 * If object is dying (will not be cached), removed it
96 * from hash table and LRU.
98 * This is done with hash table and LRU lists
99 * locked. As the only way to acquire first reference
100 * to previously unreferenced object is through
101 * hash-table lookup (lu_object_find()), or LRU
102 * scanning (lu_site_purge()), that are done under
103 * hash-table and LRU lock, no race with concurrent
104 * object lookup is possible and we can safely destroy
107 hlist_del_init(&top->loh_hash);
108 list_del_init(&top->loh_lru);
113 write_unlock(&site->ls_guard);
116 * Object was already removed from hash and lru above, can
119 lu_object_free(env, orig);
121 EXPORT_SYMBOL(lu_object_put);
124 * Allocate new object.
126 * This follows object creation protocol, described in the comment within
127 * struct lu_device_operations definition.
129 static struct lu_object *lu_object_alloc(const struct lu_env *env,
131 const struct lu_fid *f)
133 struct lu_object *scan;
134 struct lu_object *top;
135 struct list_head *layers;
141 * Create top-level object slice. This will also create
144 top = s->ls_top_dev->ld_ops->ldo_object_alloc(env,
145 NULL, s->ls_top_dev);
147 RETURN(ERR_PTR(-ENOMEM));
149 * This is the only place where object fid is assigned. It's constant
152 LASSERT(fid_is_igif(f) || fid_ver(f) == 0);
153 top->lo_header->loh_fid = *f;
154 layers = &top->lo_header->loh_layers;
157 * Call ->loo_object_init() repeatedly, until no more new
158 * object slices are created.
161 list_for_each_entry(scan, layers, lo_linkage) {
162 if (scan->lo_flags & LU_OBJECT_ALLOCATED)
165 scan->lo_header = top->lo_header;
166 result = scan->lo_ops->loo_object_init(env, scan);
168 lu_object_free(env, top);
169 RETURN(ERR_PTR(result));
171 scan->lo_flags |= LU_OBJECT_ALLOCATED;
175 list_for_each_entry_reverse(scan, layers, lo_linkage) {
176 if (scan->lo_ops->loo_object_start != NULL) {
177 result = scan->lo_ops->loo_object_start(env, scan);
179 lu_object_free(env, top);
180 RETURN(ERR_PTR(result));
185 s->ls_stats.s_created ++;
192 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
194 struct list_head splice;
195 struct lu_object *scan;
198 * First call ->loo_object_delete() method to release all resources.
200 list_for_each_entry_reverse(scan,
201 &o->lo_header->loh_layers, lo_linkage) {
202 if (scan->lo_ops->loo_object_delete != NULL)
203 scan->lo_ops->loo_object_delete(env, scan);
207 * Then, splice object layers into stand-alone list, and call
208 * ->loo_object_free() on all layers to free memory. Splice is
209 * necessary, because lu_object_header is freed together with the
212 CFS_INIT_LIST_HEAD(&splice);
213 list_splice_init(&o->lo_header->loh_layers, &splice);
214 while (!list_empty(&splice)) {
215 o = container_of0(splice.next, struct lu_object, lo_linkage);
216 list_del_init(&o->lo_linkage);
217 LASSERT(o->lo_ops->loo_object_free != NULL);
218 o->lo_ops->loo_object_free(env, o);
223 * Free @nr objects from the cold end of the site LRU list.
225 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
227 struct list_head dispose;
228 struct lu_object_header *h;
229 struct lu_object_header *temp;
231 CFS_INIT_LIST_HEAD(&dispose);
233 * Under LRU list lock, scan LRU list and move unreferenced objects to
234 * the dispose list, removing them from LRU and hash table.
236 write_lock(&s->ls_guard);
237 list_for_each_entry_safe(h, temp, &s->ls_lru, loh_lru) {
239 * Objects are sorted in lru order, and "busy" objects (ones
240 * with h->loh_ref > 0) naturally tend to live near hot end
241 * that we scan last. Unfortunately, sites usually have small
242 * (less then ten) number of busy yet rarely accessed objects
243 * (some global objects, accessed directly through pointers,
244 * bypassing hash table). Currently algorithm scans them over
245 * and over again. Probably we should move busy objects out of
246 * LRU, or we can live with that.
250 if (atomic_read(&h->loh_ref) > 0)
252 hlist_del_init(&h->loh_hash);
253 list_move(&h->loh_lru, &dispose);
256 write_unlock(&s->ls_guard);
258 * Free everything on the dispose list. This is safe against races due
259 * to the reasons described in lu_object_put().
261 while (!list_empty(&dispose)) {
262 h = container_of0(dispose.next,
263 struct lu_object_header, loh_lru);
264 list_del_init(&h->loh_lru);
265 lu_object_free(env, lu_object_top(h));
266 s->ls_stats.s_lru_purged ++;
270 EXPORT_SYMBOL(lu_site_purge);
275 * Code below has to jump through certain loops to output object description
276 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
277 * composes object description from strings that are parts of _lines_ of
278 * output (i.e., strings that are not terminated by newline). This doesn't fit
279 * very well into libcfs_debug_msg() interface that assumes that each message
280 * supplied to it is a self-contained output line.
282 * To work around this, strings are collected in a temporary buffer
283 * (implemented as a value of lu_cdebug_key key), until terminating newline
284 * character is detected.
292 * XXX overflow is not handled correctly.
297 struct lu_cdebug_data {
301 char lck_area[LU_CDEBUG_LINE];
303 * fid staging area used by dt_store_open().
305 struct lu_fid_pack lck_pack;
308 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
309 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
312 * Key, holding temporary buffer. This key is registered very early by
315 struct lu_context_key lu_global_key = {
316 .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
317 .lct_init = lu_global_key_init,
318 .lct_fini = lu_global_key_fini
322 * Printer function emitting messages through libcfs_debug_msg().
324 int lu_cdebug_printer(const struct lu_env *env,
325 void *cookie, const char *format, ...)
327 struct lu_cdebug_print_info *info = cookie;
328 struct lu_cdebug_data *key;
333 va_start(args, format);
335 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
336 LASSERT(key != NULL);
338 used = strlen(key->lck_area);
339 complete = format[strlen(format) - 1] == '\n';
341 * Append new chunk to the buffer.
343 vsnprintf(key->lck_area + used,
344 ARRAY_SIZE(key->lck_area) - used, format, args);
346 libcfs_debug_msg(NULL, info->lpi_subsys, info->lpi_mask,
347 (char *)info->lpi_file, info->lpi_fn,
348 info->lpi_line, "%s", key->lck_area);
349 key->lck_area[0] = 0;
354 EXPORT_SYMBOL(lu_cdebug_printer);
357 * Print object header.
359 static void lu_object_header_print(const struct lu_env *env,
360 void *cookie, lu_printer_t printer,
361 const struct lu_object_header *hdr)
363 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
364 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
366 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
367 list_empty(&hdr->loh_lru) ? "" : " lru",
368 hdr->loh_attr & LOHA_EXISTS ? " exist":"");
372 * Print human readable representation of the @o to the @printer.
374 void lu_object_print(const struct lu_env *env, void *cookie,
375 lu_printer_t printer, const struct lu_object *o)
377 static const char ruler[] = "........................................";
378 struct lu_object_header *top;
382 lu_object_header_print(env, cookie, printer, top);
383 (*printer)(env, cookie, "\n");
384 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
385 depth = o->lo_depth + 4;
386 LASSERT(o->lo_ops->loo_object_print != NULL);
388 * print `.' @depth times.
390 (*printer)(env, cookie, "%*.*s", depth, depth, ruler);
391 o->lo_ops->loo_object_print(env, cookie, printer, o);
392 (*printer)(env, cookie, "\n");
395 EXPORT_SYMBOL(lu_object_print);
398 * Check object consistency.
400 int lu_object_invariant(const struct lu_object *o)
402 struct lu_object_header *top;
405 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
406 if (o->lo_ops->loo_object_invariant != NULL &&
407 !o->lo_ops->loo_object_invariant(o))
412 EXPORT_SYMBOL(lu_object_invariant);
414 static struct lu_object *htable_lookup(struct lu_site *s,
415 const struct hlist_head *bucket,
416 const struct lu_fid *f)
418 struct lu_object_header *h;
419 struct hlist_node *scan;
421 hlist_for_each_entry(h, scan, bucket, loh_hash) {
422 s->ls_stats.s_cache_check ++;
423 if (likely(lu_fid_eq(&h->loh_fid, f) &&
424 !lu_object_is_dying(h))) {
425 /* bump reference count... */
426 if (atomic_add_return(1, &h->loh_ref) == 1)
428 /* and move to the head of the LRU */
430 * XXX temporary disable this to measure effects of
431 * read-write locking.
433 /* list_move_tail(&h->loh_lru, &s->ls_lru); */
434 s->ls_stats.s_cache_hit ++;
435 return lu_object_top(h);
438 s->ls_stats.s_cache_miss ++;
442 static __u32 fid_hash(const struct lu_fid *f, int bits)
444 /* all objects with same id and different versions will belong to same
445 * collisions list. */
446 return hash_long(fid_flatten(f), bits);
450 * Search cache for an object with the fid @f. If such object is found, return
451 * it. Otherwise, create new object, insert it into cache and return it. In
452 * any case, additional reference is acquired on the returned object.
454 struct lu_object *lu_object_find(const struct lu_env *env,
455 struct lu_site *s, const struct lu_fid *f)
458 struct lu_object *shadow;
459 struct hlist_head *bucket;
462 * This uses standard index maintenance protocol:
464 * - search index under lock, and return object if found;
465 * - otherwise, unlock index, allocate new object;
466 * - lock index and search again;
467 * - if nothing is found (usual case), insert newly created
469 * - otherwise (race: other thread inserted object), free
470 * object just allocated.
475 bucket = s->ls_hash + fid_hash(f, s->ls_hash_bits);
477 read_lock(&s->ls_guard);
478 o = htable_lookup(s, bucket, f);
479 read_unlock(&s->ls_guard);
485 * Allocate new object. This may result in rather complicated
486 * operations, including fld queries, inode loading, etc.
488 o = lu_object_alloc(env, s, f);
489 if (unlikely(IS_ERR(o)))
492 LASSERT(lu_fid_eq(lu_object_fid(o), f));
494 write_lock(&s->ls_guard);
495 shadow = htable_lookup(s, bucket, f);
496 if (likely(shadow == NULL)) {
497 hlist_add_head(&o->lo_header->loh_hash, bucket);
498 list_add_tail(&o->lo_header->loh_lru, &s->ls_lru);
504 s->ls_stats.s_cache_race ++;
505 write_unlock(&s->ls_guard);
507 lu_object_free(env, o);
510 EXPORT_SYMBOL(lu_object_find);
513 * Global list of all sites on this node
515 static CFS_LIST_HEAD(lu_sites);
516 static DECLARE_MUTEX(lu_sites_guard);
519 * Global environment used by site shrinker.
521 static struct lu_env lu_shrink_env;
524 * Print all objects in @s.
526 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
527 lu_printer_t printer)
531 for (i = 0; i < s->ls_hash_size; ++i) {
532 struct lu_object_header *h;
533 struct hlist_node *scan;
535 read_lock(&s->ls_guard);
536 hlist_for_each_entry(h, scan, &s->ls_hash[i], loh_hash) {
538 if (!list_empty(&h->loh_layers)) {
539 const struct lu_object *obj;
541 obj = lu_object_top(h);
542 lu_object_print(env, cookie, printer, obj);
544 lu_object_header_print(env, cookie, printer, h);
546 read_unlock(&s->ls_guard);
549 EXPORT_SYMBOL(lu_site_print);
552 LU_CACHE_PERCENT = 20,
556 * Return desired hash table order.
558 static int lu_htable_order(void)
560 unsigned long cache_size;
564 * Calculate hash table size, assuming that we want reasonable
565 * performance when 20% of total memory is occupied by cache of
568 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
570 cache_size = num_physpages;
572 #if BITS_PER_LONG == 32
573 /* limit hashtable size for lowmem systems to low RAM */
574 if (cache_size > 1 << (30 - CFS_PAGE_SHIFT))
575 cache_size = 1 << (30 - CFS_PAGE_SHIFT) * 3 / 4;
578 cache_size = cache_size / 100 * LU_CACHE_PERCENT *
579 (CFS_PAGE_SIZE / 1024);
581 for (bits = 1; (1 << bits) < cache_size; ++bits) {
588 * Initialize site @s, with @d as the top level device.
590 int lu_site_init(struct lu_site *s, struct lu_device *top)
597 memset(s, 0, sizeof *s);
598 rwlock_init(&s->ls_guard);
599 CFS_INIT_LIST_HEAD(&s->ls_lru);
600 CFS_INIT_LIST_HEAD(&s->ls_linkage);
605 for (bits = lu_htable_order(), size = 1 << bits;
607 cfs_alloc_large(size * sizeof s->ls_hash[0])) == NULL;
608 --bits, size >>= 1) {
610 * Scale hash table down, until allocation succeeds.
615 s->ls_hash_size = size;
616 s->ls_hash_bits = bits;
617 s->ls_hash_mask = size - 1;
619 for (i = 0; i < size; i++)
620 INIT_HLIST_HEAD(&s->ls_hash[i]);
624 EXPORT_SYMBOL(lu_site_init);
627 * Finalize @s and release its resources.
629 void lu_site_fini(struct lu_site *s)
631 LASSERT(list_empty(&s->ls_lru));
632 LASSERT(s->ls_total == 0);
634 down(&lu_sites_guard);
635 list_del_init(&s->ls_linkage);
638 if (s->ls_hash != NULL) {
640 for (i = 0; i < s->ls_hash_size; i++)
641 LASSERT(hlist_empty(&s->ls_hash[i]));
642 cfs_free_large(s->ls_hash);
645 if (s->ls_top_dev != NULL) {
646 s->ls_top_dev->ld_site = NULL;
647 lu_device_put(s->ls_top_dev);
648 s->ls_top_dev = NULL;
651 EXPORT_SYMBOL(lu_site_fini);
654 * Called when initialization of stack for this site is completed.
656 int lu_site_init_finish(struct lu_site *s)
659 down(&lu_sites_guard);
660 result = lu_context_refill(&lu_shrink_env.le_ctx);
662 list_add(&s->ls_linkage, &lu_sites);
666 EXPORT_SYMBOL(lu_site_init_finish);
669 * Acquire additional reference on device @d
671 void lu_device_get(struct lu_device *d)
673 atomic_inc(&d->ld_ref);
675 EXPORT_SYMBOL(lu_device_get);
678 * Release reference on device @d.
680 void lu_device_put(struct lu_device *d)
682 atomic_dec(&d->ld_ref);
684 EXPORT_SYMBOL(lu_device_put);
687 * Initialize device @d of type @t.
689 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
691 memset(d, 0, sizeof *d);
692 atomic_set(&d->ld_ref, 0);
696 EXPORT_SYMBOL(lu_device_init);
699 * Finalize device @d.
701 void lu_device_fini(struct lu_device *d)
703 if (d->ld_obd != NULL)
705 lprocfs_obd_cleanup(d->ld_obd);
707 LASSERTF(atomic_read(&d->ld_ref) == 0,
708 "Refcount is %u\n", atomic_read(&d->ld_ref));
710 EXPORT_SYMBOL(lu_device_fini);
713 * Initialize object @o that is part of compound object @h and was created by
716 int lu_object_init(struct lu_object *o,
717 struct lu_object_header *h, struct lu_device *d)
719 memset(o, 0, sizeof *o);
723 CFS_INIT_LIST_HEAD(&o->lo_linkage);
726 EXPORT_SYMBOL(lu_object_init);
729 * Finalize object and release its resources.
731 void lu_object_fini(struct lu_object *o)
733 LASSERT(list_empty(&o->lo_linkage));
735 if (o->lo_dev != NULL) {
736 lu_device_put(o->lo_dev);
740 EXPORT_SYMBOL(lu_object_fini);
743 * Add object @o as first layer of compound object @h
745 * This is typically called by the ->ldo_object_alloc() method of top-level
748 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
750 list_move(&o->lo_linkage, &h->loh_layers);
752 EXPORT_SYMBOL(lu_object_add_top);
755 * Add object @o as a layer of compound object, going after @before.1
757 * This is typically called by the ->ldo_object_alloc() method of
760 void lu_object_add(struct lu_object *before, struct lu_object *o)
762 list_move(&o->lo_linkage, &before->lo_linkage);
764 EXPORT_SYMBOL(lu_object_add);
767 * Initialize compound object.
769 int lu_object_header_init(struct lu_object_header *h)
771 memset(h, 0, sizeof *h);
772 atomic_set(&h->loh_ref, 1);
773 INIT_HLIST_NODE(&h->loh_hash);
774 CFS_INIT_LIST_HEAD(&h->loh_lru);
775 CFS_INIT_LIST_HEAD(&h->loh_layers);
778 EXPORT_SYMBOL(lu_object_header_init);
781 * Finalize compound object.
783 void lu_object_header_fini(struct lu_object_header *h)
785 LASSERT(list_empty(&h->loh_layers));
786 LASSERT(list_empty(&h->loh_lru));
787 LASSERT(hlist_unhashed(&h->loh_hash));
789 EXPORT_SYMBOL(lu_object_header_fini);
792 * Given a compound object, find its slice, corresponding to the device type
795 struct lu_object *lu_object_locate(struct lu_object_header *h,
796 struct lu_device_type *dtype)
800 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
801 if (o->lo_dev->ld_type == dtype)
806 EXPORT_SYMBOL(lu_object_locate);
811 * Finalize and free devices in the device stack.
813 * Finalize device stack by purging object cache, and calling
814 * lu_device_type_operations::ldto_device_fini() and
815 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
817 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
819 struct lu_site *site = top->ld_site;
820 struct lu_device *scan;
821 struct lu_device *next;
823 lu_site_purge(env, site, ~0);
824 for (scan = top; scan != NULL; scan = next) {
825 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
830 lu_site_purge(env, site, ~0);
832 if (!list_empty(&site->ls_lru) || site->ls_total != 0) {
834 * Uh-oh, objects still exist.
836 static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
838 lu_site_print(env, site, &cookie, lu_cdebug_printer);
841 for (scan = top; scan != NULL; scan = next) {
842 const struct lu_device_type *ldt = scan->ld_type;
843 struct obd_type *type;
845 next = ldt->ldt_ops->ldto_device_free(env, scan);
846 type = ldt->ldt_obd_type;
848 class_put_type(type);
851 EXPORT_SYMBOL(lu_stack_fini);
855 * Maximal number of tld slots.
857 LU_CONTEXT_KEY_NR = 16
860 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
862 static spinlock_t lu_keys_guard = SPIN_LOCK_UNLOCKED;
867 int lu_context_key_register(struct lu_context_key *key)
872 LASSERT(key->lct_init != NULL);
873 LASSERT(key->lct_fini != NULL);
874 LASSERT(key->lct_tags != 0);
875 LASSERT(key->lct_owner != NULL);
878 spin_lock(&lu_keys_guard);
879 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
880 if (lu_keys[i] == NULL) {
882 atomic_set(&key->lct_used, 1);
888 spin_unlock(&lu_keys_guard);
891 EXPORT_SYMBOL(lu_context_key_register);
893 static void key_fini(struct lu_context *ctx, int index)
895 if (ctx->lc_value[index] != NULL) {
896 struct lu_context_key *key;
898 key = lu_keys[index];
899 LASSERT(key != NULL);
900 LASSERT(key->lct_fini != NULL);
901 LASSERT(atomic_read(&key->lct_used) > 1);
903 key->lct_fini(ctx, key, ctx->lc_value[index]);
904 atomic_dec(&key->lct_used);
905 LASSERT(key->lct_owner != NULL);
906 if (!(ctx->lc_tags & LCT_NOREF)) {
907 LASSERT(module_refcount(key->lct_owner) > 0);
908 module_put(key->lct_owner);
910 ctx->lc_value[index] = NULL;
917 void lu_context_key_degister(struct lu_context_key *key)
919 LASSERT(atomic_read(&key->lct_used) >= 1);
920 LASSERT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
922 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
924 if (atomic_read(&key->lct_used) > 1)
925 CERROR("key has instances.\n");
926 spin_lock(&lu_keys_guard);
927 lu_keys[key->lct_index] = NULL;
928 spin_unlock(&lu_keys_guard);
930 EXPORT_SYMBOL(lu_context_key_degister);
933 * Return value associated with key @key in context @ctx.
935 void *lu_context_key_get(const struct lu_context *ctx,
936 struct lu_context_key *key)
938 LASSERT(ctx->lc_state == LCS_ENTERED);
939 LASSERT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
940 return ctx->lc_value[key->lct_index];
942 EXPORT_SYMBOL(lu_context_key_get);
944 static void keys_fini(struct lu_context *ctx)
948 if (ctx->lc_value != NULL) {
949 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
951 OBD_FREE(ctx->lc_value,
952 ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
953 ctx->lc_value = NULL;
957 static int keys_fill(const struct lu_context *ctx)
961 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
962 struct lu_context_key *key;
965 if (ctx->lc_value[i] == NULL &&
966 key != NULL && key->lct_tags & ctx->lc_tags) {
969 LASSERT(key->lct_init != NULL);
970 LASSERT(key->lct_index == i);
972 value = key->lct_init(ctx, key);
973 if (unlikely(IS_ERR(value)))
974 return PTR_ERR(value);
975 LASSERT(key->lct_owner != NULL);
976 if (!(ctx->lc_tags & LCT_NOREF))
977 try_module_get(key->lct_owner);
978 atomic_inc(&key->lct_used);
979 ctx->lc_value[i] = value;
985 static int keys_init(struct lu_context *ctx)
989 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
990 if (likely(ctx->lc_value != NULL))
991 result = keys_fill(ctx);
1001 * Initialize context data-structure. Create values for all keys.
1003 int lu_context_init(struct lu_context *ctx, __u32 tags)
1005 memset(ctx, 0, sizeof *ctx);
1006 ctx->lc_state = LCS_INITIALIZED;
1007 ctx->lc_tags = tags;
1008 return keys_init(ctx);
1010 EXPORT_SYMBOL(lu_context_init);
1013 * Finalize context data-structure. Destroy key values.
1015 void lu_context_fini(struct lu_context *ctx)
1017 LASSERT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1018 ctx->lc_state = LCS_FINALIZED;
1021 EXPORT_SYMBOL(lu_context_fini);
1024 * Called before entering context.
1026 void lu_context_enter(struct lu_context *ctx)
1028 LASSERT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1029 ctx->lc_state = LCS_ENTERED;
1031 EXPORT_SYMBOL(lu_context_enter);
1034 * Called after exiting from @ctx
1036 void lu_context_exit(struct lu_context *ctx)
1040 LASSERT(ctx->lc_state == LCS_ENTERED);
1041 ctx->lc_state = LCS_LEFT;
1042 if (ctx->lc_value != NULL) {
1043 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1044 if (ctx->lc_value[i] != NULL) {
1045 struct lu_context_key *key;
1048 LASSERT(key != NULL);
1049 if (key->lct_exit != NULL)
1051 key, ctx->lc_value[i]);
1056 EXPORT_SYMBOL(lu_context_exit);
1059 * Allocate for context all missing keys that were registered after context
1062 int lu_context_refill(const struct lu_context *ctx)
1064 LASSERT(ctx->lc_value != NULL);
1065 return keys_fill(ctx);
1067 EXPORT_SYMBOL(lu_context_refill);
1069 static int lu_env_setup(struct lu_env *env, struct lu_context *ses,
1070 __u32 tags, int noref)
1074 LASSERT(ergo(!noref, !(tags & LCT_NOREF)));
1077 result = lu_context_init(&env->le_ctx, tags);
1078 if (likely(result == 0))
1079 lu_context_enter(&env->le_ctx);
1083 static int lu_env_init_noref(struct lu_env *env, struct lu_context *ses,
1086 return lu_env_setup(env, ses, tags, 1);
1089 int lu_env_init(struct lu_env *env, struct lu_context *ses, __u32 tags)
1091 return lu_env_setup(env, ses, tags, 0);
1093 EXPORT_SYMBOL(lu_env_init);
1095 void lu_env_fini(struct lu_env *env)
1097 lu_context_exit(&env->le_ctx);
1098 lu_context_fini(&env->le_ctx);
1101 EXPORT_SYMBOL(lu_env_fini);
1103 static int lu_cache_shrink(int nr, unsigned int gfp_mask)
1106 struct lu_site *tmp;
1109 CFS_LIST_HEAD(splice);
1111 if (nr != 0 && !(gfp_mask & __GFP_FS))
1114 down(&lu_sites_guard);
1115 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1117 remain = lu_site_purge(&lu_shrink_env, s, remain);
1119 * Move just shrunk site to the tail of site list to
1120 * assure shrinking fairness.
1122 list_move_tail(&s->ls_linkage, &splice);
1124 read_lock(&s->ls_guard);
1125 cached += s->ls_total - s->ls_busy;
1126 read_unlock(&s->ls_guard);
1130 list_splice(&splice, lu_sites.prev);
1131 up(&lu_sites_guard);
1135 static struct shrinker *lu_site_shrinker = NULL;
1138 * Initialization of global lu_* data.
1140 int lu_global_init(void)
1144 LU_CONTEXT_KEY_INIT(&lu_global_key);
1145 result = lu_context_key_register(&lu_global_key);
1148 * At this level, we don't know what tags are needed, so
1149 * allocate them conservatively. This should not be too bad,
1150 * because this environment is global.
1152 down(&lu_sites_guard);
1153 result = lu_env_init_noref(&lu_shrink_env, NULL, LCT_SHRINKER);
1154 up(&lu_sites_guard);
1157 * seeks estimation: 3 seeks to read a record from oi,
1158 * one to read inode, one for ea. Unfortunately
1159 * setting this high value results in lu_object/inode
1160 * cache consuming all the memory.
1162 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS,
1165 result = lu_time_global_init();
1172 * Dual to lu_global_init().
1174 void lu_global_fini(void)
1176 lu_time_global_fini();
1177 if (lu_site_shrinker != NULL) {
1178 remove_shrinker(lu_site_shrinker);
1179 lu_site_shrinker = NULL;
1182 lu_context_key_degister(&lu_global_key);
1185 * Tear shrinker environment down _after_ de-registering
1186 * lu_global_key, because the latter has a value in the former.
1188 down(&lu_sites_guard);
1189 lu_env_fini(&lu_shrink_env);
1190 up(&lu_sites_guard);
1193 struct lu_buf LU_BUF_NULL = {
1197 EXPORT_SYMBOL(LU_BUF_NULL);
1200 * XXX: Functions below logically belong to fid module, but they are used by
1201 * dt_store_open(). Put them here until better place is found.
1204 void fid_pack(struct lu_fid_pack *pack, const struct lu_fid *fid,
1205 struct lu_fid *befider)
1215 * Two cases: compact 6 bytes representation for a common case, and
1216 * full 17 byte representation for "unusual" fid.
1220 * Check that usual case is really usual.
1222 CLASSERT(LUSTRE_SEQ_MAX_WIDTH < 0xffffull);
1224 if (fid_is_igif(fid) ||
1225 seq > 0xffffffull || oid > 0xffff || fid_ver(fid) != 0) {
1226 fid_cpu_to_be(befider, fid);
1227 recsize = sizeof *befider;
1229 unsigned char *small_befider;
1231 small_befider = (char *)befider;
1233 small_befider[0] = seq >> 16;
1234 small_befider[1] = seq >> 8;
1235 small_befider[2] = seq;
1237 small_befider[3] = oid >> 8;
1238 small_befider[4] = oid;
1242 memcpy(pack->fp_area, befider, recsize);
1243 pack->fp_len = recsize + 1;
1245 EXPORT_SYMBOL(fid_pack);
1247 int fid_unpack(const struct lu_fid_pack *pack, struct lu_fid *fid)
1252 switch (pack->fp_len) {
1253 case sizeof *fid + 1:
1254 memcpy(fid, pack->fp_area, sizeof *fid);
1255 fid_be_to_cpu(fid, fid);
1258 const unsigned char *area;
1260 area = pack->fp_area;
1261 fid->f_seq = (area[0] << 16) | (area[1] << 8) | area[2];
1262 fid->f_oid = (area[3] << 8) | area[4];
1267 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1272 EXPORT_SYMBOL(fid_unpack);
1274 const char *lu_time_names[LU_TIME_NR] = {
1275 [LU_TIME_FIND_LOOKUP] = "find_lookup",
1276 [LU_TIME_FIND_ALLOC] = "find_alloc",
1277 [LU_TIME_FIND_INSERT] = "find_insert"
1279 EXPORT_SYMBOL(lu_time_names);