Review Session 9 Outline 1. Introduction to FUSE (10 min) - VFS (3 min) - FUSE (7 min) 2. Data Structure for Lab6 (25 min) - Super block (5 min) - Free block bitmap (5 min) - Inode (10 min) - Directory (5 min) 3. Files System Operations (10 min) - Open a file (5 min) - Create a file (5 min) 1. Introduction to FUSE [Draw on board the FUSE graph on lab6] - VFS basic (3 min) VFS is short for virtual file system. It is an abstraction layer on top of a more concrete file system. The purpose of a VFS is to allow client applications to access different types of concrete file systems in a uniform way. A VFS can, for example, be used to access local and network storage devices transparently without the client application noticing the difference. - FUSE basic (7 min) FUSE is short for Filesystem in Userspace which is an operating system mechanism for letting non-privileged users create their own file systems without editing kernel code. There are two parts for FUSE: FUSE kernel module and FUSE library. FUSE kernel module is running in kernel mode and connecting VFS and user level file system driver, which is written by developers. FUSE library provides developers several interfaces to registers a set of callbacks to the FUSE system. These callbacks are things like read, write, etc. Your job is to write code for callback functions and implement the function of disk operations open/link/unlink/truncate. [Handout Ex1. If your FUSE driver code have bugs, will it crash the whole system? What if FUSE library has bugs? What if FUSE kernel module has bugs? ] No; No; Yes Put them all together: When the file system user, Process A, makes a request to the file system, such as "cat hello.txt", the cat process issues two kinds of syscalls (open, read, etc.). 1. The kernel hands the system call to VFS. 2. VFS finds that the system call is referencing a file or directory that is managed by FUSE (hello.txt in this case). 3. VFS then dispatches the request to FUSE, which dispatches it to the corresponding FUSE driver (which is where you will write your code). 4. The FUSE driver handles the request by interacting with the "disk", which is implemented as an ordinary file. The FUSE driver then responds, and the responses go back through the chain. 2. Data Structure for Lab6 (25 min) [Draw on board the overview of our fs layout] ------------------------------------------------- | Super block | Free block bitmap | Data blocks | ------------------------------------------------- - Super block (5 min) Super block is used to store important meta-data for the whole file system. Such as how many disk blocks does this file system have; where is the root directory inode; etc. In our lab, the super block is at block 0 for simplicity. See the handout for the struct of superblock. [Handout: Ex2. struct superblock { uint32_t s_magic; // Magic number: FS_MAGIC. uint32_t s_nblocks; // Total number of blocks on disk. uint32_t s_root; // Inum of the root directory inode. } __attribute__((packed)); Q: What if our file system crash and s_magic is wrong? What if s_nblocks is wrong? What if s_root is wrong? ] s_magic: the fs can not even start, since fs cannot be recognized. s_nblocks: the the number of block will be wrong. s_root: the fs cannot find where the root is, likely it will crash. - Free Block Bitmap (5 min) Free block bitmap is used to track which blocks are free to use. It uses one bit to represent one block in disk. If the bit is 0, then the corresponding block is free; Otherwise it is being used. There are 8 bits in a byte. [Handout: Ex3. Suppose "uint32_t *bitmap;" is the pointer point to the head of bitmap. And we want to check whether blockno (int blockno = 202) is free or not. Q: write a line of valid C code to check: if ( ) { printf("block 202 is used\n"); } else { printf("block 202 is free\n"); } ] Answer: bitmap[blockno/32] & (1 << (blockno%32)) - Inode (10 min) One inode represents a file. In the real file system there is an inode array. But in our lab6, for simplicity, inode is also stored in data blocks. If you want, you can do exercise 7 to implement an inode array. In current fs, one inode will consume one block which is 512B. See your handout for the inode definition of our lab. There are metadata for file, such as onwer of the file (inode), the Permissions for the file, and other states. More important, there are direct and indirect pointers to data blocks. [Handout Ex4. struct inode { uid_t i_owner; // Owner of inode. gid_t i_group; // Group membership of inode. mode_t i_mode; // Permissions and type of inode. dev_t i_rdev; // Device represented by inode, if any. uint16_t i_nlink; // The number of hard links. int64_t i_atime; // Access time (reads). int64_t i_ctime; // Change time (chmod, chown). int64_t i_mtime; // Modification time (writes). uint32_t i_size; // The size of the inode in bytes. // Block pointers. // A block is allocated iff its value is != 0. uint32_t i_direct[N_DIRECT]; // Direct blocks. uint32_t i_indirect; // Indirect block. uint32_t i_double; // Double-indirect block. } __attribute__((packed)); Q: When N_DIRECT is 10, what's the maximum number of blocks can one file in our filesystem have? ] Answer: 10 + 512/4 + (512/4)^2 - Directory (5 min) Directory is nothing but file with (name, inode) pairs as the content. See your handout for this pair's data structure. [Handout Ex5. struct dirent { uint32_t d_inum; // Block number of the referenced inode. char d_name[NAME_MAX]; // File name. } __attribute__((packed)); Q: When NAME_MAX is 124, how many files can one directory have? (based on the maximum file size on Ex4) ] Answer: maximum file size / one pair size = 512 * (10 + 512/4 + (512/4)^2) / 128 3. File System Operations (10 min) - Open a file (5 min) (Logically explain how to find a file. Students are not starting lab at this time.) We have the path to the file, say "/home/cs202/hello" step1: find the root direcotry from superblock step2: check the content of the directory pair (name, inode) to find the next subdir step3: find file "hello" in the last subdir [Handout Ex6. If we want to open file "/home/cs202/hello", how many direcotry should we query? ] Three, "/", "/home", "/home/cs202". - Create a file (5 min) (Logically explain how to create a file. Students are not starting lab at this time.) Step1: allocate a new inode (how? allocate a new block in our lab) Step2: add the (name, inode) pair to its parent direcotry