274e1bad7fa9701a67bb0ff637fd7379314e2402
[openafs.git] / src / vfsck / pass4.c
1 /*
2  * Copyright (c) 1980, 1986 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21 RCSID("$Header$");
22
23 #define VICE
24 #include <sys/param.h>
25 #include <sys/time.h>
26 #ifdef  AFS_OSF_ENV
27 #include <sys/vnode.h>
28 #include <sys/mount.h>
29 #include <ufs/inode.h>
30 #include <ufs/fs.h>
31 #define _BSD
32 #define _KERNEL
33 #include <ufs/dir.h>
34 #undef  _KERNEL
35 #undef  _BSD
36 #include <stdio.h>
37 #else   /* AFS_OSF_ENV */
38 #ifdef AFS_VFSINCL_ENV
39 #include <sys/vnode.h>
40 #ifdef    AFS_SUN5_ENV
41 #include <stdio.h>
42 #include <unistd.h>
43 #include <sys/fs/ufs_inode.h>
44 #include <sys/fs/ufs_fs.h>
45 #define _KERNEL
46 #include <sys/fs/ufs_fsdir.h>
47 #undef _KERNEL
48 #include <sys/fs/ufs_mount.h>
49 #else
50 #include <ufs/inode.h>
51 #include <ufs/fs.h>
52 #endif
53 #else /* AFS_VFSINCL_ENV */
54 #include <sys/inode.h>
55 #ifdef  AFS_HPUX_ENV
56 #include <ctype.h>
57 #define LONGFILENAMES   1
58 #include <sys/sysmacros.h>
59 #include <sys/ino.h>
60 #endif
61 #include <sys/fs.h>
62 #endif /* AFS_VFSINCL_ENV */
63 #endif  /* AFS_OSF_ENV */
64 #include <afs/osi_inode.h>
65
66 #include "fsck.h"
67
68 int     pass4check();
69
70 pass4()
71 {
72         register ino_t inumber;
73         register struct zlncnt *zlnp;
74         struct inodesc idesc;
75         int n;
76 #if defined(ACLS) && defined(AFS_HPUX_ENV)
77         register struct dinode *dp;
78 #endif /* ACLS */
79
80
81         bzero((char *)&idesc, sizeof(struct inodesc));
82         idesc.id_type = ADDR;
83         idesc.id_func = pass4check;
84         for (inumber = ROOTINO; inumber <= lastino; inumber++) {
85                 idesc.id_number = inumber;
86 #if defined(ACLS) && defined(AFS_HPUX_ENV)
87                 switch (statemap[inumber] & STATE) {
88 #else /* no ACLS */
89                 switch (statemap[inumber]) {
90 #endif /* ACLS */
91
92                 case FSTATE:
93                 case DFOUND:
94                         n = lncntp[inumber];
95                         if (n)
96                                 adjust(&idesc, (short)n);
97                         else {
98                                 for (zlnp = zlnhead; zlnp; zlnp = zlnp->next)
99                                         if (zlnp->zlncnt == inumber) {
100                                                 zlnp->zlncnt = zlnhead->zlncnt;
101                                                 zlnp = zlnhead;
102                                                 zlnhead = zlnhead->next;
103                                                 free((char *)zlnp);
104                                                 clri(&idesc, "UNREF", 1);
105                                                 break;
106                                         }
107                         }
108                         break;
109
110 #ifdef VICE
111                 case VSTATE:
112                         nViceFiles++;
113                         break;
114 #endif /* VICE */
115
116                 case DSTATE:
117                         clri(&idesc, "UNREF", 1);
118                         break;
119
120                 case DCLEAR:
121                 case FCLEAR:
122                         clri(&idesc, "BAD/DUP", 1);
123                         break;
124
125                 case USTATE:
126                         break;
127
128 #if defined(ACLS) && defined(AFS_HPUX_ENV)
129                 /* 
130                  * UNreferenced continuation inode 
131                  */
132                 case CSTATE:
133                         clri(&idesc, "UNREF", 2);
134                         break;
135
136                 /* 
137                  * referenced continuation inode 
138                  */
139                 case CRSTATE:
140                         if ((dp = ginode(inumber)) == NULL)
141                             break;
142                         if (dp->di_nlink != 1)
143                             if (!qflag) {
144                                 pwarn("BAD LINK COUNT IN CONTINUATION INODE ");
145                                 pwarn("I=%u (%ld should be %ld)",
146                                             inumber, dp->di_nlink, 1);
147                                 if (preen)
148 #ifdef VICE
149                                         vprintf(" (CORRECTED)\n");
150 #else
151                                         printf(" (CORRECTED)\n");
152 #endif /* VICE */
153                                 else {
154                                         if (reply("CORRECT") == 0)
155                                         continue;
156                                 }
157                                 dp->di_nlink = 1;
158                                 inodirty();
159                             }
160                         break;
161 #endif /* ACLS */
162
163                 default:
164                         errexit("BAD STATE %d FOR INODE I=%d",
165                             statemap[inumber], inumber);
166                 }
167         }
168 }
169
170 pass4check(idesc)
171         register struct inodesc *idesc;
172 {
173         register struct dups *dlp;
174         int nfrags, res = KEEPON;
175         daddr_t blkno = idesc->id_blkno;
176
177         for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
178                 if (chkrange(blkno, 1)) {
179                         res = SKIP;
180                 } else if (testbmap(blkno)) {
181                         for (dlp = duplist; dlp; dlp = dlp->next) {
182                                 if (dlp->dup != blkno)
183                                         continue;
184                                 dlp->dup = duplist->dup;
185                                 dlp = duplist;
186                                 duplist = duplist->next;
187                                 free((char *)dlp);
188                                 break;
189                         }
190                         if (dlp == 0) {
191                                 clrbmap(blkno);
192                                 n_blks--;
193                         }
194                 }
195         }
196         return (res);
197 }