FUSE文件系统中的df-statfs

时间:2017-05-06 09:51:28

标签: fuse

我正在为FUSE编写ramdisk代码。在我挂载我的ramdisk(它的大小我指定为参数)后,我希望使用df -h来查看我的ramdisk占用的空间是否与我作为参数提到的大小相同。

为此,我使用了statfs()函数,例如"示例"中的示例fusexmp.c中给出的函数。 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;
}

然而,我在" 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"查看免费RAM,我得到:

              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"显示正确的内存值?

1 个答案:

答案 0 :(得分: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_frsize、{{1} }、f_blocksf_bfreef_bavailf_filesf_ffreef_favailf_fsidf_flag

有关更多信息,请访问谷歌“struct statvfs”。

相关问题