Try DIOCWLABEL ioctl only if write op fails with EROFS

because the ioctl is required and valid only for raw devices.
This commit is contained in:
tsutsui 2008-05-24 19:15:21 +00:00
parent c2987416cd
commit ff1b9852ce
1 changed files with 23 additions and 21 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: macppc.c,v 1.10 2008/05/09 10:14:35 tsutsui Exp $ */ /* $NetBSD: macppc.c,v 1.11 2008/05/24 19:15:21 tsutsui Exp $ */
/*- /*-
* Copyright (c) 2002 The NetBSD Foundation, Inc. * Copyright (c) 2002 The NetBSD Foundation, Inc.
@ -35,13 +35,14 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if !defined(__lint) #if !defined(__lint)
__RCSID("$NetBSD: macppc.c,v 1.10 2008/05/09 10:14:35 tsutsui Exp $"); __RCSID("$NetBSD: macppc.c,v 1.11 2008/05/24 19:15:21 tsutsui Exp $");
#endif /* !__lint */ #endif /* !__lint */
#include <sys/param.h> #include <sys/param.h>
#ifndef HAVE_NBTOOL_CONFIG_H #ifndef HAVE_NBTOOL_CONFIG_H
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/dkio.h> #include <sys/dkio.h>
#include <errno.h>
#endif #endif
#include <assert.h> #include <assert.h>
@ -97,9 +98,6 @@ writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params,
struct apple_drvr_map dm; struct apple_drvr_map dm;
struct apple_part_map_entry pme; struct apple_part_map_entry pme;
int rv; int rv;
#ifdef DIOCWLABEL
int enable;
#endif
assert (params != NULL); assert (params != NULL);
assert (bb_params != NULL); assert (bb_params != NULL);
@ -118,24 +116,28 @@ writeapplepartmap(ib_params *params, struct bbinfo_params *bb_params,
dm.sbBlockSize = htobe16(512); dm.sbBlockSize = htobe16(512);
dm.sbBlkCount = htobe32(0); dm.sbBlkCount = htobe32(0);
#ifdef DIOCWLABEL
/*
* block 0 is LABELSECTOR which might be protected by
* bounds_check_with_label(9).
*/
enable = 1;
rv = ioctl(params->fsfd, DIOCWLABEL, &enable);
if (rv != 0) {
warn("Cannot enable writes to the label sector");
return 0;
}
#endif
rv = pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0); rv = pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0);
#ifdef DIOCWLABEL #ifdef DIOCWLABEL
/* Reset write-protect. */ if (rv == -1 && errno == EROFS) {
enable = 0; /*
(void)ioctl(params->fsfd, DIOCWLABEL, &enable); * block 0 is LABELSECTOR which might be protected by
* bounds_check_with_label(9).
*/
int enable;
enable = 1;
rv = ioctl(params->fsfd, DIOCWLABEL, &enable);
if (rv != 0) {
warn("Cannot enable writes to the label sector");
return 0;
}
rv = pwrite(params->fsfd, &dm, MACPPC_BOOT_BLOCK_BLOCKSIZE, 0);
/* Reset write-protect. */
enable = 0;
(void)ioctl(params->fsfd, DIOCWLABEL, &enable);
}
#endif #endif
if (rv != MACPPC_BOOT_BLOCK_BLOCKSIZE) { if (rv != MACPPC_BOOT_BLOCK_BLOCKSIZE) {
warn("Can't write sector 0 of `%s'", params->filesystem); warn("Can't write sector 0 of `%s'", params->filesystem);