Whamcloud - gitweb
Branch HEAD
[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[TRACEFILE_NAME_SIZE];
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                 if (rc != -EEXIST)
659                         printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
660                                filename, rc);
661                 goto out;
662         }
663
664         spin_lock_init(&pc.pc_lock);
665         pc.pc_want_daemon_pages = 1;
666         collect_pages(&pc);
667         if (list_empty(&pc.pc_pages)) {
668                 rc = 0;
669                 goto close;
670         }
671
672         /* ok, for now, just write the pages.  in the future we'll be building
673          * iobufs with the pages and calling generic_direct_IO */
674         CFS_MMSPACE_OPEN;
675         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
676
677                 __LASSERT_TAGE_INVARIANT(tage);
678
679                 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
680                                     tage->used, cfs_filp_poff(filp));
681                 if (rc != (int)tage->used) {
682                         printk(KERN_WARNING "wanted to write %u but wrote "
683                                "%d\n", tage->used, rc);
684                         put_pages_back(&pc);
685                         __LASSERT(list_empty(&pc.pc_pages));
686                         break;
687                 }
688                 list_del(&tage->linkage);
689                 tage_free(tage);
690         }
691         CFS_MMSPACE_CLOSE;
692         rc = cfs_filp_fsync(filp);
693         if (rc)
694                 printk(KERN_ERR "sync returns %d\n", rc);
695  close:
696         cfs_filp_close(filp);
697  out:
698         tracefile_write_unlock();
699         return rc;
700 }
701
702 void trace_flush_pages(void)
703 {
704         struct page_collection pc;
705         struct trace_page *tage;
706         struct trace_page *tmp;
707
708         spin_lock_init(&pc.pc_lock);
709
710         pc.pc_want_daemon_pages = 1;
711         collect_pages(&pc);
712         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
713
714                 __LASSERT_TAGE_INVARIANT(tage);
715
716                 list_del(&tage->linkage);
717                 tage_free(tage);
718         }
719 }
720
721 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
722                         const char *usr_buffer, int usr_buffer_nob)
723 {
724         int    nob;
725         
726         if (usr_buffer_nob > knl_buffer_nob)
727                 return -EOVERFLOW;
728         
729         if (copy_from_user((void *)knl_buffer, 
730                            (void *)usr_buffer, usr_buffer_nob))
731                 return -EFAULT;
732
733         nob = strnlen(knl_buffer, usr_buffer_nob);
734         while (nob-- >= 0)                      /* strip trailing whitespace */
735                 if (!isspace(knl_buffer[nob]))
736                         break;
737
738         if (nob < 0)                            /* empty string */
739                 return -EINVAL;
740
741         if (nob == knl_buffer_nob)              /* no space to terminate */
742                 return -EOVERFLOW;
743
744         knl_buffer[nob + 1] = 0;                /* terminate */
745         return 0;
746 }
747
748 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
749                          const char *knl_buffer, char *append)
750 {
751         /* NB if 'append' != NULL, it's a single character to append to the
752          * copied out string - usually "\n", for /proc entries and "" (i.e. a
753          * terminating zero byte) for sysctl entries */
754         int   nob = strlen(knl_buffer);
755         
756         if (nob > usr_buffer_nob)
757                 nob = usr_buffer_nob;
758         
759         if (copy_to_user(usr_buffer, knl_buffer, nob))
760                 return -EFAULT;
761         
762         if (append != NULL && nob < usr_buffer_nob) {
763                 if (copy_to_user(usr_buffer + nob, append, 1))
764                         return -EFAULT;
765                 
766                 nob++;
767         }
768
769         return nob;
770 }
771
772 int trace_allocate_string_buffer(char **str, int nob)
773 {
774         if (nob > 2 * CFS_PAGE_SIZE)            /* string must be "sensible" */
775                 return -EINVAL;
776         
777         *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
778         if (*str == NULL)
779                 return -ENOMEM;
780
781         return 0;
782 }
783
784 void trace_free_string_buffer(char *str, int nob)
785 {
786         cfs_free(str);
787 }
788
789 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
790 {
791         char         *str;
792         int           rc;
793
794         rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
795         if (rc != 0)
796                 return rc;
797
798         rc = trace_copyin_string(str, usr_str_nob + 1,
799                                  usr_str, usr_str_nob);
800         if (rc != 0)
801                 goto out;
802
803 #if !defined(__WINNT__)
804         if (str[0] != '/') {
805                 rc = -EINVAL;
806                 goto out;
807         }
808 #endif
809         rc = tracefile_dump_all_pages(str);
810 out:
811         trace_free_string_buffer(str, usr_str_nob + 1);
812         return rc;
813 }
814
815 int trace_daemon_command(char *str)
816 {
817         int       rc = 0;
818         
819         tracefile_write_lock();
820
821         if (strcmp(str, "stop") == 0) {
822                 trace_stop_thread();
823                 memset(tracefile, 0, sizeof(tracefile));
824
825         } else if (strncmp(str, "size=", 5) == 0) {
826                 tracefile_size = simple_strtoul(str + 5, NULL, 0);
827                 if (tracefile_size < 10 || tracefile_size > 20480)
828                         tracefile_size = TRACEFILE_SIZE;
829                 else
830                         tracefile_size <<= 20;
831
832         } else if (strlen(str) >= sizeof(tracefile)) {
833                 rc = -ENAMETOOLONG;
834 #ifndef __WINNT__
835         } else if (str[0] != '/') {
836                 rc = -EINVAL;
837 #endif
838         } else {
839                 strcpy(tracefile, str);
840
841                 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
842                        "to %s (%lukB max)\n", tracefile,
843                        (long)(tracefile_size >> 10));
844
845                 trace_start_thread();
846         }
847
848         tracefile_write_unlock();
849         return rc;
850 }
851
852 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
853 {
854         char *str;
855         int   rc;
856
857         rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
858         if (rc != 0)
859                 return rc;
860
861         rc = trace_copyin_string(str, usr_str_nob + 1,
862                                  usr_str, usr_str_nob);
863         if (rc == 0)
864                 rc = trace_daemon_command(str);
865
866         trace_free_string_buffer(str, usr_str_nob + 1);
867         return rc;
868 }
869
870 int trace_set_debug_mb(int mb)
871 {
872         int i;
873         int limit = trace_max_debug_mb();
874         
875         if (mb <= 0)
876                 return -EINVAL;
877
878         if (mb > limit) {
879                 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to "
880                        "%dMB - limit is %d\n", mb, limit);
881                 return -EINVAL;
882         }
883
884         mb /= smp_num_cpus;
885
886         tracefile_write_lock();
887
888         for (i = 0; i < NR_CPUS; i++) {
889                 struct trace_cpu_data *tcd = &trace_data[i].tcd;
890
891                 tcd->tcd_max_pages = mb << (20 - CFS_PAGE_SHIFT);
892         }
893
894         tracefile_write_unlock();
895
896         return 0;
897 }
898
899 int trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
900 {
901         char     str[32];
902         int      rc;
903
904         rc = trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
905         if (rc < 0)
906                 return rc;
907
908         return trace_set_debug_mb(simple_strtoul(str, NULL, 0));
909 }
910
911 int trace_get_debug_mb(void)
912 {
913         int i;
914         int total_pages = 0;
915         
916         tracefile_read_lock();
917
918         for (i = 0; i < NR_CPUS; i++) {
919                 struct trace_cpu_data *tcd = &trace_data[i].tcd;
920
921                 total_pages += tcd->tcd_max_pages;
922         }
923
924         tracefile_read_unlock();
925
926         return total_pages >> (20 - CFS_PAGE_SHIFT);
927 }
928
929 static int tracefiled(void *arg)
930 {
931         struct page_collection pc;
932         struct tracefiled_ctl *tctl = arg;
933         struct trace_page *tage;
934         struct trace_page *tmp;
935         struct ptldebug_header *hdr;
936         cfs_file_t *filp;
937         int rc;
938
939         CFS_DECL_MMSPACE;
940
941         /* we're started late enough that we pick up init's fs context */
942         /* this is so broken in uml?  what on earth is going on? */
943         cfs_daemonize("ktracefiled");
944
945         spin_lock_init(&pc.pc_lock);
946         complete(&tctl->tctl_start);
947
948         while (1) {
949                 cfs_waitlink_t __wait;
950
951                 cfs_waitlink_init(&__wait);
952                 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
953                 set_current_state(TASK_INTERRUPTIBLE);
954                 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
955                                     cfs_time_seconds(1));
956                 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
957
958                 if (atomic_read(&tctl->tctl_shutdown))
959                         break;
960
961                 pc.pc_want_daemon_pages = 0;
962                 collect_pages(&pc);
963                 if (list_empty(&pc.pc_pages))
964                         continue;
965
966                 filp = NULL;
967                 tracefile_read_lock();
968                 if (tracefile[0] != 0) {
969                         filp = cfs_filp_open(tracefile,
970                                              O_CREAT | O_RDWR | O_LARGEFILE,
971                                              0600, &rc);
972                         if (!(filp))
973                                 printk(KERN_WARNING "couldn't open %s: %d\n",
974                                        tracefile, rc);
975                 }
976                 tracefile_read_unlock();
977                 if (filp == NULL) {
978                         put_pages_on_daemon_list(&pc);
979                         __LASSERT(list_empty(&pc.pc_pages));
980                         continue;
981                 }
982
983                 CFS_MMSPACE_OPEN;
984
985                 /* mark the first header, so we can sort in chunks */
986                 tage = tage_from_list(pc.pc_pages.next);
987                 __LASSERT_TAGE_INVARIANT(tage);
988
989                 hdr = cfs_page_address(tage->page);
990                 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
991
992                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
993                         static loff_t f_pos;
994
995                         __LASSERT_TAGE_INVARIANT(tage);
996
997                         if (f_pos >= (off_t)tracefile_size)
998                                 f_pos = 0;
999                         else if (f_pos > cfs_filp_size(filp))
1000                                 f_pos = cfs_filp_size(filp);
1001
1002                         rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1003                                             tage->used, &f_pos);
1004                         if (rc != (int)tage->used) {
1005                                 printk(KERN_WARNING "wanted to write %u but "
1006                                        "wrote %d\n", tage->used, rc);
1007                                 put_pages_back(&pc);
1008                                 __LASSERT(list_empty(&pc.pc_pages));
1009                         }
1010                 }
1011                 CFS_MMSPACE_CLOSE;
1012
1013                 cfs_filp_close(filp);
1014                 put_pages_on_daemon_list(&pc);
1015                 __LASSERT(list_empty(&pc.pc_pages));
1016         }
1017         complete(&tctl->tctl_stop);
1018         return 0;
1019 }
1020
1021 int trace_start_thread(void)
1022 {
1023         struct tracefiled_ctl *tctl = &trace_tctl;
1024         int rc = 0;
1025
1026         mutex_down(&trace_thread_sem);
1027         if (thread_running)
1028                 goto out;
1029
1030         init_completion(&tctl->tctl_start);
1031         init_completion(&tctl->tctl_stop);
1032         cfs_waitq_init(&tctl->tctl_waitq);
1033         atomic_set(&tctl->tctl_shutdown, 0);
1034
1035         if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1036                 rc = -ECHILD;
1037                 goto out;
1038         }
1039
1040         wait_for_completion(&tctl->tctl_start);
1041         thread_running = 1;
1042 out:
1043         mutex_up(&trace_thread_sem);
1044         return rc;
1045 }
1046
1047 void trace_stop_thread(void)
1048 {
1049         struct tracefiled_ctl *tctl = &trace_tctl;
1050
1051         mutex_down(&trace_thread_sem);
1052         if (thread_running) {
1053                 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1054                 atomic_set(&tctl->tctl_shutdown, 1);
1055                 wait_for_completion(&tctl->tctl_stop);
1056                 thread_running = 0;
1057         }
1058         mutex_up(&trace_thread_sem);
1059 }
1060
1061 int tracefile_init(void)
1062 {
1063         struct trace_cpu_data *tcd;
1064         int                    i;
1065         int                    rc;
1066
1067         rc = tracefile_init_arch();
1068         if (rc != 0)
1069                 return rc;
1070
1071         for (i = 0; i < NR_CPUS; i++) {
1072                 tcd = &trace_data[i].tcd;
1073                 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1074                 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1075                 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1076                 tcd->tcd_cur_pages = 0;
1077                 tcd->tcd_cur_stock_pages = 0;
1078                 tcd->tcd_cur_daemon_pages = 0;
1079                 tcd->tcd_max_pages = TCD_MAX_PAGES;
1080                 tcd->tcd_shutting_down = 0;
1081                 tcd->tcd_cpu = i;
1082         }
1083
1084         return 0;
1085 }
1086
1087 static void trace_cleanup_on_cpu(void *info)
1088 {
1089         struct trace_cpu_data *tcd;
1090         struct trace_page *tage;
1091         struct trace_page *tmp;
1092
1093         tcd = trace_get_tcd();
1094         __LASSERT (tcd != NULL);
1095
1096         tcd->tcd_shutting_down = 1;
1097
1098         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1099                 __LASSERT_TAGE_INVARIANT(tage);
1100
1101                 list_del(&tage->linkage);
1102                 tage_free(tage);
1103         }
1104         tcd->tcd_cur_pages = 0;
1105
1106         trace_put_tcd(tcd);
1107 }
1108
1109 static void trace_cleanup(void)
1110 {
1111         struct page_collection pc;
1112
1113         CFS_INIT_LIST_HEAD(&pc.pc_pages);
1114         spin_lock_init(&pc.pc_lock);
1115
1116         trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
1117
1118         tracefile_fini_arch();
1119 }
1120
1121 void tracefile_exit(void)
1122 {
1123         trace_stop_thread();
1124         trace_cleanup();
1125 }