#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: kern_fileassoc.c,v 1.40 2026/01/04 01:33:39 riastradh Exp $");
#include "opt_fileassoc.h"
#include <sys/param.h>
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/fileassoc.h>
#include <sys/hash.h>
#include <sys/kmem.h>
#include <sys/mount.h>
#include <sys/mutex.h>
#include <sys/once.h>
#include <sys/queue.h>
#include <sys/sdt.h>
#include <sys/specificdata.h>
#include <sys/vnode.h>
#include <sys/xcall.h>
#define FILEASSOC_INITIAL_TABLESIZE 128
static specificdata_domain_t fileassoc_domain = NULL;
static specificdata_key_t fileassoc_mountspecific_key;
static ONCE_DECL(control);
struct fileassoc {
LIST_ENTRY(fileassoc) assoc_list;
const char *assoc_name;
fileassoc_cleanup_cb_t assoc_cleanup_cb;
specificdata_key_t assoc_key;
};
static LIST_HEAD(, fileassoc) fileassoc_list;
struct fileassoc_file {
fhandle_t *faf_handle;
specificdata_reference faf_data;
u_int faf_nassocs;
LIST_ENTRY(fileassoc_file) faf_list;
};
LIST_HEAD(fileassoc_hash_entry, fileassoc_file);
struct fileassoc_table {
struct fileassoc_hash_entry *tbl_hash;
u_long tbl_mask;
size_t tbl_nslots;
size_t tbl_nused;
specificdata_reference tbl_data;
};
#define FILEASSOC_HASH(tbl, handle) \
(hash32_buf((handle), FHANDLE_SIZE(handle), HASH32_BUF_INIT) \
& ((tbl)->tbl_mask))
static struct {
kmutex_t lock;
uint64_t nassocs;
volatile bool inuse;
} fileassoc_global __cacheline_aligned;
static void
fileassoc_incuse(void)
{
mutex_enter(&fileassoc_global.lock);
if (fileassoc_global.nassocs++ == 0) {
KASSERT(!fileassoc_global.inuse);
atomic_store_relaxed(&fileassoc_global.inuse, true);
xc_barrier(0);
}
mutex_exit(&fileassoc_global.lock);
}
static void
fileassoc_decuse(void)
{
mutex_enter(&fileassoc_global.lock);
KASSERT(fileassoc_global.nassocs > 0);
KASSERT(fileassoc_global.inuse);
if (--fileassoc_global.nassocs == 0)
atomic_store_relaxed(&fileassoc_global.inuse, false);
mutex_exit(&fileassoc_global.lock);
}
static bool
fileassoc_inuse(void)
{
return __predict_false(atomic_load_relaxed(&fileassoc_global.inuse));
}
static void *
file_getdata(struct fileassoc_file *faf, const struct fileassoc *assoc)
{
return specificdata_getspecific(fileassoc_domain, &faf->faf_data,
assoc->assoc_key);
}
static void
file_setdata(struct fileassoc_file *faf, const struct fileassoc *assoc,
void *data)
{
specificdata_setspecific(fileassoc_domain, &faf->faf_data,
assoc->assoc_key, data);
}
static void
file_cleanup(struct fileassoc_file *faf, const struct fileassoc *assoc)
{
fileassoc_cleanup_cb_t cb;
void *data;
cb = assoc->assoc_cleanup_cb;
if (cb == NULL) {
return;
}
data = file_getdata(faf, assoc);
(*cb)(data);
}
static void
file_free(struct fileassoc_file *faf)
{
struct fileassoc *assoc;
LIST_REMOVE(faf, faf_list);
LIST_FOREACH(assoc, &fileassoc_list, assoc_list) {
file_cleanup(faf, assoc);
fileassoc_decuse();
}
vfs_composefh_free(faf->faf_handle);
specificdata_fini(fileassoc_domain, &faf->faf_data);
kmem_free(faf, sizeof(*faf));
}
static void
table_dtor(void *v)
{
struct fileassoc_table *tbl = v;
u_long i;
for (i = 0; i < tbl->tbl_nslots; i++) {
struct fileassoc_file *faf;
while ((faf = LIST_FIRST(&tbl->tbl_hash[i])) != NULL) {
file_free(faf);
}
}
hashdone(tbl->tbl_hash, HASH_LIST, tbl->tbl_mask);
specificdata_fini(fileassoc_domain, &tbl->tbl_data);
kmem_free(tbl, sizeof(*tbl));
}
static int
fileassoc_init(void)
{
int error;
error = mount_specific_key_create(&fileassoc_mountspecific_key,
table_dtor);
if (error) {
return error;
}
fileassoc_domain = specificdata_domain_create();
mutex_init(&fileassoc_global.lock, MUTEX_DEFAULT, IPL_NONE);
return 0;
}
int
fileassoc_register(const char *name, fileassoc_cleanup_cb_t cleanup_cb,
fileassoc_t *result)
{
int error;
specificdata_key_t key;
struct fileassoc *assoc;
error = RUN_ONCE(&control, fileassoc_init);
if (error) {
return error;
}
error = specificdata_key_create(fileassoc_domain, &key, NULL);
if (error) {
return error;
}
assoc = kmem_alloc(sizeof(*assoc), KM_SLEEP);
assoc->assoc_name = name;
assoc->assoc_cleanup_cb = cleanup_cb;
assoc->assoc_key = key;
LIST_INSERT_HEAD(&fileassoc_list, assoc, assoc_list);
*result = assoc;
return 0;
}
int
fileassoc_deregister(fileassoc_t assoc)
{
LIST_REMOVE(assoc, assoc_list);
specificdata_key_delete(fileassoc_domain, assoc->assoc_key);
kmem_free(assoc, sizeof(*assoc));
return 0;
}
static struct fileassoc_table *
fileassoc_table_lookup(struct mount *mp)
{
int error;
if (!fileassoc_inuse())
return NULL;
error = RUN_ONCE(&control, fileassoc_init);
if (error) {
return NULL;
}
return mount_getspecific(mp, fileassoc_mountspecific_key);
}
static struct fileassoc_file *
fileassoc_file_lookup(struct vnode *vp, fhandle_t *hint)
{
struct fileassoc_table *tbl;
struct fileassoc_hash_entry *hash_entry;
struct fileassoc_file *faf;
size_t indx;
fhandle_t *th;
int error;
tbl = fileassoc_table_lookup(vp->v_mount);
if (tbl == NULL) {
return NULL;
}
if (hint == NULL) {
error = vfs_composefh_alloc(vp, &th);
if (error)
return (NULL);
} else {
th = hint;
}
indx = FILEASSOC_HASH(tbl, th);
hash_entry = &(tbl->tbl_hash[indx]);
LIST_FOREACH(faf, hash_entry, faf_list) {
if (((FHANDLE_FILEID(faf->faf_handle)->fid_len ==
FHANDLE_FILEID(th)->fid_len)) &&
(memcmp(FHANDLE_FILEID(faf->faf_handle), FHANDLE_FILEID(th),
(FHANDLE_FILEID(th))->fid_len) == 0)) {
break;
}
}
if (hint == NULL)
vfs_composefh_free(th);
return faf;
}
void *
fileassoc_lookup(struct vnode *vp, fileassoc_t assoc)
{
struct fileassoc_file *faf;
faf = fileassoc_file_lookup(vp, NULL);
if (faf == NULL)
return (NULL);
return file_getdata(faf, assoc);
}
static struct fileassoc_table *
fileassoc_table_resize(struct fileassoc_table *tbl)
{
struct fileassoc_table *newtbl;
u_long i;
newtbl = kmem_zalloc(sizeof(*newtbl), KM_SLEEP);
newtbl->tbl_nslots = (tbl->tbl_nslots * 2);
if (newtbl->tbl_nslots < tbl->tbl_nslots)
newtbl->tbl_nslots = tbl->tbl_nslots;
newtbl->tbl_hash = hashinit(newtbl->tbl_nslots, HASH_LIST,
true, &newtbl->tbl_mask);
newtbl->tbl_nused = 0;
specificdata_init(fileassoc_domain, &newtbl->tbl_data);
for (i = 0; i < tbl->tbl_nslots; i++) {
struct fileassoc_file *faf;
while ((faf = LIST_FIRST(&tbl->tbl_hash[i])) != NULL) {
struct fileassoc_hash_entry *hash_entry;
size_t indx;
LIST_REMOVE(faf, faf_list);
indx = FILEASSOC_HASH(newtbl, faf->faf_handle);
hash_entry = &(newtbl->tbl_hash[indx]);
LIST_INSERT_HEAD(hash_entry, faf, faf_list);
newtbl->tbl_nused++;
}
}
if (tbl->tbl_nused != newtbl->tbl_nused)
panic("fileassoc_table_resize: inconsistency detected! "
"needed %zu entries, got %zu", tbl->tbl_nused,
newtbl->tbl_nused);
hashdone(tbl->tbl_hash, HASH_LIST, tbl->tbl_mask);
specificdata_fini(fileassoc_domain, &tbl->tbl_data);
kmem_free(tbl, sizeof(*tbl));
return (newtbl);
}
static struct fileassoc_table *
fileassoc_table_add(struct mount *mp)
{
struct fileassoc_table *tbl;
tbl = fileassoc_table_lookup(mp);
if (tbl != NULL)
return (tbl);
tbl = kmem_zalloc(sizeof(*tbl), KM_SLEEP);
tbl->tbl_nslots = FILEASSOC_INITIAL_TABLESIZE;
tbl->tbl_hash = hashinit(tbl->tbl_nslots, HASH_LIST, true,
&tbl->tbl_mask);
tbl->tbl_nused = 0;
specificdata_init(fileassoc_domain, &tbl->tbl_data);
mount_setspecific(mp, fileassoc_mountspecific_key, tbl);
return (tbl);
}
int
fileassoc_table_delete(struct mount *mp)
{
struct fileassoc_table *tbl;
tbl = fileassoc_table_lookup(mp);
if (tbl == NULL)
return SET_ERROR(EEXIST);
mount_setspecific(mp, fileassoc_mountspecific_key, NULL);
table_dtor(tbl);
return 0;
}
int
fileassoc_table_run(struct mount *mp, fileassoc_t assoc, fileassoc_cb_t cb,
void *cookie)
{
struct fileassoc_table *tbl;
u_long i;
tbl = fileassoc_table_lookup(mp);
if (tbl == NULL)
return SET_ERROR(EEXIST);
for (i = 0; i < tbl->tbl_nslots; i++) {
struct fileassoc_file *faf;
LIST_FOREACH(faf, &tbl->tbl_hash[i], faf_list) {
void *data;
data = file_getdata(faf, assoc);
if (data != NULL)
cb(data, cookie);
}
}
return 0;
}
int
fileassoc_table_clear(struct mount *mp, fileassoc_t assoc)
{
struct fileassoc_table *tbl;
u_long i;
tbl = fileassoc_table_lookup(mp);
if (tbl == NULL)
return SET_ERROR(EEXIST);
for (i = 0; i < tbl->tbl_nslots; i++) {
struct fileassoc_file *faf;
LIST_FOREACH(faf, &tbl->tbl_hash[i], faf_list) {
file_cleanup(faf, assoc);
file_setdata(faf, assoc, NULL);
fileassoc_decuse();
}
}
return 0;
}
static struct fileassoc_file *
fileassoc_file_add(struct vnode *vp, fhandle_t *hint)
{
struct fileassoc_table *tbl;
struct fileassoc_hash_entry *hash_entry;
struct fileassoc_file *faf;
size_t indx;
fhandle_t *th;
int error;
if (hint == NULL) {
error = vfs_composefh_alloc(vp, &th);
if (error)
return NULL;
} else
th = hint;
faf = fileassoc_file_lookup(vp, th);
if (faf != NULL) {
if (hint == NULL)
vfs_composefh_free(th);
return faf;
}
tbl = fileassoc_table_lookup(vp->v_mount);
if (tbl == NULL) {
tbl = fileassoc_table_add(vp->v_mount);
}
indx = FILEASSOC_HASH(tbl, th);
hash_entry = &(tbl->tbl_hash[indx]);
faf = kmem_zalloc(sizeof(*faf), KM_SLEEP);
faf->faf_handle = th;
specificdata_init(fileassoc_domain, &faf->faf_data);
LIST_INSERT_HEAD(hash_entry, faf, faf_list);
if (++(tbl->tbl_nused) == tbl->tbl_nslots) {
struct fileassoc_table *newtbl;
newtbl = fileassoc_table_resize(tbl);
mount_setspecific(vp->v_mount, fileassoc_mountspecific_key,
newtbl);
}
return faf;
}
int
fileassoc_file_delete(struct vnode *vp)
{
struct fileassoc_table *tbl;
struct fileassoc_file *faf;
if (!fileassoc_inuse())
return SET_ERROR(ENOENT);
KERNEL_LOCK(1, NULL);
faf = fileassoc_file_lookup(vp, NULL);
if (faf == NULL) {
KERNEL_UNLOCK_ONE(NULL);
return SET_ERROR(ENOENT);
}
file_free(faf);
tbl = fileassoc_table_lookup(vp->v_mount);
KASSERT(tbl != NULL);
--(tbl->tbl_nused);
KERNEL_UNLOCK_ONE(NULL);
return 0;
}
int
fileassoc_add(struct vnode *vp, fileassoc_t assoc, void *data)
{
struct fileassoc_file *faf;
void *olddata;
faf = fileassoc_file_lookup(vp, NULL);
if (faf == NULL) {
faf = fileassoc_file_add(vp, NULL);
if (faf == NULL)
return SET_ERROR(ENOTDIR);
}
olddata = file_getdata(faf, assoc);
if (olddata != NULL)
return SET_ERROR(EEXIST);
fileassoc_incuse();
file_setdata(faf, assoc, data);
faf->faf_nassocs++;
return 0;
}
int
fileassoc_clear(struct vnode *vp, fileassoc_t assoc)
{
struct fileassoc_file *faf;
faf = fileassoc_file_lookup(vp, NULL);
if (faf == NULL)
return SET_ERROR(ENOENT);
file_cleanup(faf, assoc);
file_setdata(faf, assoc, NULL);
--(faf->faf_nassocs);
fileassoc_decuse();
return 0;
}