OSDN Git Service

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