无法通过 FUSE 文件系统"read"任何内容

Can not "read" anything through the FUSE file system

本文关键字:read 任何内 文件系统 FUSE      更新时间:2023-10-16

我在MIT 6.824实验室中使用fuse构建了自己的文件系统,read操作就是在这个函数中实现的。

void
fuseserver_read(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi)
{
std::string buf;
int r;
if ((r = yfs->read(ino, size, off, buf)) == yfs_client::OK) {
char* retbuf = (char *)malloc(buf.size());
memcpy(retbuf,buf.data(),buf.size());
//Print the information of the result.
printf("debug read in fuse: the content of %lu is %s, size %lun",ino,retbuf, buf.size());
fuse_reply_buf(req,retbuf,buf.size());    
} else {
fuse_reply_err(req, ENOENT);
}
//global definition
//struct fuse_lowlevel_ops fuseserver_oper;
//In main()
//    fuseserver_oper.read       = fuseserver_read;

我在buf返回之前打印它的信息。

当然,还实现了write操作。

然后我做了一个简单的测试来读出一些单词。

//test.c
int main(){
//./yfs1 is the mount point of my filesystem
int fd = open("./yfs1/test-file",O_RDWR | O_CREAT,0777);
char* buf = "123";
char* readout;
readout = (char *)malloc(3);
int writesize = write(fd,buf,3);
int readsize = read(fd,readout,3);
printf("%s,%dn",buf,writesize);
printf("%s,%dn",readout,readsize);
close(fd);
}

通过读取(fd,readout,3(,我什么也得不到,但fuseserver_read打印的信息显示,在fuse_reply_buf之前,缓冲区已成功读取

$ ./test
123,3
,0
debug read in fuse: the content of 2 is 123, size 3

那么,为什么test.c中的read()不能从我的文件系统中读取任何内容呢??

首先,我在编写测试文件时犯了一个错误。文件指针将指向"写入"后的文件末尾,当然以后什么也读不到。因此,只需重新打开文件就可以使测试正常进行。其次,在FUSE的read((操作之前,FUSE将首先获取attr((,并用文件的"size"属性截断read(((操作的结果。因此,操作文件的属性必须非常小心。

还需要通过发送一个空缓冲区(作为"EOF"(来通知您已完成读取。您可以使用reply_buf_limited来完成此操作。看看保险丝源树中的hello_ll示例:

static void tfs_read(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi) {
(void) fi;
assert(ino == FILE_INO);
reply_buf_limited(req, file_contents, file_size, off, size);
}
static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
off_t off, size_t maxsize)
{
if (off < bufsize)
return fuse_reply_buf(req, buf + off,
min(bufsize - off, maxsize));
else
return fuse_reply_buf(req, NULL, 0);
}