0% found this document useful (0 votes)
101 views

Lecture3-Inode Table Content and InodeConversion

The document discusses the internal representation of files in UNIX systems. It covers inodes, which contain metadata about files such as ownership, permissions, size. Every file has a unique inode number but can have multiple names mapped to that inode. When a process refers to a file by name, the kernel parses the path and retrieves the inode. Inodes contain disk addresses of file data blocks. Directories store inode numbers and names of contained files. The namei algorithm is used to convert pathnames to inodes by parsing the path component by component and looking up each name in directory inodes until reaching the target inode.

Uploaded by

Yamini
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
101 views

Lecture3-Inode Table Content and InodeConversion

The document discusses the internal representation of files in UNIX systems. It covers inodes, which contain metadata about files such as ownership, permissions, size. Every file has a unique inode number but can have multiple names mapped to that inode. When a process refers to a file by name, the kernel parses the path and retrieves the inode. Inodes contain disk addresses of file data blocks. Directories store inode numbers and names of contained files. The namei algorithm is used to convert pathnames to inodes by parsing the path component by component and looking up each name in directory inodes until reaching the target inode.

Uploaded by

Yamini
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 33

Lecture-3

Internal Representation Of Files,


Incore- Inode, Conversion of a
Pathname to an Inode
Internal Representation Of Files
• Every file on a UNIX system has a unique inode (contains
information necessary for a process to access a file).
• Every file has one inode, but it may have several names,
all of which map into the inode. Each name is called a link.
• When a process refers to a file by name, the kernel parses
the file name one component at a time, checks that the
process has permission to search the directories in the
path, and eventually retrieves the inode for the file.
• When a process creates a new file, the kernel assigns it an
unused inode. Inodes are stored in the file system.
Inodes
• Consist of:
– File type (regular, directory, character or block special, or
FIFO – pipes)
– File owner identifier (individual owner & group owner)
– File access permissions (owner, group, other: read, write,
execute)
– File access time (File last accessed, File last modified, inode
last modified)
– Number of links to the file
– Table of contents for the disk addresses of data in a file
– File size
Description of Inode table

1) File owner identifier. Ownership is divided between an individual


