- Written
- Published
- Updated
Lab: file system
This is a little complex. Since there a a lot of data structures and locks.
File system implementation. Five layers:
-
Blocks: allocator for raw disk blocks.
-
Log: crash recovery for multi-step updates.
-
Files: inode allocator, reading, writing, metadata.
-
Directories: inode with special contents (list of other inodes!)
-
Names: paths like
/usr/rtm/xv6/fs.cfor convenient naming.
Hardware interface
virtio_disk_rw() in virtio_disk.c is hardware interface to read/write a block to disk.
Using this we can have ability of reading or writing a block in disk.
Buffer cache layer(Blocks)
When we want to operate on blocks in disk, we first need to load it into main memory. Thus we need a data structure to manage these blocks in memory.
We call this data structure buffer cache.
struct {
struct spinlock lock;
struct buf buf[NBUF];
// Linked list of all buffers, through prev/next.
// Sorted by how recently the buffer was used.
// head.next is most recent, head.prev is least.
struct buf head;
} bcache;
It is a double linked list, each element is called buf with a block in it.
struct buf {
int valid; // has data been read from disk?
int disk; // does disk "own" buf?
uint dev;
uint blockno;
struct sleeplock lock;
uint refcnt;
struct buf *prev; // LRU cache list
struct buf *next;
uchar data[BSIZE];
};
When we try to find a block we need to search in this bache list. Thus we need to first get the lock in bcache. After find the correct buf, we need acquire the sleeplock in buf before we access the data in that buf. NOTE: we need first release lock in bache since when process sleep, it can't hold spin lock.
We use refcnt in buf to indicate reference number of such block in memory. When it reduces to zero, it will be reused or mapped by other block in disk if need.
Log layer
begin_op() and end_op() are used to open a transaction in operating disk.
In the internal of them, use log_write() to log these write operations and end_op() will commit these operations.
begin_op()
...
log_write(blockA)
...
log_write(blockB)
...
end_op()
Inode (file) layer
We use a inode to store a file structure.
// in-memory copy of an inode
struct inode {
uint dev; // Device number
uint inum; // Inode number
int ref; // Reference count
struct sleeplock lock; // protects everything below here
int valid; // inode has been read from disk?
short type; // copy of disk inode
short major;
short minor;
short nlink;
uint size;
uint addrs[NDIRECT+2];
};
Inode is a data in disk, we need copy it into memory and use a data structure to manage it.
struct {
struct spinlock lock;
struct inode inode[NINODE];
} itable;
Like buffer cache, when we want to get a inode we first need hold lock in itable to find it. Then release it and get lock in inode to access the data.
There is also reference number of a inode i.e. ref. Every time we need access inode we need increase it. After using, we should use iput() to decrease reference.
Directory layer
A directory is implemented internally much like a file. Its inode has type T_DIR and its data is a sequence of directory entries. Each entry is a struct dirent (kernel/fs.h:56), which contains a name and an inode number.
See book xv6 8.11 for more information.
File descriptor layer
Xv6 gives each process its own table of open files, or file descriptors, as we saw in Chapter 1. Each open file is represented by a struct file (kernel/file.h:1), which is a wrapper around either an inode or a pipe, plus an I/O offset.
All the open files in the system are kept in a global file table, the ftable.
See book xv6 8.13 for more information.
Lab: mmap
The manual page (run man 2 mmap) shows this declaration for mmap:
void *mmap(void *addr, size_t len, int prot, int flags,
int fd, off_t offset);
mmap can be called in many ways, but this lab requires only a subset of its features relevant to memory-mapping a file. You can assume that addr will always be zero, meaning that the kernel should decide the virtual address at which to map the file. mmap returns that address, or 0xffffffffffffffff if it fails. len is the number of bytes to map; it might not be the same as the file's length. prot indicates whether the memory should be mapped readable, writeable, and/or executable; you can assume that prot is PROT_READ or PROT_WRITE or both. flags will be either MAP_SHARED, meaning that modifications to the mapped memory should be written back to the file, or MAP_PRIVATE, meaning that they should not. You don't have to implement any other bits in flags. fd is the open file descriptor of the file to map. You can assume offset is zero (it's the starting point in the file at which to map).
Include order
When c compiler try to compiler a file like proc.c and it has
#include "types.h"
#include "param.h"
#include "memlayout.h"
#include "spinlock.h"
#include "sleeplock.h"
#include "file.h"
#include "proc.h"
The C preprocessor (cpp) literally replaces each #include with the full text of that file — like copy-pasting.
So after preprocessing, the compiler sees one giant file that looks like this (simplified):
// types.h content...
typedef unsigned int uint;
// param.h content...
#define NPROC 64
// spinlock.h content...
struct spinlock { ... };
// file.h content...
struct file { struct sleeplock lock; ... };
// proc.h content...
struct proc { struct file *ofile[NOFILE]; ... };
If any of these appear before the required definitions (e.g., you use struct spinlock before it’s defined), you get errors like:
error: field ‘lock’ has incomplete type
So you must keep the include order correct for every source code file(.c).
Scheduling
Here are no lab for Scheduling but it is a significant part of OS.
See xv6 book chapter 7 for more information.
Comments
No approved comments yet.
Your display name and plain-text comment become public only after owner approval. No account or email is required.