1/*	$OpenBSD: pass1b.c,v 1.22 2020/06/20 07:49:04 otto Exp $	*/
2/*	$NetBSD: pass1b.c,v 1.10 1996/09/23 16:18:37 christos Exp $	*/
3
4/*
5 * Copyright (c) 1980, 1986, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/time.h>
34#include <ufs/ufs/dinode.h>
35#include <ufs/ffs/fs.h>
36
37#include <stdio.h>
38#include <string.h>
39#include "fsck.h"
40#include "extern.h"
41
42static int	pass1bcheck(struct inodesc *);
43static struct dups *duphead;
44
45static ino_t info_inumber;
46
47static int
48pass1b_info(char *buf, size_t buflen)
49{
50	return (snprintf(buf, buflen, "phase 1b, inode %llu/%llu",
51	    (unsigned long long)info_inumber,
52	    (unsigned long long)sblock.fs_ipg * sblock.fs_ncg) > 0);
53}
54
55void
56pass1b(void)
57{
58	u_int c, i;
59	union dinode *dp;
60	struct inodesc idesc;
61	ino_t inumber;
62
63	memset(&idesc, 0, sizeof(struct inodesc));
64	idesc.id_type = ADDR;
65	idesc.id_func = pass1bcheck;
66	duphead = duplist;
67	inumber = 0;
68	info_fn = pass1b_info;
69	for (c = 0; c < sblock.fs_ncg; c++) {
70		for (i = 0; i < sblock.fs_ipg; i++, inumber++) {
71			info_inumber = inumber;
72			if (inumber < ROOTINO)
73				continue;
74			dp = ginode(inumber);
75			if (dp == NULL)
76				continue;
77			idesc.id_number = inumber;
78			if (GET_ISTATE(inumber) != USTATE &&
79			    (ckinode(dp, &idesc) & STOP))
80				return;
81		}
82	}
83	info_fn = NULL;
84}
85
86static int
87pass1bcheck(struct inodesc *idesc)
88{
89	struct dups *dlp;
90	int nfrags, res = KEEPON;
91	daddr_t blkno = idesc->id_blkno;
92
93	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
94		if (chkrange(blkno, 1))
95			res = SKIP;
96		for (dlp = duphead; dlp; dlp = dlp->next) {
97			if (dlp->dup == blkno) {
98				blkerror(idesc->id_number, "DUP", blkno);
99				dlp->dup = duphead->dup;
100				duphead->dup = blkno;
101				duphead = duphead->next;
102			}
103			if (dlp == muldup)
104				break;
105		}
106		if (muldup == 0 || duphead == muldup->next)
107			return (STOP);
108	}
109	return (res);
110}
111