⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rd.c

📁 讲述linux的初始化过程
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * ramdisk.c - Multiple RAM disk driver - gzip-loading version - v. 0.8 beta. *  * (C) Chad Page, Theodore Ts'o, et. al, 1995.  * * This RAM disk is designed to have filesystems created on it and mounted * just like a regular floppy disk.   *   * It also does something suggested by Linus: use the buffer cache as the * RAM disk data.  This makes it possible to dynamically allocate the RAM disk * buffer - with some consequences I have to deal with as I write this.  *  * This code is based on the original ramdisk.c, written mostly by * Theodore Ts'o (TYT) in 1991.  The code was largely rewritten by * Chad Page to use the buffer cache to store the RAM disk data in * 1995; Theodore then took over the driver again, and cleaned it up * for inclusion in the mainline kernel. * * The original CRAMDISK code was written by Richard Lyons, and * adapted by Chad Page to use the new RAM disk interface.  Theodore * Ts'o rewrote it so that both the compressed RAM disk loader and the * kernel decompressor uses the same inflate.c codebase.  The RAM disk * loader now also loads into a dynamic (buffer cache based) RAM disk, * not the old static RAM disk.  Support for the old static RAM disk has * been completely removed. * * Loadable module support added by Tom Dyas. * * Further cleanups by Chad Page (page0588@sundance.sjsu.edu): *	Cosmetic changes in #ifdef MODULE, code movement, etc. * 	When the RAM disk module is removed, free the protected buffers * 	Default RAM disk size changed to 2.88 MB * *  Added initrd: Werner Almesberger & Hans Lermen, Feb '96 * * 4/25/96 : Made RAM disk size a parameter (default is now 4 MB)  *		- Chad Page * * Add support for fs images split across >1 disk, Paul Gortmaker, Mar '98 * * Make block size and block size shift for RAM disks a global macro * and set blk_size for -ENOSPC,     Werner Fink <werner@suse.de>, Apr '99 */#include <linux/config.h>#include <linux/sched.h>#include <linux/minix_fs.h>#include <linux/ext2_fs.h>#include <linux/romfs_fs.h>#include <linux/fs.h>#include <linux/kernel.h>#include <linux/hdreg.h>#include <linux/string.h>#include <linux/mm.h>#include <linux/mman.h>#include <linux/malloc.h>#include <linux/ioctl.h>#include <linux/fd.h>#include <linux/module.h>#include <linux/init.h>#include <linux/devfs_fs_kernel.h>#include <linux/smp_lock.h>#include <asm/system.h>#include <asm/uaccess.h>#include <asm/byteorder.h>extern void wait_for_keypress(void);/* * 35 has been officially registered as the RAMDISK major number, but * so is the original MAJOR number of 1.  We're using 1 in * include/linux/major.h for now */#define MAJOR_NR RAMDISK_MAJOR#include <linux/blk.h>#include <linux/blkpg.h>/* The RAM disk size is now a parameter */#define NUM_RAMDISKS 16		/* This cannot be overridden (yet) */ #ifndef MODULE/* We don't have to load RAM disks or gunzip them in a module. */#define RD_LOADER#define BUILD_CRAMDISKvoid rd_load(void);static int crd_load(struct file *fp, struct file *outfp);#ifdef CONFIG_BLK_DEV_INITRDstatic int initrd_users;#endif#endif/* Various static variables go here.  Most are used only in the RAM disk code. */static unsigned long rd_length[NUM_RAMDISKS];	/* Size of RAM disks in bytes   */static int rd_hardsec[NUM_RAMDISKS];		/* Size of real blocks in bytes */static int rd_blocksizes[NUM_RAMDISKS];		/* Size of 1024 byte blocks :)  */static int rd_kbsize[NUM_RAMDISKS];		/* Size in blocks of 1024 bytes */static devfs_handle_t devfs_handle;static struct inode *rd_inode[NUM_RAMDISKS];	/* Protected device inodes *//* * Parameters for the boot-loading of the RAM disk.  These are set by * init/main.c (from arguments to the kernel command line) or from the * architecture-specific setup routine (from the stored boot sector * information).  */int rd_size = CONFIG_BLK_DEV_RAM_SIZE;		/* Size of the RAM disks *//* * It would be very desiderable to have a soft-blocksize (that in the case * of the ramdisk driver is also the hardblocksize ;) of PAGE_SIZE because * doing that we'll achieve a far better MM footprint. Using a rd_blocksize of * BLOCK_SIZE in the worst case we'll make PAGE_SIZE/BLOCK_SIZE buffer-pages * unfreeable. With a rd_blocksize of PAGE_SIZE instead we are sure that only * 1 page will be protected. Depending on the size of the ramdisk you * may want to change the ramdisk blocksize to achieve a better or worse MM * behaviour. The default is still BLOCK_SIZE (needed by rd_load_image that * supposes the filesystem in the image uses a BLOCK_SIZE blocksize). */int rd_blocksize = BLOCK_SIZE;			/* blocksize of the RAM disks */#ifndef MODULEint rd_doload;			/* 1 = load RAM disk, 0 = don't load */int rd_prompt = 1;		/* 1 = prompt for RAM disk, 0 = don't prompt */int rd_image_start;		/* starting block # of image */#ifdef CONFIG_BLK_DEV_INITRDunsigned long initrd_start, initrd_end;int mount_initrd = 1;		/* zero if initrd should not be mounted */int initrd_below_start_ok;static int __init no_initrd(char *str){	mount_initrd = 0;	return 1;}__setup("noinitrd", no_initrd);#endifstatic int __init ramdisk_start_setup(char *str){	rd_image_start = simple_strtol(str,NULL,0);	return 1;}static int __init load_ramdisk(char *str){	rd_doload = simple_strtol(str,NULL,0) & 3;	return 1;}static int __init prompt_ramdisk(char *str){	rd_prompt = simple_strtol(str,NULL,0) & 1;	return 1;}static int __init ramdisk_size(char *str){	rd_size = simple_strtol(str,NULL,0);	return 1;}static int __init ramdisk_size2(char *str){	return ramdisk_size(str);}static int __init ramdisk_blocksize(char *str){	rd_blocksize = simple_strtol(str,NULL,0);	return 1;}__setup("ramdisk_start=", ramdisk_start_setup);__setup("load_ramdisk=", load_ramdisk);__setup("prompt_ramdisk=", prompt_ramdisk);__setup("ramdisk=", ramdisk_size);__setup("ramdisk_size=", ramdisk_size2);__setup("ramdisk_blocksize=", ramdisk_blocksize);#endif/* *  Basically, my strategy here is to set up a buffer-head which can't be *  deleted, and make that my Ramdisk.  If the request is outside of the *  allocated size, we must get rid of it... * * 19-JAN-1998  Richard Gooch <rgooch@atnf.csiro.au>  Added devfs support * */static int rd_make_request(request_queue_t * q, int rw, struct buffer_head *sbh){	unsigned int minor;	unsigned long offset, len;	struct buffer_head *rbh;	char *bdata;		minor = MINOR(sbh->b_rdev);	if (minor >= NUM_RAMDISKS)		goto fail;		offset = sbh->b_rsector << 9;	len = sbh->b_size;	if ((offset + len) > rd_length[minor])		goto fail;	if (rw==READA)		rw=READ;	if ((rw != READ) && (rw != WRITE)) {		printk(KERN_INFO "RAMDISK: bad command: %d\n", rw);		goto fail;	}	rbh = getblk(sbh->b_rdev, sbh->b_rsector/(sbh->b_size>>9), sbh->b_size);	/* I think that it is safe to assume that rbh is not in HighMem, though	 * sbh might be - NeilBrown	 */	bdata = bh_kmap(sbh);	if (rw == READ) {		if (sbh != rbh)			memcpy(bdata, rbh->b_data, rbh->b_size);	} else		if (sbh != rbh)			memcpy(rbh->b_data, bdata, rbh->b_size);	bh_kunmap(sbh);	mark_buffer_protected(rbh);	brelse(rbh);	sbh->b_end_io(sbh,1);	return 0; fail:	sbh->b_end_io(sbh,0);	return 0;} static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){	unsigned int minor;	if (!inode || !inode->i_rdev) 			return -EINVAL;	minor = MINOR(inode->i_rdev);	switch (cmd) {		case BLKFLSBUF:			if (!capable(CAP_SYS_ADMIN))				return -EACCES;			/* special: we want to release the ramdisk memory,			   it's not like with the other blockdevices where			   this ioctl only flushes away the buffer cache. */			if ((atomic_read(&inode->i_bdev->bd_openers) > 2))				return -EBUSY;			destroy_buffers(inode->i_rdev);			rd_blocksizes[minor] = 0;			break;         	case BLKGETSIZE:   /* Return device size */			if (!arg)  return -EINVAL;			return put_user(rd_kbsize[minor] << 1, (long *) arg);		case BLKROSET:		case BLKROGET:		case BLKSSZGET:			return blk_ioctl(inode->i_rdev, cmd, arg);		default:			return -EINVAL;	};	return 0;}#ifdef CONFIG_BLK_DEV_INITRDstatic ssize_t initrd_read(struct file *file, char *buf,			   size_t count, loff_t *ppos){	int left;	left = initrd_end - initrd_start - *ppos;	if (count > left) count = left;	if (count == 0) return 0;	copy_to_user(buf, (char *)initrd_start + *ppos, count);	*ppos += count;	return count;}static int initrd_release(struct inode *inode,struct file *file){	extern void free_initrd_mem(unsigned long, unsigned long);	lock_kernel();	if (!--initrd_users) {		blkdev_put(inode->i_bdev, BDEV_FILE);		iput(inode);		free_initrd_mem(initrd_start, initrd_end);		initrd_start = 0;	}	unlock_kernel();	return 0;}static struct file_operations initrd_fops = {	read:		initrd_read,	release:	initrd_release,};#endifstatic int rd_open(struct inode * inode, struct file * filp){#ifdef CONFIG_BLK_DEV_INITRD	if (DEVICE_NR(inode->i_rdev) == INITRD_MINOR) {		if (!initrd_start) return -ENODEV;		initrd_users++;		filp->f_op = &initrd_fops;		return 0;	}#endif	if (DEVICE_NR(inode->i_rdev) >= NUM_RAMDISKS)		return -ENXIO;	/*	 * Immunize device against invalidate_buffers() and prune_icache().	 */	if (rd_inode[DEVICE_NR(inode->i_rdev)] == NULL) {		if (!inode->i_bdev) return -ENXIO;		if ((rd_inode[DEVICE_NR(inode->i_rdev)] = igrab(inode)) != NULL)			atomic_inc(&rd_inode[DEVICE_NR(inode->i_rdev)]->i_bdev->bd_openers);	}	MOD_INC_USE_COUNT;	return 0;}static int rd_release(struct inode * inode, struct file * filp){	MOD_DEC_USE_COUNT;	return 0;}static struct block_device_operations fd_fops = {	open:		rd_open,	release:	rd_release,	ioctl:		rd_ioctl,};#ifdef MODULE/* Before freeing the module, invalidate all of the protected buffers! */static void __exit rd_cleanup (void){	int i;	for (i = 0 ; i < NUM_RAMDISKS; i++) {		if (rd_inode[i]) {			/* withdraw invalidate_buffers() and prune_icache() immunity */			atomic_dec(&rd_inode[i]->i_bdev->bd_openers);			/* remove stale pointer to module address space */			rd_inode[i]->i_bdev->bd_op = NULL;			iput(rd_inode[i]);		}		destroy_buffers(MKDEV(MAJOR_NR, i));	}	devfs_unregister (devfs_handle);	unregister_blkdev( MAJOR_NR, "ramdisk" );	hardsect_size[MAJOR_NR] = NULL;	blksize_size[MAJOR_NR] = NULL;	blk_size[MAJOR_NR] = NULL;}#endif/* This is the registration and initialization section of the RAM disk driver */int __init rd_init (void){	int		i;	if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||	    (rd_blocksize & (rd_blocksize-1)))	{		printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",		       rd_blocksize);		rd_blocksize = BLOCK_SIZE;	}	if (register_blkdev(MAJOR_NR, "ramdisk", &fd_fops)) {		printk("RAMDISK: Could not get major %d", MAJOR_NR);		return -EIO;	}	blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), &rd_make_request);	for (i = 0; i < NUM_RAMDISKS; i++) {		/* rd_size is given in kB */		rd_length[i] = rd_size << 10;		rd_hardsec[i] = rd_blocksize;		rd_blocksizes[i] = rd_blocksize;		rd_kbsize[i] = rd_size;	}	devfs_handle = devfs_mk_dir (NULL, "rd", NULL);	devfs_register_series (devfs_handle, "%u", NUM_RAMDISKS,			       DEVFS_FL_DEFAULT, MAJOR_NR, 0,			       S_IFBLK | S_IRUSR | S_IWUSR,			       &fd_fops, NULL);	for (i = 0; i < NUM_RAMDISKS; i++)		register_disk(NULL, MKDEV(MAJOR_NR,i), 1, &fd_fops, rd_size<<1);#ifdef CONFIG_BLK_DEV_INITRD	/* We ought to separate initrd operations here */	register_disk(NULL, MKDEV(MAJOR_NR,INITRD_MINOR), 1, &fd_fops, rd_size<<1);#endif	hardsect_size[MAJOR_NR] = rd_hardsec;		/* Size of the RAM disk blocks */	blksize_size[MAJOR_NR] = rd_blocksizes;		/* Avoid set_blocksize() check */	blk_size[MAJOR_NR] = rd_kbsize;			/* Size of the RAM disk in kB  */		/* rd_size is given in kB */	printk("RAMDISK driver initialized: "	       "%d RAM disks of %dK size %d blocksize\n",	       NUM_RAMDISKS, rd_size, rd_blocksize);	return 0;}#ifdef MODULEmodule_init(rd_init);module_exit(rd_cleanup);#endif/* loadable module support */MODULE_PARM     (rd_size, "1i");MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");MODULE_PARM     (rd_blocksize, "i");MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");/* End of non-loading portions of the RAM disk driver */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -