Review Session 8 Outline 1. inode 2. Directories 3. Problem for inodes and directories 4. Hard/soft links 1. inode and unix file systems inode is the representation of files in unix file systems. -- inode array, file, i-number and inode -- inode array: A fixed-size global array on the disk. Each entry of this array is one inode structure. -- file: User's view of a set of data. -- i-number: The index to the global inode array. -- inode: Inodes can be treated as the implementation of files in a unix file system. It contains metadata of a file and the pointers to the data blocks. (more details later) Put them together: Each regular file has one i-number, by given this i-number, the file system will perform a lookup in the inode array to find the inode structure. Then the file system can provide the access/modification time to a file (and other metadata) by looking at the metadata part in inode structure, and the data blocks addresses (actual content) by looking at the pointers in inode structure. [Handout -- inode structure: -- metadata: Size of a file Owner's user id Permission: File type: directory, regular file or soft link. Access permission: how different users (owner, group or others) can access it. Timestamps: Access time, modification time and change time. Link count: How many files are associated with this inode. -- Pointers: Direct Pointer 1 --> data block Direct Pointer 2 --> data block ... Direct Pointer 12 --> data block Single Indirect Pointer --> indirect block: Pointer --> data block Pointer --> data block Pointer --> data block ... Pointer --> data block Double Indirect Pointer --> indirect block: Indirect Pointer --> indirect block: Pointer --> data block Pointer --> data block Pointer --> data block ... Pointer --> data block ... Indirect Pointer --> ditto Triple Indirect Pointer --> ... ] [Question: Which factors determine many pointers one indirect block can hold?] Size of block & size of pointer [Question: Given this structure, can we have file of any size?] No. [Question: Say we have 512-byte block, 4-byte pointer, 12 direct pointers, 1 single indirect pointer, 1 double, and 1 triple. How many regular files (no links) can we have? What is the maximum size of a file?] Number of regular files: number of entries in inode array. Direct Pointers: 12 * 512B + Single Pointer: 1 * (512 / 4) * 512B + Double Pointer: 1 * (512 / 4)^2 * 512B + Triple Pointer: 1 * (512 / 4)^3 * 512B 2. Directories Directories are files. Differences between a regular file: 1. File type field in its inode 2. Content of directory is *a mapping from filenames to i-numbers* [Handout: In a terminal: $ cd / Note that / is a file. Say we have the file *content* to '/' as: [name, i-number] ... Briefly escribe the actions when we type: $ ls bin ] 1. Get the i-number associated with filename "bin", 1021; 2. Find 1021 entry in *inode array* from disk, and load the inode into memory; 3. Check permissions for current user, and file type is a directory; 4. Use inode pointers to get the content; [Question: what should the content look like?] Say we have the content as: [name, i-number] ... 5. Foreach entry in its content (*map*) Use the i-number to look for the inode; Get metadata from the inode; 6. Output 3. Problem for inodes and directories [Handout: Consider a very simple file system for a tiny disk. Each sector on the disk holds 2 integers, and all data blocks, indirect blocks, and inodes are 1 disk sector in size (each contains 2 integers). All files stored on disk are interpreted as directories by the file system (there are no "data files"). The file system defines the layout for the following data types on disk: -- inode = 1 pointer to a data block + 1 pointer to indirect block -- indirect block = 2 pointers to data blocks -- directory = a regular file containing zero or more pairs of integers; the first integer of each pair is a file name and the second is the file's inumber The value "99" signifies a null pointer when referring to a disk block address or directory name. An empty directory has one disk block with the contents "99 99". The inumber for root directory is "/" is 0. The following data are stored on disk: [On the board inode array: +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |10 | 7 | | 8 | | | 3 | | | | | | | | | | | 6 |99 | |99 | | |99 | | | | | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ disk blocks: +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | |32 | |96 | | | 1 |99 |99 | |57 | | | | | | | | 3 | | 1 | | |99 |99 |99 | | 6 | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ ] 1. How many entries can appear in a maximum-sized directory? (Each entry is a pair of integers) Answer: 3. One data block and two other data blocks pointed by the one indirect block 2. List all directories stored on this disk (full path names) along with the names of the files stored in each directory. [On the board: draw an empty one +----------+----------+-----------------+-------------+---------------+ | path | i-number | indirect blocks | data blocks | subdirs | +----------+----------+-----------------+-------------+---------------+ | / | 0 | 6 | 10 1 | /57, /32 | +----------+----------+-----------------+-------------+---------------+ | /32 | 3 | n/a | 8 | n/a | +----------+----------+-----------------+-------------+---------------+ | /57 | 6 | n/a | 3 | /57/96 | +----------+----------+-----------------+-------------+---------------+ | /57/96 | 1 | n/a | 7 | n/a | +----------+----------+-----------------+-------------+---------------+ ] 3. Modify the above data structures to add an empty directory called "87" to directory "/" Answers: * marks answers inode array: +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ |10 | 7 | | 8 | | | 3 | | |14*| | | | | | | | 6 |99 | |99 | | |99 | | |99*| | | | | | | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ disk blocks: +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |10 |11 |12 |13 |14 |15 | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ | |32 | |96 | | | 1 |99 |99 | |57 | | |87*|99*| | | | 3 | | 1 | | |13*|99 |99 | | 6 | | | 9*|99*| | +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+ +----------+----------+-----------------+-------------+---------------+ | path | i-number | indirect blocks | data blocks | subdirs | +----------+----------+-----------------+-------------+---------------+ | / | 0 | 6 | 10 1 13 *| /57, /32, /87*| +----------+----------+-----------------+-------------+---------------+ | /32 | 3 | n/a | 8 | n/a | +----------+----------+-----------------+-------------+---------------+ | /57 | 6 | n/a | 3 | /57/96 | +----------+----------+-----------------+-------------+---------------+ | /57/96 | 1 | n/a | 7 | n/a | +----------+----------+-----------------+-------------+---------------+ | /87 *| 9 *| n/a *| 14 *| n/a *| +----------+----------+-----------------+-------------+---------------+ ] 4. Hard/soft links Hard link: multiple files are associated with the same i-number; Soft link: A soft link is a file having another file's path as its content, and its metadata (in inode) contains a bit saying its a soft link. AKA symbolic link [On the board: Create a hard link: $ ln source target Create a soft link: $ ln -s source target ] [Handout: What happen when we type: $ ln /bin/cp cc pwd data: /bin data +-----------+ +-----------+ | | | cp - 1001 | +-----------+ +-----------+ |... | |... | inode 1001: link count: 1 ] --> pwd data: /bin data +-----------+ +-----------+ | cc - 1001 | | cp - 1001 | +-----------+ +-----------+ |... | |... | inode 1001: link count: 2 [Handout: What happen when we type: $ ln -s /bin/cp cc pwd data: /bin data +-----------+ +-----------+ | | | cp - 1001 | +-----------+ +-----------+ |... | |... | inode 1001: link count: 1 ] --> pwd data: /bin data +-----------+ +-----------+ | cc - 1002 | | cp - 1001 | +-----------+ +-----------+ |... | |... | inode 1001: link count: 1 inode 1002: link count: 1 inode 1002 data: +-----------+ | /bin/cp | +-----------+ [Question: If we have a hard link hard_link --> /tmp/log.txt, then we type: $ mv /tmp/log.txt /tmp/good_log.txt What will happen? If we have a soft link soft_link --> /tmp/log.txt, then we type: $ mv /tmp/log.txt /tmp/good_log.txt What will happen? ] Hard link will have no influence, but a soft link will be invalid... [Question: What if we do $ rm -f /tmp/log.txt What happen if we have link_file as hard/soft link? ] Hard link: 1. link count for that inode will decrement. 2. link_file still valid since link count is not 0. Soft link: Simply not valid anymore...