19.4 Some Comments

“namei” is a key procedure which would seem to have been written very early, to have been thoroughly debugged and then to have been left essentially unchanged. The interface between “namei” and the rest of the system is rather complex, and for that reason alone, it would not win the prize for “Procedure of the Year”.

“namei” is called thirteen times by twelve different procedures:

line

routine

parameters

3034

exec

uchar

0

3543

chdir

uchar

0

5770

open

uchar

0

5914

link

uchar

0

6033

stat

uchar

0

6097

smount

uchar

0

6186

getmdev

uchar

0

6976

owner

uchar

0

5786

creat

uchar

1

5928

link

uchar

1

5958

mknod

uchar

1

3515

unlink

uchar

2

4101

core

schar

1

It will be seen that:

(a)

there are two calls from “link”;

(b)

the calls can be divided into four categories, of which the first is by far the largest;

(c)

the last two categories have only one representative each;

(d)

in particular, there is only one call involving the routine “schar”, which is always for a file called “core”. (If this case were handled as a special case e.g. where the second parameter had the value “3”, then the “uchar”s and “schar” could be eliminated.)

“namei” may terminate in a variety of ways:

(a)

if there has been an error, then a “NULL” value is returned and the variable “u.u_error” is set.

(Most errors result in a branch to the label “out” (7669) so that reference counts for the inodes are properly maintained (7670). This is not necessary if the failure occurs in “iget” (7664).);

(b)

if “flag==2” (i.e. the call is from “unlink”), the value returned (in normal circumstances) is an “inode” pointer for the parent directory of the named file (7660);

(c)

if “flag==1” (i.e. the call is from “creat” or “link” or “mknod”, and a file is to be created if it does not already exist) and if the named file does not exist, then a “NULL” value is returned (7610). In this case a pointer to the “inode” for the directory which will point to the new file, is left in “u.u_pdir” (7606). (Note also that in this case, “u.u_offset” is left pointing either at an empty directory entry or at the end of the directory file.);

(d)

if in the remaining cases, the file exists, an “inode” pointer for the file is returned (7551). The “inode” is locked and the reference count has been incremented. A call to “iput” is needed subsequently to undo both these side effects.