Download Filesystem - Operating System Design and Implementation - Lecture Slides and more Slides Operating Systems in PDF only on Docsity! 15-410, F’13 1 1 File System (Interface) Nov. 13, 2013 Dave Eckhardt & Todd Mowry Contributions from n Rahul Iyer L31_Filesystem 15-410 “...RADIX-50??...” 15-410, F’13 2 2 Synchronization Today n Chapter 10, File system interface n Ok to skip: remote/distributed (10.5.2!!) Also read Chapter 13 n Might help demystify readline() some 15-410, F’13 5 5 “Extended” file attributes BSD Unix n archived n nodump n append-only (by user/by operating system) n immutable (by user/by operating system) MacOS n icon color Plan 9 n Identity of most recent mutator 15-410, F’13 6 6 Operations on Files Create – locate space (maybe), enter into directory Write, Read – often via position pointer/“cursor” Seek – adjust position pointer for next access Delete – remove from directory, release space (maybe) Truncate n Trim some data from end of file (common case: all data) Append – write at end of file (implicit synchronization) Rename n Change name of file inside a directory n Move a file between two directories (maybe) 15-410, F’13 7 7 I/O to a File – Take 1 Users will read/write files n Not being able to defies the point in having them So, how do you read from and write to one? n read(“README.dox”, input_buffer, num_bytes); n What's the problem with this? 15-410, F’13 10 10 Open files (Unix Model) “In-core” file state – avoid going to disk repeatedly n Mirror of on-disk structure n File number, size, permissions, modification time, ... n Housekeeping info n Back pointer to enclosing file system n Pointer to disk device hosting the file n Who holds locks on ranges of file n How to access file (vector of methods) n Pointer to file's type-specific data Shared when file is opened multiple times 15-410, F’13 11 11 Open files (Unix Model) “Open file” state – result of one open() call n Results are retained for use by multiple I/O calls n Pointer to underlying “open file” n Credentials of process (when it opened the file) n Access mode (read vs. write, auto-append, ...) n Cursor position Shared by multiple processes n “copied” by fork() n inherited across exec() 15-410, F’13 12 12 Example int fd1, fd2, fd3; off_t pos2, pos3; char buf[10]; fd1 = open(“foo.c”, O_RDONLY, 0); fd2 = dup(fd1); fd3 = open(“foo.c”, O_RDONLY, 0); read(fd1, &buf, sizeof (buf)); pos2 = lseek(fd2, 0L, SEEK_CUR); /* ⇒? */ pos3 = lseek(fd3, 0L, SEEK_CUR); /* ⇒? */ 15-410, F’13 15 15 File types (or not) Goal n Avoid printing a binary executable file n Find program which “understands” a file selected by user Derive “type” from file names n *.exe are executable, *.c are C Tag file with type information (extended attributes) n MacOS: 4-byte type, 4-byte creator Unix: Both/neither n Leave it (mostly) up to users (maybe: GUI, libraries, etc.) 15-410, F’13 16 16 File Structure What's in a file? n Stream of bytes? n What character set? US-ASCII? Latin-1? Unicode? n Stream of records? n Array of records? Tree of records? Record structure? n End of “line” n CR, LF, CR+LF n Fixed-length? Varying? Bounded? 15-410, F’13 17 17 File Structure - Unix Program loader needs to know about executables n “Magic numbers” in first two bytes n obsolete A.OUT types - OMAGIC, NMAGIC, ZMAGIC n ELF n #! - script Otherwise, array of bytes n User/application remembers meaning (hopefully!) For a good time... n Try the “file” command n Read /usr/share/misc/magic or /usr/share/file/magic n Marvel at the dedication of the masses – 16,000 lines! 15-410, F’13 20 20 Access Methods – Indexed File contains records Records contain keys Index maps keys ⇒ records n Sort data portion by key n Binary search in multi-level list Fancy extensions n Multiple keys, multiple indices n Are we having a database yet? n Missing: relations, triggers, consistency, transactions, ... n Unix equivalent: dbm/ndbm/gdbm/bdb/... 15-410, F’13 21 21 Directory Operations Lookup(“index.html”) Create(“index.html”) Delete(“index.html”) Rename(“index.html”, “index.html~”); Iterate over directory contents Scan file system n Unix “find” command n Backup program “Log changes to directory tree ____” 15-410, F’13 22 22 Directory Types Single-level n Flat global namespace – only one test.c n Ok for floppy disks (maybe) Two-level n Every user has a directory n One test.c per user n [1003,221]PROFILE.CMD vs. [1207,438]PROFILE.CMD n Typical of early timesharing Are we having fun yet? 15-410, F’13 25 25 Tree Directories Directories are special files n Created with special system calls – mkdir() n Format understood & maintained by OS Current directory (“.”) n “Where I am now” (e.g., /usr/zzz) n Start of relative pathname n ./stuff/foo.c or stuff/foo.c ⇒ /usr/zzz/stuff/foo.c n ../joe/foo.c ⇒ /usr/joe/foo.c n Directory reference in, e.g., p->p_fd->fd_cdir 15-410, F’13 26 26 DAG Directories Share files and directories between users Not mine, not yours: ours Destroy when everybody deletes Unix “hard link” n Remove an open file? n Stays “alive” until last close n Files, not directories n (“.. problem”) usr mji / paper.ms owens 15-410, F’13 27 27 Soft links Hard links “too hard”? n Need a level of indirection in file system? n Want “one true name” for a file? n Need to cross to a different file system of a different type? Alternative: soft link / symbolic link / “short cut” n Tiny file, special type n Contains name of another file n OS dereferences link when you open() it n Link can point to a file anywhere n A file in a different type of file system n A remote file 15-410, F’13 30 30 Mounting Multiple disks on machine Multiple partitions on disk File system within a partition n Or, within a volume / logical volume / ... How to name files in “another” file system? n Wrong way n C:\temp vs. D:\temp n [1003,221]PROFILE.CMD vs. [1207,438]PROFILE.CMD 15-410, F’13 31 31 Mounting mji owens / dae jdi / usr0 usr1 / 15-410, F’13 32 32 Multiple Users Users want to share files What's a user? n Strings can be cumbersome n Integers are nicer for OS to compare n Unix: User ID / “uid” n Windows: Security ID / “SID” What's a group? n A set of users n Typically has its own gid / SID 15-410, F’13 35 35 Protection – typical File specifies owner, group n Permissions for owner, permissions for group members n Read, write, ... n Permissions for “other” / “world” n Read, write, ... Unix n r, w, x = 4, 2, 1 n r w x r – x - — x = 0751 (octal) n V7 Unix: 3 16-bit words specified all permission info n permission bits, user #, group # n As of 2013-03-25, Andrew supports ~33,963 users... 16 bits is a little tight. 15-410, F’13 36 36 Summary File n Abstraction of disk/tape storage n Records, not sectors n Type information n Naming n Complexity due to linking n Ownership, permissions n Semantics of multiple open()s Extra details in 20.7, 20.8 15-410, F’13 37 37 The “.. Problem” Foo Bar $ ln .. bar