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.
30 * Copyright (c) 2012, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/lu_ref.c
40 * Author: Nikita Danilov <nikita.danilov@sun.com>
43 #define DEBUG_SUBSYSTEM S_CLASS
45 #include <libcfs/libcfs.h>
47 #include <obd_class.h>
48 #include <obd_support.h>
54 * Asserts a condition for a given lu_ref. Must be called with
55 * lu_ref::lf_guard held.
57 #define REFASSERT(ref, expr) do { \
58 struct lu_ref *__tmp = (ref); \
60 if (unlikely(!(expr))) { \
61 lu_ref_print(__tmp); \
62 spin_unlock(&__tmp->lf_guard); \
65 spin_lock(&__tmp->lf_guard); \
69 static struct kmem_cache *lu_ref_link_kmem;
71 static struct lu_kmem_descr lu_ref_caches[] = {
73 .ckd_cache = &lu_ref_link_kmem,
74 .ckd_name = "lu_ref_link_kmem",
75 .ckd_size = sizeof (struct lu_ref_link)
83 * Global list of active (initialized, but not finalized) lu_ref's.
85 * Protected by lu_ref_refs_guard.
87 static struct list_head lu_ref_refs;
88 static spinlock_t lu_ref_refs_guard;
89 static struct lu_ref lu_ref_marker = {
90 .lf_guard = __SPIN_LOCK_UNLOCKED(lu_ref_marker.lf_guard),
91 .lf_list = LIST_HEAD_INIT(lu_ref_marker.lf_list),
92 .lf_linkage = LIST_HEAD_INIT(lu_ref_marker.lf_linkage)
95 void lu_ref_print(const struct lu_ref *ref)
97 struct lu_ref_link *link;
99 CERROR("lu_ref: %p %d %d %s:%d\n",
100 ref, ref->lf_refs, ref->lf_failed, ref->lf_func, ref->lf_line);
101 list_for_each_entry(link, &ref->lf_list, ll_linkage) {
102 CERROR(" link: %s %p\n", link->ll_scope, link->ll_source);
106 static int lu_ref_is_marker(const struct lu_ref *ref)
108 return (ref == &lu_ref_marker);
111 void lu_ref_print_all(void)
115 spin_lock(&lu_ref_refs_guard);
116 list_for_each_entry(ref, &lu_ref_refs, lf_linkage) {
117 if (lu_ref_is_marker(ref))
120 spin_lock(&ref->lf_guard);
122 spin_unlock(&ref->lf_guard);
124 spin_unlock(&lu_ref_refs_guard);
127 void lu_ref_init_loc(struct lu_ref *ref, const char *func, const int line)
132 spin_lock_init(&ref->lf_guard);
133 INIT_LIST_HEAD(&ref->lf_list);
134 spin_lock(&lu_ref_refs_guard);
135 list_add(&ref->lf_linkage, &lu_ref_refs);
136 spin_unlock(&lu_ref_refs_guard);
139 void lu_ref_fini(struct lu_ref *ref)
141 REFASSERT(ref, list_empty(&ref->lf_list));
142 REFASSERT(ref, ref->lf_refs == 0);
143 spin_lock(&lu_ref_refs_guard);
144 list_del_init(&ref->lf_linkage);
145 spin_unlock(&lu_ref_refs_guard);
148 static struct lu_ref_link *lu_ref_add_context(struct lu_ref *ref,
153 struct lu_ref_link *link;
156 if (lu_ref_link_kmem != NULL) {
157 OBD_SLAB_ALLOC_PTR_GFP(link, lu_ref_link_kmem, flags);
160 link->ll_scope = scope;
161 link->ll_source = source;
162 spin_lock(&ref->lf_guard);
163 list_add_tail(&link->ll_linkage, &ref->lf_list);
165 spin_unlock(&ref->lf_guard);
170 spin_lock(&ref->lf_guard);
172 spin_unlock(&ref->lf_guard);
173 link = ERR_PTR(-ENOMEM);
179 void lu_ref_add(struct lu_ref *ref, const char *scope, const void *source)
182 lu_ref_add_context(ref, GFP_IOFS, scope, source);
185 void lu_ref_add_at(struct lu_ref *ref, struct lu_ref_link *link,
186 const char *scope, const void *source)
189 link->ll_scope = scope;
190 link->ll_source = source;
191 spin_lock(&ref->lf_guard);
192 list_add_tail(&link->ll_linkage, &ref->lf_list);
194 spin_unlock(&ref->lf_guard);
198 * Version of lu_ref_add() to be used in non-blockable contexts.
200 void lu_ref_add_atomic(struct lu_ref *ref, const char *scope,
203 lu_ref_add_context(ref, GFP_ATOMIC, scope, source);
206 static inline int lu_ref_link_eq(const struct lu_ref_link *link,
207 const char *scope, const void *source)
209 return link->ll_source == source && !strcmp(link->ll_scope, scope);
213 * Maximal chain length seen so far.
215 static unsigned lu_ref_chain_max_length = 127;
218 * Searches for a lu_ref_link with given [scope, source] within given lu_ref.
220 static struct lu_ref_link *lu_ref_find(struct lu_ref *ref, const char *scope,
223 struct lu_ref_link *link;
227 list_for_each_entry(link, &ref->lf_list, ll_linkage) {
229 if (lu_ref_link_eq(link, scope, source)) {
230 if (iterations > lu_ref_chain_max_length) {
231 CWARN("Long lu_ref chain %d \"%s\":%p\n",
232 iterations, scope, source);
233 lu_ref_chain_max_length = iterations * 3 / 2;
241 void lu_ref_del(struct lu_ref *ref, const char *scope, const void *source)
243 struct lu_ref_link *link;
245 spin_lock(&ref->lf_guard);
246 link = lu_ref_find(ref, scope, source);
248 list_del(&link->ll_linkage);
250 spin_unlock(&ref->lf_guard);
251 OBD_SLAB_FREE(link, lu_ref_link_kmem, sizeof(*link));
253 REFASSERT(ref, ref->lf_failed > 0);
255 spin_unlock(&ref->lf_guard);
259 void lu_ref_set_at(struct lu_ref *ref, struct lu_ref_link *link,
261 const void *source0, const void *source1)
263 REFASSERT(ref, link != NULL && !IS_ERR(link));
265 spin_lock(&ref->lf_guard);
266 REFASSERT(ref, link->ll_ref == ref);
267 REFASSERT(ref, lu_ref_link_eq(link, scope, source0));
268 link->ll_source = source1;
269 spin_unlock(&ref->lf_guard);
272 void lu_ref_del_at(struct lu_ref *ref, struct lu_ref_link *link,
273 const char *scope, const void *source)
275 REFASSERT(ref, link != NULL && !IS_ERR(link));
276 spin_lock(&ref->lf_guard);
277 REFASSERT(ref, link->ll_ref == ref);
278 REFASSERT(ref, lu_ref_link_eq(link, scope, source));
279 list_del(&link->ll_linkage);
281 spin_unlock(&ref->lf_guard);
284 #ifdef CONFIG_PROC_FS
286 static void *lu_ref_seq_start(struct seq_file *seq, loff_t *pos)
288 struct lu_ref *ref = seq->private;
290 spin_lock(&lu_ref_refs_guard);
291 if (list_empty(&ref->lf_linkage))
293 spin_unlock(&lu_ref_refs_guard);
298 static void *lu_ref_seq_next(struct seq_file *seq, void *p, loff_t *pos)
300 struct lu_ref *ref = p;
303 LASSERT(seq->private == p);
304 LASSERT(!list_empty(&ref->lf_linkage));
306 spin_lock(&lu_ref_refs_guard);
307 next = list_entry(ref->lf_linkage.next, struct lu_ref, lf_linkage);
308 if (&next->lf_linkage == &lu_ref_refs) {
312 list_move(&ref->lf_linkage, &next->lf_linkage);
314 spin_unlock(&lu_ref_refs_guard);
318 static void lu_ref_seq_stop(struct seq_file *seq, void *p)
324 static int lu_ref_seq_show(struct seq_file *seq, void *p)
326 struct lu_ref *ref = p;
329 spin_lock(&lu_ref_refs_guard);
330 next = list_entry(ref->lf_linkage.next, struct lu_ref, lf_linkage);
331 if ((&next->lf_linkage == &lu_ref_refs) || lu_ref_is_marker(next)) {
332 spin_unlock(&lu_ref_refs_guard);
336 /* print the entry */
337 spin_lock(&next->lf_guard);
338 seq_printf(seq, "lu_ref: %p %d %d %s:%d\n",
339 next, next->lf_refs, next->lf_failed,
340 next->lf_func, next->lf_line);
341 if (next->lf_refs > 64) {
342 seq_printf(seq, " too many references, skip\n");
344 struct lu_ref_link *link;
347 list_for_each_entry(link, &next->lf_list, ll_linkage)
348 seq_printf(seq, " #%d link: %s %p\n",
349 i++, link->ll_scope, link->ll_source);
351 spin_unlock(&next->lf_guard);
352 spin_unlock(&lu_ref_refs_guard);
357 static struct seq_operations lu_ref_seq_ops = {
358 .start = lu_ref_seq_start,
359 .stop = lu_ref_seq_stop,
360 .next = lu_ref_seq_next,
361 .show = lu_ref_seq_show
364 static int lu_ref_seq_open(struct inode *inode, struct file *file)
366 struct lu_ref *marker = &lu_ref_marker;
369 result = seq_open(file, &lu_ref_seq_ops);
371 spin_lock(&lu_ref_refs_guard);
372 if (!list_empty(&marker->lf_linkage))
375 list_add(&marker->lf_linkage, &lu_ref_refs);
376 spin_unlock(&lu_ref_refs_guard);
379 struct seq_file *f = file->private_data;
382 seq_release(inode, file);
389 static int lu_ref_seq_release(struct inode *inode, struct file *file)
391 struct lu_ref *ref = ((struct seq_file *)file->private_data)->private;
393 spin_lock(&lu_ref_refs_guard);
394 list_del_init(&ref->lf_linkage);
395 spin_unlock(&lu_ref_refs_guard);
397 return seq_release(inode, file);
400 static struct file_operations lu_ref_dump_fops = {
401 .owner = THIS_MODULE,
402 .open = lu_ref_seq_open,
405 .release = lu_ref_seq_release
408 #endif /* CONFIG_PROC_FS */
410 int lu_ref_global_init(void)
415 "lu_ref tracking is enabled. Performance isn't.\n");
417 INIT_LIST_HEAD(&lu_ref_refs);
418 spin_lock_init(&lu_ref_refs_guard);
419 result = lu_kmem_init(lu_ref_caches);
421 #ifdef CONFIG_PROC_FS
423 result = lprocfs_seq_create(proc_lustre_root, "lu_refs",
424 0444, &lu_ref_dump_fops, NULL);
426 lu_kmem_fini(lu_ref_caches);
428 #endif /* CONFIG_PROC_FS */
433 void lu_ref_global_fini(void)
435 #ifdef CONFIG_PROC_FS
436 lprocfs_remove_proc_entry("lu_refs", proc_lustre_root);
437 #endif /* CONFIG_PROC_FS */
438 lu_kmem_fini(lu_ref_caches);
441 #endif /* USE_LU_REF */