root/drivers/block/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. do_load
  4. 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_NR  MEM_MAJOR
  21 #include "blk.h"
  22 
  23 #define RAMDISK_MINOR   1
  24 
  25 
  26 char    *rd_start;
  27 int     rd_length = 0;
  28 static int rd_blocksizes[2] = {0, 0};
  29 
  30 static void do_rd_request(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  31 {
  32         int     len;
  33         char    *addr;
  34 
  35 repeat:
  36         INIT_REQUEST;
  37         addr = rd_start + (CURRENT->sector << 9);
  38         len = CURRENT->current_nr_sectors << 9;
  39 
  40         if ((MINOR(CURRENT->dev) != RAMDISK_MINOR) ||
  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(MEM_MAJOR,"rd",&rd_fops)) {
  81                 printk("RAMDISK: Unable to get major %d.\n", MEM_MAJOR);
  82                 return 0;
  83         }
  84         blk_dev[MEM_MAJOR].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 
  91         for(i=0;i<2;i++) rd_blocksizes[i] = 1024;
  92         blksize_size[MAJOR_NR] = rd_blocksizes;
  93 
  94         return(length);
  95 }
  96 
  97 static void do_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
  98 {
  99         struct buffer_head *bh;
 100         struct minix_super_block s;
 101         int             block, tries;
 102         int             i = 1;
 103         int             nblocks;
 104         char            *cp;
 105 
 106         /*
 107          * Check for a super block on the diskette.
 108          * The old-style boot/root diskettes had their RAM image
 109          * starting at block 512 of the boot diskette.  LINUX/Pro
 110          * uses the entire diskette as a file system, so in that
 111          * case, we have to look at block 0.  Be intelligent about
 112          * this, and check both... - FvK
 113          */
 114         for (tries = 0; tries < 1000; tries += 512) {
 115                 block = tries;
 116                 bh = breada(ROOT_DEV,block+1,BLOCK_SIZE, 0,  PAGE_SIZE);
 117                 if (!bh) {
 118                         printk("RAMDISK: I/O error while looking for super block!\n");
 119                         return;
 120                 }
 121 
 122                 /* This is silly- why do we require it to be a MINIX FS? */
 123                 *((struct minix_super_block *) &s) =
 124                         *((struct minix_super_block *) bh->b_data);
 125                 brelse(bh);
 126                 nblocks = s.s_nzones << s.s_log_zone_size;
 127                 if (s.s_magic != MINIX_SUPER_MAGIC &&
 128                     s.s_magic != MINIX_SUPER_MAGIC2) {
 129                         printk("RAMDISK: trying old-style RAM image.\n");
 130                         continue;
 131                 }
 132 
 133                 if (nblocks > (rd_length >> BLOCK_SIZE_BITS)) {
 134                         printk("RAMDISK: image too big! (%d/%d blocks)\n",
 135                                         nblocks, rd_length >> BLOCK_SIZE_BITS);
 136                         return;
 137                 }
 138                 printk("RAMDISK: Loading %d blocks into RAM disk", nblocks);
 139 
 140                 /* We found an image file system.  Load it into core! */
 141                 cp = rd_start;
 142                 while (nblocks) {
 143                         if (nblocks > 2) 
 144                                 bh = breada(ROOT_DEV, block, BLOCK_SIZE, 0,  PAGE_SIZE);
 145                         else
 146                                 bh = bread(ROOT_DEV, block, BLOCK_SIZE);
 147                         if (!bh) {
 148                                 printk("RAMDISK: I/O error on block %d, aborting!\n", 
 149                                 block);
 150                                 return;
 151                         }
 152                         (void) memcpy(cp, bh->b_data, BLOCK_SIZE);
 153                         brelse(bh);
 154                         if (!(nblocks-- & 15)) printk(".");
 155                         cp += BLOCK_SIZE;
 156                         block++;
 157                         i++;
 158                 }
 159                 printk("\ndone\n");
 160 
 161                 /* We loaded the file system image.  Prepare for mounting it. */
 162                 ROOT_DEV = ((MEM_MAJOR << 8) | RAMDISK_MINOR);
 163                 return;
 164         }
 165 }
 166 
 167 int floppy_grab_irq_and_dma(void);
 168 void floppy_release_irq_and_dma(void);
 169 
 170 /*
 171  * If the root device is the RAM disk, try to load it.
 172  * In order to do this, the root device is originally set to the
 173  * floppy, and we later change it to be RAM disk.
 174  */
 175 void rd_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 176 {
 177         /* If no RAM disk specified, give up early. */
 178         if (!rd_length)
 179                 return;
 180         printk("RAMDISK: %d bytes, starting at 0x%x\n",
 181                         rd_length, (int) rd_start);
 182 
 183         /* If we are doing a diskette boot, we might have to pre-load it. */
 184         if (MAJOR(ROOT_DEV) != FLOPPY_MAJOR)
 185                 return;
 186 
 187 /* ugly, ugly */
 188         if (floppy_grab_irq_and_dma()) {
 189                 printk("Unable to grab floppy IRQ/DMA for loading ramdisk image\n");
 190                 return;
 191         }
 192         check_disk_change(ROOT_DEV);
 193         do_load();
 194         floppy_release_irq_and_dma();
 195 }

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