Lecture: File system

preparation

overview

int fd = open("/f", O_RDWR | O_CREAT, 0644);
write(fd, "hello", 5);
close(fd);

storage devices

I/O stacks

data structures

crash safety

      inodes
      +---------------+
      | inode 1:      |
      | type = T_DIR  |
      | sz = 48       |
      | addrs[0] = 0  | --+
      | ...           |   |
      +---------------+   |
  +-> | inode 2:      |   |
  |   | type = T_FILE |   |
  |   | sz = 0        |   |
  |   | ...           |   |
  |   ~~~~~~~~~~~~~~~~~   |
  |                       |
  |   data blocks         |
  |   +---------------+   |
  |   | data block 0: | <-+
  |   | ...           |
  +-- | (2, "f")      |
      | ...           |
      ~~~~~~~~~~~~~~~~~
      inodes
      +---------------+
      | inode 1:      |
      | type = T_DIR  |
      | sz = 64       |
      | addrs[0] = 0  | --+
      | ...           |   |
      +---------------+   |
  +-> | inode 2:      |   |
  |   | type = T_FILE |   |
  |   | sz = 0        |   |
  |   | ...           |   |
  |   +---------------+   |
+---> | inode 3:      |   |
| |   | type = T_FILE |   |
| |   | sz = 0        |   |
| |   | ...           |   |
| |   ~~~~~~~~~~~~~~~~~   |
| |                       |
| |   data blocks         |
| |   +---------------+   |
| |   | data block 0: | <-+
| |   | ...           |
| +-- | (2, "f")      |
+---- | (3, "newf")   |
      | ...           |
      ~~~~~~~~~~~~~~~~~
log header     | payload (LOGSIZE blocks)
+--------------+-------------------------
| n=0          | ...
+--------------+-------------------------

write data v1 and v2 (log is still considered empty given n=0 in header):
+--------------+----+----+---------------
| n=0          | v1 | v2 | ...
+--------------+----+----+---------------

update log header with # of writes and addresses (log is considered complete):
+--------------+----+----+---------------
| n=2 (a1, a2) | v1 | v2 | ...
+--------------+----+----+---------------

... write a1 with v1 and a2 with v2 ...

reset log:
+--------------+-------------------------
| n=0          | ...
+--------------+-------------------------

Q&A

int fd = open("file.tmp", ...);
write(fd, newdata, newdatasize);
close(fd);
rename("file.tmp", "file");