Add the concept of a per-disk error string, and a function which prints it

along with the errno, if one is set.
This commit is contained in:
jmallett 2002-10-22 19:25:58 +00:00
parent 29ef1b5025
commit df1f55a8e7
6 changed files with 78 additions and 1 deletions

View File

@ -1,7 +1,7 @@
# $FreeBSD$
LIB= ufs
SRCS= block.c inode.c sblock.c type.c
SRCS= block.c error.c inode.c sblock.c type.c
INCS= libufs.h
CFLAGS+= -I${.CURDIR} -D_LIBUFS
.if defined(LIBUFS_DEBUG)

View File

@ -63,6 +63,7 @@ bread(struct uufsd *disk, ufs2_daddr_t blockno, void *data, size_t size)
*/
if (cnt != size) {
DEBUG("short read");
disk->d_error = "short read from block device";
for (cnt = 0; cnt < disk->d_fs.fs_bsize; cnt++)
buf[cnt] = 0;
return -1;
@ -80,6 +81,7 @@ bwrite(struct uufsd *disk, ufs2_daddr_t blockno, const void *data, size_t size)
cnt = pwrite(disk->d_fd, data, size, (off_t)(blockno * disk->d_bsize));
if (cnt != size) {
DEBUG("short write");
disk->d_error = "short write to block device";
return -1;
}

62
lib/libufs/error.c Normal file
View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2002 Juli Mallett. All rights reserved.
*
* This software was written by Juli Mallett <jmallett@FreeBSD.org> for the
* FreeBSD project. Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistribution of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/mount.h>
#include <sys/disklabel.h>
#include <sys/stat.h>
#include <ufs/ufs/ufsmount.h>
#include <ufs/ufs/dinode.h>
#include <ufs/ffs/fs.h>
#include <errno.h>
#include <stdio.h>
#include <libufs.h>
void
libufs_printerror(struct uufsd *disk)
{
if (disk == NULL) {
fprintf(stderr, "no disk\n");
return;
}
if (disk->d_error != NULL) {
fprintf(stderr, "disk error: %s", disk->d_error);
/*
* XXX
* Should there be a per-disk errno?
*/
if (errno)
fprintf(stderr, ": %s", strerror(errno));
fprintf(stderr, "\n");
}
}

View File

@ -73,6 +73,7 @@ struct uufsd {
char d_sb[MAXBSIZE];
/* superblock as buffer */
} d_sbunion;
const char *d_error; /* human readable disk error */
#define d_fs d_sbunion.d_fs
#define d_sb d_sbunion.d_sb
};
@ -89,6 +90,11 @@ __BEGIN_DECLS
ssize_t bread(struct uufsd *, ufs2_daddr_t, void *, size_t);
ssize_t bwrite(struct uufsd *, ufs2_daddr_t, const void *, size_t);
/*
* error.c
*/
void libufs_printerror(struct uufsd *);
/*
* inode.c
*/

View File

@ -60,6 +60,7 @@ sbread(struct uufsd *disk)
for (sb = 0; (superblock = superblocks[sb]) != -1; sb++) {
if (bread(disk, superblock, disk->d_sb, SBLOCKSIZE) == -1) {
disk->d_error = "truncated superblock";
DEBUG(NULL);
return -1;
}
@ -82,6 +83,7 @@ sbread(struct uufsd *disk)
* which to associate this disk/filesystem.
*/
DEBUG("no superblock found");
disk->d_error = "no superblock found";
errno = ENOENT;
return -1;
}
@ -104,10 +106,12 @@ sbwrite(struct uufsd *disk, int all)
disk->d_fd = open(disk->d_name, O_WRONLY);
if (disk->d_fd < 0) {
DEBUG("open");
disk->d_error = "failed to open disk";
return -1;
}
if (bwrite(disk, disk->d_sblock, fs, SBLOCKSIZE) == -1) {
DEBUG(NULL);
disk->d_error = "failed to write superblock";
return -1;
}
if (all) {
@ -115,6 +119,7 @@ sbwrite(struct uufsd *disk, int all)
if (bwrite(disk, fsbtodb(fs, cgsblock(fs, i)),
fs, SBLOCKSIZE) == -1) {
DEBUG(NULL);
disk->d_error = "failed to update a superblock";
return -1;
}
}

View File

@ -99,6 +99,7 @@ ufs_disk_fillout(struct uufsd *disk, const char *name)
fd = open(name, O_RDONLY);
if (fd == -1) {
DEBUG("open");
disk->d_error = "failed to open disk for reading";
return -1;
}
@ -109,6 +110,7 @@ ufs_disk_fillout(struct uufsd *disk, const char *name)
disk->d_inomax = 0;
disk->d_name = name;
disk->d_ufs = 0;
disk->d_error = NULL;
if (sbread(disk) == -1) {
DEBUG(NULL);