CS202 Review Session 8 Notes from [Xiangyu Gao](https://xiangyug.github.io/), TA from fall 2021 Edited by Khanh Nguyen, TA Spring 2022 Edited by Jinli Xiao, TA Spring 2023 Edited by Sam Frank, TA Spring 2024 Edited by Abigail Zhou, TA Spring 2024 1. Lab 5 Overview 1.1 File system 1.2 Inode Data Structure 1.3 Source Code 2. Debugging --------------------------------------------------------------------- In review session 7 last week, Sam covered background knowledge and lab 5. This week, in review session 8, I will cover the lab 5 overview and some debugging tips. So, some of these notes will be the same. 1. Lab 5 Overview 1.1 File system There are at least three key abstractions: file, filename, folder **File**: in reality, the data of on file might not be placed next to each other on the disk. But it looks to the users that the data of a file can be thought of as one big,long contiguous string of bytes. **Filename**: users don't have to remember any of the physical information about how the file is stored, the sector number where it starts for instance. Instead, users can assign the file with an intuitive name. **Directory**: containers of other files or directories to help organize. 1.2 Inode Data Structure Each inode contains: An inode contains metadata such as file permissions, times for file access, link count, etc... Additionally, it contains pointers to data block, indirect pointer and double indirect pointer. There is also a triple indirect pointer if necessary. - 10 direct data blocks. Each of size 4KB. - 1 indirect pointer. It has 1024 direct blocks. [Draw picture of inode data structure - see whiteboard] struct inode in fs_types.h The inode is intentionally imbalanced to handle both small and large files. If there is a small file, it can fit into the direct data blocks. Else, it can be allocated indirect pointer or double indirect pointer. Each inode is referred to by its i-number. Mental model: -> Directory entries are implemented similarly to files. Its contents entries contain a mapping: -> - There is only 1 region in which both inode and data block reside. Usually, they are divided into 2 separate regions: inode region and data block region. - Each inode is allocated its own disk block instead of being packed alongside other inodes in a single disk block. - Sector performs a read of 512B. The file system read in chunk of 4KB block size, which is 8 sectors. - Superblock is block 0. It holds metadata about the FS and pointer to the root directory. - Bitmap is an array of bits. Each bit at index i indicates if block i is allocated or not. 1 indicates it's free and 0 indicates it's used. - 1 double indirect pointer: It has 1024 indirect blocks. 1.3 Source Code fs_types.h Notice how the definition of the inode struct is exactly the same as the drawing. First we see the metadata. Then the first 10 blocks in i_direct. (Then i_indirect, then i_double.) Notice how the types of the data blocks are uint32_t. Why is that? dir.c int dir_lookup(struct inode *dir, const char *name, struct dirent **dent, struct inode **ino) Why do we use double pointers? - Assigning the address - Otherwise nothing will be changed Why do we check (r = inode_get_block(dir, i, &blk)) < 0? - Need to make sure block exists Checks every name with the input. If the name matches, set the pointer to the inode with the address of the associated file inode and the file data structure pointer (*dent) to the address of the file in the dir. Otherwise, return the no entry error. int dir_alloc_dirent(struct inode *dir, struct dirent **dent) Same with dir_lookup, we pass in a double pointer for what we want to return. The int was only for the error code. Similar to dir_lookup. It checks the current directory to see if there is a free block, then if not, adds and allocates a new block to the directory. disk_map.c void flush_block(void *addr) Look at how the address is determined: addr = (void *)((intptr_t)addr & ~(BLKSIZE - 1)); Calls msync() to flush to the actual disk. 2. Debugging Tmux - Tutorial on the course website. Could not go too in depth because I can’t show my own code.