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

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