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 vn device #24

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions sbin/umount/umount.8
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,20 @@
.\" @(#)umount.8 8.2 (Berkeley) 5/8/95
.\" $FreeBSD: src/sbin/umount/umount.8,v 1.7.2.3 2001/12/14 15:17:57 ru Exp $
.\"
.Dd September 29, 2016
.Dd March 10, 2024
.Dt UMOUNT 8
.Os
.Sh NAME
.Nm umount
.Nd unmount filesystems
.Sh SYNOPSIS
.Nm
.Op Fl fv
.Op Fl dfv
.Ar special \&| node
.Nm
.Fl a | A
.Op Fl F Ar fstab
.Op Fl fv
.Op Fl dfv
.Op Fl h Ar host
.Op Fl t Ar type
.Sh DESCRIPTION
Expand Down Expand Up @@ -71,6 +71,9 @@ are unmounted.
.It Fl A
All the currently mounted filesystems except
the root are unmounted.
.It Fl d
If the filesystem is mounted on a memory disk (see
.Xr vn 4) , detach it after unmount(2).
.It Fl F Ar fstab
Specify the
.Pa fstab
Expand Down Expand Up @@ -132,7 +135,8 @@ filesystem table
.Sh SEE ALSO
.Xr unmount 2 ,
.Xr fstab 5 ,
.Xr mount 8
.Xr mount 8 ,
.Xr vnconfig 8
.Sh HISTORY
A
.Nm
Expand Down
54 changes: 49 additions & 5 deletions sbin/umount/umount.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/vnioctl.h>

#include <netdb.h>
#include <rpc/rpc.h>
#include <vfs/nfs/rpcv2.h>

#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <fstab.h>
#include <stdio.h>
#include <stdlib.h>
Expand All @@ -55,7 +59,7 @@ typedef enum { MNTON, MNTFROM, NOTHING } mntwhat;
typedef enum { MARK, UNMARK, NAME, COUNT, FREE } dowhat;

struct addrinfo *nfshost_ai = NULL;
int fflag, vflag;
int all, dflag, fflag, vflag;
char *nfshost;

void checkmntlist (char *, char **, char **, char **);
Expand All @@ -72,25 +76,29 @@ int checkname (char *, char **);
int umountfs (char *, char *, char *);
void usage (void) __dead2;
int xdr_dir (XDR *, char *);
int vn_detach(const char *);

int
main(int argc, char *argv[])
{
int all, errs, ch, mntsize, error;
int errs, ch, mntsize, error;
char **typelist = NULL, *mntonname, *mntfromname;
char *type, *mntfromnamerev, *mntonnamerev;
struct statfs *mntbuf;
struct addrinfo hints;

all = errs = 0;
while ((ch = getopt(argc, argv, "AaF:fh:t:v")) != -1) {
while ((ch = getopt(argc, argv, "AadF:fh:t:v")) != -1) {
switch (ch) {
case 'A':
all = 2;
break;
case 'a':
all = 1;
break;
case 'd':
dflag = 1;
break;
case 'F':
setfstab(optarg);
break;
Expand Down Expand Up @@ -478,6 +486,14 @@ umountfs(char *mntfromname, char *mntonname, char *type)
auth_destroy(clp->cl_auth);
clnt_destroy(clp);
}

if (dflag) {
if (vn_detach(mntfromname) && !all)
return (-1);
else if (vflag)
printf("%s: detached\n", mntfromname);
}

return (0);
}

Expand Down Expand Up @@ -744,7 +760,35 @@ usage(void)
{

fprintf(stderr, "%s\n%s\n",
"usage: umount [-fv] special | node",
" umount -a | -A [-F fstab] [-fv] [-h host] [-t type]");
"usage: umount [-dfv] special | node",
" umount -a | -A [-F fstab] [-dfv] [-h host] [-t type]");
exit(1);
}

int
vn_detach(const char *device)
{
struct vn_ioctl vnio;
int fd;

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

memset(&vnio, 0, sizeof(vnio));

fd = open(device, O_RDONLY);
if (fd < 0)
return (-1);

if (ioctl(fd, VNIOCDETACH, &vnio) < 0) {
warn("VNIOCDETACH: %s", device);
close(fd);
return (-1);
}

close(fd);
return (0);
}