Tuesday, February 22, 2022

[SOLVED] What is the difference between an open file and inode?

Issue

I'm reading Linux Device Driver programming 3rd edition and I've been trying to get a grasp of openfiles vs inodes. From what the book says , "a file structure in the kernel is considered to be an open file." The book also says ,"an inode structure is used internally in the kernel to represent files. Therefor it is different from a file structure which is used to represent an open file." That statement in itself is totally confusing to me since a file and an open file is the same thing in my mind. I don't even understand what they mean by an open file in this context. I'm totally confused, what is an open file? What is an inode? And what is the difference?


Solution

ext2+, NTFS, and other filesystems have a master table of files on the drive, and directories are just a special kind of file full of records that point to entries in the file table. (This setup allows for hard links, as well as "temporary files" that aren't visible via the directory structure.) An "inode" is Linux's (and probably other *nixes') term for those master file table entries.

An inode doesn't track the current position within the file or the current mode (open for reading, writing, both...?), though. It only contains info that helps the OS find the contents of the file on disk and keep people who shouldn't be messing with it from doing so. You need a different structure to track that info. That'd likely be the "open file" structure you're seeing.

Apparently, the "file" structure also has a structure inside of it full of pointers to functions for stuff you can do with the file. This would be in order to support Unix's "everything is a file" philosophy and let you read and write to, say, a socket the same way you would to a regular file, as well as to provide a way to abstract away the filesystem-specific code from the code that'd work for everything (which makes supporting multiple filesystem types a lot easier).



Answered By - cHao
Answer Checked By - Timothy Miller (WPSolving Admin)