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