Whamcloud - gitweb
Branch b1_8
[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                 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,
323                                             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_with_loc(file, func, line);
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_all_cpus(struct page_collection *pc)
500 {
501         struct trace_cpu_data *tcd;
502         int i, cpu;
503
504         spin_lock(&pc->pc_lock);
505         for_each_possible_cpu(cpu) {
506                 tcd_for_each_type_lock(tcd, i, cpu) {
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,
511                                                  &pc->pc_pages);
512                                 tcd->tcd_cur_daemon_pages = 0;
513                         }
514                 }
515         }
516         spin_unlock(&pc->pc_lock);
517 }
518
519 static void collect_pages(struct page_collection *pc)
520 {
521         CFS_INIT_LIST_HEAD(&pc->pc_pages);
522
523         if (libcfs_panic_in_progress)
524                 panic_collect_pages(pc);
525         else
526                 collect_pages_on_all_cpus(pc);
527 }
528
529 static void put_pages_back_on_all_cpus(struct page_collection *pc)
530 {
531         struct trace_cpu_data *tcd;
532         struct list_head *cur_head;
533         struct trace_page *tage;
534         struct trace_page *tmp;
535         int i, cpu;
536
537         spin_lock(&pc->pc_lock);
538         for_each_possible_cpu(cpu) {
539                 tcd_for_each_type_lock(tcd, i, cpu) {
540                         cur_head = tcd->tcd_pages.next;
541
542                         list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
543                                                  linkage) {
544
545                                 __LASSERT_TAGE_INVARIANT(tage);
546
547                                 if (tage->cpu != cpu || tage->type != i)
548                                         continue;
549
550                                 tage_to_tail(tage, cur_head);
551                                 tcd->tcd_cur_pages++;
552                         }
553                 }
554         }
555         spin_unlock(&pc->pc_lock);
556 }
557
558 static void put_pages_back(struct page_collection *pc)
559 {
560         if (!libcfs_panic_in_progress)
561                 put_pages_back_on_all_cpus(pc);
562 }
563
564 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
565  * we have a good amount of data at all times for dumping during an LBUG, even
566  * if we have been steadily writing (and otherwise discarding) pages via the
567  * debug daemon. */
568 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
569                                          struct trace_cpu_data *tcd)
570 {
571         struct trace_page *tage;
572         struct trace_page *tmp;
573
574         spin_lock(&pc->pc_lock);
575         list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
576
577                 __LASSERT_TAGE_INVARIANT(tage);
578
579                 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
580                         continue;
581
582                 tage_to_tail(tage, &tcd->tcd_daemon_pages);
583                 tcd->tcd_cur_daemon_pages++;
584
585                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
586                         struct trace_page *victim;
587
588                         __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
589                         victim = tage_from_list(tcd->tcd_daemon_pages.next);
590
591                         __LASSERT_TAGE_INVARIANT(victim);
592
593                         list_del(&victim->linkage);
594                         tage_free(victim);
595                         tcd->tcd_cur_daemon_pages--;
596                 }
597         }
598         spin_unlock(&pc->pc_lock);
599 }
600
601 static void put_pages_on_daemon_list(struct page_collection *pc)
602 {
603         struct trace_cpu_data *tcd;
604         int i, cpu;
605
606         for_each_possible_cpu(cpu) {
607                 tcd_for_each_type_lock(tcd, i, cpu)
608                         put_pages_on_tcd_daemon_list(pc, tcd);
609         }
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                 tracefile_write_unlock();
831                 trace_stop_thread();
832                 tracefile_write_lock();
833                 memset(tracefile, 0, sizeof(tracefile));
834
835         } else if (strncmp(str, "size=", 5) == 0) {
836                 tracefile_size = simple_strtoul(str + 5, NULL, 0);
837                 if (tracefile_size < 10 || tracefile_size > 20480)
838                         tracefile_size = TRACEFILE_SIZE;
839                 else
840                         tracefile_size <<= 20;
841
842         } else if (strlen(str) >= sizeof(tracefile)) {
843                 rc = -ENAMETOOLONG;
844 #ifndef __WINNT__
845         } else if (str[0] != '/') {
846                 rc = -EINVAL;
847 #endif
848         } else {
849                 strcpy(tracefile, str);
850
851                 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
852                        "to %s (%lukB max)\n", tracefile,
853                        (long)(tracefile_size >> 10));
854
855                 trace_start_thread();
856         }
857
858         tracefile_write_unlock();
859         return rc;
860 }
861
862 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
863 {
864         char *str;
865         int   rc;
866
867         rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
868         if (rc != 0)
869                 return rc;
870
871         rc = trace_copyin_string(str, usr_str_nob + 1,
872                                  usr_str, usr_str_nob);
873         if (rc == 0)
874                 rc = trace_daemon_command(str);
875
876         trace_free_string_buffer(str, usr_str_nob + 1);
877
878         return rc;
879 }
880
881 int trace_set_debug_mb(int mb)
882 {
883         int i;
884         int j;
885         int pages;
886         int limit = trace_max_debug_mb();
887         struct trace_cpu_data *tcd;
888
889         if (mb < num_possible_cpus())
890                 return -EINVAL;
891
892         if (mb > limit) {
893                 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to "
894                        "%dMB - limit is %d\n", mb, limit);
895                 return -EINVAL;
896         }
897
898         mb /= num_possible_cpus();
899         pages = mb << (20 - CFS_PAGE_SHIFT);
900
901         tracefile_write_lock();
902
903         tcd_for_each(tcd, i, j)
904                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
905
906         tracefile_write_unlock();
907
908         return 0;
909 }
910
911 int trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
912 {
913         char     str[32];
914         int      rc;
915
916         rc = trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
917         if (rc < 0)
918                 return rc;
919
920         return trace_set_debug_mb(simple_strtoul(str, NULL, 0));
921 }
922
923 int trace_get_debug_mb(void)
924 {
925         int i;
926         int j;
927         struct trace_cpu_data *tcd;
928         int total_pages = 0;
929
930         tracefile_read_lock();
931
932         tcd_for_each(tcd, i, j)
933                 total_pages += tcd->tcd_max_pages;
934
935         tracefile_read_unlock();
936
937         return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
938 }
939
940 static int tracefiled(void *arg)
941 {
942         struct page_collection pc;
943         struct tracefiled_ctl *tctl = arg;
944         struct trace_page *tage;
945         struct trace_page *tmp;
946         struct ptldebug_header *hdr;
947         cfs_file_t *filp;
948         int rc;
949
950         CFS_DECL_MMSPACE;
951
952         /* we're started late enough that we pick up init's fs context */
953         /* this is so broken in uml?  what on earth is going on? */
954         cfs_daemonize("ktracefiled");
955
956         spin_lock_init(&pc.pc_lock);
957         complete(&tctl->tctl_start);
958
959         while (1) {
960                 cfs_waitlink_t __wait;
961
962                 pc.pc_want_daemon_pages = 0;
963                 collect_pages(&pc);
964                 if (list_empty(&pc.pc_pages))
965                         continue;
966
967                 filp = NULL;
968                 tracefile_read_lock();
969                 if (tracefile[0] != 0) {
970                         filp = cfs_filp_open(tracefile,
971                                              O_CREAT | O_RDWR | O_LARGEFILE,
972                                              0600, &rc);
973                         if (!(filp))
974                                 printk(KERN_WARNING "couldn't open %s: %d\n",
975                                        tracefile, rc);
976                 }
977                 tracefile_read_unlock();
978                 if (filp == NULL) {
979                         put_pages_on_daemon_list(&pc);
980                         __LASSERT(list_empty(&pc.pc_pages));
981                         continue;
982                 }
983
984                 CFS_MMSPACE_OPEN;
985
986                 /* mark the first header, so we can sort in chunks */
987                 tage = tage_from_list(pc.pc_pages.next);
988                 __LASSERT_TAGE_INVARIANT(tage);
989
990                 hdr = cfs_page_address(tage->page);
991                 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
992
993                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
994                         static loff_t f_pos;
995
996                         __LASSERT_TAGE_INVARIANT(tage);
997
998                         if (f_pos >= (off_t)tracefile_size)
999                                 f_pos = 0;
1000                         else if (f_pos > cfs_filp_size(filp))
1001                                 f_pos = cfs_filp_size(filp);
1002
1003                         rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1004                                             tage->used, &f_pos);
1005                         if (rc != (int)tage->used) {
1006                                 printk(KERN_WARNING "wanted to write %u but "
1007                                        "wrote %d\n", tage->used, rc);
1008                                 put_pages_back(&pc);
1009                                 __LASSERT(list_empty(&pc.pc_pages));
1010                         }
1011                 }
1012                 CFS_MMSPACE_CLOSE;
1013
1014                 cfs_filp_close(filp);
1015                 put_pages_on_daemon_list(&pc);
1016                 if (!list_empty(&pc.pc_pages)) {
1017                         int i;
1018
1019                         printk(KERN_ALERT "Lustre: trace pages aren't empty\n");
1020                         printk(KERN_ALERT "total cpus(%d): ", num_possible_cpus());
1021                         for (i = 0; i < num_possible_cpus(); i++)
1022                                 if (cpu_online(i))
1023                                         printk(KERN_ALERT "%d(on) ", i);
1024                                 else
1025                                         printk(KERN_ALERT "%d(off) ", i);
1026                         printk(KERN_ALERT "\n");
1027
1028                         i = 0;
1029                         list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1030                                                  linkage)
1031                                 printk(KERN_ALERT "page %d belongs to cpu %d\n",
1032                                        ++i, tage->cpu);
1033                         printk(KERN_ALERT "There are %d pages unwritten\n", i);
1034                 }
1035                 __LASSERT(list_empty(&pc.pc_pages));
1036
1037                 cfs_waitlink_init(&__wait);
1038                 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
1039                 set_current_state(TASK_INTERRUPTIBLE);
1040                 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
1041                                     cfs_time_seconds(1));
1042                 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
1043
1044                 if (atomic_read(&tctl->tctl_shutdown))
1045                         break;
1046         }
1047         complete(&tctl->tctl_stop);
1048         return 0;
1049 }
1050
1051 int trace_start_thread(void)
1052 {
1053         struct tracefiled_ctl *tctl = &trace_tctl;
1054         int rc = 0;
1055
1056         mutex_down(&trace_thread_sem);
1057         if (thread_running)
1058                 goto out;
1059
1060         init_completion(&tctl->tctl_start);
1061         init_completion(&tctl->tctl_stop);
1062         cfs_waitq_init(&tctl->tctl_waitq);
1063         atomic_set(&tctl->tctl_shutdown, 0);
1064
1065         if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1066                 rc = -ECHILD;
1067                 goto out;
1068         }
1069
1070         wait_for_completion(&tctl->tctl_start);
1071         thread_running = 1;
1072 out:
1073         mutex_up(&trace_thread_sem);
1074         return rc;
1075 }
1076
1077 void trace_stop_thread(void)
1078 {
1079         struct tracefiled_ctl *tctl = &trace_tctl;
1080
1081         mutex_down(&trace_thread_sem);
1082         if (thread_running) {
1083                 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1084                 atomic_set(&tctl->tctl_shutdown, 1);
1085                 wait_for_completion(&tctl->tctl_stop);
1086                 thread_running = 0;
1087         }
1088         mutex_up(&trace_thread_sem);
1089 }
1090
1091 int tracefile_init(int max_pages)
1092 {
1093         struct trace_cpu_data *tcd;
1094         int                    i;
1095         int                    j;
1096         int                    rc;
1097         int                    factor;
1098
1099         rc = tracefile_init_arch();
1100         if (rc != 0)
1101                 return rc;
1102
1103         tcd_for_each(tcd, i, j) {
1104                 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1105                 factor = tcd->tcd_pages_factor;
1106                 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1107                 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1108                 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1109                 tcd->tcd_cur_pages = 0;
1110                 tcd->tcd_cur_stock_pages = 0;
1111                 tcd->tcd_cur_daemon_pages = 0;
1112                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1113                 LASSERT(tcd->tcd_max_pages > 0);
1114                 tcd->tcd_shutting_down = 0;
1115         }
1116
1117         return 0;
1118 }
1119
1120 static void trace_cleanup_on_all_cpus(void)
1121 {
1122         struct trace_cpu_data *tcd;
1123         struct trace_page *tage;
1124         struct trace_page *tmp;
1125         int i, cpu;
1126
1127         for_each_possible_cpu(cpu) {
1128                 tcd_for_each_type_lock(tcd, i, cpu) {
1129                         tcd->tcd_shutting_down = 1;
1130
1131                         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages,
1132                                                  linkage) {
1133                                 __LASSERT_TAGE_INVARIANT(tage);
1134
1135                                 list_del(&tage->linkage);
1136                                 tage_free(tage);
1137                         }
1138
1139                         tcd->tcd_cur_pages = 0;
1140                 }
1141         }
1142 }
1143
1144 static void trace_cleanup(void)
1145 {
1146         struct page_collection pc;
1147
1148         CFS_INIT_LIST_HEAD(&pc.pc_pages);
1149         spin_lock_init(&pc.pc_lock);
1150
1151         trace_cleanup_on_all_cpus();
1152
1153         tracefile_fini_arch();
1154 }
1155
1156 void tracefile_exit(void)
1157 {
1158         trace_stop_thread();
1159         trace_cleanup();
1160 }