FUSE文件系统中的df-statfs

3

我正在编写一个基于FUSE的ramdisk代码。在我挂载我的ramdisk之后(我将其大小指定为参数),我希望使用df -h命令查看占用我的ramdisk空间是否与我所提到的参数相同。

为此,我使用了statfs()函数,就像示例fusexmp.c中给出的那样,在fuse-2.9.7的“examples”文件夹中:

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

return 0;
}

然而,通过“df -h”命令获得的大小并不是我挂载ramdisk时指定的大小。

例如,假设我以以下方式挂载ramdisk:

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

在此之后,我使用“df -h”命令并得到以下结果:
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.)

接下来我使用"free -h"命令查看可用的内存,结果如下:

              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

如果我在文件系统代码中不使用statfs()函数并使用"df -h /mnt/ram",那么会得到以下结果:
Filesystem      Size  Used Avail Use% Mounted on

ramdisk          0     0     0     0   /mnt/ram

我想问一下上面给出的statfs()实现是否有误?应该如何编写代码才能使"df -h"显示正确的内存值?


statvfs 对于 /mnt/ram 将会给出该文件夹所在文件系统的统计信息(可能是 /,但也可以是 /mnt)。 - Antoni
1个回答

0

int statfs(const char*, struct statvfs*) 中,你可以设置将在 df 显示的值。例如:

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
}

df 的输出结果:

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

然而,这个例子并不完整(你可以看到Avail仍然是零),你可以设置的所有字段包括:
f_bsizef_frsizef_blocksf_bfreef_bavailf_filesf_ffreef_favailf_fsidf_flagf_namemax

更多信息请搜索“struct statvfs”。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接