【fuse】struct fuse_lowlevel_ops解析-①
struct fuse_lowlevel_ops解析-①
void (*setattr)(
fuse_req_t req,
fuse_ino_t ino,
struct stat *attr,
int to_set,
struct fuse_file_info *fi
);
setattr 的 to_set 标志位说明:
| 宏定义名称 | 含义 |
| ------------------------- | -------------------- |
| `FUSE_SET_ATTR_MODE` | 修改权限(`st_mode`) |
| `FUSE_SET_ATTR_UID` | 修改所有者 UID(`st_uid`) |
| `FUSE_SET_ATTR_GID` | 修改所有者 GID(`st_gid`) |
| `FUSE_SET_ATTR_SIZE` | 截断/扩展文件大小(`st_size`) |
| `FUSE_SET_ATTR_ATIME` | 修改访问时间(`st_atime`) |
| `FUSE_SET_ATTR_MTIME` | 修改修改时间(`st_mtime`) |
| `FUSE_SET_ATTR_ATIME_NOW` | 访问时间设为当前时间 |
| `FUSE_SET_ATTR_MTIME_NOW` | 修改时间设为当前时间 |
fuse_add_direntry_plus() 是 FUSE 提供的一个函数,用于在 readdirplus 回调中构造目录项并附带文件属性(stat + entry),把它们写入 FUSE 要求的 buffer 中。
void (*readdirplus)(fuse_req_t req,
fuse_ino_t ino,
size_t size,
off_t off,
struct fuse_file_info *fi);
参数名 | 类型 | 说明 |
---|---|---|
req |
fuse_req_t |
FUSE 请求句柄,用于回复这个请求(调用 fuse_reply_buf() 或 fuse_reply_err() ) |
ino |
fuse_ino_t |
要读取的目录的 inode 编号(是哪个目录) |
size |
size_t |
用户缓冲区大小限制,你返回的目录项总大小不得超过此值 |
off |
off_t |
当前读取的偏移,用于分页读取目录(第一次为 0,后续为你返回的 next offset) |
fi |
struct fuse_file_info * |
来自 open 的 fh (文件句柄),你之前在 opendir 或 create 中设置的 fi->fh 会传回来 |
fuse_add_direntry_plus() 是 FUSE 提供的一个函数,用于在 readdirplus 回调中构造目录项并附带文件属性(stat + entry),把它们写入 FUSE 要求的 buffer 中。
size_t fuse_add_direntry_plus(fuse_req_t req,
char *buf, size_t bufsize,
const char *name,
const struct stat *st,
const struct fuse_entry_param *e,
off_t off);
buf start
┌──────────────────────────────────────────────────────────────┐
│ Entry 1: fuse_direntplus │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ fuse_dirent (inode, type, name length, entry size, ...) │ │
│ ├────────────────────────────────────────────────────────┤ │
│ │ name bytes ("file1.txt") + '\0' + padding (对齐) │ │
│ ├────────────────────────────────────────────────────────┤ │
│ │ fuse_entry_param (ino, attr/stat, timeouts, ...) │ │
│ └────────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────┘
┌──────────────────────────────────────────────────────────────┐
│ Entry 2: fuse_direntplus │
│ ┌────────────────────────────────────────────────────────┐ │
│ │ fuse_dirent (inode, type, name length, entry size, ...) │ │
│ ├────────────────────────────────────────────────────────┤ │
│ │ name bytes ("another") + '\0' + padding (对齐) │ │
│ ├────────────────────────────────────────────────────────┤ │
│ │ fuse_entry_param (ino, attr/stat, timeouts, ...) │ │
│ └────────────────────────────────────────────────────────┘ │
└──────────────────────────────────────────────────────────────┘
... (后续条目继续追加)
细节说明
- 每个 entry 由 fuse_add_direntry_plus() 追加,包含三部分:
- fuse_dirent(基本目录项:inode、类型、名字长度、offset 等)
- 名字字符串(例如 "file1.txt",以 \0 结尾),后面可能有填充以满足对齐要求
- fuse_entry_param(即 “plus” 部分:包含 inode 再次、属性 stat、attr_timeout、entry_timeout 等)
- off 值:每个 entry 传入的 off 是你定义的“下一次从哪里继续”的标记,一般递增(如使用序号或内部偏移)。内核下次请求时会带回来,告诉你从哪个 entry 继续。
- 紧密排列:每个 entry 紧接前一个写入,buf_used 累加它们的尺寸,最终总和不能超过 readdirplus 传进来的 size。
- 对齐:底层可能需要对齐(取决于系统和实现),名字后通常填充以确保后续的 fuse_entry_param 是对齐的。