> 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_lock.9.md).

# VOP\_LOCK.9

`VOP_LOCK` — 序列化对 vnode 的访问

## 名称

`VOP_LOCK`, `VOP_UNLOCK`, `VOP_ISLOCKED`, `vn_lock`

## 概要

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

```c
int
VOP_LOCK(struct vnode *vp, int flags)

int
VOP_UNLOCK(struct vnode *vp)

int
VOP_ISLOCKED(struct vnode *vp)

int
vn_lock(struct vnode *vp, int flags)
```

## 描述

这些调用用于序列化对文件系统的访问，例如防止两个写操作同时写入同一个文件。

参数如下：

**`LK_SHARED`** 共享锁。 **`LK_EXCLUSIVE`** 独占锁。 **`LK_UPGRADE`** 共享锁升级为独占锁。 **`LK_DOWNGRADE`** 独占锁降级为共享锁。 **`LK_RELEASE`** 释放任何类型的锁。 **`LK_DRAIN`** 等待所有锁活动结束。

**`LK_NOWAIT`** 不休眠等待锁。 **`LK_SLEEPFAIL`** 休眠后返回失败。 **`LK_CANRECURSE`** 允许递归独占锁。 **`LK_NOWITNESS`** 指示 [witness(4)](/man/man4/witness.4.md) 忽略此实例。

**`LK_INTERLOCK`** 当调用者已持有简单锁时指定（`VOP_LOCK` 在获取锁后会释放该简单锁）。 **`LK_RETRY`** 重试直到锁定。

**`vp`** 要锁定或解锁的 vnode。

**`flags`** 锁请求类型之一：锁类型可以与这些锁标志进行*或*运算：锁类型可以与这些控制标志进行*或*运算：内核代码应使用 `vn_lock` 来锁定 vnode，而不是直接调用 `VOP_LOCK`。`vn_lock` 也不接受线程作为参数，而是假定使用 curthread。

## 返回值

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

## 参见

[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_lock.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.
