Whamcloud - gitweb
LU-1346 libcfs: replace libcfs wrappers with kernel API
[fs/lustre-release.git] / lustre / obdclass / lu_ref.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/obdclass/lu_ref.c
35  *
36  * Lustre reference.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42
43 #ifdef __KERNEL__
44 # include <libcfs/libcfs.h>
45 #else
46 # include <liblustre.h>
47 #endif
48
49 #include <obd.h>
50 #include <obd_class.h>
51 #include <obd_support.h>
52 #include <lu_ref.h>
53
54 #ifdef USE_LU_REF
55
56 /**
57  * Asserts a condition for a given lu_ref. Must be called with
58  * lu_ref::lf_guard held.
59  */
60 #define REFASSERT(ref, expr) do {                                       \
61         struct lu_ref *__tmp = (ref);                                   \
62                                                                         \
63         if (unlikely(!(expr))) {                                        \
64                 lu_ref_print(__tmp);                                    \
65                 spin_unlock(&__tmp->lf_guard);                          \
66                 lu_ref_print_all();                                     \
67                 LASSERT(0);                                             \
68                 spin_lock(&__tmp->lf_guard);                            \
69         }                                                               \
70 } while (0)
71
72 struct lu_ref_link {
73         struct lu_ref    *ll_ref;
74         cfs_list_t        ll_linkage;
75         const char       *ll_scope;
76         const void       *ll_source;
77 };
78
79 static cfs_mem_cache_t *lu_ref_link_kmem;
80
81 static struct lu_kmem_descr lu_ref_caches[] = {
82         {
83                 .ckd_cache = &lu_ref_link_kmem,
84                 .ckd_name  = "lu_ref_link_kmem",
85                 .ckd_size  = sizeof (struct lu_ref_link)
86         },
87         {
88                 .ckd_cache = NULL
89         }
90 };
91
92 /**
93  * Global list of active (initialized, but not finalized) lu_ref's.
94  *
95  * Protected by lu_ref_refs_guard.
96  */
97 static CFS_LIST_HEAD(lu_ref_refs);
98 static spinlock_t lu_ref_refs_guard;
99 static struct lu_ref lu_ref_marker = {
100         .lf_guard   = DEFINE_SPINLOCK(lu_ref_marker.lf_guard),
101         .lf_list    = CFS_LIST_HEAD_INIT(lu_ref_marker.lf_list),
102         .lf_linkage = CFS_LIST_HEAD_INIT(lu_ref_marker.lf_linkage)
103 };
104
105 void lu_ref_print(const struct lu_ref *ref)
106 {
107         struct lu_ref_link *link;
108
109         CERROR("lu_ref: %p %d %d %s:%d\n",
110                ref, ref->lf_refs, ref->lf_failed, ref->lf_func, ref->lf_line);
111         cfs_list_for_each_entry(link, &ref->lf_list, ll_linkage) {
112                 CERROR("     link: %s %p\n", link->ll_scope, link->ll_source);
113         }
114 }
115 EXPORT_SYMBOL(lu_ref_print);
116
117 static int lu_ref_is_marker(const struct lu_ref *ref)
118 {
119         return (ref == &lu_ref_marker);
120 }
121
122 void lu_ref_print_all(void)
123 {
124         struct lu_ref *ref;
125
126         spin_lock(&lu_ref_refs_guard);
127         cfs_list_for_each_entry(ref, &lu_ref_refs, lf_linkage) {
128                 if (lu_ref_is_marker(ref))
129                         continue;
130
131                 spin_lock(&ref->lf_guard);
132                 lu_ref_print(ref);
133                 spin_unlock(&ref->lf_guard);
134         }
135         spin_unlock(&lu_ref_refs_guard);
136 }
137 EXPORT_SYMBOL(lu_ref_print_all);
138
139 void lu_ref_init_loc(struct lu_ref *ref, const char *func, const int line)
140 {
141         ref->lf_refs = 0;
142         ref->lf_func = func;
143         ref->lf_line = line;
144         spin_lock_init(&ref->lf_guard);
145         CFS_INIT_LIST_HEAD(&ref->lf_list);
146         spin_lock(&lu_ref_refs_guard);
147         cfs_list_add(&ref->lf_linkage, &lu_ref_refs);
148         spin_unlock(&lu_ref_refs_guard);
149 }
150 EXPORT_SYMBOL(lu_ref_init_loc);
151
152 void lu_ref_fini(struct lu_ref *ref)
153 {
154         REFASSERT(ref, cfs_list_empty(&ref->lf_list));
155         REFASSERT(ref, ref->lf_refs == 0);
156         spin_lock(&lu_ref_refs_guard);
157         cfs_list_del_init(&ref->lf_linkage);
158         spin_unlock(&lu_ref_refs_guard);
159 }
160 EXPORT_SYMBOL(lu_ref_fini);
161
162 static struct lu_ref_link *lu_ref_add_context(struct lu_ref *ref,
163                                               enum cfs_alloc_flags flags,
164                                               const char *scope,
165                                               const void *source)
166 {
167         struct lu_ref_link *link;
168
169         link = NULL;
170         if (lu_ref_link_kmem != NULL) {
171                 OBD_SLAB_ALLOC_PTR_GFP(link, lu_ref_link_kmem, flags);
172                 if (link != NULL) {
173                         link->ll_ref    = ref;
174                         link->ll_scope  = scope;
175                         link->ll_source = source;
176                         spin_lock(&ref->lf_guard);
177                         cfs_list_add_tail(&link->ll_linkage, &ref->lf_list);
178                         ref->lf_refs++;
179                         spin_unlock(&ref->lf_guard);
180                 }
181         }
182
183         if (link == NULL) {
184                 spin_lock(&ref->lf_guard);
185                 ref->lf_failed++;
186                 spin_unlock(&ref->lf_guard);
187                 link = ERR_PTR(-ENOMEM);
188         }
189
190         return link;
191 }
192
193 struct lu_ref_link *lu_ref_add(struct lu_ref *ref, const char *scope,
194                                const void *source)
195 {
196         cfs_might_sleep();
197         return lu_ref_add_context(ref, CFS_ALLOC_STD, scope, source);
198 }
199 EXPORT_SYMBOL(lu_ref_add);
200
201 /**
202  * Version of lu_ref_add() to be used in non-blockable contexts.
203  */
204 struct lu_ref_link *lu_ref_add_atomic(struct lu_ref *ref, const char *scope,
205                                       const void *source)
206 {
207         return lu_ref_add_context(ref, CFS_ALLOC_ATOMIC, scope, source);
208 }
209 EXPORT_SYMBOL(lu_ref_add_atomic);
210
211 static inline int lu_ref_link_eq(const struct lu_ref_link *link,
212                                  const char *scope, const void *source)
213 {
214         return link->ll_source == source && !strcmp(link->ll_scope, scope);
215 }
216
217 /**
218  * Maximal chain length seen so far.
219  */
220 static unsigned lu_ref_chain_max_length = 127;
221
222 /**
223  * Searches for a lu_ref_link with given [scope, source] within given lu_ref.
224  */
225 static struct lu_ref_link *lu_ref_find(struct lu_ref *ref, const char *scope,
226                                        const void *source)
227 {
228         struct lu_ref_link *link;
229         unsigned            iterations;
230
231         iterations = 0;
232         cfs_list_for_each_entry(link, &ref->lf_list, ll_linkage) {
233                 ++iterations;
234                 if (lu_ref_link_eq(link, scope, source)) {
235                         if (iterations > lu_ref_chain_max_length) {
236                                 CWARN("Long lu_ref chain %d \"%s\":%p\n",
237                                       iterations, scope, source);
238                                 lu_ref_chain_max_length = iterations * 3 / 2;
239                         }
240                         return link;
241                 }
242         }
243         return NULL;
244 }
245
246 void lu_ref_del(struct lu_ref *ref, const char *scope, const void *source)
247 {
248         struct lu_ref_link *link;
249
250         spin_lock(&ref->lf_guard);
251         link = lu_ref_find(ref, scope, source);
252         if (link != NULL) {
253                 cfs_list_del(&link->ll_linkage);
254                 ref->lf_refs--;
255                 spin_unlock(&ref->lf_guard);
256                 OBD_SLAB_FREE(link, lu_ref_link_kmem, sizeof(*link));
257         } else {
258                 REFASSERT(ref, ref->lf_failed > 0);
259                 ref->lf_failed--;
260                 spin_unlock(&ref->lf_guard);
261         }
262 }
263 EXPORT_SYMBOL(lu_ref_del);
264
265 void lu_ref_set_at(struct lu_ref *ref, struct lu_ref_link *link,
266                    const char *scope,
267                    const void *source0, const void *source1)
268 {
269         spin_lock(&ref->lf_guard);
270         if (link != ERR_PTR(-ENOMEM)) {
271                 REFASSERT(ref, link->ll_ref == ref);
272                 REFASSERT(ref, lu_ref_link_eq(link, scope, source0));
273                 link->ll_source = source1;
274         } else {
275                 REFASSERT(ref, ref->lf_failed > 0);
276         }
277         spin_unlock(&ref->lf_guard);
278 }
279 EXPORT_SYMBOL(lu_ref_set_at);
280
281 void lu_ref_del_at(struct lu_ref *ref, struct lu_ref_link *link,
282                    const char *scope, const void *source)
283 {
284         if (link != ERR_PTR(-ENOMEM)) {
285                 spin_lock(&ref->lf_guard);
286                 REFASSERT(ref, link->ll_ref == ref);
287                 REFASSERT(ref, lu_ref_link_eq(link, scope, source));
288                 cfs_list_del(&link->ll_linkage);
289                 ref->lf_refs--;
290                 spin_unlock(&ref->lf_guard);
291                 OBD_SLAB_FREE(link, lu_ref_link_kmem, sizeof(*link));
292         } else {
293                 spin_lock(&ref->lf_guard);
294                 REFASSERT(ref, ref->lf_failed > 0);
295                 ref->lf_failed--;
296                 spin_unlock(&ref->lf_guard);
297         }
298 }
299 EXPORT_SYMBOL(lu_ref_del_at);
300
301 #if defined(__KERNEL__) && defined(LPROCFS)
302
303 static void *lu_ref_seq_start(struct seq_file *seq, loff_t *pos)
304 {
305         struct lu_ref *ref = seq->private;
306
307         spin_lock(&lu_ref_refs_guard);
308         if (cfs_list_empty(&ref->lf_linkage))
309                 ref = NULL;
310         spin_unlock(&lu_ref_refs_guard);
311
312         return ref;
313 }
314
315 static void *lu_ref_seq_next(struct seq_file *seq, void *p, loff_t *pos)
316 {
317         struct lu_ref *ref = p;
318         struct lu_ref *next;
319
320         LASSERT(seq->private == p);
321         LASSERT(!cfs_list_empty(&ref->lf_linkage));
322
323         spin_lock(&lu_ref_refs_guard);
324         next = cfs_list_entry(ref->lf_linkage.next, struct lu_ref, lf_linkage);
325         if (&next->lf_linkage == &lu_ref_refs) {
326                 p = NULL;
327         } else {
328                 (*pos)++;
329                 cfs_list_move(&ref->lf_linkage, &next->lf_linkage);
330         }
331         spin_unlock(&lu_ref_refs_guard);
332         return p;
333 }
334
335 static void lu_ref_seq_stop(struct seq_file *seq, void *p)
336 {
337         /* Nothing to do */
338 }
339
340
341 static int lu_ref_seq_show(struct seq_file *seq, void *p)
342 {
343         struct lu_ref *ref  = p;
344         struct lu_ref *next;
345
346         spin_lock(&lu_ref_refs_guard);
347         next = cfs_list_entry(ref->lf_linkage.next, struct lu_ref, lf_linkage);
348         if ((&next->lf_linkage == &lu_ref_refs) || lu_ref_is_marker(next)) {
349                 spin_unlock(&lu_ref_refs_guard);
350                 return 0;
351         }
352
353         /* print the entry */
354         spin_lock(&next->lf_guard);
355         seq_printf(seq, "lu_ref: %p %d %d %s:%d\n",
356                    next, next->lf_refs, next->lf_failed,
357                    next->lf_func, next->lf_line);
358         if (next->lf_refs > 64) {
359                 seq_printf(seq, "  too many references, skip\n");
360         } else {
361                 struct lu_ref_link *link;
362                 int i = 0;
363
364                 cfs_list_for_each_entry(link, &next->lf_list, ll_linkage)
365                         seq_printf(seq, "  #%d link: %s %p\n",
366                                    i++, link->ll_scope, link->ll_source);
367         }
368         spin_unlock(&next->lf_guard);
369         spin_unlock(&lu_ref_refs_guard);
370
371         return 0;
372 }
373
374 static struct seq_operations lu_ref_seq_ops = {
375         .start = lu_ref_seq_start,
376         .stop  = lu_ref_seq_stop,
377         .next  = lu_ref_seq_next,
378         .show  = lu_ref_seq_show
379 };
380
381 static int lu_ref_seq_open(struct inode *inode, struct file *file)
382 {
383         struct lu_ref *marker = &lu_ref_marker;
384         int result = 0;
385
386         result = seq_open(file, &lu_ref_seq_ops);
387         if (result == 0) {
388                 spin_lock(&lu_ref_refs_guard);
389                 if (!cfs_list_empty(&marker->lf_linkage))
390                         result = -EAGAIN;
391                 else
392                         cfs_list_add(&marker->lf_linkage, &lu_ref_refs);
393                 spin_unlock(&lu_ref_refs_guard);
394
395                 if (result == 0) {
396                         struct seq_file *f = file->private_data;
397                         f->private = marker;
398                 } else {
399                         seq_release(inode, file);
400                 }
401         }
402
403         return result;
404 }
405
406 static int lu_ref_seq_release(struct inode *inode, struct file *file)
407 {
408         struct lu_ref *ref = ((struct seq_file *)file->private_data)->private;
409
410         spin_lock(&lu_ref_refs_guard);
411         cfs_list_del_init(&ref->lf_linkage);
412         spin_unlock(&lu_ref_refs_guard);
413
414         return seq_release(inode, file);
415 }
416
417 static struct file_operations lu_ref_dump_fops = {
418         .owner   = THIS_MODULE,
419         .open    = lu_ref_seq_open,
420         .read    = seq_read,
421         .llseek  = seq_lseek,
422         .release = lu_ref_seq_release
423 };
424
425 #endif
426
427 int lu_ref_global_init(void)
428 {
429         int result;
430
431         CDEBUG(D_CONSOLE,
432                "lu_ref tracking is enabled. Performance isn't.\n");
433
434         spin_lock_init(&lu_ref_refs_guard);
435         result = lu_kmem_init(lu_ref_caches);
436
437 #if defined(__KERNEL__) && defined(LPROCFS)
438         if (result == 0) {
439                 result = lprocfs_seq_create(proc_lustre_root, "lu_refs",
440                                             0444, &lu_ref_dump_fops, NULL);
441                 if (result)
442                         lu_kmem_fini(lu_ref_caches);
443         }
444 #endif
445
446         return result;
447 }
448
449 void lu_ref_global_fini(void)
450 {
451 #if defined(__KERNEL__) && defined(LPROCFS)
452         lprocfs_remove_proc_entry("lu_refs", proc_lustre_root);
453 #endif
454         lu_kmem_fini(lu_ref_caches);
455 }
456
457 #endif /* USE_LU_REF */