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