什么是NFS(What is NFS)

  • NFS是一个由SUN在1980年代创建的平台无关的远程文件系统。
  • NFS is a platform independent remote file system technology created by SUN in the 1980s.
  • 它是一个为客户端提供跨网络的文件共享存储客户端-服务器应用。
  • It is a client/server application that provides shared file storage for clients across a network.
  • 它旨在简化同一网络中非同类机器间中文件系统资源的共享。
  • It was designed to simplify the sharing of filesystems resources in a network of non-homogeneous machines.
  • 其基于RPC协议实现,并且文件可以通过网络基于虚拟文件系统(VFS)访问。RPC是一个运行在TCP/IP层的接口。
  • It is implemented using the RPC protocol and the files are available through the network via a Virtual File System (VFS), an interface that runs on top of the TCP/IP layer.
  • 允许应用程序像访问本地文件一样访问远程主机上的文件。
  • Allows an application to access files on remote hosts in the same way it access local files.

NFS 服务器: 分享文件的计算机

  • 在20世纪80年代末和90年代期间,一种常见的配置是配置一个功能强大的工作站,其中包含大量本地磁盘,并且通常没有图形显示器作为NFS服务器。
  • During the late 1980s and 1990s, a common configuration was to configure a powerful workstation with lots of local disks and often without a graphical display to be a NFS Server.
  • 然后,“精简”无盘工作站将挂载NFS服务器提供的远程文件系统,并透明地使用它们,就好像它们是本地文件一样。
  • "Thin," diskless workstations would then mount the remote file systems provided by the NFS Servers and transparently use them as if they were local files.


  • NFS不是在每个系统上复制/usr/local等公共目录,而是提供网络上所有系统共享的目录的单个副本。
  • Instead of duplicating common directories such as /usr/local on every system, NFS provides a single copy of the directory that is shared by all systems on the network.
  • 简化备份过程 – 使用NFS,sysadm只需备份服务器的磁盘,而不是为每个工作站的本地内容(以/home为例)设置备份。
  • Simplify backup procedures – Instead of setting up backup for the local contents of each workstation (of /home for exmaple), with NFS a sysadm needs to backup only the server's disks.

NFS 客户端: 访问被共享文件的计算机

  • NFS在客户端使用内核支持和用户空间守护进程的混合。
  • NFS uses a mixture of kernel support and user-space daemons on the client side.
  • 多个客户端可以挂载相同的远程文件系统,以便用户可以共享文件。
  • Multiple clients can mount the same remote file system so that users can share files.
  • 挂载可以在启动时完成。(即/home可以是用户登录时由每个客户端安装的共享目录)。
  • Mounting can be done at boot time. . /home could be a shared directory mounted by each client when user logs in).
  • 一个NFS客户端
  • An NFS client
  • (a)挂载一个远程文件系统到客户端的本地文件系统名字空间,并且
  • (a) mounts a remore file system onto the client's local file system name space and
  • (b)提供一个接口以便于访问远程文件系统中的文件,就好像它们是本地文件一样。
  • (b) provides an interface so that access to the files in the remote file system is done as if they were local files.

NFS设计的目标(Goals of NFS design)

1. 兼容性(Compatibility):

NFS应提供与本地Unix文件系统相同的语义。程序不应该或无法判断文件是远程文件还是本地文件。用户程序:OPEN(“/users/jdoe/.profile”,READONLY) – 程序无法判断“users”或“jdoe”是否为本地路径名。

NFS should provide the same semantics as a local unix file system. Programs should not need or be able to tell whether a file is remote or local. user program: OPEN("/users/jdoe/.profile", READONLY) — program cannot tell whether "users" or "jdoe" are local path names.

2. 易部署(Easy deployable):


implementation should be easily incorporated into existing systems remote files should be made available for local programs without these having to be modified or relinked.

3. 机器和操作系统无关(Machine and OS independence):


NFS Clients should run in non-unix platforms Simple protocols that could be easily implementend in other platforms.

4. 效率(Efficienty):


NFS should be good enough to satisfy users, but did not have to be as fast as local FS. Clients and Servers should be able to easily recover from machine crashes and network problems.

NFS的版本(NSF Versions)

  • Version 1: 仅在SUN公司内部使用
  • Version 1: used only inside Sun Microsystems.
  • Version 2: 1987年发布(RFC1989)
  • Version 2: Released in 1987 (RFC 1989)
  • Version 3: 1995年发布
  • Version 3: Released 1995
  • Version 4: 2000年发布
  • Version 4: Released 2000

NFS的设计(NFS design)

NFS协议(NFS Protocol)

  • 使用远程过程访问(RPC)机制
  • Uses Remote Procedure Call (RPC) mechanisms
  • RPC是同步的(客户端程序将阻塞等待服务端应答)
  • RPCs are synchronous (client application blocks while waits for the server response)
  • NFS使用一种无状态协议(服务器端不跟踪过去请求。译者注:在NFS4已经是有状态了)- 这将简化崩溃后的恢复。所需要做的仅仅是重新提交最后一个请求。
  • NFS uses a stateless protocol (server do not keep track of past requests) – This simplify crash recovery. All that is needed to resubmit the last request.
  • 这种方式下,客户端无法对崩溃后恢复的服务器和运行慢的服务器进行区分。
  • In this way, the client cannot differentiate between a server that crashed and recovered and one that is just slow.

新文件系统接口(New File system interface)

  • 修改了原始Unix文件系统接口,以便将NFS实现为Unix文件系统的扩展。
  • The original Unix file system interface was modified in order to implement NFS as an extension of the Unix file system.
  • 通过将通用文件系统操作与特定实现分离,NFS被内置到Unix内核中。有了这个,内核可以以相同的方式处理所有文件系统和节点,并且可以轻松地将新文件系统添加到内核中
  • NFS was built into the Unix kernel by separating generic file systems operations from specific implementations. With this the kernel can treat all filesystems and nodes in the same way and new file systems can be added to the kernel easily:
  • 虚拟文件系统(VFS)接口:定义可以在文件系统上执行的操作。
  • A Virtual File System (VFS) interface: defines the operations that can be done on a filesystem.
  • 虚拟节点(vnode)接口:定义可以对文件系统中的文件执行的操作。
  • A Virtual node (vnode) interface: defines the operations that can be done on a file within a filesystem.
  • vnode是一种逻辑结构,用于抽象文件或目录是由本地文件系统还是远程文件系统实现的。从这个意义上讲,应用程序必须“只”看到vnode接口,文件的实际位置(本地或远程文件系统)与应用程序无关。
  • A vnode is a logical structure that abstracts whether a file or directory is implemented by a local or a remote file system. In this sense, applications had to "see" only the vnode interface and the actual location of the file (local or remote file system) is irrelevant for the application.
  • 此外,该接口允许计算机透明地访问本地不同类型的文件系统(即ext2,ext3,Reiserfs,msdos,proc等)。
  • In addition, this interface allows a computer to transparently access locally different types of file systems . ext2, ext3, Reiserfs, msdos, proc, etc).

NFS客户端(NFS Client)


Uses a mounter program. The mounter:

1. 通过一个远程文件系统识别信息-主机:路径;

1. takes a remote file system identification host:path;

2. 将RPC发送到主机并请求一个(1)路径的文件句柄和(2)服务器网络地址。

2. sends RPC to host and asks for (1) a file handle for path and (2) server network address.

3. 将本地文件系统中的挂载点标记为与主机地址:路径对关联的远程文件系统。

3. marks the mount point in the local file system as a remote file system associated with host address:path pair.

图2 NFS架构图

NFS远程过程调用(NFS Remote Procedure Calls)


NFS client uses RPCs to implement each file system operation.


Consider the user program code below:

fd <- OPEN ("f", READONLY)
READ (fd, buf, n)
CLOSE (fd)

  • 一个应用程序打开文件"f",发送一个读请求,然后关闭该文件。
  • An application opens file "f" sends a read request and close the file.
  • 该文件"f"是一个远程文件,但该信息与应用程序无关。(译者注:也就是说远程信息细节对于应用程序来说是透明的,应用程序访问该文件与访问本地文件毫无差别。)
  • The file "f" is a remote file, but this information is irrelevant for the application.
  • 虚拟文件系统维持这一个所有已经挂载远程文件系统的主机地址与文件句柄(dirfh)的映射。
  • The virtual file system holds a map with host address and file handles (dirfh) of all the mounted remote file systems.
  • 如下是获取文件的步骤:
  • The sequence of steps to obtain the file are listed below:

1. 虚拟文件系统发现文件"f"在远程文件系统,并且发送请求到NFS客户端。

1. The Virtual File System finds that file "f" is on a remote file system, and passes the request to the NFS client.

2. NFS客户端向NFS服务端发送一个查找请求(LOOKUP(dirth, "f"),将文件句柄(dirth)和要读的文件名传递给远程文件系统。

2. The NFS client sends a lookup request (LOOKUP(dirth, "f") for the NFS Server, passing the file handler (dirth) for the remote file system and file name to be read.

3. NFS服务器接收LOOKUP请求,从dirth中提取文件系统标识符和inode号,并要求标识的文件系统在dirth中查找inode号并查找本地目录inode信息。

3. The NFS server receives LOOKUP request, extracts the file system identifier and inode number from dirth, and asks the identified file system to look up the inode number in dirth and find the local directory inode information.

4. NFS服务器在由inode编号标识的目录中搜索文件"f"。 如果找到文件,服务器会为"f"创建一个句柄并将其发送回客户端。

4. The NFS server searches the directory identified by the inode number for file "f".

If file is found, the server creates a handle for "f" and sends it back to the client.

5. NFS客户端在程序的文件描述符表中分配第一个未使用的条目,在该条目中存储对f的文件句柄的引用,并将条目的索引(fd)返回给用户程序。

5. The NFS client allocates the first unused entry in the program's file descriptor table, stores a reference to f's file handle in that entry, and returns the index for the entry (fd) to the user program.

6. 接下来,用户程序调用READ(fd,buf,n)。

6. Next, the user program calls READ(fd, buf, n).

7. NFS客户端发送RPC READ(fh,0,n)。

7. The NFS client sends the RPC READ(fh,0,n).

8. NFS服务器通过fh查找其inode,读取数据并将其以回复消息的方式发送。

8. The NFS server looks up the inode for fh, reads the data and send it in a reply message.

9. 当用户程序调用以关闭文件(CLOSE(fd))时,NFS客户端不会发出RPC,因为程序没有修改该文件。

9. When the user program calls to close the file (CLOSE(fd)), the NFS client does not issue an RPC, since the program did not modify the file.