OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [block/] [rd.c] - Blame information for rev 1275

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1275 phoenix
/*
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
 */
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
 
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
 
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
 
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
 
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
 */
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
 
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
 
320
static ssize_t initrd_read(struct file *file, char *buf,
321
                           size_t count, loff_t *ppos)
322
{
323
        int left;
324
 
325
        left = initrd_end - initrd_start - *ppos;
326
        if (count > left) count = left;
327
        if (count == 0) return 0;
328
        if (copy_to_user(buf, (char *)initrd_start + *ppos, count))
329
                return -EFAULT;
330
        *ppos += count;
331
        return count;
332
}
333
 
334
 
335
static int initrd_release(struct inode *inode,struct file *file)
336
{
337
        extern void free_initrd_mem(unsigned long, unsigned long);
338
 
339
        lock_kernel();
340
        if (!--initrd_users) {
341
                free_initrd_mem(initrd_start, initrd_end);
342
                initrd_start = 0;
343
        }
344
        unlock_kernel();
345
        blkdev_put(inode->i_bdev, BDEV_FILE);
346
        return 0;
347
}
348
 
349
 
350
static struct file_operations initrd_fops = {
351
        read:           initrd_read,
352
        release:        initrd_release,
353
};
354
 
355
#endif
356
 
357
 
358
static int rd_open(struct inode * inode, struct file * filp)
359
{
360
        int unit = DEVICE_NR(inode->i_rdev);
361
 
362
#ifdef CONFIG_BLK_DEV_INITRD
363
        if (unit == INITRD_MINOR) {
364
                if (!initrd_start) return -ENODEV;
365
                initrd_users++;
366
                filp->f_op = &initrd_fops;
367
                return 0;
368
        }
369
#endif
370
 
371
        if (unit >= NUM_RAMDISKS)
372
                return -ENXIO;
373
 
374
        /*
375
         * Immunize device against invalidate_buffers() and prune_icache().
376
         */
377
        if (rd_bdev[unit] == NULL) {
378
                rd_bdev[unit] = bdget(kdev_t_to_nr(inode->i_rdev));
379
                rd_bdev[unit]->bd_openers++;
380
                rd_bdev[unit]->bd_inode->i_mapping->a_ops = &ramdisk_aops;
381
        }
382
 
383
        return 0;
384
}
385
 
386
static struct block_device_operations rd_bd_op = {
387
        owner:          THIS_MODULE,
388
        open:           rd_open,
389
        ioctl:          rd_ioctl,
390
};
391
 
392
/* Before freeing the module, invalidate all of the protected buffers! */
393
static void __exit rd_cleanup (void)
394
{
395
        int i;
396
 
397
        for (i = 0 ; i < NUM_RAMDISKS; i++) {
398
                struct block_device *bdev = rd_bdev[i];
399
                rd_bdev[i] = NULL;
400
                if (bdev)
401
                        blkdev_put(bdev, BDEV_FILE);
402
                destroy_buffers(MKDEV(MAJOR_NR, i));
403
        }
404
 
405
        devfs_unregister (devfs_handle);
406
        unregister_blkdev( MAJOR_NR, "ramdisk" );
407
        hardsect_size[MAJOR_NR] = NULL;
408
        blksize_size[MAJOR_NR] = NULL;
409
        blk_size[MAJOR_NR] = NULL;
410
}
411
 
412
/* This is the registration and initialization section of the RAM disk driver */
413
static int __init rd_init (void)
414
{
415
        int             i;
416
 
417
        if (rd_blocksize > PAGE_SIZE || rd_blocksize < 512 ||
418
            (rd_blocksize & (rd_blocksize-1)))
419
        {
420
                printk("RAMDISK: wrong blocksize %d, reverting to defaults\n",
421
                       rd_blocksize);
422
                rd_blocksize = BLOCK_SIZE;
423
        }
424
 
425
        if (register_blkdev(MAJOR_NR, "ramdisk", &rd_bd_op)) {
426
                printk("RAMDISK: Could not get major %d", MAJOR_NR);
427
                return -EIO;
428
        }
429
 
430
        blk_queue_make_request(BLK_DEFAULT_QUEUE(MAJOR_NR), &rd_make_request);
431
 
432
        for (i = 0; i < NUM_RAMDISKS; i++) {
433
                /* rd_size is given in kB */
434
                rd_length[i] = rd_size << 10;
435
                rd_hardsec[i] = rd_blocksize;
436
                rd_blocksizes[i] = rd_blocksize;
437
                rd_kbsize[i] = rd_size;
438
        }
439
        devfs_handle = devfs_mk_dir (NULL, "rd", NULL);
440
        devfs_register_series (devfs_handle, "%u", NUM_RAMDISKS,
441
                               DEVFS_FL_DEFAULT, MAJOR_NR, 0,
442
                               S_IFBLK | S_IRUSR | S_IWUSR,
443
                               &rd_bd_op, NULL);
444
 
445
        for (i = 0; i < NUM_RAMDISKS; i++)
446
                register_disk(NULL, MKDEV(MAJOR_NR,i), 1, &rd_bd_op, rd_size<<1);
447
 
448
#ifdef CONFIG_BLK_DEV_INITRD
449
        /* We ought to separate initrd operations here */
450
        register_disk(NULL, MKDEV(MAJOR_NR,INITRD_MINOR), 1, &rd_bd_op, rd_size<<1);
451
        devfs_register(devfs_handle, "initrd", DEVFS_FL_DEFAULT, MAJOR_NR,
452
                        INITRD_MINOR, S_IFBLK | S_IRUSR, &rd_bd_op, NULL);
453
#endif
454
 
455
        hardsect_size[MAJOR_NR] = rd_hardsec;           /* Size of the RAM disk blocks */
456
        blksize_size[MAJOR_NR] = rd_blocksizes;         /* Avoid set_blocksize() check */
457
        blk_size[MAJOR_NR] = rd_kbsize;                 /* Size of the RAM disk in kB  */
458
 
459
                /* rd_size is given in kB */
460
        printk("RAMDISK driver initialized: "
461
               "%d RAM disks of %dK size %d blocksize\n",
462
               NUM_RAMDISKS, rd_size, rd_blocksize);
463
 
464
        return 0;
465
}
466
 
467
module_init(rd_init);
468
module_exit(rd_cleanup);
469
 
470
/* options - nonmodular */
471
#ifndef MODULE
472
static int __init ramdisk_size(char *str)
473
{
474
        rd_size = simple_strtol(str,NULL,0);
475
        return 1;
476
}
477
static int __init ramdisk_size2(char *str)      /* kludge */
478
{
479
        return ramdisk_size(str);
480
}
481
static int __init ramdisk_blocksize(char *str)
482
{
483
        rd_blocksize = simple_strtol(str,NULL,0);
484
        return 1;
485
}
486
__setup("ramdisk=", ramdisk_size);
487
__setup("ramdisk_size=", ramdisk_size2);
488
__setup("ramdisk_blocksize=", ramdisk_blocksize);
489
#endif
490
 
491
/* options - modular */
492
MODULE_PARM     (rd_size, "1i");
493
MODULE_PARM_DESC(rd_size, "Size of each RAM disk in kbytes.");
494
MODULE_PARM     (rd_blocksize, "i");
495
MODULE_PARM_DESC(rd_blocksize, "Blocksize of each RAM disk in bytes.");
496
 
497
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.