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 #include <linux/string.h>
   8 
   9 #include <linux/config.h>
  10 #include <linux/sched.h>
  11 #include <linux/minix_fs.h>
  12 #include <linux/fs.h>
  13 #include <linux/kernel.h>
  14 #include <asm/system.h>
  15 #include <asm/segment.h>
  16 #include <asm/memory.h>
  17 
  18 #define MAJOR_NR 1
  19 #include "blk.h"
  20 
  21 char    *rd_start;
  22 int     rd_length = 0;
  23 
  24 void do_rd_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  25 {
  26         int     len;
  27         char    *addr;
  28 
  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,                   /* no special open code */
  58         NULL                    /* no special release code */
  59 };
  60 
  61 /*
  62  * Returns amount of memory which needs to be reserved.
  63  */
  64 long rd_init(long mem_start, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
  65 {
  66         int     i;
  67         char    *cp;
  68 
  69         blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
  70         blkdev_fops[MAJOR_NR] = &rd_fops;
  71         rd_start = (char *) mem_start;
  72         rd_length = length;
  73         cp = rd_start;
  74         for (i=0; i < length; i++)
  75                 *cp++ = '\0';
  76         return(length);
  77 }
  78 
  79 /*
  80  * If the root device is the ram disk, try to load it.
  81  * In order to do this, the root device is originally set to the
  82  * floppy, and we later change it to be ram disk.
  83  */
  84 void rd_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  85 {
  86         struct buffer_head *bh;
  87         struct super_block      s;
  88         int             block = 256;    /* Start at block 256 */
  89         int             i = 1;
  90         int             nblocks;
  91         char            *cp;            /* Move pointer */
  92         
  93         if (!rd_length)
  94                 return;
  95         printk("Ram disk: %d bytes, starting at 0x%x\n", rd_length,
  96                 (int) rd_start);
  97         if (MAJOR(ROOT_DEV) != 2)
  98                 return;
  99         bh = breada(ROOT_DEV,block+1,block,block+2,-1);
 100         if (!bh) {
 101                 printk("Disk error while looking for ramdisk!\n");
 102                 return;
 103         }
 104         *((struct minix_super_block *) &s) = *((struct minix_super_block *) bh->b_data);
 105         brelse(bh);
 106         if (s.s_magic != MINIX_SUPER_MAGIC)
 107                 /* No ram disk image present, assume normal floppy boot */
 108                 return;
 109         nblocks = s.s_nzones << s.s_log_zone_size;
 110         if (nblocks > (rd_length >> BLOCK_SIZE_BITS)) {
 111                 printk("Ram disk image too big!  (%d blocks, %d avail)\n", 
 112                         nblocks, rd_length >> BLOCK_SIZE_BITS);
 113                 return;
 114         }
 115         printk("Loading %d bytes into ram disk... 0000k", 
 116                 nblocks << BLOCK_SIZE_BITS);
 117         cp = rd_start;
 118         while (nblocks) {
 119                 if (nblocks > 2) 
 120                         bh = breada(ROOT_DEV, block, block+1, block+2, -1);
 121                 else
 122                         bh = bread(ROOT_DEV, block);
 123                 if (!bh) {
 124                         printk("I/O error on block %d, aborting load\n", 
 125                                 block);
 126                         return;
 127                 }
 128                 (void) memcpy(cp, bh->b_data, BLOCK_SIZE);
 129                 brelse(bh);
 130                 printk("\010\010\010\010\010%4dk",i);
 131                 cp += BLOCK_SIZE;
 132                 block++;
 133                 nblocks--;
 134                 i++;
 135         }
 136         printk("\010\010\010\010\010done \n");
 137         ROOT_DEV=0x0101;
 138 }

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