root/kernel/blk_drv/ramdisk.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. do_rd_request
  2. rd_init
  3. rd_load

   1 /*
   2  *  linux/kernel/blk_drv/ramdisk.c
   3  *
   4  *  Written by Theodore Ts'o, 12/2/91
   5  */
   6 
   7 
   8 #include <linux/config.h>
   9 #include <linux/sched.h>
  10 #include <linux/minix_fs.h>
  11 #include <linux/fs.h>
  12 #include <linux/kernel.h>
  13 #include <linux/string.h>
  14 #include <asm/system.h>
  15 #include <asm/segment.h>
  16 
  17 #define MAJOR_NR 1
  18 #include "blk.h"
  19 
  20 char    *rd_start;
  21 int     rd_length = 0;
  22 
  23 static void do_rd_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  24 {
  25         int     len;
  26         char    *addr;
  27 
  28 repeat:
  29         INIT_REQUEST;
  30         addr = rd_start + (CURRENT->sector << 9);
  31         len = CURRENT->nr_sectors << 9;
  32         if ((MINOR(CURRENT->dev) != 1) || (addr+len > rd_start+rd_length)) {
  33                 end_request(0);
  34                 goto repeat;
  35         }
  36         if (CURRENT-> cmd == WRITE) {
  37                 (void ) memcpy(addr,
  38                               CURRENT->buffer,
  39                               len);
  40         } else if (CURRENT->cmd == READ) {
  41                 (void) memcpy(CURRENT->buffer, 
  42                               addr,
  43                               len);
  44         } else
  45                 panic("unknown ramdisk-command");
  46         end_request(1);
  47         goto repeat;
  48 }
  49 
  50 static struct file_operations rd_fops = {
  51         NULL,                   /* lseek - default */
  52         block_read,             /* read - general block-dev read */
  53         block_write,            /* write - general block-dev write */
  54         NULL,                   /* readdir - bad */
  55         NULL,                   /* select */
  56         NULL,                   /* ioctl */
  57         NULL,                   /* mmap */
  58         NULL,                   /* no special open code */
  59         NULL                    /* no special release code */
  60 };
  61 
  62 /*
  63  * Returns amount of memory which needs to be reserved.
  64  */
  65 long rd_init(long mem_start, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
  66 {
  67         int     i;
  68         char    *cp;
  69 
  70         if (register_blkdev(MAJOR_NR,"rd",&rd_fops)) {
  71                 printk("Unable to get major %d for ramdisk\n",MAJOR_NR);
  72                 return 0;
  73         }
  74         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
  75         rd_start = (char *) mem_start;
  76         rd_length = length;
  77         cp = rd_start;
  78         for (i=0; i < length; i++)
  79                 *cp++ = '\0';
  80         return(length);
  81 }
  82 
  83 /*
  84  * If the root device is the ram disk, try to load it.
  85  * In order to do this, the root device is originally set to the
  86  * floppy, and we later change it to be ram disk.
  87  */
  88 void rd_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  89 {
  90         struct buffer_head *bh;
  91         struct minix_super_block s;
  92         int             block = 512;    /* Start at block 512 */
  93         int             i = 1;
  94         int             nblocks;
  95         char            *cp;            /* Move pointer */
  96         
  97         if (!rd_length)
  98                 return;
  99         printk("Ram disk: %d bytes, starting at 0x%x\n", rd_length,
 100                 (int) rd_start);
 101         if (MAJOR(ROOT_DEV) != 2)
 102                 return;
 103         bh = breada(ROOT_DEV,block+1,block,block+2,-1);
 104         if (!bh) {
 105                 printk("Disk error while looking for ramdisk!\n");
 106                 return;
 107         }
 108         *((struct minix_super_block *) &s) = *((struct minix_super_block *) bh->b_data);
 109         brelse(bh);
 110         if (s.s_magic != MINIX_SUPER_MAGIC)
 111                 /* No ram disk image present, assume normal floppy boot */
 112                 return;
 113         nblocks = s.s_nzones << s.s_log_zone_size;
 114         if (nblocks > (rd_length >> BLOCK_SIZE_BITS)) {
 115                 printk("Ram disk image too big!  (%d blocks, %d avail)\n", 
 116                         nblocks, rd_length >> BLOCK_SIZE_BITS);
 117                 return;
 118         }
 119         printk("Loading %d bytes into ram disk\n",
 120                 nblocks << BLOCK_SIZE_BITS);
 121         cp = rd_start;
 122         while (nblocks) {
 123                 if (nblocks > 2) 
 124                         bh = breada(ROOT_DEV, block, block+1, block+2, -1);
 125                 else
 126                         bh = bread(ROOT_DEV, block, BLOCK_SIZE);
 127                 if (!bh) {
 128                         printk("I/O error on block %d, aborting load\n", 
 129                                 block);
 130                         return;
 131                 }
 132                 (void) memcpy(cp, bh->b_data, BLOCK_SIZE);
 133                 brelse(bh);
 134                 if (!(nblocks-- & 15))
 135                         printk(".");
 136                 cp += BLOCK_SIZE;
 137                 block++;
 138                 i++;
 139         }
 140         printk("\ndone\n");
 141         ROOT_DEV=0x0101;
 142 }

/* [previous][next][first][last][top][bottom][index][help] */