【问题标题】:df-statfs in FUSE filesystemFUSE 文件系统中的 df-statfs
【发布时间】:2017-10-04 18:23:30
【问题描述】:

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

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

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

【问题讨论】:

  • statvfs for /mnt/ram 将提供该文件夹所在的 fs 的统计信息(可能是 /,但也可以是 /mnt

标签: c filesystems fuse


【解决方案1】:

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'。

【讨论】:

    猜你喜欢
    • 2014-04-22
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 2011-12-11
    • 1970-01-01
    • 2012-10-16
    • 2017-09-19
    • 2013-08-09
    相关资源
    最近更新 更多