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