This commit was generated by cvs2svn to compensate for changes in r925,
[linux-2.6.git] / kernel / dump.c
1 /*
2  *  linux/kernel/dump.c
3  *
4  *  Copyright (C) 2004  FUJITSU LIMITED
5  *  Written by Nobuhiro Tachino (ntachino@jp.fujitsu.com)
6  *
7  */
8 /*
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/delay.h>
27 #include <linux/nmi.h>
28 #include <linux/timer.h>
29 #include <linux/interrupt.h>
30 #include <linux/workqueue.h>
31 #include <linux/genhd.h>
32 #include <linux/diskdump.h>
33 #include <asm/diskdump.h>
34
35 static DECLARE_MUTEX(dump_ops_mutex);
36 struct disk_dump_ops* dump_ops = NULL;
37
38 int diskdump_mode = 0;
39 EXPORT_SYMBOL_GPL(diskdump_mode);
40
41 void (*diskdump_func) (struct pt_regs *regs) = NULL;
42 EXPORT_SYMBOL_GPL(diskdump_func);
43
44 static unsigned long long timestamp_base;
45 static unsigned long timestamp_hz;
46
47
48 /*
49  * register/unregister diskdump operations
50  */
51 int diskdump_register_ops(struct disk_dump_ops* op)
52 {
53         down(&dump_ops_mutex);
54         if (dump_ops) {
55                 up(&dump_ops_mutex);
56                 return -EEXIST;
57         }
58         dump_ops = op;
59         up(&dump_ops_mutex);
60
61         return 0;
62 }
63
64 EXPORT_SYMBOL_GPL(diskdump_register_ops);
65
66 void diskdump_unregister_ops(void)
67 {
68         down(&dump_ops_mutex);
69         dump_ops = NULL;
70         up(&dump_ops_mutex);
71 }
72
73 EXPORT_SYMBOL_GPL(diskdump_unregister_ops);
74
75
76 /*
77  * sysfs interface
78  */
79 static struct gendisk *device_to_gendisk(struct device *dev)
80 {
81         struct dentry *d;
82         struct qstr qstr;
83
84         /* trace symlink to "block" */
85         qstr.name = "block";
86         qstr.len = strlen(qstr.name);
87         qstr.hash = full_name_hash(qstr.name, qstr.len);
88         d = d_lookup(dev->kobj.dentry, &qstr);
89         if (!d || !d->d_fsdata)
90                 return NULL;
91         else
92                 return container_of(d->d_fsdata, struct gendisk, kobj);
93 }
94
95 ssize_t diskdump_sysfs_store(struct device *dev, const char *buf, size_t count)
96 {
97         struct gendisk *disk;
98         struct block_device *bdev;
99         int part, remove = 0;
100
101         if (!dump_ops || !dump_ops->add_dump || !dump_ops->remove_dump)
102                 return count;
103
104         /* get partition number */
105         sscanf (buf, "%d\n", &part);
106         if (part < 0) {
107                 part = -part;
108                 remove = 1;
109         }
110
111         /* get block device */
112         if (!(disk = device_to_gendisk(dev)) ||
113             !(bdev = bdget_disk(disk, part)))
114                 return count;
115
116         /* add/remove device */
117         down(&dump_ops_mutex);
118         if (!remove)
119                 dump_ops->add_dump(dev, bdev);
120         else
121                 dump_ops->remove_dump(bdev);
122         up(&dump_ops_mutex);
123
124         return count;
125 }
126
127 EXPORT_SYMBOL_GPL(diskdump_sysfs_store);
128
129 ssize_t diskdump_sysfs_show(struct device *dev, char *buf)
130 {
131         struct gendisk *disk;
132         struct block_device *bdev;
133         int part, tmp, len = 0, maxlen = 1024;
134         char* p = buf; 
135         char name[BDEVNAME_SIZE];
136
137         if (!dump_ops || !dump_ops->find_dump)
138                 return 0;
139
140         /* get gendisk */
141         disk = device_to_gendisk(dev);
142         if (!disk || !disk->part)
143                 return 0;
144
145         /* print device */
146         down(&dump_ops_mutex);
147         for (part = 0; part < disk->minors - 1; part++) {
148                 bdev = bdget_disk(disk, part);
149                 if (dump_ops->find_dump(bdev)) {
150                         tmp = sprintf(p, "%s\n", bdevname(bdev, name));
151                         len += tmp;
152                         p += tmp;
153                 }
154                 bdput(bdev);
155                 if(len >= maxlen)
156                         break;
157         }
158         up(&dump_ops_mutex);
159
160         return len;
161 }
162
163 EXPORT_SYMBOL_GPL(diskdump_sysfs_show);
164
165 /*
166  * run timer/tasklet/workqueue during dump
167  */
168 void diskdump_setup_timestamp(void)
169 {
170         unsigned long long t;
171
172         platform_timestamp(timestamp_base);
173         udelay(1000000/HZ);
174         platform_timestamp(t);
175         timestamp_hz = (unsigned long)(t - timestamp_base);
176         diskdump_update();
177 }
178
179 EXPORT_SYMBOL_GPL(diskdump_setup_timestamp);
180
181 void diskdump_update(void)
182 {
183         unsigned long long t;
184
185         touch_nmi_watchdog();
186
187         /* update jiffies */
188         platform_timestamp(t);
189         while (t > timestamp_base + timestamp_hz) {
190                 timestamp_base += timestamp_hz;
191                 jiffies++;
192                 platform_timestamp(t);
193         }
194
195         dump_run_timers();
196         dump_run_tasklet();
197         dump_run_workqueue();
198 }
199
200 EXPORT_SYMBOL_GPL(diskdump_update);
201
202
203 /*
204  * register/unregister hook
205  */
206 int diskdump_register_hook(void (*dump_func) (struct pt_regs *))
207 {
208         if (diskdump_func)
209                 return -EEXIST;
210
211         diskdump_func = dump_func;
212
213         return 0;
214 }
215
216 EXPORT_SYMBOL_GPL(diskdump_register_hook);
217
218 void diskdump_unregister_hook(void)
219 {
220         diskdump_func = NULL;
221 }
222
223 EXPORT_SYMBOL_GPL(diskdump_unregister_hook);
224
225 void (*netdump_func) (struct pt_regs *regs) = NULL;
226 int netdump_mode = 0;
227 EXPORT_SYMBOL_GPL(netdump_mode);
228
229 /*
230  * Try crashdump. Diskdump is first, netdump is second.
231  * We clear diskdump_func before call of diskdump_func, so
232  * If double panic would occur in diskdump, netdump can handle
233  * it.
234  */
235 void try_crashdump(struct pt_regs *regs)
236 {
237         void (*func)(struct pt_regs *);
238
239         if (diskdump_func) {
240                 func = diskdump_func;
241                 diskdump_func = NULL;
242                 func(regs);
243         }
244         if (netdump_func)
245                 netdump_func(regs);
246 }