Changeset View
Changeset View
Standalone View
Standalone View
sys/fs/nullfs/null_subr.c
| Show All 34 Lines | |||||||||||
| #include <sys/param.h> | #include <sys/param.h> | ||||||||||
| #include <sys/systm.h> | #include <sys/systm.h> | ||||||||||
| #include <sys/kernel.h> | #include <sys/kernel.h> | ||||||||||
| #include <sys/lock.h> | #include <sys/lock.h> | ||||||||||
| #include <sys/rwlock.h> | #include <sys/rwlock.h> | ||||||||||
| #include <sys/malloc.h> | #include <sys/malloc.h> | ||||||||||
| #include <sys/mount.h> | #include <sys/mount.h> | ||||||||||
| #include <sys/proc.h> | #include <sys/proc.h> | ||||||||||
| #include <sys/vnode.h> | #include <sys/vnode.h> | ||||||||||
| #include <sys/smr.h> | |||||||||||
kib: Should be ordered alphabetically. | |||||||||||
Not Done Inline ActionsPlease fix this. kib: Please fix this. | |||||||||||
Not Done Inline Actions
olce: | |||||||||||
| #include <fs/nullfs/null.h> | #include <fs/nullfs/null.h> | ||||||||||
| #include <vm/uma.h> | |||||||||||
| VFS_SMR_DECLARE; | |||||||||||
| /* | /* | ||||||||||
| * Null layer cache: | * Null layer cache: | ||||||||||
| * Each cache entry holds a reference to the lower vnode | * Each cache entry holds a reference to the lower vnode | ||||||||||
| * along with a pointer to the alias vnode. When an | * along with a pointer to the alias vnode. When an | ||||||||||
| * entry is added the lower vnode is VREF'd. When the | * entry is added the lower vnode is VREF'd. When the | ||||||||||
| * alias is removed the lower vnode is vrele'd. | * alias is removed the lower vnode is vrele'd. | ||||||||||
| */ | */ | ||||||||||
| #define NULL_NHASH(vp) (&null_node_hashtbl[vfs_hash_index(vp) & null_hash_mask]) | #define NULL_NHASH(vp) (&null_node_hashtbl[vfs_hash_index(vp) & null_hash_mask]) | ||||||||||
| static LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl; | static CK_LIST_HEAD(null_node_hashhead, null_node) *null_node_hashtbl; | ||||||||||
| static struct rwlock null_hash_lock; | static struct rwlock null_hash_lock; | ||||||||||
| static u_long null_hash_mask; | static u_long null_hash_mask; | ||||||||||
| static MALLOC_DEFINE(M_NULLFSHASH, "nullfs_hash", "NULLFS hash table"); | static MALLOC_DEFINE(M_NULLFSHASH, "nullfs_hash", "NULLFS hash table"); | ||||||||||
| MALLOC_DEFINE(M_NULLFSNODE, "nullfs_node", "NULLFS vnode private part"); | uma_zone_t __read_mostly null_node_zone; | ||||||||||
| static void null_hashins(struct mount *, struct null_node *); | static void null_hashins(struct mount *, struct null_node *); | ||||||||||
| /* | /* | ||||||||||
| * Initialise cache headers | * Initialise cache headers | ||||||||||
| */ | */ | ||||||||||
| int | int | ||||||||||
| nullfs_init(struct vfsconf *vfsp) | nullfs_init(struct vfsconf *vfsp) | ||||||||||
| { | { | ||||||||||
| null_node_hashtbl = hashinit(desiredvnodes, M_NULLFSHASH, | null_node_hashtbl = hashinit(desiredvnodes, M_NULLFSHASH, | ||||||||||
| &null_hash_mask); | &null_hash_mask); | ||||||||||
| rw_init(&null_hash_lock, "nullhs"); | rw_init(&null_hash_lock, "nullhs"); | ||||||||||
| null_node_zone = uma_zcreate("nullfs node", sizeof(struct null_node), | |||||||||||
| NULL, NULL, NULL, NULL, 0, UMA_ZONE_ZINIT); | |||||||||||
| VFS_SMR_ZONE_SET(null_node_zone); | |||||||||||
| return (0); | return (0); | ||||||||||
| } | } | ||||||||||
| int | int | ||||||||||
| nullfs_uninit(struct vfsconf *vfsp) | nullfs_uninit(struct vfsconf *vfsp) | ||||||||||
| { | { | ||||||||||
| uma_zdestroy(null_node_zone); | |||||||||||
| rw_destroy(&null_hash_lock); | rw_destroy(&null_hash_lock); | ||||||||||
| hashdestroy(null_node_hashtbl, M_NULLFSHASH, null_hash_mask); | hashdestroy(null_node_hashtbl, M_NULLFSHASH, null_hash_mask); | ||||||||||
| return (0); | return (0); | ||||||||||
| } | } | ||||||||||
| /* | /* | ||||||||||
| * Return a VREF'ed alias for lower vnode if already exists, else 0. | * Return a VREF'ed alias for lower vnode if already exists, else 0. | ||||||||||
| * Lower vnode should be locked on entry and will be left locked on exit. | * Lower vnode should be locked on entry and will be left locked on exit. | ||||||||||
| */ | */ | ||||||||||
| static struct vnode * | static struct vnode * | ||||||||||
| null_hashget_locked(struct mount *mp, struct vnode *lowervp) | null_hashget_locked(struct mount *mp, struct vnode *lowervp) | ||||||||||
| { | { | ||||||||||
| struct null_node_hashhead *hd; | struct null_node_hashhead *hd; | ||||||||||
| struct null_node *a; | struct null_node *a; | ||||||||||
| struct vnode *vp; | struct vnode *vp; | ||||||||||
| ASSERT_VOP_LOCKED(lowervp, "null_hashget"); | ASSERT_VOP_LOCKED(lowervp, __func__); | ||||||||||
| rw_assert(&null_hash_lock, RA_LOCKED); | rw_assert(&null_hash_lock, RA_LOCKED); | ||||||||||
| /* | /* | ||||||||||
| * Find hash base, and then search the (two-way) linked | * Find hash base, and then search the (two-way) linked | ||||||||||
| * list looking for a null_node structure which is referencing | * list looking for a null_node structure which is referencing | ||||||||||
| * the lower vnode. If found, the increment the null_node | * the lower vnode. If found, the increment the null_node | ||||||||||
| * reference count (but NOT the lower vnode's VREF counter). | * reference count (but NOT the lower vnode's VREF counter). | ||||||||||
| */ | */ | ||||||||||
| hd = NULL_NHASH(lowervp); | hd = NULL_NHASH(lowervp); | ||||||||||
| LIST_FOREACH(a, hd, null_hash) { | CK_LIST_FOREACH(a, hd, null_hash) { | ||||||||||
| if (a->null_lowervp == lowervp && NULLTOV(a)->v_mount == mp) { | if (a->null_lowervp != lowervp) | ||||||||||
| continue; | |||||||||||
| /* | /* | ||||||||||
| * Since we have the lower node locked the nullfs | * Since we have the lower node locked the nullfs | ||||||||||
| * node can not be in the process of recycling. If | * node can not be in the process of recycling. If | ||||||||||
| * it had been recycled before we grabed the lower | * it had been recycled before we grabed the lower | ||||||||||
| * lock it would not have been found on the hash. | * lock it would not have been found on the hash. | ||||||||||
| */ | */ | ||||||||||
| vp = NULLTOV(a); | vp = NULLTOV(a); | ||||||||||
| VNPASS(!VN_IS_DOOMED(vp), vp); | |||||||||||
| if (vp->v_mount != mp) | |||||||||||
| continue; | |||||||||||
| vref(vp); | vref(vp); | ||||||||||
| return (vp); | return (vp); | ||||||||||
| } | } | ||||||||||
| } | |||||||||||
| return (NULL); | return (NULL); | ||||||||||
| } | } | ||||||||||
| struct vnode * | struct vnode * | ||||||||||
| null_hashget(struct mount *mp, struct vnode *lowervp) | null_hashget(struct mount *mp, struct vnode *lowervp) | ||||||||||
| { | { | ||||||||||
| struct null_node_hashhead *hd; | struct null_node_hashhead *hd; | ||||||||||
| struct null_node *a; | |||||||||||
| struct vnode *vp; | struct vnode *vp; | ||||||||||
| enum vgetstate vs; | |||||||||||
| hd = NULL_NHASH(lowervp); | ASSERT_VOP_LOCKED(lowervp, "null_hashget"); | ||||||||||
| if (LIST_EMPTY(hd)) | rw_assert(&null_hash_lock, RA_UNLOCKED); | ||||||||||
Not Done Inline Actions
olce: | |||||||||||
| return (NULL); | |||||||||||
| rw_rlock(&null_hash_lock); | |||||||||||
| vp = null_hashget_locked(mp, lowervp); | |||||||||||
| rw_runlock(&null_hash_lock); | |||||||||||
| vfs_smr_enter(); | |||||||||||
| hd = NULL_NHASH(lowervp); | |||||||||||
| CK_LIST_FOREACH(a, hd, null_hash) { | |||||||||||
| if (a->null_lowervp != lowervp) | |||||||||||
| continue; | |||||||||||
| /* | |||||||||||
| * See null_hashget_locked as to why the nullfs vnode can't be | |||||||||||
| * doomed here. | |||||||||||
Not Done Inline ActionsThis should be also asserted. Both there and in null_hashget_locked(). Check for the v_mount below can be considered to be part of the assert, but it is really strange: IMO we should explicitly check for doomed, and the later check for v_mount != mp actually consumes the check for v_mount != NULL. kib: This should be also asserted. Both there and in null_hashget_locked().
Check for the v_mount… | |||||||||||
| */ | |||||||||||
| vp = NULLTOV(a); | |||||||||||
| VNPASS(!VN_IS_DOOMED(vp), vp); | |||||||||||
| if (vp->v_mount != mp) | |||||||||||
| continue; | |||||||||||
| vs = vget_prep_smr(vp); | |||||||||||
| vfs_smr_exit(); | |||||||||||
| VNPASS(vs != VGET_NONE, vp); | |||||||||||
| vget_finish_ref(vp, vs); | |||||||||||
| return (vp); | return (vp); | ||||||||||
| } | } | ||||||||||
| vfs_smr_exit(); | |||||||||||
| return (NULL); | |||||||||||
| } | |||||||||||
| static void | static void | ||||||||||
| null_hashins(struct mount *mp, struct null_node *xp) | null_hashins(struct mount *mp, struct null_node *xp) | ||||||||||
| { | { | ||||||||||
| struct null_node_hashhead *hd; | struct null_node_hashhead *hd; | ||||||||||
| #ifdef INVARIANTS | #ifdef INVARIANTS | ||||||||||
| struct null_node *oxp; | struct null_node *oxp; | ||||||||||
| #endif | #endif | ||||||||||
| rw_assert(&null_hash_lock, RA_WLOCKED); | rw_assert(&null_hash_lock, RA_WLOCKED); | ||||||||||
| hd = NULL_NHASH(xp->null_lowervp); | hd = NULL_NHASH(xp->null_lowervp); | ||||||||||
| #ifdef INVARIANTS | #ifdef INVARIANTS | ||||||||||
| LIST_FOREACH(oxp, hd, null_hash) { | CK_LIST_FOREACH(oxp, hd, null_hash) { | ||||||||||
| if (oxp->null_lowervp == xp->null_lowervp && | if (oxp->null_lowervp == xp->null_lowervp && | ||||||||||
| NULLTOV(oxp)->v_mount == mp) { | NULLTOV(oxp)->v_mount == mp) { | ||||||||||
| VNASSERT(0, NULLTOV(oxp), | VNASSERT(0, NULLTOV(oxp), | ||||||||||
| ("vnode already in hash")); | ("vnode already in hash")); | ||||||||||
| } | } | ||||||||||
| } | } | ||||||||||
| #endif | #endif | ||||||||||
| LIST_INSERT_HEAD(hd, xp, null_hash); | CK_LIST_INSERT_HEAD(hd, xp, null_hash); | ||||||||||
| } | } | ||||||||||
| static void | static void | ||||||||||
| null_destroy_proto(struct vnode *vp, void *xp) | null_destroy_proto(struct vnode *vp, void *xp) | ||||||||||
| { | { | ||||||||||
| lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL); | lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL); | ||||||||||
| VI_LOCK(vp); | VI_LOCK(vp); | ||||||||||
| vp->v_data = NULL; | vp->v_data = NULL; | ||||||||||
| vp->v_vnlock = &vp->v_lock; | vp->v_vnlock = &vp->v_lock; | ||||||||||
| vp->v_op = &dead_vnodeops; | vp->v_op = &dead_vnodeops; | ||||||||||
| VI_UNLOCK(vp); | VI_UNLOCK(vp); | ||||||||||
| vgone(vp); | vgone(vp); | ||||||||||
| vput(vp); | vput(vp); | ||||||||||
| free(xp, M_NULLFSNODE); | uma_zfree_smr(null_node_zone, xp); | ||||||||||
| } | } | ||||||||||
| /* | /* | ||||||||||
| * Make a new or get existing nullfs node. | * Make a new or get existing nullfs node. | ||||||||||
| * Vp is the alias vnode, lowervp is the lower vnode. | * Vp is the alias vnode, lowervp is the lower vnode. | ||||||||||
| * | * | ||||||||||
| * The lowervp assumed to be locked and having "spare" reference. This routine | * The lowervp assumed to be locked and having "spare" reference. This routine | ||||||||||
| * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers" | * vrele lowervp if nullfs node was taken from hash. Otherwise it "transfers" | ||||||||||
| Show All 17 Lines | null_nodeget(struct mount *mp, struct vnode *lowervp, struct vnode **vpp) | ||||||||||
| } | } | ||||||||||
| /* | /* | ||||||||||
| * We do not serialize vnode creation, instead we will check for | * We do not serialize vnode creation, instead we will check for | ||||||||||
| * duplicates later, when adding new vnode to hash. | * duplicates later, when adding new vnode to hash. | ||||||||||
| * Note that duplicate can only appear in hash if the lowervp is | * Note that duplicate can only appear in hash if the lowervp is | ||||||||||
| * locked LK_SHARED. | * locked LK_SHARED. | ||||||||||
| */ | */ | ||||||||||
| xp = malloc(sizeof(struct null_node), M_NULLFSNODE, M_WAITOK); | xp = uma_zalloc_smr(null_node_zone, M_WAITOK); | ||||||||||
| error = getnewvnode("nullfs", mp, &null_vnodeops, &vp); | error = getnewvnode("nullfs", mp, &null_vnodeops, &vp); | ||||||||||
| if (error) { | if (error) { | ||||||||||
| vput(lowervp); | vput(lowervp); | ||||||||||
| free(xp, M_NULLFSNODE); | uma_zfree_smr(null_node_zone, xp); | ||||||||||
| return (error); | return (error); | ||||||||||
| } | } | ||||||||||
| VNPASS(vp->v_object == NULL, vp); | VNPASS(vp->v_object == NULL, vp); | ||||||||||
| VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) == 0, vp); | VNPASS((vn_irflag_read(vp) & VIRF_PGREAD) == 0, vp); | ||||||||||
| rw_wlock(&null_hash_lock); | rw_wlock(&null_hash_lock); | ||||||||||
| xp->null_vnode = vp; | xp->null_vnode = vp; | ||||||||||
| Show All 31 Lines | null_nodeget(struct mount *mp, struct vnode *lowervp, struct vnode **vpp) | ||||||||||
| if (error != 0) { | if (error != 0) { | ||||||||||
| rw_wunlock(&null_hash_lock); | rw_wunlock(&null_hash_lock); | ||||||||||
| vput(lowervp); | vput(lowervp); | ||||||||||
| vp->v_object = NULL; | vp->v_object = NULL; | ||||||||||
| null_destroy_proto(vp, xp); | null_destroy_proto(vp, xp); | ||||||||||
| return (error); | return (error); | ||||||||||
| } | } | ||||||||||
| null_hashins(mp, xp); | |||||||||||
| vn_set_state(vp, VSTATE_CONSTRUCTED); | vn_set_state(vp, VSTATE_CONSTRUCTED); | ||||||||||
| null_hashins(mp, xp); | |||||||||||
| rw_wunlock(&null_hash_lock); | rw_wunlock(&null_hash_lock); | ||||||||||
| *vpp = vp; | *vpp = vp; | ||||||||||
| return (0); | return (0); | ||||||||||
| } | } | ||||||||||
| /* | /* | ||||||||||
| * Remove node from hash. | * Remove node from hash. | ||||||||||
| */ | */ | ||||||||||
| void | void | ||||||||||
| null_hashrem(struct null_node *xp) | null_hashrem(struct null_node *xp) | ||||||||||
| { | { | ||||||||||
| rw_wlock(&null_hash_lock); | rw_wlock(&null_hash_lock); | ||||||||||
| LIST_REMOVE(xp, null_hash); | CK_LIST_REMOVE(xp, null_hash); | ||||||||||
| rw_wunlock(&null_hash_lock); | rw_wunlock(&null_hash_lock); | ||||||||||
| } | } | ||||||||||
| #ifdef DIAGNOSTIC | #ifdef DIAGNOSTIC | ||||||||||
| struct vnode * | struct vnode * | ||||||||||
| null_checkvp(struct vnode *vp, char *fil, int lno) | null_checkvp(struct vnode *vp, char *fil, int lno) | ||||||||||
| { | { | ||||||||||
| Show All 30 Lines | |||||||||||
Should be ordered alphabetically.