Whamcloud - gitweb
LU-12328 flr: preserve last read mirror
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * libcfs/libcfs/tracefile.c
33  *
34  * Author: Zach Brown <zab@clusterfs.com>
35  * Author: Phil Schwan <phil@clusterfs.com>
36  */
37
38
39 #define DEBUG_SUBSYSTEM S_LNET
40 #define LUSTRE_TRACEFILE_PRIVATE
41 #include "tracefile.h"
42
43 #include <linux/ctype.h>
44 #include <linux/fs.h>
45 #include <linux/kthread.h>
46 #include <linux/pagemap.h>
47 #include <linux/uaccess.h>
48 #include <libcfs/linux/linux-fs.h>
49 #include <libcfs/libcfs.h>
50
51 /* XXX move things up to the top, comment */
52 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
53
54 char cfs_tracefile[TRACEFILE_NAME_SIZE];
55 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
56 static struct tracefiled_ctl trace_tctl;
57 static DEFINE_MUTEX(cfs_trace_thread_mutex);
58 static int thread_running = 0;
59
60 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
61
62 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
63                                         struct cfs_trace_cpu_data *tcd);
64
65 static inline struct cfs_trace_page *
66 cfs_tage_from_list(struct list_head *list)
67 {
68         return list_entry(list, struct cfs_trace_page, linkage);
69 }
70
71 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
72 {
73         struct page            *page;
74         struct cfs_trace_page *tage;
75
76         /* My caller is trying to free memory */
77         if (!in_interrupt() && memory_pressure_get())
78                 return NULL;
79
80         /*
81          * Don't spam console with allocation failures: they will be reported
82          * by upper layer anyway.
83          */
84         gfp |= __GFP_NOWARN;
85         page = alloc_page(gfp);
86         if (page == NULL)
87                 return NULL;
88
89         tage = kmalloc(sizeof(*tage), gfp);
90         if (tage == NULL) {
91                 __free_page(page);
92                 return NULL;
93         }
94
95         tage->page = page;
96         atomic_inc(&cfs_tage_allocated);
97         return tage;
98 }
99
100 static void cfs_tage_free(struct cfs_trace_page *tage)
101 {
102         __LASSERT(tage != NULL);
103         __LASSERT(tage->page != NULL);
104
105         __free_page(tage->page);
106         kfree(tage);
107         atomic_dec(&cfs_tage_allocated);
108 }
109
110 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
111                              struct list_head *queue)
112 {
113         __LASSERT(tage != NULL);
114         __LASSERT(queue != NULL);
115
116         list_move_tail(&tage->linkage, queue);
117 }
118
119 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, gfp_t gfp,
120                            struct list_head *stock)
121 {
122         int i;
123
124         /*
125          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
126          * from here: this will lead to infinite recursion.
127          */
128
129         for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
130                 struct cfs_trace_page *tage;
131
132                 tage = cfs_tage_alloc(gfp);
133                 if (tage == NULL)
134                         break;
135                 list_add_tail(&tage->linkage, stock);
136         }
137         return i;
138 }
139
140 /* return a page that has 'len' bytes left at the end */
141 static struct cfs_trace_page *
142 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
143 {
144         struct cfs_trace_page *tage;
145
146         if (tcd->tcd_cur_pages > 0) {
147                 __LASSERT(!list_empty(&tcd->tcd_pages));
148                 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
149                 if (tage->used + len <= PAGE_SIZE)
150                         return tage;
151         }
152
153         if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
154                 if (tcd->tcd_cur_stock_pages > 0) {
155                         tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
156                         --tcd->tcd_cur_stock_pages;
157                         list_del_init(&tage->linkage);
158                 } else {
159                         tage = cfs_tage_alloc(GFP_ATOMIC);
160                         if (unlikely(tage == NULL)) {
161                                 if ((!memory_pressure_get() ||
162                                      in_interrupt()) && printk_ratelimit())
163                                         printk(KERN_WARNING
164                                                "cannot allocate a tage (%ld)\n",
165                                                tcd->tcd_cur_pages);
166                                 return NULL;
167                         }
168                 }
169
170                 tage->used = 0;
171                 tage->cpu = smp_processor_id();
172                 tage->type = tcd->tcd_type;
173                 list_add_tail(&tage->linkage, &tcd->tcd_pages);
174                 tcd->tcd_cur_pages++;
175
176                 if (tcd->tcd_cur_pages > 8 && thread_running) {
177                         struct tracefiled_ctl *tctl = &trace_tctl;
178                         /*
179                          * wake up tracefiled to process some pages.
180                          */
181                         wake_up(&tctl->tctl_waitq);
182                 }
183                 return tage;
184         }
185         return NULL;
186 }
187
188 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
189 {
190         int pgcount = tcd->tcd_cur_pages / 10;
191         struct page_collection pc;
192         struct cfs_trace_page *tage;
193         struct cfs_trace_page *tmp;
194
195         /*
196          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
197          * from here: this will lead to infinite recursion.
198          */
199
200         if (printk_ratelimit())
201                 printk(KERN_WARNING "debug daemon buffer overflowed; "
202                         "discarding 10%% of pages (%d of %ld)\n",
203                         pgcount + 1, tcd->tcd_cur_pages);
204
205         INIT_LIST_HEAD(&pc.pc_pages);
206
207         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
208                 if (pgcount-- == 0)
209                         break;
210
211                 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 > PAGE_SIZE) {
229                 printk(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                        i;
276         int                        remain;
277         int                        mask = msgdata->msg_mask;
278         char                      *file = (char *)msgdata->msg_file;
279         struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
280
281         if (strchr(file, '/'))
282                 file = strrchr(file, '/') + 1;
283
284         tcd = cfs_trace_get_tcd();
285
286         /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
287          * pins us to a particular CPU.  This avoids an smp_processor_id()
288          * warning on Linux when debugging is enabled. */
289         cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
290
291         if (tcd == NULL)                /* arch may not log in IRQ context */
292                 goto console;
293
294         if (tcd->tcd_cur_pages == 0)
295                 header.ph_flags |= PH_FLAG_FIRST_RECORD;
296
297         if (tcd->tcd_shutting_down) {
298                 cfs_trace_put_tcd(tcd);
299                 tcd = NULL;
300                 goto console;
301         }
302
303         known_size = strlen(file) + 1;
304         if (msgdata->msg_fn)
305                 known_size += strlen(msgdata->msg_fn) + 1;
306
307         if (libcfs_debug_binary)
308                 known_size += sizeof(header);
309
310         /*/
311          * '2' used because vsnprintf return real size required for output
312          * _without_ terminating NULL.
313          * if needed is to small for this format.
314          */
315         for (i = 0; i < 2; i++) {
316                 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
317                 if (tage == NULL) {
318                         if (needed + known_size > PAGE_SIZE)
319                                 mask |= D_ERROR;
320
321                         cfs_trace_put_tcd(tcd);
322                         tcd = NULL;
323                         goto console;
324                 }
325
326                 string_buf = (char *)page_address(tage->page) +
327                                         tage->used + known_size;
328
329                 max_nob = PAGE_SIZE - tage->used - known_size;
330                 if (max_nob <= 0) {
331                         printk(KERN_EMERG "negative max_nob: %d\n",
332                                max_nob);
333                         mask |= D_ERROR;
334                         cfs_trace_put_tcd(tcd);
335                         tcd = NULL;
336                         goto console;
337                 }
338
339                 needed = 0;
340                 if (format1) {
341                         va_copy(ap, args);
342                         needed = vsnprintf(string_buf, max_nob, format1, ap);
343                         va_end(ap);
344                 }
345
346                 if (format2) {
347                         remain = max_nob - needed;
348                         if (remain < 0)
349                                 remain = 0;
350
351                         va_start(ap, format2);
352                         needed += vsnprintf(string_buf + needed, remain,
353                                             format2, ap);
354                         va_end(ap);
355                 }
356
357                 if (needed < max_nob) /* well. printing ok.. */
358                         break;
359         }
360
361         if (*(string_buf + needed - 1) != '\n') {
362                 printk(KERN_INFO "format at %s:%d:%s doesn't end in "
363                        "newline\n", file, msgdata->msg_line, msgdata->msg_fn);
364         } else if (mask & D_TTY) {
365                 /* TTY needs '\r\n' to move carriage to leftmost position */
366                 if (needed < 2 || *(string_buf + needed - 2) != '\r')
367                         printk(KERN_INFO "format at %s:%d:%s doesn't end in "
368                                "'\\r\\n'\n", file, msgdata->msg_line,
369                                msgdata->msg_fn);
370         }
371
372         header.ph_len = known_size + needed;
373         debug_buf = (char *)page_address(tage->page) + tage->used;
374
375         if (libcfs_debug_binary) {
376                 memcpy(debug_buf, &header, sizeof(header));
377                 tage->used += sizeof(header);
378                 debug_buf += sizeof(header);
379         }
380
381         strcpy(debug_buf, file);
382         tage->used += strlen(file) + 1;
383         debug_buf += strlen(file) + 1;
384
385         if (msgdata->msg_fn) {
386                 strcpy(debug_buf, msgdata->msg_fn);
387                 tage->used += strlen(msgdata->msg_fn) + 1;
388                 debug_buf += strlen(msgdata->msg_fn) + 1;
389         }
390
391         __LASSERT(debug_buf == string_buf);
392
393         tage->used += needed;
394         __LASSERT(tage->used <= PAGE_SIZE);
395
396 console:
397         if ((mask & libcfs_printk) == 0) {
398                 /* no console output requested */
399                 if (tcd != NULL)
400                         cfs_trace_put_tcd(tcd);
401                 return 1;
402         }
403
404         if (cdls != NULL) {
405                 if (libcfs_console_ratelimit &&
406                     cdls->cdls_next != 0 &&     /* not first time ever */
407                     time_before(jiffies, cdls->cdls_next)) {
408                         /* skipping a console message */
409                         cdls->cdls_count++;
410                         if (tcd != NULL)
411                                 cfs_trace_put_tcd(tcd);
412                         return 1;
413                 }
414
415                 if (time_after(jiffies, cdls->cdls_next +
416                                         libcfs_console_max_delay +
417                                         cfs_time_seconds(10))) {
418                         /* last timeout was a long time ago */
419                         cdls->cdls_delay /= libcfs_console_backoff * 4;
420                 } else {
421                         cdls->cdls_delay *= libcfs_console_backoff;
422                 }
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                 /* ensure cdls_next is never zero after it's been seen */
430                 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
431         }
432
433         if (tcd != NULL) {
434                 cfs_print_to_console(&header, mask, string_buf, needed, file,
435                                      msgdata->msg_fn);
436                 cfs_trace_put_tcd(tcd);
437         } else {
438                 string_buf = cfs_trace_get_console_buffer();
439
440                 needed = 0;
441                 if (format1 != NULL) {
442                         va_copy(ap, args);
443                         needed = vsnprintf(string_buf,
444                                            CFS_TRACE_CONSOLE_BUFFER_SIZE,
445                                            format1, ap);
446                         va_end(ap);
447                 }
448                 if (format2 != NULL) {
449                         remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
450                         if (remain > 0) {
451                                 va_start(ap, format2);
452                                 needed += vsnprintf(string_buf+needed, remain,
453                                                     format2, ap);
454                                 va_end(ap);
455                         }
456                 }
457                 cfs_print_to_console(&header, mask,
458                                      string_buf, needed, file, msgdata->msg_fn);
459
460                 put_cpu();
461         }
462
463         if (cdls != NULL && cdls->cdls_count != 0) {
464                 string_buf = cfs_trace_get_console_buffer();
465
466                 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
467                                   "Skipped %d previous similar message%s\n",
468                                   cdls->cdls_count,
469                                   (cdls->cdls_count > 1) ? "s" : "");
470
471                 /* Do not allow print this to TTY */
472                 cfs_print_to_console(&header, mask & ~D_TTY, string_buf,
473                                      needed, file, msgdata->msg_fn);
474
475                 put_cpu();
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         smp_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         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         INIT_LIST_HEAD(&pc->pc_pages);
514
515         cfs_tcd_for_each(tcd, i, j) {
516                 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
517                 tcd->tcd_cur_pages = 0;
518
519                 if (pc->pc_want_daemon_pages) {
520                         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         for_each_possible_cpu(cpu) {
533                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
534                         list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
535                         tcd->tcd_cur_pages = 0;
536                         if (pc->pc_want_daemon_pages) {
537                                 list_splice_init(&tcd->tcd_daemon_pages,
538                                                         &pc->pc_pages);
539                                 tcd->tcd_cur_daemon_pages = 0;
540                         }
541                 }
542         }
543 }
544
545 static void collect_pages(struct page_collection *pc)
546 {
547         INIT_LIST_HEAD(&pc->pc_pages);
548
549         if (libcfs_panic_in_progress)
550                 panic_collect_pages(pc);
551         else
552                 collect_pages_on_all_cpus(pc);
553 }
554
555 static void put_pages_back_on_all_cpus(struct page_collection *pc)
556 {
557         struct cfs_trace_cpu_data *tcd;
558         struct list_head *cur_head;
559         struct cfs_trace_page *tage;
560         struct cfs_trace_page *tmp;
561         int i, cpu;
562
563         for_each_possible_cpu(cpu) {
564                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
565                         cur_head = tcd->tcd_pages.next;
566
567                         list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
568                                                  linkage) {
569
570                                 __LASSERT_TAGE_INVARIANT(tage);
571
572                                 if (tage->cpu != cpu || tage->type != i)
573                                         continue;
574
575                                 cfs_tage_to_tail(tage, cur_head);
576                                 tcd->tcd_cur_pages++;
577                         }
578                 }
579         }
580 }
581
582 static void put_pages_back(struct page_collection *pc)
583 {
584         if (!libcfs_panic_in_progress)
585                 put_pages_back_on_all_cpus(pc);
586 }
587
588 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
589  * we have a good amount of data at all times for dumping during an LBUG, even
590  * if we have been steadily writing (and otherwise discarding) pages via the
591  * debug daemon. */
592 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
593                                          struct cfs_trace_cpu_data *tcd)
594 {
595         struct cfs_trace_page *tage;
596         struct cfs_trace_page *tmp;
597
598         list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
599                 __LASSERT_TAGE_INVARIANT(tage);
600
601                 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
602                         continue;
603
604                 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
605                 tcd->tcd_cur_daemon_pages++;
606
607                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
608                         struct cfs_trace_page *victim;
609
610                         __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
611                         victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
612
613                         __LASSERT_TAGE_INVARIANT(victim);
614
615                         list_del(&victim->linkage);
616                         cfs_tage_free(victim);
617                         tcd->tcd_cur_daemon_pages--;
618                 }
619         }
620 }
621
622 static void put_pages_on_daemon_list(struct page_collection *pc)
623 {
624         struct cfs_trace_cpu_data *tcd;
625         int i, cpu;
626
627         for_each_possible_cpu(cpu) {
628                 cfs_tcd_for_each_type_lock(tcd, i, cpu)
629                         put_pages_on_tcd_daemon_list(pc, tcd);
630         }
631 }
632
633 void cfs_trace_debug_print(void)
634 {
635         struct page_collection pc;
636         struct cfs_trace_page *tage;
637         struct cfs_trace_page *tmp;
638
639         pc.pc_want_daemon_pages = 1;
640         collect_pages(&pc);
641         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
642                 char *p, *file, *fn;
643                 struct page *page;
644
645                 __LASSERT_TAGE_INVARIANT(tage);
646
647                 page = tage->page;
648                 p = page_address(page);
649                 while (p < ((char *)page_address(page) + tage->used)) {
650                         struct ptldebug_header *hdr;
651                         int len;
652                         hdr = (void *)p;
653                         p += sizeof(*hdr);
654                         file = p;
655                         p += strlen(file) + 1;
656                         fn = p;
657                         p += strlen(fn) + 1;
658                         len = hdr->ph_len - (int)(p - (char *)hdr);
659
660                         cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
661
662                         p += len;
663                 }
664
665                 list_del(&tage->linkage);
666                 cfs_tage_free(tage);
667         }
668 }
669
670 int cfs_tracefile_dump_all_pages(char *filename)
671 {
672         struct page_collection  pc;
673         struct file             *filp;
674         struct cfs_trace_page   *tage;
675         struct cfs_trace_page   *tmp;
676         char                    *buf;
677         int rc;
678
679         cfs_tracefile_write_lock();
680
681         filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
682         if (IS_ERR(filp)) {
683                 rc = PTR_ERR(filp);
684                 filp = NULL;
685                 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
686                       filename, rc);
687                 goto out;
688         }
689
690         pc.pc_want_daemon_pages = 1;
691         collect_pages(&pc);
692         if (list_empty(&pc.pc_pages)) {
693                 rc = 0;
694                 goto close;
695         }
696
697         /* ok, for now, just write the pages.  in the future we'll be building
698          * iobufs with the pages and calling generic_direct_IO */
699         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
700
701                 __LASSERT_TAGE_INVARIANT(tage);
702
703                 buf = kmap(tage->page);
704                 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
705                 kunmap(tage->page);
706                 if (rc != (int)tage->used) {
707                         printk(KERN_WARNING "wanted to write %u but wrote "
708                                "%d\n", tage->used, rc);
709                         put_pages_back(&pc);
710                         __LASSERT(list_empty(&pc.pc_pages));
711                         break;
712                 }
713                 list_del(&tage->linkage);
714                 cfs_tage_free(tage);
715         }
716
717         rc = ll_vfs_fsync_range(filp, 0, LLONG_MAX, 1);
718         if (rc)
719                 printk(KERN_ERR "sync returns %d\n", rc);
720 close:
721         filp_close(filp, NULL);
722 out:
723         cfs_tracefile_write_unlock();
724         return rc;
725 }
726
727 void cfs_trace_flush_pages(void)
728 {
729         struct page_collection pc;
730         struct cfs_trace_page *tage;
731         struct cfs_trace_page *tmp;
732
733         pc.pc_want_daemon_pages = 1;
734         collect_pages(&pc);
735         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
736
737                 __LASSERT_TAGE_INVARIANT(tage);
738
739                 list_del(&tage->linkage);
740                 cfs_tage_free(tage);
741         }
742 }
743
744 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
745                             const char __user *usr_buffer, int usr_buffer_nob)
746 {
747         int    nob;
748
749         if (usr_buffer_nob > knl_buffer_nob)
750                 return -EOVERFLOW;
751
752         if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
753                 return -EFAULT;
754
755         nob = strnlen(knl_buffer, usr_buffer_nob);
756         while (nob-- >= 0)                      /* strip trailing whitespace */
757                 if (!isspace(knl_buffer[nob]))
758                         break;
759
760         if (nob < 0)                            /* empty string */
761                 return -EINVAL;
762
763         if (nob == knl_buffer_nob)              /* no space to terminate */
764                 return -EOVERFLOW;
765
766         knl_buffer[nob + 1] = 0;                /* terminate */
767         return 0;
768 }
769 EXPORT_SYMBOL(cfs_trace_copyin_string);
770
771 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
772                              const char *knl_buffer, char *append)
773 {
774         /* NB if 'append' != NULL, it's a single character to append to the
775          * copied out string - usually "\n", for /proc entries and "" (i.e. a
776          * terminating zero byte) for sysctl entries */
777         int   nob = strlen(knl_buffer);
778
779         if (nob > usr_buffer_nob)
780                 nob = usr_buffer_nob;
781
782         if (copy_to_user(usr_buffer, knl_buffer, nob))
783                 return -EFAULT;
784
785         if (append != NULL && nob < usr_buffer_nob) {
786                 if (copy_to_user(usr_buffer + nob, append, 1))
787                         return -EFAULT;
788
789                 nob++;
790         }
791
792         return nob;
793 }
794 EXPORT_SYMBOL(cfs_trace_copyout_string);
795
796 int cfs_trace_allocate_string_buffer(char **str, int nob)
797 {
798         if (nob > 2 * PAGE_SIZE)        /* string must be "sensible" */
799                 return -EINVAL;
800
801         *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
802         if (*str == NULL)
803                 return -ENOMEM;
804
805         return 0;
806 }
807
808 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
809 {
810         char         *str;
811         int           rc;
812
813         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
814         if (rc != 0)
815                 return rc;
816
817         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
818                                      usr_str, usr_str_nob);
819         if (rc != 0)
820                 goto out;
821
822         if (str[0] != '/') {
823                 rc = -EINVAL;
824                 goto out;
825         }
826         rc = cfs_tracefile_dump_all_pages(str);
827 out:
828         kfree(str);
829         return rc;
830 }
831
832 int cfs_trace_daemon_command(char *str)
833 {
834         int       rc = 0;
835
836         cfs_tracefile_write_lock();
837
838         if (strcmp(str, "stop") == 0) {
839                 cfs_tracefile_write_unlock();
840                 cfs_trace_stop_thread();
841                 cfs_tracefile_write_lock();
842                 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
843
844         } else if (strncmp(str, "size=", 5) == 0) {
845                 unsigned long tmp;
846
847                 rc = kstrtoul(str + 5, 10, &tmp);
848                 if (!rc) {
849                         if (tmp < 10 || tmp > 20480)
850                                 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
851                         else
852                                 cfs_tracefile_size = tmp << 20;
853                 }
854         } else if (strlen(str) >= sizeof(cfs_tracefile)) {
855                 rc = -ENAMETOOLONG;
856         } else if (str[0] != '/') {
857                 rc = -EINVAL;
858         } else {
859                 strcpy(cfs_tracefile, str);
860
861                 printk(KERN_INFO
862                        "Lustre: debug daemon will attempt to start writing "
863                        "to %s (%lukB max)\n", cfs_tracefile,
864                        (long)(cfs_tracefile_size >> 10));
865
866                 cfs_trace_start_thread();
867         }
868
869         cfs_tracefile_write_unlock();
870         return rc;
871 }
872
873 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
874 {
875         char *str;
876         int   rc;
877
878         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
879         if (rc != 0)
880                 return rc;
881
882         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
883                                  usr_str, usr_str_nob);
884         if (rc == 0)
885                 rc = cfs_trace_daemon_command(str);
886
887         kfree(str);
888         return rc;
889 }
890
891 int cfs_trace_set_debug_mb(int mb)
892 {
893         int i;
894         int j;
895         int pages;
896         int limit = cfs_trace_max_debug_mb();
897         struct cfs_trace_cpu_data *tcd;
898
899         if (mb < num_possible_cpus()) {
900                 printk(KERN_WARNING
901                        "Lustre: %d MB is too small for debug buffer size, "
902                        "setting it to %d MB.\n", mb, num_possible_cpus());
903                 mb = num_possible_cpus();
904         }
905
906         if (mb > limit) {
907                 printk(KERN_WARNING
908                        "Lustre: %d MB is too large for debug buffer size, "
909                        "setting it to %d MB.\n", mb, limit);
910                 mb = limit;
911         }
912
913         mb /= num_possible_cpus();
914         pages = mb << (20 - PAGE_SHIFT);
915
916         cfs_tracefile_write_lock();
917
918         cfs_tcd_for_each(tcd, i, j)
919                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
920
921         cfs_tracefile_write_unlock();
922
923         return 0;
924 }
925
926 int cfs_trace_get_debug_mb(void)
927 {
928         int i;
929         int j;
930         struct cfs_trace_cpu_data *tcd;
931         int total_pages = 0;
932
933         cfs_tracefile_read_lock();
934
935         cfs_tcd_for_each(tcd, i, j)
936                 total_pages += tcd->tcd_max_pages;
937
938         cfs_tracefile_read_unlock();
939
940         return (total_pages >> (20 - PAGE_SHIFT)) + 1;
941 }
942
943 static int tracefiled(void *arg)
944 {
945         struct page_collection pc;
946         struct tracefiled_ctl *tctl = arg;
947         struct cfs_trace_page *tage;
948         struct cfs_trace_page *tmp;
949         struct file *filp;
950         char *buf;
951         int last_loop = 0;
952         int rc;
953
954         /* we're started late enough that we pick up init's fs context */
955         /* this is so broken in uml?  what on earth is going on? */
956
957         complete(&tctl->tctl_start);
958
959         while (1) {
960                 wait_queue_entry_t __wait;
961
962                 pc.pc_want_daemon_pages = 0;
963                 collect_pages(&pc);
964                 if (list_empty(&pc.pc_pages))
965                         goto end_loop;
966
967                 filp = NULL;
968                 cfs_tracefile_read_lock();
969                 if (cfs_tracefile[0] != 0) {
970                         filp = filp_open(cfs_tracefile,
971                                          O_CREAT | O_RDWR | O_LARGEFILE,
972                                          0600);
973                         if (IS_ERR(filp)) {
974                                 rc = PTR_ERR(filp);
975                                 filp = NULL;
976                                 printk(KERN_WARNING "couldn't open %s: "
977                                        "%d\n", cfs_tracefile, rc);
978                         }
979                 }
980                 cfs_tracefile_read_unlock();
981                 if (filp == NULL) {
982                         put_pages_on_daemon_list(&pc);
983                         __LASSERT(list_empty(&pc.pc_pages));
984                         goto end_loop;
985                 }
986
987                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
988                         struct dentry *de = file_dentry(filp);
989                         static loff_t f_pos;
990
991                         __LASSERT_TAGE_INVARIANT(tage);
992
993                         if (f_pos >= (off_t)cfs_tracefile_size)
994                                 f_pos = 0;
995                         else if (f_pos > i_size_read(de->d_inode))
996                                 f_pos = i_size_read(de->d_inode);
997
998                         buf = kmap(tage->page);
999                         rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
1000                         kunmap(tage->page);
1001                         if (rc != (int)tage->used) {
1002                                 printk(KERN_WARNING "wanted to write %u "
1003                                        "but wrote %d\n", tage->used, rc);
1004                                 put_pages_back(&pc);
1005                                 __LASSERT(list_empty(&pc.pc_pages));
1006                                 break;
1007                         }
1008                 }
1009
1010                 filp_close(filp, NULL);
1011                 put_pages_on_daemon_list(&pc);
1012                 if (!list_empty(&pc.pc_pages)) {
1013                         int i;
1014
1015                         printk(KERN_ALERT "Lustre: trace pages aren't "
1016                                " empty\n");
1017                         printk(KERN_ERR "total cpus(%d): ",
1018                                num_possible_cpus());
1019                         for (i = 0; i < num_possible_cpus(); i++)
1020                                 if (cpu_online(i))
1021                                         printk(KERN_ERR "%d(on) ", i);
1022                                 else
1023                                         printk(KERN_ERR "%d(off) ", i);
1024                         printk(KERN_ERR "\n");
1025
1026                         i = 0;
1027                         list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1028                                                      linkage)
1029                                 printk(KERN_ERR "page %d belongs to cpu "
1030                                        "%d\n", ++i, tage->cpu);
1031                         printk(KERN_ERR "There are %d pages unwritten\n",
1032                                i);
1033                 }
1034                 __LASSERT(list_empty(&pc.pc_pages));
1035 end_loop:
1036                 if (atomic_read(&tctl->tctl_shutdown)) {
1037                         if (last_loop == 0) {
1038                                 last_loop = 1;
1039                                 continue;
1040                         } else {
1041                                 break;
1042                         }
1043                 }
1044                 init_waitqueue_entry(&__wait, current);
1045                 add_wait_queue(&tctl->tctl_waitq, &__wait);
1046                 set_current_state(TASK_INTERRUPTIBLE);
1047                 schedule_timeout(cfs_time_seconds(1));
1048                 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1049         }
1050         complete(&tctl->tctl_stop);
1051         return 0;
1052 }
1053
1054 int cfs_trace_start_thread(void)
1055 {
1056         struct tracefiled_ctl *tctl = &trace_tctl;
1057         int rc = 0;
1058
1059         mutex_lock(&cfs_trace_thread_mutex);
1060         if (thread_running)
1061                 goto out;
1062
1063         init_completion(&tctl->tctl_start);
1064         init_completion(&tctl->tctl_stop);
1065         init_waitqueue_head(&tctl->tctl_waitq);
1066         atomic_set(&tctl->tctl_shutdown, 0);
1067
1068         if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1069                 rc = -ECHILD;
1070                 goto out;
1071         }
1072
1073         wait_for_completion(&tctl->tctl_start);
1074         thread_running = 1;
1075 out:
1076         mutex_unlock(&cfs_trace_thread_mutex);
1077         return rc;
1078 }
1079
1080 void cfs_trace_stop_thread(void)
1081 {
1082         struct tracefiled_ctl *tctl = &trace_tctl;
1083
1084         mutex_lock(&cfs_trace_thread_mutex);
1085         if (thread_running) {
1086                 printk(KERN_INFO
1087                        "Lustre: shutting down debug daemon thread...\n");
1088                 atomic_set(&tctl->tctl_shutdown, 1);
1089                 wait_for_completion(&tctl->tctl_stop);
1090                 thread_running = 0;
1091         }
1092         mutex_unlock(&cfs_trace_thread_mutex);
1093 }
1094
1095 int cfs_tracefile_init(int max_pages)
1096 {
1097         struct cfs_trace_cpu_data *tcd;
1098         int     i;
1099         int     j;
1100         int     rc;
1101         int     factor;
1102
1103         rc = cfs_tracefile_init_arch();
1104         if (rc != 0)
1105                 return rc;
1106
1107         cfs_tcd_for_each(tcd, i, j) {
1108                 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1109                 factor = tcd->tcd_pages_factor;
1110                 INIT_LIST_HEAD(&tcd->tcd_pages);
1111                 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1112                 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1113                 tcd->tcd_cur_pages = 0;
1114                 tcd->tcd_cur_stock_pages = 0;
1115                 tcd->tcd_cur_daemon_pages = 0;
1116                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1117                 LASSERT(tcd->tcd_max_pages > 0);
1118                 tcd->tcd_shutting_down = 0;
1119         }
1120         return 0;
1121 }
1122
1123 static void trace_cleanup_on_all_cpus(void)
1124 {
1125         struct cfs_trace_cpu_data *tcd;
1126         struct cfs_trace_page *tage;
1127         struct cfs_trace_page *tmp;
1128         int i, cpu;
1129
1130         for_each_possible_cpu(cpu) {
1131                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1132                         tcd->tcd_shutting_down = 1;
1133
1134                         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1135                                 __LASSERT_TAGE_INVARIANT(tage);
1136
1137                                 list_del(&tage->linkage);
1138                                 cfs_tage_free(tage);
1139                         }
1140                         tcd->tcd_cur_pages = 0;
1141                 }
1142         }
1143 }
1144
1145 static void cfs_trace_cleanup(void)
1146 {
1147         struct page_collection pc;
1148
1149         INIT_LIST_HEAD(&pc.pc_pages);
1150
1151         trace_cleanup_on_all_cpus();
1152
1153         cfs_tracefile_fini_arch();
1154 }
1155
1156 void cfs_tracefile_exit(void)
1157 {
1158         cfs_trace_stop_thread();
1159         cfs_trace_cleanup();
1160 }