1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * Copyright (C) 2006 Cluster File Systems, Inc.
7 * Author: Nikita Danilov <nikita@clusterfs.com>
9 * This file is part of the Lustre file system, http://www.lustre.org
10 * Lustre is a trademark of Cluster File Systems, Inc.
12 * You may have signed or agreed to another license before downloading
13 * this software. If so, you are bound by the terms and conditions
14 * of that agreement, and the following does not apply to you. See the
15 * LICENSE file included with this distribution for more information.
17 * If you did not agree to a different license, then this copy of Lustre
18 * is open source software; you can redistribute it and/or modify it
19 * under the terms of version 2 of the GNU General Public License as
20 * published by the Free Software Foundation.
22 * In either case, Lustre is distributed in the hope that it will be
23 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
24 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * license text for more details.
27 * These are the only exported functions, they provide some generic
28 * infrastructure for managing object devices
31 #define DEBUG_SUBSYSTEM S_CLASS
33 # define EXPORT_SYMTAB
36 #include <linux/seq_file.h>
37 #include <linux/module.h>
38 #include <obd_support.h>
39 #include <lustre_disk.h>
40 #include <lustre_fid.h>
41 #include <lu_object.h>
42 #include <libcfs/list.h>
44 static void lu_object_free(const struct lu_context *ctx, struct lu_object *o);
47 * Decrease reference counter on object. If last reference is freed, return
48 * object to the cache, unless lu_object_is_dying(o) holds. In the latter
49 * case, free object immediately.
51 void lu_object_put(const struct lu_context *ctxt, struct lu_object *o)
53 struct lu_object_header *top;
55 struct lu_object *orig;
59 site = o->lo_dev->ld_site;
62 spin_lock(&site->ls_guard);
63 if (-- top->loh_ref == 0) {
65 * When last reference is released, iterate over object
66 * layers, and notify them that object is no longer busy.
68 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
69 if (o->lo_ops->loo_object_release != NULL)
70 o->lo_ops->loo_object_release(ctxt, o);
73 if (lu_object_is_dying(top)) {
75 * If object is dying (will not be cached), removed it
76 * from hash table and LRU.
78 * This is done with hash table and LRU lists
79 * locked. As the only way to acquire first reference
80 * to previously unreferenced object is through
81 * hash-table lookup (lu_object_find()), or LRU
82 * scanning (lu_site_purge()), that are done under
83 * hash-table and LRU lock, no race with concurrent
84 * object lookup is possible and we can safely destroy
87 hlist_del_init(&top->loh_hash);
88 list_del_init(&top->loh_lru);
92 spin_unlock(&site->ls_guard);
95 * Object was already removed from hash and lru above, can
98 lu_object_free(ctxt, orig);
100 EXPORT_SYMBOL(lu_object_put);
103 * Allocate new object.
105 * This follows object creation protocol, described in the comment within
106 * struct lu_device_operations definition.
108 static struct lu_object *lu_object_alloc(const struct lu_context *ctxt,
110 const struct lu_fid *f)
112 struct lu_object *scan;
113 struct lu_object *top;
114 struct list_head *layers;
119 * Create top-level object slice. This will also create
122 top = s->ls_top_dev->ld_ops->ldo_object_alloc(ctxt,
123 NULL, s->ls_top_dev);
128 * This is the only place where object fid is assigned. It's constant
131 top->lo_header->loh_fid = *f;
132 layers = &top->lo_header->loh_layers;
135 * Call ->loo_object_init() repeatedly, until no more new
136 * object slices are created.
139 list_for_each_entry(scan, layers, lo_linkage) {
140 if (scan->lo_flags & LU_OBJECT_ALLOCATED)
143 scan->lo_header = top->lo_header;
144 result = scan->lo_ops->loo_object_init(ctxt, scan);
146 lu_object_free(ctxt, top);
147 RETURN(ERR_PTR(result));
149 scan->lo_flags |= LU_OBJECT_ALLOCATED;
153 list_for_each_entry_reverse(scan, layers, lo_linkage) {
154 if (scan->lo_ops->loo_object_start != NULL) {
155 result = scan->lo_ops->loo_object_start(ctxt, scan);
157 lu_object_free(ctxt, top);
158 RETURN(ERR_PTR(result));
163 s->ls_stats.s_created ++;
170 static void lu_object_free(const struct lu_context *ctx, struct lu_object *o)
172 struct list_head splice;
173 struct lu_object *scan;
176 * First call ->loo_object_delete() method to release all resources.
178 list_for_each_entry_reverse(scan,
179 &o->lo_header->loh_layers, lo_linkage) {
180 if (scan->lo_ops->loo_object_delete != NULL)
181 scan->lo_ops->loo_object_delete(ctx, scan);
183 -- o->lo_dev->ld_site->ls_total;
185 * Then, splice object layers into stand-alone list, and call
186 * ->loo_object_free() on all layers to free memory. Splice is
187 * necessary, because lu_object_header is freed together with the
190 INIT_LIST_HEAD(&splice);
191 list_splice_init(&o->lo_header->loh_layers, &splice);
192 while (!list_empty(&splice)) {
193 o = container_of0(splice.next, struct lu_object, lo_linkage);
194 list_del_init(&o->lo_linkage);
195 LASSERT(o->lo_ops->loo_object_free != NULL);
196 o->lo_ops->loo_object_free(ctx, o);
201 * Free @nr objects from the cold end of the site LRU list.
203 void lu_site_purge(const struct lu_context *ctx, struct lu_site *s, int nr)
205 struct list_head dispose;
206 struct lu_object_header *h;
207 struct lu_object_header *temp;
209 INIT_LIST_HEAD(&dispose);
211 * Under LRU list lock, scan LRU list and move unreferenced objects to
212 * the dispose list, removing them from LRU and hash table.
214 spin_lock(&s->ls_guard);
215 list_for_each_entry_safe(h, temp, &s->ls_lru, loh_lru) {
220 hlist_del_init(&h->loh_hash);
221 list_move(&h->loh_lru, &dispose);
223 spin_unlock(&s->ls_guard);
225 * Free everything on the dispose list. This is safe against races due
226 * to the reasons described in lu_object_put().
228 while (!list_empty(&dispose)) {
229 h = container_of0(dispose.next,
230 struct lu_object_header, loh_lru);
231 list_del_init(&h->loh_lru);
232 lu_object_free(ctx, lu_object_top(h));
233 s->ls_stats.s_lru_purged ++;
236 EXPORT_SYMBOL(lu_site_purge);
241 * Code below has to jump through certain loops to output object description
242 * into libcfs_debug_msg-based log. The problem is that lu_object_print()
243 * composes object description from strings that are parts of _lines_ of
244 * output (i.e., strings that are not terminated by newline). This doesn't fit
245 * very well into libcfs_debug_msg() interface that assumes that each message
246 * supplied to it is a self-contained output line.
248 * To work around this, strings are collected in a temporary buffer
249 * (implemented as a value of lu_cdebug_key key), until terminating newline
250 * character is detected.
258 * XXX overflow is not handled correctly.
263 struct lu_cdebug_data {
267 char lck_area[LU_CDEBUG_LINE];
270 static void *lu_cdebug_key_init(const struct lu_context *ctx,
271 struct lu_context_key *key)
273 struct lu_cdebug_data *value;
275 OBD_ALLOC_PTR(value);
277 value = ERR_PTR(-ENOMEM);
281 static void lu_cdebug_key_fini(const struct lu_context *ctx,
282 struct lu_context_key *key, void *data)
284 struct lu_cdebug_data *value = data;
289 * Key, holding temporary buffer. This key is registered very early by
292 static struct lu_context_key lu_cdebug_key = {
293 .lct_tags = LCT_MD_THREAD|LCT_DT_THREAD|LCT_CL_THREAD,
294 .lct_init = lu_cdebug_key_init,
295 .lct_fini = lu_cdebug_key_fini
299 * Printer function emitting messages through libcfs_debug_msg().
301 int lu_cdebug_printer(const struct lu_context *ctx,
302 void *cookie, const char *format, ...)
304 struct lu_cdebug_print_info *info = cookie;
305 struct lu_cdebug_data *key;
310 va_start(args, format);
312 key = lu_context_key_get(ctx, &lu_cdebug_key);
313 LASSERT(key != NULL);
315 used = strlen(key->lck_area);
316 complete = format[strlen(format) - 1] == '\n';
318 * Append new chunk to the buffer.
320 vsnprintf(key->lck_area + used,
321 ARRAY_SIZE(key->lck_area) - used, format, args);
323 libcfs_debug_msg(info->lpi_subsys, info->lpi_mask,
324 info->lpi_file, info->lpi_fn,
325 info->lpi_line, "%s", key->lck_area);
326 key->lck_area[0] = 0;
331 EXPORT_SYMBOL(lu_cdebug_printer);
334 * Print object header.
336 static void lu_object_header_print(const struct lu_context *ctx,
337 void *cookie, lu_printer_t printer,
338 const struct lu_object_header *hdr)
340 (*printer)(ctx, cookie, "header@%p[%#lx, %d, "DFID"%s%s]",
341 hdr, hdr->loh_flags, hdr->loh_ref, PFID(&hdr->loh_fid),
342 hlist_unhashed(&hdr->loh_hash) ? "" : " hash",
343 list_empty(&hdr->loh_lru) ? "" : " lru");
347 * Print human readable representation of the @o to the @printer.
349 void lu_object_print(const struct lu_context *ctx, void *cookie,
350 lu_printer_t printer, const struct lu_object *o)
352 static const char ruler[] = "........................................";
353 struct lu_object_header *top;
357 lu_object_header_print(ctx, cookie, printer, top);
358 (*printer)(ctx, cookie, "\n");
359 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
360 depth = o->lo_depth + 4;
361 LASSERT(o->lo_ops->loo_object_print != NULL);
363 * print `.' @depth times.
365 (*printer)(ctx, cookie, "%*.*s", depth, depth, ruler);
366 o->lo_ops->loo_object_print(ctx, cookie, printer, o);
367 (*printer)(ctx, cookie, "\n");
370 EXPORT_SYMBOL(lu_object_print);
373 * Check object consistency.
375 int lu_object_invariant(const struct lu_object *o)
377 struct lu_object_header *top;
380 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
381 if (o->lo_ops->loo_object_invariant != NULL &&
382 !o->lo_ops->loo_object_invariant(o))
387 EXPORT_SYMBOL(lu_object_invariant);
389 static struct lu_object *htable_lookup(struct lu_site *s,
390 const struct hlist_head *bucket,
391 const struct lu_fid *f)
393 struct lu_object_header *h;
394 struct hlist_node *scan;
396 hlist_for_each_entry(h, scan, bucket, loh_hash) {
397 s->ls_stats.s_cache_check ++;
398 if (lu_fid_eq(&h->loh_fid, f) && !lu_object_is_dying(h)) {
399 /* bump reference count... */
400 if (h->loh_ref ++ == 0)
402 /* and move to the head of the LRU */
403 list_move_tail(&h->loh_lru, &s->ls_lru);
404 s->ls_stats.s_cache_hit ++;
405 return lu_object_top(h);
408 s->ls_stats.s_cache_miss ++;
412 static __u32 fid_hash(const struct lu_fid *f)
414 /* all objects with same id and different versions will belong to same
415 * collisions list. */
416 return (fid_seq(f) - 1) * LUSTRE_SEQ_MAX_WIDTH + fid_oid(f);
420 * Search cache for an object with the fid @f. If such object is found, return
421 * it. Otherwise, create new object, insert it into cache and return it. In
422 * any case, additional reference is acquired on the returned object.
424 struct lu_object *lu_object_find(const struct lu_context *ctxt,
425 struct lu_site *s, const struct lu_fid *f)
428 struct lu_object *shadow;
429 struct hlist_head *bucket;
432 * This uses standard index maintenance protocol:
434 * - search index under lock, and return object if found;
435 * - otherwise, unlock index, allocate new object;
436 * - lock index and search again;
437 * - if nothing is found (usual case), insert newly created
439 * - otherwise (race: other thread inserted object), free
440 * object just allocated.
445 bucket = s->ls_hash + (fid_hash(f) & s->ls_hash_mask);
446 spin_lock(&s->ls_guard);
447 o = htable_lookup(s, bucket, f);
449 spin_unlock(&s->ls_guard);
453 * Allocate new object. This may result in rather complicated
454 * operations, including fld queries, inode loading, etc.
456 o = lu_object_alloc(ctxt, s, f);
460 LASSERT(lu_fid_eq(lu_object_fid(o), f));
462 spin_lock(&s->ls_guard);
463 shadow = htable_lookup(s, bucket, f);
464 if (shadow == NULL) {
465 hlist_add_head(&o->lo_header->loh_hash, bucket);
466 list_add_tail(&o->lo_header->loh_lru, &s->ls_lru);
471 s->ls_stats.s_cache_race ++;
472 spin_unlock(&s->ls_guard);
474 lu_object_free(ctxt, o);
477 EXPORT_SYMBOL(lu_object_find);
480 LU_SITE_HTABLE_BITS = 8,
481 LU_SITE_HTABLE_SIZE = (1 << LU_SITE_HTABLE_BITS),
482 LU_SITE_HTABLE_MASK = LU_SITE_HTABLE_SIZE - 1
486 * Initialize site @s, with @d as the top level device.
488 int lu_site_init(struct lu_site *s, struct lu_device *top)
493 memset(s, 0, sizeof *s);
494 spin_lock_init(&s->ls_guard);
495 CFS_INIT_LIST_HEAD(&s->ls_lru);
500 * XXX nikita: fixed size hash-table.
502 s->ls_hash_mask = LU_SITE_HTABLE_MASK;
503 OBD_ALLOC(s->ls_hash, LU_SITE_HTABLE_SIZE * sizeof s->ls_hash[0]);
504 if (s->ls_hash != NULL) {
506 for (i = 0; i < LU_SITE_HTABLE_SIZE; i++)
507 INIT_HLIST_HEAD(&s->ls_hash[i]);
515 EXPORT_SYMBOL(lu_site_init);
518 * Finalize @s and release its resources.
520 void lu_site_fini(struct lu_site *s)
522 LASSERT(list_empty(&s->ls_lru));
523 LASSERT(s->ls_total == 0);
524 LASSERT(s->ls_busy == 0);
526 if (s->ls_hash != NULL) {
528 for (i = 0; i < LU_SITE_HTABLE_SIZE; i++)
529 LASSERT(hlist_empty(&s->ls_hash[i]));
531 LU_SITE_HTABLE_SIZE * sizeof s->ls_hash[0]);
534 if (s->ls_top_dev != NULL) {
535 s->ls_top_dev->ld_site = NULL;
536 lu_device_put(s->ls_top_dev);
537 s->ls_top_dev = NULL;
540 EXPORT_SYMBOL(lu_site_fini);
543 * Acquire additional reference on device @d
545 void lu_device_get(struct lu_device *d)
547 atomic_inc(&d->ld_ref);
549 EXPORT_SYMBOL(lu_device_get);
552 * Release reference on device @d.
554 void lu_device_put(struct lu_device *d)
556 atomic_dec(&d->ld_ref);
558 EXPORT_SYMBOL(lu_device_put);
561 * Initialize device @d of type @t.
563 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
565 memset(d, 0, sizeof *d);
566 atomic_set(&d->ld_ref, 0);
570 EXPORT_SYMBOL(lu_device_init);
573 * Finalize device @d.
575 void lu_device_fini(struct lu_device *d)
577 LASSERT(atomic_read(&d->ld_ref) == 0);
579 EXPORT_SYMBOL(lu_device_fini);
582 * Initialize object @o that is part of compound object @h and was created by
585 int lu_object_init(struct lu_object *o,
586 struct lu_object_header *h, struct lu_device *d)
588 memset(o, 0, sizeof *o);
592 CFS_INIT_LIST_HEAD(&o->lo_linkage);
595 EXPORT_SYMBOL(lu_object_init);
598 * Finalize object and release its resources.
600 void lu_object_fini(struct lu_object *o)
602 LASSERT(list_empty(&o->lo_linkage));
604 if (o->lo_dev != NULL) {
605 lu_device_put(o->lo_dev);
609 EXPORT_SYMBOL(lu_object_fini);
612 * Add object @o as first layer of compound object @h
614 * This is typically called by the ->ldo_object_alloc() method of top-level
617 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
619 list_move(&o->lo_linkage, &h->loh_layers);
621 EXPORT_SYMBOL(lu_object_add_top);
624 * Add object @o as a layer of compound object, going after @before.1
626 * This is typically called by the ->ldo_object_alloc() method of
629 void lu_object_add(struct lu_object *before, struct lu_object *o)
631 list_move(&o->lo_linkage, &before->lo_linkage);
633 EXPORT_SYMBOL(lu_object_add);
636 * Initialize compound object.
638 int lu_object_header_init(struct lu_object_header *h)
640 memset(h, 0, sizeof *h);
642 INIT_HLIST_NODE(&h->loh_hash);
643 CFS_INIT_LIST_HEAD(&h->loh_lru);
644 CFS_INIT_LIST_HEAD(&h->loh_layers);
647 EXPORT_SYMBOL(lu_object_header_init);
650 * Finalize compound object.
652 void lu_object_header_fini(struct lu_object_header *h)
654 LASSERT(list_empty(&h->loh_layers));
655 LASSERT(list_empty(&h->loh_lru));
656 LASSERT(hlist_unhashed(&h->loh_hash));
658 EXPORT_SYMBOL(lu_object_header_fini);
661 * Given a compound object, find its slice, corresponding to the device type
664 struct lu_object *lu_object_locate(struct lu_object_header *h,
665 struct lu_device_type *dtype)
669 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
670 if (o->lo_dev->ld_type == dtype)
675 EXPORT_SYMBOL(lu_object_locate);
679 * Maximal number of tld slots.
681 LU_CONTEXT_KEY_NR = 16
684 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
686 static spinlock_t lu_keys_guard = SPIN_LOCK_UNLOCKED;
691 int lu_context_key_register(struct lu_context_key *key)
696 LASSERT(key->lct_init != NULL);
697 LASSERT(key->lct_fini != NULL);
698 LASSERT(key->lct_tags != 0);
701 spin_lock(&lu_keys_guard);
702 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
703 if (lu_keys[i] == NULL) {
711 spin_unlock(&lu_keys_guard);
714 EXPORT_SYMBOL(lu_context_key_register);
719 void lu_context_key_degister(struct lu_context_key *key)
721 LASSERT(key->lct_used >= 1);
722 LASSERT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
724 if (key->lct_used > 1)
725 CERROR("key has instances.\n");
726 spin_lock(&lu_keys_guard);
727 lu_keys[key->lct_index] = NULL;
728 spin_unlock(&lu_keys_guard);
730 EXPORT_SYMBOL(lu_context_key_degister);
733 * Return value associated with key @key in context @ctx.
735 void *lu_context_key_get(const struct lu_context *ctx,
736 struct lu_context_key *key)
738 LASSERT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
739 return ctx->lc_value[key->lct_index];
741 EXPORT_SYMBOL(lu_context_key_get);
743 static void keys_fini(struct lu_context *ctx)
747 if (ctx->lc_value != NULL) {
748 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
749 if (ctx->lc_value[i] != NULL) {
750 struct lu_context_key *key;
753 LASSERT(key != NULL);
754 LASSERT(key->lct_fini != NULL);
755 LASSERT(key->lct_used > 1);
757 key->lct_fini(ctx, key, ctx->lc_value[i]);
759 ctx->lc_value[i] = NULL;
762 OBD_FREE(ctx->lc_value,
763 ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
764 ctx->lc_value = NULL;
768 static int keys_init(struct lu_context *ctx)
773 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
774 if (ctx->lc_value != NULL) {
775 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
776 struct lu_context_key *key;
779 if (key != NULL && key->lct_tags & ctx->lc_tags) {
782 LASSERT(key->lct_init != NULL);
783 LASSERT(key->lct_index == i);
785 value = key->lct_init(ctx, key);
788 return PTR_ERR(value);
791 ctx->lc_value[i] = value;
801 * Initialize context data-structure. Create values for all keys.
803 int lu_context_init(struct lu_context *ctx, __u32 tags)
805 memset(ctx, 0, sizeof *ctx);
810 EXPORT_SYMBOL(lu_context_init);
813 * Finalize context data-structure. Destroy key values.
815 void lu_context_fini(struct lu_context *ctx)
819 EXPORT_SYMBOL(lu_context_fini);
822 * Called before entering context.
824 void lu_context_enter(struct lu_context *ctx)
827 EXPORT_SYMBOL(lu_context_enter);
830 * Called after exiting from @ctx
832 void lu_context_exit(struct lu_context *ctx)
836 if (ctx->lc_value != NULL) {
837 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
838 if (ctx->lc_value[i] != NULL) {
839 struct lu_context_key *key;
842 LASSERT(key != NULL);
843 if (key->lct_exit != NULL)
845 key, ctx->lc_value[i]);
850 EXPORT_SYMBOL(lu_context_exit);
853 * Initialization of global lu_* data.
855 int lu_global_init(void)
859 result = lu_context_key_register(&lu_cdebug_key);
864 * Dual to lu_global_init().
866 void lu_global_fini(void)
868 lu_context_key_degister(&lu_cdebug_key);