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/string.h>
47 #include <linux/slab.h>
48 #include <linux/module.h>
49 #include <linux/init.h>
50 #include <linux/devfs_fs_kernel.h>
51 #include <linux/smp_lock.h>
52 #include <asm/uaccess.h>
53 
54 /*
55  * 35 has been officially registered as the RAMDISK major number, but
56  * so is the original MAJOR number of 1.  We're using 1 in
57  * include/linux/major.h for now
58  */
59 #define MAJOR_NR RAMDISK_MAJOR
60 #include <linux/blk.h>
61 #include <linux/blkpg.h>
62 
63 /* The RAM disk size is now a parameter */
64 #define NUM_RAMDISKS 16		/* This cannot be overridden (yet) */
65 
66 #ifdef CONFIG_BLK_DEV_INITRD
67 static int initrd_users;
68 unsigned long initrd_start, initrd_end;
69 int initrd_below_start_ok;
70 #endif
71 
72 /* Various static variables go here.  Most are used only in the RAM disk code.
73  */
74 
75 static unsigned long rd_length[NUM_RAMDISKS];	/* Size of RAM disks in bytes   */
76 static int rd_hardsec[NUM_RAMDISKS];		/* Size of real blocks in bytes */
77 static int rd_blocksizes[NUM_RAMDISKS];		/* Size of 1024 byte blocks :)  */
78 static int rd_kbsize[NUM_RAMDISKS];		/* Size in blocks of 1024 bytes */
79 static devfs_handle_t devfs_handle;
80 static struct block_device *rd_bdev[NUM_RAMDISKS];/* Protected device data */
81 
82 /*
83  * Parameters for the boot-loading of the RAM disk.  These are set by
84  * init/main.c (from arguments to the kernel command line) or from the
85  * architecture-specific setup routine (from the stored boot sector
86  * information).
87  */
88 int rd_size = CONFIG_BLK_DEV_RAM_SIZE;		/* Size of the RAM disks */
89 /*
90  * It would be very desirable to have a soft-blocksize (that in the case
91  * of the ramdisk driver is also the hardblocksize ;) of PAGE_SIZE because
92  * doing that we'll achieve a far better MM footprint. Using a rd_blocksize of
93  * BLOCK_SIZE in the worst case we'll make PAGE_SIZE/BLOCK_SIZE buffer-pages
94  * unfreeable. With a rd_blocksize of PAGE_SIZE instead we are sure that only
95  * 1 page will be protected. Depending on the size of the ramdisk you
96  * may want to change the ramdisk blocksize to achieve a better or worse MM
97  * behaviour. The default is still BLOCK_SIZE (needed by rd_load_image that
98  * supposes the filesystem in the image uses a BLOCK_SIZE blocksize).
99  */
100 int rd_blocksize = BLOCK_SIZE;			/* blocksize of the RAM disks */
101 
102 /*
103  * Copyright (C) 2000 Linus Torvalds.
104  *               2000 Transmeta Corp.
105  * aops copied from ramfs.
106  */
ramdisk_updatepage(struct page * page,int need_kmap)107 static void ramdisk_updatepage(struct page * page, int need_kmap)
108 {
109 	if (!Page_Uptodate(page)) {
110 		struct buffer_head *bh = page->buffers;
111 		void * address;
112 
113 		if (need_kmap)
114 			kmap(page);
115 		address = page_address(page);
116 		if (bh) {
117 			struct buffer_head *tmp = bh;
118 			do {
119 				if (!buffer_uptodate(tmp)) {
120 					memset(address, 0, tmp->b_size);
121 					mark_buffer_uptodate(tmp, 1);
122 				}
123 				address += tmp->b_size;
124 				tmp = tmp->b_this_page;
125 			} while (tmp != bh);
126 		} else
127 			memset(address, 0, PAGE_CACHE_SIZE);
128 		if (need_kmap)
129 			kunmap(page);
130 		flush_dcache_page(page);
131 		SetPageUptodate(page);
132 	}
133 }
134 
ramdisk_readpage(struct file * file,struct page * page)135 static int ramdisk_readpage(struct file *file, struct page * page)
136 {
137 	ramdisk_updatepage(page, 1);
138 	UnlockPage(page);
139 	return 0;
140 }
141 
ramdisk_prepare_write(struct file * file,struct page * page,unsigned offset,unsigned to)142 static int ramdisk_prepare_write(struct file *file, struct page *page, unsigned offset, unsigned to)
143 {
144 	ramdisk_updatepage(page, 0);
145 	SetPageDirty(page);
146 	return 0;
147 }
148 
ramdisk_commit_write(struct file * file,struct page * page,unsigned offset,unsigned to)149 static int ramdisk_commit_write(struct file *file, struct page *page, unsigned offset, unsigned to)
150 {
151 	return 0;
152 }
153 
154 static struct address_space_operations ramdisk_aops = {
155 	readpage: ramdisk_readpage,
156 	writepage: fail_writepage,
157 	prepare_write: ramdisk_prepare_write,
158 	commit_write: ramdisk_commit_write,
159 };
160 
rd_blkdev_pagecache_IO(int rw,struct buffer_head * sbh,int minor)161 static int rd_blkdev_pagecache_IO(int rw, struct buffer_head * sbh, int minor)
162 {
163 	struct address_space * mapping;
164 	unsigned long index;
165 	int offset, size, err;
166 
167 	err = 0;
168 	mapping = rd_bdev[minor]->bd_inode->i_mapping;
169 
170 	/* writing a buffer cache not uptodate must not clear it */
171 	if (sbh->b_page->mapping == mapping) {
172 		if (rw == WRITE) {
173 			mark_buffer_uptodate(sbh, 1);
174 			SetPageDirty(sbh->b_page);
175 		}
176 		goto out;
177 	}
178 
179 	index = sbh->b_rsector >> (PAGE_CACHE_SHIFT - 9);
180 	offset = (sbh->b_rsector << 9) & ~PAGE_CACHE_MASK;
181 	size = sbh->b_size;
182 
183 	do {
184 		int count;
185 		struct page * page;
186 		char * src, * dst;
187 
188 		count = PAGE_CACHE_SIZE - offset;
189 		if (count > size)
190 			count = size;
191 		size -= count;
192 
193 		page = grab_cache_page(mapping, index);
194 		if (!page) {
195 			err = -ENOMEM;
196 			goto out;
197 		}
198 
199 		ramdisk_updatepage(page, 1);
200 
201 		index++;
202 
203 		if (rw == READ) {
204 			src = kmap(page);
205 			src += offset;
206 			dst = bh_kmap(sbh);
207 		} else {
208 			dst = kmap(page);
209 			dst += offset;
210 			src = bh_kmap(sbh);
211 		}
212 		offset = 0;
213 
214 		memcpy(dst, src, count);
215 
216 		kunmap(page);
217 		bh_kunmap(sbh);
218 
219 		if (rw == READ) {
220 			flush_dcache_page(sbh->b_page);
221 		} else {
222 			SetPageDirty(page);
223 		}
224 		UnlockPage(page);
225 		__free_page(page);
226 	} while (size);
227 
228  out:
229 	return err;
230 }
231 
232 /*
233  *  Basically, my strategy here is to set up a buffer-head which can't be
234  *  deleted, and make that my Ramdisk.  If the request is outside of the
235  *  allocated size, we must get rid of it...
236  *
237  * 19-JAN-1998  Richard Gooch <rgooch@atnf.csiro.au>  Added devfs support
238  *
239  */
rd_make_request(request_queue_t * q,int rw,struct buffer_head * sbh)240 static int rd_make_request(request_queue_t * q, int rw, struct buffer_head *sbh)
241 {
242 	unsigned int minor;
243 	unsigned long offset, len;
244 
245 	minor = MINOR(sbh->b_rdev);
246 
247 	if (minor >= NUM_RAMDISKS)
248 		goto fail;
249 
250 
251 	offset = sbh->b_rsector << 9;
252 	len = sbh->b_size;
253 
254 	if ((offset + len) > rd_length[minor])
255 		goto fail;
256 
257 	if (rw==READA)
258 		rw=READ;
259 	if ((rw != READ) && (rw != WRITE)) {
260 		printk(KERN_INFO "RAMDISK: bad command: %d\n", rw);
261 		goto fail;
262 	}
263 
264 	if (rd_blkdev_pagecache_IO(rw, sbh, minor))
265 		goto fail;
266 
267 	sbh->b_end_io(sbh,1);
268 	return 0;
269  fail:
270 	buffer_IO_error(sbh);
271 	return 0;
272 }
273 
rd_ioctl(struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)274 static int rd_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
275 {
276 	int error = -EINVAL;
277 	unsigned int minor;
278 
279 	if (!inode || !inode->i_rdev)
280 		goto out;
281 
282 	minor = MINOR(inode->i_rdev);
283 
284 	switch (cmd) {
285 		case BLKFLSBUF:
286 			if (!capable(CAP_SYS_ADMIN))
287 				return -EACCES;
288 			/* special: we want to release the ramdisk memory,
289 			   it's not like with the other blockdevices where
290 			   this ioctl only flushes away the buffer cache. */
291 			error = -EBUSY;
292 			down(&inode->i_bdev->bd_sem);
293 			if (inode->i_bdev->bd_openers <= 2) {
294 				truncate_inode_pages(inode->i_mapping, 0);
295 				error = 0;
296 			}
297 			up(&inode->i_bdev->bd_sem);
298 			invalidate_buffers(inode->i_rdev);
299 			break;
300          	case BLKGETSIZE:   /* Return device size */
301 			if (!arg)
302 				break;
303 			error = put_user(rd_kbsize[minor] << 1, (unsigned long *) arg);
304 			break;
305          	case BLKGETSIZE64:
306 			error = put_user((u64)rd_kbsize[minor]<<10, (u64*)arg);
307 			break;
308 		case BLKROSET:
309 		case BLKROGET:
310 		case BLKSSZGET:
311 			error = blk_ioctl(inode->i_rdev, cmd, arg);
312 	};
313 out:
314 	return error;
315 }
316 
317 
318 #ifdef CONFIG_BLK_DEV_INITRD
319 
initrd_read(struct file * file,char * buf,size_t count,loff_t * ppos)320 static ssize_t initrd_read(struct file *file, char *buf,
321 			   size_t count, loff_t *ppos)
322 {
323 	loff_t n = *ppos;
324 	unsigned pos = n;
325 	unsigned left = initrd_end - initrd_start;
326 
327 	if (pos != n || pos >= left)
328 		return 0;
329 
330 	left -= pos;
331 	if (count > left) count = left;
332 	if (count == 0) return 0;
333 	if (copy_to_user(buf, (char *)initrd_start + pos, count))
334 		return -EFAULT;
335 	*ppos = pos + count;
336 	return count;
337 }
338 
339 
initrd_release(struct inode * inode,struct file * file)340 static int initrd_release(struct inode *inode,struct file *file)
341 {
342 	extern void free_initrd_mem(unsigned long, unsigned long);
343 
344 	lock_kernel();
345 	if (!--initrd_users) {
346 		free_initrd_mem(initrd_start, initrd_end);
347 		initrd_start = 0;
348 	}
349 	unlock_kernel();
350 	blkdev_put(inode->i_bdev, BDEV_FILE);
351 	return 0;
352 }
353 
354 
355 static struct file_operations initrd_fops = {
356 	read:		initrd_read,
357 	release:	initrd_release,
358 };
359 
360 #endif
361 
362 
rd_open(struct inode * inode,struct file * filp)363 static int rd_open(struct inode * inode, struct file * filp)
364 {
365 	int unit = DEVICE_NR(inode->i_rdev);
366 
367 #ifdef CONFIG_BLK_DEV_INITRD
368 	if (unit == INITRD_MINOR) {
369 		if (!initrd_start) return -ENODEV;
370 		initrd_users++;
371 		filp->f_op = &initrd_fops;
372 		return 0;
373 	}
374 #endif
375 
376 	if (unit >= NUM_RAMDISKS)
377 		return -ENXIO;
378 
379 	/*
380 	 * Immunize device against invalidate_buffers() and prune_icache().
381 	 */
382 	if (rd_bdev[unit] == NULL) {
383 		rd_bdev[unit] = bdget(kdev_t_to_nr(inode->i_rdev));
384 		rd_bdev[unit]->bd_openers++;
385 		rd_bdev[unit]->bd_inode->i_mapping->a_ops = &ramdisk_aops;
386 	}
387 
388 	return 0;
389 }
390 
391 static struct block_device_operations rd_bd_op = {
392 	owner:		THIS_MODULE,
393 	open:		rd_open,
394 	ioctl:		rd_ioctl,
395 };
396 
397 /* Before freeing the module, invalidate all of the protected buffers! */
rd_cleanup(void)398 static void __exit rd_cleanup (void)
399 {
400 	int i;
401 
402 	for (i = 0 ; i < NUM_RAMDISKS; i++) {
403 		struct block_device *bdev = rd_bdev[i];
404 		rd_bdev[i] = NULL;
405 		if (bdev)
406 			blkdev_put(bdev, BDEV_FILE);
407 		destroy_buffers(MKDEV(MAJOR_NR, i));
408 	}
409 
410 	devfs_unregister (devfs_handle);
411 	unregister_blkdev( MAJOR_NR, "ramdisk" );
412 	hardsect_size[MAJOR_NR] = NULL;
413 	blksize_size[MAJOR_NR] = NULL;
414 	blk_size[MAJOR_NR] = NULL;
415 }
416 
417 /* This is the registration and initialization section of the RAM disk driver */
rd_init(void)418 static int __init rd_init (void)
419 {
420 	int		i;
421 
422 	if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
423 	    (rd_blocksize & (rd_blocksize-1)))
424 	{
425 		printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
426 		       rd_blocksize);
427 		rd_blocksize = BLOCK_SIZE;
428 	}
429 
430 	if (register_blkdev(MAJOR_NR, "ramdisk", &rd_bd_op)) {
431 		printk("RAMDISK: Could not get major %d", MAJOR_NR);
432 		return -EIO;
433 	}
434 
435 	blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), &rd_make_request);
436 
437 	for (i = 0; i < NUM_RAMDISKS; i++) {
438 		/* rd_size is given in kB */
439 		rd_length[i] = rd_size << 10;
440 		rd_hardsec[i] = rd_blocksize;
441 		rd_blocksizes[i] = rd_blocksize;
442 		rd_kbsize[i] = rd_size;
443 	}
444 	devfs_handle = devfs_mk_dir (NULL, "rd", NULL);
445 	devfs_register_series (devfs_handle, "%u", NUM_RAMDISKS,
446 			       DEVFS_FL_DEFAULT, MAJOR_NR, 0,
447 			       S_IFBLK | S_IRUSR | S_IWUSR,
448 			       &rd_bd_op, NULL);
449 
450 	for (i = 0; i < NUM_RAMDISKS; i++)
451 		register_disk(NULL, MKDEV(MAJOR_NR,i), 1, &rd_bd_op, rd_size<<1);
452 
453 #ifdef CONFIG_BLK_DEV_INITRD
454 	/* We ought to separate initrd operations here */
455 	register_disk(NULL, MKDEV(MAJOR_NR,INITRD_MINOR), 1, &rd_bd_op, rd_size<<1);
456 	devfs_register(devfs_handle, "initrd", DEVFS_FL_DEFAULT, MAJOR_NR,
457 			INITRD_MINOR, S_IFBLK | S_IRUSR, &rd_bd_op, NULL);
458 #endif
459 
460 	hardsect_size[MAJOR_NR] = rd_hardsec;		/* Size of the RAM disk blocks */
461 	blksize_size[MAJOR_NR] = rd_blocksizes;		/* Avoid set_blocksize() check */
462 	blk_size[MAJOR_NR] = rd_kbsize;			/* Size of the RAM disk in kB  */
463 
464 		/* rd_size is given in kB */
465 	printk("RAMDISK driver initialized: "
466 	       "%d RAM disks of %dK size %d blocksize\n",
467 	       NUM_RAMDISKS, rd_size, rd_blocksize);
468 
469 	return 0;
470 }
471 
472 module_init(rd_init);
473 module_exit(rd_cleanup);
474 
475 /* options - nonmodular */
476 #ifndef MODULE
ramdisk_size(char * str)477 static int __init ramdisk_size(char *str)
478 {
479 	rd_size = simple_strtol(str,NULL,0);
480 	return 1;
481 }
ramdisk_size2(char * str)482 static int __init ramdisk_size2(char *str)	/* kludge */
483 {
484 	return ramdisk_size(str);
485 }
ramdisk_blocksize(char * str)486 static int __init ramdisk_blocksize(char *str)
487 {
488 	rd_blocksize = simple_strtol(str,NULL,0);
489 	return 1;
490 }
491 __setup("ramdisk=", ramdisk_size);
492 __setup("ramdisk_size=", ramdisk_size2);
493 __setup("ramdisk_blocksize=", ramdisk_blocksize);
494 #endif
495 
496 /* options - modular */
497 MODULE_PARM     (rd_size, "1i");
498 MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
499 MODULE_PARM     (rd_blocksize, "i");
500 MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
501 
502 MODULE_LICENSE("GPL");
503