Whamcloud - gitweb
fac840b6fb995b6670eee8123bd9cb68374c0c47
[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  * 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  2008 Sun Microsystems, Inc. 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  * lnet/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/kp30.h>
48 #include <libcfs/libcfs.h>
49
50 /* XXX move things up to the top, comment */
51 union trace_data_union (*trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
52
53 char tracefile[TRACEFILE_NAME_SIZE];
54 long long tracefile_size = TRACEFILE_SIZE;
55 static struct tracefiled_ctl trace_tctl;
56 struct semaphore trace_thread_sem;
57 static int thread_running = 0;
58
59 atomic_t tage_allocated = ATOMIC_INIT(0);
60
61 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
62                                          struct trace_cpu_data *tcd);
63
64 static inline struct trace_page *tage_from_list(struct list_head *list)
65 {
66         return list_entry(list, struct trace_page, linkage);
67 }
68
69 static struct trace_page *tage_alloc(int gfp)
70 {
71         cfs_page_t        *page;
72         struct trace_page *tage;
73
74         /*
75          * Don't spam console with allocation failures: they will be reported
76          * by upper layer anyway.
77          */
78         gfp |= CFS_ALLOC_NOWARN;
79         page = cfs_alloc_page(gfp);
80         if (page == NULL)
81                 return NULL;
82
83         tage = cfs_alloc(sizeof(*tage), gfp);
84         if (tage == NULL) {
85                 cfs_free_page(page);
86                 return NULL;
87         }
88
89         tage->page = page;
90         atomic_inc(&tage_allocated);
91         return tage;
92 }
93
94 static void tage_free(struct trace_page *tage)
95 {
96         __LASSERT(tage != NULL);
97         __LASSERT(tage->page != NULL);
98
99         cfs_free_page(tage->page);
100         cfs_free(tage);
101         atomic_dec(&tage_allocated);
102 }
103
104 static void tage_to_tail(struct trace_page *tage, struct list_head *queue)
105 {
106         __LASSERT(tage != NULL);
107         __LASSERT(queue != NULL);
108
109         list_move_tail(&tage->linkage, queue);
110 }
111
112 int trace_refill_stock(struct trace_cpu_data *tcd, int gfp,
113                        struct list_head *stock)
114 {
115         int i;
116
117         /*
118          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
119          * from here: this will lead to infinite recursion.
120          */
121
122         for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
123                 struct trace_page *tage;
124
125                 tage = tage_alloc(gfp);
126                 if (tage == NULL)
127                         break;
128                 list_add_tail(&tage->linkage, stock);
129         }
130         return i;
131 }
132
133 /* return a page that has 'len' bytes left at the end */
134 static struct trace_page *trace_get_tage_try(struct trace_cpu_data *tcd,
135                                              unsigned long len)
136 {
137         struct trace_page *tage;
138
139         if (tcd->tcd_cur_pages > 0) {
140                 __LASSERT(!list_empty(&tcd->tcd_pages));
141                 tage = tage_from_list(tcd->tcd_pages.prev);
142                 if (tage->used + len <= CFS_PAGE_SIZE)
143                         return tage;
144         }
145
146         if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
147                 if (tcd->tcd_cur_stock_pages > 0) {
148                         tage = tage_from_list(tcd->tcd_stock_pages.prev);
149                         -- tcd->tcd_cur_stock_pages;
150                         list_del_init(&tage->linkage);
151                 } else {
152                         tage = tage_alloc(CFS_ALLOC_ATOMIC);
153                         if (tage == NULL) {
154                                 printk(KERN_WARNING
155                                        "failure to allocate a tage (%ld)\n",
156                                        tcd->tcd_cur_pages);
157                                 return NULL;
158                         }
159                 }
160
161                 tage->used = 0;
162                 tage->cpu = smp_processor_id();
163                 tage->type = tcd->tcd_type;
164                 list_add_tail(&tage->linkage, &tcd->tcd_pages);
165                 tcd->tcd_cur_pages++;
166
167                 if (tcd->tcd_cur_pages > 8 && thread_running) {
168                         struct tracefiled_ctl *tctl = &trace_tctl;
169                         /*
170                          * wake up tracefiled to process some pages.
171                          */
172                         cfs_waitq_signal(&tctl->tctl_waitq);
173                 }
174                 return tage;
175         }
176         return NULL;
177 }
178
179 static void tcd_shrink(struct trace_cpu_data *tcd)
180 {
181         int pgcount = tcd->tcd_cur_pages / 10;
182         struct page_collection pc;
183         struct trace_page *tage;
184         struct trace_page *tmp;
185
186         /*
187          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
188          * from here: this will lead to infinite recursion.
189          */
190
191         if (printk_ratelimit())
192                 printk(KERN_WARNING "debug daemon buffer overflowed; "
193                        "discarding  10%% of pages (%d of %ld)\n",
194                        pgcount + 1, tcd->tcd_cur_pages);
195
196         CFS_INIT_LIST_HEAD(&pc.pc_pages);
197         spin_lock_init(&pc.pc_lock);
198
199         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
200                 if (pgcount-- == 0)
201                         break;
202
203                 list_move_tail(&tage->linkage, &pc.pc_pages);
204                 tcd->tcd_cur_pages--;
205         }
206         put_pages_on_tcd_daemon_list(&pc, tcd);
207 }
208
209 /* return a page that has 'len' bytes left at the end */
210 static struct trace_page *trace_get_tage(struct trace_cpu_data *tcd,
211                                          unsigned long len)
212 {
213         struct trace_page *tage;
214
215         /*
216          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
217          * from here: this will lead to infinite recursion.
218          */
219
220         if (len > CFS_PAGE_SIZE) {
221                 printk(KERN_ERR
222                        "cowardly refusing to write %lu bytes in a page\n", len);
223                 return NULL;
224         }
225
226         tage = trace_get_tage_try(tcd, len);
227         if (tage != NULL)
228                 return tage;
229         if (thread_running)
230                 tcd_shrink(tcd);
231         if (tcd->tcd_cur_pages > 0) {
232                 tage = tage_from_list(tcd->tcd_pages.next);
233                 tage->used = 0;
234                 tage_to_tail(tage, &tcd->tcd_pages);
235         }
236         return tage;
237 }
238
239 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
240                        const char *file, const char *fn, const int line,
241                        const char *format1, va_list args,
242                        const char *format2, ...)                       
243 {
244         struct trace_cpu_data   *tcd = NULL;
245         struct ptldebug_header   header;
246         struct trace_page       *tage;
247         /* string_buf is used only if tcd != NULL, and is always set then */
248         char                    *string_buf = NULL;
249         char                    *debug_buf;
250         int                      known_size;
251         int                      needed = 85; /* average message length */
252         int                      max_nob;
253         va_list                  ap;
254         int                      depth;
255         int                      i;
256         int                      remain;
257
258         if (strchr(file, '/'))
259                 file = strrchr(file, '/') + 1;
260
261
262         set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
263
264         tcd = trace_get_tcd();
265         if (tcd == NULL)                /* arch may not log in IRQ context */
266                 goto console;
267
268         if (tcd->tcd_shutting_down) {
269                 trace_put_tcd(tcd);
270                 tcd = NULL;
271                 goto console;
272         }
273
274         depth = __current_nesting_level();
275         known_size = strlen(file) + 1 + depth;
276         if (fn)
277                 known_size += strlen(fn) + 1;
278
279         if (libcfs_debug_binary)
280                 known_size += sizeof(header);
281
282         /*/
283          * '2' used because vsnprintf return real size required for output
284          * _without_ terminating NULL.
285          * if needed is to small for this format.
286          */
287         for (i=0;i<2;i++) {
288                 tage = trace_get_tage(tcd, needed + known_size + 1);
289                 if (tage == NULL) {
290                         if (needed + known_size > CFS_PAGE_SIZE)
291                                 mask |= D_ERROR;
292
293                         trace_put_tcd(tcd);
294                         tcd = NULL;
295                         goto console;
296                 }
297
298                 string_buf = (char *)cfs_page_address(tage->page)+tage->used+known_size;
299
300                 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
301                 if (max_nob <= 0) {
302                         printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
303                         mask |= D_ERROR;
304                         trace_put_tcd(tcd);
305                         tcd = NULL;
306                         goto console;
307                 }
308
309                 needed = 0;
310                 if (format1) {
311                         va_copy(ap, args);
312                         needed = vsnprintf(string_buf, max_nob, format1, ap);
313                         va_end(ap);
314                 }
315                 
316
317                 if (format2) {
318                         remain = max_nob - needed;
319                         if (remain < 0)
320                                 remain = 0;
321                 
322                         va_start(ap, format2);
323                         needed += vsnprintf(string_buf+needed, remain, format2, ap);
324                         va_end(ap);
325                 }
326
327                 if (needed < max_nob) /* well. printing ok.. */
328                         break;
329         }
330         
331         if (*(string_buf+needed-1) != '\n')
332                 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
333                        file, line, fn);
334         
335         header.ph_len = known_size + needed;
336         debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
337
338         if (libcfs_debug_binary) {
339                 memcpy(debug_buf, &header, sizeof(header));
340                 tage->used += sizeof(header);
341                 debug_buf += sizeof(header);
342         }
343
344         /* indent message according to the nesting level */
345         while (depth-- > 0) {
346                 *(debug_buf++) = '.';
347                 ++ tage->used;
348         }
349
350         strcpy(debug_buf, file);
351         tage->used += strlen(file) + 1;
352         debug_buf += strlen(file) + 1;
353
354         if (fn) {
355                 strcpy(debug_buf, fn);
356                 tage->used += strlen(fn) + 1;
357                 debug_buf += strlen(fn) + 1;
358         }
359
360         __LASSERT(debug_buf == string_buf);
361
362         tage->used += needed;
363         __LASSERT (tage->used <= CFS_PAGE_SIZE);
364
365 console:
366         if ((mask & libcfs_printk) == 0) {
367                 /* no console output requested */
368                 if (tcd != NULL)
369                         trace_put_tcd(tcd);
370                 return 1;
371         }
372
373         if (cdls != NULL) {
374                 if (libcfs_console_ratelimit &&
375                     cdls->cdls_next != 0 &&     /* not first time ever */
376                     !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
377                         /* skipping a console message */
378                         cdls->cdls_count++;
379                         if (tcd != NULL)
380                                 trace_put_tcd(tcd);
381                         return 1;
382                 }
383
384                 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
385                                                        libcfs_console_max_delay
386                                                        + cfs_time_seconds(10))) {
387                         /* last timeout was a long time ago */
388                         cdls->cdls_delay /= libcfs_console_backoff * 4;
389                 } else {
390                         cdls->cdls_delay *= libcfs_console_backoff;
391
392                         if (cdls->cdls_delay < libcfs_console_min_delay)
393                                 cdls->cdls_delay = libcfs_console_min_delay;
394                         else if (cdls->cdls_delay > libcfs_console_max_delay)
395                                 cdls->cdls_delay = libcfs_console_max_delay;
396                 }
397
398                 /* ensure cdls_next is never zero after it's been seen */
399                 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
400         }
401
402         if (tcd != NULL) {
403                 print_to_console(&header, mask, string_buf, needed, file, fn);
404                 trace_put_tcd(tcd);
405         } else {
406                 string_buf = trace_get_console_buffer();
407
408                 needed = 0;
409                 if (format1 != NULL) {
410                         va_copy(ap, args);
411                         needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
412                         va_end(ap);
413                 }
414                 if (format2 != NULL) {
415                         remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
416                         if (remain > 0) {
417                                 va_start(ap, format2);
418                                 needed += vsnprintf(string_buf+needed, remain, format2, ap);
419                                 va_end(ap);
420                         }
421                 }
422                 print_to_console(&header, mask,
423                                  string_buf, needed, file, fn);
424
425                 trace_put_console_buffer(string_buf);
426         }
427
428         if (cdls != NULL && cdls->cdls_count != 0) {
429                 string_buf = trace_get_console_buffer();
430
431                 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
432                          "Skipped %d previous similar message%s\n",
433                          cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
434
435                 print_to_console(&header, mask,
436                                  string_buf, needed, file, fn);
437
438                 trace_put_console_buffer(string_buf);
439                 cdls->cdls_count = 0;
440         }
441
442         return 0;
443 }
444 EXPORT_SYMBOL(libcfs_debug_vmsg2);
445
446 void
447 libcfs_assertion_failed(const char *expr, const char *file,
448                         const char *func, const int line)
449 {
450         libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
451                          "ASSERTION(%s) failed\n", expr);
452         LBUG();
453 }
454 EXPORT_SYMBOL(libcfs_assertion_failed);
455
456 void
457 trace_assertion_failed(const char *str,
458                        const char *fn, const char *file, int line)
459 {
460         struct ptldebug_header hdr;
461
462         libcfs_panic_in_progress = 1;
463         libcfs_catastrophe = 1;
464         mb();
465
466         set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
467                             CDEBUG_STACK());
468
469         print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
470
471         LIBCFS_PANIC("Lustre debug assertion failure\n");
472
473         /* not reached */
474 }
475
476 static void
477 panic_collect_pages(struct page_collection *pc)
478 {
479         /* Do the collect_pages job on a single CPU: assumes that all other
480          * CPUs have been stopped during a panic.  If this isn't true for some
481          * arch, this will have to be implemented separately in each arch.  */
482         int                    i;
483         int                    j;
484         struct trace_cpu_data *tcd;
485
486         CFS_INIT_LIST_HEAD(&pc->pc_pages);
487
488         tcd_for_each(tcd, i, j) {
489                 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
490                 tcd->tcd_cur_pages = 0;
491
492                 if (pc->pc_want_daemon_pages) {
493                         list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
494                         tcd->tcd_cur_daemon_pages = 0;
495                 }
496         }
497 }
498
499 static void collect_pages_on_cpu(void *info)
500 {
501         struct trace_cpu_data *tcd;
502         struct page_collection *pc = info;
503         int i;
504
505         spin_lock(&pc->pc_lock);
506         tcd_for_each_type_lock(tcd, i) {
507                 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
508                 tcd->tcd_cur_pages = 0;
509                 if (pc->pc_want_daemon_pages) {
510                         list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
511                         tcd->tcd_cur_daemon_pages = 0;
512                 }
513         }
514         spin_unlock(&pc->pc_lock);
515 }
516
517 static void collect_pages(struct page_collection *pc)
518 {
519         CFS_INIT_LIST_HEAD(&pc->pc_pages);
520
521         if (libcfs_panic_in_progress)
522                 panic_collect_pages(pc);
523         else
524                 trace_call_on_all_cpus(collect_pages_on_cpu, pc);
525 }
526
527 static void put_pages_back_on_cpu(void *info)
528 {
529         struct page_collection *pc = info;
530         struct trace_cpu_data *tcd;
531         struct list_head *cur_head;
532         struct trace_page *tage;
533         struct trace_page *tmp;
534         int i;
535
536         spin_lock(&pc->pc_lock);
537         tcd_for_each_type_lock(tcd, i) {
538                 cur_head = tcd->tcd_pages.next;
539
540                 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
541
542                         __LASSERT_TAGE_INVARIANT(tage);
543
544                         if (tage->cpu != smp_processor_id() || tage->type != i)
545                                 continue;
546
547                         tage_to_tail(tage, cur_head);
548                         tcd->tcd_cur_pages++;
549                 }
550         }
551         spin_unlock(&pc->pc_lock);
552 }
553
554 static void put_pages_back(struct page_collection *pc)
555 {
556         if (!libcfs_panic_in_progress)
557                 trace_call_on_all_cpus(put_pages_back_on_cpu, pc);
558 }
559
560 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
561  * we have a good amount of data at all times for dumping during an LBUG, even
562  * if we have been steadily writing (and otherwise discarding) pages via the
563  * debug daemon. */
564 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
565                                          struct trace_cpu_data *tcd)
566 {
567         struct trace_page *tage;
568         struct trace_page *tmp;
569
570         spin_lock(&pc->pc_lock);
571         list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
572
573                 __LASSERT_TAGE_INVARIANT(tage);
574
575                 if (tage->cpu != smp_processor_id() ||
576                     tage->type != tcd->tcd_type)
577                         continue;
578
579                 tage_to_tail(tage, &tcd->tcd_daemon_pages);
580                 tcd->tcd_cur_daemon_pages++;
581
582                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
583                         struct trace_page *victim;
584
585                         __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
586                         victim = tage_from_list(tcd->tcd_daemon_pages.next);
587
588                         __LASSERT_TAGE_INVARIANT(victim);
589
590                         list_del(&victim->linkage);
591                         tage_free(victim);
592                         tcd->tcd_cur_daemon_pages--;
593                 }
594         }
595         spin_unlock(&pc->pc_lock);
596 }
597
598 static void put_pages_on_daemon_list_on_cpu(void *info)
599 {
600         struct trace_cpu_data *tcd;
601         int i;
602
603         tcd_for_each_type_lock(tcd, i)
604                 put_pages_on_tcd_daemon_list(info, tcd);
605 }
606
607 static void put_pages_on_daemon_list(struct page_collection *pc)
608 {
609         trace_call_on_all_cpus(put_pages_on_daemon_list_on_cpu, pc);
610 }
611
612 void trace_debug_print(void)
613 {
614         struct page_collection pc;
615         struct trace_page *tage;
616         struct trace_page *tmp;
617
618         spin_lock_init(&pc.pc_lock);
619
620         pc.pc_want_daemon_pages = 1;
621         collect_pages(&pc);
622         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
623                 char *p, *file, *fn;
624                 cfs_page_t *page;
625
626                 __LASSERT_TAGE_INVARIANT(tage);
627
628                 page = tage->page;
629                 p = cfs_page_address(page);
630                 while (p < ((char *)cfs_page_address(page) + tage->used)) {
631                         struct ptldebug_header *hdr;
632                         int len;
633                         hdr = (void *)p;
634                         p += sizeof(*hdr);
635                         file = p;
636                         p += strlen(file) + 1;
637                         fn = p;
638                         p += strlen(fn) + 1;
639                         len = hdr->ph_len - (p - (char *)hdr);
640
641                         print_to_console(hdr, D_EMERG, p, len, file, fn);
642
643                         p += len;
644                 }
645
646                 list_del(&tage->linkage);
647                 tage_free(tage);
648         }
649 }
650
651 int tracefile_dump_all_pages(char *filename)
652 {
653         struct page_collection pc;
654         cfs_file_t *filp;
655         struct trace_page *tage;
656         struct trace_page *tmp;
657         int rc;
658
659         CFS_DECL_MMSPACE;
660
661         tracefile_write_lock();
662
663         filp = cfs_filp_open(filename,
664                              O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
665         if (!filp) {
666                 if (rc != -EEXIST)
667                         printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
668                                filename, rc);
669                 goto out;
670         }
671
672         spin_lock_init(&pc.pc_lock);
673         pc.pc_want_daemon_pages = 1;
674         collect_pages(&pc);
675         if (list_empty(&pc.pc_pages)) {
676                 rc = 0;
677                 goto close;
678         }
679
680         /* ok, for now, just write the pages.  in the future we'll be building
681          * iobufs with the pages and calling generic_direct_IO */
682         CFS_MMSPACE_OPEN;
683         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
684
685                 __LASSERT_TAGE_INVARIANT(tage);
686
687                 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
688                                     tage->used, cfs_filp_poff(filp));
689                 if (rc != (int)tage->used) {
690                         printk(KERN_WARNING "wanted to write %u but wrote "
691                                "%d\n", tage->used, rc);
692                         put_pages_back(&pc);
693                         __LASSERT(list_empty(&pc.pc_pages));
694                         break;
695                 }
696                 list_del(&tage->linkage);
697                 tage_free(tage);
698         }
699         CFS_MMSPACE_CLOSE;
700         rc = cfs_filp_fsync(filp);
701         if (rc)
702                 printk(KERN_ERR "sync returns %d\n", rc);
703  close:
704         cfs_filp_close(filp);
705  out:
706         tracefile_write_unlock();
707         return rc;
708 }
709
710 void trace_flush_pages(void)
711 {
712         struct page_collection pc;
713         struct trace_page *tage;
714         struct trace_page *tmp;
715
716         spin_lock_init(&pc.pc_lock);
717
718         pc.pc_want_daemon_pages = 1;
719         collect_pages(&pc);
720         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
721
722                 __LASSERT_TAGE_INVARIANT(tage);
723
724                 list_del(&tage->linkage);
725                 tage_free(tage);
726         }
727 }
728
729 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
730                         const char *usr_buffer, int usr_buffer_nob)
731 {
732         int    nob;
733         
734         if (usr_buffer_nob > knl_buffer_nob)
735                 return -EOVERFLOW;
736         
737         if (copy_from_user((void *)knl_buffer, 
738                            (void *)usr_buffer, usr_buffer_nob))
739                 return -EFAULT;
740
741         nob = strnlen(knl_buffer, usr_buffer_nob);
742         while (nob-- >= 0)                      /* strip trailing whitespace */
743                 if (!isspace(knl_buffer[nob]))
744                         break;
745
746         if (nob < 0)                            /* empty string */
747                 return -EINVAL;
748
749         if (nob == knl_buffer_nob)              /* no space to terminate */
750                 return -EOVERFLOW;
751
752         knl_buffer[nob + 1] = 0;                /* terminate */
753         return 0;
754 }
755
756 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
757                          const char *knl_buffer, char *append)
758 {
759         /* NB if 'append' != NULL, it's a single character to append to the
760          * copied out string - usually "\n", for /proc entries and "" (i.e. a
761          * terminating zero byte) for sysctl entries */
762         int   nob = strlen(knl_buffer);
763         
764         if (nob > usr_buffer_nob)
765                 nob = usr_buffer_nob;
766         
767         if (copy_to_user(usr_buffer, knl_buffer, nob))
768                 return -EFAULT;
769         
770         if (append != NULL && nob < usr_buffer_nob) {
771                 if (copy_to_user(usr_buffer + nob, append, 1))
772                         return -EFAULT;
773                 
774                 nob++;
775         }
776
777         return nob;
778 }
779
780 int trace_allocate_string_buffer(char **str, int nob)
781 {
782         if (nob > 2 * CFS_PAGE_SIZE)            /* string must be "sensible" */
783                 return -EINVAL;
784         
785         *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
786         if (*str == NULL)
787                 return -ENOMEM;
788
789         return 0;
790 }
791
792 void trace_free_string_buffer(char *str, int nob)
793 {
794         cfs_free(str);
795 }
796
797 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
798 {
799         char         *str;
800         int           rc;
801
802         rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
803         if (rc != 0)
804                 return rc;
805
806         rc = trace_copyin_string(str, usr_str_nob + 1,
807                                  usr_str, usr_str_nob);
808         if (rc != 0)
809                 goto out;
810
811 #if !defined(__WINNT__)
812         if (str[0] != '/') {
813                 rc = -EINVAL;
814                 goto out;
815         }
816 #endif
817         rc = tracefile_dump_all_pages(str);
818 out:
819         trace_free_string_buffer(str, usr_str_nob + 1);
820         return rc;
821 }
822
823 int trace_daemon_command(char *str)
824 {
825         int       rc = 0;
826         
827         tracefile_write_lock();
828
829         if (strcmp(str, "stop") == 0) {
830                 trace_stop_thread();
831                 memset(tracefile, 0, sizeof(tracefile));
832
833         } else if (strncmp(str, "size=", 5) == 0) {
834                 tracefile_size = simple_strtoul(str + 5, NULL, 0);
835                 if (tracefile_size < 10 || tracefile_size > 20480)
836                         tracefile_size = TRACEFILE_SIZE;
837                 else
838                         tracefile_size <<= 20;
839
840         } else if (strlen(str) >= sizeof(tracefile)) {
841                 rc = -ENAMETOOLONG;
842 #ifndef __WINNT__
843         } else if (str[0] != '/') {
844                 rc = -EINVAL;
845 #endif
846         } else {
847                 strcpy(tracefile, str);
848
849                 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
850                        "to %s (%lukB max)\n", tracefile,
851                        (long)(tracefile_size >> 10));
852
853                 trace_start_thread();
854         }
855
856         tracefile_write_unlock();
857         return rc;
858 }
859
860 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
861 {
862         char *str;
863         int   rc;
864
865         rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
866         if (rc != 0)
867                 return rc;
868
869         rc = trace_copyin_string(str, usr_str_nob + 1,
870                                  usr_str, usr_str_nob);
871         if (rc == 0)
872                 rc = trace_daemon_command(str);
873
874         trace_free_string_buffer(str, usr_str_nob + 1);
875         return rc;
876 }
877
878 int trace_set_debug_mb(int mb)
879 {
880         int i;
881         int j;
882         int pages;
883         int limit = trace_max_debug_mb();
884         struct trace_cpu_data *tcd;
885         
886         if (mb < num_possible_cpus())
887                 return -EINVAL;
888
889         if (mb > limit) {
890                 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to "
891                        "%dMB - limit is %d\n", mb, limit);
892                 return -EINVAL;
893         }
894
895         mb /= num_possible_cpus();
896         pages = mb << (20 - CFS_PAGE_SHIFT);
897
898         tracefile_write_lock();
899
900         tcd_for_each(tcd, i, j)
901                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
902
903         tracefile_write_unlock();
904
905         return 0;
906 }
907
908 int trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
909 {
910         char     str[32];
911         int      rc;
912
913         rc = trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
914         if (rc < 0)
915                 return rc;
916
917         return trace_set_debug_mb(simple_strtoul(str, NULL, 0));
918 }
919
920 int trace_get_debug_mb(void)
921 {
922         int i;
923         int j;
924         struct trace_cpu_data *tcd;
925         int total_pages = 0;
926         
927         tracefile_read_lock();
928
929         tcd_for_each(tcd, i, j)
930                 total_pages += tcd->tcd_max_pages;
931
932         tracefile_read_unlock();
933
934         return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
935 }
936
937 static int tracefiled(void *arg)
938 {
939         struct page_collection pc;
940         struct tracefiled_ctl *tctl = arg;
941         struct trace_page *tage;
942         struct trace_page *tmp;
943         struct ptldebug_header *hdr;
944         cfs_file_t *filp;
945         int rc;
946
947         CFS_DECL_MMSPACE;
948
949         /* we're started late enough that we pick up init's fs context */
950         /* this is so broken in uml?  what on earth is going on? */
951         cfs_daemonize("ktracefiled");
952
953         spin_lock_init(&pc.pc_lock);
954         complete(&tctl->tctl_start);
955
956         while (1) {
957                 cfs_waitlink_t __wait;
958
959                 cfs_waitlink_init(&__wait);
960                 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
961                 set_current_state(TASK_INTERRUPTIBLE);
962                 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
963                                     cfs_time_seconds(1));
964                 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
965
966                 if (atomic_read(&tctl->tctl_shutdown))
967                         break;
968
969                 pc.pc_want_daemon_pages = 0;
970                 collect_pages(&pc);
971                 if (list_empty(&pc.pc_pages))
972                         continue;
973
974                 filp = NULL;
975                 tracefile_read_lock();
976                 if (tracefile[0] != 0) {
977                         filp = cfs_filp_open(tracefile,
978                                              O_CREAT | O_RDWR | O_LARGEFILE,
979                                              0600, &rc);
980                         if (!(filp))
981                                 printk(KERN_WARNING "couldn't open %s: %d\n",
982                                        tracefile, rc);
983                 }
984                 tracefile_read_unlock();
985                 if (filp == NULL) {
986                         put_pages_on_daemon_list(&pc);
987                         __LASSERT(list_empty(&pc.pc_pages));
988                         continue;
989                 }
990
991                 CFS_MMSPACE_OPEN;
992
993                 /* mark the first header, so we can sort in chunks */
994                 tage = tage_from_list(pc.pc_pages.next);
995                 __LASSERT_TAGE_INVARIANT(tage);
996
997                 hdr = cfs_page_address(tage->page);
998                 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
999
1000                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1001                         static loff_t f_pos;
1002
1003                         __LASSERT_TAGE_INVARIANT(tage);
1004
1005                         if (f_pos >= (off_t)tracefile_size)
1006                                 f_pos = 0;
1007                         else if (f_pos > cfs_filp_size(filp))
1008                                 f_pos = cfs_filp_size(filp);
1009
1010                         rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1011                                             tage->used, &f_pos);
1012                         if (rc != (int)tage->used) {
1013                                 printk(KERN_WARNING "wanted to write %u but "
1014                                        "wrote %d\n", tage->used, rc);
1015                                 put_pages_back(&pc);
1016                                 __LASSERT(list_empty(&pc.pc_pages));
1017                         }
1018                 }
1019                 CFS_MMSPACE_CLOSE;
1020
1021                 cfs_filp_close(filp);
1022                 put_pages_on_daemon_list(&pc);
1023                 __LASSERT(list_empty(&pc.pc_pages));
1024         }
1025         complete(&tctl->tctl_stop);
1026         return 0;
1027 }
1028
1029 int trace_start_thread(void)
1030 {
1031         struct tracefiled_ctl *tctl = &trace_tctl;
1032         int rc = 0;
1033
1034         mutex_down(&trace_thread_sem);
1035         if (thread_running)
1036                 goto out;
1037
1038         init_completion(&tctl->tctl_start);
1039         init_completion(&tctl->tctl_stop);
1040         cfs_waitq_init(&tctl->tctl_waitq);
1041         atomic_set(&tctl->tctl_shutdown, 0);
1042
1043         if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1044                 rc = -ECHILD;
1045                 goto out;
1046         }
1047
1048         wait_for_completion(&tctl->tctl_start);
1049         thread_running = 1;
1050 out:
1051         mutex_up(&trace_thread_sem);
1052         return rc;
1053 }
1054
1055 void trace_stop_thread(void)
1056 {
1057         struct tracefiled_ctl *tctl = &trace_tctl;
1058
1059         mutex_down(&trace_thread_sem);
1060         if (thread_running) {
1061                 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1062                 atomic_set(&tctl->tctl_shutdown, 1);
1063                 wait_for_completion(&tctl->tctl_stop);
1064                 thread_running = 0;
1065         }
1066         mutex_up(&trace_thread_sem);
1067 }
1068
1069 int tracefile_init(int max_pages)
1070 {
1071         struct trace_cpu_data *tcd;
1072         int                    i;
1073         int                    j;
1074         int                    rc;
1075         int                    factor;
1076
1077         rc = tracefile_init_arch();
1078         if (rc != 0)
1079                 return rc;
1080
1081         tcd_for_each(tcd, i, j) {
1082                 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1083                 factor = tcd->tcd_pages_factor;
1084                 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1085                 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1086                 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1087                 tcd->tcd_cur_pages = 0;
1088                 tcd->tcd_cur_stock_pages = 0;
1089                 tcd->tcd_cur_daemon_pages = 0;
1090                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1091                 LASSERT(tcd->tcd_max_pages > 0);
1092                 tcd->tcd_shutting_down = 0;
1093         }
1094
1095         return 0;
1096 }
1097
1098 static void trace_cleanup_on_cpu(void *info)
1099 {
1100         struct trace_cpu_data *tcd;
1101         struct trace_page *tage;
1102         struct trace_page *tmp;
1103         int i;
1104
1105         tcd_for_each_type_lock(tcd, i) {
1106                 tcd->tcd_shutting_down = 1;
1107
1108                 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1109                         __LASSERT_TAGE_INVARIANT(tage);
1110
1111                         list_del(&tage->linkage);
1112                         tage_free(tage);
1113                 }
1114                 tcd->tcd_cur_pages = 0;
1115         }
1116 }
1117
1118 static void trace_cleanup(void)
1119 {
1120         struct page_collection pc;
1121
1122         CFS_INIT_LIST_HEAD(&pc.pc_pages);
1123         spin_lock_init(&pc.pc_lock);
1124
1125         trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
1126
1127         tracefile_fini_arch();
1128 }
1129
1130 void tracefile_exit(void)
1131 {
1132         trace_stop_thread();
1133         trace_cleanup();
1134 }