From a02fe51bef9f3268aaee225de20b6b1d668f2569 Mon Sep 17 00:00:00 2001 From: pooka Date: Tue, 6 Nov 2007 15:09:07 +0000 Subject: [PATCH] Open the kernel descriptor as part of mount(), not init(). Then it doesn't matter if someone fork()s or does other tricks between init() and mount() (and besides, now it's where it logically should be). --- lib/libpuffs/puffs.3 | 9 +++++--- lib/libpuffs/puffs.c | 31 ++++++++++++++-------------- usr.sbin/puffs/mount_9p/ninepuffs.c | 10 ++++----- usr.sbin/puffs/mount_psshfs/psshfs.c | 9 ++++---- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/libpuffs/puffs.3 b/lib/libpuffs/puffs.3 index d31592592306..c4e973694d8e 100644 --- a/lib/libpuffs/puffs.3 +++ b/lib/libpuffs/puffs.3 @@ -1,4 +1,4 @@ -.\" $NetBSD: puffs.3,v 1.31 2007/11/05 17:48:17 pooka Exp $ +.\" $NetBSD: puffs.3,v 1.32 2007/11/06 15:09:07 pooka Exp $ .\" .\" Copyright (c) 2006, 2007 Antti Kantee. All rights reserved. .\" @@ -23,7 +23,7 @@ .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" -.Dd July 18, 2007 +.Dd November 6, 2007 .Dt PUFFS 3 .Os .Sh NAME @@ -254,11 +254,14 @@ and .Xr kqueue 2 are all examples of acceptable operations. .It Fn puffs_setblockingmode "pu" "mode" -Sets the library to blocking or non-blocking mode. +Sets the file system upstream access to blocking or non-blocking mode. Acceptable values for the argument are .Dv PUFFSDEV_BLOCK and .Dv PUFFSDEV_NONBLOCK . +.Pp +This routine can be called only after calling +.Fn puffs_mount . .It Fn puffs_getstate "pu" Returns the state of the file system. It is maintained by the framework and is mostly useful for the framework diff --git a/lib/libpuffs/puffs.c b/lib/libpuffs/puffs.c index ecbc57f28822..7c2fedaa2dd9 100644 --- a/lib/libpuffs/puffs.c +++ b/lib/libpuffs/puffs.c @@ -1,4 +1,4 @@ -/* $NetBSD: puffs.c,v 1.73 2007/11/05 17:48:17 pooka Exp $ */ +/* $NetBSD: puffs.c,v 1.74 2007/11/06 15:09:08 pooka Exp $ */ /* * Copyright (c) 2005, 2006, 2007 Antti Kantee. All Rights Reserved. @@ -31,7 +31,7 @@ #include #if !defined(lint) -__RCSID("$NetBSD: puffs.c,v 1.73 2007/11/05 17:48:17 pooka Exp $"); +__RCSID("$NetBSD: puffs.c,v 1.74 2007/11/06 15:09:08 pooka Exp $"); #endif /* !lint */ #include @@ -124,6 +124,8 @@ puffs_setblockingmode(struct puffs_usermount *pu, int mode) { int rv, x; + assert(puffs_getstate(pu) == PUFFS_STATE_RUNNING); + if (mode != PUFFSDEV_BLOCK && mode != PUFFSDEV_NONBLOCK) { errno = EINVAL; return -1; @@ -345,7 +347,7 @@ puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags, void *cookie) { char rp[MAXPATHLEN]; - int rv; + int rv, fd; #if 1 /* XXXkludgehere */ @@ -362,6 +364,16 @@ puffs_mount(struct puffs_usermount *pu, const char *dir, int mntflags, warnx("puffs_mount: using \"%s\" instead.", rp); } + fd = open(_PATH_PUFFS, O_RDONLY); + if (fd == -1) { + warnx("puffs_mount: cannot open %s", _PATH_PUFFS); + return -1; + } + if (fd <= 2) + warnx("puffs_init: device fd %d (<= 2), sure this is " + "what you want?", fd); + + pu->pu_kargp->pa_fd = pu->pu_fd = fd; pu->pu_kargp->pa_root_cookie = cookie; if ((rv = mount(MOUNT_PUFFS, rp, mntflags, pu->pu_kargp, sizeof(struct puffs_kargs))) == -1) @@ -385,7 +397,7 @@ _puffs_init(int develv, struct puffs_ops *pops, const char *mntfromname, { struct puffs_usermount *pu; struct puffs_kargs *pargs; - int sverrno, fd; + int sverrno; if (develv != PUFFS_DEVEL_LIBVERSION) { warnx("puffs_init: mounting with lib version %d, need %d", @@ -394,15 +406,6 @@ _puffs_init(int develv, struct puffs_ops *pops, const char *mntfromname, return NULL; } - fd = open(_PATH_PUFFS, O_RDONLY); - if (fd == -1) { - warnx("puffs_init: cannot open %s", _PATH_PUFFS); - return NULL; - } - if (fd <= 2) - warnx("puffs_init: device fd %d (<= 2), sure this is " - "what you want?", fd); - pu = malloc(sizeof(struct puffs_usermount)); if (pu == NULL) goto failfree; @@ -415,7 +418,6 @@ _puffs_init(int develv, struct puffs_ops *pops, const char *mntfromname, pargs->pa_vers = PUFFSDEVELVERS | PUFFSVERSION; pargs->pa_flags = PUFFS_FLAG_KERN(pflags); - pargs->pa_fd = pu->pu_fd = fd; fillvnopmask(pops, pargs->pa_vnopmask); (void)strlcpy(pargs->pa_typename, puffsname, sizeof(pargs->pa_typename)); @@ -459,7 +461,6 @@ _puffs_init(int develv, struct puffs_ops *pops, const char *mntfromname, failfree: /* can't unmount() from here for obvious reasons */ sverrno = errno; - close(fd); free(pu); errno = sverrno; return NULL; diff --git a/usr.sbin/puffs/mount_9p/ninepuffs.c b/usr.sbin/puffs/mount_9p/ninepuffs.c index bf5e118de74d..0331d68dda20 100644 --- a/usr.sbin/puffs/mount_9p/ninepuffs.c +++ b/usr.sbin/puffs/mount_9p/ninepuffs.c @@ -1,4 +1,4 @@ -/* $NetBSD: ninepuffs.c,v 1.20 2007/11/05 17:54:32 pooka Exp $ */ +/* $NetBSD: ninepuffs.c,v 1.21 2007/11/06 15:09:08 pooka Exp $ */ /* * Copyright (c) 2007 Antti Kantee. All Rights Reserved. @@ -31,7 +31,7 @@ #include #ifndef lint -__RCSID("$NetBSD: ninepuffs.c,v 1.20 2007/11/05 17:54:32 pooka Exp $"); +__RCSID("$NetBSD: ninepuffs.c,v 1.21 2007/11/06 15:09:08 pooka Exp $"); #endif /* !lint */ #include @@ -209,9 +209,6 @@ main(int argc, char *argv[]) exit(1); } - if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) - err(1, "setblockingmode"); - puffs_framev_init(pu, p9pbuf_read, p9pbuf_write, p9pbuf_cmp, NULL, puffs_framev_unmountonclose); if (puffs_framev_addfd(pu, p9p.servsock, @@ -224,6 +221,9 @@ main(int argc, char *argv[]) if (puffs_mount(pu, argv[1], mntflags, pn_root) == -1) err(1, "puffs_mount"); + if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) + err(1, "setblockingmode"); + if (puffs_mainloop(pu) == -1) err(1, "mainloop"); diff --git a/usr.sbin/puffs/mount_psshfs/psshfs.c b/usr.sbin/puffs/mount_psshfs/psshfs.c index a46fff585987..c6bc91c4ce16 100644 --- a/usr.sbin/puffs/mount_psshfs/psshfs.c +++ b/usr.sbin/puffs/mount_psshfs/psshfs.c @@ -1,4 +1,4 @@ -/* $NetBSD: psshfs.c,v 1.38 2007/11/05 17:54:32 pooka Exp $ */ +/* $NetBSD: psshfs.c,v 1.39 2007/11/06 15:09:08 pooka Exp $ */ /* * Copyright (c) 2006 Antti Kantee. All Rights Reserved. @@ -41,7 +41,7 @@ #include #ifndef lint -__RCSID("$NetBSD: psshfs.c,v 1.38 2007/11/05 17:54:32 pooka Exp $"); +__RCSID("$NetBSD: psshfs.c,v 1.39 2007/11/06 15:09:08 pooka Exp $"); #endif /* !lint */ #include @@ -200,8 +200,6 @@ main(int argc, char *argv[]) pssh_connect(&pctx, sshargs); - if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) - err(1, "setblockingmode"); if (exportfs) puffs_setfhsize(pu, sizeof(struct psshfs_fid), PUFFS_FHFLAG_NFSV2 | PUFFS_FHFLAG_NFSV3); @@ -224,6 +222,9 @@ main(int argc, char *argv[]) if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1) err(1, "puffs_mount"); + if (puffs_setblockingmode(pu, PUFFSDEV_NONBLOCK) == -1) + err(1, "setblockingmode"); + if (puffs_mainloop(pu) == -1) err(1, "mainloop");