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;
118 * Create top-level object slice. This will also create
121 top = s->ls_top_dev->ld_ops->ldo_object_alloc(ctxt,
122 NULL, s->ls_top_dev);
127 * This is the only place where object fid is assigned. It's constant
130 top->lo_header->loh_fid = *f;
133 * Call ->loo_object_init() repeatedly, until no more new
134 * object slices are created.
137 list_for_each_entry(scan,
138 &top->lo_header->loh_layers, lo_linkage) {
139 if (scan->lo_flags & LU_OBJECT_ALLOCATED)
142 scan->lo_header = top->lo_header;
143 result = scan->lo_ops->loo_object_init(ctxt, scan);
145 lu_object_free(ctxt, top);
146 RETURN(ERR_PTR(result));
148 scan->lo_flags |= LU_OBJECT_ALLOCATED;
151 s->ls_stats.s_created ++;
158 static void lu_object_free(const struct lu_context *ctx, struct lu_object *o)
160 struct list_head splice;
161 struct lu_object *scan;
164 * First call ->loo_object_delete() method to release all resources.
166 list_for_each_entry_reverse(scan,
167 &o->lo_header->loh_layers, lo_linkage) {
168 if (scan->lo_ops->loo_object_delete != NULL)
169 scan->lo_ops->loo_object_delete(ctx, scan);
171 -- o->lo_dev->ld_site->ls_total;
173 * Then, splice object layers into stand-alone list, and call
174 * ->loo_object_free() on all layers to free memory. Splice is
175 * necessary, because lu_object_header is freed together with the
178 INIT_LIST_HEAD(&splice);
179 list_splice_init(&o->lo_header->loh_layers, &splice);
180 while (!list_empty(&splice)) {
181 o = container_of0(splice.next, struct lu_object, lo_linkage);
182 list_del_init(&o->lo_linkage);
183 LASSERT(o->lo_ops->loo_object_free != NULL);
184 o->lo_ops->loo_object_free(ctx, o);
189 * Free @nr objects from the cold end of the site LRU list.
191 void lu_site_purge(const struct lu_context *ctx, struct lu_site *s, int nr)
193 struct list_head dispose;
194 struct lu_object_header *h;
195 struct lu_object_header *temp;
197 INIT_LIST_HEAD(&dispose);
199 * Under LRU list lock, scan LRU list and move unreferenced objects to
200 * the dispose list, removing them from LRU and hash table.
202 spin_lock(&s->ls_guard);
203 list_for_each_entry_safe(h, temp, &s->ls_lru, loh_lru) {
208 hlist_del_init(&h->loh_hash);
209 list_move(&h->loh_lru, &dispose);
211 spin_unlock(&s->ls_guard);
213 * Free everything on the dispose list. This is safe against races due
214 * to the reasons described in lu_object_put().
216 while (!list_empty(&dispose)) {
217 h = container_of0(dispose.next,
218 struct lu_object_header, loh_lru);
219 list_del_init(&h->loh_lru);
220 lu_object_free(ctx, lu_object_top(h));
221 s->ls_stats.s_lru_purged ++;
224 EXPORT_SYMBOL(lu_site_purge);
227 * Print human readable representation of the @o to the @f.
229 int lu_object_print(const struct lu_context *ctx,
230 struct seq_file *f, const struct lu_object *o)
232 static char ruler[] = "........................................";
233 struct lu_object_header *top;
239 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
241 LASSERT(o->lo_ops->loo_object_print != NULL);
243 * print `.' @depth times.
245 nob += seq_printf(f, "%*.*s", depth, depth, ruler);
246 nob += o->lo_ops->loo_object_print(ctx, f, o);
247 nob += seq_printf(f, "\n");
251 EXPORT_SYMBOL(lu_object_print);
254 * Check object consistency.
256 int lu_object_invariant(const struct lu_object *o)
258 struct lu_object_header *top;
261 list_for_each_entry(o, &top->loh_layers, lo_linkage) {
262 if (o->lo_ops->loo_object_invariant != NULL &&
263 !o->lo_ops->loo_object_invariant(o))
268 EXPORT_SYMBOL(lu_object_invariant);
270 static struct lu_object *htable_lookup(struct lu_site *s,
271 const struct hlist_head *bucket,
272 const struct lu_fid *f)
274 struct lu_object_header *h;
275 struct hlist_node *scan;
277 hlist_for_each_entry(h, scan, bucket, loh_hash) {
278 s->ls_stats.s_cache_check ++;
279 if (lu_fid_eq(&h->loh_fid, f) && !lu_object_is_dying(h)) {
280 /* bump reference count... */
281 if (h->loh_ref ++ == 0)
283 /* and move to the head of the LRU */
284 list_move_tail(&h->loh_lru, &s->ls_lru);
285 s->ls_stats.s_cache_hit ++;
286 return lu_object_top(h);
289 s->ls_stats.s_cache_miss ++;
293 static __u32 fid_hash(const struct lu_fid *f)
295 /* all objects with same id and different versions will belong to same
296 * collisions list. */
297 return (fid_seq(f) - 1) * LUSTRE_SEQ_MAX_WIDTH + fid_oid(f);
301 * Search cache for an object with the fid @f. If such object is found, return
302 * it. Otherwise, create new object, insert it into cache and return it. In
303 * any case, additional reference is acquired on the returned object.
305 struct lu_object *lu_object_find(const struct lu_context *ctxt,
306 struct lu_site *s, const struct lu_fid *f)
309 struct lu_object *shadow;
310 struct hlist_head *bucket;
313 * This uses standard index maintenance protocol:
315 * - search index under lock, and return object if found;
316 * - otherwise, unlock index, allocate new object;
317 * - lock index and search again;
318 * - if nothing is found (usual case), insert newly created
320 * - otherwise (race: other thread inserted object), free
321 * object just allocated.
326 bucket = s->ls_hash + (fid_hash(f) & s->ls_hash_mask);
327 spin_lock(&s->ls_guard);
328 o = htable_lookup(s, bucket, f);
330 spin_unlock(&s->ls_guard);
334 * Allocate new object. This may result in rather complicated
335 * operations, including fld queries, inode loading, etc.
337 o = lu_object_alloc(ctxt, s, f);
341 LASSERT(lu_fid_eq(lu_object_fid(o), f));
343 spin_lock(&s->ls_guard);
344 shadow = htable_lookup(s, bucket, f);
345 if (shadow == NULL) {
346 hlist_add_head(&o->lo_header->loh_hash, bucket);
347 list_add_tail(&s->ls_lru, &o->lo_header->loh_lru);
352 s->ls_stats.s_cache_race ++;
353 spin_unlock(&s->ls_guard);
355 lu_object_free(ctxt, o);
358 EXPORT_SYMBOL(lu_object_find);
361 LU_SITE_HTABLE_BITS = 8,
362 LU_SITE_HTABLE_SIZE = (1 << LU_SITE_HTABLE_BITS),
363 LU_SITE_HTABLE_MASK = LU_SITE_HTABLE_SIZE - 1
367 * Initialize site @s, with @d as the top level device.
369 int lu_site_init(struct lu_site *s, struct lu_device *top)
374 memset(s, 0, sizeof *s);
375 spin_lock_init(&s->ls_guard);
376 CFS_INIT_LIST_HEAD(&s->ls_lru);
381 * XXX nikita: fixed size hash-table.
383 s->ls_hash_mask = LU_SITE_HTABLE_MASK;
384 OBD_ALLOC(s->ls_hash, LU_SITE_HTABLE_SIZE * sizeof s->ls_hash[0]);
385 if (s->ls_hash != NULL) {
387 for (i = 0; i < LU_SITE_HTABLE_SIZE; i++)
388 INIT_HLIST_HEAD(&s->ls_hash[i]);
396 EXPORT_SYMBOL(lu_site_init);
399 * Finalize @s and release its resources.
401 void lu_site_fini(struct lu_site *s)
403 LASSERT(list_empty(&s->ls_lru));
404 LASSERT(s->ls_total == 0);
405 LASSERT(s->ls_busy == 0);
407 if (s->ls_hash != NULL) {
409 for (i = 0; i < LU_SITE_HTABLE_SIZE; i++)
410 LASSERT(hlist_empty(&s->ls_hash[i]));
412 LU_SITE_HTABLE_SIZE * sizeof s->ls_hash[0]);
415 if (s->ls_top_dev != NULL) {
416 s->ls_top_dev->ld_site = NULL;
417 lu_device_put(s->ls_top_dev);
418 s->ls_top_dev = NULL;
421 EXPORT_SYMBOL(lu_site_fini);
424 * Acquire additional reference on device @d
426 void lu_device_get(struct lu_device *d)
428 atomic_inc(&d->ld_ref);
430 EXPORT_SYMBOL(lu_device_get);
433 * Release reference on device @d.
435 void lu_device_put(struct lu_device *d)
437 atomic_dec(&d->ld_ref);
439 EXPORT_SYMBOL(lu_device_put);
442 * Initialize device @d of type @t.
444 int lu_device_init(struct lu_device *d, struct lu_device_type *t)
446 memset(d, 0, sizeof *d);
447 atomic_set(&d->ld_ref, 0);
451 EXPORT_SYMBOL(lu_device_init);
454 * Finalize device @d.
456 void lu_device_fini(struct lu_device *d)
458 LASSERT(atomic_read(&d->ld_ref) == 0);
460 EXPORT_SYMBOL(lu_device_fini);
463 * Initialize object @o that is part of compound object @h and was created by
466 int lu_object_init(struct lu_object *o,
467 struct lu_object_header *h, struct lu_device *d)
469 memset(o, 0, sizeof *o);
473 CFS_INIT_LIST_HEAD(&o->lo_linkage);
476 EXPORT_SYMBOL(lu_object_init);
479 * Finalize object and release its resources.
481 void lu_object_fini(struct lu_object *o)
483 LASSERT(list_empty(&o->lo_linkage));
485 if (o->lo_dev != NULL) {
486 lu_device_put(o->lo_dev);
490 EXPORT_SYMBOL(lu_object_fini);
493 * Add object @o as first layer of compound object @h
495 * This is typically called by the ->ldo_object_alloc() method of top-level
498 void lu_object_add_top(struct lu_object_header *h, struct lu_object *o)
500 list_move(&o->lo_linkage, &h->loh_layers);
502 EXPORT_SYMBOL(lu_object_add_top);
505 * Add object @o as a layer of compound object, going after @before.1
507 * This is typically called by the ->ldo_object_alloc() method of
510 void lu_object_add(struct lu_object *before, struct lu_object *o)
512 list_move(&o->lo_linkage, &before->lo_linkage);
514 EXPORT_SYMBOL(lu_object_add);
517 * Initialize compound object.
519 int lu_object_header_init(struct lu_object_header *h)
521 memset(h, 0, sizeof *h);
523 INIT_HLIST_NODE(&h->loh_hash);
524 CFS_INIT_LIST_HEAD(&h->loh_lru);
525 CFS_INIT_LIST_HEAD(&h->loh_layers);
528 EXPORT_SYMBOL(lu_object_header_init);
531 * Finalize compound object.
533 void lu_object_header_fini(struct lu_object_header *h)
535 LASSERT(list_empty(&h->loh_layers));
536 LASSERT(list_empty(&h->loh_lru));
537 LASSERT(hlist_unhashed(&h->loh_hash));
539 EXPORT_SYMBOL(lu_object_header_fini);
542 * Given a compound object, find its slice, corresponding to the device type
545 struct lu_object *lu_object_locate(struct lu_object_header *h,
546 struct lu_device_type *dtype)
550 list_for_each_entry(o, &h->loh_layers, lo_linkage) {
551 if (o->lo_dev->ld_type == dtype)
556 EXPORT_SYMBOL(lu_object_locate);
560 * Maximal number of tld slots.
562 LU_CONTEXT_KEY_NR = 16
565 static struct lu_context_key *lu_keys[LU_CONTEXT_KEY_NR] = { NULL, };
567 static spinlock_t lu_keys_guard = SPIN_LOCK_UNLOCKED;
572 int lu_context_key_register(struct lu_context_key *key)
577 LASSERT(key->lct_init != NULL);
578 LASSERT(key->lct_fini != NULL);
579 LASSERT(key->lct_tags != 0);
582 spin_lock(&lu_keys_guard);
583 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
584 if (lu_keys[i] == NULL) {
592 spin_unlock(&lu_keys_guard);
595 EXPORT_SYMBOL(lu_context_key_register);
600 void lu_context_key_degister(struct lu_context_key *key)
602 LASSERT(key->lct_used >= 1);
603 LASSERT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
605 if (key->lct_used > 1)
606 CERROR("key has instances.\n");
607 spin_lock(&lu_keys_guard);
608 lu_keys[key->lct_index] = NULL;
609 spin_unlock(&lu_keys_guard);
611 EXPORT_SYMBOL(lu_context_key_degister);
614 * Return value associated with key @key in context @ctx.
616 void *lu_context_key_get(const struct lu_context *ctx,
617 struct lu_context_key *key)
619 LASSERT(0 <= key->lct_index && key->lct_index < ARRAY_SIZE(lu_keys));
620 return ctx->lc_value[key->lct_index];
622 EXPORT_SYMBOL(lu_context_key_get);
624 static void keys_fini(struct lu_context *ctx)
628 if (ctx->lc_value != NULL) {
629 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
630 if (ctx->lc_value[i] != NULL) {
631 struct lu_context_key *key;
634 LASSERT(key != NULL);
635 LASSERT(key->lct_fini != NULL);
636 LASSERT(key->lct_used > 1);
638 key->lct_fini(ctx, key, ctx->lc_value[i]);
640 ctx->lc_value[i] = NULL;
643 OBD_FREE(ctx->lc_value,
644 ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
645 ctx->lc_value = NULL;
649 static int keys_init(struct lu_context *ctx)
654 OBD_ALLOC(ctx->lc_value, ARRAY_SIZE(lu_keys) * sizeof ctx->lc_value[0]);
655 if (ctx->lc_value != NULL) {
656 for (i = 0; i < ARRAY_SIZE(lu_keys); ++i) {
657 struct lu_context_key *key;
660 if (key != NULL && key->lct_tags & ctx->lc_tags) {
663 LASSERT(key->lct_init != NULL);
664 LASSERT(key->lct_index == i);
666 value = key->lct_init(ctx, key);
669 return PTR_ERR(value);
672 ctx->lc_value[i] = value;
682 * Initialize context data-structure. Create values for all keys.
684 int lu_context_init(struct lu_context *ctx, __u32 tags)
686 memset(ctx, 0, sizeof *ctx);
691 EXPORT_SYMBOL(lu_context_init);
694 * Finalize context data-structure. Destroy key values.
696 void lu_context_fini(struct lu_context *ctx)
700 EXPORT_SYMBOL(lu_context_fini);
703 * Called before entering context.
705 void lu_context_enter(struct lu_context *ctx)
708 EXPORT_SYMBOL(lu_context_enter);
711 * Called after exiting from @ctx
713 void lu_context_exit(struct lu_context *ctx)
716 EXPORT_SYMBOL(lu_context_exit);