What does 'stale file handle' in Linux mean?
Say I have a terminal open, and through that terminal I have cd
'ed to some directory. Through another terminal, I delete that directory and restore it back from an identical backup. When I try to vim
a file from the first terminal, in the same directory, why do I get an error about a stale file handle? What does it mean? (On a side note, I have found that it is possible to bypass this issue through cd $(pwd)
.)
When the directory is deleted, the inode for that directory (and the inodes for its contents) are recycled. The pointer your shell has to that directory's inode (and its contents's inodes) are now no longer valid. When the directory is restored from backup, the old inodes are not (necessarily) reused; the directory and its contents are stored on random inodes. The only thing that stays the same is that the parent directory reuses the same name for the restored directory (because you told it to).
Now if you attempt to access the contents of the directory that your original shell is still pointing to, it communicates that request to the file system as a request for the original inode, which has since been recycled (and may even be in use for something entirely different now). So you get a stale file handle
message because you asked for some nonexistent data.
When you perform a cd
operation, the shell reevaluates the inode location of whatever destination you give it. Now that your shell knows the new inode for the directory (and the new inodes for its contents), future requests for its contents will be valid.