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