views:

550

answers:

3

The C language convention counts array indices from 0. Why do inode numbers start from 1 and not 0?

If inode 0 is reserved is for some special use, then what is the significance of inode 0?

+10  A: 

0 is used as a sentinel value to indicate null or no inode. similar to how pointers can be NULL in C. without a sentinel, you'd need an extra bit to test if an inode in a struct was set or not.

more info here:

All block and inode addresses start at 1. The first block on the disk is block 1. 0 is used to indicate no block. (Sparse files can have these inside them)

http://uranus.chrysocome.net/explore2fs/es2fs.htm

for instance, in old filesystems where directories were represented as a fixed array of file entries, deleting a file would result in setting that entry's inode val to 0. when traversing the directory, any entry with an inode of 0 would be ignored.

jspcal
It seems inode 1 is also reserved along with inode 0 for some special purposes.
Manav MN
That seems specific to ext2fs. Your question is about filesystems in general.
Alok
@Alok: a family of unix fs's share the inode concept. none recognize 0 by convention.
jspcal
Except NTFS: $MFT is inode 0, causing it not to show up in the directory listing even though it is physically present in the root directory.
Joshua
+3  A: 

Usually, the inode 0 is reserved because a return value of 0 usually signals an error. Multiple method in the Linux kernel -- especially in the VFS layer shared by all file systems -- return an ino_t, e.g. find_inode_number.

There are more reserved inode numbers. For example in ext2:

#define EXT2_BAD_INO             1      /* Bad blocks inode */
#define EXT2_ROOT_INO            2      /* Root inode */
#define EXT2_BOOT_LOADER_INO     5      /* Boot loader inode */
#define EXT2_UNDEL_DIR_INO       6      /* Undelete directory inode */

and ext3 has:

#define EXT3_BAD_INO             1      /* Bad blocks inode */
#define EXT3_ROOT_INO            2      /* Root inode */
#define EXT3_BOOT_LOADER_INO     5      /* Boot loader inode */
#define EXT3_UNDEL_DIR_INO       6      /* Undelete directory inode */
#define EXT3_RESIZE_INO          7      /* Reserved group descriptors inode */
#define EXT3_JOURNAL_INO         8      /* Journal inode */

Other fileystems use the ino 1 as root inode number. In general, a file system is free to choose its inode numbers and its reserved ino values (with the exception of 0).

dmeister
Header file: fs/ext4/ext4.h
Manav MN
Also see macro `EXT2_FIRST_INO()` - it's not actually a constant.
MSalters
A: 

When I wrote a filesystem ages ago, I used inode 0 for the .badblocks pseudo-file.

On some filesystems .badblocks is actually present in the root directory as a regular file owned by root and mode 0. root can open it but reading or writing it is undefined.

There is some ancient tradition that inodes start from 1, #1 is .badblocks, and #2 is the root directory. Even though .badblocks is not particularly well-guaranteed, many filesystems go out of their way to make root #2.

Joshua