Add functions to open devices by device number or path.

This commit is contained in:
mlelstv 2019-11-10 06:47:30 +00:00
parent 700433206f
commit ab2ce32b2b
2 changed files with 60 additions and 3 deletions

View File

@ -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);
}

View File

@ -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, const char *, size_t,
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