Whamcloud - gitweb
LU-10560 libcfs: Use kernel_write when appropriate
[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         char                    *buf;
670         int rc;
671
672         cfs_tracefile_write_lock();
673
674         filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
675         if (IS_ERR(filp)) {
676                 rc = PTR_ERR(filp);
677                 filp = NULL;
678                 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
679                       filename, rc);
680                 goto out;
681         }
682
683         pc.pc_want_daemon_pages = 1;
684         collect_pages(&pc);
685         if (list_empty(&pc.pc_pages)) {
686                 rc = 0;
687                 goto close;
688         }
689
690         /* ok, for now, just write the pages.  in the future we'll be building
691          * iobufs with the pages and calling generic_direct_IO */
692         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
693
694                 __LASSERT_TAGE_INVARIANT(tage);
695
696                 buf = kmap(tage->page);
697                 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
698                 kunmap(tage->page);
699                 if (rc != (int)tage->used) {
700                         printk(KERN_WARNING "wanted to write %u but wrote "
701                                "%d\n", tage->used, rc);
702                         put_pages_back(&pc);
703                         __LASSERT(list_empty(&pc.pc_pages));
704                         break;
705                 }
706                 list_del(&tage->linkage);
707                 cfs_tage_free(tage);
708         }
709
710         rc = ll_vfs_fsync_range(filp, 0, LLONG_MAX, 1);
711         if (rc)
712                 printk(KERN_ERR "sync returns %d\n", rc);
713 close:
714         filp_close(filp, NULL);
715 out:
716         cfs_tracefile_write_unlock();
717         return rc;
718 }
719
720 void cfs_trace_flush_pages(void)
721 {
722         struct page_collection pc;
723         struct cfs_trace_page *tage;
724         struct cfs_trace_page *tmp;
725
726         pc.pc_want_daemon_pages = 1;
727         collect_pages(&pc);
728         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
729
730                 __LASSERT_TAGE_INVARIANT(tage);
731
732                 list_del(&tage->linkage);
733                 cfs_tage_free(tage);
734         }
735 }
736
737 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
738                             const char __user *usr_buffer, int usr_buffer_nob)
739 {
740         int    nob;
741
742         if (usr_buffer_nob > knl_buffer_nob)
743                 return -EOVERFLOW;
744
745         if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
746                 return -EFAULT;
747
748         nob = strnlen(knl_buffer, usr_buffer_nob);
749         while (nob-- >= 0)                      /* strip trailing whitespace */
750                 if (!isspace(knl_buffer[nob]))
751                         break;
752
753         if (nob < 0)                            /* empty string */
754                 return -EINVAL;
755
756         if (nob == knl_buffer_nob)              /* no space to terminate */
757                 return -EOVERFLOW;
758
759         knl_buffer[nob + 1] = 0;                /* terminate */
760         return 0;
761 }
762 EXPORT_SYMBOL(cfs_trace_copyin_string);
763
764 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
765                              const char *knl_buffer, char *append)
766 {
767         /* NB if 'append' != NULL, it's a single character to append to the
768          * copied out string - usually "\n", for /proc entries and "" (i.e. a
769          * terminating zero byte) for sysctl entries */
770         int   nob = strlen(knl_buffer);
771
772         if (nob > usr_buffer_nob)
773                 nob = usr_buffer_nob;
774
775         if (copy_to_user(usr_buffer, knl_buffer, nob))
776                 return -EFAULT;
777
778         if (append != NULL && nob < usr_buffer_nob) {
779                 if (copy_to_user(usr_buffer + nob, append, 1))
780                         return -EFAULT;
781
782                 nob++;
783         }
784
785         return nob;
786 }
787 EXPORT_SYMBOL(cfs_trace_copyout_string);
788
789 int cfs_trace_allocate_string_buffer(char **str, int nob)
790 {
791         if (nob > 2 * PAGE_SIZE)        /* string must be "sensible" */
792                 return -EINVAL;
793
794         *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
795         if (*str == NULL)
796                 return -ENOMEM;
797
798         return 0;
799 }
800
801 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
802 {
803         char         *str;
804         int           rc;
805
806         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
807         if (rc != 0)
808                 return rc;
809
810         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
811                                      usr_str, usr_str_nob);
812         if (rc != 0)
813                 goto out;
814
815         if (str[0] != '/') {
816                 rc = -EINVAL;
817                 goto out;
818         }
819         rc = cfs_tracefile_dump_all_pages(str);
820 out:
821         kfree(str);
822         return rc;
823 }
824
825 int cfs_trace_daemon_command(char *str)
826 {
827         int       rc = 0;
828
829         cfs_tracefile_write_lock();
830
831         if (strcmp(str, "stop") == 0) {
832                 cfs_tracefile_write_unlock();
833                 cfs_trace_stop_thread();
834                 cfs_tracefile_write_lock();
835                 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
836
837         } else if (strncmp(str, "size=", 5) == 0) {
838                 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
839                 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
840                         cfs_tracefile_size = CFS_TRACEFILE_SIZE;
841                 else
842                         cfs_tracefile_size <<= 20;
843
844         } else if (strlen(str) >= sizeof(cfs_tracefile)) {
845                 rc = -ENAMETOOLONG;
846         } else if (str[0] != '/') {
847                 rc = -EINVAL;
848         } else {
849                 strcpy(cfs_tracefile, str);
850
851                 printk(KERN_INFO
852                        "Lustre: debug daemon will attempt to start writing "
853                        "to %s (%lukB max)\n", cfs_tracefile,
854                        (long)(cfs_tracefile_size >> 10));
855
856                 cfs_trace_start_thread();
857         }
858
859         cfs_tracefile_write_unlock();
860         return rc;
861 }
862
863 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
864 {
865         char *str;
866         int   rc;
867
868         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
869         if (rc != 0)
870                 return rc;
871
872         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
873                                  usr_str, usr_str_nob);
874         if (rc == 0)
875                 rc = cfs_trace_daemon_command(str);
876
877         kfree(str);
878         return rc;
879 }
880
881 int cfs_trace_set_debug_mb(int mb)
882 {
883         int i;
884         int j;
885         int pages;
886         int limit = cfs_trace_max_debug_mb();
887         struct cfs_trace_cpu_data *tcd;
888
889         if (mb < num_possible_cpus()) {
890                 printk(KERN_WARNING
891                        "Lustre: %d MB is too small for debug buffer size, "
892                        "setting it to %d MB.\n", mb, num_possible_cpus());
893                 mb = num_possible_cpus();
894         }
895
896         if (mb > limit) {
897                 printk(KERN_WARNING
898                        "Lustre: %d MB is too large for debug buffer size, "
899                        "setting it to %d MB.\n", mb, limit);
900                 mb = limit;
901         }
902
903         mb /= num_possible_cpus();
904         pages = mb << (20 - PAGE_SHIFT);
905
906         cfs_tracefile_write_lock();
907
908         cfs_tcd_for_each(tcd, i, j)
909                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
910
911         cfs_tracefile_write_unlock();
912
913         return 0;
914 }
915
916 int cfs_trace_get_debug_mb(void)
917 {
918         int i;
919         int j;
920         struct cfs_trace_cpu_data *tcd;
921         int total_pages = 0;
922
923         cfs_tracefile_read_lock();
924
925         cfs_tcd_for_each(tcd, i, j)
926                 total_pages += tcd->tcd_max_pages;
927
928         cfs_tracefile_read_unlock();
929
930         return (total_pages >> (20 - PAGE_SHIFT)) + 1;
931 }
932
933 static int tracefiled(void *arg)
934 {
935         struct page_collection pc;
936         struct tracefiled_ctl *tctl = arg;
937         struct cfs_trace_page *tage;
938         struct cfs_trace_page *tmp;
939         struct file *filp;
940         char *buf;
941         int last_loop = 0;
942         int rc;
943
944         /* we're started late enough that we pick up init's fs context */
945         /* this is so broken in uml?  what on earth is going on? */
946
947         complete(&tctl->tctl_start);
948
949         while (1) {
950                 wait_queue_entry_t __wait;
951
952                 pc.pc_want_daemon_pages = 0;
953                 collect_pages(&pc);
954                 if (list_empty(&pc.pc_pages))
955                         goto end_loop;
956
957                 filp = NULL;
958                 cfs_tracefile_read_lock();
959                 if (cfs_tracefile[0] != 0) {
960                         filp = filp_open(cfs_tracefile,
961                                          O_CREAT | O_RDWR | O_LARGEFILE,
962                                          0600);
963                         if (IS_ERR(filp)) {
964                                 rc = PTR_ERR(filp);
965                                 filp = NULL;
966                                 printk(KERN_WARNING "couldn't open %s: "
967                                        "%d\n", cfs_tracefile, rc);
968                         }
969                 }
970                 cfs_tracefile_read_unlock();
971                 if (filp == NULL) {
972                         put_pages_on_daemon_list(&pc);
973                         __LASSERT(list_empty(&pc.pc_pages));
974                         goto end_loop;
975                 }
976
977                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
978                         struct dentry *de = file_dentry(filp);
979                         static loff_t f_pos;
980
981                         __LASSERT_TAGE_INVARIANT(tage);
982
983                         if (f_pos >= (off_t)cfs_tracefile_size)
984                                 f_pos = 0;
985                         else if (f_pos > i_size_read(de->d_inode))
986                                 f_pos = i_size_read(de->d_inode);
987
988                         buf = kmap(tage->page);
989                         rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
990                         kunmap(tage->page);
991                         if (rc != (int)tage->used) {
992                                 printk(KERN_WARNING "wanted to write %u "
993                                        "but wrote %d\n", tage->used, rc);
994                                 put_pages_back(&pc);
995                                 __LASSERT(list_empty(&pc.pc_pages));
996                                 break;
997                         }
998                 }
999
1000                 filp_close(filp, NULL);
1001                 put_pages_on_daemon_list(&pc);
1002                 if (!list_empty(&pc.pc_pages)) {
1003                         int i;
1004
1005                         printk(KERN_ALERT "Lustre: trace pages aren't "
1006                                " empty\n");
1007                         printk(KERN_ERR "total cpus(%d): ",
1008                                num_possible_cpus());
1009                         for (i = 0; i < num_possible_cpus(); i++)
1010                                 if (cpu_online(i))
1011                                         printk(KERN_ERR "%d(on) ", i);
1012                                 else
1013                                         printk(KERN_ERR "%d(off) ", i);
1014                         printk(KERN_ERR "\n");
1015
1016                         i = 0;
1017                         list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1018                                                      linkage)
1019                                 printk(KERN_ERR "page %d belongs to cpu "
1020                                        "%d\n", ++i, tage->cpu);
1021                         printk(KERN_ERR "There are %d pages unwritten\n",
1022                                i);
1023                 }
1024                 __LASSERT(list_empty(&pc.pc_pages));
1025 end_loop:
1026                 if (atomic_read(&tctl->tctl_shutdown)) {
1027                         if (last_loop == 0) {
1028                                 last_loop = 1;
1029                                 continue;
1030                         } else {
1031                                 break;
1032                         }
1033                 }
1034                 init_waitqueue_entry(&__wait, current);
1035                 add_wait_queue(&tctl->tctl_waitq, &__wait);
1036                 set_current_state(TASK_INTERRUPTIBLE);
1037                 schedule_timeout(cfs_time_seconds(1));
1038                 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1039         }
1040         complete(&tctl->tctl_stop);
1041         return 0;
1042 }
1043
1044 int cfs_trace_start_thread(void)
1045 {
1046         struct tracefiled_ctl *tctl = &trace_tctl;
1047         int rc = 0;
1048
1049         mutex_lock(&cfs_trace_thread_mutex);
1050         if (thread_running)
1051                 goto out;
1052
1053         init_completion(&tctl->tctl_start);
1054         init_completion(&tctl->tctl_stop);
1055         init_waitqueue_head(&tctl->tctl_waitq);
1056         atomic_set(&tctl->tctl_shutdown, 0);
1057
1058         if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1059                 rc = -ECHILD;
1060                 goto out;
1061         }
1062
1063         wait_for_completion(&tctl->tctl_start);
1064         thread_running = 1;
1065 out:
1066         mutex_unlock(&cfs_trace_thread_mutex);
1067         return rc;
1068 }
1069
1070 void cfs_trace_stop_thread(void)
1071 {
1072         struct tracefiled_ctl *tctl = &trace_tctl;
1073
1074         mutex_lock(&cfs_trace_thread_mutex);
1075         if (thread_running) {
1076                 printk(KERN_INFO
1077                        "Lustre: shutting down debug daemon thread...\n");
1078                 atomic_set(&tctl->tctl_shutdown, 1);
1079                 wait_for_completion(&tctl->tctl_stop);
1080                 thread_running = 0;
1081         }
1082         mutex_unlock(&cfs_trace_thread_mutex);
1083 }
1084
1085 int cfs_tracefile_init(int max_pages)
1086 {
1087         struct cfs_trace_cpu_data *tcd;
1088         int     i;
1089         int     j;
1090         int     rc;
1091         int     factor;
1092
1093         rc = cfs_tracefile_init_arch();
1094         if (rc != 0)
1095                 return rc;
1096
1097         cfs_tcd_for_each(tcd, i, j) {
1098                 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1099                 factor = tcd->tcd_pages_factor;
1100                 INIT_LIST_HEAD(&tcd->tcd_pages);
1101                 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1102                 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1103                 tcd->tcd_cur_pages = 0;
1104                 tcd->tcd_cur_stock_pages = 0;
1105                 tcd->tcd_cur_daemon_pages = 0;
1106                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1107                 LASSERT(tcd->tcd_max_pages > 0);
1108                 tcd->tcd_shutting_down = 0;
1109         }
1110         return 0;
1111 }
1112
1113 static void trace_cleanup_on_all_cpus(void)
1114 {
1115         struct cfs_trace_cpu_data *tcd;
1116         struct cfs_trace_page *tage;
1117         struct cfs_trace_page *tmp;
1118         int i, cpu;
1119
1120         for_each_possible_cpu(cpu) {
1121                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1122                         tcd->tcd_shutting_down = 1;
1123
1124                         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1125                                 __LASSERT_TAGE_INVARIANT(tage);
1126
1127                                 list_del(&tage->linkage);
1128                                 cfs_tage_free(tage);
1129                         }
1130                         tcd->tcd_cur_pages = 0;
1131                 }
1132         }
1133 }
1134
1135 static void cfs_trace_cleanup(void)
1136 {
1137         struct page_collection pc;
1138
1139         INIT_LIST_HEAD(&pc.pc_pages);
1140
1141         trace_cleanup_on_all_cpus();
1142
1143         cfs_tracefile_fini_arch();
1144 }
1145
1146 void cfs_tracefile_exit(void)
1147 {
1148         cfs_trace_stop_thread();
1149         cfs_trace_cleanup();
1150 }