From dec7db49602df75119f6b57d32f1eb58e0040abc Mon Sep 17 00:00:00 2001 From: Jiajie Chen Date: Tue, 24 Jan 2023 00:36:59 +0800 Subject: [PATCH] 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 --- 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 48d15ce6ab25..a49070b6060d 100644 --- a/sys/kern/vfs_vnops.c +++ b/sys/kern/vfs_vnops.c @@ -2792,6 +2792,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 edbe6a8655ff..42799a70fbfc 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. */