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