Whamcloud - gitweb
LU-6245 libcfs: remove prim wrappers for libcfs
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * libcfs/libcfs/tracefile.c
37  *
38  * Author: Zach Brown <zab@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  */
41
42
43 #define DEBUG_SUBSYSTEM S_LNET
44 #define LUSTRE_TRACEFILE_PRIVATE
45 #include "tracefile.h"
46
47 #include <linux/kthread.h>
48 #include <libcfs/libcfs.h>
49
50 /* XXX move things up to the top, comment */
51 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
52
53 char cfs_tracefile[TRACEFILE_NAME_SIZE];
54 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
55 static struct tracefiled_ctl trace_tctl;
56 static DEFINE_MUTEX(cfs_trace_thread_mutex);
57 static int thread_running = 0;
58
59 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
60
61 #define filp_size(f)    (i_size_read((f)->f_dentry->d_inode))
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_CACHE_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_CACHE_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         cfs_debug_limit_state_t   *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_CACHE_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_CACHE_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_CACHE_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                 cfs_trace_put_console_buffer(string_buf);
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                 cfs_trace_put_console_buffer(string_buf);
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         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         __oldfs = get_fs();
690         set_fs(get_ds());
691
692         /* ok, for now, just write the pages.  in the future we'll be building
693          * iobufs with the pages and calling generic_direct_IO */
694         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
695
696                 __LASSERT_TAGE_INVARIANT(tage);
697
698                 rc = vfs_write(filp, page_address(tage->page), tage->used,
699                                &filp->f_pos);
700                 if (rc != (int)tage->used) {
701                         printk(KERN_WARNING "wanted to write %u but wrote "
702                                "%d\n", tage->used, rc);
703                         put_pages_back(&pc);
704                         __LASSERT(list_empty(&pc.pc_pages));
705                         break;
706                 }
707                 list_del(&tage->linkage);
708                 cfs_tage_free(tage);
709         }
710         set_fs(__oldfs);
711         rc = ll_vfs_fsync_range(filp, 0, LLONG_MAX, 1);
712         if (rc)
713                 printk(KERN_ERR "sync returns %d\n", rc);
714 close:
715         filp_close(filp, NULL);
716 out:
717         cfs_tracefile_write_unlock();
718         return rc;
719 }
720
721 void cfs_trace_flush_pages(void)
722 {
723         struct page_collection pc;
724         struct cfs_trace_page *tage;
725         struct cfs_trace_page *tmp;
726
727         pc.pc_want_daemon_pages = 1;
728         collect_pages(&pc);
729         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
730
731                 __LASSERT_TAGE_INVARIANT(tage);
732
733                 list_del(&tage->linkage);
734                 cfs_tage_free(tage);
735         }
736 }
737
738 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
739                             const char __user *usr_buffer, int usr_buffer_nob)
740 {
741         int    nob;
742
743         if (usr_buffer_nob > knl_buffer_nob)
744                 return -EOVERFLOW;
745
746         if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
747                 return -EFAULT;
748
749         nob = strnlen(knl_buffer, usr_buffer_nob);
750         while (nob-- >= 0)                      /* strip trailing whitespace */
751                 if (!isspace(knl_buffer[nob]))
752                         break;
753
754         if (nob < 0)                            /* empty string */
755                 return -EINVAL;
756
757         if (nob == knl_buffer_nob)              /* no space to terminate */
758                 return -EOVERFLOW;
759
760         knl_buffer[nob + 1] = 0;                /* terminate */
761         return 0;
762 }
763 EXPORT_SYMBOL(cfs_trace_copyin_string);
764
765 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
766                              const char *knl_buffer, char *append)
767 {
768         /* NB if 'append' != NULL, it's a single character to append to the
769          * copied out string - usually "\n", for /proc entries and "" (i.e. a
770          * terminating zero byte) for sysctl entries */
771         int   nob = strlen(knl_buffer);
772
773         if (nob > usr_buffer_nob)
774                 nob = usr_buffer_nob;
775
776         if (copy_to_user(usr_buffer, knl_buffer, nob))
777                 return -EFAULT;
778
779         if (append != NULL && nob < usr_buffer_nob) {
780                 if (copy_to_user(usr_buffer + nob, append, 1))
781                         return -EFAULT;
782
783                 nob++;
784         }
785
786         return nob;
787 }
788 EXPORT_SYMBOL(cfs_trace_copyout_string);
789
790 int cfs_trace_allocate_string_buffer(char **str, int nob)
791 {
792         if (nob > 2 * PAGE_CACHE_SIZE)  /* string must be "sensible" */
793                 return -EINVAL;
794
795         *str = kmalloc(nob, GFP_IOFS | __GFP_ZERO);
796         if (*str == NULL)
797                 return -ENOMEM;
798
799         return 0;
800 }
801
802 void cfs_trace_free_string_buffer(char *str, int nob)
803 {
804         kfree(str);
805 }
806
807 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
808 {
809         char         *str;
810         int           rc;
811
812         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
813         if (rc != 0)
814                 return rc;
815
816         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
817                                      usr_str, usr_str_nob);
818         if (rc != 0)
819                 goto out;
820
821         if (str[0] != '/') {
822                 rc = -EINVAL;
823                 goto out;
824         }
825         rc = cfs_tracefile_dump_all_pages(str);
826 out:
827         cfs_trace_free_string_buffer(str, usr_str_nob + 1);
828         return rc;
829 }
830
831 int cfs_trace_daemon_command(char *str)
832 {
833         int       rc = 0;
834
835         cfs_tracefile_write_lock();
836
837         if (strcmp(str, "stop") == 0) {
838                 cfs_tracefile_write_unlock();
839                 cfs_trace_stop_thread();
840                 cfs_tracefile_write_lock();
841                 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
842
843         } else if (strncmp(str, "size=", 5) == 0) {
844                 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
845                 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
846                         cfs_tracefile_size = CFS_TRACEFILE_SIZE;
847                 else
848                         cfs_tracefile_size <<= 20;
849
850         } else if (strlen(str) >= sizeof(cfs_tracefile)) {
851                 rc = -ENAMETOOLONG;
852         } else if (str[0] != '/') {
853                 rc = -EINVAL;
854         } else {
855                 strcpy(cfs_tracefile, str);
856
857                 printk(KERN_INFO
858                        "Lustre: debug daemon will attempt to start writing "
859                        "to %s (%lukB max)\n", cfs_tracefile,
860                        (long)(cfs_tracefile_size >> 10));
861
862                 cfs_trace_start_thread();
863         }
864
865         cfs_tracefile_write_unlock();
866         return rc;
867 }
868
869 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
870 {
871         char *str;
872         int   rc;
873
874         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
875         if (rc != 0)
876                 return rc;
877
878         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
879                                  usr_str, usr_str_nob);
880         if (rc == 0)
881                 rc = cfs_trace_daemon_command(str);
882
883         cfs_trace_free_string_buffer(str, usr_str_nob + 1);
884         return rc;
885 }
886
887 int cfs_trace_set_debug_mb(int mb)
888 {
889         int i;
890         int j;
891         int pages;
892         int limit = cfs_trace_max_debug_mb();
893         struct cfs_trace_cpu_data *tcd;
894
895         if (mb < num_possible_cpus()) {
896                 printk(KERN_WARNING
897                        "Lustre: %d MB is too small for debug buffer size, "
898                        "setting it to %d MB.\n", mb, num_possible_cpus());
899                 mb = num_possible_cpus();
900         }
901
902         if (mb > limit) {
903                 printk(KERN_WARNING
904                        "Lustre: %d MB is too large for debug buffer size, "
905                        "setting it to %d MB.\n", mb, limit);
906                 mb = limit;
907         }
908
909         mb /= num_possible_cpus();
910         pages = mb << (20 - PAGE_CACHE_SHIFT);
911
912         cfs_tracefile_write_lock();
913
914         cfs_tcd_for_each(tcd, i, j)
915                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
916
917         cfs_tracefile_write_unlock();
918
919         return 0;
920 }
921
922 int cfs_trace_set_debug_mb_usrstr(void __user *usr_str, int usr_str_nob)
923 {
924         char     str[32];
925         int      rc;
926
927         rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
928         if (rc < 0)
929                 return rc;
930
931         return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
932 }
933
934 int cfs_trace_get_debug_mb(void)
935 {
936         int i;
937         int j;
938         struct cfs_trace_cpu_data *tcd;
939         int total_pages = 0;
940
941         cfs_tracefile_read_lock();
942
943         cfs_tcd_for_each(tcd, i, j)
944                 total_pages += tcd->tcd_max_pages;
945
946         cfs_tracefile_read_unlock();
947
948         return (total_pages >> (20 - PAGE_CACHE_SHIFT)) + 1;
949 }
950
951 static int tracefiled(void *arg)
952 {
953         struct page_collection pc;
954         struct tracefiled_ctl *tctl = arg;
955         struct cfs_trace_page *tage;
956         struct cfs_trace_page *tmp;
957         mm_segment_t __oldfs;
958         struct file *filp;
959         int last_loop = 0;
960         int rc;
961
962         /* we're started late enough that we pick up init's fs context */
963         /* this is so broken in uml?  what on earth is going on? */
964
965         complete(&tctl->tctl_start);
966
967         while (1) {
968                 wait_queue_t __wait;
969
970                 pc.pc_want_daemon_pages = 0;
971                 collect_pages(&pc);
972                 if (list_empty(&pc.pc_pages))
973                         goto end_loop;
974
975                 filp = NULL;
976                 cfs_tracefile_read_lock();
977                 if (cfs_tracefile[0] != 0) {
978                         filp = filp_open(cfs_tracefile,
979                                          O_CREAT | O_RDWR | O_LARGEFILE,
980                                          0600);
981                         if (IS_ERR(filp)) {
982                                 rc = PTR_ERR(filp);
983                                 filp = NULL;
984                                 printk(KERN_WARNING "couldn't open %s: "
985                                        "%d\n", cfs_tracefile, rc);
986                         }
987                 }
988                 cfs_tracefile_read_unlock();
989                 if (filp == NULL) {
990                         put_pages_on_daemon_list(&pc);
991                         __LASSERT(list_empty(&pc.pc_pages));
992                         goto end_loop;
993                 }
994                 __oldfs = get_fs();
995                 set_fs(get_ds());
996
997                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
998                         static loff_t f_pos;
999
1000                         __LASSERT_TAGE_INVARIANT(tage);
1001
1002                         if (f_pos >= (off_t)cfs_tracefile_size)
1003                                 f_pos = 0;
1004                         else if (f_pos > (off_t)filp_size(filp))
1005                                 f_pos = filp_size(filp);
1006
1007                         rc = vfs_write(filp, page_address(tage->page),
1008                                        tage->used, &f_pos);
1009                         if (rc != (int)tage->used) {
1010                                 printk(KERN_WARNING "wanted to write %u "
1011                                        "but wrote %d\n", tage->used, rc);
1012                                 put_pages_back(&pc);
1013                                 __LASSERT(list_empty(&pc.pc_pages));
1014                                 break;
1015                         }
1016                 }
1017                 set_fs(__oldfs);
1018
1019                 filp_close(filp, NULL);
1020                 put_pages_on_daemon_list(&pc);
1021                 if (!list_empty(&pc.pc_pages)) {
1022                         int i;
1023
1024                         printk(KERN_ALERT "Lustre: trace pages aren't "
1025                                " empty\n");
1026                         printk(KERN_ERR "total cpus(%d): ",
1027                                num_possible_cpus());
1028                         for (i = 0; i < num_possible_cpus(); i++)
1029                                 if (cpu_online(i))
1030                                         printk(KERN_ERR "%d(on) ", i);
1031                                 else
1032                                         printk(KERN_ERR "%d(off) ", i);
1033                         printk(KERN_ERR "\n");
1034
1035                         i = 0;
1036                         list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1037                                                      linkage)
1038                                 printk(KERN_ERR "page %d belongs to cpu "
1039                                        "%d\n", ++i, tage->cpu);
1040                         printk(KERN_ERR "There are %d pages unwritten\n",
1041                                i);
1042                 }
1043                 __LASSERT(list_empty(&pc.pc_pages));
1044 end_loop:
1045                 if (atomic_read(&tctl->tctl_shutdown)) {
1046                         if (last_loop == 0) {
1047                                 last_loop = 1;
1048                                 continue;
1049                         } else {
1050                                 break;
1051                         }
1052                 }
1053                 init_waitqueue_entry(&__wait, current);
1054                 add_wait_queue(&tctl->tctl_waitq, &__wait);
1055                 set_current_state(TASK_INTERRUPTIBLE);
1056                 schedule_timeout(cfs_time_seconds(1));
1057                 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1058         }
1059         complete(&tctl->tctl_stop);
1060         return 0;
1061 }
1062
1063 int cfs_trace_start_thread(void)
1064 {
1065         struct tracefiled_ctl *tctl = &trace_tctl;
1066         int rc = 0;
1067
1068         mutex_lock(&cfs_trace_thread_mutex);
1069         if (thread_running)
1070                 goto out;
1071
1072         init_completion(&tctl->tctl_start);
1073         init_completion(&tctl->tctl_stop);
1074         init_waitqueue_head(&tctl->tctl_waitq);
1075         atomic_set(&tctl->tctl_shutdown, 0);
1076
1077         if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1078                 rc = -ECHILD;
1079                 goto out;
1080         }
1081
1082         wait_for_completion(&tctl->tctl_start);
1083         thread_running = 1;
1084 out:
1085         mutex_unlock(&cfs_trace_thread_mutex);
1086         return rc;
1087 }
1088
1089 void cfs_trace_stop_thread(void)
1090 {
1091         struct tracefiled_ctl *tctl = &trace_tctl;
1092
1093         mutex_lock(&cfs_trace_thread_mutex);
1094         if (thread_running) {
1095                 printk(KERN_INFO
1096                        "Lustre: shutting down debug daemon thread...\n");
1097                 atomic_set(&tctl->tctl_shutdown, 1);
1098                 wait_for_completion(&tctl->tctl_stop);
1099                 thread_running = 0;
1100         }
1101         mutex_unlock(&cfs_trace_thread_mutex);
1102 }
1103
1104 int cfs_tracefile_init(int max_pages)
1105 {
1106         struct cfs_trace_cpu_data *tcd;
1107         int     i;
1108         int     j;
1109         int     rc;
1110         int     factor;
1111
1112         rc = cfs_tracefile_init_arch();
1113         if (rc != 0)
1114                 return rc;
1115
1116         cfs_tcd_for_each(tcd, i, j) {
1117                 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1118                 factor = tcd->tcd_pages_factor;
1119                 INIT_LIST_HEAD(&tcd->tcd_pages);
1120                 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1121                 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1122                 tcd->tcd_cur_pages = 0;
1123                 tcd->tcd_cur_stock_pages = 0;
1124                 tcd->tcd_cur_daemon_pages = 0;
1125                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1126                 LASSERT(tcd->tcd_max_pages > 0);
1127                 tcd->tcd_shutting_down = 0;
1128         }
1129         return 0;
1130 }
1131
1132 static void trace_cleanup_on_all_cpus(void)
1133 {
1134         struct cfs_trace_cpu_data *tcd;
1135         struct cfs_trace_page *tage;
1136         struct cfs_trace_page *tmp;
1137         int i, cpu;
1138
1139         for_each_possible_cpu(cpu) {
1140                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1141                         tcd->tcd_shutting_down = 1;
1142
1143                         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1144                                 __LASSERT_TAGE_INVARIANT(tage);
1145
1146                                 list_del(&tage->linkage);
1147                                 cfs_tage_free(tage);
1148                         }
1149                         tcd->tcd_cur_pages = 0;
1150                 }
1151         }
1152 }
1153
1154 static void cfs_trace_cleanup(void)
1155 {
1156         struct page_collection pc;
1157
1158         INIT_LIST_HEAD(&pc.pc_pages);
1159
1160         trace_cleanup_on_all_cpus();
1161
1162         cfs_tracefile_fini_arch();
1163 }
1164
1165 void cfs_tracefile_exit(void)
1166 {
1167         cfs_trace_stop_thread();
1168         cfs_trace_cleanup();
1169 }