implementing filesystem

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
redDot
Member
Member
Posts: 29
Joined: Sat Jan 26, 2008 2:48 am

implementing filesystem

Post by redDot »

hello friends,
i am following james tutorial, to implement the filesystems as specified in it.(http://www.jamesmolloy.co.uk/tutorial_h ... nitrd.html)
The tutorial, and the source code have only implemented the initrd_read() function, which is working fine, i am trying to implement the initrd_write() on my own.
The makeInitrd.c is the code to make the initial ramdisk, i have made a few changes as compared to the tutorials, i have added some buffer spaces(empty spaces) in the header's section and the data section, which i wanted to use later during initrd_write(), to write new headers and data into this filesystem.
the code is as follows,

Code: Select all

struct initrd_header
{
    unsigned char magic;
    char name[64];
    unsigned int offset;
    unsigned int length;
};

int main(int argc, char **argv)
{
    int nheaders = (argc-1)/2;
    struct initrd_header headers[64];   //max of 64 files
    printf("Size of the header : %d \n", sizeof(struct  initrd_header));
    memcpy(headers,0,sizeof(struct initrd_header) * 64);
    
    unsigned int off = sizeof(struct initrd_header) * 64 + sizeof(int);
    int i;
    for(i=0; i<nheaders; i++)
    {
        printf("writing files %s->%s at 0x%x\n", argv[i*2 + 1],argv[i*2 + 2], off);
        strcpy(headers[i].name, argv[i*2+2]);
        headers[i].offset = off;
        FILE *stream = fopen(argv[i*2+1],"r");
        if(stream == 0)
        {
                    printf("Error: file not found: %s\n", argv[i*2+1]);
         return 1;
       }
       fseek(stream, 0, SEEK_END);
       headers[i].length = ftell(stream);
       off += headers[i].length;
       fclose(stream);
       headers[i].magic = 0xBF;
   }
   
   
   //put the other headers as null
   for(;i<64;i++)
   {
       strcpy(headers[i].name,"");
       headers[i].offset=0;
       headers[i].length=0;
       headers[i].magic=0;
   }
   
   
   FILE *wstream = fopen("initrd.img", "w");
   unsigned char *data = (unsigned char *)malloc(off);
   
   //this may be wrong
   //fwrite(&nheaders, sizeof(int), 1, wstream);
   //fwrite(headers, sizeof(struct initrd_header), 64, wstream);
   
   //My correction... i believe this should be used 
   fwrite(&nheaders,  1, sizeof(int),wstream);
   fwrite(headers, 1, sizeof(struct initrd_header)*64, wstream);
    for(i = 0; i < nheaders; i++)
   {
     FILE *stream = fopen(argv[i*2+1], "r");
     unsigned char *buf = (unsigned char *)malloc(headers[i].length);
     fread(buf, 1, headers[i].length, stream);
     fwrite(buf, 1, headers[i].length, wstream);
     fclose(stream);
     free(buf);
   }
   
   
   //allot a chunk of free space 
   unsigned char *buf = (unsigned char *)malloc(100);
   buf[0]=' ';
   buf[1]='\0';
   fwrite(buf, 500, 100, wstream);
   
   fclose(wstream);
   free(data);
   
   return 0;
}
in the initrd_write() i am locating the last header which was filled, and then accordingly updating the various sections as required. I am getting the page fault when i am trying to initialize the header.offset value.

Code: Select all

u32int initrd_write(fs_node_t *node, u32int offset, u32int size, u8int *buffer)
{
    initrd_file_header_t header ;
    u32int off = (file_headers[(initrd_header->nfiles)-1].offset)+(file_headers[(initrd_header->nfiles)-1].length);
    
    //make the new header 
    header.magic= 0xBF;
    strcpy(header.name, "newFile.txt");
    
    puts("\nworking fine till here...");
    for(;;);
    header.offset = off;        //this is causing page fault
        
    header.length = size;
    
    
    //add the header entry to the file system
    file_headers[initrd_header->nfiles] = header;
    
    memcpy(off, buffer, size);

    
    //make a new node
        node->mask = node->uid = node->gid = 0;
        node->length = size;
        node->inode = initrd_header->nfiles;
        node->flags = FS_FILE;
        node->read = &initrd_read;
        node->write = &initrd_write;
        node->readdir = 0;
        node->finddir = 0;
        node->open = 0;
        node->close = 0;
        node->impl = 0;

    //update the root directory
        root_nodes[initrd_header->nfiles] =*node;

    puts("\nwrite is called...");
    puts("file name :");
    puts(root_nodes[initrd_header->nfiles].name);
    puts("\tcontent:");
    puts(buffer);
    
    
    //increment the total no of files
    (initrd_header->nfiles)++;
     nroot_nodes = initrd_header->nfiles;
    
    
    return size;
}
I am also confused as to what happens when GRUB loads the module? is it that the entire initial ramdisk is loaded into the memory during this ?

Code: Select all

u32int initrd_location = *((u32int*)mboot_ptr->mods_addr);
    u32int initrd_end = *(u32int*)(mboot_ptr->mods_addr+4);
    placement_address = initrd_end;
        
    initialise_paging();
    
???
Post Reply