Whamcloud - gitweb
LU-8560 libcfs: handle PAGE_CACHE_* removal in newer kernels
[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 <linux/kthread.h>
48 #include <libcfs/libcfs.h>
49
50 /* XXX move things up to the top, comment */
51 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
52
53 char cfs_tracefile[TRACEFILE_NAME_SIZE];
54 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
55 static struct tracefiled_ctl trace_tctl;
56 static DEFINE_MUTEX(cfs_trace_thread_mutex);
57 static int thread_running = 0;
58
59 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
60
61 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
62                                         struct cfs_trace_cpu_data *tcd);
63
64 static inline struct cfs_trace_page *
65 cfs_tage_from_list(struct list_head *list)
66 {
67         return list_entry(list, struct cfs_trace_page, linkage);
68 }
69
70 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
71 {
72         struct page            *page;
73         struct cfs_trace_page *tage;
74
75         /* My caller is trying to free memory */
76         if (!in_interrupt() && memory_pressure_get())
77                 return NULL;
78
79         /*
80          * Don't spam console with allocation failures: they will be reported
81          * by upper layer anyway.
82          */
83         gfp |= __GFP_NOWARN;
84         page = alloc_page(gfp);
85         if (page == NULL)
86                 return NULL;
87
88         tage = kmalloc(sizeof(*tage), gfp);
89         if (tage == NULL) {
90                 __free_page(page);
91                 return NULL;
92         }
93
94         tage->page = page;
95         atomic_inc(&cfs_tage_allocated);
96         return tage;
97 }
98
99 static void cfs_tage_free(struct cfs_trace_page *tage)
100 {
101         __LASSERT(tage != NULL);
102         __LASSERT(tage->page != NULL);
103
104         __free_page(tage->page);
105         kfree(tage);
106         atomic_dec(&cfs_tage_allocated);
107 }
108
109 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
110                              struct list_head *queue)
111 {
112         __LASSERT(tage != NULL);
113         __LASSERT(queue != NULL);
114
115         list_move_tail(&tage->linkage, queue);
116 }
117
118 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, gfp_t gfp,
119                            struct list_head *stock)
120 {
121         int i;
122
123         /*
124          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
125          * from here: this will lead to infinite recursion.
126          */
127
128         for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
129                 struct cfs_trace_page *tage;
130
131                 tage = cfs_tage_alloc(gfp);
132                 if (tage == NULL)
133                         break;
134                 list_add_tail(&tage->linkage, stock);
135         }
136         return i;
137 }
138
139 /* return a page that has 'len' bytes left at the end */
140 static struct cfs_trace_page *
141 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
142 {
143         struct cfs_trace_page *tage;
144
145         if (tcd->tcd_cur_pages > 0) {
146                 __LASSERT(!list_empty(&tcd->tcd_pages));
147                 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
148                 if (tage->used + len <= PAGE_SIZE)
149                         return tage;
150         }
151
152         if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
153                 if (tcd->tcd_cur_stock_pages > 0) {
154                         tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
155                         --tcd->tcd_cur_stock_pages;
156                         list_del_init(&tage->linkage);
157                 } else {
158                         tage = cfs_tage_alloc(GFP_ATOMIC);
159                         if (unlikely(tage == NULL)) {
160                                 if ((!memory_pressure_get() ||
161                                      in_interrupt()) && printk_ratelimit())
162                                         printk(KERN_WARNING
163                                                "cannot allocate a tage (%ld)\n",
164                                                tcd->tcd_cur_pages);
165                                 return NULL;
166                         }
167                 }
168
169                 tage->used = 0;
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++;
174
175                 if (tcd->tcd_cur_pages > 8 && thread_running) {
176                         struct tracefiled_ctl *tctl = &trace_tctl;
177                         /*
178                          * wake up tracefiled to process some pages.
179                          */
180                         wake_up(&tctl->tctl_waitq);
181                 }
182                 return tage;
183         }
184         return NULL;
185 }
186
187 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
188 {
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;
193
194         /*
195          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
196          * from here: this will lead to infinite recursion.
197          */
198
199         if (printk_ratelimit())
200                 printk(KERN_WARNING "debug daemon buffer overflowed; "
201                         "discarding 10%% of pages (%d of %ld)\n",
202                         pgcount + 1, tcd->tcd_cur_pages);
203
204         INIT_LIST_HEAD(&pc.pc_pages);
205
206         list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
207                 if (pgcount-- == 0)
208                         break;
209
210                 list_move_tail(&tage->linkage, &pc.pc_pages);
211                 tcd->tcd_cur_pages--;
212         }
213         put_pages_on_tcd_daemon_list(&pc, tcd);
214 }
215
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,
218                                                  unsigned long len)
219 {
220         struct cfs_trace_page *tage;
221
222         /*
223          * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
224          * from here: this will lead to infinite recursion.
225          */
226
227         if (len > PAGE_SIZE) {
228                 printk(KERN_ERR
229                        "cowardly refusing to write %lu bytes in a page\n", len);
230                 return NULL;
231         }
232
233         tage = cfs_trace_get_tage_try(tcd, len);
234         if (tage != NULL)
235                 return tage;
236         if (thread_running)
237                 cfs_tcd_shrink(tcd);
238         if (tcd->tcd_cur_pages > 0) {
239                 tage = cfs_tage_from_list(tcd->tcd_pages.next);
240                 tage->used = 0;
241                 cfs_tage_to_tail(tage, &tcd->tcd_pages);
242         }
243         return tage;
244 }
245
246 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
247                      const char *format, ...)
248 {
249         va_list args;
250         int     rc;
251
252         va_start(args, format);
253         rc = libcfs_debug_vmsg2(msgdata, format, args, NULL);
254         va_end(args);
255
256         return rc;
257 }
258 EXPORT_SYMBOL(libcfs_debug_msg);
259
260 int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
261                        const char *format1, va_list args,
262                        const char *format2, ...)
263 {
264         struct cfs_trace_cpu_data *tcd = NULL;
265         struct ptldebug_header     header = {0};
266         struct cfs_trace_page     *tage;
267         /* string_buf is used only if tcd != NULL, and is always set then */
268         char                      *string_buf = NULL;
269         char                      *debug_buf;
270         int                        known_size;
271         int                        needed = 85; /* average message length */
272         int                        max_nob;
273         va_list                    ap;
274         int                        i;
275         int                        remain;
276         int                        mask = msgdata->msg_mask;
277         char                      *file = (char *)msgdata->msg_file;
278         struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
279
280         if (strchr(file, '/'))
281                 file = strrchr(file, '/') + 1;
282
283         tcd = cfs_trace_get_tcd();
284
285         /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
286          * pins us to a particular CPU.  This avoids an smp_processor_id()
287          * warning on Linux when debugging is enabled. */
288         cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
289
290         if (tcd == NULL)                /* arch may not log in IRQ context */
291                 goto console;
292
293         if (tcd->tcd_cur_pages == 0)
294                 header.ph_flags |= PH_FLAG_FIRST_RECORD;
295
296         if (tcd->tcd_shutting_down) {
297                 cfs_trace_put_tcd(tcd);
298                 tcd = NULL;
299                 goto console;
300         }
301
302         known_size = strlen(file) + 1;
303         if (msgdata->msg_fn)
304                 known_size += strlen(msgdata->msg_fn) + 1;
305
306         if (libcfs_debug_binary)
307                 known_size += sizeof(header);
308
309         /*/
310          * '2' used because vsnprintf return real size required for output
311          * _without_ terminating NULL.
312          * if needed is to small for this format.
313          */
314         for (i = 0; i < 2; i++) {
315                 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
316                 if (tage == NULL) {
317                         if (needed + known_size > PAGE_SIZE)
318                                 mask |= D_ERROR;
319
320                         cfs_trace_put_tcd(tcd);
321                         tcd = NULL;
322                         goto console;
323                 }
324
325                 string_buf = (char *)page_address(tage->page) +
326                                         tage->used + known_size;
327
328                 max_nob = PAGE_SIZE - tage->used - known_size;
329                 if (max_nob <= 0) {
330                         printk(KERN_EMERG "negative max_nob: %d\n",
331                                max_nob);
332                         mask |= D_ERROR;
333                         cfs_trace_put_tcd(tcd);
334                         tcd = NULL;
335                         goto console;
336                 }
337
338                 needed = 0;
339                 if (format1) {
340                         va_copy(ap, args);
341                         needed = vsnprintf(string_buf, max_nob, format1, ap);
342                         va_end(ap);
343                 }
344
345                 if (format2) {
346                         remain = max_nob - needed;
347                         if (remain < 0)
348                                 remain = 0;
349
350                         va_start(ap, format2);
351                         needed += vsnprintf(string_buf + needed, remain,
352                                             format2, ap);
353                         va_end(ap);
354                 }
355
356                 if (needed < max_nob) /* well. printing ok.. */
357                         break;
358         }
359
360         if (*(string_buf+needed-1) != '\n')
361                 printk(KERN_INFO "format at %s:%d:%s doesn't end in "
362                        "newline\n", file, msgdata->msg_line, msgdata->msg_fn);
363
364         header.ph_len = known_size + needed;
365         debug_buf = (char *)page_address(tage->page) + tage->used;
366
367         if (libcfs_debug_binary) {
368                 memcpy(debug_buf, &header, sizeof(header));
369                 tage->used += sizeof(header);
370                 debug_buf += sizeof(header);
371         }
372
373         strcpy(debug_buf, file);
374         tage->used += strlen(file) + 1;
375         debug_buf += strlen(file) + 1;
376
377         if (msgdata->msg_fn) {
378                 strcpy(debug_buf, msgdata->msg_fn);
379                 tage->used += strlen(msgdata->msg_fn) + 1;
380                 debug_buf += strlen(msgdata->msg_fn) + 1;
381         }
382
383         __LASSERT(debug_buf == string_buf);
384
385         tage->used += needed;
386         __LASSERT(tage->used <= PAGE_SIZE);
387
388 console:
389         if ((mask & libcfs_printk) == 0) {
390                 /* no console output requested */
391                 if (tcd != NULL)
392                         cfs_trace_put_tcd(tcd);
393                 return 1;
394         }
395
396         if (cdls != NULL) {
397                 if (libcfs_console_ratelimit &&
398                     cdls->cdls_next != 0 &&     /* not first time ever */
399                     !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
400                         /* skipping a console message */
401                         cdls->cdls_count++;
402                         if (tcd != NULL)
403                                 cfs_trace_put_tcd(tcd);
404                         return 1;
405                 }
406
407                 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
408                                                        libcfs_console_max_delay
409                                                        + cfs_time_seconds(10))) {
410                         /* last timeout was a long time ago */
411                         cdls->cdls_delay /= libcfs_console_backoff * 4;
412                 } else {
413                         cdls->cdls_delay *= libcfs_console_backoff;
414                 }
415
416                 if (cdls->cdls_delay < libcfs_console_min_delay)
417                         cdls->cdls_delay = libcfs_console_min_delay;
418                 else if (cdls->cdls_delay > libcfs_console_max_delay)
419                         cdls->cdls_delay = libcfs_console_max_delay;
420
421                 /* ensure cdls_next is never zero after it's been seen */
422                 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
423         }
424
425         if (tcd != NULL) {
426                 cfs_print_to_console(&header, mask, string_buf, needed, file,
427                                      msgdata->msg_fn);
428                 cfs_trace_put_tcd(tcd);
429         } else {
430                 string_buf = cfs_trace_get_console_buffer();
431
432                 needed = 0;
433                 if (format1 != NULL) {
434                         va_copy(ap, args);
435                         needed = vsnprintf(string_buf,
436                                            CFS_TRACE_CONSOLE_BUFFER_SIZE,
437                                            format1, ap);
438                         va_end(ap);
439                 }
440                 if (format2 != NULL) {
441                         remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
442                         if (remain > 0) {
443                                 va_start(ap, format2);
444                                 needed += vsnprintf(string_buf+needed, remain,
445                                                     format2, ap);
446                                 va_end(ap);
447                         }
448                 }
449                 cfs_print_to_console(&header, mask,
450                                      string_buf, needed, file, msgdata->msg_fn);
451
452                 put_cpu();
453         }
454
455         if (cdls != NULL && cdls->cdls_count != 0) {
456                 string_buf = cfs_trace_get_console_buffer();
457
458                 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
459                                   "Skipped %d previous similar message%s\n",
460                                   cdls->cdls_count,
461                                   (cdls->cdls_count > 1) ? "s" : "");
462
463                 cfs_print_to_console(&header, mask,
464                                      string_buf, needed, file, msgdata->msg_fn);
465
466                 put_cpu();
467                 cdls->cdls_count = 0;
468         }
469
470         return 0;
471 }
472 EXPORT_SYMBOL(libcfs_debug_vmsg2);
473
474 void
475 cfs_trace_assertion_failed(const char *str,
476                            struct libcfs_debug_msg_data *msgdata)
477 {
478         struct ptldebug_header hdr;
479
480         libcfs_panic_in_progress = 1;
481         libcfs_catastrophe = 1;
482         smp_mb();
483
484         cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
485
486         cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
487                              msgdata->msg_file, msgdata->msg_fn);
488
489         panic("Lustre debug assertion failure\n");
490
491         /* not reached */
492 }
493
494 static void
495 panic_collect_pages(struct page_collection *pc)
496 {
497         /* Do the collect_pages job on a single CPU: assumes that all other
498          * CPUs have been stopped during a panic.  If this isn't true for some
499          * arch, this will have to be implemented separately in each arch.  */
500         int                        i;
501         int                        j;
502         struct cfs_trace_cpu_data *tcd;
503
504         INIT_LIST_HEAD(&pc->pc_pages);
505
506         cfs_tcd_for_each(tcd, i, j) {
507                 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
508                 tcd->tcd_cur_pages = 0;
509
510                 if (pc->pc_want_daemon_pages) {
511                         list_splice_init(&tcd->tcd_daemon_pages,
512                                                 &pc->pc_pages);
513                         tcd->tcd_cur_daemon_pages = 0;
514                 }
515         }
516 }
517
518 static void collect_pages_on_all_cpus(struct page_collection *pc)
519 {
520         struct cfs_trace_cpu_data *tcd;
521         int i, cpu;
522
523         for_each_possible_cpu(cpu) {
524                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
525                         list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
526                         tcd->tcd_cur_pages = 0;
527                         if (pc->pc_want_daemon_pages) {
528                                 list_splice_init(&tcd->tcd_daemon_pages,
529                                                         &pc->pc_pages);
530                                 tcd->tcd_cur_daemon_pages = 0;
531                         }
532                 }
533         }
534 }
535
536 static void collect_pages(struct page_collection *pc)
537 {
538         INIT_LIST_HEAD(&pc->pc_pages);
539
540         if (libcfs_panic_in_progress)
541                 panic_collect_pages(pc);
542         else
543                 collect_pages_on_all_cpus(pc);
544 }
545
546 static void put_pages_back_on_all_cpus(struct page_collection *pc)
547 {
548         struct cfs_trace_cpu_data *tcd;
549         struct list_head *cur_head;
550         struct cfs_trace_page *tage;
551         struct cfs_trace_page *tmp;
552         int i, cpu;
553
554         for_each_possible_cpu(cpu) {
555                 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
556                         cur_head = tcd->tcd_pages.next;
557
558                         list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
559                                                  linkage) {
560
561                                 __LASSERT_TAGE_INVARIANT(tage);
562
563                                 if (tage->cpu != cpu || tage->type != i)
564                                         continue;
565
566                                 cfs_tage_to_tail(tage, cur_head);
567                                 tcd->tcd_cur_pages++;
568                         }
569                 }
570         }
571 }
572
573 static void put_pages_back(struct page_collection *pc)
574 {
575         if (!libcfs_panic_in_progress)
576                 put_pages_back_on_all_cpus(pc);
577 }
578
579 /* Add pages to a per-cpu debug daemon ringbuffer.  This buffer makes sure that
580  * we have a good amount of data at all times for dumping during an LBUG, even
581  * if we have been steadily writing (and otherwise discarding) pages via the
582  * debug daemon. */
583 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
584                                          struct cfs_trace_cpu_data *tcd)
585 {
586         struct cfs_trace_page *tage;
587         struct cfs_trace_page *tmp;
588
589         list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
590                 __LASSERT_TAGE_INVARIANT(tage);
591
592                 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
593                         continue;
594
595                 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
596                 tcd->tcd_cur_daemon_pages++;
597
598                 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
599                         struct cfs_trace_page *victim;
600
601                         __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
602                         victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
603
604                         __LASSERT_TAGE_INVARIANT(victim);
605
606                         list_del(&victim->linkage);
607                         cfs_tage_free(victim);
608                         tcd->tcd_cur_daemon_pages--;
609                 }
610         }
611 }
612
613 static void put_pages_on_daemon_list(struct page_collection *pc)
614 {
615         struct cfs_trace_cpu_data *tcd;
616         int i, cpu;
617
618         for_each_possible_cpu(cpu) {
619                 cfs_tcd_for_each_type_lock(tcd, i, cpu)
620                         put_pages_on_tcd_daemon_list(pc, tcd);
621         }
622 }
623
624 void cfs_trace_debug_print(void)
625 {
626         struct page_collection pc;
627         struct cfs_trace_page *tage;
628         struct cfs_trace_page *tmp;
629
630         pc.pc_want_daemon_pages = 1;
631         collect_pages(&pc);
632         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
633                 char *p, *file, *fn;
634                 struct page *page;
635
636                 __LASSERT_TAGE_INVARIANT(tage);
637
638                 page = tage->page;
639                 p = page_address(page);
640                 while (p < ((char *)page_address(page) + tage->used)) {
641                         struct ptldebug_header *hdr;
642                         int len;
643                         hdr = (void *)p;
644                         p += sizeof(*hdr);
645                         file = p;
646                         p += strlen(file) + 1;
647                         fn = p;
648                         p += strlen(fn) + 1;
649                         len = hdr->ph_len - (int)(p - (char *)hdr);
650
651                         cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
652
653                         p += len;
654                 }
655
656                 list_del(&tage->linkage);
657                 cfs_tage_free(tage);
658         }
659 }
660
661 int cfs_tracefile_dump_all_pages(char *filename)
662 {
663         struct page_collection  pc;
664         struct file             *filp;
665         struct cfs_trace_page   *tage;
666         struct cfs_trace_page   *tmp;
667         mm_segment_t            __oldfs;
668         char                    *buf;
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                 buf = kmap(tage->page);
698                 rc = vfs_write(filp, (__force const char __user *)buf,
699                                tage->used, &filp->f_pos);
700                 kunmap(tage->page);
701                 if (rc != (int)tage->used) {
702                         printk(KERN_WARNING "wanted to write %u but wrote "
703                                "%d\n", tage->used, rc);
704                         put_pages_back(&pc);
705                         __LASSERT(list_empty(&pc.pc_pages));
706                         break;
707                 }
708                 list_del(&tage->linkage);
709                 cfs_tage_free(tage);
710         }
711         set_fs(__oldfs);
712         rc = ll_vfs_fsync_range(filp, 0, LLONG_MAX, 1);
713         if (rc)
714                 printk(KERN_ERR "sync returns %d\n", rc);
715 close:
716         filp_close(filp, NULL);
717 out:
718         cfs_tracefile_write_unlock();
719         return rc;
720 }
721
722 void cfs_trace_flush_pages(void)
723 {
724         struct page_collection pc;
725         struct cfs_trace_page *tage;
726         struct cfs_trace_page *tmp;
727
728         pc.pc_want_daemon_pages = 1;
729         collect_pages(&pc);
730         list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
731
732                 __LASSERT_TAGE_INVARIANT(tage);
733
734                 list_del(&tage->linkage);
735                 cfs_tage_free(tage);
736         }
737 }
738
739 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
740                             const char __user *usr_buffer, int usr_buffer_nob)
741 {
742         int    nob;
743
744         if (usr_buffer_nob > knl_buffer_nob)
745                 return -EOVERFLOW;
746
747         if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
748                 return -EFAULT;
749
750         nob = strnlen(knl_buffer, usr_buffer_nob);
751         while (nob-- >= 0)                      /* strip trailing whitespace */
752                 if (!isspace(knl_buffer[nob]))
753                         break;
754
755         if (nob < 0)                            /* empty string */
756                 return -EINVAL;
757
758         if (nob == knl_buffer_nob)              /* no space to terminate */
759                 return -EOVERFLOW;
760
761         knl_buffer[nob + 1] = 0;                /* terminate */
762         return 0;
763 }
764 EXPORT_SYMBOL(cfs_trace_copyin_string);
765
766 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
767                              const char *knl_buffer, char *append)
768 {
769         /* NB if 'append' != NULL, it's a single character to append to the
770          * copied out string - usually "\n", for /proc entries and "" (i.e. a
771          * terminating zero byte) for sysctl entries */
772         int   nob = strlen(knl_buffer);
773
774         if (nob > usr_buffer_nob)
775                 nob = usr_buffer_nob;
776
777         if (copy_to_user(usr_buffer, knl_buffer, nob))
778                 return -EFAULT;
779
780         if (append != NULL && nob < usr_buffer_nob) {
781                 if (copy_to_user(usr_buffer + nob, append, 1))
782                         return -EFAULT;
783
784                 nob++;
785         }
786
787         return nob;
788 }
789 EXPORT_SYMBOL(cfs_trace_copyout_string);
790
791 int cfs_trace_allocate_string_buffer(char **str, int nob)
792 {
793         if (nob > 2 * PAGE_SIZE)        /* string must be "sensible" */
794                 return -EINVAL;
795
796         *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
797         if (*str == NULL)
798                 return -ENOMEM;
799
800         return 0;
801 }
802
803 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
804 {
805         char         *str;
806         int           rc;
807
808         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
809         if (rc != 0)
810                 return rc;
811
812         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
813                                      usr_str, usr_str_nob);
814         if (rc != 0)
815                 goto out;
816
817         if (str[0] != '/') {
818                 rc = -EINVAL;
819                 goto out;
820         }
821         rc = cfs_tracefile_dump_all_pages(str);
822 out:
823         kfree(str);
824         return rc;
825 }
826
827 int cfs_trace_daemon_command(char *str)
828 {
829         int       rc = 0;
830
831         cfs_tracefile_write_lock();
832
833         if (strcmp(str, "stop") == 0) {
834                 cfs_tracefile_write_unlock();
835                 cfs_trace_stop_thread();
836                 cfs_tracefile_write_lock();
837                 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
838
839         } else if (strncmp(str, "size=", 5) == 0) {
840                 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
841                 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
842                         cfs_tracefile_size = CFS_TRACEFILE_SIZE;
843                 else
844                         cfs_tracefile_size <<= 20;
845
846         } else if (strlen(str) >= sizeof(cfs_tracefile)) {
847                 rc = -ENAMETOOLONG;
848         } else if (str[0] != '/') {
849                 rc = -EINVAL;
850         } else {
851                 strcpy(cfs_tracefile, str);
852
853                 printk(KERN_INFO
854                        "Lustre: debug daemon will attempt to start writing "
855                        "to %s (%lukB max)\n", cfs_tracefile,
856                        (long)(cfs_tracefile_size >> 10));
857
858                 cfs_trace_start_thread();
859         }
860
861         cfs_tracefile_write_unlock();
862         return rc;
863 }
864
865 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
866 {
867         char *str;
868         int   rc;
869
870         rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
871         if (rc != 0)
872                 return rc;
873
874         rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
875                                  usr_str, usr_str_nob);
876         if (rc == 0)
877                 rc = cfs_trace_daemon_command(str);
878
879         kfree(str);
880         return rc;
881 }
882
883 int cfs_trace_set_debug_mb(int mb)
884 {
885         int i;
886         int j;
887         int pages;
888         int limit = cfs_trace_max_debug_mb();
889         struct cfs_trace_cpu_data *tcd;
890
891         if (mb < num_possible_cpus()) {
892                 printk(KERN_WARNING
893                        "Lustre: %d MB is too small for debug buffer size, "
894                        "setting it to %d MB.\n", mb, num_possible_cpus());
895                 mb = num_possible_cpus();
896         }
897
898         if (mb > limit) {
899                 printk(KERN_WARNING
900                        "Lustre: %d MB is too large for debug buffer size, "
901                        "setting it to %d MB.\n", mb, limit);
902                 mb = limit;
903         }
904
905         mb /= num_possible_cpus();
906         pages = mb << (20 - PAGE_SHIFT);
907
908         cfs_tracefile_write_lock();
909
910         cfs_tcd_for_each(tcd, i, j)
911                 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
912
913         cfs_tracefile_write_unlock();
914
915         return 0;
916 }
917
918 int cfs_trace_set_debug_mb_usrstr(void __user *usr_str, int usr_str_nob)
919 {
920         char     str[32];
921         int      rc;
922
923         rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
924         if (rc < 0)
925                 return rc;
926
927         return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
928 }
929
930 int cfs_trace_get_debug_mb(void)
931 {
932         int i;
933         int j;
934         struct cfs_trace_cpu_data *tcd;
935         int total_pages = 0;
936
937         cfs_tracefile_read_lock();
938
939         cfs_tcd_for_each(tcd, i, j)
940                 total_pages += tcd->tcd_max_pages;
941
942         cfs_tracefile_read_unlock();
943
944         return (total_pages >> (20 - PAGE_SHIFT)) + 1;
945 }
946
947 static int tracefiled(void *arg)
948 {
949         struct page_collection pc;
950         struct tracefiled_ctl *tctl = arg;
951         struct cfs_trace_page *tage;
952         struct cfs_trace_page *tmp;
953         mm_segment_t __oldfs;
954         struct file *filp;
955         char *buf;
956         int last_loop = 0;
957         int rc;
958
959         /* we're started late enough that we pick up init's fs context */
960         /* this is so broken in uml?  what on earth is going on? */
961
962         complete(&tctl->tctl_start);
963
964         while (1) {
965                 wait_queue_t __wait;
966
967                 pc.pc_want_daemon_pages = 0;
968                 collect_pages(&pc);
969                 if (list_empty(&pc.pc_pages))
970                         goto end_loop;
971
972                 filp = NULL;
973                 cfs_tracefile_read_lock();
974                 if (cfs_tracefile[0] != 0) {
975                         filp = filp_open(cfs_tracefile,
976                                          O_CREAT | O_RDWR | O_LARGEFILE,
977                                          0600);
978                         if (IS_ERR(filp)) {
979                                 rc = PTR_ERR(filp);
980                                 filp = NULL;
981                                 printk(KERN_WARNING "couldn't open %s: "
982                                        "%d\n", cfs_tracefile, rc);
983                         }
984                 }
985                 cfs_tracefile_read_unlock();
986                 if (filp == NULL) {
987                         put_pages_on_daemon_list(&pc);
988                         __LASSERT(list_empty(&pc.pc_pages));
989                         goto end_loop;
990                 }
991                 __oldfs = get_fs();
992                 set_fs(get_ds());
993
994                 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
995                         struct dentry *de = file_dentry(filp);
996                         static loff_t f_pos;
997
998                         __LASSERT_TAGE_INVARIANT(tage);
999
1000                         if (f_pos >= (off_t)cfs_tracefile_size)
1001                                 f_pos = 0;
1002                         else if (f_pos > i_size_read(de->d_inode))
1003                                 f_pos = i_size_read(de->d_inode);
1004
1005                         buf = kmap(tage->page);
1006                         rc = vfs_write(filp, (__force const char __user *)buf,
1007                                        tage->used, &f_pos);
1008                         kunmap(tage->page);
1009                         if (rc != (int)tage->used) {
1010                                 printk(KERN_WARNING "wanted to write %u "
1011                                        "but wrote %d\n", tage->used, rc);
1012                                 put_pages_back(&pc);
1013                                 __LASSERT(list_empty(&pc.pc_pages));
1014                                 break;
1015                         }
1016                 }
1017                 set_fs(__oldfs);
1018
1019                 filp_close(filp, NULL);
1020                 put_pages_on_daemon_list(&pc);
1021                 if (!list_empty(&pc.pc_pages)) {
1022                         int i;
1023
1024                         printk(KERN_ALERT "Lustre: trace pages aren't "
1025                                " empty\n");
1026                         printk(KERN_ERR "total cpus(%d): ",
1027                                num_possible_cpus());
1028                         for (i = 0; i < num_possible_cpus(); i++)
1029                                 if (cpu_online(i))
1030                                         printk(KERN_ERR "%d(on) ", i);
1031                                 else
1032                                         printk(KERN_ERR "%d(off) ", i);
1033                         printk(KERN_ERR "\n");
1034
1035                         i = 0;
1036                         list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1037                                                      linkage)
1038                                 printk(KERN_ERR "page %d belongs to cpu "
1039                                        "%d\n", ++i, tage->cpu);
1040                         printk(KERN_ERR "There are %d pages unwritten\n",
1041                                i);
1042                 }
1043                 __LASSERT(list_empty(&pc.pc_pages));
1044 end_loop:
1045                 if (atomic_read(&tctl->tctl_shutdown)) {
1046                         if (last_loop == 0) {
1047                                 last_loop = 1;
1048                                 continue;
1049                         } else {
1050                                 break;
1051                         }
1052                 }
1053                 init_waitqueue_entry(&__wait, current);
1054                 add_wait_queue(&tctl->tctl_waitq, &__wait);
1055                 set_current_state(TASK_INTERRUPTIBLE);
1056                 schedule_timeout(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 }