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