Re: Hard link count reported by "ls -l" is wrong

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, Feb 25, 2020 at 09:59:49AM -0500, Franklin, Jason wrote:
> When using "ls -l" to view a detailed listing in the current directory,
> I get output similar to the following:

> drwxrwx--- 1 jfrankli jfrankli  38 Feb 25 09:54 Desktop/
> drwxrwx--- 1 jfrankli jfrankli  36 Jan 24 10:37 Documents/

> Notice that these are all directories with a hard link count of "1".
> 
> I have always seen directories possessing a hard link count of "2" or
> greater.  This is because the directory itself is a hard link, and it
> also contains the "." entry (the second hard link).

That's an implementation detail of filesystems on truly ancient Unices
(from the times before my birth -- and, with bearing kids at age 14 being
semi-popular these days, I could have been a great-grandfather).  And there,
indeed, subdirectories and ".." (not ".") had been implemented as actual
hard links.  Heck, you could even unlink() them with nasty results for the
filesystem -- remnants for such unlinking (but not the lore) can still be
seen in "perldoc -f unlink" and "man 2 unlink".

> Any immediate child directory of a directory also adds +1 to the hard
> link count on other file systems.  This is because each child directory
> contains the ".." hard link pointing to its parent directory.
> 
> Why does this not happen with btrfs?

Most filesystems emulate that behaviour.  For btrfs, such emulation would
have a bad effect for performance (the kernel would need to either count
subdirectories every time, or store and keep updated such count), thus
this value has been skipped.

And POSIX allows such behaviour.

Heck, if not for POSIX declaring some fields (st_nlink, st_rdev, st_size,
st_blksize and st_blocks) as not required to be meaningful, ext4 would be in
a violation of the spec as it gives st_size of an empty directory as
non-zero. :)


Meow!
-- 
⢀⣴⠾⠻⢶⣦⠀ We domesticated dogs 36000 years ago; together we chased
⣾⠁⢰⠒⠀⣿⡁ animals, hung out and licked or scratched our private parts.
⢿⡄⠘⠷⠚⠋⠀ Cats domesticated us 9500 years ago, and immediately we got
⠈⠳⣄⠀⠀⠀⠀ agriculture, towns then cities.     -- whitroth on /.



[Index of Archives]     [Linux Filesystem Development]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux