目录
正在加载目录…
正在阅读专栏 OSTEP 操作系统专栏 第 46 篇 · 共 62 篇 · 74%

OSTEP:The Andrew File System(AFS)

发布于 2026-09-17 21:41 · 最后编辑于 2026-09-17 22:08 · 版本 v2 · 字数 13,778 · 预计阅读 28 分钟 👁 18 次阅读

OSTEP:The Andrew File System(AFS)

本篇对应 OSTEP Version 1.10 的 Chapter 50 “The Andrew File System (AFS)”。核心问题是:如何通过 whole-file caching、callback 和 client-side pathname traversal,让 distributed file system 支持更多 client。

类型与定位

  • 笔记类型:正文翻译/知识整理
  • 翻译基准:官方 The Andrew File System(AFS)
  • 术语约定:whole-file caching、callback、FID、Venus、Vice、volume、last writer wins 等保留英文或中英并列。
  • 图件说明:原文包含 AFS protocol 和 cache consistency timeline;在对应位置用 Mermaid 与表格重绘,没有独立 raster image。

TL;DR

AFS 的目标是 scalability。它把整个 file cache 到 client local disk,后续 read/write 直接在本地执行,并在 close 时将修改后的完整 file flush 到 server。AFSv2 用 callback 替代周期性 TestAuth,用 FID 替代 server 端完整 pathname traversal,从而减少 server interaction。

AFS 的“版本”是演进关系:早期 AFSv1(原始系统实际称为 ITC distributed file system)已经具备 whole-file caching,但没有达到预期 scale;测量结果促成了 AFSv2 的 protocol redesign。这里的重点不是某个 API 的逐项罗列,而是通过 measurement 找到 server CPU bottleneck,再用 protocol 把常见工作转移到 client。

目录

章节内容
AFS 的目标scalability 与可理解的 consistency
AFSv1whole-file cache、Venus 与 Vice
AFSv1 的瓶颈pathname traversal 与 TestAuth
AFSv2 的改进callback、FID 与 volumes
Cache consistencyclose、invalidate 与 last writer wins
Crash recoveryserver/client failure 的影响
设计取舍NFS 与 AFS 对比
Scale 与 performanceAFSv2 的测量与 workload

AFS 的目标

AFS 由 Carnegie Mellon University 的项目发展而来,首要目标是 scalability:让一个 server 支持尽可能多的 client。

NFS 等系统若要求 client 周期性询问 server“我的 cache 还有效吗”,每次检查都会消耗 server CPU 和 network bandwidth。client 数量增多后,这些检查本身可能成为瓶颈。

AFS 还强调 user-visible behavior:cache consistency 应容易描述和理解,而不是依赖难以预测的 timeout 和低级 implementation detail。

AFSv1

AFSv1 的基本策略是 whole-file caching:

mermaid
sequenceDiagram
  participant A as Client application
  participant V as Venus
  participant I as Local disk
  participant X as Vice server
  A->>V: open(file)
  V->>X: Fetch(pathname)
  X-->>V: entire file
  V->>I: cache whole file
  A->>V: read/write
  V->>I: local file operations
  A->>V: close(file)
  V->>X: Store(modified whole file)

组件名称:

  • Venus:client-side AFS code;
  • Vice:server-side file service;
  • client local disk:保存 whole-file cache;
  • server:保存 authoritative copy。

原文 Figure 50.1 的 AFSv1 protocol highlights 可整理为:

Message作用
TestAuth检查 cached file 是否已经改变
GetFileStat获取 file 的 stat information
Fetch从 server 获取整个 file
Store将整个 file 保存回 server
SetFileStat设置 file 的 stat information
ListDir获取 directory contents

open 时,client 将整个 file 从 server Fetch 到 local disk;之后 application 的 read/write 不再需要 network。close 时,如果 file 修改过,client 将完整 file Store 回 server。

与 NFS 的对照:

维度NFSAFS
cache unitblock/data + metadatawhole file
cache locationclient memory 等client local disk
server interaction更频繁的 lookup/attribute checkopen/close 为主要边界
目标transparent remote file以较少 server work 支持大规模 client

