From df1f55a8e733bbc4e0c100fe74528b50da43f5f1 Mon Sep 17 00:00:00 2001 From: jmallett Date: Tue, 22 Oct 2002 19:25:58 +0000 Subject: Add the concept of a per-disk error string, and a function which prints it along with the errno, if one is set. --- lib/libufs/Makefile | 2 +- lib/libufs/block.c | 2 ++ lib/libufs/error.c | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++ lib/libufs/libufs.h | 6 ++++++ lib/libufs/sblock.c | 5 +++++ lib/libufs/type.c | 2 ++ 6 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 lib/libufs/error.c (limited to 'lib') diff --git a/lib/libufs/Makefile b/lib/libufs/Makefile index 0ac1a74..c3b3efc 100644 --- a/lib/libufs/Makefile +++ b/lib/libufs/Makefile @@ -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) diff --git a/lib/libufs/block.c b/lib/libufs/block.c index 054896f..45975b5 100644 --- a/lib/libufs/block.c +++ b/lib/libufs/block.c @@ -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; } diff --git a/lib/libufs/error.c b/lib/libufs/error.c new file mode 100644 index 0000000..5211e77 --- /dev/null +++ b/lib/libufs/error.c @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2002 Juli Mallett. All rights reserved. + * + * This software was written by Juli Mallett 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 +__FBSDID("$FreeBSD$"); + +#include +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include + +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"); + } +} diff --git a/lib/libufs/libufs.h b/lib/libufs/libufs.h index 569d8a0..6f7dc7e 100644 --- a/lib/libufs/libufs.h +++ b/lib/libufs/libufs.h @@ -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 }; @@ -90,6 +91,11 @@ 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 */ int getino(struct uufsd *, void **, ino_t, int *); diff --git a/lib/libufs/sblock.c b/lib/libufs/sblock.c index aaac7b4..5262412 100644 --- a/lib/libufs/sblock.c +++ b/lib/libufs/sblock.c @@ -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; } } diff --git a/lib/libufs/type.c b/lib/libufs/type.c index 0b16c80..8bc30bd 100644 --- a/lib/libufs/type.c +++ b/lib/libufs/type.c @@ -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); -- cgit v1.1