Nachos Instructional OS Part 3 B CS 170
Nachos Instructional OS: Part 3 B CS 170, Tao Yang
Topics n n File system implementation. Assignment 3 B for File system extension 9/25/2020 2
Nachos Kernel Read/write Nachos File System Linux n n Two versions: open. File objects n A ``stub'' version is simply a front-end to the Linux. n A Nachos version implemented on top of a raw disk. Synch. Disk Function layers (from top to down): n File. System object n n Created by every call to open/access a file in a directory. Synch. Disk object n n Disk Open. File objects (many) n n Directory support. File control block (I-node) support Free block map Created by synch. Disk = new Synch. Disk("DISK") in system. cc The Disk object. n Created by disk = new Disk(name, . . ) in Synch. Disk: : Synch. Disk() 9/25/2020 3
Disk Size Nachos Kernel Read/write open. File objects Machine/disk. h #define Sector. Size 128 // number of bytes per disk Synch. Disk sector #define Sectors. Per. Track 32 // number of sectors per disk track #define Num. Tracks 32 // number of tracks per disk Disk #define Num. Sectors (Sectors. Per. Track * Num. Tracks) // total # of sectors per disk n 9/25/2020 4
File System Layers in Nachos Directory: Manage a directory of file names File. Header: i-node (File control block) Free. Map Bitmap for 32 x 32 disk sectors Nachos kernel thead File. System: Directory management file create/open/delete Open. File: access individual file with read/write/seek. Operate on bytes. Synch. Disk: synchronous access to a disk with concurrent threads. Operate on sectors. Disk: low-level interface that initiates asynchronous IO to physical disk sectors Base Operating System (Linux for our class)
Operations of File System Object n File. System(bool format). n called once at ``boot'' time. n For the stub version, just use Unix file system n Otherwise create a directory/control info. n n n Sector 0 is fileheader (i-node) for Free. Map. Sector 1 is i-node for directory table. Create(char *name, int initial. Size). n Create a Nachos disk file. ``initial. Size'' is ignored in the stub version; Unix doesn't require it. Open. File *Open(char *name). n Assume both read and write access. n in the stub version, Open simply opens the Unix file. Remove(char *name) List() 9/25/2020 6
Directory Object contains 10 Directory. Entry objects (directory. h) n n n Directory content has 10 table entries. fit into a 128 B sector. Stored in Sector 1 Directory space is allocated in File. System() during initialization … Bool in. Use --- is this entry used? Int sector --- location of file header (i-node) Filename -- 9 characters+ 1 for ‘n’ 128 bytes … 9/25/2020 7
memory directory. cc n n File. Name. Max. Len 9 // 9 characters at most for a file name Num. Dir. Entries 10 // at most 10 files Directory Block Directory Object attributes n n n Disk Limit n n Directory table. Size -- # of directory entries which is 10 now. table = in-memory directory entries, each of which is for one Nachos file. Content is copied from and copies back to a disk Directory. File. Operations n n n Directory (int size) -- Create an in-memory directory of the desired size. Fetch. From (Open. File *file) -- Fetch disk Directory. File that contains directory entries, and copy to in-memory directory table. Write. Back(Open. File *file) -- Write back in-memory directory content to disk Directory. File n n called every time when a file is added or removed to directory. Find. Index (Char *name) --Search a file string name in a directory Add (Char *name, int sector) – Add file name into a directory with disk sector no that contains the file header (i-node). Remove(char *name) – remove a file. 9/25/2020 8
filehdr. h: File header (File control block, i-node) … int num. Bytes; // file size int num. Sectors; //sectors used int data. Sectors[Num. Direct] Data sector pointer 0 Data sector pointer 1 … Data sector pointer 29 n n Fit in one sector (128 bytes) Operations n n Data sector memory File control block Allocate(Bit. Map *bit. Map, int file. Size); Fetch. From (int Sector. No) -- Fetch Fileheader sector from disk. Write. Back(int Sector. No) -- Write back in-memory file header content to disk Byte. To. Sector(int offset) –Convert a file offset in bytes to a sector no 9/25/2020 Disk File header 9
Nachos Kernel Read/write Open. File Object n n n Read(char *buffer, int num. Bytes) Write(char *buffer, int num. Bytes) Disk Open. File Obj Read. At(char *buffer, int num. Bytes, int File. Position). n n Synch. Disk Memory File control block Read/write in a specific position n Identify specific sectors in the file and fetch them, copy to buffer. Write. At(char *buffer, int num. Bytes, int File. Position) n n open. File objects open. File objec Open. File(int sector) n sector – disk location of file control block. n Load FCB from disk create an in-memory Open. File object Seek(int position) Read/write in the current position May read partially written sectors first, and then write out updated sectors. Length(). Return the file size. 9/25/2020 10
Synch. Disk Object: Synchronously access a disk n n n open. File objects Filesys/synch. Disk. cc Synch. Disk () n Called only once to create a physical disk object. Set the disk Synch. Disk interrput hander as Requst. Done(). n Create lock/semaphore for synchronization. n Notice that physical disk is an asynchronous device (disk requests return immediately, and an interrupt happens later on). Read. Sector(int sector. Number, char* buffer) n n n Send a read request for a sector Wait IO complete (wait for interrupt using a semaphone) Write. Sector(int sector. Number, char* data) n n Nachos Kernel Read/write Send a sector write request. Wait for interrupt. Request. Done() n Disk interrupt handler. Wake up any thread waiting for the disk request to finish. 9/25/2020 11
Disk Object Nachos Kernel Read/write open. File objects n n n Machine/disk. h and disk. cc Simulates the behavior of a disk I/O device. Synch. Disk n a single platter, with multiple tracks (32). n Each track contains 32 sectors n Each sector is 128 bytes Disk n Disk size = 32 x 32*128 =128 K n Asynchronous read/write with an option of disk cache Supported operations: n Disk(char *name, Void. Function. Ptr call. When. Done, int call. Arg) n Create a disk: just create a Unix file to simulate. n Setup a handling function (Request. Done in Synch. Disk. cc) when a read/write request is done. 9/25/2020 12
Operations in Disk device Nachos Kernel Read/write open. File objects n n n Read. Request(int sector. Number, char *buffer) Synch. Disk n Read from the specific location of the “physical disk”. n ticks = Compute. Latency(sector. Number, FALSE); n interrupt->Schedule(Disk. Done, (int) this, ticks, Disk. Int); Write. Request(int sector. Number, char *data) Disk n Write data to specific location of the “physical disk”. n ticks = Compute. Latency(sector. Number, TRUE); n interrupt->Schedule(Disk. Done, (int) this, ticks, Disk. Int); Compute. Latency(int new. Sector, bool writing) n Latency= seek time + rotation time 9/25/2020 13
Assignment 3 B Workload n Test C programs ~cs 170/nachosprojtest/proj 3 n n Part B. ~200 in filesys (few in userprog) Part A and B can be done in parallel n n Work on filesys subdirectoy mainly. n n Part B may use Project 2 code without VM. And change system call handling code in userprog if needed (to use this extended file system). Extend the size of each file from 4 K to 100 K n single indirect pointers in i-node (file control block). 9/25/2020 14
Emphasis of Project 3 B n Play tradeoffs n n Understand the design options Deadline-driven approach n n Choose something simpler and easier to do first. Still not enough time? n Complete with minimal efforts to pass the tests. n n n Hidden tests are very similar to public test cases. No complex test cases. You are good as long as your code can run virtual memory>physical, can support files up to 100 K. Interpret the requirement towards your advantage if vague. If not specified, do something easy. 9/25/2020 15
Part B: Steps for Nachos file system extension 1. 2. Understand how the Nachos file system operates and how Nachos implements files. Modify the file system calls from Project 2 to use Nachos file system 1. 3. Donot have to support VM (Part A/Part B can be done in parallel) Modify Nachos file system so that the file size can be upto 100 K
Files to be modified n Directory filesys n filehdr. cc/. h n n openfile. cc n n Support 30 direct data pointers New: Support single indirect pointers in i-node and can expand the file length with more sectors allocated. Write method Extend. File() that allows a file to grow by certain number of sectors. Expand file length/allocate sectors when writing data at the end or beyond. Indirect. Pointer. Block. cc (new file to add) This class provides a way for a file header to contain not just direct sectors, but indirect blocks that each have their individual collection of sectors. This allows the file size to exceed the usual 4 K. n Directory userprog n exception. cc n Make sure file system calls use extended Nachos file system.
Useful methods in Indirect. Pointer. Block. cc // Constructor Indirect. Block: : Indirect. Block() //Writes the modified object back to the specified sector on disk. void Indirect. Block: : Write. Back(int sector. Num) // Retrieves the serialized object from disk. void Indirect. Block: : Fetch. From(int sector. Num) // Adds a newly allocated data sector from the file header to this indirect pointer block's list of allocated sectors. void Indirect. Block: : Put. Sector(int sector. Num) // Removes the allocated sectors from the global map of allocated sectors. Essentially removes the data held by this indirect pointer block. void Indirect. Block: : Deallocate(Bit. Map* alloc. Sectors. Map) // Returns the disk sector that is corresponding the specified byte at offset from this indirect pointer block int Indirect. Block: : Byte. To. Sector(int offset. From. Start. Of. IB)
Comparison: Unix file i-node (4 K bytes per block)
Step 3: Extend maximum file size n Modify the File. Header i-node (file control block) to support 100 K size. n Option 1: Include 5 data sector pointers, and 25 sectors for single indirect mapping. n At most map 5 + 25*32=805 blocks. Maximum file length = 805*128=103040 bytes. Option 2: single indirect mapping at every entry Suggest to add a class called Indirect. Pointer. Block which takes 1 sector (32 entries) n n n Fetch. From (int sector). Write. Back(int sector). Add. Sector (int sector) – add a new sector. Deallocate(Bit. Map *free. Map) – deallocate all sectors. Byte. To. Sector (int offset). – convert an offset in bytes to a sector.
Step 4: File size can grow n n Modify the current implementation so that a write beyond the end of the file extends the size of the file. Gracefully handle the case of the disk being full or maximum size is reached - the user process should not crash. Instead an error should be returned.
Step 4 n Modify Write. At() method in openfile. cc : n n Case 1: Write request is within the bound of the file. Handle like the existing code Case 2: Write request overwrites some portion of existing file and goes beyond by N bytes. Case 3: No overwrite to the current data, but goes beyond the end of the file. Here. To extend a file dynamically, n Extend i-node/data dynamically: include a routine in File. Header to extend a file by N bytes (by a sector or by a set of sectors)
Testing issues n n n Need to load user binaries/data into Nachos file system during execution Example: $ pwd cs 170/nachos/code/filesys $. /nachos -f -cp. . /test/Prog 1 -x Prog 1 Or nachos –f nachos -cp Prog 1 nachos -x Prog 1 Nachos –f Linux file Nachos –cp Prog 1 Nachos file system Nachos –x Prog 1
- Slides: 23