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/ext2_fs.h>
  15 #include <linux/fs.h>
  16 #include <linux/kernel.h>
  17 #include <linux/string.h>
  18 #include <asm/system.h>
  19 #include <asm/segment.h>
  20 
  21 #define MAJOR_NR  MEM_MAJOR
  22 #include "blk.h"
  23 
  24 #define RAMDISK_MINOR   1
  25 
  26 extern void wait_for_keypress(void);
  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->current_nr_sectors << 9;
  41 
  42         if ((MINOR(CURRENT->dev) != RAMDISK_MINOR) ||
  43             (addr+len > rd_start+rd_length)) {
  44                 end_request(0);
  45                 goto repeat;
  46         }
  47         if (CURRENT-> cmd == WRITE) {
  48                 (void ) memcpy(addr,
  49                               CURRENT->buffer,
  50                               len);
  51         } else if (CURRENT->cmd == READ) {
  52                 (void) memcpy(CURRENT->buffer, 
  53                               addr,
  54                               len);
  55         } else
  56                 panic("RAMDISK: unknown RAM disk command !\n");
  57         end_request(1);
  58         goto repeat;
  59 }
  60 
  61 static struct file_operations rd_fops = {
  62         NULL,                   /* lseek - default */
  63         block_read,             /* read - general block-dev read */
  64         block_write,            /* write - general block-dev write */
  65         NULL,                   /* readdir - bad */
  66         NULL,                   /* select */
  67         NULL,                   /* ioctl */
  68         NULL,                   /* mmap */
  69         NULL,                   /* no special open code */
  70         NULL,                   /* no special release code */
  71         block_fsync             /* fsync */
  72 };
  73 
  74 /*
  75  * Returns amount of memory which needs to be reserved.
  76  */
  77 long rd_init(long mem_start, int length)
     /* [previous][next][first][last][top][bottom][index][help] */
  78 {
  79         int     i;
  80         char    *cp;
  81 
  82         if (register_blkdev(MEM_MAJOR,"rd",&rd_fops)) {
  83                 printk("RAMDISK: Unable to get major %d.\n", MEM_MAJOR);
  84                 return 0;
  85         }
  86         blk_dev[MEM_MAJOR].request_fn = DEVICE_REQUEST;
  87         rd_start = (char *) mem_start;
  88         rd_length = length;
  89         cp = rd_start;
  90         for (i=0; i < length; i++)
  91                 *cp++ = '\0';
  92 
  93         for(i=0;i<2;i++) rd_blocksizes[i] = 1024;
  94         blksize_size[MAJOR_NR] = rd_blocksizes;
  95 
  96         return(length);
  97 }
  98 
  99 static void do_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 100 {
 101         struct buffer_head *bh;
 102         struct super_block {
 103           union
 104           {
 105             char minix [sizeof (struct minix_super_block)];
 106             char ext2 [sizeof (struct ext2_super_block)];
 107           } record;
 108         } sb;
 109         struct minix_super_block *minixsb =
 110                 (struct minix_super_block *)&sb;
 111         struct ext2_super_block *ext2sb =
 112                 (struct ext2_super_block *)&sb;
 113         int             block, tries;
 114         int             i = 1;
 115         int             nblocks;
 116         char            *cp;
 117         
 118         /*
 119          * Check for a super block on the diskette.
 120          * The old-style boot/root diskettes had their RAM image
 121          * starting at block 512 of the boot diskette.  LINUX/Pro
 122          * uses the entire diskette as a file system, so in that
 123          * case, we have to look at block 0.  Be intelligent about
 124          * this, and check both... - FvK
 125          */
 126         for (tries = 0; tries < 1000; tries += 512) {
 127                 block = tries;
 128                 bh = breada(ROOT_DEV,block+1,BLOCK_SIZE, 0,  PAGE_SIZE);
 129                 if (!bh) {
 130                         printk("RAMDISK: I/O error while looking for super block!\n");
 131                         return;
 132                 }
 133 
 134                 /* This is silly- why do we require it to be a MINIX FS? */
 135                 *((struct super_block *) &sb) =
 136                         *((struct super_block *) bh->b_data);
 137                 brelse(bh);
 138 
 139 
 140                 /* Try Minix */
 141                 nblocks = -1;
 142                 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
 143                     minixsb->s_magic == MINIX_SUPER_MAGIC2) {
 144                         printk("RAMDISK: Minix filesystem found at block %d\n",
 145                                 block);
 146                         nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
 147                 }
 148 
 149                 /* Try ext2 */
 150                 if (nblocks == -1 && (ext2sb->s_magic ==
 151                         EXT2_PRE_02B_MAGIC ||
 152                         ext2sb->s_magic == EXT2_SUPER_MAGIC))
 153                 {
 154                         printk("RAMDISK: Ext2 filesystem found at block %d\n",
 155                                 block);
 156                         nblocks = ext2sb->s_blocks_count;
 157                 }
 158 
 159                 if (nblocks == -1)
 160                 {
 161                         printk("RAMDISK: trying old-style RAM image.\n");
 162                         continue;
 163                 }
 164 
 165                 if (nblocks > (rd_length >> BLOCK_SIZE_BITS)) {
 166                         printk("RAMDISK: image too big! (%d/%d blocks)\n",
 167                                         nblocks, rd_length >> BLOCK_SIZE_BITS);
 168                         return;
 169                 }
 170                 printk("RAMDISK: Loading %d blocks into RAM disk", nblocks);
 171 
 172                 /* We found an image file system.  Load it into core! */
 173                 cp = rd_start;
 174                 while (nblocks) {
 175                         if (nblocks > 2) 
 176                                 bh = breada(ROOT_DEV, block, BLOCK_SIZE, 0,  PAGE_SIZE);
 177                         else
 178                                 bh = bread(ROOT_DEV, block, BLOCK_SIZE);
 179                         if (!bh) {
 180                                 printk("RAMDISK: I/O error on block %d, aborting!\n", 
 181                                 block);
 182                                 return;
 183                         }
 184                         (void) memcpy(cp, bh->b_data, BLOCK_SIZE);
 185                         brelse(bh);
 186                         if (!(nblocks-- & 15)) printk(".");
 187                         cp += BLOCK_SIZE;
 188                         block++;
 189                         i++;
 190                 }
 191                 printk("\ndone\n");
 192 
 193                 /* We loaded the file system image.  Prepare for mounting it. */
 194                 ROOT_DEV = ((MEM_MAJOR << 8) | RAMDISK_MINOR);
 195                 return;
 196         }
 197 }
 198 
 199 /*
 200  * If the root device is the RAM disk, try to load it.
 201  * In order to do this, the root device is originally set to the
 202  * floppy, and we later change it to be RAM disk.
 203  */
 204 void rd_load(void)
     /* [previous][next][first][last][top][bottom][index][help] */
 205 {
 206         struct inode inode;
 207         struct file filp;
 208 
 209         /* If no RAM disk specified, give up early. */
 210         if (!rd_length)
 211                 return;
 212         printk("RAMDISK: %d bytes, starting at 0x%x\n",
 213                         rd_length, (int) rd_start);
 214 
 215         /* If we are doing a diskette boot, we might have to pre-load it. */
 216         if (MAJOR(ROOT_DEV) != FLOPPY_MAJOR)
 217                 return;
 218 
 219         /* for Slackware install disks */
 220         printk(KERN_NOTICE "VFS: Insert ramdisk floppy and press ENTER\n");
 221         wait_for_keypress();
 222 
 223         memset(&filp, 0, sizeof(filp));
 224         memset(&inode, 0, sizeof(inode));
 225         inode.i_rdev = ROOT_DEV;
 226         filp.f_mode = 1; /* read only */
 227         filp.f_inode = &inode;
 228         if(blkdev_open(&inode, &filp) == 0 ){
 229                 do_load();
 230                 if(filp.f_op && filp.f_op->release)
 231                         filp.f_op->release(&inode,&filp);
 232         }
 233 }

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