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