Dr.Knowitall
Dr.Knowitall

Reputation: 10508

What is the difference between an open file and inode?

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?

Upvotes: 7

Views: 8029

Answers (5)

firo
firo

Reputation: 1062

An inode is an instance/body of a 'file'(e.g. /etc/passwd) or 'directory'(e.g. /etc). However, the 'struct file' is a part of the instance of the changing(e.g. open-read/write-close) on the inode of a 'file' or 'directory'.

Every file system(including VFS) has to provide to basic mechanisms: 1) Representation of a 'file' or 'directory'; that is the inode. 2) Operations to manipulate the inode; that is open-read/write-close. Every time we open a 'file' or 'directory', we get a 'struct file' in kernel. So we could have many 'struct file's for a 'file' or 'directory'. Every 'struct file' reflects an operation on it. There are some flags and mode and lseek in 'struct file' which we used to assist the open-read/write-close.

Upvotes: 0

Mohsen Zahraee
Mohsen Zahraee

Reputation: 3483

according to kernel device driver 3rd (ldd3):

The inode structure is used by the kernel internally to represent files. Therefore, it is different from the file structure that represents an open file descriptor. There can be numerous file structures representing multiple open descriptors on a single file, but they all point to a single inode structure.

Upvotes: 0

cHao
cHao

Reputation: 86575

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).

Upvotes: 3

Marc B
Marc B

Reputation: 360872

An inode is on-disk metadata that represents a file. It contains the file's permission bits, the creation/modification/access timestamps, the actual file type, size of the file, etc...

A "open file" is simply a bit of memory in your process. Generally it's an array entry, which the filehandle you get from fopen() calls is the specific array key of. That array entry will contain (among other things) the current location of the cursor in the file where you'd be reading/write data from using the fwrite/fread calls.

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 882596

An inode is basically the data sitting in one of more files (it's actually an index into a table of some sort with which you can locate that data).

A file, on the other hand, is a directory entry that points to the inode.

This is how UNIX-like operating systems implement hard links, the ability for two files to be "equivalent", so that changing one will change the other. Since inodes are unique per file system, you can have more than one file referencing the same data (as long as it's on the same file system, of course).

Upvotes: 4

Related Questions