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