Whamcloud - gitweb
0b8e61ee1200f29241470505bbb8c0161b3b2f24
[fs/lustre-release.git] / lnet / libcfs / tracefile.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  *   Author: Zach Brown <zab@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24
25 #define DEBUG_SUBSYSTEM S_LNET
26 #define LUSTRE_TRACEFILE_PRIVATE
27 #include "tracefile.h"
28
29 #include <libcfs/kp30.h>
30 #include <libcfs/libcfs.h>
31
32 /* XXX move things up to the top, comment */
33 union trace_data_union trace_data[NR_CPUS] __cacheline_aligned;
34
35 char *tracefile = NULL;
36 int64_t tracefile_size = TRACEFILE_SIZE;
37 static struct tracefiled_ctl trace_tctl;
38 struct semaphore trace_thread_sem;
39 static int thread_running = 0;
40
41 atomic_t tage_allocated = ATOMIC_INIT(0);
42
43 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
44                                          struct trace_cpu_data *tcd);
45
46 static inline struct trace_page *tage_from_list(struct list_head *list)
47 {
48         return list_entry(list, struct trace_page, linkage);
49 }
50
51 static struct trace_page *tage_alloc(int gfp)
52 {
53         cfs_page_t        *page;
54         struct trace_page *tage;
55
56         /*
57          * Don't spam console with allocation failures: they will be reported
58          * by upper layer anyway.
59          */
60         gfp |= CFS_ALLOC_NOWARN;
61         page = cfs_alloc_page(gfp);
62         if (page == NULL)
63                 return NULL;
64
65         tage = cfs_alloc(sizeof(*tage), gfp);
66         if (tage == NULL) {
67                 cfs_free_page(page);
68                 return NULL;
69         }
70
71         tage->page = page;
72         atomic_inc(&tage_allocated);
73         return tage;
74 }
75
76 static void tage_free(struct trace_page *tage)
77 {
78         __LASSERT(tage != NULL);
79         __LASSERT(tage->page != NULL);
80
81         cfs_free_page(tage->page);
82         cfs_free(tage);
83         atomic_dec(&tage_allocated);
84 }
85
86 static void tage_to_tail(struct trace_page *tage, struct list_head *queue)
87 {
88         __LASSERT(tage != NULL);
89         __LASSERT(queue != NULL);
90
91         list_move_tail(&tage->linkage, queue);
92 }
93
94 int trace_refill_stock(struct trace_cpu_data *tcd, int gfp,
95                        struct list_head *stock)
96 {
97         int i;
98
99         /*
100          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
101          * from here: this will lead to infinite recursion.
102          */
103
104         for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
105                 struct trace_page *tage;
106
107                 tage = tage_alloc(gfp);
108                 if (tage == NULL)
109                         break;
110                 list_add_tail(&tage->linkage, stock);
111         }
112         return i;
113 }
114
115 /* return a page that has 'len' bytes left at the end */
116 static struct trace_page *trace_get_tage_try(struct trace_cpu_data *tcd,
117                                              unsigned long len)
118 {
119         struct trace_page *tage;
120
121         if (tcd->tcd_cur_pages > 0) {
122                 __LASSERT(!list_empty(&tcd->tcd_pages));
123                 tage = tage_from_list(tcd->tcd_pages.prev);
124                 if (tage->used + len <= CFS_PAGE_SIZE)
125                         return tage;
126         }
127
128         if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
129                 if (tcd->tcd_cur_stock_pages > 0) {
130                         tage = tage_from_list(tcd->tcd_stock_pages.prev);
131                         -- tcd->tcd_cur_stock_pages;
132                         list_del_init(&tage->linkage);
133                 } else {
134                         tage = tage_alloc(CFS_ALLOC_ATOMIC);
135                         if (tage == NULL) {
136                                 printk(KERN_WARNING
137                                        "failure to allocate a tage (%ld)\n",
138                                        tcd->tcd_cur_pages);
139                                 return NULL;
140                         }
141                 }
142
143                 tage->used = 0;
144                 tage->cpu = smp_processor_id();
145                 list_add_tail(&tage->linkage, &tcd->tcd_pages);
146                 tcd->tcd_cur_pages++;
147
148                 if (tcd->tcd_cur_pages > 8 && thread_running) {
149                         struct tracefiled_ctl *tctl = &trace_tctl;
150                         /*
151                          * wake up tracefiled to process some pages.
152                          */
153                         cfs_waitq_signal(&tctl->tctl_waitq);
154                 }
155                 return tage;
156         }
157         return NULL;
158 }
159
160 static void tcd_shrink(struct trace_cpu_data *tcd)
161 {
162         int pgcount = tcd->tcd_cur_pages / 10;
163         struct page_collection pc;
164         struct trace_page *tage;
165         struct trace_page *tmp;
166
167         /*
168          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
169          * from here: this will lead to infinite recursion.
170          */
171
172         printk(KERN_WARNING "debug daemon buffer overflowed; discarding"
173                " 10%% of pages (%d of %ld)\n", pgcount + 1, tcd->tcd_cur_pages);
174
175         CFS_INIT_LIST_HEAD(&pc.pc_pages);
176         spin_lock_init(&pc.pc_lock);
177
178         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
179                 if (pgcount-- == 0)
180                         break;
181
182                 list_move_tail(&tage->linkage, &pc.pc_pages);
183                 tcd->tcd_cur_pages--;
184         }
185         put_pages_on_tcd_daemon_list(&pc, tcd);
186 }
187
188 /* return a page that has 'len' bytes left at the end */
189 static struct trace_page *trace_get_tage(struct trace_cpu_data *tcd,
190                                          unsigned long len)
191 {
192         struct trace_page *tage;
193
194         /*
195          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
196          * from here: this will lead to infinite recursion.
197          */
198
199         if (len > CFS_PAGE_SIZE) {
200                 printk(KERN_ERR
201                        "cowardly refusing to write %lu bytes in a page\n", len);
202                 return NULL;
203         }
204
205         tage = trace_get_tage_try(tcd, len);
206         if (tage != NULL)
207                 return tage;
208         if (thread_running)
209                 tcd_shrink(tcd);
210         if (tcd->tcd_cur_pages > 0) {
211                 tage = tage_from_list(tcd->tcd_pages.next);
212                 tage->used = 0;
213                 tage_to_tail(tage, &tcd->tcd_pages);
214         }
215         return tage;
216 }
217
218 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
219                        const char *file, const char *fn, const int line,
220                        const char *format1, va_list args,
221                        const char *format2, ...)                       
222 {
223         struct trace_cpu_data   *tcd = NULL;
224         struct ptldebug_header   header;
225         struct trace_page       *tage;
226         /* string_buf is used only if tcd != NULL, and is always set then */
227         char                    *string_buf = NULL;
228         char                    *debug_buf;
229         int                      known_size;
230         int                      needed = 85; /* average message length */
231         int                      max_nob;
232         va_list                  ap;
233         int                      depth;
234         int                      i;
235         int                      remain;
236
237         if (strchr(file, '/'))
238                 file = strrchr(file, '/') + 1;
239
240
241         set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
242
243         tcd = trace_get_tcd();
244         if (tcd == NULL)                /* arch may not log in IRQ context */
245                 goto console;
246
247         if (tcd->tcd_shutting_down) {
248                 trace_put_tcd(tcd);
249                 tcd = NULL;
250                 goto console;
251         }
252
253         depth = __current_nesting_level();
254         known_size = strlen(file) + 1 + depth;
255         if (fn)
256                 known_size += strlen(fn) + 1;
257
258         if (libcfs_debug_binary)
259                 known_size += sizeof(header);
260
261         /*/
262          * '2' used because vsnprintf return real size required for output
263          * _without_ terminating NULL.
264          * if needed is to small for this format.
265          */
266         for (i=0;i<2;i++) {
267                 tage = trace_get_tage(tcd, needed + known_size + 1);
268                 if (tage == NULL) {
269                         if (needed + known_size > CFS_PAGE_SIZE)
270                                 mask |= D_ERROR;
271
272                         trace_put_tcd(tcd);
273                         tcd = NULL;
274                         goto console;
275                 }
276
277                 string_buf = (char *)cfs_page_address(tage->page)+tage->used+known_size;
278
279                 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
280                 if (max_nob <= 0) {
281                         printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
282                         mask |= D_ERROR;
283                         trace_put_tcd(tcd);
284                         tcd = NULL;
285                         goto console;
286                 }
287
288                 needed = 0;
289                 if (format1) {
290                         va_copy(ap, args);
291                         needed = vsnprintf(string_buf, max_nob, format1, ap);
292                         va_end(ap);
293                 }
294                 
295
296                 if (format2) {
297                         remain = max_nob - needed;
298                         if (remain < 0)
299                                 remain = 0;
300                 
301                         va_start(ap, format2);
302                         needed += vsnprintf(string_buf+needed, remain, format2, ap);
303                         va_end(ap);
304                 }
305
306                 if (needed < max_nob) /* well. printing ok.. */
307                         break;
308         }
309         
310         if (*(string_buf+needed-1) != '\n')
311                 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
312                        file, line, fn);
313         
314         header.ph_len = known_size + needed;
315         debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
316
317         if (libcfs_debug_binary) {
318                 memcpy(debug_buf, &header, sizeof(header));
319                 tage->used += sizeof(header);
320                 debug_buf += sizeof(header);
321         }
322
323         /* indent message according to the nesting level */
324         while (depth-- > 0) {
325                 *(debug_buf++) = '.';
326                 ++ tage->used;
327         }
328
329         strcpy(debug_buf, file);
330         tage->used += strlen(file) + 1;
331         debug_buf += strlen(file) + 1;
332
333         if (fn) {
334                 strcpy(debug_buf, fn);
335                 tage->used += strlen(fn) + 1;
336                 debug_buf += strlen(fn) + 1;
337         }
338
339         __LASSERT(debug_buf == string_buf);
340
341         tage->used += needed;
342         __LASSERT (tage->used <= CFS_PAGE_SIZE);
343
344 console:
345         if (!((mask & D_CANTMASK) != 0 || (mask & libcfs_printk) != 0)) {
346                 /* no console output requested */
347                 if (tcd != NULL)
348                         trace_put_tcd(tcd);
349                 return 1;
350         }
351
352         if (cdls != NULL) {
353                 cfs_time_t      t = cdls->cdls_next +
354                                     cfs_time_seconds(CDEBUG_MAX_LIMIT + 10);
355                 cfs_duration_t  dmax = cfs_time_seconds(CDEBUG_MAX_LIMIT);
356
357                 if (libcfs_console_ratelimit &&
358                     cdls->cdls_next != 0 &&     /* not first time ever */
359                     !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
360                         /* skipping a console message */
361                         cdls->cdls_count++;
362                         if (tcd != NULL)
363                                 trace_put_tcd(tcd);
364                         return 1;
365                 }
366
367                 if (cfs_time_after(cfs_time_current(), t)) {
368                         /* last timeout was a long time ago */
369                         cdls->cdls_delay /= 8;
370                 } else {
371                         cdls->cdls_delay *= 2;
372
373                         if (cdls->cdls_delay < CFS_TICK)
374                                 cdls->cdls_delay = CFS_TICK;
375                         else if (cdls->cdls_delay > dmax)
376                                 cdls->cdls_delay = dmax;
377                 }
378
379                 /* ensure cdls_next is never zero after it's been seen */
380                 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
381         }
382
383         if (tcd != NULL) {
384                 print_to_console(&header, mask, string_buf, needed, file, fn);
385                 trace_put_tcd(tcd);
386         } else {
387                 string_buf = trace_get_console_buffer();
388
389                 needed = 0;
390                 if (format1 != NULL) {
391                         va_copy(ap, args);
392                         needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
393                         va_end(ap);
394                 }
395                 if (format2 != NULL) {
396                         remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
397                         if (remain > 0) {
398                                 va_start(ap, format2);
399                                 needed += vsnprintf(string_buf+needed, remain, format2, ap);
400                                 va_end(ap);
401                         }
402                 }
403                 print_to_console(&header, mask,
404                                  string_buf, needed, file, fn);
405
406                 trace_put_console_buffer(string_buf);
407         }
408
409         if (cdls != NULL && cdls->cdls_count != 0) {
410                 string_buf = trace_get_console_buffer();
411
412                 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
413                          "Skipped %d previous similar message%s\n",
414                          cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
415
416                 print_to_console(&header, mask,
417                                  string_buf, needed, file, fn);
418
419                 trace_put_console_buffer(string_buf);
420                 cdls->cdls_count = 0;
421         }
422
423         return 0;
424 }
425 EXPORT_SYMBOL(libcfs_debug_vmsg2);
426
427 void
428 libcfs_assertion_failed(const char *expr, const char *file,
429                         const char *func, const int line)
430 {
431         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
432                          "ASSERTION(%s) failed\n", expr);
433         LBUG();
434 }
435 EXPORT_SYMBOL(libcfs_assertion_failed);
436
437 void
438 trace_assertion_failed(const char *str,
439                        const char *fn, const char *file, int line)
440 {
441         struct ptldebug_header hdr;
442
443         libcfs_panic_in_progress = 1;
444         libcfs_catastrophe = 1;
445         mb();
446
447         set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
448                             CDEBUG_STACK());
449
450         print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
451
452         LIBCFS_PANIC("Lustre debug assertion failure\n");
453
454         /* not reached */
455 }
456
457 static void
458 panic_collect_pages(struct page_collection *pc)
459 {
460         /* Do the collect_pages job on a single CPU: assumes that all other
461          * CPUs have been stopped during a panic.  If this isn't true for some
462          * arch, this will have to be implemented separately in each arch.  */
463         int                    i;
464         struct trace_cpu_data *tcd;
465
466         CFS_INIT_LIST_HEAD(&pc->pc_pages);
467
468         for (i = 0; i < NR_CPUS; i++) {
469                 tcd = &trace_data[i].tcd;
470
471                 list_splice(&tcd->tcd_pages, &pc->pc_pages);
472                 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
473                 tcd->tcd_cur_pages = 0;
474
475                 if (pc->pc_want_daemon_pages) {
476                         list_splice(&tcd->tcd_daemon_pages, &pc->pc_pages);
477                         CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
478                         tcd->tcd_cur_daemon_pages = 0;
479                 }
480         }
481 }
482
483 static void collect_pages_on_cpu(void *info)
484 {
485         struct trace_cpu_data *tcd;
486         struct page_collection *pc = info;
487
488         tcd = trace_get_tcd();
489         __LASSERT (tcd != NULL);
490
491         spin_lock(&pc->pc_lock);
492         list_splice(&tcd->tcd_pages, &pc->pc_pages);
493         CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
494         tcd->tcd_cur_pages = 0;
495         if (pc->pc_want_daemon_pages) {
496                 list_splice(&tcd->tcd_daemon_pages, &pc->pc_pages);
497                 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
498                 tcd->tcd_cur_daemon_pages = 0;
499         }
500         spin_unlock(&pc->pc_lock);
501
502         trace_put_tcd(tcd);
503 }
504
505 static void collect_pages(struct page_collection *pc)
506 {
507         CFS_INIT_LIST_HEAD(&pc->pc_pages);
508
509         if (libcfs_panic_in_progress)
510                 panic_collect_pages(pc);
511         else
512                 trace_call_on_all_cpus(collect_pages_on_cpu, pc);
513 }
514
515 static void put_pages_back_on_cpu(void *info)
516 {
517         struct page_collection *pc = info;
518         struct trace_cpu_data *tcd;
519         struct list_head *cur_head;
520         struct trace_page *tage;
521         struct trace_page *tmp;
522
523         tcd = trace_get_tcd();
524         __LASSERT (tcd != NULL);
525
526         cur_head = tcd->tcd_pages.next;
527
528         spin_lock(&pc->pc_lock);
529         list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
530
531                 __LASSERT_TAGE_INVARIANT(tage);
532
533                 if (tage->cpu != smp_processor_id())
534                         continue;
535
536                 tage_to_tail(tage, cur_head);
537                 tcd->tcd_cur_pages++;
538         }
539         spin_unlock(&pc->pc_lock);
540
541         trace_put_tcd(tcd);
542 }
543
544 static void put_pages_back(struct page_collection *pc)
545 {
546         if (!libcfs_panic_in_progress)
547                 trace_call_on_all_cpus(put_pages_back_on_cpu, pc);
548 }
549
550 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
551  * we have a good amount of data at all times for dumping during an LBUG, even
552  * if we have been steadily writing (and otherwise discarding) pages via the
553  * debug daemon. */
554 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
555                                          struct trace_cpu_data *tcd)
556 {
557         struct trace_page *tage;
558         struct trace_page *tmp;
559
560         spin_lock(&pc->pc_lock);
561         list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
562
563                 __LASSERT_TAGE_INVARIANT(tage);
564
565                 if (tage->cpu != smp_processor_id())
566                         continue;
567
568                 tage_to_tail(tage, &tcd->tcd_daemon_pages);
569                 tcd->tcd_cur_daemon_pages++;
570
571                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
572                         struct trace_page *victim;
573
574                         __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
575                         victim = tage_from_list(tcd->tcd_daemon_pages.next);
576
577                         __LASSERT_TAGE_INVARIANT(victim);
578
579                         list_del(&victim->linkage);
580                         tage_free(victim);
581                         tcd->tcd_cur_daemon_pages--;
582                 }
583         }
584         spin_unlock(&pc->pc_lock);
585 }
586
587 static void put_pages_on_daemon_list_on_cpu(void *info)
588 {
589         struct trace_cpu_data *tcd;
590
591         tcd = trace_get_tcd();
592         __LASSERT (tcd != NULL);
593
594         put_pages_on_tcd_daemon_list(info, tcd);
595
596         trace_put_tcd(tcd);
597 }
598
599 static void put_pages_on_daemon_list(struct page_collection *pc)
600 {
601         trace_call_on_all_cpus(put_pages_on_daemon_list_on_cpu, pc);
602 }
603
604 void trace_debug_print(void)
605 {
606         struct page_collection pc;
607         struct trace_page *tage;
608         struct trace_page *tmp;
609
610         spin_lock_init(&pc.pc_lock);
611
612         pc.pc_want_daemon_pages = 1;
613         collect_pages(&pc);
614         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
615                 char *p, *file, *fn;
616                 cfs_page_t *page;
617
618                 __LASSERT_TAGE_INVARIANT(tage);
619
620                 page = tage->page;
621                 p = cfs_page_address(page);
622                 while (p < ((char *)cfs_page_address(page) + tage->used)) {
623                         struct ptldebug_header *hdr;
624                         int len;
625                         hdr = (void *)p;
626                         p += sizeof(*hdr);
627                         file = p;
628                         p += strlen(file) + 1;
629                         fn = p;
630                         p += strlen(fn) + 1;
631                         len = hdr->ph_len - (p - (char *)hdr);
632
633                         print_to_console(hdr, D_EMERG, p, len, file, fn);
634
635                         p += len;
636                 }
637
638                 list_del(&tage->linkage);
639                 tage_free(tage);
640         }
641 }
642
643 int tracefile_dump_all_pages(char *filename)
644 {
645         struct page_collection pc;
646         cfs_file_t *filp;
647         struct trace_page *tage;
648         struct trace_page *tmp;
649         int rc;
650
651         CFS_DECL_MMSPACE;
652
653         tracefile_write_lock();
654
655         filp = cfs_filp_open(filename,
656                              O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
657         if (!filp) {
658                 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
659                        filename, rc);
660                 goto out;
661         }
662
663         spin_lock_init(&pc.pc_lock);
664         pc.pc_want_daemon_pages = 1;
665         collect_pages(&pc);
666         if (list_empty(&pc.pc_pages)) {
667                 rc = 0;
668                 goto close;
669         }
670
671         /* ok, for now, just write the pages.  in the future we'll be building
672          * iobufs with the pages and calling generic_direct_IO */
673         CFS_MMSPACE_OPEN;
674         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
675
676                 __LASSERT_TAGE_INVARIANT(tage);
677
678                 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
679                                     tage->used, cfs_filp_poff(filp));
680                 if (rc != (int)tage->used) {
681                         printk(KERN_WARNING "wanted to write %u but wrote "
682                                "%d\n", tage->used, rc);
683                         put_pages_back(&pc);
684                         __LASSERT(list_empty(&pc.pc_pages));
685                         break;
686                 }
687                 list_del(&tage->linkage);
688                 tage_free(tage);
689         }
690         CFS_MMSPACE_CLOSE;
691         rc = cfs_filp_fsync(filp);
692         if (rc)
693                 printk(KERN_ERR "sync returns %d\n", rc);
694  close:
695         cfs_filp_close(filp);
696  out:
697         tracefile_write_unlock();
698         return rc;
699 }
700
701 void trace_flush_pages(void)
702 {
703         struct page_collection pc;
704         struct trace_page *tage;
705         struct trace_page *tmp;
706
707         spin_lock_init(&pc.pc_lock);
708
709         pc.pc_want_daemon_pages = 1;
710         collect_pages(&pc);
711         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
712
713                 __LASSERT_TAGE_INVARIANT(tage);
714
715                 list_del(&tage->linkage);
716                 tage_free(tage);
717         }
718 }
719
720 int trace_dk(struct file *file, const char *buffer, unsigned long count,
721              void *data)
722 {
723         char *name;
724         unsigned long off;
725         int rc;
726
727         name = cfs_alloc(count + 1, CFS_ALLOC_STD);
728         if (name == NULL)
729                 return -ENOMEM;
730
731         if (copy_from_user((void *)name, (void *)buffer, count)) {
732                 rc = -EFAULT;
733                 goto out;
734         }
735
736 #if !defined(__WINNT__)
737         if (name[0] != '/') {
738                 rc = -EINVAL;
739                 goto out;
740         }
741 #endif
742
743         /* be nice and strip out trailing '\n' */
744         for (off = count ; off > 2 && isspace(name[off - 1]); off--)
745                 ;
746
747         name[off] = '\0';
748         rc = tracefile_dump_all_pages(name);
749 out:
750         if (name)
751                 cfs_free(name);
752         return count;
753 }
754 EXPORT_SYMBOL(trace_dk);
755
756 static int tracefiled(void *arg)
757 {
758         struct page_collection pc;
759         struct tracefiled_ctl *tctl = arg;
760         struct trace_page *tage;
761         struct trace_page *tmp;
762         struct ptldebug_header *hdr;
763         cfs_file_t *filp;
764         int rc;
765
766         CFS_DECL_MMSPACE;
767
768         /* we're started late enough that we pick up init's fs context */
769         /* this is so broken in uml?  what on earth is going on? */
770         cfs_daemonize("ktracefiled");
771
772         spin_lock_init(&pc.pc_lock);
773         complete(&tctl->tctl_start);
774
775         while (1) {
776                 cfs_waitlink_t __wait;
777
778                 cfs_waitlink_init(&__wait);
779                 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
780                 set_current_state(TASK_INTERRUPTIBLE);
781                 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
782                                     cfs_time_seconds(1));
783                 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
784
785                 if (atomic_read(&tctl->tctl_shutdown))
786                         break;
787
788                 pc.pc_want_daemon_pages = 0;
789                 collect_pages(&pc);
790                 if (list_empty(&pc.pc_pages))
791                         continue;
792
793                 filp = NULL;
794                 tracefile_read_lock();
795                 if (tracefile != NULL) {
796                         filp = cfs_filp_open(tracefile,
797                                              O_CREAT | O_RDWR | O_LARGEFILE,
798                                              0600, &rc);
799                         if (!(filp))
800                                 printk("couldn't open %s: %d\n", tracefile, rc);
801                 }
802                 tracefile_read_unlock();
803                 if (filp == NULL) {
804                         put_pages_on_daemon_list(&pc);
805                         __LASSERT(list_empty(&pc.pc_pages));
806                         continue;
807                 }
808
809                 CFS_MMSPACE_OPEN;
810
811                 /* mark the first header, so we can sort in chunks */
812                 tage = tage_from_list(pc.pc_pages.next);
813                 __LASSERT_TAGE_INVARIANT(tage);
814
815                 hdr = cfs_page_address(tage->page);
816                 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
817
818                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
819                         static loff_t f_pos;
820
821                         __LASSERT_TAGE_INVARIANT(tage);
822
823                         if (f_pos >= (off_t)tracefile_size)
824                                 f_pos = 0;
825                         else if (f_pos > cfs_filp_size(filp))
826                                 f_pos = cfs_filp_size(filp);
827
828                         rc = cfs_filp_write(filp, cfs_page_address(tage->page),
829                                             tage->used, &f_pos);
830                         if (rc != (int)tage->used) {
831                                 printk(KERN_WARNING "wanted to write %u but "
832                                        "wrote %d\n", tage->used, rc);
833                                 put_pages_back(&pc);
834                                 __LASSERT(list_empty(&pc.pc_pages));
835                         }
836                 }
837                 CFS_MMSPACE_CLOSE;
838
839                 cfs_filp_close(filp);
840                 put_pages_on_daemon_list(&pc);
841                 __LASSERT(list_empty(&pc.pc_pages));
842         }
843         complete(&tctl->tctl_stop);
844         return 0;
845 }
846
847 int trace_start_thread(void)
848 {
849         struct tracefiled_ctl *tctl = &trace_tctl;
850         int rc = 0;
851
852         mutex_down(&trace_thread_sem);
853         if (thread_running)
854                 goto out;
855
856         init_completion(&tctl->tctl_start);
857         init_completion(&tctl->tctl_stop);
858         cfs_waitq_init(&tctl->tctl_waitq);
859         atomic_set(&tctl->tctl_shutdown, 0);
860
861         if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
862                 rc = -ECHILD;
863                 goto out;
864         }
865
866         wait_for_completion(&tctl->tctl_start);
867         thread_running = 1;
868 out:
869         mutex_up(&trace_thread_sem);
870         return rc;
871 }
872
873 void trace_stop_thread(void)
874 {
875         struct tracefiled_ctl *tctl = &trace_tctl;
876
877         mutex_down(&trace_thread_sem);
878         if (thread_running) {
879                 printk(KERN_INFO "Shutting down debug daemon thread...\n");
880                 atomic_set(&tctl->tctl_shutdown, 1);
881                 wait_for_completion(&tctl->tctl_stop);
882                 thread_running = 0;
883         }
884         mutex_up(&trace_thread_sem);
885 }
886
887 int tracefile_init(void)
888 {
889         struct trace_cpu_data *tcd;
890         int                    i;
891         int                    rc;
892
893         rc = tracefile_init_arch();
894         if (rc != 0)
895                 return rc;
896
897         for (i = 0; i < NR_CPUS; i++) {
898                 tcd = &trace_data[i].tcd;
899                 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
900                 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
901                 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
902                 tcd->tcd_cur_pages = 0;
903                 tcd->tcd_cur_stock_pages = 0;
904                 tcd->tcd_cur_daemon_pages = 0;
905                 tcd->tcd_max_pages = TCD_MAX_PAGES;
906                 tcd->tcd_shutting_down = 0;
907                 tcd->tcd_cpu = i;
908         }
909
910         return 0;
911 }
912
913 static void trace_cleanup_on_cpu(void *info)
914 {
915         struct trace_cpu_data *tcd;
916         struct trace_page *tage;
917         struct trace_page *tmp;
918
919         tcd = trace_get_tcd();
920         __LASSERT (tcd != NULL);
921
922         tcd->tcd_shutting_down = 1;
923
924         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
925                 __LASSERT_TAGE_INVARIANT(tage);
926
927                 list_del(&tage->linkage);
928                 tage_free(tage);
929         }
930         tcd->tcd_cur_pages = 0;
931
932         trace_put_tcd(tcd);
933 }
934
935 static void trace_cleanup(void)
936 {
937         struct page_collection pc;
938
939         CFS_INIT_LIST_HEAD(&pc.pc_pages);
940         spin_lock_init(&pc.pc_lock);
941
942         trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
943
944         tracefile_fini_arch();
945 }
946
947 void tracefile_exit(void)
948 {
949         trace_stop_thread();
950         trace_cleanup();
951 }