Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

df-statfs in FUSE filesystem

I am writing a code for ramdisk in FUSE. After I mount my ramdisk (its size I specify as an argument), I wish to use df -h to see if the space occupied by my ramdisk is the same as the size that I had mentioned as an argument.

For this, I used statfs() function like the one given in example fusexmp.c in the "examples" folder inside fuse-2.9.7 :

static int ramdisk_statfs(const char *path, struct statvfs *stbuf)
{
 int res;
 res = statvfs(path, stbuf);
 if (res == -1)
 return -errno;

return 0;
}

However, the size I get in the output of "df -h" command is not the size that I mention while mounting my ramdisk.

For example, suppose I mount my ramdisk with :

./ramdisk /mnt/ram 2
(where 2 is the size I specify for my ramdisk.)

After this, I use "df -h" and I get :

Filesystem      Size    Used Avail Use% Mounted on

ramdisk          46G  5.8G   37G  14% /mnt/ram 
(but this is not the size specified by me above for my ramdisk.)

After this I use "free -h" to view free RAM, and I get :

              total       used       free     shared    buffers     cached
Mem:          1.7G       806M       954M       5.5M        85M       296M
-/+ buffers/cache:       424M       1.3G
Swap:         3.9G         0B       3.9G

If I dont use the statfs() function in my filesystem code, and I use "df -h /mnt/ram" , I get :

Filesystem      Size  Used Avail Use% Mounted on

ramdisk          0     0     0     0   /mnt/ram

I wish to ask if the implementation of statfs() given above is wrong? How should the code be written so that "df -h" shows the correct memory values?

like image 624
Harshada Kelkar Avatar asked Dec 06 '25 09:12

Harshada Kelkar


1 Answers

Inside int statfs(const char*, struct statvfs*) you can set the values which will be shown in df. Example:

static int ramdisk_statfs(const char *path, struct statvfs *stbuf) {
    stbuf->f_bsize  = 4096; // block size
    stbuf->f_frsize = 4096; // fragment size
    stbuf->f_blocks = 1024; // blocks

    return 0; // assume no errors occurred, just return 0
}

Output of df:

Filesystem      Size  Used Avail Use% Mounted on
test            4.0M  4.0M     0 100% /home/antoni/c/fuse/mnt

However, this example is not complete (you see Avail is still zero), all fields you can set are:
f_bsize, f_frsize, f_blocks, f_bfree, f_bavail, f_files, f_ffree, f_favail, f_fsid, f_flag and f_namemax.

For more info google 'struct statvfs'.

like image 154
Antoni Avatar answered Dec 08 '25 18:12

Antoni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!