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);
62 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
63 struct cfs_trace_cpu_data *tcd);
65 static inline struct cfs_trace_page *
66 cfs_tage_from_list(struct list_head *list)
68 return list_entry(list, struct cfs_trace_page, linkage);
71 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
74 struct cfs_trace_page *tage;
76 /* My caller is trying to free memory */
77 if (!in_interrupt() && memory_pressure_get())
81 * Don't spam console with allocation failures: they will be reported
82 * by upper layer anyway.
85 page = alloc_page(gfp);
89 tage = kmalloc(sizeof(*tage), gfp);
96 atomic_inc(&cfs_tage_allocated);
100 static void cfs_tage_free(struct cfs_trace_page *tage)
102 __LASSERT(tage != NULL);
103 __LASSERT(tage->page != NULL);
105 __free_page(tage->page);
107 atomic_dec(&cfs_tage_allocated);
110 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
111 struct list_head *queue)
113 __LASSERT(tage != NULL);
114 __LASSERT(queue != NULL);
116 list_move_tail(&tage->linkage, queue);
119 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, gfp_t gfp,
120 struct list_head *stock)
125 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
126 * from here: this will lead to infinite recursion.
129 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
130 struct cfs_trace_page *tage;
132 tage = cfs_tage_alloc(gfp);
135 list_add_tail(&tage->linkage, stock);
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)
144 struct cfs_trace_page *tage;
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)
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);
159 tage = cfs_tage_alloc(GFP_ATOMIC);
160 if (unlikely(tage == NULL)) {
161 if ((!memory_pressure_get() ||
162 in_interrupt()) && printk_ratelimit())
163 pr_warn("Lustre: cannot allocate a tage (%ld)\n",
170 tage->cpu = smp_processor_id();
171 tage->type = tcd->tcd_type;
172 list_add_tail(&tage->linkage, &tcd->tcd_pages);
173 tcd->tcd_cur_pages++;
175 if (tcd->tcd_cur_pages > 8 && thread_running) {
176 struct tracefiled_ctl *tctl = &trace_tctl;
178 * wake up tracefiled to process some pages.
180 wake_up(&tctl->tctl_waitq);
187 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
189 int pgcount = tcd->tcd_cur_pages / 10;
190 struct page_collection pc;
191 struct cfs_trace_page *tage;
192 struct cfs_trace_page *tmp;
195 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
196 * from here: this will lead to infinite recursion.
199 if (printk_ratelimit())
200 pr_warn("Lustre: debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
201 pgcount + 1, tcd->tcd_cur_pages);
203 INIT_LIST_HEAD(&pc.pc_pages);
205 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
209 list_move_tail(&tage->linkage, &pc.pc_pages);
210 tcd->tcd_cur_pages--;
212 put_pages_on_tcd_daemon_list(&pc, tcd);
215 /* return a page that has 'len' bytes left at the end */
216 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
219 struct cfs_trace_page *tage;
222 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
223 * from here: this will lead to infinite recursion.
226 if (len > PAGE_SIZE) {
227 pr_err("LustreError: cowardly refusing to write %lu bytes in a page\n",
232 tage = cfs_trace_get_tage_try(tcd, len);
237 if (tcd->tcd_cur_pages > 0) {
238 tage = cfs_tage_from_list(tcd->tcd_pages.next);
240 cfs_tage_to_tail(tage, &tcd->tcd_pages);
245 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
246 const char *format, ...)
248 struct cfs_trace_cpu_data *tcd = NULL;
249 struct ptldebug_header header = {0};
250 struct cfs_trace_page *tage;
251 /* string_buf is used only if tcd != NULL, and is always set then */
252 char *string_buf = NULL;
255 int needed = 85; /* average message length */
260 int mask = msgdata->msg_mask;
261 char *file = (char *)msgdata->msg_file;
262 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
264 if (strchr(file, '/'))
265 file = strrchr(file, '/') + 1;
267 tcd = cfs_trace_get_tcd();
269 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
270 * pins us to a particular CPU. This avoids an smp_processor_id()
271 * warning on Linux when debugging is enabled. */
272 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
274 if (tcd == NULL) /* arch may not log in IRQ context */
277 if (tcd->tcd_cur_pages == 0)
278 header.ph_flags |= PH_FLAG_FIRST_RECORD;
280 if (tcd->tcd_shutting_down) {
281 cfs_trace_put_tcd(tcd);
286 known_size = strlen(file) + 1;
288 known_size += strlen(msgdata->msg_fn) + 1;
290 if (libcfs_debug_binary)
291 known_size += sizeof(header);
294 * '2' used because vsnprintf return real size required for output
295 * _without_ terminating NULL.
296 * if needed is to small for this format.
298 for (i = 0; i < 2; i++) {
299 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
301 if (needed + known_size > PAGE_SIZE)
304 cfs_trace_put_tcd(tcd);
309 string_buf = (char *)page_address(tage->page) +
310 tage->used + known_size;
312 max_nob = PAGE_SIZE - tage->used - known_size;
314 pr_emerg("LustreError: negative max_nob: %d\n",
317 cfs_trace_put_tcd(tcd);
323 remain = max_nob - needed;
327 va_start(ap, format);
328 needed += vsnprintf(string_buf + needed, remain,
332 if (needed < max_nob) /* well. printing ok.. */
336 if (*(string_buf + needed - 1) != '\n') {
337 pr_info("Lustre: format at %s:%d:%s doesn't end in newline\n",
338 file, msgdata->msg_line, msgdata->msg_fn);
339 } else if (mask & D_TTY) {
340 /* TTY needs '\r\n' to move carriage to leftmost position */
341 if (needed < 2 || *(string_buf + needed - 2) != '\r')
342 pr_info("Lustre: format at %s:%d:%s doesn't end in '\\r\\n'\n",
343 file, msgdata->msg_line, msgdata->msg_fn);
346 header.ph_len = known_size + needed;
347 debug_buf = (char *)page_address(tage->page) + tage->used;
349 if (libcfs_debug_binary) {
350 memcpy(debug_buf, &header, sizeof(header));
351 tage->used += sizeof(header);
352 debug_buf += sizeof(header);
355 strcpy(debug_buf, file);
356 tage->used += strlen(file) + 1;
357 debug_buf += strlen(file) + 1;
359 if (msgdata->msg_fn) {
360 strcpy(debug_buf, msgdata->msg_fn);
361 tage->used += strlen(msgdata->msg_fn) + 1;
362 debug_buf += strlen(msgdata->msg_fn) + 1;
365 __LASSERT(debug_buf == string_buf);
367 tage->used += needed;
368 __LASSERT(tage->used <= PAGE_SIZE);
371 if ((mask & libcfs_printk) == 0) {
372 /* no console output requested */
374 cfs_trace_put_tcd(tcd);
379 if (libcfs_console_ratelimit &&
380 cdls->cdls_next != 0 && /* not first time ever */
381 time_before(jiffies, cdls->cdls_next)) {
382 /* skipping a console message */
385 cfs_trace_put_tcd(tcd);
389 if (time_after(jiffies, cdls->cdls_next +
390 libcfs_console_max_delay +
391 cfs_time_seconds(10))) {
392 /* last timeout was a long time ago */
393 cdls->cdls_delay /= libcfs_console_backoff * 4;
395 cdls->cdls_delay *= libcfs_console_backoff;
398 if (cdls->cdls_delay < libcfs_console_min_delay)
399 cdls->cdls_delay = libcfs_console_min_delay;
400 else if (cdls->cdls_delay > libcfs_console_max_delay)
401 cdls->cdls_delay = libcfs_console_max_delay;
403 /* ensure cdls_next is never zero after it's been seen */
404 cdls->cdls_next = (jiffies + cdls->cdls_delay) | 1;
408 cfs_print_to_console(&header, mask, string_buf, needed, file,
410 cfs_trace_put_tcd(tcd);
412 string_buf = cfs_trace_get_console_buffer();
415 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
417 va_start(ap, format);
418 needed += vsnprintf(string_buf+needed, remain,
423 cfs_print_to_console(&header, mask,
424 string_buf, needed, file, msgdata->msg_fn);
429 if (cdls != NULL && cdls->cdls_count != 0) {
430 string_buf = cfs_trace_get_console_buffer();
432 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
433 "Skipped %d previous similar message%s\n",
435 (cdls->cdls_count > 1) ? "s" : "");
437 /* Do not allow print this to TTY */
438 cfs_print_to_console(&header, mask & ~D_TTY, string_buf,
439 needed, file, msgdata->msg_fn);
442 cdls->cdls_count = 0;
447 EXPORT_SYMBOL(libcfs_debug_msg);
450 cfs_trace_assertion_failed(const char *str,
451 struct libcfs_debug_msg_data *msgdata)
453 struct ptldebug_header hdr;
455 libcfs_panic_in_progress = 1;
456 libcfs_catastrophe = 1;
459 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
461 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
462 msgdata->msg_file, msgdata->msg_fn);
464 panic("Lustre debug assertion failure\n");
470 panic_collect_pages(struct page_collection *pc)
472 /* Do the collect_pages job on a single CPU: assumes that all other
473 * CPUs have been stopped during a panic. If this isn't true for some
474 * arch, this will have to be implemented separately in each arch. */
477 struct cfs_trace_cpu_data *tcd;
479 INIT_LIST_HEAD(&pc->pc_pages);
481 cfs_tcd_for_each(tcd, i, j) {
482 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
483 tcd->tcd_cur_pages = 0;
485 if (pc->pc_want_daemon_pages) {
486 list_splice_init(&tcd->tcd_daemon_pages,
488 tcd->tcd_cur_daemon_pages = 0;
493 static void collect_pages_on_all_cpus(struct page_collection *pc)
495 struct cfs_trace_cpu_data *tcd;
498 for_each_possible_cpu(cpu) {
499 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
500 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
501 tcd->tcd_cur_pages = 0;
502 if (pc->pc_want_daemon_pages) {
503 list_splice_init(&tcd->tcd_daemon_pages,
505 tcd->tcd_cur_daemon_pages = 0;
511 static void collect_pages(struct page_collection *pc)
513 INIT_LIST_HEAD(&pc->pc_pages);
515 if (libcfs_panic_in_progress)
516 panic_collect_pages(pc);
518 collect_pages_on_all_cpus(pc);
521 static void put_pages_back_on_all_cpus(struct page_collection *pc)
523 struct cfs_trace_cpu_data *tcd;
524 struct list_head *cur_head;
525 struct cfs_trace_page *tage;
526 struct cfs_trace_page *tmp;
529 for_each_possible_cpu(cpu) {
530 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
531 cur_head = tcd->tcd_pages.next;
533 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
536 __LASSERT_TAGE_INVARIANT(tage);
538 if (tage->cpu != cpu || tage->type != i)
541 cfs_tage_to_tail(tage, cur_head);
542 tcd->tcd_cur_pages++;
548 static void put_pages_back(struct page_collection *pc)
550 if (!libcfs_panic_in_progress)
551 put_pages_back_on_all_cpus(pc);
554 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
555 * we have a good amount of data at all times for dumping during an LBUG, even
556 * if we have been steadily writing (and otherwise discarding) pages via the
558 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
559 struct cfs_trace_cpu_data *tcd)
561 struct cfs_trace_page *tage;
562 struct cfs_trace_page *tmp;
564 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
565 __LASSERT_TAGE_INVARIANT(tage);
567 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
570 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
571 tcd->tcd_cur_daemon_pages++;
573 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
574 struct cfs_trace_page *victim;
576 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
577 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
579 __LASSERT_TAGE_INVARIANT(victim);
581 list_del(&victim->linkage);
582 cfs_tage_free(victim);
583 tcd->tcd_cur_daemon_pages--;
588 static void put_pages_on_daemon_list(struct page_collection *pc)
590 struct cfs_trace_cpu_data *tcd;
593 for_each_possible_cpu(cpu) {
594 cfs_tcd_for_each_type_lock(tcd, i, cpu)
595 put_pages_on_tcd_daemon_list(pc, tcd);
599 void cfs_trace_debug_print(void)
601 struct page_collection pc;
602 struct cfs_trace_page *tage;
603 struct cfs_trace_page *tmp;
605 pc.pc_want_daemon_pages = 1;
607 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
611 __LASSERT_TAGE_INVARIANT(tage);
614 p = page_address(page);
615 while (p < ((char *)page_address(page) + tage->used)) {
616 struct ptldebug_header *hdr;
621 p += strlen(file) + 1;
624 len = hdr->ph_len - (int)(p - (char *)hdr);
626 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
631 list_del(&tage->linkage);
636 int cfs_tracefile_dump_all_pages(char *filename)
638 struct page_collection pc;
640 struct cfs_trace_page *tage;
641 struct cfs_trace_page *tmp;
645 cfs_tracefile_write_lock();
647 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
651 pr_err("LustreError: can't open %s for dump: rc = %d\n",
656 pc.pc_want_daemon_pages = 1;
658 if (list_empty(&pc.pc_pages)) {
663 /* ok, for now, just write the pages. in the future we'll be building
664 * iobufs with the pages and calling generic_direct_IO */
665 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
667 __LASSERT_TAGE_INVARIANT(tage);
669 buf = kmap(tage->page);
670 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
672 if (rc != (int)tage->used) {
673 pr_warn("Lustre: wanted to write %u but wrote %d\n",
676 __LASSERT(list_empty(&pc.pc_pages));
679 list_del(&tage->linkage);
683 rc = vfs_fsync_range(filp, 0, LLONG_MAX, 1);
685 pr_err("LustreError: sync returns: rc = %d\n", rc);
687 filp_close(filp, NULL);
689 cfs_tracefile_write_unlock();
693 void cfs_trace_flush_pages(void)
695 struct page_collection pc;
696 struct cfs_trace_page *tage;
697 struct cfs_trace_page *tmp;
699 pc.pc_want_daemon_pages = 1;
701 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
703 __LASSERT_TAGE_INVARIANT(tage);
705 list_del(&tage->linkage);
710 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
711 const char __user *usr_buffer, int usr_buffer_nob)
715 if (usr_buffer_nob > knl_buffer_nob)
718 if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
721 nob = strnlen(knl_buffer, usr_buffer_nob);
722 while (--nob >= 0) /* strip trailing whitespace */
723 if (!isspace(knl_buffer[nob]))
726 if (nob < 0) /* empty string */
729 if (nob == knl_buffer_nob) /* no space to terminate */
732 knl_buffer[nob + 1] = 0; /* terminate */
735 EXPORT_SYMBOL(cfs_trace_copyin_string);
737 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
738 const char *knl_buffer, char *append)
740 /* NB if 'append' != NULL, it's a single character to append to the
741 * copied out string - usually "\n", for /proc entries and "" (i.e. a
742 * terminating zero byte) for sysctl entries */
743 int nob = strlen(knl_buffer);
745 if (nob > usr_buffer_nob)
746 nob = usr_buffer_nob;
748 if (copy_to_user(usr_buffer, knl_buffer, nob))
751 if (append != NULL && nob < usr_buffer_nob) {
752 if (copy_to_user(usr_buffer + nob, append, 1))
760 EXPORT_SYMBOL(cfs_trace_copyout_string);
762 int cfs_trace_allocate_string_buffer(char **str, int nob)
764 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
767 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
774 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
779 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
783 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
784 usr_str, usr_str_nob);
792 rc = cfs_tracefile_dump_all_pages(str);
798 int cfs_trace_daemon_command(char *str)
802 cfs_tracefile_write_lock();
804 if (strcmp(str, "stop") == 0) {
805 cfs_tracefile_write_unlock();
806 cfs_trace_stop_thread();
807 cfs_tracefile_write_lock();
808 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
810 } else if (strncmp(str, "size=", 5) == 0) {
813 rc = kstrtoul(str + 5, 10, &tmp);
815 if (tmp < 10 || tmp > 20480)
816 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
818 cfs_tracefile_size = tmp << 20;
820 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
822 } else if (str[0] != '/') {
825 strcpy(cfs_tracefile, str);
827 pr_info("Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
828 cfs_tracefile, (long)(cfs_tracefile_size >> 10));
830 cfs_trace_start_thread();
833 cfs_tracefile_write_unlock();
837 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
842 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
846 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
847 usr_str, usr_str_nob);
849 rc = cfs_trace_daemon_command(str);
855 int cfs_trace_set_debug_mb(int mb)
860 int limit = cfs_trace_max_debug_mb();
861 struct cfs_trace_cpu_data *tcd;
863 if (mb < num_possible_cpus()) {
864 pr_warn("Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
865 mb, num_possible_cpus());
866 mb = num_possible_cpus();
870 pr_warn("Lustre: %d MB is too large for debug buffer size, setting it to %d MB.\n",
875 mb /= num_possible_cpus();
876 pages = mb << (20 - PAGE_SHIFT);
878 cfs_tracefile_write_lock();
880 cfs_tcd_for_each(tcd, i, j)
881 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
883 cfs_tracefile_write_unlock();
888 int cfs_trace_get_debug_mb(void)
892 struct cfs_trace_cpu_data *tcd;
895 cfs_tracefile_read_lock();
897 cfs_tcd_for_each(tcd, i, j)
898 total_pages += tcd->tcd_max_pages;
900 cfs_tracefile_read_unlock();
902 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
905 static int tracefiled(void *arg)
907 struct page_collection pc;
908 struct tracefiled_ctl *tctl = arg;
909 struct cfs_trace_page *tage;
910 struct cfs_trace_page *tmp;
916 /* we're started late enough that we pick up init's fs context */
917 /* this is so broken in uml? what on earth is going on? */
919 complete(&tctl->tctl_start);
922 wait_queue_entry_t __wait;
924 pc.pc_want_daemon_pages = 0;
926 if (list_empty(&pc.pc_pages))
930 cfs_tracefile_read_lock();
931 if (cfs_tracefile[0] != 0) {
932 filp = filp_open(cfs_tracefile,
933 O_CREAT | O_RDWR | O_LARGEFILE,
938 pr_warn("Lustre: couldn't open %s: rc = %d\n",
942 cfs_tracefile_read_unlock();
944 put_pages_on_daemon_list(&pc);
945 __LASSERT(list_empty(&pc.pc_pages));
949 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
950 struct dentry *de = file_dentry(filp);
953 __LASSERT_TAGE_INVARIANT(tage);
955 if (f_pos >= (off_t)cfs_tracefile_size)
957 else if (f_pos > i_size_read(de->d_inode))
958 f_pos = i_size_read(de->d_inode);
960 buf = kmap(tage->page);
961 rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
963 if (rc != (int)tage->used) {
964 pr_warn("Lustre: wanted to write %u but wrote %d\n",
967 __LASSERT(list_empty(&pc.pc_pages));
972 filp_close(filp, NULL);
973 put_pages_on_daemon_list(&pc);
974 if (!list_empty(&pc.pc_pages)) {
977 pr_alert("Lustre: trace pages aren't empty\n");
978 pr_err("Lustre: total cpus(%d): ", num_possible_cpus());
979 for (i = 0; i < num_possible_cpus(); i++)
981 pr_cont("%d(on) ", i);
983 pr_cont("%d(off) ", i);
987 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
989 pr_err("Lustre: page %d belongs to cpu %d\n",
991 pr_err("Lustre: There are %d pages unwritten\n", i);
993 __LASSERT(list_empty(&pc.pc_pages));
995 if (atomic_read(&tctl->tctl_shutdown)) {
996 if (last_loop == 0) {
1003 init_waitqueue_entry(&__wait, current);
1004 add_wait_queue(&tctl->tctl_waitq, &__wait);
1005 schedule_timeout_interruptible(cfs_time_seconds(1));
1006 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1008 complete(&tctl->tctl_stop);
1012 int cfs_trace_start_thread(void)
1014 struct tracefiled_ctl *tctl = &trace_tctl;
1017 mutex_lock(&cfs_trace_thread_mutex);
1021 init_completion(&tctl->tctl_start);
1022 init_completion(&tctl->tctl_stop);
1023 init_waitqueue_head(&tctl->tctl_waitq);
1024 atomic_set(&tctl->tctl_shutdown, 0);
1026 if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1031 wait_for_completion(&tctl->tctl_start);
1034 mutex_unlock(&cfs_trace_thread_mutex);
1038 void cfs_trace_stop_thread(void)
1040 struct tracefiled_ctl *tctl = &trace_tctl;
1042 mutex_lock(&cfs_trace_thread_mutex);
1043 if (thread_running) {
1044 pr_info("Lustre: shutting down debug daemon thread...\n");
1045 atomic_set(&tctl->tctl_shutdown, 1);
1046 wait_for_completion(&tctl->tctl_stop);
1049 mutex_unlock(&cfs_trace_thread_mutex);
1052 int cfs_tracefile_init(int max_pages)
1054 struct cfs_trace_cpu_data *tcd;
1060 rc = cfs_tracefile_init_arch();
1064 cfs_tcd_for_each(tcd, i, j) {
1065 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1066 factor = tcd->tcd_pages_factor;
1067 INIT_LIST_HEAD(&tcd->tcd_pages);
1068 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1069 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1070 tcd->tcd_cur_pages = 0;
1071 tcd->tcd_cur_stock_pages = 0;
1072 tcd->tcd_cur_daemon_pages = 0;
1073 tcd->tcd_max_pages = (max_pages * factor) / 100;
1074 LASSERT(tcd->tcd_max_pages > 0);
1075 tcd->tcd_shutting_down = 0;
1080 static void trace_cleanup_on_all_cpus(void)
1082 struct cfs_trace_cpu_data *tcd;
1083 struct cfs_trace_page *tage;
1084 struct cfs_trace_page *tmp;
1087 for_each_possible_cpu(cpu) {
1088 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1089 tcd->tcd_shutting_down = 1;
1091 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1092 __LASSERT_TAGE_INVARIANT(tage);
1094 list_del(&tage->linkage);
1095 cfs_tage_free(tage);
1097 tcd->tcd_cur_pages = 0;
1102 static void cfs_trace_cleanup(void)
1104 struct page_collection pc;
1106 INIT_LIST_HEAD(&pc.pc_pages);
1108 trace_cleanup_on_all_cpus();
1110 cfs_tracefile_fini_arch();
1113 void cfs_tracefile_exit(void)
1115 cfs_trace_stop_thread();
1116 cfs_trace_cleanup();