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