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