Skip to contentSkip to conversation

Can getdents return zero inode numbers?

DISCUSSION Florian Weimer 3 participants 7 messages Mar 11, 2016 – Apr 20, 2016 VFS

lore
Share & tools
lore
#0 lore raw

Is it possible that getdents returns a zero inode number for a name which actually has a file (or directory, device node etc.) behind it?

Thanks,
Florian

#1 lore raw
Quoted 3 lines · Florian Weimer

Is it possible that getdents returns a zero inode number for a name which actually has a file (or directory, device node etc.) behind it?

The d_ino == 0 value is valid to return and means that the filename was unlinked from the directory but the entry was not actually removed.

I guess if there are files with hard links it is possible that the inode referenced by the unlinked name may still have other paths referencing it, or it is possible if there is a race condition (e.g. getdents() returns a previously deleted name returning d_ino == 0, then a file with the same name being created). POSIX doesn't require that readdir()/getdents() be up-to-date with current file create/unlink state, only when the dirfd was first opened, or if rewinddir() is called:

http://pubs.opengroup.org/onlinepubs/009695399/functions/readdir.html

If a file is removed from or added to the directory after the most recent call to opendir() or rewinddir(), whether a subsequent call to readdir() returns an entry for that file is unspecified.

Cheers, Andreas

#2 lore raw
Quoted 10 lines · Andreas Dilger
Quoted 4 lines · Florian Weimer

Is it possible that getdents returns a zero inode number for a name which actually has a file (or directory, device node etc.) behind it?

The d_ino == 0 value is valid to return and means that the filename was unlinked from the directory but the entry was not actually removed.

Okay, so it makes sense to skip over entries with d_ino == 0. I found code in glibc's glob implementation which does that and was a bit puzzled and worried that there might be Linux file systems which have zero inode numbers (so that glob would fail on them).

Thanks,
Florian

#3 lore raw
In reply to Andreas Dilger

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Quoted 10 lines · Andreas Dilger
Quoted 4 lines · Florian Weimer

Is it possible that getdents returns a zero inode number for a name which actually has a file (or directory, device node etc.) behind it?

The d_ino == 0 value is valid to return and means that the filename was unlinked from the directory but the entry was not actually removed.

Thanks.

Which file systems do this? Is it possible to see this in action?

Florian

#4 lore raw
Quoted 14 lines · Florian Weimer

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Quoted 7 lines · Andreas Dilger
Quoted 4 lines · Florian Weimer

Is it possible that getdents returns a zero inode number for a name which actually has a file (or directory, device node etc.) behind it?

Quoted 7 lines
Quoted 3 lines

The d_ino == 0 value is valid to return and means that the filename was unlinked from the directory but the entry was not actually removed.

Thanks.

Which file systems do this? Is it possible to see this in action?

I suspect that this is mostly or completely a non-issue in Linux, since all of the directory entries are copied from the filesystem directory into struct dirent, and ext4 does not return entries with i_ino = 0 back to userspace.

This was more of a problem with older *nix that allowed applications to read the on-disk directory data directly. GNU ls has NOT_AN_INODE_NUMBER=0 and treats those dirents specially, but that code is so awfully structured it is hard to see what it is doing.

Cheers, Andreas

#5 lore raw
In reply to Andreas Dilger
Quoted 7 lines · Andreas Dilger
Quoted 3 lines · Florian Weimer

Is it possible that getdents returns a zero inode number for a name which actually has a file (or directory, device node etc.) behind it?

The d_ino == 0 value is valid to return and means that the filename was unlinked from the directory but the entry was not actually removed.

Until recently, didn't the filesystems which use get_next_ino() accept inode 0 as a legit inode number?

commit 2adc376c551943a07170cbe70f43e6d6065f8906

Author: Carlos Maiolino <[email protected]>
Date:   Thu Jun 25 12:25:58 2015 -0300

vfs: avoid creation of inode number 0 in get_next_ino

-Eric

#6 lore raw
Quoted 5 lines · Eric Sandeen
Quoted 2 lines · Andreas Dilger
Quoted 1 line · Florian Weimer
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Is it possible that getdents returns a zero inode number for a name which actually has a file (or directory, device node etc.) behind it?

The d_ino == 0 value is valid to return and means that the filename was unlinked from the directory but the entry was not actually removed.

Until recently, didn't the filesystems which use get_next_ino() accept inode 0 as a legit inode number?

Thanks Eric for unearthing this change.

Quoted 5 lines

commit 2adc376c551943a07170cbe70f43e6d6065f8906

Author: Carlos Maiolino <[email protected]>
Date:   Thu Jun 25 12:25:58 2015 -0300

vfs: avoid creation of inode number 0 in get_next_ino

As far as I understand it, this does nothing to fix up existing files with inode 0, so I filed bugs to fix this in glibc as well:

https://sourceware.org/bugzilla/show_bug.cgi?id=19970
https://sourceware.org/bugzilla/show_bug.cgi?id=19971

We had no idea on the glibc side that this wasn't just a theoretical issue.

Thanks,
Florian