More Makefile cleanups, otherwise mainly noticeable are the netfilter fix
[davej-history.git] / drivers / block / rd.c
blob1799ccef536eed59611461bf9190804ed5d77b7a
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>
62 #include <linux/smp_lock.h>
64 #include <asm/system.h>
65 #include <asm/uaccess.h>
66 #include <asm/byteorder.h>
68 extern void wait_for_keypress(void);
71 * 35 has been officially registered as the RAMDISK major number, but
72 * so is the original MAJOR number of 1. We're using 1 in
73 * include/linux/major.h for now
75 #define MAJOR_NR RAMDISK_MAJOR
76 #include <linux/blk.h>
77 #include <linux/blkpg.h>
79 /* The RAM disk size is now a parameter */
80 #define NUM_RAMDISKS 16 /* This cannot be overridden (yet) */
82 #ifndef MODULE
83 /* We don't have to load RAM disks or gunzip them in a module. */
84 #define RD_LOADER
85 #define BUILD_CRAMDISK
87 void rd_load(void);
88 static int crd_load(struct file *fp, struct file *outfp);
90 #ifdef CONFIG_BLK_DEV_INITRD
91 static int initrd_users;
92 #endif
93 #endif
95 /* Various static variables go here. Most are used only in the RAM disk code.
98 static unsigned long rd_length[NUM_RAMDISKS]; /* Size of RAM disks in bytes */
99 static int rd_hardsec[NUM_RAMDISKS]; /* Size of real blocks in bytes */
100 static int rd_blocksizes[NUM_RAMDISKS]; /* Size of 1024 byte blocks :) */
101 static int rd_kbsize[NUM_RAMDISKS]; /* Size in blocks of 1024 bytes */
102 static devfs_handle_t devfs_handle;
103 static struct inode *rd_inode[NUM_RAMDISKS]; /* Protected device inodes */
106 * Parameters for the boot-loading of the RAM disk. These are set by
107 * init/main.c (from arguments to the kernel command line) or from the
108 * architecture-specific setup routine (from the stored boot sector
109 * information).
111 int rd_size = CONFIG_BLK_DEV_RAM_SIZE; /* Size of the RAM disks */
113 * It would be very desiderable to have a soft-blocksize (that in the case
114 * of the ramdisk driver is also the hardblocksize ;) of PAGE_SIZE because
115 * doing that we'll achieve a far better MM footprint. Using a rd_blocksize of
116 * BLOCK_SIZE in the worst case we'll make PAGE_SIZE/BLOCK_SIZE buffer-pages
117 * unfreeable. With a rd_blocksize of PAGE_SIZE instead we are sure that only
118 * 1 page will be protected. Depending on the size of the ramdisk you
119 * may want to change the ramdisk blocksize to achieve a better or worse MM
120 * behaviour. The default is still BLOCK_SIZE (needed by rd_load_image that
121 * supposes the filesystem in the image uses a BLOCK_SIZE blocksize).
123 int rd_blocksize = BLOCK_SIZE; /* blocksize of the RAM disks */
125 #ifndef MODULE
127 int rd_doload; /* 1 = load RAM disk, 0 = don't load */
128 int rd_prompt = 1; /* 1 = prompt for RAM disk, 0 = don't prompt */
129 int rd_image_start; /* starting block # of image */
130 #ifdef CONFIG_BLK_DEV_INITRD
131 unsigned long initrd_start, initrd_end;
132 int mount_initrd = 1; /* zero if initrd should not be mounted */
133 int initrd_below_start_ok;
135 static int __init no_initrd(char *str)
137 mount_initrd = 0;
138 return 1;
141 __setup("noinitrd", no_initrd);
143 #endif
145 static int __init ramdisk_start_setup(char *str)
147 rd_image_start = simple_strtol(str,NULL,0);
148 return 1;
151 static int __init load_ramdisk(char *str)
153 rd_doload = simple_strtol(str,NULL,0) & 3;
154 return 1;
157 static int __init prompt_ramdisk(char *str)
159 rd_prompt = simple_strtol(str,NULL,0) & 1;
160 return 1;
163 static int __init ramdisk_size(char *str)
165 rd_size = simple_strtol(str,NULL,0);
166 return 1;
169 static int __init ramdisk_size2(char *str)
171 return ramdisk_size(str);
174 static int __init ramdisk_blocksize(char *str)
176 rd_blocksize = simple_strtol(str,NULL,0);
177 return 1;
180 __setup("ramdisk_start=", ramdisk_start_setup);
181 __setup("load_ramdisk=", load_ramdisk);
182 __setup("prompt_ramdisk=", prompt_ramdisk);
183 __setup("ramdisk=", ramdisk_size);
184 __setup("ramdisk_size=", ramdisk_size2);
185 __setup("ramdisk_blocksize=", ramdisk_blocksize);
187 #endif
190 * Basically, my strategy here is to set up a buffer-head which can't be
191 * deleted, and make that my Ramdisk. If the request is outside of the
192 * allocated size, we must get rid of it...
194 * 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Added devfs support
197 static int rd_make_request(request_queue_t * q, int rw, struct buffer_head *sbh)
199 unsigned int minor;
200 unsigned long offset, len;
201 struct buffer_head *rbh;
202 char *bdata;
205 minor = MINOR(sbh->b_rdev);
207 if (minor >= NUM_RAMDISKS)
208 goto fail;
211 offset = sbh->b_rsector << 9;
212 len = sbh->b_size;
214 if ((offset + len) > rd_length[minor])
215 goto fail;
217 if (rw==READA)
218 rw=READ;
219 if ((rw != READ) && (rw != WRITE)) {
220 printk(KERN_INFO "RAMDISK: bad command: %d\n", rw);
221 goto fail;
224 rbh = getblk(sbh->b_rdev, sbh->b_rsector/(sbh->b_size>>9), sbh->b_size);
225 /* I think that it is safe to assume that rbh is not in HighMem, though
226 * sbh might be - NeilBrown
228 bdata = bh_kmap(sbh);
229 if (rw == READ) {
230 if (sbh != rbh)
231 memcpy(bdata, rbh->b_data, rbh->b_size);
232 } else
233 if (sbh != rbh)
234 memcpy(rbh->b_data, bdata, rbh->b_size);
235 bh_kunmap(sbh);
236 mark_buffer_protected(rbh);
237 brelse(rbh);
239 sbh->b_end_io(sbh,1);
240 return 0;
241 fail:
242 sbh->b_end_io(sbh,0);
243 return 0;
246 static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
248 unsigned int minor;
250 if (!inode || !inode->i_rdev)
251 return -EINVAL;
253 minor = MINOR(inode->i_rdev);
255 switch (cmd) {
256 case BLKFLSBUF:
257 if (!capable(CAP_SYS_ADMIN))
258 return -EACCES;
259 /* special: we want to release the ramdisk memory,
260 it's not like with the other blockdevices where
261 this ioctl only flushes away the buffer cache. */
262 if ((atomic_read(&inode->i_bdev->bd_openers) > 2))
263 return -EBUSY;
264 destroy_buffers(inode->i_rdev);
265 rd_blocksizes[minor] = 0;
266 break;
268 case BLKGETSIZE: /* Return device size */
269 if (!arg) return -EINVAL;
270 return put_user(rd_kbsize[minor] << 1, (long *) arg);
272 case BLKROSET:
273 case BLKROGET:
274 case BLKSSZGET:
275 return blk_ioctl(inode->i_rdev, cmd, arg);
277 default:
278 return -EINVAL;
281 return 0;
285 #ifdef CONFIG_BLK_DEV_INITRD
287 static ssize_t initrd_read(struct file *file, char *buf,
288 size_t count, loff_t *ppos)
290 int left;
292 left = initrd_end - initrd_start - *ppos;
293 if (count > left) count = left;
294 if (count == 0) return 0;
295 copy_to_user(buf, (char *)initrd_start + *ppos, count);
296 *ppos += count;
297 return count;
301 static int initrd_release(struct inode *inode,struct file *file)
303 extern void free_initrd_mem(unsigned long, unsigned long);
305 lock_kernel();
306 if (!--initrd_users) {
307 blkdev_put(inode->i_bdev, BDEV_FILE);
308 iput(inode);
309 free_initrd_mem(initrd_start, initrd_end);
310 initrd_start = 0;
312 unlock_kernel();
313 return 0;
317 static struct file_operations initrd_fops = {
318 read: initrd_read,
319 release: initrd_release,
322 #endif
325 static int rd_open(struct inode * inode, struct file * filp)
327 #ifdef CONFIG_BLK_DEV_INITRD
328 if (DEVICE_NR(inode->i_rdev) == INITRD_MINOR) {
329 if (!initrd_start) return -ENODEV;
330 initrd_users++;
331 filp->f_op = &initrd_fops;
332 return 0;
334 #endif
336 if (DEVICE_NR(inode->i_rdev) >= NUM_RAMDISKS)
337 return -ENXIO;
340 * Immunize device against invalidate_buffers() and prune_icache().
342 if (rd_inode[DEVICE_NR(inode->i_rdev)] == NULL) {
343 if (!inode->i_bdev) return -ENXIO;
344 if ((rd_inode[DEVICE_NR(inode->i_rdev)] = igrab(inode)) != NULL)
345 atomic_inc(&rd_inode[DEVICE_NR(inode->i_rdev)]->i_bdev->bd_openers);
348 MOD_INC_USE_COUNT;
350 return 0;
353 static int rd_release(struct inode * inode, struct file * filp)
355 MOD_DEC_USE_COUNT;
356 return 0;
359 static struct block_device_operations fd_fops = {
360 open: rd_open,
361 release: rd_release,
362 ioctl: rd_ioctl,
365 #ifdef MODULE
366 /* Before freeing the module, invalidate all of the protected buffers! */
367 static void __exit rd_cleanup (void)
369 int i;
371 for (i = 0 ; i < NUM_RAMDISKS; i++) {
372 if (rd_inode[i]) {
373 /* withdraw invalidate_buffers() and prune_icache() immunity */
374 atomic_dec(&rd_inode[i]->i_bdev->bd_openers);
375 /* remove stale pointer to module address space */
376 rd_inode[i]->i_bdev->bd_op = NULL;
377 iput(rd_inode[i]);
379 destroy_buffers(MKDEV(MAJOR_NR, i));
382 devfs_unregister (devfs_handle);
383 unregister_blkdev( MAJOR_NR, "ramdisk" );
384 hardsect_size[MAJOR_NR] = NULL;
385 blksize_size[MAJOR_NR] = NULL;
386 blk_size[MAJOR_NR] = NULL;
388 #endif
390 /* This is the registration and initialization section of the RAM disk driver */
391 int __init rd_init (void)
393 int i;
395 if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
396 (rd_blocksize & (rd_blocksize-1)))
398 printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
399 rd_blocksize);
400 rd_blocksize = BLOCK_SIZE;
403 if (register_blkdev(MAJOR_NR, "ramdisk", &fd_fops)) {
404 printk("RAMDISK: Could not get major %d", MAJOR_NR);
405 return -EIO;
408 blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), &rd_make_request);
410 for (i = 0; i < NUM_RAMDISKS; i++) {
411 /* rd_size is given in kB */
412 rd_length[i] = rd_size << 10;
413 rd_hardsec[i] = rd_blocksize;
414 rd_blocksizes[i] = rd_blocksize;
415 rd_kbsize[i] = rd_size;
417 devfs_handle = devfs_mk_dir (NULL, "rd", NULL);
418 devfs_register_series (devfs_handle, "%u", NUM_RAMDISKS,
419 DEVFS_FL_DEFAULT, MAJOR_NR, 0,
420 S_IFBLK | S_IRUSR | S_IWUSR,
421 &fd_fops, NULL);
423 for (i = 0; i < NUM_RAMDISKS; i++)
424 register_disk(NULL, MKDEV(MAJOR_NR,i), 1, &fd_fops, rd_size<<1);
426 #ifdef CONFIG_BLK_DEV_INITRD
427 /* We ought to separate initrd operations here */
428 register_disk(NULL, MKDEV(MAJOR_NR,INITRD_MINOR), 1, &fd_fops, rd_size<<1);
429 #endif
431 hardsect_size[MAJOR_NR] = rd_hardsec; /* Size of the RAM disk blocks */
432 blksize_size[MAJOR_NR] = rd_blocksizes; /* Avoid set_blocksize() check */
433 blk_size[MAJOR_NR] = rd_kbsize; /* Size of the RAM disk in kB */
435 /* rd_size is given in kB */
436 printk("RAMDISK driver initialized: "
437 "%d RAM disks of %dK size %d blocksize\n",
438 NUM_RAMDISKS, rd_size, rd_blocksize);
440 return 0;
443 #ifdef MODULE
444 module_init(rd_init);
445 module_exit(rd_cleanup);
446 #endif
448 /* loadable module support */
449 MODULE_PARM (rd_size, "1i");
450 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
451 MODULE_PARM (rd_blocksize, "i");
452 MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
454 /* End of non-loading portions of the RAM disk driver */
456 #ifdef RD_LOADER
458 * This routine tries to find a RAM disk image to load, and returns the
459 * number of blocks to read for a non-compressed image, 0 if the image
460 * is a compressed image, and -1 if an image with the right magic
461 * numbers could not be found.
463 * We currently check for the following magic numbers:
464 * minix
465 * ext2
466 * romfs
467 * gzip
469 int __init
470 identify_ramdisk_image(kdev_t device, struct file *fp, int start_block)
472 const int size = 512;
473 struct minix_super_block *minixsb;
474 struct ext2_super_block *ext2sb;
475 struct romfs_super_block *romfsb;
476 int nblocks = -1;
477 unsigned char *buf;
479 buf = kmalloc(size, GFP_KERNEL);
480 if (buf == 0)
481 return -1;
483 minixsb = (struct minix_super_block *) buf;
484 ext2sb = (struct ext2_super_block *) buf;
485 romfsb = (struct romfs_super_block *) buf;
486 memset(buf, 0xe5, size);
489 * Read block 0 to test for gzipped kernel
491 if (fp->f_op->llseek)
492 fp->f_op->llseek(fp, start_block * BLOCK_SIZE, 0);
493 fp->f_pos = start_block * BLOCK_SIZE;
495 fp->f_op->read(fp, buf, size, &fp->f_pos);
498 * If it matches the gzip magic numbers, return -1
500 if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
501 printk(KERN_NOTICE
502 "RAMDISK: Compressed image found at block %d\n",
503 start_block);
504 nblocks = 0;
505 goto done;
508 /* romfs is at block zero too */
509 if (romfsb->word0 == ROMSB_WORD0 &&
510 romfsb->word1 == ROMSB_WORD1) {
511 printk(KERN_NOTICE
512 "RAMDISK: romfs filesystem found at block %d\n",
513 start_block);
514 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
515 goto done;
519 * Read block 1 to test for minix and ext2 superblock
521 if (fp->f_op->llseek)
522 fp->f_op->llseek(fp, (start_block+1) * BLOCK_SIZE, 0);
523 fp->f_pos = (start_block+1) * BLOCK_SIZE;
525 fp->f_op->read(fp, buf, size, &fp->f_pos);
527 /* Try minix */
528 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
529 minixsb->s_magic == MINIX_SUPER_MAGIC2) {
530 printk(KERN_NOTICE
531 "RAMDISK: Minix filesystem found at block %d\n",
532 start_block);
533 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
534 goto done;
537 /* Try ext2 */
538 if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
539 printk(KERN_NOTICE
540 "RAMDISK: ext2 filesystem found at block %d\n",
541 start_block);
542 nblocks = le32_to_cpu(ext2sb->s_blocks_count);
543 goto done;
546 printk(KERN_NOTICE
547 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
548 start_block);
550 done:
551 if (fp->f_op->llseek)
552 fp->f_op->llseek(fp, start_block * BLOCK_SIZE, 0);
553 fp->f_pos = start_block * BLOCK_SIZE;
555 kfree(buf);
556 return nblocks;
560 * This routine loads in the RAM disk image.
562 static void __init rd_load_image(kdev_t device, int offset, int unit)
564 struct inode *inode, *out_inode;
565 struct file infile, outfile;
566 struct dentry in_dentry, out_dentry;
567 mm_segment_t fs;
568 kdev_t ram_device;
569 int nblocks, i;
570 char *buf;
571 unsigned short rotate = 0;
572 unsigned short devblocks = 0;
573 char rotator[4] = { '|' , '/' , '-' , '\\' };
575 ram_device = MKDEV(MAJOR_NR, unit);
577 if ((inode = get_empty_inode()) == NULL)
578 return;
579 memset(&infile, 0, sizeof(infile));
580 memset(&in_dentry, 0, sizeof(in_dentry));
581 infile.f_mode = 1; /* read only */
582 infile.f_dentry = &in_dentry;
583 in_dentry.d_inode = inode;
584 infile.f_op = &def_blk_fops;
585 init_special_inode(inode, S_IFBLK | S_IRUSR, kdev_t_to_nr(device));
587 if ((out_inode = get_empty_inode()) == NULL)
588 goto free_inode;
589 memset(&outfile, 0, sizeof(outfile));
590 memset(&out_dentry, 0, sizeof(out_dentry));
591 outfile.f_mode = 3; /* read/write */
592 outfile.f_dentry = &out_dentry;
593 out_dentry.d_inode = out_inode;
594 outfile.f_op = &def_blk_fops;
595 init_special_inode(out_inode, S_IFBLK | S_IRUSR | S_IWUSR, kdev_t_to_nr(ram_device));
597 if (blkdev_open(inode, &infile) != 0)
598 goto free_inode;
599 if (blkdev_open(out_inode, &outfile) != 0)
600 goto free_inodes;
602 fs = get_fs();
603 set_fs(KERNEL_DS);
605 nblocks = identify_ramdisk_image(device, &infile, offset);
606 if (nblocks < 0)
607 goto done;
609 if (nblocks == 0) {
610 #ifdef BUILD_CRAMDISK
611 if (crd_load(&infile, &outfile) == 0)
612 goto successful_load;
613 #else
614 printk(KERN_NOTICE
615 "RAMDISK: Kernel does not support compressed "
616 "RAM disk images\n");
617 #endif
618 goto done;
622 * NOTE NOTE: nblocks suppose that the blocksize is BLOCK_SIZE, so
623 * rd_load_image will work only with filesystem BLOCK_SIZE wide!
624 * So make sure to use 1k blocksize while generating ext2fs
625 * ramdisk-images.
627 if (nblocks > (rd_length[unit] >> BLOCK_SIZE_BITS)) {
628 printk("RAMDISK: image too big! (%d/%ld blocks)\n",
629 nblocks, rd_length[unit] >> BLOCK_SIZE_BITS);
630 goto done;
634 * OK, time to copy in the data
636 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
637 if (buf == 0) {
638 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
639 goto done;
642 if (blk_size[MAJOR(device)])
643 devblocks = blk_size[MAJOR(device)][MINOR(device)];
645 #ifdef CONFIG_BLK_DEV_INITRD
646 if (MAJOR(device) == MAJOR_NR && MINOR(device) == INITRD_MINOR)
647 devblocks = nblocks;
648 #endif
650 if (devblocks == 0) {
651 printk(KERN_ERR "RAMDISK: could not determine device size\n");
652 goto done;
655 printk(KERN_NOTICE "RAMDISK: Loading %d blocks [%d disk%s] into ram disk... ",
656 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
657 for (i=0; i < nblocks; i++) {
658 if (i && (i % devblocks == 0)) {
659 printk("done disk #%d.\n", i/devblocks);
660 rotate = 0;
661 invalidate_buffers(device);
662 if (infile.f_op->release)
663 infile.f_op->release(inode, &infile);
664 printk("Please insert disk #%d and press ENTER\n", i/devblocks+1);
665 wait_for_keypress();
666 if (blkdev_open(inode, &infile) != 0) {
667 printk("Error opening disk.\n");
668 goto done;
670 infile.f_pos = 0;
671 printk("Loading disk #%d... ", i/devblocks+1);
673 infile.f_op->read(&infile, buf, BLOCK_SIZE, &infile.f_pos);
674 outfile.f_op->write(&outfile, buf, BLOCK_SIZE, &outfile.f_pos);
675 #if !defined(CONFIG_ARCH_S390)
676 if (!(i % 16)) {
677 printk("%c\b", rotator[rotate & 0x3]);
678 rotate++;
680 #endif
682 printk("done.\n");
683 kfree(buf);
685 successful_load:
686 invalidate_buffers(device);
687 ROOT_DEV = MKDEV(MAJOR_NR, unit);
688 if (ROOT_DEVICE_NAME != NULL) strcpy (ROOT_DEVICE_NAME, "rd/0");
690 done:
691 if (infile.f_op->release)
692 infile.f_op->release(inode, &infile);
693 set_fs(fs);
694 return;
695 free_inodes: /* free inodes on error */
696 iput(out_inode);
697 blkdev_put(inode->i_bdev, BDEV_FILE);
698 free_inode:
699 iput(inode);
702 #ifdef CONFIG_MAC_FLOPPY
703 int swim3_fd_eject(int devnum);
704 #endif
706 static void __init rd_load_disk(int n)
708 #ifdef CONFIG_BLK_DEV_INITRD
709 extern kdev_t real_root_dev;
710 #endif
712 if (rd_doload == 0)
713 return;
715 if (MAJOR(ROOT_DEV) != FLOPPY_MAJOR
716 #ifdef CONFIG_BLK_DEV_INITRD
717 && MAJOR(real_root_dev) != FLOPPY_MAJOR
718 #endif
720 return;
722 if (rd_prompt) {
723 #ifdef CONFIG_BLK_DEV_FD
724 floppy_eject();
725 #endif
726 #ifdef CONFIG_MAC_FLOPPY
727 if(MAJOR(ROOT_DEV) == FLOPPY_MAJOR)
728 swim3_fd_eject(MINOR(ROOT_DEV));
729 else if(MAJOR(real_root_dev) == FLOPPY_MAJOR)
730 swim3_fd_eject(MINOR(real_root_dev));
731 #endif
732 printk(KERN_NOTICE
733 "VFS: Insert root floppy disk to be loaded into RAM disk and press ENTER\n");
734 wait_for_keypress();
737 rd_load_image(ROOT_DEV,rd_image_start, n);
741 void __init rd_load(void)
743 rd_load_disk(0);
746 void __init rd_load_secondary(void)
748 rd_load_disk(1);
751 #ifdef CONFIG_BLK_DEV_INITRD
752 void __init initrd_load(void)
754 rd_load_image(MKDEV(MAJOR_NR, INITRD_MINOR),rd_image_start,0);
756 #endif
758 #endif /* RD_LOADER */
760 #ifdef BUILD_CRAMDISK
763 * gzip declarations
766 #define OF(args) args
768 #ifndef memzero
769 #define memzero(s, n) memset ((s), 0, (n))
770 #endif
772 typedef unsigned char uch;
773 typedef unsigned short ush;
774 typedef unsigned long ulg;
776 #define INBUFSIZ 4096
777 #define WSIZE 0x8000 /* window size--must be a power of two, and */
778 /* at least 32K for zip's deflate method */
780 static uch *inbuf;
781 static uch *window;
783 static unsigned insize; /* valid bytes in inbuf */
784 static unsigned inptr; /* index of next byte to be processed in inbuf */
785 static unsigned outcnt; /* bytes in output buffer */
786 static int exit_code;
787 static long bytes_out;
788 static struct file *crd_infp, *crd_outfp;
790 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
792 /* Diagnostic functions (stubbed out) */
793 #define Assert(cond,msg)
794 #define Trace(x)
795 #define Tracev(x)
796 #define Tracevv(x)
797 #define Tracec(c,x)
798 #define Tracecv(c,x)
800 #define STATIC static
802 static int fill_inbuf(void);
803 static void flush_window(void);
804 static void *malloc(int size);
805 static void free(void *where);
806 static void error(char *m);
807 static void gzip_mark(void **);
808 static void gzip_release(void **);
810 #include "../../lib/inflate.c"
812 static void __init *malloc(int size)
814 return kmalloc(size, GFP_KERNEL);
817 static void __init free(void *where)
819 kfree(where);
822 static void __init gzip_mark(void **ptr)
826 static void __init gzip_release(void **ptr)
831 /* ===========================================================================
832 * Fill the input buffer. This is called only when the buffer is empty
833 * and at least one byte is really needed.
835 static int __init fill_inbuf(void)
837 if (exit_code) return -1;
839 insize = crd_infp->f_op->read(crd_infp, inbuf, INBUFSIZ,
840 &crd_infp->f_pos);
841 if (insize == 0) return -1;
843 inptr = 1;
845 return inbuf[0];
848 /* ===========================================================================
849 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
850 * (Used for the decompressed data only.)
852 static void __init flush_window(void)
854 ulg c = crc; /* temporary variable */
855 unsigned n;
856 uch *in, ch;
858 crd_outfp->f_op->write(crd_outfp, window, outcnt, &crd_outfp->f_pos);
859 in = window;
860 for (n = 0; n < outcnt; n++) {
861 ch = *in++;
862 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
864 crc = c;
865 bytes_out += (ulg)outcnt;
866 outcnt = 0;
869 static void __init error(char *x)
871 printk(KERN_ERR "%s", x);
872 exit_code = 1;
875 static int __init
876 crd_load(struct file * fp, struct file *outfp)
878 int result;
880 insize = 0; /* valid bytes in inbuf */
881 inptr = 0; /* index of next byte to be processed in inbuf */
882 outcnt = 0; /* bytes in output buffer */
883 exit_code = 0;
884 bytes_out = 0;
885 crc = (ulg)0xffffffffL; /* shift register contents */
887 crd_infp = fp;
888 crd_outfp = outfp;
889 inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
890 if (inbuf == 0) {
891 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
892 return -1;
894 window = kmalloc(WSIZE, GFP_KERNEL);
895 if (window == 0) {
896 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
897 kfree(inbuf);
898 return -1;
900 makecrc();
901 result = gunzip();
902 kfree(inbuf);
903 kfree(window);
904 return result;
907 #endif /* BUILD_CRAMDISK */