Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 8: The Virtual Filesystem


link_countprevents recursive loops, andtotal_link_countlimits the maximum number of links in a
pathname. By default, the kernel permitsMAX_NESTED_LINKS(usually set to 8) recursive and 40 consecu-
tive links — the latter constant is hardcoded and not definable via a pre-processor symbol.

At the beginning of thedo_follow_linkroutine, the kernel first checks whether the maximum value of
either of the counters has been exceeded. If so,do_follow_linkis aborted with the error code-ELOOP.

If not, both counters are incremented by 1, and the filesystem-specificfollow_linkroutine is invoked
to follow the current link. If the link does not point to a further link (and the function therefore simply
returns the newdentryentry), the value oflink_countis decremented by 1 as shown in the following
code segment:

fs/namei.c
static inline int do_follow_link(struct dentry *dentry, struct nameidata *nd)
{
...
current->link_count++;
current->total_link_count++;
err = __do_follow_link(path, nd);
current->link_count--;
...
}

When is the value oftotal_link_countreset? Not at all — at least not during lookup for a single path
component. Because this counter is a mechanism to limit thetotal numberof links used (which need not
be recursive to reach a high figure), the counter is reset to 0 when lookup is initiated for afullpath or
filename inpath_walk(this function is called bydo_path_lookup).Everysymbolic link in the lookup
operation (not just recursive links) adds to its value.

Opening Files


Files must be opened before reading or writing. In the view of the application, this is done by theopen
function of the standard library, which returns a file descriptor.^23 The function uses the identically named
opensystem call, which invokes thesys_openfunction infs/open.c. The associated code flow diagram
is shown in Figure 8-11.

Asafirststep,force_o_largefilechecks if the flagO_LARGEFILEshould always be set irregardless of
which flags were passed from userland. This is the case if the word size of the underlying processor is not
32 bits, that is, a 64-bit system. Such systems use 64-bit indexing, and large files are thus the only sensible
default on them. The proper work of opening the file is then delegated todo_sys_open.

In the kernel, each opened file is represented by a file descriptor that acts as a position index for
a process-specific array (task_struct->files->fd_array). This array contains an instance of the
abovementionedfilestructure with all necessary file information for each opened file. For this reason,
get_unused_fd_flagsis first invoked to find a used file descriptor.

Because a string with the name of the file is used as a system call parameter, the main problem is to find
the matching inode. The procedure described above does this.

(^23) It would also be possible to useopenat, which opens a file relative to a directory. The mechanisms are, however, more or less
identical.

Free download pdf