> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man9/vop_readdir.9.md).

# VOP\_READDIR.9

`VOP_READDIR` — 读取目录内容

## 名称

`VOP_READDIR`

## 概要

```c
#include <sys/param.h>
#include <sys/dirent.h>
#include <sys/vnode.h>
```

```c
int
VOP_READDIR(struct vnode *vp, struct uio *uio, struct ucred *cred,
    int *eofflag, int *ncookies, uint64_t **cookies)
```

## 描述

读取目录条目。

**`vp`** 目录的 vnode。

**`uio`** 读取目录内容的位置。

**`cred`** 调用者的凭证。

**`eofflag`** 返回文件末尾状态（如果不需要则为 `NULL`）。

**`ncookies`** 为 NFS 生成的目录 cookie 数量（如果不需要则为 `NULL`）。

**`cookies`** 为 NFS 生成的目录查找 cookie（如果不需要则为 `NULL`）。

目录内容被读取到 `struct dirent` 结构中。如果磁盘上的数据结构与此不同，则应进行转换。

## 锁定

目录应在进入时已锁定，并在退出时仍保持锁定。

## 返回值

成功时返回零，否则返回错误代码。

如果从 NFS 服务器调用，将提供额外参数 `eofflag`、`ncookies` 和 `cookies`。如果在读取时到达目录末尾，应将 `*eofflag` 的值设置为 TRUE。目录查找 cookie 返回给 NFS 客户端，可稍后用于从目录中间重新开始读取目录。每个目录条目应返回一个 cookie。cookie 的值应为相应目录条目磁盘版本在目录中开始的偏移量。cookie 的内存应使用以下方式分配：

```c
	...;
	*ncookies = number of entries read;
	*cookies = malloc(*ncookies * sizeof(**cookies), M_TEMP, M_WAITOK);
```

## 错误

\[`EINVAL`] 试图从目录中的非法偏移位置读取。

\[`EIO`] 读取目录时发生读取错误。

\[`EINTEGRITY`] 读取目录时检测到损坏的数据。

## 参见

[vnode(9)](/man/man9/vnode.9.md)

## 作者

本手册页面由 Doug Rabson 编写。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man9/vop_readdir.9.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
