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