Module Name: src Committed By: mlelstv Date: Sun Nov 10 06:47:30 UTC 2019
Modified Files: src/sys/kern: vfs_vnops.c src/sys/sys: vnode.h Log Message: Add functions to open devices by device number or path. To generate a diff of this commit: cvs rdiff -u -r1.201 -r1.202 src/sys/kern/vfs_vnops.c cvs rdiff -u -r1.282 -r1.283 src/sys/sys/vnode.h Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/sys/kern/vfs_vnops.c diff -u src/sys/kern/vfs_vnops.c:1.201 src/sys/kern/vfs_vnops.c:1.202 --- src/sys/kern/vfs_vnops.c:1.201 Sun Sep 15 20:24:25 2019 +++ src/sys/kern/vfs_vnops.c Sun Nov 10 06:47:30 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: vfs_vnops.c,v 1.201 2019/09/15 20:24:25 christos Exp $ */ +/* $NetBSD: vfs_vnops.c,v 1.202 2019/11/10 06:47:30 mlelstv Exp $ */ /*- * Copyright (c) 2009 The NetBSD Foundation, Inc. @@ -66,7 +66,7 @@ */ #include <sys/cdefs.h> -__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.201 2019/09/15 20:24:25 christos Exp $"); +__KERNEL_RCSID(0, "$NetBSD: vfs_vnops.c,v 1.202 2019/11/10 06:47:30 mlelstv Exp $"); #include "veriexec.h" @@ -1189,3 +1189,56 @@ vn_fifo_bypass(void *v) return VOCALL(fifo_vnodeop_p, ap->a_desc->vdesc_offset, v); } + +/* + * Open block device by device number + */ +int +vn_bdev_open(dev_t dev, struct vnode **vpp, struct lwp *l) +{ + int error; + + if ((error = bdevvp(dev, vpp)) != 0) + return error; + + if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) { + vrele(*vpp); + return error; + } + mutex_enter((*vpp)->v_interlock); + (*vpp)->v_writecount++; + mutex_exit((*vpp)->v_interlock); + + return 0; +} + +/* + * Lookup the provided name in the filesystem. If the file exists, + * is a valid block device, and isn't being used by anyone else, + * set *vpp to the file's vnode. + */ +int +vn_bdev_openpath(struct pathbuf *pb, struct vnode **vpp, struct lwp *l) +{ + struct nameidata nd; + struct vnode *vp; + dev_t dev; + enum vtype vt; + int error; + + NDINIT(&nd, LOOKUP, FOLLOW, pb); + if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) + return error; + + vp = nd.ni_vp; + dev = vp->v_rdev; + vt = vp->v_type; + + VOP_UNLOCK(vp); + (void) vn_close(vp, FREAD | FWRITE, l->l_cred); + + if (vt != VBLK) + return ENOTBLK; + + return vn_bdev_open(dev, vpp, l); +} Index: src/sys/sys/vnode.h diff -u src/sys/sys/vnode.h:1.282 src/sys/sys/vnode.h:1.283 --- src/sys/sys/vnode.h:1.282 Thu Sep 26 20:57:19 2019 +++ src/sys/sys/vnode.h Sun Nov 10 06:47:30 2019 @@ -1,4 +1,4 @@ -/* $NetBSD: vnode.h,v 1.282 2019/09/26 20:57:19 christos Exp $ */ +/* $NetBSD: vnode.h,v 1.283 2019/11/10 06:47:30 mlelstv Exp $ */ /*- * Copyright (c) 2008 The NetBSD Foundation, Inc. @@ -490,6 +490,7 @@ struct vop_generic_args { struct file; struct filedesc; struct nameidata; +struct pathbuf; struct proc; struct stat; struct uio; @@ -550,6 +551,9 @@ int vn_extattr_set(struct vnode *, int, int vn_extattr_rm(struct vnode *, int, int, const char *, struct lwp *); void vn_ra_allocctx(struct vnode *); int vn_fifo_bypass(void *); +int vn_bdev_open(dev_t, struct vnode **, struct lwp *); +int vn_bdev_openpath(struct pathbuf *pb, struct vnode **, struct lwp *); + #ifdef DIAGNOSTIC static __inline bool