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         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
  71         blkdev_fops[MAJOR_NR] = &rd_fops;
  72         rd_start = (char *) mem_start;
  73         rd_length = length;
  74         cp = rd_start;
  75         for (i=0; i < length; i++)
  76                 *cp++ = '\0';
  77         return(length);
  78 }
  79 
  80 /*
  81  * If the root device is the ram disk, try to load it.
  82  * In order to do this, the root device is originally set to the
  83  * floppy, and we later change it to be ram disk.
  84  */
  85 void rd_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  86 {
  87         struct buffer_head *bh;
  88         struct minix_super_block s;
  89         int             block = 512;    /* Start at block 512 */
  90         int             i = 1;
  91         int             nblocks;
  92         char            *cp;            /* Move pointer */
  93         
  94         if (!rd_length)
  95                 return;
  96         printk("Ram disk: %d bytes, starting at 0x%x\n", rd_length,
  97                 (int) rd_start);
  98         if (MAJOR(ROOT_DEV) != 2)
  99                 return;
 100         bh = breada(ROOT_DEV,block+1,block,block+2,-1);
 101         if (!bh) {
 102                 printk("Disk error while looking for ramdisk!\n");
 103                 return;
 104         }
 105         *((struct minix_super_block *) &s) = *((struct minix_super_block *) bh->b_data);
 106         brelse(bh);
 107         if (s.s_magic != MINIX_SUPER_MAGIC)
 108                 /* No ram disk image present, assume normal floppy boot */
 109                 return;
 110         nblocks = s.s_nzones << s.s_log_zone_size;
 111         if (nblocks > (rd_length >> BLOCK_SIZE_BITS)) {
 112                 printk("Ram disk image too big!  (%d blocks, %d avail)\n", 
 113                         nblocks, rd_length >> BLOCK_SIZE_BITS);
 114                 return;
 115         }
 116         printk("Loading %d bytes into ram disk\n",
 117                 nblocks << BLOCK_SIZE_BITS);
 118         cp = rd_start;
 119         while (nblocks) {
 120                 if (nblocks > 2) 
 121                         bh = breada(ROOT_DEV, block, block+1, block+2, -1);
 122                 else
 123                         bh = bread(ROOT_DEV, block, BLOCK_SIZE);
 124                 if (!bh) {
 125                         printk("I/O error on block %d, aborting load\n", 
 126                                 block);
 127                         return;
 128                 }
 129                 (void) memcpy(cp, bh->b_data, BLOCK_SIZE);
 130                 brelse(bh);
 131                 if (!(nblocks-- & 15))
 132                         printk(".");
 133                 cp += BLOCK_SIZE;
 134                 block++;
 135                 i++;
 136         }
 137         printk("\ndone\n");
 138         ROOT_DEV=0x0101;
 139 }

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