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