class: title # Filesystem Implementation ## CSC 213 – October 3, 2025 --- # Agenda for Today 1. Department Events 2. Upcoming Work 3. Files and Directories 4. Filesystem Implementation 5. Wrap Up --- # Department Events ## Flu Shot Clinic .indent[ October 2nd and 3rd from 9am–4pm in the Natatorium Lobby Register with Hy-Vee at
] ## CS Extra: Topic TBD .indent[ Thursday, October 9th at 4:15pm in Noyce 3821 ] ## *No CS Table Next Week* *To receive 0.25% extra credit, send an email to let me know you attended no more than 48 hours after the event.* --- class: section, blue # Upcoming Work --- # Lab: Malloc **Due:** Wednesday, October 8th by 11:59pm **Late Deadline:** Wednesday, October 15th by 11:59pm ## Questions **Why does my allocator make programs crash on startup?** Almost all programs make one very large allocation during startup. You'll need to handle requests for more than 2048 bytes before you can run test programs, even inside gdb. **What should we do to debug if the allocator makes programs crash during startup?** Your best bet is to add logging. It helps to know what sizes are being requested, what your allocator is doing, and the resulting pointer that you return. You can use `snprintf` and `log_message` to do this safely: ```c char buffer[256]; snprintf(buffer, 256, "xxmalloc(%d) called\n", size); log_message(buffer); ``` --- # Assignment: Listing Directories **Due:** Monday, October 6th by 11:59pm ## Questions? --- class: section, blue # Files and Directories --- # Puzzle Rules ## Format Each puzzle shows a program fragment. The program finishes executing and exits without any errors. ## Setup The first puzzle program runs in an empty directory. Each following puzzle runs in the same directory, which has all the outputs from the previous puzzles. ## Responses Your task is to determine (without running the code) what files will be in the directory after each puzzle program runs, and what those files will contain. --- # Puzzle 1 ```c int fd = open("message.txt", O_RDWR | O_CREAT, 0640); char* message = "Hello"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 2 ```c int fd = open("message.txt", O_RDWR | O_CREAT | O_APPEND, 0640); char* message = " world!\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 3 ```c int fd = open("message.txt", O_RDWR); char* message = "J"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 4 ```c link("message.txt", "greeting.txt"); int fd = open("greeting.txt", O_RDWR | O_APPEND); char* message = "Greetings.\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 5 ```c symlink("message.txt", "symlink.txt"); int fd = open("symlink.txt", O_RDWR | O_APPEND); char* message = "What?\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 6 ```c int fd = open("message.txt", O_RDWR | O_TRUNC); unlink("message.txt"); char* message = "Well, this is awkward.\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 7 ```c int fd = open("message.txt", O_RDWR | O_CREAT | O_TRUNC, 0640); char* message = "Curiouser and curiouser!\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 8 ```c mkdir("somedir", 0755); int fd = open("somedir/message.txt", O_RDWR | O_CREAT | O_TRUNC, 0640); char* message = "Hello world!\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 9 ```c chdir("somedir"); int fd = open("greeting.txt", O_RDWR | O_CREAT | O_TRUNC, 0640); char* message = "Greetings.\n"; write(fd, message, strlen(message)); close(fd); ``` --- # Puzzle 10 ```c int fd = open("message.txt", O_RDWR, 0640); char* data = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); data[0] = 'F'; data[14] = 'f'; munmap(data, 4096); close(fd); ``` --- # Puzzle 11 ```c int fd = open("fork.txt", O_RDWR | O_CREAT, 0640); pid_t child_id = fork(); if (child_id == 0) { char* message = "fork\n"; write(fd, message, strlen(message)); } else { wait(NULL); char* message = "spoon\n"; write(fd, message, strlen(message)); } close(fd); ``` --- # Puzzle 12 ```c int fd = open("dup.txt", O_RDWR | O_CREAT, 0640); dup2(fd, STDOUT_FILENO); char* message = "Hello from write().\n"; write(fd, message, strlen(message)); printf("Hello from printf().\n"); message = "Interesting...\n"; write(fd, message, strlen(message)); close(fd); ``` --- class: section, blue # Filesystem Implementation --- class: section, gray # Whiteboard: Elements of a Filesystem --- # A Simple Filesystem .left-col[ ## Disk Parameters 256 blocks, each 4KB 1 byte disk addresses ## inodes 56 bytes of metadata 8 direct block pointers ## Disk Layout 1 superblock 1 block for inode bitmap 1 block for data block bitmap 4 blocks for inode entries ] .right-col[ ## Questions 1. What is the total size of the disk? 2. How many inode entries or data blocks can be tracked with a single bitmap block? 3. What is the size of an inode entry? 4. How many inodes does this filesystem support? 5. How many data blocks does this filesystem have? 6. What is the largest supported file size? ] --- # A (Slightly) Better Simple Filesystem .left-col[ ## Disk Parameters 256 blocks, each 4KB 1 byte disk addresses ## inodes 56 bytes of metadata ***7 direct block pointers*** ***1 indirect block pointer*** ## Disk Layout 1 superblock 1 block for inode bitmap 1 block for data block bitmap 4 blocks for inode entries ] .right-col[ ## Questions This is almost the same as the filesytem from before, but now each inode entry contains an indirect block pointer. **What is the effect of this change?** Describe the effect, and calculate any new limits for the updated filesystem. ] --- class: section, blue # Wrap Up --- # Reminders ## Lab The malloc lab is due next week. ## Assignment The listing directories assignment is due next Monday. ## Need Help? My regular office hours schedule is now posted and times are bookable. Don't forget about Sunday's mentor session from 3–5pm ## Reading - **Scheduling** (OSTEP, Chapter 7)