Link to home
Start Free TrialLog in
Avatar of achille67
achille67

asked on

files && file systems

Hi Guys,
I am totally new in programming with C i have few more questions:
1. )How can i check total file length is small enough to be
           represented in MAX_BLOCKS_PER_FILE (known)?

 2.) How can i check there is a free inode

 3.) How can i check there is room in the directory

 4.) How can i update my structure( bitmap, directory, inode, datablocks, superblock) after theses  operations  
Note: I am operating between linux file system and my own file syst. composed of:
SuperBlock
Bit Map
Root Directory
Inodes
DATA Blocks
Thanks
Avatar of stefan73
stefan73
Flag of Germany image

1) With ext2fs, this is probably more than HDs than you can mount in your PC :-)
   AFAIK, max file size is 32bit for block x 1K block size = 2^42 = 4TB. 3rd level indirect block should also be sufficient for this.

2.) with "df" command.

3.) There is. As long as you have space on HD and inodes. Directories can grow VERY big.

4) What do you want to do with that?

Avatar of achille67
achille67

ASKER

Thanks Stefan73, but i don't think i make myself clear enough:
The idea is if creat my file own file system in the directory "userfs" using for example the command: ./userfs -reformat 800000 file.system
and that file system has the following structure:
SuperBlock
Bit Map
Root Directory
Inodes
DATA Blocks
Thanks,
1. )How can i check total file length is small enough to be
           represented in MAX_BLOCKS_PER_FILE (known)?

 2.) How can i check there is a free inode

 3.) How can i check there is room in the directory

 4.) How can i update my structure( bitmap, directory, inode, datablocks, superblock) after theses  operations  


hey Stefan
this is very broad and there is no specific answers to my questions. I really appreciate it if you porvide me with some pseudocode.
Avatar of sunnycoder
1. )How can i check total file length is small enough to be
           represented in MAX_BLOCKS_PER_FILE (known)?

In linux filesystem or your own ? If in linux, you can use fstat() or stat() to get the filesize and you can carry out the comparison ... If in your iwn, then you need to maintain the filesize information explicitly or you can calculate it whenever required ... for a file of length n blocks, size on disk will be n * block size and file size will be n-1 * blocksize + bytes occupied in the last block

 2.) How can i check there is a free inode
You have to maintain a bitmap or some sort of data-struture to keep track of free and used inodes

 3.) How can i check there is room in the directory
You mean in a filesystem? Unless you have quotas, you can put as much in a directory as the disk space would permit you to !! Room in a filesystem will depend on how many free blocks you have and you have to keep track of blocks .... free space = number of free blocks * block size

 4.) How can i update my structure( bitmap, directory, inode, datablocks, superblock) after theses  operations  
let us see that data structures you are using

I would suggest that you take off sometime for reading some simple filesystems such as those used in nachos or minix
thanks sunnycoder,
as you recommended i did some reading again, the concept is a little tricky to capture(i am new in this)

1.) thanks for question 1.)
2.)i am using a simple array. could you please give me some pseudocode?
3.) You mean in a filesystem? yes i mean my own file system i allocate by running the command: ./userfs -reformat disk_size file_name
where userfs is the directory in which i am working. Angain some pseudocode will help better?
4.)Let us see that data structures you are using: after executiong
./userfs -reformat disk_size file_name my filesystem looks like this:

SuperBlock
Bit Map
Root Directory
Inodes
DATA Blocks

The detail of the homewok i am solving can be found on page:
http://www.clarkson.edu/~jnm/os/homework/userfs/fs.htm
>2.)i am using a simple array. could you please give me some pseudocode?
I will prefer not to give pseudocode... logic development and research are integral parts of assignments of this level ... You are using array of what? ints/chars or an array of chars as a bitmap ?

>3.) You mean in a filesystem? yes i mean my own file system i allocate by running the
>command: ./userfs -reformat disk_size file_name
>where userfs is the directory in which i am working. Again some pseudocode will help
>better?
This information again will be held in your data structures ... It is requirements like these which define the data structures you should be using ... If you are aware that you need to calculate remaining disk space often, you should either devise data structures which always hold this information up-to-date (deletion, creation, append etc. will modify this) or devise such data structures which will allow you to calculate the remaining space in a very short period of time

>4.)Let us see that data structures you are using: after executiong
>./userfs -reformat disk_size file_name my filesystem looks like this:
Data structures would be the structs and unions which you would have written to hold the information that must reside in each of these compenents ... e.g. where do you hold the information that makes a superblock
It is that that information that you should be working on first ...

You need to start *working* towrads the solution and I can help you refine it
Hi sunnycoder,
thanks for your help.
>2.You are using array of what? ints/chars or an array of chars as a bitmap ?
in the u_import i am using an array BIT bit_map[BIT_MAP_SIZE] where
BIT is defined as #define BIT  unsigned -------> so an array of assigned bit.


