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