AFSv1 的瓶颈

实验测量发现 AFSv1 的主要问题:

  1. Path traversal cost:Fetch 或 Store 携带完整 pathname,server 每次都从 root 逐级遍历 directory;
  2. 过多 TestAuth:client 频繁询问 server 本地 cache 是否仍有效,即使绝大多数答案都是“没有改变”。

此外,server load 可能不均衡;为每个 client 使用独立 process 也会带来 context-switch 和 management overhead。

这些问题说明,scalability 不只取决于 cache size,还取决于 protocol 是否把工作推给 client,以及 server 是否重复执行同样的检查。

AFSv1 的两个主要瓶颈使 server 大约只能服务 20 个 client:一是每个 Fetch/Store 都要从 root 开始遍历完整 pathname,二是大量 TestAuth request 只是在反复得到“文件没有改变”的回答。另有两个实现层面的负担:server 之间的 load imbalance,以及每个 client 一个独立 process 带来的 context switching 和 process-management overhead。AFSv2 分别用 volumes 和 threads 改善后两者。

AFSv2 的改进

Callback

callback 是 server 对 client 的承诺:如果 client cache 的 file 被修改,server 会通知 client。

mermaid
flowchart LR
  C[Client caches file F] --> K[Server grants callback]
  K --> V[Client assumes cache valid]
  S[Another client updates F] --> X[Server breaks callback]
  X --> I[Client invalidates cache]

有 callback 后,client 不必周期性发送 TestAuth;在收到 server 的 callback break 之前,可以假设 cache 仍然有效。这种模式类似 interrupt 相对于 polling:server 在状态变化时主动通知 client。

callback 把 state 引入系统,但用更少的常规 server interaction 换取更高 scalability。

AFS 的 cache unit 是 whole file,而不是 NFS 式的 block request。第一次 open 可能需要 Fetch,之后的多次 read 在 client local disk 上完成;close 时再 Store 修改后的完整 file。server 通过 callback 告诉 client:“只要 callback 仍有效,你缓存的版本没有被别人修改”,从而避免每次 open 都向 server polling。

File Identifier

AFSv2 使用 FID(File Identifier)表示 file,而不是每次把完整 pathname 交给 server。FID 包含:

  • volume identifier;
  • file identifier;
  • uniquifier,用于避免删除后复用 ID 造成混淆。

client 逐段执行 pathname traversal,并把 directory lookup 结果 cache 到 local disk。之后可以直接使用 FID 访问 object,降低 server 的 pathname traversal cost。

例如访问 /home/remzi/notes.txt 时,client 可以先 Fetch home 的 directory contents 并建立 callback,再 Fetch remzi directory,最后 Fetch notes.txt。第一次访问需要多个 message,但同时把目录和 file 放入 local-disk cache;之后只要 callbacks 仍有效,pathname lookup 和 file read 都可以在 client 完成。AFS 把“polling:不断询问 server”转换成“interrupt-like:只有变化时 server 才通知”。

Volumes 与 threads

volume 可以在 server 之间移动,帮助 administrator 做 load balancing。server 使用 threads 而不是每个 client 一个 process,可以降低 context-switch 和 process management overhead。

Cache consistency

AFS 的跨机器语义主要是 close-to-open:

  1. client open file,获取最新的一致版本;
  2. client 在本地反复 read/write;
  3. client close 时把修改后的 whole file flush 到 server;
  4. server 更新 authoritative copy;
  5. server break 其他 client 对旧版本的 callback;
  6. 其他 client 下次 open 时重新 Fetch 新版本。

同一 machine 上的 local processes 可以立即看到彼此的写入,更接近普通 UNIX semantics。跨 machine 时,其他 client 通常要等 writer close 后再看到更新。

若两个不同 client 同时修改同一个 file,AFS 采用 last writer wins(更准确地说常常是 last closer wins):最后 close 的完整 file 覆盖 server 上的结果。这样最终 file 是某一个 client 的完整版本,而不是两个 client 按 block 交错写出的混合版本。

