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