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