Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

umount: Add -d option to detach vnd devices #31

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions sbin/umount/umount.8
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@
.\"
.\" @(#)umount.8 8.2 (Berkeley) 5/8/95
.\"
.Dd September 12, 2016
.Dd February 3, 2024
.Dt UMOUNT 8
.Os
.Sh NAME
.Nm umount
.Nd unmount file systems
.Sh SYNOPSIS
.Nm
.Op Fl fvFR
.Op Fl dfvFR
.Op Fl t Ar fstypelist
.Ar special | node
.Nm
.Fl a
.Op Fl fvF
.Op Fl dfvF
.Op Fl h Ar host
.Op Fl t Ar fstypelist
.Sh DESCRIPTION
Expand Down Expand Up @@ -83,6 +83,11 @@ The options are as follows:
.Bl -tag -width indent
.It Fl a
All the currently mounted file systems except the root are unmounted.
.It Fl d
If the filesystem is mounted on an
.Xr vnd 4
device (a vnode disk), detach it after
.Xr unmount 2 .
.It Fl f
The file system is forcibly unmounted.
Active special devices continue to work,
Expand Down Expand Up @@ -165,7 +170,8 @@ file system table
.Sh SEE ALSO
.Xr unmount 2 ,
.Xr fstab 5 ,
.Xr mount 8
.Xr mount 8 ,
.Xr vndconfig 8
.Sh HISTORY
A
.Nm
Expand Down
67 changes: 61 additions & 6 deletions sbin/umount/umount.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ __RCSID("$NetBSD: umount.c,v 1.53 2020/04/23 04:21:13 christos Exp $");
#include <rpc/pmap_prot.h>
#include <nfs/rpcv2.h>
#include <nfs/nfsmount.h>

#include <dev/vndvar.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#endif /* !SMALL */

#include <err.h>
Expand All @@ -72,25 +76,26 @@ typedef enum { MNTANY, MNTON, MNTFROM } mntwhat;
#ifndef SMALL
#include "mountprog.h"

static int fake, verbose;
static int dflag, fake, verbose;
static char *nfshost;
static struct addrinfo *nfshost_ai = NULL;

static int namematch(const struct addrinfo *);
static int sacmp(const struct sockaddr *, const struct sockaddr *);
static int xdr_dir(XDR *, char *);
static const char *getmntproto(const char *);
static int vn_detach(const char *);
#endif /* !SMALL */

static int fflag;
static int all, fflag;
static char *getmntname(const char *, mntwhat, char **);
static int umountfs(const char *, const char **, int);
static void usage(void) __dead;

int
main(int argc, char *argv[])
{
int ch, errs, all = 0, raw = 0;
int ch, errs, raw = 0;
char mntfromname[MAXPATHLEN];
#ifndef SMALL
int mnts;
Expand All @@ -102,7 +107,7 @@ main(int argc, char *argv[])
#ifdef SMALL
#define OPTS "fR"
#else
#define OPTS "AaFfh:Rt:v"
#define OPTS "AadFfh:Rt:v"
#endif
while ((ch = getopt(argc, argv, OPTS)) != -1)
switch (ch) {
Expand All @@ -117,6 +122,9 @@ main(int argc, char *argv[])
case 'a':
all = 1;
break;
case 'd':
dflag = 1;
break;
case 'F':
fake = 1;
break;
Expand Down Expand Up @@ -195,6 +203,7 @@ umountfs(const char *name, const char **typelist, int raw)
char *type, rname[MAXPATHLEN], umountprog[MAXPATHLEN];
mntwhat what;
struct stat sb;
struct statvfs sfs;

if (raw) {
mntpt = name;
Expand Down Expand Up @@ -310,6 +319,11 @@ umountfs(const char *name, const char **typelist, int raw)
#ifndef SMALL
if (verbose)
(void)printf("(No separate unmount program.)\n");

if (dflag && statvfs(mntpt, &sfs) < 0) {
warn("%s: statvfs", mntpt);
return 1;
}
#endif

if (unmount(mntpt, fflag) == -1) {
Expand All @@ -336,6 +350,15 @@ umountfs(const char *name, const char **typelist, int raw)
auth_destroy(clp->cl_auth);
clnt_destroy(clp);
}

if (dflag) {
if (vn_detach(sfs.f_mntfromname) == 0) {
if (verbose)
(void)printf("%s: detached\n",
sfs.f_mntfromname);
} else if (!all)
return (-1);
}
#endif /* ! SMALL */
return 0;
}
Expand Down Expand Up @@ -444,6 +467,38 @@ getmntproto(const char *name)
(void)mount("nfs", name, MNT_GETARGS, &nfsargs, sizeof(nfsargs));
return nfsargs.sotype == SOCK_STREAM ? "tcp" : "udp";
}

int
vn_detach(const char *dev)
{
struct vnd_ioctl vndio;
char rdev[MAXPATHLEN + 1];
int fd;

if (strncmp(dev, "/dev/vnd", sizeof("/dev/vnd") - 1)) {
if (!all)
warnx("invalid vnd device: %s", dev);
return -1;
}

fd = opendisk(dev, O_RDWR, rdev, sizeof(rdev), 0);
if (fd < 0) {
warn("%s: opendisk", rdev);
return -1;
}

memset(&vndio, 0, sizeof(vndio));
vndio.vnd_flags = fflag ? VNDIOF_FORCE : 0;

if (ioctl(fd, VNDIOCCLR, &vndio) < 0) {
warn("%s: VNDIOCCLR", rdev);
close(fd);
return -1;
}
close(fd);

return 0;
}
#endif /* !SMALL */

static void
Expand All @@ -454,8 +509,8 @@ usage(void)
"Usage: %s [-fR] special | node\n", getprogname());
#else
(void)fprintf(stderr,
"Usage: %s [-fvFR] [-t fstypelist] special | node\n"
"\t %s -a[fvF] [-h host] [-t fstypelist]\n", getprogname(),
"Usage: %s [-dfvFR] [-t fstypelist] special | node\n"
"\t %s -a[dfvF] [-h host] [-t fstypelist]\n", getprogname(),
getprogname());
#endif /* SMALL */
exit(1);
Expand Down