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