Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 8: The Virtual Filesystem


structures are managed. I explain this below, but first have to clarify what the remaining members of the
structure mean:

❑ fdis an array of pointers tofilestructures that manage all information on an opened file. The
file descriptor of the userspace process acts as an array index. The current size of the array is
defined bymax_fds.
❑ open_fdsis a pointer to a bit field that manages the descriptors of all currently opened files.
There is just one bit for each possible file descriptor; if it is set to 1, the descriptor is in use; other-
wise, it is unused. The current maximum number of bit positions is specified bymax_fdset.
❑ close_on_execis also a pointer to a bit field that holds the descriptors of all files to be closed on
theexecsystem call (see Chapter 2).

At a first glance, some information seems to be duplicated betweenstruct fdtableandstruct
files_struct: the close-on-exec and open file descriptor bitmap as well as thefilearray. This is
not the case because the elements infile_structare real instances of some data structure, while the
elements offdtableare pointers. Indeed,fd,open_fds,andclose_on_execare initialized so that
they point to these three elements in the structure. As a result, thefdarray containsNR_OPEN_DEFAULT
entries;close_on_execandopen_fdsare represented by bitmaps withBITS_PER_LONGentries initially
as I mentioned above. SinceNR_OPEN_DEFAULTis set toBITS_PER_LONG, all share the same size. Should
the need for more open files arise, the kernel allocates an instance offd_setto replace the initial
embedded_fd_set.fd_setis defined as follows:

<posix_types.h>
#define __NFDBITS (8 * sizeof(unsigned long))
#define __FD_SETSIZE 1024
#define __FDSET_LONGS (__FD_SETSIZE/__NFDBITS)

typedef struct {
unsigned long fds_bits [__FDSET_LONGS];
} __kernel_fd_set;

typedef __kernel_fd_set fd_set;

Note thatstruct embedded_fd_setcan be typecast intostruct fd_set.Inthissense,embedded_fd_set
is a shrunken version offd_setthat can be used in the same way but requires less space.

If one of the initial limits for the bitmaps or thefdarray is too low, the kernel can expand the relevant
elements by making the pointers point to correspondingly larger structures. The arrays are expanded in
different steps — this explains why there are different maximum values for the descriptor numbers and
file elements in the structure.

One component used for the definition offiles_structstill needs to be discussed:struct file.The
structure holds characteristic information on a file as seen by the kernel. Slightly simplified, it is defined
as follows:

<fs.h>
struct file {
struct list_head fu_list;
struct path f_path;
#define f_dentry f_path.dentry
Free download pdf