Whamcloud - gitweb
LU-5126 libcfs: fix illegal page access of tracefiled()
[fs/lustre-release.git] / libcfs / libcfs / tracefile.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, 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 <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(int 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, int 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; "
200                         "discarding 10%% of pages (%d of %ld)\n",
201                         pgcount + 1, tcd->tcd_cur_pages);
202
203         INIT_LIST_HEAD(&pc.pc_pages);
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         char                      *file = (char *)msgdata->msg_file;
278         cfs_debug_limit_state_t   *cdls = msgdata->msg_cdls;
279
280         if (strchr(file, '/'))
281                 file = strrchr(file, '/') + 1;
282
283         tcd = cfs_trace_get_tcd();
284
285         /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
286          * pins us to a particular CPU.  This avoids an smp_processor_id()
287          * warning on Linux when debugging is enabled. */
288         cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
289
290         if (tcd == NULL)                /* arch may not log in IRQ context */
291                 goto console;
292
293         if (tcd->tcd_cur_pages == 0)
294                 header.ph_flags |= PH_FLAG_FIRST_RECORD;
295
296         if (tcd->tcd_shutting_down) {
297                 cfs_trace_put_tcd(tcd);
298                 tcd = NULL;
299                 goto console;
300         }
301
302         depth = __current_nesting_level();
303         known_size = strlen(file) + 1 + depth;
304         if (msgdata->msg_fn)
305                 known_size += strlen(msgdata->msg_fn) + 1;
306
307         if (libcfs_debug_binary)
308                 known_size += sizeof(header);
309
310         /*/
311          * '2' used because vsnprintf return real size required for output
312          * _without_ terminating NULL.
313          * if needed is to small for this format.
314          */
315         for (i = 0; i < 2; i++) {
316                 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
317                 if (tage == NULL) {
318                         if (needed + known_size > PAGE_CACHE_SIZE)
319                                 mask |= D_ERROR;
320
321                         cfs_trace_put_tcd(tcd);
322                         tcd = NULL;
323                         goto console;
324                 }
325
326                 string_buf = (char *)page_address(tage->page) +
327                                         tage->used + known_size;
328
329                 max_nob = PAGE_CACHE_SIZE - tage->used - known_size;
330                 if (max_nob <= 0) {
331                         printk(KERN_EMERG "negative max_nob: %d\n",
332                                max_nob);
333                         mask |= D_ERROR;
334                         cfs_trace_put_tcd(tcd);
335                         tcd = NULL;
336                         goto console;
337                 }
338
339                 needed = 0;
340                 if (format1) {
341                         va_copy(ap, args);
342                         needed = vsnprintf(string_buf, max_nob, format1, ap);
343                         va_end(ap);
344                 }
345
346                 if (format2) {
347                         remain = max_nob - needed;
348                         if (remain < 0)
349                                 remain = 0;
350
351                         va_start(ap, format2);
352                         needed += vsnprintf(string_buf + needed, remain,
353                                             format2, ap);
354                         va_end(ap);
355                 }
356
357                 if (needed < max_nob) /* well. printing ok.. */
358                         break;
359         }
360
361         if (*(string_buf+needed-1) != '\n')
362                 printk(KERN_INFO "format at %s:%d:%s doesn't end in "
363                        "newline\n", file, msgdata->msg_line, msgdata->msg_fn);
364
365         header.ph_len = known_size + needed;
366         debug_buf = (char *)page_address(tage->page) + tage->used;
367
368         if (libcfs_debug_binary) {
369                 memcpy(debug_buf, &header, sizeof(header));
370                 tage->used += sizeof(header);
371                 debug_buf += sizeof(header);
372         }
373
374         /* indent message according to the nesting level */
375         while (depth-- > 0) {
376                 *(debug_buf++) = '.';
377                 ++ tage->used;
378         }
379
380         strcpy(debug_buf, file);
381         tage->used += strlen(file) + 1;
382         debug_buf += strlen(file) + 1;
383
384         if (msgdata->msg_fn) {
385                 strcpy(debug_buf, msgdata->msg_fn);
386                 tage->used += strlen(msgdata->msg_fn) + 1;
387                 debug_buf += strlen(msgdata->msg_fn) + 1;
388         }
389
390         __LASSERT(debug_buf == string_buf);
391
392         tage->used += needed;
393         __LASSERT(tage->used <= PAGE_CACHE_SIZE);
394
395 console:
396         if ((mask & libcfs_printk) == 0) {
397                 /* no console output requested */
398                 if (tcd != NULL)
399                         cfs_trace_put_tcd(tcd);
400                 return 1;
401         }
402
403         if (cdls != NULL) {
404                 if (libcfs_console_ratelimit &&
405                     cdls->cdls_next != 0 &&     /* not first time ever */
406                     !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
407                         /* skipping a console message */
408                         cdls->cdls_count++;
409                         if (tcd != NULL)
410                                 cfs_trace_put_tcd(tcd);
411                         return 1;
412                 }
413
414                 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
415                                                        libcfs_console_max_delay
416                                                        + cfs_time_seconds(10))) {
417                         /* last timeout was a long time ago */
418                         cdls->cdls_delay /= libcfs_console_backoff * 4;
419                 } else {
420                         cdls->cdls_delay *= libcfs_console_backoff;
421                 }
422
423                 if (cdls->cdls_delay < libcfs_console_min_delay)
424                         cdls->cdls_delay = libcfs_console_min_delay;
425                 else if (cdls->cdls_delay > libcfs_console_max_delay)
426                         cdls->cdls_delay = libcfs_console_max_delay;
427
428                 /* ensure cdls_next is never zero after it's been seen */
429                 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
430         }
431
432         if (tcd != NULL) {
433                 cfs_print_to_console(&header, mask, string_buf, needed, file,
434                                      msgdata->msg_fn);
435                 cfs_trace_put_tcd(tcd);
436         } else {
437                 string_buf = cfs_trace_get_console_buffer();
438
439                 needed = 0;
440                 if (format1 != NULL) {
441                         va_copy(ap, args);
442                         needed = vsnprintf(string_buf,
443                                            CFS_TRACE_CONSOLE_BUFFER_SIZE,
444                                            format1, ap);
445                         va_end(ap);
446                 }
447                 if (format2 != NULL) {
448                         remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
449                         if (remain > 0) {
450                                 va_start(ap, format2);
451                                 needed += vsnprintf(string_buf+needed, remain,
452                                                     format2, ap);
453                                 va_end(ap);
454                         }
455                 }
456                 cfs_print_to_console(&header, mask,
457                                      string_buf, needed, file, msgdata->msg_fn);
458
459                 cfs_trace_put_console_buffer(string_buf);
460         }
461
462         if (cdls != NULL && cdls->cdls_count != 0) {
463                 string_buf = cfs_trace_get_console_buffer();
464
465                 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
466                                   "Skipped %d previous similar message%s\n",
467                                   cdls->cdls_count,
468                                   (cdls->cdls_count > 1) ? "s" : "");
469
470                 cfs_print_to_console(&header, mask,
471                                      string_buf, needed, file, msgdata->msg_fn);
472
473                 cfs_trace_put_console_buffer(string_buf);
474                 cdls->cdls_count = 0;
475         }
476
477         return 0;
478 }
479 EXPORT_SYMBOL(libcfs_debug_vmsg2);
480
481 void
482 cfs_trace_assertion_failed(const char *str,
483                            struct libcfs_debug_msg_data *msgdata)
484 {
485         struct ptldebug_header hdr;
486
487         libcfs_panic_in_progress = 1;
488         libcfs_catastrophe = 1;
489         smp_mb();
490
491         cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
492
493         cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
494                              msgdata->msg_file, msgdata->msg_fn);
495
496         panic("Lustre debug assertion failure\n");
497
498         /* not reached */
499 }
500
501 static void
502 panic_collect_pages(struct page_collection *pc)
503 {
504         /* Do the collect_pages job on a single CPU: assumes that all other
505          * CPUs have been stopped during a panic.  If this isn't true for some
506          * arch, this will have to be implemented separately in each arch.  */
507         int                        i;
508         int                        j;
509         struct cfs_trace_cpu_data *tcd;
510
511         INIT_LIST_HEAD(&pc->pc_pages);
512
513         cfs_tcd_for_each(tcd, i, j) {
514                 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
515                 tcd->tcd_cur_pages = 0;
516
517                 if (pc->pc_want_daemon_pages) {
518                         list_splice_init(&tcd->tcd_daemon_pages,
519                                                 &pc->pc_pages);
520                         tcd->tcd_cur_daemon_pages = 0;
521                 }
522         }
523 }
524
525 static void collect_pages_on_all_cpus(struct page_collection *pc)
526 {
527         struct cfs_trace_cpu_data *tcd;
528         int i, cpu;
529
530         cfs_for_each_possible_cpu(cpu) {
531                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
532                         list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
533                         tcd->tcd_cur_pages = 0;
534                         if (pc->pc_want_daemon_pages) {
535                                 list_splice_init(&tcd->tcd_daemon_pages,
536                                                         &pc->pc_pages);
537                                 tcd->tcd_cur_daemon_pages = 0;
538                         }
539                 }
540         }
541 }
542
543 static void collect_pages(struct page_collection *pc)
544 {
545         INIT_LIST_HEAD(&pc->pc_pages);
546
547         if (libcfs_panic_in_progress)
548                 panic_collect_pages(pc);
549         else
550                 collect_pages_on_all_cpus(pc);
551 }
552
553 static void put_pages_back_on_all_cpus(struct page_collection *pc)
554 {
555         struct cfs_trace_cpu_data *tcd;
556         struct list_head *cur_head;
557         struct cfs_trace_page *tage;
558         struct cfs_trace_page *tmp;
559         int i, cpu;
560
561         cfs_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 }
579
580 static void put_pages_back(struct page_collection *pc)
581 {
582         if (!libcfs_panic_in_progress)
583                 put_pages_back_on_all_cpus(pc);
584 }
585
586 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
587  * we have a good amount of data at all times for dumping during an LBUG, even
588  * if we have been steadily writing (and otherwise discarding) pages via the
589  * debug daemon. */
590 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
591                                          struct cfs_trace_cpu_data *tcd)
592 {
593         struct cfs_trace_page *tage;
594         struct cfs_trace_page *tmp;
595
596         list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
597                 __LASSERT_TAGE_INVARIANT(tage);
598
599                 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
600                         continue;
601
602                 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
603                 tcd->tcd_cur_daemon_pages++;
604
605                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
606                         struct cfs_trace_page *victim;
607
608                         __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
609                         victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
610
611                         __LASSERT_TAGE_INVARIANT(victim);
612
613                         list_del(&victim->linkage);
614                         cfs_tage_free(victim);
615                         tcd->tcd_cur_daemon_pages--;
616                 }
617         }
618 }
619
620 static void put_pages_on_daemon_list(struct page_collection *pc)
621 {
622         struct cfs_trace_cpu_data *tcd;
623         int i, cpu;
624
625         cfs_for_each_possible_cpu(cpu) {
626                 cfs_tcd_for_each_type_lock(tcd, i, cpu)
627                         put_pages_on_tcd_daemon_list(pc, tcd);
628         }
629 }
630
631 void cfs_trace_debug_print(void)
632 {
633         struct page_collection pc;
634         struct cfs_trace_page *tage;
635         struct cfs_trace_page *tmp;
636
637         pc.pc_want_daemon_pages = 1;
638         collect_pages(&pc);
639         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
640                 char *p, *file, *fn;
641                 struct page *page;
642
643                 __LASSERT_TAGE_INVARIANT(tage);
644
645                 page = tage->page;
646                 p = page_address(page);
647                 while (p < ((char *)page_address(page) + tage->used)) {
648                         struct ptldebug_header *hdr;
649                         int len;
650                         hdr = (void *)p;
651                         p += sizeof(*hdr);
652                         file = p;
653                         p += strlen(file) + 1;
654                         fn = p;
655                         p += strlen(fn) + 1;
656                         len = hdr->ph_len - (int)(p - (char *)hdr);
657
658                         cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
659
660                         p += len;
661                 }
662
663                 list_del(&tage->linkage);
664                 cfs_tage_free(tage);
665         }
666 }
667
668 int cfs_tracefile_dump_all_pages(char *filename)
669 {
670         struct page_collection  pc;
671         struct file             *filp;
672         struct cfs_trace_page   *tage;
673         struct cfs_trace_page   *tmp;
674         int rc;
675
676         DECL_MMSPACE;
677
678         cfs_tracefile_write_lock();
679
680         filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
681         if (IS_ERR(filp)) {
682                 rc = PTR_ERR(filp);
683                 filp = NULL;
684                 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
685                       filename, rc);
686                 goto out;
687         }
688
689         pc.pc_want_daemon_pages = 1;
690         collect_pages(&pc);
691         if (list_empty(&pc.pc_pages)) {
692                 rc = 0;
693                 goto close;
694         }
695
696         /* ok, for now, just write the pages.  in the future we'll be building
697          * iobufs with the pages and calling generic_direct_IO */
698         MMSPACE_OPEN;
699         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
700
701                 __LASSERT_TAGE_INVARIANT(tage);
702
703                 rc = filp_write(filp, page_address(tage->page),
704                                 tage->used, filp_poff(filp));
705                 if (rc != (int)tage->used) {
706                         printk(KERN_WARNING "wanted to write %u but wrote "
707                                "%d\n", tage->used, rc);
708                         put_pages_back(&pc);
709                         __LASSERT(list_empty(&pc.pc_pages));
710                         break;
711                 }
712                 list_del(&tage->linkage);
713                 cfs_tage_free(tage);
714         }
715         MMSPACE_CLOSE;
716         rc = filp_fsync(filp, 0, LLONG_MAX);
717         if (rc)
718                 printk(KERN_ERR "sync returns %d\n", rc);
719 close:
720         filp_close(filp, NULL);
721 out:
722         cfs_tracefile_write_unlock();
723         return rc;
724 }
725
726 void cfs_trace_flush_pages(void)
727 {
728         struct page_collection pc;
729         struct cfs_trace_page *tage;
730         struct cfs_trace_page *tmp;
731
732         pc.pc_want_daemon_pages = 1;
733         collect_pages(&pc);
734         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
735
736                 __LASSERT_TAGE_INVARIANT(tage);
737
738                 list_del(&tage->linkage);
739                 cfs_tage_free(tage);
740         }
741 }
742
743 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
744                             const char *usr_buffer, int usr_buffer_nob)
745 {
746         int    nob;
747
748         if (usr_buffer_nob > knl_buffer_nob)
749                 return -EOVERFLOW;
750
751         if (copy_from_user((void *)knl_buffer,
752                            (void *)usr_buffer, usr_buffer_nob))
753                 return -EFAULT;
754
755         nob = strnlen(knl_buffer, usr_buffer_nob);
756         while (nob-- >= 0)                      /* strip trailing whitespace */
757                 if (!isspace(knl_buffer[nob]))
758                         break;
759
760         if (nob < 0)                            /* empty string */
761                 return -EINVAL;
762
763         if (nob == knl_buffer_nob)              /* no space to terminate */
764                 return -EOVERFLOW;
765
766         knl_buffer[nob + 1] = 0;                /* terminate */
767         return 0;
768 }
769 EXPORT_SYMBOL(cfs_trace_copyin_string);
770
771 int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
772                              const char *knl_buffer, char *append)
773 {
774         /* NB if 'append' != NULL, it's a single character to append to the
775          * copied out string - usually "\n", for /proc entries and "" (i.e. a
776          * terminating zero byte) for sysctl entries */
777         int   nob = strlen(knl_buffer);
778
779         if (nob > usr_buffer_nob)
780                 nob = usr_buffer_nob;
781
782         if (copy_to_user(usr_buffer, knl_buffer, nob))
783                 return -EFAULT;
784
785         if (append != NULL && nob < usr_buffer_nob) {
786                 if (copy_to_user(usr_buffer + nob, append, 1))
787                         return -EFAULT;
788
789                 nob++;
790         }
791
792         return nob;
793 }
794 EXPORT_SYMBOL(cfs_trace_copyout_string);
795
796 int cfs_trace_allocate_string_buffer(char **str, int nob)
797 {
798         if (nob > 2 * PAGE_CACHE_SIZE)  /* string must be "sensible" */
799                 return -EINVAL;
800
801         *str = kmalloc(nob, GFP_IOFS | __GFP_ZERO);
802         if (*str == NULL)
803                 return -ENOMEM;
804
805         return 0;
806 }
807
808 void cfs_trace_free_string_buffer(char *str, int nob)
809 {
810         kfree(str);
811 }
812
813 int cfs_trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
814 {
815         char         *str;
816         int           rc;
817
818         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
819         if (rc != 0)
820                 return rc;
821
822         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
823                                      usr_str, usr_str_nob);
824         if (rc != 0)
825                 goto out;
826
827 #if !defined(__WINNT__)
828         if (str[0] != '/') {
829                 rc = -EINVAL;
830                 goto out;
831         }
832 #endif
833         rc = cfs_tracefile_dump_all_pages(str);
834 out:
835         cfs_trace_free_string_buffer(str, usr_str_nob + 1);
836         return rc;
837 }
838
839 int cfs_trace_daemon_command(char *str)
840 {
841         int       rc = 0;
842
843         cfs_tracefile_write_lock();
844
845         if (strcmp(str, "stop") == 0) {
846                 cfs_tracefile_write_unlock();
847                 cfs_trace_stop_thread();
848                 cfs_tracefile_write_lock();
849                 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
850
851         } else if (strncmp(str, "size=", 5) == 0) {
852                 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
853                 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
854                         cfs_tracefile_size = CFS_TRACEFILE_SIZE;
855                 else
856                         cfs_tracefile_size <<= 20;
857
858         } else if (strlen(str) >= sizeof(cfs_tracefile)) {
859                 rc = -ENAMETOOLONG;
860 #ifndef __WINNT__
861         } else if (str[0] != '/') {
862                 rc = -EINVAL;
863 #endif
864         } else {
865                 strcpy(cfs_tracefile, str);
866
867                 printk(KERN_INFO
868                        "Lustre: debug daemon will attempt to start writing "
869                        "to %s (%lukB max)\n", cfs_tracefile,
870                        (long)(cfs_tracefile_size >> 10));
871
872                 cfs_trace_start_thread();
873         }
874
875         cfs_tracefile_write_unlock();
876         return rc;
877 }
878
879 int cfs_trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
880 {
881         char *str;
882         int   rc;
883
884         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
885         if (rc != 0)
886                 return rc;
887
888         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
889                                  usr_str, usr_str_nob);
890         if (rc == 0)
891                 rc = cfs_trace_daemon_command(str);
892
893         cfs_trace_free_string_buffer(str, usr_str_nob + 1);
894         return rc;
895 }
896
897 int cfs_trace_set_debug_mb(int mb)
898 {
899         int i;
900         int j;
901         int pages;
902         int limit = cfs_trace_max_debug_mb();
903         struct cfs_trace_cpu_data *tcd;
904
905         if (mb < num_possible_cpus()) {
906                 printk(KERN_WARNING
907                        "Lustre: %d MB is too small for debug buffer size, "
908                        "setting it to %d MB.\n", mb, num_possible_cpus());
909                 mb = num_possible_cpus();
910         }
911
912         if (mb > limit) {
913                 printk(KERN_WARNING
914                        "Lustre: %d MB is too large for debug buffer size, "
915                        "setting it to %d MB.\n", mb, limit);
916                 mb = limit;
917         }
918
919         mb /= num_possible_cpus();
920         pages = mb << (20 - PAGE_CACHE_SHIFT);
921
922         cfs_tracefile_write_lock();
923
924         cfs_tcd_for_each(tcd, i, j)
925                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
926
927         cfs_tracefile_write_unlock();
928
929         return 0;
930 }
931
932 int cfs_trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
933 {
934         char     str[32];
935         int      rc;
936
937         rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
938         if (rc < 0)
939                 return rc;
940
941         return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
942 }
943
944 int cfs_trace_get_debug_mb(void)
945 {
946         int i;
947         int j;
948         struct cfs_trace_cpu_data *tcd;
949         int total_pages = 0;
950
951         cfs_tracefile_read_lock();
952
953         cfs_tcd_for_each(tcd, i, j)
954                 total_pages += tcd->tcd_max_pages;
955
956         cfs_tracefile_read_unlock();
957
958         return (total_pages >> (20 - PAGE_CACHE_SHIFT)) + 1;
959 }
960
961 static int tracefiled(void *arg)
962 {
963         struct page_collection pc;
964         struct tracefiled_ctl *tctl = arg;
965         struct cfs_trace_page *tage;
966         struct cfs_trace_page *tmp;
967         struct file *filp;
968         int last_loop = 0;
969         int rc;
970
971         DECL_MMSPACE;
972
973         /* we're started late enough that we pick up init's fs context */
974         /* this is so broken in uml?  what on earth is going on? */
975
976         complete(&tctl->tctl_start);
977
978         while (1) {
979                 wait_queue_t __wait;
980
981                 pc.pc_want_daemon_pages = 0;
982                 collect_pages(&pc);
983                 if (list_empty(&pc.pc_pages))
984                         goto end_loop;
985
986                 filp = NULL;
987                 cfs_tracefile_read_lock();
988                 if (cfs_tracefile[0] != 0) {
989                         filp = filp_open(cfs_tracefile,
990                                          O_CREAT | O_RDWR | O_LARGEFILE,
991                                          0600);
992                         if (IS_ERR(filp)) {
993                                 rc = PTR_ERR(filp);
994                                 filp = NULL;
995                                 printk(KERN_WARNING "couldn't open %s: "
996                                        "%d\n", cfs_tracefile, rc);
997                         }
998                 }
999                 cfs_tracefile_read_unlock();
1000                 if (filp == NULL) {
1001                         put_pages_on_daemon_list(&pc);
1002                         __LASSERT(list_empty(&pc.pc_pages));
1003                         goto end_loop;
1004                 }
1005
1006                 MMSPACE_OPEN;
1007
1008                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1009                         static loff_t f_pos;
1010
1011                         __LASSERT_TAGE_INVARIANT(tage);
1012
1013                         if (f_pos >= (off_t)cfs_tracefile_size)
1014                                 f_pos = 0;
1015                         else if (f_pos > (off_t)filp_size(filp))
1016                                 f_pos = filp_size(filp);
1017
1018                         rc = filp_write(filp, page_address(tage->page),
1019                                         tage->used, &f_pos);
1020                         if (rc != (int)tage->used) {
1021                                 printk(KERN_WARNING "wanted to write %u "
1022                                        "but wrote %d\n", tage->used, rc);
1023                                 put_pages_back(&pc);
1024                                 __LASSERT(list_empty(&pc.pc_pages));
1025                                 break;
1026                         }
1027                 }
1028                 MMSPACE_CLOSE;
1029
1030                 filp_close(filp, NULL);
1031                 put_pages_on_daemon_list(&pc);
1032                 if (!list_empty(&pc.pc_pages)) {
1033                         int i;
1034
1035                         printk(KERN_ALERT "Lustre: trace pages aren't "
1036                                " empty\n");
1037                         printk(KERN_ERR "total cpus(%d): ",
1038                                num_possible_cpus());
1039                         for (i = 0; i < num_possible_cpus(); i++)
1040                                 if (cpu_online(i))
1041                                         printk(KERN_ERR "%d(on) ", i);
1042                                 else
1043                                         printk(KERN_ERR "%d(off) ", i);
1044                         printk(KERN_ERR "\n");
1045
1046                         i = 0;
1047                         list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1048                                                      linkage)
1049                                 printk(KERN_ERR "page %d belongs to cpu "
1050                                        "%d\n", ++i, tage->cpu);
1051                         printk(KERN_ERR "There are %d pages unwritten\n",
1052                                i);
1053                 }
1054                 __LASSERT(list_empty(&pc.pc_pages));
1055 end_loop:
1056                 if (atomic_read(&tctl->tctl_shutdown)) {
1057                         if (last_loop == 0) {
1058                                 last_loop = 1;
1059                                 continue;
1060                         } else {
1061                                 break;
1062                         }
1063                 }
1064                 init_waitqueue_entry_current(&__wait);
1065                 add_wait_queue(&tctl->tctl_waitq, &__wait);
1066                 set_current_state(TASK_INTERRUPTIBLE);
1067                 waitq_timedwait(&__wait, TASK_INTERRUPTIBLE,
1068                                 cfs_time_seconds(1));
1069                 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1070         }
1071         complete(&tctl->tctl_stop);
1072         return 0;
1073 }
1074
1075 int cfs_trace_start_thread(void)
1076 {
1077         struct tracefiled_ctl *tctl = &trace_tctl;
1078         int rc = 0;
1079
1080         mutex_lock(&cfs_trace_thread_mutex);
1081         if (thread_running)
1082                 goto out;
1083
1084         init_completion(&tctl->tctl_start);
1085         init_completion(&tctl->tctl_stop);
1086         init_waitqueue_head(&tctl->tctl_waitq);
1087         atomic_set(&tctl->tctl_shutdown, 0);
1088
1089         if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1090                 rc = -ECHILD;
1091                 goto out;
1092         }
1093
1094         wait_for_completion(&tctl->tctl_start);
1095         thread_running = 1;
1096 out:
1097         mutex_unlock(&cfs_trace_thread_mutex);
1098         return rc;
1099 }
1100
1101 void cfs_trace_stop_thread(void)
1102 {
1103         struct tracefiled_ctl *tctl = &trace_tctl;
1104
1105         mutex_lock(&cfs_trace_thread_mutex);
1106         if (thread_running) {
1107                 printk(KERN_INFO
1108                        "Lustre: shutting down debug daemon thread...\n");
1109                 atomic_set(&tctl->tctl_shutdown, 1);
1110                 wait_for_completion(&tctl->tctl_stop);
1111                 thread_running = 0;
1112         }
1113         mutex_unlock(&cfs_trace_thread_mutex);
1114 }
1115
1116 int cfs_tracefile_init(int max_pages)
1117 {
1118         struct cfs_trace_cpu_data *tcd;
1119         int     i;
1120         int     j;
1121         int     rc;
1122         int     factor;
1123
1124         rc = cfs_tracefile_init_arch();
1125         if (rc != 0)
1126                 return rc;
1127
1128         cfs_tcd_for_each(tcd, i, j) {
1129                 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1130                 factor = tcd->tcd_pages_factor;
1131                 INIT_LIST_HEAD(&tcd->tcd_pages);
1132                 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1133                 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1134                 tcd->tcd_cur_pages = 0;
1135                 tcd->tcd_cur_stock_pages = 0;
1136                 tcd->tcd_cur_daemon_pages = 0;
1137                 tcd->tcd_max_pages = (max_pages * factor) / 100;
1138                 LASSERT(tcd->tcd_max_pages > 0);
1139                 tcd->tcd_shutting_down = 0;
1140         }
1141         return 0;
1142 }
1143
1144 static void trace_cleanup_on_all_cpus(void)
1145 {
1146         struct cfs_trace_cpu_data *tcd;
1147         struct cfs_trace_page *tage;
1148         struct cfs_trace_page *tmp;
1149         int i, cpu;
1150
1151         cfs_for_each_possible_cpu(cpu) {
1152                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1153                         tcd->tcd_shutting_down = 1;
1154
1155                         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1156                                 __LASSERT_TAGE_INVARIANT(tage);
1157
1158                                 list_del(&tage->linkage);
1159                                 cfs_tage_free(tage);
1160                         }
1161                         tcd->tcd_cur_pages = 0;
1162                 }
1163         }
1164 }
1165
1166 static void cfs_trace_cleanup(void)
1167 {
1168         struct page_collection pc;
1169
1170         INIT_LIST_HEAD(&pc.pc_pages);
1171
1172         trace_cleanup_on_all_cpus();
1173
1174         cfs_tracefile_fini_arch();
1175 }
1176
1177 void cfs_tracefile_exit(void)
1178 {
1179         cfs_trace_stop_thread();
1180         cfs_trace_cleanup();
1181 }