Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys Add functions to open devices by device number or path.
details: https://anonhg.NetBSD.org/src/rev/f9b686d2b2b1
branches: trunk
changeset: 966656:f9b686d2b2b1
user: mlelstv <mlelstv%NetBSD.org@localhost>
date: Sun Nov 10 06:47:30 2019 +0000
description:
Add functions to open devices by device number or path.
diffstat:
sys/kern/vfs_vnops.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++-
sys/sys/vnode.h | 6 ++++-
2 files changed, 60 insertions(+), 3 deletions(-)
diffs (102 lines):
diff -r c9dc3c7f4a21 -r f9b686d2b2b1 sys/kern/vfs_vnops.c
--- a/sys/kern/vfs_vnops.c Sun Nov 10 00:54:57 2019 +0000
+++ b/sys/kern/vfs_vnops.c Sun Nov 10 06:47:30 2019 +0000
@@ -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 @@
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);
+}
diff -r c9dc3c7f4a21 -r f9b686d2b2b1 sys/sys/vnode.h
--- a/sys/sys/vnode.h Sun Nov 10 00:54:57 2019 +0000
+++ b/sys/sys/vnode.h Sun Nov 10 06:47:30 2019 +0000
@@ -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 file;
struct filedesc;
struct nameidata;
+struct pathbuf;
struct proc;
struct stat;
struct uio;
@@ -550,6 +551,9 @@
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
Home |
Main Index |
Thread Index |
Old Index