Network and distributed file systems
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
A network file system lets a program open a file that lives on another computer's disk, with the same open() and read() calls it uses for local files. Follow one NFS client as it looks up a file, survives a server crash by simply asking again, and then reads stale data from its own cache after another client changes the file. You will see why a stateless server makes crashes easy and why caching makes consistency hard. It builds on Files, directories and file descriptors, Inodes and data blocks, and The buffer cache and writing back.
A file on another computer
Your program opens /nfs/a, but the file lives on a server's disk across the network. The kernel's NFS client turns each file call into a request message. The server answers each request and then forgets it.
client A server
program open, read, ... NFS server
kernel NFS client ----> disk: /nfs/a = milk
(a file system
that talks)
/nfs is a mount: the server's exported directory
appears inside the client's own directory tree.اقرأ الدرس خطوة بخطوة
A file on another computer
Your program opens /nfs/a, but the file lives on a server's disk across the network. The kernel's NFS client turns each file call into a request message. The server answers each request and then forgets it.
client A server program open, read, ... NFS server kernel NFS client ----> disk: /nfs/a = milk (a file system that talks) /nfs is a mount: the server's exported directory appears inside the client's own directory tree.open() sends LOOKUP
open() first needs to know which file a is. The NFS client sends LOOKUP with two things: the handle of the directory /nfs, which it got when it mounted the server, and the name a.
line 1 fd = open("/nfs/a", O_RDONLY); LOOKUP(dir handle of /nfs, "a") dir handle from the mount, when /nfs was set up name "a" returns: file handle, attributesThe file handle
The server finds a in that directory on its disk and replies with its file handle: a volume number, an inode number and a generation number. The client notes that descriptor 3 means this handle. The server notes nothing.
file handle (vol 1, inode 12, gen 1) volume which exported file system inode which file in it generation bumped when an inode number is reused, so an old handle cannot reach a new file client A fd 3 -> handle, file position 0 server remembers nothing about client Aread(): the server crashes
read() becomes READ(handle, offset 0, count 4). Everything the server needs is inside the message. But the server has just crashed, so the request is lost and no reply comes back.
line 2 read(fd, buf, 4); READ((vol 1, inode 12, gen 1), offset 0, count 4) the client tracks the file position; the message carries it as an offset server down: request lost, no replyRetry after a timeout
The client's timer runs out, so it sends the same READ again. The rebooted server has forgotten everything and needs nothing: the handle, offset and count are all in the request. Reading twice gives the same answer, so retrying is safe.
client timer runs out -> send the same READ again (Linux over TCP: 60 s, timeo=600; a hard mount retries forever) server rebooted, no state to rebuild idempotent: doing it twice = doing it once LOOKUP, READ only read WRITE carries the exact offsetData, and a cached copy
The server reads 4 bytes, milk, from its disk and replies with the data and the file's attributes, including when it was last modified. The client keeps a copy in its cache, and read() returns 4.
reply data "milk", attributes (modified: version 0) cache milk, attributes checked at t = 0 s read() returns 4 (bytes)Another client writes
Client B writes eggs at offset 0 and closes the file. On close, its NFS client sends WRITE(handle, offset 0, eggs). The server puts the bytes on its disk before it replies, so a server crash cannot lose them.
client B write eggs, close() flush-on-close: dirty data goes to the server WRITE((vol 1, inode 12, gen 1), offset 0, "eggs") server disk: /nfs/a = eggs (modified: version 1) then replies success client A cache still says milkA stale read
One second later, A reads again. Its cached attributes are under 3 seconds old, so the cache answers and nothing is sent. pread() returns milk: stale data, while the server already holds eggs.
line 3 pread(fd, buf, 4, 0); // 1 s t = 1 s attributes checked 1 s ago 1 < 3: trust the cache, no message pread() returns milk (server has eggs)Checking with GETATTR
At 5 seconds the cached attributes are too old. Before using its copy, the client sends GETATTR. The reply shows the file was modified after the copy was made, so the client throws the cached data away.
line 4 pread(fd, buf, 4, 0); // 5 s t = 5 s 5 >= 3: check with the server GETATTR((vol 1, inode 12, gen 1)) reply modified: version 1 cache has version 0 client drops the cached dataFresh data
So the client sends READ(handle, offset 0, count 4) once more. The server replies eggs, the client caches the new copy, and pread() returns the current data.
READ((vol 1, inode 12, gen 1), offset 0, count 4) reply data "eggs", modified: version 1 cache eggs, checked at t = 5 s pread() returns eggsSimple server, tricky cache
A stateless server makes crashes easy: clients just repeat requests that are safe to repeat. Caches make reads fast, but a client may return old data for a few seconds. NFS chooses speed, and checks with the server now and then.
the same idea as a Python simulation: disk = {12: ["milk", 0]} # server: inode -> [data, modified] def server(op, ino, data=None): # stateless: the request says it all if op == "READ": return disk[ino] if op == "GETATTR": return disk[ino][1] disk[ino] = [data, disk[ino][1] + 1] # WRITE: on disk, then reply cache = {} # client A: ino -> [data, modified, checked] def read(ino, now): if ino in cache: data, mod, checked = cache[ino] if now - checked < 3: # attributes fresh: ask nobody return data if server("GETATTR", ino) == mod: cache[ino][2] = now return data data, mod = server("READ", ino) cache[ino] = [data, mod, now] return data print(0, read(12, 0)) print("retry", server("READ", 12) == server("READ", 12)) server("WRITE", 12, "eggs") # client B writes and closes print(1, read(12, 1)) print(5, read(12, 5)) prints: 0 milk retry True 1 milk 5 eggs
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01What does an NFS file handle identify?
The handle names the file on the server exactly. The generation number changes when an inode number is reused, so an old handle cannot reach a new file. The client, not the server, keeps the file descriptor and the file position.
02The server crashes and reboots while a READ is on its way. What does the NFS client do?
Every READ carries the handle, offset and count, so a freshly rebooted server can answer it. Reading the same bytes twice gives the same result: the request is idempotent, so retrying is safe.
03Why is WRITE(handle, offset 0, "eggs") safe to retry, while "append eggs to the file" would not be?
An operation is idempotent when doing it several times has the same effect as doing it once. Writing eggs at offset 0 twice still leaves eggs at offset 0; appending twice would add the bytes twice.
04Client A read the file at time 0 and caches it. Client B then changes it. At 1 second, A reads again. With Linux's default attribute timeout of 3 seconds, what can A get?
A trusts its cached attributes for at least 3 seconds (acregmin), so it sends no message and returns its cached copy: stale data. After the timeout it sends GETATTR, sees the file changed and reads the new data.
05A program on client A writes eggs to /nfs/a and calls close(). What happens before close() returns?
This is flush-on-close (close-to-open consistency): close() sends all changes, so the next client to open the file sees them. The server must put the data on disk before it replies, because a client never resends data the server said was safe.
المسرد
- NFS
- Network File System: a protocol that lets a client use files stored on a server as if they were local. The client's kernel sends requests such as LOOKUP, READ, WRITE and GETATTR.
- File handle
- The server's name for a file, sent in every request: a volume identifier, an inode number and a generation number, kept opaque to the client.
- Stateless server
- A server that keeps no memory of its clients between requests. Each request carries everything needed to answer it, so a reboot loses nothing the clients depend on.
- Idempotent
- An operation that has the same effect whether it is done once or many times, such as reading bytes or writing bytes at a fixed offset. Such requests are safe to retry.
- Mount
- Attaching a file system, here one exported by a server, at a directory such as /nfs in the client's directory tree.
- Attributes
- A file's metadata: size, owner, permissions and the last-modified time. GETATTR fetches them; the client compares the modified time with its cached copy.
- Stale data
- An old copy of data that has since changed elsewhere, for example a client cache that still holds milk after the server's file says eggs.
- Flush-on-close
- Also called close-to-open consistency: when a program closes a file, the client sends all its changes to the server, so the next client to open the file sees them.
الملاحظات والمصادر
- The lesson follows NFS versions 2 and 3, the classic stateless design described in OSTEP. NFS version 4 adds state on the server (opens and locks), but it keeps the file handle, the client cache and the attribute checks.
- The handle values (vol 1, inode 12, gen 1) are made up for the example. Real handles are opaque bytes chosen by the server (up to 64 bytes in NFS version 3) and must stay valid across renames and server reboots; clients never look inside.
- Times are Linux defaults: cached file attributes are trusted for at least 3 seconds (acregmin) and at most 60 (acregmax), and over TCP a request waits 60 seconds before a retry. Opening the file again would have asked the server at once (close-to-open).
- In NFS version 3 a client may send faster "unstable" WRITEs that the server keeps in memory, followed by a COMMIT that forces them to disk; close() waits for that COMMIT. Either way, the data is on the server's disk before close() returns.
- The card shows the four calls that matter. The whole program (in the step details) is real C99 with POSIX calls and sleeps 1 and then 4 seconds between reads. The figure is simplified: on a real Linux mount you would see similar steps, plus a GETATTR or ACCESS at open() (close-to-open), LOOKUP often answered from the client's own name cache, READs sized to a page or more, and no crash.
- R. and A. Arpaci-Dusseau, Operating Systems: Three Easy Pieces, ch. 49: Sun's Network File System (NFS)
- Linux man-pages: nfs(5), NFS mount options (attribute cache, close-to-open, timeo, hard)
- RFC 1813: NFS Version 3 Protocol Specification
- RFC 7530: Network File System (NFS) Version 4 Protocol (OPEN, CLOSE and lease-based lock state)
- Linux kernel documentation: Making Filesystems Exportable (NFS file handles)
- Linux man-pages: open(2)
- Linux man-pages: pread(2)
- Berkeley CS162: Operating Systems and System Programming (distributed file systems: NFS, AFS)
اختبار يتاح بعد الخطوة الأخيرة.