Do files in /proc/PID directory have their own proc_dir_entry instance? - linux

Do files in /proc/PID directory (including /proc/PID ) have their own proc_dir_entry instance?
As I known, each normal file in /proc including /proc has their proc_dir_entry instance.
(The instance address is stored in proc_inode.pde.)
After surfing the procfs source code in Linux 2.6.11, seems that the kernel doesn't create a corresponding proc_dir_entry instance for each pid directory in /proc and each file in pid directory.
Is this true?
If it's not true, which file in the kernel source code shows that the kernel create proc_dir_entry instance for pid directory in /proc.

I think you're right, it looks like the pid entries are handled differently. See fs/proc/base.c.

Yes, every process has its proc_dir_entry that is /proc/PID/task directory in common.

Related

Linux kernel: finding task struct for given file struct

I'm doing some research of Linux kernel file descriptors maintenance. I'm trying to find out how the file descriptors are working from Linux kernel space perspective. I know that task_struct contains files_struct which contains in turn table of descriptors. So for given task_struct I can list the descriptors for this task. Each of descriptor is of type "struct file".
Now I'd like to know how to do reverse mapping - I mean having the pointer to "struct file" - how can I find out to which "task_struct" it belongs?

How to check in linux kernel at vfs layer whether the file object is for a directory or a file

How to check in linux kernel at vfs layer whether the file object is for a directory or a file?
I have found that there is a function called is_dx(dir) which checks for this but it is present in namei.c in ext3 or ext4. I need to do this at vfs layer that is independent of the file system.
How about the S_ISDIR() macro defined in include/linux/stat.h? It takesinode->i_mode field to check if the inode in question belongs to a directory or a file.
Having in hand the inode of the initial directory, the code
examines the entry matching the first name to get the
corresponding inode.
q Then the directory file having that node is read from disk and
the entry matching the second name is examined to derive the
corresponding inode.
q This procedure is repeated for each name included in the path.
The dentry cache considerably speeds up the procedure
File system operations are mostly done at the dcache level , so
they are all under kernel lock.

Why do we need directory structure for file system?

Jos of MIT OS lesson only uses File structure to describe regular file or dir.
But linux kernel uses dentry/inode/file structure to describe files.
Is it neccessary to use dentry for file system?
In Linux, dentry is a directory entry that associates inode and file object, but it is not necessary just a directory, could represent a file. Dentry enables the hard link which allows allow multiple hard links to be created for the same file. So you can create multiple names for the same file.
Dentry cache also does matter for performance of File system. The following picture is from "Understanding the Linux Kernel, 3rd Edition" which shows interactions between processes and VFS objects.
Jos does use directory entries. It just uses the File object to store directories (they use teh same object for storing directory data and file data)

creating inode while creating pipe, fifo or socket

I have general question about Linux. Will the inode be created if I create a fifo? pipe? socket?
On Linux the answer can be obtained from /proc/<PID>/fd directory. To quote /proc documentation ( man 5 proc ):
For file descriptors for pipes and sockets, the entries will be
symbolic links whose content is the file type with the inode. A
readlink(2) call on this file returns a string in the format:
type:[inode]
For example, socket:[2248868] will be a socket and its inode is
2248868. For sockets, that inode can be used to find more information in one of the files under /proc/net/.
Let's verify that:
$ bash -c 'true | ls -l /proc/self/fd/0'
lr-x------ 1 user user 64 Sep 13 03:58 /proc/self/fd/0 -> 'pipe:[54741]'
So will pipes and sockets have an inode ? Yes ! What about FIFOs ? We can guess that since they have a filename, they do have inode ( and I don't think directory entries without inode can exist ). But lets verify:
$ mkfifo foobar.fifo
$ ls -i foobar.fifo
1093642 foobar.fifo
The answer is "yes, FIFOs have inodes,too".
However, this raises an important question: inodes are properties of filesystems, and inodes aren't unique accross filesystems, so which filesystem is being referenced when we see a pipe inode ? Well, turns out there exists pipefs virtual filesystem which is mounted in Kernel space, rather than userspace. It manages both pipes and FIFOs, so the inode number you see is the /proc example is the property of those filesystems, rather than the filesystem you have on disk. And yes, anonymous pipes and anonymous sockets won't have inode on disk filesystem, because there's no filename and no bytes on disk (although there may be caching of data, and in fact old Unixes cached pipes to disk). FIFOs and Unix-domain sockets, however, have filename on the filesystem, so in foobar.fifo example that inode belongs on the disk filesystem.
See also:
How pipes work in Linux
What is the difference between “Redirection” and “Pipe”?
No inode will be created for an anonymous pipe or a socket, as an inode is a property of a filesystem and neither of these two lives as a filesystem entity (they don't have a file path). They only have file descriptors.
However, for named pipes (aka fifo) an inode is created as it lives as an filesystem entity.

Unmounting proc file system

As far as I know proc file system is a virtual file system. Is there any way to unmount the proc file system and even if I do that what will be the consequences after that.
You can check (as root) who is using a mounted filesystem like so:
fuser -m /proc
Typically, your box will not be very usable if you kill all the processes using /proc. Otherwise, there is no law saying it has to be mounted, beyond all and sundry developer assuming that it is.
umount will work like on any other file system (same conditions for a filesystem to be unmonted). You can expect a whole lot of this to stop working as soon as you do that though (including very simple utilities like ps).

Resources