Linux Kernel Architecture

(Jacob Rumans) #1

Chapter 8: The Virtual Filesystem


struct fdtable fdtab;

int next_fd;
struct embedded_fd_set close_on_exec_init;
struct embedded_fd_set open_fds_init;
struct file * fd_array[NR_OPEN_DEFAULT];
};

next_fddenotes the number of the file descriptor that will be used when a new file is opened.
close_on_exec_initandopen_fds_initare bitmaps.close_on_execcontains a set bit for all file
descriptors that will be closed on exec.open_fds_initis the initial set of file descriptors.struct
embedded_fd_setis just a simpleunsigned longencapsulated in a special structure.


<file.h>
struct embedded_fd_set {
unsigned long fds_bits[1];
};

fd_arraycontains a pointer to an instance ofstruct filefor every open file; I will discuss this structure
in a moment.


By default, the kernel allows each process to openNR_OPEN_DEFAULTfiles.Thisvalueisdefined
ininclude/linux/sched.hwith the default setting ofBITS_PER_LONG. On 32-bit systems, the
initial number of files is therefore 32; 64-bit systems can handle 64 files simultaneously. If a process
attempts to open more files at the same time, the kernel must increase the memory space for various
elements offiles_structthat are used to manage information on all files associated with the
process.


The most important information is contained infdtab. The kernel defines another data structure for this
purpose.


<file.h>
struct fdtable {
unsigned int max_fds;
struct file ** fd; /* current fd array */
fd_set *close_on_exec;
fd_set *open_fds;
struct rcu_head rcu;
struct files_struct *free_files;
struct fdtable *next;
};

Both an instance of this structure itself and a pointer to an instance are included instruct files_struct
because the RCU mechanism is used to enable lock-free reading of these data structures, which
speeds up things. Before I come back to how this is done, I need to introduce the meaning of the
elements:


max_fdsspecifies the current maximum number of file objects and file descriptors that the process
can handle. There are no fundamental upper limits because both values can be increased if neces-
sary (providing they do not exceed the value specified by Rlimit — but this has nothing to do with
the file structure). Although the same number of file objects and file descriptors is always used, the
kernel must define different maximum numbers. This is due to the way in which the associated data

Free download pdf