Figure 50.3 的核心时序可压缩为下表;其中同一 machine 上的 local process 可以立即看到写入,而 remote process 通常要等到 close 后重新 open:

操作顺序Client 1 / local cacheClient 2 / local cacheServer可见性含义
Client 1 open、write(A)、closeA—A首次写入在 close 时提交
Client 1 再次 open、write(B)B—A同机 local process 可看到 B
Client 2 open、readBAA远端仍看到旧版本 A
Client 1 closeBABserver 更新并 break Client 2 callback
Client 2 再次 open、readBBB重新 Fetch 后看到 B
两个 client 并发写 C/D 后分别 closeC 或 DC 或 D最后 close 的版本last closer wins,不做 merge

Crash recovery

whole-file caching 简化了普通 read/write path,但 crash 仍需要明确语义:

  • client 在 close 前 crash,server 可能看不到 client local 的未提交修改;
  • server 在 Store 期间 crash,client 需要处理 retry 或错误;
  • callback 断开、重连和 cache validity 需要 recovery protocol;
  • 多个 client 的 concurrent update 可能造成最后关闭者覆盖先前版本。

如果 client 在 reboot 期间收不到 server 发出的 callback break,它重新加入后必须把本地 cache 视为 suspect;下一次访问时先用 TestAuth 验证 cached copy,确认仍有效后才能使用,否则重新 Fetch。server crash 更复杂:callback 本身保存在 server memory 中,重启后 server 不知道哪些 client 缓存了哪些 file。因此所有 client 都需要意识到 server 曾经 crash,并重新验证 cache;实现方式可以是 server 重启后主动通知 clients,也可以是 client 使用 heartbeat 检查 server 是否存活。

AFS 的 cache consistency 是可理解的,但它不是强一致性,也不提供自动 merge。application 若需要并发编辑合并、版本控制或 transaction semantics,必须在 file system 之上另行实现。

cache consistency 不是 panacea。代码 repository、共享编辑器或 transaction database 等 application 若真的关心 concurrent update,仍需显式 file locking、version control 或 conflict-resolution;仅依赖 close-to-open 只能保证一种可理解的 baseline behavior,不能保证业务层面的“正确合并”。

这体现了 AFS 的典型 trade-off:把更多 work 和 storage 放到 client,换取 server scalability;代价是 close-to-open 规则、last closer wins 和 callback recovery 必须让用户能够理解。whole-file cache 对共享写入密集型 workload 并不理想,但对大量 read-mostly、跨多台 machine 重复读取的 workload 很有效。

Workload 决定性能

AFS 与 NFS 的差异可以用 L_net > L_disk > L_mem 解释:第一次读取通常都受远端 network cost 主导;大文件 sequential re-read 时,AFS 可从 local disk cache 读取,而 NFS 若 client memory 放不下则需要重新从 server 获取。相反,AFS sequential overwrite 会先 Fetch 整个旧 file,再 Store 整个新 file;NFS 可以按 block 覆盖。只读或只写大文件中的一个小范围时,AFS 也可能浪费地传输整个 file,而 NFS 的 block granularity 更合适。

因此 AFS 的设计假设——大多数 file 不频繁共享,并且通常按整体 sequential access——对 read-mostly workload 很合理;但 periodic append 的大型 log、random update 的 transaction database 等 workload 会暴露 whole-file caching 的成本。系统设计必须先测量真实 workload,再决定 cache granularity 和 consistency 语义。

原文 Figure 50.4 的定性比较如下。设 small/medium/large file 分别包含 N_s、N_m、N_L 个 blocks,且 L_net > L_disk > L_mem;表中只保留原文的主要数量级:

