Note: the code fragments in this section won't match the sources for your own kernel exactly, as the /proc filesystem has been expanded since this was originally written, and is being expanding still more. For instance, the root_dir structure has nearly doubled in size from the one quoted here below.
Unlike in most filesystems, not all inode numbers in the proc filesystem are unique. Some files are declared in structures like
and some files are dynamically created as the filesystem is read. All the process directories (those with numerical names and self) essentially have inode numbers that are the pid shifted left 16 bits, but the files within those directories re-use low (1-10 or so) inode numbers, which are added at runtime to the pid of the process. This is done in inode.c by careful re-assignment of inode_operation structures.
Most of the short read-only files in the root directory and in each process subdirectory one use a simplified interface provided by the array_inode_operations structure, within array.c.
Other directories, such as /proc/net/, have their own inode numbers. For instance, the net directory itself has inode number 8. The files within that directory use inode numbers from the range 128-160, and those are uniquely identified in inode.c and the files given the proper permissions when looked up and read.
Adding a file is relatively simple, and is left as an exersize for the reader. Adding a new directory is a little bit harder. Assuming that it is not a dynamically allocated directory like the process directories, here are the steps:
add a section that looks like this:
but modify it to to do what you want. For instance, perhaps you have a range of 200-256, and some files, inodes 200, 201, and 202, and some directories, which are inodes 204 and 205. You also have a file that is readable only by root, inode 206.
Your example might look like this:
You will then add a new file to this structure, like this, using the next available inode number:
You will then have to provide for this new directory in inode.c, so:
[Please note: I have made changes similar to these (I wrote the /proc/net/ support). However, this has been written from memory, and may be unintentionally incomplete. If you notice any inadequacies, please explain them to me in as complete detail as possible. My email address is firstname.lastname@example.org]