File system image mkfs.c XV6

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
Crixus
Posts: 4
Joined: Sat May 06, 2023 5:40 pm

File system image mkfs.c XV6

Post by Crixus »

Hello,
There is something that i cant understand.

Why all numerical data in mkfs.c XV6 file system image creator, are converted from big<-->little endian and vice versa.

example :
in the superblock :

Code: Select all

    sb.size = xint(FSSIZE);
    sb.nblocks = xint(nblocks);
    sb.ninodes = xint(NINODES);
    sb.nlog = xint(nlog);
    sb.logstart = xint(2);
    sb.inodestart = xint(2 + nlog);
    sb.bmapstart = xint(2 + nlog + ninodeblocks);
then :

Code: Select all

uint xint(uint x) {
    uint y;
    uchar* a = (uchar*)&y;
    a[0] = x;
    a[1] = x >> 8;
    a[2] = x >> 16;
    a[3] = x >> 24;
    return y;
}
I have try to figure out why but it is pretty closed subject.

I hope this question relevant.
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: File system image mkfs.c XV6

Post by Octocontrabass »

Crixus wrote:Why all numerical data in mkfs.c XV6 file system image creator, are converted from big<-->little endian and vice versa.
It converts between host-endian and little-endian. On a big-endian host, it converts between big-endian and little-endian. On a little-endian host, the conversion doesn't do anything.

It does that because mkfs needs to be able to run on both big-endian and little-endian CPUs, but the filesystem is always little-endian.
Crixus
Posts: 4
Joined: Sat May 06, 2023 5:40 pm

Re: File system image mkfs.c XV6

Post by Crixus »

like that ?

Assuming 0x12345678:

On a Big Endian system:
Original x in memory: [12][34][56][78]
After xint(), y in memory: [78][56][34][12] (bytes are reversed)
This will appear as a change when viewing memory.

On a Little Endian system:
Original x in memory: [78][56][34][12]
After xint(), y in memory: [78][56][34][12] (bytes stay in same order)
This will appear as no change when viewing memory.


And so the disk image is in little endian... when a big endian proc have to use it, it should reverse all numerical data every times ?
Octocontrabass
Member
Member
Posts: 5560
Joined: Mon Mar 25, 2013 7:01 pm

Re: File system image mkfs.c XV6

Post by Octocontrabass »

Crixus wrote:like that ?
Yep, you've got it.
Crixus wrote:And so the disk image is in little endian... when a big endian proc have to use it, it should reverse all numerical data every times ?
Yes, but xv6 only runs on little-endian CPUs.
Post Reply