WorkloadNFSAFSAFS/NFS 的主要结论
small file,sequential readN_s · L_netN_s · L_net约相同
small file,sequential re-readN_s · L_memN_s · L_mem约相同
medium file,sequential readN_m · L_netN_m · L_net约相同
medium file,sequential re-readN_m · L_memN_m · L_mem约相同
large file,sequential readN_L · L_netN_L · L_net首次读取由 network 主导
large file,sequential re-readN_L · L_netN_L · L_diskAFS 利用 local-disk cache
large file,single readL_netN_L · L_netNFS 只取所需 block
small file,sequential writeN_s · L_netN_s · L_net约相同
large file,sequential writeN_L · L_netN_L · L_net约相同
large file,sequential overwriteN_L · L_net2 · N_L · L_netAFS 先 Fetch、后 Store 整个 file
large file,single writeL_net2 · N_L · L_netNFS 的 block granularity 更有利

Scale 与 performance

AFSv2 的 protocol redesign 让一个 server 的可支持 client 数量从 AFSv1 的约 20 个提高到约 50 个;在常见 cache-hit 情形下,client performance 也可以接近 local file system,因为 read 通常只访问 client local disk(甚至 local memory),只有 create 或修改后的 close 才需要向 server 发送 Store。

用 L_net > L_disk > L_mem 做粗略分析:第一次访问未命中任何 cache 时,AFS 与 NFS 都会受 remote network latency 主导;大 file 的重复读取若超出 client memory,AFS 可以从 local disk cache 读取,而 NFS 可能重新从 server 获取,因此 AFS 更有优势。另一方面,AFS 修改大 file 中很小的一段时,可能先 Fetch 整个 file、close 时再 Store 整个 file;NFS 的 block granularity 反而更节省。AFS 的优势依赖 read-mostly、整体访问和较少 concurrent sharing 的 workload。

Global namespace 与 access control

AFS 还提供 global namespace,使不同 client 对同一 file 使用相同 pathname;NFS 允许各 client 以不同方式 mount server,统一命名往往需要额外的 administration。AFS 也提供更灵活的 user-managed access control 和 authentication。由此可见,distributed file system 的 usability 不只由 cache protocol 决定,也包括 namespace、security 和 administrator tooling。

学习检查:afs.py simulator

官方 afs.py simulator 可用于追踪 callback 与 last-closer-wins:

  1. 改变 random seed、文件数 -f、client 数 -C、read ratio -r,预测每一步 client 读到的 value 和最终 server file。
  2. 使用 -c -d 3 观察 server 何时发起 callback,归纳 callback 发生的精确条件。
  3. 使用 -c -d 7 预测每一步 local cache state。
  4. 用 -A oa1:w1:c1,oa1:r1:c1,比较随机 scheduler 下 client 1 可能读到的值,并枚举不同 interleaving。
  5. 固定 -S 01、-S 100011、-S 011100 等 schedule,比较 schedule 如何决定 read value 和最终 file。
  6. 使用 -A oa1:w1:c1,oa1:w1:c1,改变 schedule;特别比较 -S 011100 与 -S 010011,观察两个 client 都写同一个 file 时最后的结果,并说明为什么最后 close 的 client 决定 server 上的完整版本。

原文逐段复核补充

AFS 的历史与设计方法

AFS 于 1980s 在 Carnegie-Mellon University(CMU)引入,由 M. Satyanarayanan 主导,核心目标始终是 scale:一个 server 如何支持尽可能多的 client。原始 AFSv1 实际称为 ITC distributed file system,已经包含 whole-file caching 的基本设计,但未达到预期 scale,测量结果促成了 AFSv2 的 redesign。这里体现了一个贯穿 systems research 的方法:先用实验测量真实 workload 和 bottleneck,再设计 protocol 改进,而不是凭直觉添加机制。

AFSv1 的精确访问路径

AFSv1 第一次访问 file 时,client 会 Fetch 整个 file,将其写到 client local disk;后续 read() 与 write() 直接重定向到该 local copy,并且这些 local read 还可能进一步命中 client memory cache。close() 时,如果 file 曾以 writing mode 打开并被修改,client 发送包含完整 file 与 pathname 的 Store message。

AFSv1 的 TestAuth 只检查 cached file 是否改变;directory 在这一版本中仍只保存在 server,不像 AFSv2 那样被 client cache。这个限制与“每次需要 server pathname traversal”共同造成了 server-side workload。

Patterson’s Law

