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