2 * Implements the dump driver interface for saving a dump to
3 * a block device through the kernel's generic low level block I/O
4 * routines, or through polling I/O.
6 * Started: June 2002 - Mohamed Abbas <mohamed.abbas@intel.com>
7 * Moved original lkcd kiobuf dump i/o code from dump_base.c
8 * to use generic dump device interfaces
10 * Sept 2002 - Bharata B. Rao <bharata@in.ibm.com>
11 * Convert dump i/o to directly use bio instead of kiobuf for 2.5
13 * Oct 2002 - Suparna Bhattacharya <suparna@in.ibm.com>
14 * Rework to new dumpdev.h structures, implement open/close/
15 * silence, misc fixes (blocknr removal, bio_add_page usage)
16 * Oct 2004 - Mar 2005 - Mohamed Abbas <mohamed.abbas@intel.com>
17 * Jason Uhlenkott <jasonuhl@sgi.com>
18 * Implement polling I/O (adapted from lkdump, with thanks
19 * to Nobuhiro Tachino).
21 * Copyright (C) 1999 - 2005 Silicon Graphics, Inc. All rights reserved.
22 * Copyright (C) 2001 - 2002 Matt D. Robinson. All rights reserved.
23 * Copyright (C) 2002 International Business Machines Corp.
24 * Copyright (C) 2004 FUJITSU LIMITED
26 * This code is released under version 2 of the GNU GPL.
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/module.h>
32 #include <linux/init.h>
33 #include <linux/blkdev.h>
34 #include <linux/bio.h>
35 #include <linux/diskdump.h>
36 #include <linux/dump.h>
37 #include <linux/delay.h>
39 #include <asm/hardirq.h>
40 #include "dump_methods.h"
43 /* ----- Support functions for interrupt-driven dumps ----- */
46 extern void *dump_page_buf;
48 /* The end_io callback for dump i/o completion */
50 dump_bio_end_io(struct bio *bio, unsigned int bytes_done, int error)
52 struct dump_blockdev *dump_bdev;
55 /* some bytes still left to transfer */
56 return 1; /* not complete */
59 dump_bdev = (struct dump_blockdev *)bio->bi_private;
61 printk("LKCD: IO error while writing the dump, aborting\n");
64 dump_bdev->err = error;
66 /* no wakeup needed, since caller polls for completion */
70 /* Check if the dump bio is already mapped to the specified buffer */
72 dump_block_map_valid(struct dump_blockdev *dev, struct page *page,
75 struct bio *bio = dev->bio;
76 unsigned long bsize = 0;
79 return 0; /* first time, not mapped */
82 if ((bio_page(bio) != page) || (len > bio->bi_vcnt << PAGE_SHIFT))
83 return 0; /* buffer not mapped */
85 bsize = bdev_hardsect_size(bio->bi_bdev);
86 if ((len & (PAGE_SIZE - 1)) || (len & bsize))
87 return 0; /* alignment checks needed */
89 /* quick check to decide if we need to redo bio_add_page */
90 if (bdev_get_queue(bio->bi_bdev)->merge_bvec_fn)
91 return 0; /* device may have other restrictions */
93 return 1; /* already mapped */
97 * Set up the dump bio for i/o from the specified buffer
98 * Return value indicates whether the full buffer could be mapped or not
101 dump_block_map(struct dump_blockdev *dev, void *buf, int len)
103 struct page *page = virt_to_page(buf);
104 struct bio *bio = dev->bio;
105 unsigned long bsize = 0;
107 bio->bi_bdev = dev->bdev;
108 bio->bi_sector = (dev->start_offset + dev->ddev.curr_offset) >> 9;
109 bio->bi_idx = 0; /* reset index to the beginning */
111 if (dump_block_map_valid(dev, page, len)) {
112 /* already mapped and usable rightaway */
113 bio->bi_size = len; /* reset size to the whole bio */
114 bio->bi_vcnt = (len + PAGE_SIZE - 1) / PAGE_SIZE; /* Set the proper vector cnt */
116 /* need to map the bio */
119 bsize = bdev_hardsect_size(bio->bi_bdev);
121 /* first a few sanity checks */
123 printk("LKCD: map: len less than hardsect size \n");
127 if ((unsigned long)buf & bsize) {
128 printk("LKCD: map: not aligned\n");
132 /* assume contig. page aligned low mem buffer( no vmalloc) */
133 if ((page_address(page) != buf) || (len & (PAGE_SIZE - 1))) {
134 printk("LKCD: map: invalid buffer alignment!\n");
137 /* finally we can go ahead and map it */
138 while (bio->bi_size < len)
139 if (bio_add_page(bio, page++, PAGE_SIZE, 0) == 0) {
143 bio->bi_end_io = dump_bio_end_io;
144 bio->bi_private = dev;
147 if (bio->bi_size != len) {
148 printk("LKCD: map: bio size = %d not enough for len = %d!\n",
156 dump_free_bio(struct bio *bio)
159 kfree(bio->bi_io_vec);
164 /* ----- Support functions for polling I/O based dumps ----- */
166 static DECLARE_MUTEX(disk_dump_mutex);
167 static LIST_HEAD(disk_dump_types);
168 static struct disk_dump_device dump_device;
169 static struct disk_dump_partition dump_part;
170 static unsigned long long timestamp_base;
171 static unsigned long timestamp_hz;
172 static unsigned long flags_global;
173 static int polling_mode;
174 static void dump_blockdev_unconfigure(void);
176 void diskdump_setup_timestamp(void)
178 unsigned long long t=0;
180 platform_timestamp(timestamp_base);
182 platform_timestamp(t);
183 timestamp_hz = (unsigned long)(t - timestamp_base);
187 void diskdump_update(void)
189 unsigned long long t=0;
191 touch_nmi_watchdog();
194 platform_timestamp(t);
195 while (t > timestamp_base + timestamp_hz) {
196 timestamp_base += timestamp_hz;
198 platform_timestamp(t);
203 dump_run_workqueue();
205 EXPORT_SYMBOL_GPL(diskdump_update);
207 static void *find_real_device(struct device *dev,
208 struct disk_dump_type **_dump_type)
211 struct disk_dump_type *dump_type;
213 list_for_each_entry(dump_type, &disk_dump_types, list) {
214 real_device = dump_type->probe(dev);
216 *_dump_type = dump_type;
223 static int register_disk_dump_device(struct device *dev, struct block_device *bdev)
225 struct disk_dump_type *dump_type = NULL;
232 down(&disk_dump_mutex);
234 real_device = find_real_device(dev, &dump_type);
240 if (dump_device.device == real_device) {
243 } else if (dump_device.device) {
247 dump_device.device = real_device;
249 ret = dump_type->add_device(&dump_device);
251 dump_device.device = NULL;
255 dump_device.dump_type = dump_type;
256 dump_part.device = &dump_device;
257 dump_part.bdev = bdev;
258 dump_part.nr_sects = bdev->bd_part->nr_sects;
259 dump_part.start_sect = bdev->bd_part->start_sect;
262 up(&disk_dump_mutex);
266 static void unregister_disk_dump_device(struct block_device *bdev)
268 struct disk_dump_type *dump_type;
270 down(&disk_dump_mutex);
272 if(!dump_part.device) {
273 up(&disk_dump_mutex);
277 BUG_ON(dump_part.device != &dump_device);
278 BUG_ON(dump_part.bdev != bdev);
280 dump_part.device = NULL;
281 dump_part.bdev = NULL;
283 dump_type = dump_device.dump_type;
284 dump_type->remove_device(&dump_device);
285 dump_device.device = NULL;
286 dump_device.dump_type = NULL;
288 up(&disk_dump_mutex);
291 int register_disk_dump_type(struct disk_dump_type *dump_type)
293 down(&disk_dump_mutex);
294 list_add(&dump_type->list, &disk_dump_types);
295 up(&disk_dump_mutex);
299 EXPORT_SYMBOL_GPL(register_disk_dump_type);
301 int unregister_disk_dump_type(struct disk_dump_type *dump_type)
303 lock_kernel(); /* guard against the dump ioctl */
305 if (dump_device.dump_type == dump_type)
306 dump_blockdev_unconfigure();
308 down(&disk_dump_mutex);
309 list_del(&dump_type->list);
310 up(&disk_dump_mutex);
315 EXPORT_SYMBOL_GPL(unregister_disk_dump_type);
318 /* --------------------------------------------------- */
322 dump_block_intr_open(struct dump_dev *dev, unsigned long arg)
324 struct dump_blockdev *dump_bdev = DUMP_BDEV(dev);
325 struct block_device *bdev;
327 struct bio_vec *bvec;
329 /* make sure this is a valid block device */
335 /* Convert it to the new dev_t format */
336 arg = MKDEV((arg >> OLDMINORBITS), (arg & OLDMINORMASK));
338 /* get a corresponding block_dev struct for this */
339 bdev = bdget((dev_t)arg);
345 /* get the block device opened */
346 if ((retval = blkdev_get(bdev, O_RDWR | O_LARGEFILE, 0))) {
350 if ((dump_bdev->bio = kmalloc(sizeof(struct bio), GFP_KERNEL))
352 printk("LKCD: Cannot allocate bio\n");
357 bio_init(dump_bdev->bio);
359 if ((bvec = kmalloc(sizeof(struct bio_vec) *
360 (DUMP_BUFFER_SIZE >> PAGE_SHIFT), GFP_KERNEL)) == NULL) {
365 /* assign the new dump dev structure */
366 dump_bdev->dev_id = (dev_t)arg;
367 dump_bdev->bdev = bdev;
369 /* make a note of the limit */
370 dump_bdev->limit = bdev->bd_inode->i_size;
372 /* now make sure we can map the dump buffer */
373 dump_bdev->bio->bi_io_vec = bvec;
374 dump_bdev->bio->bi_max_vecs = DUMP_BUFFER_SIZE >> PAGE_SHIFT;
376 retval = dump_block_map(dump_bdev, dump_config.dumper->dump_buf,
380 printk("LKCD: open: dump_block_map failed, ret %d\n", retval);
384 printk("LKCD: Block device (%d,%d) successfully configured for dumping\n",
385 MAJOR(dump_bdev->dev_id),
386 MINOR(dump_bdev->dev_id));
389 /* after opening the block device, return */
392 err2: dump_free_bio(dump_bdev->bio);
393 dump_bdev->bio = NULL;
396 dump_bdev->bdev = NULL;
401 dump_block_poll_open(struct dump_dev *dev, unsigned long arg)
403 struct dump_blockdev *dump_bdev = DUMP_BDEV(dev);
404 struct block_device *bdev;
406 struct device *target = NULL;
408 /* make sure this is a valid block device */
414 /* Convert it to the new dev_t format */
415 arg = MKDEV((arg >> OLDMINORBITS), (arg & OLDMINORMASK));
417 /* get a corresponding block_dev struct for this */
418 bdev = bdget((dev_t)arg);
424 /* get the block device opened */
425 if ((retval = blkdev_get(bdev, O_RDWR | O_LARGEFILE, 0))) {
430 dump_bdev->dev_id = (dev_t)arg;
431 dump_bdev->bdev = bdev;
433 /* make a note of the limit */
434 dump_bdev->limit = bdev->bd_inode->i_size;
436 target = get_device(bdev->bd_disk->driverfs_dev);
441 retval = register_disk_dump_device(target,bdev);
442 if (retval == -EEXIST)
447 printk("LKCD: Block device (%d,%d) successfully configured for dumping using polling I/O\n",
448 MAJOR((dev_t)arg), MINOR((dev_t)arg));
450 /* after opening the block device, return */
459 * Prepares the dump device so we can take a dump later.
460 * The caller is expected to have filled up the dev_id field in the
461 * block dump dev structure.
463 * At dump time when dump_block_write() is invoked it will be too
464 * late to recover, so as far as possible make sure obvious errors
465 * get caught right here and reported back to the caller.
468 dump_block_open(struct dump_dev *dev, const char *arg)
472 if ((sscanf(arg, "%lx", &devid)) != 1)
478 if (dump_config.polling){
480 if (!dump_block_poll_open(dev, devid)) {
484 * If polling I/O isn't supported by this
485 * device, fall back to interrupt-driven mode.
487 dump_config.polling = 0;
492 return dump_block_intr_open(dev, devid);
496 * Close the dump device and release associated resources.
497 * Invoked when unconfiguring the dump device.
500 dump_block_release(struct dump_dev *dev)
502 struct dump_blockdev *dump_bdev = DUMP_BDEV(dev);
504 /* release earlier bdev if present */
505 if (dump_bdev->bdev) {
506 unregister_disk_dump_device(dump_bdev->bdev);
507 blkdev_put(dump_bdev->bdev);
508 dump_bdev->bdev = NULL;
511 if (dump_bdev->bio) {
512 dump_free_bio(dump_bdev->bio);
513 dump_bdev->bio = NULL;
520 dump_block_intr_silence(struct dump_dev *dev)
522 struct dump_blockdev *dump_bdev = DUMP_BDEV(dev);
523 struct request_queue *q = bdev_get_queue(dump_bdev->bdev);
526 /* If we can't get request queue lock, refuse to take the dump */
527 if (!spin_trylock(q->queue_lock))
530 ret = elv_queue_empty(q);
531 spin_unlock(q->queue_lock);
533 /* For now we assume we have the device to ourselves */
534 /* Just a quick sanity check */
536 /* Warn the user and move on */
537 printk("LKCD: Warning: Non-empty request queue\n");
538 printk("LKCD: I/O requests in flight at dump time\n");
542 * Move to a softer level of silencing where no spin_lock_irqs
543 * are held on other cpus
545 dump_silence_level = DUMP_SOFT_SPIN_CPUS;
547 ret = __dump_irq_enable();
552 printk("LKCD: Dumping to block device (%d,%d) on CPU %d ...\n",
553 MAJOR(dump_bdev->dev_id), MINOR(dump_bdev->dev_id),
560 dump_block_poll_silence(struct dump_dev *dev)
562 struct dump_blockdev *dump_bdev = DUMP_BDEV(dev);
565 local_irq_save(flags_global);
568 touch_nmi_watchdog();
570 if (down_trylock(&disk_dump_mutex))
573 dump_polling_oncpu = smp_processor_id() + 1;
576 * Setup timer/tasklet
579 dump_clear_tasklet();
580 dump_clear_workqueue();
582 diskdump_setup_timestamp();
584 BUG_ON(dump_part.bdev != dump_bdev->bdev);
587 * Move to a softer level of silencing where no spin_lock_irqs
588 * are held on other cpus
590 dump_silence_level = DUMP_SOFT_SPIN_CPUS;
592 touch_nmi_watchdog();
594 if (dump_device.ops.quiesce)
595 if ((ret = dump_device.ops.quiesce(&dump_device)) < 0) {
596 printk("LKCD: Quiesce failed. error %d\n", ret);
599 touch_nmi_watchdog();
600 printk("LKCD: Dumping to block device (%d,%d) on CPU %d using polling I/O ...\n",
601 MAJOR(dump_bdev->dev_id), MINOR(dump_bdev->dev_id),
608 * Prepare the dump device for use (silence any ongoing activity
609 * and quiesce state) when the system crashes.
612 dump_block_silence(struct dump_dev *dev)
615 return dump_block_poll_silence(dev);
617 return dump_block_intr_silence(dev);
622 dump_block_intr_resume(struct dump_dev *dev)
624 __dump_irq_restore();
629 dump_block_poll_resume(struct dump_dev *dev)
631 struct dump_blockdev *dump_bdev = DUMP_BDEV(dev);
633 BUG_ON(dump_part.bdev != dump_bdev->bdev);
635 if (dump_device.device && dump_device.ops.shutdown)
636 if (dump_device.ops.shutdown(&dump_device))
637 printk("LKCD: polling dev: adapter shutdown failed.\n");
639 dump_polling_oncpu = 0;
640 preempt_enable_no_resched();
641 local_irq_restore(flags_global);
642 up(&disk_dump_mutex);
647 * Invoked when dumping is done. This is the time to put things back
648 * (i.e. undo the effects of dump_block_silence) so the device is
649 * available for normal use.
652 dump_block_resume(struct dump_dev *dev)
655 return dump_block_poll_resume(dev);
657 return dump_block_intr_resume(dev);
662 * Seek to the specified offset in the dump device.
663 * Makes sure this is a valid offset, otherwise returns an error.
666 dump_block_seek(struct dump_dev *dev, loff_t off)
668 struct dump_blockdev *dump_bdev = DUMP_BDEV(dev);
669 loff_t offset = off + dump_bdev->start_offset;
671 if (offset & ( PAGE_SIZE - 1)) {
672 printk("LKCD: seek: non-page aligned\n");
676 if (offset & (bdev_hardsect_size(dump_bdev->bdev) - 1)) {
677 printk("LKCD: seek: not sector aligned \n");
681 if (offset > dump_bdev->limit) {
682 printk("LKCD: seek: not enough space left on device!\n");
685 dev->curr_offset = off;
691 dump_block_intr_write(struct dump_dev *dev, void *buf,
694 struct dump_blockdev *dump_bdev = DUMP_BDEV(dev);
695 loff_t offset = dev->curr_offset + dump_bdev->start_offset;
696 int retval = -ENOSPC;
698 if (offset >= dump_bdev->limit) {
699 printk("LKCD: write: not enough space left on device!\n");
703 /* don't write more blocks than our max limit */
704 if (offset + len > dump_bdev->limit)
705 len = dump_bdev->limit - offset;
708 retval = dump_block_map(dump_bdev, buf, len);
710 printk("LKCD: write: dump_block_map failed! err %d\n", retval);
715 * Write out the data to disk.
716 * Assumes the entire buffer mapped to a single bio, which we can
717 * submit and wait for io completion. In the future, may consider
718 * increasing the dump buffer size and submitting multiple bio s
719 * for better throughput.
721 dump_bdev->err = -EAGAIN;
722 submit_bio(WRITE, dump_bdev->bio);
724 dump_bdev->ddev.curr_offset += len;
731 dump_block_poll_write(struct dump_dev *dev, void *buf,
734 struct dump_blockdev *dump_bdev = DUMP_BDEV(dev);
735 loff_t offset = dev->curr_offset + dump_bdev->start_offset;
736 int retval = -ENOSPC;
739 if (offset >= dump_bdev->limit) {
740 printk("LKCD: write: not enough space left on device!\n");
744 /* don't write more blocks than our max limit */
745 if (offset + len > dump_bdev->limit)
746 len = dump_bdev->limit - offset;
748 if (dump_part.bdev != dump_bdev->bdev) {
754 touch_nmi_watchdog();
755 ret = dump_device.ops.rw_block(&dump_part, WRITE, offset >> DUMP_PAGE_SHIFT,
756 buf, len >> DUMP_PAGE_SHIFT);
758 printk("LKCD: write error\n");
768 * Write out a buffer after checking the device limitations,
769 * sector sizes, etc. Assumes the buffer is in directly mapped
770 * kernel address space (not vmalloc'ed).
772 * Returns: number of bytes written or -ERRNO.
775 dump_block_write(struct dump_dev *dev, void *buf,
779 return dump_block_poll_write(dev, buf, len);
781 return dump_block_intr_write(dev, buf, len);
786 * Name: dump_block_ready()
787 * Func: check if the last dump i/o is over and ready for next request
790 dump_block_ready(struct dump_dev *dev, void *buf)
792 struct dump_blockdev *dump_bdev;
798 dump_bdev = DUMP_BDEV(dev);
799 q = bdev_get_queue(dump_bdev->bio->bi_bdev);
801 /* check for io completion */
802 if (dump_bdev->err == -EAGAIN) {
807 if (dump_bdev->err) {
808 printk("LKCD: dump i/o err\n");
809 return dump_bdev->err;
816 struct dump_dev_ops dump_blockdev_ops = {
817 .open = dump_block_open,
818 .release = dump_block_release,
819 .silence = dump_block_silence,
820 .resume = dump_block_resume,
821 .seek = dump_block_seek,
822 .write = dump_block_write,
823 /* .read not implemented */
824 .ready = dump_block_ready
827 static struct dump_blockdev default_dump_blockdev = {
828 .ddev = {.type = 1, .ops = &dump_blockdev_ops,
831 * leave enough room for the longest swap header possibly written
832 * written by mkswap (likely the largest page size supported by
835 .start_offset = DUMP_HEADER_OFFSET,
837 /* assume the rest of the fields are zeroed by default */
840 struct dump_blockdev *dump_blockdev = &default_dump_blockdev;
843 * Unregister and reregister ourselves. This has the side effect
844 * of unconfiguring the current dump device.
847 dump_blockdev_unconfigure(void)
849 dump_unregister_device(&dump_blockdev->ddev);
850 if (dump_register_device(&dump_blockdev->ddev) < 0)
851 printk("LKCD: block device driver registration failed\n");
855 dump_blockdev_init(void)
857 if (dump_register_device(&dump_blockdev->ddev) < 0) {
858 printk("LKCD: block device driver registration failed\n");
862 printk("LKCD: block device driver registered\n");
867 dump_blockdev_cleanup(void)
869 dump_unregister_device(&dump_blockdev->ddev);
870 printk("LKCD: block device driver unregistered\n");
873 MODULE_AUTHOR("LKCD Development Team <lkcd-devel@lists.sourceforge.net>");
874 MODULE_DESCRIPTION("Block Dump Driver for Linux Kernel Crash Dump (LKCD)");
875 MODULE_LICENSE("GPL");
877 module_init(dump_blockdev_init);
878 module_exit(dump_blockdev_cleanup);