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  * Modifications by Fred N. van Kempen to allow for bootable root
   7  * disks (which are used in LINUX/Pro).  Also some cleanups.  03/03/93
   8  */
   9 
  10 
  11 #include <linux/config.h>
  12 #include <linux/sched.h>
  13 #include <linux/minix_fs.h>
  14 #include <linux/fs.h>
  15 #include <linux/kernel.h>
  16 #include <linux/string.h>
  17 #include <asm/system.h>
  18 #include <asm/segment.h>
  19 
  20 #define MAJOR_RAMDISK   1               /* should be in <linux/major.h> */
  21 #define MAJOR_FLOPPY    2               /* should be in <linux/major.h> */
  22 #define MINOR_RAMDISK   1
  23 
  24 #define MAJOR_NR        MAJOR_RAMDISK   /* weird hack- FvK */
  25 #include "blk.h"
  26 
  27 
  28 char    *rd_start;
  29 int     rd_length = 0;
  30 static int rd_blocksizes[2] = {0, 0};
  31 
  32 static void do_rd_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  33 {
  34         int     len;
  35         char    *addr;
  36 
  37 repeat:
  38         INIT_REQUEST;
  39         addr = rd_start + (CURRENT->sector << 9);
  40         len = CURRENT->nr_sectors << 9;
  41         if ((MINOR(CURRENT->dev) != MINOR_RAMDISK) ||
  42             (addr+len > rd_start+rd_length)) {
  43                 end_request(0);
  44                 goto repeat;
  45         }
  46         if (CURRENT-> cmd == WRITE) {
  47                 (void ) memcpy(addr,
  48                               CURRENT->buffer,
  49                               len);
  50         } else if (CURRENT->cmd == READ) {
  51                 (void) memcpy(CURRENT->buffer, 
  52                               addr,
  53                               len);
  54         } else
  55                 panic("RAMDISK: unknown RAM disk command !\n");
  56         end_request(1);
  57         goto repeat;
  58 }
  59 
  60 static struct file_operations rd_fops = {
  61         NULL,                   /* lseek - default */
  62         block_read,             /* read - general block-dev read */
  63         block_write,            /* write - general block-dev write */
  64         NULL,                   /* readdir - bad */
  65         NULL,                   /* select */
  66         NULL,                   /* ioctl */
  67         NULL,                   /* mmap */
  68         NULL,                   /* no special open code */
  69         NULL,                   /* no special release code */
  70         block_fsync             /* fsync */
  71 };
  72 
  73 /*
  74  * Returns amount of memory which needs to be reserved.
  75  */
  76 long rd_init(long mem_start, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
  77 {
  78         int     i;
  79         char    *cp;
  80 
  81         if (register_blkdev(MAJOR_RAMDISK,"rd",&rd_fops)) {
  82                 printk("RAMDISK: Unable to get major %d.\n", MAJOR_RAMDISK);
  83                 return 0;
  84         }
  85         blk_dev[MAJOR_RAMDISK].request_fn = DEVICE_REQUEST;
  86         rd_start = (char *) mem_start;
  87         rd_length = length;
  88         cp = rd_start;
  89         for (i=0; i < length; i++)
  90                 *cp++ = '\0';
  91 
  92         for(i=0;i<2;i++) rd_blocksizes[i] = 1024;
  93         blksize_size[MAJOR_NR] = rd_blocksizes;
  94 
  95         return(length);
  96 }
  97 
  98 /*
  99  * If the root device is the RAM disk, try to load it.
 100  * In order to do this, the root device is originally set to the
 101  * floppy, and we later change it to be RAM disk.
 102  */
 103 void rd_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 104 {
 105         struct buffer_head *bh;
 106         struct minix_super_block s;
 107         int             block, try;
 108         int             i = 1;
 109         int             nblocks;
 110         char            *cp;
 111 
 112         /* If no RAM disk specified, give up early. */
 113         if (!rd_length) return;
 114         printk("RAMDISK: %d bytes, starting at 0x%x\n",
 115                                         rd_length, (int) rd_start);
 116 
 117         /* If we are doing a diskette boot, we might have to pre-load it. */
 118         if (MAJOR(ROOT_DEV) != MAJOR_FLOPPY) return;
 119 
 120         /*
 121          * Check for a super block on the diskette.
 122          * The old-style boot/root diskettes had their RAM image
 123          * starting at block 512 of the boot diskette.  LINUX/Pro
 124          * uses the enire diskette as a file system, so in that
 125          * case, we have to look at block 0.  Be intelligent about
 126          * this, and check both... - FvK
 127          */
 128         for (try = 0; try < 1000; try += 512) {
 129                 block = try;
 130                 bh = breada(ROOT_DEV,block+1,block,block+2,-1);
 131                 if (!bh) {
 132                         printk("RAMDISK: I/O error while looking for super block!\n");
 133                         return;
 134                 }
 135 
 136                 /* This is silly- why do we require it to be a MINIX FS? */
 137                 *((struct minix_super_block *) &s) =
 138                         *((struct minix_super_block *) bh->b_data);
 139                 brelse(bh);
 140                 nblocks = s.s_nzones << s.s_log_zone_size;
 141                 if (s.s_magic != MINIX_SUPER_MAGIC) {
 142                         printk("RAMDISK: trying old-style RAM image.\n");
 143                         continue;
 144                 }
 145 
 146                 if (nblocks > (rd_length >> BLOCK_SIZE_BITS)) {
 147                         printk("RAMDISK: image too big! (%d/%d blocks)\n",
 148                                         nblocks, rd_length >> BLOCK_SIZE_BITS);
 149                         return;
 150                 }
 151                 printk("RAMDISK: Loading %d blocks into RAM disk", nblocks);
 152 
 153                 /* We found an image file system.  Load it into core! */
 154                 cp = rd_start;
 155                 while (nblocks) {
 156                         if (nblocks > 2) 
 157                                 bh = breada(ROOT_DEV, block, block+1, block+2, -1);
 158                         else
 159                                 bh = bread(ROOT_DEV, block, BLOCK_SIZE);
 160                         if (!bh) {
 161                                 printk("RAMDISK: I/O error on block %d, aborting!\n", 
 162                                 block);
 163                                 return;
 164                         }
 165                         (void) memcpy(cp, bh->b_data, BLOCK_SIZE);
 166                         brelse(bh);
 167                         if (!(nblocks-- & 15)) printk(".");
 168                         cp += BLOCK_SIZE;
 169                         block++;
 170                         i++;
 171                 }
 172                 printk("\ndone\n");
 173 
 174                 /* We loaded the file system image.  Prepare for mounting it. */
 175                 ROOT_DEV = ((MAJOR_RAMDISK << 8) | MINOR_RAMDISK);
 176                 return;
 177         }
 178 }

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