AFS 设计者先对 prototype 做测量,发现两个主要问题:Fetch/Store 携带 pathname,server 每次都要从 root walk pathname;client 又频繁发送 TestAuth,尽管绝大多数答案都是“file 没有变化”。另外,server 间 load 不平衡,以及每个 client 一个独立 process 带来的 context switching 与 management overhead,也会限制 scale。AFSv2 用 client-side pathname traversal/FID、callback、volume placement 与 threads 分别处理这些问题。

这就是原文所谓 Patterson’s Law:Measure then build。measurement 不仅证明问题真实存在,也迫使设计者先明确如何测量改进后的系统,避免“看起来更好”却无法证明的优化。

AFSv2 的 pathname 与 callback 流程

访问 /home/remzi/notes.txt 时,client 可先 Fetch home directory 的 contents,并在 home 上建立 callback;再使用 home 的本地结果 Fetch remzi,建立第二个 callback;最后 Fetch notes.txt,缓存 regular file 并建立第三个 callback。返回 application file descriptor 前,client 已经把相关目录和 file 纳入 local cache。

下一次 open() 时,client 依次检查 home、remzi 和 notes.txt 的 callback 是否仍 valid;若都有效,整个 pathname traversal 与 open 都在本地完成;只有 callback 被 break 才重新 Fetch。于是 AFS 把 NFS 式“每隔一段时间 polling”变成 callback 驱动的 interrupt-like notification。

Cache consistency 的精确语义

AFS 在不同 machine 之间让“server 可见新版本”和“旧 cached copy 被 invalidate”发生在 file close 时:writer close 后,完整 file flush 到 server,server break 其他 client 的 callback;这些 client 下一次 open 时必须重新 Fetch,并为新版本建立 callback。相同 machine 上的 processes 则仍立即看到彼此的写入,保持普通 UNIX 语义。

跨 machine 同时修改同一 file 时,AFS 不做 merge,而是采用 last writer wins(更准确地说是 last closer wins)。最后 close 的 client 将整个 file 写回 server,覆盖较早 close 的完整版本。这个 baseline consistency 适合用户在不同 machine 间切换、希望看到“合理版本”的 casual usage;code repository、并发编辑器和 transaction database 等真正关心冲突的 application,仍需显式 locking、version control 或 conflict-resolution。

为什么 AFSv2 的 scale 更好

AFSv2 的实测结果约为每个 server 支持 50 个 client,而 AFSv1 约为 20 个。常见 cache-hit 路径接近 local file-system performance:file 通常从 client local disk 读取,还可能命中 client memory;只有新建 file 或 close 修改后的 file 才需要 Store 到 server。代价是 server crash 后 callback state 更难恢复:server 重启会忘记哪些 client 持有 callback,client 必须重新确认 cache validity,因此 AFS 相比 NFS 以更复杂的 recovery 换取更少的常规 server interaction。

Workload table 的边界

L_net > L_disk > L_mem 只是粗粒度的性能模型。首次读取 small/medium/large file 时,AFS 与 NFS 都由 network fetch 主导;大型 file 的 sequential re-read 若超出 client memory,AFS 可以从 local disk cache 读取,而 NFS 可能重新从 server 获取。可是只访问大型 file 中很小的一段时,NFS 的 block granularity 更有优势:AFS 必须 Fetch 整个 file,若发生修改还要 Store 整个 file。原文还假设 NFS write 是 block-sized、block-aligned,且 AFS open 没有 O_TRUNC;否则比较结果需要调整。

AFS 的长期影响

AFS 的 single namespace、security、access-control lists 和 administrator tooling 都改善了 distributed file-system 的 usability。它后来在 marketplace 上被 NFS 与 Windows-oriented CIFS 压过,但设计思想仍有影响;尤其 NFSv4 引入了 server state(例如 open protocol message)后,与 AFS 的基本 protocol 越来越相似。换句话说,AFS 的 lasting influence 很可能更多来自 whole-file caching、callback、namespace 和 client/server state 的思想,而不是原系统本身的市场占有率。

原文总结

