Lecture 3 & 4: FILE SYSTEM
INTERNAL REPRESENTATION OF FILES Every file on a UNIX system has a unique inode (contains information necessary for a process to access a file) Inodes exist in a static form on disk and the kernel reads them into an in-core inode to manipulate them
Inodes Consist of: File owner identifier (individual owner & group owner) File type (regular, directory, character or block special, or FIFO – pipes) File access permissions (owner, group, other: read, write, execute) File access time Number of links to the file Table of contents for the disk addresses of data in a file File size
Sample Disk Inode Owner mjb Group os Type regular file Perms rwxr-xr-x Accessed Oct 23 1984 1:45 P.M. Modified Oct 22 1984 10:30 A.M. Inode Oct 23 1984 1:30 P.M. Size 6030 bytes Disk addresses
Algorithm for allocation of in-core inodes algorithm iget input: file system inode number output: locked inode { while (not done) if (inode in inode cache) if (inode locked) sleep (event inode becomes unlocked); continue; /*loop back to while */ } /*special processing for mount points */ if (inode on inode free list) remove from free list; increment inode reference count; return (inode); /*inode not in inode cache */ if (no inodes on free list) return (error); remove new inode from free list; reset inode number and fiel system; remove inode from old hash queue, place on new one; read inode from disk (algorithm bread); initialise inode (e.g. reference count to 1);
Structure of a regular file File A File C File B …….. Block address 40 50 60 70 …….. File A Free File C File B …….. 40 50 60 70 81 Block address Allocation of Contigiguous Files and Fregmentation of Free Space
Direct and Indirect Blocks in Inode Single direct Double direct Triple direct . . . .
Directories Give the file system its hierarchical structure Important role in conversion of a file name to an inode number Initial access: open, chdir, link system calls Algorithm namei uses intermediate indoes as it parses a path name
Algorithm for conversion of path name to an inode algorithm namei /* convert path name to inode */ input: path name output: locked inode { if (path name starts from root) working inode = root inode (algorithm iget); else working inode = current directory inode (algorithm iget); while (there is more path name) read next path name component from input; verify that working inode is of directory, access permissions OK; if (working inode is of root and component is “..”) continue; /*loop back to while */ read directory (working inode) by repeated use of algorithms bmap, bread and brelse; if (component matches an entry in directory (working inode)) get inode number for matched component; release working inode (algorithm iput); working inode = inode of matched component (algorithm iget); } else /*component not in directory */ return (no inode); return (working node);
Super block Consists of: The size of the file system The number of free blocks in the file system A list of free blocks available on the file system The index of the next free block in the free block list The size of the inode list The number of free inodes in the file system A list of free inodes in the file system The index of the next free inode in the free inode list Lock fields for the free block and free inode lists A flag indicating that the super block has been modified
Algorithm for assigning new inodes algorithm ialloc /* allocate inode */ input: file system output: locked inode { while (not done) { if (super blocked locked) { sleep (event super block becomes free); continue; /*loop back to while */ } if (inode list in super block is empty) { lock super block; get remembered inode for free inode search; search disk for free inodes until super block full, or no more free inodes (algorithms bread and brelse); unlock super block; wake up (event super block becomes free); if (no free inodes found on disk) return (no inode); set remembered inode for next free inode search; /*there are inodes in super block inode list */ get inode number form super block inode list; get inode (algorithm iget); if (inode not free after all) { write inode to disk; release inode (algorithm iput); continue; /*inode is free */ initialise inode; write inode to disk; decrement file system free inode count; return (inode);}
Assigning Free Inode from Middle of List Free inodes Super Block Free Inode List 83 48 empty 18 19 20 index array 1 array 2
Assigning Free Inode – Super Block List Empty Super Block Free Inode List 470 empty array 1 index Remembered inode Super Block Free Inode List array 2 535 free inodes 476 475 471 48 49 50 index
The File System The best way to learn about files is to play with them. Creating a small file: $ ed a Now is the time For all good people . w junk 36 q $ ls –l junk -rw-r--r– 1 you 36 Sept 27 06:11 junk $ $ cat junk
Basics Cat shows what the file looks like. The command od (octal dump) prints a visible representation of all the bytes of a file. Try: $ od –c junk -c means “interpret bytes as characters”. Turning on the –b option will show the bytes as octal (base 8) numbers as well. $ od -cb junk Try something different: type some characters and then a ctr-d, rather than a return. This will immediately send the characters you have typed to the program that is reading from the terminal. (Ctr-d logs you out, as the shell sees no more input). What happens when you type ctl-d to ed?
What is in a file? The file command makes an educated guess: $ file /bin /bin/ed /usr/src/cmd/ed.c /usr/man/man1/ed.1 /bin: directory /bin/ed: pure executable /usr/src/cmd/ed.c: c program text /usr/man/man1/ed.1 roff, nroff, or eqn input text -c means “interpret bytes as characters”. Turning on the –b option will show the bytes as octal (base 8) numbers as well. $ sort /bin/ed Directories and filenames: $ ls $ pwd $ mkdir recipes $ file * $ ls recipes
Permissions Every file has a set of permissions associated with it, which determine who can do what with the file. The file /etc/passwd is the password file; it contains all the login information about each user. You can discover you uid and groupid, as does the system, by looking up your name in /etc/passwd $ grep you /etc/passwd you:gkmbCTrJ04COM:604:1:Y.O.A.People:/usr/you: The –l option of ls command prints the permissions information, among other things: $ ls –l /etc/passwd -rw-r- - r - - 1 root 5115 Aug 30 10:40 /etc/passwd The chmod command changes permissions on files: $ chmod permissions filenames…. $ chmod +x command $ chmod –w command
Inodes A file has several components: a name, contents and administrative information such as permissions and modification times. The administrative information is stored in the inode along with essential system data such as how long it is, where on the disc the contents of the file are stored and so on There are three times in the inode: the time that the contents of the file were last modified (written); the time that the file was last used (read or executed); and the time that the inode itself was last changed, for example to set the permissions
Interesting directories / Root of the file system /bin Essential programs in executable form (“binaries”) /dev Device files /etc System miscellany /etc/motd Login message of the day /etc/passwd Password file /lib Essenhtial libraries /tmp Temporary files; cleaned when system is restarted /unix Executable form of the operating system /usr User file system /usr/adm System administration: accounting info /usr/bin User binaries /usr/dict Dictionary and support for spell() /usr/games Game programs /usr/include Header files for C programs
Interesting directories /usr/include/sys System header files for C programs /usr/lib Libraries for C, etc /usr/man On-line manual /usr/man/man1 Manual pages for section 1 of manual /usr/mdec Hardware diagnostics, bootstrap programs /usr/news Community service messages /usr/pub Public oddments /usr/src Source code for utilities and libraries /usr/src/cmd Source for commands /usr/src/lib Source code for subroutine libraries /usr/spool Working directories for communication programs /usr/spool/lpd Line printer temporary directory /usr/spool/mail Mail in-boxes /usr/spool/uucp Working directory for the uucp programs /usr/sys Source for the operating system kernel /usr/tmp Arternate temporary directory /usr/you Your login directory /usr/you/bin Your personal programs