15-410, F’04- 1 -File System (Interface)Nov. 10, 2004Dave EckhardtDave EckhardtBruce MaggsBruce MaggsL27_Filesystem15-410“...RADIX-50??...”15-410, F’04- 2 -SynchronizationTodayTodayChapter 11, File system interfaceOk to skip: remote/distributed (11.5.2!!)Also read Chapter 13Also read Chapter 13Mght help demystify readline() some15-410, F’04- 3 -SynchronizationTwo interesting papers about disksTwo interesting papers about diskshttp://www.seagate.com/content/docs/pdf/whitepaper/D2c_More_than_Interface_ATA_vs_SCSI_042003.pdfGoogle for “200 ways to revive a hard drive”15-410, F’04- 4 -What's a file?Abstraction of Abstraction of persistent storagepersistent storageHide details of storage devicessector addressing: CHS vs. LBASCSI vs. IDEHide details of allocation/location on a storage deviceLogicalLogical grouping of data grouping of dataMay be physically scatteredPrograms, dataPrograms, dataSome internal structureSome internal structure15-410, F’04- 5 -Typical file attributesName – 14? 8.3? 255?Name – 14? 8.3? 255?Unicode? ASCII? 6-bit? RADIX-50?Identifier - “file number”Identifier - “file number”Type (or not)Type (or not)Location – device, block listLocation – device, block listSize – real or otherwiseSize – real or otherwiseProtection – Who can do what?Protection – Who can do what?Time, date, last modifier – monitoring, curiousityTime, date, last modifier – monitoring, curiousity15-410, F’04- 6 -“ Extended” file attributesBSD UnixBSD Unixarchivednodumpappend-only (by user/by operating system)immutable (by user/by operating system)MacOSMacOSicon color15-410, F’04- 7 -Operations on FilesCreate – locate space, enter into directoryCreate – locate space, enter into directoryWrite, Read – according to position pointer/cursorWrite, Read – according to position pointer/cursorSeek – adjust position pointerSeek – adjust position pointerDelete – remove from directory, release spaceDelete – remove from directory, release spaceTruncateTruncateTrim data from endOften all of itAppend, RenameAppend, Rename15-410, F’04- 8 -Open-file StateExpensive to specify name for each read()/write()Expensive to specify name for each read()/write()String-based operationDirectory look-up“ Open-file” structure stores“ Open-file” structure storesFile-system / partitionFile-system-relative file numberRead vs. writeCursor position15-410, F’04- 9 -Open files (Unix Model)“In-core”“In-core” file state file stateMirror of on-disk structureFile number, size, permissions, modification time, ...Housekeeping info Back pointer to enclosing file system Pointer to disk device hosting the file Who holds locks on ranges of fileHow to access file (vector of methods)Pointer to file's type-specific dataSharedShared when file is opened multiple times when file is opened multiple times15-410, F’04- 10 -Open files (Unix Model)“ Open file”“ Open file” state (result of one open() call) state (result of one open() call)Access mode (read vs. write, auto-append, ...)Credentials of process (when it opened the file)Cursor positionPointer to underlying “ open file”SharedShared by multiple processes by multiple processes “ copied” by fork() inherited across exec()15-410, F’04- 11 -Exampleint 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);/*10*/pos3 = lseek(fd3, 0L, SEEK_CUR);/*0*/15-410, F’04- 12 -“ Open File” vs. “ In-Core File”Processfd1: 3fd2: 4fd3: 5012345vnode #334readers 2writers 0ttyp5rows: 24cols: 80r/or/wPos 10Pos 015-410, F’04- 13 -File types (or not)GoalGoalAvoid printing a binary executable fileFind program which “understands” a file selected by userDerive “ type” from file namesDerive “ type” from file names*.exe are executable, *.c are CTag file with type informationTag file with type informationMacOS: 4-byte type, 4-byte creatorUnix: Both/neither – Leave it (mostly) up to usersUnix: Both/neither – Leave it (mostly) up to users15-410, F’04- 14 -File StructureWhat's What's inin a file? a file?Stream of bytes? What character set? US-ASCII? Roman-1? Unicode?Stream of records?Array of records? Tree of records?Record structure?Record structure?End of “ line” CR, LF, CR+LFFixed-length? Varying? Bounded?15-410, F’04- 15 -File Structure - UnixProgram loader Program loader needs to knowneeds to know about executables about executables“ Magic numbers” in first two bytes obsolete A.OUT types - OMAGIC, NMAGIC, ZMAGIC ELF #! - scriptOtherwise, Otherwise, array of bytesarray of bytesUser/application remembers meaning (hopefully!)For a good time...For a good time...Try the “ file” commandRead /usr/share/magic Marvel at the dedication of the masses15-410, F’04- 16 -File Structure – MacOSData forkData forkArray of bytesApplication-dependent structureResource forkResource forkTable of resourcesIcon, Menu, Window, Dialog boxMany resources are widely used & understoodDesktop program displays icons from resource fork15-410, F’04- 17 -Access MethodsProvided by OS or optional program libraryProvided by OS or optional program librarySequentialSequentialLike a taperead() next, write() next, rewind()Sometimes: skip forward/backwardDirect/relativeDirect/relativeArray of fixed-size recordsRead/write any record, by #15-410, F’04- 18 -Access Methods – IndexedFile contains File contains recordsrecordsRecords contain Records contain keyskeysIndexIndex maps keys maps keys ⇒⇒ records recordsSort data portion by keyBinary search in multi-level listFancy extensionsFancy extensionsMultiple keys, multiple indicesAre we having a database yet?Missing: relations, triggers, consistency, transactions, ...Unix equivalent: dbm/ndbm/gdbm/bdb/...15-410, F’04- 19 -Directory OperationsLookup(“ index.html” )Lookup(“ index.html” )Create(“ index.html” )Create(“ index.html” )Delete(“ index.html” )Delete(“ index.html” )Rename(“ index.html” , “ index.html~” );Rename(“ index.html” , “ index.html~” );Iterate over directory contentsIterate over directory contentsScan file systemScan file systemUnix “ find” commandBackup
View Full Document