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.danilov@sun.com>
45 #define DEBUG_SUBSYSTEM S_CLASS
47 # define EXPORT_SYMTAB
50 #include <libcfs/libcfs.h>
53 # include <linux/module.h>
57 #include <libcfs/libcfs_hash.h>
58 #include <obd_class.h>
59 #include <obd_support.h>
60 #include <lustre_disk.h>
61 #include <lustre_fid.h>
62 #include <lu_object.h>
63 #include <libcfs/list.h>
64 /* lu_time_global_{init,fini}() */
67 static void lu_object_free(const struct lu_env *env, struct lu_object *o);
70 * Decrease reference counter on object. If last reference is freed, return
71 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
72 * case, free object immediately.
74 void lu_object_put(const struct lu_env *env, struct lu_object *o)
76 struct lu_object_header *top;
78 struct lu_object *orig;
82 site = o->lo_dev->ld_site;
85 write_lock(&site->ls_guard);
86 if (atomic_dec_and_test(&top->loh_ref)) {
88 * When last reference is released, iterate over object
89 * layers, and notify them that object is no longer busy.
91 list_for_each_entry_reverse(o, &top->loh_layers, lo_linkage) {
92 if (o->lo_ops->loo_object_release != NULL)
93 o->lo_ops->loo_object_release(env, o);
96 if (lu_object_is_dying(top)) {
98 * If object is dying (will not be cached), removed it
99 * from hash table and LRU.
101 * This is done with hash table and LRU lists
102 * locked. As the only way to acquire first reference
103 * to previously unreferenced object is through
104 * hash-table lookup (lu_object_find()), or LRU
105 * scanning (lu_site_purge()), that are done under
106 * hash-table and LRU lock, no race with concurrent
107 * object lookup is possible and we can safely destroy
110 hlist_del_init(&top->loh_hash);
111 list_del_init(&top->loh_lru);
116 write_unlock(&site->ls_guard);
119 * Object was already removed from hash and lru above, can
122 lu_object_free(env, orig);
124 EXPORT_SYMBOL(lu_object_put);
127 * Allocate new object.
129 * This follows object creation protocol, described in the comment within
130 * struct lu_device_operations definition.
132 static struct lu_object *lu_object_alloc(const struct lu_env *env,
133 struct lu_device *dev,
134 const struct lu_fid *f,
135 const struct lu_object_conf *conf)
137 struct lu_object *scan;
138 struct lu_object *top;
139 struct list_head *layers;
145 * Create top-level object slice. This will also create
148 top = dev->ld_ops->ldo_object_alloc(env, NULL, dev);
150 RETURN(ERR_PTR(-ENOMEM));
152 * This is the only place where object fid is assigned. It's constant
155 LASSERT(fid_is_igif(f) || fid_ver(f) == 0);
156 top->lo_header->loh_fid = *f;
157 layers = &top->lo_header->loh_layers;
160 * Call ->loo_object_init() repeatedly, until no more new
161 * object slices are created.
164 list_for_each_entry(scan, layers, lo_linkage) {
165 if (scan->lo_flags & LU_OBJECT_ALLOCATED)
168 scan->lo_header = top->lo_header;
169 result = scan->lo_ops->loo_object_init(env, scan, conf);
171 lu_object_free(env, top);
172 RETURN(ERR_PTR(result));
174 scan->lo_flags |= LU_OBJECT_ALLOCATED;
178 list_for_each_entry_reverse(scan, layers, lo_linkage) {
179 if (scan->lo_ops->loo_object_start != NULL) {
180 result = scan->lo_ops->loo_object_start(env, scan);
182 lu_object_free(env, top);
183 RETURN(ERR_PTR(result));
188 dev->ld_site->ls_stats.s_created ++;
195 static void lu_object_free(const struct lu_env *env, struct lu_object *o)
197 struct list_head splice;
198 struct lu_object *scan;
199 struct lu_site *site;
200 struct list_head *layers;
202 site = o->lo_dev->ld_site;
203 layers = &o->lo_header->loh_layers;
205 * First call ->loo_object_delete() method to release all resources.
207 list_for_each_entry_reverse(scan, layers, lo_linkage) {
208 if (scan->lo_ops->loo_object_delete != NULL)
209 scan->lo_ops->loo_object_delete(env, scan);
213 * Then, splice object layers into stand-alone list, and call
214 * ->loo_object_free() on all layers to free memory. Splice is
215 * necessary, because lu_object_header is freed together with the
218 CFS_INIT_LIST_HEAD(&splice);
219 list_splice_init(layers, &splice);
220 while (!list_empty(&splice)) {
222 * Free layers in bottom-to-top order, so that object header
223 * lives as long as possible and ->loo_object_free() methods
224 * can look at its contents.
226 o = container_of0(splice.prev, struct lu_object, lo_linkage);
227 list_del_init(&o->lo_linkage);
228 LASSERT(o->lo_ops->loo_object_free != NULL);
229 o->lo_ops->loo_object_free(env, o);
231 cfs_waitq_broadcast(&site->ls_marche_funebre);
235 * Free \a nr objects from the cold end of the site LRU list.
237 int lu_site_purge(const struct lu_env *env, struct lu_site *s, int nr)
239 struct list_head dispose;
240 struct lu_object_header *h;
241 struct lu_object_header *temp;
243 CFS_INIT_LIST_HEAD(&dispose);
245 * Under LRU list lock, scan LRU list and move unreferenced objects to
246 * the dispose list, removing them from LRU and hash table.
248 write_lock(&s->ls_guard);
249 list_for_each_entry_safe(h, temp, &s->ls_lru, loh_lru) {
251 * Objects are sorted in lru order, and "busy" objects (ones
252 * with h->loh_ref > 0) naturally tend to live near hot end
253 * that we scan last. Unfortunately, sites usually have small
254 * (less then ten) number of busy yet rarely accessed objects
255 * (some global objects, accessed directly through pointers,
256 * bypassing hash table). Currently algorithm scans them over
257 * and over again. Probably we should move busy objects out of
258 * LRU, or we can live with that.
262 if (atomic_read(&h->loh_ref) > 0)
264 hlist_del_init(&h->loh_hash);
265 list_move(&h->loh_lru, &dispose);
268 write_unlock(&s->ls_guard);
270 * Free everything on the dispose list. This is safe against races due
271 * to the reasons described in lu_object_put().
273 while (!list_empty(&dispose)) {
274 h = container_of0(dispose.next,
275 struct lu_object_header, loh_lru);
276 list_del_init(&h->loh_lru);
277 lu_object_free(env, lu_object_top(h));
278 s->ls_stats.s_lru_purged ++;
282 EXPORT_SYMBOL(lu_site_purge);
287 * Code below has to jump through certain loops to output object description
288 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
289 * composes object description from strings that are parts of _lines_ of
290 * output (i.e., strings that are not terminated by newline). This doesn't fit
291 * very well into libcfs_debug_msg() interface that assumes that each message
292 * supplied to it is a self-contained output line.
294 * To work around this, strings are collected in a temporary buffer
295 * (implemented as a value of lu_cdebug_key key), until terminating newline
296 * character is detected.
304 * XXX overflow is not handled correctly.
309 struct lu_cdebug_data {
313 char lck_area[LU_CDEBUG_LINE];
316 /* context key constructor/destructor: lu_global_key_init, lu_global_key_fini */
317 LU_KEY_INIT_FINI(lu_global, struct lu_cdebug_data);
320 * Key, holding temporary buffer. This key is registered very early by
323 struct lu_context_key lu_global_key = {
324 .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
325 .lct_init = lu_global_key_init,
326 .lct_fini = lu_global_key_fini
330 * Printer function emitting messages through libcfs_debug_msg().
332 int lu_cdebug_printer(const struct lu_env *env,
333 void *cookie, const char *format, ...)
335 struct lu_cdebug_print_info *info = cookie;
336 struct lu_cdebug_data *key;
341 va_start(args, format);
343 key = lu_context_key_get(&env->le_ctx, &lu_global_key);
344 LASSERT(key != NULL);
346 used = strlen(key->lck_area);
347 complete = format[strlen(format) - 1] == '\n';
349 * Append new chunk to the buffer.
351 vsnprintf(key->lck_area + used,
352 ARRAY_SIZE(key->lck_area) - used, format, args);
354 if (cdebug_show(info->lpi_mask, info->lpi_subsys))
355 libcfs_debug_msg(NULL, info->lpi_subsys, info->lpi_mask,
356 (char *)info->lpi_file, info->lpi_fn,
357 info->lpi_line, "%s", key->lck_area);
358 key->lck_area[0] = 0;
363 EXPORT_SYMBOL(lu_cdebug_printer);
366 * Print object header.
368 void lu_object_header_print(const struct lu_env *env, void *cookie,
369 lu_printer_t printer,
370 const struct lu_object_header *hdr)
372 (*printer)(env, cookie, "header@%p[%#lx, %d, "DFID"%s%s%s]",
373 hdr, hdr->loh_flags, atomic_read(&hdr->loh_ref),
375 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
376 list_empty((struct list_head *)&hdr->loh_lru) ? "" : " lru",
377 hdr->loh_attr & LOHA_EXISTS ? " exist":"");
379 EXPORT_SYMBOL(lu_object_header_print);
382 * Print human readable representation of the \a o to the \a printer.
384 void lu_object_print(const struct lu_env *env, void *cookie,
385 lu_printer_t printer, const struct lu_object *o)
387 static const char ruler[] = "........................................";
388 struct lu_object_header *top;
392 lu_object_header_print(env, cookie, printer, top);
393 (*printer)(env, cookie, "{ \n");
394 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
395 depth = o->lo_depth + 4;
398 * print `.' \a depth times followed by type name and address
400 (*printer)(env, cookie, "%*.*s%s@%p", depth, depth, ruler,
401 o->lo_dev->ld_type->ldt_name, o);
402 if (o->lo_ops->loo_object_print != NULL)
403 o->lo_ops->loo_object_print(env, cookie, printer, o);
404 (*printer)(env, cookie, "\n");
406 (*printer)(env, cookie, "} header@%p\n", top);
408 EXPORT_SYMBOL(lu_object_print);
411 * Check object consistency.
413 int lu_object_invariant(const struct lu_object *o)
415 struct lu_object_header *top;
418 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
419 if (o->lo_ops->loo_object_invariant != NULL &&
420 !o->lo_ops->loo_object_invariant(o))
425 EXPORT_SYMBOL(lu_object_invariant);
427 static struct lu_object *htable_lookup(struct lu_site *s,
428 const struct hlist_head *bucket,
429 const struct lu_fid *f,
430 cfs_waitlink_t *waiter)
432 struct lu_object_header *h;
433 struct hlist_node *scan;
435 hlist_for_each_entry(h, scan, bucket, loh_hash) {
436 s->ls_stats.s_cache_check ++;
437 if (likely(lu_fid_eq(&h->loh_fid, f))) {
438 if (unlikely(lu_object_is_dying(h))) {
440 * Lookup found an object being destroyed;
441 * this object cannot be returned (to assure
442 * that references to dying objects are
443 * eventually drained), and moreover, lookup
444 * has to wait until object is freed.
446 cfs_waitlink_init(waiter);
447 cfs_waitq_add(&s->ls_marche_funebre, waiter);
448 set_current_state(CFS_TASK_UNINT);
449 s->ls_stats.s_cache_death_race ++;
450 return ERR_PTR(-EAGAIN);
452 /* bump reference count... */
453 if (atomic_add_return(1, &h->loh_ref) == 1)
455 /* and move to the head of the LRU */
457 * XXX temporary disable this to measure effects of
458 * read-write locking.
460 /* list_move_tail(&h->loh_lru, &s->ls_lru); */
461 s->ls_stats.s_cache_hit ++;
462 return lu_object_top(h);
465 s->ls_stats.s_cache_miss ++;
469 static __u32 fid_hash(const struct lu_fid *f, int bits)
471 /* all objects with same id and different versions will belong to same
472 * collisions list. */
473 return hash_long(fid_flatten(f), bits);
477 * Search cache for an object with the fid \a f. If such object is found,
478 * return it. Otherwise, create new object, insert it into cache and return
479 * it. In any case, additional reference is acquired on the returned object.
481 struct lu_object *lu_object_find(const struct lu_env *env,
482 struct lu_device *dev, const struct lu_fid *f,
483 const struct lu_object_conf *conf)
485 return lu_object_find_at(env, dev->ld_site->ls_top_dev, f, conf);
487 EXPORT_SYMBOL(lu_object_find);
490 * Core logic of lu_object_find*() functions.
492 static struct lu_object *lu_object_find_try(const struct lu_env *env,
493 struct lu_device *dev,
494 const struct lu_fid *f,
495 const struct lu_object_conf *conf,
496 cfs_waitlink_t *waiter)
500 struct lu_object *shadow;
501 struct hlist_head *bucket;
504 * This uses standard index maintenance protocol:
506 * - search index under lock, and return object if found;
507 * - otherwise, unlock index, allocate new object;
508 * - lock index and search again;
509 * - if nothing is found (usual case), insert newly created
511 * - otherwise (race: other thread inserted object), free
512 * object just allocated.
516 * If dying object is found during index search, add @waiter to the
517 * site wait-queue and return ERR_PTR(-EAGAIN).
521 bucket = s->ls_hash + fid_hash(f, s->ls_hash_bits);
523 read_lock(&s->ls_guard);
524 o = htable_lookup(s, bucket, f, waiter);
525 read_unlock(&s->ls_guard);
531 * Allocate new object. This may result in rather complicated
532 * operations, including fld queries, inode loading, etc.
534 o = lu_object_alloc(env, dev, f, conf);
535 if (unlikely(IS_ERR(o)))
538 LASSERT(lu_fid_eq(lu_object_fid(o), f));
540 write_lock(&s->ls_guard);
541 shadow = htable_lookup(s, bucket, f, waiter);
542 if (likely(shadow == NULL)) {
543 hlist_add_head(&o->lo_header->loh_hash, bucket);
544 list_add_tail(&o->lo_header->loh_lru, &s->ls_lru);
550 s->ls_stats.s_cache_race ++;
551 write_unlock(&s->ls_guard);
553 lu_object_free(env, o);
558 * Much like lu_object_find(), but top level device of object is specifically
559 * \a dev rather than top level device of the site. This interface allows
560 * objects of different "stacking" to be created within the same site.
562 struct lu_object *lu_object_find_at(const struct lu_env *env,
563 struct lu_device *dev,
564 const struct lu_fid *f,
565 const struct lu_object_conf *conf)
567 struct lu_object *obj;
571 obj = lu_object_find_try(env, dev, f, conf, &wait);
572 if (obj == ERR_PTR(-EAGAIN)) {
574 * lu_object_find_try() already added waiter into the
577 cfs_waitq_wait(&wait, CFS_TASK_UNINT);
578 cfs_waitq_del(&dev->ld_site->ls_marche_funebre, &wait);
584 EXPORT_SYMBOL(lu_object_find_at);
587 * Find object with given fid, and return its slice belonging to given device.
589 struct lu_object *lu_object_find_slice(const struct lu_env *env,
590 struct lu_device *dev,
591 const struct lu_fid *f,
592 const struct lu_object_conf *conf)
594 struct lu_object *top;
595 struct lu_object *obj;
597 top = lu_object_find(env, dev, f, conf);
599 obj = lu_object_locate(top->lo_header, dev->ld_type);
601 lu_object_put(env, top);
606 EXPORT_SYMBOL(lu_object_find_slice);
609 * Global list of all device types.
611 static CFS_LIST_HEAD(lu_device_types);
613 int lu_device_type_init(struct lu_device_type *ldt)
617 CFS_INIT_LIST_HEAD(&ldt->ldt_linkage);
618 result = ldt->ldt_ops->ldto_init(ldt);
620 list_add(&ldt->ldt_linkage, &lu_device_types);
623 EXPORT_SYMBOL(lu_device_type_init);
625 void lu_device_type_fini(struct lu_device_type *ldt)
627 list_del_init(&ldt->ldt_linkage);
628 ldt->ldt_ops->ldto_fini(ldt);
630 EXPORT_SYMBOL(lu_device_type_fini);
632 void lu_types_stop(void)
634 struct lu_device_type *ldt;
636 list_for_each_entry(ldt, &lu_device_types, ldt_linkage) {
637 if (ldt->ldt_device_nr == 0)
638 ldt->ldt_ops->ldto_stop(ldt);
641 EXPORT_SYMBOL(lu_types_stop);
644 * Global list of all sites on this node
646 static CFS_LIST_HEAD(lu_sites);
647 static DECLARE_MUTEX(lu_sites_guard);
650 * Global environment used by site shrinker.
652 static struct lu_env lu_shrink_env;
655 * Print all objects in \a s.
657 void lu_site_print(const struct lu_env *env, struct lu_site *s, void *cookie,
658 lu_printer_t printer)
662 for (i = 0; i < s->ls_hash_size; ++i) {
663 struct lu_object_header *h;
664 struct hlist_node *scan;
666 read_lock(&s->ls_guard);
667 hlist_for_each_entry(h, scan, &s->ls_hash[i], loh_hash) {
669 if (!list_empty(&h->loh_layers)) {
670 const struct lu_object *obj;
672 obj = lu_object_top(h);
673 lu_object_print(env, cookie, printer, obj);
675 lu_object_header_print(env, cookie, printer, h);
677 read_unlock(&s->ls_guard);
680 EXPORT_SYMBOL(lu_site_print);
683 LU_CACHE_PERCENT = 20,
687 * Return desired hash table order.
689 static int lu_htable_order(void)
691 unsigned long cache_size;
695 * Calculate hash table size, assuming that we want reasonable
696 * performance when 20% of total memory is occupied by cache of
699 * Size of lu_object is (arbitrary) taken as 1K (together with inode).
701 cache_size = num_physpages;
703 #if BITS_PER_LONG == 32
704 /* limit hashtable size for lowmem systems to low RAM */
705 if (cache_size > 1 << (30 - CFS_PAGE_SHIFT))
706 cache_size = 1 << (30 - CFS_PAGE_SHIFT) * 3 / 4;
709 cache_size = cache_size / 100 * LU_CACHE_PERCENT *
710 (CFS_PAGE_SIZE / 1024);
712 for (bits = 1; (1 << bits) < cache_size; ++bits) {
718 static struct lock_class_key lu_site_guard_class;
721 * Initialize site \a s, with \a d as the top level device.
723 int lu_site_init(struct lu_site *s, struct lu_device *top)
730 memset(s, 0, sizeof *s);
731 rwlock_init(&s->ls_guard);
732 lockdep_set_class(&s->ls_guard, &lu_site_guard_class);
733 CFS_INIT_LIST_HEAD(&s->ls_lru);
734 CFS_INIT_LIST_HEAD(&s->ls_linkage);
735 cfs_waitq_init(&s->ls_marche_funebre);
739 lu_ref_add(&top->ld_reference, "site-top", s);
741 for (bits = lu_htable_order(), size = 1 << bits;
743 cfs_alloc_large(size * sizeof s->ls_hash[0])) == NULL;
744 --bits, size >>= 1) {
746 * Scale hash table down, until allocation succeeds.
751 s->ls_hash_size = size;
752 s->ls_hash_bits = bits;
753 s->ls_hash_mask = size - 1;
755 for (i = 0; i < size; i++)
756 INIT_HLIST_HEAD(&s->ls_hash[i]);
760 EXPORT_SYMBOL(lu_site_init);
763 * Finalize \a s and release its resources.
765 void lu_site_fini(struct lu_site *s)
767 LASSERT(list_empty(&s->ls_lru));
768 LASSERT(s->ls_total == 0);
770 down(&lu_sites_guard);
771 list_del_init(&s->ls_linkage);
774 if (s->ls_hash != NULL) {
776 for (i = 0; i < s->ls_hash_size; i++)
777 LASSERT(hlist_empty(&s->ls_hash[i]));
778 cfs_free_large(s->ls_hash);
781 if (s->ls_top_dev != NULL) {
782 s->ls_top_dev->ld_site = NULL;
783 lu_ref_del(&s->ls_top_dev->ld_reference, "site-top", s);
784 lu_device_put(s->ls_top_dev);
785 s->ls_top_dev = NULL;
788 EXPORT_SYMBOL(lu_site_fini);
791 * Called when initialization of stack for this site is completed.
793 int lu_site_init_finish(struct lu_site *s)
796 down(&lu_sites_guard);
797 result = lu_context_refill(&lu_shrink_env.le_ctx);
799 list_add(&s->ls_linkage, &lu_sites);
803 EXPORT_SYMBOL(lu_site_init_finish);
806 * Acquire additional reference on device \a d
808 void lu_device_get(struct lu_device *d)
810 atomic_inc(&d->ld_ref);
812 EXPORT_SYMBOL(lu_device_get);
815 * Release reference on device \a d.
817 void lu_device_put(struct lu_device *d)
819 LASSERT(atomic_read(&d->ld_ref) > 0);
820 atomic_dec(&d->ld_ref);
822 EXPORT_SYMBOL(lu_device_put);
825 * Initialize device \a d of type \a t.
827 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
829 if (t->ldt_device_nr++ == 0 && t->ldt_ops->ldto_start != NULL)
830 t->ldt_ops->ldto_start(t);
831 memset(d, 0, sizeof *d);
832 atomic_set(&d->ld_ref, 0);
834 lu_ref_init(&d->ld_reference);
837 EXPORT_SYMBOL(lu_device_init);
840 * Finalize device \a d.
842 void lu_device_fini(struct lu_device *d)
844 struct lu_device_type *t;
847 if (d->ld_obd != NULL)
849 lprocfs_obd_cleanup(d->ld_obd);
851 lu_ref_fini(&d->ld_reference);
852 LASSERTF(atomic_read(&d->ld_ref) == 0,
853 "Refcount is %u\n", atomic_read(&d->ld_ref));
854 LASSERT(t->ldt_device_nr > 0);
855 if (--t->ldt_device_nr == 0 && t->ldt_ops->ldto_stop != NULL)
856 t->ldt_ops->ldto_stop(t);
858 EXPORT_SYMBOL(lu_device_fini);
861 * Initialize object \a o that is part of compound object \a h and was created
864 int lu_object_init(struct lu_object *o,
865 struct lu_object_header *h, struct lu_device *d)
867 memset(o, 0, sizeof *o);
871 o->lo_dev_ref = lu_ref_add(&d->ld_reference, "lu_object", o);
872 CFS_INIT_LIST_HEAD(&o->lo_linkage);
875 EXPORT_SYMBOL(lu_object_init);
878 * Finalize object and release its resources.
880 void lu_object_fini(struct lu_object *o)
882 struct lu_device *dev = o->lo_dev;
884 LASSERT(list_empty(&o->lo_linkage));
887 lu_ref_del_at(&dev->ld_reference,
888 o->lo_dev_ref , "lu_object", o);
893 EXPORT_SYMBOL(lu_object_fini);
896 * Add object \a o as first layer of compound object \a h
898 * This is typically called by the ->ldo_object_alloc() method of top-level
901 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
903 list_move(&o->lo_linkage, &h->loh_layers);
905 EXPORT_SYMBOL(lu_object_add_top);
908 * Add object \a o as a layer of compound object, going after \a before.
910 * This is typically called by the ->ldo_object_alloc() method of \a
913 void lu_object_add(struct lu_object *before, struct lu_object *o)
915 list_move(&o->lo_linkage, &before->lo_linkage);
917 EXPORT_SYMBOL(lu_object_add);
920 * Initialize compound object.
922 int lu_object_header_init(struct lu_object_header *h)
924 memset(h, 0, sizeof *h);
925 atomic_set(&h->loh_ref, 1);
926 INIT_HLIST_NODE(&h->loh_hash);
927 CFS_INIT_LIST_HEAD(&h->loh_lru);
928 CFS_INIT_LIST_HEAD(&h->loh_layers);
929 lu_ref_init(&h->loh_reference);
932 EXPORT_SYMBOL(lu_object_header_init);
935 * Finalize compound object.
937 void lu_object_header_fini(struct lu_object_header *h)
939 LASSERT(list_empty(&h->loh_layers));
940 LASSERT(list_empty(&h->loh_lru));
941 LASSERT(hlist_unhashed(&h->loh_hash));
942 lu_ref_fini(&h->loh_reference);
944 EXPORT_SYMBOL(lu_object_header_fini);
947 * Given a compound object, find its slice, corresponding to the device type
950 struct lu_object *lu_object_locate(struct lu_object_header *h,
951 const struct lu_device_type *dtype)
955 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
956 if (o->lo_dev->ld_type == dtype)
961 EXPORT_SYMBOL(lu_object_locate);
966 * Finalize and free devices in the device stack.
968 * Finalize device stack by purging object cache, and calling
969 * lu_device_type_operations::ldto_device_fini() and
970 * lu_device_type_operations::ldto_device_free() on all devices in the stack.
972 void lu_stack_fini(const struct lu_env *env, struct lu_device *top)
974 struct lu_site *site = top->ld_site;
975 struct lu_device *scan;
976 struct lu_device *next;
978 lu_site_purge(env, site, ~0);
979 for (scan = top; scan != NULL; scan = next) {
980 next = scan->ld_type->ldt_ops->ldto_device_fini(env, scan);
981 lu_ref_del(&scan->ld_reference, "lu-stack", &lu_site_init);
986 lu_site_purge(env, site, ~0);
988 if (!list_empty(&site->ls_lru) || site->ls_total != 0) {
990 * Uh-oh, objects still exist.
992 static DECLARE_LU_CDEBUG_PRINT_INFO(cookie, D_ERROR);
994 lu_site_print(env, site, &cookie, lu_cdebug_printer);
997 for (scan = top; scan != NULL; scan = next) {
998 const struct lu_device_type *ldt = scan->ld_type;
999 struct obd_type *type;
1001 next = ldt->ldt_ops->ldto_device_free(env, scan);
1002 type = ldt->ldt_obd_type;
1005 class_put_type(type);
1009 EXPORT_SYMBOL(lu_stack_fini);
1013 * Maximal number of tld slots.
1015 LU_CONTEXT_KEY_NR = 32
1018 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
1020 static spinlock_t lu_keys_guard = SPIN_LOCK_UNLOCKED;
1023 * Global counter incremented whenever key is registered, unregistered,
1024 * revived or quiesced. This is used to void unnecessary calls to
1025 * lu_context_refill(). No locking is provided, as initialization and shutdown
1026 * are supposed to be externally serialized.
1028 static unsigned key_set_version = 0;
1033 int lu_context_key_register(struct lu_context_key *key)
1038 LASSERT(key->lct_init != NULL);
1039 LASSERT(key->lct_fini != NULL);
1040 LASSERT(key->lct_tags != 0);
1041 LASSERT(key->lct_owner != NULL);
1044 spin_lock(&lu_keys_guard);
1045 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1046 if (lu_keys[i] == NULL) {
1048 atomic_set(&key->lct_used, 1);
1050 lu_ref_init(&key->lct_reference);
1056 spin_unlock(&lu_keys_guard);
1059 EXPORT_SYMBOL(lu_context_key_register);
1061 static void key_fini(struct lu_context *ctx, int index)
1063 if (ctx->lc_value[index] != NULL) {
1064 struct lu_context_key *key;
1066 key = lu_keys[index];
1067 LASSERT(key != NULL);
1068 LASSERT(key->lct_fini != NULL);
1069 LASSERT(atomic_read(&key->lct_used) > 1);
1071 key->lct_fini(ctx, key, ctx->lc_value[index]);
1072 lu_ref_del(&key->lct_reference, "ctx", ctx);
1073 atomic_dec(&key->lct_used);
1074 LASSERT(key->lct_owner != NULL);
1075 if (!(ctx->lc_tags & LCT_NOREF)) {
1076 LASSERT(module_refcount(key->lct_owner) > 0);
1077 module_put(key->lct_owner);
1079 ctx->lc_value[index] = NULL;
1086 void lu_context_key_degister(struct lu_context_key *key)
1088 LASSERT(atomic_read(&key->lct_used) >= 1);
1089 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1092 key_fini(&lu_shrink_env.le_ctx, key->lct_index);
1094 if (atomic_read(&key->lct_used) > 1)
1095 CERROR("key has instances.\n");
1096 spin_lock(&lu_keys_guard);
1097 lu_keys[key->lct_index] = NULL;
1098 spin_unlock(&lu_keys_guard);
1100 EXPORT_SYMBOL(lu_context_key_degister);
1103 * Register a number of keys. This has to be called after all keys have been
1104 * initialized by a call to LU_CONTEXT_KEY_INIT().
1106 int lu_context_key_register_many(struct lu_context_key *k, ...)
1108 struct lu_context_key *key = k;
1114 result = lu_context_key_register(key);
1117 key = va_arg(args, struct lu_context_key *);
1118 } while (key != NULL);
1124 lu_context_key_degister(k);
1125 k = va_arg(args, struct lu_context_key *);
1132 EXPORT_SYMBOL(lu_context_key_register_many);
1135 * De-register a number of keys. This is a dual to
1136 * lu_context_key_register_many().
1138 void lu_context_key_degister_many(struct lu_context_key *k, ...)
1144 lu_context_key_degister(k);
1145 k = va_arg(args, struct lu_context_key*);
1146 } while (k != NULL);
1149 EXPORT_SYMBOL(lu_context_key_degister_many);
1152 * Revive a number of keys.
1154 void lu_context_key_revive_many(struct lu_context_key *k, ...)
1160 lu_context_key_revive(k);
1161 k = va_arg(args, struct lu_context_key*);
1162 } while (k != NULL);
1165 EXPORT_SYMBOL(lu_context_key_revive_many);
1168 * Quiescent a number of keys.
1170 void lu_context_key_quiesce_many(struct lu_context_key *k, ...)
1176 lu_context_key_quiesce(k);
1177 k = va_arg(args, struct lu_context_key*);
1178 } while (k != NULL);
1181 EXPORT_SYMBOL(lu_context_key_quiesce_many);
1184 * Return value associated with key \a key in context \a ctx.
1186 void *lu_context_key_get(const struct lu_context *ctx,
1187 const struct lu_context_key *key)
1189 LINVRNT(ctx->lc_state == LCS_ENTERED);
1190 LINVRNT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
1191 return ctx->lc_value[key->lct_index];
1193 EXPORT_SYMBOL(lu_context_key_get);
1196 * List of remembered contexts. XXX document me.
1198 static CFS_LIST_HEAD(lu_context_remembered);
1201 * Destroy \a key in all remembered contexts. This is used to destroy key
1202 * values in "shared" contexts (like service threads), when a module owning
1203 * the key is about to be unloaded.
1205 void lu_context_key_quiesce(struct lu_context_key *key)
1207 struct lu_context *ctx;
1209 if (!(key->lct_tags & LCT_QUIESCENT)) {
1210 key->lct_tags |= LCT_QUIESCENT;
1212 * XXX memory barrier has to go here.
1214 spin_lock(&lu_keys_guard);
1215 list_for_each_entry(ctx, &lu_context_remembered, lc_remember)
1216 key_fini(ctx, key->lct_index);
1217 spin_unlock(&lu_keys_guard);
1221 EXPORT_SYMBOL(lu_context_key_quiesce);
1223 void lu_context_key_revive(struct lu_context_key *key)
1225 key->lct_tags &= ~LCT_QUIESCENT;
1228 EXPORT_SYMBOL(lu_context_key_revive);
1230 static void keys_fini(struct lu_context *ctx)
1234 if (ctx->lc_value != NULL) {
1235 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i)
1237 OBD_FREE(ctx->lc_value,
1238 ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1239 ctx->lc_value = NULL;
1243 static int keys_fill(struct lu_context *ctx)
1247 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1248 struct lu_context_key *key;
1251 if (ctx->lc_value[i] == NULL && key != NULL &&
1252 (key->lct_tags & ctx->lc_tags) &&
1254 * Don't create values for a LCT_QUIESCENT key, as this
1255 * will pin module owning a key.
1257 !(key->lct_tags & LCT_QUIESCENT)) {
1260 LINVRNT(key->lct_init != NULL);
1261 LINVRNT(key->lct_index == i);
1263 value = key->lct_init(ctx, key);
1264 if (unlikely(IS_ERR(value)))
1265 return PTR_ERR(value);
1266 LASSERT(key->lct_owner != NULL);
1267 if (!(ctx->lc_tags & LCT_NOREF))
1268 try_module_get(key->lct_owner);
1269 lu_ref_add_atomic(&key->lct_reference, "ctx", ctx);
1270 atomic_inc(&key->lct_used);
1272 * This is the only place in the code, where an
1273 * element of ctx->lc_value[] array is set to non-NULL
1276 ctx->lc_value[i] = value;
1277 if (key->lct_exit != NULL)
1278 ctx->lc_tags |= LCT_HAS_EXIT;
1280 ctx->lc_version = key_set_version;
1285 static int keys_init(struct lu_context *ctx)
1289 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
1290 if (likely(ctx->lc_value != NULL))
1291 result = keys_fill(ctx);
1301 * Initialize context data-structure. Create values for all keys.
1303 int lu_context_init(struct lu_context *ctx, __u32 tags)
1305 memset(ctx, 0, sizeof *ctx);
1306 ctx->lc_state = LCS_INITIALIZED;
1307 ctx->lc_tags = tags;
1308 if (tags & LCT_REMEMBER) {
1309 spin_lock(&lu_keys_guard);
1310 list_add(&ctx->lc_remember, &lu_context_remembered);
1311 spin_unlock(&lu_keys_guard);
1313 CFS_INIT_LIST_HEAD(&ctx->lc_remember);
1314 return keys_init(ctx);
1316 EXPORT_SYMBOL(lu_context_init);
1319 * Finalize context data-structure. Destroy key values.
1321 void lu_context_fini(struct lu_context *ctx)
1323 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1324 ctx->lc_state = LCS_FINALIZED;
1326 spin_lock(&lu_keys_guard);
1327 list_del_init(&ctx->lc_remember);
1328 spin_unlock(&lu_keys_guard);
1330 EXPORT_SYMBOL(lu_context_fini);
1333 * Called before entering context.
1335 void lu_context_enter(struct lu_context *ctx)
1337 LINVRNT(ctx->lc_state == LCS_INITIALIZED || ctx->lc_state == LCS_LEFT);
1338 ctx->lc_state = LCS_ENTERED;
1340 EXPORT_SYMBOL(lu_context_enter);
1343 * Called after exiting from \a ctx
1345 void lu_context_exit(struct lu_context *ctx)
1349 LINVRNT(ctx->lc_state == LCS_ENTERED);
1350 ctx->lc_state = LCS_LEFT;
1351 if (ctx->lc_tags & LCT_HAS_EXIT && ctx->lc_value != NULL) {
1352 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
1353 if (ctx->lc_value[i] != NULL) {
1354 struct lu_context_key *key;
1357 LASSERT(key != NULL);
1358 if (key->lct_exit != NULL)
1360 key, ctx->lc_value[i]);
1365 EXPORT_SYMBOL(lu_context_exit);
1368 * Allocate for context all missing keys that were registered after context
1371 int lu_context_refill(struct lu_context *ctx)
1373 LINVRNT(ctx->lc_value != NULL);
1374 return ctx->lc_version == key_set_version ? 0 : keys_fill(ctx);
1376 EXPORT_SYMBOL(lu_context_refill);
1378 static int lu_env_setup(struct lu_env *env, struct lu_context *ses,
1379 __u32 tags, int noref)
1383 LINVRNT(ergo(!noref, !(tags & LCT_NOREF)));
1386 result = lu_context_init(&env->le_ctx, tags);
1387 if (likely(result == 0))
1388 lu_context_enter(&env->le_ctx);
1392 static int lu_env_init_noref(struct lu_env *env, struct lu_context *ses,
1395 return lu_env_setup(env, ses, tags, 1);
1398 int lu_env_init(struct lu_env *env, struct lu_context *ses, __u32 tags)
1400 return lu_env_setup(env, ses, tags, 0);
1402 EXPORT_SYMBOL(lu_env_init);
1404 void lu_env_fini(struct lu_env *env)
1406 lu_context_exit(&env->le_ctx);
1407 lu_context_fini(&env->le_ctx);
1410 EXPORT_SYMBOL(lu_env_fini);
1412 int lu_env_refill(struct lu_env *env)
1416 result = lu_context_refill(&env->le_ctx);
1417 if (result == 0 && env->le_ses != NULL)
1418 result = lu_context_refill(env->le_ses);
1421 EXPORT_SYMBOL(lu_env_refill);
1423 static struct shrinker *lu_site_shrinker = NULL;
1426 static int lu_cache_shrink(int nr, unsigned int gfp_mask)
1429 struct lu_site *tmp;
1432 CFS_LIST_HEAD(splice);
1434 if (nr != 0 && !(gfp_mask & __GFP_FS))
1437 down(&lu_sites_guard);
1438 list_for_each_entry_safe(s, tmp, &lu_sites, ls_linkage) {
1440 remain = lu_site_purge(&lu_shrink_env, s, remain);
1442 * Move just shrunk site to the tail of site list to
1443 * assure shrinking fairness.
1445 list_move_tail(&s->ls_linkage, &splice);
1447 read_lock(&s->ls_guard);
1448 cached += s->ls_total - s->ls_busy;
1449 read_unlock(&s->ls_guard);
1453 list_splice(&splice, lu_sites.prev);
1454 up(&lu_sites_guard);
1458 #else /* !__KERNEL__ */
1459 static int lu_cache_shrink(int nr, unsigned int gfp_mask)
1463 #endif /* __KERNEL__ */
1465 int lu_ref_global_init(void);
1466 void lu_ref_global_fini(void);
1469 * Initialization of global lu_* data.
1471 int lu_global_init(void)
1475 CDEBUG(D_CONSOLE, "Lustre LU module (%p).\n", &lu_keys);
1477 LU_CONTEXT_KEY_INIT(&lu_global_key);
1478 result = lu_context_key_register(&lu_global_key);
1482 * At this level, we don't know what tags are needed, so allocate them
1483 * conservatively. This should not be too bad, because this
1484 * environment is global.
1486 down(&lu_sites_guard);
1487 result = lu_env_init_noref(&lu_shrink_env, NULL, LCT_SHRINKER);
1488 up(&lu_sites_guard);
1492 result = lu_ref_global_init();
1496 * seeks estimation: 3 seeks to read a record from oi, one to read
1497 * inode, one for ea. Unfortunately setting this high value results in
1498 * lu_object/inode cache consuming all the memory.
1500 lu_site_shrinker = set_shrinker(DEFAULT_SEEKS, lu_cache_shrink);
1501 if (lu_site_shrinker == NULL)
1504 result = lu_time_global_init();
1509 * Dual to lu_global_init().
1511 void lu_global_fini(void)
1513 lu_time_global_fini();
1514 if (lu_site_shrinker != NULL) {
1515 remove_shrinker(lu_site_shrinker);
1516 lu_site_shrinker = NULL;
1519 lu_context_key_degister(&lu_global_key);
1522 * Tear shrinker environment down _after_ de-registering
1523 * lu_global_key, because the latter has a value in the former.
1525 down(&lu_sites_guard);
1526 lu_env_fini(&lu_shrink_env);
1527 up(&lu_sites_guard);
1529 lu_ref_global_fini();
1532 struct lu_buf LU_BUF_NULL = {
1536 EXPORT_SYMBOL(LU_BUF_NULL);
1539 * Output site statistical counters into a buffer. Suitable for
1540 * lprocfs_rd_*()-style functions.
1542 int lu_site_stats_print(const struct lu_site *s, char *page, int count)
1548 * How many hash buckets are not-empty? Don't bother with locks: it's
1549 * an estimation anyway.
1551 for (i = 0, populated = 0; i < s->ls_hash_size; i++)
1552 populated += !hlist_empty(&s->ls_hash[i]);
1554 return snprintf(page, count, "%d %d %d/%d %d %d %d %d %d %d %d\n",
1559 s->ls_stats.s_created,
1560 s->ls_stats.s_cache_hit,
1561 s->ls_stats.s_cache_miss,
1562 s->ls_stats.s_cache_check,
1563 s->ls_stats.s_cache_race,
1564 s->ls_stats.s_cache_death_race,
1565 s->ls_stats.s_lru_purged);
1567 EXPORT_SYMBOL(lu_site_stats_print);
1570 * XXX: Functions below logically belong to the fid module, but they are used
1571 * by dt_store_open(). Put them here until better place is found.
1574 void fid_pack(struct lu_fid_pack *pack, const struct lu_fid *fid,
1575 struct lu_fid *befider)
1585 * Two cases: compact 6 bytes representation for a common case, and
1586 * full 17 byte representation for "unusual" fid.
1590 * Check that usual case is really usual.
1592 CLASSERT(LUSTRE_SEQ_MAX_WIDTH < 0xffffull);
1594 if (fid_is_igif(fid) ||
1595 seq > 0xffffffull || oid > 0xffff || fid_ver(fid) != 0) {
1596 fid_cpu_to_be(befider, fid);
1597 recsize = sizeof *befider;
1599 unsigned char *small_befider;
1601 small_befider = (char *)befider;
1603 small_befider[0] = seq >> 16;
1604 small_befider[1] = seq >> 8;
1605 small_befider[2] = seq;
1607 small_befider[3] = oid >> 8;
1608 small_befider[4] = oid;
1612 memcpy(pack->fp_area, befider, recsize);
1613 pack->fp_len = recsize + 1;
1615 EXPORT_SYMBOL(fid_pack);
1617 int fid_unpack(const struct lu_fid_pack *pack, struct lu_fid *fid)
1622 switch (pack->fp_len) {
1623 case sizeof *fid + 1:
1624 memcpy(fid, pack->fp_area, sizeof *fid);
1625 fid_be_to_cpu(fid, fid);
1628 const unsigned char *area;
1630 area = pack->fp_area;
1631 fid->f_seq = (area[0] << 16) | (area[1] << 8) | area[2];
1632 fid->f_oid = (area[3] << 8) | area[4];
1637 CERROR("Unexpected packed fid size: %d\n", pack->fp_len);
1642 EXPORT_SYMBOL(fid_unpack);
1644 const char *lu_time_names[LU_TIME_NR] = {
1645 [LU_TIME_FIND_LOOKUP] = "find_lookup",
1646 [LU_TIME_FIND_ALLOC] = "find_alloc",
1647 [LU_TIME_FIND_INSERT] = "find_insert"
1649 EXPORT_SYMBOL(lu_time_names);
1652 * Helper function to initialize a number of kmem slab caches at once.
1654 int lu_kmem_init(struct lu_kmem_descr *caches)
1658 for (result = 0; caches->ckd_cache != NULL; ++caches) {
1659 *caches->ckd_cache = cfs_mem_cache_create(caches->ckd_name,
1662 if (*caches->ckd_cache == NULL) {
1669 EXPORT_SYMBOL(lu_kmem_init);
1672 * Helper function to finalize a number of kmem slab cached at once. Dual to
1675 void lu_kmem_fini(struct lu_kmem_descr *caches)
1679 for (; caches->ckd_cache != NULL; ++caches) {
1680 if (*caches->ckd_cache != NULL) {
1681 rc = cfs_mem_cache_destroy(*caches->ckd_cache);
1682 LASSERTF(rc == 0, "couldn't destroy %s slab\n",
1684 *caches->ckd_cache = NULL;
1688 EXPORT_SYMBOL(lu_kmem_fini);