Info

struct fdtable {

unsigned int max_fds;

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 in struct 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_fds specifies 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 necessary (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 structures are managed. I explain this below, but first have to clarify what the remaining members of the structure mean:

□ fd is an array of pointers to file structures 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 by max_fds.

□ open_fds is 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; otherwise, it is unused. The current maximum number of bit positions is specified by max_fdset.

□ close_on_exec is also a pointer to a bit field that holds the descriptors of all files to be closed on the exec system call (see Chapter 2).

At a first glance, some information seems to be duplicated between struct fdtable and struct files_struct: the close-on-exec and open file descriptor bitmap as well as the file array. This is not the case because the elements in file_struct are real instances of some data structure, while the elements of fdtable are pointers. Indeed, fd, open_fds, and close_on_exec are initialized so that they point to these three elements in the structure. As a result, the fd array contains nr_open_default entries; close_on_exec and open_fds are represented by bitmaps with bits_per_long entries initially as I mentioned above. Since nr_open_default is set to bits_per_long, all share the same size. Should the need for more open files arise, the kernel allocates an instance of fd_set to replace the initial embedded_fd_set. fd_set is defined as follows:

Continue reading here: Info

Was this article helpful?

0 0