ENع
→Network file systems
الدرس S4 · Special topics

Network and distributed file systems

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة11 خطوة5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل S4 · تفاعلي
01 / 11

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.
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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.
  2. 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, attributes
  3. The 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 A
  4. read(): 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 reply
  5. Retry 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 offset
  6. Data, 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)
  7. 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 milk
  8. A 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)
  9. 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 data
  10. Fresh 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 eggs
  11. Simple 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

اختبار يتاح بعد الخطوة الأخيرة.