mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 08:33:30 +00:00
4075 lines
270 KiB
Markdown
4075 lines
270 KiB
Markdown
---
|
||
title: "使用存储设备与VFS层"
|
||
description: "Developing storage 设备驱动程序 and VFS integration"
|
||
partNumber: 6
|
||
partName: "编写特定传输层驱动程序"
|
||
chapter: 27
|
||
lastUpdated: "2026-04-19"
|
||
status: "complete"
|
||
author: "Edson Brandi"
|
||
reviewer: "TBD"
|
||
translator: "AI辅助翻译为简体中文"
|
||
estimatedReadTime: 225
|
||
language: "zh-CN"
|
||
---
|
||
|
||
# 使用存储设备与VFS层
|
||
|
||
## 引言
|
||
|
||
In the previous chapter, we walked carefully through the life of a USB 串行驱动程序. We followed the 设备 from the moment the 内核 noticed it on the 总线, through 探测 and 附加, into its active life as a 字符设备, and finally out through 分离 when the hardware was unplugged. That walkthrough taught us how transport-specific 驱动程序 live inside FreeBSD. They participate in a 总线, they expose a user-facing abstraction, and they accept that they may vanish at any moment because the underlying hardware is removable.
|
||
|
||
Storage 驱动程序 live in a very different country. The hardware is still real, and many storage 设备 can still be removed unexpectedly, but the role of the 驱动程序 shifts in an important way. A USB serial adapter offers a stream of bytes to one process at a time. A storage 设备 offers a 块-addressable, long-lived, structured surface on which 文件系统 are built. When a user plugs in a USB serial adapter, they might immediately open `/dev/cuaU0` and begin a session. When a user plugs in a disk, they almost never read it as a raw stream. They 挂载 it, and from that moment on the disk disappears behind a 文件系统, behind a cache, behind the Virtual File System layer, and behind the many processes that share files on it.
|
||
|
||
This chapter teaches you what happens on the 驱动程序 side of that arrangement. You will learn what the VFS layer is, how it differs from `devfs`, and how 存储驱动程序 plug into the GEOM 框架 rather than speaking to the VFS layer directly. You will write a small pseudo 块设备 from scratch, expose it as a GEOM provider, give it a working backing store, watch `newfs_ufs` format it, 挂载 the result, create files on it, 卸载 it cleanly, and 分离 it without leaving footprints in the 内核. By the end of the chapter you will have a working mental model of the storage stack and a concrete example 驱动程序 that exercises every layer we discuss.
|
||
|
||
The chapter is long because the topic is layered. Unlike a character 驱动程序, where the main unit of interaction is a single `read` or `write` call from a process, a 存储驱动程序 lives inside a chain of 框架. Requests travel from a process through VFS, through the 缓冲区 cache, through the 文件系统, through GEOM, and only then reach the 驱动程序. Replies travel back the other way. Understanding that chain is essential before writing any real storage code, and it is essential again when diagnosing the kind of subtle failures that appear only under load or during 卸载. We will move slowly through the foundations, then gradually bring more layers into view.
|
||
|
||
As with 第26章, the goal here is not to ship a production 块 驱动程序. The goal is to give you a sturdy, correct, readable first 块 驱动程序 that you understand completely. Real production 存储驱动程序, for SATA disks, NVMe drives, SCSI controllers, SD cards, or virtual 块设备, build on the same patterns. Once the foundations are clear, the step from pseudo to real is mostly a matter of replacing the backing store with code that talks to hardware 寄存器 and DMA engines, and of handling the much richer error and recovery surface that real disks expose.
|
||
|
||
You will also see how 存储驱动程序 interact with tools the reader already knows from the user side of FreeBSD. `mdconfig(8)` will appear as a close cousin of our 驱动程序, since the 内核's `md(4)` RAM disk is exactly the kind of thing we are building. `newfs_ufs(8)`, `挂载(8)`, `u挂载(8)`, `diskinfo(8)`, `gstat(8)`, and `geom(8)` will become tools of verification, not just tools that other people use. The chapter is structured so that by the time you finish, you can look at the output of `gstat -I 1` while running `dd` against your 设备 and read it with understanding.
|
||
|
||
Finally, a note on what we will not cover here. We will not write a real 总线 驱动程序 that talks to a physical storage controller. We will not discuss the internals of UFS, ZFS, FUSE, or other specific 文件系统 beyond what is needed to understand how they meet a 块设备 at the boundary. We will not cover DMA, PCIe, NVMe queues, or SCSI command sets. All of those topics deserve their own treatment and, where relevant, will appear in later chapters that cover specific 总线es and specific subsystems. What we will do here is give you a complete, self-contained 块-layer experience that is representative of how all 存储驱动程序 in FreeBSD integrate with the 内核.
|
||
|
||
Take your time with this chapter. Read slowly, type the code, boot the module, format it, 挂载 it, break it on purpose, watch what happens. The storage stack rewards patience and punishes shortcuts. You are not in a race.
|
||
|
||
## How 第6部分 Differs from Parts 1 Through 5
|
||
|
||
A short framing note before the chapter begins. 第27章 sits inside a Part that asks you to change one specific habit, and that shift is easier to make when it is named up front.
|
||
|
||
Parts 1 through 5 built a single running 驱动程序, `myfirst`, through twenty consecutive chapters, each adding one discipline to the same source tree. 第26章 extended that family with `myfirst_usb` as a transport sibling so the step into real hardware would not also be a step into unfamiliar source. **From 第27章 onward, the running `myfirst` 驱动程序 pauses as the backbone of the book.** 第6部分 shifts to new, self-contained demos that fit each subsystem it teaches: a pseudo 块设备 for storage here in 第27章, and a pseudo 网络接口 for networking in 第28章. These demos are parallel to `myfirst` in spirit but distinct in code, because the patterns that define a 存储驱动程序 or a 网络驱动程序 do not fit the character-设备 mould that `myfirst` grew from.
|
||
|
||
The **discipline and didactic shape continue unchanged**. Each chapter still walks you through 探测, 附加, the primary data path, the cleanup path, labs, challenge exercises, troubleshooting, and a bridge to the next chapter. Each chapter still grounds its examples in real FreeBSD source under `/usr/src`. The habits you built in Chapters 25 and earlier, the labelled-goto cleanup chain, rate-limited logging, `INVARIANTS` and `WITNESS`, the production-readiness checklist, carry over without modification. What changes is the code artefact in front of you: a small, focused 驱动程序 whose shape matches the subsystem under study, rather than one more stage in the `myfirst` timeline.
|
||
|
||
This is a deliberate didactic choice, not an accident of scope. A 存储驱动程序 and a 网络驱动程序 each have their own lifecycle, their own data flow, their own preferred idioms, and their own 框架 to plug into. Teaching them as fresh 驱动程序, rather than as further mutations of `myfirst`, keeps the focus on what makes each subsystem distinctive. A reader who tries to stretch `myfirst` into a 块设备 or a 网络接口 quickly ends up with code that teaches nothing about storage or networking. Fresh demos are the cleaner path, and they are the path this Part takes.
|
||
|
||
Part 7 returns to cumulative learning, but rather than resuming a single running 驱动程序, it revisits the 驱动程序 you have already written (`myfirst`, `myfirst_usb`, and the 第6部分 demos) and teaches the production-minded topics that matter once a first version of a 驱动程序 exists: portability across architectures, advanced debugging, performance tuning, security review, and contribution to the upstream project. The habit of building cumulatively stays with you; only the specific artefact in front of you changes.
|
||
|
||
Keep this framing in mind as 第27章 unfolds. If the switch from `myfirst` to a new pseudo 块设备 feels jarring after twenty chapters of the same source tree, that reaction is expected and passes quickly, usually by the end of Section 3.
|
||
|
||
## 读者指南: How to Use This Chapter
|
||
|
||
This chapter is designed as a guided course through the storage side of the FreeBSD 内核. It is one of the longer chapters in the book because the subject matter is layered and every layer has its own vocabulary, its own concerns, and its own failure modes. You do not need to rush through it.
|
||
|
||
If you choose the **reading-only path**, expect to spend around two to three hours going through the chapter carefully. You will come away with a clear picture of how VFS, the 缓冲区 cache, 文件系统, GEOM, and the 块设备 boundary fit together, and you will have a concrete 驱动程序 in front of you as an anchor for your mental model. This is a legitimate way to use the chapter, especially on a first pass.
|
||
|
||
If you choose the **reading-plus-labs path**, plan for four to six hours spread across one or two evenings, depending on your comfort with 内核模块 from 第26章. You will build the 驱动程序, format it, 挂载 it, watch it under load, and take it apart safely. Expect the mechanics of `kldload`, `kldunload`, `newfs_ufs`, and `挂载` to become second nature by the end.
|
||
|
||
If you choose the **reading-plus-labs-plus-challenges path**, plan for a weekend or two evenings spread over a week. The challenges extend the 驱动程序 in small directions that matter in practice: adding optional flush semantics, responding to `BIO_DELETE` with zeroing, supporting multiple units, exporting extra attributes through `disk_getattr`, and enforcing read-only mode cleanly. Each challenge is self-contained and uses only what the chapter has already covered.
|
||
|
||
Whichever path you choose, do not skip the troubleshooting section. Storage bugs tend to look alike from the outside, and the ability to recognise them by symptom is far more useful in practice than memorising the names of every function in GEOM. The troubleshooting material is placed near the end for readability, but you may find yourself turning back to it while working through the labs.
|
||
|
||
A word on prerequisites. This chapter builds directly on 第26章, so at minimum you should be comfortable writing a small 内核模块, declaring a softc, allocating and freeing resources, and walking through the load and unload path. You should also be comfortable enough with the shell to run `kldload`, `kldstat`, `dmesg`, `挂载`, and `u挂载` without stopping to look up flags. If any of that feels unfamiliar, it is worth revisiting Chapters 5, 14, and 26 before continuing.
|
||
|
||
You should work on a throwaway FreeBSD 14.3 system, a virtual machine, or a branch where you do not mind the occasional 内核 panic. A panic is unlikely if you follow the text carefully, but the cost of a mistake on your development laptop is much higher than the cost of a mistake on a VM snapshot you can roll back. We have said this before and we will keep saying it: 内核 work is safe when you work in a safe place.
|
||
|
||
### Work Section by Section
|
||
|
||
The chapter is organised as a progression. Section 1 introduces VFS. Section 2 contrasts `devfs` with VFS and positions our 驱动程序 in that contrast. Section 3 寄存器 a minimal pseudo 块设备. Section 4 exposes it as a GEOM provider. Section 5 implements real read and write paths. Section 6 挂载s a 文件系统 on top. Section 7 gives the 设备 persistence. Section 8 teaches safe 卸载 and cleanup. Section 9 talks about refactoring, versioning, and what to do as the 驱动程序 grows.
|
||
|
||
You are meant to read them in order. Each section assumes the previous sections are fresh in your mind, and the labs build on each other. If you jump in the middle, pieces will look strange.
|
||
|
||
### Type the Code
|
||
|
||
Typing the code by hand remains the most effective way to internalise 内核 idioms. The companion files under `examples/part-06/ch27-storage-vfs/` exist so that you can check your work, not so that you can skip the typing. Reading code is not the same as writing it.
|
||
|
||
### Open the FreeBSD Source Tree
|
||
|
||
You will be asked several times to open real FreeBSD source files, not only the companion examples. The files of interest include `/usr/src/sys/geom/geom.h`, `/usr/src/sys/sys/bio.h`, `/usr/src/sys/geom/geom_disk.h`, `/usr/src/sys/dev/md/md.c`, and `/usr/src/sys/geom/zero/g_zero.c`. Each of these is a primary reference, and the prose in this chapter will often refer back to them. If you have not already cloned or installed the 14.3 source tree, now is a good moment to do so.
|
||
|
||
### Use Your Lab Logbook
|
||
|
||
Keep your lab logbook from 第26章 open while you work. You will want to record the output of `gstat -I 1`, the messages emitted by `dmesg` when you load and unload the module, the time it takes to format the 设备, and any warnings or panics you see. Kernel work is much easier when you keep notes, because many symptoms look similar at first glance and the logbook lets you compare across sessions.
|
||
|
||
### Pace Yourself
|
||
|
||
If you feel your understanding blurring in a particular section, stop. Read it again. Try a small experiment on the running module. Do not push through a section that has not settled. Storage 驱动程序 punish confusion more severely than character 驱动程序, because confusion at the 块 layer often becomes 文件系统 corruption at the higher layer, and 文件系统 corruption takes time and care to repair even in a throwaway VM.
|
||
|
||
## How to Get the Most Out of This Chapter
|
||
|
||
The chapter is structured so that every section adds exactly one new concept on top of what came before. To make the most of that structure, treat the chapter as a workshop rather than as a reference. You are not here to find a quick answer. You are here to build a correct mental model.
|
||
|
||
### Work in Sections
|
||
|
||
Do not read the whole chapter end to end without stopping. Read one section, then pause. Try the experiment or lab that goes with it. Look at the related FreeBSD source. Write a few lines in your logbook. Only then move on. Storage programming in the 内核 is strongly cumulative, and skipping ahead usually means that you will be confused about the next thing for a reason that was explained two sections ago.
|
||
|
||
### Keep the Driver Running
|
||
|
||
Once you have loaded the 驱动程序 in Section 3, keep it loaded as much as possible while you read. Modify it, reload, poke it with `gstat`, run `dd` against it, call `diskinfo` on it. Having a live, observable example is far more valuable than any 挂载 of reading. You will notice things that no chapter could ever tell you about, because no chapter can show you real timing, real jitter, or real corner cases in your particular setup.
|
||
|
||
### Consult Manual Pages
|
||
|
||
FreeBSD's manual pages are part of the teaching material, not a separate formality. Section 9 of the manual is where the 内核 接口 live. We will refer several times to pages such as `g_bio(9)`, `geom(4)`, `DEVICE_IDENTIFY(9)`, `disk(9)`, `总线_dma(9)`, and `devstat(9)`. Read them alongside this chapter. They are shorter than they look, and they are written by the same community that wrote the 内核 you are working inside.
|
||
|
||
### Type the Code, Then Mutate It
|
||
|
||
When you build the 驱动程序 from the companion examples, type it first. Once it works, start changing things. Rename a method and watch the build fail. Remove an `if` branch and watch what happens when you load the module. Hardcode a smaller media size and watch `newfs_ufs` react. Kernel code becomes understandable through deliberate mutation far more than through pure reading.
|
||
|
||
### Trust the Tooling
|
||
|
||
FreeBSD gives you a wealth of tools for inspecting the storage stack: `geom`, `gstat`, `diskinfo`, `dd`, `mdconfig`, `dmesg`, `kldstat`, `sysctl`. Use them. When something goes wrong, the first move is almost never to read more source. It is to ask the system what state it is in. `geom disk list` and `geom part show` are often more informative than five minutes of grep.
|
||
|
||
### Take Breaks
|
||
|
||
Kernel work is cognitively dense. Two or three focused hours are usually more productive than a seven-hour sprint. If you catch yourself making the same typo three times, or copy-pasting without reading, that is your cue to stand up for ten minutes.
|
||
|
||
With those habits established, let us begin.
|
||
|
||
## 第1节: What Is the Virtual File System Layer?
|
||
|
||
When a process opens a file on FreeBSD, it calls `open(2)` with a path. That path might resolve to a file on UFS, a file on ZFS, a file on a remotely 挂载ed NFS share, a pseudo-file in `devfs`, a file under `procfs`, or even a file inside a FUSE-挂载ed userland 文件系统. The process cannot tell. The process receives a file 描述符 and then reads and writes as if there were only one kind of file in the world. That uniformity is not an accident. It is the work of the Virtual File System layer.
|
||
|
||
### The Problem VFS Solves
|
||
|
||
Before VFS, UNIX 内核s generally knew how to talk to one 文件系统 only. If you wanted a new 文件系统, you modified the code paths for `open`, `read`, `write`, `stat`, `unlink`, `rename`, and every other system call that touched files. That approach worked for a while, but it did not scale. New 文件系统 arrived: NFS, for remote access. MFS, for in-memory scratch space. Procfs, for exposing process state. ISO 9660, for CD-ROM media. FAT, for interoperability. Every addition meant new forks in every file-related system call.
|
||
|
||
Sun Microsystems introduced the Virtual File System architecture in the mid-1980s as a way out of this mess. The idea is simple. The 内核 talks to a single abstract 接口, defined in terms of generic operations on generic file objects. Each concrete 文件系统 寄存器 implementations of those operations, and the 内核 calls them through function pointers. When the 内核 needs to read a file, it does not know or care whether the file lives on UFS or NFS or ZFS. It knows there is a node with a `VOP_READ` method, and it calls that method.
|
||
|
||
FreeBSD adopted this architecture and has extended it significantly over the decades. The result is that adding a 文件系统 to FreeBSD no longer requires modifying the core system calls. A 文件系统 is a separate 内核模块 that 寄存器 a set of operations with VFS, and from that moment VFS routes the right requests to it.
|
||
|
||
### The VFS Object Model
|
||
|
||
VFS defines three main kinds of objects.
|
||
|
||
The first is the **挂载 point**, represented in the 内核 by `struct 挂载`. Every 挂载ed 文件系统 has one, and it records where in the namespace the 文件系统 is 附加ed, what flags it has, and which 文件系统 code is responsible for it.
|
||
|
||
The second is the **vnode**, represented by `struct vnode`. A vnode is the 内核's handle on a single file or directory within a 挂载ed 文件系统. It is not the file itself. It is the 内核's runtime representation of that file for as long as something in the 内核 cares about it. Every file that a process has open has a vnode. Every directory the 内核 is walking through has a vnode. When nothing holds a reference to a vnode, it can be reclaimed, and the 内核 keeps a pool of them to avoid pressure on small-inode cases.
|
||
|
||
The third is the **vnode operations vector**, represented by `struct vop_vector`, which lists the operations each 文件系统 must implement on vnodes. The operations have names like `VOP_LOOKUP`, `VOP_READ`, `VOP_WRITE`, `VOP_CREATE`, `VOP_REMOVE`, `VOP_GETATTR`, and `VOP_SETATTR`. Each 文件系统 provides a pointer to its own vector, and the 内核 invokes operations through these vectors whenever it needs to do anything to a file.
|
||
|
||
The elegant thing about this design is that from the system call side of the 内核, only the abstract 接口 matters. The system call layer calls `VOP_READ(vp, uio, ioflag, cred)` and does not care whether `vp` belongs to UFS, ZFS, NFS, or tmpfs. From the 文件系统 side, only the abstract 接口 matters too. UFS implements the vnode operations and never sees the system call code.
|
||
|
||
### Where Storage Drivers Fit
|
||
|
||
Here is the question that matters for this chapter. If VFS is where 文件系统 live, where do 存储驱动程序 live?
|
||
|
||
The answer is: not directly inside VFS. A 存储驱动程序 does not implement `VOP_READ`. It implements a much lower-level abstraction that looks like a disk. Filesystems then sit on top, consuming the disk-like abstraction, translating file-level operations into 块-level operations, and calling down.
|
||
|
||
The chain of layers between a process and a 块设备 in FreeBSD typically looks like this.
|
||
|
||
```text
|
||
+------------------+
|
||
| user process |
|
||
+--------+---------+
|
||
|
|
||
| read(fd, buf, n)
|
||
v
|
||
+--------+---------+
|
||
| system calls | sys_read, sys_write, sys_open, ...
|
||
+--------+---------+
|
||
|
|
||
v
|
||
+--------+---------+
|
||
| VFS | vfs_read, VOP_READ, vnode cache
|
||
+--------+---------+
|
||
|
|
||
v
|
||
+--------+---------+
|
||
| filesystem | UFS, ZFS, NFS, tmpfs, ...
|
||
+--------+---------+
|
||
|
|
||
v
|
||
+--------+---------+
|
||
| buffer cache | bufcache, bwrite, bread, getblk
|
||
+--------+---------+
|
||
|
|
||
| struct bio
|
||
v
|
||
+--------+---------+
|
||
| GEOM | classes, providers, consumers
|
||
+--------+---------+
|
||
|
|
||
v
|
||
+--------+---------+
|
||
| storage driver | disk_strategy, bio handler
|
||
+--------+---------+
|
||
|
|
||
v
|
||
+--------+---------+
|
||
| hardware | real disk, SSD, or memory buffer
|
||
+------------------+
|
||
```
|
||
|
||
Each layer in this stack has a job. VFS hides 文件系统 differences from system calls. The 文件系统 translates files into 块. The 缓冲区 cache holds recently used 块 in RAM. GEOM routes 块 requests through transforms, 分区s, and mirrors. The 存储驱动程序 converts 块 requests into real I/O. The hardware does the work.
|
||
|
||
For this chapter, almost everything we do happens at the bottom two layers: GEOM and the 存储驱动程序. We will touch the 文件系统 layer briefly when we 挂载 UFS on our 设备, and we will touch VFS only in the sense that `挂载(8)` calls it. The layers above GEOM are not our code.
|
||
|
||
### VFS in the Kernel Source
|
||
|
||
If you want to look at VFS directly, the entry points are under `/usr/src/sys/kern/vfs_*.c`. The vnode layer lives in `vfs_vnops.c` and `vfs_subr.c`. The 挂载 side lives in `vfs_挂载.c`. The vnode operations vector is defined and handled in `vfs_default.c`. UFS, our primary 文件系统 in this chapter, lives under `/usr/src/sys/ufs/ufs/` and `/usr/src/sys/ufs/ffs/`. You do not need to read any of those to follow this chapter. You should know where they are so that you understand what sits above the code you are about to write.
|
||
|
||
### What This Means for Our Driver
|
||
|
||
Because VFS is not our direct caller, we do not need to implement `VOP_` methods. We need to implement the 块-layer 接口 that the 文件系统 ultimately calls into. That 接口 is defined by GEOM and, for disk-like 设备 in particular, by the `g_disk` subsystem. Our 驱动程序 will expose a GEOM provider. A 文件系统 will consume it. The flow of I/O will go through `struct bio` rather than through `struct uio`, and the unit of work will be a 块 rather than a byte range.
|
||
|
||
This is also why 存储驱动程序 rarely interact with `cdevsw` or `make_dev` directly the way character 驱动程序 do. The `/dev` node for a disk is created by GEOM, not by the 驱动程序. The 驱动程序 describes itself to GEOM, and GEOM publishes a provider, which then appears in `/dev` with an automatically generated name.
|
||
|
||
### The VFS Call Chain in Practice
|
||
|
||
Let us trace what happens when a user runs `cat /mnt/myfs/hello.txt`, assuming `/mnt/myfs` is 挂载ed on our future 块设备.
|
||
|
||
First, the process calls `open("/mnt/myfs/hello.txt", O_RDONLY)`. That goes to `sys_openat` in the system call layer, which asks VFS to resolve the path. VFS walks the path one component at a time, calling `VOP_LOOKUP` on each directory vnode. When it reaches `myfs`, it notices that the vnode is a 挂载 point and crosses into the 挂载ed 文件系统. It eventually arrives at the vnode for `hello.txt` and returns a file 描述符.
|
||
|
||
Second, the process calls `read(fd, buf, 64)`. That goes to `sys_read`, which calls `vn_read`, which calls `VOP_READ` on the vnode. The UFS implementation of `VOP_READ` consults its inode, figures out which disk 块 hold the requested bytes, and asks the 缓冲区 cache for those 块. If the 块 are not cached, the 缓冲区 cache calls `bread`, which ultimately builds a `struct bio` and hands it to GEOM.
|
||
|
||
Third, GEOM looks at the provider the 文件系统 is consuming. Through a chain of providers and consumers, the `bio` ends up at the bottom provider, which is our 驱动程序's provider. Our strategy function receives the `bio`, reads the requested bytes from our backing store, and calls `biodone` or `g_io_deliver` to complete the request.
|
||
|
||
Fourth, the reply travels back the other way. The 缓冲区 cache gets its data, the 文件系统 returns to `vn_read`, `vn_read` copies the data into the user 缓冲区, and `sys_read` returns.
|
||
|
||
None of that code is ours except the last hop. But understanding the whole chain is what lets you make sensible design choices when you write the last hop.
|
||
|
||
### 总结 Section 1
|
||
|
||
VFS is the layer that unifies 文件系统 in FreeBSD. It sits between the system call 接口 and the various concrete 文件系统, and it provides the abstraction that makes files look identical regardless of where they live. Storage 驱动程序 do not live inside VFS. They live at the bottom of the stack, far below VFS, behind GEOM and the 缓冲区 cache. Our job in this chapter is to write a 驱动程序 that participates correctly in that lower layer, and to understand enough about the upper layers to avoid confusion when diagnosing problems.
|
||
|
||
In the next section, we will sharpen the distinction between `devfs` and VFS, because that distinction determines which mental model applies when you think about a given 设备 node.
|
||
|
||
## 第2节: devfs vs VFS
|
||
|
||
Beginners often assume that `devfs` and the Virtual File System layer are two names for the same thing. They are not. They are related, but they play very different roles. Getting this distinction right early saves a great deal of confusion later, especially when thinking about 存储驱动程序, because 存储驱动程序 straddle both of them.
|
||
|
||
### What devfs Is
|
||
|
||
`devfs` is a 文件系统. That sounds circular, but it is true. `devfs` is implemented as a 文件系统 module, 注册ed with VFS, and 挂载ed at `/dev` on every FreeBSD system. When you read a file under `/dev`, you are reading through VFS, which hands the request to `devfs`, which recognises that the "file" you are reading is really a 内核 设备 node and routes the call to the appropriate 驱动程序.
|
||
|
||
`devfs` has several special properties that distinguish it from an ordinary 文件系统 like UFS.
|
||
|
||
First, its contents are not stored on disk. The "files" in `devfs` are synthesised by the 内核 based on which 驱动程序 are currently loaded and which 设备 are currently present. When a 驱动程序 calls `make_dev(9)` to create `/dev/mybox`, `devfs` adds the corresponding node to its view. When the 驱动程序 destroys that 设备 with `destroy_dev(9)`, `devfs` removes the node. The user sees `/dev/mybox` appear and disappear in real time.
|
||
|
||
Second, the read and write paths for `devfs` nodes are not file data paths. When you write to `/dev/myserial0`, you are not appending bytes to a stored file. You are invoking the 驱动程序's `d_write` function through `cdevsw`, and that function decides what those bytes mean. In the case of a USB 串行驱动程序, they mean bytes to transmit on the wire. In the case of a pseudo 设备 like `/dev/null`, they mean bytes to discard.
|
||
|
||
Third, the metadata of `devfs` nodes, such as permissions and ownership, is managed by a policy layer in the 内核 rather than by the 文件系统 itself. `devfs_ruleset(8)` and the `devd` 框架 configure that policy.
|
||
|
||
Fourth, `devfs` supports cloning, which character 驱动程序 like `pty`, `tun`, and `bpf` use to create a new minor 设备 whenever a process opens the node. This is how `/dev/ptyp0`, `/dev/ptyp1`, and their successors come into existence on demand.
|
||
|
||
### What VFS Is
|
||
|
||
VFS, as we saw in Section 1, is the abstract 文件系统 layer. Every 文件系统 on a FreeBSD system, including `devfs`, is 注册ed with VFS and invoked through VFS. VFS is not a 文件系统. It is the 框架 that 文件系统 plug into.
|
||
|
||
When you open a file on UFS, the chain is: system call -> VFS -> UFS -> 缓冲区 cache -> GEOM -> 驱动程序. When you open a node in `devfs`, the chain is: system call -> VFS -> devfs -> 驱动程序. Both go through VFS. Only the UFS chain involves GEOM.
|
||
|
||
### Why Storage Drivers Live on Both Sides
|
||
|
||
This is where 存储驱动程序 become interesting.
|
||
|
||
A 存储驱动程序 exposes a 块设备, and that 块设备 eventually appears as a node under `/dev`. For example, if we 注册 our 驱动程序 and tell GEOM about it, a node called `/dev/myblk0` may appear in `devfs`. When a user writes `dd if=image.iso of=/dev/myblk0`, they are writing through `devfs` to a special character 接口 that GEOM provides on top of our disk. The requests flow as BIO through GEOM and into our strategy function.
|
||
|
||
But when a user runs `newfs_ufs /dev/myblk0` and then `挂载 /dev/myblk0 /mnt`, the usage pattern changes. The 内核 now 挂载s UFS on top of the 设备. When a process later reads a file under `/mnt`, the path is: system call -> VFS -> UFS -> 缓冲区 cache -> GEOM -> 驱动程序. The `/dev/myblk0` node in `devfs` is not even involved in the hot path. UFS and the 缓冲区 cache talk directly to the GEOM provider. The `devfs` node is essentially a handle that tools use to refer to the 设备, not the pipe that file data flows through during normal operation.
|
||
|
||
### A Closer Look at the Buffer Cache
|
||
|
||
Between the 文件系统 and GEOM in the storage path sits the 缓冲区 cache. We have mentioned it several times without pausing to describe it. Let us pause now, because it explains several of the behaviours you will observe when testing your 驱动程序.
|
||
|
||
The 缓冲区 cache is a pool of fixed-size 缓冲区 in 内核 memory, each of which holds one 文件系统 块. When a 文件系统 reads a 块, the 缓冲区 cache gets involved: the 文件系统 asks the cache for the 块, and the cache either returns a hit (the 块 is already in memory) or issues a miss (the cache allocates a 缓冲区, calls down through GEOM to fetch the data, and returns the 缓冲区 once the read completes). When a 文件系统 writes a 块, the same cache path applies in reverse: the write fills a 缓冲区, the 缓冲区 is marked dirty, and the cache schedules a write-back at some later point.
|
||
|
||
The 缓冲区 cache is why consecutive reads of the same file data do not always hit the 驱动程序. The first read misses, causing a BIO to travel to the 驱动程序. The second read hits the cache and returns immediately. This is a great feature for performance. It can be mildly confusing when you are first debugging a 驱动程序, because your `printf` in the strategy function does not fire on every user-space read.
|
||
|
||
The 缓冲区 cache is also why writes can appear to happen faster than the underlying 驱动程序. A `dd if=/dev/zero of=/mnt/myblk/big bs=1m count=16` may appear to complete in a fraction of a second because the writes land in the cache and the cache defers the actual BIOs for a while. The 文件系统 issues the real writes to GEOM over the next second or two. If the system crashes before that happens, the file on disk is incomplete. `sync(2)` forces the cache to flush to the underlying 设备. `fsync(2)` flushes only the 缓冲区 associated with a single file 描述符.
|
||
|
||
The 缓冲区 cache is distinct from the page cache. FreeBSD has both, and they cooperate. The page cache holds memory pages that back memory-mapped files and anonymous memory. The 缓冲区 cache holds 缓冲区 that back 文件系统-块 operations. Modern FreeBSD has largely unified them for many data paths, but the distinction still shows up in the source tree, particularly around `bread`, `bwrite`, `getblk`, and `brelse`, which are the 缓冲区-cache side of the 接口.
|
||
|
||
The 缓冲区 cache has a single most important implication for our 驱动程序: we will almost never see completely synchronous BIO traffic. When a 文件系统 wants to read a 块, a BIO arrives in our strategy function; when a 文件系统 wants to write a 块, another BIO arrives, but usually some time later than the write system call that prompted it. BIOs also arrive in bursts when the cache flushes. This is normal, and your 驱动程序 must not make assumptions about timing or ordering across BIOs other than what is strictly documented. Each BIO is an independent request.
|
||
|
||
### The Read and Write Paths
|
||
|
||
Let us trace a concrete example through the whole chain.
|
||
|
||
When a user runs `cat /mnt/myblk/hello.txt`, the shell runs `cat`, which calls `open("/mnt/myblk/hello.txt", O_RDONLY)`. The `open` goes to `sys_openat`, which hands off to VFS. VFS calls `namei` to walk the path. For each path component, VFS calls `VOP_LOOKUP` on the current directory's vnode. When VFS reaches the `myblk` 挂载, it crosses into UFS, which walks the UFS directory structure to find `hello.txt`. UFS returns the vnode for that file, and VFS returns a file 描述符.
|
||
|
||
The user then calls `read(fd, buf, 64)`. `sys_read` calls `vn_read`, which calls `VOP_READ` on the vnode. UFS's `VOP_READ` consults the inode to find the 块 address of the requested bytes, then calls `bread` on the 缓冲区 cache to fetch the 块. The 缓冲区 cache either returns a hit or issues a BIO.
|
||
|
||
If it is a cache miss, the 缓冲区 cache allocates a fresh 缓冲区, builds a BIO that asks for the relevant 块 from the underlying GEOM provider, and hands it off. The BIO travels down through GEOM, through our strategy function, and back. When the BIO completes, the 缓冲区 cache un块 the waiting `bread` call. UFS then copies the requested bytes from the 缓冲区 into the user's `buf`. `read` returns.
|
||
|
||
For writes, the chain is symmetric but the timing is different. UFS's `VOP_WRITE` calls `bread` or `getblk` to obtain the target 缓冲区, copies the user's data into the 缓冲区, marks the 缓冲区 dirty, and calls `bdwrite` or `bawrite` to schedule the write-back. The user's `write` call returns long before the BIO is issued to the 驱动程序. Later, the 缓冲区 cache's syncer thread picks up dirty 缓冲区 and issues BIO_WRITE requests to the 驱动程序.
|
||
|
||
The net effect is that our 驱动程序's strategy function sees a stream of BIOs that is related to, but not identical to, the stream of user-space reads and writes. The 缓冲区 cache mediates the two.
|
||
|
||
In other words, the same 存储驱动程序 can be reached two different ways.
|
||
|
||
1. **Raw access through `/dev`**: a user-space program opens `/dev/myblk0` and issues `read(2)` or `write(2)` calls. Those calls go through `devfs` and the GEOM character 接口, ending up in our strategy function.
|
||
2. **Filesystem access through 挂载**: the 内核 挂载s a 文件系统 on the 设备. File I/O flows through VFS, the 文件系统, the 缓冲区 cache, and GEOM. `devfs` is not part of the hot path for those requests.
|
||
|
||
Both paths converge at the GEOM provider, which is why GEOM is the correct abstraction for 存储驱动程序 even though character 驱动程序 typically deal with `devfs` more directly.
|
||
|
||
### Why This Distinction Matters
|
||
|
||
This matters for two reasons.
|
||
|
||
First, it clarifies why we will not use `make_dev` for our 块 驱动程序. `make_dev` is the right call for character 驱动程序 that want to publish a `cdevsw` under `/dev`. It is the wrong call for a 块设备, because GEOM creates the `/dev` node for us as soon as we publish a provider. If you call `make_dev` in a 存储驱动程序, you typically end up with two `/dev` nodes competing for the same 设备, one of which is not connected to the GEOM topology, which leads to confusing behaviour.
|
||
|
||
Second, the distinction explains why the 内核 has two sets of tools for inspecting 设备 state. `devfs_ruleset(8)`, `devfs.rules`, and per-node permissions belong to `devfs`. `geom(8)`, `gstat(8)`, `diskinfo(8)`, and the GEOM class tree belong to GEOM. When you are diagnosing a permissions problem, you look at `devfs`. When you are diagnosing an I/O problem, you look at GEOM.
|
||
|
||
### A Concrete Example: /dev/null and /dev/ada0
|
||
|
||
Compare two examples you already know.
|
||
|
||
`/dev/null` is a classic 字符设备. It lives under `/dev` because `devfs` creates it. The 驱动程序 is `null(4)`, and its source is in `/usr/src/sys/dev/null/null.c`. When you write to `/dev/null`, `devfs` routes the request through `cdevsw` to the `null` 驱动程序's write function, which simply discards the bytes. There is no GEOM, no 缓冲区 cache, no 文件系统. It is a raw `devfs` character node.
|
||
|
||
`/dev/ada0` is a 块设备. It also lives under `/dev`. But the node is created by GEOM, not by a direct `make_dev` call in the `ada` 驱动程序. When you read raw bytes from `/dev/ada0`, those bytes flow through GEOM's character 接口 layer and arrive at the `ada` 驱动程序's strategy function. When you 挂载 UFS on `/dev/ada0` and then read a file, the file data flows through VFS, UFS, the 缓冲区 cache, and GEOM, and ends up in the same strategy function, without passing through `devfs` for each request.
|
||
|
||
The node in `devfs` is the same. The usage pattern is different. The 驱动程序 must handle both.
|
||
|
||
### How We Will Proceed
|
||
|
||
We will not write a character 驱动程序 in this chapter. We already wrote one in 第26章. Instead, we will write a 驱动程序 that 寄存器 with GEOM as a disk, and we will let GEOM create the `/dev` node for us. The devfs integration will be automatic.
|
||
|
||
This is the dominant pattern for 块 驱动程序 in FreeBSD 14.3. You can see it in `md(4)`, in `ata(4)`, in `nvme(4)`, and in almost every other 存储驱动程序. Each of them 寄存器 with GEOM, each of them receives `bio` requests, and each of them lets GEOM handle the `/dev` node.
|
||
|
||
### 总结 Section 2
|
||
|
||
`devfs` and VFS are distinct layers. `devfs` is a 文件系统 挂载ed at `/dev`, and VFS is the abstract 框架 that all 文件系统 plug into, including `devfs`. Storage 驱动程序 interact with both, but through GEOM, which takes care of creating the `/dev` node and of routing requests from both raw-access and 文件系统-access paths. For this chapter, we will use GEOM as our entry point and let it manage `devfs` on our behalf.
|
||
|
||
In the next section, we will begin building the 驱动程序. We will start with the minimum needed to 注册 a pseudo 块设备 with GEOM, without yet implementing real I/O. Once that is in place, we will add the backing store, the `bio` handler, and everything else in later sections.
|
||
|
||
## 第3节: Registering a Pseudo Block Device
|
||
|
||
In this section we will create a skeleton 驱动程序 that 寄存器 a pseudo 块设备 with the 内核. We will not yet implement read or write. We will not yet wire it up to a backing store. Our goal is more modest and more important: we want to understand exactly what it takes to make the 内核 recognise our code as a 存储驱动程序, publish a `/dev` node for it, and let tools like `geom(8)` see it.
|
||
|
||
Once that is working, everything we add later will be purely incremental. The registration itself is the step that feels most mysterious, and it is the one the rest of the 驱动程序 builds on.
|
||
|
||
### The g_disk API
|
||
|
||
FreeBSD gives 存储驱动程序 a high-level registration API called `g_disk`. It lives in `/usr/src/sys/geom/geom_disk.c` and `/usr/src/sys/geom/geom_disk.h`. The API wraps the lower-level GEOM class machinery and exposes a simpler 接口 that matches what disk 驱动程序 usually need.
|
||
|
||
Using `g_disk` saves us from implementing a full `g_class` by hand. With `g_disk`, we allocate a `struct disk`, fill in a handful of fields and 回调 pointers, and call `disk_create`. The API takes care of building the GEOM class, creating the geom, publishing the provider, wiring up the character 接口, starting devstat accounting, and making our 设备 visible to userland through `/dev`.
|
||
|
||
Not every 存储驱动程序 uses `g_disk`. GEOM classes that do transformations on other providers, like `g_nop`, `g_mirror`, `g_stripe`, or `g_eli`, are built directly on the lower-level `g_class` machinery because they are not disk-shaped. But for anything that looks like a disk, and certainly for a pseudo-disk like ours, `g_disk` is the right starting point.
|
||
|
||
You can see the public structure in `/usr/src/sys/geom/geom_disk.h`. The shape is roughly the following, abbreviated for clarity.
|
||
|
||
```c
|
||
struct disk {
|
||
struct g_geom *d_geom;
|
||
struct devstat *d_devstat;
|
||
|
||
const char *d_name;
|
||
u_int d_unit;
|
||
|
||
disk_open_t *d_open;
|
||
disk_close_t *d_close;
|
||
disk_strategy_t *d_strategy;
|
||
disk_ioctl_t *d_ioctl;
|
||
disk_getattr_t *d_getattr;
|
||
disk_gone_t *d_gone;
|
||
|
||
u_int d_sectorsize;
|
||
off_t d_mediasize;
|
||
u_int d_fwsectors;
|
||
u_int d_fwheads;
|
||
u_int d_maxsize;
|
||
u_int d_flags;
|
||
|
||
void *d_drv1;
|
||
|
||
/* other fields elided */
|
||
};
|
||
```
|
||
|
||
The fields break down into three groups.
|
||
|
||
**Identification**: `d_name` is a short string like `"myblk"` that names the disk class, and `d_unit` is a small integer that distinguishes multiple instances. Together they form the `/dev` node name. A 驱动程序 with `d_name = "myblk"` and `d_unit = 0` publishes `/dev/myblk0`.
|
||
|
||
**Callbacks**: the `d_open`, `d_close`, `d_strategy`, `d_ioctl`, `d_getattr`, and `d_gone` pointers are the functions the 内核 will call into our 驱动程序. Of these, only `d_strategy` is strictly required, because that is the function that handles actual I/O. The others are optional and we will discuss them as they become relevant.
|
||
|
||
**Geometry**: `d_扇区ize`, `d_mediasize`, `d_fw扇区`, `d_fwheads`, and `d_maxsize` describe the disk's physical and logical shape. `d_扇区ize` is the size of a 扇区 in bytes, typically 512 or 4096. `d_mediasize` is the total size of the 设备 in bytes. `d_fw扇区` and `d_fwheads` are advisory hints used by 分区ing tools. `d_maxsize` is the largest single I/O the 驱动程序 can accept, which GEOM will use to split large requests.
|
||
|
||
**Driver state**: `d_drv1` is a generic pointer for the 驱动程序 to stash its own context. It is the closest equivalent to `设备_get_softc(dev)` in the New总线 world.
|
||
|
||
### A Minimum Skeleton
|
||
|
||
Let us now sketch a minimum skeleton. We will place this in `examples/part-06/ch27-storage-vfs/myfirst_blk.c`. This initial version does almost nothing useful. It 寄存器 a disk, returns success on every operation, and un寄存器 cleanly on unload. But it is enough to appear in `/dev`, to be visible in `geom disk list`, and to be 探测d by `newfs_ufs` or `fdisk` without the 内核 crashing.
|
||
|
||
```c
|
||
/*
|
||
* myfirst_blk.c - a minimal pseudo block device driver.
|
||
*
|
||
* This driver registers a single pseudo disk called myblk0 with
|
||
* the g_disk subsystem. It is intentionally not yet capable of
|
||
* performing real I/O. Sections 4 and 5 of Chapter 27 will add
|
||
* the BIO handler and the backing store.
|
||
*/
|
||
|
||
#include <sys/param.h>
|
||
#include <sys/systm.h>
|
||
#include <sys/kernel.h>
|
||
#include <sys/module.h>
|
||
#include <sys/malloc.h>
|
||
#include <sys/lock.h>
|
||
#include <sys/mutex.h>
|
||
#include <sys/bio.h>
|
||
|
||
#include <geom/geom.h>
|
||
#include <geom/geom_disk.h>
|
||
|
||
#define MYBLK_NAME "myblk"
|
||
#define MYBLK_SECTOR 512
|
||
#define MYBLK_MEDIASIZE (1024 * 1024) /* 1 MiB to start */
|
||
|
||
struct myblk_softc {
|
||
struct disk *disk;
|
||
struct mtx lock;
|
||
u_int unit;
|
||
};
|
||
|
||
static MALLOC_DEFINE(M_MYBLK, "myblk", "myfirst_blk driver state");
|
||
|
||
static struct myblk_softc *myblk_unit0;
|
||
|
||
static void
|
||
myblk_strategy(struct bio *bp)
|
||
{
|
||
|
||
/*
|
||
* No real I/O yet. Mark every request as successful
|
||
* but unimplemented so the caller does not hang.
|
||
*/
|
||
bp->bio_error = ENXIO;
|
||
bp->bio_flags |= BIO_ERROR;
|
||
bp->bio_resid = bp->bio_bcount;
|
||
biodone(bp);
|
||
}
|
||
|
||
static int
|
||
myblk_attach_unit(struct myblk_softc *sc)
|
||
{
|
||
|
||
sc->disk = disk_alloc();
|
||
sc->disk->d_name = MYBLK_NAME;
|
||
sc->disk->d_unit = sc->unit;
|
||
sc->disk->d_strategy = myblk_strategy;
|
||
sc->disk->d_sectorsize = MYBLK_SECTOR;
|
||
sc->disk->d_mediasize = MYBLK_MEDIASIZE;
|
||
sc->disk->d_maxsize = MAXPHYS;
|
||
sc->disk->d_drv1 = sc;
|
||
|
||
disk_create(sc->disk, DISK_VERSION);
|
||
return (0);
|
||
}
|
||
|
||
static void
|
||
myblk_detach_unit(struct myblk_softc *sc)
|
||
{
|
||
|
||
if (sc->disk != NULL) {
|
||
disk_destroy(sc->disk);
|
||
sc->disk = NULL;
|
||
}
|
||
}
|
||
|
||
static int
|
||
myblk_loader(struct module *m, int what, void *arg)
|
||
{
|
||
struct myblk_softc *sc;
|
||
int error;
|
||
|
||
switch (what) {
|
||
case MOD_LOAD:
|
||
sc = malloc(sizeof(*sc), M_MYBLK, M_WAITOK | M_ZERO);
|
||
mtx_init(&sc->lock, "myblk lock", NULL, MTX_DEF);
|
||
sc->unit = 0;
|
||
error = myblk_attach_unit(sc);
|
||
if (error != 0) {
|
||
mtx_destroy(&sc->lock);
|
||
free(sc, M_MYBLK);
|
||
return (error);
|
||
}
|
||
myblk_unit0 = sc;
|
||
printf("myblk: loaded, /dev/%s%u size=%jd bytes\n",
|
||
MYBLK_NAME, sc->unit,
|
||
(intmax_t)sc->disk->d_mediasize);
|
||
return (0);
|
||
|
||
case MOD_UNLOAD:
|
||
sc = myblk_unit0;
|
||
if (sc == NULL)
|
||
return (0);
|
||
myblk_detach_unit(sc);
|
||
mtx_destroy(&sc->lock);
|
||
free(sc, M_MYBLK);
|
||
myblk_unit0 = NULL;
|
||
printf("myblk: unloaded\n");
|
||
return (0);
|
||
|
||
default:
|
||
return (EOPNOTSUPP);
|
||
}
|
||
}
|
||
|
||
static moduledata_t myblk_mod = {
|
||
"myblk",
|
||
myblk_loader,
|
||
NULL
|
||
};
|
||
|
||
DECLARE_MODULE(myblk, myblk_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
|
||
MODULE_VERSION(myblk, 1);
|
||
```
|
||
|
||
Take a moment to read this through. Only a handful of moving pieces are visible, but each of them is doing real work.
|
||
|
||
The `myblk_softc` structure is the 驱动程序-local context. It holds a pointer to our `struct disk`, a 互斥锁 for future use, and the unit number. We allocate it on module load and free it on unload.
|
||
|
||
The `myblk_strategy` function is the 回调 that GEOM will invoke whenever a `bio` is directed at our 设备. In this first version, we simply fail every request with `ENXIO`. That is not polite, but it is correct as a placeholder: the 内核 will not 块 waiting for us, and we will not pretend that I/O succeeded when it did not. In Section 5 we will replace this with a working handler.
|
||
|
||
The `myblk_附加_unit` function allocates a `struct disk`, fills in the identification, 回调, and geometry fields, and publishes it with `disk_create`. The call to `disk_create` is what actually produces the `/dev` node and 寄存器 the disk in the GEOM topology.
|
||
|
||
The `myblk_分离_unit` function reverses that. `disk_destroy` asks GEOM to wither the provider, cancel any pending I/O, and remove the `/dev` node. We set `sc->disk` to `NULL` so that subsequent unload attempts do not try to free an already-freed structure, though in the load/unload path we follow that cannot happen.
|
||
|
||
The module loader is a standard `moduledata_t` boilerplate that you saw in 第26章. On `MOD_LOAD` it allocates the softc and calls `myblk_附加_unit`. On `MOD_UNLOAD` it calls `myblk_分离_unit`, frees the softc, and returns.
|
||
|
||
One line deserves special attention.
|
||
|
||
The call `disk_create(sc->disk, DISK_VERSION)` passes the current ABI version of the disk structure. `DISK_VERSION` is defined in `/usr/src/sys/geom/geom_disk.h` and increments every time the `g_disk` ABI changes incompatibly. If you compile a 驱动程序 against the wrong tree, the 内核 will refuse to 注册 the disk and will print a diagnostic. This versioning is what allows the 内核 to evolve without silently breaking out-of-tree 驱动程序.
|
||
|
||
You may wonder why we do not use `MODULE_DEPEND` to declare a dependency on `g_disk`. The reason is that `g_disk` is not a loadable 内核模块 in the usual sense. It is a GEOM class declared in the 内核 via `DECLARE_GEOM_CLASS(g_disk_class, g_disk)` in `/usr/src/sys/geom/geom_disk.c`, and it is always present whenever GEOM itself is compiled into the 内核. There is no separate `g_disk.ko` file you can unload or reload independently, and `MODULE_DEPEND(myblk, g_disk, ...)` would not resolve to a real module. The symbols we call (`disk_alloc`, `disk_create`, `disk_destroy`) come from the 内核 itself.
|
||
|
||
### The Makefile
|
||
|
||
The Makefile for this module is almost identical to the one from 第26章.
|
||
|
||
```make
|
||
# Makefile for myfirst_blk.
|
||
#
|
||
# Companion file for Chapter 27 of
|
||
# "FreeBSD Device Drivers: From First Steps to Kernel Mastery".
|
||
|
||
KMOD = myblk
|
||
SRCS = myfirst_blk.c
|
||
|
||
# Where the kernel build machinery lives.
|
||
.include <bsd.kmod.mk>
|
||
```
|
||
|
||
Place this in the same directory as `myfirst_blk.c`. Running `make` will build `myblk.ko`. Running `make load` will load it if you have the 内核 sources installed in the usual place. Running `make unload` will unload it.
|
||
|
||
### Loading and Inspecting the Skeleton
|
||
|
||
Once the module is loaded, the 内核 will have created a pseudo disk and a `/dev` node for it. Let us walk through what you should see.
|
||
|
||
```console
|
||
# kldload ./myblk.ko
|
||
# dmesg | tail -n 1
|
||
myblk: loaded, /dev/myblk0 size=1048576 bytes
|
||
# ls -l /dev/myblk0
|
||
crw-r----- 1 root operator 0x8b Apr 19 18:04 /dev/myblk0
|
||
# diskinfo -v /dev/myblk0
|
||
/dev/myblk0
|
||
512 # sectorsize
|
||
1048576 # mediasize in bytes (1.0M)
|
||
2048 # mediasize in sectors
|
||
0 # stripesize
|
||
0 # stripeoffset
|
||
myblk0 # Disk ident.
|
||
```
|
||
|
||
The `c` at the beginning of the permissions string tells us that GEOM has created a 字符设备 node, which is how FreeBSD exposes 块-oriented 设备 under `/dev` in the modern 内核. The 设备 major number, here `0x8b`, is assigned dynamically.
|
||
|
||
Now let us look at the GEOM topology.
|
||
|
||
```console
|
||
# geom disk list myblk0
|
||
Geom name: myblk0
|
||
Providers:
|
||
1. Name: myblk0
|
||
Mediasize: 1048576 (1.0M)
|
||
Sectorsize: 512
|
||
Mode: r0w0e0
|
||
descr: (null)
|
||
ident: (null)
|
||
rotationrate: unknown
|
||
fwsectors: 0
|
||
fwheads: 0
|
||
```
|
||
|
||
`Mode: r0w0e0` means zero readers, zero writers, zero exclusive holders. Nobody is using the disk.
|
||
|
||
Now try something harmless.
|
||
|
||
```console
|
||
# dd if=/dev/myblk0 of=/dev/null bs=512 count=1
|
||
dd: /dev/myblk0: Device not configured
|
||
0+0 records in
|
||
0+0 records out
|
||
0 bytes transferred in 0.000123 secs (0 bytes/sec)
|
||
```
|
||
|
||
The `Device not configured` error is the `ENXIO` we deliberately returned. Our strategy function ran, marked the BIO as failed, and `dd` faithfully reported the failure. This is the first real evidence that our 驱动程序 is being reached by the 内核's 块-layer code.
|
||
|
||
Try a read that expects success to fail loudly.
|
||
|
||
```console
|
||
# newfs_ufs /dev/myblk0
|
||
newfs: /dev/myblk0: read-only
|
||
# newfs_ufs -N /dev/myblk0
|
||
/dev/myblk0: 1.0MB (2048 sectors) block size 32768, fragment size 4096
|
||
using 4 cylinder groups of 0.31MB, 10 blks, 40 inodes.
|
||
super-block backups (for fsck_ffs -b #) at:
|
||
192, 832, 1472, 2112
|
||
```
|
||
|
||
The `-N` flag tells `newfs` to plan the 文件系统 layout without writing anything. We can see that it thinks of our 设备 as a small disk with 2048 扇区 of 512 bytes each. That matches the geometry we declared. It is not yet actually writing anything because our strategy function would still fail, but the planning works.
|
||
|
||
Finally, let us unload the module cleanly.
|
||
|
||
```console
|
||
# kldunload myblk
|
||
# dmesg | tail -n 1
|
||
myblk: unloaded
|
||
# ls /dev/myblk0
|
||
ls: /dev/myblk0: No such file or directory
|
||
```
|
||
|
||
That is the complete life cycle of the skeleton.
|
||
|
||
### Why the Failures Are Expected
|
||
|
||
At this stage, any user-space tool that actually tries to read or write data will fail. That is correct. Our strategy function does not yet know how to do anything, and we must not fake success. Faking success would lead to corruption the moment a 文件系统 tried to read back what it thought it had written.
|
||
|
||
The fact that the 内核 and the tools gracefully handle our failure is evidence that the 块 layer is doing its job. A `bio` came down, the 驱动程序 rejected it, the error propagated back up to user space, and no one crashed. That is the kind of behaviour we want.
|
||
|
||
### How Things Fit Together
|
||
|
||
Before moving on, let us name the pieces so we can refer to them later without ambiguity.
|
||
|
||
Our **驱动程序 module** is `myblk.ko`. It is what the user loads with `kldload`.
|
||
|
||
Our **softc** is `struct myblk_softc`. It holds 驱动程序-local state. There is exactly one instance in this first version.
|
||
|
||
Our **disk** is a `struct disk` allocated by `disk_alloc` and 注册ed with `disk_create`. The 内核 owns its memory. We do not free it directly. We ask the 内核 to free it by calling `disk_destroy`.
|
||
|
||
Our **geom** is the GEOM object the `g_disk` subsystem creates on our behalf. We do not see it directly in our code. It exists in the GEOM topology as the parent of our provider.
|
||
|
||
Our **provider** is the producer-facing face of our 设备. It is what other GEOM classes consume when they connect to us. GEOM automatically creates a 字符设备 node for our provider under `/dev`.
|
||
|
||
Our **consumer** is still empty. We have no one connected to us yet. Consumers are how GEOM classes that sit above us, like a 分区ing layer or a 文件系统's GEOM consumer, 附加.
|
||
|
||
Our **/dev node** is `/dev/myblk0`. It is a live handle that user-space tools can use to issue raw I/O. When a 文件系统 is later 挂载ed on the 设备, it will also refer to the 设备 by this name, even though the hot I/O path will not pass through `devfs` for each request.
|
||
|
||
### 总结 Section 3
|
||
|
||
We built the smallest possible 驱动程序 that participates in the FreeBSD storage stack. It 寄存器 a pseudo disk with the `g_disk` subsystem, publishes a `/dev` node through GEOM, accepts BIO requests, and declines them politely. It loads, it appears in `geom disk list`, and it unloads without leaks.
|
||
|
||
In the next section, we will look at GEOM more directly. We will understand what a provider really is, what a consumer really is, and how the class-based design lets transformations like 分区ing, mirroring, encryption, and compression compose with our 驱动程序 for free. That understanding will set us up for Section 5, where we replace the placeholder strategy function with one that actually serves reads and writes from a backing store.
|
||
|
||
## 第4节: Exposing a GEOM-Backed Provider
|
||
|
||
The previous section let us 注册 a disk with `g_disk` and take the word of the 框架 for what happens under the hood. That is a reasonable first step, and for many 驱动程序 it is all the involvement with GEOM that they ever need. But storage work rewards understanding the layer you are sitting on. When a 文件系统 挂载 fails, when `gstat` shows requests piling up, or when a `kldunload` 块 for longer than you expect, you will want to know the vocabulary of GEOM well enough to ask the right questions.
|
||
|
||
This section is a tour of GEOM from the storage-驱动程序 perspective. It is not an exhaustive reference. There are entire chapters in the FreeBSD Developer's Handbook devoted to GEOM, and we will not duplicate them. What we will do is describe the concepts and objects that matter to a 驱动程序 author, and show how `g_disk` fits into that picture.
|
||
|
||
### GEOM in One Page
|
||
|
||
GEOM is a storage 框架. It sits between 文件系统 and the 块 驱动程序 that talk to real hardware, and it composes by design. That composition is the whole point.
|
||
|
||
The idea is that a storage stack is built out of small transformations. One transformation presents a raw disk. Another transformation splits it into 分区s. Another transformation mirrors two disks into one. Another transformation encrypts a 分区. Another transformation compresses a 文件系统. Each transformation is a small piece of code that takes in I/O requests from above, does something to them, and either returns a result directly or passes them along to the next layer below.
|
||
|
||
In GEOM's vocabulary, each transformation is a **class**. Each instance of a class is a **geom**. Each geom has some number of **providers**, which are its outputs, and some number of **consumers**, which are its inputs. Providers face upward toward the next layer. Consumers face downward toward the previous layer. A geom with no consumer is at the bottom of the stack: it must produce I/O on its own. A geom with no provider is at the top of the stack: it must terminate I/O and deliver it somewhere outside GEOM, typically into a 文件系统 or into a `devfs` 字符设备.
|
||
|
||
Requests flow from providers to consumers through the stack. Replies flow back. The unit of I/O is a `struct bio`, which we will study in detail in Section 5.
|
||
|
||
### A Concrete Example of Composition
|
||
|
||
Imagine you have a 1 TB SATA SSD. The 内核's `ada(4)` 驱动程序 runs on the SATA controller and publishes a disk provider called `ada0`. That is a geom with no consumer at the bottom and one provider at the top.
|
||
|
||
You slice the SSD with `gpart`. The `PART` class creates a geom whose single consumer is 附加ed to `ada0`, and which publishes multiple providers, one per 分区: `ada0p1`, `ada0p2`, `ada0p3`, and so on.
|
||
|
||
You encrypt `ada0p2` with `geli`. The `ELI` class creates a geom whose single consumer is 附加ed to `ada0p2`, and which publishes a single provider called `ada0p2.eli`.
|
||
|
||
You 挂载 UFS on `ada0p2.eli`. UFS opens that provider, reads its super块, and begins serving files.
|
||
|
||
When a process reads a file, the request travels from UFS, to `ada0p2.eli`, through the `geli` geom which decrypts the relevant 块, to `ada0p2`, through the `PART` geom which offsets the 块 addresses, to `ada0`, where the `ada` 驱动程序 talks to the SATA controller.
|
||
|
||
At no point does UFS know that its underlying storage is encrypted, 分区ed, or even a physical disk. It just sees a provider. The layers below it can be as simple or as elaborate as the administrator chooses.
|
||
|
||
That composition is the reason GEOM exists. A single 存储驱动程序 only needs to know how to be a reliable bottom-of-stack producer of I/O. Everything above it is reusable.
|
||
|
||
### Providers and Consumers in Code
|
||
|
||
In the 内核, a provider is a `struct g_provider` and a consumer is a `struct g_consumer`. Both are defined in `/usr/src/sys/geom/geom.h`. As a disk 驱动程序 author, you almost never allocate either directly. `g_disk` allocates a provider on your behalf when you call `disk_create`, and you never need a consumer, because a disk 驱动程序 does not 附加 to anything underneath.
|
||
|
||
What you do need is a mental model of what they mean.
|
||
|
||
A provider is a named, seekable, 块-addressable surface that something can read and write. It has a size, a 扇区 size, a name, and some access counters. GEOM publishes providers in `/dev` via its character-设备 integration, so the administrator can refer to them by name.
|
||
|
||
A consumer is a channel from one geom into another geom's provider. The consumer is where the upper geom issues I/O requests, and it is where the upper geom 寄存器 access rights. When you 挂载 UFS on `ada0p2.eli`, the 挂载 operation causes a consumer to be 附加ed inside UFS's GEOM hook, and that consumer acquires access rights on the `ada0p2.eli` provider.
|
||
|
||
### Access Rights
|
||
|
||
Providers have three access counters: read (`r`), write (`w`), and exclusive (`e`). They are visible in `gstat` and `geom disk list` as `r0w0e0` or similar. Each number is incremented when a consumer asks for that kind of access and decremented when the consumer releases it.
|
||
|
||
An exclusive access is what `挂载`, `newfs`, and similar administrative tools acquire when they need to be sure no other process is writing the 设备. An exclusive count of zero means no exclusive access is held. An exclusive count greater than zero means the provider is 总线y.
|
||
|
||
The access counts are not trivia. They are a real synchronisation tool. When you call `disk_destroy` to remove a disk, the 内核 will refuse to destroy the provider if it still has open users, because destroying it under the feet of a 挂载ed 文件系统 would be catastrophic. This is the same mechanism that makes `kldunload` 块 if the module is in use, but it operates at the GEOM layer, one level higher than the module subsystem.
|
||
|
||
You can watch the access counters change in real time.
|
||
|
||
```console
|
||
# geom disk list myblk0 | grep Mode
|
||
Mode: r0w0e0
|
||
# dd if=/dev/myblk0 of=/dev/null bs=512 count=1 &
|
||
# geom disk list myblk0 | grep Mode
|
||
Mode: r1w0e0
|
||
```
|
||
|
||
When `dd` finishes, the mode returns to `r0w0e0`.
|
||
|
||
### The BIO Object and Its Life Cycle
|
||
|
||
The unit of work in GEOM is the BIO, defined as `struct bio` in `/usr/src/sys/sys/bio.h`. A BIO represents one I/O request. It has a command (`bio_cmd`), an offset (`bio_offset`), a length (`bio_length`), a data pointer (`bio_data`), a byte count (`bio_bcount`), a residual (`bio_resid`), an error (`bio_error`), flags (`bio_flags`), and a number of other fields that we will meet as we need them.
|
||
|
||
The `bio_cmd` values tell the 驱动程序 what kind of I/O is being requested. The most common values are `BIO_READ`, `BIO_WRITE`, `BIO_DELETE`, `BIO_GETATTR`, and `BIO_FLUSH`. `BIO_READ` and `BIO_WRITE` are what you expect. `BIO_DELETE` asks the 驱动程序 to release the 块 in the range, the way `TRIM` does on SSDs or `mdconfig -d` does on a memory disk. `BIO_GETATTR` queries an attribute by name and is how GEOM layers discover 分区 types, media labels, and other metadata. `BIO_FLUSH` asks the 驱动程序 to commit outstanding writes to stable storage.
|
||
|
||
A BIO travels downward from one geom to the next via `g_io_request`. When it reaches the bottom of the stack, the 驱动程序's strategy function is called. When the 驱动程序 is done, it completes the BIO by calling `biodone` or, at the GEOM class level, `g_io_deliver`. The completion call releases the BIO back up the stack.
|
||
|
||
`g_disk` 驱动程序 get a slightly simpler view because the `g_disk` infrastructure translates GEOM-level BIO handling into `biodone`-style completion. When you implement `d_strategy`, you receive a `struct bio` and you must eventually call `biodone(bp)` to complete it. You do not call `g_io_deliver` directly. The 框架 does.
|
||
|
||
### The GEOM Topology Lock
|
||
|
||
GEOM has a global lock called the topology lock. It protects modifications to the tree of geoms, providers, and consumers. When a provider is created or destroyed, when a consumer is 附加ed or 分离ed, when access counts change, or when GEOM walks the tree to route a request, the topology lock is taken.
|
||
|
||
The topology lock is held across operations that can take time, which is unusual for 内核 locks, so GEOM operates much of its real work asynchronously through a dedicated thread called the event queue. When you look at `g_class` definitions in the source tree, the `init`, `fini`, `access`, and similar methods are invoked in the context of the GEOM event thread, not in the context of the user process that triggered the operation.
|
||
|
||
For a 驱动程序 using `g_disk`, this matters in one specific way. You should not hold your own 驱动程序 lock across a call into GEOM-level functions, because GEOM may acquire the topology lock inside those functions, and nested locking in the wrong order leads to deadlock. `g_disk` is written carefully enough that you do not usually have to think about this as long as you follow the patterns we show. But the fact is worth knowing.
|
||
|
||
### The GEOM Event Queue
|
||
|
||
GEOM processes many events on a single dedicated 内核 thread called `g_event`. If you have the 内核 running with debugging enabled, you can see it in `procstat -kk`. This thread picks up events placed on its queue and processes them one at a time. Typical events include creating a geom, destroying a geom, 附加ing a consumer, 分离ing a consumer, and retasting a provider.
|
||
|
||
A practical consequence is that some actions you take from your 驱动程序, such as `disk_destroy`, do not happen synchronously in the context of the calling thread. They get queued for the event thread, and the actual destruction happens a moment later. `disk_destroy` handles the waiting correctly so that by the time it returns, the disk is gone. But if you are chasing a subtle ordering bug, remembering that GEOM has its own thread can help.
|
||
|
||
### How g_disk Wraps All of This
|
||
|
||
With that vocabulary in hand, we can now describe what `g_disk` does for us more precisely.
|
||
|
||
When we call `disk_alloc`, we receive a `struct disk` that is pre-initialised enough to be filled in. We set the name, unit, 回调, and geometry, then call `disk_create`.
|
||
|
||
`disk_create` does the following for us, through the event queue:
|
||
|
||
1. creates a GEOM class if one does not already exist for this disk name,
|
||
2. creates a geom under that class,
|
||
3. creates a provider associated with the geom,
|
||
4. sets up devstat accounting so that `iostat` and `gstat` have data,
|
||
5. wires up GEOM's character-设备 接口 so that `/dev/<name><unit>` appears,
|
||
6. arranges for BIO requests to flow into our `d_strategy` 回调.
|
||
|
||
It also sets up a few optional behaviours. If we provide a `d_ioctl`, the 内核 routes user-space `ioctl` calls on the `/dev` node through to our function. If we provide a `d_getattr`, GEOM routes `BIO_GETATTR` requests through to it. If we provide a `d_gone`, the 内核 calls it if something outside our 驱动程序 decides the disk is gone, such as a hotplug removal event.
|
||
|
||
On the teardown side, `disk_destroy` queues the removal, waits for all pending I/O to drain, releases the provider, destroys the geom, and frees the `struct disk`. We do not call `free` on the disk ourselves. The 框架 does that.
|
||
|
||
### Where to Read Source
|
||
|
||
You now have enough vocabulary to benefit from reading the `g_disk` source directly. Open `/usr/src/sys/geom/geom_disk.c` and look for the following.
|
||
|
||
The function `disk_alloc` is early in the file. It is a simple allocator that returns a zeroed `struct disk`. Nothing dramatic.
|
||
|
||
The function `disk_create` is longer. Skim it and notice the event-based approach: most of the real work is queued rather than performed inline. Also notice the sanity checks on the disk's fields, which catch 驱动程序 that forget to set the 扇区 size, the media size, or the strategy function.
|
||
|
||
The function `disk_destroy` is similarly event-queued. It guards the teardown with an access-count check, because destroying a disk that is still open would be a bug.
|
||
|
||
The function `g_disk_start` is the inner strategy function. It validates a BIO, updates devstat, and calls the 驱动程序's `d_strategy`.
|
||
|
||
Take a moment to look at the code. You do not need to understand every branch. You do need to recognise the overall shape: events for structural changes, inline work for I/O. That is the shape of most GEOM-based code.
|
||
|
||
### Comparing md(4) and g_zero
|
||
|
||
Two real 驱动程序 make good reading as counterpoints to `g_disk`. The first is the `md(4)` 驱动程序, in `/usr/src/sys/dev/md/md.c`. This is a memory-disk 驱动程序 that uses both `g_disk` and directly managed GEOM structures. It is the most thorough example of a 存储驱动程序 in the tree, supporting multiple backing-store types, resizing, dumping, and many other features. It is a large file, but it is the closest relative of what we are building.
|
||
|
||
The second is `g_zero`, in `/usr/src/sys/geom/zero/g_zero.c`. This is a minimal GEOM class that reads always return zeroed memory and writes are discarded. It is roughly 145 lines and uses the lower-level `DECLARE_GEOM_CLASS` API directly rather than `g_disk`. It is a great counterpoint because it shows the GEOM class mechanics without any of the disk-specific adornment. When you want to understand what `g_disk` hides, read `g_zero`.
|
||
|
||
### Why Our Driver Uses g_disk
|
||
|
||
You might ask whether we should build our 驱动程序 directly on the lower-level `g_class` API, the way `g_zero` does, to expose more of the machinery. We will not, for three reasons.
|
||
|
||
First, `g_disk` is the idiomatic choice for anything that looks like a disk, which our pseudo 块设备 does. Reviewers of real FreeBSD 驱动程序 patches would push back on a 驱动程序 that used `g_class` directly when `g_disk` would do.
|
||
|
||
Second, `g_disk` gives us devstat integration, standard ioctls, and `/dev` node management for free. Re-implementing those by hand would be a substantial distraction from the teaching goal of this chapter.
|
||
|
||
Third, the simpler the first working 驱动程序, the easier it is to reason about. We have plenty of code to write in the next few sections. We do not need to spend pages on class-level GEOM plumbing that `g_disk` already gets right.
|
||
|
||
That said, if you are curious, you should absolutely read `g_zero.c`. It is a small file and it reveals the mechanics that `g_disk` abstracts. The 总结 for this section will point you to it one last time.
|
||
|
||
### A Walk Through g_class
|
||
|
||
For readers who want a little more of the underlying machinery, let us walk through what a `g_class` structure looks like in code, without yet building one of our own.
|
||
|
||
The following is reproduced (slightly simplified) from `/usr/src/sys/geom/zero/g_zero.c`.
|
||
|
||
```c
|
||
static struct g_class g_zero_class = {
|
||
.name = G_ZERO_CLASS_NAME,
|
||
.version = G_VERSION,
|
||
.start = g_zero_start,
|
||
.init = g_zero_init,
|
||
.fini = g_zero_fini,
|
||
.destroy_geom = g_zero_destroy_geom
|
||
};
|
||
|
||
DECLARE_GEOM_CLASS(g_zero_class, g_zero);
|
||
```
|
||
|
||
`.name` is the class name, used in `geom -t` output. `.version` must match `G_VERSION` for the running 内核; mismatched versions are rejected at load time. `.start` is the function called when a BIO arrives at a provider of this class. `.init` is called when the class is first instantiated, typically to create the initial geom and its provider. `.fini` is the teardown counterpart to `.init`. `.destroy_geom` is called when a specific geom under this class is being removed.
|
||
|
||
`DECLARE_GEOM_CLASS` is a macro that expands to a module declaration that loads this class into the 内核 when the module is loaded. It hides the `moduledata_t`, the `SYSINIT`, and the `g_modevent` wiring behind a single line.
|
||
|
||
Our 驱动程序 does not use `g_class` directly. `g_disk` does it for us, and the class it declares under the hood is the universal `DISK` class that all disk-shaped 驱动程序 share. But understanding the structure is useful because, if you ever write a transformation class (a GEOM-level encrypt, compress, or 分区 layer), you will define your own `g_class`.
|
||
|
||
### The Life of a BIO, In Detail
|
||
|
||
We covered the BIO life cycle briefly earlier. Here it is in more detail, because every storage-驱动程序 bug touches this life cycle at some point.
|
||
|
||
A BIO originates somewhere above the 驱动程序. For our 驱动程序, the most common origins are:
|
||
|
||
1. **A 文件系统's 缓冲区-cache write-back**. UFS calls `bwrite` or `bawrite` on a 缓冲区, which builds a BIO and hands it to GEOM through `g_io_request`.
|
||
2. **A 文件系统's 缓冲区-cache read**. UFS calls `bread`, which checks the cache and, on a miss, issues a BIO.
|
||
3. **A raw access through `/dev/myblk0`**. A program calls `read(2)` or `write(2)` on the node. `devfs` and GEOM's character-设备 integration build a BIO and issue it.
|
||
4. **A tool-issued operation**. `newfs_ufs`, `diskinfo`, `dd`, and similar tools issue BIOs the same way as a raw access.
|
||
|
||
Once built, the BIO is routed through GEOM's topology. Each consumer -> provider hop along the way may transform or validate the BIO. For a simple stack (our 驱动程序 with no intermediate geoms), there are no intermediate hops; the BIO arrives at our provider and is dispatched to our strategy function.
|
||
|
||
Inside `g_disk`, the strategy function is preceded by three small pieces of bookkeeping:
|
||
|
||
1. Some sanity checks (for instance, verifying that the BIO's offset and length are within the media).
|
||
2. A call to `devstat_start_transaction_bio` to start timing the request.
|
||
3. A call to the 驱动程序's `d_strategy`.
|
||
|
||
On completion, `g_disk` intercepts the `biodone` call, records the end time with `devstat_end_transaction_bio`, and forwards the completion up the stack.
|
||
|
||
From the 驱动程序's point of view, the only thing that matters is that `d_strategy` gets called, and that `biodone` is called once per BIO. Everything else is plumbing.
|
||
|
||
### Error Propagation
|
||
|
||
When a BIO fails, the 驱动程序 sets `bio_error` to an `errno` value and sets the `BIO_ERROR` flag in `bio_flags`. `biodone` is then called as normal.
|
||
|
||
Above the 驱动程序, GEOM's completion code checks for the error. If set, the error is propagated up the stack. The 文件系统 sees the error and decides what to do; typically, a read error on metadata is fatal and the 文件系统 reports EIO to user space. A write error is often delayed; the 文件系统 may retry, or may mark the associated 缓冲区 as needing attention on the next sync.
|
||
|
||
Common `errno` values in the BIO path:
|
||
|
||
- `EIO`: a generic I/O error. The 内核 assumes the 设备 is having trouble.
|
||
- `ENXIO`: the 设备 is not configured or has gone away.
|
||
- `EOPNOTSUPP`: the 驱动程序 does not support this operation.
|
||
- `EROFS`: the medium is read-only.
|
||
- `ENOSPC`: no space available.
|
||
- `EFAULT`: an address in the request is invalid. Very rare in the BIO path.
|
||
|
||
For our in-memory 驱动程序, the only errors that should ever appear are the bounds-check error (`EIO`) and the unknown-command error (`EOPNOTSUPP`).
|
||
|
||
### What g_disk Does That You Do Not See
|
||
|
||
We have mentioned that `g_disk` takes care of several things on our behalf. Here is a fuller list.
|
||
|
||
- It creates the GEOM class for the `DISK` type if it does not already exist, and it shares this class among all disk 驱动程序.
|
||
- It creates a geom under that class when we call `disk_create`.
|
||
- It creates a provider on the geom and publishes it in `/dev`.
|
||
- It wires up devstat accounting automatically.
|
||
- It handles the GEOM access protocol, converting user-space `open` and `close` calls on `/dev/myblk0` into provider access-count changes.
|
||
- It handles the GEOM character-设备 接口, converting read and write on `/dev/myblk0` into BIOs to our strategy function.
|
||
- It handles the BIO_GETATTR default cases (most attributes have sensible defaults).
|
||
- It handles withering on `disk_destroy`, waiting for in-flight BIOs.
|
||
- It forwards `d_ioctl` calls for ioctls it does not handle itself.
|
||
|
||
Each of these is a piece of code you would have to write if you built directly on `g_class`. Reading `/usr/src/sys/geom/geom_disk.c` is a good way to appreciate just how much `g_disk` does for us.
|
||
|
||
### Inspecting Our Provider
|
||
|
||
Let us take our skeleton 驱动程序 from Section 3, load it, and inspect it through GEOM's eyes.
|
||
|
||
```console
|
||
# kldload ./myblk.ko
|
||
# geom disk list myblk0
|
||
Geom name: myblk0
|
||
Providers:
|
||
1. Name: myblk0
|
||
Mediasize: 1048576 (1.0M)
|
||
Sectorsize: 512
|
||
Mode: r0w0e0
|
||
descr: (null)
|
||
ident: (null)
|
||
rotationrate: unknown
|
||
fwsectors: 0
|
||
fwheads: 0
|
||
```
|
||
|
||
`geom disk list` shows us only the `DISK` class's geoms. Each of those geoms has one provider. We can also see the full class tree.
|
||
|
||
```console
|
||
# geom -t | head -n 40
|
||
Geom Class Provider
|
||
ada0 DISK ada0
|
||
ada0p1 PART ada0p1
|
||
ada0p2 PART ada0p2
|
||
ada0p3 PART ada0p3
|
||
myblk0 DISK myblk0
|
||
```
|
||
|
||
Our geom is a sibling of the real disks, without any upper-layer class 附加ed to it yet. In later sections we will see what happens when a 文件系统 附加.
|
||
|
||
```console
|
||
# geom stats myblk0
|
||
```
|
||
|
||
`geom stats` returns detailed performance counters. On an idle, unused 设备 like ours, all the counters are zero.
|
||
|
||
```console
|
||
# gstat -I 1
|
||
dT: 1.002s w: 1.000s
|
||
L(q) ops/s r/s kBps ms/r w/s kBps ms/w %busy Name
|
||
0 0 0 0 0.0 0 0 0.0 0.0| ada0
|
||
0 0 0 0 0.0 0 0 0.0 0.0| myblk0
|
||
```
|
||
|
||
`gstat` is a more compact view that updates live. We will use this heavily in later sections.
|
||
|
||
### 总结 Section 4
|
||
|
||
GEOM is a composable 块-layer 框架 made of classes, geoms, providers, and consumers. Requests flow through it as `struct bio` objects, with `BIO_READ`, `BIO_WRITE`, and a handful of other commands. Access rights, topology locking, and event-driven structure management are the mechanisms that keep the 框架 safe to evolve under load. `g_disk` wraps all of this for disk-shaped 驱动程序 and gives them a friendlier 接口 with little loss of expressiveness.
|
||
|
||
Our skeleton 驱动程序 is now a first-class GEOM participant, even though it cannot yet do any real I/O. In the next section, we will give it that missing piece. We will allocate a backing 缓冲区, implement a strategy function that actually reads and writes, and watch the 内核's storage stack exercise our code from both raw-access and 文件系统-access directions.
|
||
|
||
## 第5节: Implementing Basic Read and Write
|
||
|
||
In Section 3 we returned `ENXIO` for every BIO. In Section 4 we learned enough about GEOM to know exactly what kind of request our strategy function receives and what its obligations are. In this section we will replace that placeholder with a working handler that reads and writes real bytes against an in-memory backing store. By the end, our 驱动程序 will serve traffic through `dd`, will return sane data, and will survive being formatted by `newfs_ufs`.
|
||
|
||
### The Backing Store
|
||
|
||
Our backing store for now is simply an array of bytes in 内核 memory, sized to match `d_mediasize`. It is the simplest possible representation of a disk: a flat 缓冲区. Real 存储驱动程序 replace this with hardware DMA, with a vnode-backed file, or with a swap-backed VM object, but a flat 缓冲区 is enough to teach every other concept in this chapter without distraction.
|
||
|
||
For 1 MiB we can simply `malloc` the 缓冲区. For larger sizes we would need a different allocator, because the 内核 heap does not scale gracefully to contiguous allocations of tens or hundreds of megabytes. `md(4)` avoids the issue for large memory disks by using page-at-a-time allocation and a custom indirection structure. We do not need that level of sophistication yet, but we will note the limitation in the code.
|
||
|
||
Let us update `myblk_softc` to include the backing store.
|
||
|
||
```c
|
||
struct myblk_softc {
|
||
struct disk *disk;
|
||
struct mtx lock;
|
||
u_int unit;
|
||
uint8_t *backing;
|
||
size_t backing_size;
|
||
};
|
||
```
|
||
|
||
Two new fields: `backing` is the pointer to the 内核 memory we allocated, and `backing_size` is the number of bytes we allocated. These should always be equal to `d_mediasize`, but storing the size explicitly is cleaner than relying on indirection through `disk->d_mediasize`.
|
||
|
||
Now, in `myblk_附加_unit`, allocate the backing 缓冲区.
|
||
|
||
```c
|
||
static int
|
||
myblk_attach_unit(struct myblk_softc *sc)
|
||
{
|
||
|
||
sc->backing_size = MYBLK_MEDIASIZE;
|
||
sc->backing = malloc(sc->backing_size, M_MYBLK, M_WAITOK | M_ZERO);
|
||
|
||
sc->disk = disk_alloc();
|
||
sc->disk->d_name = MYBLK_NAME;
|
||
sc->disk->d_unit = sc->unit;
|
||
sc->disk->d_strategy = myblk_strategy;
|
||
sc->disk->d_sectorsize = MYBLK_SECTOR;
|
||
sc->disk->d_mediasize = MYBLK_MEDIASIZE;
|
||
sc->disk->d_maxsize = MAXPHYS;
|
||
sc->disk->d_drv1 = sc;
|
||
|
||
disk_create(sc->disk, DISK_VERSION);
|
||
return (0);
|
||
}
|
||
```
|
||
|
||
`malloc` with `M_WAITOK | M_ZERO` returns a zeroed 缓冲区 or sleeps until one is available. It cannot fail for small allocations on a healthy system, which is why we do not check the return value here. If we were allocating a very large 缓冲区 we might want `M_NOWAIT` and explicit error handling, but for 1 MiB `M_WAITOK` is the idiomatic choice.
|
||
|
||
`myblk_分离_unit` must free the backing store after destroying the disk.
|
||
|
||
```c
|
||
static void
|
||
myblk_detach_unit(struct myblk_softc *sc)
|
||
{
|
||
|
||
if (sc->disk != NULL) {
|
||
disk_destroy(sc->disk);
|
||
sc->disk = NULL;
|
||
}
|
||
if (sc->backing != NULL) {
|
||
free(sc->backing, M_MYBLK);
|
||
sc->backing = NULL;
|
||
sc->backing_size = 0;
|
||
}
|
||
}
|
||
```
|
||
|
||
Order matters here. We destroy the disk first, which ensures there are no more BIOs in flight. Only then do we free the backing 缓冲区. If we freed the 缓冲区 first, an in-flight BIO might try to `memcpy` into or out of a pointer that no longer refers to our memory, and the 内核 would crash on the next I/O.
|
||
|
||
### The Strategy Function
|
||
|
||
Now for the heart of the change. Replace the placeholder `myblk_strategy` with a function that actually services BIOs.
|
||
|
||
```c
|
||
static void
|
||
myblk_strategy(struct bio *bp)
|
||
{
|
||
struct myblk_softc *sc;
|
||
off_t offset;
|
||
size_t len;
|
||
|
||
sc = bp->bio_disk->d_drv1;
|
||
offset = bp->bio_offset;
|
||
len = bp->bio_bcount;
|
||
|
||
if (offset < 0 ||
|
||
offset > sc->backing_size ||
|
||
len > sc->backing_size - offset) {
|
||
bp->bio_error = EIO;
|
||
bp->bio_flags |= BIO_ERROR;
|
||
bp->bio_resid = len;
|
||
biodone(bp);
|
||
return;
|
||
}
|
||
|
||
switch (bp->bio_cmd) {
|
||
case BIO_READ:
|
||
mtx_lock(&sc->lock);
|
||
memcpy(bp->bio_data, sc->backing + offset, len);
|
||
mtx_unlock(&sc->lock);
|
||
bp->bio_resid = 0;
|
||
break;
|
||
|
||
case BIO_WRITE:
|
||
mtx_lock(&sc->lock);
|
||
memcpy(sc->backing + offset, bp->bio_data, len);
|
||
mtx_unlock(&sc->lock);
|
||
bp->bio_resid = 0;
|
||
break;
|
||
|
||
case BIO_DELETE:
|
||
mtx_lock(&sc->lock);
|
||
memset(sc->backing + offset, 0, len);
|
||
mtx_unlock(&sc->lock);
|
||
bp->bio_resid = 0;
|
||
break;
|
||
|
||
case BIO_FLUSH:
|
||
/*
|
||
* In-memory backing store is always "flushed".
|
||
* Nothing to do.
|
||
*/
|
||
bp->bio_resid = 0;
|
||
break;
|
||
|
||
default:
|
||
bp->bio_error = EOPNOTSUPP;
|
||
bp->bio_flags |= BIO_ERROR;
|
||
bp->bio_resid = len;
|
||
break;
|
||
}
|
||
|
||
biodone(bp);
|
||
}
|
||
```
|
||
|
||
Let us read this carefully. It is not a long function, but every line is doing something that matters.
|
||
|
||
The first line finds our softc. GEOM gives us the BIO with a pointer to the disk in `bp->bio_disk`. We stashed our softc in `d_drv1` during `disk_create`, so we retrieve it from there. This is the 块-驱动程序 equivalent of `设备_get_softc(dev)` in the New总线 world.
|
||
|
||
The second pair of lines extract the offset and length of the request. `bio_offset` is a byte offset into the media. `bio_bcount` is the number of bytes to transfer. GEOM has already translated file-level operations through whatever layers sit above us into a linear byte range.
|
||
|
||
The bounds check that follows is defensive programming. GEOM will not normally send us a request that exceeds the media size, because it splits and validates BIOs on our behalf. But defensive 驱动程序 check anyway, because a silently accepted out-of-bounds write can smash 内核 memory, and because the cost of the check is a few instructions per request. We also guard against arithmetic overflow by rewriting the obvious `offset + len > backing_size` check as `len > backing_size - offset`, which cannot overflow because `offset <= backing_size` at this point.
|
||
|
||
The switch is where the real work happens. Each BIO command gets its own case.
|
||
|
||
`BIO_READ` copies `len` bytes from our backing store at `offset` into `bp->bio_data`. GEOM has allocated `bp->bio_data` for us, and it will be released when the BIO completes. Our job is just to fill it.
|
||
|
||
`BIO_WRITE` copies `len` bytes from `bp->bio_data` into our backing store at `offset`. Symmetrical to the read case.
|
||
|
||
`BIO_DELETE` zeroes the range. For a real disk, `BIO_DELETE` is how 文件系统 signal that a range of 块 is no longer in use, and the disk is free to reclaim it. SSDs use it to drive TRIM. For our in-memory 驱动程序, there is nothing to reclaim, but zeroing the range is a reasonable response because it reflects the "data is gone" semantics.
|
||
|
||
`BIO_FLUSH` is a request to commit outstanding writes to stable storage. Our storage is never volatile in the sense that a FLUSH would help: every `memcpy` is already visible to the next `memcpy` in the same order it was issued. We return success with nothing to do.
|
||
|
||
Any other command we do not recognise gets `EOPNOTSUPP`. GEOM layers above us will see this and react accordingly.
|
||
|
||
At the end, `biodone(bp)` completes the BIO. This is not optional. Every BIO that enters the strategy function must leave through `biodone` exactly once, or the BIO will be leaked, the caller will 块 forever, and you will have a difficult time diagnosing the issue.
|
||
|
||
### The Role of bio_resid
|
||
|
||
Notice the handling of `bp->bio_resid`. This field represents the number of bytes remaining to transfer after the 驱动程序 is done. When the full transfer succeeds, `bio_resid` is zero. When the transfer fails completely, `bio_resid` equals `bio_bcount`. When the transfer partially succeeds, `bio_resid` is the number of bytes that did not make it.
|
||
|
||
Our 驱动程序 either transfers everything or nothing, so we set `bio_resid` to either `0` (success) or `len` (error). A real hardware 驱动程序 might set it to an intermediate value if a transfer stopped partway through. Filesystems and user-space tools use `bio_resid` to figure out how much data actually moved.
|
||
|
||
### The Lock
|
||
|
||
We take `sc->lock` around the `memcpy`. For an in-memory 驱动程序 that services one request at a time, the lock is not doing much visible work: the 内核's BIO scheduling makes truly concurrent requests unlikely on our toy 设备. But the lock is good hygiene. GEOM does not promise that your strategy function will be invoked serially, and even if it did, a future change to the 驱动程序 to add an asynchronous worker thread would require the lock anyway. Adding it now is cheaper than adding it later.
|
||
|
||
A more sophisticated 驱动程序 might use a fine-grained lock, or might use an MPSAFE approach that relies on atomic operations. For now, a coarse 互斥锁 around the `memcpy` is fine. It is correct, it is easy to reason about, and it does not hurt performance on a pseudo 设备.
|
||
|
||
### Rebuilding and Reloading
|
||
|
||
After updating the source and `kldunload`-ing the old version, rebuild and reload.
|
||
|
||
```console
|
||
# make
|
||
cc -O2 -pipe -fno-strict-aliasing ...
|
||
# kldunload myblk
|
||
# kldload ./myblk.ko
|
||
# dmesg | tail -n 1
|
||
myblk: loaded, /dev/myblk0 size=1048576 bytes
|
||
```
|
||
|
||
Now let us try some real I/O.
|
||
|
||
```console
|
||
# dd if=/dev/zero of=/dev/myblk0 bs=4096 count=16
|
||
16+0 records in
|
||
16+0 records out
|
||
65536 bytes transferred in 0.001104 secs (59 MB/sec)
|
||
# dd if=/dev/myblk0 of=/dev/null bs=4096 count=16
|
||
16+0 records in
|
||
16+0 records out
|
||
65536 bytes transferred in 0.000512 secs (128 MB/sec)
|
||
```
|
||
|
||
We wrote 64 KiB of zeros and read them back. The speeds you see will depend on your hardware and on how much the 缓冲区 cache helps, but any speed above a few MB/sec is fine for a first run.
|
||
|
||
```console
|
||
# dd if=/dev/random of=/dev/myblk0 bs=4096 count=16
|
||
16+0 records in
|
||
16+0 records out
|
||
65536 bytes transferred in 0.001233 secs (53 MB/sec)
|
||
# dd if=/dev/myblk0 of=pattern.bin bs=4096 count=16
|
||
16+0 records in
|
||
16+0 records out
|
||
# dd if=/dev/myblk0 of=pattern2.bin bs=4096 count=16
|
||
16+0 records in
|
||
16+0 records out
|
||
# cmp pattern.bin pattern2.bin
|
||
#
|
||
```
|
||
|
||
We wrote random data, read it back twice, and confirmed that both reads return the same content. Our 驱动程序 is now a coherent store.
|
||
|
||
### A Quick Look Under Load
|
||
|
||
Let us run a short stress test and watch `gstat`.
|
||
|
||
In one terminal:
|
||
|
||
```console
|
||
# while true; do dd if=/dev/urandom of=/dev/myblk0 bs=4096 \
|
||
count=256 2>/dev/null; done
|
||
```
|
||
|
||
In another terminal:
|
||
|
||
```console
|
||
# gstat -I 1 -f myblk0
|
||
dT: 1.002s w: 1.000s
|
||
L(q) ops/s r/s kBps ms/r w/s kBps ms/w %busy Name
|
||
0 251 0 0 0.0 251 1004 0.0 2.0| myblk0
|
||
```
|
||
|
||
About 250 write operations per second at 4 KiB each, approximately 1 MB/sec. The latency is very low because the backing store is RAM. For a real disk the numbers would be very different, but the structure of what you are watching is the same.
|
||
|
||
Stop the stress test with `Ctrl-C` on the first terminal.
|
||
|
||
### Refining the Driver with ioctl Support
|
||
|
||
Many storage tools send an ioctl to the 设备 to query geometry or to issue commands. GEOM handles the common ones for us, but if we provide a `d_ioctl` 回调, the 内核 will route unknown ioctls through to our function. For now we do not implement any custom ioctl. We only note that the hook exists.
|
||
|
||
```c
|
||
static int
|
||
myblk_ioctl(struct disk *d, u_long cmd, void *data, int flag,
|
||
struct thread *td)
|
||
{
|
||
|
||
(void)d; (void)data; (void)flag; (void)td;
|
||
|
||
switch (cmd) {
|
||
/* No custom ioctls yet. */
|
||
default:
|
||
return (ENOIOCTL);
|
||
}
|
||
}
|
||
```
|
||
|
||
We 注册 the 回调 by assigning `sc->disk->d_ioctl = myblk_ioctl;` before calling `disk_create`. Returning `ENOIOCTL` from the default case tells GEOM that we do not handle the command and gives it the chance to pass the request to its own default handler.
|
||
|
||
### Refining the Driver with getattr Support
|
||
|
||
GEOM uses `BIO_GETATTR` to ask storage 设备 for named attributes. A 文件系统 might ask for `GEOM::rotation_rate` to know whether it is on spinning media. The 分区ing layer might ask for `GEOM::ident` to get a stable identifier. A `d_getattr` 回调 is the hook that lets us respond.
|
||
|
||
```c
|
||
static int
|
||
myblk_getattr(struct bio *bp)
|
||
{
|
||
struct myblk_softc *sc;
|
||
|
||
sc = bp->bio_disk->d_drv1;
|
||
|
||
if (strcmp(bp->bio_attribute, "GEOM::ident") == 0) {
|
||
if (bp->bio_length < sizeof("MYBLK0"))
|
||
return (EFAULT);
|
||
strlcpy(bp->bio_data, "MYBLK0", bp->bio_length);
|
||
bp->bio_completed = strlen("MYBLK0") + 1;
|
||
return (0);
|
||
}
|
||
|
||
/* Let g_disk fall back to default behaviour. */
|
||
(void)sc;
|
||
return (-1);
|
||
}
|
||
```
|
||
|
||
The return-value convention for `d_getattr` is worth pausing on, because it trips up many first-time readers. Returning `0` with `bio_completed` set tells `g_disk` that we handled the attribute successfully. Returning a positive errno value (such as `EFAULT` for a too-small 缓冲区) tells `g_disk` that we handled the attribute but the operation failed. Returning `-1` tells `g_disk` that we did not recognise the attribute and it should try its built-in default handler. That is why we return `-1` at the bottom: we want `g_disk` to answer standard attributes such as `GEOM::fw扇区` on our behalf. For our 驱动程序, responding to `GEOM::ident` with a short string is enough to show up in `diskinfo -v`. Register this with `sc->disk->d_getattr = myblk_getattr;` before `disk_create`.
|
||
|
||
### Partial Writes and Short Reads
|
||
|
||
Our 驱动程序 does not actually produce partial writes or short reads, because the backing store is in RAM and every transfer either fully succeeds or fully fails. But for a real hardware 驱动程序, partial transfers are normal: a disk may return a few 扇区 successfully and then fail on a bad 扇区. The BIO 框架 supports this through `bio_resid`, and a 驱动程序 should set `bio_resid` to the number of bytes that did not complete.
|
||
|
||
The practical guidance is to always set `bio_resid` explicitly before calling `biodone`. If the transfer fully succeeded, set it to zero. If it partially succeeded, set it to the residual. If it fully failed, set it to `bio_bcount`. Forgetting to set `bio_resid` leaves whatever garbage was in the field when the BIO was allocated, which can confuse callers.
|
||
|
||
### 常见错误 in Strategy Functions
|
||
|
||
Before we continue, let us name three common mistakes that appear in first-time strategy functions.
|
||
|
||
**Forgetting `biodone`.** Every path out of the strategy function must call `biodone(bp)` on the BIO. If you forget, the BIO is leaked and the caller hangs. This is the single most common source of "my 挂载 hangs" problems.
|
||
|
||
**Holding a lock across `biodone`.** `biodone` may call upward into GEOM or into a 文件系统's completion handler. Those handlers may take other locks, or may need to acquire locks you already hold, leading to lock-order reversal and potential deadlock. The safest pattern is to drop your lock before calling `biodone`. Our simple version does this implicitly: the `mtx_unlock` is always inside the switch, and `biodone` runs after the switch.
|
||
|
||
**Returning from the strategy function with an error code.** `d_strategy` is a `void` function. Errors are reported by setting `bio_error` and the `BIO_ERROR` flag on the BIO, not by returning. Compilers catch this if you declare the function correctly, but beginners sometimes write it as returning `int`, which causes compiler warnings that should not be ignored.
|
||
|
||
### Chained BIOs and BIO Hierarchies
|
||
|
||
A BIO can have a child. GEOM uses this when a transformation class needs to split, combine, or transform a request into one or more downstream requests. For example, a mirror class might take a BIO_WRITE and issue two child BIOs, one to each mirror member. A 分区 class might take a BIO_READ and issue a single child BIO with the offset shifted into the underlying provider's address space.
|
||
|
||
The parent-child relationship is recorded in `bio_parent`. When a child completes, its error is propagated to the parent by `biodone`, which accumulates errors and delivers the parent when all children have completed.
|
||
|
||
Our 驱动程序 does not produce child BIOs. It receives them as leaves of the chain. From the 驱动程序's perspective, every BIO is self-contained: it has an offset, a length, and a data 缓冲区, and our job is to service it.
|
||
|
||
But if you ever find yourself needing to split a BIO inside your 驱动程序 (for example, if a request spans a boundary that your backing store handles in separate chunks), you can use `g_clone_bio` to create a child BIO, `g_io_request` to dispatch it, and `g_std_done` or a custom completion handler to reassemble the parent. The pattern is visible in several places in the 内核, including `g_mirror` and `g_raid`.
|
||
|
||
### The Thread Context of the Strategy Function
|
||
|
||
The strategy function runs in whatever thread submitted the BIO. For 文件系统-originated BIOs, that is typically the 文件系统's syncer thread or a 缓冲区-cache worker. For direct user-space access, it is the user thread that called `read` or `write` on `/dev/myblk0`. For GEOM transformations, it might be the GEOM event thread or a class-specific worker thread.
|
||
|
||
What this means for your 驱动程序 is that `d_strategy` can run in many different thread contexts. You cannot assume that `curthread` belongs to any particular process, and you cannot 块 for a long time or the calling 文件系统 (or user program) will stall.
|
||
|
||
If your strategy function needs to do something slow (I/O against a vnode, waiting for hardware, or complex locking), the right pattern is to enqueue the BIO on an internal queue and have a dedicated worker thread process it. This is what `md(4)` does for all backing types, because vnode I/O (for instance) can 块 arbitrarily long.
|
||
|
||
Our 驱动程序 is entirely in-memory and does `memcpy` only, so we do not need a worker thread. But understanding the pattern is important for the future.
|
||
|
||
### A Worked Example: Reading Across a Boundary
|
||
|
||
Suppose a 文件系统 issues a BIO_READ with offset 100000 and length 8192. That spans bytes 100000 through 108191. Let us trace how our strategy function handles it.
|
||
|
||
1. `bp->bio_cmd` is `BIO_READ`.
|
||
2. `bp->bio_offset` is 100000.
|
||
3. `bp->bio_bcount` is 8192.
|
||
4. `bp->bio_data` points to a 内核 缓冲区 (or a user 缓冲区 mapped into the 内核) where the 8192 bytes should go.
|
||
|
||
Our code computes `offset = 100000` and `len = 8192`. The bounds check passes: `100000 + 8192 = 108192`, which is less than our `backing_size` of 32 MiB (33554432).
|
||
|
||
The switch enters the `BIO_READ` case. We acquire the lock, `memcpy` 8192 bytes from `sc->backing + 100000` into `bp->bio_data`, and release the lock. We set `bp->bio_resid = 0` to indicate a complete transfer. We fall through to `biodone(bp)`, which completes the BIO.
|
||
|
||
The 文件系统 receives the completion, notices the error is zero, and uses the 8192 bytes. The read is complete.
|
||
|
||
Now suppose, instead, the offset was 33554431 and the length was 2 bytes. That is one byte inside the backing store and one byte past the end.
|
||
|
||
1. `offset = 33554431`.
|
||
2. `len = 2`.
|
||
|
||
The bounds check: `offset > sc->backing_size` evaluates `33554431 > 33554432`, which is false. `len > sc->backing_size - offset` evaluates `2 > 33554432 - 33554431`, which evaluates to `2 > 1`, which is true. The check fails, and we fall into the error path: set `bio_error = EIO`, set the `BIO_ERROR` flag, set `bio_resid = 2`, and call `biodone`. The 文件系统 sees the error and handles it.
|
||
|
||
Notice how we used subtraction to avoid the overflow risk. Had we written `offset + len > sc->backing_size`, and had `offset` and `len` both been close to the maximum of `off_t`, the addition could wrap around to a small number and the check would silently pass for a malformed request. Defensive bounds checks always rearrange arithmetic to avoid overflow.
|
||
|
||
### The Devstat Side Effect
|
||
|
||
One pleasant feature of using `g_disk` is that devstat accounting is automatic. Every BIO we service is counted by `iostat` and `gstat`. No extra code is needed.
|
||
|
||
You can verify this with `iostat -x 1` in another terminal while running the stress loop.
|
||
|
||
```text
|
||
extended device statistics
|
||
device r/s w/s kr/s kw/s ms/r ms/w ms/o ms/t qlen %b
|
||
ada0 0 2 0 48 0.0 0.1 0.0 0.1 0 0
|
||
myblk0 0 251 0 1004 0.0 0.0 0.0 0.0 0 2
|
||
```
|
||
|
||
If our 驱动程序 were built on the raw `g_class` API rather than `g_disk`, we would have to wire up devstat ourselves. This is one of the small quality-of-life features that `g_disk` gives us for free.
|
||
|
||
### 总结 Section 5
|
||
|
||
We replaced the placeholder strategy function with a working handler. Our 驱动程序 now services `BIO_READ`, `BIO_WRITE`, `BIO_DELETE`, and `BIO_FLUSH` correctly against an in-memory backing store. It participates in devstat, it cooperates with `gstat`, and it accepts real traffic from `dd`.
|
||
|
||
In the next section, we will cross the boundary from raw 块 access to 文件系统 access. We will format the 设备 with `newfs_ufs`, 挂载 it, create files on it, and observe how the request path changes when a real 文件系统 sits above the provider.
|
||
|
||
## Section 6: Mounting a Filesystem on the Device
|
||
|
||
Up to this point, our 驱动程序 has been exercised through raw access: `dd`, `diskinfo`, and similar tools reading and writing the entire surface as a flat byte range. That is a valuable mode, but it is not the mode most storage 设备 live in. Storage 设备 in real life serve 文件系统. This section takes our 驱动程序 the last mile: we will format it, 挂载 a real 文件系统 on it, create files, and observe how the 内核's 块-layer plumbing routes requests when a 文件系统 is in the picture.
|
||
|
||
This is also the first section where the theoretical distinction between raw access and 文件系统 access becomes concrete. Understanding the difference, and being able to see it in action, is one of the most useful insights a storage-驱动程序 author can acquire.
|
||
|
||
### The Plan
|
||
|
||
We will do the following in this section, in order.
|
||
|
||
1. Increase the media size of our 驱动程序 from 1 MiB to something large enough to hold a usable UFS 文件系统.
|
||
2. Build and load the updated 驱动程序.
|
||
3. Run `newfs_ufs` against the 设备 to create a 文件系统.
|
||
4. Mount the 文件系统 on a scratch directory.
|
||
5. Create some files and verify that the data is read back correctly.
|
||
6. Un挂载 the 文件系统.
|
||
7. Reload the module and watch what happens.
|
||
|
||
By the end, you will have seen a complete 文件系统 on top of your own 块 驱动程序.
|
||
|
||
### Increasing the Media Size
|
||
|
||
UFS has a minimum practical size. You can create tiny UFS 文件系统, but the overhead of the super块, cylinder groups, and inode tables takes a noticeable fraction of the space on anything smaller than a few megabytes. For our purposes, 32 MiB is a comfortable size: it is small enough that the backing store still fits in a plain `malloc`, and large enough that UFS has room to breathe.
|
||
|
||
Update the size definitions at the top of `myfirst_blk.c`.
|
||
|
||
```c
|
||
#define MYBLK_SECTOR 512
|
||
#define MYBLK_MEDIASIZE (32 * 1024 * 1024) /* 32 MiB */
|
||
```
|
||
|
||
Rebuild.
|
||
|
||
```console
|
||
# make clean
|
||
# make
|
||
# kldunload myblk
|
||
# kldload ./myblk.ko
|
||
# diskinfo -v /dev/myblk0
|
||
/dev/myblk0
|
||
512 # sectorsize
|
||
33554432 # mediasize in bytes (32M)
|
||
65536 # mediasize in sectors
|
||
0 # stripesize
|
||
0 # stripeoffset
|
||
```
|
||
|
||
32 MiB is enough.
|
||
|
||
### Formatting with newfs_ufs
|
||
|
||
`newfs_ufs` is the standard UFS formatter on FreeBSD. It lays down the super块, the cylinder groups, the root inode, and all the other structures a UFS 文件系统 requires. Let us run it on our 设备.
|
||
|
||
```console
|
||
# newfs_ufs /dev/myblk0
|
||
/dev/myblk0: 32.0MB (65536 sectors) block size 32768, fragment size 4096
|
||
using 4 cylinder groups of 8.00MB, 256 blks, 1280 inodes.
|
||
super-block backups (for fsck_ffs -b #) at:
|
||
192, 16576, 32960, 49344
|
||
```
|
||
|
||
A few things happened under the hood.
|
||
|
||
`newfs_ufs` opened `/dev/myblk0` for writing, which caused the GEOM access count to tick up. Our strategy function then received a stream of writes: the super块 first, then the cylinder groups, then the empty root directory, then the several backup super块. Each of those writes is a BIO, and each BIO was handled by our 驱动程序.
|
||
|
||
You can verify that `newfs_ufs` really wrote to the 设备 by reading a few bytes back.
|
||
|
||
```console
|
||
# dd if=/dev/myblk0 bs=1 count=16 2>/dev/null | hexdump -C
|
||
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||
```
|
||
|
||
The first few bytes of a UFS 分区 are deliberately zero because the super块 does not sit at offset zero: it is at offset 65536 (块 128) to leave room for boot 块 and other preambles. Let us peek there.
|
||
|
||
```console
|
||
# dd if=/dev/myblk0 bs=512 count=2 skip=128 2>/dev/null | hexdump -C | head
|
||
00010000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
|
||
00010010 80 00 00 00 80 00 00 00 a0 00 00 00 00 00 00 00
|
||
...
|
||
```
|
||
|
||
You should see non-zero bytes now. That is the super块 that `newfs_ufs` laid down on our backing store.
|
||
|
||
### Mounting the Filesystem
|
||
|
||
Create a 挂载 point and 挂载 the 文件系统.
|
||
|
||
```console
|
||
# mkdir -p /mnt/myblk
|
||
# mount /dev/myblk0 /mnt/myblk
|
||
# mount | grep myblk
|
||
/dev/myblk0 on /mnt/myblk (ufs, local)
|
||
# df -h /mnt/myblk
|
||
Filesystem Size Used Avail Capacity Mounted on
|
||
/dev/myblk0 31M 8.0K 28M 0% /mnt/myblk
|
||
```
|
||
|
||
Our pseudo 设备 is now a real 文件系统. Watch the GEOM access counts.
|
||
|
||
```console
|
||
# geom disk list myblk0 | grep Mode
|
||
Mode: r1w1e1
|
||
```
|
||
|
||
`r1w1e1` means one reader, one writer, one exclusive holder. The exclusive hold is UFS: it has told GEOM that it is the sole authority over writes to the 设备 until it is 卸载ed.
|
||
|
||
### Creating and Reading Files
|
||
|
||
Let us actually use the 文件系统.
|
||
|
||
```console
|
||
# echo "hello from myblk" > /mnt/myblk/hello.txt
|
||
# ls -l /mnt/myblk
|
||
total 4
|
||
-rw-r--r-- 1 root wheel 17 Apr 19 18:17 hello.txt
|
||
# cat /mnt/myblk/hello.txt
|
||
hello from myblk
|
||
```
|
||
|
||
Note what just happened. The call `echo "hello from myblk" > /mnt/myblk/hello.txt` traveled through the system call layer to `sys_openat`, then to VFS, then to UFS, which opened the root directory's inode, created a new inode for `hello.txt`, allocated a data 块, copied the 17 bytes into the 缓冲区 cache, and scheduled a write-back. The 缓冲区 cache eventually called down to GEOM, which called down to our strategy function, which copied those bytes into our backing store.
|
||
|
||
When you then ran `cat`, the request traveled down the same stack. Except, because the data was still in the 缓冲区 cache from the recent write, UFS did not actually need to read from our 设备. The 缓冲区 cache served the read from RAM. If you 卸载 and re挂载, you will see an actual read.
|
||
|
||
```console
|
||
# umount /mnt/myblk
|
||
# mount /dev/myblk0 /mnt/myblk
|
||
# cat /mnt/myblk/hello.txt
|
||
hello from myblk
|
||
```
|
||
|
||
That second `cat` probably did cause BIO_READ requests to reach our 驱动程序, because the 卸载-and-re挂载 cycle invalidated the 缓冲区 cache for that 文件系统.
|
||
|
||
### Watching the Traffic
|
||
|
||
`gstat` shows us the BIO traffic in real time. Open another terminal and run `gstat -I 1 -f myblk0`. Then in the first terminal, create a big file.
|
||
|
||
```console
|
||
# dd if=/dev/zero of=/mnt/myblk/big bs=1m count=16
|
||
16+0 records in
|
||
16+0 records out
|
||
16777216 bytes transferred in 0.150 secs (112 MB/sec)
|
||
```
|
||
|
||
In the `gstat` terminal, you should see a burst of writes, perhaps spread across a second or two depending on how quickly the 缓冲区 cache flushes.
|
||
|
||
```text
|
||
L(q) ops/s r/s kBps ms/r w/s kBps ms/w %busy Name
|
||
0 128 0 0 0.0 128 16384 0.0 12.0| myblk0
|
||
```
|
||
|
||
These are the 4 KiB or 32 KiB (depending on UFS's 块 size) writes that UFS is issuing to fill the file. We can verify the file's presence.
|
||
|
||
```console
|
||
# ls -lh /mnt/myblk
|
||
total 16460
|
||
-rw-r--r-- 1 root wheel 16M Apr 19 18:19 big
|
||
-rw-r--r-- 1 root wheel 17B Apr 19 18:17 hello.txt
|
||
# du -ah /mnt/myblk
|
||
16M /mnt/myblk/big
|
||
4.5K /mnt/myblk/hello.txt
|
||
16M /mnt/myblk
|
||
```
|
||
|
||
And we can delete it again to watch the BIO_DELETE traffic.
|
||
|
||
```console
|
||
# rm /mnt/myblk/big
|
||
```
|
||
|
||
UFS by default does not issue `BIO_DELETE` unless the 文件系统 was 挂载ed with the `trim` option, so on a plain 挂载 you will see almost no BIO traffic on delete: UFS just marks the 块 as free in its own metadata. To see `BIO_DELETE`, we would need to 挂载 with `-o trim`, which we will cover briefly in the labs.
|
||
|
||
### Un挂载ing
|
||
|
||
Un挂载 the 文件系统 before unloading the module.
|
||
|
||
```console
|
||
# umount /mnt/myblk
|
||
# geom disk list myblk0 | grep Mode
|
||
Mode: r0w0e0
|
||
```
|
||
|
||
The access count dropped back to zero as soon as UFS released its exclusive hold. Our 驱动程序 is now free to be unloaded or further mucked with.
|
||
|
||
### Attempting to Unload While Mounted
|
||
|
||
What happens if you forget the `u挂载` and try to unload the module?
|
||
|
||
```console
|
||
# mount /dev/myblk0 /mnt/myblk
|
||
# kldunload myblk
|
||
kldunload: can't unload file: Device busy
|
||
```
|
||
|
||
The 内核 refuses. The `g_disk` subsystem knows that our provider still has an active exclusive holder, and it will not let `disk_destroy` proceed until the hold is released. This is the same mechanism we saw in 第26章 protecting the USB serial 设备 during an active session, lifted to the GEOM layer.
|
||
|
||
This is a safety feature. Unloading the module while a 文件系统 is 挂载ed on the backing 设备 would cause the 内核 to panic on the next BIO: the strategy function would no longer exist, but UFS would still try to call into it.
|
||
|
||
Un挂载 first, then unload.
|
||
|
||
```console
|
||
# umount /mnt/myblk
|
||
# kldunload myblk
|
||
# kldstat | grep myblk
|
||
#
|
||
```
|
||
|
||
Clean.
|
||
|
||
### A Brief Anatomy of UFS on Top of Our Driver
|
||
|
||
Now that we have UFS 挂载ed on our 设备, it is worth pausing to notice what is actually on the backing store. UFS is a well-documented 文件系统, and seeing its structures in place on a 设备 we control is illuminating.
|
||
|
||
The first 65535 bytes of a UFS 文件系统 are reserved for the boot area. On our 设备, these bytes are all zero because `newfs_ufs` does not write a boot 扇区 by default.
|
||
|
||
At offset 65536 lives the super块. The super块 is a fixed-size structure that describes the geometry of the 文件系统: the 块 size, the fragment size, the number of cylinder groups, the location of the root inode, and many other invariants. `newfs_ufs` writes the super块 first, and it also writes backup copies at predictable offsets in case the primary is corrupted.
|
||
|
||
Following the super块 come the cylinder groups. Each cylinder group holds inodes, data 块, and metadata for a chunk of the 文件系统's address space. The number and size of cylinder groups depends on the 文件系统 size. Our 32 MiB 文件系统 has four cylinder groups of 8 MiB each.
|
||
|
||
Within each cylinder group sit inode 块. Each inode is a small structure (256 bytes on FreeBSD UFS2) that describes a single file or directory: its type, owner, permissions, timestamps, size, and the 块 addresses of its data.
|
||
|
||
Finally, the data 块 themselves hold file contents. These are allocated from the free-块 map in the cylinder group.
|
||
|
||
When we wrote `"hello from myblk"` into `/mnt/myblk/hello.txt`, the 内核 did roughly the following:
|
||
|
||
1. VFS asked UFS to create a new file `hello.txt` in the root directory.
|
||
2. UFS allocated an inode from the root cylinder group's inode table.
|
||
3. UFS updated the root directory's inode to include an entry for `hello.txt`.
|
||
4. UFS allocated a data 块 for the file.
|
||
5. UFS wrote the 17 bytes of content into that data 块.
|
||
6. UFS wrote the updated inode back.
|
||
7. UFS wrote the updated directory entry back.
|
||
8. UFS updated its internal bookkeeping.
|
||
|
||
Each of those steps turned into one or more BIOs to our 驱动程序. Most were small writes on metadata 块. The file content itself was one BIO. UFS's Soft Updates feature orders the writes to ensure crash consistency.
|
||
|
||
If you want to see these BIOs in action, run your DTrace one-liner from Lab 7 while creating a file. You will see a small burst of writes around the time of the `echo`.
|
||
|
||
### How Mount Actually Works
|
||
|
||
The `挂载(8)` command is a wrapper around the `挂载(2)` system call. That system call takes a 文件系统 type, a source 设备, and a target 挂载 point, and it asks the 内核 to perform the 挂载.
|
||
|
||
The 内核's response is to find the appropriate 文件系统 code by type (UFS, ZFS, tmpfs, etc.) and to call its 挂载 handler, which in UFS's case is `ufs_挂载` in `/usr/src/sys/ufs/ffs/ffs_vfsops.c`. The 挂载 handler validates the source, opens it as a GEOM consumer, reads the super块, verifies that it is well-formed, allocates an in-memory 挂载 structure, and installs it in the namespace.
|
||
|
||
From our 驱动程序's point of view, none of this is visible. We see a series of BIOs: first a few reads for the super块, then whatever UFS needs to bootstrap its in-memory state. Once 挂载 has succeeded, UFS issues BIOs on its own schedule as the 文件系统 is used.
|
||
|
||
If 挂载 fails, UFS reports an error and the 内核's 挂载 code cleans up. The GEOM consumer is 分离ed, the access count drops, and the namespace is left alone. Our 驱动程序 does not need to do anything special on 挂载 failure.
|
||
|
||
### The GEOM Character Interface
|
||
|
||
Earlier in the chapter we said that raw access through `/dev/myblk0` goes through "GEOM's character 接口". Here is what that means in more detail.
|
||
|
||
GEOM publishes a 字符设备 for every provider. This is not the same as a `cdev` created with `make_dev`; it is a specialised path within GEOM that presents a provider as a 字符设备 to `devfs`. The code for this lives in `/usr/src/sys/geom/geom_dev.c`.
|
||
|
||
When a user program opens `/dev/myblk0`, `devfs` routes the `open` to GEOM's character-接口 code, which 附加 a consumer to our provider with the requested access mode. When the program writes, GEOM's character-接口 code builds a BIO and issues it to our provider, which routes it to our strategy function. When the program closes the file 描述符, GEOM 分离 the consumer, releasing the access.
|
||
|
||
The character-接口 layer translates between `struct uio` (the user-space I/O 描述符) and `struct bio` (the 块-layer I/O 描述符). It splits large user I/O into multiple BIOs when necessary, respecting the `d_maxsize` we specified.
|
||
|
||
All of this is invisible to our 驱动程序. We just receive BIOs. But knowing that the character 接口 exists helps you understand why certain user-space operations map to certain BIO patterns, and why `d_maxsize` matters.
|
||
|
||
### What Filesystems Need from a Block Driver
|
||
|
||
Now that we have actually 挂载ed a 文件系统 on our 驱动程序, we can describe more precisely what a 文件系统 requires from a 块 驱动程序 underneath.
|
||
|
||
A 文件系统 needs **correct reads and writes**. If a write at offset X is followed by a read at offset X, the read must return what the write put there, up to the granularity of the 扇区 size. We guaranteed this with `memcpy` into and out of our backing store.
|
||
|
||
A 文件系统 needs **correct bounds**. The 块 驱动程序 must not accept reads or writes that extend beyond the media size. We check this explicitly in the strategy function.
|
||
|
||
A 文件系统 needs **stable media size**. The size of the 设备 must not change under the 文件系统's feet once it is 挂载ed, because 文件系统 metadata encodes offsets and counts that assume a fixed size. Our 驱动程序 holds the media size constant.
|
||
|
||
A 文件系统 needs **crash safety**, to the extent the underlying storage provides it. UFS can recover from an unclean shutdown if the backing store does not lose previously committed writes. Our RAM-backed 驱动程序 loses everything on reboot, but it is at least self-consistent while running. In Section 7, we will introduce options for persistence.
|
||
|
||
A 文件系统 sometimes needs **flush semantics**. A call to `BIO_FLUSH` should ensure that all previously issued writes are durable before returning. Our RAM-backed 驱动程序 trivially satisfies this, because there is no deferred writeback in its path.
|
||
|
||
Finally, a 文件系统 benefits from **fast sequential access**. This is a quality-of-service matter rather than a correctness matter, but our 驱动程序 is fine in this regard because `memcpy` is fast.
|
||
|
||
### Raw Access Versus Filesystem Access, Visualised
|
||
|
||
Let us draw the two access paths side by side, using our actual 驱动程序 as the anchor.
|
||
|
||
```text
|
||
Raw access: Filesystem access:
|
||
|
||
dd(1) cat(1)
|
||
| |
|
||
v v
|
||
open("/dev/myblk0") open("/mnt/myblk/hello.txt")
|
||
| |
|
||
v v
|
||
read(fd, ...) read(fd, ...)
|
||
| |
|
||
v v
|
||
sys_read sys_read
|
||
| |
|
||
v v
|
||
devfs VFS
|
||
| |
|
||
v v
|
||
GEOM character UFS
|
||
interface (VOP_READ, bmap)
|
||
| |
|
||
| v
|
||
| buffer cache
|
||
| |
|
||
v v
|
||
GEOM topology <--------------------- GEOM topology
|
||
| |
|
||
v v
|
||
myblk_strategy (BIO_READ) myblk_strategy (BIO_READ)
|
||
```
|
||
|
||
The last two hops are identical. Our strategy function is called exactly the same way regardless of whether the request came from `dd` or from `cat` on a 挂载ed file. This is the great advantage of living at the 块 layer: we do not need to distinguish between the two paths. The upper layers sort out how to translate file-level operations into 块-level operations, and we deal in 块.
|
||
|
||
### Watching the Request Path with DTrace
|
||
|
||
If you want to see the request path explicitly, DTrace can help.
|
||
|
||
```console
|
||
# dtrace -n 'fbt::myblk_strategy:entry { printf("cmd=%d off=%lld len=%u", \
|
||
args[0]->bio_cmd, args[0]->bio_offset, args[0]->bio_bcount); }'
|
||
```
|
||
|
||
With the 探测 running, do something on the 挂载ed 文件系统 in another terminal and watch the BIOs arrive. You will see reads come through in 512-byte to 32 KiB chunks, depending on UFS's 块 size and what operation you performed. Running `dd if=/dev/zero of=/mnt/myblk/test bs=1m count=1` produces a burst of 32 KiB writes.
|
||
|
||
DTrace is one of the most capable observability tools FreeBSD provides, and it comes alive with storage work because the BIO path is so instrumented. We will use it more in later chapters, but even a one-liner like the above is enough to make the abstract path concrete.
|
||
|
||
### 总结 Section 6
|
||
|
||
Our pseudo 块设备 now plays the full role of a storage 设备: raw access through `dd`, 文件系统 access through UFS, and safe coexistence with 内核 卸载 protections. The strategy function we wrote in Section 5 did not need to change at all for UFS to work, because UFS and `dd` share the same 块-layer protocol below them.
|
||
|
||
We have also seen the end-to-end flow: VFS at the top, UFS just below, the 缓冲区 cache between, GEOM below that, and our 驱动程序 at the very bottom. That flow is the same for every 存储驱动程序 in FreeBSD. You now know how to occupy the bottom of it.
|
||
|
||
In the next section, we will turn our attention to persistence. A RAM-backed 设备 is convenient for testing but loses its contents on every reload. We will discuss options for making the backing store persistent, what trade-offs each option brings, and how to add one of them to our 驱动程序.
|
||
|
||
## Section 7: Persistence and In-Memory Backing Stores
|
||
|
||
Our 驱动程序 is self-consistent while running. If you write a byte at offset X, you can read it back at offset X moments later. If you create a file on the 挂载ed 文件系统, you can read it again until you 卸载 or unload. This is already useful for testing and for short-lived workloads.
|
||
|
||
It is not, however, durable. Unload the module and the backing 缓冲区 is freed. Reboot the machine and every byte vanishes. For a teaching 驱动程序 that is arguably a feature: it reboots clean, it does not accumulate state across runs, and it cannot silently corrupt a previous session. But understanding the options for making storage persistent is essential for real 驱动程序 work, so this section walks through the major choices and then shows how to add the simplest kind of persistence to our 驱动程序.
|
||
|
||
### Why Persistence Is Hard
|
||
|
||
Storage persistence is not just about where the bytes live. It is about three intertwined properties.
|
||
|
||
**Durability** means that once a write returns, the data is safe against a crash. On a hardware disk, durability is typically coupled with the disk's own cache policy: the write hits the drive's internal 缓冲区, then the platter, then the drive reports completion. `BIO_FLUSH` is the hook that gives 文件系统 a way to demand flush-to-platter semantics.
|
||
|
||
**Consistency** means that a read at offset X returns the most recent write at offset X, not some earlier or partial version. Consistency is usually provided by the hardware or by careful locking in the 驱动程序.
|
||
|
||
**Crash safety** means that after an unclean shutdown, the state of the storage is usable. Either it reflects all committed writes, or it reflects a well-defined prefix of them. UFS has SU+J (Soft Updates with Journaling) to help recover from a crash; ZFS uses copy-on-write and atomic transactions. All of that relies on a 块 layer that behaves predictably.
|
||
|
||
For a teaching 驱动程序, we do not need to address all three with full rigour. We need to understand what the choices are and to pick one that fits our goals.
|
||
|
||
### The Options
|
||
|
||
There are four common ways to back a pseudo 块设备.
|
||
|
||
**In-memory backing (our current choice)**. Fast, simple, lost on reload. Implemented as a `malloc`'d 缓冲区. Scales poorly past a few MiB because it demands contiguous 内核 memory.
|
||
|
||
**Page-at-a-time in-memory backing**. `md(4)` uses this internally for large memory disks. Instead of one big 缓冲区, the 驱动程序 keeps an indirection table of page-sized allocations and fills them on demand. This scales to very large sizes and avoids wasting memory on sparse regions, but it is more complex.
|
||
|
||
**Vnode backing**. The 驱动程序 opens a file in the host 文件系统 and uses it as the backing store. `mdconfig -t vnode` is the classic example. Reads and writes go through the host's 文件系统, which gives persistence at the cost of speed and of a dependency on the host 文件系统's correctness. This is how FreeBSD often boots from a memory-disk image embedded in the 内核: the 内核 loads the image, presents it as `/dev/md0`, and the root 文件系统 runs on it.
|
||
|
||
**Swap backing**. The 驱动程序 uses a swap-backed VM object as the backing store. `mdconfig -t swap` uses this. It provides persistence across reboots only to the extent that swap is persistent, which on most systems it is not. But it provides a very large sparse address space without consuming physical memory until touched, which is useful for scratch storage.
|
||
|
||
For this chapter, we will stick with the in-memory option. It is the simplest, it is enough for the labs, and it demonstrates every other concept cleanly. We will discuss how to switch to vnode-backed storage as an exercise, and we will point to `md(4)` for those who want to see a full-featured implementation.
|
||
|
||
### Saving and Restoring the Buffer
|
||
|
||
If we want our 设备 to remember its contents across reloads, without changing the backing approach, we can save the 缓冲区 to a file on unload and restore it on load. This is not elegant, but it is direct, and it illustrates the contract clearly: the 驱动程序 is responsible for getting the backing bytes into memory before the first BIO arrives and for flushing them to safety before the last BIO leaves.
|
||
|
||
In our case, the mechanics would look like this.
|
||
|
||
On module load, after allocating the backing 缓冲区 but before calling `disk_create`, optionally read a file on the host 文件系统 into the 缓冲区. On module unload, after `disk_destroy` has completed, optionally write the 缓冲区 back to that file.
|
||
|
||
Doing this cleanly from inside the 内核 requires the vnode API. The 内核 provides `vn_open`, `vn_rdwr`, and `vn_close`, which together let a module read or write a path in the host 文件系统. These are not APIs we want to use casually, because they are not designed for high-throughput I/O from inside a 驱动程序, and because they run on whatever 文件系统 happens to be 挂载ed at that path, which is not always safe. But for a one-shot save and restore at load and unload time, they are acceptable.
|
||
|
||
For teaching purposes we will not implement this. The correct way to persist a 块设备's contents is to use a real backing store, not to snapshot a RAM 缓冲区. But understanding the technique helps clarify the contract.
|
||
|
||
### The Contract With Upper Layers
|
||
|
||
Whatever your backing store, the contract with upper layers is precise.
|
||
|
||
**A BIO_WRITE that completes successfully must be visible to all subsequent BIO_READ requests**, regardless of 缓冲区ing layers. Our in-memory 驱动程序 satisfies this because `memcpy` is the visible effect.
|
||
|
||
**A BIO_FLUSH that completes successfully must have made all previously successful BIO_WRITE requests durable**. Our in-memory 驱动程序 satisfies this trivially because there is no lower layer between our `memcpy` and the backing memory; all writes are "durable" in the sense we can offer. A real disk 驱动程序 typically issues a cache-flush command to the hardware in response to `BIO_FLUSH`.
|
||
|
||
**A BIO_DELETE may discard data but must not corrupt neighbouring 块**. Our in-memory 驱动程序 satisfies this by zeroing only the requested range. A real SSD 驱动程序 might issue TRIM for the range; a real HDD 驱动程序 typically has no hardware support for DELETE and can safely ignore it.
|
||
|
||
**A BIO_READ must return the media contents or an error; it must not return uninitialised memory, stale cached data from a different transaction, or random bytes**. Our in-memory 驱动程序 satisfies this by zeroing the backing on allocation and writing only through the strategy function.
|
||
|
||
If you keep these four rules in mind as you design a new 驱动程序, you will avoid nearly every correctness bug that plagues new 存储驱动程序.
|
||
|
||
### What md(4) Does Differently
|
||
|
||
The 内核's `md(4)` 驱动程序 is a mature, multi-type memory-disk 驱动程序. It supports five backing types: malloc, preload, swap, vnode, and null. Each type has its own strategy function that knows how to serve requests for that backing kind. Reading `/usr/src/sys/dev/md/md.c` is a valuable follow-up to this chapter because it shows how a real 驱动程序 handles all of the cases we are glossing over.
|
||
|
||
A few specific things `md(4)` does that we do not.
|
||
|
||
`md(4)` uses a dedicated worker thread per unit. Incoming BIOs are queued on the softc, and the worker thread dequeues them one by one and dispatches them. This lets the strategy function be very simple: just enqueue and signal. It also isolates 块ing work in the worker, which matters for the vnode backing type because `vn_rdwr` can 块.
|
||
|
||
`md(4)` uses `DEV_BSHIFT` (which is `9`, meaning 512-byte 扇区) consistently and uses integer arithmetic rather than floating point to handle offsets. This is standard practice in the 块 layer.
|
||
|
||
`md(4)` has a full ioctl surface for configuration. The `mdconfig` tool talks to the 内核 through ioctls on `/dev/mdctl`, and the 驱动程序 supports `MDIOCATTACH`, `MDIOCDETACH`, `MDIOCQUERY`, and `MDIOCRESIZE`. We have not implemented anything comparable, because for our pseudo 设备 the configuration is baked in at compile time.
|
||
|
||
`md(4)` uses `DISK_VERSION_06`, which is the current version of the `g_disk` ABI. Our 驱动程序 does the same, through the `DISK_VERSION` macro.
|
||
|
||
If you want to see a production-quality pseudo 块设备, `md(4)` is the canonical reference. Almost everything we are building would, in a real 驱动程序, grow to resemble the shape of `md(4)` over time.
|
||
|
||
### A Note on Swap-Backed Memory
|
||
|
||
One technique worth naming, even though we will not use it here, is swap-backed memory. Instead of a `malloc`'d 缓冲区, a 驱动程序 can allocate a VM object of type `OBJT_SWAP` and map pages from it on demand. The pages are backed by swap space, which means they can be paged out when the system is under memory pressure and paged back in when touched. This gives you a very large, sparse, on-demand backing store that behaves like RAM when hot and like disk when cold.
|
||
|
||
`md(4)` uses exactly this approach for its swap-backed memory disks. The swap VM object acts as a backing store that the 内核's VM subsystem manages for us, without the 驱动程序 needing to allocate contiguous physical memory up front. The `OBJT_SWAP` object can hold terabytes of addressable space on a system with only gigabytes of RAM, because most of that space is never touched.
|
||
|
||
If you ever need to prototype a 块设备 larger than a few hundred MiB, swap-backed memory is likely the right tool. The VM API for it lives in `/usr/src/sys/vm/swap_pager.c`. Reading it is not light work, but it is educational.
|
||
|
||
### A Note on Preloaded Images
|
||
|
||
FreeBSD has a mechanism called **preloaded modules**. During boot, the loader can bring in not just 内核模块 but also arbitrary data blobs, which are made available to the 内核 through `preload_fetch_addr` and `preload_fetch_size`. `md(4)` uses this to expose preloaded 文件系统 images as `/dev/md*` 设备, which is one of the ways FreeBSD can boot entirely from a memory-disk root.
|
||
|
||
Preloaded images are not a persistence mechanism per se. They are a way to ship data with a 内核模块. But they are often used in embedded systems, where the root 文件系统 is too precious to live on writable storage.
|
||
|
||
### A Small Extension: Persisting Across Module Reloads Only
|
||
|
||
We are not going to add real persistence to our 驱动程序, but this is a good moment to talk about what it would actually take to make a backing store survive a module unload and reload within the same 内核 boot. The naive first idea, and one that beginners reach for quickly, is to put the backing pointer in a file-scope `static` variable and simply not free it in the unload handler. Let us look at why that does not work and what does.
|
||
|
||
Consider this sketch:
|
||
|
||
```c
|
||
static uint8_t *myblk_persistent_backing; /* wishful thinking */
|
||
static size_t myblk_persistent_size;
|
||
```
|
||
|
||
The intuition is that if we allocate `myblk_persistent_backing` on first 附加 and refuse to free it on 分离, a subsequent `kldload` will see the pointer still set and reuse the 缓冲区. The problem is that this picture ignores how a KLD is actually loaded and unloaded. When `kldunload` removes our module, the 内核 reclaims the module's text, data, and `.bss` segments along with the rest of its image. Our static pointer does not persist in some stable location; it vanishes together with the module. When `kldload` then brings the module back, the 内核 allocates a fresh `.bss`, zeroes it, and our pointer starts life as `NULL` again. The `malloc`'d 缓冲区 we allocated on the previous 附加 is still sitting in 内核 heap somewhere, but we have lost every handle to it. We have leaked it.
|
||
|
||
`SYSUNINIT` does not help either, because in a KLD context it fires on `kldunload`, not on some later "final tear-down" event. Registering a `SYSUNINIT` to free the 缓冲区 would free it on every unload, which is exactly what we did not want. There is no KLD-level hook that means "the module file is really, truly being removed from memory for good" distinct from plain `kldunload`.
|
||
|
||
Two techniques actually achieve cross-unload persistence, and both are used by `md(4)` in production. The first is a **file-backed store**. Instead of allocating a 内核 heap 缓冲区, the 驱动程序 opens a file on an existing 文件系统 using the vnode I/O API (`VOP_READ`, `VOP_WRITE`, and the vnode reference taken via `vn_open`) and services BIOs by reading from and writing to that file. On unload, the 驱动程序 closes the file; on the next load, it re-opens it. The persistence is real because it lives in a 文件系统 whose state is independent of our module. This is exactly what `md -t vnode -f /path/to/image.img` does, and you can study it in `/usr/src/sys/dev/md/md.c`.
|
||
|
||
The second technique is a **swap-backed store**. The 驱动程序 allocates a VM object of type `OBJT_SWAP`, as we mentioned earlier, and maps pages from it on demand. The pager lives at a higher level of the 内核 than our module, so the object can outlive any particular `kldunload` as long as something else holds a reference to it. In practice, `md(4)` uses this for swap-backed memory disks, and it ties the object's lifetime to a 内核-wide list rather than to a module instance.
|
||
|
||
For our teaching 驱动程序, we will not implement either technique. The point of showing this discussion is to make sure you understand why the apparent shortcut does not work, so that you do not spend an afternoon debugging a 缓冲区 that keeps disappearing after `kldunload`. If you want to experiment with real cross-unload persistence, read `md.c` carefully, particularly the `MD_VNODE` and `MD_SWAP` branches in `mdstart_vnode` and `mdstart_swap`, and note how the backing objects are 附加ed to the per-unit `struct md_s` rather than to module-scope globals. That structural choice is what makes those backends work across module lifecycles.
|
||
|
||
### Sketching a Vnode-Backed Strategy Function
|
||
|
||
To make the earlier discussion concrete, let us sketch what a vnode-backed strategy function looks like at the code level. We are not going to drop this into our teaching 驱动程序. We are showing it so that you can see what the "real" solution involves and can recognise the same shape in `md.c` when you read it.
|
||
|
||
The idea is that the per-unit softc holds a reference to a vnode, acquired at 附加 time from a path provided by the administrator. The strategy function translates each BIO into a `vn_rdwr` call at the right offset and completes the BIO based on the result.
|
||
|
||
Attach acquires the vnode:
|
||
|
||
```c
|
||
static int
|
||
myblk_vnode_attach(struct myblk_softc *sc, const char *path)
|
||
{
|
||
struct nameidata nd;
|
||
int flags, error;
|
||
|
||
flags = FREAD | FWRITE;
|
||
NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, path);
|
||
error = vn_open(&nd, &flags, 0, NULL);
|
||
if (error != 0)
|
||
return (error);
|
||
NDFREE_PNBUF(&nd);
|
||
VOP_UNLOCK(nd.ni_vp);
|
||
sc->vp = nd.ni_vp;
|
||
sc->vp_cred = curthread->td_ucred;
|
||
crhold(sc->vp_cred);
|
||
return (0);
|
||
}
|
||
```
|
||
|
||
`vn_open` looks up the path and returns a locked, referenced vnode. We then drop the lock, because we want to hold a reference without 块ing other operations, and we hang the vnode pointer on our softc. We also keep a reference to the credentials we will use for subsequent I/O.
|
||
|
||
The strategy function services BIOs against the vnode:
|
||
|
||
```c
|
||
static void
|
||
myblk_vnode_strategy(struct bio *bp)
|
||
{
|
||
struct myblk_softc *sc = bp->bio_disk->d_drv1;
|
||
int error;
|
||
|
||
switch (bp->bio_cmd) {
|
||
case BIO_READ:
|
||
error = vn_rdwr(UIO_READ, sc->vp, bp->bio_data,
|
||
bp->bio_length, bp->bio_offset, UIO_SYSSPACE,
|
||
IO_DIRECT, sc->vp_cred, NOCRED, NULL, curthread);
|
||
break;
|
||
case BIO_WRITE:
|
||
error = vn_rdwr(UIO_WRITE, sc->vp, bp->bio_data,
|
||
bp->bio_length, bp->bio_offset, UIO_SYSSPACE,
|
||
IO_DIRECT | IO_SYNC, sc->vp_cred, NOCRED, NULL,
|
||
curthread);
|
||
break;
|
||
case BIO_FLUSH:
|
||
error = VOP_FSYNC(sc->vp, MNT_WAIT, curthread);
|
||
break;
|
||
case BIO_DELETE:
|
||
/* Vnode-backed devices usually do not support punching
|
||
* holes through BIO_DELETE without additional plumbing.
|
||
*/
|
||
error = EOPNOTSUPP;
|
||
break;
|
||
default:
|
||
error = EOPNOTSUPP;
|
||
break;
|
||
}
|
||
|
||
if (error != 0) {
|
||
bp->bio_error = error;
|
||
bp->bio_flags |= BIO_ERROR;
|
||
bp->bio_resid = bp->bio_bcount;
|
||
} else {
|
||
bp->bio_resid = 0;
|
||
}
|
||
biodone(bp);
|
||
}
|
||
```
|
||
|
||
Notice how the shape of the switch is identical to our RAM-backed strategy function. The only difference is what the case arms do: instead of `memcpy` into a 缓冲区, we call `vn_rdwr` against a vnode. The 框架 above us, GEOM and the 缓冲区 cache, does not know or care which backend we chose.
|
||
|
||
Detach releases the vnode:
|
||
|
||
```c
|
||
static void
|
||
myblk_vnode_detach(struct myblk_softc *sc)
|
||
{
|
||
|
||
if (sc->vp != NULL) {
|
||
(void)vn_close(sc->vp, FREAD | FWRITE, sc->vp_cred,
|
||
curthread);
|
||
sc->vp = NULL;
|
||
}
|
||
if (sc->vp_cred != NULL) {
|
||
crfree(sc->vp_cred);
|
||
sc->vp_cred = NULL;
|
||
}
|
||
}
|
||
```
|
||
|
||
`vn_close` releases the vnode reference and, if this was the last reference, allows the vnode to be recycled. The credentials are reference-counted the same way.
|
||
|
||
Why does this give us cross-unload persistence? Because the state we care about, namely the contents of the backing store, lives in a file on a real 文件系统 whose lifetime is completely independent of our module. When we call `kldunload`, the vnode reference is released and the file closes; its contents on disk are preserved by the 文件系统. When we call `kldload` again and 附加, we open the file again and pick up where we left off.
|
||
|
||
The remaining subtleties are substantial. Error paths need to release the vnode if `vn_open` succeeded but subsequent registration steps failed. Calls to `vn_rdwr` can sleep, which means the strategy function must not be called from a context that disallows sleeping; in practice, that is why `md(4)` uses a dedicated worker thread for vnode-backed units. Reading a file can race with the administrator modifying it, so production 驱动程序 usually take measures to detect concurrent external changes. `VOP_FSYNC` is not free, so a fast path that batches writes before flushing is typical. And the vnode lifetime itself is bound by VFS's own reference counting, which interacts with 卸载 of the containing 文件系统.
|
||
|
||
We will not add this to our teaching 驱动程序, but when you read `mdstart_vnode` in `/usr/src/sys/dev/md/md.c`, you will recognise every one of these issues handled carefully and explicitly.
|
||
|
||
### 总结 Section 7
|
||
|
||
Persistence is a layered concept. Durability, consistency, and crash safety are all part of what a real storage 设备 must provide, and different backing stores give different subsets of those guarantees. For a teaching 驱动程序, a `malloc`'d in-memory 缓冲区 is a reasonable choice, and we can add "survives module reload" semantics without much code by 分离ing the 缓冲区 from the per-instance softc.
|
||
|
||
For production, the techniques grow more elaborate: page-at-a-time allocation, swap-backed VM objects, vnode-backed files, dedicated worker threads, BIO_FLUSH coordination, and careful handling of every error path. `md(4)` is the canonical example in the FreeBSD tree, and reading it is strongly recommended.
|
||
|
||
In the next section, we will focus on the teardown path in detail. We will look at how GEOM coordinates 卸载, 分离, and cleanup; how access counts gate the module unload path; and how our 驱动程序 should behave when something goes wrong mid-teardown. Storage 卸载 bugs are some of the nastier kinds of 内核 bug, and careful attention here pays off for the rest of your 驱动程序-writing career.
|
||
|
||
## Section 8: Safe Un挂载 and Cleanup
|
||
|
||
Storage 驱动程序 handle the end of their lives with more care than character 驱动程序 because the stakes are higher. When a character 驱动程序 unloads cleanly, the worst thing that can happen is that an open session is torn down, possibly with some bytes in flight being lost. When a 存储驱动程序 unloads while a 文件系统 is 挂载ed on it, the worst thing that can happen is that the 内核 panics on the next BIO, and the user is left with a 文件系统 image that may or may not have been in a consistent state when the 驱动程序 disappeared.
|
||
|
||
The good news is that the 内核's defences make the catastrophic case almost impossible if you use `g_disk` correctly. The refusal of `kldunload` to proceed when the GEOM access count is non-zero, which we saw in Section 6, is the primary safety net. But it is not the only concern. This section walks through the teardown path in detail so that you know what to expect, what to implement, and what to test.
|
||
|
||
### The Expected Teardown Sequence
|
||
|
||
The nominal sequence of events when a user wants to remove a 存储驱动程序 is as follows.
|
||
|
||
1. The user 卸载s every 文件系统 that is 挂载ed on the 设备.
|
||
2. The user closes any program that has `/dev/myblk0` open for raw access.
|
||
3. The user calls `kldunload`.
|
||
4. The module unload function calls `disk_destroy`.
|
||
5. `disk_destroy` queues the provider for withering, which runs on the GEOM event thread.
|
||
6. The withering process waits for any in-flight BIO to complete.
|
||
7. The provider is removed from the GEOM topology and the `/dev` node is destroyed.
|
||
8. `disk_destroy` returns control to our unload function.
|
||
9. Our unload function frees the softc and the backing store.
|
||
10. The 内核 unloads the module.
|
||
|
||
Each step has its own failure modes. Let us walk through them.
|
||
|
||
### Step 1: Un挂载
|
||
|
||
The user runs `u挂载 /mnt/myblk`. VFS asks UFS to flush the 文件系统, which causes the 缓冲区 cache to issue any pending writes to GEOM, which routes them to our 驱动程序. Our strategy function services the writes and calls `biodone`. The 缓冲区 cache reports success; UFS disposes of its in-memory state; VFS releases the 挂载 point. The consumer that UFS had 附加ed to our provider is 分离ed. The access count drops.
|
||
|
||
Our 驱动程序 does not do anything special during this phase. We keep handling BIOs as they arrive until UFS stops issuing them.
|
||
|
||
### Step 2: Close Raw Access
|
||
|
||
The user ensures that no program holds `/dev/myblk0` open. If a `dd` is running, kill it. If a shell has the 设备 open via `exec`, close that. Until every open handle is released, the access count will remain non-zero on at least one of the `r`, `w`, or `e` counters.
|
||
|
||
Again, our 驱动程序 does nothing special. The `close(2)` calls on `/dev/myblk0` propagate through `devfs`, through GEOM's character-设备 integration, and release their access. No BIOs are issued for close.
|
||
|
||
### Step 3: kldunload
|
||
|
||
The user runs `kldunload myblk`. The 内核's module subsystem calls our unload function with `MOD_UNLOAD`. Our unload function calls `myblk_分离_unit`, which calls `disk_destroy`.
|
||
|
||
At this point, our 驱动程序 is about to stop existing. We must not be holding any lock that could 块, we must not be 块ing on our own worker threads (we do not have any in this design), and we must not be issuing new BIOs. Nothing we do now should cause new work for the 内核.
|
||
|
||
### Step 4: disk_destroy
|
||
|
||
`disk_destroy` is the point of no return. Reading the source in `/usr/src/sys/geom/geom_disk.c` reveals that it does three things:
|
||
|
||
1. It sets a flag on the disk to indicate that destruction is in progress.
|
||
2. It queues a GEOM event that will actually dismantle the provider.
|
||
3. It waits for the event to complete.
|
||
|
||
While we are waiting, the GEOM event thread picks up the event and walks our geom. If the access counts are zero, the event proceeds. If they are not zero, the event panics with a message about trying to destroy a disk that still has users.
|
||
|
||
This is where the importance of Step 1 and Step 2 shows up. If you skip them and try to unload while the 文件系统 is 挂载ed, the panic happens here. Fortunately, `g_disk` refuses to reach the panic because the module subsystem has already refused the unload earlier, but if you were to bypass the module subsystem and call `disk_destroy` directly from some other context, this is the check that protects the 内核.
|
||
|
||
### Step 5 to 7: Withering
|
||
|
||
The GEOM withering process is how providers are removed from the topology. It works by marking the provider as withered, cancelling any BIOs that were queued but not yet delivered, waiting for any in-flight BIOs to complete, removing the provider from the geom's provider list, and then removing the geom from the class. The `/dev` node is removed as part of this.
|
||
|
||
During withering, the strategy function may still be called for BIOs that were in flight before the withering started. Our strategy function will handle them normally, because our 驱动程序 does not know or care that withering is in progress. The 框架 is responsible for ensuring no new BIOs are issued after the point of no return.
|
||
|
||
If our 驱动程序 had worker threads, a queue, or other internal state, we would need to coordinate with withering carefully. `md(4)` is a good example of a 驱动程序 that does this: its worker thread watches for a shutdown flag and drains its queue before exiting. Since our 驱动程序 is entirely synchronous and single-threaded, we do not have this complication.
|
||
|
||
### Step 8 to 9: Free Resources
|
||
|
||
Once `disk_destroy` returns, the disk is gone, the provider is gone, and no more BIOs will arrive. It is safe to free the backing store and destroy the 互斥锁.
|
||
|
||
```c
|
||
static void
|
||
myblk_detach_unit(struct myblk_softc *sc)
|
||
{
|
||
|
||
if (sc->disk != NULL) {
|
||
disk_destroy(sc->disk);
|
||
sc->disk = NULL;
|
||
}
|
||
if (sc->backing != NULL) {
|
||
free(sc->backing, M_MYBLK);
|
||
sc->backing = NULL;
|
||
sc->backing_size = 0;
|
||
}
|
||
}
|
||
```
|
||
|
||
Our unload function then destroys the 互斥锁 and frees the softc.
|
||
|
||
```c
|
||
case MOD_UNLOAD:
|
||
sc = myblk_unit0;
|
||
if (sc == NULL)
|
||
return (0);
|
||
myblk_detach_unit(sc);
|
||
mtx_destroy(&sc->lock);
|
||
free(sc, M_MYBLK);
|
||
myblk_unit0 = NULL;
|
||
printf("myblk: unloaded\n");
|
||
return (0);
|
||
```
|
||
|
||
### Step 10: Module Unload
|
||
|
||
The module subsystem unloads the `.ko` file. At this point, the 驱动程序 is gone. Any attempt to reference the module by name will fail until the user loads it again.
|
||
|
||
### What Can Go Wrong
|
||
|
||
The happy path is smooth. Let us enumerate the unhappy paths and how to recognise them.
|
||
|
||
**`kldunload` returns `Device 总线y`**. The 文件系统 is still 挂载ed, or a program still has the raw 设备 open. Un挂载 and close, then retry. This is the most common failure, and it is benign.
|
||
|
||
**`disk_destroy` never returns**. Something is holding a BIO that will never complete, and the withering process is waiting for it. In practice, this happens if your strategy function fails to call `biodone` on some path. Look at `procstat -kk` of the `g_event` thread; if it is stuck in `g_waitfor_event`, you have a leaked BIO. The fix is in your strategy function: ensure that every path calls `biodone` exactly once.
|
||
|
||
**The 内核 panics with "g_disk: destroy with open count"**. Your 驱动程序 called `disk_destroy` while the provider still had users. This should not happen if you only call `disk_destroy` from the module unload path, because the module subsystem refuses to unload 总线y modules. But if you call `disk_destroy` in response to some other event, you must check the access count yourself or tolerate the panic.
|
||
|
||
**The 内核 panics with "Freeing free memory"**. Your 驱动程序 tried to free the softc or the backing store twice. Check your 分离 path for race conditions or for early exits that free and then fall through to free again.
|
||
|
||
**The 内核 panics with "Page fault in 内核 mode"**. Something is dereferencing a freed pointer, most often the backing store after it has been freed while a BIO is still in flight. The fix is to ensure `disk_destroy` completes before freeing anything the strategy function touches.
|
||
|
||
### The d_gone Callback
|
||
|
||
There is one more piece of the teardown story worth discussing. The `d_gone` 回调 is invoked when something other than our 驱动程序 decides the disk should go away. The canonical example is hotplug removal: a user yanks a USB drive, the USB stack tells the 存储驱动程序 the 设备 is gone, and the 存储驱动程序 wants to tell GEOM to tear down the disk as gracefully as possible even though I/O will start failing.
|
||
|
||
Our 驱动程序 is a pseudo 设备; it does not have a physical disappearance event. But 注册ing a `d_gone` 回调 costs nothing and makes the 驱动程序 slightly more ro总线t against future extensions.
|
||
|
||
```c
|
||
static void
|
||
myblk_disk_gone(struct disk *dp)
|
||
{
|
||
|
||
printf("myblk: disk_gone(%s%u)\n", dp->d_name, dp->d_unit);
|
||
}
|
||
```
|
||
|
||
Register it with `sc->disk->d_gone = myblk_disk_gone;` before `disk_create`. The function is called by `g_disk` when `disk_gone` is invoked. You can trigger it manually during development by calling `disk_gone(sc->disk)` from a test path; you will not usually call it yourself in a pseudo 驱动程序.
|
||
|
||
Note the difference between `disk_gone` and `disk_destroy`. `disk_gone` says "this disk has physically vanished; stop accepting I/O and mark the provider as error-returning". `disk_destroy` says "remove this disk from the topology and free its resources". In a 热拔出 path, `disk_gone` is usually called first (by the 总线 驱动程序, when it notices the 设备 is gone), and `disk_destroy` is called later (by the module unload, or by the 总线 驱动程序's 分离 function). Between the two calls, the disk still exists in the topology but all I/O fails. Our 驱动程序 does not implement this dual-phase teardown; a USB mass 存储驱动程序, for instance, must.
|
||
|
||
### Testing the Teardown
|
||
|
||
Teardown bugs are often discovered not by careful testing but by accident, months later, when some user finds an unusual sequence that triggers them. It is much cheaper to test teardown deliberately.
|
||
|
||
Here are the tests I recommend running on any new 存储驱动程序.
|
||
|
||
**Basic unload**. Load, format, 挂载, 卸载, unload. Verify `dmesg` shows our load and unload messages and nothing else. Repeat ten times to catch slow leaks.
|
||
|
||
**Unload without 卸载**. Load, format, 挂载. Attempt to unload. Verify the unload is refused. Un挂载, then unload. Verify no lingering state.
|
||
|
||
**Unload under load**. Load, format, 挂载, start a `dd if=/dev/urandom of=/mnt/myblk/stress bs=1m count=64`. While the `dd` runs, attempt to unload. Verify the unload is refused. Wait for `dd` to finish. Un挂载. Unload. Verify clean.
|
||
|
||
**Unload with raw open**. Load. In another terminal, run `cat > /dev/myblk0` to hold the 设备 open. Attempt to unload. Verify the unload is refused. Kill the cat. Unload. Verify clean.
|
||
|
||
**Reload stress**. Load, unload, load, unload in a tight loop for a minute. If `vmstat -m` or `zpool list` starts showing leaks, investigate.
|
||
|
||
**Panic on corruption**. This one is harder: deliberately corrupt the module state via a 内核 debugger hook and verify that the 驱动程序 does not silently return bad data. In practice, few beginners do this, and it is not required for a teaching 驱动程序.
|
||
|
||
If all of these pass, you have a reasonably ro总线t teardown. Continue testing every change that touches the unload path.
|
||
|
||
### The Idempotency Principle
|
||
|
||
A good teardown path is idempotent: calling it twice is no worse than calling it once. This matters because error paths during 附加 may call the teardown before everything has been set up.
|
||
|
||
Write your teardown to check whether each resource was actually allocated before trying to free it.
|
||
|
||
```c
|
||
static void
|
||
myblk_detach_unit(struct myblk_softc *sc)
|
||
{
|
||
|
||
if (sc == NULL)
|
||
return;
|
||
|
||
if (sc->disk != NULL) {
|
||
disk_destroy(sc->disk);
|
||
sc->disk = NULL;
|
||
}
|
||
if (sc->backing != NULL) {
|
||
free(sc->backing, M_MYBLK);
|
||
sc->backing = NULL;
|
||
sc->backing_size = 0;
|
||
}
|
||
}
|
||
```
|
||
|
||
Setting pointers to `NULL` after freeing them is a small discipline that pays off. It makes double-free mistakes obvious at runtime (they become no-ops rather than corruptions), and it makes the teardown function idempotent.
|
||
|
||
### Ordering and Reverse Order
|
||
|
||
A general teardown guideline: free resources in the reverse order of allocation. If 附加 goes `A -> B -> C`, 分离 should go `C -> B -> A`.
|
||
|
||
In our 驱动程序, 附加 goes `malloc backing -> disk_alloc -> disk_create`. So 分离 goes `disk_destroy -> free backing`. We skip freeing the disk because `disk_destroy` frees it for us.
|
||
|
||
This pattern is universal. Every well-written teardown function reverses the allocation order. When you see a 分离 that runs in the same order as the 附加, suspect a bug.
|
||
|
||
### The MOD_QUIESCE Event
|
||
|
||
There is a third module event we have not mentioned: `MOD_QUIESCE`. It is delivered before `MOD_UNLOAD` and gives the module a chance to refuse unload if the 驱动程序 is in a state where unloading is unsafe.
|
||
|
||
For most 驱动程序, the GEOM access-count check is sufficient, and implementing `MOD_QUIESCE` is not needed. But if your 驱动程序 has internal state that makes unload unsafe independent of GEOM (for example, a cache that must be flushed), `MOD_QUIESCE` is where you decline the unload by returning an error.
|
||
|
||
Our 驱动程序 does not implement `MOD_QUIESCE`. The default behaviour is to accept it silently, which is the right thing for us.
|
||
|
||
### Coordinating With Future Worker Threads
|
||
|
||
If you ever add a worker thread to the 驱动程序, the teardown contract changes. You must:
|
||
|
||
1. Signal the worker to stop, typically by setting a flag on the softc.
|
||
2. Wake the worker if it is sleeping, typically with `wakeup` or `cv_signal`.
|
||
3. Wait for the worker to exit, typically with a `kthread_exit`-visible termination flag.
|
||
4. Only then call `disk_destroy`.
|
||
5. Free the softc and backing store.
|
||
|
||
Skipping any of these steps is a recipe for a panic. The usual failure mode is that the worker thread is sleeping inside a function that touches softc state after the softc has been freed. `md(4)` handles this carefully, and it is worth reading its worker shutdown code if you plan to add a worker to your own 驱动程序.
|
||
|
||
### Cleanup in the Face of Errors
|
||
|
||
One last concern: what happens if 附加 fails partway through? Suppose `disk_alloc` succeeds, but `disk_create` fails. Or suppose we add code that validates the 扇区 size and rejects invalid configurations before calling `disk_create`.
|
||
|
||
The pattern for handling this is "single cleanup path". Write the 附加 function so that any failure jumps to a cleanup label that unwinds everything allocated so far, in reverse order.
|
||
|
||
```c
|
||
static int
|
||
myblk_attach_unit(struct myblk_softc *sc)
|
||
{
|
||
int error;
|
||
|
||
sc->backing_size = MYBLK_MEDIASIZE;
|
||
sc->backing = malloc(sc->backing_size, M_MYBLK, M_WAITOK | M_ZERO);
|
||
|
||
sc->disk = disk_alloc();
|
||
sc->disk->d_name = MYBLK_NAME;
|
||
sc->disk->d_unit = sc->unit;
|
||
sc->disk->d_strategy = myblk_strategy;
|
||
sc->disk->d_ioctl = myblk_ioctl;
|
||
sc->disk->d_getattr = myblk_getattr;
|
||
sc->disk->d_gone = myblk_disk_gone;
|
||
sc->disk->d_sectorsize = MYBLK_SECTOR;
|
||
sc->disk->d_mediasize = MYBLK_MEDIASIZE;
|
||
sc->disk->d_maxsize = MAXPHYS;
|
||
sc->disk->d_drv1 = sc;
|
||
|
||
error = 0; /* disk_create is void; no error path from it */
|
||
disk_create(sc->disk, DISK_VERSION);
|
||
return (error);
|
||
|
||
/*
|
||
* Future expansion: if we add a step that can fail between
|
||
* disk_alloc and disk_create, use a cleanup label here.
|
||
*/
|
||
}
|
||
```
|
||
|
||
For our 驱动程序, `disk_alloc` does not fail in practice (it uses `M_WAITOK`), and `disk_create` is a `void` function that queues the real work asynchronously. So the 附加 path cannot really fail. But the pattern of preparing a single cleanup label is worth keeping in mind for 驱动程序 that grow more complex.
|
||
|
||
### 总结 Section 8
|
||
|
||
Safe 卸载 and cleanup for a 存储驱动程序 comes down to a small set of disciplines: handle every BIO through to `biodone`, never hold locks during completion 回调, only call `disk_destroy` when the provider has no users, free resources in the reverse order of allocation, and test the teardown under load. The `g_disk` 框架 handles most of the hard parts; your job is to avoid breaking its invariants.
|
||
|
||
In the next section, we will step back from the teardown specifics and talk about how to let a 存储驱动程序 grow. We will discuss refactoring, versioning, how to support multiple units cleanly, and what to do when the 驱动程序 becomes more than a single source file. These are the habits that turn a teaching 驱动程序 into something you can keep evolving for a long time.
|
||
|
||
## Section 9: Refactoring and Versioning
|
||
|
||
Our 驱动程序 fits in a single file and solves one problem: it exposes a single pseudo disk of a fixed size, backed by RAM. That is a useful teaching starting point, but it is not where most real 驱动程序 live. A real 存储驱动程序 evolves. It grows ioctl support. It grows multi-unit support. It grows tunable parameters. It splits into multiple source files. Its on-disk representation, if any, goes through format changes. It accumulates a history of compatibility choices.
|
||
|
||
This section is about the habits that let a 驱动程序 grow gracefully. We will not add massive new features here; the companion labs and challenges will do that. What we will do is survey the refactoring and versioning questions that arise as any 存储驱动程序 matures, and we will point to the FreeBSD-idiomatic answers for each.
|
||
|
||
### Multi-Unit Support
|
||
|
||
Right now our 驱动程序 supports exactly one instance, hardcoded as `myblk0`. If you wanted two or three pseudo disks, the current code would need duplicate softcs and duplicate disk registrations. Real 驱动程序 solve this with a data structure that can hold any number of units.
|
||
|
||
The idiomatic FreeBSD pattern is a global list protected by a lock. The softc is allocated per unit and linked into the list. A loader-time tunable or an ioctl-driven call decides when to create a new unit. The unit number is allocated from a `unrhdr` (unique number range) allocator.
|
||
|
||
A sketch:
|
||
|
||
```c
|
||
static struct mtx myblk_list_lock;
|
||
static LIST_HEAD(, myblk_softc) myblk_list =
|
||
LIST_HEAD_INITIALIZER(myblk_list);
|
||
static struct unrhdr *myblk_unit_pool;
|
||
|
||
static int
|
||
myblk_create_unit(size_t mediasize, struct myblk_softc **scp)
|
||
{
|
||
struct myblk_softc *sc;
|
||
int unit;
|
||
|
||
unit = alloc_unr(myblk_unit_pool);
|
||
if (unit < 0)
|
||
return (ENOMEM);
|
||
|
||
sc = malloc(sizeof(*sc), M_MYBLK, M_WAITOK | M_ZERO);
|
||
mtx_init(&sc->lock, "myblk unit", NULL, MTX_DEF);
|
||
sc->unit = unit;
|
||
sc->backing_size = mediasize;
|
||
sc->backing = malloc(mediasize, M_MYBLK, M_WAITOK | M_ZERO);
|
||
|
||
sc->disk = disk_alloc();
|
||
sc->disk->d_name = MYBLK_NAME;
|
||
sc->disk->d_unit = sc->unit;
|
||
sc->disk->d_strategy = myblk_strategy;
|
||
sc->disk->d_sectorsize = MYBLK_SECTOR;
|
||
sc->disk->d_mediasize = mediasize;
|
||
sc->disk->d_maxsize = MAXPHYS;
|
||
sc->disk->d_drv1 = sc;
|
||
disk_create(sc->disk, DISK_VERSION);
|
||
|
||
mtx_lock(&myblk_list_lock);
|
||
LIST_INSERT_HEAD(&myblk_list, sc, link);
|
||
mtx_unlock(&myblk_list_lock);
|
||
|
||
*scp = sc;
|
||
return (0);
|
||
}
|
||
|
||
static void
|
||
myblk_destroy_unit(struct myblk_softc *sc)
|
||
{
|
||
|
||
mtx_lock(&myblk_list_lock);
|
||
LIST_REMOVE(sc, link);
|
||
mtx_unlock(&myblk_list_lock);
|
||
|
||
disk_destroy(sc->disk);
|
||
free(sc->backing, M_MYBLK);
|
||
mtx_destroy(&sc->lock);
|
||
free_unr(myblk_unit_pool, sc->unit);
|
||
free(sc, M_MYBLK);
|
||
}
|
||
```
|
||
|
||
The loader initialises the unit pool once, and then individual units can be created and destroyed independently. This is very close to the pattern `md(4)` uses.
|
||
|
||
We will not refactor our chapter 驱动程序 to multi-unit yet, because the added code distracts from the other teaching goals. But you should know that this is where the 驱动程序 would go. Supporting multiple units is one of the first extensions real 驱动程序 need.
|
||
|
||
### Ioctl Surface for Runtime Configuration
|
||
|
||
With multiple units comes the need to configure them at runtime. You do not want to compile a new module every time you want a second unit or a different size. The answer is an ioctl on a control 设备.
|
||
|
||
`md(4)` follows this pattern. There is a single `/dev/mdctl` 设备, and `mdconfig(8)` talks to it with ioctls. `MDIOCATTACH` creates a new unit with a specified size and backing type. `MDIOCDETACH` destroys a unit. `MDIOCQUERY` reads the state of a unit. `MDIOCRESIZE` changes the size.
|
||
|
||
For a 驱动程序 of any sophistication, this is the right place to invest. Compile-time configuration via macros is fine for a toy. Runtime configuration via ioctls is what real administrators want.
|
||
|
||
If you were to add this to our 驱动程序, you would:
|
||
|
||
1. Create a `cdev` for the control 设备 using `make_dev`.
|
||
2. Implement `d_ioctl` on the cdev, switching on a small set of ioctl numbers you define.
|
||
3. Write a user-space tool that issues the ioctls.
|
||
|
||
This is a substantial addition, which is why we mention it here without implementing it. 第28章 and later chapters will revisit this pattern.
|
||
|
||
### Splitting the Source File
|
||
|
||
At some point, a 驱动程序 outgrows a single file. The usual decomposition for a FreeBSD 存储驱动程序 is roughly:
|
||
|
||
- `驱动程序_name.c`: the public module entry, ioctl dispatch, and 附加/分离 wiring.
|
||
- `驱动程序_name_bio.c`: the strategy function and BIO path.
|
||
- `驱动程序_name_backing.c`: the backing-store implementation.
|
||
- `驱动程序_name_util.c`: small helpers, validation, and debug printing.
|
||
- `驱动程序_name.h`: the shared header that declares the softc, enums, and function prototypes.
|
||
|
||
The Makefile is updated to list all of them in `SRCS`, and the build system handles the rest. This is the shape of `md(4)`, of `ata(4)`, and of most substantial 驱动程序 in the tree.
|
||
|
||
We will keep our 驱动程序 in one file for the chapter. But when the challenges or your own extensions push it past, say, 500 lines, a decomposition like the above is the right move. Readers who want a concrete example should look at `/usr/src/sys/dev/ata/`, which splits a complex 驱动程序 across many files along clean lines.
|
||
|
||
### Versioning
|
||
|
||
A 存储驱动程序 has several kinds of versioning to care about.
|
||
|
||
**Module version**, declared with `MODULE_VERSION(myblk, 1)`. This is a monotonically increasing integer that other modules or userland tools can check. Bump it whenever you change the module's external behaviour in a way that cannot be detected from the code.
|
||
|
||
**Disk ABI version**, encoded in `DISK_VERSION`. This is the version of the `g_disk` 接口 that your 驱动程序 was compiled against. If the 内核's `g_disk` changes incompatibly, it increments the version, and a 驱动程序 compiled against the old version will fail to 注册. You do not set this directly; you pass the `DISK_VERSION` macro through `disk_create`, and it picks up whatever version the compile found in `geom_disk.h`. You should recompile 驱动程序 against the 内核 you are targeting.
|
||
|
||
**On-disk format version**, for 驱动程序 that have any on-disk metadata. If your 驱动程序 stamps a magic number and a version into a reserved 扇区, you must handle upgrades. Our 驱动程序 has no on-disk format, so this does not apply yet, but it would if we added a proper backing-store header.
|
||
|
||
**Ioctl number version**. Once you define ioctls, their numbers are part of the userland ABI. Changing them breaks older userland tools. Use `_IO`, `_IOR`, `_IOW`, `_IOWR` with stable magic letters, and do not repurpose numbers.
|
||
|
||
For our chapter 驱动程序, the only version we care about right now is the module version. But keeping these four kinds of versioning in mind saves pain later.
|
||
|
||
### Debugging and Observability Helpers
|
||
|
||
As the 驱动程序 grows, you will want to observe its state more richly than `dmesg` alone allows. Three tools are worth introducing now.
|
||
|
||
**`sysctl` nodes**. FreeBSD's `sysctl(3)` 框架 lets a module publish read-only or read-write variables that user-space tools can query. You create a tree under a chosen name and 附加 values to it. The pattern is standard; in roughly ten lines of code you can expose the number of BIOs serviced, the number of bytes read and written, and the current media size.
|
||
|
||
```c
|
||
SYSCTL_NODE(_dev, OID_AUTO, myblk, CTLFLAG_RD, 0,
|
||
"myblk driver parameters");
|
||
static u_long myblk_reads = 0;
|
||
SYSCTL_ULONG(_dev_myblk, OID_AUTO, reads, CTLFLAG_RD, &myblk_reads,
|
||
0, "Number of BIO_READ requests serviced");
|
||
```
|
||
|
||
**Devstat**. We are already using this through `g_disk`. It gives `iostat` and `gstat` their data. No further work needed.
|
||
|
||
**DTrace 探测**. The `SDT` 框架 lets a module define static DTrace 探测 that incur zero cost when the 探测 is not being watched. These are especially useful in the BIO path because they let you see live request flow without recompiling.
|
||
|
||
```c
|
||
#include <sys/sdt.h>
|
||
SDT_PROVIDER_DECLARE(myblk);
|
||
SDT_PROBE_DEFINE3(myblk, , strategy, request,
|
||
"int" /* cmd */, "off_t" /* offset */, "size_t" /* length */);
|
||
|
||
/* inside myblk_strategy: */
|
||
SDT_PROBE3(myblk, , strategy, request,
|
||
bp->bio_cmd, bp->bio_offset, bp->bio_bcount);
|
||
```
|
||
|
||
You can then watch with `dtrace -n 'myblk::strategy:request {...}'`.
|
||
|
||
For the chapter 驱动程序 we will not add all of this, but these are the patterns you should reach for as the 驱动程序 grows.
|
||
|
||
### Naming Stability
|
||
|
||
One habit that is easy to overlook: do not rename things casually. The name `myblk` is in the 设备 node, in the module version record, in the devstat name, possibly in sysctl nodes, in DTrace 探测, and in documentation. Renaming it cascades through all of those. For a project 驱动程序, pick a name you can live with forever. `md`, `ada`, `nvd`, `zvol`, and other 存储驱动程序 have kept their names for years because renaming is an ABI-affecting change for user-space tooling.
|
||
|
||
### Keeping the Teaching Driver Simple
|
||
|
||
Everything in this section is a direction your 驱动程序 might grow in. None of it is required for the teaching 驱动程序 in this chapter. We are pointing at the directions so that you can recognise them when you see them in real 驱动程序 source, and so that when you extend your own 驱动程序 you do not have to invent these patterns from scratch.
|
||
|
||
The companion `myfirst_blk.c` remains a single file at the end of this chapter. Its README documents the extension points, and the challenge exercises add some of them. Beyond that, you are free to keep extending it, and every extension you make will use these patterns in some form.
|
||
|
||
### A Short Design Patterns Recap
|
||
|
||
At this point we have accumulated enough patterns that listing them helps. When you start your next 存储驱动程序, these are the patterns to reach for.
|
||
|
||
**The softc pattern.** One per-instance struct to hold everything the 驱动程序 needs. Pointed at by `d_drv1`. Retrieved inside 回调 via `bp->bio_disk->d_drv1`.
|
||
|
||
**The 附加/分离 pair.** Attach allocates, initialises, and 寄存器. Detach reverses the sequence. Both must be idempotent.
|
||
|
||
**The switch-and-biodone pattern.** Every strategy function switches on `bio_cmd`, services each command, sets `bio_resid`, and calls `biodone` exactly once.
|
||
|
||
**The defensive bounds check.** Validate offset and length against media size, using subtraction to avoid overflow.
|
||
|
||
**The coarse lock pattern.** A single 互斥锁 around the hot path is often enough for a teaching 驱动程序. Split it only when performance demands.
|
||
|
||
**The reverse-order teardown.** Free resources in the opposite order of allocation.
|
||
|
||
**The null-after-free pattern.** After freeing a pointer, set it to `NULL`. Catches double-frees.
|
||
|
||
**The single cleanup label.** In 附加 functions that can fail, all failures jump to a single cleanup label that unwinds state so far.
|
||
|
||
**The versioned ABI.** Pass `DISK_VERSION` to `disk_create`. Declare `MODULE_VERSION`. Use `MODULE_DEPEND` on every 内核模块 you rely on.
|
||
|
||
**The deferred-work pattern.** Work that must 块 (like vnode I/O) belongs in a worker thread, not in `d_strategy`.
|
||
|
||
**The observability-first habit.** Add `printf`, `sysctl`, or DTrace 探测 as you build. Observability retrofitted late is harder than observability designed in.
|
||
|
||
These are not exhaustive, but they are the patterns you will use most often. Each of them appears somewhere in our 驱动程序, and each of them appears throughout the real FreeBSD storage code.
|
||
|
||
### 总结 Section 9
|
||
|
||
A maturing 存储驱动程序 grows in predictable directions: multi-unit support, runtime configuration through ioctls, multiple source files, and stable versioning of every 接口 it exposes. None of this has to appear in the first version. Knowing where the growth will happen lets you make early choices that do not need to be undone later.
|
||
|
||
We have now covered every concept the chapter set out to teach. Before the hands-on labs, one more topic deserves a dedicated section, because it will repay you many times over as a 驱动程序 author: observing a running 存储驱动程序. In the next section, we will look at the tools FreeBSD gives you for watching your 驱动程序 in real time and for measuring its behaviour in a disciplined way.
|
||
|
||
## Section 10: Observability and Measuring Your Driver
|
||
|
||
Writing a 存储驱动程序 is mostly a matter of getting the structure right. Once the structure is right, the 驱动程序 just runs. But for the structure to stay right, you must be able to observe what is happening while the 驱动程序 runs. You will want to know how many BIOs per second are hitting the strategy function, how long each one takes, how the latency distribution looks, how much memory the backing store consumes, whether any BIOs are getting retried, and whether any path is leaking completion.
|
||
|
||
FreeBSD gives you a remarkable set of tools for this, many of which we have already used casually. In this section we will walk through the most important ones in turn, with the goal of making you comfortable enough to reach for the right tool when the next strange symptom appears.
|
||
|
||
### gstat
|
||
|
||
`gstat` is the first tool to reach for. It updates a per-provider view of I/O activity in real time, and it shows you exactly what is happening at the GEOM layer.
|
||
|
||
```console
|
||
# gstat -I 1
|
||
dT: 1.002s w: 1.000s
|
||
L(q) ops/s r/s kBps ms/r w/s kBps ms/w %busy Name
|
||
0 117 0 0 0.0 117 468 0.1 1.1| ada0
|
||
0 0 0 0 0.0 0 0 0.0 0.0| myblk0
|
||
```
|
||
|
||
The columns, from left to right, are:
|
||
|
||
- `L(q)`: queue length. The number of BIOs currently outstanding on this provider.
|
||
- `ops/s`: total operations per second, regardless of direction.
|
||
- `r/s`: reads per second.
|
||
- `kBps` (for reads): read throughput in kilobytes per second.
|
||
- `ms/r`: average read latency, in milliseconds.
|
||
- `w/s`: writes per second.
|
||
- `kBps` (for writes): write throughput in kilobytes per second.
|
||
- `ms/w`: average write latency, in milliseconds.
|
||
- `%总线y`: the percentage of time the provider was not idle.
|
||
- `Name`: the provider name.
|
||
|
||
For a 驱动程序 you have just built, `gstat` tells you at a glance whether the 内核 is sending traffic to your 设备 and how your 驱动程序 is performing relative to real disks. If the numbers look wildly different from what you expect, you have a starting point for investigation.
|
||
|
||
`gstat -p` shows only providers (the default). `gstat -c` shows only consumers, which is less useful for 驱动程序 debugging. `gstat -f <regex>` filters by name. `gstat -b` batches the output one screen at a time instead of refreshing in place.
|
||
|
||
### iostat
|
||
|
||
`iostat` has a more traditional style but provides the same underlying data. It is useful when you want a text log rather than an interactive display.
|
||
|
||
```console
|
||
# iostat -x myblk0 1
|
||
extended device statistics
|
||
device r/s w/s kr/s kw/s ms/r ms/w ms/o ms/t qlen %b
|
||
myblk0 0 128 0 512 0.0 0.1 0.0 0.1 0 2
|
||
myblk0 0 128 0 512 0.0 0.1 0.0 0.1 0 2
|
||
```
|
||
|
||
`iostat` can watch multiple 设备 at once and can be redirected to a log file for later analysis. For quick live views, `gstat` is usually better.
|
||
|
||
### diskinfo
|
||
|
||
`diskinfo` is less about live traffic and more about static properties. We have already used it to confirm our media size.
|
||
|
||
```console
|
||
# diskinfo -v /dev/myblk0
|
||
/dev/myblk0
|
||
512 # sectorsize
|
||
33554432 # mediasize in bytes (32M)
|
||
65536 # mediasize in sectors
|
||
0 # stripesize
|
||
0 # stripeoffset
|
||
myblk0 # Disk ident.
|
||
```
|
||
|
||
`diskinfo -c` runs a timing test, reading a few hundred megabytes and reporting the sustained rate. This is useful for a first-order performance comparison.
|
||
|
||
```console
|
||
# diskinfo -c /dev/myblk0
|
||
/dev/myblk0
|
||
512 # sectorsize
|
||
33554432 # mediasize in bytes (32M)
|
||
65536 # mediasize in sectors
|
||
0 # stripesize
|
||
0 # stripeoffset
|
||
myblk0 # Disk ident.
|
||
|
||
I/O command overhead:
|
||
time to read 10MB block 0.000234 sec = 0.000 msec/sector
|
||
time to read 20480 sectors 0.001189 sec = 0.000 msec/sector
|
||
calculated command overhead = 0.000 msec/sector
|
||
|
||
Seek times:
|
||
Full stroke: 250 iter in 0.000080 sec = 0.000 msec
|
||
Half stroke: 250 iter in 0.000085 sec = 0.000 msec
|
||
Quarter stroke: 500 iter in 0.000172 sec = 0.000 msec
|
||
Short forward: 400 iter in 0.000136 sec = 0.000 msec
|
||
Short backward: 400 iter in 0.000137 sec = 0.000 msec
|
||
Seq outer: 2048 iter in 0.000706 sec = 0.000 msec
|
||
Seq inner: 2048 iter in 0.000701 sec = 0.000 msec
|
||
|
||
Transfer rates:
|
||
outside: 102400 kbytes in 0.017823 sec = 5746 MB/sec
|
||
inside: 102400 kbytes in 0.017684 sec = 5791 MB/sec
|
||
```
|
||
|
||
These numbers are unusually fast because the backing store is RAM. On a real disk they would look very different, and comparing the numbers across 设备 is often the first diagnosis step for performance problems.
|
||
|
||
### sysctl
|
||
|
||
`sysctl` is how the 内核 exposes its internal variables to user space. Many subsystems publish data through `sysctl`. You can browse the storage-related sysctls with:
|
||
|
||
```console
|
||
# sysctl -a | grep -i kern.geom
|
||
# sysctl -a | grep -i vfs
|
||
```
|
||
|
||
Adding your own sysctl tree to your 驱动程序, as we discussed in Section 9, lets you expose whatever metrics your 驱动程序 needs to track, without the ceremony of defining a new tool.
|
||
|
||
### vmstat
|
||
|
||
`vmstat -m` shows memory allocation by `MALLOC_DEFINE` tag. Our 驱动程序 uses `M_MYBLK`, so we can see how much memory our 驱动程序 has allocated.
|
||
|
||
```console
|
||
# vmstat -m | grep myblk
|
||
myblk 1 32768K - 12 32K,32M
|
||
```
|
||
|
||
The columns are type, number of allocations, current size, protection requests, total requests, and possible sizes. For a 驱动程序 that holds a 32 MiB backing store, the current size of 32 MiB is exactly what we expect. If it grew over time without an equivalent decrease on unload, we would have a leak.
|
||
|
||
`vmstat -z` shows zone allocator statistics. Much storage-related state lives in zones (GEOM providers, BIOs, disk structures), and `vmstat -z` is where to look if you suspect GEOM-level leaks.
|
||
|
||
### procstat
|
||
|
||
`procstat` shows per-thread 内核 stacks. It is indispensable when something is stuck.
|
||
|
||
```console
|
||
# procstat -kk -t $(pgrep -x g_event)
|
||
PID TID COMM TDNAME KSTACK
|
||
4 100038 geom - mi_switch sleepq_switch ...
|
||
```
|
||
|
||
If the `g_event` thread is sleeping, the GEOM layer is idle. If it is stuck in a function with your 驱动程序's name on its stack, you have a BIO that is not completing.
|
||
|
||
```console
|
||
# procstat -kk $(pgrep -x kldload)
|
||
```
|
||
|
||
If `kldload` or `kldunload` is stuck, this shows you exactly where. Most often the culprit is a `disk_destroy` waiting for BIOs to drain.
|
||
|
||
### DTrace for the Block Layer
|
||
|
||
We introduced DTrace briefly in Section 6 and in Lab 7. Here let us go a little deeper, because DTrace is the single most effective tool for understanding live storage behaviour.
|
||
|
||
The Function Boundary Tracing (FBT) provider lets you place 探测 on the entry and return of nearly any 内核 function. For our 驱动程序's strategy function, the 探测 name is `fbt::myblk_strategy:entry` for the entry and `fbt::myblk_strategy:return` for the return.
|
||
|
||
A simple one-liner that counts BIOs by command:
|
||
|
||
```console
|
||
# dtrace -n 'fbt::myblk_strategy:entry \
|
||
{ @c[args[0]->bio_cmd] = count(); }'
|
||
```
|
||
|
||
When you interrupt the script (with `Ctrl-C`), it prints a count per command value. `BIO_READ` is 1, `BIO_WRITE` is 2, `BIO_DELETE` is 3, `BIO_GETATTR` is 4, and `BIO_FLUSH` is 5. (The exact numbers are in `/usr/src/sys/sys/bio.h`.)
|
||
|
||
A latency histogram:
|
||
|
||
```console
|
||
# dtrace -n '
|
||
fbt::myblk_strategy:entry { self->t = timestamp; }
|
||
fbt::myblk_strategy:return /self->t/ {
|
||
@lat = quantize(timestamp - self->t);
|
||
self->t = 0;
|
||
}'
|
||
```
|
||
|
||
This gives you a log-scale histogram of how long each strategy-function execution took. For our in-memory 驱动程序, most buckets should be in the hundreds-of-nanoseconds range; anything in the millisecond range for an in-memory 驱动程序 is suspicious.
|
||
|
||
A breakdown of I/O size:
|
||
|
||
```console
|
||
# dtrace -n 'fbt::myblk_strategy:entry \
|
||
{ @sz = quantize(args[0]->bio_bcount); }'
|
||
```
|
||
|
||
This shows you the distribution of BIO sizes. For a UFS-backed 文件系统, you should see peaks at 4 KiB, 8 KiB, 16 KiB, and 32 KiB. For a raw `dd` with `bs=1m`, you should see a peak at 1 MiB (or the `MAXPHYS` cap, whichever is smaller).
|
||
|
||
DTrace is extraordinarily capable. The one-liners above barely scratch the surface. Two books to pick up, if you want to go deeper, are the original Sun "DTrace Guide" and Brendan Gregg's "DTrace Book". Both are older than FreeBSD 14.3 but the fundamentals still apply.
|
||
|
||
### kgdb and Crash Dumps
|
||
|
||
When your 驱动程序 panics, FreeBSD can capture a crash dump. Configure the dump 设备 in `/etc/rc.conf` (typically `dumpdev="AUTO"`) and verify with `dumpon`.
|
||
|
||
After a panic, reboot. `/var/crash/vmcore.last` (a symlink) points to the most recent dump. `kgdb /boot/内核/内核 /var/crash/vmcore.last` opens the dump for inspection. Useful commands inside `kgdb`:
|
||
|
||
- `bt`: backtrace of the thread that panicked.
|
||
- `info threads`: list all threads in the crashed system.
|
||
- `thread N` then `bt`: backtrace of thread N.
|
||
- `print *var`: inspect a variable.
|
||
- `list function`: show source around a function.
|
||
|
||
If you have compiled your module with debugging symbols (the default for most 内核 configurations), `kgdb` can show you source-level variables in your own code. This is a transformative capability once you get used to it.
|
||
|
||
### ktrace
|
||
|
||
`ktrace` is a user-space-oriented tool, but it can be useful for storage debugging when you want to see exactly what system calls a user program is making. If `newfs_ufs` is behaving oddly, you can trace it:
|
||
|
||
```console
|
||
# ktrace -f /tmp/newfs.ktr newfs_ufs /dev/myblk0
|
||
# kdump /tmp/newfs.ktr | head -n 50
|
||
```
|
||
|
||
The resulting trace shows the sequence of system calls, their arguments, and their results. For storage tools, this reveals exactly which ioctls are being issued and which file 描述符 are being opened.
|
||
|
||
### dmesg and the Kernel Log
|
||
|
||
The humble `dmesg` is often the fastest way to diagnose a problem. Our 驱动程序 prints to it on load and unload. The 内核 prints to it on many other events, including GEOM class creation, access-count violations, and panics that the system recovers from.
|
||
|
||
Pro tip: redirect `dmesg -a` to a file at the start of each lab session. If something goes wrong you will have a complete log.
|
||
|
||
```console
|
||
# dmesg -a > /tmp/session.log
|
||
# # ... work ...
|
||
# dmesg -a > /tmp/session-final.log
|
||
# diff /tmp/session.log /tmp/session-final.log
|
||
```
|
||
|
||
This gives you a precise log of what the 内核 reported during your session.
|
||
|
||
### A Simple Measurement Recipe
|
||
|
||
Here is a recipe you can use to produce a one-page performance profile of your 驱动程序.
|
||
|
||
1. Load the 驱动程序.
|
||
2. Run `diskinfo -c /dev/myblk0` and record the three transfer-rate numbers.
|
||
3. Format the 设备 and 挂载 it.
|
||
4. In one terminal, start `gstat -I 1 -f myblk0 -b` redirected to a file.
|
||
5. In another terminal, run `dd if=/dev/zero of=/mnt/myblk/stress bs=1m count=128`.
|
||
6. Stop `gstat` after `dd` completes and save the log.
|
||
7. Parse the log with `awk` to extract the peak ops/s, the peak throughput, and the average latency.
|
||
8. Un挂载 and unload.
|
||
|
||
This recipe scales. For a real 驱动程序 you would automate it, run it on a matrix of 块 sizes, and plot the results. For a teaching 驱动程序, running it once or twice gives you a feeling for the numbers and a baseline to compare against after future changes.
|
||
|
||
### Comparing Against md(4)
|
||
|
||
One of the most useful exercises is to load `md(4)` in the same configuration as your 驱动程序 and compare.
|
||
|
||
```console
|
||
# mdconfig -a -t malloc -s 32m
|
||
md0
|
||
# diskinfo -c /dev/md0
|
||
```
|
||
|
||
The numbers will likely be within a small factor of your 驱动程序's. If they are very different, something interesting is going on. The usual differences are:
|
||
|
||
- `md(4)` uses a worker thread that receives BIOs from the strategy function and processes them in a separate context. This adds a small 挂载 of latency per BIO but allows higher concurrency.
|
||
- `md(4)` uses page-at-a-time backing, which is slightly slower per byte for sequential I/O but scales to much larger sizes.
|
||
- `md(4)` supports more BIO commands and attributes than our 驱动程序.
|
||
|
||
Comparing against `md(4)` is a form of debugging: if your 驱动程序 is much slower or much faster than `md(4)` on the same workload, either you have done something unusual, or you have uncovered a difference worth understanding.
|
||
|
||
### 总结 Section 10
|
||
|
||
Observability is not an afterthought. For a 存储驱动程序, it is how you keep your bearings. `gstat`, `iostat`, `diskinfo`, `sysctl`, `vmstat`, `procstat`, and DTrace are the tools you will reach for most often. `kgdb` and crash dumps are your backstop when things go catastrophically wrong.
|
||
|
||
Learn these tools now, while the 驱动程序 is simple, because they will be the same tools you use when the 驱动程序 is complex. A developer who can observe a running 驱动程序 is much more effective than one who can only read source.
|
||
|
||
We have now covered every concept the chapter set out to teach, plus observability and measurement. Before we move on to the hands-on labs, let us spend some time reading real FreeBSD source. The case studies that follow anchor everything we have learned in code from the tree.
|
||
|
||
## Case Studies in Real FreeBSD Storage Code
|
||
|
||
Reading production 驱动程序 source is the fastest way to internalise patterns. In this section we will walk through excerpts from three real 驱动程序 in `/usr/src/sys/`, with commentary that points out what each excerpt is doing and why. The excerpts are short on purpose; we will not read every line of each 驱动程序. We will pick the lines that matter.
|
||
|
||
Open the files alongside the text and follow along. The point is for you to see the same patterns in our 驱动程序 reappearing in real 驱动程序, under different names and with different constraints.
|
||
|
||
### Case Study 1: g_zero.c
|
||
|
||
`g_zero.c` is the simplest GEOM class in the tree. It is a read-always-zero, write-discard provider, with no real backing store and no real work to do. Its purpose is to give you a standard "null disk" you can test against. It is also an excellent teaching reference because it exercises the full `g_class` API in fewer than 150 lines.
|
||
|
||
Let us look at its strategy function, called `g_zero_start`.
|
||
|
||
```c
|
||
static void
|
||
g_zero_start(struct bio *bp)
|
||
{
|
||
switch (bp->bio_cmd) {
|
||
case BIO_READ:
|
||
bzero(bp->bio_data, bp->bio_length);
|
||
g_io_deliver(bp, 0);
|
||
break;
|
||
case BIO_WRITE:
|
||
g_io_deliver(bp, 0);
|
||
break;
|
||
case BIO_GETATTR:
|
||
default:
|
||
g_io_deliver(bp, EOPNOTSUPP);
|
||
break;
|
||
}
|
||
}
|
||
```
|
||
|
||
Three behaviours, with `BIO_GETATTR` intentionally folded into the default case. Reads get zeroed. Writes are silently accepted. Anything else, including attribute queries, gets `EOPNOTSUPP`. The real `/usr/src/sys/geom/zero/g_zero.c` also handles `BIO_DELETE` in the successful-write path; our simplified excerpt above drops that case so you can see the shape clearly. Notice the call to `g_io_deliver` rather than `biodone`. That is because `g_zero` is a class-level GEOM module, not a `g_disk` module. `g_io_deliver` is the class-level completion call; `biodone` is the `g_disk` wrapper.
|
||
|
||
If you re-read our 驱动程序's strategy function side by side with this, you will see the same structure: a switch on `bio_cmd`, a case for each supported operation, a default error path. Our 驱动程序 has more cases and it has a real backing store, but the shape is identical.
|
||
|
||
The `init` function that `g_zero` 寄存器 with the class is also small:
|
||
|
||
```c
|
||
static void
|
||
g_zero_init(struct g_class *mp)
|
||
{
|
||
struct g_geom *gp;
|
||
|
||
gp = g_new_geomf(mp, "gzero");
|
||
gp->start = g_zero_start;
|
||
gp->access = g_std_access;
|
||
g_new_providerf(gp, "%s", gp->name);
|
||
g_error_provider(g_provider_by_name(gp->name), 0);
|
||
}
|
||
```
|
||
|
||
When the `g_zero` module is loaded, this runs. It creates a new geom under the class, points the `start` method at the strategy function, uses the standard access handler, and creates a provider. That is everything it takes to expose `/dev/gzero`.
|
||
|
||
In our 驱动程序, `g_disk` does the equivalent of all this when `disk_create` is called. You can see here, once more, what `g_disk` is abstracting away. For most disk 驱动程序 that is a good trade; for `g_zero`, which does not want `g_disk`'s disk-specific features, using the class API directly is the better fit.
|
||
|
||
### Case Study 2: md.c, the Malloc Strategy Function
|
||
|
||
`md(4)` is a memory-disk 驱动程序 with several backing types. The malloc backing type is the closest to our 驱动程序, and its strategy function is worth reading in detail.
|
||
|
||
Here is a simplified version of what happens when `md(4)`'s worker thread picks up a BIO for a `MD_MALLOC`-type disk. (In real `md(4)`, this is the function `mdstart_malloc`.)
|
||
|
||
```c
|
||
static int
|
||
mdstart_malloc(struct md_s *sc, struct bio *bp)
|
||
{
|
||
u_char *dst, *src;
|
||
off_t offset;
|
||
size_t resid, len;
|
||
int error;
|
||
|
||
error = 0;
|
||
resid = bp->bio_length;
|
||
offset = bp->bio_offset;
|
||
|
||
switch (bp->bio_cmd) {
|
||
case BIO_READ:
|
||
/* find the page that contains offset */
|
||
/* copy len bytes out of it */
|
||
/* advance, repeat until resid == 0 */
|
||
break;
|
||
case BIO_WRITE:
|
||
/* find the page that contains offset */
|
||
/* allocate it if not allocated yet */
|
||
/* copy len bytes into it */
|
||
/* advance, repeat until resid == 0 */
|
||
break;
|
||
case BIO_DELETE:
|
||
/* free pages in the range */
|
||
break;
|
||
}
|
||
|
||
bp->bio_resid = 0;
|
||
return (error);
|
||
}
|
||
```
|
||
|
||
The key difference from our 驱动程序 is the page-at-a-time backing. `md(4)` does not allocate one big 缓冲区. It allocates 4 KiB pages on demand and indexes them through a data structure inside the softc. The benefit is that memory disks can be much larger than a single contiguous `malloc` would allow, and sparse regions (never written) consume no memory.
|
||
|
||
The cost is that every BIO may span multiple pages, so the strategy function has to loop. Each iteration copies `len` bytes into the current page, decrements `resid`, advances `offset`, and either exits when `resid` hits zero or moves on to the next page.
|
||
|
||
Our 驱动程序 avoids this complexity at the cost of supporting only contiguous backing, which is fine up to a few tens of megabytes but not beyond.
|
||
|
||
If you wanted to extend our 驱动程序 to match `md(4)`'s scale, the page-at-a-time pattern is the direction you would go. It is straightforward once you have `md(4)` in front of you as a reference.
|
||
|
||
### Case Study 3: md.c, the Module Load Path
|
||
|
||
Another piece of `md(4)` worth studying is how it bootstraps its class and sets up the control 设备.
|
||
|
||
```c
|
||
static void
|
||
g_md_init(struct g_class *mp __unused)
|
||
{
|
||
/*
|
||
* Populate sc_list with pre-loaded memory disks
|
||
* (preloaded kernel images, ramdisks from boot, etc.)
|
||
*/
|
||
/* ... */
|
||
|
||
/*
|
||
* Create the control device /dev/mdctl.
|
||
*/
|
||
status = make_dev_p(MAKEDEV_CHECKNAME | MAKEDEV_WAITOK,
|
||
&status_dev, &mdctl_cdevsw, 0, UID_ROOT, GID_WHEEL,
|
||
0600, MDCTL_NAME);
|
||
/* ... */
|
||
}
|
||
```
|
||
|
||
The `g_md_init` function runs once per 内核 boot, when the `md(4)` class is first instantiated. It handles any memory disks that the loader preloaded into memory (so that the 内核 can boot from a memory-disk root) and it creates the control 设备 `/dev/mdctl` through which `mdconfig` will later talk to the 驱动程序.
|
||
|
||
Compare this to our loader, which is a simple `moduledata_t` that calls `disk_create` directly. `md(4)` does not create any memory disks by default. It only creates them in response to preload events or in response to `MDIOCATTACH` ioctls on the control 设备.
|
||
|
||
The pattern here is generalisable. If you want a 存储驱动程序 that creates units on demand rather than at load time, you:
|
||
|
||
1. Register the class (or, for `g_disk`-based 驱动程序, set up the infrastructure).
|
||
2. Create a control 设备 with a cdevsw that supports ioctls.
|
||
3. Implement create, destroy, and query ioctls.
|
||
4. Write a user-space tool that talks to the control 设备.
|
||
|
||
`md(4)` is the canonical example. Other 驱动程序, like `geli(4)` and `gmirror(4)`, use a slightly different pattern because they are GEOM transformation classes rather than disk 驱动程序, but the overall shape is similar.
|
||
|
||
### Case Study 4: The new总线 Side of a Real Storage Driver
|
||
|
||
For contrast, let us look briefly at how a real hardware-backed 存储驱动程序 附加. The `ada(4)` 驱动程序, for example, is a CAM-based ATA 驱动程序. Its 附加 path is not directly visible as a single function, because CAM mediates between the 驱动程序 and the hardware, but the end of the chain looks like this (abbreviated from `/usr/src/sys/cam/ata/ata_da.c`):
|
||
|
||
```c
|
||
static void
|
||
adaregister(struct cam_periph *periph, void *arg)
|
||
{
|
||
struct ada_softc *softc;
|
||
/* ... */
|
||
|
||
softc->disk = disk_alloc();
|
||
softc->disk->d_open = adaopen;
|
||
softc->disk->d_close = adaclose;
|
||
softc->disk->d_strategy = adastrategy;
|
||
softc->disk->d_getattr = adagetattr;
|
||
softc->disk->d_dump = adadump;
|
||
softc->disk->d_gone = adadiskgonecb;
|
||
softc->disk->d_name = "ada";
|
||
softc->disk->d_drv1 = periph;
|
||
/* ... */
|
||
softc->disk->d_unit = periph->unit_number;
|
||
/* ... */
|
||
softc->disk->d_sectorsize = softc->params.secsize;
|
||
softc->disk->d_mediasize = ...;
|
||
/* ... */
|
||
|
||
disk_create(softc->disk, DISK_VERSION);
|
||
/* ... */
|
||
}
|
||
```
|
||
|
||
The structure is identical to ours: fill in a `struct disk` and call `disk_create`. The differences are:
|
||
|
||
- `d_strategy` is `adastrategy`, which translates BIOs into ATA commands and issues them to the controller via CAM.
|
||
- `d_dump` is implemented, because `ada(4)` supports 内核 crash dumps. Our 驱动程序 does not implement this.
|
||
- The fields like `d_扇区ize` and `d_mediasize` come from hardware probing, not from macros.
|
||
|
||
From `g_disk`'s perspective, however, `ada0` and our `myblk0` are the same kind of thing. Both are disks. Both receive BIOs. Both are completed with `biodone`. The difference is in where the bytes actually go.
|
||
|
||
This is the uniformity that `g_disk` provides. Your 驱动程序 can choose any backing technology, and as long as it fills in `struct disk` correctly, it looks like any other disk to the rest of the 内核.
|
||
|
||
### Takeaways from the Case Studies
|
||
|
||
Three patterns become clearer after reading these excerpts.
|
||
|
||
First, the strategy function is always a switch on `bio_cmd`. The cases vary, but the switch is always there. Memorise this pattern: incoming BIO -> switch -> case per command -> completion. It is the heart of every 存储驱动程序.
|
||
|
||
Second, `g_disk` 驱动程序 are structurally identical at the registration level. Whether the 驱动程序 is a RAM disk or a real SATA drive, the registration code looks the same. The differences are in what happens when the BIO arrives.
|
||
|
||
Third, more sophisticated 驱动程序 enqueue work to a dedicated thread. Our 驱动程序 does not, because it can do its work synchronously in any thread. Drivers that do slow or 块ing work must enqueue, because strategy functions run in the caller's thread context.
|
||
|
||
With these patterns in mind, you can now read almost any 存储驱动程序 in the FreeBSD tree and follow its overall structure, even if specific details about hardware or sub-框架 require further study.
|
||
|
||
We have now covered every concept the chapter set out to teach, plus observability, measurement, and a few real case studies. In the next part of the chapter, we will put this knowledge to work through hands-on labs. The labs build on the 驱动程序 you have been writing and on the skills you have been using, and they take you from the minimum working 驱动程序 through persistence, 挂载, and cleanup scenarios. Let us begin.
|
||
|
||
## 动手实验
|
||
|
||
Each lab is a self-contained checkpoint. They are designed to be done in order, but you can revisit any lab later if you want to practise a specific skill. Every lab has a companion folder under `examples/part-06/ch27-storage-vfs/`, which contains the reference implementation and the artifacts you would produce if you typed the code by hand.
|
||
|
||
Before you start, make sure you have the chapter's 驱动程序 building cleanly against your local 内核. From a fresh checkout of the examples tree:
|
||
|
||
```console
|
||
# cd examples/part-06/ch27-storage-vfs
|
||
# make
|
||
# ls myblk.ko
|
||
myblk.ko
|
||
```
|
||
|
||
If that works, you are ready. If not, revisit the Makefile and the advice in 第26章, section "Your Build Environment".
|
||
|
||
### Lab 1: Explore GEOM on a Running System
|
||
|
||
**Goal.** Build comfort with the GEOM inspection tools before you touch any code.
|
||
|
||
**What you do.**
|
||
|
||
On your FreeBSD 14.3 system, run the following commands and take notes in your lab logbook.
|
||
|
||
```console
|
||
# geom disk list
|
||
# geom part show
|
||
# geom -t | head -n 40
|
||
# gstat -I 1
|
||
# diskinfo -v /dev/ada0 # or whatever your primary disk is called
|
||
```
|
||
|
||
**What you look for.**
|
||
|
||
Identify every `DISK` class geom. For each, note its provider name, its media size, its 扇区 size, and its current mode. Notice which geoms have 分区ing layers on top and which do not. If your system has `geli` or `zfs`, notice the chain of classes.
|
||
|
||
**Stretch question.** Which of your geoms have non-zero access counts right now? Which ones are free? What would happen if you tried to run `newfs_ufs` on each?
|
||
|
||
**Reference implementation.** `examples/part-06/ch27-storage-vfs/lab01-explore-geom/README.md` contains a suggested walkthrough and a sample output transcript from a typical system.
|
||
|
||
### Lab 2: Build the Skeleton Driver
|
||
|
||
**Goal.** Get the Section 3 skeleton 驱动程序 compiling and loading on your system.
|
||
|
||
**What you do.**
|
||
|
||
Copy `examples/part-06/ch27-storage-vfs/lab02-skeleton/myfirst_blk.c` and its `Makefile` into a working directory. Build it.
|
||
|
||
```console
|
||
# cp -r examples/part-06/ch27-storage-vfs/lab02-skeleton /tmp/myblk
|
||
# cd /tmp/myblk
|
||
# make
|
||
```
|
||
|
||
Load the module.
|
||
|
||
```console
|
||
# kldload ./myblk.ko
|
||
# dmesg | tail -n 2
|
||
# ls /dev/myblk0
|
||
# geom disk list myblk0
|
||
```
|
||
|
||
Unload it.
|
||
|
||
```console
|
||
# kldunload myblk
|
||
# ls /dev/myblk0
|
||
```
|
||
|
||
**What you look for.**
|
||
|
||
Confirm that the 内核 printed your `myblk: loaded` message. Confirm that `/dev/myblk0` appeared. Confirm that `geom disk list` reported the expected media size. Confirm that the node disappeared after unload.
|
||
|
||
**Stretch question.** What happens if you try `newfs_ufs -N /dev/myblk0` with the skeleton 驱动程序? Can you read the output? Why does the dry run succeed even though real writes would fail?
|
||
|
||
### Lab 3: Implement the BIO Handler
|
||
|
||
**Goal.** Add the working strategy function from Section 5 to the skeleton 驱动程序.
|
||
|
||
**What you do.**
|
||
|
||
Starting from the skeleton, implement `myblk_strategy` with support for `BIO_READ`, `BIO_WRITE`, `BIO_DELETE`, and `BIO_FLUSH`. Allocate the backing 缓冲区 in `myblk_附加_unit` and free it in `myblk_分离_unit`.
|
||
|
||
Build, load, and test.
|
||
|
||
```console
|
||
# dd if=/dev/zero of=/dev/myblk0 bs=4096 count=16
|
||
# dd if=/dev/myblk0 of=/dev/null bs=4096 count=16
|
||
# dd if=/dev/random of=/dev/myblk0 bs=4096 count=16
|
||
# dd if=/dev/myblk0 of=/tmp/a bs=4096 count=16
|
||
# dd if=/dev/myblk0 of=/tmp/b bs=4096 count=16
|
||
# cmp /tmp/a /tmp/b
|
||
```
|
||
|
||
**What you look for.**
|
||
|
||
The last `cmp` must succeed with no output. If it prints `differ: byte N`, your strategy function is racing or returning stale data.
|
||
|
||
**Stretch question.** Put a `printf` in the strategy function that reports `bio_cmd`, `bio_offset`, and `bio_bcount`. Run `dd if=/dev/myblk0 of=/dev/null bs=1m count=1` and look at `dmesg`. What size did `dd` actually issue? Do you see fragmentation?
|
||
|
||
**Reference implementation.** `examples/part-06/ch27-storage-vfs/lab03-bio-handler/myfirst_blk.c`.
|
||
|
||
### Lab 4: Increase Size and Mount UFS
|
||
|
||
**Goal.** Increase the backing store to 32 MiB and 挂载 UFS on the 设备.
|
||
|
||
**What you do.**
|
||
|
||
Change `MYBLK_MEDIASIZE` to `(32 * 1024 * 1024)` and rebuild. Load the module. Format and 挂载.
|
||
|
||
```console
|
||
# newfs_ufs /dev/myblk0
|
||
# mkdir -p /mnt/myblk
|
||
# mount /dev/myblk0 /mnt/myblk
|
||
# echo "hello" > /mnt/myblk/greeting.txt
|
||
# cat /mnt/myblk/greeting.txt
|
||
# umount /mnt/myblk
|
||
# mount /dev/myblk0 /mnt/myblk
|
||
# cat /mnt/myblk/greeting.txt
|
||
# umount /mnt/myblk
|
||
# kldunload myblk
|
||
```
|
||
|
||
**What you look for.**
|
||
|
||
Verify that the file survives an 卸载-and-re挂载. Verify that the access counts in `geom disk list` are zero after 卸载. Verify that `kldunload` succeeds cleanly.
|
||
|
||
**Stretch question.** Watch `gstat -I 1` while running `dd if=/dev/zero of=/mnt/myblk/big bs=1m count=16`. Can you see the writes arrive in bursts? What size are the individual BIOs? Hint: UFS's default 块 size is typically 32 KiB on a 文件系统 this small.
|
||
|
||
**Reference implementation.** `examples/part-06/ch27-storage-vfs/lab04-挂载-ufs/myfirst_blk.c`.
|
||
|
||
### Lab 5: Observing Real Cross-Reload Persistence with md(4)
|
||
|
||
**Goal.** Confirm experimentally that cross-reload persistence requires external backing, as Section 7 argued, by using `md(4)`'s vnode mode as a control.
|
||
|
||
**What you do.**
|
||
|
||
First, demonstrate that our RAM-backed `myblk` loses its 文件系统 on reload. Load, format, 挂载, write, 卸载, unload, reload, 挂载 again, and observe the empty 文件系统.
|
||
|
||
```console
|
||
# kldload ./myblk.ko
|
||
# newfs_ufs /dev/myblk0
|
||
# mount /dev/myblk0 /mnt/myblk
|
||
# echo "not persistent" > /mnt/myblk/token.txt
|
||
# umount /mnt/myblk
|
||
# kldunload myblk
|
||
# kldload ./myblk.ko
|
||
# mount /dev/myblk0 /mnt/myblk
|
||
# ls /mnt/myblk
|
||
```
|
||
|
||
The `ls` should show an empty or fresh UFS directory; the `token.txt` is gone because the backing 缓冲区 was reclaimed by the 内核 when the module unloaded.
|
||
|
||
Now do the same sequence with `md(4)`'s vnode backend, which uses a real file on disk:
|
||
|
||
```console
|
||
# truncate -s 64m /var/tmp/mdimage.img
|
||
# mdconfig -a -t vnode -f /var/tmp/mdimage.img -u 9
|
||
# newfs_ufs /dev/md9
|
||
# mount /dev/md9 /mnt/md
|
||
# echo "persistent" > /mnt/md/token.txt
|
||
# umount /mnt/md
|
||
# mdconfig -d -u 9
|
||
# mdconfig -a -t vnode -f /var/tmp/mdimage.img -u 9
|
||
# mount /dev/md9 /mnt/md
|
||
# cat /mnt/md/token.txt
|
||
persistent
|
||
```
|
||
|
||
**What you look for.**
|
||
|
||
The first sequence loses the file; the second preserves it. The difference is that `md9` is backed by a real file on disk, whose state survives regardless of what happens inside the 内核. Contrast this with `myblk0`, which is backed by 内核 heap that disappears on `kldunload`.
|
||
|
||
**Stretch question.** Read the `MD_VNODE` branch of `mdstart_vnode` in `/usr/src/sys/dev/md/md.c`. Identify where the vnode reference is stored (hint: it lives on the per-unit `struct md_s`, not a module-scope global). Explain in your own words why that design is what lets the backing survive module lifecycles.
|
||
|
||
**Reference implementation.** `examples/part-06/ch27-storage-vfs/lab05-persistence/README.md` walks through both sequences and their diagnostic output.
|
||
|
||
### Lab 6: Safe Un挂载 Under Load
|
||
|
||
**Goal.** Verify that the teardown path handles an active 文件系统 correctly.
|
||
|
||
**What you do.**
|
||
|
||
Load the module, format it, 挂载 it. In one terminal, start a stress loop.
|
||
|
||
```console
|
||
# while true; do dd if=/dev/urandom of=/mnt/myblk/stress bs=4k \
|
||
count=512 2>/dev/null; sync; done
|
||
```
|
||
|
||
In another terminal, attempt to unload.
|
||
|
||
```console
|
||
# kldunload myblk
|
||
kldunload: can't unload file: Device busy
|
||
```
|
||
|
||
Stop the stress loop. Un挂载. Unload.
|
||
|
||
**What you look for.**
|
||
|
||
The initial unload must fail gracefully. After 卸载, the final unload must succeed. `dmesg` must not show any 内核 warnings.
|
||
|
||
**Stretch question.** Instead of killing the stress loop, try `u挂载 /mnt/myblk` directly. Does UFS let you 卸载 while writes are in flight? What is the error, and what does it mean?
|
||
|
||
**Reference implementation.** `examples/part-06/ch27-storage-vfs/lab06-safe-卸载/` includes a test script that performs the sequence above and reports failures.
|
||
|
||
### Lab 7: Observing BIO Traffic with DTrace
|
||
|
||
**Goal.** Use DTrace to see the BIO path as it happens.
|
||
|
||
**What you do.**
|
||
|
||
With the 驱动程序 loaded and a 文件系统 挂载ed, run the following DTrace one-liner in one terminal:
|
||
|
||
```console
|
||
# dtrace -n 'fbt::myblk_strategy:entry { \
|
||
printf("cmd=%d off=%lld len=%u", \
|
||
args[0]->bio_cmd, args[0]->bio_offset, \
|
||
args[0]->bio_bcount); \
|
||
@count[args[0]->bio_cmd] = count(); \
|
||
}'
|
||
```
|
||
|
||
In another terminal, create and read files on the 挂载ed 文件系统.
|
||
|
||
**What you look for.**
|
||
|
||
Note which BIO commands you see and in what quantities. Note the typical offsets and lengths. Compare the patterns from `dd` traffic versus `cp` traffic versus `tar` traffic. Note how `cp` or `mv` might produce very different BIO patterns depending on what the 缓冲区 cache decides to flush.
|
||
|
||
**Stretch question.** Issue `sync` while the DTrace is running. What BIO commands does `sync` cause? What about `newfs_ufs`?
|
||
|
||
**Reference implementation.** `examples/part-06/ch27-storage-vfs/lab07-dtrace/README.md` with sample DTrace output and notes.
|
||
|
||
### Lab 8: Adding a getattr Attribute
|
||
|
||
**Goal.** Implement a `d_getattr` 回调 that responds to `GEOM::ident`.
|
||
|
||
**What you do.**
|
||
|
||
Add the `myblk_getattr` function from Section 5 to the 驱动程序, and 注册 it on the disk before `disk_create`. Rebuild, reload, and check `diskinfo -v /dev/myblk0`.
|
||
|
||
**What you look for.**
|
||
|
||
The `ident` field should now show `MYBLK0` instead of `(null)`.
|
||
|
||
**Stretch question.** What other attributes might a 文件系统 query? Look at `/usr/src/sys/geom/geom.h` for named attributes like `GEOM::rotation_rate`. Try implementing that too.
|
||
|
||
**Reference implementation.** `examples/part-06/ch27-storage-vfs/lab08-getattr/myfirst_blk.c`.
|
||
|
||
### Lab 9: Exploring md(4) for Comparison
|
||
|
||
**Goal.** Read a real FreeBSD 存储驱动程序 and identify the patterns we have used.
|
||
|
||
**What you do.**
|
||
|
||
Open `/usr/src/sys/dev/md/md.c`. It is a long file. Do not try to read every line. Instead, find and understand these specific things:
|
||
|
||
1. The `g_md_class` structure at the top of the file.
|
||
2. The `struct md_s` softc.
|
||
3. The `mdstart_malloc` function that handles BIO_READ and BIO_WRITE for `MD_MALLOC` memory disks.
|
||
4. The worker-thread pattern in `md_kthread` (or its equivalent in your version).
|
||
5. The `MDIOCATTACH` ioctl handler that creates new units on demand.
|
||
|
||
Compare each of these to the corresponding code in our 驱动程序.
|
||
|
||
**What you look for.**
|
||
|
||
Spot the differences. Where does `md(4)` have features we do not? Where does our 驱动程序 have the same mechanism in simpler form? Where would you need to extend our 驱动程序 to add one of `md(4)`'s features?
|
||
|
||
**Reference notes.** `examples/part-06/ch27-storage-vfs/lab09-md-comparison/NOTES.md` contains a mapped walkthrough of the relevant sections of `md.c` for FreeBSD 14.3.
|
||
|
||
### Lab 10: Break It On Purpose
|
||
|
||
**Goal.** Induce known failure modes so that you can recognise them quickly in real work.
|
||
|
||
**What you do.**
|
||
|
||
Take a clean copy of the completed 驱动程序 from Lab 8. In separate copies (do not mix the breakages together), introduce the following bugs one at a time, rebuild, load, and observe.
|
||
|
||
**Breakage 1: Forget biodone.** Comment out the `biodone(bp)` call in the `BIO_READ` case. Load, 挂载, and run `cat` on a file. The `cat` will hang forever. Attempt to kill it with `Ctrl-C`; it may not respond. Use `procstat -kk` on the stuck PID to see where the process is waiting. This is the classic leaked-BIO symptom.
|
||
|
||
**Breakage 2: Free backing before disk_destroy.** In `myblk_分离_unit`, swap the order so that `free(sc->backing, ...)` comes before `disk_destroy(sc->disk)`. Load, format, 挂载, 卸载, and try to unload. If no BIO is in flight during the unload window, you will escape unharmed. If any BIO is in flight (use a running `dd` to ensure this), you will panic with a page fault.
|
||
|
||
**Breakage 3: Skip bio_resid.** Remove the `bp->bio_resid = 0` line from the `BIO_READ` case. Load, format, 挂载, and create a file. Read it back. Depending on what garbage was in `bio_resid` at allocation time, the 文件系统 may report incorrect read sizes and may log errors. Sometimes it works; sometimes it does not. This is the characteristic intermittent failure of a forgotten `bio_resid`.
|
||
|
||
**Breakage 4: Off-by-one bounds.** Change the bounds check from `offset > sc->backing_size` to `offset >= sc->backing_size`. This rejects valid reads at the last offset. Load, format, 挂载. Try to write a file that extends to the very last 块. Observe whether UFS notices; whether `dd` notices; what error is reported.
|
||
|
||
**What you look for.**
|
||
|
||
In each case, describe in your logbook what you observed, which tool revealed the problem (dmesg, `procstat`, `gstat`, panic trace), and what the fix would be. Then apply the fix and confirm normal operation.
|
||
|
||
**Stretch question.** What sequence of commands reliably reproduces each failure? Can you write a shell script that deterministically triggers Breakage 1 or Breakage 2?
|
||
|
||
**Reference notes.** `examples/part-06/ch27-storage-vfs/lab10-break-on-purpose/BREAKAGES.md` contains short descriptions and a test script for each failure mode.
|
||
|
||
### Lab 11: Measure Under Varying Block Sizes
|
||
|
||
**Goal.** Understand how BIO size affects throughput.
|
||
|
||
**What you do.**
|
||
|
||
With the 驱动程序 loaded and a 文件系统 挂载ed, run `dd` with progressively larger 块 sizes and time each run.
|
||
|
||
```console
|
||
# for bs in 512 4096 32768 131072 524288 1048576; do
|
||
rm -f /mnt/myblk/bench
|
||
time dd if=/dev/zero of=/mnt/myblk/bench bs=$bs count=$((16*1024*1024/bs))
|
||
done
|
||
```
|
||
|
||
Record the throughput in each case.
|
||
|
||
**What you look for.**
|
||
|
||
Throughput should increase as 块 size increases, then plateau at or near `d_maxsize` (typically 128 KiB). Very small 块 sizes will be dominated by per-BIO overhead.
|
||
|
||
**Stretch question.** At what 块 size does the curve visibly plateau? Why?
|
||
|
||
### Lab 12: Race Test With Two Processes
|
||
|
||
**Goal.** Observe how the 驱动程序 handles simultaneous access from multiple processes.
|
||
|
||
**What you do.**
|
||
|
||
With the 驱动程序 loaded and a 文件系统 挂载ed, run two `dd` processes in parallel writing to different files.
|
||
|
||
```console
|
||
# dd if=/dev/urandom of=/mnt/myblk/a bs=4k count=1024 &
|
||
# dd if=/dev/urandom of=/mnt/myblk/b bs=4k count=1024 &
|
||
# wait
|
||
```
|
||
|
||
Record the combined throughput.
|
||
|
||
**What you look for.**
|
||
|
||
Both writes should complete without corruption. Verify with `md5` or `sha256` on each file. The combined throughput may be slightly less than twice the single-process throughput because of lock contention in our coarse 互斥锁.
|
||
|
||
**Stretch question.** Does removing the 互斥锁 affect throughput? Does it cause corruption? Why or why not?
|
||
|
||
### A Word on Lab Discipline
|
||
|
||
Every lab is small, and none of them is an exam. If you get stuck, the reference implementation is there for you to compare against. Do not copy-paste it as your first attempt, though. The copying is not the skill. The skill is in typing, reading, diagnosing, and verifying.
|
||
|
||
Keep your logbook open. Record what you ran, what you saw, and what surprised you. Storage bugs often repeat across projects, and your future self will thank your current self for the notes.
|
||
|
||
## 挑战练习
|
||
|
||
The challenge exercises stretch the 驱动程序 a little further. Each one is scoped to something a beginner can accomplish with the material already covered in the chapter, combined with a thoughtful reading of FreeBSD source. They are not timed. Take your time. Open the source tree. Consult manual pages. Compare your solution to `md(4)` when in doubt.
|
||
|
||
Every challenge below has a stub folder under `examples/part-06/ch27-storage-vfs/`, but no reference solution is provided. The point is to work through them yourself. Solutions are left as follow-ups that you can compare with peers or post in your study notes.
|
||
|
||
### Challenge 1: Expose a Read-Only Mode
|
||
|
||
Add a module-load tunable that lets the 驱动程序 come up in read-only mode. In read-only mode, `BIO_WRITE` and `BIO_DELETE` should fail with `EROFS`. `newfs_ufs` should refuse to format the 设备, and `挂载` without `-r` should refuse to 挂载 it.
|
||
|
||
Hint. The tunable can be a `sysctl_int` bound to a static variable. `TUNABLE_INT` is another way, used at load time only. Your strategy function can check the variable before dispatching writes. Remember that changing the mode at runtime while a 文件系统 is 挂载ed is a recipe for corruption; you can either disallow the change or document that the tunable only takes effect at module load.
|
||
|
||
### Challenge 2: Implement a Second Unit
|
||
|
||
Add support for exactly two units: `myblk0` and `myblk1`. Each should have its own backing store of its own size. Do not try to implement fully dynamic unit allocation; just hardcode two softcs and two 附加 calls in the module loader.
|
||
|
||
Hint. Move the backing allocation, disk allocation, and disk creation into `myblk_附加_unit` parameterised by unit number and size, and call it twice from the loader. Make sure the 分离 path walks both units.
|
||
|
||
### Challenge 3: Honour BIO_DELETE with a Sysctl Counter
|
||
|
||
Extend `BIO_DELETE` handling to also bump a `sysctl` counter that reports total bytes deleted. Verify with `sysctl dev.myblk` while running `fstrim /mnt/myblk` or while `dd` writes and overwrites files.
|
||
|
||
Hint. UFS by default does not issue `BIO_DELETE`. To see delete traffic, 挂载 with `-o trim`. You can verify the trim flow with your DTrace one-liner from Lab 7.
|
||
|
||
### Challenge 4: Respond to BIO_GETATTR for rotation_rate
|
||
|
||
Extend `myblk_getattr` to answer `GEOM::rotation_rate` with `DISK_RR_NON_ROTATING` (defined in `/usr/src/sys/geom/geom_disk.h`). Verify with `gpart show` and `diskinfo -v` that the 设备 reports as non-rotating.
|
||
|
||
Hint. The attribute is returned as a plain `u_int`. Look at how `md(4)` handles `BIO_GETATTR` for comparable attributes.
|
||
|
||
### Challenge 5: Resizing the Device
|
||
|
||
Add an ioctl that lets user space resize the backing store while nothing is 挂载ed. If a 文件系统 is 挂载ed, the ioctl must fail with `EBUSY`. If the resize succeeds, update `d_mediasize` and notify GEOM so that `diskinfo` reports the new size.
|
||
|
||
Hint. Look at `md(4)`'s `MDIOCRESIZE` handling for the pattern. This is a non-trivial challenge; take your time and test with throwaway 文件系统. Do not attempt this on any backing that you would be sad to lose.
|
||
|
||
### Challenge 6: A Write Counter and Rate Display
|
||
|
||
Add per-second write-byte counters, exposed via `sysctl`, and a small user-space shell script that reads the sysctl every second and prints a human-readable rate. This is useful for testing and it gives you experience wiring metrics through the 内核's observability machinery.
|
||
|
||
Hint. Use `atomic_add_long` on the counters. The shell script is a one-liner in `while true` loops.
|
||
|
||
### Challenge 7: A Fixed Pattern Backing Store
|
||
|
||
Implement a backing-store mode where reads always return a fixed byte pattern and writes are silently discarded. This is similar to `g_zero` but with a configurable pattern byte. It is useful for stress-testing the layers above when you do not care about data content.
|
||
|
||
Hint. Branch on a mode variable inside the strategy function. Keep the in-memory backing for the normal mode and skip the `memcpy` in pattern mode.
|
||
|
||
### Challenge 8: Write a mdconfig-Like Control Utility
|
||
|
||
Write a small user-space program that talks to a control 设备 on your 驱动程序 (you will need to add one) and can create, destroy, and query units at runtime. The program should accept command-line flags similar to `mdconfig`.
|
||
|
||
Hint. This is a substantial challenge. Start with a single ioctl that prints "hello" and build up from there. `make_dev` on a cdev for your control 设备, then implement `d_ioctl` on that cdev.
|
||
|
||
### Challenge 9: Survive a Simulated Crash
|
||
|
||
Add a mode where the 驱动程序 drops every Nth write silently (pretending the write succeeded but actually doing nothing). Use this to test UFS's resilience to lost writes.
|
||
|
||
Hint. This is a dangerous mode. Only run it on throwaway 文件系统. You should be able to reproduce interesting `fsck_ffs` repair scenarios with it. Be ready to explain to yourself why this mode is only safe on pseudo 设备 you can regenerate from scratch.
|
||
|
||
### Challenge 10: Understand md(4) Well Enough to Teach It
|
||
|
||
Write a one-page explanation of how `md(4)` creates a new unit in response to `MDIOCATTACH`. Cover the ioctl path, the softc allocation, the backing-type-specific initialisation, the `g_disk` wiring, and the worker thread creation. This is a reading challenge rather than a coding challenge, but it is one of the most useful exercises you can do to deepen your grasp of the storage stack.
|
||
|
||
Hint. `/usr/src/sys/dev/md/md.c` and `/usr/src/sbin/mdconfig/mdconfig.c` are the two files to read. Pay attention to the `struct md_ioctl` structure in `/usr/src/sys/sys/mdioctl.h`, because that is the ABI between user space and the 内核.
|
||
|
||
### When to Attempt Challenges
|
||
|
||
You do not need to do all of them. Pick one or two that speak to something you are curious about or something you can imagine using later. A single carefully-done challenge is worth more than five half-done ones. The reference `md(4)` implementation will be there whenever you want to compare your approach to a production 驱动程序.
|
||
|
||
## Troubleshooting
|
||
|
||
Storage 驱动程序 have a particular family of failure modes. Some of them are obvious as they happen. Others are silent at first and become obvious only after reboot, sometimes with data corruption in between. This section lists the symptoms you are most likely to see while working through the chapter and the labs, along with the usual causes and fixes. Use it as a reference when things go wrong, and read it through at least once before you start, because it is much easier to recognise a failure mode the second time.
|
||
|
||
### `kldload` Succeeds but No /dev Node Appears
|
||
|
||
**Symptom.** `kldload` returns zero. `kldstat` shows the module loaded. But `/dev/myblk0` does not exist.
|
||
|
||
**Likely causes.**
|
||
|
||
- You forgot to call `disk_create`. The softc is allocated, the disk is allocated, but the disk is not 注册ed with GEOM.
|
||
- You called `disk_create` with `d_name` set to a null pointer or an empty string.
|
||
- You called `disk_create` with `d_mediasize` set to zero. `g_disk` silently refuses to create a provider with zero size.
|
||
- You called `disk_create` before filling in the fields. The 框架 captures the field values at registration time and does not re-read them later.
|
||
|
||
**Fix.** Check the 内核 message 缓冲区 with `dmesg`. `g_disk` prints a diagnostic when it rejects a registration. Fix the field value and rebuild.
|
||
|
||
### `kldload` Fails with "module version mismatch"
|
||
|
||
**Symptom.** Loading the module reports `kldload: can't load ./myblk.ko: No such file or directory` or a more explicit error about version mismatch.
|
||
|
||
**Likely causes.**
|
||
|
||
- You compiled against a different 内核 than the one currently running.
|
||
- You changed `DISK_VERSION` on your own, which you should never do.
|
||
- You forgot `MODULE_VERSION(myblk, 1)`.
|
||
|
||
**Fix.** Check `uname -a` and the 内核 version your build chose. Recompile against the running 内核.
|
||
|
||
### `diskinfo` Prints the Wrong Size
|
||
|
||
**Symptom.** `diskinfo -v /dev/myblk0` prints a size that does not match `MYBLK_MEDIASIZE`.
|
||
|
||
**Likely causes.**
|
||
|
||
- You set `d_mediasize` to the wrong expression. A common off-by-one is setting it to the 扇区 count rather than the byte count.
|
||
- You have `MYBLK_MEDIASIZE` defined as something other than `(size * 1024 * 1024)` and the macro is being interpreted differently than you intended. Parenthesise aggressively.
|
||
|
||
**Fix.** Print the size in your load message and sanity-check against `diskinfo -v`.
|
||
|
||
### `newfs_ufs` Fails with "Device not configured"
|
||
|
||
**Symptom.** `newfs_ufs /dev/myblk0` prints `newfs: /dev/myblk0: Device not configured`.
|
||
|
||
**Likely causes.**
|
||
|
||
- Your strategy function is still the placeholder that returns `ENXIO` for everything. `ENXIO` is mapped to the `Device not configured` message by `errno`.
|
||
|
||
**Fix.** Implement the strategy function from Section 5.
|
||
|
||
### `newfs_ufs` Hangs
|
||
|
||
**Symptom.** `newfs_ufs /dev/myblk0` starts up but never completes.
|
||
|
||
**Likely causes.**
|
||
|
||
- Your strategy function does not call `biodone` on some path. `newfs_ufs` issues a BIO, waits for its completion, and will wait forever if completion never comes.
|
||
- Your strategy function calls `biodone` twice on some path. The first call returns success; the second call usually panics, but in some cases the BIO state is corrupted enough to hang.
|
||
|
||
**Fix.** Audit your strategy function. Every control-flow path must end with exactly one call to `biodone(bp)`. A useful pattern is to use a single exit point at the end of the function.
|
||
|
||
### `挂载` Fails with "bad super块"
|
||
|
||
**Symptom.** `挂载 /dev/myblk0 /mnt/myblk` reports `挂载: /dev/myblk0: bad magic`.
|
||
|
||
**Likely causes.**
|
||
|
||
- Your strategy function is returning wrong data for some offsets. The super块 is at offset 65536, and UFS validates it carefully.
|
||
- Your bounds check is rejecting a legitimate read.
|
||
- Your `memcpy` is copying from the wrong address (usually an off-by-one in the offset arithmetic).
|
||
|
||
**Fix.** Write a known pattern to the 设备 with `dd`, then read it back with `dd` at various offsets and compare with `cmp`. If the pattern round-trips, the basic I/O is correct. If not, find the first offset where it diverges and inspect the code at the corresponding bounds check or address arithmetic.
|
||
|
||
### `kldunload` Hangs
|
||
|
||
**Symptom.** `kldunload myblk` does not return.
|
||
|
||
**Likely causes.**
|
||
|
||
- A BIO is in flight and your strategy function never calls `biodone`. `disk_destroy` is waiting for the BIO to complete.
|
||
- You added a worker thread and it is sleeping inside a function that will never be awakened.
|
||
|
||
**Fix.** Run `procstat -kk` in another terminal. Look at the `g_event` thread's stack and at any of your 驱动程序's threads. If they are stuck in a `sleep` or `waitfor` state, you have a leaked BIO or a misbehaving worker.
|
||
|
||
### `kldunload` Returns "Device 总线y"
|
||
|
||
**Symptom.** `kldunload myblk` reports `Device 总线y` and exits.
|
||
|
||
**Likely causes.**
|
||
|
||
- A 文件系统 is still 挂载ed on `/dev/myblk0`.
|
||
- A program still has `/dev/myblk0` open for raw access.
|
||
- A `dd` from a previous terminal session is still running in the background.
|
||
|
||
**Fix.** Run `挂载 | grep myblk` to check for active 挂载s. Run `fuser /dev/myblk0` to find open handles. Un挂载, close, then unload.
|
||
|
||
### Kernel Panic with "freeing free memory"
|
||
|
||
**Symptom.** The 内核 panics with a message about freeing already-freed memory, showing a stack trace through your 驱动程序.
|
||
|
||
**Likely causes.**
|
||
|
||
- The 分离 path is freeing the softc or the backing twice.
|
||
- A worker thread survived `disk_destroy` and tried to access freed state.
|
||
|
||
**Fix.** Review the 分离 order. Destroy the disk first (which waits for in-flight BIOs), then free the backing, then destroy the 互斥锁, then free the softc. If you added a worker thread, make sure it has exited before any `free` is called.
|
||
|
||
### Kernel Panic with "vm_fault: 内核 mode"
|
||
|
||
**Symptom.** The 内核 panics with a page fault inside your 驱动程序, typically in the strategy function or in the 分离 path.
|
||
|
||
**Likely causes.**
|
||
|
||
- You dereferenced a null or freed pointer. The most common case is using `sc->backing` after it has been freed.
|
||
- You confused `bp->bio_data` with `bp->bio_disk` and read from the wrong pointer.
|
||
|
||
**Fix.** Audit pointer lifetimes. If the backing store is freed during 分离, ensure no BIOs can still reach the strategy function after that point. The order `disk_destroy` -> `free(backing)` is the correct order.
|
||
|
||
### `gstat` Shows No Activity
|
||
|
||
**Symptom.** You are running `dd` or `newfs_ufs` against the 设备, but `gstat -f myblk0` shows zero ops/s.
|
||
|
||
**Likely causes.**
|
||
|
||
- You are watching the wrong 设备. `gstat -f myblk0` uses a regex; make sure your 设备 name matches.
|
||
- Your 驱动程序 uses a custom GEOM class name that `gstat` is filtering out.
|
||
|
||
**Fix.** Run `gstat` without the filter and look for your 设备. Check the name field carefully.
|
||
|
||
### "Operation not supported" for DELETE
|
||
|
||
**Symptom.** Mount-with-trim fails or `fstrim` prints "Operation not supported".
|
||
|
||
**Likely causes.**
|
||
|
||
- Your strategy function does not handle `BIO_DELETE` and returns `EOPNOTSUPP`.
|
||
- The 文件系统 探测d `BIO_DELETE` support during 挂载 and cached the negative result.
|
||
|
||
**Fix.** Implement `BIO_DELETE` in the strategy function, then 卸载 and re挂载. Most 文件系统 only 探测 at 挂载 time.
|
||
|
||
### /dev/myblk0 Does Not Appear Until Several Seconds After kldload
|
||
|
||
**Symptom.** Immediately after `kldload`, `ls /dev/myblk0` fails. A few moments later, it succeeds.
|
||
|
||
**Likely causes.**
|
||
|
||
- GEOM processes events asynchronously. `disk_create` queues an event, and the provider is not published until the event thread picks it up.
|
||
- On a system under load, the event queue may be slow.
|
||
|
||
**Fix.** This is normal behaviour. If your scripts depend on the node existing immediately after `kldload`, add a small sleep or a polling loop.
|
||
|
||
### Data Written Is Readable but Garbled
|
||
|
||
**Symptom.** A read after a write returns the right number of bytes but with different content.
|
||
|
||
**Likely causes.**
|
||
|
||
- Off-by-one in the backing-store offset arithmetic.
|
||
- A concurrent BIO is overlapping with the one you expect, and your lock is not held for long enough.
|
||
- The strategy function is reading from `bp->bio_data` before the 内核 has finished setting it up (this is extremely unlikely for normal BIOs, but can happen with bugs in how you parse attributes).
|
||
|
||
**Fix.** Add a `printf` to the strategy function that logs the first few bytes before and after the `memcpy`. Repeat the test with a known pattern and look for the mismatch.
|
||
|
||
### Backing Store Not Freed, Memory Grows With Every Reload
|
||
|
||
**Symptom.** `vmstat -m | grep myblk` shows the allocation bytes growing with each load/unload cycle.
|
||
|
||
**Likely causes.**
|
||
|
||
- The `MOD_UNLOAD` handler returned without calling `myblk_分离_unit`, so the `free(sc->backing, M_MYBLK)` was skipped.
|
||
- An error path in `MOD_UNLOAD` returned early before reaching the free. Every error path needs to free or the allocation leaks.
|
||
- A worker thread is holding a reference to the softc and the handler refuses to free while that reference exists.
|
||
|
||
**Fix.** Audit the `MOD_UNLOAD` path. `vmstat -m` is a blunt but effective tool. Add a `printf` in the free path to confirm it is being reached.
|
||
|
||
### `gstat` Shows Very High Queue Length
|
||
|
||
**Symptom.** `gstat -I 1` shows `L(q)` rising into the tens or hundreds and never returning to zero.
|
||
|
||
**Likely causes.**
|
||
|
||
- Your strategy function is slow or 块ing, causing BIOs to queue faster than they are serviced.
|
||
- You added a worker thread but it is scheduled less often than it should be.
|
||
- A synchronisation bottleneck (a heavily contested 互斥锁) is serialising the work.
|
||
|
||
**Fix.** Profile with DTrace to find what the strategy function is doing. If the latency per BIO has grown, investigate why. For an in-memory 驱动程序, this should almost never happen; if it does, you have likely introduced a `vn_rdwr` or other 块ing call into the hot path.
|
||
|
||
### Strategy Function Called With NULL bio_disk
|
||
|
||
**Symptom.** Kernel panic in the strategy function when dereferencing `bp->bio_disk->d_drv1`.
|
||
|
||
**Likely causes.**
|
||
|
||
- The BIO was synthesised incorrectly by code outside your 驱动程序.
|
||
- You are accessing `bp->bio_disk` from the wrong context. In some GEOM paths, `bp->bio_disk` is only valid inside the strategy function of a `g_disk` 驱动程序.
|
||
|
||
**Fix.** If you need to access the softc, do it at the start of the strategy function. Cache the pointer in a local variable. Do not access `bp->bio_disk` from a different thread or from a deferred 回调.
|
||
|
||
### Mysterious I/O Errors After Reload
|
||
|
||
**Symptom.** After `kldunload` and `kldload`, reads return EIO on offsets that worked before the unload.
|
||
|
||
**Likely causes.**
|
||
|
||
- You are working with a file-backed or vnode-backed experiment (from Lab 5 or from your own modifications) and the file's size or contents have been changed between loads.
|
||
- A type mismatch between the saved and new offsets (for instance, changing `d_扇区ize` between loads).
|
||
- `d_mediasize` has changed but the underlying file still reflects the old layout.
|
||
|
||
**Fix.** Ensure the backing file and the 驱动程序's geometry agree on both size and 扇区 layout. If you change `d_mediasize` or `d_扇区ize`, regenerate the backing file to match. For a straightforward reload with no changes, the 缓冲区 on a RAM-backed 驱动程序 is always fresh, so mysterious post-reload EIOs usually point to a geometry mismatch rather than data loss.
|
||
|
||
### Access Count Stuck at Non-Zero After Un挂载
|
||
|
||
**Symptom.** After `u挂载`, `geom disk list` still shows non-zero access counts.
|
||
|
||
**Likely causes.**
|
||
|
||
- A program still has the raw 设备 open. `fuser /dev/myblk0` will reveal it.
|
||
- The 文件系统 did not 卸载 cleanly. Check `挂载 | grep myblk` to see if it is still 挂载ed.
|
||
- A lingering NFS client or similar is holding the 文件系统 open. Unlikely for a local memory disk, but possible on shared systems.
|
||
|
||
**Fix.** Find and close the open handle. If `u挂载` reports success but the access count remains, rebooting is the safest recovery.
|
||
|
||
### Driver Loaded But Not Visible in geom -t
|
||
|
||
**Symptom.** `kldstat` shows the module loaded, but `geom -t` does not show any geom of our name.
|
||
|
||
**Likely causes.**
|
||
|
||
- The loader ran but never called `disk_create`.
|
||
- `disk_create` was called but the event thread has not run yet.
|
||
|
||
**Fix.** Add a `printf` to confirm `disk_create` ran. Wait one or two seconds after `kldload` before checking, to give the event thread a chance.
|
||
|
||
### Panic on Second Load
|
||
|
||
**Symptom.** Loading the module once works. Unloading works. Loading a second time panics.
|
||
|
||
**Likely causes.**
|
||
|
||
- A `MOD_UNLOAD` handler did not reset all the state that `MOD_LOAD` assumes is fresh.
|
||
- A static pointer holds a reference to a freed structure across the unload boundary; the next load sees a dangling pointer.
|
||
- A GEOM class that was 注册ed on first load did not un注册.
|
||
|
||
**Fix.** Audit your load and unload paths as a matched pair. Every allocation on load needs a corresponding free on unload, and every pointer written on load needs to be cleared on unload. For GEOM classes, `DECLARE_GEOM_CLASS` handles the unregistration for you, but if you bypass it you must do the work.
|
||
|
||
### newfs_ufs Aborts with "File system too small"
|
||
|
||
**Symptom.** `newfs_ufs /dev/myblk0` aborts with `newfs: /dev/myblk0: 分区 smaller than minimum UFS size`.
|
||
|
||
**Likely causes.**
|
||
|
||
- `MYBLK_MEDIASIZE` is too small for UFS's minimum practical size.
|
||
- You forgot to rebuild the module after changing the size.
|
||
|
||
**Fix.** Ensure the media size is at least a few megabytes. UFS's absolute minimum is around 1 MiB, but practical minimums are 4-8 MiB and comfortable minimums are 32 MiB or more.
|
||
|
||
### 挂载 -o trim Does Not Trigger BIO_DELETE
|
||
|
||
**Symptom.** Mounting with `-o trim` succeeds, but `gstat` shows no delete operations even during heavy deletion.
|
||
|
||
**Likely causes.**
|
||
|
||
- UFS issues `BIO_DELETE` only on certain patterns; it does not unconditionally trim every freed 块.
|
||
- Your 驱动程序 does not advertise `BIO_DELETE` support in its `d_flags`.
|
||
|
||
**Fix.** Set `sc->disk->d_flags |= DISKFLAG_CANDELETE;` before `disk_create`. This tells GEOM and 文件系统 that your 驱动程序 supports `BIO_DELETE` and is willing to handle them.
|
||
|
||
### UFS Complains About "Fragment out of bounds"
|
||
|
||
**Symptom.** After a 挂载, UFS logs an error about a fragment being out of bounds, and file operations start returning EIO.
|
||
|
||
**Likely causes.**
|
||
|
||
- Your 驱动程序 is returning wrong data on some offset, and UFS has read a corrupted metadata 块.
|
||
- The backing store was partially overwritten during some other test.
|
||
- Bounds-check arithmetic is returning incorrect ranges.
|
||
|
||
**Fix.** Un挂载, run `fsck_ffs -y /dev/myblk0` to repair, then re-test. If the error recurs with fresh 文件系统, look for offset-computation bugs in the strategy function.
|
||
|
||
### Kernel Printing "interrupt storm" Messages
|
||
|
||
**Symptom.** `dmesg` shows messages about interrupt storms, and system responsiveness degrades.
|
||
|
||
**Likely causes.**
|
||
|
||
- A real hardware 驱动程序 (not yours) is misbehaving.
|
||
- Your 驱动程序 is fine; this is a different subsystem's problem.
|
||
|
||
**Fix.** Verify that the storm is not related to your module. If it is, the issue is almost certainly in an 中断处理程序, which our pseudo 驱动程序 does not have.
|
||
|
||
### Reboot Hangs on Un挂载 During Shutdown
|
||
|
||
**Symptom.** On shutdown, the system hangs while 卸载ing, with a message like "Syncing disks, vnodes remaining...".
|
||
|
||
**Likely causes.**
|
||
|
||
- A 文件系统 is still 挂载ed on your 设备, and your 驱动程序 is holding a BIO.
|
||
- A syncer thread is stuck waiting for completion.
|
||
|
||
**Fix.** Ensure your 驱动程序 卸载s cleanly before system shutdown. A ro总线t way is to add a `shutdown_post_sync` event handler that 卸载s the 文件系统 and unloads the module. For development, 卸载 and unload manually before issuing `shutdown -r now`.
|
||
|
||
### General Advice
|
||
|
||
Whenever something goes wrong, the first step is to read `dmesg` and look for messages from your own printfs and from 内核 subsystems. The second step is to run `procstat -kk` and look at what threads are doing. The third step is to consult `gstat`, `geom disk list`, and `geom -t` for the storage topology. These three tools will tell you most of what you need in nearly every case.
|
||
|
||
If a panic happens, FreeBSD drops you into the debugger. Capture a backtrace with `bt` and a 注册 dump with `show 寄存器`, then reboot with `reboot`. If a crash dump was taken, `kgdb` on `/var/crash/vmcore.last` will let you inspect the state offline. Keeping crash dumps around, at least in a development environment, pays off immediately when you are chasing intermittent bugs.
|
||
|
||
And above all, when something fails, try to reproduce it. Intermittent bugs in 存储驱动程序 are almost always caused by timing differences in how many BIOs are in flight, how long they take, and when the scheduler decides to run your thread. If you can find a reliable reproduction, you are most of the way to a fix.
|
||
|
||
## 总结
|
||
|
||
This has been a long chapter. Let us take a moment to step back and see what we have covered.
|
||
|
||
We started by situating 存储驱动程序 in FreeBSD's layered architecture. The Virtual File System layer sits between system calls and 文件系统, giving every 文件系统 a common shape. `devfs` is itself a 文件系统, providing the `/dev` directory that user-space tools and administrators use to refer to 设备. Storage 驱动程序 do not live inside VFS. They live at the bottom of the stack, below GEOM and below the 缓冲区 cache, and they communicate with the rest of the 内核 through `struct bio`.
|
||
|
||
We built a working pseudo 块 设备驱动程序 from scratch. In Section 3 we wrote the skeleton that 注册ed a disk with `g_disk` and published a `/dev` node. In Section 4 we explored GEOM's concepts of classes, geoms, providers, and consumers, and we understood how the topology composes and how access counts keep the system safe during teardown. In Section 5 we implemented the strategy function that actually services `BIO_READ`, `BIO_WRITE`, `BIO_DELETE`, and `BIO_FLUSH` against an in-memory backing store. In Section 6 we formatted the 设备 with `newfs_ufs`, 挂载ed a real 文件系统 on it, and saw the two access paths (raw and 文件系统) converge in our strategy function. In Section 7 we surveyed persistence options and added a simple technique for surviving module reloads. In Section 8 we walked through the teardown path in detail and learned how to test it. In Section 9 we looked at the directions a growing 驱动程序 tends to go: multi-unit support, ioctl surfaces, source-file splits, and stable versioning.
|
||
|
||
We exercised the 驱动程序 through labs and stretched it with challenges. We collected the common failure modes in a troubleshooting section. And throughout, we kept our eyes on the real FreeBSD source tree, because the goal of this book is not to teach toy 内核 code but to teach the real thing.
|
||
|
||
You should now be able to read `md(4)` with real comprehension rather than just staring at it. You should be able to read `g_zero.c` and recognise every function it calls. You should be able to diagnose the common classes of storage-驱动程序 bug by symptom. And you should have a working, if simple, pseudo 块设备 that you wrote yourself.
|
||
|
||
That is a substantial 挂载 of ground covered. Take a moment to notice how far you have come. In 第26章 you knew how to write a character 驱动程序. Now you can also write a 块 驱动程序. The two chapters together give you the foundation for nearly every other kind of 驱动程序 in FreeBSD, because most 驱动程序 are either character-oriented or 块-oriented at the boundary where they meet the rest of the 内核.
|
||
|
||
### A Summary of the Key Moves
|
||
|
||
For quick recall, here are the moves that define a minimal 存储驱动程序.
|
||
|
||
1. Include the right headers: `sys/bio.h`, `geom/geom.h`, `geom/geom_disk.h`.
|
||
2. Allocate a `struct disk` with `disk_alloc`.
|
||
3. Fill in `d_name`, `d_unit`, `d_strategy`, `d_扇区ize`, `d_mediasize`, `d_maxsize`, and `d_drv1`.
|
||
4. Call `disk_create(sc->disk, DISK_VERSION)`.
|
||
5. In `d_strategy`, switch on `bio_cmd` and service the request. Always call `biodone` exactly once.
|
||
6. In the unload path, call `disk_destroy` before freeing anything the strategy function touches.
|
||
7. Declare `MODULE_DEPEND` on `g_disk`.
|
||
8. Use `MAXPHYS` for `d_maxsize` unless you have a specific reason to be smaller.
|
||
9. Test the unload path under load. Test it with a 挂载ed 文件系统. Test it with a raw `cat` holding the 设备 open.
|
||
10. Read `dmesg`, `gstat`, `geom disk list`, and `procstat -kk` when something goes wrong.
|
||
|
||
These ten moves are the skeleton of every FreeBSD 存储驱动程序 you will ever write. They appear in different clothing in `ada(4)`, in `nvme(4)`, in `mmcsd(4)`, in `zvol(4)`, and in every other 驱动程序 in the tree. Once you see the pattern, the variety across real 驱动程序 becomes much less mysterious.
|
||
|
||
### A Reminder About Raw Access
|
||
|
||
Even with a 文件系统 挂载ed, your 驱动程序 is still reachable as a raw 块设备. `/dev/myblk0` remains a valid handle that tools like `dd`, `diskinfo`, `gstat`, and `dtrace` can use. The two access paths coexist through GEOM's discipline: both paths issue BIOs, both paths respect the access counts, and your strategy function services both without distinguishing between them. That uniformity is the great gift of GEOM to storage-驱动程序 authors.
|
||
|
||
### A Reminder About Safety
|
||
|
||
Working on a shared system while developing a 存储驱动程序 is an invitation for pain. Use a virtual machine, or at the very least a system you can reinstall. Keep a rescue image handy. Keep backups of anything you cannot afford to lose, including code you are in the middle of writing. The chapter's 驱动程序 is well-behaved and should not damage anything, but 驱动程序 you write in the future may not be, and the cost of being prepared is very small compared to the cost of being unprepared.
|
||
|
||
### Where to Look Next in the FreeBSD Tree
|
||
|
||
If you want to keep exploring storage before the next chapter, three areas of the tree repay careful reading.
|
||
|
||
- `/usr/src/sys/geom/` has the GEOM 框架 itself, including `g_class`, `g_disk`, and many transformation classes like `g_mirror`, `g_stripe`, and `g_eli`.
|
||
- `/usr/src/sys/dev/md/md.c` is the full-featured memory-disk 驱动程序, already mentioned many times in this chapter.
|
||
- `/usr/src/sys/ufs/` is the UFS 文件系统. Not required reading for 驱动程序 work, but it helps to see the layer immediately above yours.
|
||
|
||
Reading these is not a prerequisite for the next chapter. It is a recommendation for your own growth.
|
||
|
||
## Bridge to the Next Chapter
|
||
|
||
In this chapter we built a 存储驱动程序 from scratch. The data flowing through it was internal to the system: bytes written to a file, bytes read from a file, super块 and cylinder groups and inodes shuffling around in the 缓冲区 cache. No byte ever left the machine. The 驱动程序's entire world was the 内核's own memory and the processes that consume it.
|
||
|
||
第28章 takes us into a different world. We will write a 网络接口 驱动程序. Network 驱动程序 are transport 驱动程序 like the USB 串行驱动程序 of 第26章 and the 存储驱动程序 of this chapter, but their conversation partner is not a process and not a 文件系统. It is a network stack, and the unit of work is not a byte range and not a 块 but a 数据包. The 数据包 is a structured object with headers and payload, and the 驱动程序 participates in a stack that includes IP, ARP, ICMP, TCP, UDP, and many other protocols.
|
||
|
||
The patterns you have internalised in this chapter will reappear, with different names. Instead of `struct bio`, you will see `struct mbuf`. Instead of `g_disk`, you will see the `ifnet` 接口. Instead of `disk_strategy`, you will see the `if_transmit` and `if_input` hooks. Instead of GEOM providers and consumers, you will see 网络接口 objects linked into the 内核's network stack. The role is the same: a transport 驱动程序 takes requests from above, delivers them below, accepts responses below, and delivers them above.
|
||
|
||
Many of the concerns will also be the same. Locking. Hot unplug. Resource cleanup on 分离. Observability through 内核 tools. Safety in the face of errors. The fundamentals carry over. What changes is the vocabulary, the structure of the unit of work, and some of the specific tools.
|
||
|
||
Before you move on, take a short break. Unload your 存储驱动程序. Run `kldstat` and confirm that nothing from this chapter is still loaded. Close your lab logbook. Stand up. Refill your coffee. The next chapter is going to be just as substantive as this one, and you will want a clear head.
|
||
|
||
When you come back, 第28章 will start the same way this one did: with a gentle 引言 and a clear picture of where we are going. See you there.
|
||
|
||
## 快速参考
|
||
|
||
The tables below are intended as a quick lookup when you are writing or debugging a 存储驱动程序 and need to remember a name, a command, or a path. They are not a substitute for the full explanations earlier in the chapter.
|
||
|
||
### Key Headers
|
||
|
||
| Header | Defines |
|
||
|--------|---------|
|
||
| `sys/bio.h` | `struct bio`, `BIO_READ`, `BIO_WRITE`, `BIO_DELETE`, `BIO_FLUSH`, `BIO_GETATTR` |
|
||
| `geom/geom.h` | `struct g_class`, `struct g_geom`, `struct g_provider`, `struct g_consumer`, topology primitives |
|
||
| `geom/geom_disk.h` | `struct disk`, `DISK_VERSION`, `disk_alloc`, `disk_create`, `disk_destroy`, `disk_gone` |
|
||
| `sys/module.h` | `DECLARE_MODULE`, `MODULE_VERSION`, `MODULE_DEPEND` |
|
||
| `sys/malloc.h` | `MALLOC_DEFINE`, `malloc`, `free`, `M_WAITOK`, `M_NOWAIT`, `M_ZERO` |
|
||
| `sys/lock.h`, `sys/互斥锁.h` | `struct mtx`, `mtx_init`, `mtx_lock`, `mtx_unlock`, `mtx_destroy` |
|
||
|
||
### Key Structures
|
||
|
||
| Structure | Role |
|
||
|-----------|------|
|
||
| `struct disk` | The `g_disk` representation of a disk. Filled in by the 驱动程序, owned by the 框架. |
|
||
| `struct bio` | One I/O request, passed between GEOM layers and into the 驱动程序's strategy function. |
|
||
| `struct g_provider` | The producer-facing 接口 of a geom. Filesystems and other geoms consume from providers. |
|
||
| `struct g_consumer` | The connection from one geom into another geom's provider. |
|
||
| `struct g_geom` | An instance of a `g_class`. |
|
||
| `struct g_class` | The template from which geoms are created. Defines methods like `init`, `fini`, `start`, `access`. |
|
||
|
||
### Common BIO Commands
|
||
|
||
| Command | Meaning |
|
||
|---------|---------|
|
||
| `BIO_READ` | Read bytes from the 设备 into a 缓冲区. |
|
||
| `BIO_WRITE` | Write bytes from a 缓冲区 into the 设备. |
|
||
| `BIO_DELETE` | Discard a range of 块. Used for TRIM. |
|
||
| `BIO_FLUSH` | Commit outstanding writes to durable storage. |
|
||
| `BIO_GETATTR` | Query a named attribute from the provider. |
|
||
| `BIO_ZONE` | Zoned-块-设备 operations. Not commonly used. |
|
||
|
||
### Common GEOM Tools
|
||
|
||
| Tool | Purpose |
|
||
|------|---------|
|
||
| `geom disk list` | List 注册ed disks and their providers. |
|
||
| `geom -t` | Show the entire GEOM topology as a tree. |
|
||
| `geom part show` | Show 分区 geoms and their providers. |
|
||
| `gstat` | Live per-provider I/O statistics. |
|
||
| `diskinfo -v /dev/xxx` | Show disk geometry and attributes. |
|
||
| `iostat -x 1` | Live per-设备 throughput and latency. |
|
||
| `dd if=... of=...` | Raw 块 I/O for testing. |
|
||
| `newfs_ufs /dev/xxx` | Create a UFS 文件系统 on a 设备. |
|
||
| `挂载 /dev/xxx /mnt` | Mount a 文件系统. |
|
||
| `u挂载 /mnt` | Un挂载 a 文件系统. |
|
||
| `mdconfig` | Create or destroy memory disks. |
|
||
| `fuser` | Find processes holding a file open. |
|
||
| `procstat -kk` | Show 内核 stack traces for all threads. |
|
||
|
||
### Key Callback Typedefs
|
||
|
||
| Typedef | Purpose |
|
||
|---------|---------|
|
||
| `disk_strategy_t` | Handles BIOs. The core I/O function. Required. |
|
||
| `disk_open_t` | Called when a new access is being granted. Optional. |
|
||
| `disk_close_t` | Called when an access is being released. Optional. |
|
||
| `disk_ioctl_t` | Handles ioctls on the `/dev` node. Optional. |
|
||
| `disk_getattr_t` | Answers `BIO_GETATTR` queries. Optional. |
|
||
| `disk_gone_t` | Notifies the 驱动程序 when the disk is being forced away. Optional. |
|
||
|
||
### File and Path Reference
|
||
|
||
| Path | What lives there |
|
||
|------|------------------|
|
||
| `/usr/src/sys/geom/geom_disk.c` | The `g_disk` implementation. |
|
||
| `/usr/src/sys/geom/geom_disk.h` | The public `g_disk` 接口. |
|
||
| `/usr/src/sys/geom/geom.h` | Core GEOM structures and functions. |
|
||
| `/usr/src/sys/sys/bio.h` | The `struct bio` definition. |
|
||
| `/usr/src/sys/dev/md/md.c` | The reference memory-disk 驱动程序. |
|
||
| `/usr/src/sys/geom/zero/g_zero.c` | A minimal GEOM class, useful as a reading reference. |
|
||
| `/usr/src/sys/ufs/ffs/ffs_vfsops.c` | UFS's 挂载 path. Read if you want to see what 挂载 does at the 文件系统 side. |
|
||
| `/usr/src/share/man/man9/disk.9` | The `disk(9)` manual page. |
|
||
| `/usr/src/share/man/man9/g_bio.9` | The `g_bio(9)` manual page. |
|
||
|
||
### Common Disk Flags
|
||
|
||
| Flag | Meaning |
|
||
|------|---------|
|
||
| `DISKFLAG_CANDELETE` | The 驱动程序 handles `BIO_DELETE`. |
|
||
| `DISKFLAG_CANFLUSHCACHE` | The 驱动程序 handles `BIO_FLUSH`. |
|
||
| `DISKFLAG_UNMAPPED_BIO` | The 驱动程序 accepts unmapped BIOs (advanced). |
|
||
| `DISKFLAG_WRITE_PROTECT` | The 设备 is read-only. |
|
||
| `DISKFLAG_DIRECT_COMPLETION` | Completion is safe from any context (advanced). |
|
||
|
||
These flags are set on `sc->disk->d_flags` before `disk_create`. They let the 内核 make smarter choices about how to issue BIOs to your 驱动程序.
|
||
|
||
### Patterns for d_strategy
|
||
|
||
Here are the three most common shapes of a strategy function.
|
||
|
||
**Pattern 1: Synchronous, in-memory.** Our 驱动程序 uses this. The function serves the BIO inline and returns after calling `biodone`.
|
||
|
||
```c
|
||
void strategy(struct bio *bp) {
|
||
/* validate */
|
||
switch (bp->bio_cmd) {
|
||
case BIO_READ: memcpy(bp->bio_data, ...); break;
|
||
case BIO_WRITE: memcpy(..., bp->bio_data); break;
|
||
}
|
||
bp->bio_resid = 0;
|
||
biodone(bp);
|
||
}
|
||
```
|
||
|
||
**Pattern 2: Enqueue to a worker thread.** `md(4)` uses this. The function 附加 the BIO to a queue and signals a worker.
|
||
|
||
```c
|
||
void strategy(struct bio *bp) {
|
||
mtx_lock(&sc->lock);
|
||
TAILQ_INSERT_TAIL(&sc->queue, bp, bio_queue);
|
||
wakeup(&sc->queue);
|
||
mtx_unlock(&sc->lock);
|
||
}
|
||
```
|
||
|
||
The worker dequeues BIOs, services them one at a time (perhaps calling `vn_rdwr` or issuing hardware commands), and completes each with `biodone`.
|
||
|
||
**Pattern 3: Hardware DMA with interrupt completion.** Real hardware 驱动程序 use this. The function programs the hardware, sets up DMA, and returns. A later 中断处理程序 completes the BIO.
|
||
|
||
```c
|
||
void strategy(struct bio *bp) {
|
||
/* validate */
|
||
program_hardware(bp);
|
||
/* strategy returns, interrupt will call biodone eventually */
|
||
}
|
||
```
|
||
|
||
Each pattern has trade-offs. Pattern 1 is simplest but cannot 块. Pattern 2 handles 块ing work but adds latency. Pattern 3 is needed for real hardware but requires interrupt handling, which adds a whole other layer of complexity.
|
||
|
||
Our chapter 驱动程序 uses Pattern 1. `md(4)` uses Pattern 2. `ada(4)`, `nvme(4)`, and friends use Pattern 3.
|
||
|
||
### Minimum Registration Sequence
|
||
|
||
For the 驱动程序 writer in a hurry, the minimum sequence to 注册 a 块设备 is:
|
||
|
||
```c
|
||
sc->disk = disk_alloc();
|
||
sc->disk->d_name = "myblk";
|
||
sc->disk->d_unit = sc->unit;
|
||
sc->disk->d_strategy = myblk_strategy;
|
||
sc->disk->d_sectorsize = 512;
|
||
sc->disk->d_mediasize = size_in_bytes;
|
||
sc->disk->d_maxsize = MAXPHYS;
|
||
sc->disk->d_drv1 = sc;
|
||
disk_create(sc->disk, DISK_VERSION);
|
||
```
|
||
|
||
And the minimum teardown sequence is:
|
||
|
||
```c
|
||
disk_destroy(sc->disk);
|
||
free(sc->backing, M_MYBLK);
|
||
mtx_destroy(&sc->lock);
|
||
free(sc, M_MYBLK);
|
||
```
|
||
|
||
## 术语表
|
||
|
||
**Access count.** A tuple of three counters on a GEOM provider that tracks how many readers, writers, and exclusive holders currently have access to it. Displayed as `rNwNeN` in `geom disk list`.
|
||
|
||
**Attach.** In the New总线 sense, the step where a 驱动程序 takes responsibility for a 设备. In the storage sense, the step where the 驱动程序 calls `disk_create` to 注册 with `g_disk`. The word overloads; use context.
|
||
|
||
**Backing store.** The place where the bytes of a storage 设备 actually live. For our 驱动程序, the backing store is a `malloc`'d 缓冲区 in 内核 memory. For real disks, it is the platter or the flash. For `md(4)` in vnode mode, it is a file in the host 文件系统.
|
||
|
||
**BIO.** A `struct bio`. The unit of I/O that flows through GEOM.
|
||
|
||
**BIO_DELETE.** A BIO command that asks the 驱动程序 to discard a range of 块. Used for TRIM on SSDs.
|
||
|
||
**BIO_FLUSH.** A BIO command that asks the 驱动程序 to make all previous writes durable before returning.
|
||
|
||
**BIO_GETATTR.** A BIO command that asks the 驱动程序 to return the value of a named attribute.
|
||
|
||
**BIO_READ.** A BIO command that asks the 驱动程序 to read a range of bytes.
|
||
|
||
**BIO_WRITE.** A BIO command that asks the 驱动程序 to write a range of bytes.
|
||
|
||
**Block 设备.** A 设备 that is addressed in fixed-size 块, with seekable random access. Historically distinct from 字符设备 in BSD; in modern FreeBSD, 块 and character access converge through GEOM but the mental distinction still matters.
|
||
|
||
**Buffer cache.** The 内核 subsystem that holds recently used 文件系统 块 in RAM. Sits between 文件系统 and GEOM. Not to be confused with the page cache; they are related but distinct in FreeBSD.
|
||
|
||
**Cache coherency.** The property that reads and writes see each other in a consistent order. The strategy function must not return data that is stale relative to recent writes on the same offset.
|
||
|
||
**Cdev.** A 字符设备 node as represented by `struct cdev`. Character 驱动程序 create them with `make_dev`. Block 驱动程序 usually do not.
|
||
|
||
**Consumer.** The input-facing side of a geom. A consumer 附加 to a provider and issues BIOs into it.
|
||
|
||
**d_drv1.** A generic pointer in `struct disk` where the 驱动程序 stores its private context, typically the softc.
|
||
|
||
**d_mediasize.** The total size of the 设备 in bytes.
|
||
|
||
**d_maxsize.** The largest single BIO the 驱动程序 can accept. Usually `MAXPHYS` for pseudo 设备.
|
||
|
||
**d_扇区ize.** The size of a 扇区 in bytes. Typically 512 or 4096.
|
||
|
||
**d_strategy.** The 驱动程序's BIO-handling function.
|
||
|
||
**Devfs.** A pseudo-文件系统 挂载ed at `/dev` that synthesises file nodes for 内核 设备.
|
||
|
||
**Devstat.** The 内核's 设备 statistics subsystem, used by `iostat`, `gstat`, and others. Storage 驱动程序 using `g_disk` get devstat integration automatically.
|
||
|
||
**Disk_alloc.** Allocates a `struct disk`. Never fails; uses `M_WAITOK` internally.
|
||
|
||
**Disk_create.** Registers a filled-in `struct disk` with `g_disk`. The real work is done asynchronously.
|
||
|
||
**Disk_destroy.** Un寄存器 and destroys a `struct disk`. Waits for in-flight BIOs to complete. Panics if the provider still has users.
|
||
|
||
**Disk_gone.** Notifies `g_disk` that the underlying media is gone. Used in 热拔出 scenarios. Distinct from `disk_destroy`.
|
||
|
||
**DISK_VERSION.** The ABI version of the `struct disk` 接口. Defined in `geom_disk.h` and passed to `disk_create`.
|
||
|
||
**DTrace.** FreeBSD's dynamic tracing facility. Especially useful for observing BIO traffic.
|
||
|
||
**Event thread.** The single 内核 thread that GEOM uses to process topology events such as creating and destroying geoms. Usually called `g_event` in `procstat` output.
|
||
|
||
**Exclusive access.** A kind of access on a provider that forbids other writers. Filesystems acquire exclusive access on the 设备 they 挂载.
|
||
|
||
**Filesystem.** A concrete implementation of file storage semantics, such as UFS, ZFS, tmpfs, or NFS. Plugs into VFS.
|
||
|
||
**GEOM.** The FreeBSD 框架 for composable 块-layer transformations. Classes, geoms, providers, and consumers are its main objects.
|
||
|
||
**g_disk.** The GEOM subsystem that wraps disk-shaped 驱动程序 with a simpler API. Our 驱动程序 uses it.
|
||
|
||
**g_event.** The GEOM event thread that processes topology changes.
|
||
|
||
**g_io_deliver.** The function used at the class level to complete a BIO. `g_disk` calls it for us; our 驱动程序 calls `biodone`.
|
||
|
||
**g_io_request.** The function used at the class level to issue a BIO downward. Only used in 驱动程序 that implement their own GEOM class.
|
||
|
||
**Hotplug.** A 设备 that can appear or disappear without reboot.
|
||
|
||
**Ioctl.** A control operation on a 设备, distinct from read or write. In the storage path, ioctls on `/dev/diskN` go through GEOM and may be handled by `g_disk` or by the 驱动程序's `d_ioctl`.
|
||
|
||
**md(4).** The FreeBSD memory-disk 驱动程序. The canonical pseudo 块设备 in the tree, and a recommended reading reference.
|
||
|
||
**Mount.** The act of 附加ing a 文件系统 to a point in the namespace. Calls VFS, which calls the 文件系统's own 挂载 routine, which typically opens a GEOM provider.
|
||
|
||
**New总线.** FreeBSD's 总线 框架. Used for character and hardware 驱动程序. Our 存储驱动程序 does not directly use New总线 because it is a pseudo 设备; real 存储驱动程序 almost always do.
|
||
|
||
**Provider.** The output-facing side of a geom. Other geoms or `/dev` nodes consume providers.
|
||
|
||
**Softc.** The per-instance state structure of a 驱动程序.
|
||
|
||
**Strategy function.** The 驱动程序's BIO handler. Called `d_strategy` in the `struct disk` API.
|
||
|
||
**Super块.** A small on-disk structure that describes the layout of a 文件系统. UFS's is at offset 65536.
|
||
|
||
**Topology.** The tree of GEOM classes, geoms, providers, and consumers. Protected by the topology lock.
|
||
|
||
**Topology lock.** The global lock protecting the GEOM topology from concurrent modification.
|
||
|
||
**UFS.** The Unix File System, FreeBSD's default 文件系统. Lives under `/usr/src/sys/ufs/`.
|
||
|
||
**Unit.** A numbered instance of a 驱动程序. `myblk0` is unit 0 of the `myblk` 驱动程序.
|
||
|
||
**VFS.** The Virtual File System layer. Sits between system calls and concrete 文件系统.
|
||
|
||
**Vnode.** The 内核's runtime handle on an open file or directory. Lives inside VFS.
|
||
|
||
**Withering.** The GEOM process of removing a provider from the topology. Queued on the event thread, waits for in-flight BIOs, and finally destroys the provider.
|
||
|
||
**Zone.** In VM-subsystem vocabulary, a pool of fixed-size objects allocated through the UMA (Universal Memory Allocator). Many 内核 structures, including BIOs and GEOM providers, are allocated from zones.
|
||
|
||
**BIO_ORDERED.** A BIO flag that asks the 驱动程序 to execute this BIO only after all previously issued BIOs have completed. Used for write barriers.
|
||
|
||
**BIO_UNMAPPED.** A BIO flag that indicates `bio_data` is not a mapped 内核 virtual address but rather a list of unmapped pages. Drivers that can handle unmapped data should set `DISKFLAG_UNMAPPED_BIO`.
|
||
|
||
**Direct completion.** Completing a BIO in the same thread that submitted it, without going through a deferred 回调. Usually faster but not always safe.
|
||
|
||
**Drivers in Tree.** Drivers that live inside the FreeBSD source tree and are built as part of the standard 内核 build. Contrast with out-of-tree 驱动程序, which are maintained separately.
|
||
|
||
**Out-of-tree 驱动程序.** A 驱动程序 that is not part of the FreeBSD source tree. These need to be compiled against a matching 内核 and may need updates when the 内核's ABI changes.
|
||
|
||
**ABI.** Application Binary Interface. The set of conventions for function calling, structure layout, and type sizes that allow two pieces of compiled code to interoperate. `DISK_VERSION` is one kind of ABI marker.
|
||
|
||
**API.** Application Programming Interface. The set of function signatures and types that code uses at the source level. Distinct from ABI: two 内核s with the same API might have different ABIs if they were compiled differently.
|
||
|
||
**KPI.** Kernel Programming Interface. FreeBSD's preferred term for the 内核's API. Guarantees about KPI stability are limited; always recompile against the 内核 you are running.
|
||
|
||
**KLD.** Kernel loadable module. The `.ko` file we produce. The "KLD" stands for Kernel Loadable Driver, though modules are not necessarily 驱动程序.
|
||
|
||
**Module.** See KLD.
|
||
|
||
**Taste.** In GEOM vocabulary, the process of offering a provider to all classes so that each class can decide whether to 附加 to it. Tasting happens automatically when new providers appear.
|
||
|
||
**Retaste.** Forcing GEOM to taste a provider again, usually after its contents have changed. `geom provider retaste` triggers this for one provider; `geom retaste` triggers it globally.
|
||
|
||
**Orphan.** In GEOM vocabulary, a provider whose underlying storage has gone away. Orphans are cleaned up by the event thread.
|
||
|
||
**Spoil.** A GEOM concept related to cache invalidation. If a provider's contents change in a way that could invalidate caches, it is said to have been spoiled.
|
||
|
||
**Bufobj.** A 内核 object that associates a vnode (or a GEOM consumer) with a 缓冲区 cache. Each 块设备 and each file has one.
|
||
|
||
**bdev_strategy.** A legacy synonym for `d_strategy`. Modern code uses `d_strategy` directly.
|
||
|
||
**Schedule.** The act of placing a BIO onto an internal queue for later execution. Distinct from "executing".
|
||
|
||
**Plug/unplug.** In some 内核s, the plug is a batching mechanism for BIO submission. FreeBSD does not have plug/unplug; it delivers BIOs immediately.
|
||
|
||
**Elevator.** A BIO scheduler that sorts BIOs by offset to reduce disk seek time. FreeBSD's GEOM does not implement an elevator at the GEOM layer; it is the 块设备's responsibility, if relevant.
|
||
|
||
**Super块.** The first metadata 块 of a 文件系统. Describes geometry. At offset 65536 for UFS.
|
||
|
||
**Cylinder group.** A UFS concept. The 文件系统 is divided into regions, each with its own inode table and 块 allocation bitmap. Keeps related data physically close on a spinning disk and limits the damage a single bad region can cause.
|
||
|
||
**Inode.** A UFS (and POSIX) structure describing one file: its mode, owner, size, timestamps, and pointers to data 块. Filenames live in directory entries, not in inodes.
|
||
|
||
**Vop_vector.** The dispatch table that a 文件系统 provides to VFS, listing all the operations VFS knows how to ask about (open, close, read, write, lookup, rename, and so on). VFS calls these as indirect function pointers.
|
||
|
||
**Devstat.** A 内核 structure 附加ed to 设备 that records aggregate I/O statistics. `iostat(8)` reads devstat data; `g_disk` allocates and feeds a devstat structure for every disk it creates.
|
||
|
||
**bp.** Shorthand in 内核 source for a BIO pointer. Used almost universally in strategy functions and completion 回调. When you see `struct bio *bp`, read it as "the current request".
|
||
|
||
**Bread.** Buffer-cache function that reads a 块, consulting the cache first and issuing I/O only on a miss. Used by 文件系统, not by 驱动程序.
|
||
|
||
**Bwrite.** Buffer-cache function that writes a 块 synchronously. The 文件系统 uses it; your strategy function eventually sees the resulting BIO.
|
||
|
||
**Getblk.** Buffer-cache function that returns a 缓冲区 for a given 块, allocating it if necessary. Used by 文件系统 as the entry point to both reading and writing.
|
||
|
||
**Bdwrite.** Delayed 缓冲区-cache write. Marks the 缓冲区 dirty but does not issue I/O immediately. Will be written later by the syncer or by 缓冲区 cache pressure.
|
||
|
||
**Bawrite.** Asynchronous 缓冲区-cache write. Like `bwrite` but does not wait for completion.
|
||
|
||
**Syncer.** A 内核 thread that periodically flushes dirty 缓冲区 to their backing 设备. Closing a 文件系统 cleanly requires the syncer to finish.
|
||
|
||
**Taskqueue.** A 内核 facility for running 回调 in a separate thread. Useful when your strategy function wants to defer work. Covered in more depth when we discuss 中断处理程序s in later chapters.
|
||
|
||
**Callout.** A 内核 facility for scheduling a one-shot or periodic 回调 at a given time. Not commonly used in simple 存储驱动程序 but very common in hardware 驱动程序 that implement timeouts.
|
||
|
||
**Witness.** A 内核 subsystem that detects lock-order violations and prints warnings. Always enabled in debug 内核s; saves hours of debugging.
|
||
|
||
**INVARIANTS.** A 内核 compile option that adds runtime assertions. Always enabled in debug 内核s; catches many storage bugs before they become silent corruption.
|
||
|
||
**Debug 内核.** A 内核 built with `INVARIANTS`, `WITNESS`, and related options. Slower but much safer for 驱动程序 development. Use one during lab work.
|
||
|
||
## Frequently Asked Questions
|
||
|
||
### Do I need to support BIO_ORDERED?
|
||
|
||
For a pseudo 设备 that services BIOs synchronously, no. Each BIO completes before the next one is processed, which trivially preserves ordering. For an asynchronous 驱动程序, you must respect `BIO_ORDERED` by deferring subsequent BIOs until the ordered one completes.
|
||
|
||
### What is the relationship between d_maxsize and MAXPHYS?
|
||
|
||
`d_maxsize` is the maximum BIO size your 驱动程序 can accept. `MAXPHYS` is the compile-time upper bound on BIO size, defined in `/usr/src/sys/sys/param.h`. On 64-bit systems such as amd64 and arm64, `MAXPHYS` is 1 MiB; on 32-bit systems it is 128 KiB. FreeBSD 14.3 also exposes a runtime tunable, `maxphys`, which some subsystems consult through the `MAXPHYS` macro or the `maxphys` variable. Setting `d_maxsize = MAXPHYS` accepts whatever the 内核 is willing to issue. For most pseudo 驱动程序 this is fine.
|
||
|
||
### Can my 驱动程序 issue BIOs to itself?
|
||
|
||
Technically yes, but it rarely makes sense. The pattern is used by GEOM transformation classes (they take in BIOs from above and issue new BIOs downward). A `g_disk` 驱动程序 is at the bottom of the stack and has no downward; if you need to split work across multiple backing units, you probably want worker threads rather than nested BIOs.
|
||
|
||
### Why do some fields in struct disk use u_int and others off_t?
|
||
|
||
`u_int` is used for unsigned integer sizes that fit in 32 bits (扇区 size, number of heads, etc.). `off_t` is a signed 64-bit type used for byte offsets and sizes that can exceed 32 bits (media size, request offsets). The distinction matters for large disks; a media size of 10 TB requires more than 32 bits.
|
||
|
||
### Is disk_alloc safe to call at any time?
|
||
|
||
`disk_alloc` uses `M_WAITOK` and will sleep if memory is tight. Do not call it while holding a spin lock or a 互斥锁 you cannot release. Call it at 附加 time, outside any lock.
|
||
|
||
### What happens if I call disk_create twice with the same name?
|
||
|
||
`disk_create` will happily create multiple disks with the same name if the unit numbers differ. If both the name and unit number match, GEOM will reject the second registration and the resulting behaviour is implementation-defined. Avoid this case.
|
||
|
||
### Can the strategy function sleep?
|
||
|
||
Technically yes, but it should not. The strategy function runs in the caller's thread context, and sleeping there stalls the caller. For work that must 块, use a worker thread.
|
||
|
||
### How do I know when all BIOs have finished for a given 文件系统?
|
||
|
||
You usually do not need to. `u挂载(2)` does the work: it flushes dirty 缓冲区, drains in-flight BIOs, and returns only after the 文件系统 is fully quiesced. After `u挂载` returns, no BIOs will arrive for that 挂载 point unless something else opens the 设备.
|
||
|
||
### Can I pass pointers between threads through bio_caller1 or similar fields?
|
||
|
||
Yes. `bio_caller1` and `bio_caller2` are opaque fields meant for the issuer of the BIO to stash context that the completion handler can use. As long as you own the BIO (which you do, because you issued it), the fields are yours. `g_disk` 驱动程序 do not usually need them because the BIO arrives from above and is completed by calling `biodone`, with `g_disk` handling the 回调 routing.
|
||
|
||
### My 驱动程序 works on my laptop but not on the server. Why?
|
||
|
||
Possibilities: different 内核 ABI (recompile against the server's 内核), different `MAXPHYS` (should be identical on 14.3 systems but check), different GEOM classes loaded (unlikely but possible), different memory size (your allocation might fail on a smaller system), different clock speed (affecting timing). Compare `uname -a` and `sysctl -a | grep kern.maxphys` to start.
|
||
|
||
### Where does the /dev node's name actually come from?
|
||
|
||
From `d_name` and `d_unit` in the `struct disk` you pass to `disk_create`. GEOM concatenates them without a separator: `d_name = "myblk"`, `d_unit = 0` produces `/dev/myblk0`. If you want a different convention, set `d_name` accordingly. There is no separator character between the name and the unit.
|
||
|
||
### What is the maximum number of units I can create?
|
||
|
||
Limited by `d_unit`, which is `u_int`, so 2^32 - 1 in theory. In practice, per-unit memory consumption and the practical limits of `/dev` name space will stop you long before that.
|
||
|
||
### Can I change d_mediasize after disk_create?
|
||
|
||
Yes, but carefully. Filesystems 挂载ed on the disk will not pick up the change automatically; most will require 卸载 and re挂载. `md(4)` supports `MDIOCRESIZE` and there is infrastructure for signalling the change to GEOM, but the pattern is non-trivial.
|
||
|
||
### What happens if I forget MODULE_DEPEND?
|
||
|
||
The 内核 may fail to load your module if `g_disk` is not already loaded, or may load it successfully if `g_disk` happens to be built into the 内核. Always declare `MODULE_DEPEND` explicitly to avoid surprise.
|
||
|
||
### Should I use biodone or g_io_deliver in my 驱动程序?
|
||
|
||
Use `biodone`. The `g_disk` wrapper provides a `d_strategy` style 接口 where the correct completion call is `biodone`. If you write your own `g_class`, you will call `g_io_deliver` instead, but that is a different path and a different chapter's worth of complexity.
|
||
|
||
### How does BIO_DELETE relate to TRIM and UNMAP?
|
||
|
||
`BIO_DELETE` is the in-内核 abstraction. For SATA SSDs it maps to the ATA TRIM command, for SCSI/SAS to UNMAP, and for NVMe to Dataset Management with the deallocate bit. Userland triggers it through `fstrim(8)` or the `-o trim` 挂载 option on UFS. Our 驱动程序 is free to treat it as a hint or to honour it by zeroing memory, since backing is in RAM.
|
||
|
||
### Why does my strategy function sometimes receive a BIO with bio_length of zero?
|
||
|
||
In normal operation you should never see this. If it happens, treat it as a defensive case: call `biodone(bp)` with no error and return. A length-zero BIO is not illegal, but it indicates something odd upstream. Filing a PR against the issuing code is reasonable.
|
||
|
||
### What is the difference between d_flags and bio_flags?
|
||
|
||
`d_flags` is static configuration for the whole disk, set once at registration and describing what the 驱动程序 can do (handles DELETE, can FLUSH, accepts unmapped BIOs, and so on). `bio_flags` is dynamic metadata on a single BIO, changing per request (ordered, unmapped, direct completion). Do not confuse them.
|
||
|
||
### Can my 驱动程序 present itself as removable media?
|
||
|
||
Yes, set `DISKFLAG_CANDELETE` and consider honouring `disk_gone` to simulate ejection. Tools like `camcontrol` and 文件系统 handlers generally treat any GEOM provider uniformly, so "removable" in the user-visible sense is less distinct than in other operating systems.
|
||
|
||
### What thread actually calls my strategy function?
|
||
|
||
It depends. For synchronous submission from the 缓冲区 cache, it is the thread that called `bwrite` or `bread`. For asynchronous completion paths it is often a GEOM worker or the 缓冲区 cache's flusher thread. Your strategy must be written to tolerate any caller. Do not assume a specific thread identity or a specific priority.
|
||
|
||
### How do I know which process caused a given BIO?
|
||
|
||
You usually cannot, because BIOs can be reordered, coalesced, merged, and issued from background threads that are not the original requester. `dtrace` with the `io:::start` 探测 plus stack captures can get you close, but it is investigative work, not a routine 驱动程序 responsibility.
|
||
|
||
### Can two different 文件系统 be 挂载ed simultaneously on two unit numbers of my 驱动程序?
|
||
|
||
Yes, if you implemented multi-unit support. Each unit presents its own GEOM provider. Their backing stores are independent. The only shared state is your module's global variables and the 内核 itself, so the two 挂载s do not interact unless you make them.
|
||
|
||
### Should my 驱动程序 handle power management events?
|
||
|
||
For a pseudo 设备, no. For a real hardware 驱动程序, yes: suspend and resume events flow through New总线 as method calls, and the 驱动程序 must quiesce I/O on suspend and revalidate 设备 state on resume. Storage 驱动程序 on laptops are a common source of suspend-related bugs, so real 驱动程序 take this seriously.
|
||
|
||
### What is the practical impact of choosing 512 versus 4096 as d_扇区ize?
|
||
|
||
On modern 文件系统, very little: UFS, ZFS, and most other FreeBSD 文件系统 work happily with either. On the 驱动程序 side, a larger 扇区 size reduces the number of BIOs for large transfers. On the workload side, applications doing O_DIRECT or aligned I/O may care. When in doubt, pick 4096 for new 驱动程序; it matches modern flash and avoids alignment penalties.
|
||
|
||
### If I reload my 驱动程序 many times, does memory leak?
|
||
|
||
Only if you have a bug. In our design, `MOD_UNLOAD` calls `myblk_分离_unit`, which frees the backing store and the softc. The persistence variant deliberately retains the backing store across reloads but uses a single global pointer, so there is no leak; the same memory is reused. If `vmstat -m | grep myblk` climbs across reloads, investigate.
|
||
|
||
### Why does `挂载` sometimes succeed on my raw 设备 but `newfs_ufs` fail?
|
||
|
||
`newfs_ufs` writes structured metadata (super块, cylinder groups, inode tables) and then reads some of it back to verify. If the 设备 is too small, corrupts writes silently, or returns errors only under certain conditions, `newfs_ufs` catches it first. `挂载` is much less strict at the write path; it can read in a broken super块 and produce odd errors later. A successful `newfs` is a stronger correctness signal than a successful `挂载`.
|
||
|
||
### How do I verify that my BIO_FLUSH implementation actually makes data durable?
|
||
|
||
For our in-memory 驱动程序, durability is bounded by the host's power: flushing does nothing useful because a power cut takes everything with it. For a real 驱动程序 backed by persistent storage, issuing a flush command to the underlying media and confirming completion before calling `biodone` is the contract. Testing requires a power-cycle harness or a simulator; there is no shortcut.
|
||
|
||
### What are the correct locking rules inside d_strategy?
|
||
|
||
Hold the 驱动程序's lock long enough to protect the backing store against concurrent access, and release it before calling `biodone`. Never hold a lock across a call into another subsystem. Never call `malloc(M_WAITOK)` with a lock held. Never sleep. If you need to sleep, schedule the work on a taskqueue and call `biodone` from the worker.
|
||
|
||
### Why is BIO_FLUSH not a percent-of-capacity barrier like write barriers on Linux?
|
||
|
||
FreeBSD's BIO_FLUSH is a point-in-time barrier: when it completes, all previously issued writes are durable. It is not associated with a particular range or percentage of the 设备. Drivers can implement it as a strict barrier or as an opportunistic flush, but the minimum contract is the point-in-time guarantee.
|
||
|
||
### Are there any tools that generate BIO traffic to help me test?
|
||
|
||
Yes. `dd(1)` with various `bs=` values, `fio(1)` from ports, `ioping(8)` from ports, plus the usual suspects: `newfs`, `tar`, `rsync`, `cp`. `diskinfo -t` runs a suite of benchmark reads and is useful for rough throughput numbers. The test harnesses under `/usr/src/tools/regression/` can also be adapted.
|
||
|
||
## What This Chapter Did Not Cover
|
||
|
||
This chapter is long, but there are several related topics we deliberately left for later. Naming them here helps you plan future study and prevents the false impression that 存储驱动程序 end at the BIO handler.
|
||
|
||
**Real hardware 存储驱动程序** such as those for SATA, SAS, and NVMe controllers live under CAM and require significant additional machinery: command-块 allocation, tagged queueing, 热插拔 event handling, firmware upload, SMART data, and error-recovery protocols. We introduced the CAM world briefly through the `ada_da.c` excerpt but did not explore it in depth. Chapters 33 through 36 will tackle these 接口, and the md(4) 驱动程序 you read in this chapter is a deliberately small staircase by comparison.
|
||
|
||
**ZFS integration** is its own world. ZFS consumes GEOM providers through its vdev layer but adds copy-on-write semantics, end-to-end checksums, pooled storage, and snapshots that no simple 块 驱动程序 would ever need to know about. If your 驱动程序 works under UFS it almost certainly works under ZFS, but the reverse is not guaranteed: ZFS exercises BIO paths, especially flushing and write ordering, that less demanding 文件系统 skip.
|
||
|
||
**GEOM class authoring** is a larger topic than `g_disk` wrapping. A full class implements taste, start, access, 附加, 分离, dumpconf, destroy_geom, and orphan methods. It can also create and destroy consumers, build multi-level topologies, and respond to configuration via `gctl`. The mirror, stripe, and crypt classes are good starting points once you decide to dig in.
|
||
|
||
**Quotas, ACLs, and extended attributes** are 文件系统 features that live above the GEOM layer entirely. They matter for userland but do not touch the 存储驱动程序. This is a useful piece of clarity: the 驱动程序's job ends at the BIO boundary.
|
||
|
||
**Tracing and debugging 内核 crashes** deserves its own chapter. Kernel core dumps land on a dump 设备 configured via `dumpon(8)`, analysed with `kgdb(1)` or `crashinfo(8)`. If your 驱动程序 panics the system, being able to load the core file and inspect backtraces is a professional-level skill that this chapter only gestured at.
|
||
|
||
**High-performance storage paths** use features such as unmapped I/O, direct-dispatch completion, CPU pinning, NUMA-aware allocation, and dedicated queues. These optimisations matter for gigabytes-per-second workloads but are irrelevant to a teaching 驱动程序. When you start chasing microseconds, come back to `/usr/src/sys/dev/nvme/` and study how the real professionals do it.
|
||
|
||
**Filesystem-specific behaviour** varies widely. UFS asks for one set of BIOs; ZFS asks for a different set; msdosfs and ext2fs ask for something different again. A good 存储驱动程序 is 文件系统-agnostic, but observing different 文件系统 on your 驱动程序 is a fantastic way to build intuition. Try `msdosfs`, `ext2fs`, and `tmpfs` for contrast after you are comfortable with UFS.
|
||
|
||
**iSCSI and network 块设备** present themselves as GEOM providers too, but they are created by userland control daemons and talk to the network stack. 第28章 begins the networking work that makes those providers possible.
|
||
|
||
Our treatment of the storage path was deliberately focused. We wrote a 驱动程序 that 文件系统 accept as real, we understood why and how it is seen that way, and we traced the data path from `write(2)` to RAM. That foundation is enough to make the unexplored topics above readable rather than bewildering.
|
||
|
||
## Final Reflection
|
||
|
||
Storage 驱动程序 have a reputation as forbidding territory. This chapter should have replaced some of that reputation with familiarity: the BIO is just a structure, the strategy function is just a dispatcher, GEOM is just a graph, and `disk_create` is just a registration call. What elevates storage work from routine is not the underlying APIs, which are compact, but the operational demands that accumulate around them: performance, durability, error recovery, and correctness under contention.
|
||
|
||
Those demands do not go away when you leave pseudo 设备 for real hardware. They multiply. But you already have the vocabulary to understand them. You know what a BIO is and where it comes from. You know which thread calls your code and what it expects. You know how to 注册 with GEOM, how to un注册 cleanly, and how to recognise an in-flight request from its shadow in `gstat`. When you sit in front of a real SATA controller 驱动程序 and start reading, you will recognise the shape of the code even though the specifics differ.
|
||
|
||
The craft of storage-驱动程序 writing is, ultimately, patient. You learn by writing small 驱动程序, reading the source tree, reproducing simple experiments, and building instincts about when something that looks right is actually right. The chapter you just finished is a single long step along that journey. The next chapters will step again, each time in different directions.
|
||
|
||
## Further Reading
|
||
|
||
If this chapter has whetted your appetite for storage internals, here are some places to go next.
|
||
|
||
**Manual pages**. `disk(9)`, `g_bio(9)`, `geom(4)`, `devfs(5)`, `ufs(5)`, `newfs(8)`, `mdconfig(8)`, `gstat(8)`, `diskinfo(8)`, `挂载(2)`, `挂载(8)`. Read them in that order.
|
||
|
||
**The FreeBSD Architecture Handbook**. The storage chapter is a good complement to this one.
|
||
|
||
**Kirk McKusick et al., "The Design and Implementation of the FreeBSD Operating System".** The book's chapters on the 文件系统 are especially relevant.
|
||
|
||
**DTrace books.** Brendan Gregg's "DTrace Book" is a practical reference; Sun's "Dynamic Tracing Guide" is the original tutorial.
|
||
|
||
**The FreeBSD source tree.** `/usr/src/sys/geom/`, `/usr/src/sys/dev/md/`, `/usr/src/sys/ufs/`, and `/usr/src/sys/cam/ata/` (where `ata_da.c` implements the `ada` disk 驱动程序). Every pattern discussed in this chapter is grounded in that code.
|
||
|
||
**The mailing list archives.** `freebsd-geom@` and `freebsd-fs@` are the two most relevant lists. Reading historic threads is one of the best ways to pick up the institutional knowledge that books do not capture.
|
||
|
||
**Commit history on GitHub mirrors.** The FreeBSD source tree has a long, well-annotated commit history. For any file you open, running `git log --follow` against its mirror will often reveal the rationale behind design choices, the bugs that shaped the current code, and the people who maintain it. Historical context makes the present code much easier to read.
|
||
|
||
**The Transactions of the FreeBSD Developer Summit.** Several summits have included storage-focused sessions. Recordings and slides, when available, are excellent for picking up the state of the art and the open design debates.
|
||
|
||
**Reading other operating systems' storage stacks.** Once you know FreeBSD's storage path, Linux's 块 layer, Illumos's SD 框架, and macOS's IOKit storage classes all become comprehensible in a way they probably were not before. The specific APIs differ, but the fundamental shapes, BIOs or their equivalents, 文件系统 above, hardware below, are universal.
|
||
|
||
**Testing 框架 for 内核 code.** The `kyua(1)` harness runs regression tests against real 内核s. The `/usr/tests/sys/geom/` tree has examples of what well-written tests for storage code look like; reading them builds both testing instincts and confidence that your code is right.
|
||
|
||
**FreeBSD Foundation blog posts.** The Foundation funds several storage-related projects and publishes readable summaries that complement the source tree.
|
||
|
||
---
|
||
|
||
End of 第27章. Close your lab logbook, make sure your 驱动程序 is unloaded and your 挂载 points are released, and take a break before 第28章.
|
||
|
||
You have just written a 存储驱动程序, 挂载ed a 文件系统 on it, traced data through the 缓冲区 cache, into GEOM, through your strategy function, and back again. That is a real accomplishment. Rest on it for a moment before you turn the page.
|