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