"Peter Cheung" <
mche...@gmail.com> wrote:
Rod Pemberton wrote:
"Peter Cheung" <
mche...@gmail.com> wrote:
> > How an OS convert the filepath into an inode number?
>
>In newlib stub, in the _read function stub, i can get an integer to
> represent the file, i think that is the inode number.
No, that should be the fd "file descriptor" number returned by
the open() system call.
The file descriptor is usually an index into a table maintained
by each process. Each entry in the table may point to an
in-memory inode structure for a given file or indirectly through
an intermediate 'filp' structure which holds the descriptors's
file pointer/seek position and a pointer to the inode.
Multiple file descriptors can point to and share a single filp and
is used to duplicate file descriptors for dup() and fork() calls in Unix.
> But i need to file path to find out the location of that file
> in the FS, thats why i was asking the way to convert the
> inode number into a file path.
Inodes are on-disk structures that contain information about a file,
the sectors it used, file size, permissions etc. They are held in a
table on the disk either all in one place or spread across the disk.
In Unix the directories are like normal files and contain entries
with the filename and inode number as a pair. So the inode of
a file can be found.
Inodes are on-disk but when they are opened either a copy
or an in-kernel version of it is allocated within the kernel.
In Unix the open() and other system calls that have a pathname as an
argument use the function namei() to convert a pathname to a locked
inode. This looks up each component of a pathname, starting in the
current directory (it should hold a pointer to it's inode). For each
component it searches the directory to find the inode number
of the matching component. If the next component isn't the last
component and is a directory then the lookup is repeated with
the new inode.
inode = namei (pathname);
next_inode = lookup (directory_inode, component);
Of course now that we have VFSs and multiple file systems
inodes in memory are now vnodes.
Also namei() is a lot more complicated with the need to handle
different file system types as well as options to control what
is actually looked up.
For example to create a file you need to lookup the filename
to see if it exists and if not create it in the parent directory,
so namei() needs to return the opened file inode or the parent
directory's inode into which it can create it.
Also handling of symbolic links. Normally you'll want to
open whatever a symbolic link points to, but to delete a
symbolic link you actually want to open it so namei() needs
flags for that.
namei() may also be named as lookupname() or lookuppn()
depending on the OS.
From page 75 of Bach's, "The Design of The Unic Operating
System," namei() looks like this:
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 inode);
--
Marv