aboutsummaryrefslogtreecommitdiff
path: root/sbin/clri
diff options
context:
space:
mode:
authorKirk McKusick <mckusick@FreeBSD.org>2018-01-26 00:58:32 +0000
committerKirk McKusick <mckusick@FreeBSD.org>2018-01-26 00:58:32 +0000
commitdffce2150eeac1c7f424324a5cade7a97b308979 (patch)
tree3de8c223a1cb007078f3bb3e3c062127eb1b80a8 /sbin/clri
parenta99028fc70be602db425c092b70dd0221e253bb3 (diff)
downloadsrc-dffce2150eeac1c7f424324a5cade7a97b308979.tar.gz
src-dffce2150eeac1c7f424324a5cade7a97b308979.zip
Refactoring of reading and writing of the UFS/FFS superblock.
Specifically reading is done if ffs_sbget() and writing is done in ffs_sbput(). These functions are exported to libufs via the sbget() and sbput() functions which then used in the various filesystem utilities. This work is in preparation for adding subperblock check hashes. No functional change intended. Reviewed by: kib
Notes
Notes: svn path=/head/; revision=328426
Diffstat (limited to 'sbin/clri')
-rw-r--r--sbin/clri/Makefile1
-rw-r--r--sbin/clri/clri.c63
2 files changed, 28 insertions, 36 deletions
diff --git a/sbin/clri/Makefile b/sbin/clri/Makefile
index 114f56382a18..5f8777f30391 100644
--- a/sbin/clri/Makefile
+++ b/sbin/clri/Makefile
@@ -4,6 +4,7 @@
PACKAGE=runtime
PROG= clri
MAN= clri.8
+LIBADD= ufs
WARNS?= 2
.include <bsd.prog.mk>
diff --git a/sbin/clri/clri.c b/sbin/clri/clri.c
index 7745aa56d18f..3407297ceb6f 100644
--- a/sbin/clri/clri.c
+++ b/sbin/clri/clri.c
@@ -54,17 +54,14 @@ __FBSDID("$FreeBSD$");
#include <ufs/ffs/fs.h>
#include <err.h>
+#include <errno.h>
#include <fcntl.h>
+#include <libufs.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
-/*
- * Possible superblock locations ordered from most to least likely.
- */
-static int sblock_try[] = SBLOCKSEARCH;
-
static void
usage(void)
{
@@ -75,41 +72,35 @@ usage(void)
int
main(int argc, char *argv[])
{
- struct fs *sbp;
+ struct fs *fs;
struct ufs1_dinode *dp1;
struct ufs2_dinode *dp2;
char *ibuf[MAXBSIZE];
long generation, bsize;
off_t offset;
- int i, fd, inonum;
- char *fs, sblock[SBLOCKSIZE];
+ int fd, ret, inonum;
+ char *fsname;
void *v = ibuf;
if (argc < 3)
usage();
- fs = *++argv;
- sbp = NULL;
+ fsname = *++argv;
/* get the superblock. */
- if ((fd = open(fs, O_RDWR, 0)) < 0)
- err(1, "%s", fs);
- for (i = 0; sblock_try[i] != -1; i++) {
- if (lseek(fd, (off_t)(sblock_try[i]), SEEK_SET) < 0)
- err(1, "%s", fs);
- if (read(fd, sblock, sizeof(sblock)) != sizeof(sblock))
- errx(1, "%s: can't read superblock", fs);
- sbp = (struct fs *)sblock;
- if ((sbp->fs_magic == FS_UFS1_MAGIC ||
- (sbp->fs_magic == FS_UFS2_MAGIC &&
- sbp->fs_sblockloc == sblock_try[i])) &&
- sbp->fs_bsize <= MAXBSIZE &&
- sbp->fs_bsize >= (int)sizeof(struct fs))
- break;
+ if ((fd = open(fsname, O_RDWR, 0)) < 0)
+ err(1, "%s", fsname);
+ if ((ret = sbget(fd, &fs, -1)) != 0) {
+ switch (ret) {
+ case ENOENT:
+ warn("Cannot find file system superblock");
+ return (1);
+ default:
+ warn("Unable to read file system superblock");
+ return (1);
+ }
}
- if (sblock_try[i] == -1)
- errx(2, "cannot find file system superblock");
- bsize = sbp->fs_bsize;
+ bsize = fs->fs_bsize;
/* remaining arguments are inode numbers. */
while (*++argv) {
@@ -119,20 +110,20 @@ main(int argc, char *argv[])
(void)printf("clearing %d\n", inonum);
/* read in the appropriate block. */
- offset = ino_to_fsba(sbp, inonum); /* inode to fs blk */
- offset = fsbtodb(sbp, offset); /* fs blk disk blk */
+ offset = ino_to_fsba(fs, inonum); /* inode to fs blk */
+ offset = fsbtodb(fs, offset); /* fs blk disk blk */
offset *= DEV_BSIZE; /* disk blk to bytes */
/* seek and read the block */
if (lseek(fd, offset, SEEK_SET) < 0)
- err(1, "%s", fs);
+ err(1, "%s", fsname);
if (read(fd, ibuf, bsize) != bsize)
- err(1, "%s", fs);
+ err(1, "%s", fsname);
- if (sbp->fs_magic == FS_UFS2_MAGIC) {
+ if (fs->fs_magic == FS_UFS2_MAGIC) {
/* get the inode within the block. */
dp2 = &(((struct ufs2_dinode *)v)
- [ino_to_fsbo(sbp, inonum)]);
+ [ino_to_fsbo(fs, inonum)]);
/* clear the inode, and bump the generation count. */
generation = dp2->di_gen + 1;
@@ -141,7 +132,7 @@ main(int argc, char *argv[])
} else {
/* get the inode within the block. */
dp1 = &(((struct ufs1_dinode *)v)
- [ino_to_fsbo(sbp, inonum)]);
+ [ino_to_fsbo(fs, inonum)]);
/* clear the inode, and bump the generation count. */
generation = dp1->di_gen + 1;
@@ -151,9 +142,9 @@ main(int argc, char *argv[])
/* backup and write the block */
if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
- err(1, "%s", fs);
+ err(1, "%s", fsname);
if (write(fd, ibuf, bsize) != bsize)
- err(1, "%s", fs);
+ err(1, "%s", fsname);
(void)fsync(fd);
}
(void)close(fd);