Whamcloud - gitweb
LU-506 kernel: FC15 - reiserfs remove & cleanup
[fs/lustre-release.git] / libcfs / libcfs / tracefile.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/tracefile.c
37  *
38  * Author: Zach Brown <zab@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  */
41
42
43 #define DEBUG_SUBSYSTEM S_LNET
44 #define LUSTRE_TRACEFILE_PRIVATE
45 #include "tracefile.h"
46
47 #include <libcfs/libcfs.h>
48
49 /* XXX move things up to the top, comment */
50 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[CFS_NR_CPUS] __cacheline_aligned;
51
52 char cfs_tracefile[TRACEFILE_NAME_SIZE];
53 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
54 static struct tracefiled_ctl trace_tctl;
55 cfs_semaphore_t cfs_trace_thread_sem;
56 static int thread_running = 0;
57
58 cfs_atomic_t cfs_tage_allocated = CFS_ATOMIC_INIT(0);
59
60 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
61                                          struct cfs_trace_cpu_data *tcd);
62
63 static inline struct cfs_trace_page *
64 cfs_tage_from_list(cfs_list_t *list)
65 {
66         return cfs_list_entry(list, struct cfs_trace_page, linkage);
67 }
68
69 static struct cfs_trace_page *cfs_tage_alloc(int gfp)
70 {
71         cfs_page_t            *page;
72         struct cfs_trace_page *tage;
73
74         /* My caller is trying to free memory */
75         if (!cfs_in_interrupt() && cfs_memory_pressure_get())
76                 return NULL;
77
78         /*
79          * Don't spam console with allocation failures: they will be reported
80          * by upper layer anyway.
81          */
82         gfp |= CFS_ALLOC_NOWARN;
83         page = cfs_alloc_page(gfp);
84         if (page == NULL)
85                 return NULL;
86
87         tage = cfs_alloc(sizeof(*tage), gfp);
88         if (tage == NULL) {
89                 cfs_free_page(page);
90                 return NULL;
91         }
92
93         tage->page = page;
94         cfs_atomic_inc(&cfs_tage_allocated);
95         return tage;
96 }
97
98 static void cfs_tage_free(struct cfs_trace_page *tage)
99 {
100         __LASSERT(tage != NULL);
101         __LASSERT(tage->page != NULL);
102
103         cfs_free_page(tage->page);
104         cfs_free(tage);
105         cfs_atomic_dec(&cfs_tage_allocated);
106 }
107
108 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
109                              cfs_list_t *queue)
110 {
111         __LASSERT(tage != NULL);
112         __LASSERT(queue != NULL);
113
114         cfs_list_move_tail(&tage->linkage, queue);
115 }
116
117 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, int gfp,
118                            cfs_list_t *stock)
119 {
120         int i;
121
122         /*
123          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
124          * from here: this will lead to infinite recursion.
125          */
126
127         for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
128                 struct cfs_trace_page *tage;
129
130                 tage = cfs_tage_alloc(gfp);
131                 if (tage == NULL)
132                         break;
133                 cfs_list_add_tail(&tage->linkage, stock);
134         }
135         return i;
136 }
137
138 /* return a page that has 'len' bytes left at the end */
139 static struct cfs_trace_page *
140 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
141 {
142         struct cfs_trace_page *tage;
143
144         if (tcd->tcd_cur_pages > 0) {
145                 __LASSERT(!cfs_list_empty(&tcd->tcd_pages));
146                 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
147                 if (tage->used + len <= CFS_PAGE_SIZE)
148                         return tage;
149         }
150
151         if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
152                 if (tcd->tcd_cur_stock_pages > 0) {
153                         tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
154                         -- tcd->tcd_cur_stock_pages;
155                         cfs_list_del_init(&tage->linkage);
156                 } else {
157                         tage = cfs_tage_alloc(CFS_ALLOC_ATOMIC);
158                         if (tage == NULL) {
159                                 if (printk_ratelimit())
160                                         printk(CFS_KERN_WARNING
161                                                "cannot allocate a tage (%ld)\n",
162                                        tcd->tcd_cur_pages);
163                                 return NULL;
164                         }
165                 }
166
167                 tage->used = 0;
168                 tage->cpu = cfs_smp_processor_id();
169                 tage->type = tcd->tcd_type;
170                 cfs_list_add_tail(&tage->linkage, &tcd->tcd_pages);
171                 tcd->tcd_cur_pages++;
172
173                 if (tcd->tcd_cur_pages > 8 && thread_running) {
174                         struct tracefiled_ctl *tctl = &trace_tctl;
175                         /*
176                          * wake up tracefiled to process some pages.
177                          */
178                         cfs_waitq_signal(&tctl->tctl_waitq);
179                 }
180                 return tage;
181         }
182         return NULL;
183 }
184
185 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
186 {
187         int pgcount = tcd->tcd_cur_pages / 10;
188         struct page_collection pc;
189         struct cfs_trace_page *tage;
190         struct cfs_trace_page *tmp;
191
192         /*
193          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
194          * from here: this will lead to infinite recursion.
195          */
196
197         if (printk_ratelimit())
198                 printk(CFS_KERN_WARNING "debug daemon buffer overflowed; "
199                        "discarding 10%% of pages (%d of %ld)\n",
200                        pgcount + 1, tcd->tcd_cur_pages);
201
202         CFS_INIT_LIST_HEAD(&pc.pc_pages);
203         cfs_spin_lock_init(&pc.pc_lock);
204
205         cfs_list_for_each_entry_safe_typed(tage, tmp, &tcd->tcd_pages,
206                                            struct cfs_trace_page, linkage) {
207                 if (pgcount-- == 0)
208                         break;
209
210                 cfs_list_move_tail(&tage->linkage, &pc.pc_pages);
211                 tcd->tcd_cur_pages--;
212         }
213         put_pages_on_tcd_daemon_list(&pc, tcd);
214 }
215
216 /* return a page that has 'len' bytes left at the end */
217 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
218                                                  unsigned long len)
219 {
220         struct cfs_trace_page *tage;
221
222         /*
223          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
224          * from here: this will lead to infinite recursion.
225          */
226
227         if (len > CFS_PAGE_SIZE) {
228                 printk(CFS_KERN_ERR
229                        "cowardly refusing to write %lu bytes in a page\n", len);
230                 return NULL;
231         }
232
233         tage = cfs_trace_get_tage_try(tcd, len);
234         if (tage != NULL)
235                 return tage;
236         if (thread_running)
237                 cfs_tcd_shrink(tcd);
238         if (tcd->tcd_cur_pages > 0) {
239                 tage = cfs_tage_from_list(tcd->tcd_pages.next);
240                 tage->used = 0;
241                 cfs_tage_to_tail(tage, &tcd->tcd_pages);
242         }
243         return tage;
244 }
245
246 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
247                        const char *file, const char *fn, const int line,
248                        const char *format1, va_list args,
249                        const char *format2, ...)
250 {
251         struct cfs_trace_cpu_data *tcd = NULL;
252         struct ptldebug_header     header = {0};
253         struct cfs_trace_page     *tage;
254         /* string_buf is used only if tcd != NULL, and is always set then */
255         char                      *string_buf = NULL;
256         char                      *debug_buf;
257         int                        known_size;
258         int                        needed = 85; /* average message length */
259         int                        max_nob;
260         va_list                    ap;
261         int                        depth;
262         int                        i;
263         int                        remain;
264
265         if (strchr(file, '/'))
266                 file = strrchr(file, '/') + 1;
267
268         tcd = cfs_trace_get_tcd();
269
270         /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
271          * pins us to a particular CPU.  This avoids an smp_processor_id()
272          * warning on Linux when debugging is enabled. */
273         cfs_set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
274
275         if (tcd == NULL)                /* arch may not log in IRQ context */
276                 goto console;
277
278         if (tcd->tcd_cur_pages == 0)
279                 header.ph_flags |= PH_FLAG_FIRST_RECORD;
280
281         if (tcd->tcd_shutting_down) {
282                 cfs_trace_put_tcd(tcd);
283                 tcd = NULL;
284                 goto console;
285         }
286
287         depth = __current_nesting_level();
288         known_size = strlen(file) + 1 + depth;
289         if (fn)
290                 known_size += strlen(fn) + 1;
291
292         if (libcfs_debug_binary)
293                 known_size += sizeof(header);
294
295         /*/
296          * '2' used because vsnprintf return real size required for output
297          * _without_ terminating NULL.
298          * if needed is to small for this format.
299          */
300         for (i = 0; i < 2; i++) {
301                 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
302                 if (tage == NULL) {
303                         if (needed + known_size > CFS_PAGE_SIZE)
304                                 mask |= D_ERROR;
305
306                         cfs_trace_put_tcd(tcd);
307                         tcd = NULL;
308                         goto console;
309                 }
310
311                 string_buf = (char *)cfs_page_address(tage->page) +
312                                         tage->used + known_size;
313
314                 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
315                 if (max_nob <= 0) {
316                         printk(CFS_KERN_EMERG "negative max_nob: %d\n",
317                                max_nob);
318                         mask |= D_ERROR;
319                         cfs_trace_put_tcd(tcd);
320                         tcd = NULL;
321                         goto console;
322                 }
323
324                 needed = 0;
325                 if (format1) {
326                         va_copy(ap, args);
327                         needed = vsnprintf(string_buf, max_nob, format1, ap);
328                         va_end(ap);
329                 }
330
331                 if (format2) {
332                         remain = max_nob - needed;
333                         if (remain < 0)
334                                 remain = 0;
335
336                         va_start(ap, format2);
337                         needed += vsnprintf(string_buf + needed, remain,
338                                             format2, ap);
339                         va_end(ap);
340                 }
341
342                 if (needed < max_nob) /* well. printing ok.. */
343                         break;
344         }
345
346         if (*(string_buf+needed-1) != '\n')
347                 printk(CFS_KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
348                        file, line, fn);
349
350         header.ph_len = known_size + needed;
351         debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
352
353         if (libcfs_debug_binary) {
354                 memcpy(debug_buf, &header, sizeof(header));
355                 tage->used += sizeof(header);
356                 debug_buf += sizeof(header);
357         }
358
359         /* indent message according to the nesting level */
360         while (depth-- > 0) {
361                 *(debug_buf++) = '.';
362                 ++ tage->used;
363         }
364
365         strcpy(debug_buf, file);
366         tage->used += strlen(file) + 1;
367         debug_buf += strlen(file) + 1;
368
369         if (fn) {
370                 strcpy(debug_buf, fn);
371                 tage->used += strlen(fn) + 1;
372                 debug_buf += strlen(fn) + 1;
373         }
374
375         __LASSERT(debug_buf == string_buf);
376
377         tage->used += needed;
378         __LASSERT (tage->used <= CFS_PAGE_SIZE);
379
380 console:
381         if ((mask & libcfs_printk) == 0) {
382                 /* no console output requested */
383                 if (tcd != NULL)
384                         cfs_trace_put_tcd(tcd);
385                 return 1;
386         }
387
388         if (cdls != NULL) {
389                 if (libcfs_console_ratelimit &&
390                     cdls->cdls_next != 0 &&     /* not first time ever */
391                     !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
392                         /* skipping a console message */
393                         cdls->cdls_count++;
394                         if (tcd != NULL)
395                                 cfs_trace_put_tcd(tcd);
396                         return 1;
397                 }
398
399                 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
400                                                        libcfs_console_max_delay
401                                                        + cfs_time_seconds(10))) {
402                         /* last timeout was a long time ago */
403                         cdls->cdls_delay /= libcfs_console_backoff * 4;
404                 } else {
405                         cdls->cdls_delay *= libcfs_console_backoff;
406
407                         if (cdls->cdls_delay < libcfs_console_min_delay)
408                                 cdls->cdls_delay = libcfs_console_min_delay;
409                         else if (cdls->cdls_delay > libcfs_console_max_delay)
410                                 cdls->cdls_delay = libcfs_console_max_delay;
411                 }
412
413                 /* ensure cdls_next is never zero after it's been seen */
414                 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
415         }
416
417         if (tcd != NULL) {
418                 cfs_print_to_console(&header, mask, string_buf, needed, file,
419                                      fn);
420                 cfs_trace_put_tcd(tcd);
421         } else {
422                 string_buf = cfs_trace_get_console_buffer();
423
424                 needed = 0;
425                 if (format1 != NULL) {
426                         va_copy(ap, args);
427                         needed = vsnprintf(string_buf,
428                                            CFS_TRACE_CONSOLE_BUFFER_SIZE,
429                                            format1, ap);
430                         va_end(ap);
431                 }
432                 if (format2 != NULL) {
433                         remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
434                         if (remain > 0) {
435                                 va_start(ap, format2);
436                                 needed += vsnprintf(string_buf+needed, remain, format2, ap);
437                                 va_end(ap);
438                         }
439                 }
440                 cfs_print_to_console(&header, mask,
441                                      string_buf, needed, file, fn);
442
443                 cfs_trace_put_console_buffer(string_buf);
444         }
445
446         if (cdls != NULL && cdls->cdls_count != 0) {
447                 string_buf = cfs_trace_get_console_buffer();
448
449                 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
450                          "Skipped %d previous similar message%s\n",
451                          cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
452
453                 cfs_print_to_console(&header, mask,
454                                  string_buf, needed, file, fn);
455
456                 cfs_trace_put_console_buffer(string_buf);
457                 cdls->cdls_count = 0;
458         }
459
460         return 0;
461 }
462 EXPORT_SYMBOL(libcfs_debug_vmsg2);
463
464 void
465 libcfs_assertion_failed(const char *expr, const char *file,
466                         const char *func, const int line)
467 {
468         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
469                          "ASSERTION(%s) failed\n", expr);
470         /* cfs_enter_debugger(); */
471         lbug_with_loc(file, func, line);
472 }
473 EXPORT_SYMBOL(libcfs_assertion_failed);
474
475 void
476 cfs_trace_assertion_failed(const char *str,
477                            const char *fn, const char *file, int line)
478 {
479         struct ptldebug_header hdr;
480
481         libcfs_panic_in_progress = 1;
482         libcfs_catastrophe = 1;
483         cfs_mb();
484
485         cfs_set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
486                                 CDEBUG_STACK());
487
488         cfs_print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
489
490         LIBCFS_PANIC("Lustre debug assertion failure\n");
491
492         /* not reached */
493 }
494
495 static void
496 panic_collect_pages(struct page_collection *pc)
497 {
498         /* Do the collect_pages job on a single CPU: assumes that all other
499          * CPUs have been stopped during a panic.  If this isn't true for some
500          * arch, this will have to be implemented separately in each arch.  */
501         int                        i;
502         int                        j;
503         struct cfs_trace_cpu_data *tcd;
504
505         CFS_INIT_LIST_HEAD(&pc->pc_pages);
506
507         cfs_tcd_for_each(tcd, i, j) {
508                 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
509                 tcd->tcd_cur_pages = 0;
510
511                 if (pc->pc_want_daemon_pages) {
512                         cfs_list_splice_init(&tcd->tcd_daemon_pages,
513                                              &pc->pc_pages);
514                         tcd->tcd_cur_daemon_pages = 0;
515                 }
516         }
517 }
518
519 static void collect_pages_on_all_cpus(struct page_collection *pc)
520 {
521         struct cfs_trace_cpu_data *tcd;
522         int i, cpu;
523
524         cfs_spin_lock(&pc->pc_lock);
525         cfs_for_each_possible_cpu(cpu) {
526                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
527                         cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
528                         tcd->tcd_cur_pages = 0;
529                         if (pc->pc_want_daemon_pages) {
530                                 cfs_list_splice_init(&tcd->tcd_daemon_pages,
531                                                      &pc->pc_pages);
532                                 tcd->tcd_cur_daemon_pages = 0;
533                         }
534                 }
535         }
536         cfs_spin_unlock(&pc->pc_lock);
537 }
538
539 static void collect_pages(struct page_collection *pc)
540 {
541         CFS_INIT_LIST_HEAD(&pc->pc_pages);
542
543         if (libcfs_panic_in_progress)
544                 panic_collect_pages(pc);
545         else
546                 collect_pages_on_all_cpus(pc);
547 }
548
549 static void put_pages_back_on_all_cpus(struct page_collection *pc)
550 {
551         struct cfs_trace_cpu_data *tcd;
552         cfs_list_t *cur_head;
553         struct cfs_trace_page *tage;
554         struct cfs_trace_page *tmp;
555         int i, cpu;
556
557         cfs_spin_lock(&pc->pc_lock);
558         cfs_for_each_possible_cpu(cpu) {
559                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
560                         cur_head = tcd->tcd_pages.next;
561
562                         cfs_list_for_each_entry_safe_typed(tage, tmp,
563                                                            &pc->pc_pages,
564                                                            struct cfs_trace_page,
565                                                            linkage) {
566
567                                 __LASSERT_TAGE_INVARIANT(tage);
568
569                                 if (tage->cpu != cpu || tage->type != i)
570                                         continue;
571
572                                 cfs_tage_to_tail(tage, cur_head);
573                                 tcd->tcd_cur_pages++;
574                         }
575                 }
576         }
577         cfs_spin_unlock(&pc->pc_lock);
578 }
579
580 static void put_pages_back(struct page_collection *pc)
581 {
582         if (!libcfs_panic_in_progress)
583                 put_pages_back_on_all_cpus(pc);
584 }
585
586 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
587  * we have a good amount of data at all times for dumping during an LBUG, even
588  * if we have been steadily writing (and otherwise discarding) pages via the
589  * debug daemon. */
590 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
591                                          struct cfs_trace_cpu_data *tcd)
592 {
593         struct cfs_trace_page *tage;
594         struct cfs_trace_page *tmp;
595
596         cfs_spin_lock(&pc->pc_lock);
597         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
598                                            struct cfs_trace_page, linkage) {
599
600                 __LASSERT_TAGE_INVARIANT(tage);
601
602                 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
603                         continue;
604
605                 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
606                 tcd->tcd_cur_daemon_pages++;
607
608                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
609                         struct cfs_trace_page *victim;
610
611                         __LASSERT(!cfs_list_empty(&tcd->tcd_daemon_pages));
612                         victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
613
614                         __LASSERT_TAGE_INVARIANT(victim);
615
616                         cfs_list_del(&victim->linkage);
617                         cfs_tage_free(victim);
618                         tcd->tcd_cur_daemon_pages--;
619                 }
620         }
621         cfs_spin_unlock(&pc->pc_lock);
622 }
623
624 static void put_pages_on_daemon_list(struct page_collection *pc)
625 {
626         struct cfs_trace_cpu_data *tcd;
627         int i, cpu;
628
629         cfs_for_each_possible_cpu(cpu) {
630                 cfs_tcd_for_each_type_lock(tcd, i, cpu)
631                         put_pages_on_tcd_daemon_list(pc, tcd);
632         }
633 }
634
635 void cfs_trace_debug_print(void)
636 {
637         struct page_collection pc;
638         struct cfs_trace_page *tage;
639         struct cfs_trace_page *tmp;
640
641         cfs_spin_lock_init(&pc.pc_lock);
642
643         pc.pc_want_daemon_pages = 1;
644         collect_pages(&pc);
645         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
646                                            struct cfs_trace_page, linkage) {
647                 char *p, *file, *fn;
648                 cfs_page_t *page;
649
650                 __LASSERT_TAGE_INVARIANT(tage);
651
652                 page = tage->page;
653                 p = cfs_page_address(page);
654                 while (p < ((char *)cfs_page_address(page) + tage->used)) {
655                         struct ptldebug_header *hdr;
656                         int len;
657                         hdr = (void *)p;
658                         p += sizeof(*hdr);
659                         file = p;
660                         p += strlen(file) + 1;
661                         fn = p;
662                         p += strlen(fn) + 1;
663                         len = hdr->ph_len - (int)(p - (char *)hdr);
664
665                         cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
666
667                         p += len;
668                 }
669
670                 cfs_list_del(&tage->linkage);
671                 cfs_tage_free(tage);
672         }
673 }
674
675 int cfs_tracefile_dump_all_pages(char *filename)
676 {
677         struct page_collection pc;
678         cfs_file_t *filp;
679         struct cfs_trace_page *tage;
680         struct cfs_trace_page *tmp;
681         int rc;
682
683         CFS_DECL_MMSPACE;
684
685         cfs_tracefile_write_lock();
686
687         filp = cfs_filp_open(filename,
688                              O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
689         if (!filp) {
690                 if (rc != -EEXIST)
691                         printk(CFS_KERN_ERR
692                                "LustreError: can't open %s for dump: rc %d\n",
693                                filename, rc);
694                 goto out;
695         }
696
697         cfs_spin_lock_init(&pc.pc_lock);
698         pc.pc_want_daemon_pages = 1;
699         collect_pages(&pc);
700         if (cfs_list_empty(&pc.pc_pages)) {
701                 rc = 0;
702                 goto close;
703         }
704
705         /* ok, for now, just write the pages.  in the future we'll be building
706          * iobufs with the pages and calling generic_direct_IO */
707         CFS_MMSPACE_OPEN;
708         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
709                                            struct cfs_trace_page, linkage) {
710
711                 __LASSERT_TAGE_INVARIANT(tage);
712
713                 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
714                                     tage->used, cfs_filp_poff(filp));
715                 if (rc != (int)tage->used) {
716                         printk(CFS_KERN_WARNING "wanted to write %u but wrote "
717                                "%d\n", tage->used, rc);
718                         put_pages_back(&pc);
719                         __LASSERT(cfs_list_empty(&pc.pc_pages));
720                         break;
721                 }
722                 cfs_list_del(&tage->linkage);
723                 cfs_tage_free(tage);
724         }
725         CFS_MMSPACE_CLOSE;
726         rc = cfs_filp_fsync(filp);
727         if (rc)
728                 printk(CFS_KERN_ERR "sync returns %d\n", rc);
729  close:
730         cfs_filp_close(filp);
731  out:
732         cfs_tracefile_write_unlock();
733         return rc;
734 }
735
736 void cfs_trace_flush_pages(void)
737 {
738         struct page_collection pc;
739         struct cfs_trace_page *tage;
740         struct cfs_trace_page *tmp;
741
742         cfs_spin_lock_init(&pc.pc_lock);
743
744         pc.pc_want_daemon_pages = 1;
745         collect_pages(&pc);
746         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
747                                            struct cfs_trace_page, linkage) {
748
749                 __LASSERT_TAGE_INVARIANT(tage);
750
751                 cfs_list_del(&tage->linkage);
752                 cfs_tage_free(tage);
753         }
754 }
755
756 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
757                             const char *usr_buffer, int usr_buffer_nob)
758 {
759         int    nob;
760
761         if (usr_buffer_nob > knl_buffer_nob)
762                 return -EOVERFLOW;
763
764         if (cfs_copy_from_user((void *)knl_buffer,
765                            (void *)usr_buffer, usr_buffer_nob))
766                 return -EFAULT;
767
768         nob = strnlen(knl_buffer, usr_buffer_nob);
769         while (nob-- >= 0)                      /* strip trailing whitespace */
770                 if (!isspace(knl_buffer[nob]))
771                         break;
772
773         if (nob < 0)                            /* empty string */
774                 return -EINVAL;
775
776         if (nob == knl_buffer_nob)              /* no space to terminate */
777                 return -EOVERFLOW;
778
779         knl_buffer[nob + 1] = 0;                /* terminate */
780         return 0;
781 }
782
783 int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
784                              const char *knl_buffer, char *append)
785 {
786         /* NB if 'append' != NULL, it's a single character to append to the
787          * copied out string - usually "\n", for /proc entries and "" (i.e. a
788          * terminating zero byte) for sysctl entries */
789         int   nob = strlen(knl_buffer);
790
791         if (nob > usr_buffer_nob)
792                 nob = usr_buffer_nob;
793
794         if (cfs_copy_to_user(usr_buffer, knl_buffer, nob))
795                 return -EFAULT;
796
797         if (append != NULL && nob < usr_buffer_nob) {
798                 if (cfs_copy_to_user(usr_buffer + nob, append, 1))
799                         return -EFAULT;
800
801                 nob++;
802         }
803
804         return nob;
805 }
806 EXPORT_SYMBOL(cfs_trace_copyout_string);
807
808 int cfs_trace_allocate_string_buffer(char **str, int nob)
809 {
810         if (nob > 2 * CFS_PAGE_SIZE)            /* string must be "sensible" */
811                 return -EINVAL;
812
813         *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
814         if (*str == NULL)
815                 return -ENOMEM;
816
817         return 0;
818 }
819
820 void cfs_trace_free_string_buffer(char *str, int nob)
821 {
822         cfs_free(str);
823 }
824
825 int cfs_trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
826 {
827         char         *str;
828         int           rc;
829
830         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
831         if (rc != 0)
832                 return rc;
833
834         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
835                                      usr_str, usr_str_nob);
836         if (rc != 0)
837                 goto out;
838
839 #if !defined(__WINNT__)
840         if (str[0] != '/') {
841                 rc = -EINVAL;
842                 goto out;
843         }
844 #endif
845         rc = cfs_tracefile_dump_all_pages(str);
846 out:
847         cfs_trace_free_string_buffer(str, usr_str_nob + 1);
848         return rc;
849 }
850
851 int cfs_trace_daemon_command(char *str)
852 {
853         int       rc = 0;
854
855         cfs_tracefile_write_lock();
856
857         if (strcmp(str, "stop") == 0) {
858                 cfs_tracefile_write_unlock();
859                 cfs_trace_stop_thread();
860                 cfs_tracefile_write_lock();
861                 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
862
863         } else if (strncmp(str, "size=", 5) == 0) {
864                 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
865                 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
866                         cfs_tracefile_size = CFS_TRACEFILE_SIZE;
867                 else
868                         cfs_tracefile_size <<= 20;
869
870         } else if (strlen(str) >= sizeof(cfs_tracefile)) {
871                 rc = -ENAMETOOLONG;
872 #ifndef __WINNT__
873         } else if (str[0] != '/') {
874                 rc = -EINVAL;
875 #endif
876         } else {
877                 strcpy(cfs_tracefile, str);
878
879                 printk(CFS_KERN_INFO
880                        "Lustre: debug daemon will attempt to start writing "
881                        "to %s (%lukB max)\n", cfs_tracefile,
882                        (long)(cfs_tracefile_size >> 10));
883
884                 cfs_trace_start_thread();
885         }
886
887         cfs_tracefile_write_unlock();
888         return rc;
889 }
890
891 int cfs_trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
892 {
893         char *str;
894         int   rc;
895
896         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
897         if (rc != 0)
898                 return rc;
899
900         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
901                                  usr_str, usr_str_nob);
902         if (rc == 0)
903                 rc = cfs_trace_daemon_command(str);
904
905         cfs_trace_free_string_buffer(str, usr_str_nob + 1);
906         return rc;
907 }
908
909 int cfs_trace_set_debug_mb(int mb)
910 {
911         int i;
912         int j;
913         int pages;
914         int limit = cfs_trace_max_debug_mb();
915         struct cfs_trace_cpu_data *tcd;
916
917         if (mb < cfs_num_possible_cpus()) {
918                 printk(KERN_ERR "Cannot set debug_mb to %d, the value should be >= %d\n",
919                        mb, cfs_num_possible_cpus());
920                 return -EINVAL;
921         }
922
923         if (mb > limit) {
924                 printk(CFS_KERN_ERR "Lustre: Refusing to set debug buffer size "
925                        "to %dMB - limit is %d\n", mb, limit);
926                 return -EINVAL;
927         }
928
929         mb /= cfs_num_possible_cpus();
930         pages = mb << (20 - CFS_PAGE_SHIFT);
931
932         cfs_tracefile_write_lock();
933
934         cfs_tcd_for_each(tcd, i, j)
935                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
936
937         cfs_tracefile_write_unlock();
938
939         return 0;
940 }
941
942 int cfs_trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
943 {
944         char     str[32];
945         int      rc;
946
947         rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
948         if (rc < 0)
949                 return rc;
950
951         return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
952 }
953
954 int cfs_trace_get_debug_mb(void)
955 {
956         int i;
957         int j;
958         struct cfs_trace_cpu_data *tcd;
959         int total_pages = 0;
960
961         cfs_tracefile_read_lock();
962
963         cfs_tcd_for_each(tcd, i, j)
964                 total_pages += tcd->tcd_max_pages;
965
966         cfs_tracefile_read_unlock();
967
968         return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
969 }
970
971 static int tracefiled(void *arg)
972 {
973         struct page_collection pc;
974         struct tracefiled_ctl *tctl = arg;
975         struct cfs_trace_page *tage;
976         struct cfs_trace_page *tmp;
977         cfs_file_t *filp;
978         int last_loop = 0;
979         int rc;
980
981         CFS_DECL_MMSPACE;
982
983         /* we're started late enough that we pick up init's fs context */
984         /* this is so broken in uml?  what on earth is going on? */
985         cfs_daemonize("ktracefiled");
986
987         cfs_spin_lock_init(&pc.pc_lock);
988         cfs_complete(&tctl->tctl_start);
989
990         while (1) {
991                 cfs_waitlink_t __wait;
992
993                 pc.pc_want_daemon_pages = 0;
994                 collect_pages(&pc);
995                 if (cfs_list_empty(&pc.pc_pages))
996                         goto end_loop;
997
998                 filp = NULL;
999                 cfs_tracefile_read_lock();
1000                 if (cfs_tracefile[0] != 0) {
1001                         filp = cfs_filp_open(cfs_tracefile,
1002                                              O_CREAT | O_RDWR | O_LARGEFILE,
1003                                              0600, &rc);
1004                         if (!(filp))
1005                                 printk(CFS_KERN_WARNING "couldn't open %s: "
1006                                        "%d\n", cfs_tracefile, rc);
1007                 }
1008                 cfs_tracefile_read_unlock();
1009                 if (filp == NULL) {
1010                         put_pages_on_daemon_list(&pc);
1011                         __LASSERT(cfs_list_empty(&pc.pc_pages));
1012                         goto end_loop;
1013                 }
1014
1015                 CFS_MMSPACE_OPEN;
1016
1017                 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
1018                                                    struct cfs_trace_page,
1019                                                    linkage) {
1020                         static loff_t f_pos;
1021
1022                         __LASSERT_TAGE_INVARIANT(tage);
1023
1024                         if (f_pos >= (off_t)cfs_tracefile_size)
1025                                 f_pos = 0;
1026                         else if (f_pos > (off_t)cfs_filp_size(filp))
1027                                 f_pos = cfs_filp_size(filp);
1028
1029                         rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1030                                             tage->used, &f_pos);
1031                         if (rc != (int)tage->used) {
1032                                 printk(CFS_KERN_WARNING "wanted to write %u "
1033                                        "but wrote %d\n", tage->used, rc);
1034                                 put_pages_back(&pc);
1035                                 __LASSERT(cfs_list_empty(&pc.pc_pages));
1036                         }
1037                 }
1038                 CFS_MMSPACE_CLOSE;
1039
1040                 cfs_filp_close(filp);
1041                 put_pages_on_daemon_list(&pc);
1042                 if (!cfs_list_empty(&pc.pc_pages)) {
1043                         int i;
1044
1045                         printk(CFS_KERN_ALERT "Lustre: trace pages aren't "
1046                                " empty\n");
1047                         printk(CFS_KERN_ERR "total cpus(%d): ",
1048                                cfs_num_possible_cpus());
1049                         for (i = 0; i < cfs_num_possible_cpus(); i++)
1050                                 if (cfs_cpu_online(i))
1051                                         printk(CFS_KERN_ERR "%d(on) ", i);
1052                                 else
1053                                         printk(CFS_KERN_ERR "%d(off) ", i);
1054                         printk(CFS_KERN_ERR "\n");
1055
1056                         i = 0;
1057                         cfs_list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1058                                                      linkage)
1059                                 printk(CFS_KERN_ERR "page %d belongs to cpu "
1060                                        "%d\n", ++i, tage->cpu);
1061                         printk(CFS_KERN_ERR "There are %d pages unwritten\n",
1062                                i);
1063                 }
1064                 __LASSERT(cfs_list_empty(&pc.pc_pages));
1065 end_loop:
1066                 if (cfs_atomic_read(&tctl->tctl_shutdown)) {
1067                         if (last_loop == 0) {
1068                                 last_loop = 1;
1069                                 continue;
1070                         } else {
1071                                 break;
1072                         }
1073                 }
1074                 cfs_waitlink_init(&__wait);
1075                 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
1076                 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
1077                 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
1078                                     cfs_time_seconds(1));
1079                 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
1080         }
1081         cfs_complete(&tctl->tctl_stop);
1082         return 0;
1083 }
1084
1085 int cfs_trace_start_thread(void)
1086 {
1087         struct tracefiled_ctl *tctl = &trace_tctl;
1088         int rc = 0;
1089
1090         cfs_mutex_down(&cfs_trace_thread_sem);
1091         if (thread_running)
1092                 goto out;
1093
1094         cfs_init_completion(&tctl->tctl_start);
1095         cfs_init_completion(&tctl->tctl_stop);
1096         cfs_waitq_init(&tctl->tctl_waitq);
1097         cfs_atomic_set(&tctl->tctl_shutdown, 0);
1098
1099         if (cfs_create_thread(tracefiled, tctl, 0) < 0) {
1100                 rc = -ECHILD;
1101                 goto out;
1102         }
1103
1104         cfs_wait_for_completion(&tctl->tctl_start);
1105         thread_running = 1;
1106 out:
1107         cfs_mutex_up(&cfs_trace_thread_sem);
1108         return rc;
1109 }
1110
1111 void cfs_trace_stop_thread(void)
1112 {
1113         struct tracefiled_ctl *tctl = &trace_tctl;
1114
1115         cfs_mutex_down(&cfs_trace_thread_sem);
1116         if (thread_running) {
1117                 printk(CFS_KERN_INFO
1118                        "Lustre: shutting down debug daemon thread...\n");
1119                 cfs_atomic_set(&tctl->tctl_shutdown, 1);
1120                 cfs_wait_for_completion(&tctl->tctl_stop);
1121                 thread_running = 0;
1122         }
1123         cfs_mutex_up(&cfs_trace_thread_sem);
1124 }
1125
1126 int cfs_tracefile_init(int max_pages)
1127 {
1128         struct cfs_trace_cpu_data *tcd;
1129         int                    i;
1130         int                    j;
1131         int                    rc;
1132         int                    factor;
1133
1134         rc = cfs_tracefile_init_arch();
1135         if (rc != 0)
1136                 return rc;
1137
1138         cfs_tcd_for_each(tcd, i, j) {
1139                 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1140                 factor = tcd->tcd_pages_factor;
1141                 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1142                 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1143                 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1144                 tcd->tcd_cur_pages = 0;
1145                 tcd->tcd_cur_stock_pages = 0;
1146                 tcd->tcd_cur_daemon_pages = 0;
1147                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1148                 LASSERT(tcd->tcd_max_pages > 0);
1149                 tcd->tcd_shutting_down = 0;
1150         }
1151
1152         return 0;
1153 }
1154
1155 static void trace_cleanup_on_all_cpus(void)
1156 {
1157         struct cfs_trace_cpu_data *tcd;
1158         struct cfs_trace_page *tage;
1159         struct cfs_trace_page *tmp;
1160         int i, cpu;
1161
1162         cfs_for_each_possible_cpu(cpu) {
1163                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1164                         tcd->tcd_shutting_down = 1;
1165
1166                         cfs_list_for_each_entry_safe_typed(tage, tmp,
1167                                                            &tcd->tcd_pages,
1168                                                            struct cfs_trace_page,
1169                                                            linkage) {
1170                                 __LASSERT_TAGE_INVARIANT(tage);
1171
1172                                 cfs_list_del(&tage->linkage);
1173                                 cfs_tage_free(tage);
1174                         }
1175
1176                         tcd->tcd_cur_pages = 0;
1177                 }
1178         }
1179 }
1180
1181 static void cfs_trace_cleanup(void)
1182 {
1183         struct page_collection pc;
1184
1185         CFS_INIT_LIST_HEAD(&pc.pc_pages);
1186         cfs_spin_lock_init(&pc.pc_lock);
1187
1188         trace_cleanup_on_all_cpus();
1189
1190         cfs_tracefile_fini_arch();
1191 }
1192
1193 void cfs_tracefile_exit(void)
1194 {
1195         cfs_trace_stop_thread();
1196         cfs_trace_cleanup();
1197 }