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