Whamcloud - gitweb
Ratelimit a memory allocation failure message that can be too chatty.
[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         lbug_with_loc(file, func, line);
455 }
456 EXPORT_SYMBOL(libcfs_assertion_failed);
457
458 void
459 trace_assertion_failed(const char *str,
460                        const char *fn, const char *file, int line)
461 {
462         struct ptldebug_header hdr;
463
464         libcfs_panic_in_progress = 1;
465         libcfs_catastrophe = 1;
466         mb();
467
468         set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
469                             CDEBUG_STACK());
470
471         print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
472
473         LIBCFS_PANIC("Lustre debug assertion failure\n");
474
475         /* not reached */
476 }
477
478 static void
479 panic_collect_pages(struct page_collection *pc)
480 {
481         /* Do the collect_pages job on a single CPU: assumes that all other
482          * CPUs have been stopped during a panic.  If this isn't true for some
483          * arch, this will have to be implemented separately in each arch.  */
484         int                    i;
485         int                    j;
486         struct trace_cpu_data *tcd;
487
488         CFS_INIT_LIST_HEAD(&pc->pc_pages);
489
490         tcd_for_each(tcd, i, j) {
491                 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
492                 tcd->tcd_cur_pages = 0;
493
494                 if (pc->pc_want_daemon_pages) {
495                         list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
496                         tcd->tcd_cur_daemon_pages = 0;
497                 }
498         }
499 }
500
501 static void collect_pages_on_cpu(void *info)
502 {
503         struct trace_cpu_data *tcd;
504         struct page_collection *pc = info;
505         int i;
506
507         spin_lock(&pc->pc_lock);
508         tcd_for_each_type_lock(tcd, i) {
509                 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
510                 tcd->tcd_cur_pages = 0;
511                 if (pc->pc_want_daemon_pages) {
512                         list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
513                         tcd->tcd_cur_daemon_pages = 0;
514                 }
515         }
516         spin_unlock(&pc->pc_lock);
517 }
518
519 static void collect_pages(struct page_collection *pc)
520 {
521         CFS_INIT_LIST_HEAD(&pc->pc_pages);
522
523         if (libcfs_panic_in_progress)
524                 panic_collect_pages(pc);
525         else
526                 trace_call_on_all_cpus(collect_pages_on_cpu, pc);
527 }
528
529 static void put_pages_back_on_cpu(void *info)
530 {
531         struct page_collection *pc = info;
532         struct trace_cpu_data *tcd;
533         struct list_head *cur_head;
534         struct trace_page *tage;
535         struct trace_page *tmp;
536         int i;
537
538         spin_lock(&pc->pc_lock);
539         tcd_for_each_type_lock(tcd, i) {
540                 cur_head = tcd->tcd_pages.next;
541
542                 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
543                                                    struct trace_page, linkage) {
544
545                         __LASSERT_TAGE_INVARIANT(tage);
546
547                         if (tage->cpu != smp_processor_id() || tage->type != i)
548                                 continue;
549
550                         tage_to_tail(tage, cur_head);
551                         tcd->tcd_cur_pages++;
552                 }
553         }
554         spin_unlock(&pc->pc_lock);
555 }
556
557 static void put_pages_back(struct page_collection *pc)
558 {
559         if (!libcfs_panic_in_progress)
560                 trace_call_on_all_cpus(put_pages_back_on_cpu, pc);
561 }
562
563 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
564  * we have a good amount of data at all times for dumping during an LBUG, even
565  * if we have been steadily writing (and otherwise discarding) pages via the
566  * debug daemon. */
567 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
568                                          struct trace_cpu_data *tcd)
569 {
570         struct trace_page *tage;
571         struct trace_page *tmp;
572
573         spin_lock(&pc->pc_lock);
574         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
575                                            struct trace_page, linkage) {
576
577                 __LASSERT_TAGE_INVARIANT(tage);
578
579                 if (tage->cpu != smp_processor_id() ||
580                     tage->type != tcd->tcd_type)
581                         continue;
582
583                 tage_to_tail(tage, &tcd->tcd_daemon_pages);
584                 tcd->tcd_cur_daemon_pages++;
585
586                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
587                         struct trace_page *victim;
588
589                         __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
590                         victim = tage_from_list(tcd->tcd_daemon_pages.next);
591
592                         __LASSERT_TAGE_INVARIANT(victim);
593
594                         list_del(&victim->linkage);
595                         tage_free(victim);
596                         tcd->tcd_cur_daemon_pages--;
597                 }
598         }
599         spin_unlock(&pc->pc_lock);
600 }
601
602 static void put_pages_on_daemon_list_on_cpu(void *info)
603 {
604         struct trace_cpu_data *tcd;
605         int i;
606
607         tcd_for_each_type_lock(tcd, i)
608                 put_pages_on_tcd_daemon_list(info, tcd);
609 }
610
611 static void put_pages_on_daemon_list(struct page_collection *pc)
612 {
613         trace_call_on_all_cpus(put_pages_on_daemon_list_on_cpu, pc);
614 }
615
616 void trace_debug_print(void)
617 {
618         struct page_collection pc;
619         struct trace_page *tage;
620         struct trace_page *tmp;
621
622         spin_lock_init(&pc.pc_lock);
623
624         pc.pc_want_daemon_pages = 1;
625         collect_pages(&pc);
626         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
627                                            struct trace_page, linkage) {
628                 char *p, *file, *fn;
629                 cfs_page_t *page;
630
631                 __LASSERT_TAGE_INVARIANT(tage);
632
633                 page = tage->page;
634                 p = cfs_page_address(page);
635                 while (p < ((char *)cfs_page_address(page) + tage->used)) {
636                         struct ptldebug_header *hdr;
637                         int len;
638                         hdr = (void *)p;
639                         p += sizeof(*hdr);
640                         file = p;
641                         p += strlen(file) + 1;
642                         fn = p;
643                         p += strlen(fn) + 1;
644                         len = hdr->ph_len - (int)(p - (char *)hdr);
645
646                         print_to_console(hdr, D_EMERG, p, len, file, fn);
647
648                         p += len;
649                 }
650
651                 list_del(&tage->linkage);
652                 tage_free(tage);
653         }
654 }
655
656 int tracefile_dump_all_pages(char *filename)
657 {
658         struct page_collection pc;
659         cfs_file_t *filp;
660         struct trace_page *tage;
661         struct trace_page *tmp;
662         int rc;
663
664         CFS_DECL_MMSPACE;
665
666         tracefile_write_lock();
667
668         filp = cfs_filp_open(filename,
669                              O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
670         if (!filp) {
671                 if (rc != -EEXIST)
672                         printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
673                                filename, rc);
674                 goto out;
675         }
676
677         spin_lock_init(&pc.pc_lock);
678         pc.pc_want_daemon_pages = 1;
679         collect_pages(&pc);
680         if (list_empty(&pc.pc_pages)) {
681                 rc = 0;
682                 goto close;
683         }
684
685         /* ok, for now, just write the pages.  in the future we'll be building
686          * iobufs with the pages and calling generic_direct_IO */
687         CFS_MMSPACE_OPEN;
688         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
689                                            struct trace_page, linkage) {
690
691                 __LASSERT_TAGE_INVARIANT(tage);
692
693                 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
694                                     tage->used, cfs_filp_poff(filp));
695                 if (rc != (int)tage->used) {
696                         printk(KERN_WARNING "wanted to write %u but wrote "
697                                "%d\n", tage->used, rc);
698                         put_pages_back(&pc);
699                         __LASSERT(list_empty(&pc.pc_pages));
700                         break;
701                 }
702                 list_del(&tage->linkage);
703                 tage_free(tage);
704         }
705         CFS_MMSPACE_CLOSE;
706         rc = cfs_filp_fsync(filp);
707         if (rc)
708                 printk(KERN_ERR "sync returns %d\n", rc);
709  close:
710         cfs_filp_close(filp);
711  out:
712         tracefile_write_unlock();
713         return rc;
714 }
715
716 void trace_flush_pages(void)
717 {
718         struct page_collection pc;
719         struct trace_page *tage;
720         struct trace_page *tmp;
721
722         spin_lock_init(&pc.pc_lock);
723
724         pc.pc_want_daemon_pages = 1;
725         collect_pages(&pc);
726         cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
727                                            struct trace_page, linkage) {
728
729                 __LASSERT_TAGE_INVARIANT(tage);
730
731                 list_del(&tage->linkage);
732                 tage_free(tage);
733         }
734 }
735
736 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
737                         const char *usr_buffer, int usr_buffer_nob)
738 {
739         int    nob;
740
741         if (usr_buffer_nob > knl_buffer_nob)
742                 return -EOVERFLOW;
743
744         if (copy_from_user((void *)knl_buffer,
745                            (void *)usr_buffer, usr_buffer_nob))
746                 return -EFAULT;
747
748         nob = strnlen(knl_buffer, usr_buffer_nob);
749         while (nob-- >= 0)                      /* strip trailing whitespace */
750                 if (!isspace(knl_buffer[nob]))
751                         break;
752
753         if (nob < 0)                            /* empty string */
754                 return -EINVAL;
755
756         if (nob == knl_buffer_nob)              /* no space to terminate */
757                 return -EOVERFLOW;
758
759         knl_buffer[nob + 1] = 0;                /* terminate */
760         return 0;
761 }
762
763 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
764                          const char *knl_buffer, char *append)
765 {
766         /* NB if 'append' != NULL, it's a single character to append to the
767          * copied out string - usually "\n", for /proc entries and "" (i.e. a
768          * terminating zero byte) for sysctl entries */
769         int   nob = strlen(knl_buffer);
770
771         if (nob > usr_buffer_nob)
772                 nob = usr_buffer_nob;
773
774         if (copy_to_user(usr_buffer, knl_buffer, nob))
775                 return -EFAULT;
776
777         if (append != NULL && nob < usr_buffer_nob) {
778                 if (copy_to_user(usr_buffer + nob, append, 1))
779                         return -EFAULT;
780
781                 nob++;
782         }
783
784         return nob;
785 }
786
787 int trace_allocate_string_buffer(char **str, int nob)
788 {
789         if (nob > 2 * CFS_PAGE_SIZE)            /* string must be "sensible" */
790                 return -EINVAL;
791
792         *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
793         if (*str == NULL)
794                 return -ENOMEM;
795
796         return 0;
797 }
798
799 void trace_free_string_buffer(char *str, int nob)
800 {
801         cfs_free(str);
802 }
803
804 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
805 {
806         char         *str;
807         int           rc;
808
809         rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
810         if (rc != 0)
811                 return rc;
812
813         rc = trace_copyin_string(str, usr_str_nob + 1,
814                                  usr_str, usr_str_nob);
815         if (rc != 0)
816                 goto out;
817
818 #if !defined(__WINNT__)
819         if (str[0] != '/') {
820                 rc = -EINVAL;
821                 goto out;
822         }
823 #endif
824         rc = tracefile_dump_all_pages(str);
825 out:
826         trace_free_string_buffer(str, usr_str_nob + 1);
827         return rc;
828 }
829
830 int trace_daemon_command(char *str)
831 {
832         int       rc = 0;
833
834         tracefile_write_lock();
835
836         if (strcmp(str, "stop") == 0) {
837                 trace_stop_thread();
838                 memset(tracefile, 0, sizeof(tracefile));
839
840         } else if (strncmp(str, "size=", 5) == 0) {
841                 tracefile_size = simple_strtoul(str + 5, NULL, 0);
842                 if (tracefile_size < 10 || tracefile_size > 20480)
843                         tracefile_size = TRACEFILE_SIZE;
844                 else
845                         tracefile_size <<= 20;
846
847         } else if (strlen(str) >= sizeof(tracefile)) {
848                 rc = -ENAMETOOLONG;
849 #ifndef __WINNT__
850         } else if (str[0] != '/') {
851                 rc = -EINVAL;
852 #endif
853         } else {
854                 strcpy(tracefile, str);
855
856                 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
857                        "to %s (%lukB max)\n", tracefile,
858                        (long)(tracefile_size >> 10));
859
860                 trace_start_thread();
861         }
862
863         tracefile_write_unlock();
864         return rc;
865 }
866
867 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
868 {
869         char *str;
870         int   rc;
871
872         rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
873         if (rc != 0)
874                 return rc;
875
876         rc = trace_copyin_string(str, usr_str_nob + 1,
877                                  usr_str, usr_str_nob);
878         if (rc == 0)
879                 rc = trace_daemon_command(str);
880
881         trace_free_string_buffer(str, usr_str_nob + 1);
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 rc;
953
954         CFS_DECL_MMSPACE;
955
956         /* we're started late enough that we pick up init's fs context */
957         /* this is so broken in uml?  what on earth is going on? */
958         cfs_daemonize("ktracefiled");
959
960         spin_lock_init(&pc.pc_lock);
961         complete(&tctl->tctl_start);
962
963         while (1) {
964                 cfs_waitlink_t __wait;
965
966                 cfs_waitlink_init(&__wait);
967                 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
968                 set_current_state(TASK_INTERRUPTIBLE);
969                 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
970                                     cfs_time_seconds(1));
971                 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
972
973                 if (atomic_read(&tctl->tctl_shutdown))
974                         break;
975
976                 pc.pc_want_daemon_pages = 0;
977                 collect_pages(&pc);
978                 if (list_empty(&pc.pc_pages))
979                         continue;
980
981                 filp = NULL;
982                 tracefile_read_lock();
983                 if (tracefile[0] != 0) {
984                         filp = cfs_filp_open(tracefile,
985                                              O_CREAT | O_RDWR | O_LARGEFILE,
986                                              0600, &rc);
987                         if (!(filp))
988                                 printk(KERN_WARNING "couldn't open %s: %d\n",
989                                        tracefile, rc);
990                 }
991                 tracefile_read_unlock();
992                 if (filp == NULL) {
993                         put_pages_on_daemon_list(&pc);
994                         __LASSERT(list_empty(&pc.pc_pages));
995                         continue;
996                 }
997
998                 CFS_MMSPACE_OPEN;
999
1000                 /* mark the first header, so we can sort in chunks */
1001                 tage = tage_from_list(pc.pc_pages.next);
1002                 __LASSERT_TAGE_INVARIANT(tage);
1003
1004                 hdr = cfs_page_address(tage->page);
1005                 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
1006
1007                 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
1008                                                    struct trace_page, linkage) {
1009                         static loff_t f_pos;
1010
1011                         __LASSERT_TAGE_INVARIANT(tage);
1012
1013                         if (f_pos >= (off_t)tracefile_size)
1014                                 f_pos = 0;
1015                         else if (f_pos > (off_t)cfs_filp_size(filp))
1016                                 f_pos = cfs_filp_size(filp);
1017
1018                         rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1019                                             tage->used, &f_pos);
1020                         if (rc != (int)tage->used) {
1021                                 printk(KERN_WARNING "wanted to write %u but "
1022                                        "wrote %d\n", tage->used, rc);
1023                                 put_pages_back(&pc);
1024                                 __LASSERT(list_empty(&pc.pc_pages));
1025                         }
1026                 }
1027                 CFS_MMSPACE_CLOSE;
1028
1029                 cfs_filp_close(filp);
1030                 put_pages_on_daemon_list(&pc);
1031                 __LASSERT(list_empty(&pc.pc_pages));
1032         }
1033         complete(&tctl->tctl_stop);
1034         return 0;
1035 }
1036
1037 int trace_start_thread(void)
1038 {
1039         struct tracefiled_ctl *tctl = &trace_tctl;
1040         int rc = 0;
1041
1042         mutex_down(&trace_thread_sem);
1043         if (thread_running)
1044                 goto out;
1045
1046         init_completion(&tctl->tctl_start);
1047         init_completion(&tctl->tctl_stop);
1048         cfs_waitq_init(&tctl->tctl_waitq);
1049         atomic_set(&tctl->tctl_shutdown, 0);
1050
1051         if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1052                 rc = -ECHILD;
1053                 goto out;
1054         }
1055
1056         wait_for_completion(&tctl->tctl_start);
1057         thread_running = 1;
1058 out:
1059         mutex_up(&trace_thread_sem);
1060         return rc;
1061 }
1062
1063 void trace_stop_thread(void)
1064 {
1065         struct tracefiled_ctl *tctl = &trace_tctl;
1066
1067         mutex_down(&trace_thread_sem);
1068         if (thread_running) {
1069                 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1070                 atomic_set(&tctl->tctl_shutdown, 1);
1071                 wait_for_completion(&tctl->tctl_stop);
1072                 thread_running = 0;
1073         }
1074         mutex_up(&trace_thread_sem);
1075 }
1076
1077 int tracefile_init(int max_pages)
1078 {
1079         struct trace_cpu_data *tcd;
1080         int                    i;
1081         int                    j;
1082         int                    rc;
1083         int                    factor;
1084
1085         rc = tracefile_init_arch();
1086         if (rc != 0)
1087                 return rc;
1088
1089         tcd_for_each(tcd, i, j) {
1090                 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1091                 factor = tcd->tcd_pages_factor;
1092                 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1093                 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1094                 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1095                 tcd->tcd_cur_pages = 0;
1096                 tcd->tcd_cur_stock_pages = 0;
1097                 tcd->tcd_cur_daemon_pages = 0;
1098                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1099                 LASSERT(tcd->tcd_max_pages > 0);
1100                 tcd->tcd_shutting_down = 0;
1101         }
1102
1103         return 0;
1104 }
1105
1106 static void trace_cleanup_on_cpu(void *info)
1107 {
1108         struct trace_cpu_data *tcd;
1109         struct trace_page *tage;
1110         struct trace_page *tmp;
1111         int i;
1112
1113         tcd_for_each_type_lock(tcd, i) {
1114                 tcd->tcd_shutting_down = 1;
1115
1116                 cfs_list_for_each_entry_safe_typed(tage, tmp, &tcd->tcd_pages,
1117                                                    struct trace_page, linkage) {
1118                         __LASSERT_TAGE_INVARIANT(tage);
1119
1120                         list_del(&tage->linkage);
1121                         tage_free(tage);
1122                 }
1123                 tcd->tcd_cur_pages = 0;
1124         }
1125 }
1126
1127 static void trace_cleanup(void)
1128 {
1129         struct page_collection pc;
1130
1131         CFS_INIT_LIST_HEAD(&pc.pc_pages);
1132         spin_lock_init(&pc.pc_lock);
1133
1134         trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
1135
1136         tracefile_fini_arch();
1137 }
1138
1139 void tracefile_exit(void)
1140 {
1141         trace_stop_thread();
1142         trace_cleanup();
1143 }