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