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

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