Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[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)                            \
61   do {                                                  \
62           struct lu_ref *__ref = (ref);                 \
63                                                         \
64           if (unlikely(!(expr))) {                      \
65                   lu_ref_print(__ref);                  \
66                   cfs_spin_unlock(&__ref->lf_guard);    \
67                   lu_ref_print_all();                   \
68                   LASSERT(0);                           \
69                   cfs_spin_lock(&__ref->lf_guard);      \
70           }                                             \
71   } while (0)
72
73 struct lu_ref_link {
74         struct lu_ref    *ll_ref;
75         cfs_list_t        ll_linkage;
76         const char       *ll_scope;
77         const void       *ll_source;
78 };
79
80 static cfs_mem_cache_t *lu_ref_link_kmem;
81
82 static struct lu_kmem_descr lu_ref_caches[] = {
83         {
84                 .ckd_cache = &lu_ref_link_kmem,
85                 .ckd_name  = "lu_ref_link_kmem",
86                 .ckd_size  = sizeof (struct lu_ref_link)
87         },
88         {
89                 .ckd_cache = NULL
90         }
91 };
92
93 /**
94  * Global list of active (initialized, but not finalized) lu_ref's.
95  *
96  * Protected by lu_ref_refs_guard.
97  */
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)
104 };
105
106 void lu_ref_print(const struct lu_ref *ref)
107 {
108         struct lu_ref_link *link;
109
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);
114         }
115 }
116 EXPORT_SYMBOL(lu_ref_print);
117
118 static int lu_ref_is_marker(const struct lu_ref *ref)
119 {
120         return (ref == &lu_ref_marker);
121 }
122
123 void lu_ref_print_all(void)
124 {
125         struct lu_ref *ref;
126
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))
130                         continue;
131
132                 cfs_spin_lock(&ref->lf_guard);
133                 lu_ref_print(ref);
134                 cfs_spin_unlock(&ref->lf_guard);
135         }
136         cfs_spin_unlock(&lu_ref_refs_guard);
137 }
138 EXPORT_SYMBOL(lu_ref_print_all);
139
140 void lu_ref_init_loc(struct lu_ref *ref, const char *func, const int line)
141 {
142         ref->lf_refs = 0;
143         ref->lf_func = func;
144         ref->lf_line = 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);
150 }
151 EXPORT_SYMBOL(lu_ref_init_loc);
152
153 void lu_ref_fini(struct lu_ref *ref)
154 {
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);
160 }
161 EXPORT_SYMBOL(lu_ref_fini);
162
163 static struct lu_ref_link *lu_ref_add_context(struct lu_ref *ref,
164                                               enum cfs_alloc_flags flags,
165                                               const char *scope,
166                                               const void *source)
167 {
168         struct lu_ref_link *link;
169
170         link = NULL;
171         if (lu_ref_link_kmem != NULL) {
172                 OBD_SLAB_ALLOC_PTR_GFP(link, lu_ref_link_kmem, flags);
173                 if (link != NULL) {
174                         link->ll_ref    = ref;
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);
179                         ref->lf_refs++;
180                         cfs_spin_unlock(&ref->lf_guard);
181                 }
182         }
183
184         if (link == NULL) {
185                 cfs_spin_lock(&ref->lf_guard);
186                 ref->lf_failed++;
187                 cfs_spin_unlock(&ref->lf_guard);
188                 link = ERR_PTR(-ENOMEM);
189         }
190
191         return link;
192 }
193
194 struct lu_ref_link *lu_ref_add(struct lu_ref *ref, const char *scope,
195                                const void *source)
196 {
197         cfs_might_sleep();
198         return lu_ref_add_context(ref, CFS_ALLOC_STD, scope, source);
199 }
200 EXPORT_SYMBOL(lu_ref_add);
201
202 /**
203  * Version of lu_ref_add() to be used in non-blockable contexts.
204  */
205 struct lu_ref_link *lu_ref_add_atomic(struct lu_ref *ref, const char *scope,
206                                       const void *source)
207 {
208         return lu_ref_add_context(ref, CFS_ALLOC_ATOMIC, scope, source);
209 }
210 EXPORT_SYMBOL(lu_ref_add_atomic);
211
212 static inline int lu_ref_link_eq(const struct lu_ref_link *link,
213                                  const char *scope, const void *source)
214 {
215         return link->ll_source == source && !strcmp(link->ll_scope, scope);
216 }
217
218 /**
219  * Maximal chain length seen so far.
220  */
221 static unsigned lu_ref_chain_max_length = 127;
222
223 /**
224  * Searches for a lu_ref_link with given [scope, source] within given lu_ref.
225  */
226 static struct lu_ref_link *lu_ref_find(struct lu_ref *ref, const char *scope,
227                                        const void *source)
228 {
229         struct lu_ref_link *link;
230         unsigned            iterations;
231
232         iterations = 0;
233         cfs_list_for_each_entry(link, &ref->lf_list, ll_linkage) {
234                 ++iterations;
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;
240                         }
241                         return link;
242                 }
243         }
244         return NULL;
245 }
246
247 void lu_ref_del(struct lu_ref *ref, const char *scope, const void *source)
248 {
249         struct lu_ref_link *link;
250
251         cfs_spin_lock(&ref->lf_guard);
252         link = lu_ref_find(ref, scope, source);
253         if (link != NULL) {
254                 cfs_list_del(&link->ll_linkage);
255                 ref->lf_refs--;
256                 cfs_spin_unlock(&ref->lf_guard);
257                 OBD_SLAB_FREE(link, lu_ref_link_kmem, sizeof(*link));
258         } else {
259                 REFASSERT(ref, ref->lf_failed > 0);
260                 ref->lf_failed--;
261                 cfs_spin_unlock(&ref->lf_guard);
262         }
263 }
264 EXPORT_SYMBOL(lu_ref_del);
265
266 void lu_ref_set_at(struct lu_ref *ref, struct lu_ref_link *link,
267                    const char *scope,
268                    const void *source0, const void *source1)
269 {
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;
275         } else {
276                 REFASSERT(ref, ref->lf_failed > 0);
277         }
278         cfs_spin_unlock(&ref->lf_guard);
279 }
280 EXPORT_SYMBOL(lu_ref_set_at);
281
282 void lu_ref_del_at(struct lu_ref *ref, struct lu_ref_link *link,
283                    const char *scope, const void *source)
284 {
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);
290                 ref->lf_refs--;
291                 cfs_spin_unlock(&ref->lf_guard);
292                 OBD_SLAB_FREE(link, lu_ref_link_kmem, sizeof(*link));
293         } else {
294                 cfs_spin_lock(&ref->lf_guard);
295                 REFASSERT(ref, ref->lf_failed > 0);
296                 ref->lf_failed--;
297                 cfs_spin_unlock(&ref->lf_guard);
298         }
299 }
300 EXPORT_SYMBOL(lu_ref_del_at);
301
302 #if defined(__KERNEL__) && defined(LPROCFS)
303
304 static void *lu_ref_seq_start(struct seq_file *seq, loff_t *pos)
305 {
306         struct lu_ref *ref = seq->private;
307
308         cfs_spin_lock(&lu_ref_refs_guard);
309         if (cfs_list_empty(&ref->lf_linkage))
310                 ref = NULL;
311         cfs_spin_unlock(&lu_ref_refs_guard);
312
313         return ref;
314 }
315
316 static void *lu_ref_seq_next(struct seq_file *seq, void *p, loff_t *pos)
317 {
318         struct lu_ref *ref = p;
319         struct lu_ref *next;
320
321         LASSERT(seq->private == p);
322         LASSERT(!cfs_list_empty(&ref->lf_linkage));
323
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) {
327                 p = NULL;
328         } else {
329                 (*pos)++;
330                 cfs_list_move(&ref->lf_linkage, &next->lf_linkage);
331         }
332         cfs_spin_unlock(&lu_ref_refs_guard);
333         return p;
334 }
335
336 static void lu_ref_seq_stop(struct seq_file *seq, void *p)
337 {
338         /* Nothing to do */
339 }
340
341
342 static int lu_ref_seq_show(struct seq_file *seq, void *p)
343 {
344         struct lu_ref *ref  = p;
345         struct lu_ref *next; 
346
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);
351                 return 0;
352         }
353
354         /* print the entry */
355
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");
362         } else {
363                 struct lu_ref_link *link;
364                 int i = 0;
365
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);
369         }
370         cfs_spin_unlock(&next->lf_guard);
371         cfs_spin_unlock(&lu_ref_refs_guard);
372
373         return 0;
374 }
375
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
381 };
382
383 static int lu_ref_seq_open(struct inode *inode, struct file *file)
384 {
385         struct lu_ref *marker = &lu_ref_marker;
386         int result = 0;
387
388         result = seq_open(file, &lu_ref_seq_ops);
389         if (result == 0) {
390                 cfs_spin_lock(&lu_ref_refs_guard);
391                 if (!cfs_list_empty(&marker->lf_linkage))
392                         result = -EAGAIN;
393                 else
394                         cfs_list_add(&marker->lf_linkage, &lu_ref_refs);
395                 cfs_spin_unlock(&lu_ref_refs_guard);
396
397                 if (result == 0) {
398                         struct seq_file *f = file->private_data;
399                         f->private = marker;
400                 } else {
401                         seq_release(inode, file);
402                 }
403         }
404
405         return result;
406 }
407
408 static int lu_ref_seq_release(struct inode *inode, struct file *file)
409 {
410         struct lu_ref *ref = ((struct seq_file *)file->private_data)->private;
411
412         cfs_spin_lock(&lu_ref_refs_guard);
413         cfs_list_del_init(&ref->lf_linkage);
414         cfs_spin_unlock(&lu_ref_refs_guard);
415
416         return seq_release(inode, file);
417 }
418
419 static struct file_operations lu_ref_dump_fops = {
420         .owner   = THIS_MODULE,
421         .open    = lu_ref_seq_open,
422         .read    = seq_read,
423         .llseek  = seq_lseek,
424         .release = lu_ref_seq_release
425 };
426
427 #endif
428
429 int lu_ref_global_init(void)
430 {
431         int result;
432
433         CDEBUG(D_CONSOLE,
434                "lu_ref tracking is enabled. Performance isn't.\n");
435
436
437         cfs_spin_lock_init(&lu_ref_refs_guard);
438         result = lu_kmem_init(lu_ref_caches);
439
440 #if defined(__KERNEL__) && defined(LPROCFS)
441         if (result == 0) {
442                 result = lprocfs_seq_create(proc_lustre_root, "lu_refs",
443                                             0444, &lu_ref_dump_fops, NULL);
444                 if (result)
445                         lu_kmem_fini(lu_ref_caches);
446         }
447 #endif
448
449         return result;
450 }
451
452 void lu_ref_global_fini(void)
453 {
454 #if defined(__KERNEL__) && defined(LPROCFS)
455         lprocfs_remove_proc_entry("lu_refs", proc_lustre_root);
456 #endif
457         lu_kmem_fini(lu_ref_caches);
458 }
459
460 #endif /* USE_LU_REF */