4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/obdclass/lu_ref.c
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_CLASS
44 # include <libcfs/libcfs.h>
46 # include <liblustre.h>
50 #include <obd_class.h>
51 #include <obd_support.h>
57 * Asserts a condition for a given lu_ref. Must be called with
58 * lu_ref::lf_guard held.
60 #define REFASSERT(ref, expr) \
62 struct lu_ref *__ref = (ref); \
64 if (unlikely(!(expr))) { \
65 lu_ref_print(__ref); \
66 cfs_spin_unlock(&__ref->lf_guard); \
69 cfs_spin_lock(&__ref->lf_guard); \
74 struct lu_ref *ll_ref;
75 cfs_list_t ll_linkage;
77 const void *ll_source;
80 static cfs_mem_cache_t *lu_ref_link_kmem;
82 static struct lu_kmem_descr lu_ref_caches[] = {
84 .ckd_cache = &lu_ref_link_kmem,
85 .ckd_name = "lu_ref_link_kmem",
86 .ckd_size = sizeof (struct lu_ref_link)
94 * Global list of active (initialized, but not finalized) lu_ref's.
96 * Protected by lu_ref_refs_guard.
98 static CFS_LIST_HEAD(lu_ref_refs);
99 static cfs_spinlock_t lu_ref_refs_guard;
100 static struct lu_ref lu_ref_marker = {
101 .lf_guard = CFS_SPIN_LOCK_UNLOCKED,
102 .lf_list = CFS_LIST_HEAD_INIT(lu_ref_marker.lf_list),
103 .lf_linkage = CFS_LIST_HEAD_INIT(lu_ref_marker.lf_linkage)
106 void lu_ref_print(const struct lu_ref *ref)
108 struct lu_ref_link *link;
110 CERROR("lu_ref: %p %d %d %s:%d\n",
111 ref, ref->lf_refs, ref->lf_failed, ref->lf_func, ref->lf_line);
112 cfs_list_for_each_entry(link, &ref->lf_list, ll_linkage) {
113 CERROR(" link: %s %p\n", link->ll_scope, link->ll_source);
116 EXPORT_SYMBOL(lu_ref_print);
118 static int lu_ref_is_marker(const struct lu_ref *ref)
120 return (ref == &lu_ref_marker);
123 void lu_ref_print_all(void)
127 cfs_spin_lock(&lu_ref_refs_guard);
128 cfs_list_for_each_entry(ref, &lu_ref_refs, lf_linkage) {
129 if (lu_ref_is_marker(ref))
132 cfs_spin_lock(&ref->lf_guard);
134 cfs_spin_unlock(&ref->lf_guard);
136 cfs_spin_unlock(&lu_ref_refs_guard);
138 EXPORT_SYMBOL(lu_ref_print_all);
140 void lu_ref_init_loc(struct lu_ref *ref, const char *func, const int line)
145 cfs_spin_lock_init(&ref->lf_guard);
146 CFS_INIT_LIST_HEAD(&ref->lf_list);
147 cfs_spin_lock(&lu_ref_refs_guard);
148 cfs_list_add(&ref->lf_linkage, &lu_ref_refs);
149 cfs_spin_unlock(&lu_ref_refs_guard);
151 EXPORT_SYMBOL(lu_ref_init_loc);
153 void lu_ref_fini(struct lu_ref *ref)
155 REFASSERT(ref, cfs_list_empty(&ref->lf_list));
156 REFASSERT(ref, ref->lf_refs == 0);
157 cfs_spin_lock(&lu_ref_refs_guard);
158 cfs_list_del_init(&ref->lf_linkage);
159 cfs_spin_unlock(&lu_ref_refs_guard);
161 EXPORT_SYMBOL(lu_ref_fini);
163 static struct lu_ref_link *lu_ref_add_context(struct lu_ref *ref,
164 enum cfs_alloc_flags flags,
168 struct lu_ref_link *link;
171 if (lu_ref_link_kmem != NULL) {
172 OBD_SLAB_ALLOC_PTR_GFP(link, lu_ref_link_kmem, flags);
175 link->ll_scope = scope;
176 link->ll_source = source;
177 cfs_spin_lock(&ref->lf_guard);
178 cfs_list_add_tail(&link->ll_linkage, &ref->lf_list);
180 cfs_spin_unlock(&ref->lf_guard);
185 cfs_spin_lock(&ref->lf_guard);
187 cfs_spin_unlock(&ref->lf_guard);
188 link = ERR_PTR(-ENOMEM);
194 struct lu_ref_link *lu_ref_add(struct lu_ref *ref, const char *scope,
198 return lu_ref_add_context(ref, CFS_ALLOC_STD, scope, source);
200 EXPORT_SYMBOL(lu_ref_add);
203 * Version of lu_ref_add() to be used in non-blockable contexts.
205 struct lu_ref_link *lu_ref_add_atomic(struct lu_ref *ref, const char *scope,
208 return lu_ref_add_context(ref, CFS_ALLOC_ATOMIC, scope, source);
210 EXPORT_SYMBOL(lu_ref_add_atomic);
212 static inline int lu_ref_link_eq(const struct lu_ref_link *link,
213 const char *scope, const void *source)
215 return link->ll_source == source && !strcmp(link->ll_scope, scope);
219 * Maximal chain length seen so far.
221 static unsigned lu_ref_chain_max_length = 127;
224 * Searches for a lu_ref_link with given [scope, source] within given lu_ref.
226 static struct lu_ref_link *lu_ref_find(struct lu_ref *ref, const char *scope,
229 struct lu_ref_link *link;
233 cfs_list_for_each_entry(link, &ref->lf_list, ll_linkage) {
235 if (lu_ref_link_eq(link, scope, source)) {
236 if (iterations > lu_ref_chain_max_length) {
237 CWARN("Long lu_ref chain %d \"%s\":%p\n",
238 iterations, scope, source);
239 lu_ref_chain_max_length = iterations * 3 / 2;
247 void lu_ref_del(struct lu_ref *ref, const char *scope, const void *source)
249 struct lu_ref_link *link;
251 cfs_spin_lock(&ref->lf_guard);
252 link = lu_ref_find(ref, scope, source);
254 cfs_list_del(&link->ll_linkage);
256 cfs_spin_unlock(&ref->lf_guard);
257 OBD_SLAB_FREE(link, lu_ref_link_kmem, sizeof(*link));
259 REFASSERT(ref, ref->lf_failed > 0);
261 cfs_spin_unlock(&ref->lf_guard);
264 EXPORT_SYMBOL(lu_ref_del);
266 void lu_ref_set_at(struct lu_ref *ref, struct lu_ref_link *link,
268 const void *source0, const void *source1)
270 cfs_spin_lock(&ref->lf_guard);
271 if (link != ERR_PTR(-ENOMEM)) {
272 REFASSERT(ref, link->ll_ref == ref);
273 REFASSERT(ref, lu_ref_link_eq(link, scope, source0));
274 link->ll_source = source1;
276 REFASSERT(ref, ref->lf_failed > 0);
278 cfs_spin_unlock(&ref->lf_guard);
280 EXPORT_SYMBOL(lu_ref_set_at);
282 void lu_ref_del_at(struct lu_ref *ref, struct lu_ref_link *link,
283 const char *scope, const void *source)
285 if (link != ERR_PTR(-ENOMEM)) {
286 cfs_spin_lock(&ref->lf_guard);
287 REFASSERT(ref, link->ll_ref == ref);
288 REFASSERT(ref, lu_ref_link_eq(link, scope, source));
289 cfs_list_del(&link->ll_linkage);
291 cfs_spin_unlock(&ref->lf_guard);
292 OBD_SLAB_FREE(link, lu_ref_link_kmem, sizeof(*link));
294 cfs_spin_lock(&ref->lf_guard);
295 REFASSERT(ref, ref->lf_failed > 0);
297 cfs_spin_unlock(&ref->lf_guard);
300 EXPORT_SYMBOL(lu_ref_del_at);
302 #if defined(__KERNEL__) && defined(LPROCFS)
304 static void *lu_ref_seq_start(struct seq_file *seq, loff_t *pos)
306 struct lu_ref *ref = seq->private;
308 cfs_spin_lock(&lu_ref_refs_guard);
309 if (cfs_list_empty(&ref->lf_linkage))
311 cfs_spin_unlock(&lu_ref_refs_guard);
316 static void *lu_ref_seq_next(struct seq_file *seq, void *p, loff_t *pos)
318 struct lu_ref *ref = p;
321 LASSERT(seq->private == p);
322 LASSERT(!cfs_list_empty(&ref->lf_linkage));
324 cfs_spin_lock(&lu_ref_refs_guard);
325 next = cfs_list_entry(ref->lf_linkage.next, struct lu_ref, lf_linkage);
326 if (&next->lf_linkage == &lu_ref_refs) {
330 cfs_list_move(&ref->lf_linkage, &next->lf_linkage);
332 cfs_spin_unlock(&lu_ref_refs_guard);
336 static void lu_ref_seq_stop(struct seq_file *seq, void *p)
342 static int lu_ref_seq_show(struct seq_file *seq, void *p)
344 struct lu_ref *ref = p;
347 cfs_spin_lock(&lu_ref_refs_guard);
348 next = cfs_list_entry(ref->lf_linkage.next, struct lu_ref, lf_linkage);
349 if ((&next->lf_linkage == &lu_ref_refs) || lu_ref_is_marker(next)) {
350 cfs_spin_unlock(&lu_ref_refs_guard);
354 /* print the entry */
356 cfs_spin_lock(&next->lf_guard);
357 seq_printf(seq, "lu_ref: %p %d %d %s:%d\n",
358 next, next->lf_refs, next->lf_failed,
359 next->lf_func, next->lf_line);
360 if (next->lf_refs > 64) {
361 seq_printf(seq, " too many references, skip\n");
363 struct lu_ref_link *link;
366 cfs_list_for_each_entry(link, &next->lf_list, ll_linkage)
367 seq_printf(seq, " #%d link: %s %p\n",
368 i++, link->ll_scope, link->ll_source);
370 cfs_spin_unlock(&next->lf_guard);
371 cfs_spin_unlock(&lu_ref_refs_guard);
376 static struct seq_operations lu_ref_seq_ops = {
377 .start = lu_ref_seq_start,
378 .stop = lu_ref_seq_stop,
379 .next = lu_ref_seq_next,
380 .show = lu_ref_seq_show
383 static int lu_ref_seq_open(struct inode *inode, struct file *file)
385 struct lu_ref *marker = &lu_ref_marker;
388 result = seq_open(file, &lu_ref_seq_ops);
390 cfs_spin_lock(&lu_ref_refs_guard);
391 if (!cfs_list_empty(&marker->lf_linkage))
394 cfs_list_add(&marker->lf_linkage, &lu_ref_refs);
395 cfs_spin_unlock(&lu_ref_refs_guard);
398 struct seq_file *f = file->private_data;
401 seq_release(inode, file);
408 static int lu_ref_seq_release(struct inode *inode, struct file *file)
410 struct lu_ref *ref = ((struct seq_file *)file->private_data)->private;
412 cfs_spin_lock(&lu_ref_refs_guard);
413 cfs_list_del_init(&ref->lf_linkage);
414 cfs_spin_unlock(&lu_ref_refs_guard);
416 return seq_release(inode, file);
419 static struct file_operations lu_ref_dump_fops = {
420 .owner = THIS_MODULE,
421 .open = lu_ref_seq_open,
424 .release = lu_ref_seq_release
429 int lu_ref_global_init(void)
434 "lu_ref tracking is enabled. Performance isn't.\n");
437 cfs_spin_lock_init(&lu_ref_refs_guard);
438 result = lu_kmem_init(lu_ref_caches);
440 #if defined(__KERNEL__) && defined(LPROCFS)
442 result = lprocfs_seq_create(proc_lustre_root, "lu_refs",
443 0444, &lu_ref_dump_fops, NULL);
445 lu_kmem_fini(lu_ref_caches);
452 void lu_ref_global_fini(void)
454 #if defined(__KERNEL__) && defined(LPROCFS)
455 lprocfs_remove_proc_entry("lu_refs", proc_lustre_root);
457 lu_kmem_fini(lu_ref_caches);
460 #endif /* USE_LU_REF */