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