owner and a "group" owner and defines the set of users who have
access rights to a file. The super user has access rights to all files in the
system.
2) File type. Files may be of type regular, directory, character or block
special, or FIFO (pipes).
3) File access permissions. The system protects files according to three
classes: the owner and the group owner of the file, and other users;
each class has access rights to read, write and execute the file, which
can be set individually.
4) File access times, giving the time the file was last modified, when it was
last accessed, and when the inode was last modified.
Description of Inode table cont’d
5) Number of links to the file, representing the number of names
the file has in the directory hierarchy.
6) Table of contents for the disk addresses of data in a file. Although
users treat the data in a file as a logical stream of bytes, the
kernel saves the data in discontiguous disk blocks. The inode
identifies the disk blocks that contain the file's data.
7) File size. Data in a file is addressable by the number of bytes
from the beginning of the file, starting from byte offset 0, and the
file size is l greater than the highest byte offset of data in the file.
For example, if a user creates a file and writes only 1 byte of data at
byte offset 1000 in the file, the size of the file is 1001 bytes.
Sample Disk Inode
• You can also use stat command to find out inode number and its
attribute:
$ stat /etc/passwd
• Output:
File: `/etc/passwd' Size: 1988 Blocks: 8
IO Block: 4096 regular file Device: 341h/833d
Inode: 32820 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root)
Gid: ( 0/ root)
Access: 2005-11-10 01:26:01.000000000 +0530
Modify: 2005-10-27 13:26:56.000000000 +0530
Change: 2005-10-27 13:26:56.000000000 +0530
Structure of a Regular File
• The inode contains the table of contents to locate
a file's data on disk. Since each block on a disk is
addressable by number, the table of contents
consists of a set of disk block numbers.
• The kernel allocates file space one block at a time
and allows the data in a file to be spread
throughout the file system. But this allocation
scheme complicates the task of locating the data.
Structure of a Regular File
• The system V UNIX system runs with 13 entries in the inode table of contents, but
the principles are independent of the number of entries.
• The blocks marked "direct" in the figure contain the numbers of disk blocks that
contain real data.
• The block marked "single indirect" refers to a block that contains a list of direct
block numbers.
• To access the data via the indirect block, the kernel must read the indirect block,
find the appropriate direct block entry, and then read the direct block to find the
data.
• The block marked "double indirect" contains a list of indirect block numbers, and
the block marked "triple indirect" contains a list of double indirect block numbers.
• The maximum number of bytes that could be held in a file is calculated at well
over 16 gigabytes, using 10 direct blocks and 1 indirect, 1 double indirect, and 1
triple indirect block in the inode .
Byte Capacity of a File - 1 K Bytes per
Block
Directories
• Directories are the files that give the file system its hierarchical
structure; they play an important role in conversion of a file name
to an inode number.
• A directory is a file whose data is a sequence of entries, each
consisting of an inode number and the name of a file contained in
the directory.
• A path name is a null terminated character string divided into
separate components by the slash ("/") character. Each
component except the last must be the name of a directory, but
the last component may be a non-directory file.
• UNIX System V restricts component names to a maximum of 14
characters; with a 2 byte entry for the inode number, the size of a
directory entry is 16 bytes.
Directory Layout for /etc
Directory Layout for /etc
• Every directory contains the file names dot and dot-dot
("." and “.. ") whose inode numbers are those of the
directory and its parent directory, respectively.
• The inode number of "." in "/etc" is located at offset 0 in
the file, and its value is 83. The inode number of ".." is
located at offset 16, and its value is 2. Directory entries
may be empty, indicated by an inode number of 0.
• The kernel stores data for a directory just as it stores data
for an ordinary file, using the inode structure and levels
of direct and indirect blocks.
Inode
• The contents of a file change only when writing it.
• The content of an inode change when changing
the contents of a file or when changing its owner,
permission, or link settings.
• Changing the content of a file automatically
implies a change to the inode, but changing the
inode does not imply that the contents of the file
change.
In-core Copy Of The Inode
• Inodes exist in a static form on disk and the kernel reads
them into an in-core inode to manipulate them.
• Difference between disk inode and in-core inode:
– On-disk inode refers to inode stored in disk within the inode list
– In-core inode refers to inode stored in memory when a file is open
• When a file is opened then the kernel copies the inode
into memory. As the file changes, the in-core inode is
updated usually more often than the on-disk copy. And the
in-core inode has a few extra fields that are only needed
while the file is opened.
In-core copy contains
• The in-core copy of the inode contains the
following fields in addition to the fields of the
disk inode:
– status of the in-core inode
– logical device number of file system
– inode number
– reference count
Conversion Of A Pathname to An Inode

• The kernel works internally with inodes rather than with


path names, it converts the path names to inodes to access
files.
• The algorithm namei parses the path name one component
at a time, converting each component into an inode, based
on its name and the directory being searched, and
eventually returns the inode of the input path name.
• All path name searches start from the current directory of
the process unless the path name starts with the slash
character, significantly that the search should start from
the root directory.
Algorithm used by namei
• iget Algorithm
– Creates in-core copy of disk inode, making inode locked and increase the reference
count by 1.
• iput Algorithm
– It releases the in-core copy and write to disk, it decrement the RC by 1 and if count
drops 0, kernel writes the inode to the disk.
• bmap Algorithm
– It converts the byte offset in the directory to the appropriate disk block.
• bread Algorithm
– Reads the disk block given by bmap. It searches the block for the pathname
component, treating the content of the block as a sequence of directory entries.
• brelse Algorithm
– If bread matches pathname, the brelse records the inode number of the matched
directory entry and releases the block.
How conversion done:
• Namei uses intermediate inodes as it parses a path name; call them working
inodes.
• The kernel does a linear search of the directory file associated with the working
inode, trying to match the path name component to a directory name.
• Starting at byte offset 0, it converts the byte offset in the directory to the
appropriate disk block according to algorithm bmap and reads the block using
algorithm bread.
• If it find a match , it records the inode number of the matched directory entry,
release the block (algorithm brelse) and the old working inode (algorithm iput),
and allocates the inode of the matched component (algorithm iget).
• If kernel does not match the path name with any names in the block, it releases
the block, adjusts the byte offset by the number of bytes in a block, converts the
new offset to a disk block number ( algorithm bmap), and reads the next block.
• The kernel repeats the procedure until it matches the pathname component with a
directory entry name, or until reaches the end of the directory.
Path conversion to an inode
Algorithm namei /* convert path name to inode */
Input: path name
Output: locked inode
if (path name starts with root)
working inode= root inode (algorithm iget);
else
working inode= current directory inode (algorihtm iget);
while (there is more path name)
read next path name component from input
Verify that working inode is of directory, access permission ok;
If( working inode is of root and component is “..”)
continue; /* loop back to while */
read directory (working inode) 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 inode);
19
Conversion of a path name to an inode
• Algorithm namei
– If path name starts from root, then the kernel assigns root
inode(iget) to working inode
– Otherwise, the kernel assigns current directory inode to
working inode
– While there is more path name, the kernel reads next
path name component from input, and verifies that
working inode is of directory, access permissions OK.
• If working inode is of root and component is ‘..’, then the
kernel checks whether there is more path name or not.
• Otherwise the kernel reads directory by repeated use of
bmap, bread, brelse.
Example using namei algorithm
• Suppose a process wants to open the file “/etc/passwd”.
• When the kernel starts parsing the file name, it encounters
“/” and gets the system root inode. Making root its current
working inode
• After checking that the current inode is that of the
directory(“/”) and that the process has the necessary
permissions to search it, the kernel searches root for a file
whose name is “etc”.
• It accesses the data in the root directory block by block and
searches each block one entry at a time until it locates an
entry for “etc”.
Example continue….
• On finding the entry, the kernel releases the inode for
root(algorithm iput) and allocates the inode for “etc”
(algorithm iget) according to the inode number of the entry
just found.
• After ascertaining that “etc” is a directory and that it has the
required search permission, the kernel searches “etc” block
by block for a directory structure entry for the file “passwd”.
• On finding it, the kernel releases the inode for “etc”, allocates
the inode for “passwd”.
• And since the path name is exhausted- return that inode.
Home Directory
• The directory in which you find yourself when you first login is called your
home directory.
• You will be doing much of your work in your home directory and
subdirectories that you'll be creating to organize your files.
• You can go in your home directory anytime using the following command

$cd ~
$
• Here ~ indicates home directory. If you want to go in any other user's
home directory then use the following command −
$cd ~username
$
• To go in your last directory you can use following command −
$cd -
$
Absolute/Relative Pathnames
• Directories are arranged in a hierarchy with root (/) at the top. The position of any
file within the hierarchy is described by its pathname.
• Elements of a pathname are separated by a /.
• A pathname is absolute if it is described in relation to root, so absolute pathnames
always begin with a /.
• These are some example of absolute filenames.
/etc/passwd
/users/sjones/chem/notes
/dev/rdsk/Os3
• A pathname can also be relative to your current working directory. Relative
pathnames never begin with /. Relative to user amrood' home directory, some
pathnames might look like this −
chem/notes
personal/res
• To determine where you are within the filesystem hierarchy at any time, enter the
command pwd to print the current working directory −
$pwd /user0/home/amrood
$
Listing Directories
• To list the files in a directory you can use the
following syntax −
$ls dirname
• Following is the example to list all the files
contained in /usr/local directory −
• $ls /usr/local
X11 bin gimp Jikes sbin
ace doc include lib share
atalk etc info man ami
Creating Directories
• Directories are created by the following command −
$mkdir dirname
Here, directory is the absolute or relative pathname of the directory you want to
create. For example, the command −
$mkdir mydir
$
• Creates the directory mydir in the current directory. Here is another example −
$mkdir /tmp/test_dir
$
• This command creates the directory test_dir in the /tmp directory. The mkdir
command produces no output if it successfully creates the requested directory.
• If you give more than one directory on the command line, mkdir creates each of
the directories. For example −
$mkdir docs pub
$
• Creates the directories docs and pub under the current directory.
Creating Parent Directories
• Sometimes when you want to create a directory, its parent
directory or directories might not exist. In this case, mkdir issues
an error message as follows −
$mkdir /tmp/amrood/test
mkdir: Failed to make directory "/tmp/amrood/test";
No such file or directory
$
• In such cases, you can specify the -p option to the mkdir
command. It creates all the necessary directories for you. For
example −
$mkdir -p /tmp/amrood/test
$
• Above command creates all the required parent directories.
Removing Directories
• Directories can be deleted using the rmdir command as follows −
$rmdir dirname
$
• Note − To remove a directory make sure it is empty which means
there should not be any file or sub-directory inside this directory.
• You can remove multiple directories at a time as follows −
$rmdir dirname1 dirname2 dirname3
$
• Above command removes the directories dirname1, dirname2,
and dirname2 if they are empty.
• The rmdir command produces no output if it is successful.
Renaming Directories
• The mv (move) command can also be used to
rename a directory. The syntax is as follows −
$mv olddir newdir
$
• You can rename a directory mydir to yourdir
as follows −
$mv mydir yourdir
$
The directories . (dot) and .. (dot dot)
• The filename . (dot) represents the current working directory; and
the filename .. (dot dot) represent the directory one level above
the current working directory, often referred to as the parent
directory.
• If we enter the command to show a listing of the current working
directories files and use the -a option to list all the files and the -l
option provides the long listing, this is the result.
$ls -la
drwxrwxr-x 4 teacher class 2048 Jul 16 17.56 .
drwxr-xr-x 60 root 1536 Jul 13 14:18 ..
$
$ cd ..
will take you one level up directory.
Unix Commands - find
• Find is one of the powerful utility of unix or linux used for searching the files in a
directory hierarchy.
• Syntax:
$ find [pathnames] [condition]
• Examples:
1. How to find for a file using name?
Find –name “abc.txt”
This will find all the files with name abc.txt in the current and sub- directories.
2. How to find for a file in the current directory only?
find –maxdepth 1 –name “abc.txt”
3. How to find for a file in a specific directory?
find /etc –name “abc.txt”
4. How to find for files using name and ignoring case?
find -iname “abc.txt“
5. How to find the files whose name are not "sum.java"?
find -not -name “abc.txt"
How to find files based on the file type?

1. Finding directories
find –type d
2. Finding socket files
find –type s
3. Finding regular files
find –type f
4. Finding hidden directories
find –type d –name “.*”
5. Finding hidden files
find –type f –name “.*”
How to find files based on the file size?
1. Finding files whose size is exactly 10M
find –size 10M
2. Finding files larger than 10M size
find . -size +10M
3. Finding files smaller than 10M size
find . -size -10M
4. How to find the files which are modified after the modification of a give file.
find -newer “abc.txt“
5. Display the files which are accessed after the modification of a give file.
find -anewer "sum.java“
6. How to find the files based on the file permissions?
find . -perm 777
7. Find the files which are modified within 30 minutes.
find . -mmin -30
8. Find the files which are accesed within 1 day.
find . -atime -1

You might also like