4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/libcfs/tracefile.c
34 * Author: Zach Brown <zab@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LNET
40 #define LUSTRE_TRACEFILE_PRIVATE
41 #include "tracefile.h"
43 #include <linux/ctype.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>
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;
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;
60 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
61 static DECLARE_RWSEM(cfs_tracefile_sem);
63 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
64 struct cfs_trace_cpu_data *tcd);
66 static inline struct cfs_trace_page *
67 cfs_tage_from_list(struct list_head *list)
69 return list_entry(list, struct cfs_trace_page, linkage);
72 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
75 struct cfs_trace_page *tage;
77 /* My caller is trying to free memory */
78 if (!in_interrupt() && memory_pressure_get())
82 * Don't spam console with allocation failures: they will be reported
83 * by upper layer anyway.
86 page = alloc_page(gfp);
90 tage = kmalloc(sizeof(*tage), gfp);
97 atomic_inc(&cfs_tage_allocated);
101 static void cfs_tage_free(struct cfs_trace_page *tage)
103 __LASSERT(tage != NULL);
104 __LASSERT(tage->page != NULL);
106 __free_page(tage->page);
108 atomic_dec(&cfs_tage_allocated);
111 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
112 struct list_head *queue)
114 __LASSERT(tage != NULL);
115 __LASSERT(queue != NULL);
117 list_move_tail(&tage->linkage, queue);
120 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, gfp_t gfp,
121 struct list_head *stock)
126 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
127 * from here: this will lead to infinite recursion.
130 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
131 struct cfs_trace_page *tage;
133 tage = cfs_tage_alloc(gfp);
136 list_add_tail(&tage->linkage, stock);
141 /* return a page that has 'len' bytes left at the end */
142 static struct cfs_trace_page *
143 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
145 struct cfs_trace_page *tage;
147 if (tcd->tcd_cur_pages > 0) {
148 __LASSERT(!list_empty(&tcd->tcd_pages));
149 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
150 if (tage->used + len <= PAGE_SIZE)
154 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
155 if (tcd->tcd_cur_stock_pages > 0) {
156 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
157 --tcd->tcd_cur_stock_pages;
158 list_del_init(&tage->linkage);
160 tage = cfs_tage_alloc(GFP_ATOMIC);
161 if (unlikely(tage == NULL)) {
162 if ((!memory_pressure_get() ||
163 in_interrupt()) && printk_ratelimit())
164 pr_warn("Lustre: cannot allocate a tage (%ld)\n",
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++;
176 if (tcd->tcd_cur_pages > 8 && thread_running) {
177 struct tracefiled_ctl *tctl = &trace_tctl;
179 * wake up tracefiled to process some pages.
181 wake_up(&tctl->tctl_waitq);
188 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
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;
196 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
197 * from here: this will lead to infinite recursion.
200 if (printk_ratelimit())
201 pr_warn("Lustre: debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
202 pgcount + 1, tcd->tcd_cur_pages);
204 INIT_LIST_HEAD(&pc.pc_pages);
206 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
210 list_move_tail(&tage->linkage, &pc.pc_pages);
211 tcd->tcd_cur_pages--;
213 put_pages_on_tcd_daemon_list(&pc, tcd);
216 /* return a page that has 'len' bytes left at the end */
217 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
220 struct cfs_trace_page *tage;
223 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
224 * from here: this will lead to infinite recursion.
227 if (len > PAGE_SIZE) {
228 pr_err("LustreError: cowardly refusing to write %lu bytes in a page\n",
233 tage = cfs_trace_get_tage_try(tcd, len);
238 if (tcd->tcd_cur_pages > 0) {
239 tage = cfs_tage_from_list(tcd->tcd_pages.next);
241 cfs_tage_to_tail(tage, &tcd->tcd_pages);
246 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
247 const char *format, ...)
249 struct cfs_trace_cpu_data *tcd = NULL;
250 struct ptldebug_header header = {0};
251 struct cfs_trace_page *tage;
252 /* string_buf is used only if tcd != NULL, and is always set then */
253 char *string_buf = NULL;
256 int needed = 85; /* average message length */
261 int mask = msgdata->msg_mask;
262 char *file = (char *)msgdata->msg_file;
263 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
265 if (strchr(file, '/'))
266 file = strrchr(file, '/') + 1;
268 tcd = cfs_trace_get_tcd();
270 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
271 * pins us to a particular CPU. This avoids an smp_processor_id()
272 * warning on Linux when debugging is enabled. */
273 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
275 if (tcd == NULL) /* arch may not log in IRQ context */
278 if (tcd->tcd_cur_pages == 0)
279 header.ph_flags |= PH_FLAG_FIRST_RECORD;
281 if (tcd->tcd_shutting_down) {
282 cfs_trace_put_tcd(tcd);
287 known_size = strlen(file) + 1;
289 known_size += strlen(msgdata->msg_fn) + 1;
291 if (libcfs_debug_binary)
292 known_size += sizeof(header);
295 * '2' used because vsnprintf return real size required for output
296 * _without_ terminating NULL.
297 * if needed is to small for this format.
299 for (i = 0; i < 2; i++) {
300 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
302 if (needed + known_size > PAGE_SIZE)
305 cfs_trace_put_tcd(tcd);
310 string_buf = (char *)page_address(tage->page) +
311 tage->used + known_size;
313 max_nob = PAGE_SIZE - tage->used - known_size;
315 pr_emerg("LustreError: negative max_nob: %d\n",
318 cfs_trace_put_tcd(tcd);
324 remain = max_nob - needed;
328 va_start(ap, format);
329 needed += vsnprintf(string_buf + needed, remain,
333 if (needed < max_nob) /* well. printing ok.. */
337 if (*(string_buf + needed - 1) != '\n') {
338 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
339 file, msgdata->msg_line, msgdata->msg_fn);
340 } else if (mask & D_TTY) {
341 /* TTY needs '\r\n' to move carriage to leftmost position */
342 if (needed < 2 || *(string_buf + needed - 2) != '\r')
343 pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
344 file, msgdata->msg_line, msgdata->msg_fn);
347 header.ph_len = known_size + needed;
348 debug_buf = (char *)page_address(tage->page) + tage->used;
350 if (libcfs_debug_binary) {
351 memcpy(debug_buf, &header, sizeof(header));
352 tage->used += sizeof(header);
353 debug_buf += sizeof(header);
356 strcpy(debug_buf, file);
357 tage->used += strlen(file) + 1;
358 debug_buf += strlen(file) + 1;
360 if (msgdata->msg_fn) {
361 strcpy(debug_buf, msgdata->msg_fn);
362 tage->used += strlen(msgdata->msg_fn) + 1;
363 debug_buf += strlen(msgdata->msg_fn) + 1;
366 __LASSERT(debug_buf == string_buf);
368 tage->used += needed;
369 __LASSERT(tage->used <= PAGE_SIZE);
372 if ((mask & libcfs_printk) == 0) {
373 /* no console output requested */
375 cfs_trace_put_tcd(tcd);
380 if (libcfs_console_ratelimit &&
381 cdls->cdls_next != 0 && /* not first time ever */
382 time_before(jiffies, cdls->cdls_next)) {
383 /* skipping a console message */
386 cfs_trace_put_tcd(tcd);
390 if (time_after(jiffies, cdls->cdls_next +
391 libcfs_console_max_delay +
392 cfs_time_seconds(10))) {
393 /* last timeout was a long time ago */
394 cdls->cdls_delay /= libcfs_console_backoff * 4;
396 cdls->cdls_delay *= libcfs_console_backoff;
399 if (cdls->cdls_delay < libcfs_console_min_delay)
400 cdls->cdls_delay = libcfs_console_min_delay;
401 else if (cdls->cdls_delay > libcfs_console_max_delay)
402 cdls->cdls_delay = libcfs_console_max_delay;
404 /* ensure cdls_next is never zero after it's been seen */
405 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
409 cfs_print_to_console(&header, mask, string_buf, needed, file,
411 cfs_trace_put_tcd(tcd);
413 string_buf = cfs_trace_get_console_buffer();
416 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
418 va_start(ap, format);
419 needed += vsnprintf(string_buf+needed, remain,
424 cfs_print_to_console(&header, mask,
425 string_buf, needed, file, msgdata->msg_fn);
430 if (cdls != NULL && cdls->cdls_count != 0) {
431 string_buf = cfs_trace_get_console_buffer();
433 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
434 "Skipped %d previous similar message%s\n",
436 (cdls->cdls_count > 1) ? "s" : "");
438 /* Do not allow print this to TTY */
439 cfs_print_to_console(&header, mask & ~D_TTY, string_buf,
440 needed, file, msgdata->msg_fn);
443 cdls->cdls_count = 0;
448 EXPORT_SYMBOL(libcfs_debug_msg);
451 cfs_trace_assertion_failed(const char *str,
452 struct libcfs_debug_msg_data *msgdata)
454 struct ptldebug_header hdr;
456 libcfs_panic_in_progress = 1;
457 libcfs_catastrophe = 1;
460 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
462 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
463 msgdata->msg_file, msgdata->msg_fn);
465 panic("Lustre debug assertion failure\n");
471 panic_collect_pages(struct page_collection *pc)
473 /* Do the collect_pages job on a single CPU: assumes that all other
474 * CPUs have been stopped during a panic. If this isn't true for some
475 * arch, this will have to be implemented separately in each arch. */
478 struct cfs_trace_cpu_data *tcd;
480 INIT_LIST_HEAD(&pc->pc_pages);
482 cfs_tcd_for_each(tcd, i, j) {
483 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
484 tcd->tcd_cur_pages = 0;
486 if (pc->pc_want_daemon_pages) {
487 list_splice_init(&tcd->tcd_daemon_pages,
489 tcd->tcd_cur_daemon_pages = 0;
494 static void collect_pages_on_all_cpus(struct page_collection *pc)
496 struct cfs_trace_cpu_data *tcd;
499 for_each_possible_cpu(cpu) {
500 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
501 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
502 tcd->tcd_cur_pages = 0;
503 if (pc->pc_want_daemon_pages) {
504 list_splice_init(&tcd->tcd_daemon_pages,
506 tcd->tcd_cur_daemon_pages = 0;
512 static void collect_pages(struct page_collection *pc)
514 INIT_LIST_HEAD(&pc->pc_pages);
516 if (libcfs_panic_in_progress)
517 panic_collect_pages(pc);
519 collect_pages_on_all_cpus(pc);
522 static void put_pages_back_on_all_cpus(struct page_collection *pc)
524 struct cfs_trace_cpu_data *tcd;
525 struct list_head *cur_head;
526 struct cfs_trace_page *tage;
527 struct cfs_trace_page *tmp;
530 for_each_possible_cpu(cpu) {
531 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
532 cur_head = tcd->tcd_pages.next;
534 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
537 __LASSERT_TAGE_INVARIANT(tage);
539 if (tage->cpu != cpu || tage->type != i)
542 cfs_tage_to_tail(tage, cur_head);
543 tcd->tcd_cur_pages++;
549 static void put_pages_back(struct page_collection *pc)
551 if (!libcfs_panic_in_progress)
552 put_pages_back_on_all_cpus(pc);
555 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
556 * we have a good amount of data at all times for dumping during an LBUG, even
557 * if we have been steadily writing (and otherwise discarding) pages via the
559 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
560 struct cfs_trace_cpu_data *tcd)
562 struct cfs_trace_page *tage;
563 struct cfs_trace_page *tmp;
565 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
566 __LASSERT_TAGE_INVARIANT(tage);
568 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
571 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
572 tcd->tcd_cur_daemon_pages++;
574 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
575 struct cfs_trace_page *victim;
577 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
578 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
580 __LASSERT_TAGE_INVARIANT(victim);
582 list_del(&victim->linkage);
583 cfs_tage_free(victim);
584 tcd->tcd_cur_daemon_pages--;
589 static void put_pages_on_daemon_list(struct page_collection *pc)
591 struct cfs_trace_cpu_data *tcd;
594 for_each_possible_cpu(cpu) {
595 cfs_tcd_for_each_type_lock(tcd, i, cpu)
596 put_pages_on_tcd_daemon_list(pc, tcd);
600 void cfs_trace_debug_print(void)
602 struct page_collection pc;
603 struct cfs_trace_page *tage;
604 struct cfs_trace_page *tmp;
606 pc.pc_want_daemon_pages = 1;
608 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
612 __LASSERT_TAGE_INVARIANT(tage);
615 p = page_address(page);
616 while (p < ((char *)page_address(page) + tage->used)) {
617 struct ptldebug_header *hdr;
622 p += strlen(file) + 1;
625 len = hdr->ph_len - (int)(p - (char *)hdr);
627 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
632 list_del(&tage->linkage);
637 int cfs_tracefile_dump_all_pages(char *filename)
639 struct page_collection pc;
641 struct cfs_trace_page *tage;
642 struct cfs_trace_page *tmp;
646 down_write(&cfs_tracefile_sem);
648 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
652 pr_err("LustreError: can't open %s for dump: rc = %d\n",
657 pc.pc_want_daemon_pages = 1;
659 if (list_empty(&pc.pc_pages)) {
664 /* ok, for now, just write the pages. in the future we'll be building
665 * iobufs with the pages and calling generic_direct_IO */
666 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
668 __LASSERT_TAGE_INVARIANT(tage);
670 buf = kmap(tage->page);
671 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
673 if (rc != (int)tage->used) {
674 pr_warn("Lustre: wanted to write %u but wrote %d\n",
677 __LASSERT(list_empty(&pc.pc_pages));
680 list_del(&tage->linkage);
684 rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
686 pr_err("LustreError: sync returns: rc = %d\n", rc);
688 filp_close(filp, NULL);
690 up_write(&cfs_tracefile_sem);
694 void cfs_trace_flush_pages(void)
696 struct page_collection pc;
697 struct cfs_trace_page *tage;
698 struct cfs_trace_page *tmp;
700 pc.pc_want_daemon_pages = 1;
702 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
704 __LASSERT_TAGE_INVARIANT(tage);
706 list_del(&tage->linkage);
711 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
712 const char __user *usr_buffer, int usr_buffer_nob)
716 if (usr_buffer_nob > knl_buffer_nob)
719 if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
722 nob = strnlen(knl_buffer, usr_buffer_nob);
723 while (--nob >= 0) /* strip trailing whitespace */
724 if (!isspace(knl_buffer[nob]))
727 if (nob < 0) /* empty string */
730 if (nob == knl_buffer_nob) /* no space to terminate */
733 knl_buffer[nob + 1] = 0; /* terminate */
736 EXPORT_SYMBOL(cfs_trace_copyin_string);
738 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
739 const char *knl_buffer, char *append)
741 /* NB if 'append' != NULL, it's a single character to append to the
742 * copied out string - usually "\n", for /proc entries and "" (i.e. a
743 * terminating zero byte) for sysctl entries */
744 int nob = strlen(knl_buffer);
746 if (nob > usr_buffer_nob)
747 nob = usr_buffer_nob;
749 if (copy_to_user(usr_buffer, knl_buffer, nob))
752 if (append != NULL && nob < usr_buffer_nob) {
753 if (copy_to_user(usr_buffer + nob, append, 1))
761 EXPORT_SYMBOL(cfs_trace_copyout_string);
763 int cfs_trace_allocate_string_buffer(char **str, int nob)
765 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
768 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
775 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
780 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
784 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
785 usr_str, usr_str_nob);
793 rc = cfs_tracefile_dump_all_pages(str);
799 int cfs_trace_daemon_command(char *str)
803 down_write(&cfs_tracefile_sem);
805 if (strcmp(str, "stop") == 0) {
806 up_write(&cfs_tracefile_sem);
807 cfs_trace_stop_thread();
808 down_write(&cfs_tracefile_sem);
809 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
811 } else if (strncmp(str, "size=", 5) == 0) {
814 rc = kstrtoul(str + 5, 10, &tmp);
816 if (tmp < 10 || tmp > 20480)
817 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
819 cfs_tracefile_size = tmp << 20;
821 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
823 } else if (str[0] != '/') {
826 strcpy(cfs_tracefile, str);
828 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
829 cfs_tracefile, (long)(cfs_tracefile_size >> 10));
831 cfs_trace_start_thread();
834 up_write(&cfs_tracefile_sem);
838 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
843 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
847 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
848 usr_str, usr_str_nob);
850 rc = cfs_trace_daemon_command(str);
856 int cfs_trace_set_debug_mb(int mb)
861 int limit = cfs_trace_max_debug_mb();
862 struct cfs_trace_cpu_data *tcd;
864 if (mb < num_possible_cpus()) {
865 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
866 mb, num_possible_cpus());
867 mb = num_possible_cpus();
871 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %d MB.\n",
876 mb /= num_possible_cpus();
877 pages = mb << (20 - PAGE_SHIFT);
879 down_write(&cfs_tracefile_sem);
881 cfs_tcd_for_each(tcd, i, j)
882 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
884 up_write(&cfs_tracefile_sem);
889 int cfs_trace_get_debug_mb(void)
893 struct cfs_trace_cpu_data *tcd;
896 down_read(&cfs_tracefile_sem);
898 cfs_tcd_for_each(tcd, i, j)
899 total_pages += tcd->tcd_max_pages;
901 up_read(&cfs_tracefile_sem);
903 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
906 static int tracefiled(void *arg)
908 struct page_collection pc;
909 struct tracefiled_ctl *tctl = arg;
910 struct cfs_trace_page *tage;
911 struct cfs_trace_page *tmp;
917 /* we're started late enough that we pick up init's fs context */
918 /* this is so broken in uml? what on earth is going on? */
920 complete(&tctl->tctl_start);
923 wait_queue_entry_t __wait;
925 pc.pc_want_daemon_pages = 0;
927 if (list_empty(&pc.pc_pages))
931 down_read(&cfs_tracefile_sem);
932 if (cfs_tracefile[0] != 0) {
933 filp = filp_open(cfs_tracefile,
934 O_CREAT | O_RDWR | O_LARGEFILE,
939 pr_warn("Lustre: couldn't open %s: rc = %d\n",
943 up_read(&cfs_tracefile_sem);
945 put_pages_on_daemon_list(&pc);
946 __LASSERT(list_empty(&pc.pc_pages));
950 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
951 struct dentry *de = file_dentry(filp);
954 __LASSERT_TAGE_INVARIANT(tage);
956 if (f_pos >= (off_t)cfs_tracefile_size)
958 else if (f_pos > i_size_read(de->d_inode))
959 f_pos = i_size_read(de->d_inode);
961 buf = kmap(tage->page);
962 rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
964 if (rc != (int)tage->used) {
965 pr_warn("Lustre: wanted to write %u but wrote %d\n",
968 __LASSERT(list_empty(&pc.pc_pages));
973 filp_close(filp, NULL);
974 put_pages_on_daemon_list(&pc);
975 if (!list_empty(&pc.pc_pages)) {
978 pr_alert("Lustre: trace pages aren't empty\n");
979 pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
980 for (i = 0; i < num_possible_cpus(); i++)
982 pr_cont("%d(on) ", i);
984 pr_cont("%d(off) ", i);
988 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
990 pr_err("Lustre: page %d belongs to cpu %d\n",
992 pr_err("Lustre: There are %d pages unwritten\n", i);
994 __LASSERT(list_empty(&pc.pc_pages));
996 if (atomic_read(&tctl->tctl_shutdown)) {
997 if (last_loop == 0) {
1004 init_waitqueue_entry(&__wait, current);
1005 add_wait_queue(&tctl->tctl_waitq, &__wait);
1006 schedule_timeout_interruptible(cfs_time_seconds(1));
1007 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1009 complete(&tctl->tctl_stop);
1013 int cfs_trace_start_thread(void)
1015 struct tracefiled_ctl *tctl = &trace_tctl;
1018 mutex_lock(&cfs_trace_thread_mutex);
1022 init_completion(&tctl->tctl_start);
1023 init_completion(&tctl->tctl_stop);
1024 init_waitqueue_head(&tctl->tctl_waitq);
1025 atomic_set(&tctl->tctl_shutdown, 0);
1027 if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1032 wait_for_completion(&tctl->tctl_start);
1035 mutex_unlock(&cfs_trace_thread_mutex);
1039 void cfs_trace_stop_thread(void)
1041 struct tracefiled_ctl *tctl = &trace_tctl;
1043 mutex_lock(&cfs_trace_thread_mutex);
1044 if (thread_running) {
1045 pr_info("Lustre: shutting down debug daemon thread...\n");
1046 atomic_set(&tctl->tctl_shutdown, 1);
1047 wait_for_completion(&tctl->tctl_stop);
1050 mutex_unlock(&cfs_trace_thread_mutex);
1053 int cfs_tracefile_init(int max_pages)
1055 struct cfs_trace_cpu_data *tcd;
1061 rc = cfs_tracefile_init_arch();
1065 cfs_tcd_for_each(tcd, i, j) {
1066 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1067 factor = tcd->tcd_pages_factor;
1068 INIT_LIST_HEAD(&tcd->tcd_pages);
1069 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1070 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1071 tcd->tcd_cur_pages = 0;
1072 tcd->tcd_cur_stock_pages = 0;
1073 tcd->tcd_cur_daemon_pages = 0;
1074 tcd->tcd_max_pages = (max_pages * factor) / 100;
1075 LASSERT(tcd->tcd_max_pages > 0);
1076 tcd->tcd_shutting_down = 0;
1081 static void trace_cleanup_on_all_cpus(void)
1083 struct cfs_trace_cpu_data *tcd;
1084 struct cfs_trace_page *tage;
1085 struct cfs_trace_page *tmp;
1088 for_each_possible_cpu(cpu) {
1089 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1090 tcd->tcd_shutting_down = 1;
1092 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1093 __LASSERT_TAGE_INVARIANT(tage);
1095 list_del(&tage->linkage);
1096 cfs_tage_free(tage);
1098 tcd->tcd_cur_pages = 0;
1103 static void cfs_trace_cleanup(void)
1105 struct page_collection pc;
1107 INIT_LIST_HEAD(&pc.pc_pages);
1109 trace_cleanup_on_all_cpus();
1111 cfs_tracefile_fini_arch();
1114 void cfs_tracefile_exit(void)
1116 cfs_trace_stop_thread();
1117 cfs_trace_cleanup();