ia64/xen-unstable

view old/xenolinux-2.4.16-sparse/drivers/block/rd.c @ 235:d7d0a23b2e07

bitkeeper revision 1.93 (3e5a4e6bkPheUp3x1uufN2MS3LAB7A)

Latest and Greatest version of XenoLinux based on the Linux-2.4.21-pre4
kernel.
author iap10@labyrinth.cl.cam.ac.uk
date Mon Feb 24 16:55:07 2003 +0000 (2003-02-24)
parents
children
line source
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.
5 *
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.
12 *
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.
18 *
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.
26 *
27 * Loadable module support added by Tom Dyas.
28 *
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
33 *
34 * Added initrd: Werner Almesberger & Hans Lermen, Feb '96
35 *
36 * 4/25/96 : Made RAM disk size a parameter (default is now 4 MB)
37 * - Chad Page
38 *
39 * Add support for fs images split across >1 disk, Paul Gortmaker, Mar '98
40 *
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
43 */
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/slab.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);
70 /*
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
74 */
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.
96 */
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 block_device *rd_bdev[NUM_RAMDISKS];/* Protected device data */
105 /*
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).
110 */
111 int rd_size = CONFIG_BLK_DEV_RAM_SIZE; /* Size of the RAM disks */
112 /*
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).
122 */
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)
136 {
137 mount_initrd = 0;
138 return 1;
139 }
141 __setup("noinitrd", no_initrd);
143 #endif
145 static int __init ramdisk_start_setup(char *str)
146 {
147 rd_image_start = simple_strtol(str,NULL,0);
148 return 1;
149 }
151 static int __init load_ramdisk(char *str)
152 {
153 rd_doload = simple_strtol(str,NULL,0) & 3;
154 return 1;
155 }
157 static int __init prompt_ramdisk(char *str)
158 {
159 rd_prompt = simple_strtol(str,NULL,0) & 1;
160 return 1;
161 }
163 static int __init ramdisk_size(char *str)
164 {
165 rd_size = simple_strtol(str,NULL,0);
166 return 1;
167 }
169 static int __init ramdisk_size2(char *str)
170 {
171 return ramdisk_size(str);
172 }
174 static int __init ramdisk_blocksize(char *str)
175 {
176 rd_blocksize = simple_strtol(str,NULL,0);
177 return 1;
178 }
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
189 /*
190 * Copyright (C) 2000 Linus Torvalds.
191 * 2000 Transmeta Corp.
192 * aops copied from ramfs.
193 */
194 static int ramdisk_readpage(struct file *file, struct page * page)
195 {
196 if (!Page_Uptodate(page)) {
197 memset(kmap(page), 0, PAGE_CACHE_SIZE);
198 kunmap(page);
199 flush_dcache_page(page);
200 SetPageUptodate(page);
201 }
202 UnlockPage(page);
203 return 0;
204 }
206 static int ramdisk_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
207 {
208 if (!Page_Uptodate(page)) {
209 void *addr = page_address(page);
210 memset(addr, 0, PAGE_CACHE_SIZE);
211 flush_dcache_page(page);
212 SetPageUptodate(page);
213 }
214 SetPageDirty(page);
215 return 0;
216 }
218 static int ramdisk_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
219 {
220 return 0;
221 }
223 static struct address_space_operations ramdisk_aops = {
224 readpage: ramdisk_readpage,
225 writepage: fail_writepage,
226 prepare_write: ramdisk_prepare_write,
227 commit_write: ramdisk_commit_write,
228 };
230 static int rd_blkdev_pagecache_IO(int rw, struct buffer_head * sbh, int minor)
231 {
232 struct address_space * mapping;
233 unsigned long index;
234 int offset, size, err;
236 err = -EIO;
237 err = 0;
238 mapping = rd_bdev[minor]->bd_inode->i_mapping;
240 index = sbh->b_rsector >> (PAGE_CACHE_SHIFT - 9);
241 offset = (sbh->b_rsector << 9) & ~PAGE_CACHE_MASK;
242 size = sbh->b_size;
244 do {
245 int count;
246 struct page ** hash;
247 struct page * page;
248 char * src, * dst;
249 int unlock = 0;
251 count = PAGE_CACHE_SIZE - offset;
252 if (count > size)
253 count = size;
254 size -= count;
256 hash = page_hash(mapping, index);
257 page = __find_get_page(mapping, index, hash);
258 if (!page) {
259 page = grab_cache_page(mapping, index);
260 err = -ENOMEM;
261 if (!page)
262 goto out;
263 err = 0;
265 if (!Page_Uptodate(page)) {
266 memset(kmap(page), 0, PAGE_CACHE_SIZE);
267 kunmap(page);
268 SetPageUptodate(page);
269 }
271 unlock = 1;
272 }
274 index++;
276 if (rw == READ) {
277 src = kmap(page);
278 src += offset;
279 dst = bh_kmap(sbh);
280 } else {
281 dst = kmap(page);
282 dst += offset;
283 src = bh_kmap(sbh);
284 }
285 offset = 0;
287 memcpy(dst, src, count);
289 kunmap(page);
290 bh_kunmap(sbh);
292 if (rw == READ) {
293 flush_dcache_page(page);
294 } else {
295 SetPageDirty(page);
296 }
297 if (unlock)
298 UnlockPage(page);
299 __free_page(page);
300 } while (size);
302 out:
303 return err;
304 }
306 /*
307 * Basically, my strategy here is to set up a buffer-head which can't be
308 * deleted, and make that my Ramdisk. If the request is outside of the
309 * allocated size, we must get rid of it...
310 *
311 * 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Added devfs support
312 *
313 */
314 static int rd_make_request(request_queue_t * q, int rw, struct buffer_head *sbh)
315 {
316 unsigned int minor;
317 unsigned long offset, len;
319 minor = MINOR(sbh->b_rdev);
321 if (minor >= NUM_RAMDISKS)
322 goto fail;
325 offset = sbh->b_rsector << 9;
326 len = sbh->b_size;
328 if ((offset + len) > rd_length[minor])
329 goto fail;
331 if (rw==READA)
332 rw=READ;
333 if ((rw != READ) && (rw != WRITE)) {
334 printk(KERN_INFO "RAMDISK: bad command: %d\n", rw);
335 goto fail;
336 }
338 if (rd_blkdev_pagecache_IO(rw, sbh, minor))
339 goto fail;
341 sbh->b_end_io(sbh,1);
342 return 0;
343 fail:
344 sbh->b_end_io(sbh,0);
345 return 0;
346 }
348 static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
349 {
350 int error = -EINVAL;
351 unsigned int minor;
353 if (!inode || !inode->i_rdev)
354 goto out;
356 minor = MINOR(inode->i_rdev);
358 switch (cmd) {
359 case BLKFLSBUF:
360 if (!capable(CAP_SYS_ADMIN))
361 return -EACCES;
362 /* special: we want to release the ramdisk memory,
363 it's not like with the other blockdevices where
364 this ioctl only flushes away the buffer cache. */
365 error = -EBUSY;
366 down(&inode->i_bdev->bd_sem);
367 if (inode->i_bdev->bd_openers <= 2) {
368 truncate_inode_pages(inode->i_mapping, 0);
369 error = 0;
370 }
371 up(&inode->i_bdev->bd_sem);
372 break;
373 case BLKGETSIZE: /* Return device size */
374 if (!arg)
375 break;
376 error = put_user(rd_kbsize[minor] << 1, (unsigned long *) arg);
377 break;
378 case BLKGETSIZE64:
379 error = put_user((u64)rd_kbsize[minor]<<10, (u64*)arg);
380 break;
381 case BLKROSET:
382 case BLKROGET:
383 case BLKSSZGET:
384 error = blk_ioctl(inode->i_rdev, cmd, arg);
385 };
386 out:
387 return error;
388 }
391 #ifdef CONFIG_BLK_DEV_INITRD
393 static ssize_t initrd_read(struct file *file, char *buf,
394 size_t count, loff_t *ppos)
395 {
396 int left;
398 left = initrd_end - initrd_start - *ppos;
399 if (count > left) count = left;
400 if (count == 0) return 0;
401 copy_to_user(buf, (char *)initrd_start + *ppos, count);
402 *ppos += count;
403 return count;
404 }
407 static int initrd_release(struct inode *inode,struct file *file)
408 {
409 extern void free_initrd_mem(unsigned long, unsigned long);
411 lock_kernel();
412 if (!--initrd_users) {
413 free_initrd_mem(initrd_start, initrd_end);
414 initrd_start = 0;
415 }
416 unlock_kernel();
417 blkdev_put(inode->i_bdev, BDEV_FILE);
418 return 0;
419 }
422 static struct file_operations initrd_fops = {
423 read: initrd_read,
424 release: initrd_release,
425 };
427 #endif
430 static int rd_open(struct inode * inode, struct file * filp)
431 {
432 int unit = DEVICE_NR(inode->i_rdev);
434 #ifdef CONFIG_BLK_DEV_INITRD
435 if (unit == INITRD_MINOR) {
436 if (!initrd_start) return -ENODEV;
437 initrd_users++;
438 filp->f_op = &initrd_fops;
439 return 0;
440 }
441 #endif
443 if (unit >= NUM_RAMDISKS)
444 return -ENXIO;
446 /*
447 * Immunize device against invalidate_buffers() and prune_icache().
448 */
449 if (rd_bdev[unit] == NULL) {
450 rd_bdev[unit] = bdget(kdev_t_to_nr(inode->i_rdev));
451 rd_bdev[unit]->bd_openers++;
452 rd_bdev[unit]->bd_inode->i_mapping->a_ops = &ramdisk_aops;
453 }
455 return 0;
456 }
458 static struct block_device_operations rd_bd_op = {
459 owner: THIS_MODULE,
460 open: rd_open,
461 ioctl: rd_ioctl,
462 };
464 #ifdef MODULE
465 /* Before freeing the module, invalidate all of the protected buffers! */
466 static void __exit rd_cleanup (void)
467 {
468 int i;
470 for (i = 0 ; i < NUM_RAMDISKS; i++) {
471 struct block_device *bdev = rd_bdev[i];
472 rd_bdev[i] = NULL;
473 if (bdev)
474 blkdev_put(bdev, BDEV_FILE);
475 destroy_buffers(MKDEV(MAJOR_NR, i));
476 }
478 devfs_unregister (devfs_handle);
479 unregister_blkdev( MAJOR_NR, "ramdisk" );
480 hardsect_size[MAJOR_NR] = NULL;
481 blksize_size[MAJOR_NR] = NULL;
482 blk_size[MAJOR_NR] = NULL;
483 }
484 #endif
486 /* This is the registration and initialization section of the RAM disk driver */
487 int __init rd_init (void)
488 {
489 int i;
491 if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
492 (rd_blocksize & (rd_blocksize-1)))
493 {
494 printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
495 rd_blocksize);
496 rd_blocksize = BLOCK_SIZE;
497 }
499 if (register_blkdev(MAJOR_NR, "ramdisk", &rd_bd_op)) {
500 printk("RAMDISK: Could not get major %d", MAJOR_NR);
501 return -EIO;
502 }
504 blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), &rd_make_request);
506 for (i = 0; i < NUM_RAMDISKS; i++) {
507 /* rd_size is given in kB */
508 rd_length[i] = rd_size << 10;
509 rd_hardsec[i] = rd_blocksize;
510 rd_blocksizes[i] = rd_blocksize;
511 rd_kbsize[i] = rd_size;
512 }
513 devfs_handle = devfs_mk_dir (NULL, "rd", NULL);
514 devfs_register_series (devfs_handle, "%u", NUM_RAMDISKS,
515 DEVFS_FL_DEFAULT, MAJOR_NR, 0,
516 S_IFBLK | S_IRUSR | S_IWUSR,
517 &rd_bd_op, NULL);
519 for (i = 0; i < NUM_RAMDISKS; i++)
520 register_disk(NULL, MKDEV(MAJOR_NR,i), 1, &rd_bd_op, rd_size<<1);
522 #ifdef CONFIG_BLK_DEV_INITRD
523 /* We ought to separate initrd operations here */
524 register_disk(NULL, MKDEV(MAJOR_NR,INITRD_MINOR), 1, &rd_bd_op, rd_size<<1);
525 #endif
527 hardsect_size[MAJOR_NR] = rd_hardsec; /* Size of the RAM disk blocks */
528 blksize_size[MAJOR_NR] = rd_blocksizes; /* Avoid set_blocksize() check */
529 blk_size[MAJOR_NR] = rd_kbsize; /* Size of the RAM disk in kB */
531 /* rd_size is given in kB */
532 printk("RAMDISK driver initialized: "
533 "%d RAM disks of %dK size %d blocksize\n",
534 NUM_RAMDISKS, rd_size, rd_blocksize);
536 return 0;
537 }
539 #ifdef MODULE
540 module_init(rd_init);
541 module_exit(rd_cleanup);
542 #endif
544 /* loadable module support */
545 MODULE_PARM (rd_size, "1i");
546 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
547 MODULE_PARM (rd_blocksize, "i");
548 MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
550 MODULE_LICENSE("GPL");
552 /* End of non-loading portions of the RAM disk driver */
554 #ifdef RD_LOADER
555 /*
556 * This routine tries to find a RAM disk image to load, and returns the
557 * number of blocks to read for a non-compressed image, 0 if the image
558 * is a compressed image, and -1 if an image with the right magic
559 * numbers could not be found.
560 *
561 * We currently check for the following magic numbers:
562 * minix
563 * ext2
564 * romfs
565 * gzip
566 */
567 static int __init
568 identify_ramdisk_image(kdev_t device, struct file *fp, int start_block)
569 {
570 const int size = 512;
571 struct minix_super_block *minixsb;
572 struct ext2_super_block *ext2sb;
573 struct romfs_super_block *romfsb;
574 int nblocks = -1;
575 unsigned char *buf;
577 buf = kmalloc(size, GFP_KERNEL);
578 if (buf == 0)
579 return -1;
581 minixsb = (struct minix_super_block *) buf;
582 ext2sb = (struct ext2_super_block *) buf;
583 romfsb = (struct romfs_super_block *) buf;
584 memset(buf, 0xe5, size);
586 /*
587 * Read block 0 to test for gzipped kernel
588 */
589 if (fp->f_op->llseek)
590 fp->f_op->llseek(fp, start_block * BLOCK_SIZE, 0);
591 fp->f_pos = start_block * BLOCK_SIZE;
593 fp->f_op->read(fp, buf, size, &fp->f_pos);
595 /*
596 * If it matches the gzip magic numbers, return -1
597 */
598 if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
599 printk(KERN_NOTICE
600 "RAMDISK: Compressed image found at block %d\n",
601 start_block);
602 nblocks = 0;
603 goto done;
604 }
606 /* romfs is at block zero too */
607 if (romfsb->word0 == ROMSB_WORD0 &&
608 romfsb->word1 == ROMSB_WORD1) {
609 printk(KERN_NOTICE
610 "RAMDISK: romfs filesystem found at block %d\n",
611 start_block);
612 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
613 goto done;
614 }
616 /*
617 * Read block 1 to test for minix and ext2 superblock
618 */
619 if (fp->f_op->llseek)
620 fp->f_op->llseek(fp, (start_block+1) * BLOCK_SIZE, 0);
621 fp->f_pos = (start_block+1) * BLOCK_SIZE;
623 fp->f_op->read(fp, buf, size, &fp->f_pos);
625 /* Try minix */
626 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
627 minixsb->s_magic == MINIX_SUPER_MAGIC2) {
628 printk(KERN_NOTICE
629 "RAMDISK: Minix filesystem found at block %d\n",
630 start_block);
631 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
632 goto done;
633 }
635 /* Try ext2 */
636 if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
637 printk(KERN_NOTICE
638 "RAMDISK: ext2 filesystem found at block %d\n",
639 start_block);
640 nblocks = le32_to_cpu(ext2sb->s_blocks_count);
641 goto done;
642 }
644 printk(KERN_NOTICE
645 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
646 start_block);
648 done:
649 if (fp->f_op->llseek)
650 fp->f_op->llseek(fp, start_block * BLOCK_SIZE, 0);
651 fp->f_pos = start_block * BLOCK_SIZE;
653 kfree(buf);
654 return nblocks;
655 }
657 /*
658 * This routine loads in the RAM disk image.
659 */
660 static void __init rd_load_image(kdev_t device, int offset, int unit)
661 {
662 struct inode *inode, *out_inode;
663 struct file infile, outfile;
664 struct dentry in_dentry, out_dentry;
665 mm_segment_t fs;
666 kdev_t ram_device;
667 int nblocks, i;
668 char *buf;
669 unsigned short rotate = 0;
670 unsigned short devblocks = 0;
671 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES) && !defined(CONFIG_XENO)
672 char rotator[4] = { '|' , '/' , '-' , '\\' };
673 #endif
674 ram_device = MKDEV(MAJOR_NR, unit);
676 if ((inode = get_empty_inode()) == NULL)
677 return;
678 memset(&infile, 0, sizeof(infile));
679 memset(&in_dentry, 0, sizeof(in_dentry));
680 infile.f_mode = 1; /* read only */
681 infile.f_dentry = &in_dentry;
682 in_dentry.d_inode = inode;
683 infile.f_op = &def_blk_fops;
684 init_special_inode(inode, S_IFBLK | S_IRUSR, kdev_t_to_nr(device));
686 if ((out_inode = get_empty_inode()) == NULL)
687 goto free_inode;
688 memset(&outfile, 0, sizeof(outfile));
689 memset(&out_dentry, 0, sizeof(out_dentry));
690 outfile.f_mode = 3; /* read/write */
691 outfile.f_dentry = &out_dentry;
692 out_dentry.d_inode = out_inode;
693 outfile.f_op = &def_blk_fops;
694 init_special_inode(out_inode, S_IFBLK | S_IRUSR | S_IWUSR, kdev_t_to_nr(ram_device));
696 if (blkdev_open(inode, &infile) != 0) {
697 iput(out_inode);
698 goto free_inode;
699 }
700 if (blkdev_open(out_inode, &outfile) != 0)
701 goto free_inodes;
703 fs = get_fs();
704 set_fs(KERNEL_DS);
706 nblocks = identify_ramdisk_image(device, &infile, offset);
707 if (nblocks < 0)
708 goto done;
710 if (nblocks == 0) {
711 #ifdef BUILD_CRAMDISK
712 if (crd_load(&infile, &outfile) == 0)
713 goto successful_load;
714 #else
715 printk(KERN_NOTICE
716 "RAMDISK: Kernel does not support compressed "
717 "RAM disk images\n");
718 #endif
719 goto done;
720 }
722 /*
723 * NOTE NOTE: nblocks suppose that the blocksize is BLOCK_SIZE, so
724 * rd_load_image will work only with filesystem BLOCK_SIZE wide!
725 * So make sure to use 1k blocksize while generating ext2fs
726 * ramdisk-images.
727 */
728 if (nblocks > (rd_length[unit] >> BLOCK_SIZE_BITS)) {
729 printk("RAMDISK: image too big! (%d/%ld blocks)\n",
730 nblocks, rd_length[unit] >> BLOCK_SIZE_BITS);
731 goto done;
732 }
734 /*
735 * OK, time to copy in the data
736 */
737 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
738 if (buf == 0) {
739 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
740 goto done;
741 }
743 if (blk_size[MAJOR(device)])
744 devblocks = blk_size[MAJOR(device)][MINOR(device)];
746 #ifdef CONFIG_BLK_DEV_INITRD
747 if (MAJOR(device) == MAJOR_NR && MINOR(device) == INITRD_MINOR)
748 devblocks = nblocks;
749 #endif
751 if (devblocks == 0) {
752 printk(KERN_ERR "RAMDISK: could not determine device size\n");
753 goto done;
754 }
756 printk(KERN_NOTICE "RAMDISK: Loading %d blocks [%d disk%s] into ram disk... ",
757 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
758 for (i=0; i < nblocks; i++) {
759 if (i && (i % devblocks == 0)) {
760 printk("done disk #%d.\n", i/devblocks);
761 rotate = 0;
762 if (infile.f_op->release(inode, &infile) != 0) {
763 printk("Error closing the disk.\n");
764 goto noclose_input;
765 }
766 printk("Please insert disk #%d and press ENTER\n", i/devblocks+1);
767 wait_for_keypress();
768 if (blkdev_open(inode, &infile) != 0) {
769 printk("Error opening disk.\n");
770 goto noclose_input;
771 }
772 infile.f_pos = 0;
773 printk("Loading disk #%d... ", i/devblocks+1);
774 }
775 infile.f_op->read(&infile, buf, BLOCK_SIZE, &infile.f_pos);
776 outfile.f_op->write(&outfile, buf, BLOCK_SIZE, &outfile.f_pos);
777 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES) && !defined(CONFIG_XENO)
778 if (!(i % 16)) {
779 printk("%c\b", rotator[rotate & 0x3]);
780 rotate++;
781 }
782 #endif
783 }
784 printk("done.\n");
785 kfree(buf);
787 successful_load:
788 ROOT_DEV = MKDEV(MAJOR_NR, unit);
789 if (ROOT_DEVICE_NAME != NULL) strcpy (ROOT_DEVICE_NAME, "rd/0");
791 done:
792 infile.f_op->release(inode, &infile);
793 noclose_input:
794 blkdev_close(out_inode, &outfile);
795 iput(inode);
796 iput(out_inode);
797 set_fs(fs);
798 return;
799 free_inodes: /* free inodes on error */
800 iput(out_inode);
801 infile.f_op->release(inode, &infile);
802 free_inode:
803 iput(inode);
804 }
806 #ifdef CONFIG_MAC_FLOPPY
807 int swim3_fd_eject(int devnum);
808 #endif
810 static void __init rd_load_disk(int n)
811 {
813 if (rd_doload == 0)
814 return;
816 if (MAJOR(ROOT_DEV) != FLOPPY_MAJOR
817 #ifdef CONFIG_BLK_DEV_INITRD
818 && MAJOR(real_root_dev) != FLOPPY_MAJOR
819 #endif
820 )
821 return;
823 if (rd_prompt) {
824 #ifdef CONFIG_BLK_DEV_FD
825 floppy_eject();
826 #endif
827 #ifdef CONFIG_MAC_FLOPPY
828 if(MAJOR(ROOT_DEV) == FLOPPY_MAJOR)
829 swim3_fd_eject(MINOR(ROOT_DEV));
830 else if(MAJOR(real_root_dev) == FLOPPY_MAJOR)
831 swim3_fd_eject(MINOR(real_root_dev));
832 #endif
833 printk(KERN_NOTICE
834 "VFS: Insert root floppy disk to be loaded into RAM disk and press ENTER\n");
835 wait_for_keypress();
836 }
838 rd_load_image(ROOT_DEV,rd_image_start, n);
840 }
842 void __init rd_load(void)
843 {
844 rd_load_disk(0);
845 }
847 void __init rd_load_secondary(void)
848 {
849 rd_load_disk(1);
850 }
852 #ifdef CONFIG_BLK_DEV_INITRD
853 void __init initrd_load(void)
854 {
855 rd_load_image(MKDEV(MAJOR_NR, INITRD_MINOR),rd_image_start,0);
856 }
857 #endif
859 #endif /* RD_LOADER */
861 #ifdef BUILD_CRAMDISK
863 /*
864 * gzip declarations
865 */
867 #define OF(args) args
869 #ifndef memzero
870 #define memzero(s, n) memset ((s), 0, (n))
871 #endif
873 typedef unsigned char uch;
874 typedef unsigned short ush;
875 typedef unsigned long ulg;
877 #define INBUFSIZ 4096
878 #define WSIZE 0x8000 /* window size--must be a power of two, and */
879 /* at least 32K for zip's deflate method */
881 static uch *inbuf;
882 static uch *window;
884 static unsigned insize; /* valid bytes in inbuf */
885 static unsigned inptr; /* index of next byte to be processed in inbuf */
886 static unsigned outcnt; /* bytes in output buffer */
887 static int exit_code;
888 static long bytes_out;
889 static struct file *crd_infp, *crd_outfp;
891 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
893 /* Diagnostic functions (stubbed out) */
894 #define Assert(cond,msg)
895 #define Trace(x)
896 #define Tracev(x)
897 #define Tracevv(x)
898 #define Tracec(c,x)
899 #define Tracecv(c,x)
901 #define STATIC static
903 static int fill_inbuf(void);
904 static void flush_window(void);
905 static void *malloc(int size);
906 static void free(void *where);
907 static void error(char *m);
908 static void gzip_mark(void **);
909 static void gzip_release(void **);
911 #include "../../lib/inflate.c"
913 static void __init *malloc(int size)
914 {
915 return kmalloc(size, GFP_KERNEL);
916 }
918 static void __init free(void *where)
919 {
920 kfree(where);
921 }
923 static void __init gzip_mark(void **ptr)
924 {
925 }
927 static void __init gzip_release(void **ptr)
928 {
929 }
932 /* ===========================================================================
933 * Fill the input buffer. This is called only when the buffer is empty
934 * and at least one byte is really needed.
935 */
936 static int __init fill_inbuf(void)
937 {
938 if (exit_code) return -1;
940 insize = crd_infp->f_op->read(crd_infp, inbuf, INBUFSIZ,
941 &crd_infp->f_pos);
942 if (insize == 0) return -1;
944 inptr = 1;
946 return inbuf[0];
947 }
949 /* ===========================================================================
950 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
951 * (Used for the decompressed data only.)
952 */
953 static void __init flush_window(void)
954 {
955 ulg c = crc; /* temporary variable */
956 unsigned n;
957 uch *in, ch;
959 crd_outfp->f_op->write(crd_outfp, window, outcnt, &crd_outfp->f_pos);
960 in = window;
961 for (n = 0; n < outcnt; n++) {
962 ch = *in++;
963 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
964 }
965 crc = c;
966 bytes_out += (ulg)outcnt;
967 outcnt = 0;
968 }
970 static void __init error(char *x)
971 {
972 printk(KERN_ERR "%s", x);
973 exit_code = 1;
974 }
976 static int __init
977 crd_load(struct file * fp, struct file *outfp)
978 {
979 int result;
981 insize = 0; /* valid bytes in inbuf */
982 inptr = 0; /* index of next byte to be processed in inbuf */
983 outcnt = 0; /* bytes in output buffer */
984 exit_code = 0;
985 bytes_out = 0;
986 crc = (ulg)0xffffffffL; /* shift register contents */
988 crd_infp = fp;
989 crd_outfp = outfp;
990 inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
991 if (inbuf == 0) {
992 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
993 return -1;
994 }
995 window = kmalloc(WSIZE, GFP_KERNEL);
996 if (window == 0) {
997 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
998 kfree(inbuf);
999 return -1;
1001 makecrc();
1002 result = gunzip();
1003 kfree(inbuf);
1004 kfree(window);
1005 return result;
1008 #endif /* BUILD_CRAMDISK */