Whamcloud - gitweb
Land b_smallfix onto HEAD (20040414_1359)
[fs/lustre-release.git] / lnet / libcfs / debug.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2002 Cluster File Systems, Inc.
5  *   Author: Phil Schwan <phil@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26
27 #include <linux/config.h>
28 #include <linux/module.h>
29 #include <linux/kmod.h>
30 #include <linux/notifier.h>
31 #include <linux/kernel.h>
32 #include <linux/mm.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/errno.h>
36 #include <linux/smp_lock.h>
37 #include <linux/unistd.h>
38 #include <linux/interrupt.h>
39 #include <asm/system.h>
40 #include <asm/uaccess.h>
41 #include <linux/completion.h>
42
43 #include <linux/fs.h>
44 #include <linux/stat.h>
45 #include <asm/uaccess.h>
46 #include <asm/segment.h>
47 #include <linux/miscdevice.h>
48
49 # define DEBUG_SUBSYSTEM S_PORTALS
50
51 #include <linux/kp30.h>
52 #include <linux/portals_compat25.h>
53 #include <linux/libcfs.h>
54
55 unsigned int portal_subsystem_debug = ~0 - (S_PORTALS | S_QSWNAL | S_SOCKNAL |
56                                             S_GMNAL | S_IBNAL);
57 EXPORT_SYMBOL(portal_subsystem_debug);
58
59 unsigned int portal_debug = (D_WARNING | D_DLMTRACE | D_ERROR | D_EMERG | D_HA |
60                              D_RPCTRACE | D_VFSTRACE);
61 EXPORT_SYMBOL(portal_debug);
62
63 unsigned int portal_cerror = 1;
64 EXPORT_SYMBOL(portal_cerror);
65
66 unsigned int portal_printk;
67 EXPORT_SYMBOL(portal_printk);
68
69 unsigned int portal_stack;
70 EXPORT_SYMBOL(portal_stack);
71
72 #ifdef __KERNEL__
73 atomic_t portal_kmemory = ATOMIC_INIT(0);
74 EXPORT_SYMBOL(portal_kmemory);
75 #endif
76
77 #define DEBUG_OVERFLOW 1024
78 static char *debug_buf = NULL;
79 static unsigned long debug_size = 0;
80 static atomic_t debug_off_a = ATOMIC_INIT(0);
81 static int debug_wrapped;
82 static DECLARE_WAIT_QUEUE_HEAD(debug_ctlwq);
83 #define DAEMON_SND_SIZE      (64 << 10)
84
85 /*
86  * used by the daemon to keep track the offset into debug_buffer for the next
87  * write to the file.  Usually, the daemon is to write out buffer
88  * from debug_daemon_next_write upto debug_off
89  *  variable usage
90  *      Reader - portals_debug_msg()
91  *      Writer - portals_debug_daemon()
92  *               portals_debug_daemon_start() during daemon init time
93  *               portals_debug_daemon_continue() to reset to debug_off
94  *               portals_debug_clear_buffer() reset to debug_off for clear
95  *      Note that *_start(), *_continue() & *clear_buffer() should serialized;
96  */
97 static atomic_t   debug_daemon_next_write;
98
99 /*
100  * A debug_daemon can be in following states
101  *      stopped - stopped state means there is no debug_daemon running.
102  *                accordingly, it must be in paused state
103  *                a daemon is in !stopped && !paused state after
104  *                "lctl debug_daemon start" creates debug_daemon successfully
105  *                Variable Usage
106  *                      Reader - portals_debug_daemon()
107  *                               portals_debug_set_daemon() routines
108  *                      Writer - portals_debug_set_daemon() routines
109  *                              portals_debug_daemon() on IO error
110  *      paused -  a debug_daemon state is changed from !paused into paused
111  *                when "lctl debug_daemon paused" is issued
112  *                "lctl debug_daemon continue" gets a daemon into !paused mode
113  *                      Reader - portals_debug_set_daemon() routines
114  *                               portals_debug_msg()
115  *                      Writer - portals_debug_set_daemon() on init
116  *                               portals_debug_daemon()
117  *
118  *        Daemon  state diagram.
119  *                      (stopped, paused)
120  *                              |  <-- debug_daemon start
121  *                              V
122  *                      (!stopped, !paused)
123  *                              |  <-- debug_daemon pause
124  *                              V
125  *                      (!stopped, paused)
126  *                              |  <-- debug_daemon continue
127  *                              V
128  *                      (!stopped, !paused)
129  *                              |  <-- debug_daemon stop
130  *                              V
131  *                      (stopped, paused)
132  *      Overlapped - this is a state when CDEBUG is too fast for the daemon to
133  *                   write out the debug_bufferr.  That is, debug_off is to
134  *                   overlap debug_daemon_next_write;
135  *                     Reader - portals_debug_msg()
136  *                     Writer - portals_debug_msg()
137  */
138
139 /*
140  * Description on Trace Daemon Synchronization
141  *
142  * Three categories of code are synchronizing between each other
143  * 1.   lctl, portals_debug_set_daemon(), the user debug control code, 
144  *      as well as portals_debug_clear_buffer()
145  * 2.   CDEBUG, portals_debug_msg(), the debug put messages routine
146  * 3.   Daemon, portals_debug_daemon(), to write out debug log file
147  *
148  *
149  * Three different controls for synchronizations
150  *
151  * 1.   debug_daemon_semaphore
152  *      The usage of this semaphore is to serialize multiple lctl controls 
153  *      in manipulating debug daemon state.  The semaphore serves as the 
154  *      gatekeeper to allow only one user control thread, at any giving time, 
155  *      to access debug daemon state and keeps the other user control requests 
156  *      in wait state until the current control request is serviced.
157  *
158  * 2.   wait_queue_head_t lctl (paired with lctl_event flag)
159  *      Lctl event is the event between portals_debug_set_daemon() and 
160  *      portals_debug_daemon().  Lctl is an indicator for portals_debug_daemon()
161  *      to flush data out to file.  portals_debug_daemon() is to use lctl event
162  *      as signal channel to wakeup portals_debug_set_daemon() upon flush 
163  *      operation is done.
164  *
165  *      Producer :
166  *              portals_debug_daemon() uses to wake up 
167  *              portals_debug_set_daemon(), pause and stop, routines
168  *      Consumer :
169  *              portals_debug_set_daemon(), stop and pause operations, 
170  *              wait and sleep on the event
171  *
172  * 3.   wait_queue_head_t daemon (paired with daemon_event flag)
173  *      This is an event channel to wakeup portals_debug_daemon.  Daemon 
174  *      wakes up to run whenever there is an event posted.   Daemon handles 
175  *      2 types of operations . 1. Writes data out to debug file, 2. Flushes 
176  *      file and terminates base on lctl event. 
177  *      File operation -
178  *              Daemon is normally in a sleep state.  
179  *              Daemon is woken up through daemon event whenever CDEBUG is 
180  *              putting data over any 64K boundary. 
181  *      File flush and termination -
182  *              On portals_debug_daemon_stop/pause() operations, lctl control 
183  *              is to wake up daemon through daemon event.
184  *
185  *      We can't use sleep_on() and wake_up() to replace daemon event because 
186  *      portals_debug_daemon() must catch the wakeup operation posted by 
187  *      portals_debug_daemon_stop/pause().  Otherwise, stop and pause may 
188  *      stuck in lctl wait event.
189  *
190  *      Producer :
191  *           a. portals_debug_daemon_pause() and portals_debug_daemon_stop() 
192  *              uses the event to wake up portals_debug_daemon()
193  *           b. portals_debug_msg() uses the event to wake up 
194  *              portals_debug_daemon() whenever the data output is acrossing 
195  *              a 64K bytes boundary.
196  *      Consumer :
197  *              portals_debug_daemon() wakes up upon daemon event.
198  *
199  * Sequence for portals_debug_daemon_stop() operation
200  *
201  * _Portals_debug_daemon_stop()_          _Daemon_
202  *                                      Wait_event(daemon) or running
203  *      Paused = 1;
204  *      Wakeup_event (daemon)
205  *      Wait_event(lctl)
206  *                                      Set force_flush flag if lctlevnt
207  *                                      Flush data
208  *                                      Wakeup_event (lctl)
209  *                                      Wait_event(daemon)
210  *      Stopped = 1;
211  *      Wakeup_event (daemon)
212  *      Wait_event(lctl)
213  *                                      Exit daemon loop if (Stopped)
214  *                                      Wakeup_event (lctl)
215  *                                      Exit
216  *      Return to user application
217  *
218  *
219  * _Portals_debug_msg()_                  _Daemon_
220  *                                      Wait_event(daemon) or running
221  *      If (WriteStart<64K<WriteEnd)
222  *         Wakeup_event(daemon)
223  *                                      Do file IO
224  *                                      Wait_event(daemon)
225  */
226 struct debug_daemon_state {
227         unsigned long overlapped;
228         unsigned long stopped;
229         atomic_t paused;
230         unsigned long   lctl_event;     /* event for lctl */
231         wait_queue_head_t lctl;
232         unsigned long   daemon_event;   /* event for daemon */
233         wait_queue_head_t daemon;
234 };
235 static struct debug_daemon_state debug_daemon_state;
236 static DECLARE_MUTEX(debug_daemon_semaphore);
237
238 static loff_t daemon_file_size_limit;
239 char debug_daemon_file_path[1024] = "";
240
241 spinlock_t portals_debug_lock = SPIN_LOCK_UNLOCKED;
242 char debug_file_path[1024] = "/tmp/lustre-log";
243 char debug_file_name[1024];
244 int handled_panic; /* to avoid recursive calls to notifiers */
245 char portals_upcall[1024] = "/usr/lib/lustre/portals_upcall";
246
247
248 int portals_do_debug_dumplog(void *arg)
249 {
250         struct file *file;
251         void *journal_info;
252         int rc;
253         mm_segment_t oldfs;
254         unsigned long debug_off;
255
256         kportal_daemonize("");
257
258         reparent_to_init();
259         journal_info = current->journal_info;
260         current->journal_info = NULL;
261         sprintf(debug_file_name, "%s.%ld", debug_file_path, CURRENT_SECONDS);
262         file = filp_open(debug_file_name, O_CREAT|O_EXCL|O_RDWR, 0644);
263
264         if (!file || IS_ERR(file)) {
265                 CERROR("cannot open %s for dumping: %ld\n", debug_file_name,
266                        PTR_ERR(file));
267                 GOTO(out, PTR_ERR(file));
268         } else {
269                 printk(KERN_ALERT "LustreError: dumping log to %s ...\n",
270                        debug_file_name);
271         }
272
273         debug_off = atomic_read(&debug_off_a);
274         oldfs = get_fs();
275         set_fs(get_ds());
276         if (debug_wrapped) {
277                 rc = file->f_op->write(file, debug_buf + debug_off + 1,
278                                        debug_size-debug_off-1, &file->f_pos);
279                 rc += file->f_op->write(file, debug_buf, debug_off + 1,
280                                         &file->f_pos);
281         } else {
282                 rc = file->f_op->write(file, debug_buf, debug_off,&file->f_pos);
283         }
284         printk("LustreError: wrote %d bytes\n", rc);
285         set_fs(oldfs);
286
287         rc = file->f_op->fsync(file, file->f_dentry, 1);
288         if (rc)
289                 CERROR("sync returns %d\n", rc);
290         filp_close(file, 0);
291 out:
292         current->journal_info = journal_info;
293         wake_up(&debug_ctlwq);
294         return 0;
295 }
296
297 int portals_debug_daemon(void *arg)
298 {
299         struct file *file;
300         void *journal_info;
301         mm_segment_t oldfs;
302         unsigned long force_flush = 0;
303         unsigned long size, off, flags;
304         int rc;
305
306         kportal_daemonize("ldebug_daemon");
307         reparent_to_init();
308         journal_info = current->journal_info;
309         current->journal_info = NULL;
310
311         file = filp_open(debug_daemon_file_path,
312                          O_CREAT|O_TRUNC|O_RDWR|O_LARGEFILE, 0644);
313
314         if (!file || IS_ERR(file)) {
315                 CERROR("cannot open %s for logging", debug_daemon_file_path);
316                 GOTO(out1, PTR_ERR(file));
317         }
318         printk(KERN_INFO "daemon dumping log to %s\n", debug_daemon_file_path);
319
320         debug_daemon_state.overlapped = 0;
321         debug_daemon_state.stopped = 0;
322
323         spin_lock_irqsave(&portals_debug_lock, flags);
324         off = atomic_read(&debug_off_a) + 1;
325         if (debug_wrapped)
326                 off = (off >= debug_size)? 0 : off;
327         else
328                 off = 0;
329         atomic_set(&debug_daemon_next_write, off);
330         atomic_set(&debug_daemon_state.paused, 0);
331         spin_unlock_irqrestore(&portals_debug_lock, flags);
332
333         oldfs = get_fs();
334         set_fs(KERNEL_DS);
335         while (1) {
336                 unsigned long ending;
337                 unsigned long start, tail;
338                 long delta;
339
340                 debug_daemon_state.daemon_event = 0;
341
342                 ending = atomic_read(&debug_off_a);
343                 start = atomic_read(&debug_daemon_next_write);
344
345                 /* check if paused is imposed by lctl ? */
346                 force_flush = !debug_daemon_state.lctl_event;
347
348                 delta = ending - start;
349                 tail = debug_size - start;
350                 size = (delta >= 0) ? delta : tail;
351                 while (size && (force_flush || (delta < 0) ||
352                                 (size >= DAEMON_SND_SIZE))) {
353                         if (daemon_file_size_limit) {
354                                int ssize = daemon_file_size_limit - file->f_pos;
355                                if (size > ssize)
356                                         size = ssize;
357                         }
358
359                         rc = file->f_op->write(file, debug_buf+start,
360                                                size, &file->f_pos);
361                         if (rc < 0) {
362                                 printk(KERN_ALERT "LustreError: Debug_daemon "
363                                        "write error %d\n", rc);
364                                 goto out;
365                         }
366                         start += rc;
367                         delta = ending - start;
368                         tail = debug_size - start;
369                         if (tail == 0)
370                                 start = 0;
371                         if (delta >= 0)
372                                 size = delta;
373                         else
374                                 size = (tail == 0) ? ending : tail;
375                         if (daemon_file_size_limit == file->f_pos) {
376                                 // file wrapped around
377                                 file->f_pos = 0;
378                         }
379                 }
380                 atomic_set(&debug_daemon_next_write, start);
381                 if (force_flush) {
382                         rc = file->f_op->fsync(file, file->f_dentry, 1);
383                         if (rc < 0) {
384                                 printk(KERN_ALERT "LustreError: Debug_daemon "
385                                        "sync error %d\n", rc);
386                                 goto out;
387                         }
388                         if (debug_daemon_state.stopped)
389                                break;
390                         debug_daemon_state.lctl_event = 1;
391                         wake_up(&debug_daemon_state.lctl);
392                 }
393                 wait_event(debug_daemon_state.daemon,
394                            debug_daemon_state.daemon_event);
395                 }
396 out:
397         atomic_set(&debug_daemon_state.paused, 1);
398         debug_daemon_state.stopped = 1;
399         set_fs(oldfs);
400         filp_close(file, 0);
401         current->journal_info = journal_info;
402 out1:
403         debug_daemon_state.lctl_event = 1;
404         wake_up(&debug_daemon_state.lctl);
405         return 0;
406 }
407
408 void portals_debug_print(void)
409 {
410         unsigned long dumplen = 64 * 1024;
411         char *start1, *start2;
412         char *end1, *end2;
413         unsigned long debug_off = atomic_read(&debug_off_a);
414
415         start1 = debug_buf + debug_off - dumplen;
416         if (start1 < debug_buf) {
417                 start1 += debug_size;
418                 end1 = debug_buf + debug_size - 1;
419                 start2 = debug_buf;
420                 end2 = debug_buf + debug_off;
421         } else {
422                 end1 = debug_buf + debug_off;
423                 start2 = debug_buf + debug_off;
424                 end2 = debug_buf + debug_off;
425         }
426
427         while (start1 < end1) {
428                 int count = MIN(1024, end1 - start1);
429                 printk("LustreError: %*s", count, start1);
430                 start1 += 1024;
431         }
432         while (start2 < end2) {
433                 int count = MIN(1024, end2 - start2);
434                 printk("LustreError: %*s", count, start2);
435                 start2 += 1024;
436         }
437 }
438
439 void portals_debug_dumplog(void)
440 {
441         int rc;
442         DECLARE_WAITQUEUE(wait, current);
443         ENTRY;
444
445         /* we're being careful to ensure that the kernel thread is
446          * able to set our state to running as it exits before we
447          * get to schedule() */
448         set_current_state(TASK_INTERRUPTIBLE);
449         add_wait_queue(&debug_ctlwq, &wait);
450
451         rc = kernel_thread(portals_do_debug_dumplog,
452                            NULL, CLONE_VM | CLONE_FS | CLONE_FILES);
453         if (rc < 0)
454                 printk(KERN_ERR "LustreError: cannot start log dump thread: "
455                        "%d\n", rc);
456         else
457                 schedule();
458
459         /* be sure to teardown if kernel_thread() failed */
460         remove_wait_queue(&debug_ctlwq, &wait);
461         set_current_state(TASK_RUNNING);
462 }
463
464 int portals_debug_daemon_start(char *file, unsigned int size)
465 {
466         int rc;
467
468         if (!debug_daemon_state.stopped)
469                 return -EALREADY;
470
471         if (file != NULL)
472                 strncpy(debug_daemon_file_path, file, 1024);
473
474         init_waitqueue_head(&debug_daemon_state.lctl);
475         init_waitqueue_head(&debug_daemon_state.daemon);
476
477         daemon_file_size_limit = size << 20;
478
479         debug_daemon_state.lctl_event = 0;
480         rc = kernel_thread(portals_debug_daemon, NULL, 0);
481         if (rc < 0) {
482                 printk(KERN_ERR "LustreError: cannot start debug daemon thread\n");
483                 strncpy(debug_daemon_file_path, "\0", 1);
484                 return rc;
485         }
486         wait_event(debug_daemon_state.lctl, debug_daemon_state.lctl_event);
487         return 0;
488 }
489
490 int portals_debug_daemon_pause(void)
491 {
492         if (atomic_read(&debug_daemon_state.paused))
493                 return -EALREADY;
494
495         atomic_set(&debug_daemon_state.paused, 1);
496         debug_daemon_state.lctl_event = 0;
497         debug_daemon_state.daemon_event = 1;
498         wake_up(&debug_daemon_state.daemon);
499         wait_event(debug_daemon_state.lctl, debug_daemon_state.lctl_event);
500         return 0;
501 }
502
503 int portals_debug_daemon_continue(void)
504 {
505         if (!atomic_read(&debug_daemon_state.paused))
506                 return -EINVAL;
507         if (debug_daemon_state.stopped)
508                 return -EINVAL;
509
510         debug_daemon_state.overlapped = 0;
511         atomic_set(&debug_daemon_next_write, atomic_read(&debug_off_a));
512         atomic_set(&debug_daemon_state.paused, 0);
513         return 0;
514 }
515
516 int portals_debug_daemon_stop(void)
517 {
518         if (debug_daemon_state.stopped)
519                 return -EALREADY;
520
521         if (!atomic_read(&debug_daemon_state.paused))
522                 portals_debug_daemon_pause();
523
524         debug_daemon_state.lctl_event = 0;
525         debug_daemon_state.stopped = 1;
526
527         debug_daemon_state.daemon_event = 1;
528         wake_up(&debug_daemon_state.daemon);
529         wait_event(debug_daemon_state.lctl, debug_daemon_state.lctl_event);
530
531         debug_daemon_file_path[0] = '\0';
532         return 0;
533 }
534
535 int portals_debug_set_daemon(unsigned int cmd, unsigned int length,
536                              char *filename, unsigned int size)
537 {
538         int rc = -EINVAL;
539
540         down(&debug_daemon_semaphore);
541         switch (cmd) {
542                 case DEBUG_DAEMON_START:
543                         if (length && (filename[length -1] != '\0')) {
544                                 CERROR("Invalid filename for debug_daemon\n");
545                                 rc = -EINVAL;
546                                 break;
547                         }
548                         rc = portals_debug_daemon_start(filename, size);
549                         break;
550                 case DEBUG_DAEMON_STOP:
551                         rc = portals_debug_daemon_stop();
552                         break;
553                 case DEBUG_DAEMON_PAUSE:
554                         rc = portals_debug_daemon_pause();
555                         break;
556                 case DEBUG_DAEMON_CONTINUE:
557                         rc = portals_debug_daemon_continue();
558                         break;
559                 default:
560                         CERROR("unknown set_daemon cmd\n");
561         }
562         up(&debug_daemon_semaphore);
563         return rc;
564 }
565
566 static int panic_dumplog(struct notifier_block *self, unsigned long unused1,
567                          void *unused2)
568 {
569         if (handled_panic)
570                 return 0;
571         else
572                 handled_panic = 1;
573
574         if (in_interrupt()) {
575                 portals_debug_print();
576                 return 0;
577         }
578
579         while (current->lock_depth >= 0)
580                 unlock_kernel();
581         portals_debug_dumplog();
582         return 0;
583 }
584
585 static struct notifier_block lustre_panic_notifier = {
586         notifier_call :     panic_dumplog,
587         next :              NULL,
588         priority :          10000
589 };
590
591 int portals_debug_init(unsigned long bufsize)
592 {
593         unsigned long debug_off = atomic_read(&debug_off_a);
594         if (debug_buf != NULL)
595                 return -EALREADY;
596
597         atomic_set(&debug_daemon_state.paused, 1);
598         debug_daemon_state.stopped = 1;
599
600         debug_buf = vmalloc(bufsize + DEBUG_OVERFLOW);
601         if (debug_buf == NULL)
602                 return -ENOMEM;
603         memset(debug_buf, 0, bufsize + DEBUG_OVERFLOW);
604         debug_wrapped = 0;
605
606         //printk(KERN_INFO "Portals: allocated %lu byte debug buffer at %p.\n",
607                //bufsize, debug_buf);
608         atomic_set(&debug_off_a, debug_off);
609         notifier_chain_register(&panic_notifier_list, &lustre_panic_notifier);
610         debug_size = bufsize;
611
612         return 0;
613 }
614
615 int portals_debug_cleanup(void)
616 {
617         notifier_chain_unregister(&panic_notifier_list, &lustre_panic_notifier);
618         if (debug_buf == NULL)
619                 return -EINVAL;
620
621         down(&debug_daemon_semaphore);
622         portals_debug_daemon_stop();
623
624         vfree(debug_buf);
625         atomic_set(&debug_off_a, 0);
626         up(&debug_daemon_semaphore);
627
628         return 0;
629 }
630
631 int portals_debug_clear_buffer(void)
632 {
633         unsigned long flags;
634         unsigned long state;
635
636         if (debug_buf == NULL)
637                 return -EINVAL;
638
639         down(&debug_daemon_semaphore);
640         state = atomic_read(&debug_daemon_state.paused);
641         if (!state)
642                 portals_debug_daemon_pause();
643         spin_lock_irqsave(&portals_debug_lock, flags);
644         atomic_set(&debug_off_a, 0);
645         debug_wrapped = 0;
646         atomic_set(&debug_daemon_next_write, 0);
647         debug_daemon_state.overlapped = 0;
648         spin_unlock_irqrestore(&portals_debug_lock, flags);
649
650         if (!state)
651                 atomic_set(&debug_daemon_state.paused, 0);
652         up(&debug_daemon_semaphore);
653
654         return 0;
655 }
656
657 /* Debug markers, although printed by S_PORTALS
658  * should not be be marked as such.
659  */
660 #undef DEBUG_SUBSYSTEM
661 #define DEBUG_SUBSYSTEM S_UNDEFINED
662 int portals_debug_mark_buffer(char *text)
663 {
664         if (debug_buf == NULL)
665                 return -EINVAL;
666
667         CDEBUG(D_TRACE,"***************************************************\n");
668         CWARN("DEBUG MARKER: %s\n", text);
669         CDEBUG(D_TRACE,"***************************************************\n");
670
671         return 0;
672 }
673 #undef DEBUG_SUBSYSTEM
674 #define DEBUG_SUBSYSTEM S_PORTALS
675
676 /* this copies a snapshot of the debug buffer into an array of pages
677  * before doing the potentially blocking copy into userspace. it could
678  * be warning userspace if things wrap heavily while its off copying. */
679 __s32 portals_debug_copy_to_user(char *buf, unsigned long len)
680 {
681         int rc;
682         unsigned long total, debug_off, i, off, copied;
683         unsigned long flags;
684         struct page *page;
685         LIST_HEAD(my_pages);
686         struct list_head *pos, *n;
687
688         if (len < debug_size)
689                 return -ENOSPC;
690
691         for (i = 0 ; i < debug_size; i += PAGE_SIZE) {
692                 page = alloc_page(GFP_NOFS);
693                 if (page == NULL) {
694                         rc = -ENOMEM;
695                         goto cleanup;
696                 }
697                 list_add(&PAGE_LIST(page), &my_pages);
698         }
699
700         spin_lock_irqsave(&portals_debug_lock, flags);
701         debug_off = atomic_read(&debug_off_a);
702
703         /* Sigh. If the buffer is empty, then skip to the end. */
704         if (debug_off == 0 && !debug_wrapped) {
705                 spin_unlock_irqrestore(&portals_debug_lock, flags);
706                 rc = 0;
707                 goto cleanup;
708         }
709
710         if (debug_wrapped) {
711                 off = debug_off + 1;
712                 total = debug_size;
713         } else {
714                 off = 0;
715                 total = debug_off;
716         }
717         copied = 0;
718         list_for_each(pos, &my_pages) {
719                 unsigned long to_copy;
720                 void *addr;
721
722                 page = list_entry(pos, struct page, PAGE_LIST_ENTRY);
723                 to_copy = min(total - off, PAGE_SIZE);
724                 if (to_copy == 0) {
725                         off = 0;
726                         to_copy = min(debug_size - off, PAGE_SIZE);
727                 }
728 finish_partial:
729                 addr = kmap_atomic(page, KM_USER0);
730                 memcpy(addr, debug_buf + off, to_copy);
731                 kunmap_atomic(addr, KM_USER0);
732                 copied += to_copy;
733                 if (copied >= total)
734                         break;
735
736                 off += to_copy;
737                 if (off >= debug_size) {
738                         off = 0;
739                         if (to_copy != PAGE_SIZE) {
740                                 to_copy = PAGE_SIZE - to_copy;
741                                 goto finish_partial;
742                         }
743                 }
744         }
745
746         spin_unlock_irqrestore(&portals_debug_lock, flags);
747
748         off = 0;
749         list_for_each(pos, &my_pages) {
750                 unsigned long to_copy;
751                 page = list_entry(pos, struct page, PAGE_LIST_ENTRY);
752
753                 to_copy = min(copied - off, PAGE_SIZE);
754                 rc = copy_to_user(buf + off, kmap(page), to_copy);
755                 kunmap(page);
756                 if (rc) {
757                         rc = -EFAULT;
758                         goto cleanup;
759                 }
760                 off += to_copy;
761                 if (off >= copied)
762                         break;
763         }
764         rc = copied;
765
766 cleanup:
767         list_for_each_safe(pos, n, &my_pages) {
768                 page = list_entry(pos, struct page, PAGE_LIST_ENTRY);
769                 list_del(&PAGE_LIST(page));
770                 __free_page(page);
771         }
772         return rc;
773 }
774
775 /* FIXME: I'm not very smart; someone smarter should make this better. */
776 void
777 portals_debug_msg(int subsys, int mask, char *file, const char *fn,
778                   const int line, unsigned long stack, char *format, ...)
779 {
780         va_list       ap;
781         unsigned long flags;
782         int           max_nob;
783         int           prefix_nob;
784         int           msg_nob;
785         struct timeval tv;
786         unsigned long base_offset;
787         unsigned long debug_off;
788
789         if (debug_buf == NULL) {
790                 printk("LustreError: portals_debug_msg: debug_buf is NULL!\n");
791                 return;
792         }
793
794         spin_lock_irqsave(&portals_debug_lock, flags);
795         debug_off = atomic_read(&debug_off_a);
796         if (!atomic_read(&debug_daemon_state.paused)) {
797                 unsigned long available;
798                 long delta;
799                 long v = atomic_read(&debug_daemon_next_write);
800
801                 delta = debug_off - v;
802                 available = (delta>=0) ? debug_size-delta : -delta;
803                 // Check if we still have enough debug buffer for CDEBUG
804                 if (available < DAEMON_SND_SIZE) {
805                         /* Drop CDEBUG packets until enough debug_buffer is
806                          * available */
807                         if (debug_daemon_state.overlapped)
808                                  goto out;
809                         /* If this is the first time, leave a marker in the
810                          * output */
811                         debug_daemon_state.overlapped = 1;
812                         format = "DEBUG MARKER: Debug buffer overlapped\n";
813                         printk(KERN_ERR "LustreError: debug daemon buffer "
814                                "overlapped\n");
815                 } else  /* More space just became available */
816                         debug_daemon_state.overlapped = 0;
817         }
818
819         max_nob = debug_size - debug_off + DEBUG_OVERFLOW;
820         if (max_nob <= 0) {
821                 spin_unlock_irqrestore(&portals_debug_lock, flags);
822                 printk("LustreError: logic error in portals_debug_msg: "
823                        "< 0 bytes to write\n");
824                 return;
825         }
826
827         /* NB since we pass a non-zero sized buffer (at least) on the first
828          * print, we can be assured that by the end of all the snprinting,
829          * we _do_ have a terminated buffer, even if our message got truncated.
830          */
831
832         do_gettimeofday(&tv);
833
834         prefix_nob = snprintf(debug_buf + debug_off, max_nob,
835                               "%06x:%06x:%d:%lu.%06lu:%lu:%d:",
836                               subsys, mask, smp_processor_id(),
837                               tv.tv_sec, tv.tv_usec, stack, current->pid);
838         max_nob -= prefix_nob;
839
840         if(*(format + strlen(format) - 1) != '\n')
841                 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
842                        file, line, fn);
843
844 #if defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,4,20))
845         msg_nob = snprintf(debug_buf + debug_off + prefix_nob, max_nob,
846                            "%d:(%s:%d:%s()) ",
847                            current->thread.extern_pid, file, line, fn);
848 #elif defined(__arch_um__) && (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
849         msg_nob = snprintf(debug_buf + debug_off + prefix_nob, max_nob,
850                            "%d:(%s:%d:%s()) ",
851                            current->thread.mode.tt.extern_pid, file, line, fn);
852 #else
853         msg_nob = snprintf(debug_buf + debug_off + prefix_nob, max_nob,
854                            "%d:(%s:%d:%s()) ",
855                            current->pid, file, line, fn);
856 #endif
857
858         va_start(ap, format);
859         msg_nob += vsnprintf(debug_buf + debug_off + prefix_nob + msg_nob,
860                              max_nob, format, ap);
861         max_nob -= msg_nob;
862         va_end(ap);
863
864         /* Print to console, while msg is contiguous in debug_buf */
865         /* NB safely terminated see above */
866         if ((mask & D_EMERG) != 0)
867                 printk(KERN_EMERG "LustreError: %s",
868                        debug_buf + debug_off + prefix_nob);
869         else if ((mask & D_ERROR) != 0)
870                 printk(KERN_ERR "LustreError: %s",
871                        debug_buf + debug_off + prefix_nob);
872         else if ((mask & D_WARNING) != 0)
873                 printk(KERN_WARNING "Lustre: %s",
874                        debug_buf + debug_off + prefix_nob);
875         else if (portal_printk)
876                 printk("<%d>Lustre: %s", portal_printk,
877                        debug_buf+debug_off+prefix_nob);
878         base_offset = debug_off & 0xFFFF;
879
880         debug_off += prefix_nob + msg_nob;
881         if (debug_off > debug_size) {
882                 memcpy(debug_buf, debug_buf + debug_size,
883                        debug_off - debug_size + 1);
884                 debug_off -= debug_size;
885                 debug_wrapped = 1;
886         }
887
888         atomic_set(&debug_off_a, debug_off);
889         if (!atomic_read(&debug_daemon_state.paused) &&
890             ((base_offset+prefix_nob+msg_nob) >= DAEMON_SND_SIZE)) {
891                 debug_daemon_state.daemon_event = 1;
892                 wake_up(&debug_daemon_state.daemon);
893         }
894 out:
895         spin_unlock_irqrestore(&portals_debug_lock, flags);
896 }
897
898 void portals_debug_set_level(unsigned int debug_level)
899 {
900         printk("Lustre: Setting portals debug level to %08x\n", debug_level);
901         portal_debug = debug_level;
902 }
903
904 void portals_run_upcall(char **argv)
905 {
906         int   rc;
907         int   argc;
908         char *envp[] = {
909                 "HOME=/",
910                 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
911                 NULL};
912         ENTRY;
913
914         argv[0] = portals_upcall;
915         argc = 1;
916         while (argv[argc] != NULL)
917                 argc++;
918
919         LASSERT(argc >= 2);
920
921         rc = USERMODEHELPER(argv[0], argv, envp);
922         if (rc < 0) {
923                 CERROR("Error %d invoking portals upcall %s %s%s%s%s%s%s%s%s; "
924                        "check /proc/sys/portals/upcall\n",
925                        rc, argv[0], argv[1],
926                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
927                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
928                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
929                        argc < 6 ? "" : ",...");
930         } else {
931                 CERROR("Invoked portals upcall %s %s%s%s%s%s%s%s%s\n",
932                        argv[0], argv[1],
933                        argc < 3 ? "" : ",", argc < 3 ? "" : argv[2],
934                        argc < 4 ? "" : ",", argc < 4 ? "" : argv[3],
935                        argc < 5 ? "" : ",", argc < 5 ? "" : argv[4],
936                        argc < 6 ? "" : ",...");
937         }
938 }
939
940 void portals_run_lbug_upcall(char *file, const char *fn, const int line)
941 {
942         char *argv[6];
943         char buf[32];
944
945         ENTRY;
946         snprintf (buf, sizeof buf, "%d", line);
947
948         argv[1] = "LBUG";
949         argv[2] = file;
950         argv[3] = (char *)fn;
951         argv[4] = buf;
952         argv[5] = NULL;
953
954         portals_run_upcall (argv);
955 }
956
957 char *portals_nid2str(int nal, ptl_nid_t nid, char *str)
958 {
959         switch(nal){
960 /* XXX this could be a nal method of some sort, 'cept it's config
961  * dependent whether (say) socknal NIDs are actually IP addresses... */
962 #ifndef CRAY_PORTALS 
963         case TCPNAL:
964                 /* userspace NAL */
965         case SOCKNAL:
966                 snprintf(str, PTL_NALFMT_SIZE-1,
967                          "%u:%d.%d.%d.%d", (__u32)(nid >> 32), HIPQUAD(nid));
968                 break;
969         case QSWNAL:
970         case GMNAL:
971         case IBNAL:
972         case SCIMACNAL:
973                 snprintf(str, PTL_NALFMT_SIZE-1, LPD64, nid);
974                 break;
975 #endif
976         default:
977                 snprintf(str, PTL_NALFMT_SIZE-1, "(?%llx)", (long long)nid);
978         }
979         return str;
980 }
981
982 #ifdef __KERNEL__
983 char stack_backtrace[LUSTRE_TRACE_SIZE];
984 spinlock_t stack_backtrace_lock = SPIN_LOCK_UNLOCKED;
985
986 #if defined(__arch_um__)
987
988 extern int is_kernel_text_address(unsigned long addr);
989
990 char *portals_debug_dumpstack(void)
991 {
992         asm("int $3");
993         return "dump stack\n";
994 }
995
996 #elif defined(__i386__)
997
998 extern int is_kernel_text_address(unsigned long addr);
999 extern int lookup_symbol(unsigned long address, char *buf, int buflen);
1000
1001 char *portals_debug_dumpstack(void)
1002 {
1003         unsigned long esp = current->thread.esp;
1004         unsigned long *stack = (unsigned long *)&esp;
1005         int size;
1006         unsigned long addr;
1007         char *buf = stack_backtrace;
1008         char *pbuf = buf;
1009         static char buffer[512];
1010         int rc = 0;
1011
1012         /* User space on another CPU? */
1013         if ((esp ^ (unsigned long)current) & (PAGE_MASK<<1)){
1014                 buf[0] = '\0';
1015                 goto out;
1016         }
1017
1018         size = sprintf(pbuf, " Call Trace: ");
1019         pbuf += size;
1020         while (((long) stack & (THREAD_SIZE-1)) != 0) {
1021                 addr = *stack++;
1022                 if (is_kernel_text_address(addr)) {
1023                         rc = lookup_symbol(addr, buffer, 512);
1024                         if (rc == -ENOSYS) {
1025                                 if (buf + LUSTRE_TRACE_SIZE <= pbuf + 12)
1026                                         break;
1027                                 size = sprintf(pbuf, "[<%08lx>] ", addr);
1028                         } else {
1029                                 if (buf + LUSTRE_TRACE_SIZE
1030                                             /* fix length + sizeof('\0') */
1031                                     <= pbuf + strlen(buffer) + 28 + 1)
1032                                         break;
1033                                 size = sprintf(pbuf, "([<%08lx>] %s (0x%p)) ",
1034                                                addr, buffer, stack-1);
1035                         }
1036                         pbuf += size;
1037                 }
1038         }
1039 out:
1040         return buf;
1041 }
1042
1043 #else /* !__arch_um__ && !__i386__ */
1044
1045 char *portals_debug_dumpstack(void)
1046 {
1047         char *buf = stack_backtrace;
1048         buf[0] = '\0';
1049         return buf;
1050 }
1051
1052 #endif /* __arch_um__ */
1053 EXPORT_SYMBOL(stack_backtrace_lock);
1054 EXPORT_SYMBOL(portals_debug_dumpstack);
1055 #endif /* __KERNEL__ */
1056
1057 EXPORT_SYMBOL(portals_debug_dumplog);
1058 EXPORT_SYMBOL(portals_debug_msg);
1059 EXPORT_SYMBOL(portals_debug_set_level);
1060 EXPORT_SYMBOL(portals_run_upcall);
1061 EXPORT_SYMBOL(portals_run_lbug_upcall);
1062 EXPORT_SYMBOL(portals_nid2str);