Try to get rid of all wedges we created (after unmounting).

This commit is contained in:
martin 2022-01-29 15:32:49 +00:00
parent 2c163b14db
commit 8bb96d39e3
4 changed files with 70 additions and 7 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: defs.h,v 1.76 2021/12/05 02:52:17 msaitoh Exp $ */
/* $NetBSD: defs.h,v 1.77 2022/01/29 15:32:49 martin Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -927,6 +927,7 @@ int target_dir_exists_p(const char *);
int target_file_exists_p(const char *);
int target_symlink_exists_p(const char *);
void unwind_mounts(void);
void register_post_umount_delwedge(const char *disk, const char *wedge);
int target_mounted(void);
void umount_root(void);

View File

@ -1,4 +1,4 @@
/* $NetBSD: gpt.c,v 1.26 2021/07/17 19:27:22 martin Exp $ */
/* $NetBSD: gpt.c,v 1.27 2022/01/29 15:32:49 martin Exp $ */
/*
* Copyright 2018 The NetBSD Foundation, Inc.
@ -1680,6 +1680,9 @@ gpt_free(struct disk_partitions *arg)
assert(parts != NULL);
for (p = parts->partitions; p != NULL; p = n) {
if (p->gp_flags & GPEF_WEDGE)
register_post_umount_delwedge(parts->dp.disk,
p->gp_dev_name);
free(__UNCONST(p->last_mounted));
n = p->gp_next;
free(p);

View File

@ -1,4 +1,4 @@
/* $NetBSD: target.c,v 1.15 2021/01/31 22:45:47 rillig Exp $ */
/* $NetBSD: target.c,v 1.16 2022/01/29 15:32:49 martin Exp $ */
/*
* Copyright 1997 Jonathan Stone
@ -71,7 +71,7 @@
#include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: target.c,v 1.15 2021/01/31 22:45:47 rillig Exp $");
__RCSID("$NetBSD: target.c,v 1.16 2022/01/29 15:32:49 martin Exp $");
#endif
/*
@ -83,6 +83,7 @@ __RCSID("$NetBSD: target.c,v 1.15 2021/01/31 22:45:47 rillig Exp $");
#include <sys/param.h> /* XXX vm_param.h always defines TRUE*/
#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <sys/stat.h> /* stat() */
#include <sys/mount.h> /* statfs() */
@ -93,7 +94,7 @@ __RCSID("$NetBSD: target.c,v 1.15 2021/01/31 22:45:47 rillig Exp $");
#include <unistd.h>
#include <curses.h> /* defines TRUE, but checks */
#include <errno.h>
#include <util.h>
#include "defs.h"
#include "md.h"
@ -119,6 +120,13 @@ struct unwind_mount {
char um_mountpoint[4]; /* Allocated longer... */
};
/* Record a wedge for later deletion after all file systems have been unmounted */
struct umount_delwedge {
struct umount_delwedge *next;
char disk[MAXPATHLEN], wedge[MAXPATHLEN];
};
struct umount_delwedge *post_umount_dwlist = NULL;
/* Unwind-mount stack */
struct unwind_mount *unwind_mountlist = NULL;
@ -519,6 +527,35 @@ target_mount(const char *opts, const char *from, const char *on)
return target_mount_do(opts, from, on);
}
static bool
delete_wedge(const char *disk, const char *wedge)
{
struct dkwedge_info dkw;
char diskpath[MAXPATHLEN];
int fd, error;
fd = opendisk(disk, O_RDWR, diskpath, sizeof(diskpath), 0);
if (fd < 0)
return false;
memset(&dkw, 0, sizeof(dkw));
strlcpy(dkw.dkw_devname, wedge, sizeof(dkw.dkw_devname));
error = ioctl(fd, DIOCDWEDGE, &dkw);
close(fd);
return error == 0;
}
void
register_post_umount_delwedge(const char *disk, const char *wedge)
{
struct umount_delwedge *dw;
dw = calloc(1, sizeof(*dw));
dw->next = post_umount_dwlist;
strlcpy(dw->disk, disk, sizeof(dw->disk));
strlcpy(dw->wedge, wedge, sizeof(dw->wedge));
post_umount_dwlist = dw;
}
/*
* unwind the mount stack, unmounting mounted filesystems.
* For now, ignore any errors in unmount.
@ -529,6 +566,7 @@ void
unwind_mounts(void)
{
struct unwind_mount *m;
struct umount_delwedge *dw;
static volatile int unwind_in_progress = 0;
/* signal safety */
@ -547,6 +585,11 @@ unwind_mounts(void)
target_prefix(), m->um_mountpoint);
free(m);
}
while ((dw = post_umount_dwlist) != NULL) {
post_umount_dwlist = dw->next;
delete_wedge(dw->disk, dw->wedge);
free(dw);
}
unwind_in_progress = 0;
}

View File

@ -1,4 +1,4 @@
/* $NetBSD: util.c,v 1.63 2022/01/03 11:44:02 martin Exp $ */
/* $NetBSD: util.c,v 1.64 2022/01/29 15:32:49 martin Exp $ */
/*
* Copyright 1997 Piermont Information Systems Inc.
@ -2541,9 +2541,9 @@ free_usage_set(struct partition_usage_set *wanted)
void
free_install_desc(struct install_partition_desc *install)
{
#ifndef NO_CLONES
size_t i, j;
#ifndef NO_CLONES
for (i = 0; i < install->num; i++) {
struct selected_partitions *src = install->infos[i].clone_src;
if (!(install->infos[i].flags & PUIFLG_CLONE_PARTS) ||
@ -2555,6 +2555,22 @@ free_install_desc(struct install_partition_desc *install)
install->infos[j].clone_src = NULL;
}
#endif
for (i = 0; i < install->num; i++) {
struct disk_partitions * parts = install->infos[i].parts;
if (parts == NULL)
continue;
if (parts->pscheme->free)
parts->pscheme->free(parts);
/* NULL all other references to this parts */
for (j = i+1; j < install->num; j++)
if (install->infos[j].parts == parts)
install->infos[j].parts = NULL;
}
free(install->write_back);
free(install->infos);
}