Import 2.3.99pre10-3
[davej-history.git] / drivers / block / rd.c
blobb78d88ef7c43a99654d88ac275383f9727ce9b6d
1 /*
2 * ramdisk.c - Multiple RAM disk driver - gzip-loading version - v. 0.8 beta.
3 *
4 * (C) Chad Page, Theodore Ts'o, et. al, 1995.
6 * This RAM disk is designed to have filesystems created on it and mounted
7 * just like a regular floppy disk.
8 *
9 * It also does something suggested by Linus: use the buffer cache as the
10 * RAM disk data. This makes it possible to dynamically allocate the RAM disk
11 * buffer - with some consequences I have to deal with as I write this.
13 * This code is based on the original ramdisk.c, written mostly by
14 * Theodore Ts'o (TYT) in 1991. The code was largely rewritten by
15 * Chad Page to use the buffer cache to store the RAM disk data in
16 * 1995; Theodore then took over the driver again, and cleaned it up
17 * for inclusion in the mainline kernel.
19 * The original CRAMDISK code was written by Richard Lyons, and
20 * adapted by Chad Page to use the new RAM disk interface. Theodore
21 * Ts'o rewrote it so that both the compressed RAM disk loader and the
22 * kernel decompressor uses the same inflate.c codebase. The RAM disk
23 * loader now also loads into a dynamic (buffer cache based) RAM disk,
24 * not the old static RAM disk. Support for the old static RAM disk has
25 * been completely removed.
27 * Loadable module support added by Tom Dyas.
29 * Further cleanups by Chad Page (page0588@sundance.sjsu.edu):
30 * Cosmetic changes in #ifdef MODULE, code movement, etc.
31 * When the RAM disk module is removed, free the protected buffers
32 * Default RAM disk size changed to 2.88 MB
34 * Added initrd: Werner Almesberger & Hans Lermen, Feb '96
36 * 4/25/96 : Made RAM disk size a parameter (default is now 4 MB)
37 * - Chad Page
39 * Add support for fs images split across >1 disk, Paul Gortmaker, Mar '98
41 * Make block size and block size shift for RAM disks a global macro
42 * and set blk_size for -ENOSPC, Werner Fink <werner@suse.de>, Apr '99
45 #include <linux/config.h>
46 #include <linux/sched.h>
47 #include <linux/minix_fs.h>
48 #include <linux/ext2_fs.h>
49 #include <linux/romfs_fs.h>
50 #include <linux/fs.h>
51 #include <linux/kernel.h>
52 #include <linux/hdreg.h>
53 #include <linux/string.h>
54 #include <linux/mm.h>
55 #include <linux/mman.h>
56 #include <linux/malloc.h>
57 #include <linux/ioctl.h>
58 #include <linux/fd.h>
59 #include <linux/module.h>
60 #include <linux/init.h>
61 #include <linux/devfs_fs_kernel.h>
63 #include <asm/system.h>
64 #include <asm/uaccess.h>
65 #include <asm/byteorder.h>
67 extern void wait_for_keypress(void);
70 * 35 has been officially registered as the RAMDISK major number, but
71 * so is the original MAJOR number of 1. We're using 1 in
72 * include/linux/major.h for now
74 #define MAJOR_NR RAMDISK_MAJOR
75 #include <linux/blk.h>
76 #include <linux/blkpg.h>
78 /* The RAM disk size is now a parameter */
79 #define NUM_RAMDISKS 16 /* This cannot be overridden (yet) */
81 #ifndef MODULE
82 /* We don't have to load RAM disks or gunzip them in a module. */
83 #define RD_LOADER
84 #define BUILD_CRAMDISK
86 void rd_load(void);
87 static int crd_load(struct file *fp, struct file *outfp);
89 #ifdef CONFIG_BLK_DEV_INITRD
90 static int initrd_users;
91 #endif
92 #endif
94 /* Various static variables go here. Most are used only in the RAM disk code.
97 static unsigned long rd_length[NUM_RAMDISKS]; /* Size of RAM disks in bytes */
98 static int rd_hardsec[NUM_RAMDISKS]; /* Size of real blocks in bytes */
99 static int rd_blocksizes[NUM_RAMDISKS]; /* Size of 1024 byte blocks :) */
100 static int rd_kbsize[NUM_RAMDISKS]; /* Size in blocks of 1024 bytes */
101 static devfs_handle_t devfs_handle = NULL;
102 static struct inode *rd_inode[NUM_RAMDISKS]; /* Protected device inodes */
105 * Parameters for the boot-loading of the RAM disk. These are set by
106 * init/main.c (from arguments to the kernel command line) or from the
107 * architecture-specific setup routine (from the stored boot sector
108 * information).
110 int rd_size = 4096; /* Size of the RAM disks */
112 * It would be very desiderable to have a soft-blocksize (that in the case
113 * of the ramdisk driver is also the hardblocksize ;) of PAGE_SIZE because
114 * doing that we'll achieve a far better MM footprint. Using a rd_blocksize of
115 * BLOCK_SIZE in the worst case we'll make PAGE_SIZE/BLOCK_SIZE buffer-pages
116 * unfreeable. With a rd_blocksize of PAGE_SIZE instead we are sure that only
117 * 1 page will be protected. Depending on the size of the ramdisk you
118 * may want to change the ramdisk blocksize to achieve a better or worse MM
119 * behaviour. The default is still BLOCK_SIZE (needed by rd_load_image that
120 * supposes the filesystem in the image uses a BLOCK_SIZE blocksize).
122 int rd_blocksize = BLOCK_SIZE; /* Size of the RAM disks */
124 #ifndef MODULE
126 int rd_doload; /* 1 = load RAM disk, 0 = don't load */
127 int rd_prompt = 1; /* 1 = prompt for RAM disk, 0 = don't prompt */
128 int rd_image_start; /* starting block # of image */
129 #ifdef CONFIG_BLK_DEV_INITRD
130 unsigned long initrd_start, initrd_end;
131 int mount_initrd = 1; /* zero if initrd should not be mounted */
132 int initrd_below_start_ok;
134 static int __init no_initrd(char *str)
136 mount_initrd = 0;
137 return 1;
140 __setup("noinitrd", no_initrd);
142 #endif
144 static int __init ramdisk_start_setup(char *str)
146 rd_image_start = simple_strtol(str,NULL,0);
147 return 1;
150 static int __init load_ramdisk(char *str)
152 rd_doload = simple_strtol(str,NULL,0) & 3;
153 return 1;
156 static int __init prompt_ramdisk(char *str)
158 rd_prompt = simple_strtol(str,NULL,0) & 1;
159 return 1;
162 static int __init ramdisk_size(char *str)
164 rd_size = simple_strtol(str,NULL,0);
165 return 1;
168 static int __init ramdisk_size2(char *str)
170 return ramdisk_size(str);
173 static int __init ramdisk_blocksize(char *str)
175 rd_blocksize = simple_strtol(str,NULL,0);
176 return 1;
179 __setup("ramdisk_start=", ramdisk_start_setup);
180 __setup("load_ramdisk=", load_ramdisk);
181 __setup("prompt_ramdisk=", prompt_ramdisk);
182 __setup("ramdisk=", ramdisk_size);
183 __setup("ramdisk_size=", ramdisk_size2);
184 __setup("ramdisk_blocksize=", ramdisk_blocksize);
186 #endif
189 * Basically, my strategy here is to set up a buffer-head which can't be
190 * deleted, and make that my Ramdisk. If the request is outside of the
191 * allocated size, we must get rid of it...
193 * 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Added devfs support
196 static void rd_request(request_queue_t * q)
198 unsigned int minor;
199 unsigned long offset, len;
200 struct buffer_head *rbh;
201 struct buffer_head *sbh;
203 repeat:
204 INIT_REQUEST;
206 minor = MINOR(CURRENT->rq_dev);
208 if (minor >= NUM_RAMDISKS) {
209 end_request(0);
210 goto repeat;
213 offset = CURRENT->sector << 9;
214 len = CURRENT->current_nr_sectors << 9;
216 if ((offset + len) > rd_length[minor]) {
217 end_request(0);
218 goto repeat;
221 if ((CURRENT->cmd != READ) && (CURRENT->cmd != WRITE)) {
222 printk(KERN_INFO "RAMDISK: bad command: %d\n", CURRENT->cmd);
223 end_request(0);
224 goto repeat;
227 sbh = CURRENT->bh;
228 rbh = getblk(sbh->b_dev, sbh->b_blocknr, sbh->b_size);
229 if (CURRENT->cmd == READ) {
230 if (sbh != rbh)
231 memcpy(CURRENT->buffer, rbh->b_data, rbh->b_size);
232 } else
233 if (sbh != rbh)
234 memcpy(rbh->b_data, CURRENT->buffer, rbh->b_size);
235 mark_buffer_protected(rbh);
236 brelse(rbh);
238 end_request(1);
239 goto repeat;
242 static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
244 unsigned int minor;
246 if (!inode || !inode->i_rdev)
247 return -EINVAL;
249 minor = MINOR(inode->i_rdev);
251 switch (cmd) {
252 case BLKFLSBUF:
253 if (!capable(CAP_SYS_ADMIN))
254 return -EACCES;
255 /* special: we want to release the ramdisk memory,
256 it's not like with the other blockdevices where
257 this ioctl only flushes away the buffer cache. */
258 if ((atomic_read(&inode->i_bdev->bd_openers) > 2))
259 return -EBUSY;
260 destroy_buffers(inode->i_rdev);
261 rd_blocksizes[minor] = 0;
262 break;
264 case BLKGETSIZE: /* Return device size */
265 if (!arg) return -EINVAL;
266 return put_user(rd_kbsize[minor] << 1, (long *) arg);
268 case BLKROSET:
269 case BLKROGET:
270 case BLKSSZGET:
271 return blk_ioctl(inode->i_rdev, cmd, arg);
273 default:
274 return -EINVAL;
277 return 0;
281 #ifdef CONFIG_BLK_DEV_INITRD
283 static ssize_t initrd_read(struct file *file, char *buf,
284 size_t count, loff_t *ppos)
286 int left;
288 left = initrd_end - initrd_start - *ppos;
289 if (count > left) count = left;
290 if (count == 0) return 0;
291 copy_to_user(buf, (char *)initrd_start + *ppos, count);
292 *ppos += count;
293 return count;
297 static int initrd_release(struct inode *inode,struct file *file)
299 extern void free_initrd_mem(unsigned long, unsigned long);
301 if (--initrd_users) return 0;
302 blkdev_put(inode->i_bdev, BDEV_FILE);
303 iput(inode);
304 free_initrd_mem(initrd_start, initrd_end);
305 initrd_start = 0;
306 return 0;
310 static struct file_operations initrd_fops = {
311 read: initrd_read,
312 release: initrd_release,
315 #endif
318 static int rd_open(struct inode * inode, struct file * filp)
320 #ifdef CONFIG_BLK_DEV_INITRD
321 if (DEVICE_NR(inode->i_rdev) == INITRD_MINOR) {
322 if (!initrd_start) return -ENODEV;
323 initrd_users++;
324 filp->f_op = &initrd_fops;
325 return 0;
327 #endif
329 if (DEVICE_NR(inode->i_rdev) >= NUM_RAMDISKS)
330 return -ENXIO;
333 * Immunize device against invalidate_buffers() and prune_icache().
335 if (rd_inode[DEVICE_NR(inode->i_rdev)] == NULL) {
336 if (!inode->i_bdev) return -ENXIO;
337 if ((rd_inode[DEVICE_NR(inode->i_rdev)] = igrab(inode)) != NULL)
338 atomic_inc(&rd_inode[DEVICE_NR(inode->i_rdev)]->i_bdev->bd_openers);
341 MOD_INC_USE_COUNT;
343 return 0;
346 static int rd_release(struct inode * inode, struct file * filp)
348 MOD_DEC_USE_COUNT;
349 return 0;
352 static struct block_device_operations fd_fops = {
353 open: rd_open,
354 release: rd_release,
355 ioctl: rd_ioctl,
358 #ifdef MODULE
359 /* Before freeing the module, invalidate all of the protected buffers! */
360 static void __exit rd_cleanup (void)
362 int i;
364 for (i = 0 ; i < NUM_RAMDISKS; i++) {
365 if (rd_inode[i]) {
366 /* withdraw invalidate_buffers() and prune_icache() immunity */
367 atomic_dec(&rd_inode[i]->i_bdev->bd_openers);
368 /* remove stale pointer to module address space */
369 rd_inode[i]->i_bdev->bd_op = NULL;
370 iput(rd_inode[i]);
372 destroy_buffers(MKDEV(MAJOR_NR, i));
375 devfs_unregister (devfs_handle);
376 unregister_blkdev( MAJOR_NR, "ramdisk" );
377 blk_cleanup_queue(BLK_DEFAULT_QUEUE(MAJOR_NR));
378 hardsect_size[MAJOR_NR] = NULL;
379 blksize_size[MAJOR_NR] = NULL;
380 blk_size[MAJOR_NR] = NULL;
382 #endif
384 /* This is the registration and initialization section of the RAM disk driver */
385 int __init rd_init (void)
387 int i;
389 if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
390 (rd_blocksize & (rd_blocksize-1)))
392 printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
393 rd_blocksize);
394 rd_blocksize = BLOCK_SIZE;
397 if (register_blkdev(MAJOR_NR, "ramdisk", &fd_fops)) {
398 printk("RAMDISK: Could not get major %d", MAJOR_NR);
399 return -EIO;
402 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), &rd_request);
404 for (i = 0; i < NUM_RAMDISKS; i++) {
405 /* rd_size is given in kB */
406 rd_length[i] = rd_size << 10;
407 rd_hardsec[i] = rd_blocksize;
408 rd_blocksizes[i] = rd_blocksize;
409 rd_kbsize[i] = rd_size;
411 devfs_handle = devfs_mk_dir (NULL, "rd", 0, NULL);
412 devfs_register_series (devfs_handle, "%u", NUM_RAMDISKS,
413 DEVFS_FL_DEFAULT, MAJOR_NR, 0,
414 S_IFBLK | S_IRUSR | S_IWUSR, 0, 0,
415 &fd_fops, NULL);
417 for (i = 0; i < NUM_RAMDISKS; i++)
418 register_disk(NULL, MKDEV(MAJOR_NR,i), 1, &fd_fops, rd_size<<1);
420 #ifdef CONFIG_BLK_DEV_INITRD
421 /* We ought to separate initrd operations here */
422 register_disk(NULL, MKDEV(MAJOR_NR,INITRD_MINOR), 1, &fd_fops, rd_size<<1);
423 #endif
425 hardsect_size[MAJOR_NR] = rd_hardsec; /* Size of the RAM disk blocks */
426 blksize_size[MAJOR_NR] = rd_blocksizes; /* Avoid set_blocksize() check */
427 blk_size[MAJOR_NR] = rd_kbsize; /* Size of the RAM disk in kB */
429 /* rd_size is given in kB */
430 printk("RAMDISK driver initialized: "
431 "%d RAM disks of %dK size %d blocksize\n",
432 NUM_RAMDISKS, rd_size, rd_blocksize);
434 return 0;
437 #ifdef MODULE
438 module_init(rd_init);
439 #endif
440 module_exit(rd_cleanup);
442 /* loadable module support */
443 MODULE_PARM (rd_size, "1i");
444 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
445 MODULE_PARM (rd_blocksize, "i");
446 MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
448 /* End of non-loading portions of the RAM disk driver */
450 #ifdef RD_LOADER
452 * This routine tries to find a RAM disk image to load, and returns the
453 * number of blocks to read for a non-compressed image, 0 if the image
454 * is a compressed image, and -1 if an image with the right magic
455 * numbers could not be found.
457 * We currently check for the following magic numbers:
458 * minix
459 * ext2
460 * romfs
461 * gzip
463 int __init
464 identify_ramdisk_image(kdev_t device, struct file *fp, int start_block)
466 const int size = 512;
467 struct minix_super_block *minixsb;
468 struct ext2_super_block *ext2sb;
469 struct romfs_super_block *romfsb;
470 int nblocks = -1;
471 unsigned char *buf;
473 buf = kmalloc(size, GFP_KERNEL);
474 if (buf == 0)
475 return -1;
477 minixsb = (struct minix_super_block *) buf;
478 ext2sb = (struct ext2_super_block *) buf;
479 romfsb = (struct romfs_super_block *) buf;
480 memset(buf, 0xe5, size);
483 * Read block 0 to test for gzipped kernel
485 if (fp->f_op->llseek)
486 fp->f_op->llseek(fp, start_block * BLOCK_SIZE, 0);
487 fp->f_pos = start_block * BLOCK_SIZE;
489 fp->f_op->read(fp, buf, size, &fp->f_pos);
492 * If it matches the gzip magic numbers, return -1
494 if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
495 printk(KERN_NOTICE
496 "RAMDISK: Compressed image found at block %d\n",
497 start_block);
498 nblocks = 0;
499 goto done;
502 /* romfs is at block zero too */
503 if (romfsb->word0 == ROMSB_WORD0 &&
504 romfsb->word1 == ROMSB_WORD1) {
505 printk(KERN_NOTICE
506 "RAMDISK: romfs filesystem found at block %d\n",
507 start_block);
508 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
509 goto done;
513 * Read block 1 to test for minix and ext2 superblock
515 if (fp->f_op->llseek)
516 fp->f_op->llseek(fp, (start_block+1) * BLOCK_SIZE, 0);
517 fp->f_pos = (start_block+1) * BLOCK_SIZE;
519 fp->f_op->read(fp, buf, size, &fp->f_pos);
521 /* Try minix */
522 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
523 minixsb->s_magic == MINIX_SUPER_MAGIC2) {
524 printk(KERN_NOTICE
525 "RAMDISK: Minix filesystem found at block %d\n",
526 start_block);
527 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
528 goto done;
531 /* Try ext2 */
532 if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
533 printk(KERN_NOTICE
534 "RAMDISK: ext2 filesystem found at block %d\n",
535 start_block);
536 nblocks = le32_to_cpu(ext2sb->s_blocks_count);
537 goto done;
540 printk(KERN_NOTICE
541 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
542 start_block);
544 done:
545 if (fp->f_op->llseek)
546 fp->f_op->llseek(fp, start_block * BLOCK_SIZE, 0);
547 fp->f_pos = start_block * BLOCK_SIZE;
549 kfree(buf);
550 return nblocks;
554 * This routine loads in the RAM disk image.
556 static void __init rd_load_image(kdev_t device, int offset, int unit)
558 struct inode *inode, *out_inode;
559 struct file infile, outfile;
560 struct dentry in_dentry, out_dentry;
561 mm_segment_t fs;
562 kdev_t ram_device;
563 int nblocks, i;
564 char *buf;
565 unsigned short rotate = 0;
566 unsigned short devblocks = 0;
567 char rotator[4] = { '|' , '/' , '-' , '\\' };
569 ram_device = MKDEV(MAJOR_NR, unit);
571 if ((inode = get_empty_inode()) == NULL)
572 return;
573 memset(&infile, 0, sizeof(infile));
574 memset(&in_dentry, 0, sizeof(in_dentry));
575 infile.f_mode = 1; /* read only */
576 infile.f_dentry = &in_dentry;
577 in_dentry.d_inode = inode;
578 infile.f_op = &def_blk_fops;
579 init_special_inode(inode, S_IFBLK | S_IRUSR, kdev_t_to_nr(device));
581 if ((out_inode = get_empty_inode()) == NULL)
582 goto free_inode;
583 memset(&outfile, 0, sizeof(outfile));
584 memset(&out_dentry, 0, sizeof(out_dentry));
585 outfile.f_mode = 3; /* read/write */
586 outfile.f_dentry = &out_dentry;
587 out_dentry.d_inode = out_inode;
588 outfile.f_op = &def_blk_fops;
589 init_special_inode(out_inode, S_IFBLK | S_IRUSR | S_IWUSR, kdev_t_to_nr(ram_device));
591 if (blkdev_open(inode, &infile) != 0)
592 goto free_inode;
593 if (blkdev_open(out_inode, &outfile) != 0)
594 goto free_inodes;
596 fs = get_fs();
597 set_fs(KERNEL_DS);
599 nblocks = identify_ramdisk_image(device, &infile, offset);
600 if (nblocks < 0)
601 goto done;
603 if (nblocks == 0) {
604 #ifdef BUILD_CRAMDISK
605 if (crd_load(&infile, &outfile) == 0)
606 goto successful_load;
607 #else
608 printk(KERN_NOTICE
609 "RAMDISK: Kernel does not support compressed "
610 "RAM disk images\n");
611 #endif
612 goto done;
616 * NOTE NOTE: nblocks suppose that the blocksize is BLOCK_SIZE, so
617 * rd_load_image will work only with filesystem BLOCK_SIZE wide!
618 * So make sure to use 1k blocksize while generating ext2fs
619 * ramdisk-images.
621 if (nblocks > (rd_length[unit] >> BLOCK_SIZE_BITS)) {
622 printk("RAMDISK: image too big! (%d/%ld blocks)\n",
623 nblocks, rd_length[unit] >> BLOCK_SIZE_BITS);
624 goto done;
628 * OK, time to copy in the data
630 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
631 if (buf == 0) {
632 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
633 goto done;
636 if (blk_size[MAJOR(device)])
637 devblocks = blk_size[MAJOR(device)][MINOR(device)];
639 #ifdef CONFIG_BLK_DEV_INITRD
640 if (MAJOR(device) == MAJOR_NR && MINOR(device) == INITRD_MINOR)
641 devblocks = nblocks;
642 #endif
644 if (devblocks == 0) {
645 printk(KERN_ERR "RAMDISK: could not determine device size\n");
646 goto done;
649 printk(KERN_NOTICE "RAMDISK: Loading %d blocks [%d disk%s] into ram disk... ",
650 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
651 for (i=0; i < nblocks; i++) {
652 if (i && (i % devblocks == 0)) {
653 printk("done disk #%d.\n", i/devblocks);
654 rotate = 0;
655 invalidate_buffers(device);
656 if (infile.f_op->release)
657 infile.f_op->release(inode, &infile);
658 printk("Please insert disk #%d and press ENTER\n", i/devblocks+1);
659 wait_for_keypress();
660 if (blkdev_open(inode, &infile) != 0) {
661 printk("Error opening disk.\n");
662 goto done;
664 infile.f_pos = 0;
665 printk("Loading disk #%d... ", i/devblocks+1);
667 infile.f_op->read(&infile, buf, BLOCK_SIZE, &infile.f_pos);
668 outfile.f_op->write(&outfile, buf, BLOCK_SIZE, &outfile.f_pos);
669 #if !defined(CONFIG_ARCH_S390)
670 if (!(i % 16)) {
671 printk("%c\b", rotator[rotate & 0x3]);
672 rotate++;
674 #endif
676 printk("done.\n");
677 kfree(buf);
679 successful_load:
680 invalidate_buffers(device);
681 ROOT_DEV = MKDEV(MAJOR_NR, unit);
682 if (ROOT_DEVICE_NAME != NULL) strcpy (ROOT_DEVICE_NAME, "rd/0");
684 done:
685 if (infile.f_op->release)
686 infile.f_op->release(inode, &infile);
687 set_fs(fs);
688 return;
689 free_inodes: /* free inodes on error */
690 iput(out_inode);
691 blkdev_put(inode->i_bdev, BDEV_FILE);
692 free_inode:
693 iput(inode);
697 static void __init rd_load_disk(int n)
699 #ifdef CONFIG_BLK_DEV_INITRD
700 extern kdev_t real_root_dev;
701 #endif
703 if (rd_doload == 0)
704 return;
706 if (MAJOR(ROOT_DEV) != FLOPPY_MAJOR
707 #ifdef CONFIG_BLK_DEV_INITRD
708 && MAJOR(real_root_dev) != FLOPPY_MAJOR
709 #endif
711 return;
713 if (rd_prompt) {
714 #ifdef CONFIG_BLK_DEV_FD
715 floppy_eject();
716 #endif
717 printk(KERN_NOTICE
718 "VFS: Insert root floppy disk to be loaded into RAM disk and press ENTER\n");
719 wait_for_keypress();
722 rd_load_image(ROOT_DEV,rd_image_start, n);
726 void __init rd_load(void)
728 rd_load_disk(0);
731 void __init rd_load_secondary(void)
733 rd_load_disk(1);
736 #ifdef CONFIG_BLK_DEV_INITRD
737 void __init initrd_load(void)
739 rd_load_image(MKDEV(MAJOR_NR, INITRD_MINOR),rd_image_start,0);
741 #endif
743 #endif /* RD_LOADER */
745 #ifdef BUILD_CRAMDISK
748 * gzip declarations
751 #define OF(args) args
753 #ifndef memzero
754 #define memzero(s, n) memset ((s), 0, (n))
755 #endif
757 typedef unsigned char uch;
758 typedef unsigned short ush;
759 typedef unsigned long ulg;
761 #define INBUFSIZ 4096
762 #define WSIZE 0x8000 /* window size--must be a power of two, and */
763 /* at least 32K for zip's deflate method */
765 static uch *inbuf;
766 static uch *window;
768 static unsigned insize = 0; /* valid bytes in inbuf */
769 static unsigned inptr = 0; /* index of next byte to be processed in inbuf */
770 static unsigned outcnt = 0; /* bytes in output buffer */
771 static int exit_code = 0;
772 static long bytes_out = 0;
773 static struct file *crd_infp, *crd_outfp;
775 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
777 /* Diagnostic functions (stubbed out) */
778 #define Assert(cond,msg)
779 #define Trace(x)
780 #define Tracev(x)
781 #define Tracevv(x)
782 #define Tracec(c,x)
783 #define Tracecv(c,x)
785 #define STATIC static
787 static int fill_inbuf(void);
788 static void flush_window(void);
789 static void *malloc(int size);
790 static void free(void *where);
791 static void error(char *m);
792 static void gzip_mark(void **);
793 static void gzip_release(void **);
795 #include "../../lib/inflate.c"
797 static void __init *malloc(int size)
799 return kmalloc(size, GFP_KERNEL);
802 static void __init free(void *where)
804 kfree(where);
807 static void __init gzip_mark(void **ptr)
811 static void __init gzip_release(void **ptr)
816 /* ===========================================================================
817 * Fill the input buffer. This is called only when the buffer is empty
818 * and at least one byte is really needed.
820 static int __init fill_inbuf(void)
822 if (exit_code) return -1;
824 insize = crd_infp->f_op->read(crd_infp, inbuf, INBUFSIZ,
825 &crd_infp->f_pos);
826 if (insize == 0) return -1;
828 inptr = 1;
830 return inbuf[0];
833 /* ===========================================================================
834 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
835 * (Used for the decompressed data only.)
837 static void __init flush_window(void)
839 ulg c = crc; /* temporary variable */
840 unsigned n;
841 uch *in, ch;
843 crd_outfp->f_op->write(crd_outfp, window, outcnt, &crd_outfp->f_pos);
844 in = window;
845 for (n = 0; n < outcnt; n++) {
846 ch = *in++;
847 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
849 crc = c;
850 bytes_out += (ulg)outcnt;
851 outcnt = 0;
854 static void __init error(char *x)
856 printk(KERN_ERR "%s", x);
857 exit_code = 1;
860 static int __init
861 crd_load(struct file * fp, struct file *outfp)
863 int result;
865 insize = 0; /* valid bytes in inbuf */
866 inptr = 0; /* index of next byte to be processed in inbuf */
867 outcnt = 0; /* bytes in output buffer */
868 exit_code = 0;
869 bytes_out = 0;
870 crc = (ulg)0xffffffffL; /* shift register contents */
872 crd_infp = fp;
873 crd_outfp = outfp;
874 inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
875 if (inbuf == 0) {
876 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
877 return -1;
879 window = kmalloc(WSIZE, GFP_KERNEL);
880 if (window == 0) {
881 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
882 kfree(inbuf);
883 return -1;
885 makecrc();
886 result = gunzip();
887 kfree(inbuf);
888 kfree(window);
889 return result;
892 #endif /* BUILD_CRAMDISK */