git: ba1fc13882d1 - stable/13 - Add kf_file_nlink field to kf_file and populate it

From: Mateusz Guzik <mjg_at_FreeBSD.org>
Date: Sun, 29 Jan 2023 14:38:24 UTC
The branch stable/13 has been updated by mjg:

URL: https://cgit.FreeBSD.org/src/commit/?id=ba1fc13882d126a986f629b095ed6bab7378a8f4

commit ba1fc13882d126a986f629b095ed6bab7378a8f4
Author:     Jiajie Chen <c@jia.je>
AuthorDate: 2023-01-23 16:36:59 +0000
Commit:     Mateusz Guzik <mjg@FreeBSD.org>
CommitDate: 2023-01-29 14:37:08 +0000

    Add kf_file_nlink field to kf_file and populate it
    
    This will allow user-space programs (e.g. lsof) to locate deleted files
    whose nlink equals zero. Prior to this commit, programs has to use
    stat(kf_path) to get nlink, but that will fail if the file is deleted.
    
    [mjg: s/fail/file in the commit message]
    
    Reviewed by:    mjg
    Differential Revision:  https://reviews.freebsd.org/D38169
    
    (cherry picked from commit dec7db49602df75119f6b57d32f1eb58e0040abc)
---
 sys/kern/vfs_vnops.c | 1 +
 sys/sys/user.h       | 4 +++-
 2 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/sys/kern/vfs_vnops.c b/sys/kern/vfs_vnops.c
index a54d2c463dc7..f4274ee38689 100644
--- a/sys/kern/vfs_vnops.c
+++ b/sys/kern/vfs_vnops.c
@@ -2774,6 +2774,7 @@ vn_fill_kinfo_vnode(struct vnode *vp, struct kinfo_file *kif)
 	kif->kf_un.kf_file.kf_file_rdev = va.va_rdev;
 	kif->kf_un.kf_file.kf_file_rdev_freebsd11 =
 	    kif->kf_un.kf_file.kf_file_rdev; /* truncate */
+	kif->kf_un.kf_file.kf_file_nlink = va.va_nlink;
 	return (0);
 }
 
diff --git a/sys/sys/user.h b/sys/sys/user.h
index 433e44c0f19e..6309c708be95 100644
--- a/sys/sys/user.h
+++ b/sys/sys/user.h
@@ -389,7 +389,9 @@ struct kinfo_file {
 				int		kf_file_type;
 				/* Space for future use */
 				int		kf_spareint[3];
-				uint64_t	kf_spareint64[30];
+				uint64_t	kf_spareint64[29];
+				/* Number of references to file. */
+				uint64_t	kf_file_nlink;
 				/* Vnode filesystem id. */
 				uint64_t	kf_file_fsid;
 				/* File device. */