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

# g\_attach.9

`g_attach` — 将 GEOM 消费者附加到/从提供者分离

## 名称

`g_attach`, `g_detach`

## 概要

```c
#include <geom/geom.h>

int
g_attach(struct g_consumer *cp, struct g_provider *pp)

void
g_detach(struct g_consumer *cp)
```

## 描述

`g_attach` 函数将给定消费者 `cp` 附加到给定提供者 `pp`，从而在消费者和提供者之间建立通信通道，允许更改访问计数并执行 I/O 操作。

`g_detach` 函数将给定消费者 `cp` 从其对应的提供者分离，拆除它们之间的通信通道。

## 限制/条件

`g_attach`：

* 消费者不得已附加到提供者。
* 操作不得创建拓扑环。
* 必须持有拓扑锁。

`g_detach`：

* 消费者必须已附加。
* 访问计数必须为 0。
* 不能有活动请求。
* 必须持有拓扑锁。

## 返回值

`g_attach` 函数成功时返回 0；否则返回错误码。

## 实例

创建一个消费者，将其附加到给定提供者，获取读取访问权限并清理。

```c
void
some_function(struct g_geom *mygeom, struct g_provider *pp)
{
	struct g_consumer *cp;
	g_topology_assert();
	/* 在“mygeom”geom 上创建新消费者。 */
	cp = g_new_consumer(mygeom);
	/* 将新创建的消费者附加到给定提供者。 */
	if (g_attach(cp, pp) != 0) {
		g_destroy_consumer(cp);
		return;
	}
	/* 通过我们的消费者以读取方式打开提供者。 */
	if (g_access(cp, 1, 0, 0) != 0) {
		g_detach(cp);
		g_destroy_consumer(cp);
		return;
	}
	g_topology_unlock();
	/*
	 * 从提供者读取数据。
	 */
	g_topology_lock();
	/* 断开与提供者的连接（释放访问计数）。 */
	g_access(cp, -1, 0, 0);
	/* 从提供者分离。 */
	g_detach(cp);
	/* 销毁消费者。 */
	g_destroy_consumer(cp);
}
```

## 错误

可能的错误：

**\[`ELOOP`]** 操作创建了拓扑环。

**\[`ENXIO`]** 提供者已成孤儿。

## 参见

[geom(4)](/man/man4/geom.4.md), [DECLARE\_GEOM\_CLASS(9)](https://github.com/FreeBSD-Ask/freebsd-man-sc/tree/main/man9/declare_geom_class.9.md), [g\_access(9)](/man/man9/g_access.9.md), [g\_bio(9)](/man/man9/g_bio.9.md), [g\_consumer(9)](/man/man9/g_consumer.9.md), [g\_data(9)](/man/man9/g_data.9.md), [g\_event(9)](/man/man9/g_event.9.md), [g\_geom(9)](/man/man9/g_geom.9.md), [g\_provider(9)](/man/man9/g_provider.9.md), [g\_provider\_by\_name(9)](/man/man9/g_provider_by_name.9.md), [g\_wither\_geom(9)](/man/man9/g_wither_geom.9.md)

## 作者

本手册页由 Pawel Jakub Dawidek <pjd@FreeBSD.org> 编写。


---

# 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/g_attach.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.
