Whamcloud - gitweb
86eeea5da3a3edb1b701c517ce649fd0d7485d6b
[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_mutex_t cfs_trace_thread_mutex;
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_msg(struct libcfs_debug_msg_data *msgdata,
247                      const char *format, ...)
248 {
249         va_list args;
250         int     rc;
251
252         va_start(args, format);
253         rc = libcfs_debug_vmsg2(msgdata, format, args, NULL);
254         va_end(args);
255
256         return rc;
257 }
258 EXPORT_SYMBOL(libcfs_debug_msg);
259
260 int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
261                        const char *format1, va_list args,
262                        const char *format2, ...)
263 {
264         struct cfs_trace_cpu_data *tcd = NULL;
265         struct ptldebug_header     header = {0};
266         struct cfs_trace_page     *tage;
267         /* string_buf is used only if tcd != NULL, and is always set then */
268         char                      *string_buf = NULL;
269         char                      *debug_buf;
270         int                        known_size;
271         int                        needed = 85; /* average message length */
272         int                        max_nob;
273         va_list                    ap;
274         int                        depth;
275         int                        i;
276         int                        remain;
277         int                        mask = msgdata->msg_mask;
278         char                      *file = (char *)msgdata->msg_file;
279         cfs_debug_limit_state_t   *cdls = msgdata->msg_cdls;
280
281         if (strchr(file, '/'))
282                 file = strrchr(file, '/') + 1;
283
284         tcd = cfs_trace_get_tcd();
285
286         /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
287          * pins us to a particular CPU.  This avoids an smp_processor_id()
288          * warning on Linux when debugging is enabled. */
289         cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
290
291         if (tcd == NULL)                /* arch may not log in IRQ context */
292                 goto console;
293
294         if (tcd->tcd_cur_pages == 0)
295                 header.ph_flags |= PH_FLAG_FIRST_RECORD;
296
297         if (tcd->tcd_shutting_down) {
298                 cfs_trace_put_tcd(tcd);
299                 tcd = NULL;
300                 goto console;
301         }
302
303         depth = __current_nesting_level();
304         known_size = strlen(file) + 1 + depth;
305         if (msgdata->msg_fn)
306                 known_size += strlen(msgdata->msg_fn) + 1;
307
308         if (libcfs_debug_binary)
309                 known_size += sizeof(header);
310
311         /*/
312          * '2' used because vsnprintf return real size required for output
313          * _without_ terminating NULL.
314          * if needed is to small for this format.
315          */
316         for (i = 0; i < 2; i++) {
317                 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
318                 if (tage == NULL) {
319                         if (needed + known_size > CFS_PAGE_SIZE)
320                                 mask |= D_ERROR;
321
322                         cfs_trace_put_tcd(tcd);
323                         tcd = NULL;
324                         goto console;
325                 }
326
327                 string_buf = (char *)cfs_page_address(tage->page) +
328                                         tage->used + known_size;
329
330                 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
331                 if (max_nob <= 0) {
332                         printk(CFS_KERN_EMERG "negative max_nob: %d\n",
333                                max_nob);
334                         mask |= D_ERROR;
335                         cfs_trace_put_tcd(tcd);
336                         tcd = NULL;
337                         goto console;
338                 }
339
340                 needed = 0;
341                 if (format1) {
342                         va_copy(ap, args);
343                         needed = vsnprintf(string_buf, max_nob, format1, ap);
344                         va_end(ap);
345                 }
346
347                 if (format2) {
348                         remain = max_nob - needed;
349                         if (remain < 0)
350                                 remain = 0;
351
352                         va_start(ap, format2);
353                         needed += vsnprintf(string_buf + needed, remain,
354                                             format2, ap);
355                         va_end(ap);
356                 }
357
358                 if (needed < max_nob) /* well. printing ok.. */
359                         break;
360         }
361
362         if (*(string_buf+needed-1) != '\n')
363                 printk(CFS_KERN_INFO "format at %s:%d:%s doesn't end in "
364                        "newline\n", file, msgdata->msg_line, msgdata->msg_fn);
365
366         header.ph_len = known_size + needed;
367         debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
368
369         if (libcfs_debug_binary) {
370                 memcpy(debug_buf, &header, sizeof(header));
371                 tage->used += sizeof(header);
372                 debug_buf += sizeof(header);
373         }
374
375         /* indent message according to the nesting level */
376         while (depth-- > 0) {
377                 *(debug_buf++) = '.';
378                 ++ tage->used;
379         }
380
381         strcpy(debug_buf, file);
382         tage->used += strlen(file) + 1;
383         debug_buf += strlen(file) + 1;
384
385         if (msgdata->msg_fn) {
386                 strcpy(debug_buf, msgdata->msg_fn);
387                 tage->used += strlen(msgdata->msg_fn) + 1;
388                 debug_buf += strlen(msgdata->msg_fn) + 1;
389         }
390
391         __LASSERT(debug_buf == string_buf);
392
393         tage->used += needed;
394         __LASSERT (tage->used <= CFS_PAGE_SIZE);
395
396 console:
397         if ((mask & libcfs_printk) == 0) {
398                 /* no console output requested */
399                 if (tcd != NULL)
400                         cfs_trace_put_tcd(tcd);
401                 return 1;
402         }
403
404         if (cdls != NULL) {
405                 if (libcfs_console_ratelimit &&
406                     cdls->cdls_next != 0 &&     /* not first time ever */
407                     !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
408                         /* skipping a console message */
409                         cdls->cdls_count++;
410                         if (tcd != NULL)
411                                 cfs_trace_put_tcd(tcd);
412                         return 1;
413                 }
414
415                 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
416                                                        libcfs_console_max_delay
417                                                        + cfs_time_seconds(10))) {
418                         /* last timeout was a long time ago */
419                         cdls->cdls_delay /= libcfs_console_backoff * 4;
420                 } else {
421                         cdls->cdls_delay *= libcfs_console_backoff;
422
423                         if (cdls->cdls_delay < libcfs_console_min_delay)
424                                 cdls->cdls_delay = libcfs_console_min_delay;
425                         else if (cdls->cdls_delay > libcfs_console_max_delay)
426                                 cdls->cdls_delay = libcfs_console_max_delay;
427                 }
428
429                 /* ensure cdls_next is never zero after it's been seen */
430                 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
431         }
432
433         if (tcd != NULL) {
434                 cfs_print_to_console(&header, mask, string_buf, needed, file,
435                                      msgdata->msg_fn);
436                 cfs_trace_put_tcd(tcd);
437         } else {
438                 string_buf = cfs_trace_get_console_buffer();
439
440                 needed = 0;
441                 if (format1 != NULL) {
442                         va_copy(ap, args);
443                         needed = vsnprintf(string_buf,
444                                            CFS_TRACE_CONSOLE_BUFFER_SIZE,
445                                            format1, ap);
446                         va_end(ap);
447                 }
448                 if (format2 != NULL) {
449                         remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
450                         if (remain > 0) {
451                                 va_start(ap, format2);
452                                 needed += vsnprintf(string_buf+needed, remain,
453                                                     format2, ap);
454                                 va_end(ap);
455                         }
456                 }
457                 cfs_print_to_console(&header, mask,
458                                      string_buf, needed, file, msgdata->msg_fn);
459
460                 cfs_trace_put_console_buffer(string_buf);
461         }
462
463         if (cdls != NULL && cdls->cdls_count != 0) {
464                 string_buf = cfs_trace_get_console_buffer();
465
466                 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
467                                   "Skipped %d previous similar message%s\n",
468                                   cdls->cdls_count,
469                                   (cdls->cdls_count > 1) ? "s" : "");
470
471                 cfs_print_to_console(&header, mask,
472                                      string_buf, needed, file, msgdata->msg_fn);
473
474                 cfs_trace_put_console_buffer(string_buf);
475                 cdls->cdls_count = 0;
476         }
477
478         return 0;
479 }
480 EXPORT_SYMBOL(libcfs_debug_vmsg2);
481
482 void
483 libcfs_assertion_failed(const char *expr, struct libcfs_debug_msg_data *msgdata)
484 {
485         libcfs_debug_msg(msgdata, "ASSERTION(%s) failed\n", expr);
486         /* cfs_enter_debugger(); */
487         lbug_with_loc(msgdata);
488 }
489 EXPORT_SYMBOL(libcfs_assertion_failed);
490
491 void
492 cfs_trace_assertion_failed(const char *str,
493                            struct libcfs_debug_msg_data *msgdata)
494 {
495         struct ptldebug_header hdr;
496
497         libcfs_panic_in_progress = 1;
498         libcfs_catastrophe = 1;
499         cfs_mb();
500
501         cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
502
503         cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
504                              msgdata->msg_file, msgdata->msg_fn);
505
506         LIBCFS_PANIC("Lustre debug assertion failure\n");
507
508         /* not reached */
509 }
510
511 static void
512 panic_collect_pages(struct page_collection *pc)
513 {
514         /* Do the collect_pages job on a single CPU: assumes that all other
515          * CPUs have been stopped during a panic.  If this isn't true for some
516          * arch, this will have to be implemented separately in each arch.  */
517         int                        i;
518         int                        j;
519         struct cfs_trace_cpu_data *tcd;
520
521         CFS_INIT_LIST_HEAD(&pc->pc_pages);
522
523         cfs_tcd_for_each(tcd, i, j) {
524                 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
525                 tcd->tcd_cur_pages = 0;
526
527                 if (pc->pc_want_daemon_pages) {
528                         cfs_list_splice_init(&tcd->tcd_daemon_pages,
529                                              &pc->pc_pages);
530                         tcd->tcd_cur_daemon_pages = 0;
531                 }
532         }
533 }
534
535 static void collect_pages_on_all_cpus(struct page_collection *pc)
536 {
537         struct cfs_trace_cpu_data *tcd;
538         int i, cpu;
539
540         cfs_spin_lock(&pc->pc_lock);
541         cfs_for_each_possible_cpu(cpu) {
542                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
543                         cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
544                         tcd->tcd_cur_pages = 0;
545                         if (pc->pc_want_daemon_pages) {
546                                 cfs_list_splice_init(&tcd->tcd_daemon_pages,
547                                                      &pc->pc_pages);
548                                 tcd->tcd_cur_daemon_pages = 0;
549                         }
550                 }
551         }
552         cfs_spin_unlock(&pc->pc_lock);
553 }
554
555 static void collect_pages(struct page_collection *pc)
556 {
557         CFS_INIT_LIST_HEAD(&pc->pc_pages);
558
559         if (libcfs_panic_in_progress)
560                 panic_collect_pages(pc);
561         else
562                 collect_pages_on_all_cpus(pc);
563 }
564
565 static void put_pages_back_on_all_cpus(struct page_collection *pc)
566 {
567         struct cfs_trace_cpu_data *tcd;
568         cfs_list_t *cur_head;
569         struct cfs_trace_page *tage;
570         struct cfs_trace_page *tmp;
571         int i, cpu;
572
573         cfs_spin_lock(&pc->pc_lock);
574         cfs_for_each_possible_cpu(cpu) {
575                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
576                         cur_head = tcd->tcd_pages.next;
577
578                         cfs_list_for_each_entry_safe_typed(tage, tmp,
579                                                            &pc->pc_pages,
580                                                            struct cfs_trace_page,
581                                                            linkage) {
582
583                                 __LASSERT_TAGE_INVARIANT(tage);
584
585                                 if (tage->cpu != cpu || tage->type != i)
586                                         continue;
587
588                                 cfs_tage_to_tail(tage, cur_head);
589                                 tcd->tcd_cur_pages++;
590                         }
591                 }
592         }
593         cfs_spin_unlock(&pc->pc_lock);
594 }
595
596 static void put_pages_back(struct page_collection *pc)
597 {
598         if (!libcfs_panic_in_progress)
599                 put_pages_back_on_all_cpus(pc);
600 }
601
602 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
603  * we have a good amount of data at all times for dumping during an LBUG, even
604  * if we have been steadily writing (and otherwise discarding) pages via the
605  * debug daemon. */
606 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
607                                          struct cfs_trace_cpu_data *tcd)
608 {
609         struct cfs_trace_page *tage;
610         struct cfs_trace_page *tmp;
611
612         cfs_spin_lock(&pc->pc_lock);
613         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
614                                            struct cfs_trace_page, linkage) {
615
616                 __LASSERT_TAGE_INVARIANT(tage);
617
618                 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
619                         continue;
620
621                 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
622                 tcd->tcd_cur_daemon_pages++;
623
624                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
625                         struct cfs_trace_page *victim;
626
627                         __LASSERT(!cfs_list_empty(&tcd->tcd_daemon_pages));
628                         victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
629
630                         __LASSERT_TAGE_INVARIANT(victim);
631
632                         cfs_list_del(&victim->linkage);
633                         cfs_tage_free(victim);
634                         tcd->tcd_cur_daemon_pages--;
635                 }
636         }
637         cfs_spin_unlock(&pc->pc_lock);
638 }
639
640 static void put_pages_on_daemon_list(struct page_collection *pc)
641 {
642         struct cfs_trace_cpu_data *tcd;
643         int i, cpu;
644
645         cfs_for_each_possible_cpu(cpu) {
646                 cfs_tcd_for_each_type_lock(tcd, i, cpu)
647                         put_pages_on_tcd_daemon_list(pc, tcd);
648         }
649 }
650
651 void cfs_trace_debug_print(void)
652 {
653         struct page_collection pc;
654         struct cfs_trace_page *tage;
655         struct cfs_trace_page *tmp;
656
657         cfs_spin_lock_init(&pc.pc_lock);
658
659         pc.pc_want_daemon_pages = 1;
660         collect_pages(&pc);
661         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
662                                            struct cfs_trace_page, linkage) {
663                 char *p, *file, *fn;
664                 cfs_page_t *page;
665
666                 __LASSERT_TAGE_INVARIANT(tage);
667
668                 page = tage->page;
669                 p = cfs_page_address(page);
670                 while (p < ((char *)cfs_page_address(page) + tage->used)) {
671                         struct ptldebug_header *hdr;
672                         int len;
673                         hdr = (void *)p;
674                         p += sizeof(*hdr);
675                         file = p;
676                         p += strlen(file) + 1;
677                         fn = p;
678                         p += strlen(fn) + 1;
679                         len = hdr->ph_len - (int)(p - (char *)hdr);
680
681                         cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
682
683                         p += len;
684                 }
685
686                 cfs_list_del(&tage->linkage);
687                 cfs_tage_free(tage);
688         }
689 }
690
691 int cfs_tracefile_dump_all_pages(char *filename)
692 {
693         struct page_collection pc;
694         cfs_file_t *filp;
695         struct cfs_trace_page *tage;
696         struct cfs_trace_page *tmp;
697         int rc;
698
699         CFS_DECL_MMSPACE;
700
701         cfs_tracefile_write_lock();
702
703         filp = cfs_filp_open(filename,
704                              O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
705         if (!filp) {
706                 if (rc != -EEXIST)
707                         printk(CFS_KERN_ERR
708                                "LustreError: can't open %s for dump: rc %d\n",
709                                filename, rc);
710                 goto out;
711         }
712
713         cfs_spin_lock_init(&pc.pc_lock);
714         pc.pc_want_daemon_pages = 1;
715         collect_pages(&pc);
716         if (cfs_list_empty(&pc.pc_pages)) {
717                 rc = 0;
718                 goto close;
719         }
720
721         /* ok, for now, just write the pages.  in the future we'll be building
722          * iobufs with the pages and calling generic_direct_IO */
723         CFS_MMSPACE_OPEN;
724         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
725                                            struct cfs_trace_page, linkage) {
726
727                 __LASSERT_TAGE_INVARIANT(tage);
728
729                 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
730                                     tage->used, cfs_filp_poff(filp));
731                 if (rc != (int)tage->used) {
732                         printk(CFS_KERN_WARNING "wanted to write %u but wrote "
733                                "%d\n", tage->used, rc);
734                         put_pages_back(&pc);
735                         __LASSERT(cfs_list_empty(&pc.pc_pages));
736                         break;
737                 }
738                 cfs_list_del(&tage->linkage);
739                 cfs_tage_free(tage);
740         }
741         CFS_MMSPACE_CLOSE;
742         rc = cfs_filp_fsync(filp);
743         if (rc)
744                 printk(CFS_KERN_ERR "sync returns %d\n", rc);
745  close:
746         cfs_filp_close(filp);
747  out:
748         cfs_tracefile_write_unlock();
749         return rc;
750 }
751
752 void cfs_trace_flush_pages(void)
753 {
754         struct page_collection pc;
755         struct cfs_trace_page *tage;
756         struct cfs_trace_page *tmp;
757
758         cfs_spin_lock_init(&pc.pc_lock);
759
760         pc.pc_want_daemon_pages = 1;
761         collect_pages(&pc);
762         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
763                                            struct cfs_trace_page, linkage) {
764
765                 __LASSERT_TAGE_INVARIANT(tage);
766
767                 cfs_list_del(&tage->linkage);
768                 cfs_tage_free(tage);
769         }
770 }
771
772 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
773                             const char *usr_buffer, int usr_buffer_nob)
774 {
775         int    nob;
776
777         if (usr_buffer_nob > knl_buffer_nob)
778                 return -EOVERFLOW;
779
780         if (cfs_copy_from_user((void *)knl_buffer,
781                            (void *)usr_buffer, usr_buffer_nob))
782                 return -EFAULT;
783
784         nob = strnlen(knl_buffer, usr_buffer_nob);
785         while (nob-- >= 0)                      /* strip trailing whitespace */
786                 if (!isspace(knl_buffer[nob]))
787                         break;
788
789         if (nob < 0)                            /* empty string */
790                 return -EINVAL;
791
792         if (nob == knl_buffer_nob)              /* no space to terminate */
793                 return -EOVERFLOW;
794
795         knl_buffer[nob + 1] = 0;                /* terminate */
796         return 0;
797 }
798
799 int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
800                              const char *knl_buffer, char *append)
801 {
802         /* NB if 'append' != NULL, it's a single character to append to the
803          * copied out string - usually "\n", for /proc entries and "" (i.e. a
804          * terminating zero byte) for sysctl entries */
805         int   nob = strlen(knl_buffer);
806
807         if (nob > usr_buffer_nob)
808                 nob = usr_buffer_nob;
809
810         if (cfs_copy_to_user(usr_buffer, knl_buffer, nob))
811                 return -EFAULT;
812
813         if (append != NULL && nob < usr_buffer_nob) {
814                 if (cfs_copy_to_user(usr_buffer + nob, append, 1))
815                         return -EFAULT;
816
817                 nob++;
818         }
819
820         return nob;
821 }
822 EXPORT_SYMBOL(cfs_trace_copyout_string);
823
824 int cfs_trace_allocate_string_buffer(char **str, int nob)
825 {
826         if (nob > 2 * CFS_PAGE_SIZE)            /* string must be "sensible" */
827                 return -EINVAL;
828
829         *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
830         if (*str == NULL)
831                 return -ENOMEM;
832
833         return 0;
834 }
835
836 void cfs_trace_free_string_buffer(char *str, int nob)
837 {
838         cfs_free(str);
839 }
840
841 int cfs_trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
842 {
843         char         *str;
844         int           rc;
845
846         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
847         if (rc != 0)
848                 return rc;
849
850         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
851                                      usr_str, usr_str_nob);
852         if (rc != 0)
853                 goto out;
854
855 #if !defined(__WINNT__)
856         if (str[0] != '/') {
857                 rc = -EINVAL;
858                 goto out;
859         }
860 #endif
861         rc = cfs_tracefile_dump_all_pages(str);
862 out:
863         cfs_trace_free_string_buffer(str, usr_str_nob + 1);
864         return rc;
865 }
866
867 int cfs_trace_daemon_command(char *str)
868 {
869         int       rc = 0;
870
871         cfs_tracefile_write_lock();
872
873         if (strcmp(str, "stop") == 0) {
874                 cfs_tracefile_write_unlock();
875                 cfs_trace_stop_thread();
876                 cfs_tracefile_write_lock();
877                 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
878
879         } else if (strncmp(str, "size=", 5) == 0) {
880                 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
881                 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
882                         cfs_tracefile_size = CFS_TRACEFILE_SIZE;
883                 else
884                         cfs_tracefile_size <<= 20;
885
886         } else if (strlen(str) >= sizeof(cfs_tracefile)) {
887                 rc = -ENAMETOOLONG;
888 #ifndef __WINNT__
889         } else if (str[0] != '/') {
890                 rc = -EINVAL;
891 #endif
892         } else {
893                 strcpy(cfs_tracefile, str);
894
895                 printk(CFS_KERN_INFO
896                        "Lustre: debug daemon will attempt to start writing "
897                        "to %s (%lukB max)\n", cfs_tracefile,
898                        (long)(cfs_tracefile_size >> 10));
899
900                 cfs_trace_start_thread();
901         }
902
903         cfs_tracefile_write_unlock();
904         return rc;
905 }
906
907 int cfs_trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
908 {
909         char *str;
910         int   rc;
911
912         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
913         if (rc != 0)
914                 return rc;
915
916         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
917                                  usr_str, usr_str_nob);
918         if (rc == 0)
919                 rc = cfs_trace_daemon_command(str);
920
921         cfs_trace_free_string_buffer(str, usr_str_nob + 1);
922         return rc;
923 }
924
925 int cfs_trace_set_debug_mb(int mb)
926 {
927         int i;
928         int j;
929         int pages;
930         int limit = cfs_trace_max_debug_mb();
931         struct cfs_trace_cpu_data *tcd;
932
933         if (mb < cfs_num_possible_cpus()) {
934                 printk(CFS_KERN_WARNING
935                        "Lustre: %d MB is too small for debug buffer size, "
936                        "setting it to %d MB.\n", mb, cfs_num_possible_cpus());
937                 mb = cfs_num_possible_cpus();
938         }
939
940         if (mb > limit) {
941                 printk(CFS_KERN_WARNING
942                        "Lustre: %d MB is too large for debug buffer size, "
943                        "setting it to %d MB.\n", mb, limit);
944                 mb = limit;
945         }
946
947         mb /= cfs_num_possible_cpus();
948         pages = mb << (20 - CFS_PAGE_SHIFT);
949
950         cfs_tracefile_write_lock();
951
952         cfs_tcd_for_each(tcd, i, j)
953                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
954
955         cfs_tracefile_write_unlock();
956
957         return 0;
958 }
959
960 int cfs_trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
961 {
962         char     str[32];
963         int      rc;
964
965         rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
966         if (rc < 0)
967                 return rc;
968
969         return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
970 }
971
972 int cfs_trace_get_debug_mb(void)
973 {
974         int i;
975         int j;
976         struct cfs_trace_cpu_data *tcd;
977         int total_pages = 0;
978
979         cfs_tracefile_read_lock();
980
981         cfs_tcd_for_each(tcd, i, j)
982                 total_pages += tcd->tcd_max_pages;
983
984         cfs_tracefile_read_unlock();
985
986         return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
987 }
988
989 static int tracefiled(void *arg)
990 {
991         struct page_collection pc;
992         struct tracefiled_ctl *tctl = arg;
993         struct cfs_trace_page *tage;
994         struct cfs_trace_page *tmp;
995         cfs_file_t *filp;
996         int last_loop = 0;
997         int rc;
998
999         CFS_DECL_MMSPACE;
1000
1001         /* we're started late enough that we pick up init's fs context */
1002         /* this is so broken in uml?  what on earth is going on? */
1003         cfs_daemonize("ktracefiled");
1004
1005         cfs_spin_lock_init(&pc.pc_lock);
1006         cfs_complete(&tctl->tctl_start);
1007
1008         while (1) {
1009                 cfs_waitlink_t __wait;
1010
1011                 pc.pc_want_daemon_pages = 0;
1012                 collect_pages(&pc);
1013                 if (cfs_list_empty(&pc.pc_pages))
1014                         goto end_loop;
1015
1016                 filp = NULL;
1017                 cfs_tracefile_read_lock();
1018                 if (cfs_tracefile[0] != 0) {
1019                         filp = cfs_filp_open(cfs_tracefile,
1020                                              O_CREAT | O_RDWR | O_LARGEFILE,
1021                                              0600, &rc);
1022                         if (!(filp))
1023                                 printk(CFS_KERN_WARNING "couldn't open %s: "
1024                                        "%d\n", cfs_tracefile, rc);
1025                 }
1026                 cfs_tracefile_read_unlock();
1027                 if (filp == NULL) {
1028                         put_pages_on_daemon_list(&pc);
1029                         __LASSERT(cfs_list_empty(&pc.pc_pages));
1030                         goto end_loop;
1031                 }
1032
1033                 CFS_MMSPACE_OPEN;
1034
1035                 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
1036                                                    struct cfs_trace_page,
1037                                                    linkage) {
1038                         static loff_t f_pos;
1039
1040                         __LASSERT_TAGE_INVARIANT(tage);
1041
1042                         if (f_pos >= (off_t)cfs_tracefile_size)
1043                                 f_pos = 0;
1044                         else if (f_pos > (off_t)cfs_filp_size(filp))
1045                                 f_pos = cfs_filp_size(filp);
1046
1047                         rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1048                                             tage->used, &f_pos);
1049                         if (rc != (int)tage->used) {
1050                                 printk(CFS_KERN_WARNING "wanted to write %u "
1051                                        "but wrote %d\n", tage->used, rc);
1052                                 put_pages_back(&pc);
1053                                 __LASSERT(cfs_list_empty(&pc.pc_pages));
1054                         }
1055                 }
1056                 CFS_MMSPACE_CLOSE;
1057
1058                 cfs_filp_close(filp);
1059                 put_pages_on_daemon_list(&pc);
1060                 if (!cfs_list_empty(&pc.pc_pages)) {
1061                         int i;
1062
1063                         printk(CFS_KERN_ALERT "Lustre: trace pages aren't "
1064                                " empty\n");
1065                         printk(CFS_KERN_ERR "total cpus(%d): ",
1066                                cfs_num_possible_cpus());
1067                         for (i = 0; i < cfs_num_possible_cpus(); i++)
1068                                 if (cpu_online(i))
1069                                         printk(CFS_KERN_ERR "%d(on) ", i);
1070                                 else
1071                                         printk(CFS_KERN_ERR "%d(off) ", i);
1072                         printk(CFS_KERN_ERR "\n");
1073
1074                         i = 0;
1075                         cfs_list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1076                                                      linkage)
1077                                 printk(CFS_KERN_ERR "page %d belongs to cpu "
1078                                        "%d\n", ++i, tage->cpu);
1079                         printk(CFS_KERN_ERR "There are %d pages unwritten\n",
1080                                i);
1081                 }
1082                 __LASSERT(cfs_list_empty(&pc.pc_pages));
1083 end_loop:
1084                 if (cfs_atomic_read(&tctl->tctl_shutdown)) {
1085                         if (last_loop == 0) {
1086                                 last_loop = 1;
1087                                 continue;
1088                         } else {
1089                                 break;
1090                         }
1091                 }
1092                 cfs_waitlink_init(&__wait);
1093                 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
1094                 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
1095                 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
1096                                     cfs_time_seconds(1));
1097                 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
1098         }
1099         cfs_complete(&tctl->tctl_stop);
1100         return 0;
1101 }
1102
1103 int cfs_trace_start_thread(void)
1104 {
1105         struct tracefiled_ctl *tctl = &trace_tctl;
1106         int rc = 0;
1107
1108         cfs_mutex_lock(&cfs_trace_thread_mutex);
1109         if (thread_running)
1110                 goto out;
1111
1112         cfs_init_completion(&tctl->tctl_start);
1113         cfs_init_completion(&tctl->tctl_stop);
1114         cfs_waitq_init(&tctl->tctl_waitq);
1115         cfs_atomic_set(&tctl->tctl_shutdown, 0);
1116
1117         if (cfs_create_thread(tracefiled, tctl, 0) < 0) {
1118                 rc = -ECHILD;
1119                 goto out;
1120         }
1121
1122         cfs_wait_for_completion(&tctl->tctl_start);
1123         thread_running = 1;
1124 out:
1125         cfs_mutex_unlock(&cfs_trace_thread_mutex);
1126         return rc;
1127 }
1128
1129 void cfs_trace_stop_thread(void)
1130 {
1131         struct tracefiled_ctl *tctl = &trace_tctl;
1132
1133         cfs_mutex_lock(&cfs_trace_thread_mutex);
1134         if (thread_running) {
1135                 printk(CFS_KERN_INFO
1136                        "Lustre: shutting down debug daemon thread...\n");
1137                 cfs_atomic_set(&tctl->tctl_shutdown, 1);
1138                 cfs_wait_for_completion(&tctl->tctl_stop);
1139                 thread_running = 0;
1140         }
1141         cfs_mutex_unlock(&cfs_trace_thread_mutex);
1142 }
1143
1144 int cfs_tracefile_init(int max_pages)
1145 {
1146         struct cfs_trace_cpu_data *tcd;
1147         int                    i;
1148         int                    j;
1149         int                    rc;
1150         int                    factor;
1151
1152         rc = cfs_tracefile_init_arch();
1153         if (rc != 0)
1154                 return rc;
1155
1156         cfs_tcd_for_each(tcd, i, j) {
1157                 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1158                 factor = tcd->tcd_pages_factor;
1159                 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1160                 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1161                 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1162                 tcd->tcd_cur_pages = 0;
1163                 tcd->tcd_cur_stock_pages = 0;
1164                 tcd->tcd_cur_daemon_pages = 0;
1165                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1166                 LASSERT(tcd->tcd_max_pages > 0);
1167                 tcd->tcd_shutting_down = 0;
1168         }
1169
1170         return 0;
1171 }
1172
1173 static void trace_cleanup_on_all_cpus(void)
1174 {
1175         struct cfs_trace_cpu_data *tcd;
1176         struct cfs_trace_page *tage;
1177         struct cfs_trace_page *tmp;
1178         int i, cpu;
1179
1180         cfs_for_each_possible_cpu(cpu) {
1181                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1182                         tcd->tcd_shutting_down = 1;
1183
1184                         cfs_list_for_each_entry_safe_typed(tage, tmp,
1185                                                            &tcd->tcd_pages,
1186                                                            struct cfs_trace_page,
1187                                                            linkage) {
1188                                 __LASSERT_TAGE_INVARIANT(tage);
1189
1190                                 cfs_list_del(&tage->linkage);
1191                                 cfs_tage_free(tage);
1192                         }
1193
1194                         tcd->tcd_cur_pages = 0;
1195                 }
1196         }
1197 }
1198
1199 static void cfs_trace_cleanup(void)
1200 {
1201         struct page_collection pc;
1202
1203         CFS_INIT_LIST_HEAD(&pc.pc_pages);
1204         cfs_spin_lock_init(&pc.pc_lock);
1205
1206         trace_cleanup_on_all_cpus();
1207
1208         cfs_tracefile_fini_arch();
1209 }
1210
1211 void cfs_tracefile_exit(void)
1212 {
1213         cfs_trace_stop_thread();
1214         cfs_trace_cleanup();
1215 }