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