我正在编写一个基于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