---- Given the overall problem(http://www.clarkson.edu/~jnm/os/homework/userfs/fs.htm) and the implementation of u_import that i have so far(see below) do u think i am on right path?
int u_import(char* linux_file, char* u_file)
{


        int free_space;
        int num_block;
        struct stat st;
        int file_size=0;
        int remainder=0;
        int i;
        int j;

        free_space = u_quota();

        handle = open(linux_file,O_RDONLY);
        if ( -1 == handle ) {
                printf("error, reading file %s\n",linux_file);
                return 0;
        }

        fstat(handle,&st);
        file_size=(int)st.st_size;
        num_block=(int)file_size/BLOCK_SIZE_BYTES;
        if((num_block*BLOCK_SIZE_BYTES)!=file_size)
          {
            num_block++;
            remainder=file_size-(num_block*BLOCK_SIZE_BYTES);
          }
 /* check there is enough free space */

        /* check file name is short enough */


        /* check total file length is small enough to be
           represented in MAX_BLOCKS_PER_FILE */
        assert(sizeof(BIT)* BIT_MAP_SIZE <= BLOCK_SIZE_BYTES);
        /* check there is a free inode */

        /* check there is room in the directory */
        assert(sizeof(dir_struct) <= BLOCK_SIZE_BYTES);

        /* then update the structures: what all needs to be updates?
           bitmap, directory, inode, datablocks, superblock(?) */
        assert(sizeof(superblock) <= BLOCK_SIZE_BYTES);

        /* what order will you update them in? how will you detect
           a partial operation if it crashes part way through? */


        for(i=0;i<num_block;i++)
          {
            lseek(handle,BLOCK_SIZE_BYTES*i,SEEK_SET);
             read(handle,&buffer,BLOCK_SIZE_BYTES);
    //      crash_write(virtual_disk, bit_map, sizeof(BIT)*BIT_MAP_SIZE );

             // bit_map[i] array and finds the next available free bitmap
             //position lseek to that place with
                     for (j=0; j< BIT_MAP_SIZE; j++){
             assert( free_space >= 1);
               allocate_block(i);

                 lseek(virtual_disk,BLOCK_SIZE_BYTES,SEEK_SET);

                     }


               crash_write(virtual_disk, &buffer, BLOCK_SIZE_BYTES);

             //MARK bit_map that which you wrote in as not free
             //ADD that block to the inode.block[i]
          }

        if(remainder > 0)
          //      all writes same excpet BLOCK_SIZE_BYTES is now remainder
                       crash_write(virtual_disk, &buffer, remainder);
          //write file structure
          //write directory structure


        close(handle);
        close(virtual_disk);
retrun 1;
}
>in the u_import i am using an array BIT bit_map[BIT_MAP_SIZE] where
>BIT is defined as #define BIT  unsigned -------> so an array of assigned bit

there has to be little more to it ... unsigned int or unsigned char ? most likely it will be unsigned char .. check again :o)

you are on the right track but there is a lot more to be done ... some observations

>        /* check total file length is small enough to be
>           represented in MAX_BLOCKS_PER_FILE */
>        assert(sizeof(BIT)* BIT_MAP_SIZE <= BLOCK_SIZE_BYTES);
I am not too sure about this check .... sizeof bit * bit_map_size will be the total space occupied by your bitmap ... checking it to be less that or equal to block size translates to checking if the bitmap will fit on a single block !!! It has no relationship with file size !!! .... the result will be the same for 0 byte file and for 10Gb file

then again
>      /* check there is room in the directory */
>        assert(sizeof(dir_struct) <= BLOCK_SIZE_BYTES);
Here you do not check how many entries you have left ... all you see is if sizeof directory struct is less than a block ... this will be true/false irrespective of the space left in the directory !!!

Take a pen and paper and design your assignment before you start coding it ... Designing will take sometime but it will save you a lot of coding time and efforts and you will end up with a better quality filesystem
>there has to be little more to it ... unsigned int or unsigned char ? most likely >it will be
This assign. is a piece of code that i am building on. all was given to me is:
#define BIT  unsigned
and used as:
BIT bit_map[BIT_MAP_SIZE]
also i know this is array is find next available free space and then used lseek( )
to seek to that spot as: lseek(virtual_disk,BLOCK_SIZE_BYTES,SEEK_SET);
To do this search of free space in bit_map[] i was doing the following:
 for (j=0; j< BIT_MAP_SIZE; j++){
             assert( free_space >= 1);
               allocate_block(i);
                 lseek(virtual_disk,BLOCK_SIZE_BYTES,SEEK_SET);
                     }
but i wasn't sure i am doing the right thing.


>Take a pen and paper and design your assignment before you start coding >it ...
 You are right on this, but and i have done so. I think i am still not fully picturing what my file system looks like after i runn the command:
./userfs -reformat disk_size file_name
For instance, after all my reading the following concepts are not clear in my mind.
#define SUPERBLOCK_BLOCK 0
#define BIT_MAP_BLOCK 1
#define DIRECTORY_BLOCK 2
#define INODE_BLOCK 3

 I wish you can provide me with a picture with all this means and what exactly i need to do to implement u_import function.

Thanks sunnycoder.


ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i emailed u at <email-id edited by sunnycoder, Page Editor>, did u get it?

achille67
Hi sunnycoder,
I wannted to know if weather or not someone can finally porvide
me with a correction of this homework that was already due.
http://www.clarkson.edu/~jnm/os/homework/userfs/fs.htm
 i have worked on so hard but still not get it working properly.
i would could then use that solution as a learning model, i will
be willing to pay for the time the expert will spend on it. please
contact me at <email id edited by sunnycoder, Page Editor>

achille67
>Please do not post mail-ids in question threads. EE is searched by spammers for mail->ids and you may end up getting tons of spam. I shall be replying to your mail shortly.

thanks for imforming me this. I am no longer using the email address i used to open this account with EE. my new one is <komla30 at hotmail dot com>
you may update this now.

achille67
achille67,

I have already replied to your mail ... Seems like either you missed it or your mail filter has filtered it to bulk or trash

sunnycoder