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