File: /usr/src/linux/drivers/block/rd.c
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 */
44
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>
63
64 #include <asm/system.h>
65 #include <asm/uaccess.h>
66 #include <asm/byteorder.h>
67
68 extern void wait_for_keypress(void);
69
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>
78
79 /* The RAM disk size is now a parameter */
80 #define NUM_RAMDISKS 16 /* This cannot be overridden (yet) */
81
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
86
87 void rd_load(void);
88 static int crd_load(struct file *fp, struct file *outfp);
89
90 #ifdef CONFIG_BLK_DEV_INITRD
91 static int initrd_users;
92 #endif
93 #endif
94
95 /* Various static variables go here. Most are used only in the RAM disk code.
96 */
97
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 */
104
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 */
124
125 #ifndef MODULE
126
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;
134
135 static int __init no_initrd(char *str)
136 {
137 mount_initrd = 0;
138 return 1;
139 }
140
141 __setup("noinitrd", no_initrd);
142
143 #endif
144
145 static int __init ramdisk_start_setup(char *str)
146 {
147 rd_image_start = simple_strtol(str,NULL,0);
148 return 1;
149 }
150
151 static int __init load_ramdisk(char *str)
152 {
153 rd_doload = simple_strtol(str,NULL,0) & 3;
154 return 1;
155 }
156
157 static int __init prompt_ramdisk(char *str)
158 {
159 rd_prompt = simple_strtol(str,NULL,0) & 1;
160 return 1;
161 }
162
163 static int __init ramdisk_size(char *str)
164 {
165 rd_size = simple_strtol(str,NULL,0);
166 return 1;
167 }
168
169 static int __init ramdisk_size2(char *str)
170 {
171 return ramdisk_size(str);
172 }
173
174 static int __init ramdisk_blocksize(char *str)
175 {
176 rd_blocksize = simple_strtol(str,NULL,0);
177 return 1;
178 }
179
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);
186
187 #endif
188
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 }
205
206 /*
207 * Writing: just make sure the page gets marked dirty, so that
208 * the page stealer won't grab it.
209 */
210 static int ramdisk_writepage(struct page *page)
211 {
212 SetPageDirty(page);
213 UnlockPage(page);
214 return 0;
215 }
216
217 static int ramdisk_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
218 {
219 if (!Page_Uptodate(page)) {
220 void *addr = page_address(page);
221 memset(addr, 0, PAGE_CACHE_SIZE);
222 flush_dcache_page(page);
223 SetPageUptodate(page);
224 }
225 SetPageDirty(page);
226 return 0;
227 }
228
229 static int ramdisk_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
230 {
231 return 0;
232 }
233
234 static struct address_space_operations ramdisk_aops = {
235 readpage: ramdisk_readpage,
236 writepage: ramdisk_writepage,
237 prepare_write: ramdisk_prepare_write,
238 commit_write: ramdisk_commit_write,
239 };
240
241 static int rd_blkdev_pagecache_IO(int rw, struct buffer_head * sbh, int minor)
242 {
243 struct address_space * mapping;
244 unsigned long index;
245 int offset, size, err;
246
247 err = -EIO;
248 err = 0;
249 mapping = rd_bdev[minor]->bd_inode->i_mapping;
250
251 index = sbh->b_rsector >> (PAGE_CACHE_SHIFT - 9);
252 offset = (sbh->b_rsector << 9) & ~PAGE_CACHE_MASK;
253 size = sbh->b_size;
254
255 do {
256 int count;
257 struct page ** hash;
258 struct page * page;
259 char * src, * dst;
260 int unlock = 0;
261
262 count = PAGE_CACHE_SIZE - offset;
263 if (count > size)
264 count = size;
265 size -= count;
266
267 hash = page_hash(mapping, index);
268 page = __find_get_page(mapping, index, hash);
269 if (!page) {
270 page = grab_cache_page(mapping, index);
271 err = -ENOMEM;
272 if (!page)
273 goto out;
274 err = 0;
275
276 if (!Page_Uptodate(page)) {
277 memset(kmap(page), 0, PAGE_CACHE_SIZE);
278 kunmap(page);
279 flush_dcache_page(page);
280 SetPageUptodate(page);
281 }
282
283 unlock = 1;
284 }
285
286 index++;
287
288 if (rw == READ) {
289 src = kmap(page);
290 src += offset;
291 dst = bh_kmap(sbh);
292 } else {
293 dst = kmap(page);
294 dst += offset;
295 src = bh_kmap(sbh);
296 }
297 offset = 0;
298
299 memcpy(dst, src, count);
300
301 kunmap(page);
302 bh_kunmap(sbh);
303
304 if (rw != READ)
305 SetPageDirty(page);
306 if (unlock)
307 UnlockPage(page);
308 __free_page(page);
309 } while (size);
310
311 out:
312 return err;
313 }
314
315 /*
316 * Basically, my strategy here is to set up a buffer-head which can't be
317 * deleted, and make that my Ramdisk. If the request is outside of the
318 * allocated size, we must get rid of it...
319 *
320 * 19-JAN-1998 Richard Gooch <rgooch@atnf.csiro.au> Added devfs support
321 *
322 */
323 static int rd_make_request(request_queue_t * q, int rw, struct buffer_head *sbh)
324 {
325 unsigned int minor;
326 unsigned long offset, len;
327
328 minor = MINOR(sbh->b_rdev);
329
330 if (minor >= NUM_RAMDISKS)
331 goto fail;
332
333
334 offset = sbh->b_rsector << 9;
335 len = sbh->b_size;
336
337 if ((offset + len) > rd_length[minor])
338 goto fail;
339
340 if (rw==READA)
341 rw=READ;
342 if ((rw != READ) && (rw != WRITE)) {
343 printk(KERN_INFO "RAMDISK: bad command: %d\n", rw);
344 goto fail;
345 }
346
347 if (rd_blkdev_pagecache_IO(rw, sbh, minor))
348 goto fail;
349
350 sbh->b_end_io(sbh,1);
351 return 0;
352 fail:
353 sbh->b_end_io(sbh,0);
354 return 0;
355 }
356
357 static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
358 {
359 int error = -EINVAL;
360 unsigned int minor;
361
362 if (!inode || !inode->i_rdev)
363 goto out;
364
365 minor = MINOR(inode->i_rdev);
366
367 switch (cmd) {
368 case BLKFLSBUF:
369 if (!capable(CAP_SYS_ADMIN))
370 return -EACCES;
371 /* special: we want to release the ramdisk memory,
372 it's not like with the other blockdevices where
373 this ioctl only flushes away the buffer cache. */
374 error = -EBUSY;
375 down(&inode->i_bdev->bd_sem);
376 if (inode->i_bdev->bd_openers <= 2) {
377 truncate_inode_pages(inode->i_mapping, 0);
378 error = 0;
379 }
380 up(&inode->i_bdev->bd_sem);
381 break;
382 case BLKGETSIZE: /* Return device size */
383 if (!arg)
384 break;
385 error = put_user(rd_kbsize[minor] << 1, (long *) arg);
386 break;
387 case BLKGETSIZE64:
388 error = put_user((u64)rd_kbsize[minor]<<10, (u64*)arg);
389 break;
390 case BLKROSET:
391 case BLKROGET:
392 case BLKSSZGET:
393 error = blk_ioctl(inode->i_rdev, cmd, arg);
394 };
395 out:
396 return error;
397 }
398
399
400 #ifdef CONFIG_BLK_DEV_INITRD
401
402 static ssize_t initrd_read(struct file *file, char *buf,
403 size_t count, loff_t *ppos)
404 {
405 int left;
406
407 left = initrd_end - initrd_start - *ppos;
408 if (count > left) count = left;
409 if (count == 0) return 0;
410 copy_to_user(buf, (char *)initrd_start + *ppos, count);
411 *ppos += count;
412 return count;
413 }
414
415
416 static int initrd_release(struct inode *inode,struct file *file)
417 {
418 extern void free_initrd_mem(unsigned long, unsigned long);
419
420 lock_kernel();
421 if (!--initrd_users) {
422 free_initrd_mem(initrd_start, initrd_end);
423 initrd_start = 0;
424 }
425 unlock_kernel();
426 blkdev_put(inode->i_bdev, BDEV_FILE);
427 return 0;
428 }
429
430
431 static struct file_operations initrd_fops = {
432 read: initrd_read,
433 release: initrd_release,
434 };
435
436 #endif
437
438
439 static int rd_open(struct inode * inode, struct file * filp)
440 {
441 int unit = DEVICE_NR(inode->i_rdev);
442
443 #ifdef CONFIG_BLK_DEV_INITRD
444 if (unit == INITRD_MINOR) {
445 if (!initrd_start) return -ENODEV;
446 initrd_users++;
447 filp->f_op = &initrd_fops;
448 return 0;
449 }
450 #endif
451
452 if (unit >= NUM_RAMDISKS)
453 return -ENXIO;
454
455 /*
456 * Immunize device against invalidate_buffers() and prune_icache().
457 */
458 if (rd_bdev[unit] == NULL) {
459 rd_bdev[unit] = bdget(kdev_t_to_nr(inode->i_rdev));
460 rd_bdev[unit]->bd_openers++;
461 rd_bdev[unit]->bd_inode->i_mapping->a_ops = &ramdisk_aops;
462 }
463
464 MOD_INC_USE_COUNT;
465
466 return 0;
467 }
468
469 static int rd_release(struct inode * inode, struct file * filp)
470 {
471 MOD_DEC_USE_COUNT;
472 return 0;
473 }
474
475 static struct block_device_operations rd_bd_op = {
476 open: rd_open,
477 release: rd_release,
478 ioctl: rd_ioctl,
479 };
480
481 #ifdef MODULE
482 /* Before freeing the module, invalidate all of the protected buffers! */
483 static void __exit rd_cleanup (void)
484 {
485 int i;
486
487 for (i = 0 ; i < NUM_RAMDISKS; i++) {
488 struct block_device *bdev = rd_bdev[i];
489 rd_bdev[i] = NULL;
490 if (bdev)
491 blkdev_put(bdev, BDEV_FILE);
492 destroy_buffers(MKDEV(MAJOR_NR, i));
493 }
494
495 devfs_unregister (devfs_handle);
496 unregister_blkdev( MAJOR_NR, "ramdisk" );
497 hardsect_size[MAJOR_NR] = NULL;
498 blksize_size[MAJOR_NR] = NULL;
499 blk_size[MAJOR_NR] = NULL;
500 }
501 #endif
502
503 /* This is the registration and initialization section of the RAM disk driver */
504 int __init rd_init (void)
505 {
506 int i;
507
508 if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
509 (rd_blocksize & (rd_blocksize-1)))
510 {
511 printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
512 rd_blocksize);
513 rd_blocksize = BLOCK_SIZE;
514 }
515
516 if (register_blkdev(MAJOR_NR, "ramdisk", &rd_bd_op)) {
517 printk("RAMDISK: Could not get major %d", MAJOR_NR);
518 return -EIO;
519 }
520
521 blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), &rd_make_request);
522
523 for (i = 0; i < NUM_RAMDISKS; i++) {
524 /* rd_size is given in kB */
525 rd_length[i] = rd_size << 10;
526 rd_hardsec[i] = rd_blocksize;
527 rd_blocksizes[i] = rd_blocksize;
528 rd_kbsize[i] = rd_size;
529 }
530 devfs_handle = devfs_mk_dir (NULL, "rd", NULL);
531 devfs_register_series (devfs_handle, "%u", NUM_RAMDISKS,
532 DEVFS_FL_DEFAULT, MAJOR_NR, 0,
533 S_IFBLK | S_IRUSR | S_IWUSR,
534 &rd_bd_op, NULL);
535
536 for (i = 0; i < NUM_RAMDISKS; i++)
537 register_disk(NULL, MKDEV(MAJOR_NR,i), 1, &rd_bd_op, rd_size<<1);
538
539 #ifdef CONFIG_BLK_DEV_INITRD
540 /* We ought to separate initrd operations here */
541 register_disk(NULL, MKDEV(MAJOR_NR,INITRD_MINOR), 1, &rd_bd_op, rd_size<<1);
542 #endif
543
544 hardsect_size[MAJOR_NR] = rd_hardsec; /* Size of the RAM disk blocks */
545 blksize_size[MAJOR_NR] = rd_blocksizes; /* Avoid set_blocksize() check */
546 blk_size[MAJOR_NR] = rd_kbsize; /* Size of the RAM disk in kB */
547
548 /* rd_size is given in kB */
549 printk("RAMDISK driver initialized: "
550 "%d RAM disks of %dK size %d blocksize\n",
551 NUM_RAMDISKS, rd_size, rd_blocksize);
552
553 return 0;
554 }
555
556 #ifdef MODULE
557 module_init(rd_init);
558 module_exit(rd_cleanup);
559 #endif
560
561 /* loadable module support */
562 MODULE_PARM (rd_size, "1i");
563 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
564 MODULE_PARM (rd_blocksize, "i");
565 MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
566
567 MODULE_LICENSE("GPL");
568
569 /* End of non-loading portions of the RAM disk driver */
570
571 #ifdef RD_LOADER
572 /*
573 * This routine tries to find a RAM disk image to load, and returns the
574 * number of blocks to read for a non-compressed image, 0 if the image
575 * is a compressed image, and -1 if an image with the right magic
576 * numbers could not be found.
577 *
578 * We currently check for the following magic numbers:
579 * minix
580 * ext2
581 * romfs
582 * gzip
583 */
584 static int __init
585 identify_ramdisk_image(kdev_t device, struct file *fp, int start_block)
586 {
587 const int size = 512;
588 struct minix_super_block *minixsb;
589 struct ext2_super_block *ext2sb;
590 struct romfs_super_block *romfsb;
591 int nblocks = -1;
592 unsigned char *buf;
593
594 buf = kmalloc(size, GFP_KERNEL);
595 if (buf == 0)
596 return -1;
597
598 minixsb = (struct minix_super_block *) buf;
599 ext2sb = (struct ext2_super_block *) buf;
600 romfsb = (struct romfs_super_block *) buf;
601 memset(buf, 0xe5, size);
602
603 /*
604 * Read block 0 to test for gzipped kernel
605 */
606 if (fp->f_op->llseek)
607 fp->f_op->llseek(fp, start_block * BLOCK_SIZE, 0);
608 fp->f_pos = start_block * BLOCK_SIZE;
609
610 fp->f_op->read(fp, buf, size, &fp->f_pos);
611
612 /*
613 * If it matches the gzip magic numbers, return -1
614 */
615 if (buf[0] == 037 && ((buf[1] == 0213) || (buf[1] == 0236))) {
616 printk(KERN_NOTICE
617 "RAMDISK: Compressed image found at block %d\n",
618 start_block);
619 nblocks = 0;
620 goto done;
621 }
622
623 /* romfs is at block zero too */
624 if (romfsb->word0 == ROMSB_WORD0 &&
625 romfsb->word1 == ROMSB_WORD1) {
626 printk(KERN_NOTICE
627 "RAMDISK: romfs filesystem found at block %d\n",
628 start_block);
629 nblocks = (ntohl(romfsb->size)+BLOCK_SIZE-1)>>BLOCK_SIZE_BITS;
630 goto done;
631 }
632
633 /*
634 * Read block 1 to test for minix and ext2 superblock
635 */
636 if (fp->f_op->llseek)
637 fp->f_op->llseek(fp, (start_block+1) * BLOCK_SIZE, 0);
638 fp->f_pos = (start_block+1) * BLOCK_SIZE;
639
640 fp->f_op->read(fp, buf, size, &fp->f_pos);
641
642 /* Try minix */
643 if (minixsb->s_magic == MINIX_SUPER_MAGIC ||
644 minixsb->s_magic == MINIX_SUPER_MAGIC2) {
645 printk(KERN_NOTICE
646 "RAMDISK: Minix filesystem found at block %d\n",
647 start_block);
648 nblocks = minixsb->s_nzones << minixsb->s_log_zone_size;
649 goto done;
650 }
651
652 /* Try ext2 */
653 if (ext2sb->s_magic == cpu_to_le16(EXT2_SUPER_MAGIC)) {
654 printk(KERN_NOTICE
655 "RAMDISK: ext2 filesystem found at block %d\n",
656 start_block);
657 nblocks = le32_to_cpu(ext2sb->s_blocks_count);
658 goto done;
659 }
660
661 printk(KERN_NOTICE
662 "RAMDISK: Couldn't find valid RAM disk image starting at %d.\n",
663 start_block);
664
665 done:
666 if (fp->f_op->llseek)
667 fp->f_op->llseek(fp, start_block * BLOCK_SIZE, 0);
668 fp->f_pos = start_block * BLOCK_SIZE;
669
670 kfree(buf);
671 return nblocks;
672 }
673
674 /*
675 * This routine loads in the RAM disk image.
676 */
677 static void __init rd_load_image(kdev_t device, int offset, int unit)
678 {
679 struct inode *inode, *out_inode;
680 struct file infile, outfile;
681 struct dentry in_dentry, out_dentry;
682 mm_segment_t fs;
683 kdev_t ram_device;
684 int nblocks, i;
685 char *buf;
686 unsigned short rotate = 0;
687 unsigned short devblocks = 0;
688 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
689 char rotator[4] = { '|' , '/' , '-' , '\\' };
690 #endif
691 ram_device = MKDEV(MAJOR_NR, unit);
692
693 if ((inode = get_empty_inode()) == NULL)
694 return;
695 memset(&infile, 0, sizeof(infile));
696 memset(&in_dentry, 0, sizeof(in_dentry));
697 infile.f_mode = 1; /* read only */
698 infile.f_dentry = &in_dentry;
699 in_dentry.d_inode = inode;
700 infile.f_op = &def_blk_fops;
701 init_special_inode(inode, S_IFBLK | S_IRUSR, kdev_t_to_nr(device));
702
703 if ((out_inode = get_empty_inode()) == NULL)
704 goto free_inode;
705 memset(&outfile, 0, sizeof(outfile));
706 memset(&out_dentry, 0, sizeof(out_dentry));
707 outfile.f_mode = 3; /* read/write */
708 outfile.f_dentry = &out_dentry;
709 out_dentry.d_inode = out_inode;
710 outfile.f_op = &def_blk_fops;
711 init_special_inode(out_inode, S_IFBLK | S_IRUSR | S_IWUSR, kdev_t_to_nr(ram_device));
712
713 if (blkdev_open(inode, &infile) != 0) {
714 iput(out_inode);
715 goto free_inode;
716 }
717 if (blkdev_open(out_inode, &outfile) != 0)
718 goto free_inodes;
719
720 fs = get_fs();
721 set_fs(KERNEL_DS);
722
723 nblocks = identify_ramdisk_image(device, &infile, offset);
724 if (nblocks < 0)
725 goto done;
726
727 if (nblocks == 0) {
728 #ifdef BUILD_CRAMDISK
729 if (crd_load(&infile, &outfile) == 0)
730 goto successful_load;
731 #else
732 printk(KERN_NOTICE
733 "RAMDISK: Kernel does not support compressed "
734 "RAM disk images\n");
735 #endif
736 goto done;
737 }
738
739 /*
740 * NOTE NOTE: nblocks suppose that the blocksize is BLOCK_SIZE, so
741 * rd_load_image will work only with filesystem BLOCK_SIZE wide!
742 * So make sure to use 1k blocksize while generating ext2fs
743 * ramdisk-images.
744 */
745 if (nblocks > (rd_length[unit] >> BLOCK_SIZE_BITS)) {
746 printk("RAMDISK: image too big! (%d/%ld blocks)\n",
747 nblocks, rd_length[unit] >> BLOCK_SIZE_BITS);
748 goto done;
749 }
750
751 /*
752 * OK, time to copy in the data
753 */
754 buf = kmalloc(BLOCK_SIZE, GFP_KERNEL);
755 if (buf == 0) {
756 printk(KERN_ERR "RAMDISK: could not allocate buffer\n");
757 goto done;
758 }
759
760 if (blk_size[MAJOR(device)])
761 devblocks = blk_size[MAJOR(device)][MINOR(device)];
762
763 #ifdef CONFIG_BLK_DEV_INITRD
764 if (MAJOR(device) == MAJOR_NR && MINOR(device) == INITRD_MINOR)
765 devblocks = nblocks;
766 #endif
767
768 if (devblocks == 0) {
769 printk(KERN_ERR "RAMDISK: could not determine device size\n");
770 goto done;
771 }
772
773 printk(KERN_NOTICE "RAMDISK: Loading %d blocks [%d disk%s] into ram disk... ",
774 nblocks, ((nblocks-1)/devblocks)+1, nblocks>devblocks ? "s" : "");
775 for (i=0; i < nblocks; i++) {
776 if (i && (i % devblocks == 0)) {
777 printk("done disk #%d.\n", i/devblocks);
778 rotate = 0;
779 if (infile.f_op->release(inode, &infile) != 0) {
780 printk("Error closing the disk.\n");
781 goto noclose_input;
782 }
783 printk("Please insert disk #%d and press ENTER\n", i/devblocks+1);
784 wait_for_keypress();
785 if (blkdev_open(inode, &infile) != 0) {
786 printk("Error opening disk.\n");
787 goto noclose_input;
788 }
789 infile.f_pos = 0;
790 printk("Loading disk #%d... ", i/devblocks+1);
791 }
792 infile.f_op->read(&infile, buf, BLOCK_SIZE, &infile.f_pos);
793 outfile.f_op->write(&outfile, buf, BLOCK_SIZE, &outfile.f_pos);
794 #if !defined(CONFIG_ARCH_S390) && !defined(CONFIG_PPC_ISERIES)
795 if (!(i % 16)) {
796 printk("%c\b", rotator[rotate & 0x3]);
797 rotate++;
798 }
799 #endif
800 }
801 printk("done.\n");
802 kfree(buf);
803
804 successful_load:
805 ROOT_DEV = MKDEV(MAJOR_NR, unit);
806 if (ROOT_DEVICE_NAME != NULL) strcpy (ROOT_DEVICE_NAME, "rd/0");
807
808 done:
809 infile.f_op->release(inode, &infile);
810 noclose_input:
811 blkdev_close(out_inode, &outfile);
812 iput(inode);
813 iput(out_inode);
814 set_fs(fs);
815 return;
816 free_inodes: /* free inodes on error */
817 iput(out_inode);
818 infile.f_op->release(inode, &infile);
819 free_inode:
820 iput(inode);
821 }
822
823 #ifdef CONFIG_MAC_FLOPPY
824 int swim3_fd_eject(int devnum);
825 #endif
826
827 static void __init rd_load_disk(int n)
828 {
829 #ifdef CONFIG_BLK_DEV_INITRD
830 extern kdev_t real_root_dev;
831 #endif
832
833 if (rd_doload == 0)
834 return;
835
836 if (MAJOR(ROOT_DEV) != FLOPPY_MAJOR
837 #ifdef CONFIG_BLK_DEV_INITRD
838 && MAJOR(real_root_dev) != FLOPPY_MAJOR
839 #endif
840 )
841 return;
842
843 if (rd_prompt) {
844 #ifdef CONFIG_BLK_DEV_FD
845 floppy_eject();
846 #endif
847 #ifdef CONFIG_MAC_FLOPPY
848 if(MAJOR(ROOT_DEV) == FLOPPY_MAJOR)
849 swim3_fd_eject(MINOR(ROOT_DEV));
850 else if(MAJOR(real_root_dev) == FLOPPY_MAJOR)
851 swim3_fd_eject(MINOR(real_root_dev));
852 #endif
853 printk(KERN_NOTICE
854 "VFS: Insert root floppy disk to be loaded into RAM disk and press ENTER\n");
855 wait_for_keypress();
856 }
857
858 rd_load_image(ROOT_DEV,rd_image_start, n);
859
860 }
861
862 void __init rd_load(void)
863 {
864 rd_load_disk(0);
865 }
866
867 void __init rd_load_secondary(void)
868 {
869 rd_load_disk(1);
870 }
871
872 #ifdef CONFIG_BLK_DEV_INITRD
873 void __init initrd_load(void)
874 {
875 rd_load_image(MKDEV(MAJOR_NR, INITRD_MINOR),rd_image_start,0);
876 }
877 #endif
878
879 #endif /* RD_LOADER */
880
881 #ifdef BUILD_CRAMDISK
882
883 /*
884 * gzip declarations
885 */
886
887 #define OF(args) args
888
889 #ifndef memzero
890 #define memzero(s, n) memset ((s), 0, (n))
891 #endif
892
893 typedef unsigned char uch;
894 typedef unsigned short ush;
895 typedef unsigned long ulg;
896
897 #define INBUFSIZ 4096
898 #define WSIZE 0x8000 /* window size--must be a power of two, and */
899 /* at least 32K for zip's deflate method */
900
901 static uch *inbuf;
902 static uch *window;
903
904 static unsigned insize; /* valid bytes in inbuf */
905 static unsigned inptr; /* index of next byte to be processed in inbuf */
906 static unsigned outcnt; /* bytes in output buffer */
907 static int exit_code;
908 static long bytes_out;
909 static struct file *crd_infp, *crd_outfp;
910
911 #define get_byte() (inptr < insize ? inbuf[inptr++] : fill_inbuf())
912
913 /* Diagnostic functions (stubbed out) */
914 #define Assert(cond,msg)
915 #define Trace(x)
916 #define Tracev(x)
917 #define Tracevv(x)
918 #define Tracec(c,x)
919 #define Tracecv(c,x)
920
921 #define STATIC static
922
923 static int fill_inbuf(void);
924 static void flush_window(void);
925 static void *malloc(int size);
926 static void free(void *where);
927 static void error(char *m);
928 static void gzip_mark(void **);
929 static void gzip_release(void **);
930
931 #include "../../lib/inflate.c"
932
933 static void __init *malloc(int size)
934 {
935 return kmalloc(size, GFP_KERNEL);
936 }
937
938 static void __init free(void *where)
939 {
940 kfree(where);
941 }
942
943 static void __init gzip_mark(void **ptr)
944 {
945 }
946
947 static void __init gzip_release(void **ptr)
948 {
949 }
950
951
952 /* ===========================================================================
953 * Fill the input buffer. This is called only when the buffer is empty
954 * and at least one byte is really needed.
955 */
956 static int __init fill_inbuf(void)
957 {
958 if (exit_code) return -1;
959
960 insize = crd_infp->f_op->read(crd_infp, inbuf, INBUFSIZ,
961 &crd_infp->f_pos);
962 if (insize == 0) return -1;
963
964 inptr = 1;
965
966 return inbuf[0];
967 }
968
969 /* ===========================================================================
970 * Write the output window window[0..outcnt-1] and update crc and bytes_out.
971 * (Used for the decompressed data only.)
972 */
973 static void __init flush_window(void)
974 {
975 ulg c = crc; /* temporary variable */
976 unsigned n;
977 uch *in, ch;
978
979 crd_outfp->f_op->write(crd_outfp, window, outcnt, &crd_outfp->f_pos);
980 in = window;
981 for (n = 0; n < outcnt; n++) {
982 ch = *in++;
983 c = crc_32_tab[((int)c ^ ch) & 0xff] ^ (c >> 8);
984 }
985 crc = c;
986 bytes_out += (ulg)outcnt;
987 outcnt = 0;
988 }
989
990 static void __init error(char *x)
991 {
992 printk(KERN_ERR "%s", x);
993 exit_code = 1;
994 }
995
996 static int __init
997 crd_load(struct file * fp, struct file *outfp)
998 {
999 int result;
1000
1001 insize = 0; /* valid bytes in inbuf */
1002 inptr = 0; /* index of next byte to be processed in inbuf */
1003 outcnt = 0; /* bytes in output buffer */
1004 exit_code = 0;
1005 bytes_out = 0;
1006 crc = (ulg)0xffffffffL; /* shift register contents */
1007
1008 crd_infp = fp;
1009 crd_outfp = outfp;
1010 inbuf = kmalloc(INBUFSIZ, GFP_KERNEL);
1011 if (inbuf == 0) {
1012 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip buffer\n");
1013 return -1;
1014 }
1015 window = kmalloc(WSIZE, GFP_KERNEL);
1016 if (window == 0) {
1017 printk(KERN_ERR "RAMDISK: Couldn't allocate gzip window\n");
1018 kfree(inbuf);
1019 return -1;
1020 }
1021 makecrc();
1022 result = gunzip();
1023 kfree(inbuf);
1024 kfree(window);
1025 return result;
1026 }
1027
1028 #endif /* BUILD_CRAMDISK */
1029
1030