Open file descriptor reference count implementation in driver

jd1008 jd1008 at gmail.com
Wed Nov 5 22:13:40 UTC 2014


Hi Sibananda,
As you can see below, si_refcount  and si_usecount are used by the
kernel in kern_conf.c,  and the generic FS vnode layer, and the device 
FS vnode layer.

./kern/kern_conf.c: dev->si_refcount++;
./kern/kern_conf.c: dev->si_refcount++;
./kern/kern_conf.c: dev->si_refcount--;
./kern/kern_conf.c: KASSERT(dev->si_refcount >= 0,
./kern/kern_conf.c: if (dev->si_usecount == 0 &&
./kern/kern_conf.c: if (dev->si_devsw == NULL && dev->si_refcount == 0) {
./kern/kern_conf.c: dev->si_refcount++;        /* Avoid race with 
dev_rel() */
./kern/kern_conf.c: dev->si_refcount--;        /* Avoid race with 
dev_rel() */
./kern/kern_conf.c: if (dev->si_refcount > 0) {
./kern/kern_conf.c:     dev->si_name, dev->si_refcount, dev->si_usecount,

The FS Vnode layer:

./kern/vfs_bio.c:   KASSERT(dev->si_refcount > 0,
./kern/vfs_subr.c:          vp->v_rdev->si_usecount++;
./kern/vfs_subr.c:          vp->v_rdev->si_usecount++;
./kern/vfs_subr.c:          vp->v_rdev->si_usecount--;
./kern/vfs_subr.c:          vp->v_rdev->si_usecount--;
./kern/vfs_subr.c:  count = vp->v_rdev->si_usecount;
./kern/vfs_subr.c:  count = dev->si_usecount;
./sys/conf.h:       int             si_refcount;
./sys/conf.h:       u_long          si_usecount;

The device FS vnode layer:

./fs/devfs/devfs_vnops.c:   KASSERT((*devp)->si_refcount > 0,
./fs/devfs/devfs_vnops.c:           dev->si_usecount += vp->v_usecount;
./fs/devfs/devfs_vnops.c:   KASSERT(dev->si_refcount > 0,
./fs/devfs/devfs_vnops.c:   dev->si_usecount -= vp->v_usecount;


So, it would seem like YOUR DRIVER has no business checking/modifying 
these variables.
But your device driver's open and close functions will be  called by the 
upper vnode
operations (functions) and they take care of these variables (among others).
When your driver's close function is called as a final act of the vnode 
layer when
the counts go to 0.
Whay your XX_close() function will do depends a lot on what your driver is
supposed to achieve. Mostlly, release driver locks and memory allocated for
the very first open.


More information about the freebsd-questions mailing list