AFS 通过 whole-file caching 与 callbacks 尽量减少 server interaction,使单个 server 支持更多 client,并减少站点需要维护的 server 数量。它的 single namespace、security 与 ACL 让系统易用,close-to-open consistency 也比 NFS 的 timeout-based behavior 更容易理解;代价是 callback recovery、whole-file transfer 和 last-closer-wins 在共享写入 workload 中可能不理想。

源文档核验记录

  • 源文档:OSTEP Version 1.10 官方 dist-afs.pdf。
  • 页数:14 页(含正文与 References)。
  • 章节范围:50.1 AFS Version 1、50.2 Problems with Version 1、50.3 Improving the Protocol、50.4 AFS Version 2、50.5 Cache Consistency、50.6 Crash Recovery、50.7 Scale And Performance of AFSv2、50.8 AFS in the Real World,以及 Summary。
  • 图件核对:Figure 50.1–50.4 的 AFSv1 protocol、AFSv2 client/server path、cache-consistency timeline 和 AFS/NFS workload comparison 已在对应位置以表格、Mermaid 和时序说明重绘;原 PDF 没有需要另存的独立 raster 图片。
  • 内容核对:已覆盖 CMU/ITC 历史、whole-file/local-disk cache、Venus/Vice、TestAuth、Patterson’s Law、path traversal、callback、FID、volume/threads、close-to-open、last-closer-wins、client/server crash recovery、L_net > L_disk > L_mem workload comparison、global namespace/access control、afs.py 实验题与完整官方 References。

原文 References

  • [B+91] Mary Baker、John Hartman、Martin Kupfer、Ken Shirriff、John Ousterhout,《Measurements of a Distributed File System》,SOSP ’91,1991。
  • [H+11] Tyler Harter、Chris Dragga、Michael Vaughn、Andrea C. Arpaci-Dusseau、Remzi H. Arpaci-Dusseau,《A File is Not a File: Understanding the I/O Behavior of Apple Desktop Applications》,SOSP ’11,2011。
  • [H+88] John H. Howard、Michael L. Kazar、Sherri G. Menees、David A. Nichols、M. Satyanarayanan、Robert N. Sidebotham、Michael J. West,《Scale and Performance in a Distributed File System》,ACM Transactions on Computer Systems,Vol. 6,No. 1,1988。
  • [R+00] Drew Roselli、Jacob R. Lorch、Thomas E. Anderson,《A Comparison of File System Workloads》,USENIX ’00,2000。
  • [S+85] M. Satyanarayanan、J. H. Howard、D. A. Nichols、R. N. Sidebotham、A. Spector、M. J. West,《The ITC Distributed File System: Principles and Design》,SOSP ’85,1985。
  • [V99] Werner Vogels,《File System Usage in Windows NT 4.0》,SOSP ’99,1999。

设计取舍

设计优点代价
whole-file cache后续 local I/O 快,减少 network calls大文件 fetch/store 成本高
local-disk cacheclient memory 压力较小,cache 可持久cache eviction 与 crash 处理更复杂
callback减少轮询与 server loadserver 维护 callback state
FID减少 server pathname traversalclient 需要执行和 cache traversal
close-to-opensemantics 简单易懂close 前远端不可见
last writer wins避免 block-level 混合 fileconcurrent updates 可能丢失

关键结论

  • AFS 首要目标是通过 protocol 和 caching 提高 distributed file system scalability。
  • whole-file caching 让 open 后的 read/write 主要在 client local disk 上完成。
  • callback 以 server 主动通知替代 client 周期性 TestAuth。
  • FID 让 client 可以 cache pathname traversal 结果,减少 server CPU。
  • AFS 的 close-to-open 和 last writer wins 语义简单,但不是强一致性或冲突合并。
  • 与 NFS 的比较说明:cache granularity、cache location 和 consistency protocol 会共同决定 scalability。

与已有笔记的关系

参考资料

← 返回列表
← 上一篇 OSTEP:Sun's Network File System(NFS) 下一篇 → OSTEP:Introduction to Operating System Security

评论 (0)

暂无评论,来留下第一条吧。
登录 或 注册 后才能发表评论