RAMDISK

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
GLneo
Member
Member
Posts: 237
Joined: Wed Dec 20, 2006 7:56 pm

RAMDISK

Post by GLneo »

I am currently trying to port new lib and i found most of the system calls are file IO, my current floppy / FAT driver is to unstable, so i've decided to make a system for making and read/writing ram disks, i would like file hierarchy to be similar to most UNIX systems ( /usr/src, etc.. ), i have written this:

Code: Select all

/*
 *  los/drivers/ramdisk/ramdisk.c
 *
 *  Copyright (C) 2007  Andrew Davis
 */

#include <los/ramdisk.h>

struct ram_disk * RAMDISK_HEAD = NULL;

int ramdisk_create( int size, char disk_name )
{
    struct ram_disk *new_disk;
    new_disk = (struct ram_disk *)malloc(sizeof(struct ram_disk));
    if(new_disk == NULL)
        return NO_MEM_FOR_DISK;
    new_disk->mem_location = malloc( size );
    strcpy( new_disk->disk_name, disk_name );
    new_disk->prev = RAMDISK_HEAD;
    RAMDISK_HEAD = new_disk;
    return 0;
}
with header:

Code: Select all

#ifndef __MAIN_H
#define __MAIN_H

#define NO_MEM_FOR_DISK -2;

struct ram_disk
{
    unsigned int mem_location;
    char disk_name[32];
    struct ram_disk *prev;
};    

#endif
Now I am confused about what do do next( read(), write() )? Does anyone have ideas about how to write these for a ramdisk?

thx!
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Post by JamesM »

It depends what format you want to use. I have one ramdisk, the initrd, which loads as a GRUB module. It is in a propriatery (sp) format that is just basically a load of headers then data. Headers give offset of data start and length etc. Subdirectories are not supported.

I then have read/write defined like:

Code: Select all

int read(int off, int l, char *buf)
{
  Header *curHeader = <get header for cur file>
  <do error checking on offset-length>
  memcpy(buf, header->data_start+off, l);
  return l;
}
int write(int off, int l, char *buf)
{
  return EROFS;
}
So, you can't write to it :P This takes a lot of the complexity out of the ramdisk driver, and considering I only need it so I can load my other drivers, I never need to write anyway!

JamesM
GLneo
Member
Member
Posts: 237
Joined: Wed Dec 20, 2006 7:56 pm

Post by GLneo »

well, I'm going to most likely to mount my ramdisk as root ( / ), so there will be a lot of reading and writing, i was kinda thinking it could somehow be done as a linked-list tree:

Code: Select all


        /: [prev=none; next=none; sub=*usr]


        /usr/: [prev=none; next=*lib; sub=*src]


        /lib/: [prev=*usr; next=*etc; sub=*X11]


this is probably really stupid :roll:

i do like linked-list's :P
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

Or you could just make the interface to your ram disk the same as your floppy/hard drive and use a common file system such as FAT or SFS.
User avatar
Brynet-Inc
Member
Member
Posts: 2426
Joined: Tue Oct 17, 2006 9:29 pm
Libera.chat IRC: brynet
Location: Canada
Contact:

Post by Brynet-Inc »

Indeed, A "unified" way of treating storage devices should be implemented.. :)

http://en.wikipedia.org/wiki/Virtual_file_system
Image
Twitter: @canadianbryan. Award by smcerm, I stole it. Original was larger.
GLneo
Member
Member
Posts: 237
Joined: Wed Dec 20, 2006 7:56 pm

Post by GLneo »

how to access the FS helps, but i still don't have down how things will be organized on the disk...
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

GLneo wrote:how to access the FS helps, but i still don't have down how things will be organized on the disk...
I would organize it the same way it is on a floppy disk. It would start with a boot sector and then be followed up by more sectors, it wouldn't be forced to be 1.44mb though. Or you could think of it as a partition.
User avatar
Dex
Member
Member
Posts: 1444
Joined: Fri Jan 27, 2006 12:00 am
Contact:

Post by Dex »

See here: http://www.osdev.org/phpBB2/viewtopic.p ... 662#104662
for a simple ramdisk example.
GLneo
Member
Member
Posts: 237
Joined: Wed Dec 20, 2006 7:56 pm

Post by GLneo »

Hmm... it seems you think I'm making a virtual floppy disk, why would i need a boot sector on a ram disk?

I've been doing some thinking, I might save memory space by not allocating the whole disk, just malloc for new files:

ramdisk_make( int size ), would just set the max size of the disk and as more disk space is needed memory is added via a linked-list style thing.

also why would i want to use FAT, that seems meant for real disk's, a memory disk should be easier to access( just store file location's in variables, ( not in a table )), this also would prevent fragmentation that occurs with physical disks.

what do you think?
frank
Member
Member
Posts: 729
Joined: Sat Dec 30, 2006 2:31 pm
Location: East Coast, USA

Post by frank »

GLneo wrote:I've been doing some thinking, I might save memory space by not allocating the whole disk, just malloc for new files:

ramdisk_make( int size ), would just set the max size of the disk and as more disk space is needed memory is added via a linked-list style thing.

also why would i want to use FAT, that seems meant for real disk's, a memory disk should be easier to access( just store file location's in variables, ( not in a table )), this also would prevent fragmentation that occurs with physical disks.

what do you think?
That would work just fine.

In my kernel all disk drives are derived from CDrive so that all I have to do is pass a pointer to a CDrive to my file system driver and it can call read though that pointer. Each individual driver is responsible for detecting whether it can use this drive(actually I pass a pointer to a CPartition class) or not. If I were to implement a ram disk I would just derive a CRamdisk class and then pass that to the file system driver. That way you can use any supported file system on the ram disk and there is no special casing in order to support it.
Post Reply