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