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