In preparation for adding inode check-hashes, convert the clri(8)

program to use the libufs library interface. No functional change
(as for now the libufs library does not do inode check-hashes).

Reviewed by:  kib
Sponsored by: Netflix
This commit is contained in:
Kirk McKusick 2018-11-01 03:38:57 +00:00
parent 6bc6a54280
commit a3d27cad4d
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=339983

View File

@ -62,6 +62,11 @@ __FBSDID("$FreeBSD$");
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
union dinodep {
struct ufs1_dinode *dp1;
struct ufs2_dinode *dp2;
};
static void static void
usage(void) usage(void)
{ {
@ -72,81 +77,51 @@ usage(void)
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
struct fs *fs; union dinodep dp;
struct ufs1_dinode *dp1; struct uufsd disk;
struct ufs2_dinode *dp2; long generation;
char *ibuf[MAXBSIZE]; int inonum, exitval;
long generation, bsize;
off_t offset;
int fd, ret, inonum;
char *fsname; char *fsname;
void *v = ibuf;
if (argc < 3) if (argc < 3)
usage(); usage();
fsname = *++argv;
/* get the superblock. */ /* get the superblock. */
if ((fd = open(fsname, O_RDWR, 0)) < 0) fsname = *++argv;
err(1, "%s", fsname); if (ufs_disk_fillout(&disk, fsname) == -1) {
if ((ret = sbget(fd, &fs, -1)) != 0) { printf("loading superblock: %s\n", disk.d_error);
switch (ret) { exit (1);
case ENOENT:
warn("Cannot find file system superblock");
return (1);
default:
warn("Unable to read file system superblock");
return (1);
} }
}
bsize = fs->fs_bsize;
/* remaining arguments are inode numbers. */ /* remaining arguments are inode numbers. */
exitval = 0;
while (*++argv) { while (*++argv) {
/* get the inode number. */ /* get the inode number. */
if ((inonum = atoi(*argv)) <= 0) if ((inonum = atoi(*argv)) < UFS_ROOTINO) {
errx(1, "%s is not a valid inode number", *argv); printf("%s is not a valid inode number", *argv);
exitval = 1;
continue;
}
(void)printf("clearing %d\n", inonum); (void)printf("clearing %d\n", inonum);
/* read in the appropriate block. */ if (getino(&disk, (void **)&dp, inonum, NULL) == -1) {
offset = ino_to_fsba(fs, inonum); /* inode to fs blk */ printf("getino: %s\n", disk.d_error);
offset = fsbtodb(fs, offset); /* fs blk disk blk */ exitval = 1;
offset *= DEV_BSIZE; /* disk blk to bytes */ continue;
}
/* seek and read the block */
if (lseek(fd, offset, SEEK_SET) < 0)
err(1, "%s", fsname);
if (read(fd, ibuf, bsize) != bsize)
err(1, "%s", fsname);
if (fs->fs_magic == FS_UFS2_MAGIC) {
/* get the inode within the block. */
dp2 = &(((struct ufs2_dinode *)v)
[ino_to_fsbo(fs, inonum)]);
/* clear the inode, and bump the generation count. */ /* clear the inode, and bump the generation count. */
generation = dp2->di_gen + 1; if (disk.d_fs.fs_magic == FS_UFS1_MAGIC) {
memset(dp2, 0, sizeof(*dp2)); generation = dp.dp1->di_gen + 1;
dp2->di_gen = generation; memset(dp.dp1, 0, sizeof(*dp.dp1));
dp.dp1->di_gen = generation;
} else { } else {
/* get the inode within the block. */ generation = dp.dp2->di_gen + 1;
dp1 = &(((struct ufs1_dinode *)v) memset(dp.dp2, 0, sizeof(*dp.dp2));
[ino_to_fsbo(fs, inonum)]); dp.dp2->di_gen = generation;
/* clear the inode, and bump the generation count. */
generation = dp1->di_gen + 1;
memset(dp1, 0, sizeof(*dp1));
dp1->di_gen = generation;
} }
putino(&disk);
/* backup and write the block */ (void)fsync(disk.d_fd);
if (lseek(fd, (off_t)-bsize, SEEK_CUR) < 0)
err(1, "%s", fsname);
if (write(fd, ibuf, bsize) != bsize)
err(1, "%s", fsname);
(void)fsync(fd);
} }
(void)close(fd); (void)ufs_disk_close(&disk);
exit(0); exit(exitval);
} }