Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to set for st_nlink as attribute when implementing FUSE?

Tags:

linux

fs

fuse

When implementing FUSE library, attributes for the directories should be defined by getattr() callback.

But what to set for this st_nlink?

In this example, they set it to 2, but I don't know the exact reason.

https://github.com/lxc/lxcfs/blob/2ed933d959f54bee70985e260660a468ef9d21a1/src/proc_fuse.c#L87

What is st_nlink for? And should it be 1 or 2 as default for my user-space FUSE directories?

For example, if I define a directory called "/dev", and there are ".", ".." and "snd" sub-entries in it, should the st_nlink for "/dev" be 2? Because the hard link (entry) are /dev/snd and /dev/snd/./

like image 912
leoleohu Avatar asked Oct 18 '25 16:10

leoleohu


1 Answers

Interesting, I did read the following.

st_nlink is the number of hard links to the file. This count keeps track of how many directories have entries for this file. If the count is ever decremented to zero, then the file itself is discarded as soon as no process still holds it open. Symbolic links are not counted in the total.

https://www.gnu.org/software/libc/manual/html_node/Attribute-Meanings.html

I assume the "file" is being used twice, and there for it's st_nlink is being set to two.

I also assume it is used for a reface counting design https://en.wikipedia.org/wiki/Reference_counting

like image 114
John b Avatar answered Oct 20 '25 08:10

John b