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