Homework: Files and Disk I/O

Read: readi, writei, fileread, filewrite, create, and dirlink, and code related to these calls in fs.c, bio.c, ide.c, file.c, and sysfile.c

This homework should be turned in at the beginning of lecture.

Part 1 (3 marks)

Insert a print statement in bwrite so that you get a print every time a block is written to disk:

  cprintf("bwrite sector %d\n", b->sector);

Build and boot a new kernel and run these three commands at the shell:

  echo > a
  echo x > a
  rm a
  mkdir d
(Try rm d if you are curious; it should look much the same as rm a.)

You should see a sequence of bwrite prints after running each command. Record the list and annotate it with the calling function and what block is being written (for example, "file's i-node block", "file's data block", &c). Hand in the annotated list.

Hint: an easy way to get the name of the calling function is to add a string argument to bwrite, edit all the calls to bwrite to pass the name of the calling function, and just print it. You should be able to reason about what kind of block is being written just from the calling function.

You need not write the following up, but try to understand why each write is happening. This will help your understanding of the file system layout and the code.

Part 2 (1 mark)

Look at bget() and brelease(). Which replacement policy does the buffer cache implement? Is the buffer cache write-back or write-through?

Part 3 (2 marks)

What do flags B_BUSY and B_VALID indicate? Why does bget() panic if there are no free blocks? Could we sleep/wait instead of panicking?

Part 4 (2 marks)

If the block is not in cache, it is read from disk. Are disk block reads synchronous or asynchronous? Why does the ide driver need a queue?

Part 5 (1 mark)

What does ilock(ip) after ialloc() do? How does ilock() implement locks? Is it a spinlock?

Part 6 (1 mark)

For the rm command, why is the old block written (at iupdate(ip)) in the sys_unlink() function?

Part 7 (no marks)

Read and understand the code here to update the "grades" file atomically with respect to crashes.


This completes the homework.